2 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
13 #include <sys/types.h>
16 #include "Ecore_Config.h"
18 #include "ecore_config_private.h"
19 typedef struct __Ecore_Config_Arg_Callback _Ecore_Config_Arg_Callback;
20 struct __Ecore_Config_Arg_Callback
26 void (*func)(char *val, void *data);
27 Ecore_Config_Type type;
28 _Ecore_Config_Arg_Callback *next;
31 char *__ecore_config_app_description;
32 _Ecore_Config_Arg_Callback *_ecore_config_arg_callbacks;
34 /* shorthand prop setup code to make client apps a little smaller ;) */
37 * Creates a new property, if it does not already exist, and sets its
38 * attributes to those given.
40 * The type of the property is guessed from the key and the value
43 * @param key The property key.
44 * @param val Pointer to default value of key.
45 * @param short_opt Short option used to set the property from command
47 * @param long_opt Long option used to set the property from command line.
48 * @param desc String description of property.
49 * @return @c ECORE_CONFIG_ERR_SUCC on success.
50 * @ingroup Ecore_Config_Create_Group
53 ecore_config_create(const char *key, void *val, char short_opt, char *long_opt,
56 int type = ecore_config_type_guess(key, val);
58 return ecore_config_typed_create(key, val, type, short_opt, long_opt, desc);
62 * Creates a new property, if it does not already exist, and sets its
63 * attributes to those given.
64 * @param key The property key.
65 * @param val Pointer to default value of key.
66 * @param type Type of the property.
67 * @param short_opt Short option used to set the property from
69 * @param long_opt Long option used to set the property from command line.
70 * @param desc String description of property.
71 * @return @c ECORE_CONFIG_ERR_SUCC on success.
72 * @ingroup Ecore_Config_Create_Group
75 ecore_config_typed_create(const char *key, void *val, int type, char short_opt,
76 char *long_opt, char *desc)
81 ecore_config_typed_default(key, val, type)) != ECORE_CONFIG_ERR_SUCC)
84 ecore_config_short_opt_set(key, short_opt)) != ECORE_CONFIG_ERR_SUCC)
87 ecore_config_long_opt_set(key, long_opt)) != ECORE_CONFIG_ERR_SUCC)
89 ret = ecore_config_describe(key, desc);
94 * Creates a new boolean property, if it does not already exist, and sets its
95 * attributes to those given.
96 * @param key The property key.
97 * @param val Default boolean value of key.
98 * @param short_opt Short option used to set the property from command
100 * @param long_opt Long option used to set the property from command line.
101 * @param desc String description of property.
102 * @return @c ECORE_CONFIG_ERR_SUCC on success.
103 * @ingroup Ecore_Config_Create_Group
106 ecore_config_boolean_create(const char *key, int val, char short_opt,
107 char *long_opt, char *desc)
110 ecore_config_typed_create(key, (void *)&val, ECORE_CONFIG_BLN, short_opt, long_opt,
115 * Creates a new integer property, if it does not already exist, and sets its
116 * attributes to those given.
117 * @param key The property key.
118 * @param val Default integer value of key.
119 * @param short_opt Short option used to set the property from command
121 * @param long_opt Long option used to set the property from command line.
122 * @param desc String description of property.
123 * @return @c ECORE_CONFIG_ERR_SUCC on success.
124 * @ingroup Ecore_Config_Create_Group
127 ecore_config_int_create(const char *key, int val, char short_opt,
128 char *long_opt, char *desc)
131 ecore_config_typed_create(key, (void *)&val, ECORE_CONFIG_INT, short_opt, long_opt,
136 * Creates a new integer property, if it does not already exist, and sets its
137 * attributes to those given.
138 * @param key The property key.
139 * @param val Default integer value of key.
140 * @param low Lowest valid integer value for the property.
141 * @param high Highest valid integer value for the property.
142 * @param step Increment value for the property.
143 * @param short_opt Short option used to set the property from command
145 * @param long_opt Long option used to set the property from command line.
146 * @param desc String description of property.
147 * @return @c ECORE_CONFIG_ERR_SUCC on success.
148 * @ingroup Ecore_Config_Create_Group
151 ecore_config_int_create_bound(const char *key, int val, int low, int high,
152 int step, char short_opt, char *long_opt,
155 Ecore_Config_Prop *e;
159 ecore_config_typed_create(key, (void *)&val, ECORE_CONFIG_INT, short_opt, long_opt,
161 if (ret != ECORE_CONFIG_ERR_SUCC)
163 e = ecore_config_get(key);
167 e->flags |= ECORE_CONFIG_FLAG_BOUNDS;
170 ecore_config_bound(e);
176 * Creates a new string property, if it does not already exist, and sets its
177 * attributes to those given.
178 * @param key The property key.
179 * @param val Default value of key.
180 * @param short_opt Short option used to set the property from command
182 * @param long_opt Long option used to set the property from command line.
183 * @param desc String description of property.
184 * @return @c ECORE_CONFIG_ERR_SUCC on success.
185 * @ingroup Ecore_Config_Create_Group
188 ecore_config_string_create(const char *key, char *val, char short_opt,
189 char *long_opt, char *desc)
192 ecore_config_typed_create(key, (void *)val, ECORE_CONFIG_STR, short_opt, long_opt,
197 * Creates a new float property, if it does not already exist, and sets its
198 * attributes to those given.
199 * @param key The property key.
200 * @param val Default float value of key.
201 * @param short_opt Short option used to set the property from command
203 * @param long_opt Long option used to set the property from command line.
204 * @param desc String description of property.
205 * @return @c ECORE_CONFIG_ERR_SUCC on success.
206 * @ingroup Ecore_Config_Create_Group
209 ecore_config_float_create(const char *key, float val, char short_opt,
210 char *long_opt, char *desc)
213 ecore_config_typed_create(key, (void *)&val, ECORE_CONFIG_FLT, short_opt, long_opt,
218 * Creates a new float property, if it does not already exist, and sets its
219 * attributes to those given.
220 * @param key The property key.
221 * @param val Default float value of key.
222 * @param low Lowest valid float value for the property.
223 * @param high Highest valid float value for the property.
224 * @param step Increment value for the property.
225 * @param short_opt Short option used to set the property from command
227 * @param long_opt Long option used to set the property from command line.
228 * @param desc String description of property.
229 * @return @c ECORE_CONFIG_ERR_SUCC on success.
230 * @ingroup Ecore_Config_Create_Group
233 ecore_config_float_create_bound(const char *key, float val, float low,
234 float high, float step, char short_opt,
235 char *long_opt, char *desc)
237 Ecore_Config_Prop *e;
241 ecore_config_typed_create(key, (void *)&val, ECORE_CONFIG_FLT, short_opt, long_opt,
243 e = ecore_config_get(key);
246 e->step = (int)(step * ECORE_CONFIG_FLOAT_PRECISION);
247 e->flags |= ECORE_CONFIG_FLAG_BOUNDS;
248 e->lo = (int)(low * ECORE_CONFIG_FLOAT_PRECISION);
249 e->hi = (int)(high * ECORE_CONFIG_FLOAT_PRECISION);
250 ecore_config_bound(e);
256 * Creates a new color property, if it does not already exist, and sets its
257 * attributes to those given.
258 * @param key The property key.
259 * @param val Default color value of key, as a hexadecimal string.
260 * @param short_opt Short option used to set the property from command
262 * @param long_opt Long option used to set the property from command line.
263 * @param desc String description of property.
264 * @return @c ECORE_CONFIG_ERR_SUCC on success.
265 * @ingroup Ecore_Config_Create_Group
268 ecore_config_argb_create(const char *key, char *val, char short_opt,
269 char *long_opt, char *desc)
272 ecore_config_typed_create(key, (void *)val, ECORE_CONFIG_RGB, short_opt, long_opt,
277 * Creates a new theme property, if it does not already exist, and sets its
278 * attributes to those given.
279 * @param key The property key.
280 * @param val Default theme name for the property.
281 * @param short_opt Short option used to set the property from command
283 * @param long_opt Long option used to set the property from command line.
284 * @param desc String description of property.
285 * @return @c ECORE_CONFIG_ERR_SUCC on success.
286 * @ingroup Ecore_Config_Create_Group
289 ecore_config_theme_create(const char *key, char *val, char short_opt,
290 char *long_opt, char *desc)
293 ecore_config_typed_create(key, (void *)val, ECORE_CONFIG_THM, short_opt, long_opt,
297 /* this should only be built if evas is present */
300 * Calls evas_font_path_append on @p evas for each of the font names stored
301 * in the property "/e/font/path".
302 * @param evas Evas object to append the font names to.
303 * @return @c ECORE_CONFIG_ERR_SUCC on success. @c ECORE_CONFIG_ERR_NODATA
304 * is returned if the property has not been set.
307 ecore_config_evas_font_path_apply(Evas * evas)
309 char *font_path, *font_path_tmp, *ptr, *end;
311 font_path = ecore_config_string_get("/e/font/path");
314 return ECORE_CONFIG_ERR_NODATA;
316 end = font_path + strlen(font_path);
317 font_path_tmp = font_path;
318 while (ptr && ptr < end)
320 while (*ptr != '|' && ptr < end)
325 evas_font_path_append(evas, font_path_tmp);
332 return ECORE_CONFIG_ERR_SUCC;
336 * Retrieves the default theme search path.
338 * @return The default theme search path.
341 ecore_config_theme_default_path_get(void)
346 home = getenv("HOME");
347 len = strlen(PACKAGE_DATA_DIR "/../") + strlen(__ecore_config_app_name) +
348 strlen("/themes/") + 1;
350 len += strlen(home) + strlen("/.e/apps/") +
351 strlen(__ecore_config_app_name) +
352 strlen("/themes/|"); /* no \0, as that is above */
354 if (!(path = malloc(len)))
361 strcat(path, "/.e/apps/");
362 strcat(path, __ecore_config_app_name);
363 strcat(path, "/themes/|");
365 strcat(path, PACKAGE_DATA_DIR "/../");
366 strcat(path, __ecore_config_app_name);
367 strcat(path, "/themes/");
373 * Retrieves the search path used to find themes.
375 * The search path is stored in the property "/e/themes/search_path". If
376 * the property has not been set, the default path used is
377 * "/usr/local/share/<app_name>/themes|~/.e/apps/<app_name>/themes".
378 * See @ref ecore_config_theme_default_path_get for more information about
381 * @return The search path. @c NULL is returned if there is no memory left.
384 ecore_config_theme_search_path_get(void)
387 search_path = ecore_config_string_get("/e/themes/search_path");
389 /* this should no longer be the case, as it is defaulted in init */
392 search_path = ecore_config_theme_default_path_get();
395 ecore_config_string_default("/e/themes/search_path", search_path);
403 * Adds the given path to the search path used to find themes.
405 * If the search path is successfully, the new search path will be saved
406 * into the property "/e/themes/search_path". Therefore, this function
407 * should be called @b after @ref ecore_config_load to allow a user to
408 * override the default search path.
410 * @param path The given
411 * @return @c ECORE_CONFIG_ERR_SUCC on success. @c ECORE_CONFIG_ERR_FAIL
412 * will be returned if @p path already exists in the search path.
413 * @c ECORE_CONFIG_ERR_FAIL is returned if @p path is @c NULL.
416 ecore_config_theme_search_path_append(const char *path)
418 char *search_path, *loc, *new_search_path;
420 Ecore_Config_Prop *prop;
423 return ECORE_CONFIG_ERR_NODATA;
424 search_path = ecore_config_theme_search_path_get();
426 loc = strstr(search_path, path);
428 search_len = strlen(search_path);
430 if (loc == NULL || (loc != search_path && *(loc - 1) != '|') ||
431 (loc != (search_path + search_len - len) && *(loc + len - 1) != '|'))
433 new_search_path = malloc(search_len + len + 2); /* 2 = \0 + | */
434 strcpy(new_search_path, search_path);
435 strncat(new_search_path, "|", 1);
436 strncat(new_search_path, path, len);
438 ecore_config_string_set("/e/themes/search_path", new_search_path);
439 prop = ecore_config_get("/e/themes/search_path");
441 prop->flags &= ~ECORE_CONFIG_FLAG_MODIFIED;
443 free(new_search_path);
445 return ECORE_CONFIG_ERR_SUCC;
447 return ECORE_CONFIG_ERR_FAIL;
451 * Retrieve a theme file's full path.
453 * The search path for theme files is given by @ref
454 * ecore_config_theme_search_path_get .
456 * @param name The name of the theme.
457 * @return A full path to the theme on success. @c NULL will be returned
458 * if @p name is @c NULL or no theme matching the given name could
462 ecore_config_theme_with_path_from_name_get(char *name)
464 char *search_path, *search_path_tmp, *ptr, *end, *file;
468 return NULL; /* no theme specified (nor a default) */
470 search_path = ecore_config_theme_search_path_get();
472 end = search_path + strlen(search_path);
473 search_path_tmp = search_path;
474 while (ptr && ptr < end)
476 while (*ptr != '|' && ptr < end)
481 file = malloc(strlen(search_path_tmp) + strlen(name) + 6);
482 /* 6 = / + .edj + \0 */
484 snprintf(file, strlen(search_path_tmp) + strlen(name) + 6,
485 "%s/%s.edj", search_path_tmp, name);
487 if (stat(file, &st) == 0)
494 search_path_tmp = ptr;
499 return NULL; /* we could not find the theme with that name in search path */
503 * Retrieves the full path to the theme file of the theme stored in the
506 * The search path for themes is given by @ref
507 * ecore_config_theme_search_path_get .
509 * @param key The given property.
510 * @return A full path to the theme on success, or @c NULL on failure.
511 * This function will fail if no key is specified or not theme
512 * matching that given by the property @p key could be found.
515 ecore_config_theme_with_path_get(const char *key)
518 ecore_config_theme_with_path_from_name_get(ecore_config_theme_get(key));
521 static const char *_ecore_config_short_types[] =
522 { " ", "<int> ", "<flt> ", "<str> ", "<rgb> ", "<str> ", "<bool>" };
525 * Prints the property list of the local configuration bundle to output.
528 ecore_config_args_display(void)
530 Ecore_Config_Prop *props;
531 _Ecore_Config_Arg_Callback *callbacks;
533 if (__ecore_config_app_description)
534 ERR("%s\n\n", __ecore_config_app_description);
535 ERR("Supported Options:");
536 ERR(" -h, --help\t Print this text");
537 if (!__ecore_config_bundle_local)
539 props = __ecore_config_bundle_local->data;
542 /* if it is a system prop, or cannot be set on command line hide it */
543 if (props->flags & ECORE_CONFIG_FLAG_SYSTEM || (!props->short_opt && !props->long_opt))
548 INF(" %c%c%c --%s\t%s %s", props->short_opt ? '-' : ' ',
549 props->short_opt ? props->short_opt : ' ',
550 props->short_opt ? ',' : ' ',
551 props->long_opt ? props->long_opt : props->key,
552 _ecore_config_short_types[props->type],
553 props->description ? props->description :
554 "(no description available)");
558 callbacks = _ecore_config_arg_callbacks;
561 INF(" %c%c%c --%s\t%s %s", callbacks->short_opt ? '-' : ' ',
562 callbacks->short_opt ? callbacks->short_opt : ' ',
563 callbacks->short_opt ? ',' : ' ',
564 callbacks->long_opt ? callbacks->long_opt : "",
565 _ecore_config_short_types[callbacks->type],
566 callbacks->description ? callbacks->description :
567 "(no description available)");
569 callbacks = callbacks->next;
574 ecore_config_parse_set(Ecore_Config_Prop * prop, char *arg, char *opt,
580 ERR("Missing expected argument for option --%s", opt);
582 ERR("Missing expected argument for option -%c", opt2);
583 return ECORE_CONFIG_PARSE_EXIT;
587 ecore_config_set(prop->key, arg);
588 prop->flags |= ECORE_CONFIG_FLAG_CMDLN;
590 return ECORE_CONFIG_PARSE_CONTINUE;
594 ecore_config_args_callback_add(char short_opt, char *long_opt, char *desc,
595 void (*func)(char *val, void *data),
596 void *data, Ecore_Config_Type type) {
597 _Ecore_Config_Arg_Callback *new_cb;
599 new_cb = malloc(sizeof(_Ecore_Config_Arg_Callback));
600 new_cb->short_opt = short_opt;
602 new_cb->long_opt = strdup(long_opt);
604 new_cb->description = strdup(desc);
609 new_cb->next = _ecore_config_arg_callbacks;
610 _ecore_config_arg_callbacks = new_cb;
614 ecore_config_args_callback_str_add(char short_opt, char *long_opt, char *desc,
615 void (*func)(char *val, void *data),
617 ecore_config_args_callback_add(short_opt, long_opt, desc, func, data, ECORE_CONFIG_STR);
621 ecore_config_args_callback_noarg_add(char short_opt, char *long_opt, char *desc,
622 void (*func)(char *val, void *data),
624 ecore_config_args_callback_add(short_opt, long_opt, desc, func, data, ECORE_CONFIG_NIL);
628 * Parse the arguments set by @ref ecore_app_args_set and set properties
631 * @return @c ECORE_CONFIG_PARSE_CONTINUE if successful.
632 * @c ECORE_CONFIG_PARSE_EXIT is returned if an unrecognised option
633 * is found. @c ECORE_CONFIG_PARSE_HELP is returned if help was
637 ecore_config_args_parse(void)
641 int nextarg, next_short_opt, found, ret;
643 char *long_opt, short_opt;
644 Ecore_Config_Prop *prop;
645 _Ecore_Config_Arg_Callback *callback;
647 ecore_app_args_get(&argc, &argv);
649 while (nextarg < argc)
655 ERR("Unexpected attribute \"%s\"", arg);
661 short_opt = *(arg + next_short_opt);
663 if (short_opt == '-')
667 if (!strcmp(long_opt, "help"))
669 ecore_config_args_display();
670 return ECORE_CONFIG_PARSE_HELP;
674 prop = __ecore_config_bundle_local->data;
677 if ((prop->long_opt && !strcmp(long_opt, prop->long_opt))
678 || !strcmp(long_opt, prop->key))
682 ecore_config_parse_set(prop, argv[++nextarg],
685 ECORE_CONFIG_PARSE_CONTINUE)
693 callback = _ecore_config_arg_callbacks;
696 if ((callback->long_opt &&
697 !strcmp(long_opt, callback->long_opt)))
700 if (callback->type == ECORE_CONFIG_NIL)
702 callback->func(NULL, callback->data);
706 if (!argv[++nextarg])
708 ERR("Missing expected argument for option --%s", long_opt);
709 return ECORE_CONFIG_PARSE_EXIT;
711 callback->func(argv[nextarg], callback->data);
715 callback = callback->next;
720 ERR("Unrecognised option \"%s\"", long_opt);
721 ERR("Try using -h or --help for more information.\n");
722 return ECORE_CONFIG_PARSE_EXIT;
729 if (short_opt == 'h')
731 ecore_config_args_display();
732 return ECORE_CONFIG_PARSE_HELP;
737 prop = __ecore_config_bundle_local->data;
740 if (short_opt == prop->short_opt)
744 ecore_config_parse_set(prop,
748 ECORE_CONFIG_PARSE_CONTINUE)
757 callback = _ecore_config_arg_callbacks;
760 if (short_opt == callback->short_opt)
763 if (callback->type == ECORE_CONFIG_NIL)
765 callback->func(NULL, callback->data);
769 if (!argv[++nextarg])
771 ERR("Missing expected argument for option -%c", short_opt);
772 return ECORE_CONFIG_PARSE_EXIT;
774 callback->func(argv[nextarg], callback->data);
778 callback = callback->next;
783 ERR("Unrecognised option '%c'", short_opt);
784 ERR("Try using -h or --help for more information.\n");
785 return ECORE_CONFIG_PARSE_EXIT;
788 short_opt = *(arg + ++next_short_opt);
794 return ECORE_CONFIG_PARSE_CONTINUE;
798 * Sets the description string used by @ref ecore_config_args_display .
799 * @param description Description of application.
802 ecore_config_app_describe(char *description)
804 if (__ecore_config_app_description)
805 free(__ecore_config_app_description);
806 __ecore_config_app_description = strdup(description);