edje is now also prefix aware.

SVN revision: 23821
This commit is contained in:
Carsten Haitzler 2006-07-10 18:55:23 +00:00
parent 3bc78e5f07
commit f41aec9158
7 changed files with 489 additions and 17 deletions

View File

@ -30,7 +30,9 @@ edje_thumb
edje_SOURCES = \
edje_main.c \
edje_main.h
edje_main.h \
edje_prefix.c \
edje_prefix.h
edje_LDADD = \
$(top_builddir)/src/lib/libedje.la
@ -45,7 +47,9 @@ edje_cc_out.c \
edje_cc_parse.c \
edje_cc_mem.c \
edje_cc_handlers.c \
edje_cc_sources.c
edje_cc_sources.c \
edje_prefix.c \
edje_prefix.h
edje_cc_LDADD = \
$(top_builddir)/src/lib/libedje.la

View File

@ -119,6 +119,8 @@ main(int argc, char **argv)
exit(-1);
}
e_prefix_determine(argv[0]);
/* check whether file_in exists */
#ifdef HAVE_REALPATH
if (!realpath(file_in, rpath) || stat(rpath, &st) || !S_ISREG(st.st_mode))

View File

@ -623,8 +623,9 @@ data_write(void)
if (fd >= 0)
{
snprintf(buf, sizeof(buf),
EMBRYO_PREFIX"/bin/embryo_cc -i %s -o %s %s",
DAT"include", tmpo, tmpn);
"embryo_cc -i %s -o %s %s",
"%s/include", tmpo, tmpn,
e_prefix_data_get());
ret = system(buf);
/* accept warnings in the embryo code */
if (ret < 0 || ret > 1)

View File

