summaryrefslogtreecommitdiff
path: root/src/lib/eo/eo_event_emitter.c
blob: 89e4cd25298fc956d20512cdc8974de793daff67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "eo_event_emitter.h"

typedef struct {
   Event_Node *node;
} Event_Node_Array_Wrapper;

//two compare functions for sorting two inarrays

static int
_event_node_cb(const void *a, const void *b)
{
   const Event_Node_Array_Wrapper *wa = a, *wb = b;

   if (wa->node->desc < wb->node->desc) return -1;
   else if (wa->node->desc == wb->node->desc) return 0;
   else return 1;
}

static int
_event_node_legacy_cb(const void *a, const void *b)
{
   const Event_Node_Array_Wrapper *wa = a, *wb = b;

   if (wa->node->desc < wb->node->desc) return -1;
   else if ((wa->node->desc->legacy_is != wb->node->desc->legacy_is) && wa->node->desc == wb->node->desc) return 0;
   else if (!strcmp(wa->node->desc->name, wb->node->desc->name)) return 0;
   else return 1;
}

static int
_callback_node_cb(const void *a, const void *b)
{
   const Callback_Node *ca = a, *cb = b;

   if (ca->priority < cb->priority) return 1;
   else if (ca->priority == cb->priority) return 0;
   else  return -1;
}

static int
_callback_node_data_cb(const void *a, const void *b)
{
   const Callback_Node *ca = a, *cb = b;

  if ((ca->func == cb->func) &&
      (ca->data == cb->data))
    return 0;
  return 1;
}

static unsigned int
_generation_latest(Eo_Event_Emitter *emitter)
{
   if (emitter->most_recent) return emitter->most_recent->generation;
   return 0;
}

static inline Event_Node*
_emitter_ev_node_get(Eo_Event_Emitter *emitter, const Efl_Event_Description *desc, int *index, Eina_Bool legacy_compatibility)
{
   int event_index;
   Event_Node node = { .desc = desc };
   Event_Node_Array_Wrapper tmp = { &node };
   Event_Node_Array_Wrapper *res;

   if (legacy_compatibility)
     event_index = eina_inarray_search(emitter->callbacks, &tmp, _event_node_legacy_cb);
   else
     event_index = eina_inarray_search_sorted(emitter->callbacks, &tmp, _event_node_cb);

   if (index) *index = event_index;
   if (event_index == -1) return NULL;

   res = eina_inarray_nth(emitter->callbacks, event_index);

   return res->node;
}

#define NODE_USAGE(x, cb) \
if (ev_node->simple) \
  { \
     Callback_Node *node = &ev_node->callback.simple; \
     do { \
       cb \
     } while(0); \
  } \
else \
  { \
    for (; x > 0; x--) \
      { \
         Callback_Node *node = eina_inarray_nth(ev_node->callback.chain, x - 1); \
         cb \
      } \
  } \

Eina_Bool
eo_event_emitter_emit(Eo_Event_Emitter *emitter, const Efl_Event *event_info, Eina_Bool legacy)
{
   int event_index;
   Eo_Event_Emitter_Frame frame, *restart_frame = NULL;
   Event_Node *ev_node = NULL;

   ev_node = _emitter_ev_node_get(emitter, event_info->desc, NULL, legacy);

   if (!ev_node) return EINA_TRUE;

   frame.generation = _generation_latest(emitter) + 1;
   if (!ev_node->simple)
     frame.runner_index = eina_inarray_count(ev_node->callback.chain);
   else
     frame.runner_index = 1;

   if (event_info->desc->restart)
     {
        for (Eo_Event_Emitter_Frame *oframe = emitter->most_recent ; oframe; oframe = oframe->prev)
          {
             if (oframe->desc == event_info->desc)
               {
                  frame.runner_index = oframe->runner_index;
               }
          }
     }

   frame.desc = event_info->desc;
   frame.prev = emitter->most_recent;
   frame.cleanup = EINA_FALSE;
   frame.terminate = EINA_FALSE;
   emitter->most_recent = &frame;

   NODE_USAGE(frame.runner_index,
     {
        //ignore callbacks that are added later
        if (node->generation >= frame.generation) continue;

        node->func((void*)node->data, event_info);

        if (frame.terminate) break;
     }
   )

   //restore the old frame to make this other thing start at the end again like a new run
   if (restart_frame)
     {
        if (!ev_node->simple)
          restart_frame->runner_index = eina_inarray_count(ev_node->callback.chain) - 1;
        else
          restart_frame->runner_index = 1;
        restart_frame = 0;
     }

   emitter->most_recent = frame.prev;

   if (frame.cleanup)
     {
        int highest_generation = 0;
        int x;

        if (!ev_node->simple)
          x = eina_inarray_count(ev_node->callback.chain) - 1;
        else
          x = 0;

        for (Eo_Event_Emitter_Frame *frame = emitter->most_recent ; frame; frame = frame->prev)
          {
             if (frame->desc == event_info->desc)
               {
                  highest_generation = frame->generation;
               }
          }

        NODE_USAGE(x, {
          if (node->generation == UINT_MAX && highest_generation == 0)
            {
               eo_event_emitter_unregister(emitter, node->func, event_info->desc, node->data);
            }
          else if (node->generation > highest_generation)
            {
               node->generation = highest_generation;
            }
        })
     }
   return !frame.terminate;
}


