8 #include "Ecore_Config.h"
13 typedef struct __Ecore_Config_Arg_Callback _Ecore_Config_Arg_Callback;
14 struct __Ecore_Config_Arg_Callback
20 void (*func)(char *val, void *data);
21 Ecore_Config_Type type;
22 _Ecore_Config_Arg_Callback *next;
25 char *__ecore_config_app_description;
26 _Ecore_Config_Arg_Callback *_ecore_config_arg_callbacks;
28 /* shorthand prop setup code to make client apps a little smaller ;) */
31 * Creates a new property, if it does not already exist, and sets its
32 * attributes to those given.
34 * The type of the property is guessed from the key and the value
37 * @param key The property key.
38 * @param val Pointer to default value of key.
39 * @param short_opt Short option used to set the property from command
41 * @param long_opt Long option used to set the property from command line.
42 * @param desc String description of property.
43 * @return @c ECORE_CONFIG_ERR_SUCC on success.
44 * @ingroup Ecore_Config_Create_Group
47 ecore_config_create(const char *key, void *val, char short_opt, char *long_opt,
50 int type = ecore_config_type_guess(key, val);
52 return ecore_config_typed_create(key, val, type, short_opt, long_opt, desc);
56 * Creates a new property, if it does not already exist, and sets its
57 * attributes to those given.
58 * @param key The property key.
59 * @param val Pointer to default value of key.
60 * @param type Type of the property.
61 * @param short_opt Short option used to set the property from
63 * @param long_opt Long option used to set the property from command line.
64 * @param desc String description of property.
65 * @return @c ECORE_CONFIG_ERR_SUCC on success.
66 * @ingroup Ecore_Config_Create_Group
69 ecore_config_typed_create(const char *key, void *val, int type, char short_opt,
70 char *long_opt, char *desc)
75 ecore_config_typed_default(key, val, type)) != ECORE_CONFIG_ERR_SUCC)
78 ecore_config_short_opt_set(key, short_opt)) != ECORE_CONFIG_ERR_SUCC)
81 ecore_config_long_opt_set(key, long_opt)) != ECORE_CONFIG_ERR_SUCC)
83 ret = ecore_config_describe(key, desc);
88 * Creates a new boolean property, if it does not already exist, and sets its
89 * attributes to those given.
90 * @param key The property key.
91 * @param val Default boolean value of key.
92 * @param short_opt Short option used to set the property from command
94 * @param long_opt Long option used to set the property from command line.
95 * @param desc String description of property.
96 * @return @c ECORE_CONFIG_ERR_SUCC on success.
97 * @ingroup Ecore_Config_Create_Group
100 ecore_config_boolean_create(const char *key, int val, char short_opt,
101 char *long_opt, char *desc)
104 ecore_config_typed_create(key, (void *)&val, ECORE_CONFIG_BLN, short_opt, long_opt,
109 * Creates a new integer property, if it does not already exist, and sets its
110 * attributes to those given.
111 * @param key The property key.
112 * @param val Default integer value of key.
113 * @param short_opt Short option used to set the property from command
115 * @param long_opt Long option used to set the property from command line.
116 * @param desc String description of property.
117 * @return @c ECORE_CONFIG_ERR_SUCC on success.
118 * @ingroup Ecore_Config_Create_Group
121 ecore_config_int_create(const char *key, int val, char short_opt,
122 char *long_opt, char *desc)
125 ecore_config_typed_create(key, (void *)&val, ECORE_CONFIG_INT, short_opt, long_opt,
130 * Creates a new integer property, if it does not already exist, and sets its
131 * attributes to those given.
132 * @param key The property key.
133 * @param val Default integer value of key.
134 * @param low Lowest valid integer value for the property.
135 * @param high Highest valid integer value for the property.
136 * @param step Increment value for the property.
137 * @param short_opt Short option used to set the property from command
139 * @param long_opt Long option used to set the property from command line.
140 * @param desc String description of property.
141 * @return @c ECORE_CONFIG_ERR_SUCC on success.
142 * @ingroup Ecore_Config_Create_Group
145 ecore_config_int_create_bound(const char *key, int val, int low, int high,
146 int step, char short_opt, char *long_opt,
149 Ecore_Config_Prop *e;
153 ecore_config_typed_create(key, (void *)&val, ECORE_CONFIG_INT, short_opt, long_opt,
155 if (ret != ECORE_CONFIG_ERR_SUCC)
157 e = ecore_config_get(key);
161 e->flags |= ECORE_CONFIG_FLAG_BOUNDS;
164 ecore_config_bound(e);
170 * Creates a new string property, if it does not already exist, and sets its
171 * attributes to those given.
172 * @param key The property key.
173 * @param val Default value of key.
174 * @param short_opt Short option used to set the property from command
176 * @param long_opt Long option used to set the property from command line.
177 * @param desc String description of property.
178 * @return @c ECORE_CONFIG_ERR_SUCC on success.
179 * @ingroup Ecore_Config_Create_Group
182 ecore_config_string_create(const char *key, char *val, char short_opt,
183 char *long_opt, char *desc)
186 ecore_config_typed_create(key, (void *)val, ECORE_CONFIG_STR, short_opt, long_opt,
191 * Creates a new float property, if it does not already exist, and sets its
192 * attributes to those given.
193 * @param key The property key.
194 * @param val Default float value of key.
195 * @param short_opt Short option used to set the property from command
197 * @param long_opt Long option used to set the property from command line.
198 * @param desc String description of property.
199 * @return @c ECORE_CONFIG_ERR_SUCC on success.
200 * @ingroup Ecore_Config_Create_Group
203 ecore_config_float_create(const char *key, float val, char short_opt,
204 char *long_opt, char *desc)
207 ecore_config_typed_create(key, (void *)&val, ECORE_CONFIG_FLT, short_opt, long_opt,
212 * Creates a new float property, if it does not already exist, and sets its
213 * attributes to those given.
214 * @param key The property key.
215 * @param val Default float value of key.
216 * @param low Lowest valid float value for the property.
217 * @param high Highest valid float value for the property.
218 * @param step Increment value for the property.
219 * @param short_opt Short option used to set the property from command
221 * @param long_opt Long option used to set the property from command line.
222 * @param desc String description of property.
223 * @return @c ECORE_CONFIG_ERR_SUCC on success.
224 * @ingroup Ecore_Config_Create_Group
227 ecore_config_float_create_bound(const char *key, float val, float low,
228 float high, float step, char short_opt,
229 char *long_opt, char *desc)
231 Ecore_Config_Prop *e;
235 ecore_config_typed_create(key, (void *)&val, ECORE_CONFIG_FLT, short_opt, long_opt,
237 e = ecore_config_get(key);
240 e->step = (int)(step * ECORE_CONFIG_FLOAT_PRECISION);
241 e->flags |= ECORE_CONFIG_FLAG_BOUNDS;
242 e->lo = (int)(low * ECORE_CONFIG_FLOAT_PRECISION);
243 e->hi = (int)(high * ECORE_CONFIG_FLOAT_PRECISION);
244 ecore_config_bound(e);
250 * Creates a new color property, if it does not already exist, and sets its
251 * attributes to those given.
252 * @param key The property key.
253 * @param val Default color value of key, as a hexadecimal string.
254 * @param short_opt Short option used to set the property from command
256 * @param long_opt Long option used to set the property from command line.
257 * @param desc String description of property.
258 * @return @c ECORE_CONFIG_ERR_SUCC on success.
259 * @ingroup Ecore_Config_Create_Group
262 ecore_config_argb_create(const char *key, char *val, char short_opt,
263 char *long_opt, char *desc)
266 ecore_config_typed_create(key, (void *)val, ECORE_CONFIG_RGB, short_opt, long_opt,
271 * Creates a new theme property, if it does not already exist, and sets its
272 * attributes to those given.
273 * @param key The property key.
274 * @param val Default theme name for the property.
275 * @param short_opt Short option used to set the property from command
277 * @param long_opt Long option used to set the property from command line.
278 * @param desc String description of property.
279 * @return @c ECORE_CONFIG_ERR_SUCC on success.
280 * @ingroup Ecore_Config_Create_Group
283 ecore_config_theme_create(const char *key, char *val, char short_opt,
284 char *long_opt, char *desc)
287 ecore_config_typed_create(key, (void *)val, ECORE_CONFIG_THM, short_opt, long_opt,
291 /* this should only be built if evas is present */
294 * Calls evas_font_path_append on @p evas for each of the font names stored
295 * in the property "/e/font/path".
296 * @param evas Evas object to append the font names to.
297 * @return @c ECORE_CONFIG_ERR_SUCC on success. @c ECORE_CONFIG_ERR_NODATA
298 * is returned if the property has not been set.
301 ecore_config_evas_font_path_apply(Evas * evas)
303 char *font_path, *font_path_tmp, *ptr, *end;
305 font_path = ecore_config_string_get("/e/font/path");
308 return ECORE_CONFIG_ERR_NODATA;
310 end = font_path + strlen(font_path);
311 font_path_tmp = font_path;
312 while (ptr && ptr < end)
314 while (*ptr != '|' && ptr < end)
319 evas_font_path_append(evas, font_path_tmp);
326 return ECORE_CONFIG_ERR_SUCC;
330 * Retrieves the default theme search path.
332 * @return The default theme search path.
335 ecore_config_theme_default_path_get(void)
340 home = getenv("HOME");
341 len = strlen(PACKAGE_DATA_DIR "/../") + strlen(__ecore_config_app_name) +
342 strlen("/themes/") + 1;
344 len += strlen(home) + strlen("/.e/apps/") +
345 strlen(__ecore_config_app_name) +
346 strlen("/themes/|"); /* no \0, as that is above */
348 if (!(path = malloc(len)))
355 strcat(path, "/.e/apps/");
356 strcat(path, __ecore_config_app_name);
357 strcat(path, "/themes/|");
359 strcat(path, PACKAGE_DATA_DIR "/../");
360 strcat(path, __ecore_config_app_name);
361 strcat(path, "/themes/");
367 * Retrieves the search path used to find themes.
369 * The search path is stored in the property "/e/themes/search_path". If
370 * the property has not been set, the default path used is
371 * "/usr/local/share/<app_name>/themes|~/.e/apps/<app_name>/themes".
372 * See @ref ecore_config_theme_default_path_get for more information about
375 * @return The search path. @c NULL is returned if there is no memory left.
378 ecore_config_theme_search_path_get(void)
381 search_path = ecore_config_string_get("/e/themes/search_path");
383 /* this should no longer be the case, as it is defaulted in init */
386 search_path = ecore_config_theme_default_path_get();
389 ecore_config_string_default("/e/themes/search_path", search_path);
397 * Adds the given path to the search path used to find themes.
399 * If the search path is successfully, the new search path will be saved
400 * into the property "/e/themes/search_path". Therefore, this function
401 * should be called @b after @ref ecore_config_load to allow a user to
402 * override the default search path.
404 * @param path The given
405 * @return @c ECORE_CONFIG_ERR_SUCC on success. @c ECORE_CONFIG_ERR_FAIL
406 * will be returned if @p path already exists in the search path.
407 * @c ECORE_CONFIG_ERR_FAIL is returned if @p path is @c NULL.
410 ecore_config_theme_search_path_append(const char *path)
412 char *search_path, *loc, *new_search_path;
414 Ecore_Config_Prop *prop;
417 return ECORE_CONFIG_ERR_NODATA;
418 search_path = ecore_config_theme_search_path_get();
420 loc = strstr(search_path, path);
422 search_len = strlen(search_path);
424 if (loc == NULL || (loc != search_path && *(loc - 1) != '|') ||
425 (loc != (search_path + search_len - len) && *(loc + len - 1) != '|'))
427 new_search_path = malloc(search_len + len + 2); /* 2 = \0 + | */
428 strcpy(new_search_path, search_path);
429 strncat(new_search_path, "|", 1);
430 strncat(new_search_path, path, len);
432 ecore_config_string_set("/e/themes/search_path", new_search_path);
433 prop = ecore_config_get("/e/themes/search_path");
435 prop->flags &= ~ECORE_CONFIG_FLAG_MODIFIED;
437 free(new_search_path);
439 return ECORE_CONFIG_ERR_SUCC;
441 return ECORE_CONFIG_ERR_FAIL;
445 * Retrieve a theme file's full path.
447 * The search path for theme files is given by @ref
448 * ecore_config_theme_search_path_get .
450 * @param name The name of the theme.
451 * @return A full path to the theme on success. @c NULL will be returned
452 * if @p name is @c NULL or no theme matching the given name could
456 ecore_config_theme_with_path_from_name_get(char *name)
458 char *search_path, *search_path_tmp, *ptr, *end, *file;
462 return NULL; /* no theme specified (nor a default) */
464 search_path = ecore_config_theme_search_path_get();
466 end = search_path + strlen(search_path);
467 search_path_tmp = search_path;
468 while (ptr && ptr < end)
470 while (*ptr != '|' && ptr < end)
475 file = malloc(strlen(search_path_tmp) + strlen(name) + 6);
476 /* 6 = / + .edj + \0 */
478 snprintf(file, strlen(search_path_tmp) + strlen(name) + 6,
479 "%s/%s.edj", search_path_tmp, name);
481 if (stat(file, &st) == 0)
488 search_path_tmp = ptr;
493 return NULL; /* we could not find the theme with that name in search path */
497 * Retrieves the full path to the theme file of the theme stored in the
500 * The search path for themes is given by @ref
501 * ecore_config_theme_search_path_get .
503 * @param key The given property.
504 * @return A full path to the theme on success, or @c NULL on failure.
505 * This function will fail if no key is specified or not theme
506 * matching that given by the property @p key could be found.
509 ecore_config_theme_with_path_get(const char *key)
512 ecore_config_theme_with_path_from_name_get(ecore_config_theme_get(key));
515 static const char *_ecore_config_short_types[] =
516 { " ", "<int> ", "<flt> ", "<str> ", "<rgb> ", "<str> ", "<bool>" };
519 * Prints the property list of the local configuration bundle to output.
522 ecore_config_args_display(void)
524 Ecore_Config_Prop *props;
525 _Ecore_Config_Arg_Callback *callbacks;
527 if (__ecore_config_app_description)
528 printf("%s\n\n", __ecore_config_app_description);
529 printf("Supported Options:\n");
530 printf(" -h, --help\t Print this text\n");
531 if (!__ecore_config_bundle_local)
533 props = __ecore_config_bundle_local->data;
536 /* if it is a system prop, or cannot be set on command line hide it */
537 if (props->flags & ECORE_CONFIG_FLAG_SYSTEM || (!props->short_opt && !props->long_opt))
542 printf(" %c%c%c --%s\t%s %s\n", props->short_opt ? '-' : ' ',
543 props->short_opt ? props->short_opt : ' ',
544 props->short_opt ? ',' : ' ',
545 props->long_opt ? props->long_opt : props->key,
546 _ecore_config_short_types[props->type],
547 props->description ? props->description :
548 "(no description available)");
552 callbacks = _ecore_config_arg_callbacks;
555 printf(" %c%c%c --%s\t%s %s\n", callbacks->short_opt ? '-' : ' ',
556 callbacks->short_opt ? callbacks->short_opt : ' ',
557 callbacks->short_opt ? ',' : ' ',
558 callbacks->long_opt ? callbacks->long_opt : "",
559 _ecore_config_short_types[callbacks->type],
560 callbacks->description ? callbacks->description :
561 "(no description available)");
563 callbacks = callbacks->next;
568 ecore_config_parse_set(Ecore_Config_Prop * prop, char *arg, char *opt,
574 printf("Missing expected argument for option --%s\n", opt);
576 printf("Missing expected argument for option -%c\n", opt2);
577 return ECORE_CONFIG_PARSE_EXIT;
581 ecore_config_set(prop->key, arg);
582 prop->flags |= ECORE_CONFIG_FLAG_CMDLN;
584 return ECORE_CONFIG_PARSE_CONTINUE;
588 ecore_config_args_callback_add(char short_opt, char *long_opt, char *desc,
589 void (*func)(char *val, void *data),
590 void *data, Ecore_Config_Type type) {
591 _Ecore_Config_Arg_Callback *new_cb;
593 new_cb = malloc(sizeof(_Ecore_Config_Arg_Callback));
594 new_cb->short_opt = short_opt;
596 new_cb->long_opt = strdup(long_opt);
598 new_cb->description = strdup(desc);
603 new_cb->next = _ecore_config_arg_callbacks;
604 _ecore_config_arg_callbacks = new_cb;
608 ecore_config_args_callback_str_add(char short_opt, char *long_opt, char *desc,
609 void (*func)(char *val, void *data),
611 ecore_config_args_callback_add(short_opt, long_opt, desc, func, data, ECORE_CONFIG_STR);
615 ecore_config_args_callback_noarg_add(char short_opt, char *long_opt, char *desc,
616 void (*func)(char *val, void *data),
618 ecore_config_args_callback_add(short_opt, long_opt, desc, func, data, ECORE_CONFIG_NIL);
622 * Parse the arguments set by @ref ecore_app_args_set and set properties
625 * @return @c ECORE_CONFIG_PARSE_CONTINUE if successful.
626 * @c ECORE_CONFIG_PARSE_EXIT is returned if an unrecognised option
627 * is found. @c ECORE_CONFIG_PARSE_HELP is returned if help was
631 ecore_config_args_parse(void)
635 int nextarg, next_short_opt, found, ret;
637 char *long_opt, short_opt;
638 Ecore_Config_Prop *prop;
639 _Ecore_Config_Arg_Callback *callback;
641 ecore_app_args_get(&argc, &argv);
643 while (nextarg < argc)
649 printf("Unexpected attribute \"%s\"\n", arg);
655 short_opt = *(arg + next_short_opt);
657 if (short_opt == '-')
661 if (!strcmp(long_opt, "help"))
663 ecore_config_args_display();
664 return ECORE_CONFIG_PARSE_HELP;
668 prop = __ecore_config_bundle_local->data;
671 if ((prop->long_opt && !strcmp(long_opt, prop->long_opt))
672 || !strcmp(long_opt, prop->key))
676 ecore_config_parse_set(prop, argv[++nextarg],
679 ECORE_CONFIG_PARSE_CONTINUE)
687 callback = _ecore_config_arg_callbacks;
690 if ((callback->long_opt &&
691 !strcmp(long_opt, callback->long_opt)))
694 if (callback->type == ECORE_CONFIG_NIL)
696 callback->func(NULL, callback->data);
700 if (!argv[++nextarg])
702 printf("Missing expected argument for option --%s\n", long_opt);
703 return ECORE_CONFIG_PARSE_EXIT;
705 callback->func(argv[nextarg], callback->data);
709 callback = callback->next;
714 printf("Unrecognised option \"%s\"\n", long_opt);
715 printf("Try using -h or --help for more information.\n\n");
716 return ECORE_CONFIG_PARSE_EXIT;
723 if (short_opt == 'h')
725 ecore_config_args_display();
726 return ECORE_CONFIG_PARSE_HELP;
731 prop = __ecore_config_bundle_local->data;
734 if (short_opt == prop->short_opt)
738 ecore_config_parse_set(prop,
742 ECORE_CONFIG_PARSE_CONTINUE)
751 callback = _ecore_config_arg_callbacks;
754 if (short_opt == callback->short_opt)
757 if (callback->type == ECORE_CONFIG_NIL)
759 callback->func(NULL, callback->data);
763 if (!argv[++nextarg])
765 printf("Missing expected argument for option -%c\n", short_opt);
766 return ECORE_CONFIG_PARSE_EXIT;
768 callback->func(argv[nextarg], callback->data);
772 callback = callback->next;
777 printf("Unrecognised option '%c'\n", short_opt);
779 ("Try using -h or --help for more information.\n\n");
780 return ECORE_CONFIG_PARSE_EXIT;
783 short_opt = *(arg + ++next_short_opt);
789 return ECORE_CONFIG_PARSE_CONTINUE;
793 * Sets the description string used by @ref ecore_config_args_display .
794 * @param description Description of application.
797 ecore_config_app_describe(char *description)
799 if (__ecore_config_app_description)
800 free(__ecore_config_app_description);
801 __ecore_config_app_description = strdup(description);