1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4 
5 #include <Eina.h>
6 #include <fnmatch.h>
7 
8 #include <Eo.h>
9 
10 #include "eo_internal.h"
11 
12 #include "eo_ptr_indirection.h"
13 #include "eo_private.h"
14 #include "eina_promise_private.h"
15 
16 EAPI const Efl_Event_Description _EFL_EVENT_CALLBACK_ADD =
17     EFL_EVENT_DESCRIPTION_HOT("callback,add");
18 
19 EAPI const Efl_Event_Description _EFL_EVENT_CALLBACK_DEL =
20     EFL_EVENT_DESCRIPTION_HOT("callback,del");
21 
22 static int event_freeze_count = 0;
23 
24 typedef struct _Eo_Callback_Description  Eo_Callback_Description;
25 typedef struct _Efl_Event_Callback_Frame Efl_Event_Callback_Frame;
26 typedef struct _Efl_Event_Forwarder Efl_Event_Forwarder;
27 
28 struct _Efl_Event_Forwarder
29 {
30    const Efl_Event_Description *desc;
31    Eo *source;
32    Eo *new_obj;
33 
34    short priority;
35 
36    Eina_Bool inserted : 1;
37 };
38 
39 struct _Efl_Event_Callback_Frame
40 {
41    const Efl_Event_Description    *desc;
42    Efl_Event_Callback_Frame *next;
43    unsigned int              idx;
44    unsigned int              inserted_before;
45    unsigned short            generation;
46 };
47 
48 typedef struct
49 {
50    const char                *name;
51    const char                *comment;
52    Eo                        *composite_parent;
53    Eina_Inlist               *generic_data;
54    Eo                      ***wrefs;
55    Eina_Hash                 *providers;
56    Eina_Hash                 *schedulers;
57    Eina_Hash                 *forwarders;
58 } Efl_Object_Extension;
59 
60 #define EFL_OBJECT_EVENT_CALLBACK(Event) Eina_Bool event_cb_##Event : 1;
61 
62 struct _Efl_Object_Data
63 {
64    Eina_Inlist               *children;
65    Eo                        *parent;
66 
67    Efl_Object_Extension      *ext;
68 
69    Efl_Event_Callback_Frame  *event_frame;
70    Eo_Callback_Description  **callbacks;
71 #ifdef EFL64
72    uint64_t                   callbacks_mask;
73 #else
74    uint32_t                   callbacks_mask;
75 #endif
76    Eina_Inlist               *pending_futures;
77    unsigned int               callbacks_count;
78 
79    unsigned short             event_freeze_count;
80 
81    EFL_OBJECT_EVENT_CALLBACK(EFL_EVENT_CALLBACK_ADD);
82    EFL_OBJECT_EVENT_CALLBACK(EFL_EVENT_CALLBACK_DEL);
83    EFL_OBJECT_EVENT_CALLBACK(EFL_EVENT_DEL);
84    EFL_OBJECT_EVENT_CALLBACK(EFL_EVENT_NOREF);
85 
86    EFL_OBJECT_EVENT_CALLBACK(EFL_EVENT_INVALIDATE);
87    EFL_OBJECT_EVENT_CALLBACK(EFL_EVENT_DESTRUCT); // No proper count: minor optimization triggered at destruction only
88    Eina_Bool                  callback_stopped : 1;
89    Eina_Bool                  need_cleaning : 1;
90 
91    Eina_Bool                  allow_parent_unref : 1; // Allows unref to zero even with a parent
92 };
93 
94 typedef enum
95 {
96    DATA_PTR,
97    DATA_OBJ,
98    DATA_OBJ_WEAK,
99    DATA_VAL
100 } Eo_Generic_Data_Node_Type;
101 
102 typedef struct
103 {
104    EINA_INLIST;
105    const Eo                  *obj;
106    Eina_Stringshare          *key;
107    union {
108         Eina_Value           *val;
109         Eo                   *obj;
110         void                 *ptr;
111    } d;
112    Eo_Generic_Data_Node_Type  d_type;
113 } Eo_Generic_Data_Node;
114 
115 typedef struct _Efl_Future_Pending
116 {
117    EINA_INLIST;
118    const Eo *o;
119    Eina_Future *future;
120    Efl_Future_Cb_Desc desc;
121 } Efl_Future_Pending;
122 
123 
124 typedef struct
125 {
126    EINA_INLIST;
127    const Efl_Event_Description *desc;
128    unsigned int current;
129 } Eo_Current_Callback_Description;
130 
131 #define EVENT_STACK_PUSH(pd, fr) do { \
132    (fr)->next = (pd)->event_frame; \
133    (pd)->event_frame = (fr); \
134 } while (0)
135 #define EVENT_STACK_POP(pd) do { \
136    if ((pd)->event_frame) (pd)->event_frame = (pd)->event_frame->next; \
137 } while (0)
138 
139 static void _efl_event_forwarder_callback(void *data, const Efl_Event *event);
140 
141 static int _eo_nostep_alloc = -1;
142 
143 static void
_efl_pending_futures_clear(Efl_Object_Data * pd)144 _efl_pending_futures_clear(Efl_Object_Data *pd)
145 {
146    while (pd->pending_futures)
147      {
148         Efl_Future_Pending *pending = EINA_INLIST_CONTAINER_GET(pd->pending_futures, Efl_Future_Pending);
149         Eina_Future *future = *pending->desc.storage;
150         assert(future);
151         eina_future_cancel(future);
152      }
153 }
154 
155 static inline void
_efl_object_extension_free(Efl_Object_Extension * ext)156 _efl_object_extension_free(Efl_Object_Extension *ext)
157 {
158    eina_freeq_ptr_main_add(ext, free, sizeof(*ext));
159 }
160 
161 static inline Efl_Object_Extension *
_efl_object_extension_need(Efl_Object_Data * pd)162 _efl_object_extension_need(Efl_Object_Data *pd)
163 {
164    if (!pd->ext) pd->ext = calloc(1, sizeof(Efl_Object_Extension));
165    return pd->ext;
166 }
167 
168 static inline void
_efl_object_extension_noneed(Efl_Object_Data * pd)169 _efl_object_extension_noneed(Efl_Object_Data *pd)
170 {
171    Efl_Object_Extension *ext = pd->ext;
172    if ((!ext) ||
173        (ext->name) ||
174        (ext->comment) ||
175        (ext->generic_data) ||
176        (ext->wrefs) ||
177        (ext->composite_parent) ||
178        (ext->providers) ||
179        (ext->schedulers) ||
180        (ext->forwarders)) return;
181    _efl_object_extension_free(pd->ext);
182    pd->ext = NULL;
183 }
184 
185 static void
_efl_object_invalidate(Eo * obj_id,Efl_Object_Data * pd)186 _efl_object_invalidate(Eo *obj_id, Efl_Object_Data *pd)
187 {
188    _efl_pending_futures_clear(pd);
189 
190    if (pd->ext && pd->ext->forwarders)
191      {
192         eina_hash_free(pd->ext->forwarders);
193         pd->ext->forwarders = NULL;
194         _efl_object_extension_noneed(pd);
195      }
196 
197    if (pd->ext && pd->ext->providers)
198      {
199         eina_hash_free(pd->ext->providers);
200         pd->ext->providers = NULL;
201         _efl_object_extension_noneed(pd);
202      }
203    if (pd->ext && pd->ext->schedulers)
204      {
205         eina_hash_free(pd->ext->schedulers);
206         pd->ext->schedulers = NULL;
207         _efl_object_extension_noneed(pd);
208      }
209 
210    EO_OBJ_POINTER_RETURN(obj_id, obj);
211 
212    // Finally invalidate itself if it wasn't done already
213    // I am not sure this is a good idea, but it force the
214    // behavior of calling directly efl_invalidate to be the
215    // same as efl_parent_set(NULL).
216    if (!obj->is_invalidating)
217      efl_parent_set(obj_id, NULL);
218 
219    EO_OBJ_DONE(obj_id);
220 }
221 
222 // Generate the invalidate event in all case and make sure it happens
223 // before any user code can change the children invalidate state. This
224 // make sure that the entire tree of object is valid at the time of
225 // the invalidate event.
226 void
_efl_invalidate(_Eo_Object * obj)227 _efl_invalidate(_Eo_Object *obj)
228 {
229    Eina_Array stash = { 0 };
230    Efl_Object_Data *pd;
231    _Eo_Object *child;
232    Eo *id;
233 
234    if (obj->is_invalidating) return ;
235    obj->is_invalidating = EINA_TRUE;
236    if (obj->invalidate) return;
237 
238    id = _eo_obj_id_get(obj);
239 
240    pd = efl_data_scope_get(id, EFL_OBJECT_CLASS);
241    if (pd->event_cb_EFL_EVENT_INVALIDATE)
242      efl_event_callback_call(id, EFL_EVENT_INVALIDATE, NULL);
243 
244    efl_invalidate(id);
245 
246    eina_array_step_set(&stash, sizeof (stash), 4);
247 
248    // Invalidate all children too
249    EINA_INLIST_FOREACH(pd->children, child)
250      eina_array_push(&stash, _efl_ref(child));
251 
252    while ((child = eina_array_pop(&stash)))
253      {
254         Eo *child_id = _eo_obj_id_get(child);
255 
256         efl_parent_set(child_id, NULL);
257         _efl_unref(child);
258      }
259 
260    eina_array_flush(&stash);
261 
262    obj->invalidate = EINA_TRUE;
263 }
264 
265 static void _key_generic_cb_del(void *data, const Efl_Event *event);
266 
267 static void
_eo_generic_data_node_free(Eo_Generic_Data_Node * node)268 _eo_generic_data_node_free(Eo_Generic_Data_Node *node)
269 {
270    switch (node->d_type)
271      {
272       case DATA_PTR:
273         break;
274       case DATA_OBJ:
275         // FIXME: should this use "destruct" event instead?
276         efl_event_callback_del(node->d.obj, EFL_EVENT_DEL, _key_generic_cb_del, node);
277         efl_unref(node->d.obj);
278         break;
279       case DATA_OBJ_WEAK:
280         // FIXME: should this use "destruct" event instead?
281         efl_event_callback_del(node->d.obj, EFL_EVENT_DEL, _key_generic_cb_del, node);
282         break;
283       case DATA_VAL:
284         eina_value_free(node->d.val);
285         break;
286      }
287    eina_stringshare_del(node->key);
288    eina_freeq_ptr_main_add(node, free, sizeof(*node));
289 }
290 
291 static void
_eo_generic_data_del_all(Eo * obj EINA_UNUSED,Efl_Object_Data * pd)292 _eo_generic_data_del_all(Eo *obj EINA_UNUSED, Efl_Object_Data *pd)
293 {
294    Eo_Generic_Data_Node *node;
295    Efl_Object_Extension *ext = pd->ext;
296 
297    if (!ext) return;
298 
299    while (ext->generic_data)
300      {
301         node = (Eo_Generic_Data_Node *)ext->generic_data;
302         ext->generic_data = eina_inlist_remove(ext->generic_data,
303               EINA_INLIST_GET(node));
304 
305         _eo_generic_data_node_free(node);
306      }
307 }
308 
309 static void
_eo_key_generic_direct_del(Efl_Object_Data * pd,Eo_Generic_Data_Node * node,Eina_Bool call_free)310 _eo_key_generic_direct_del(Efl_Object_Data *pd, Eo_Generic_Data_Node *node, Eina_Bool call_free)
311 {
312    Efl_Object_Extension *ext = pd->ext;
313 
314    ext->generic_data = eina_inlist_remove
315      (ext->generic_data, EINA_INLIST_GET(node));
316    if (call_free) _eo_generic_data_node_free(node);
317 }
318 
319 static void
_eo_key_generic_del(const Eo * obj EINA_UNUSED,Efl_Object_Data * pd,const char * key,Eina_Bool call_free)320 _eo_key_generic_del(const Eo *obj EINA_UNUSED, Efl_Object_Data *pd, const char *key, Eina_Bool call_free)
321 {
322    Eo_Generic_Data_Node *node;
323    Efl_Object_Extension *ext = pd->ext;
324 
325    EINA_INLIST_FOREACH(ext->generic_data, node)
326      {
327         if (!strcmp(node->key, key))
328           {
329              ext->generic_data = eina_inlist_remove
330                (ext->generic_data, EINA_INLIST_GET(node));
331              if (call_free) _eo_generic_data_node_free(node);
332              return;
333           }
334      }
335 }
336 
337 /* Return TRUE if the object was newly added. */
338 static Eo_Generic_Data_Node *
_key_generic_set(const Eo * obj,Efl_Object_Data * pd,const char * key,const void * data,Eo_Generic_Data_Node_Type d_type,Eina_Bool call_free)339 _key_generic_set(const Eo *obj, Efl_Object_Data *pd, const char *key, const void *data, Eo_Generic_Data_Node_Type d_type, Eina_Bool call_free)
340 {
341    Eo_Generic_Data_Node *node;
342    Efl_Object_Extension *ext = pd->ext;
343 
344    if (!key) return NULL;
345    if (ext)
346      {
347         if (!data)
348           {
349              _eo_key_generic_del(obj, pd, key, call_free);
350              return NULL;
351           }
352         EINA_INLIST_FOREACH(ext->generic_data, node)
353           {
354              if (!strcmp(node->key, key))
355                {
356                   if ((node->d_type == d_type) && (node->d.ptr == data))
357                      return NULL;
358                   ext->generic_data = eina_inlist_remove
359                     (ext->generic_data, EINA_INLIST_GET(node));
360                   _eo_generic_data_node_free(node);
361                   break;
362                }
363           }
364      }
365 
366    ext = _efl_object_extension_need(pd);
367    if (ext)
368      {
369         node = calloc(1, sizeof(Eo_Generic_Data_Node));
370         if (!node) return NULL;
371         node->obj = obj;
372         node->key = eina_stringshare_add(key);
373         node->d.ptr = (void *) data;
374         node->d_type = d_type;
375         ext->generic_data = eina_inlist_prepend
376           (ext->generic_data, EINA_INLIST_GET(node));
377         return node;
378      }
379 
380    return NULL;
381 }
382 
383 static void *
_key_generic_get(const Eo * obj,Efl_Object_Data * pd,const char * key,Eo_Generic_Data_Node_Type d_type)384 _key_generic_get(const Eo *obj, Efl_Object_Data *pd, const char *key, Eo_Generic_Data_Node_Type d_type)
385 {
386    Eo_Generic_Data_Node *node;
387    Efl_Object_Extension *ext = pd->ext;
388 
389    if (!key) return NULL;
390    if (!ext) return NULL;
391    EINA_INLIST_FOREACH(ext->generic_data, node)
392      {
393         if (node->key && !strcmp(node->key, key))
394           {
395              if (node->d_type == d_type)
396                {
397                   ext->generic_data = eina_inlist_promote
398                     (ext->generic_data, EINA_INLIST_GET(node));
399                   return node->d.ptr;
400                }
401              else
402                {
403                   ERR("Object %p key '%s' asked for %d but is %d'",
404                       obj, key, d_type, node->d_type);
405                   return NULL;
406                }
407           }
408      }
409    return NULL;
410 }
411 
412 static void
_key_generic_cb_del(void * data,const Efl_Event * event EINA_UNUSED)413 _key_generic_cb_del(void *data, const Efl_Event *event EINA_UNUSED)
414 {
415    Eo_Generic_Data_Node *node = data;
416    Efl_Object_Data *pd = efl_data_scope_get(node->obj, EFL_OBJECT_CLASS);
417    _eo_key_generic_direct_del(pd, node, EINA_TRUE);
418 }
419 
420 EOLIAN static void
_efl_object_key_data_set(Eo * obj,Efl_Object_Data * pd,const char * key,const void * data)421 _efl_object_key_data_set(Eo *obj, Efl_Object_Data *pd, const char *key, const void *data)
422 {
423    _key_generic_set(obj, pd, key, data, DATA_PTR, EINA_TRUE);
424 }
425 
426 EOAPI EFL_VOID_FUNC_BODYV(efl_key_data_set, EFL_FUNC_CALL(key, data),
427                           const char *key, const void *data);
428 
429 EOLIAN static void *
_efl_object_key_data_get(Eo * obj,Efl_Object_Data * pd,const char * key)430 _efl_object_key_data_get(Eo *obj, Efl_Object_Data *pd, const char *key)
431 {
432    return _key_generic_get(obj, pd, key, DATA_PTR);
433 }
434 
435 EOAPI EFL_FUNC_BODYV_CONST(efl_key_data_get, void *, NULL, EFL_FUNC_CALL(key),
436                            const char *key);
437 
438 EOLIAN static void
_efl_object_key_ref_set(Eo * obj EINA_UNUSED,Efl_Object_Data * pd,const char * key,const Eo * objdata)439 _efl_object_key_ref_set(Eo *obj EINA_UNUSED, Efl_Object_Data *pd, const char *key, const Eo *objdata)
440 {
441    Eo_Generic_Data_Node *node;
442 
443    if (!_eo_id_domain_compatible(obj, objdata)) return;
444    node = _key_generic_set(obj, pd, key, objdata, DATA_OBJ, EINA_TRUE);
445    if (node)
446      {
447         efl_ref(objdata);
448         efl_event_callback_add((Eo *)objdata, EFL_EVENT_DEL, _key_generic_cb_del, node);
449      }
450 }
451 
452 EOAPI EFL_VOID_FUNC_BODYV(efl_key_ref_set, EFL_FUNC_CALL(key, objdata),
453                           const char *key, const Efl_Object *objdata);
454 
455 EOLIAN static Eo *
_efl_object_key_ref_get(Eo * obj,Efl_Object_Data * pd,const char * key)456 _efl_object_key_ref_get(Eo *obj, Efl_Object_Data *pd, const char *key)
457 {
458    return _key_generic_get(obj, pd, key, DATA_OBJ);
459 }
460 
461 EOAPI EFL_FUNC_BODYV_CONST(efl_key_ref_get, Efl_Object *, NULL,
462                            EFL_FUNC_CALL(key), const char *key);
463 
464 EOLIAN static void
_efl_object_key_wref_set(Eo * obj,Efl_Object_Data * pd,const char * key,const Efl_Object * objdata)465 _efl_object_key_wref_set(Eo *obj, Efl_Object_Data *pd, const char * key, const Efl_Object *objdata)
466 {
467    Eo_Generic_Data_Node *node;
468 
469    if (!_eo_id_domain_compatible(obj, objdata)) return;
470    node = _key_generic_set(obj, pd, key, objdata, DATA_OBJ_WEAK, EINA_TRUE);
471    if (node)
472      {
473         efl_event_callback_add((Eo *)objdata, EFL_EVENT_DEL, _key_generic_cb_del, node);
474      }
475 }
476 
477 EOAPI EFL_VOID_FUNC_BODYV(efl_key_wref_set, EFL_FUNC_CALL(key, objdata),
478                           const char *key, const Efl_Object *objdata);
479 
480 EOLIAN static Eo *
_efl_object_key_wref_get(Eo * obj,Efl_Object_Data * pd,const char * key)481 _efl_object_key_wref_get(Eo *obj, Efl_Object_Data *pd, const char * key)
482 {
483    return _key_generic_get(obj, pd, key, DATA_OBJ_WEAK);
484 }
485 
486 EOAPI EFL_FUNC_BODYV_CONST(efl_key_wref_get, Efl_Object *, NULL,
487                            EFL_FUNC_CALL(key), const char *key);
488 
489 EOLIAN static void
_efl_object_key_value_set(Eo * obj EINA_UNUSED,Efl_Object_Data * pd,const char * key,Eina_Value * value)490 _efl_object_key_value_set(Eo *obj EINA_UNUSED, Efl_Object_Data *pd, const char *key, Eina_Value *value)
491 {
492    _key_generic_set(obj, pd, key, value, DATA_VAL, EINA_TRUE);
493 }
494 
495 EOAPI EFL_VOID_FUNC_BODYV(efl_key_value_set, EFL_FUNC_CALL(key, value),
496                           const char *key, Eina_Value *value);
497 
498 EOLIAN static Eina_Value *
_efl_object_key_value_get(Eo * obj,Efl_Object_Data * pd,const char * key)499 _efl_object_key_value_get(Eo *obj, Efl_Object_Data *pd, const char *key)
500 {
501    return _key_generic_get(obj, pd, key, DATA_VAL);
502 }
503 
504 EOAPI EFL_FUNC_BODYV_CONST(efl_key_value_get, Eina_Value *, NULL,
505                            EFL_FUNC_CALL(key), const char *key);
506 
507 EOLIAN static void
_efl_object_name_set(Eo * obj EINA_UNUSED,Efl_Object_Data * pd,const char * name)508 _efl_object_name_set(Eo *obj EINA_UNUSED, Efl_Object_Data *pd, const char *name)
509 {
510    if ((name) && (!name[0])) name = NULL;
511    if (name)
512      {
513         _efl_object_extension_need(pd);
514         if (pd->ext) eina_stringshare_replace(&(pd->ext->name), name);
515      }
516    else
517      {
518         if (!pd->ext) return;
519         if (pd->ext->name)
520           {
521              eina_stringshare_replace(&(pd->ext->name), name);
522              _efl_object_extension_noneed(pd);
523           }
524      }
525 }
526 
527 EOLIAN static const char *
_efl_object_name_get(const Eo * obj EINA_UNUSED,Efl_Object_Data * pd)528 _efl_object_name_get(const Eo *obj EINA_UNUSED, Efl_Object_Data *pd)
529 {
530    if (!pd->ext) return NULL;
531    return pd->ext->name;
532 }
533 
534 static inline Eina_Bool
_name_match(const char * match,Eina_Bool is_glob,const char * str)535 _name_match(const char *match, Eina_Bool is_glob, const char *str)
536 {
537    if (str)
538      {
539         if (is_glob)
540           {
541              // if match string is empty - then it matches - same as "*"
542              if (!match[0]) return EINA_TRUE;
543              // if match string is "*" special case it and match
544              if ((match[0] == '*') && (match[1] == 0)) return EINA_TRUE;
545              // actual compare
546              if (!fnmatch(match, str, 0)) return EINA_TRUE;
547           }
548         else
549           {
550              // if match string is empty - then it matches - same as "*"
551              if (!match[0]) return EINA_TRUE;
552              // if pointers are the same they must be the same
553              if (match == str) return EINA_TRUE;
554              // actual compare
555              if (!strcmp(match, str)) return EINA_TRUE;
556           }
557      }
558    return EINA_FALSE;
559 }
560 
561 static inline Eina_Bool
_matchall(const char * match)562 _matchall(const char *match)
563 {
564    if ((match[0] == 0) || ((match[0] == '*') && (match[1] == 0)))
565      return EINA_TRUE;
566    return EINA_FALSE;
567 }
568 
569 static Eina_Bool
_hasglob(const char * match)570 _hasglob(const char *match)
571 {
572    if (strpbrk(match, "*?[")) return EINA_TRUE;
573    return EINA_FALSE;
574 }
575 
576 static Eina_Bool
_ismultiglob(const char * match)577 _ismultiglob(const char *match)
578 {
579    if ((match[0] == '*') && (match[1] == '*') && (match[2] == 0))
580      return EINA_TRUE;
581    if ((match[0] == '*') && (match[1] == '*') && (match[2] == '/'))
582      return EINA_TRUE;
583    if ((match[0] == '/') && (match[1] == '*') && (match[2] == '*') && (match[3] == 0))
584      return EINA_TRUE;
585    if ((match[0] == '/') && (match[1] == '*') && (match[2] == '*') && (match[3] == '/'))
586      return EINA_TRUE;
587    return EINA_FALSE;
588 }
589 
590 EOLIAN static Efl_Object *
_efl_object_name_find(const Eo * obj EINA_UNUSED,Efl_Object_Data * pd,const char * search)591 _efl_object_name_find(const Eo *obj EINA_UNUSED, Efl_Object_Data *pd, const char *search)
592 {
593    Eo *child;
594    _Eo_Object *child_eo;
595    const char *name, *p, *klass_name;
596 
597    // notes:
598    // if search contains NO "/" char, then its just a name search.
599    // if there is one or more "/" chars, then these are explicitly object
600    // delimiters.
601    // a name of "**" means 0 or more objects in the heirachy chain
602    // if the string has no "/" char at the start, it implies "/**/"
603    // a name can be a name or the form "class:name" where the object must
604    // be of class named "class" and name "name". if "name" is empty like:
605    // "class:" then an object of any name will match like "class:*". an
606    // empty class like ":name" is the sanme as "*:name" which is the same
607    // as "name". class ane name of course can be basic globs but not **
608 
609    // search string NULL or "" is invalid
610    if (!search) return NULL;
611    if (!search[0]) return NULL;
612 
613    if (strchr(search, '/'))
614      {
615         ERR("Looking up object by path '%s' is not supported", search);
616         return NULL;
617      }
618    else
619      {
620         // if this is a multi glob - "**" then we don't have a name or
621         // class to match at all so just don't look
622         if (_ismultiglob(search)) return NULL;
623         // check if this is "class:name" or just "name"
624         if ((p = strchr(search, ':')))
625           {
626              // "class:name"
627              char *klass;
628              char *search_name;
629              size_t colon_location = p - search;
630              Eina_Bool klass_glob = EINA_FALSE;
631              Eina_Bool name_glob = EINA_FALSE;
632 
633              // split class:name into 2 strings dropping :
634              klass = alloca(strlen(search) + 1);
635              strcpy(klass, search);
636              klass[colon_location] = '\0';
637              search_name = klass + colon_location + 1;
638 
639              // figure out if class or name are globs
640              klass_glob = _hasglob(klass);
641              name_glob = _hasglob(search_name);
642              EINA_INLIST_FOREACH(pd->children, child_eo)
643                {
644                   child = _eo_obj_id_get(child_eo);
645                   name = efl_name_get(child);
646                   klass_name = efl_class_name_get(efl_class_get(child));
647                   if (_name_match(klass, klass_glob, klass_name) &&
648                       (((!_matchall(klass)) && (!name) && (_matchall(search_name))) ||
649                        ((name) && _name_match(search_name, name_glob, name))))
650                     return child;
651                   child = efl_name_find(child, search);
652                   if (child) return child;
653                }
654           }
655         else
656           {
657              if (_hasglob(search))
658                {
659                   // we have a glob - fnmatch
660                   EINA_INLIST_FOREACH(pd->children, child_eo)
661                     {
662                        child = _eo_obj_id_get(child_eo);
663                        name = efl_name_get(child);
664                        if ((name) && (_name_match(search, EINA_TRUE, name)))
665                          return child;
666                        child = efl_name_find(child, search);
667                        if (child) return child;
668                     }
669                }
670              else
671                {
672                   // fast path for simple "name"
673                   EINA_INLIST_FOREACH(pd->children, child_eo)
674                     {
675                        child = _eo_obj_id_get(child_eo);
676                        name = efl_name_get(child);
677                        if ((name) && (_name_match(search, EINA_FALSE, name)))
678                          return child;
679                        child = efl_name_find(child, search);
680                        if (child) return child;
681                     }
682                }
683           }
684      }
685    return NULL;
686 }
687 
688 EOLIAN static void
_efl_object_comment_set(Eo * obj EINA_UNUSED,Efl_Object_Data * pd,const char * comment)689 _efl_object_comment_set(Eo *obj EINA_UNUSED, Efl_Object_Data *pd, const char *comment)
690 {
691    if ((comment) && (!comment[0])) comment = NULL;
692    if (comment)
693      {
694         _efl_object_extension_need(pd);
695         if (pd->ext) eina_stringshare_replace(&(pd->ext->comment), comment);
696      }
697    else
698      {
699         if (!pd->ext) return;
700         if (pd->ext->comment)
701           {
702              eina_stringshare_replace(&(pd->ext->comment), comment);
703              _efl_object_extension_noneed(pd);
704           }
705      }
706 }
707 
708 EOLIAN static const char *
_efl_object_comment_get(const Eo * obj EINA_UNUSED,Efl_Object_Data * pd)709 _efl_object_comment_get(const Eo *obj EINA_UNUSED, Efl_Object_Data *pd)
710 {
711    if (!pd->ext) return NULL;
712    return pd->ext->comment;
713 }
714 
715 EOLIAN static void
_efl_object_debug_name_override(Eo * obj_id EINA_UNUSED,Efl_Object_Data * pd EINA_UNUSED,Eina_Strbuf * sb EINA_UNUSED)716 _efl_object_debug_name_override(Eo *obj_id EINA_UNUSED, Efl_Object_Data *pd EINA_UNUSED, Eina_Strbuf *sb EINA_UNUSED)
717 {
718 }
719 
720 EAPI void
efl_del(const Eo * obj)721 efl_del(const Eo *obj)
722 {
723    if (!obj) return ;
724    EO_OBJ_POINTER_RETURN(obj, oid);
725    _efl_ref(oid);
726    if (efl_parent_get((Eo *) obj))
727      {
728         efl_parent_set((Eo *) obj, NULL);
729      }
730    else
731      {
732         ERR("Calling efl_del on object %s with no parent is not advised any more.", efl_debug_name_get(obj));
733         efl_unref(obj);
734      }
735    _efl_unref(oid);
736    EO_OBJ_DONE(obj);
737 }
738 
739 void
_efl_object_reuse(_Eo_Object * obj)740 _efl_object_reuse(_Eo_Object *obj)
741 {
742    obj->is_invalidating = EINA_FALSE;
743    obj->invalidate = EINA_FALSE;
744 }
745 
746 EOLIAN void
_efl_object_parent_set(Eo * obj,Efl_Object_Data * pd,Eo * parent_id)747 _efl_object_parent_set(Eo *obj, Efl_Object_Data *pd, Eo *parent_id)
748 {
749    Eo *prev_parent = pd->parent;
750    Eina_Bool bad_parent = EINA_FALSE;
751 
752    if ((pd->parent == parent_id) ||
753        ((parent_id) && (!_eo_id_domain_compatible(parent_id, obj))))
754      return;
755 
756    if (parent_id != NULL)
757      {
758         EO_OBJ_POINTER_GOTO(parent_id, parent_obj, err_impossible);
759         bad_parent = parent_obj->invalidate || (obj == parent_id);
760         EO_OBJ_DONE(parent_id);
761      }
762 
763    if (bad_parent) goto err_parent;
764 
765    EO_OBJ_POINTER_GOTO(obj, eo_obj, err_impossible);
766 
767    // Invalidated object can not be bring back to life
768    if (eo_obj->invalidate)
769      {
770         ERR("Call of efl_parent_set(%p, %p) when object of clas '%s' is already invalidated.\n", obj, parent_id, efl_class_name_get(obj));
771         goto err_impossible;
772      }
773 
774    if (!parent_id)
775      {
776         if (prev_parent) _efl_invalidate(eo_obj);
777      }
778 
779    if (pd->parent)
780      {
781         Efl_Object_Data *old_parent_pd = efl_data_scope_get(pd->parent,
782                                                             EFL_OBJECT_CLASS);
783 
784         if (old_parent_pd)
785           old_parent_pd->children = eina_inlist_remove(old_parent_pd->children,
786                                                        EINA_INLIST_GET(eo_obj));
787         // this error is highly unlikely so move it out of the normal
788         // instruction path to avoid l1 cache pollution
789         else goto err_impossible;
790      }
791 
792    /* Set new parent */
793    if (parent_id)
794      {
795         Efl_Object_Data *parent_pd = efl_data_scope_get(parent_id,
796                                                         EFL_OBJECT_CLASS);
797 
798         if (EINA_LIKELY(parent_pd != NULL))
799           {
800              pd->parent = parent_id;
801              parent_pd->children = eina_inlist_append(parent_pd->children,
802                                                       EINA_INLIST_GET(eo_obj));
803              if (!prev_parent) efl_ref(obj);
804           }
805         else
806           {
807              pd->parent = NULL;
808              if (prev_parent) efl_unref(obj);
809              // unlikely this error happens, so move it out of execution path
810              // to improve l1 cache efficiency
811              goto err_parent_done;
812           }
813 
814         eo_obj->parent = EINA_TRUE;
815      }
816    else
817      {
818         pd->parent = NULL;
819         eo_obj->parent = EINA_FALSE;
820 
821         if (prev_parent && !eo_obj->del_triggered) efl_unref(obj);
822      }
823 
824    EO_OBJ_DONE(obj);
825    return;
826 
827 err_parent_done:
828    EO_OBJ_DONE(obj);
829 err_parent:
830    if (obj == parent_id)
831      ERR("New parent %p for object %p will not be set: THIS IS THE SAME OBJECT.",
832          parent_id, obj);
833    else
834      ERR("New parent %p for object %p is not a valid Eo object.",
835          parent_id, obj);
836    return;
837 
838 err_impossible:
839    ERR("CONTACT DEVS!!! SHOULD NEVER HAPPEN!!! Old parent %p for object %p is not a valid Eo object.",
840        pd->parent, obj);
841 }
842 
843 EOLIAN static Eo *
_efl_object_parent_get(const Eo * obj EINA_UNUSED,Efl_Object_Data * pd)844 _efl_object_parent_get(const Eo *obj EINA_UNUSED, Efl_Object_Data *pd)
845 {
846    return pd->parent;
847 }
848 
849 EOLIAN static Eina_Bool
_efl_object_finalized_get(const Eo * obj_id,Efl_Object_Data * pd EINA_UNUSED)850 _efl_object_finalized_get(const Eo *obj_id, Efl_Object_Data *pd EINA_UNUSED)
851 {
852    Eina_Bool finalized;
853    EO_OBJ_POINTER_RETURN_VAL(obj_id, obj, EINA_FALSE);
854    finalized = obj->finalized;
855    EO_OBJ_DONE(obj_id);
856    return finalized;
857 }
858 
859 EOLIAN static Eina_Bool
_efl_object_invalidated_get(const Eo * obj_id,Efl_Object_Data * pd)860 _efl_object_invalidated_get(const Eo *obj_id, Efl_Object_Data *pd)
861 {
862    Eina_Bool invalidate;
863    EO_OBJ_POINTER_RETURN_VAL(obj_id, obj, EINA_TRUE);
864    invalidate = obj->invalidate;
865    EO_OBJ_DONE(obj_id);
866    if (!invalidate && pd && pd->parent)
867      return efl_invalidated_get(pd->parent);
868    return invalidate;
869 }
870 
871 EOLIAN static Eina_Bool
_efl_object_invalidating_get(const Eo * obj_id,Efl_Object_Data * pd EINA_UNUSED)872 _efl_object_invalidating_get(const Eo *obj_id, Efl_Object_Data *pd EINA_UNUSED)
873 {
874    Eina_Bool invalidating;
875    EO_OBJ_POINTER_RETURN_VAL(obj_id, obj, EINA_TRUE);
876    invalidating = obj->is_invalidating;
877    EO_OBJ_DONE(obj_id);
878    return invalidating;
879 }
880 
881 EOLIAN static Efl_Object *
_efl_object_provider_find(const Eo * obj,Efl_Object_Data * pd,const Efl_Object * klass)882 _efl_object_provider_find(const Eo *obj, Efl_Object_Data *pd, const Efl_Object *klass)
883 {
884    Eina_Bool invalidate;
885    Efl_Object *r = NULL;
886 
887    invalidate = _efl_object_invalidated_get((Eo*) obj, NULL);
888    if (invalidate)
889      {
890         ERR("Calling efl_provider_find(%p) after the object was invalidated.", obj);
891         return NULL;
892      }
893 
894    if (efl_isa(obj, klass)) return (Eo *) obj;
895 
896    if (pd->ext) r = eina_hash_find(pd->ext->providers, &klass);
897    if (r) return r;
898 
899    if (pd->parent) return efl_provider_find(pd->parent, klass);
900    return NULL;
901 }
902 
903 static Eina_Bool
_efl_object_provider_register(Eo * obj EINA_UNUSED,Efl_Object_Data * pd,const Efl_Class * klass,const Efl_Object * provider)904 _efl_object_provider_register(Eo *obj EINA_UNUSED, Efl_Object_Data *pd, const Efl_Class *klass, const Efl_Object *provider)
905 {
906    // The passed object does not provide that said class.
907    if (!efl_isa(provider, klass)) return EINA_FALSE;
908 
909    _efl_object_extension_need(pd);
910    if (!pd->ext) return EINA_FALSE;
911    if (!pd->ext->providers) pd->ext->providers = eina_hash_pointer_new(EINA_FREE_CB(efl_unref));
912 
913    // Prevent double insertion for the same class
914    if (eina_hash_find(pd->ext->providers, &klass)) return EINA_FALSE;
915 
916    // Note: I would prefer to use efl_xref here, but I can't figure a nice way to
917    // call efl_xunref on hash destruction.
918    return eina_hash_add(pd->ext->providers, &klass, efl_ref(provider));
919 }
920 
921 static Eina_Bool
_efl_object_provider_unregister(Eo * obj EINA_UNUSED,Efl_Object_Data * pd,const Efl_Class * klass,const Efl_Object * provider)922 _efl_object_provider_unregister(Eo *obj EINA_UNUSED, Efl_Object_Data *pd, const Efl_Class *klass, const Efl_Object *provider)
923 {
924    Eina_Bool r;
925 
926    if (!pd->ext) return EINA_FALSE;
927    r = eina_hash_del(pd->ext->providers, &klass, provider);
928 
929    if (eina_hash_population(pd->ext->providers) != 0) return r;
930    eina_hash_free(pd->ext->providers);
931    pd->ext->providers = NULL;
932    _efl_object_extension_noneed(pd);
933 
934    return r;
935 }
936 
937 /* Children accessor */
938 typedef struct _Eo_Children_Iterator Eo_Children_Iterator;
939 struct _Eo_Children_Iterator
940 {
941    Eina_Iterator iterator;
942    Eina_Inlist *current;
943    _Eo_Object *obj;
944    Eo *obj_id;
945 };
946 
947 static Eina_Bool
_efl_children_iterator_next(Eo_Children_Iterator * it,void ** data)948 _efl_children_iterator_next(Eo_Children_Iterator *it, void **data)
949 {
950    if (!it->current) return EINA_FALSE;
951 
952    if (data)
953      {
954         _Eo_Object *eo_obj = EINA_INLIST_CONTAINER_GET(it->current, _Eo_Object);
955         *data = _eo_obj_id_get(eo_obj);
956      }
957    it->current = it->current->next;
958 
959    return EINA_TRUE;
960 }
961 
962 static Eo *
_efl_children_iterator_container(Eo_Children_Iterator * it)963 _efl_children_iterator_container(Eo_Children_Iterator *it)
964 {
965    return it->obj_id;
966 }
967 
968 static void
_efl_children_iterator_free(Eo_Children_Iterator * it)969 _efl_children_iterator_free(Eo_Children_Iterator *it)
970 {
971    _Efl_Class *klass;
972    _Eo_Object *obj;
973 
974    klass = (_Efl_Class*) it->obj->klass;
975    obj = it->obj;
976 
977    eina_spinlock_take(&klass->iterators.trash_lock);
978    if (klass->iterators.trash_count < 8)
979      {
980         klass->iterators.trash_count++;
981         eina_trash_push(&klass->iterators.trash, it);
982      }
983    else
984      {
985         eina_freeq_ptr_main_add(it, free, sizeof(*it));
986      }
987    eina_spinlock_release(&klass->iterators.trash_lock);
988 
989    _efl_unref(obj);
990 }
991 
992 EOLIAN static Eina_Iterator *
_efl_object_children_iterator_new(Eo * obj_id,Efl_Object_Data * pd)993 _efl_object_children_iterator_new(Eo *obj_id, Efl_Object_Data *pd)
994 {
995    _Efl_Class *klass;
996    Eo_Children_Iterator *it = NULL;
997 
998    EO_OBJ_POINTER_RETURN_VAL(obj_id, obj, NULL);
999 
1000    if (pd->children)
1001      {
1002         klass = (_Efl_Class *)obj->klass;
1003 
1004         eina_spinlock_take(&klass->iterators.trash_lock);
1005         it = eina_trash_pop(&klass->iterators.trash);
1006         if (it)
1007           {
1008              klass->iterators.trash_count--;
1009              memset(it, 0, sizeof (Eo_Children_Iterator));
1010           }
1011         else it = calloc(1, sizeof (Eo_Children_Iterator));
1012         eina_spinlock_release(&klass->iterators.trash_lock);
1013         // very unlikely to not allocate the iterator to move this error
1014         // handling out of l1 instruction cache
1015         if (!it) goto done;
1016 
1017         EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
1018         it->current = pd->children;
1019         it->obj = _efl_ref(obj);
1020         it->obj_id = obj_id;
1021         it->iterator.next = FUNC_ITERATOR_NEXT(_efl_children_iterator_next);
1022         it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(_efl_children_iterator_container);
1023         it->iterator.free = FUNC_ITERATOR_FREE(_efl_children_iterator_free);
1024      }
1025 done:
1026    EO_OBJ_DONE(obj_id);
1027    return (Eina_Iterator *)it;
1028 }
1029 
1030 EOLIAN static void
_efl_object_dbg_info_get(Eo * obj EINA_UNUSED,Efl_Object_Data * pd EINA_UNUSED,Efl_Dbg_Info * root_node EINA_UNUSED)1031 _efl_object_dbg_info_get(Eo *obj EINA_UNUSED, Efl_Object_Data *pd EINA_UNUSED, Efl_Dbg_Info *root_node EINA_UNUSED)
1032 {  /* No info required in the meantime */
1033    return;
1034 }
1035 
1036 EOAPI EFL_VOID_FUNC_BODYV(efl_dbg_info_get, EFL_FUNC_CALL(root_node), Efl_Dbg_Info *root_node);
1037 
1038 /* Weak reference. */
1039 
1040 static inline size_t
_wref_count(Efl_Object_Data * pd)1041 _wref_count(Efl_Object_Data *pd)
1042 {
1043    Eo ***itr;
1044    size_t count = 0;
1045    Efl_Object_Extension *ext = pd->ext;
1046 
1047    if ((!ext) || (!ext->wrefs)) return 0;
1048    for (itr = ext->wrefs; *itr; itr++) count++;
1049 
1050    return count;
1051 }
1052 
1053 EOLIAN static void
_efl_object_wref_add(Eo * obj,Efl_Object_Data * pd,Eo ** wref)1054 _efl_object_wref_add(Eo *obj, Efl_Object_Data *pd, Eo **wref)
1055 {
1056    size_t count;
1057    Eo ***tmp;
1058    Efl_Object_Extension *ext;
1059 
1060    count = _wref_count(pd);
1061    count += 1; /* New wref. */
1062 
1063    ext = _efl_object_extension_need(pd);
1064    if (ext)
1065      {
1066         tmp = realloc(ext->wrefs, sizeof(*ext->wrefs) * (count + 1));
1067         if (tmp)
1068           {
1069              ext->wrefs = tmp;
1070              ext->wrefs[count - 1] = wref;
1071              ext->wrefs[count] = NULL;
1072              *wref = obj;
1073           }
1074      }
1075 }
1076 
1077 EOAPI EFL_VOID_FUNC_BODYV(efl_wref_add, EFL_FUNC_CALL(wref), Efl_Object **wref);
1078 
1079 EOLIAN static void
_efl_object_wref_del(Eo * obj,Efl_Object_Data * pd,Eo ** wref)1080 _efl_object_wref_del(Eo *obj, Efl_Object_Data *pd, Eo **wref)
1081 {
1082    size_t count;
1083    Efl_Object_Extension *ext = pd->ext;
1084 
1085    // very unlikely so improve l1 instr cache by using goto
1086    if (*wref != obj) goto err_wref_not_obj;
1087 
1088    // very unlikely so improve l1 instr cache by using goto
1089    if ((!ext) || (!ext->wrefs)) goto err_wref_none;
1090 
1091    /* Move the last item in the array instead of the current wref. */
1092    count = _wref_count(pd);
1093 
1094      {
1095         Eo ***itr;
1096         for (itr = ext->wrefs; *itr; itr++)
1097           {
1098              if (*itr == wref)
1099                {
1100                   *itr = ext->wrefs[count - 1];
1101                   break;
1102                }
1103           }
1104         // very unlikely so improve l1 instr cache by using goto
1105         if (!*itr) goto err_noiter;
1106      }
1107 
1108    if (count > 1)
1109      {
1110         Eo ***tmp;
1111         // No count--; because of the NULL that is not included in the count
1112         tmp = realloc(ext->wrefs, sizeof(*ext->wrefs) * count);
1113         if (!tmp) return;
1114         ext->wrefs = tmp;
1115         ext->wrefs[count - 1] = NULL;
1116         *wref = NULL;
1117         return;
1118      }
1119    else
1120      {
1121         eina_freeq_ptr_main_add(ext->wrefs, free, 0);
1122         ext->wrefs = NULL;
1123         _efl_object_extension_noneed(pd);
1124      }
1125 
1126    *wref = NULL;
1127    return;
1128 
1129 err_noiter:
1130    ERR("Wref %p is not associated with object %p", wref, obj);
1131    goto err_null;
1132 err_wref_none:
1133    ERR("There are no weak refs for object %p", obj);
1134 err_null:
1135    *wref = NULL;
1136    return;
1137 err_wref_not_obj:
1138    ERR("Wref is a weak ref to %p, while this function was called on %p.",
1139        *wref, obj);
1140    return;
1141 }
1142 
1143 EOAPI EFL_VOID_FUNC_BODYV(efl_wref_del, EFL_FUNC_CALL(wref), Efl_Object **wref);
1144 
1145 static inline void
_wref_destruct(Efl_Object_Data * pd)1146 _wref_destruct(Efl_Object_Data *pd)
1147 {
1148    Eo ***itr;
1149    Efl_Object_Extension *ext = pd->ext;
1150 
1151    if ((!ext) || (!ext->wrefs)) return;
1152    for (itr = ext->wrefs; *itr; itr++) **itr = NULL;
1153    eina_freeq_ptr_main_add(ext->wrefs, free, 0);
1154    ext->wrefs = NULL;
1155 }
1156 
1157 /* EOF Weak reference. */
1158 
1159 /* Event callbacks */
1160 
1161 /* Callbacks */
1162 
1163 /* XXX: Legacy support, remove when legacy is dead. */
1164 static Eina_Hash *_legacy_events_hash = NULL;
1165 
1166 EAPI const Efl_Event_Description *
efl_object_legacy_only_event_description_get(const char * _event_name)1167 efl_object_legacy_only_event_description_get(const char *_event_name)
1168 {
1169    Eina_Stringshare *event_name = eina_stringshare_add(_event_name);
1170    Efl_Event_Description *event_desc = eina_hash_find(_legacy_events_hash, event_name);
1171    if (!event_desc)
1172      {
1173         event_desc = calloc(1, sizeof(Efl_Event_Description));
1174         event_desc->name = event_name;
1175         event_desc->legacy_is = EINA_TRUE;
1176         event_desc->unfreezable = EINA_TRUE;
1177         eina_hash_add(_legacy_events_hash, event_name, event_desc);
1178      }
1179    else
1180      {
1181         eina_stringshare_del(event_name);
1182      }
1183 
1184    return event_desc;
1185 }
1186 
1187 static inline Eina_Bool
_legacy_event_desc_is(const Efl_Event_Description * desc)1188 _legacy_event_desc_is(const Efl_Event_Description *desc)
1189 {
1190    return desc->legacy_is;
1191 }
1192 
1193 static void
_legacy_events_hash_free_cb(void * _desc)1194 _legacy_events_hash_free_cb(void *_desc)
1195 {
1196    Efl_Event_Description *desc = _desc;
1197    eina_stringshare_del(desc->name);
1198    eina_freeq_ptr_main_add(desc, free, sizeof(*desc));
1199 }
1200 
1201 /* EOF Legacy */
1202 struct _Eo_Callback_Description
1203 {
1204    union
1205      {
1206         Efl_Callback_Array_Item item;
1207         const Efl_Callback_Array_Item *item_array;
1208      } items;
1209 
1210    void *func_data;
1211    Efl_Callback_Priority priority;
1212 
1213    unsigned short generation;
1214 
1215    Eina_Bool delete_me : 1;
1216    Eina_Bool func_array : 1;
1217 };
1218 
1219 static Eina_Mempool *_eo_callback_mempool = NULL;
1220 static Eina_Mempool *_efl_pending_future_mempool = NULL;
1221 static Eina_Mempool *_efl_future_scheduler_entry_mempool = NULL;
1222 
1223 static void
_eo_callback_free(Eo_Callback_Description * cb)1224 _eo_callback_free(Eo_Callback_Description *cb)
1225 {
1226    eina_mempool_free(_eo_callback_mempool, cb);
1227 }
1228 
1229 static Eo_Callback_Description *
_eo_callback_new(void)1230 _eo_callback_new(void)
1231 {
1232    return eina_mempool_calloc(_eo_callback_mempool,
1233                               sizeof(Eo_Callback_Description));
1234 }
1235 
1236 static void
_efl_pending_future_free(Efl_Future_Pending * pending)1237 _efl_pending_future_free(Efl_Future_Pending *pending)
1238 {
1239    eina_mempool_free(_efl_pending_future_mempool, pending);
1240 }
1241 
1242 static Efl_Future_Pending *
_efl_pending_future_new(void)1243 _efl_pending_future_new(void)
1244 {
1245    return eina_mempool_calloc(_efl_pending_future_mempool,
1246                               sizeof(Efl_Future_Pending));
1247 }
1248 
1249 #define CB_COUNT_INC(cnt) do { if ((cnt) != 0xffff) (cnt)++; } while(0)
1250 #define CB_COUNT_DEC(cnt) do { if ((cnt) != 0xffff) (cnt)--; } while(0)
1251 
1252 static inline unsigned char
_pointer_hash(const uintptr_t val)1253 _pointer_hash(const uintptr_t val)
1254 {
1255    static unsigned char shift = 0;
1256 
1257    /* Sadly LLVM doesn't have log2 in its compile time optimization. So
1258       we can not use static const here for portability sake. */
1259    if (EINA_UNLIKELY((!shift)))
1260      shift = (unsigned char) log2(1 + sizeof (Efl_Event_Description));
1261 #ifdef EFL64
1262    return (unsigned char)(((val) >> shift) & 0x3F);
1263 #else
1264    return (unsigned char)(((val) >> shift) & 0x1F);
1265 #endif
1266 }
1267 
1268 #define EFL_OBJECT_EVENT_CB_INC(Obj, It, Pd, Event, Update_Hash)        \
1269   if (It->desc == Event && !Pd->event_cb_##Event)                       \
1270     {                                                                   \
1271        Update_Hash = EINA_FALSE;                                        \
1272        Pd->event_cb_##Event = EINA_TRUE;                                \
1273     }
1274 
1275 #define EFL_OBJECT_EVENT_CB_DEC(Obj, It, Pd, Event)     \
1276   if (It->desc == Event && Pd->event_cb_##Event)        \
1277     {                                                   \
1278        if (!efl_event_callback_count(Obj, Event))       \
1279          Pd->event_cb_##Event = EINA_FALSE;             \
1280     }
1281 
1282 static inline void
_special_event_count_inc(Eo * obj_id,Efl_Object_Data * pd,const Efl_Callback_Array_Item * it)1283 _special_event_count_inc(Eo *obj_id, Efl_Object_Data *pd, const Efl_Callback_Array_Item *it)
1284 {
1285    Eina_Bool update_hash = EINA_TRUE;
1286 
1287    EFL_OBJECT_EVENT_CB_INC(obj_id, it, pd, EFL_EVENT_CALLBACK_ADD, update_hash)
1288    else EFL_OBJECT_EVENT_CB_INC(obj_id, it, pd, EFL_EVENT_CALLBACK_DEL, update_hash)
1289    else EFL_OBJECT_EVENT_CB_INC(obj_id, it, pd, EFL_EVENT_DEL, update_hash)
1290    else EFL_OBJECT_EVENT_CB_INC(obj_id, it, pd, EFL_EVENT_INVALIDATE, update_hash)
1291    else EFL_OBJECT_EVENT_CB_INC(obj_id, it, pd, EFL_EVENT_DESTRUCT, update_hash)
1292    else if (it->desc == EFL_EVENT_NOREF && !pd->event_cb_EFL_EVENT_NOREF)
1293      {
1294         update_hash = EINA_FALSE;
1295         EO_OBJ_POINTER_RETURN(obj_id, obj);
1296         obj->noref_event = EINA_TRUE;
1297         EO_OBJ_DONE(obj_id);
1298         pd->event_cb_EFL_EVENT_NOREF = EINA_TRUE;
1299      }
1300    else if (it->desc == EFL_EVENT_OWNERSHIP_SHARED || it->desc == EFL_EVENT_OWNERSHIP_UNIQUE)
1301      {
1302         EO_OBJ_POINTER_RETURN(obj_id, obj);
1303         obj->ownership_track = EINA_TRUE;
1304         EO_OBJ_DONE(obj_id);
1305      }
1306 
1307    if (pd->ext && pd->ext->forwarders)
1308      {
1309         Efl_Event_Forwarder *forwarder;
1310         Eina_List *l;
1311 
1312         // Check if some event need to be forwarded now
1313         EINA_LIST_FOREACH(eina_hash_find(pd->ext->forwarders, it->desc), l, forwarder)
1314           {
1315              if (!forwarder->source) continue;
1316              if (forwarder->inserted) continue;
1317              efl_event_callback_priority_add(forwarder->source,
1318                                              forwarder->desc,
1319                                              forwarder->priority,
1320                                              _efl_event_forwarder_callback, obj_id);
1321              forwarder->inserted = EINA_TRUE;
1322           }
1323      }
1324 
1325    if (update_hash)
1326      {
1327         unsigned char event_hash;
1328 
1329         event_hash = _pointer_hash((uintptr_t) it->desc);
1330 
1331         pd->callbacks_mask |= 1ULL << event_hash;
1332      }
1333 }
1334 
1335 static inline void
_special_event_count_dec(Eo * obj_id,Efl_Object_Data * pd,const Efl_Callback_Array_Item * it)1336 _special_event_count_dec(Eo *obj_id, Efl_Object_Data *pd, const Efl_Callback_Array_Item *it)
1337 {
1338    EFL_OBJECT_EVENT_CB_DEC(obj_id, it, pd, EFL_EVENT_CALLBACK_ADD)
1339    else EFL_OBJECT_EVENT_CB_DEC(obj_id, it, pd, EFL_EVENT_CALLBACK_DEL)
1340    else EFL_OBJECT_EVENT_CB_DEC(obj_id, it, pd, EFL_EVENT_DEL)
1341    else EFL_OBJECT_EVENT_CB_DEC(obj_id, it, pd, EFL_EVENT_INVALIDATE)
1342    else if (it->desc == EFL_EVENT_NOREF && pd->event_cb_EFL_EVENT_NOREF)
1343      {
1344         if (efl_event_callback_count(obj_id, EFL_EVENT_NOREF) == 0)
1345           {
1346              EO_OBJ_POINTER_RETURN(obj_id, obj);
1347              obj->noref_event = EINA_FALSE;
1348              EO_OBJ_DONE(obj_id);
1349 
1350              pd->event_cb_EFL_EVENT_NOREF = EINA_FALSE;
1351           }
1352      }
1353 }
1354 
1355 /* Actually remove, doesn't care about walking list, or delete_me */
1356 static void
_eo_callback_remove(Eo * obj,Efl_Object_Data * pd,Eo_Callback_Description ** cb)1357 _eo_callback_remove(Eo *obj, Efl_Object_Data *pd, Eo_Callback_Description **cb)
1358 {
1359    Eo_Callback_Description *tmp = *cb;
1360    unsigned int length;
1361    const Efl_Callback_Array_Item *it;
1362 
1363    length = pd->callbacks_count - (cb - pd->callbacks);
1364    if (length > 1)
1365      memmove(cb, cb + 1, (length - 1) * sizeof(Eo_Callback_Description *));
1366    pd->callbacks_count--;
1367 
1368    if (_eo_nostep_alloc) pd->callbacks = realloc(pd->callbacks, pd->callbacks_count * sizeof (Eo_Callback_Description*));
1369 
1370    if (pd->callbacks_count == 0)
1371      {
1372         free(pd->callbacks);
1373         pd->callbacks = NULL;
1374      }
1375 
1376    if (tmp->func_array)
1377      {
1378         for (it = tmp->items.item_array; it->func; it++)
1379           _special_event_count_dec(obj, pd, it);
1380      }
1381    else _special_event_count_dec(obj, pd, &(tmp->items.item));
1382 
1383    _eo_callback_free(tmp);
1384 }
1385 
1386 /* Actually remove, doesn't care about walking list, or delete_me */
1387 static void
_eo_callback_remove_all(Efl_Object_Data * pd)1388 _eo_callback_remove_all(Efl_Object_Data *pd)
1389 {
1390    unsigned int i;
1391 
1392    for (i = 0; i < pd->callbacks_count; i++)
1393      _eo_callback_free(pd->callbacks[i]);
1394 
1395    eina_freeq_ptr_main_add(pd->callbacks, free, 0);
1396    pd->callbacks = NULL;
1397    pd->callbacks_count = 0;
1398    pd->event_cb_EFL_EVENT_DESTRUCT = EINA_FALSE;
1399    pd->event_cb_EFL_EVENT_CALLBACK_ADD = EINA_FALSE;
1400    pd->event_cb_EFL_EVENT_CALLBACK_DEL = EINA_FALSE;
1401    pd->event_cb_EFL_EVENT_DEL = EINA_FALSE;
1402    pd->event_cb_EFL_EVENT_NOREF = EINA_FALSE;
1403    pd->event_cb_EFL_EVENT_INVALIDATE = EINA_FALSE;
1404 }
1405 
1406 static void
_eo_callbacks_clear(Eo * obj,Efl_Object_Data * pd)1407 _eo_callbacks_clear(Eo *obj, Efl_Object_Data *pd)
1408 {
1409    Eo_Callback_Description **itr;
1410    unsigned int i = 0;
1411    Eina_Bool remove_callbacks;
1412    unsigned int generation_clamp;
1413 
1414    /* If there are no deletions waiting. */
1415    if (!pd->need_cleaning) return;
1416 
1417 
1418    if (pd->event_frame)
1419      {
1420         /* there is still a event emission going on ... do not delete anything! */
1421         remove_callbacks = EINA_FALSE;
1422         /* if we are in event subscription we need to clamp the generations at the current frame otherwise we are possiblity not executing that later */
1423         generation_clamp = pd->event_frame->generation;
1424      }
1425    else
1426      {
1427         /* no event emission running */
1428         /* remove deleted callbacks */
1429         remove_callbacks = EINA_TRUE;
1430         /* clap to 0 generation */
1431         generation_clamp = 0;
1432         /* we dont need to clean later */
1433         pd->need_cleaning = EINA_FALSE;
1434      }
1435 
1436    while (i < pd->callbacks_count)
1437      {
1438         itr = pd->callbacks + i;
1439         if (remove_callbacks && (*itr)->delete_me)
1440           {
1441              _eo_callback_remove(obj, pd, itr);
1442           }
1443         else
1444           {
1445              if ((*itr)->generation > generation_clamp)
1446                (*itr)->generation = generation_clamp;
1447              i++;
1448           }
1449      }
1450 }
1451 
1452 static inline unsigned int
_eo_callback_search_sorted_near(const Efl_Object_Data * pd,const Eo_Callback_Description * look)1453 _eo_callback_search_sorted_near(const Efl_Object_Data *pd, const Eo_Callback_Description *look)
1454 {
1455    unsigned int start, last, middle;
1456    const Eo_Callback_Description *p;
1457    int cmp;
1458 
1459    if      (pd->callbacks_count == 0) return 0;
1460    else if (pd->callbacks_count == 1) return 0;
1461 
1462    start = 0;
1463    last = pd->callbacks_count - 1;
1464    do
1465      {
1466         middle = start + ((last - start) / 2);
1467         p = pd->callbacks[middle];
1468 
1469         cmp = p->priority - look->priority;
1470         if      (cmp    == 0) return middle;
1471         else if (cmp    >  0) start = middle + 1;
1472         else if (middle >  0) last  = middle - 1;
1473         else break;
1474      }
1475    while (start <= last);
1476    return middle;
1477 }
1478 
1479 static void
_eo_callbacks_sorted_insert(Efl_Object_Data * pd,Eo_Callback_Description * cb)1480 _eo_callbacks_sorted_insert(Efl_Object_Data *pd, Eo_Callback_Description *cb)
1481 {
1482    Eo_Callback_Description **itr;
1483    unsigned int length, j;
1484    Efl_Event_Callback_Frame *frame;
1485 
1486    // Do a dichotomic searh
1487    j = _eo_callback_search_sorted_near(pd, cb);
1488    // Adjust for both case of length == 0 and when priority is equal.
1489    while ((j < pd->callbacks_count) &&
1490           (pd->callbacks[j]->priority >= cb->priority)) j++;
1491 
1492    // Increase the callbacks storage by 16 entries at a time
1493    if (_eo_nostep_alloc || (pd->callbacks_count & 0xF) == 0x0)
1494      {
1495         Eo_Callback_Description **tmp;
1496         unsigned int new_len = (pd->callbacks_count | 0xF) + 1;
1497 
1498         if (_eo_nostep_alloc) new_len = pd->callbacks_count + 1;
1499 
1500         tmp = realloc(pd->callbacks,
1501                       new_len * sizeof(Eo_Callback_Description *));
1502         if (EINA_UNLIKELY(!tmp)) return;
1503         pd->callbacks = tmp;
1504      }
1505 
1506    // FIXME: Potential improvement, merge single callback description of the same priority
1507    // into an array when possible
1508    itr = pd->callbacks + j;
1509    length = pd->callbacks_count - j;
1510    if (length > 0) memmove(itr + 1, itr,
1511                            length * sizeof(Eo_Callback_Description *));
1512    *itr = cb;
1513 
1514    pd->callbacks_count++;
1515 
1516    // Update possible event emissions
1517    for (frame = pd->event_frame; frame; frame = frame->next)
1518      {
1519         if ((itr - pd->callbacks) < (ptrdiff_t)frame->idx)
1520           frame->inserted_before++;
1521      }
1522 }
1523 
1524 static unsigned short
_efl_event_generation(Efl_Object_Data * pd)1525 _efl_event_generation(Efl_Object_Data *pd)
1526 {
1527    if (!pd->event_frame) return 0;
1528 
1529    return ((Efl_Event_Callback_Frame*)pd->event_frame)->generation;
1530 }
1531 
1532 EOLIAN static Eina_Bool
_efl_object_event_callback_priority_add(Eo * obj,Efl_Object_Data * pd,const Efl_Event_Description * desc,Efl_Callback_Priority priority,Efl_Event_Cb func,const void * user_data)1533 _efl_object_event_callback_priority_add(Eo *obj, Efl_Object_Data *pd,
1534                                         const Efl_Event_Description *desc,
1535                                         Efl_Callback_Priority priority,
1536                                         Efl_Event_Cb func,
1537                                         const void *user_data)
1538 {
1539    const Efl_Callback_Array_Item_Full arr[] =
1540      { {desc, priority, func, (void *)user_data}, {NULL, 0, NULL, NULL}};
1541    Eo_Callback_Description *cb = _eo_callback_new();
1542 #ifdef EO_DEBUG
1543    unsigned int idx, r = 0, entries = 0;
1544 
1545    for (idx = pd->callbacks_count ; idx > 0; idx--)
1546      {
1547         Eo_Callback_Description **cb;
1548 
1549         cb = pd->callbacks + idx - 1;
1550         if (!(*cb)->func_array)
1551           {
1552              if (((*cb)->items.item.desc == desc) &&
1553                  ((*cb)->items.item.func == func) &&
1554                  ((*cb)->priority == priority) &&
1555                  ((*cb)->generation == _efl_event_generation(pd)))
1556                r++;
1557           }
1558         entries++;
1559      }
1560    if (r > 1) INF("Object '%s' got %i callback with event '%s' registered.",
1561                   efl_debug_name_get(obj), r, desc->name);
1562    if (entries > 10) INF("Object '%s' got %i callbacks.",
1563                          efl_debug_name_get(obj), entries);
1564 #endif
1565 
1566    // very unlikely so improve l1 instr cache by using goto
1567    if (EINA_UNLIKELY(!cb || !desc || !func)) goto err;
1568    cb->items.item.desc = desc;
1569    cb->items.item.func = func;
1570    cb->func_data = (void *)user_data;
1571    cb->priority = priority;
1572    cb->generation = _efl_event_generation(pd);
1573    if (cb->generation) pd->need_cleaning = EINA_TRUE;
1574 
1575    _eo_callbacks_sorted_insert(pd, cb);
1576    _special_event_count_inc(obj, pd, &(cb->items.item));
1577 
1578    if (pd->event_cb_EFL_EVENT_CALLBACK_ADD)
1579      efl_event_callback_call(obj, EFL_EVENT_CALLBACK_ADD, (void *)arr);
1580 
1581    return EINA_TRUE;
1582 
1583 err: EINA_COLD
1584    ERR("Tried adding callback with invalid values: cb: %p desc: %p func: %p", cb, desc, func);
1585    _eo_callback_free(cb);
1586    return EINA_FALSE;
1587 }
1588 
1589 EOAPI EFL_FUNC_BODYV(efl_event_callback_priority_add,
1590                      Eina_Bool, 0, EFL_FUNC_CALL(desc, priority, cb, data),
1591                      const Efl_Event_Description *desc,
1592                      Efl_Callback_Priority priority,
1593                      Efl_Event_Cb cb, const void *data);
1594 
1595 static void
_efl_object_event_callback_clean(Eo * obj,Efl_Object_Data * pd,const Efl_Callback_Array_Item_Full * array,Eo_Callback_Description ** cb)1596 _efl_object_event_callback_clean(Eo *obj, Efl_Object_Data *pd,
1597                                  const Efl_Callback_Array_Item_Full *array,
1598                                  Eo_Callback_Description **cb)
1599 {
1600    (*cb)->delete_me = EINA_TRUE;
1601    if (pd->event_frame)
1602      pd->need_cleaning = EINA_TRUE;
1603    else
1604      _eo_callback_remove(obj, pd, cb);
1605    if (pd->event_cb_EFL_EVENT_CALLBACK_DEL)
1606      efl_event_callback_call(obj, EFL_EVENT_CALLBACK_DEL, (void *)array);
1607 }
1608 
1609 EOLIAN static Eina_Bool
_efl_object_event_callback_del(Eo * obj,Efl_Object_Data * pd,const Efl_Event_Description * desc,Efl_Event_Cb func,const void * user_data)1610 _efl_object_event_callback_del(Eo *obj, Efl_Object_Data *pd,
1611                                const Efl_Event_Description *desc,
1612                                Efl_Event_Cb func,
1613                                const void *user_data)
1614 {
1615    Eo_Callback_Description **cb;
1616    unsigned int i;
1617 
1618    for (cb = pd->callbacks, i = 0;
1619         i < pd->callbacks_count;
1620         cb++, i++)
1621      {
1622         if (!(*cb)->delete_me &&
1623             ((*cb)->items.item.desc == desc) &&
1624             ((*cb)->items.item.func == func) &&
1625             ((*cb)->func_data == user_data))
1626           {
1627              const Efl_Callback_Array_Item_Full arr[] =
1628                { {desc, (*cb)->priority, func, (*cb)->func_data}, {NULL, 0, NULL, NULL}};
1629 
1630              _efl_object_event_callback_clean(obj, pd, arr, cb);
1631              return EINA_TRUE;
1632           }
1633      }
1634 
1635    DBG("Callback of object %p with function %p and data %p not found.", obj, func, user_data);
1636    return EINA_FALSE;
1637 }
1638 
1639 EOAPI EFL_FUNC_BODYV(efl_event_callback_del,
1640                      Eina_Bool, 0, EFL_FUNC_CALL(desc, func, user_data),
1641                      const Efl_Event_Description *desc,
1642                      Efl_Event_Cb func, const void *user_data);
1643 
1644 EOLIAN static Eina_Bool
_efl_object_event_callback_array_priority_add(Eo * obj,Efl_Object_Data * pd,const Efl_Callback_Array_Item * array,Efl_Callback_Priority priority,const void * user_data)1645 _efl_object_event_callback_array_priority_add(Eo *obj, Efl_Object_Data *pd,
1646                                               const Efl_Callback_Array_Item *array,
1647                                               Efl_Callback_Priority priority,
1648                                               const void *user_data)
1649 {
1650    Eo_Callback_Description *cb = _eo_callback_new();
1651    const Efl_Callback_Array_Item *it;
1652    unsigned int num, i;
1653    Efl_Callback_Array_Item_Full *ev_array;
1654 #ifdef EO_DEBUG
1655    const Efl_Callback_Array_Item *prev;
1656    unsigned int idx, r = 0, entries = 0;
1657 #endif
1658 
1659    // very unlikely so improve l1 instr cache by using goto
1660    if (!cb || !array) goto err;
1661 #ifdef EO_DEBUG
1662    prev = array;
1663    for (it = prev + 1; prev->func && it->func; it++, prev++)
1664      {
1665         if (efl_callbacks_cmp(prev, it) > 0)
1666           {
1667              ERR("Trying to insert a non sorted array callbacks (%p).", array);
1668              _eo_callback_free(cb);
1669              return EINA_FALSE;
1670           }
1671      }
1672 #endif
1673 
1674    cb->func_data = (void *) user_data;
1675    cb->priority = priority;
1676    cb->items.item_array = array;
1677    cb->func_array = EINA_TRUE;
1678    cb->generation = _efl_event_generation(pd);
1679    if (!!cb->generation) pd->need_cleaning = EINA_TRUE;
1680 
1681 #ifdef EO_DEBUG
1682    for (idx = pd->callbacks_count ; idx > 0; idx--)
1683      {
1684         Eo_Callback_Description **cb;
1685 
1686         cb = pd->callbacks + idx - 1;
1687         if ((*cb)->func_array)
1688           {
1689              if (((*cb)->items.item_array == array) &&
1690                  ((*cb)->priority == priority) &&
1691                  ((*cb)->generation == _efl_event_generation(pd)))
1692                r++;
1693           }
1694         entries++;
1695      }
1696    if (r > 1)
1697      {
1698         Eina_Strbuf *buf = eina_strbuf_new();
1699         Eina_Bool first = EINA_TRUE;
1700 
1701         for (it = array; it->func; it++)
1702           {
1703              if (first) eina_strbuf_append(buf, it->desc->name);
1704              else eina_strbuf_append_printf(buf, ", %s", it->desc->name);
1705              first = EINA_FALSE;
1706           }
1707         INF("Object '%s' got %i callback with events array %s registered.",
1708             efl_debug_name_get(obj), r, eina_strbuf_string_get(buf));
1709         eina_strbuf_free(buf);
1710      }
1711    if (entries > 10) INF("Object '%s' got %i callbacks.",
1712                          efl_debug_name_get(obj), entries);
1713 #endif
1714 
1715    _eo_callbacks_sorted_insert(pd, cb);
1716    for (it = cb->items.item_array; it->func; it++)
1717      _special_event_count_inc(obj, pd, it);
1718 
1719    num = 0;
1720    for (it = cb->items.item_array; it->func; it++) num++;
1721    ev_array = alloca((num + 1) * sizeof(Efl_Callback_Array_Item_Full));
1722    for (i = 0, it = cb->items.item_array; it->func; it++, i++)
1723      {
1724         ev_array[i].desc = cb->items.item_array[i].desc;
1725         ev_array[i].priority = cb->priority;
1726         ev_array[i].func = cb->items.item_array[i].func;
1727         ev_array[i].user_data = cb->func_data;
1728      }
1729    ev_array[i].desc = NULL;
1730    ev_array[i].priority = 0;
1731    ev_array[i].func = NULL;
1732    ev_array[i].user_data = NULL;
1733    if (pd->event_cb_EFL_EVENT_CALLBACK_ADD)
1734      efl_event_callback_call(obj, EFL_EVENT_CALLBACK_ADD, ev_array);
1735 
1736    return EINA_TRUE;
1737 
1738 err:
1739    ERR("Tried adding array of callbacks with invalid values: cb: %p array: %p.", cb, array);
1740    _eo_callback_free(cb);
1741    return EINA_FALSE;
1742 }
1743 
1744 EOAPI EFL_FUNC_BODYV(efl_event_callback_array_priority_add,
1745                      Eina_Bool, 0, EFL_FUNC_CALL(array, priority, data),
1746                      const Efl_Callback_Array_Item *array,
1747                      Efl_Callback_Priority priority, const void *data);
1748 
1749 EOLIAN static Eina_Bool
_efl_object_event_callback_array_del(Eo * obj,Efl_Object_Data * pd,const Efl_Callback_Array_Item * array,const void * user_data)1750 _efl_object_event_callback_array_del(Eo *obj, Efl_Object_Data *pd,
1751                                      const Efl_Callback_Array_Item *array,
1752                                      const void *user_data)
1753 {
1754    Eo_Callback_Description **cb;
1755    unsigned int j;
1756 
1757    for (cb = pd->callbacks, j = 0;
1758         j < pd->callbacks_count;
1759         cb++, j++)
1760      {
1761         if (!(*cb)->delete_me &&
1762             ((*cb)->items.item_array == array) &&
1763             ((*cb)->func_data == user_data))
1764           {
1765              const Efl_Callback_Array_Item *it;
1766              unsigned int num, i;
1767              Efl_Callback_Array_Item_Full *ev_array;
1768 
1769              num = 0;
1770              for (it = (*cb)->items.item_array; it->func; it++) num++;
1771              ev_array = alloca((num + 1) * sizeof(Efl_Callback_Array_Item_Full));
1772              for (i = 0, it = (*cb)->items.item_array; it->func; it++, i++)
1773                {
1774                   ev_array[i].desc = (*cb)->items.item_array[i].desc;
1775                   ev_array[i].priority = (*cb)->priority;
1776                   ev_array[i].func = (*cb)->items.item_array[i].func;
1777                   ev_array[i].user_data = (*cb)->func_data;
1778                }
1779              ev_array[i].desc = NULL;
1780              ev_array[i].priority = 0;
1781              ev_array[i].func = NULL;
1782              ev_array[i].user_data = NULL;
1783              _efl_object_event_callback_clean(obj, pd, ev_array, cb);
1784              return EINA_TRUE;
1785           }
1786      }
1787 
1788    DBG("Callback of object %p with function array %p and data %p not found.", obj, array, user_data);
1789    return EINA_FALSE;
1790 }
1791 
1792 EOAPI EFL_FUNC_BODYV(efl_event_callback_array_del,
1793                      Eina_Bool, 0, EFL_FUNC_CALL(array, user_data),
1794                      const Efl_Callback_Array_Item *array,
1795                      const void *user_data);
1796 
1797 typedef struct _Efl_Future_Scheduler Efl_Future_Scheduler;
1798 typedef struct _Efl_Future_Scheduler_Entry Efl_Future_Scheduler_Entry;
1799 
1800 struct _Efl_Future_Scheduler
1801 {
1802    Eina_Future_Scheduler scheduler;
1803 
1804    const Efl_Callback_Array_Item *array;
1805    const Eo *self;
1806 
1807    Eina_List *futures;
1808 
1809    Eina_Bool listener : 1;
1810 };
1811 
1812 struct _Efl_Future_Scheduler_Entry
1813 {
1814    Eina_Future_Schedule_Entry base;
1815    Eina_Future_Scheduler_Cb cb;
1816    Eina_Future *future;
1817    Eina_Value value;
1818 };
1819 
1820 static Eina_Trash *schedulers_trash = NULL;
1821 static unsigned char schedulers_count = 0;
1822 
1823 static void
_future_scheduler_cleanup(Efl_Object_Data * pd)1824 _future_scheduler_cleanup(Efl_Object_Data *pd)
1825 {
1826    if (eina_hash_population(pd->ext->schedulers)) return ;
1827 
1828    eina_hash_free(pd->ext->schedulers);
1829    pd->ext->schedulers = NULL;
1830    _efl_object_extension_noneed(pd);
1831 }
1832 
1833 static void
_futures_dispatch_cb(void * data,const Efl_Event * ev EINA_UNUSED)1834 _futures_dispatch_cb(void *data, const Efl_Event *ev EINA_UNUSED)
1835 {
1836    Efl_Future_Scheduler *sched = data;
1837    Eina_List *entries = sched->futures;
1838    Efl_Future_Scheduler_Entry *entry;
1839 
1840    sched->futures = NULL;
1841 
1842    efl_event_callback_array_del((Eo *) sched->self, sched->array, sched);
1843    sched->listener = EINA_FALSE;
1844 
1845    // Now trigger callbacks
1846    EINA_LIST_FREE(entries, entry)
1847      {
1848         entry->cb(entry->future, entry->value);
1849         eina_mempool_free(_efl_future_scheduler_entry_mempool, entry);
1850      }
1851 }
1852 
1853 static void
_futures_cancel_cb(void * data)1854 _futures_cancel_cb(void *data)
1855 {
1856    Efl_Future_Scheduler *sched = data;
1857    Eina_List *entries = sched->futures;
1858    Efl_Future_Scheduler_Entry *entry;
1859 
1860    efl_event_callback_array_del((Eo *) sched->self, sched->array, sched);
1861    sched->listener = EINA_FALSE;
1862    sched->futures = NULL;
1863 
1864    EINA_LIST_FREE(entries, entry)
1865      {
1866         eina_future_cancel(entry->future);
1867         eina_value_flush(&entry->value);
1868         eina_mempool_free(_efl_future_scheduler_entry_mempool, entry);
1869      }
1870 
1871    if (schedulers_count > 8)
1872      {
1873         free(sched);
1874      }
1875    else
1876      {
1877         eina_trash_push(&schedulers_trash, sched);
1878         schedulers_count++;
1879      }
1880 }
1881 
1882 static Eina_Future_Schedule_Entry *
_efl_event_future_scheduler(Eina_Future_Scheduler * s_sched,Eina_Future_Scheduler_Cb cb,Eina_Future * future,Eina_Value value)1883 _efl_event_future_scheduler(Eina_Future_Scheduler *s_sched,
1884                             Eina_Future_Scheduler_Cb cb,
1885                             Eina_Future *future,
1886                             Eina_Value value)
1887 {
1888    Efl_Future_Scheduler *sched = (Efl_Future_Scheduler *)s_sched;
1889    Efl_Future_Scheduler_Entry *entry;
1890 
1891    entry = eina_mempool_malloc(_efl_future_scheduler_entry_mempool, sizeof(*entry));
1892    EINA_SAFETY_ON_NULL_RETURN_VAL(entry, NULL);
1893 
1894    entry->base.scheduler = &sched->scheduler;
1895    entry->cb = cb;
1896    entry->future = future;
1897    entry->value = value;
1898 
1899    if (!sched->listener)
1900      {
1901         efl_event_callback_array_add((Eo *) sched->self, sched->array, sched);
1902         sched->listener = EINA_TRUE;
1903      }
1904 
1905    sched->futures = eina_list_append(sched->futures, entry);
1906    return &entry->base;
1907 }
1908 
1909 static void
_efl_event_future_recall(Eina_Future_Schedule_Entry * s_entry)1910 _efl_event_future_recall(Eina_Future_Schedule_Entry *s_entry)
1911 {
1912    Efl_Future_Scheduler_Entry *entry = (Efl_Future_Scheduler_Entry *)s_entry;
1913    Efl_Future_Scheduler *sched;
1914    Eina_List *lookup;
1915 
1916    sched = (Efl_Future_Scheduler *) entry->base.scheduler;
1917 
1918    lookup = eina_list_data_find_list(sched->futures, entry);
1919    if (!lookup) return;
1920 
1921    sched->futures = eina_list_remove_list(sched->futures, lookup);
1922    if (!sched->futures)
1923      {
1924         Efl_Object_Data *pd = efl_data_scope_get(sched->self, EFL_OBJECT_CLASS);
1925 
1926         _future_scheduler_cleanup(pd);
1927      }
1928 
1929    eina_value_flush(&entry->value);
1930    eina_mempool_free(_efl_future_scheduler_entry_mempool, entry);
1931 }
1932 
1933 EOLIAN static Eina_Future_Scheduler *
_efl_object_event_future_scheduler_get(const Eo * obj,Efl_Object_Data * pd,Efl_Callback_Array_Item * array)1934 _efl_object_event_future_scheduler_get(const Eo *obj, Efl_Object_Data *pd, Efl_Callback_Array_Item *array)
1935 {
1936    Efl_Object_Extension *ext;
1937    Efl_Future_Scheduler *sched;
1938    unsigned int i;
1939 
1940    if (!array) return NULL;
1941 
1942    ext = _efl_object_extension_need(pd);
1943    EINA_SAFETY_ON_NULL_RETURN_VAL(ext, NULL);
1944 
1945    // First lookup for an existing scheduler that match the provided array
1946    if (!ext->schedulers) ext->schedulers = eina_hash_pointer_new(_futures_cancel_cb);
1947    sched = eina_hash_find(ext->schedulers, &array);
1948    if (sched) return &sched->scheduler;
1949 
1950    // Define all the callback in the array to point to our internal callback,
1951    // making the array ready to use.
1952    for (i = 0; array[i].desc; i++)
1953      array[i].func = _futures_dispatch_cb;
1954 
1955    if (schedulers_count)
1956      {
1957         // Take one out of the trash for faster cycling
1958         sched = eina_trash_pop(&schedulers_trash);
1959         schedulers_count--;
1960      }
1961    else
1962      {
1963         // Need to allocate a new scheduler as none are on standby.
1964         sched = calloc(1, sizeof (Efl_Future_Scheduler));
1965      }
1966    sched->scheduler.schedule = _efl_event_future_scheduler;
1967    sched->scheduler.recall = _efl_event_future_recall;
1968    sched->array = array;
1969    sched->self = obj;
1970 
1971    eina_hash_add(ext->schedulers, &array, sched);
1972 
1973    return &sched->scheduler;
1974 }
1975 
1976 EOAPI EFL_FUNC_BODYV_CONST(efl_event_future_scheduler_get,
1977                            Eina_Future_Scheduler *, 0, EFL_FUNC_CALL(array),
1978                            Efl_Callback_Array_Item *array);
1979 
1980 EOAPI unsigned int
_efl_object_event_callback_count(const Eo * obj EINA_UNUSED,Efl_Object_Data * pd,const Efl_Event_Description * desc)1981 _efl_object_event_callback_count(const Eo *obj EINA_UNUSED,
1982                                  Efl_Object_Data *pd,
1983                                  const Efl_Event_Description *desc)
1984 {
1985    unsigned int r = 0;
1986    unsigned int idx;
1987 
1988    for (idx = pd->callbacks_count ; idx > 0; idx--)
1989      {
1990         Eo_Callback_Description **cb;
1991 
1992         cb = pd->callbacks + idx - 1;
1993 
1994         if ((*cb)->func_array)
1995           {
1996              const Efl_Callback_Array_Item *it;
1997 
1998              for (it = (*cb)->items.item_array; it->func; it++)
1999                {
2000                   if (it->desc > desc) break;
2001                   if (it->desc == desc) r++;
2002                }
2003           }
2004         else
2005           {
2006              if ((*cb)->items.item.desc == desc) r++;
2007           }
2008      }
2009    return r;
2010 }
2011 
2012 EOAPI EFL_FUNC_BODYV_CONST(efl_event_callback_count,
2013                            unsigned int, 0, EFL_FUNC_CALL(desc),
2014                            const Efl_Event_Description *desc);
2015 
2016 static Eina_Bool
_cb_desc_match(const Efl_Event_Description * a,const Efl_Event_Description * b,Eina_Bool legacy_compare)2017 _cb_desc_match(const Efl_Event_Description *a, const Efl_Event_Description *b, Eina_Bool legacy_compare)
2018 {
2019    /* If one is legacy and the other is not, strcmp. Else, pointer compare. */
2020    if (!EINA_UNLIKELY(legacy_compare && (_legacy_event_desc_is(a) != _legacy_event_desc_is(b))))
2021      return (a == b);
2022    return !strcmp(a->name, b->name);
2023 }
2024 
2025 #define EFL_OBJECT_EVENT_CALLBACK_BLOCK(Pd, Desc, Event, Need_Hash)     \
2026   if (Desc == Event)                                                    \
2027     {                                                                   \
2028        if (!(Pd->event_cb_##Event)) return EINA_TRUE;                   \
2029        Need_Hash = EINA_FALSE;                                          \
2030     }                                                                   \
2031 
2032 static inline Eina_Bool
_event_callback_call(Eo * obj_id,Efl_Object_Data * pd,const Efl_Event_Description * desc,void * event_info,Eina_Bool legacy_compare)2033 _event_callback_call(Eo *obj_id, Efl_Object_Data *pd,
2034                      const Efl_Event_Description *desc,
2035                      void *event_info,
2036                      Eina_Bool legacy_compare)
2037 {
2038    Eo_Callback_Description **cb;
2039    Efl_Event_Callback_Frame *restart_lookup = NULL; //a pointer to a frame, which is high up the stack, which we use to restore
2040    Efl_Event ev;
2041    unsigned int idx;
2042    Eina_Bool callback_already_stopped, ret;
2043    Efl_Event_Callback_Frame frame = {
2044       .desc = desc,
2045       .next = NULL,
2046       .idx = 0,
2047       .inserted_before = 0,
2048       .generation = 1,
2049    };
2050    Eina_Bool need_hash = EINA_TRUE;
2051 
2052    if (pd->callbacks_count == 0) return EINA_TRUE;
2053    else EFL_OBJECT_EVENT_CALLBACK_BLOCK(pd, desc, EFL_EVENT_CALLBACK_ADD, need_hash)
2054    else EFL_OBJECT_EVENT_CALLBACK_BLOCK(pd, desc, EFL_EVENT_CALLBACK_DEL, need_hash)
2055    else EFL_OBJECT_EVENT_CALLBACK_BLOCK(pd, desc, EFL_EVENT_DEL, need_hash)
2056    else EFL_OBJECT_EVENT_CALLBACK_BLOCK(pd, desc, EFL_EVENT_INVALIDATE, need_hash)
2057    else EFL_OBJECT_EVENT_CALLBACK_BLOCK(pd, desc, EFL_EVENT_NOREF, need_hash)
2058    else EFL_OBJECT_EVENT_CALLBACK_BLOCK(pd, desc, EFL_EVENT_DESTRUCT, need_hash)
2059 
2060    if (EINA_LIKELY(!legacy_compare && need_hash))
2061      {
2062         unsigned char event_hash;
2063 
2064         event_hash = _pointer_hash((uintptr_t) desc);
2065         if (!(pd->callbacks_mask & (1ULL << event_hash)))
2066           return EINA_TRUE;
2067      }
2068 
2069    if (pd->event_frame)
2070      frame.generation = ((Efl_Event_Callback_Frame*)pd->event_frame)->generation + 1;
2071 
2072    EVENT_STACK_PUSH(pd, &frame);
2073 
2074    callback_already_stopped = pd->callback_stopped;
2075    pd->callback_stopped = EINA_FALSE;
2076    ret = EINA_TRUE;
2077 
2078    ev.object = obj_id;
2079    ev.desc = desc;
2080    ev.info = event_info;
2081 
2082    // Handle event that require to restart where we were in the nested list walking
2083    // relatively unlikely so improve l1 instr cache by using goto
2084    if (desc->restart) goto restart;
2085    else idx = pd->callbacks_count;
2086 restart_back:
2087 
2088    for (; idx > 0; idx--)
2089      {
2090         frame.idx = idx;
2091         cb = pd->callbacks + idx - 1;
2092         if (!(*cb)->delete_me)
2093           {
2094              if ((*cb)->generation >= frame.generation)
2095                continue;
2096 
2097              if ((*cb)->func_array)
2098                {
2099                   const Efl_Callback_Array_Item *it;
2100 
2101                   for (it = (*cb)->items.item_array; it->func; it++)
2102                     {
2103                        // Array callbacks are sorted, break if we are getting to high.
2104                        if (!legacy_compare &&
2105                            ((const unsigned char *) desc < (const unsigned char *) it->desc))
2106                          break;
2107                        if (!_cb_desc_match(it->desc, desc, legacy_compare))
2108                           continue;
2109                        if (!it->desc->unfreezable &&
2110                            (event_freeze_count || pd->event_freeze_count))
2111                           continue;
2112 
2113                        it->func((void *) (*cb)->func_data, &ev);
2114                        /* Abort callback calling if the func says so. */
2115                        if (pd->callback_stopped)
2116                          {
2117                             ret = EINA_FALSE;
2118                             goto end;
2119                          }
2120                     }
2121                }
2122              else
2123                {
2124                   if (!_cb_desc_match((*cb)->items.item.desc, desc, legacy_compare))
2125                     continue;
2126                   if (!(*cb)->items.item.desc->unfreezable &&
2127                       (event_freeze_count || pd->event_freeze_count))
2128                     continue;
2129 
2130                   (*cb)->items.item.func((void *) (*cb)->func_data, &ev);
2131                   /* Abort callback calling if the func says so. */
2132                   if (pd->callback_stopped)
2133                     {
2134                        ret = EINA_FALSE;
2135                        goto end;
2136                     }
2137                }
2138           }
2139         /*
2140          * copy back the idx that might have changed due to restarts, (theoretically only needed with restarts, condition made everything slower)
2141          * additionally adjust to event subscriptions that have been added in a event callback
2142          */
2143         idx = frame.idx + frame.inserted_before;
2144         frame.inserted_before = 0;
2145      }
2146 
2147 end:
2148    // Handling restarting list walking complete exit.
2149    // This must be 1, we copy back the frame idx at the end of the for loop.
2150    // The next iteration then decrements the idx by 1 which results in the effective running idx of that frame beeing 0
2151    if (restart_lookup) restart_lookup->idx = 1;
2152 
2153    EVENT_STACK_POP(pd);
2154 
2155    _eo_callbacks_clear(obj_id, pd);
2156 
2157    pd->callback_stopped = callback_already_stopped;
2158 
2159    return ret;
2160 restart:
2161    // Search for the next frame that has the same event description
2162    for (restart_lookup = frame.next; restart_lookup; restart_lookup = restart_lookup->next)
2163      {
2164         if (restart_lookup->desc == desc) break;
2165      }
2166 
2167    // Ensure that the idx is the next from the previous run, minimum number 0
2168    if (restart_lookup) {
2169      idx = restart_lookup->idx - 1;
2170    } else {
2171      idx = 0;
2172    }
2173    // If this is 0, then we are restarting
2174    if (!idx)
2175      idx = pd->callbacks_count;
2176 
2177    goto restart_back;
2178 }
2179 
2180 EOLIAN static Eina_Bool
_efl_object_event_callback_call(Eo * obj_id,Efl_Object_Data * pd,const Efl_Event_Description * desc,void * event_info)2181 _efl_object_event_callback_call(Eo *obj_id, Efl_Object_Data *pd,
2182             const Efl_Event_Description *desc,
2183             void *event_info)
2184 {
2185    return _event_callback_call(obj_id, pd, desc, event_info, EINA_FALSE);
2186 }
2187 
2188 EOAPI EFL_FUNC_BODYV(efl_event_callback_call,
2189                      Eina_Bool, 0, EFL_FUNC_CALL(desc, event_info),
2190                      const Efl_Event_Description *desc, void *event_info);
2191 
2192 EOLIAN static Eina_Bool
_efl_object_event_callback_legacy_call(Eo * obj_id,Efl_Object_Data * pd,const Efl_Event_Description * desc,void * event_info)2193 _efl_object_event_callback_legacy_call(Eo *obj_id, Efl_Object_Data *pd,
2194             const Efl_Event_Description *desc,
2195             void *event_info)
2196 {
2197    return _event_callback_call(obj_id, pd, desc, event_info, EINA_TRUE);
2198 }
2199 
2200 EOAPI EFL_FUNC_BODYV(efl_event_callback_legacy_call,
2201                      Eina_Bool, 0, EFL_FUNC_CALL(desc, event_info),
2202                      const Efl_Event_Description *desc, void *event_info);
2203 
2204 EOLIAN static void
_efl_object_event_callback_stop(Eo * obj EINA_UNUSED,Efl_Object_Data * pd)2205 _efl_object_event_callback_stop(Eo *obj EINA_UNUSED, Efl_Object_Data *pd)
2206 {
2207    pd->callback_stopped = EINA_TRUE;
2208 }
2209 
2210 static void
_efl_event_forwarder_callback(void * data,const Efl_Event * event)2211 _efl_event_forwarder_callback(void *data, const Efl_Event *event)
2212 {
2213    Eo *new_obj = (Eo *) data;
2214    Eina_Bool ret = EINA_FALSE;
2215 
2216    ret = efl_event_callback_call(new_obj, event->desc, event->info);
2217    if (!ret)
2218      {
2219         efl_event_callback_stop(event->object);
2220      }
2221 }
2222 
2223 static void
_forwarders_list_clean(void * data)2224 _forwarders_list_clean(void *data)
2225 {
2226    Efl_Event_Forwarder *forwarder;
2227    Eina_List *l = data;
2228 
2229    EINA_LIST_FREE(l, forwarder)
2230      {
2231         if (forwarder->source)
2232           {
2233              if (forwarder->inserted)
2234                efl_event_callback_del(forwarder->source,
2235                                       forwarder->desc,
2236                                       _efl_event_forwarder_callback,
2237                                       forwarder->new_obj);
2238              efl_wref_del(forwarder->source, &forwarder->source);
2239           }
2240         free(forwarder);
2241      }
2242 }
2243 
2244 EOLIAN static void
_efl_object_event_callback_forwarder_priority_add(Eo * obj,Efl_Object_Data * pd EINA_UNUSED,const Efl_Event_Description * desc,short priority,Eo * new_obj)2245 _efl_object_event_callback_forwarder_priority_add(Eo *obj, Efl_Object_Data *pd EINA_UNUSED,
2246                                                   const Efl_Event_Description *desc,
2247                                                   short priority,
2248                                                   Eo *new_obj)
2249 {
2250    EO_OBJ_POINTER_RETURN(new_obj, new_data);
2251    EO_OBJ_DONE(new_obj);
2252    Efl_Event_Forwarder *forwarder;
2253    Efl_Object_Extension *ext;
2254    Efl_Object_Data *dpd;
2255    Eina_List *l;
2256 
2257    dpd = efl_data_scope_safe_get(new_obj, EFL_OBJECT_CLASS);
2258    EINA_SAFETY_ON_NULL_RETURN(dpd);
2259 
2260    ext = _efl_object_extension_need(dpd);
2261    EINA_SAFETY_ON_NULL_RETURN(ext);
2262 
2263    // Prevent double insertion for the same object source and event description
2264    EINA_LIST_FOREACH(eina_hash_find(ext->forwarders, desc), l, forwarder)
2265      {
2266         if (forwarder->desc == desc &&
2267             forwarder->new_obj == new_obj &&
2268             forwarder->source == obj)
2269           {
2270              ERR("Forwarder added on '%s' for event '%s' toward '%s' has already been set.\n",
2271                  efl_debug_name_get(obj), desc->name, efl_debug_name_get(new_obj));
2272              return;
2273           }
2274      }
2275 
2276    forwarder = malloc(sizeof (Efl_Event_Forwarder));
2277    EINA_SAFETY_ON_NULL_RETURN(forwarder);
2278    forwarder->desc = desc;
2279    forwarder->priority = priority;
2280    forwarder->new_obj = new_obj;
2281    efl_wref_add(obj, &forwarder->source);
2282 
2283    if (efl_event_callback_count(new_obj, desc) > 0)
2284      {
2285         efl_event_callback_priority_add(obj, desc, priority, _efl_event_forwarder_callback, new_obj);
2286         forwarder->inserted = EINA_TRUE;
2287      }
2288    else
2289      {
2290         forwarder->inserted = EINA_FALSE;
2291      }
2292 
2293    if (!ext->forwarders)
2294      ext->forwarders = eina_hash_pointer_new(_forwarders_list_clean);
2295    eina_hash_list_direct_append(ext->forwarders, forwarder->desc, forwarder);
2296 }
2297 
2298 EOLIAN static void
_efl_object_event_callback_forwarder_del(Eo * obj,Efl_Object_Data * pd EINA_UNUSED,const Efl_Event_Description * desc,Eo * new_obj)2299 _efl_object_event_callback_forwarder_del(Eo *obj, Efl_Object_Data *pd EINA_UNUSED,
2300                                          const Efl_Event_Description *desc,
2301                                          Eo *new_obj)
2302 {
2303    EO_OBJ_POINTER_RETURN(new_obj, new_data);
2304    EO_OBJ_DONE(new_obj);
2305    Efl_Event_Forwarder *forwarder;
2306    Efl_Object_Extension *ext;
2307    Efl_Object_Data *dpd;
2308    Eina_List *l, *tofree = NULL;
2309 
2310    dpd = efl_data_scope_safe_get(new_obj, EFL_OBJECT_CLASS);
2311    if (!dpd) return ;
2312 
2313    ext = dpd->ext;
2314    if (!ext) return ;
2315 
2316    EINA_LIST_FOREACH(eina_hash_find(ext->forwarders, desc), l, forwarder)
2317      {
2318         // Remove dead source at the same time we remove any forwader
2319         if (forwarder->source == obj || forwarder->source == NULL)
2320           tofree = eina_list_append(tofree, forwarder);
2321      }
2322 
2323    EINA_LIST_FREE(tofree, forwarder)
2324      {
2325         if (forwarder->source)
2326           {
2327              if (forwarder->inserted)
2328                efl_event_callback_del(obj, desc, _efl_event_forwarder_callback, new_obj);
2329              efl_wref_del(obj, &forwarder->source);
2330           }
2331         eina_hash_list_remove(ext->forwarders, desc, forwarder);
2332         free(forwarder);
2333      }
2334 }
2335 
2336 EOLIAN static void
_efl_object_event_freeze(Eo * obj EINA_UNUSED,Efl_Object_Data * pd)2337 _efl_object_event_freeze(Eo *obj EINA_UNUSED, Efl_Object_Data *pd)
2338 {
2339    pd->event_freeze_count++;
2340 }
2341 
2342 EOLIAN static void
_efl_object_event_thaw(Eo * obj,Efl_Object_Data * pd)2343 _efl_object_event_thaw(Eo *obj, Efl_Object_Data *pd)
2344 {
2345    if (pd->event_freeze_count > 0)
2346      {
2347         pd->event_freeze_count--;
2348      }
2349    else
2350      {
2351         ERR("Events for object %p have already been thawed.", obj);
2352      }
2353 }
2354 
2355 EOLIAN static int
_efl_object_event_freeze_count_get(const Eo * obj EINA_UNUSED,Efl_Object_Data * pd)2356 _efl_object_event_freeze_count_get(const Eo *obj EINA_UNUSED, Efl_Object_Data *pd)
2357 {
2358    return pd->event_freeze_count;
2359 }
2360 
2361 EOLIAN static void
_efl_object_event_global_freeze(void)2362 _efl_object_event_global_freeze(void)
2363 {
2364    event_freeze_count++;
2365 }
2366 
2367 EOLIAN static void
_efl_object_event_global_thaw(void)2368 _efl_object_event_global_thaw(void)
2369 {
2370    if (event_freeze_count > 0)
2371      {
2372         event_freeze_count--;
2373      }
2374    else
2375      {
2376         ERR("Global events have already been thawed.");
2377      }
2378 }
2379 
2380 EOLIAN static int
_efl_object_event_global_freeze_count_get(void)2381 _efl_object_event_global_freeze_count_get(void)
2382 {
2383    return event_freeze_count;
2384 }
2385 
2386 EOLIAN static Eina_Bool
_efl_object_composite_attach(Eo * parent_id,Efl_Object_Data * pd EINA_UNUSED,Eo * comp_obj_id)2387 _efl_object_composite_attach(Eo *parent_id, Efl_Object_Data *pd EINA_UNUSED, Eo *comp_obj_id)
2388 {
2389    Efl_Object_Optional *opt;
2390    Eo *emb_obj_id = NULL;
2391 
2392    EO_OBJ_POINTER_RETURN_VAL(comp_obj_id, comp_obj, EINA_FALSE);
2393    EO_OBJ_POINTER_GOTO(parent_id, parent, err_parent);
2394 
2395    /* FIXME: composite should fail if domains are different */
2396 
2397    /* Don't composite if we already have a composite object of this type */
2398      {
2399         Eina_List *itr;
2400         EINA_LIST_FOREACH(parent->opt->composite_objects, itr, emb_obj_id)
2401           {
2402              EO_OBJ_POINTER_GOTO(emb_obj_id, emb_obj, err_klass);
2403              if (EINA_UNLIKELY(emb_obj->klass == comp_obj->klass)) goto err_klass;
2404           }
2405         emb_obj_id = NULL;
2406      }
2407 
2408    Efl_Object_Data *comp_pd = efl_data_scope_get(comp_obj_id, EFL_OBJECT_CLASS);
2409 
2410    if (efl_composite_part_is(comp_obj_id))
2411      efl_composite_detach(comp_pd->ext->composite_parent, comp_obj_id);
2412 
2413    /* Set the parent comp on the child. */
2414    _efl_object_extension_need(comp_pd);
2415    comp_pd->ext->composite_parent = parent_id;
2416 
2417    opt = EO_OPTIONAL_COW_WRITE(parent);
2418    opt->composite_objects = eina_list_prepend(opt->composite_objects, comp_obj_id);
2419    EO_OPTIONAL_COW_END(opt, parent);
2420 
2421    if (emb_obj_id) EO_OBJ_DONE(emb_obj_id);
2422    EO_OBJ_DONE(parent_id);
2423    EO_OBJ_DONE(comp_obj_id);
2424    return EINA_TRUE;
2425 
2426 err_klass:
2427    if (emb_obj_id) EO_OBJ_DONE(emb_obj_id);
2428    EO_OBJ_DONE(parent_id);
2429 err_parent:
2430    EO_OBJ_DONE(comp_obj_id);
2431    return EINA_FALSE;
2432 }
2433 
2434 EOLIAN static Eina_Bool
_efl_object_composite_detach(Eo * parent_id,Efl_Object_Data * pd EINA_UNUSED,Eo * comp_obj_id)2435 _efl_object_composite_detach(Eo *parent_id, Efl_Object_Data *pd EINA_UNUSED, Eo *comp_obj_id)
2436 {
2437    Efl_Object_Optional *opt;
2438 
2439    EO_OBJ_POINTER_RETURN_VAL(comp_obj_id, comp_obj, EINA_FALSE);
2440    EO_OBJ_POINTER_GOTO(parent_id, parent, err_parent);
2441 
2442    // unlikely so improve l1 instr cache by using goto
2443    if (!efl_composite_part_is(comp_obj_id)) goto err_part;
2444 
2445    opt = EO_OPTIONAL_COW_WRITE(parent);
2446    opt->composite_objects = eina_list_remove(opt->composite_objects, comp_obj_id);
2447    EO_OPTIONAL_COW_END(opt, parent);
2448 
2449    /* Clear the comp parent on the child. */
2450      {
2451         Efl_Object_Data *comp_pd = efl_data_scope_get(comp_obj_id, EFL_OBJECT_CLASS);
2452         comp_pd->ext->composite_parent = NULL;
2453         _efl_object_extension_noneed(comp_pd);
2454      }
2455 
2456    EO_OBJ_DONE(parent_id);
2457    EO_OBJ_DONE(comp_obj_id);
2458    return EINA_TRUE;
2459 
2460 err_part:
2461    EO_OBJ_DONE(parent_id);
2462 err_parent:
2463    EO_OBJ_DONE(comp_obj_id);
2464    return EINA_FALSE;
2465 }
2466 
2467 EOLIAN static Eina_Bool
_efl_object_composite_part_is(Eo * comp_obj_id EINA_UNUSED,Efl_Object_Data * pd)2468 _efl_object_composite_part_is(Eo *comp_obj_id EINA_UNUSED, Efl_Object_Data *pd)
2469 {
2470    return pd->ext && pd->ext->composite_parent;
2471 }
2472 
2473 /* Eo_Dbg */
2474 EAPI void
efl_dbg_info_free(Efl_Dbg_Info * info)2475 efl_dbg_info_free(Efl_Dbg_Info *info)
2476 {
2477    eina_value_flush(&(info->value));
2478    eina_freeq_ptr_main_add(info, free, sizeof(*info));
2479 }
2480 
2481 static Eina_Bool
_eo_dbg_info_setup(const Eina_Value_Type * type,void * mem)2482 _eo_dbg_info_setup(const Eina_Value_Type *type, void *mem)
2483 {
2484    memset(mem, 0, type->value_size);
2485    return EINA_TRUE;
2486 }
2487 
2488 static Eina_Bool
_eo_dbg_info_flush(const Eina_Value_Type * type EINA_UNUSED,void * _mem)2489 _eo_dbg_info_flush(const Eina_Value_Type *type EINA_UNUSED, void *_mem)
2490 {
2491    Efl_Dbg_Info *mem = *(Efl_Dbg_Info **) _mem;
2492    eina_stringshare_del(mem->name);
2493    eina_value_flush(&(mem->value));
2494    eina_freeq_ptr_main_add(mem, free, sizeof(*mem));
2495    return EINA_TRUE;
2496 }
2497 
2498 static Eina_Bool
_eo_dbg_info_copy(const Eina_Value_Type * type EINA_UNUSED,const void * _src,void * _dst)2499 _eo_dbg_info_copy(const Eina_Value_Type *type EINA_UNUSED, const void *_src, void *_dst)
2500 {
2501    const Efl_Dbg_Info **src = (const Efl_Dbg_Info **) _src;
2502    Efl_Dbg_Info **dst = _dst;
2503 
2504    *dst = calloc(1, sizeof(Efl_Dbg_Info));
2505    if (!*dst) return EINA_FALSE;
2506    (*dst)->name = eina_stringshare_ref((*src)->name);
2507    eina_value_copy(&((*src)->value), &((*dst)->value));
2508    return EINA_TRUE;
2509 }
2510 
2511 static Eina_Bool
_eo_dbg_info_convert_to(const Eina_Value_Type * type EINA_UNUSED,const Eina_Value_Type * convert,const void * type_mem,void * convert_mem)2512 _eo_dbg_info_convert_to(const Eina_Value_Type *type EINA_UNUSED, const Eina_Value_Type *convert, const void *type_mem, void *convert_mem)
2513 {
2514    /* FIXME: For the meanwhile, just use the inner type for the value. */
2515    const Efl_Dbg_Info **src = (const Efl_Dbg_Info **) type_mem;
2516    if (convert == EINA_VALUE_TYPE_STRINGSHARE ||
2517        convert == EINA_VALUE_TYPE_STRING)
2518      {
2519         Eina_Bool ret;
2520         const char *other_mem;
2521         char *inner_val = eina_value_to_string(&(*src)->value);
2522         other_mem = inner_val;
2523         ret = eina_value_type_pset(convert, convert_mem, &other_mem);
2524         eina_freeq_ptr_main_add(inner_val, free, 0);
2525         return ret;
2526      }
2527 
2528    eina_error_set(EINA_ERROR_VALUE_FAILED);
2529    return EINA_FALSE;
2530 }
2531 
2532 static Eina_Bool
_eo_dbg_info_pset(const Eina_Value_Type * type EINA_UNUSED,void * _mem,const void * _ptr)2533 _eo_dbg_info_pset(const Eina_Value_Type *type EINA_UNUSED, void *_mem, const void *_ptr)
2534 {
2535    Efl_Dbg_Info **mem = _mem;
2536    if (*mem) free(*mem);
2537    *mem = (void *) _ptr;
2538    return EINA_TRUE;
2539 }
2540 
2541 static Eina_Bool
_eo_dbg_info_pget(const Eina_Value_Type * type EINA_UNUSED,const void * _mem,void * _ptr)2542 _eo_dbg_info_pget(const Eina_Value_Type *type EINA_UNUSED, const void *_mem, void *_ptr)
2543 {
2544    Efl_Dbg_Info **ptr = _ptr;
2545    *ptr = (void *) _mem;
2546    return EINA_TRUE;
2547 }
2548 
2549 static const Eina_Value_Type _EFL_DBG_INFO_TYPE = {
2550    EINA_VALUE_TYPE_VERSION,
2551    sizeof(Efl_Dbg_Info *),
2552    "Efl_Dbg_Info_Ptr",
2553    _eo_dbg_info_setup,
2554    _eo_dbg_info_flush,
2555    _eo_dbg_info_copy,
2556    NULL,
2557    _eo_dbg_info_convert_to,
2558    NULL,
2559    NULL,
2560    _eo_dbg_info_pset,
2561    _eo_dbg_info_pget
2562 };
2563 
2564 EAPI const Eina_Value_Type *EFL_DBG_INFO_TYPE = &_EFL_DBG_INFO_TYPE;
2565 
2566 
2567 /* EOF event callbacks */
2568 
2569 /* EFL_OBJECT_CLASS stuff */
2570 #define MY_CLASS EFL_OBJECT_CLASS
2571 
2572 static Eina_Value
_efl_future_cb(void * data,const Eina_Value value,const Eina_Future * dead_future)2573 _efl_future_cb(void *data, const Eina_Value value, const Eina_Future *dead_future)
2574 {
2575    Efl_Future_Pending *pending = data;
2576    Eina_Value ret = value;
2577    const Eo *o;
2578    Efl_Object_Data *pd;
2579 
2580    EINA_SAFETY_ON_NULL_GOTO(pending, err);
2581    o = pending->o;
2582    pd = efl_data_scope_get(o, EFL_OBJECT_CLASS);
2583    EINA_SAFETY_ON_NULL_GOTO(pd, err);
2584 
2585    pd->pending_futures = eina_inlist_remove(pd->pending_futures,
2586                                             EINA_INLIST_GET(pending));
2587    efl_ref(o);
2588    EASY_FUTURE_DISPATCH(ret, value, dead_future, &pending->desc, (void*) o, (void*) pending->desc.data);
2589    efl_unref(o);
2590    _efl_pending_future_free(pending);
2591 
2592    return ret;
2593 
2594  err:
2595    eina_value_setup(&ret, EINA_VALUE_TYPE_ERROR);
2596    eina_value_set(&ret, ENOMEM);
2597    return ret;
2598 }
2599 
2600 EOAPI Eina_Future_Desc
efl_future_cb_from_desc(const Eo * o,const Efl_Future_Cb_Desc desc)2601 efl_future_cb_from_desc(const Eo *o, const Efl_Future_Cb_Desc desc)
2602 {
2603    Efl_Future_Pending *pending = NULL;
2604    Eina_Future **storage = NULL;
2605    Efl_Object_Data *pd;
2606    Eina_Bool invalidate;
2607 
2608    EINA_SAFETY_ON_NULL_GOTO(o, end);
2609    pd = efl_data_scope_get(o, EFL_OBJECT_CLASS);
2610    EINA_SAFETY_ON_NULL_GOTO(pd, end);
2611    EO_OBJ_POINTER_GOTO(o, eo_obj, end);
2612    invalidate = eo_obj->invalidate;
2613    EO_OBJ_DONE(o);
2614    EINA_SAFETY_ON_TRUE_GOTO(invalidate, end);
2615    pending = _efl_pending_future_new();
2616    EINA_SAFETY_ON_NULL_GOTO(pending, end);
2617    memcpy(&pending->desc, &desc, sizeof(Efl_Future_Cb_Desc));
2618    pending->o = o;
2619    pending->future = NULL;
2620    if (!pending->desc.storage) pending->desc.storage = &pending->future;
2621    pd->pending_futures = eina_inlist_append(pd->pending_futures,
2622                                             EINA_INLIST_GET(pending));
2623    storage = pending->desc.storage;
2624  end:
2625    return (Eina_Future_Desc){ .cb = _efl_future_cb, .data = pending, .storage = storage };
2626 }
2627 
2628 EOAPI Eina_Future *
efl_future_chain_array(Eo * obj,Eina_Future * prev,const Efl_Future_Cb_Desc descs[])2629 efl_future_chain_array(Eo *obj,
2630                        Eina_Future *prev,
2631                        const Efl_Future_Cb_Desc descs[])
2632 {
2633    ssize_t i = -1;
2634    Eina_Future *f = prev;
2635 
2636    for (i = 0; descs[i].success || descs[i].error || descs[i].free || descs[i].success_type; i++)
2637      {
2638         Eina_Future_Desc eina_desc = efl_future_cb_from_desc(obj, descs[i]);
2639         f = eina_future_then_from_desc(f, eina_desc);
2640         EINA_SAFETY_ON_NULL_GOTO(f, err);
2641      }
2642 
2643    return f;
2644 
2645  err:
2646    /*
2647      There's no need to cancel the futures, since eina_future_then_from_desc()
2648      will cancel the whole chain in case of failure.
2649      All we need to do is to free the remaining descs
2650    */
2651    for (i = i + 1; descs[i].error || descs[i].free; i++)
2652      {
2653         if (descs[i].error)
2654           {
2655              Eina_Value r = descs[i].error(obj, (void*) descs[i].data, ENOMEM);
2656              eina_value_flush(&r);
2657           }
2658         if (descs[i].free) descs[i].free(obj, (void*) descs[i].data, NULL);
2659      }
2660    return NULL;
2661 }
2662 
2663 EOLIAN static Eo *
_efl_object_constructor(Eo * obj,Efl_Object_Data * pd EINA_UNUSED)2664 _efl_object_constructor(Eo *obj, Efl_Object_Data *pd EINA_UNUSED)
2665 {
2666    DBG("%p - %s.", obj, efl_class_name_get(obj));
2667 
2668    _eo_condtor_done(obj);
2669 
2670    return obj;
2671 }
2672 
2673 EOLIAN static void
_efl_object_destructor(Eo * obj,Efl_Object_Data * pd)2674 _efl_object_destructor(Eo *obj, Efl_Object_Data *pd)
2675 {
2676    _Eo_Object *obj_child;
2677    Eina_Inlist *l;
2678    Efl_Object_Extension *ext;
2679    _Eo_Object *obj_data2 = NULL;
2680 
2681    DBG("%p - %s.", obj, efl_class_name_get(obj));
2682 
2683    // special removal - remove from children list by hand after getting
2684    // child handle in case unparent method is overridden and does
2685    // extra things like removes other children too later on in the list
2686    // this is a goto because more often than not objects do not have children
2687    // so it's unlikely they will need the child cleanup code to so to have
2688    // better l1 cache instruction coherency, move this to the end
2689    if (pd->children) goto children;
2690 children_back:
2691 
2692    // If we are a composite object, detach children. it is quite unlikely
2693    // we are a composite object, so put the core of this handling
2694    // at the end out of l1 cache prefetch
2695      {
2696         EO_OBJ_POINTER_RETURN(obj, obj_data);
2697         obj_data2 = obj_data;
2698         if (obj_data->opt->composite_objects) goto composite_obj;
2699 composite_obj_back:
2700         EO_OBJ_DONE(obj);
2701      }
2702 
2703    if (pd->ext && pd->ext->composite_parent)
2704      efl_composite_detach(pd->ext->composite_parent, obj);
2705 
2706    // parent still being here is unlikely, so move error handling out of the
2707    // code execution path
2708    if (pd->parent) goto err_parent;
2709 err_parent_back:
2710 
2711    // this isn't 100% correct, as the object is still "slightly" alive at this
2712    // point (so efl_destructed_is() returns false), but triggering the
2713    // "destruct" event here is the simplest, safest solution.
2714    if (EINA_UNLIKELY(pd->event_cb_EFL_EVENT_DESTRUCT))
2715      _event_callback_call(obj, pd, EFL_EVENT_DESTRUCT, NULL, EINA_FALSE);
2716 
2717    // remove generic data after this final event, in case they are used in a cb
2718    _eo_generic_data_del_all(obj, pd);
2719    _eo_callback_remove_all(pd);
2720 
2721    _wref_destruct(pd);
2722 
2723    ext = pd->ext;
2724    // it is rather likely we dont have any extension section for most objects
2725    // so return immediately here to avoid pulling in more instructions to
2726    // the 1l cache if we can
2727    if (!ext)
2728      {
2729         _eo_condtor_done(obj);
2730         return;
2731      }
2732    eina_stringshare_del(ext->name);
2733    ext->name = NULL;
2734    eina_stringshare_del(ext->comment);
2735    ext->comment = NULL;
2736    _efl_object_extension_noneed(pd);
2737    _eo_condtor_done(obj);
2738    return;
2739 
2740 children:
2741    ERR("Object %p of type '%s' is still holding child at time of destruction.\n",
2742        obj, efl_class_name_get(obj));
2743    EINA_INLIST_FOREACH_SAFE(pd->children, l, obj_child)
2744      {
2745         Eo *child;
2746 
2747         child = _eo_obj_id_get(obj_child);
2748         efl_parent_set(child, NULL);
2749      }
2750    goto children_back;
2751 
2752 composite_obj:
2753      {
2754         Eina_List *itr, *next;
2755         Eo *emb_obj_id;
2756 
2757         EINA_LIST_FOREACH_SAFE(obj_data2->opt->composite_objects, itr, next, emb_obj_id)
2758           {
2759              efl_composite_detach(obj, emb_obj_id);
2760           }
2761      }
2762    goto composite_obj_back;
2763 
2764 err_parent:
2765    if (EINA_LIKELY(!pd->allow_parent_unref))
2766      ERR("Object '%p' of type '%s' still has a parent at the time of destruction.", obj, efl_class_name_get(obj));
2767    efl_parent_set(obj, NULL);
2768    goto err_parent_back;
2769 }
2770 
2771 EOLIAN static void
_efl_object_allow_parent_unref_set(Eo * obj_id,Efl_Object_Data * pd,Eina_Bool allow)2772 _efl_object_allow_parent_unref_set(Eo *obj_id, Efl_Object_Data *pd, Eina_Bool allow)
2773 {
2774    EO_OBJ_POINTER_RETURN(obj_id, obj);
2775    obj->allow_parent_unref = !!allow;
2776    pd->allow_parent_unref = !!allow;
2777    EO_OBJ_DONE(obj_id);
2778 }
2779 
2780 EOLIAN static Eina_Bool
_efl_object_allow_parent_unref_get(const Eo * obj_id EINA_UNUSED,Efl_Object_Data * pd)2781 _efl_object_allow_parent_unref_get(const Eo *obj_id EINA_UNUSED, Efl_Object_Data *pd)
2782 {
2783    return pd->allow_parent_unref;
2784 }
2785 
2786 EAPI void
___efl_auto_unref_set(Eo * obj_id,Eina_Bool enable)2787 ___efl_auto_unref_set(Eo *obj_id, Eina_Bool enable)
2788 {
2789    // Write-only property
2790    EO_OBJ_POINTER_RETURN(obj_id, obj);
2791    obj->auto_unref = enable ? 1 : 0;
2792    EO_OBJ_DONE(obj_id);
2793 }
2794 
2795 EOLIAN static Eo *
_efl_object_finalize(Eo * obj,Efl_Object_Data * pd EINA_UNUSED)2796 _efl_object_finalize(Eo *obj, Efl_Object_Data *pd EINA_UNUSED)
2797 {
2798    return obj;
2799 }
2800 
2801 EOLIAN static void
_efl_object_class_constructor(Efl_Class * klass EINA_UNUSED)2802 _efl_object_class_constructor(Efl_Class *klass EINA_UNUSED)
2803 {
2804    event_freeze_count = 0;
2805    _legacy_events_hash = eina_hash_stringshared_new(_legacy_events_hash_free_cb);
2806 
2807    _eo_callback_mempool =
2808       eina_mempool_add("chained_mempool", NULL, NULL,
2809                        sizeof(Eo_Callback_Description), 256);
2810 
2811    _efl_pending_future_mempool =
2812       eina_mempool_add("chained_mempool", NULL, NULL,
2813                        sizeof(Efl_Future_Pending), 256);
2814 
2815    _efl_future_scheduler_entry_mempool =
2816      eina_mempool_add("chained_mempool", NULL, NULL,
2817                       sizeof(Efl_Future_Scheduler_Entry), 256);
2818 
2819    _eo_nostep_alloc = !!getenv("EO_NOSTEP_ALLOC");
2820 }
2821 
2822 EOLIAN static void
_efl_object_class_destructor(Efl_Class * klass EINA_UNUSED)2823 _efl_object_class_destructor(Efl_Class *klass EINA_UNUSED)
2824 {
2825    eina_mempool_del(_efl_future_scheduler_entry_mempool);
2826    eina_mempool_del(_efl_pending_future_mempool);
2827    eina_mempool_del(_eo_callback_mempool);
2828    eina_hash_free(_legacy_events_hash);
2829 }
2830 
2831 #define EFL_OBJECT_EXTRA_OPS \
2832    EFL_OBJECT_OP_FUNC(efl_event_callback_priority_add, _efl_object_event_callback_priority_add), \
2833    EFL_OBJECT_OP_FUNC(efl_event_callback_del, _efl_object_event_callback_del), \
2834    EFL_OBJECT_OP_FUNC(efl_event_callback_array_priority_add, _efl_object_event_callback_array_priority_add), \
2835    EFL_OBJECT_OP_FUNC(efl_event_callback_array_del, _efl_object_event_callback_array_del), \
2836    EFL_OBJECT_OP_FUNC(efl_event_callback_call, _efl_object_event_callback_call), \
2837    EFL_OBJECT_OP_FUNC(efl_event_callback_legacy_call, _efl_object_event_callback_legacy_call), \
2838    EFL_OBJECT_OP_FUNC(efl_event_future_scheduler_get, _efl_object_event_future_scheduler_get), \
2839    EFL_OBJECT_OP_FUNC(efl_event_callback_count, _efl_object_event_callback_count), \
2840    EFL_OBJECT_OP_FUNC(efl_dbg_info_get, _efl_object_dbg_info_get), \
2841    EFL_OBJECT_OP_FUNC(efl_wref_add, _efl_object_wref_add), \
2842    EFL_OBJECT_OP_FUNC(efl_wref_del, _efl_object_wref_del), \
2843    EFL_OBJECT_OP_FUNC(efl_key_data_set, _efl_object_key_data_set), \
2844    EFL_OBJECT_OP_FUNC(efl_key_data_get, _efl_object_key_data_get), \
2845    EFL_OBJECT_OP_FUNC(efl_key_ref_set, _efl_object_key_ref_set), \
2846    EFL_OBJECT_OP_FUNC(efl_key_ref_get, _efl_object_key_ref_get), \
2847    EFL_OBJECT_OP_FUNC(efl_key_wref_set, _efl_object_key_wref_set), \
2848    EFL_OBJECT_OP_FUNC(efl_key_wref_get, _efl_object_key_wref_get), \
2849    EFL_OBJECT_OP_FUNC(efl_key_value_set, _efl_object_key_value_set), \
2850    EFL_OBJECT_OP_FUNC(efl_key_value_get, _efl_object_key_value_get) \
2851 
2852 #include "efl_object.eo.c"
2853