@ -68,7 +68,9 @@ static int
main_start(int argc, char **argv)
{
int mode = 0;
char buf[4096];
e_prefix_determine(argv[0]);
start_time = ecore_time_get();
if (!ecore_init()) return -1;
ecore_app_args_set(argc, (const char **)argv);
@ -134,8 +136,8 @@ main_start(int argc, char **argv)
evas = ecore_evas_get(ecore_evas);
evas_image_cache_set(evas, 2 * 1024 * 1024);
evas_font_cache_set(evas, 512 * 1024);
evas_font_path_append(evas, DAT"data/test/fonts");
evas_font_path_append(evas, "fonts");
snprintf(buf, sizeof(buf), "%s/data/test/fonts", e_prefix_data_get());
evas_font_path_append(evas, buf);
return 1;
}
@ -185,13 +187,15 @@ void
bg_setup(void)
{
Evas_Object *o;
char buf[4096];
o = evas_object_image_add(evas);
evas_object_move(o, 0, 0);
evas_object_resize(o, startw, starth);
evas_object_layer_set(o, -999);
evas_object_color_set(o, 255, 255, 255, 255);
evas_object_image_file_set(o, DAT"data/test/images/bg.png", NULL);
snprintf(buf, sizeof(buf), "%s/data/test/images/bg.png", e_prefix_data_get());
evas_object_image_file_set(o, buf, NULL);
evas_object_image_fill_set(o, 0, 0, 128, 128);
evas_object_pass_events_set(o, 1);
evas_object_show(o);
@ -204,7 +208,8 @@ bg_setup(void)
evas_object_resize(o, startw, starth);
evas_object_layer_set(o, -999);
evas_object_color_set(o, 255, 255, 255, 255);
evas_object_image_file_set(o, DAT"data/test/images/shadow.png", NULL);
snprintf(buf, sizeof(buf), "%s/data/test/images/shadow.png", e_prefix_data_get());
evas_object_image_file_set(o, buf, NULL);
evas_object_image_smooth_scale_set(o, 0);
evas_object_image_fill_set(o, 0, 0, startw, starth);
evas_object_pass_events_set(o, 1);
@ -469,18 +474,22 @@ static void
list_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
Collection *co;
char buf[4096];
co = data;
evas_object_image_file_set(co->bg, DAT"data/test/images/item_selected.png", NULL);
snprintf(buf, sizeof(buf), "%s/data/test/images/item_selected.png", e_prefix_data_get());
evas_object_image_file_set(co->bg, buf, NULL);
}
static void
list_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
Collection *co;
char buf[4096];
co = data;
evas_object_image_file_set(co->bg, DAT"data/test/images/item_normal.png", NULL);
snprintf(buf, sizeof(buf), "%s/data/test/images/item_normal.png", e_prefix_data_get());
evas_object_image_file_set(co->bg, buf, NULL);
test_setup(co->file, co->part);
evas_object_color_set(co->bg, 255, 255, 255, 128);
evas_object_color_set(co->text, 0, 0, 0, 128);
@ -566,6 +575,7 @@ test_list(char *file)
Evas_Coord maxw = 128;
Collection *co_head;
Evas_List *collections = NULL;
char buf[4096];
entries = edje_file_collection_list(file);
// printf("%s\n", edje_file_data_get(file, "My Data");
@ -585,7 +595,8 @@ test_list(char *file)
o = evas_object_image_add(evas);
evas_object_layer_set(o, 10);
evas_object_image_border_set(o, 10, 10, 10, 10);
evas_object_image_file_set(o, DAT"data/test/images/item_title.png", NULL);
snprintf(buf, sizeof(buf), "%s/data/test/images/item_title.png", e_prefix_data_get());
evas_object_image_file_set(o, buf, NULL);
evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, list_head_down_cb, co);
evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_UP, list_head_up_cb, co);
evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_MOVE, list_head_move_cb, co);
@ -623,7 +634,8 @@ test_list(char *file)
o = evas_object_image_add(evas);
evas_object_layer_set(o, 10);
evas_object_image_border_set(o, 10, 10, 10, 10);
evas_object_image_file_set(o, DAT"data/test/images/item_normal.png", NULL);
snprintf(buf, sizeof(buf), "%s/data/test/images/item_normal.png", e_prefix_data_get());
evas_object_image_file_set(o, buf, NULL);
evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, list_down_cb, co);
evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_UP, list_up_cb, co);
co->bg = o;
@ -698,7 +710,7 @@ test_setup(char *file, char *name)
{
Evas_Object *o;
Demo_Edje *de;
char buf[1024];
char buf[4096];
Evas_Coord tw, th, w, h;
Evas_Coord xx, yy, ww, hh;
@ -720,7 +732,8 @@ test_setup(char *file, char *name)
edjes = evas_list_append(edjes, de);
o = evas_object_image_add(evas);
evas_object_image_file_set(o, DAT"data/test/images/border.png", NULL);
snprintf(buf, sizeof(buf), "%s/data/test/images/border.png", e_prefix_data_get());
evas_object_image_file_set(o, buf, NULL);
evas_object_image_smooth_scale_set(o, 0);
evas_object_color_set(o, 255, 255, 255, 255);
evas_object_image_border_set(o, 26, 26, 26, 26);

View File

@ -31,7 +31,6 @@
#endif
#include "edje_private.h"
#define DAT PACKAGE_DATA_DIR"/"
#include "edje_prefix.h"
#endif

View File

@ -0,0 +1,442 @@
/*
* vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/param.h>
#include <dlfcn.h>
#include <math.h>
#include <fnmatch.h>
#include <limits.h>
#include <ctype.h>
#include <time.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <glob.h>
#include "edje_prefix.h"
/* local subsystem functions */
static int _e_prefix_share_hunt(void);
static int _e_prefix_fallbacks(void);
static int _e_prefix_try_proc(void);
static int _e_prefix_try_argv(char *argv0);
/* local subsystem globals */
static char *_exe_path = NULL;
static char *_prefix_path = NULL;
static char *_prefix_path_bin = NULL;
static char *_prefix_path_data = NULL;
static char *_prefix_path_lib = NULL;
#define E_FREE(p) { if (p) {free(p); p = NULL;} }
/*#define PREFIX_CACHE_FILE 1*/
#define SHARE_D "share/edje"
#define MAGIC_FILE "include/edje.inc"
#define MAGIC_DAT SHARE_D"/"MAGIC_FILE
/* externally accessible functions */
int
e_prefix_determine(char *argv0)
{
char *p, buf[4096];
struct stat st;
e_prefix_shutdown();
/* if user provides E_PREFIX - then use that or also more specific sub
* dirs for bin, lib, data and locale */
if (getenv("E_PREFIX"))
{
_prefix_path = strdup(getenv("E_PREFIX"));
if (getenv("E_BIN_DIR"))
_prefix_path_bin = strdup(getenv("E_BIN_DIR"));
else
{
snprintf(buf, sizeof(buf), "%s/bin", _prefix_path);
_prefix_path_bin = strdup(buf);
}
if (getenv("E_LIB_DIR"))
_prefix_path_bin = strdup(getenv("E_LIB_DIR"));
else
{
snprintf(buf, sizeof(buf), "%s/lib", _prefix_path);
_prefix_path_lib = strdup(buf);
}
if (getenv("E_DATA_DIR"))
_prefix_path_data = strdup(getenv("E_DATA_DIR"));
else
{
snprintf(buf, sizeof(buf), "%s/"SHARE_D, _prefix_path);
_prefix_path_data = strdup(buf);
}
return 1;
}
/* no env var - examine process and possible argv0 */
if (!_e_prefix_try_proc())
{
if (!_e_prefix_try_argv(argv0))
{
_e_prefix_fallbacks();
return 0;
}
}
/* _exe_path is now a full absolute path TO this exe - figure out rest */
/* if
* exe = /blah/whatever/bin/exe
* then
* prefix = /blah/whatever
* bin_dir = /blah/whatever/bin
* data_dir = /blah/whatever/share/enlightenment
* lib_dir = /blah/whatever/lib
*/
p = strrchr(_exe_path, '/');
if (p)
{
p--;
while (p >= _exe_path)
{
if (*p == '/')
{
_prefix_path = malloc(p - _exe_path + 1);
if (_prefix_path)
{
strncpy(_prefix_path, _exe_path, p - _exe_path);
_prefix_path[p - _exe_path] = 0;
/* bin and lib always together */
snprintf(buf, sizeof(buf), "%s/bin", _prefix_path);
_prefix_path_bin = strdup(buf);
snprintf(buf, sizeof(buf), "%s/lib", _prefix_path);
_prefix_path_lib = strdup(buf);
/* check if AUTHORS file is there - then our guess is right */
snprintf(buf, sizeof(buf), "%s/"MAGIC_DAT, _prefix_path);
if (stat(buf, &st) == 0)
{
snprintf(buf, sizeof(buf), "%s/"SHARE_D, _prefix_path);
_prefix_path_data = strdup(buf);
}
/* AUTHORS file not there. time to start hunting! */
else
{
if (_e_prefix_share_hunt())
{
return 1;
}
else
{
e_prefix_fallback();
return 0;
}
}
return 1;
}
else
{
e_prefix_fallback();
return 0;
}
}
p--;
}
}
e_prefix_fallback();
return 0;
}
void
e_prefix_shutdown(void)
{
E_FREE(_exe_path);
E_FREE(_prefix_path);
E_FREE(_prefix_path_bin);
E_FREE(_prefix_path_data);
E_FREE(_prefix_path_lib);
}
void
e_prefix_fallback(void)
{
e_prefix_shutdown();
_e_prefix_fallbacks();
}
const char *
e_prefix_get(void)
{
return _prefix_path;
}
const char *
e_prefix_bin_get(void)
{
return _prefix_path_bin;
}
const char *
e_prefix_data_get(void)
{
return _prefix_path_data;
}
const char *
e_prefix_lib_get(void)
{
return _prefix_path_lib;
}
/* local subsystem functions */
static int
_e_prefix_share_hunt(void)
{
char buf[4096], buf2[4096], *p;
FILE *f;
struct stat st;
/* sometimes this isnt the case - so we need to do a more exhaustive search
* through more parent and subdirs. hre is an example i have seen:
*
* /blah/whatever/exec/bin/exe
* ->
* /blah/whatever/exec/bin
* /blah/whatever/common/share/enlightenment
* /blah/whatever/exec/lib
*/
/* this is pure black magic to try and find data shares */
/* 2. cache file doesn't exist or is invalid - we need to search - this is
* where the real black magic begins */
/* BLACK MAGIC 1:
* /blah/whatever/dir1/bin
* /blah/whatever/dir2/share/enlightenment
*/
if (!_prefix_path_data)
{
DIR *dirp;
struct dirent *dp;
snprintf(buf, sizeof(buf), "%s", _prefix_path);
p = strrchr(buf, '/');
if (p) *p = 0;
dirp = opendir(buf);
if (dirp)
{
char *file;
while ((dp = readdir(dirp)))
{
if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, "..")))
{
file = dp->d_name;
snprintf(buf2, sizeof(buf2), "%s/%s/"MAGIC_DAT, buf, file);
if (stat(buf2, &st) == 0)
{
snprintf(buf2, sizeof(buf2), "%s/%s/"SHARE_D, buf, file);
_prefix_path_data = strdup(buf2);
break;
}
}
}
closedir(dirp);
}
}
/* BLACK MAGIC 2:
* /blah/whatever/dir1/bin
* /blah/whatever/share/enlightenment
*/
if (!_prefix_path_data)
{
snprintf(buf, sizeof(buf), "%s", _prefix_path);
p = strrchr(buf, '/');
if (p) *p = 0;
snprintf(buf2, sizeof(buf2), "%s/"MAGIC_DAT, buf);
if (stat(buf, &st) == 0)
{
snprintf(buf2, sizeof(buf2), "%s/"SHARE_D, buf);
_prefix_path_data = strdup(buf2);
}
}
/* add more black magic as required as we discover weridnesss - remember
* this is to save users having to set environment variables to tell
* e where it lives, so e auto-adapts. so these code snippets are just
* logic to figure that out for the user
*/
/* done. we found it - write cache file */
if (_prefix_path_data)
{
return 1;
}
/* fail. everything failed */
return 0;
}
static int
_e_prefix_fallbacks(void)
{
char *p;
_prefix_path = strdup(PACKAGE_BIN_DIR);
p = strrchr(_prefix_path, '/');
if (p) *p = 0;
_prefix_path_bin = strdup(PACKAGE_BIN_DIR);
_prefix_path_data = strdup(PACKAGE_DATA_DIR);
_prefix_path_lib = strdup(PACKAGE_LIB_DIR);
printf("WARNING: Edje could not determine its installed prefix\n"
" and is falling back on the compiled in default:\n"
" %s\n",
" You might like to try setting the following environment variables:\n"
" E_PREFIX - points to the base prefix of install\n"
" E_BIN_DIR - optional in addition to E_PREFIX to provide\n"
" a more specific binary directory\n"
" E_LIB_DIR - optional in addition to E_PREFIX to provide\n"
" a more specific library dir\n"
" E_DATA_DIR - optional in addition to E_PREFIX to provide\n"
" a more specific location for shared data\n"
,
_prefix_path);
return 1;
}
static int
_e_prefix_try_proc(void)
{
FILE *f;
char buf[4096];
void *func = NULL;
func = (void *)_e_prefix_try_proc;
f = fopen("/proc/self/maps", "r");
if (!f) return 0;
while (fgets(buf, sizeof(buf), f))
{
int len;
char *p, mode[5] = "";
unsigned long ptr1 = 0, ptr2 = 0;
len = strlen(buf);
if (buf[len - 1] == '\n')
{
buf[len - 1] = 0;
len--;
}
if (sscanf(buf, "%lx-%lx %4s", &ptr1, &ptr2, mode) == 3)
{
if (!strcmp(mode, "r-xp"))
{
if (((void *)ptr1 <= func) && (func < (void *)ptr2))
{
p = strchr(buf, '/');
if (p)
{
if (len > 10)
{
if (!strcmp(buf + len - 10, " (deleted)"))
buf[len - 10] = 0;
}
_exe_path = strdup(p);
fclose(f);
return 1;
}
else
break;
}
}
}
}
fclose(f);
return 0;
}
static int
_e_prefix_try_argv(char *argv0)
{
char *path, *p, *cp, *s;
int len, lenexe;
char buf[4096], buf2[4096], buf3[4096];
/* 1. is argv0 abs path? */
if (argv0[0] == '/')
{
_exe_path = strdup(argv0);
if (access(_exe_path, X_OK) == 0) return 1;
free(_exe_path);
_exe_path = NULL;
return 0;
}
/* 2. relative path */
if (strchr(argv0, '/'))
{
if (getcwd(buf3, sizeof(buf3)))
{
snprintf(buf2, sizeof(buf2), "%s/%s", buf3, argv0);
if (realpath(buf2, buf))
{
_exe_path = strdup(buf);
if (access(_exe_path, X_OK) == 0) return 1;
free(_exe_path);
_exe_path = NULL;
}
}
}
/* 3. argv0 no path - look in PATH */
path = getenv("PATH");
if (!path) return 0;
p = path;
cp = p;
lenexe = strlen(argv0);
while ((p = strchr(cp, ':')))
{
len = p - cp;
s = malloc(len + 1 + lenexe + 1);
if (s)
{
strncpy(s, cp, len);
s[len] = '/';
strcpy(s + len + 1, argv0);
if (realpath(s, buf))
{
if (access(buf, X_OK) == 0)
{
_exe_path = strdup(buf);
free(s);
return 1;
}
}
free(s);
}
cp = p + 1;
}
len = strlen(cp);
s = malloc(len + 1 + lenexe + 1);
if (s)
{
strncpy(s, cp, len);
s[len] = '/';
strcpy(s + len + 1, argv0);
if (realpath(s, buf))
{
if (access(buf, X_OK) == 0)
{
_exe_path = strdup(buf);
free(s);
return 1;
}
}
free(s);
}
/* 4. big problems. arg[0] != executable - weird execution */
return 0;
}

View File

@ -0,0 +1,11 @@
/*
* vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
*/
int e_prefix_determine(char *argv0);
void e_prefix_shutdown(void);
void e_prefix_fallback(void);
const char *e_prefix_get(void);
const char *e_prefix_bin_get(void);
const char *e_prefix_data_get(void);
const char *e_prefix_lib_get(void);