12 #include "Ecore_Config.h"
14 #include "ecore_config_private.h"
15 typedef struct __Ecore_Config_Arg_Callback _Ecore_Config_Arg_Callback;
16 struct __Ecore_Config_Arg_Callback
22 void (*func)(char *val, void *data);
23 Ecore_Config_Type type;
24 _Ecore_Config_Arg_Callback *next;
27 char *__ecore_config_app_description;
28 _Ecore_Config_Arg_Callback *_ecore_config_arg_callbacks;
30 /* shorthand prop setup code to make client apps a little smaller ;) */
33 * Creates a new property, if it does not already exist, and sets its
34 * attributes to those given.
36 * The type of the property is guessed from the key and the value
39 * @param key The property key.
40 * @param val Pointer to default value of key.
41 * @param short_opt Short option used to set the property from command
43 * @param long_opt Long option used to set the property from command line.
44 * @param desc String description of property.
45 * @return @c ECORE_CONFIG_ERR_SUCC on success.
46 * @ingroup Ecore_Config_Create_Group
49 ecore_config_create(const char *key, void *val, char short_opt, char *long_opt,
52 int type = ecore_config_type_guess(key, val);
54 return ecore_config_typed_create(key, val, type, short_opt, long_opt, desc);
58 * Creates a new property, if it does not already exist, and sets its
59 * attributes to those given.
60 * @param key The property key.
61 * @param val Pointer to default value of key.
62 * @param type Type of the property.
63 * @param short_opt Short option used to set the property from
65 * @param long_opt Long option used to set the property from command line.
66 * @param desc String description of property.
67 * @return @c ECORE_CONFIG_ERR_SUCC on success.
68 * @ingroup Ecore_Config_Create_Group
71 ecore_config_typed_create(const char *key, void *val, int type, char short_opt,
72 char *long_opt, char *desc)
77 ecore_config_typed_default(key, val, type)) != ECORE_CONFIG_ERR_SUCC)
80 ecore_config_short_opt_set(key, short_opt)) != ECORE_CONFIG_ERR_SUCC)
83 ecore_config_long_opt_set(key, long_opt)) != ECORE_CONFIG_ERR_SUCC)
85 ret = ecore_config_describe(key, desc);
90 * Creates a new boolean property, if it does not already exist, and sets its
91 * attributes to those given.
92 * @param key The property key.
93 * @param val Default boolean value of key.
94 * @param short_opt Short option used to set the property from command
96 * @param long_opt Long option used to set the property from command line.
97 * @param desc String description of property.
98 * @return @c ECORE_CONFIG_ERR_SUCC on success.
99 * @ingroup Ecore_Config_Create_Group
102 ecore_config_boolean_create(const char *key, int val, char short_opt,
103 char *long_opt, char *desc)
106 ecore_config_typed_create(key, (void *)&val, ECORE_CONFIG_BLN, short_opt, long_opt,
111 * Creates a new integer property, if it does not already exist, and sets its
112 * attributes to those given.
113 * @param key The property key.
114 * @param val Default integer value of key.
115 * @param short_opt Short option used to set the property from command
117 * @param long_opt Long option used to set the property from command line.
118 * @param desc String description of property.
119 * @return @c ECORE_CONFIG_ERR_SUCC on success.
120 * @ingroup Ecore_Config_Create_Group
123 ecore_config_int_create(const char *key, int val, char short_opt,
124 char *long_opt, char *desc)
127 ecore_config_typed_create(key, (void *)&val, ECORE_CONFIG_INT, short_opt, long_opt,
132 * Creates a new integer property, if it does not already exist, and sets its
133 * attributes to those given.
134 * @param key The property key.
135 * @param val Default integer value of key.
136 * @param low Lowest valid integer value for the property.
137 * @param high Highest valid integer value for the property.
138 * @param step Increment value for the property.
139 * @param short_opt Short option used to set the property from command
141 * @param long_opt Long option used to set the property from command line.
142 * @param desc String description of property.
143 * @return @c ECORE_CONFIG_ERR_SUCC on success.
144 * @ingroup Ecore_Config_Create_Group
147 ecore_config_int_create_bound(const char *key, int val, int low, int high,
148 int step, char short_opt, char *long_opt,
151 Ecore_Config_Prop *e;
155 ecore_config_typed_create(key, (void *)&val, ECORE_CONFIG_INT, short_opt, long_opt,
157 if (ret != ECORE_CONFIG_ERR_SUCC)
159 e = ecore_config_get(key);
163 e->flags |= ECORE_CONFIG_FLAG_BOUNDS;
166 ecore_config_bound(e);
172 * Creates a new string property, if it does not already exist, and sets its
173 * attributes to those given.
174 * @param key The property key.
175 * @param val Default value of key.
176 * @param short_opt Short option used to set the property from command
178 * @param long_opt Long option used to set the property from command line.
179 * @param desc String description of property.
180 * @return @c ECORE_CONFIG_ERR_SUCC on success.
181 * @ingroup Ecore_Config_Create_Group
184 ecore_config_string_create(const char *key, char *val, char short_opt,
185 char *long_opt, char *desc)
188 ecore_config_typed_create(key, (void *)val, ECORE_CONFIG_STR, short_opt, long_opt,
193 * Creates a new float property, if it does not already exist, and sets its
194 * attributes to those given.
195 * @param key The property key.
196 * @param val Default float value of key.
197 * @param short_opt Short option used to set the property from command
199 * @param long_opt Long option used to set the property from command line.
200 * @param desc String description of property.
201 * @return @c ECORE_CONFIG_ERR_SUCC on success.
202 * @ingroup Ecore_Config_Create_Group
205 ecore_config_float_create(const char *key, float val, char short_opt,
206 char *long_opt, char *desc)
209 ecore_config_typed_create(key, (void *)&val, ECORE_CONFIG_FLT, short_opt, long_opt,
214 * Creates a new float property, if it does not already exist, and sets its
215 * attributes to those given.
216 * @param key The property key.
217 * @param val Default float value of key.
218 * @param low Lowest valid float value for the property.
219 * @param high Highest valid float value for the property.
220 * @param step Increment value for the property.
221 * @param short_opt Short option used to set the property from command
223 * @param long_opt Long option used to set the property from command line.
224 * @param desc String description of property.
225 * @return @c ECORE_CONFIG_ERR_SUCC on success.
226 * @ingroup Ecore_Config_Create_Group
229 ecore_config_float_create_bound(const char *key, float val, float low,
230 float high, float step, char short_opt,
231 char *long_opt, char *desc)
233 Ecore_Config_Prop *e;
237 ecore_config_typed_create(key, (void *)&val, ECORE_CONFIG_FLT, short_opt, long_opt,
239 e = ecore_config_get(key);
242 e->step = (int)(step * ECORE_CONFIG_FLOAT_PRECISION);
243 e->flags |= ECORE_CONFIG_FLAG_BOUNDS;
244 e->lo = (int)(low * ECORE_CONFIG_FLOAT_PRECISION);
245 e->hi = (int)(high * ECORE_CONFIG_FLOAT_PRECISION);
246 ecore_config_bound(e);
252 * Creates a new color property, if it does not already exist, and sets its
253 * attributes to those given.
254 * @param key The property key.
255 * @param val Default color value of key, as a hexadecimal string.
256 * @param short_opt Short option used to set the property from command
258 * @param long_opt Long option used to set the property from command line.
259 * @param desc String description of property.
260 * @return @c ECORE_CONFIG_ERR_SUCC on success.
261 * @ingroup Ecore_Config_Create_Group
264 ecore_config_argb_create(const char *key, char *val, char short_opt,
265 char *long_opt, char *desc)
268 ecore_config_typed_create(key, (void *)val, ECORE_CONFIG_RGB, short_opt, long_opt,
273 * Creates a new theme property, if it does not already exist, and sets its
274 * attributes to those given.
275 * @param key The property key.
276 * @param val Default theme name for the property.
277 * @param short_opt Short option used to set the property from command
279 * @param long_opt Long option used to set the property from command line.
280 * @param desc String description of property.
281 * @return @c ECORE_CONFIG_ERR_SUCC on success.
282 * @ingroup Ecore_Config_Create_Group
285 ecore_config_theme_create(const char *key, char *val, char short_opt,
286 char *long_opt, char *desc)
289 ecore_config_typed_create(key, (void *)val, ECORE_CONFIG_THM, short_opt, long_opt,
293 /* this should only be built if evas is present */
296 * Calls evas_font_path_append on @p evas for each of the font names stored
297 * in the property "/e/font/path".
298 * @param evas Evas object to append the font names to.
299 * @return @c ECORE_CONFIG_ERR_SUCC on success. @c ECORE_CONFIG_ERR_NODATA
300 * is returned if the property has not been set.
303 ecore_config_evas_font_path_apply(Evas * evas)
305 char *font_path, *font_path_tmp, *ptr, *end;
307 font_path = ecore_config_string_get("/e/font/path");
310 return ECORE_CONFIG_ERR_NODATA;
312 end = font_path + strlen(font_path);
313 font_path_tmp = font_path;
314 while (ptr && ptr < end)
316 while (*ptr != '|' && ptr < end)
321 evas_font_path_append(evas, font_path_tmp);
328 return ECORE_CONFIG_ERR_SUCC;
332 * Retrieves the default theme search path.
334 * @return The default theme search path.
337 ecore_config_theme_default_path_get(void)
342 home = getenv("HOME");
343 len = strlen(PACKAGE_DATA_DIR "/../") + strlen(__ecore_config_app_name) +
344 strlen("/themes/") + 1;
346 len += strlen(home) + strlen("/.e/apps/") +
347 strlen(__ecore_config_app_name) +
348 strlen("/themes/|"); /* no \0, as that is above */
350 if (!(path = malloc(len)))
357 strcat(path, "/.e/apps/");
358 strcat(path, __ecore_config_app_name);
359 strcat(path, "/themes/|");
361 strcat(path, PACKAGE_DATA_DIR "/../");
362 strcat(path, __ecore_config_app_name);
363 strcat(path, "/themes/");
369 * Retrieves the search path used to find themes.
371 * The search path is stored in the property "/e/themes/search_path". If
372 * the property has not been set, the default path used is
373 * "/usr/local/share/<app_name>/themes|~/.e/apps/<app_name>/themes".
374 * See @ref ecore_config_theme_default_path_get for more information about
377 * @return The search path. @c NULL is returned if there is no memory left.
380 ecore_config_theme_search_path_get(void)
383 search_path = ecore_config_string_get("/e/themes/search_path");
385 /* this should no longer be the case, as it is defaulted in init */
388 search_path = ecore_config_theme_default_path_get();
391 ecore_config_string_default("/e/themes/search_path", search_path);
399 * Adds the given path to the search path used to find themes.
401 * If the search path is successfully, the new search path will be saved
402 * into the property "/e/themes/search_path". Therefore, this function
403 * should be called @b after @ref ecore_config_load to allow a user to
404 * override the default search path.
406 * @param path The given
407 * @return @c ECORE_CONFIG_ERR_SUCC on success. @c ECORE_CONFIG_ERR_FAIL
408 * will be returned if @p path already exists in the search path.
409 * @c ECORE_CONFIG_ERR_FAIL is returned if @p path is @c NULL.
412 ecore_config_theme_search_path_append(const char *path)
414 char *search_path, *loc, *new_search_path;
416 Ecore_Config_Prop *prop;
419 return ECORE_CONFIG_ERR_NODATA;
420 search_path = ecore_config_theme_search_path_get();
422 loc = strstr(search_path, path);
424 search_len = strlen(search_path);
426 if (!loc || (loc != search_path && *(loc - 1) != '|') ||
427 (loc != (search_path + search_len - len) && *(loc + len - 1) != '|'))
429 new_search_path = malloc(search_len + len + 2); /* 2 = \0 + | */
430 strcpy(new_search_path, search_path);
431 strncat(new_search_path, "|", 1);
432 strncat(new_search_path, path, len);
434 ecore_config_string_set("/e/themes/search_path", new_search_path);
435 prop = ecore_config_get("/e/themes/search_path");
437 prop->flags &= ~ECORE_CONFIG_FLAG_MODIFIED;
439 free(new_search_path);
441 return ECORE_CONFIG_ERR_SUCC;
443 return ECORE_CONFIG_ERR_FAIL;
447 * Retrieve a theme file's full path.
449 * The search path for theme files is given by @ref
450 * ecore_config_theme_search_path_get .
452 * @param name The name of the theme.
453 * @return A full path to the theme on success. @c NULL will be returned
454 * if @p name is @c NULL or no theme matching the given name could
458 ecore_config_theme_with_path_from_name_get(char *name)
460 char *search_path, *search_path_tmp, *ptr, *end, *file;
464 return NULL; /* no theme specified (nor a default) */
466 search_path = ecore_config_theme_search_path_get();
468 end = search_path + strlen(search_path);
469 search_path_tmp = search_path;
470 while (ptr && ptr < end)
472 while (*ptr != '|' && ptr < end)
477 file = malloc(strlen(search_path_tmp) + strlen(name) + 6);
478 /* 6 = / + .edj + \0 */
480 snprintf(file, strlen(search_path_tmp) + strlen(name) + 6,
481 "%s/%s.edj", search_path_tmp, name);
483 if (stat(file, &st) == 0)
490 search_path_tmp = ptr;
495 return NULL; /* we could not find the theme with that name in search path */
499 * Retrieves the full path to the theme file of the theme stored in the
502 * The search path for themes is given by @ref
503 * ecore_config_theme_search_path_get .
505 * @param key The given property.
506 * @return A full path to the theme on success, or @c NULL on failure.
507 * This function will fail if no key is specified or not theme
508 * matching that given by the property @p key could be found.
511 ecore_config_theme_with_path_get(const char *key)
514 ecore_config_theme_with_path_from_name_get(ecore_config_theme_get(key));
517 static const char *_ecore_config_short_types[] =
518 { " ", "<int> ", "<flt> ", "<str> ", "<rgb> ", "<str> ", "<bool>" };
521 * Prints the property list of the local configuration bundle to output.
524 ecore_config_args_display(void)
526 Ecore_Config_Prop *props;
527 _Ecore_Config_Arg_Callback *callbacks;
529 if (__ecore_config_app_description)
530 ERR("%s\n\n", __ecore_config_app_description);
531 ERR("Supported Options:");
532 ERR(" -h, --help\t Print this text");
533 if (!__ecore_config_bundle_local)
535 props = __ecore_config_bundle_local->data;
538 /* if it is a system prop, or cannot be set on command line hide it */
539 if (props->flags & ECORE_CONFIG_FLAG_SYSTEM || (!props->short_opt && !props->long_opt))
544 INF(" %c%c%c --%s\t%s %s", props->short_opt ? '-' : ' ',
545 props->short_opt ? props->short_opt : ' ',
546 props->short_opt ? ',' : ' ',
547 props->long_opt ? props->long_opt : props->key,
548 _ecore_config_short_types[props->type],
549 props->description ? props->description :
550 "(no description available)");
554 callbacks = _ecore_config_arg_callbacks;
557 INF(" %c%c%c --%s\t%s %s", callbacks->short_opt ? '-' : ' ',
558 callbacks->short_opt ? callbacks->short_opt : ' ',
559 callbacks->short_opt ? ',' : ' ',
560 callbacks->long_opt ? callbacks->long_opt : "",
561 _ecore_config_short_types[callbacks->type],
562 callbacks->description ? callbacks->description :
563 "(no description available)");
565 callbacks = callbacks->next;
570 ecore_config_parse_set(Ecore_Config_Prop * prop, char *arg, char *opt,
576 ERR("Missing expected argument for option --%s", opt);
578 ERR("Missing expected argument for option -%c", opt2);
579 return ECORE_CONFIG_PARSE_EXIT;
583 ecore_config_set(prop->key, arg);
584 prop->flags |= ECORE_CONFIG_FLAG_CMDLN;
586 return ECORE_CONFIG_PARSE_CONTINUE;
590 ecore_config_args_callback_add(char short_opt, char *long_opt, char *desc,
591 void (*func)(char *val, void *data),
592 void *data, Ecore_Config_Type type) {
593 _Ecore_Config_Arg_Callback *new_cb;
595 new_cb = malloc(sizeof(_Ecore_Config_Arg_Callback));
596 new_cb->short_opt = short_opt;
598 new_cb->long_opt = strdup(long_opt);
600 new_cb->description = strdup(desc);
605 new_cb->next = _ecore_config_arg_callbacks;
606 _ecore_config_arg_callbacks = new_cb;
610 ecore_config_args_callback_str_add(char short_opt, char *long_opt, char *desc,
611 void (*func)(char *val, void *data),
613 ecore_config_args_callback_add(short_opt, long_opt, desc, func, data, ECORE_CONFIG_STR);
617 ecore_config_args_callback_noarg_add(char short_opt, char *long_opt, char *desc,
618 void (*func)(char *val, void *data),
620 ecore_config_args_callback_add(short_opt, long_opt, desc, func, data, ECORE_CONFIG_NIL);
624 * Parse the arguments set by @ref ecore_app_args_set and set properties
627 * @return @c ECORE_CONFIG_PARSE_CONTINUE if successful.
628 * @c ECORE_CONFIG_PARSE_EXIT is returned if an unrecognised option
629 * is found. @c ECORE_CONFIG_PARSE_HELP is returned if help was
633 ecore_config_args_parse(void)
637 int nextarg, next_short_opt, found, ret;
639 char *long_opt, short_opt;
640 Ecore_Config_Prop *prop;
641 _Ecore_Config_Arg_Callback *callback;
643 ecore_app_args_get(&argc, &argv);
645 while (nextarg < argc)
651 ERR("Unexpected attribute \"%s\"", arg);
657 short_opt = *(arg + next_short_opt);
659 if (short_opt == '-')
663 if (!strcmp(long_opt, "help"))
665 ecore_config_args_display();
666 return ECORE_CONFIG_PARSE_HELP;
670 prop = __ecore_config_bundle_local->data;
673 if ((prop->long_opt && !strcmp(long_opt, prop->long_opt))
674 || !strcmp(long_opt, prop->key))
678 ecore_config_parse_set(prop, argv[++nextarg],
681 ECORE_CONFIG_PARSE_CONTINUE)
689 callback = _ecore_config_arg_callbacks;
692 if ((callback->long_opt &&
693 !strcmp(long_opt, callback->long_opt)))
696 if (callback->type == ECORE_CONFIG_NIL)
698 callback->func(NULL, callback->data);
702 if (!argv[++nextarg])
704 ERR("Missing expected argument for option --%s", long_opt);
705 return ECORE_CONFIG_PARSE_EXIT;
707 callback->func(argv[nextarg], callback->data);
711 callback = callback->next;
716 ERR("Unrecognised option \"%s\"", long_opt);
717 ERR("Try using -h or --help for more information.\n");
718 return ECORE_CONFIG_PARSE_EXIT;
725 if (short_opt == 'h')
727 ecore_config_args_display();
728 return ECORE_CONFIG_PARSE_HELP;
733 prop = __ecore_config_bundle_local->data;
736 if (short_opt == prop->short_opt)
740 ecore_config_parse_set(prop,
744 ECORE_CONFIG_PARSE_CONTINUE)
753 callback = _ecore_config_arg_callbacks;
756 if (short_opt == callback->short_opt)
759 if (callback->type == ECORE_CONFIG_NIL)
761 callback->func(NULL, callback->data);
765 if (!argv[++nextarg])
767 ERR("Missing expected argument for option -%c", short_opt);
768 return ECORE_CONFIG_PARSE_EXIT;
770 callback->func(argv[nextarg], callback->data);
774 callback = callback->next;
779 ERR("Unrecognised option '%c'", short_opt);
780 ERR("Try using -h or --help for more information.\n");
781 return ECORE_CONFIG_PARSE_EXIT;
784 short_opt = *(arg + ++next_short_opt);
790 return ECORE_CONFIG_PARSE_CONTINUE;
794 * Sets the description string used by @ref ecore_config_args_display .
795 * @param description Description of application.
798 ecore_config_app_describe(char *description)
800 if (__ecore_config_app_description)
801 free(__ecore_config_app_description);
802 __ecore_config_app_description = strdup(description);