#define NODE_FILL(node, emitter, CB, PRIO, DATA) \
  (node)->func = CB; \
  (node)->data = DATA; \
  (node)->priority = PRIO; \
  (node)->generation = _generation_latest(emitter); \

void
eo_event_emitter_register(Eo_Event_Emitter *emitter, Efl_Event_Cb cb, const Efl_Event_Description *ev, Efl_Callback_Priority priority, const void *data)
{
   Callback_Node *node;
   Event_Node *ev_node;
   Callback_Node new_callback_node;
   int cb_index;

   ev_node = _emitter_ev_node_get(emitter, ev, NULL, EINA_FALSE);
   if (!ev_node)
     {
        Event_Node_Array_Wrapper wrapper = { calloc(1, sizeof(Event_Node)) };

        eina_inarray_insert_sorted(emitter->callbacks, &wrapper, _event_node_cb);

        wrapper.node->desc = ev;
        wrapper.node->simple = EINA_TRUE;

        NODE_FILL(&wrapper.node->callback.simple, emitter, cb, priority, data);

        return;
     }

   NODE_FILL(&new_callback_node, emitter, cb, priority, data);

   if (ev_node->simple)
     {
        Eina_Inarray *soon_chain = eina_inarray_new(sizeof(Callback_Node), 3);

        //insertion order does not matter at all here, chain is empty
        eina_inarray_insert_sorted(soon_chain, &ev_node->callback.simple, _callback_node_cb);

        ev_node->simple = EINA_FALSE;
        ev_node->callback.chain = soon_chain;
     }

   cb_index = eina_inarray_search_sorted(ev_node->callback.chain, &new_callback_node, _callback_node_cb);
   //we are searching now for the first element that has a different element in priority, to the higher side
   while(cb_index + 1 < eina_inarray_count(ev_node->callback.chain))
     {
        Callback_Node *tmp = eina_inarray_nth(ev_node->callback.chain, cb_index + 1);
        if (tmp->priority != new_callback_node.priority)
          break;
        cb_index ++;
     }
   eina_inarray_insert_at(ev_node->callback.chain, cb_index + 1, &new_callback_node);

   for (Eo_Event_Emitter_Frame *frame = emitter->most_recent ; frame; frame = frame->prev)
     {
        if (frame->desc == ev)
          {
             if (frame->runner_index >= cb_index + 1) frame->runner_index ++;
             frame->cleanup = EINA_TRUE;
          }
     }
}

void
eo_event_emitter_unregister(Eo_Event_Emitter *emitter, Efl_Event_Cb cb, const Efl_Event_Description *ev, const void *data)
{
   int ev_index = -1, cb_index = -1;
   Event_Node *ev_node, *cb_node;
   Callback_Node  tmp = { .func = cb, .data = data };
   Callback_Node *node = NULL;
   Eina_Bool disable_deletion = EINA_FALSE;

   ev_node = _emitter_ev_node_get(emitter, ev, &ev_index, EINA_FALSE);

   if (!ev_node) return;

   if (!ev_node->simple)
     {
        cb_index = eina_inarray_search(ev_node->callback.chain, &tmp, _callback_node_data_cb);
        node = eina_inarray_nth(ev_node->callback.chain, cb_index);
     }
   else
     {
        node = &ev_node->callback.simple;
     }

   if (!node) return;

   for (Eo_Event_Emitter_Frame *frame = emitter->most_recent ; frame; frame = frame->prev)
     {
        if (frame->desc == ev)
          {
             disable_deletion = EINA_TRUE;
             frame->cleanup = EINA_TRUE;
          }
     }

   if (disable_deletion)
     {
        if (node->generation == UINT_MAX) return; //this node is already flagged for deletion

        //this generation will cause the cb to be deleted once
        node->generation = UINT_MAX;
     }
   else
     {
        if (!ev_node->simple)
          eina_inarray_remove_at(ev_node->callback.chain, cb_index);
        if ((ev_node->simple) || (!eina_inarray_count(ev_node->callback.chain)))
          {
             eina_inarray_remove_at(emitter->callbacks, ev_index);
             if (!ev_node->simple) eina_inarray_free(ev_node->callback.chain);
             free(ev_node);
          }
     }
}

void
eo_event_emitter_init(Eo_Event_Emitter *emitter)
{
   emitter->callbacks = eina_inarray_new(sizeof(Event_Node_Array_Wrapper), 2);
   emitter->garbage = NULL;
   emitter->most_recent = NULL;
}

void
eo_event_emitter_free(Eo_Event_Emitter *emitter)
{
   eina_inarray_free(emitter->callbacks);
}

void
eo_event_emitter_stop(Eo_Event_Emitter *emitter)
{
   if (emitter->most_recent)
     {
        if (!emitter->most_recent->desc->unfreezable)
          emitter->most_recent->terminate = EINA_TRUE;
     }
}