Imported Upstream version 1.7.1
[platform/upstream/edje.git] / src / bin / edje_watch.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #include <Eina.h>
6 #include <Ecore.h>
7 #include <Eio.h>
8 #ifdef HAVE_EVIL
9 # include <Evil.h>
10 #endif
11
12 char watchfile[PATH_MAX];
13 char *edje_cc_command = NULL;
14 Eina_List *watching = NULL;
15 Ecore_Timer *timeout = NULL;
16
17 static void
18 read_watch_file(const char *file)
19 {
20    Eina_File *f;
21    Eina_Iterator *it;
22    Eina_File_Line *ln;
23    Eio_Monitor *mon;
24    Eina_List *r = NULL;
25
26    f = eina_file_open(file, EINA_FALSE);
27    if (!f) return ;
28
29    it = eina_file_map_lines(f);
30    if (!it) goto err;
31
32    EINA_ITERATOR_FOREACH(it, ln)
33      {
34         const char *path;
35
36         path = eina_stringshare_add_length(ln->start, ln->length);
37         r = eina_list_append(r, eio_monitor_add(path));
38         eina_stringshare_del(path);
39      }
40    eina_iterator_free(it);
41
42    EINA_LIST_FREE(watching, mon)
43      eio_monitor_del(mon);
44    watching = r;
45
46  err:
47    eina_file_close(f);
48 }
49
50 Eina_Bool
51 rebuild(void *data __UNUSED__)
52 {
53    double start, end;
54
55    start = ecore_time_get();
56    fprintf(stderr, "SYSTEM('%s')\n", edje_cc_command);
57    if (system(edje_cc_command) == 0)
58      read_watch_file(watchfile);
59    end = ecore_time_get();
60    fprintf(stderr, "DONE IN %f\n", end - start);
61
62    timeout = NULL;
63    return EINA_FALSE;
64 }
65
66 Eina_Bool
67 some_change(void *data __UNUSED__, int type __UNUSED__, void *event __UNUSED__)
68 {
69    Eio_Monitor_Event *ev = event;
70
71    fprintf(stderr, "EVENT %i on [%s]\n", type, ev->filename);
72    if (timeout) ecore_timer_del(timeout);
73    timeout = ecore_timer_add(0.5, rebuild, NULL);
74
75    return ECORE_CALLBACK_PASS_ON;
76 }
77
78 int
79 main(int argc, char **argv)
80 {
81    char *watchout;
82    Eina_Strbuf *buf;
83    double start, end;
84    int tfd;
85    int i;
86
87    eina_init();
88    ecore_init();
89    eio_init();
90
91    if (argc < 2) return -1;
92
93    ecore_event_handler_add(EIO_MONITOR_FILE_MODIFIED, some_change, NULL);
94    ecore_event_handler_add(EIO_MONITOR_FILE_CREATED, some_change, NULL);
95    ecore_event_handler_add(EIO_MONITOR_FILE_DELETED, some_change, NULL);
96    ecore_event_handler_add(EIO_MONITOR_FILE_CLOSED, some_change, NULL);
97
98 #ifdef HAVE_EVIL
99    watchout = (char *)evil_tmpdir_get();
100 #else
101    watchout = "/tmp";
102 #endif
103
104    snprintf(watchfile, PATH_MAX, "%s/edje_watch-tmp-XXXXXX", watchout);
105
106    tfd = mkstemp(watchfile);
107    if (tfd < 0) return -1;
108    close(tfd);
109
110    buf = eina_strbuf_new();
111    if (!buf) return -1;
112
113    eina_strbuf_append_printf(buf, "%s/edje_cc -threads -fastcomp -w %s ", PACKAGE_BIN_DIR, watchfile);
114    for (i = 1; i < argc; ++i)
115      eina_strbuf_append_printf(buf, "%s ", argv[i]);
116
117    edje_cc_command = eina_strbuf_string_steal(buf);
118
119    eina_strbuf_free(buf);
120
121    start = ecore_time_get();
122    fprintf(stderr, "SYSTEM('%s')\n", edje_cc_command);
123    system(edje_cc_command);
124    read_watch_file(watchfile);
125    end = ecore_time_get();
126    fprintf(stderr, "DONE %f\n", end - start);
127
128    ecore_main_loop_begin();
129
130    unlink(watchfile);
131
132    eio_shutdown();
133    ecore_shutdown();
134    eina_shutdown();
135
136    return 1;
137 }