1 #include <Elementary.h>
4 # include "elementary_config.h"
8 #include <Ecore_Evas.h>
9 #include <Ecore_Getopt.h>
11 #define EDJE_EDIT_IS_UNSTABLE_AND_I_KNOW_ABOUT_IT 1
12 #include <Edje_Edit.h>
22 # ifndef libintl_setlocale
23 # define libintl_setlocale(c, l)
28 #define DBG(...) EINA_LOG_DOM_DBG(_log_dom, __VA_ARGS__)
29 #define ERR(...) EINA_LOG_DOM_ERR(_log_dom, __VA_ARGS__)
30 #define WRN(...) EINA_LOG_DOM_WARN(_log_dom, __VA_ARGS__)
32 static Ecore_Evas *ee = NULL;
33 static char *file = NULL;
34 static char *group = NULL;
35 static char *prefix = NULL;
36 static FILE *source_fd = NULL;
37 static FILE *header_fd = NULL;
43 "#include <Edje.h>\n" \
44 "#include <Elementary.h>\n" \
45 "#include <Evas.h>\n\n" \
46 "#include <stdlib.h>\n\n"
49 "\n#endif /* _%s */\n"
54 #define H_CODEGEN_LAYOUT_ADD \
55 "/**\n * @brief Creates the layout object and set the theme\n" \
56 " * @param o The parent\n" \
57 " * @param th The theme to add to, or if NULL, the default theme\n" \
58 " * @param edje_file The path to edj, if NULL it's used the path given\n" \
59 " * to elementary_codegen\n */\n" \
60 "Evas_Object *%s_layout_add(Evas_Object *o, Elm_Theme *th, " \
61 "const char *edje_file);\n"
63 #define C_CODEGEN_LAYOUT_ADD \
65 "%s_layout_add(Evas_Object *o, Elm_Theme *th, const char *edje_file)\n" \
67 " Evas_Object *l;\n\n" \
69 " elm_theme_extension_add(th, edje_file);\n" \
71 " elm_theme_extension_add(th, \"./%s\");\n\n" \
72 " l = elm_layout_add(o);\n" \
73 " if (!l) return NULL;\n\n" \
74 " if (!elm_layout_theme_set(l, \"%s\", \"%s\", \"%s\"))\n" \
76 " evas_object_del(l);\n" \
82 #define C_CODEGEN_PART_CONTENT_SET \
84 "%s_%s_set(Evas_Object *o, Evas_Object *value)\n" \
86 " elm_layout_content_set(o, \"%s\", value);\n" \
89 #define C_CODEGEN_PART_TEXT_SET \
91 "%s_%s_set(Evas_Object *o, const char *value)\n" \
93 " elm_layout_text_set(o, \"%s\", value);\n" \
96 #define C_CODEGEN_PART_CONTENT_UNSET \
98 "%s_%s_unset(Evas_Object *o)\n" \
100 " return elm_layout_content_unset(o, \"%s\");\n" \
103 #define H_CODEGEN_PART_CONTENT_SET \
104 "void %s_%s_set(Evas_Object *o, Evas_Object *value);\n"
106 #define H_CODEGEN_PART_TEXT_SET \
107 "void %s_%s_set(Evas_Object *o, const char *value);\n"
109 #define H_CODEGEN_PART_CONTENT_UNSET \
110 "Evas_Object *%s_%s_unset(Evas_Object *o);\n"
112 #define C_CODEGEN_PART_CONTENT_GET \
114 "%s_%s_get(const Evas_Object *o)\n" \
116 " return elm_layout_content_get(o, \"%s\");\n" \
119 #define H_CODEGEN_PART_CONTENT_GET \
120 "Evas_Object *%s_%s_get(const Evas_Object *o);\n"
122 #define C_CODEGEN_PART_TEXT_GET \
124 "%s_%s_get(const Evas_Object *o)\n" \
126 " return elm_layout_text_get(o, \"%s\");\n" \
129 #define H_CODEGEN_PART_TEXT_GET \
130 "const char *%s_%s_get(const Evas_Object *o);\n"
132 #define C_CODEGEN_PART_BOX_APPEND \
134 "%s_%s_append(Evas_Object *o, Evas_Object *child)\n" \
136 " return elm_layout_box_append(o, \"%s\", child);\n" \
139 #define H_CODEGEN_PART_BOX_APPEND \
140 "Eina_Bool %s_%s_append(Evas_Object *o, Evas_Object *child);\n"
142 #define C_CODEGEN_PART_BOX_PREPEND \
144 "%s_%s_prepend(Evas_Object *o, Evas_Object *child)\n" \
146 " return elm_layout_box_prepend(o, \"%s\", child);\n" \
149 #define H_CODEGEN_PART_BOX_PREPEND \
150 "Eina_Bool %s_%s_prepend(Evas_Object *o, Evas_Object *child);\n"
152 #define C_CODEGEN_PART_BOX_INSERT_BEFORE \
154 "%s_%s_insert_before(Evas_Object *o, Evas_Object *child, " \
155 "const Evas_Object *reference)\n" \
157 " return elm_layout_box_insert_before(o, \"%s\", " \
158 "child, reference);\n" \
161 #define H_CODEGEN_PART_BOX_INSERT_BEFORE \
162 "Eina_Bool %s_%s_insert_before(Evas_Object *o, Evas_Object *child, " \
163 "const Evas_Object *reference);\n"
165 #define C_CODEGEN_PART_BOX_INSERT_AT \
167 "%s_%s_insert_at(Evas_Object *o, Evas_Object *child, unsigned int pos)\n" \
169 " return elm_layout_box_insert_at(o, \"%s\", child, pos);\n" \
172 #define H_CODEGEN_PART_BOX_INSERT_AT \
173 "Eina_Bool %s_%s_insert_at(Evas_Object *o, Evas_Object *child, " \
174 "unsigned int pos);\n"
176 #define C_CODEGEN_PART_BOX_REMOVE \
178 "%s_%s_remove(Evas_Object *o, Evas_Object *child)\n" \
180 " return elm_layout_box_remove(o, \"%s\", child);\n" \
183 #define H_CODEGEN_PART_BOX_REMOVE \
184 "Evas_Object *%s_%s_remove(Evas_Object *o, Evas_Object *child);\n"
186 #define C_CODEGEN_PART_BOX_REMOVE_ALL \
188 "%s_%s_remove_all(Evas_Object *o, Eina_Bool clear)\n" \
190 " return elm_layout_box_remove_all(o, \"%s\", clear);\n" \
193 #define H_CODEGEN_PART_BOX_REMOVE_ALL \
194 "Eina_Bool %s_%s_remove_all(Evas_Object *o, Eina_Bool clear);\n"
196 #define C_CODEGEN_PART_TABLE_PACK \
198 "%s_%s_pack(Evas_Object *o, Evas_Object *child, unsigned short col, " \
199 "unsigned short row, unsigned short colspan, unsigned short rowspan)\n" \
201 " return elm_layout_table_pack(o, \"%s\", child, col, row, " \
202 "colspan, rowspan);\n" \
205 #define H_CODEGEN_PART_TABLE_PACK \
206 "Eina_Bool %s_%s_pack(Evas_Object *o, Evas_Object *child, " \
207 "unsigned short col, unsigned short row, unsigned short " \
208 "colspan, unsigned short rowspan);\n"
210 #define C_CODEGEN_PART_TABLE_UNPACK \
212 "%s_%s_unpack(Evas_Object *o, Evas_Object *child)\n" \
214 " return elm_layout_table_unpack(o, \"%s\", child);\n" \
217 #define H_CODEGEN_PART_TABLE_UNPACK \
218 "Evas_Object *%s_%s_unpack(Evas_Object *o, Evas_Object *child);\n"
220 #define C_CODEGEN_PART_TABLE_CLEAR \
222 "%s_%s_clear(Evas_Object *o, Eina_Bool clear)\n" \
224 " return elm_layout_table_clear(o, \"%s\", clear);\n" \
227 #define H_CODEGEN_PART_TABLE_CLEAR \
228 "Eina_Bool %s_%s_clear(Evas_Object *o, Eina_Bool clear);\n"
230 #define C_CODEGEN_PROGRAM_EMIT \
232 "%s_%s_emit(Evas_Object *o)\n" \
234 " elm_layout_signal_emit(o, \"%s\", \"%s\");\n" \
237 #define H_CODEGEN_PROGRAM_EMIT \
238 "void %s_%s_emit(Evas_Object *o);\n"
240 #define C_CODEGEN_PROGRAM_CALLBACK_ADD \
242 "%s_%s_callback_add(Evas_Object *o, Edje_Signal_Cb func, void *data)\n" \
244 " elm_layout_signal_callback_add(o, \"%s\", \"%s\", func, data);\n" \
247 #define H_CODEGEN_PROGRAM_CALLBACK_ADD \
248 "void %s_%s_callback_add(Evas_Object *o, Edje_Signal_Cb func, " \
251 #define C_CODEGEN_PROGRAM_CALLBACK_DEL \
253 "%s_%s_callback_del(Evas_Object *o, Edje_Signal_Cb func)\n" \
255 " elm_layout_signal_callback_del(o, \"%s\", \"%s\", func);\n" \
258 #define H_CODEGEN_PROGRAM_CALLBACK_DEL \
259 "void %s_%s_callback_del(Evas_Object *o, Edje_Signal_Cb func);\n"
262 const Ecore_Getopt optdesc = {
264 "%prog [options] <file.edj> <group> <source_file_name> <header_file_name>",
266 "(C) 2012 - The Enlightenment Project",
268 "elm_codegen generates the boilerplate code to get and set the "
269 "parts of a group from a compiled (binary) edje "
270 "file avoiding common errors with typos.\n",
273 ECORE_GETOPT_STORE_STR('p', "prefix", "The prefix for the " \
275 ECORE_GETOPT_LICENSE('L', "license"),
276 ECORE_GETOPT_COPYRIGHT('C', "copyright"),
277 ECORE_GETOPT_VERSION('V', "version"),
278 ECORE_GETOPT_HELP('h', "help"),
279 ECORE_GETOPT_SENTINEL
284 _header_standardize(const char *filename)
286 char *str, *itr, *aux;
288 aux = strrchr(filename, '/');
289 str = itr = strdup(aux ? aux + 1 : filename);
295 *itr = toupper(*itr);
301 _file_descriptors_open(const char *source, const char *header)
303 header_fd = fopen(header, "w");
307 source_fd = fopen(source, "w");
319 _file_descriptors_close(void)
321 Eina_Bool ret = EINA_FALSE;
323 if (!fclose(header_fd))
326 if (!fclose(source_fd))
333 _headers_write(const char *filename)
338 str = _header_standardize(filename);
339 snprintf(buf, sizeof(buf), H_HEADER, str, str);
340 if (fwrite(buf, strlen(buf), 1, header_fd) != 1)
348 snprintf(buf, sizeof(buf), C_HEADER, filename);
349 if (fwrite(buf, strlen(buf), 1, source_fd) != 1)
356 _footer_write(const char *filename)
361 str = _header_standardize(filename);
362 snprintf(buf, sizeof(buf), H_FOOTER, str);
363 if (fwrite(buf, strlen(buf), 1, header_fd) != 1)
375 _theme_set_write(void)
377 Eina_Bool ret = EINA_FALSE;
378 char *str[3]; /* *klas, *style, *group */
379 char *aux, *token, *_group, buf[512];
382 str[0] = str[1] = str[2] = NULL;
383 if (strncmp(group, "elm/", 4)) return EINA_FALSE;
385 _group = strdup(group);
386 if (!_group) return EINA_FALSE;
388 token = strtok(_group, "/");
389 for (i = 0, aux = NULL; i < 3; i++, aux = NULL)
391 token = strtok(aux, "/");
397 if (!str[0] || !str[1] || !str[2])
400 snprintf(buf, sizeof(buf), C_CODEGEN_LAYOUT_ADD, prefix, file, str[0],
402 if (fwrite(buf, strlen(buf), 1, source_fd) != 1)
405 snprintf(buf, sizeof(buf), H_CODEGEN_LAYOUT_ADD, prefix);
406 if (fwrite(buf, strlen(buf), 1, header_fd) != 1)
417 _part_write(const char *apiname, const char *partname, const char *description,
422 #define TEMPLATE_NAME(sufix) \
424 snprintf(buf, sizeof(buf), C_CODEGEN_PART_##sufix, prefix, \
425 apiname, partname); \
426 if (fwrite(buf, strlen(buf), 1, source_fd) != 1) \
428 snprintf(buf, sizeof(buf), H_CODEGEN_PART_##sufix, prefix, \
430 if (fwrite(buf, strlen(buf), 1, header_fd) != 1) \
436 snprintf(buf, sizeof(buf), "\n/**\n * @brief %s\n */\n", description);
437 if (fwrite(buf, strlen(buf), 1, header_fd) != 1)
443 case EDJE_PART_TYPE_BOX:
444 TEMPLATE_NAME(BOX_APPEND);
445 TEMPLATE_NAME(BOX_PREPEND);
446 TEMPLATE_NAME(BOX_INSERT_BEFORE);
447 TEMPLATE_NAME(BOX_INSERT_AT);
448 TEMPLATE_NAME(BOX_REMOVE);
449 TEMPLATE_NAME(BOX_REMOVE_ALL);
452 case EDJE_PART_TYPE_TABLE:
453 TEMPLATE_NAME(TABLE_PACK);
454 TEMPLATE_NAME(TABLE_UNPACK);
455 TEMPLATE_NAME(TABLE_CLEAR);
458 case EDJE_PART_TYPE_TEXT:
459 TEMPLATE_NAME(TEXT_SET);
460 TEMPLATE_NAME(TEXT_GET);
464 TEMPLATE_NAME(CONTENT_SET);
465 TEMPLATE_NAME(CONTENT_UNSET);
466 TEMPLATE_NAME(CONTENT_GET);
475 ERR("Could not write the part: %s", partname);
479 static inline Eina_Bool
480 _c_id_allowed(char c)
482 if ((c >= '0') && (c <= '9')) return EINA_TRUE;
483 if ((c >= 'a') && (c <= 'z')) return EINA_TRUE;
484 if ((c >= 'A') && (c <= 'Z')) return EINA_TRUE;
490 _api_name_fix(const char *orig)
492 char *d, *d_end, buf[256];
495 if (!orig) return NULL;
499 d_end = d + sizeof(buf) - 1;
501 for (; (*s != '\0') && (d < d_end); s++, d++)
502 if (_c_id_allowed(*s)) *d = *s;
510 _part_api_name_get(Evas_Object *ed, const char *program)
515 orig = edje_edit_part_api_name_get(ed, program);
516 fix = _api_name_fix(orig);
517 edje_edit_string_free(orig);
523 _parts_parse(Evas_Object *ed)
525 Eina_List *parts, *l;
526 const char *name, *description;
529 Eina_Bool ret = EINA_TRUE;
531 parts = edje_edit_parts_list_get(ed);
532 EINA_LIST_FOREACH(parts, l, name)
534 if (!(apiname = _part_api_name_get(ed, name)))
536 DBG("filter out part '%s': not API.", name);
540 type = edje_edit_part_type_get(ed, name);
541 if ((type != EDJE_PART_TYPE_SWALLOW) &&
542 (type != EDJE_PART_TYPE_TEXT) &&
543 (type != EDJE_PART_TYPE_BOX) &&
544 (type != EDJE_PART_TYPE_TABLE))
550 description = edje_edit_part_api_description_get(ed, name);
551 if (!_part_write(apiname, name, description, type))
554 edje_edit_string_free(description);
559 edje_edit_string_free(description);
563 edje_edit_string_list_free(parts);
568 _program_emit_write(const char *apiname, const char *source, const char *sig,
569 const char *description)
573 snprintf(buf, sizeof(buf), C_CODEGEN_PROGRAM_EMIT, prefix,
574 apiname, sig, source);
575 if (fwrite(buf, strlen(buf), 1, source_fd) != 1)
580 snprintf(buf, sizeof(buf), "\n/**\n * @brief %s\n */\n", description);
581 if (fwrite(buf, strlen(buf), 1, header_fd) != 1)
585 snprintf(buf, sizeof(buf), H_CODEGEN_PROGRAM_EMIT, prefix,
587 if (fwrite(buf, strlen(buf), 1, header_fd) != 1)
593 ERR("Could not write the program: %s", apiname);
598 _program_add_write(const char *apiname, const char *source, const char *sig,
599 const char *description)
603 snprintf(buf, sizeof(buf), C_CODEGEN_PROGRAM_CALLBACK_ADD, prefix,
604 apiname, sig, source);
605 if (fwrite(buf, strlen(buf), 1, source_fd) != 1)
608 snprintf(buf, sizeof(buf), C_CODEGEN_PROGRAM_CALLBACK_DEL, prefix,
609 apiname, sig, source);
610 if (fwrite(buf, strlen(buf), 1, source_fd) != 1)
615 snprintf(buf, sizeof(buf), "\n/**\n * @brief %s\n */\n", description);
616 if (fwrite(buf, strlen(buf), 1, header_fd) != 1)
620 snprintf(buf, sizeof(buf), H_CODEGEN_PROGRAM_CALLBACK_ADD, prefix,
622 if (fwrite(buf, strlen(buf), 1, header_fd) != 1)
625 snprintf(buf, sizeof(buf), H_CODEGEN_PROGRAM_CALLBACK_DEL, prefix,
627 if (fwrite(buf, strlen(buf), 1, header_fd) != 1)
633 ERR("Could not write the program [action]: %s", apiname);
638 _program_api_name_get(Evas_Object *ed, const char *program)
643 orig = edje_edit_program_api_name_get(ed, program);
644 fix = _api_name_fix(orig);
645 edje_edit_string_free(orig);
651 _programs_parse(Evas_Object *ed)
653 Eina_Bool ret = EINA_TRUE;
654 Eina_List *programs, *l;
655 const char *name, *source = NULL, *sig = NULL, *description;
657 Edje_Action_Type type;
659 programs = edje_edit_programs_list_get(ed);
660 EINA_LIST_FOREACH(programs, l, name)
662 if (!(apiname = _program_api_name_get(ed, name)))
664 DBG("filter out program '%s': not API.", name);
668 description = edje_edit_program_api_description_get(ed, name);
669 type = edje_edit_program_action_get(ed, name);
670 if (type == EDJE_ACTION_TYPE_SIGNAL_EMIT)
672 const char *str, *str2;
673 str = edje_edit_program_state_get(ed, name);
674 str2 = edje_edit_program_state2_get(ed, name);
676 if (!_program_add_write(apiname, str2, str, description))
679 edje_edit_string_free(str);
680 edje_edit_string_free(str2);
684 edje_edit_string_free(str);
685 edje_edit_string_free(str2);
688 sig = edje_edit_program_signal_get(ed, name);
689 if (!sig) sig = eina_stringshare_add("");
691 source = edje_edit_program_source_get(ed, name);
692 if (!source) source = eina_stringshare_add("");
696 if (!_program_emit_write(apiname, source, sig, description))
703 edje_edit_string_free(description);
704 edje_edit_string_free(sig);
705 edje_edit_string_free(source);
709 edje_edit_string_list_free(programs);
712 edje_edit_string_free(description);
713 edje_edit_string_free(sig);
714 edje_edit_string_free(source);
727 ed = edje_edit_object_add(ecore_evas_get(ee));
728 if (!edje_object_file_set(ed, file, group))
730 Edje_Load_Error err = edje_object_load_error_get(ed);
731 const char *errmsg = edje_load_error_str(err);
732 ERR("could not load group '%s' from file '%s': %s",
733 group, file, errmsg);
738 ret = _parts_parse(ed) && _programs_parse(ed);
745 main(int argc, char *argv[])
747 Eina_Bool quit_option = EINA_FALSE;
748 char *source = NULL, *header = NULL;
749 int arg_index, ret = 0;
750 Ecore_Getopt_Value values[] = {
751 ECORE_GETOPT_VALUE_STR(prefix),
752 ECORE_GETOPT_VALUE_BOOL(quit_option),
753 ECORE_GETOPT_VALUE_BOOL(quit_option),
754 ECORE_GETOPT_VALUE_BOOL(quit_option),
755 ECORE_GETOPT_VALUE_BOOL(quit_option),
756 ECORE_GETOPT_VALUE_NONE
759 setlocale(LC_NUMERIC, "C");
768 fprintf(stderr, "Missing action. See '--help or -h'.\n");
773 _log_dom = eina_log_domain_register("elementary_codegen", EINA_COLOR_YELLOW);
776 EINA_LOG_CRIT("could not register log domain 'elementary_codegen'");
781 arg_index = ecore_getopt_parse(&optdesc, values, argc, argv);
784 ERR("could not parse arguments.");
788 else if (quit_option) goto error_getopt;
789 else if (arg_index != argc - 4)
791 fprintf(stderr, "Incorrect number of parameters. Requires " \
792 "fours arguments, an edje, the group, " \
793 "the source output (foo.c) and the header(foo.h).\n" \
794 "See %s --help\n", argv[0]);
799 file = argv[arg_index++];
801 // check if the file is accessible
802 if (access(file, R_OK) == -1)
804 ERR("File '%s' not accessible, error %d (%s).\n",
805 file, errno, strerror(errno));
810 group = argv[arg_index++];
811 source = argv[arg_index++];
812 header = argv[arg_index++];
814 if (!edje_file_group_exists(file, group))
816 ERR("The group %s not exists", group);
821 ee = ecore_evas_buffer_new(1, 1);
824 ERR("could not create ecore_evas_buffer");
829 if (!_file_descriptors_open(source, header))
831 ERR("Could not create the source files, error %d (%s)",
832 errno, strerror(errno));
837 if (!_headers_write(header))
839 ERR("Could not write the header, error %d (%s)",
840 errno, strerror(errno));
845 if (!_theme_set_write())
846 WRN("Theme set getter/setter not created. Group name: %s invalid.", group);
850 ERR("Could not parsing the EDJE");
855 if (!_footer_write(header))
857 ERR("Could not write the footer, error %d (%s)",
858 errno, strerror(errno));
863 if (!_file_descriptors_close())
865 ERR("Could not close the source files, error %d (%s)",
866 errno, strerror(errno));
876 ecore_evas_shutdown();
878 eina_log_domain_unregister(_log_dom);