303de703e17247d0226461c05279358adbe712c7
[platform/upstream/edje.git] / src / lib / edje_cache.c
1 #include "edje_private.h"
2
3
4 static Eina_Hash   *_edje_file_hash = NULL;
5 static int          _edje_file_cache_size = 16;
6 static Eina_List   *_edje_file_cache = NULL;
7
8 static int          _edje_collection_cache_size = 16;
9
10 static Edje_Part_Collection *
11 _edje_file_coll_open(Edje_File *edf, const char *coll)
12 {
13    Edje_Part_Collection *edc = NULL;
14    Edje_Part_Collection_Directory_Entry *ce;
15    int id = -1, size = 0;
16    Eina_List *l;
17    char buf[256];
18    char *buffer;
19    void *data;
20
21    ce = eina_hash_find(edf->collection, coll);
22    if (!ce) return NULL;
23
24    if (ce->ref)
25      {
26         ce->ref->references++;
27         return ce->ref;
28      }
29
30    EINA_LIST_FOREACH(edf->collection_cache, l, edc)
31      {
32         if (!strcmp(edc->part, coll))
33           {
34              edc->references = 1;
35              ce->ref = edc;
36
37              edf->collection_cache = eina_list_remove_list(edf->collection_cache, l);
38              return ce->ref;
39           }
40      }
41
42    id = ce->id;
43    if (id < 0) return NULL;
44
45 #define INIT_EMP(Tp, Sz, Ce)                                           \
46    buffer = alloca(strlen(ce->entry) + strlen(#Tp) + 2);               \
47    sprintf(buffer, "%s/%s", ce->entry, #Tp);                           \
48    Ce->mp.Tp = eina_mempool_add("one_big", buffer, NULL, sizeof (Sz), Ce->count.Tp); \
49    _emp_##Tp = Ce->mp.Tp;
50
51 #define INIT_EMP_BOTH(Tp, Sz, Ce)                                       \
52    INIT_EMP(Tp, Sz, Ce)                                                 \
53    Ce->mp_rtl.Tp = eina_mempool_add("one_big", buffer, NULL,            \
54          sizeof (Sz), Ce->count.Tp);
55
56    INIT_EMP_BOTH(RECTANGLE, Edje_Part_Description_Common, ce);
57    INIT_EMP_BOTH(TEXT, Edje_Part_Description_Text, ce);
58    INIT_EMP_BOTH(IMAGE, Edje_Part_Description_Image, ce);
59    INIT_EMP_BOTH(PROXY, Edje_Part_Description_Proxy, ce);
60    INIT_EMP_BOTH(SWALLOW, Edje_Part_Description_Common, ce);
61    INIT_EMP_BOTH(TEXTBLOCK, Edje_Part_Description_Text, ce);
62    INIT_EMP_BOTH(GROUP, Edje_Part_Description_Common, ce);
63    INIT_EMP_BOTH(BOX, Edje_Part_Description_Box, ce);
64    INIT_EMP_BOTH(TABLE, Edje_Part_Description_Table, ce);
65    INIT_EMP_BOTH(EXTERNAL, Edje_Part_Description_External, ce);
66    INIT_EMP_BOTH(SPACER, Edje_Part_Description_Common, ce);
67    INIT_EMP(part, Edje_Part, ce);
68
69    snprintf(buf, sizeof(buf), "edje/collections/%i", id);
70    edc = eet_data_read(edf->ef, _edje_edd_edje_part_collection, buf);
71    if (!edc) return NULL;
72
73    edc->references = 1;
74    edc->part = ce->entry;
75
76    /* For Edje file build with Edje 1.0 */
77    if (edf->version <= 3 && edf->minor <= 1)
78      {
79         /* This will preserve previous rendering */
80         unsigned int i;
81
82         /* people expect signal to not be broadcasted */
83         edc->broadcast_signal = EINA_FALSE;
84
85         /* people expect text.align to be 0.0 0.0 */
86         for (i = 0; i < edc->parts_count; ++i)
87           {
88              if (edc->parts[i]->type == EDJE_PART_TYPE_TEXTBLOCK)
89                {
90                   Edje_Part_Description_Text *text;
91                   unsigned int j;
92
93                   text = (Edje_Part_Description_Text*) edc->parts[i]->default_desc;
94                   text->text.align.x = TO_DOUBLE(0.0);
95                   text->text.align.y = TO_DOUBLE(0.0);
96
97                   for (j = 0; j < edc->parts[i]->other.desc_count; ++j)
98                     {
99                        text =  (Edje_Part_Description_Text*) edc->parts[i]->other.desc[j];
100                        text->text.align.x = TO_DOUBLE(0.0);
101                        text->text.align.y = TO_DOUBLE(0.0);
102                     }
103                }
104           }
105      }
106
107    snprintf(buf, sizeof(buf), "edje/scripts/embryo/compiled/%i", id);
108    data = eet_read(edf->ef, buf, &size);
109
110    if (data)
111      {
112         edc->script = embryo_program_new(data, size);
113         _edje_embryo_script_init(edc);
114         free(data);
115      }
116
117    snprintf(buf, sizeof(buf), "edje/scripts/lua/%i", id);
118    data = eet_read(edf->ef, buf, &size);
119
120    if (data)
121      {
122         _edje_lua2_script_load(edc, data, size);
123         free(data);
124      }
125
126    ce->ref = edc;
127
128    return edc;
129 }
130
131 #ifdef HAVE_EIO
132 static Eina_Bool
133 _edje_file_warn(void *data)
134 {
135    Edje_File *edf = data;
136    Eina_List *l, *ll;
137    Edje *ed;
138
139    edf->references++;
140
141    EINA_LIST_FOREACH(edf->edjes, l, ed)
142      _edje_ref(ed);
143
144    EINA_LIST_FOREACH(edf->edjes, l, ed)
145      {
146         _edje_emit(ed, "edje,change,file", "edje");
147      }
148
149    EINA_LIST_FOREACH_SAFE(edf->edjes, l, ll, ed)
150      _edje_unref(ed);
151
152    edf->references--;
153
154    edf->timeout = NULL;
155    return EINA_FALSE;
156 }
157
158 static Eina_Bool
159 _edje_file_change(void *data, int ev_type __UNUSED__, void *event)
160 {
161    Edje_File *edf = data;
162    Eio_Monitor_Event *ev = event;
163
164    if (ev->monitor == edf->monitor)
165      {
166         if (edf->timeout) ecore_timer_del(edf->timeout);
167         edf->timeout = ecore_timer_add(0.5, _edje_file_warn, edf);
168      }
169    return ECORE_CALLBACK_PASS_ON;
170 }
171 #endif
172
173 static Edje_File *
174 _edje_file_open(const char *file, const char *coll, int *error_ret, Edje_Part_Collection **edc_ret, time_t mtime)
175 {
176    Edje_Color_Class *cc;
177    Edje_File *edf;
178    Eina_List *l;
179    Edje_Part_Collection *edc;
180    Eet_File *ef;
181 #ifdef HAVE_EIO
182    Ecore_Event_Handler *ev;
183 #endif
184
185    ef = eet_open(file, EET_FILE_MODE_READ);
186    if (!ef)
187      {
188         *error_ret = EDJE_LOAD_ERROR_UNKNOWN_FORMAT;
189         return NULL;
190      }
191    edf = eet_data_read(ef, _edje_edd_edje_file, "edje/file");
192    if (!edf)
193      {
194         *error_ret = EDJE_LOAD_ERROR_CORRUPT_FILE;
195         eet_close(ef);
196         return NULL;
197      }
198
199    edf->ef = ef;
200    edf->mtime = mtime;
201 #ifdef HAVE_EIO
202    edf->monitor = eio_monitor_add(file);
203    ev = ecore_event_handler_add(EIO_MONITOR_FILE_DELETED, _edje_file_change, edf);
204    edf->handlers = eina_list_append(edf->handlers, ev);
205    ev = ecore_event_handler_add(EIO_MONITOR_FILE_MODIFIED, _edje_file_change, edf);
206    edf->handlers = eina_list_append(edf->handlers, ev);
207    ev = ecore_event_handler_add(EIO_MONITOR_FILE_CREATED, _edje_file_change, edf);
208    edf->handlers = eina_list_append(edf->handlers, ev);
209    ev = ecore_event_handler_add(EIO_MONITOR_SELF_DELETED, _edje_file_change, edf);
210    edf->handlers = eina_list_append(edf->handlers, ev);
211 #endif
212
213    if (edf->version != EDJE_FILE_VERSION)
214      {
215         *error_ret = EDJE_LOAD_ERROR_INCOMPATIBLE_FILE;
216         _edje_file_free(edf);
217         return NULL;
218      }
219    if (!edf->collection)
220      {
221         *error_ret = EDJE_LOAD_ERROR_CORRUPT_FILE;
222         _edje_file_free(edf);
223         return NULL;
224      }
225
226    if (edf->minor > EDJE_FILE_MINOR)
227      {
228         WRN("`%s` may use feature from a newer edje and could not show up as expected.", file);
229      }
230
231    edf->path = eina_stringshare_add(file);
232    edf->references = 1;
233
234    /* This should be done at edje generation time */
235    _edje_textblock_style_parse_and_fix(edf);
236    edf->color_hash = eina_hash_string_small_new(NULL);
237    EINA_LIST_FOREACH(edf->color_classes, l, cc)
238      if (cc->name)
239        eina_hash_direct_add(edf->color_hash, cc->name, cc);
240
241    if (coll)
242      {
243         edc = _edje_file_coll_open(edf, coll);
244         if (!edc)
245           {
246              *error_ret = EDJE_LOAD_ERROR_UNKNOWN_COLLECTION;
247           }
248         if (edc_ret) *edc_ret = edc;
249      }
250
251    return edf;
252 }
253
254 static void
255 _edje_file_dangling(Edje_File *edf)
256 {
257    if (edf->dangling) return;
258    edf->dangling = EINA_TRUE;
259
260    eina_hash_del(_edje_file_hash, edf->path, edf);
261    if (!eina_hash_population(_edje_file_hash))
262      {
263        eina_hash_free(_edje_file_hash);
264        _edje_file_hash = NULL;
265      }
266 }
267
268 Edje_File *
269 _edje_cache_file_coll_open(const char *file, const char *coll, int *error_ret, Edje_Part_Collection **edc_ret, Edje *ed)
270 {
271    Edje_File *edf;
272    Eina_List *l, *hist;
273    Edje_Part_Collection *edc;
274    Edje_Part *ep;
275    struct stat st;
276
277    if (stat(file, &st) != 0)
278       return NULL;
279
280    if (!_edje_file_hash)
281      {
282         _edje_file_hash = eina_hash_string_small_new(NULL);
283         goto open_new;
284      }
285
286    edf = eina_hash_find(_edje_file_hash, file);
287    if (edf)
288      {
289         if (edf->mtime != st.st_mtime)
290           {
291              _edje_file_dangling(edf);
292              goto open_new;
293           }
294
295         edf->references++;
296         goto open;
297      }
298
299    EINA_LIST_FOREACH(_edje_file_cache, l, edf)
300      {
301         if (!strcmp(edf->path, file))
302           {
303              if (edf->mtime != st.st_mtime)
304                {
305                   _edje_file_cache = eina_list_remove_list(_edje_file_cache, l);
306                   _edje_file_free(edf);
307                   goto open_new;
308                }
309
310              edf->references = 1;
311              _edje_file_cache = eina_list_remove_list(_edje_file_cache, l);
312              eina_hash_add(_edje_file_hash, file, edf);
313              goto open;
314           }
315      }
316
317 open_new:
318    if (!_edje_file_hash)
319       _edje_file_hash = eina_hash_string_small_new(NULL);
320
321    edf = _edje_file_open(file, coll, error_ret, edc_ret, st.st_mtime);
322    if (!edf)
323       return NULL;
324
325 #ifdef HAVE_EIO
326    if (ed) edf->edjes = eina_list_append(edf->edjes, ed);
327 #else
328    (void) ed;
329 #endif
330
331    eina_hash_add(_edje_file_hash, file, edf);
332    return edf;
333
334 open:
335    if (!coll)
336       return edf;
337
338    edc = _edje_file_coll_open(edf, coll);
339    if (!edc)
340      {
341         *error_ret = EDJE_LOAD_ERROR_UNKNOWN_COLLECTION;
342      }
343    else
344      {
345         if (!edc->checked)
346           {
347              unsigned int j;
348
349              for (j = 0; j < edc->parts_count; ++j)
350                {
351                   Edje_Part *ep2;
352
353                   ep = edc->parts[j];
354
355                   /* Register any color classes in this parts descriptions. */
356                   hist = NULL;
357                   hist = eina_list_append(hist, ep);
358                   ep2 = ep;
359                   while (ep2->dragable.confine_id >= 0)
360                     {
361                        if (ep2->dragable.confine_id >= (int) edc->parts_count)
362                          {
363                             ERR("confine_to above limit. invalidating it.");
364                             ep2->dragable.confine_id = -1;
365                             break;
366                          }
367
368                        ep2 = edc->parts[ep2->dragable.confine_id];
369                        if (eina_list_data_find(hist, ep2))
370                          {
371                             ERR("confine_to loops. invalidating loop.");
372                             ep2->dragable.confine_id = -1;
373                             break;
374                          }
375                        hist = eina_list_append(hist, ep2);
376                     }
377                   eina_list_free(hist);
378                   hist = NULL;
379                   hist = eina_list_append(hist, ep);
380                   ep2 = ep;
381                   while (ep2->dragable.event_id >= 0)
382                     {
383                        Edje_Part* prev;
384
385                        if (ep2->dragable.event_id >= (int) edc->parts_count)
386                          {
387                             ERR("event_id above limit. invalidating it.");
388                             ep2->dragable.event_id = -1;
389                             break;
390                          }
391                        prev = ep2;
392
393                        ep2 = edc->parts[ep2->dragable.event_id];
394                        if (!ep2->dragable.x && !ep2->dragable.y)
395                          {
396                             prev->dragable.event_id = -1;
397                             break;
398                          }
399
400                        if (eina_list_data_find(hist, ep2))
401                          {
402                             ERR("events_to loops. invalidating loop.");
403                             ep2->dragable.event_id = -1;
404                             break;
405                          }
406                        hist = eina_list_append(hist, ep2);
407                     }
408                   eina_list_free(hist);
409                   hist = NULL;
410                   hist = eina_list_append(hist, ep);
411                   ep2 = ep;
412                   while (ep2->clip_to_id >= 0)
413                     {
414                        if (ep2->clip_to_id >= (int) edc->parts_count)
415                          {
416                             ERR("clip_to_id above limit. invalidating it.");
417                             ep2->clip_to_id = -1;
418                             break;
419                          }
420
421                        ep2 = edc->parts[ep2->clip_to_id];
422                        if (eina_list_data_find(hist, ep2))
423                          {
424                             ERR("clip_to loops. invalidating loop.");
425                             ep2->clip_to_id = -1;
426                             break;
427                          }
428                        hist = eina_list_append(hist, ep2);
429                     }
430                   eina_list_free(hist);
431                   hist = NULL;
432                }
433             edc->checked = 1;
434           }
435      }
436 #ifdef HAVE_EIO
437    if (edc && ed) edf->edjes = eina_list_append(edf->edjes, ed);
438 #else
439    (void) ed;
440 #endif
441
442    if (edc_ret) *edc_ret = edc;
443
444    return edf;
445 }
446
447 void
448 _edje_cache_coll_clean(Edje_File *edf)
449 {
450    while ((edf->collection_cache) &&
451           (eina_list_count(edf->collection_cache) > (unsigned int) _edje_collection_cache_size))
452      {
453         Edje_Part_Collection_Directory_Entry *ce;
454         Edje_Part_Collection *edc;
455
456         edc = eina_list_data_get(eina_list_last(edf->collection_cache));
457         edf->collection_cache = eina_list_remove_list(edf->collection_cache, eina_list_last(edf->collection_cache));
458
459         ce = eina_hash_find(edf->collection, edc->part);
460         _edje_collection_free(edf, edc, ce);
461      }
462 }
463
464 void
465 _edje_cache_coll_flush(Edje_File *edf)
466 {
467    while (edf->collection_cache)
468      {
469         Edje_Part_Collection_Directory_Entry *ce;
470         Edje_Part_Collection *edc;
471         Eina_List *last;
472
473         last = eina_list_last(edf->collection_cache);
474         edc = eina_list_data_get(last);
475         edf->collection_cache = eina_list_remove_list(edf->collection_cache,
476                                                       last);
477
478         ce = eina_hash_find(edf->collection, edc->part);
479         _edje_collection_free(edf, edc, ce);
480      }
481 }
482
483 void
484 _edje_cache_coll_unref(Edje_File *edf, Edje_Part_Collection *edc)
485 {
486    Edje_Part_Collection_Directory_Entry *ce;
487
488    edc->references--;
489    if (edc->references != 0) return;
490
491    ce = eina_hash_find(edf->collection, edc->part);
492    if (!ce)
493      {
494         ERR("Something is wrong with reference count of '%s'.", edc->part);
495      }
496    else if (ce->ref)
497      {
498         ce->ref = NULL;
499
500         if (edf->dangling)
501           {
502              /* No need to keep the collection around if the file is dangling */
503              _edje_collection_free(edf, edc, ce);
504              _edje_cache_coll_flush(edf);
505           }
506         else
507           {
508              edf->collection_cache = eina_list_prepend(edf->collection_cache, edc);
509              _edje_cache_coll_clean(edf);
510           }
511      }
512 }
513
514 static void
515 _edje_cache_file_clean(void)
516 {
517    int count;
518
519    count = eina_list_count(_edje_file_cache);
520    while ((_edje_file_cache) && (count > _edje_file_cache_size))
521      {
522         Eina_List *last;
523         Edje_File *edf;
524
525         last = eina_list_last(_edje_file_cache);
526         edf = eina_list_data_get(last);
527         _edje_file_cache = eina_list_remove_list(_edje_file_cache, last);
528         _edje_file_free(edf);
529         count = eina_list_count(_edje_file_cache);
530      }
531 }
532
533 void
534 _edje_cache_file_unref(Edje_File *edf)
535 {
536    edf->references--;
537    if (edf->references != 0) return;
538
539    if (edf->dangling)
540      {
541         _edje_file_free(edf);
542         return;
543      }
544
545    eina_hash_del(_edje_file_hash, edf->path, edf);
546    if (!eina_hash_population(_edje_file_hash))
547      {
548        eina_hash_free(_edje_file_hash);
549        _edje_file_hash = NULL;
550      }
551    _edje_file_cache = eina_list_prepend(_edje_file_cache, edf);
552    _edje_cache_file_clean();
553 }
554
555 void
556 _edje_file_cache_shutdown(void)
557 {
558    edje_file_cache_flush();
559 }
560
561
562 /*============================================================================*
563  *                                 Global                                     *
564  *============================================================================*/
565
566 /*============================================================================*
567  *                                   API                                      *
568  *============================================================================*/
569
570
571 EAPI void
572 edje_file_cache_set(int count)
573 {
574    if (count < 0) count = 0;
575    _edje_file_cache_size = count;
576    _edje_cache_file_clean();
577 }
578
579
580 EAPI int
581 edje_file_cache_get(void)
582 {
583    return _edje_file_cache_size;
584 }
585
586
587 EAPI void
588 edje_file_cache_flush(void)
589 {
590    int ps;
591
592    ps = _edje_file_cache_size;
593    _edje_file_cache_size = 0;
594    _edje_cache_file_clean();
595    _edje_file_cache_size = ps;
596 }
597
598
599 EAPI void
600 edje_collection_cache_set(int count)
601 {
602    Eina_List *l;
603    Edje_File *edf;
604
605    if (count < 0) count = 0;
606    _edje_collection_cache_size = count;
607    EINA_LIST_FOREACH(_edje_file_cache, l, edf)
608      _edje_cache_coll_clean(edf);
609    /* FIXME: freach in file hash too! */
610 }
611
612
613 EAPI int
614 edje_collection_cache_get(void)
615 {
616    return _edje_collection_cache_size;
617 }
618
619
620 EAPI void
621 edje_collection_cache_flush(void)
622 {
623    int ps;
624    Eina_List *l;
625    Edje_File *edf;
626
627    ps = _edje_collection_cache_size;
628    _edje_collection_cache_size = 0;
629    EINA_LIST_FOREACH(_edje_file_cache, l, edf)
630      _edje_cache_coll_flush(edf);
631    /* FIXME: freach in file hash too! */
632    _edje_collection_cache_size = ps;
633 }