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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
|
#include <Eina.h>
#include "Eo.h"
#include "eo_private.h"
#include "config.h"
static int event_freeze_count = 0;
typedef struct
{
Eina_Inlist *generic_data;
Eo ***wrefs;
Eina_Inlist *callbacks;
int walking_list;
int event_freeze_count;
Eina_Bool deletions_waiting : 1;
} Private_Data;
typedef struct
{
EINA_INLIST;
Eina_Stringshare *key;
void *data;
eo_base_data_free_func free_func;
} Eo_Generic_Data_Node;
static void
_eo_generic_data_node_free(Eo_Generic_Data_Node *node)
{
eina_stringshare_del(node->key);
if (node->free_func)
node->free_func(node->data);
free(node);
}
static void
_eo_generic_data_del_all(Private_Data *pd)
{
Eina_Inlist *nnode;
Eo_Generic_Data_Node *node = NULL;
EINA_INLIST_FOREACH_SAFE(pd->generic_data, nnode, node)
{
pd->generic_data = eina_inlist_remove(pd->generic_data,
EINA_INLIST_GET(node));
_eo_generic_data_node_free(node);
}
}
static void
_data_set(Eo *obj, void *class_data, va_list *list)
{
Private_Data *pd = class_data;
const char *key = va_arg(*list, const char *);
const void *data = va_arg(*list, const void *);
eo_base_data_free_func free_func = va_arg(*list, eo_base_data_free_func);
Eo_Generic_Data_Node *node;
if (!key) return;
eo_do(obj, eo_base_data_del(key));
node = malloc(sizeof(Eo_Generic_Data_Node));
node->key = eina_stringshare_add(key);
node->data = (void *) data;
node->free_func = free_func;
pd->generic_data = eina_inlist_prepend(pd->generic_data,
EINA_INLIST_GET(node));
}
static void
_data_get(const Eo *obj EINA_UNUSED, const void *class_data, va_list *list)
{
/* We don't really change it... */
Private_Data *pd = (Private_Data *) class_data;
const char *key = va_arg(*list, const char *);
void **data = va_arg(*list, void **);
Eo_Generic_Data_Node *node;
if (!data) return;
*data = NULL;
if (!key) return;
EINA_INLIST_FOREACH(pd->generic_data, node)
{
if (!strcmp(node->key, key))
{
pd->generic_data =
eina_inlist_promote(pd->generic_data, EINA_INLIST_GET(node));
*data = node->data;
return;
}
}
}
static void
_data_del(Eo *obj EINA_UNUSED, void *class_data, va_list *list)
{
Private_Data *pd = class_data;
const char *key = va_arg(*list, const char *);
Eo_Generic_Data_Node *node;
if (!key) return;
EINA_INLIST_FOREACH(pd->generic_data, node)
{
if (!strcmp(node->key, key))
{
pd->generic_data = eina_inlist_remove(pd->generic_data,
EINA_INLIST_GET(node));
_eo_generic_data_node_free(node);
return;
}
}
}
/* Weak reference. */
static inline size_t
_wref_count(Private_Data *pd)
{
size_t count = 0;
if (!pd->wrefs)
return 0;
Eo ***itr;
for (itr = pd->wrefs ; *itr ; itr++)
count++;
return count;
}
static void
_wref_add(const Eo *obj, const void *class_data, va_list *list)
{
Private_Data *pd = (Private_Data *) class_data;
size_t count;
Eo **wref = va_arg(*list, Eo **);
count = _wref_count(pd);
count += 1; /* New wref. */
pd->wrefs= realloc(pd->wrefs, sizeof(*pd->wrefs) * (count + 1));
pd->wrefs[count - 1] = wref;
pd->wrefs[count] = NULL;
*wref = (Eo *) obj;
}
static void
_wref_del(const Eo *obj, const void *class_data, va_list *list)
{
Private_Data *pd = (Private_Data *) class_data;
size_t count;
Eo **wref = va_arg(*list, Eo **);
if (*wref != obj)
{
ERR("Wref is a weak ref to %p, while this function was called on %p.",
*wref, obj);
return;
}
if (!pd->wrefs)
{
ERR("There are no weak refs for object %p", obj);
*wref = NULL;
return;
}
/* Move the last item in the array instead of the current wref. */
count = _wref_count(pd);
{
Eo ***itr;
for (itr = pd->wrefs ; *itr ; itr++)
{
if (*itr == wref)
{
*itr = pd->wrefs[count - 1];
break;
}
}
if (!*itr)
{
ERR("Wref %p is not associated with object %p", wref, obj);
*wref = NULL;
return;
}
}
if (count > 1)
{
// No count--; because of the NULL that is not included in the count. */
pd->wrefs = realloc(pd->wrefs, sizeof(*pd->wrefs) * count);
pd->wrefs[count - 1] = NULL;
}
else
{
free(pd->wrefs);
pd->wrefs = NULL;
}
*wref = NULL;
}
static inline void
_wref_destruct(Private_Data *pd)
{
Eo ***itr;
if (!pd->wrefs)
return;
for (itr = pd->wrefs ; *itr ; itr++)
{
**itr = NULL;
}
free(pd->wrefs);
}
/* EOF Weak reference. */
/* Event callbacks */
/* Callbacks */
typedef struct
{
EINA_INLIST;
const Eo_Event_Description *event;
Eo_Event_Cb func;
void *func_data;
Eo_Callback_Priority priority;
Eina_Bool delete_me : 1;
} Eo_Callback_Description;
/* Actually remove, doesn't care about walking list, or delete_me */
static void
_eo_callback_remove(Private_Data *pd, Eo_Callback_Description *cb)
{
pd->callbacks = eina_inlist_remove(pd->callbacks,
EINA_INLIST_GET(cb));
free(cb);
}
/* Actually remove, doesn't care about walking list, or delete_me */
static void
_eo_callback_remove_all(Private_Data *pd)
{
Eina_Inlist *initr;
Eo_Callback_Description *cb = NULL;
EINA_INLIST_FOREACH_SAFE(pd->callbacks, initr, cb)
{
_eo_callback_remove(pd, cb);
}
}
static void
_eo_callbacks_clear(Private_Data *pd)
{
Eina_Inlist *itn;
Eo_Callback_Description *cb = NULL;
/* Abort if we are currently walking the list. */
if (pd->walking_list > 0)
return;
/* If there are no deletions waiting. */
if (!pd->deletions_waiting)
return;
pd->deletions_waiting = EINA_FALSE;
EINA_INLIST_FOREACH_SAFE(pd->callbacks, itn, cb)
{
if (cb->delete_me)
{
_eo_callback_remove(pd, cb);
}
}
}
static int
_callback_priority_cmp(const void *_a, const void *_b)
{
const Eo_Callback_Description *a, *b;
a = (const Eo_Callback_Description *) _a;
b = (const Eo_Callback_Description *) _b;
if (a->priority < b->priority)
return -1;
else
return 1;
}
static void
_ev_cb_priority_add(Eo *obj, void *class_data, va_list *list)
{
Private_Data *pd = (Private_Data *) class_data;
const Eo_Event_Description *desc = va_arg(*list, const Eo_Event_Description *);
Eo_Callback_Priority priority = va_arg(*list, int);
Eo_Event_Cb func = va_arg(*list, Eo_Event_Cb);
const void *data = va_arg(*list, const void *);
Eo_Callback_Description *cb = calloc(1, sizeof(*cb));
cb->event = desc;
cb->func = func;
cb->func_data = (void *) data;
cb->priority = priority;
pd->callbacks = eina_inlist_sorted_insert(pd->callbacks,
EINA_INLIST_GET(cb), _callback_priority_cmp);
eo_do(obj, eo_event_callback_call(EO_EV_CALLBACK_ADD, desc, NULL));
}
static void
_ev_cb_del(Eo *obj, void *class_data, va_list *list)
{
Private_Data *pd = (Private_Data *) class_data;
const Eo_Event_Description *desc = va_arg(*list, const Eo_Event_Description *);
Eo_Event_Cb func = va_arg(*list, Eo_Event_Cb);
void *user_data = va_arg(*list, void *);
Eo_Callback_Description *cb;
EINA_INLIST_FOREACH(pd->callbacks, cb)
{
if ((cb->event == desc) && (cb->func == func) &&
(cb->func_data == user_data))
{
cb->delete_me = EINA_TRUE;
pd->deletions_waiting = EINA_TRUE;
_eo_callbacks_clear(pd);
eo_do(obj, eo_event_callback_call(EO_EV_CALLBACK_DEL, desc, NULL));
return;
}
}
ERR("Callback of object %p with function %p and data %p not found.", obj, func, user_data);
}
static void
_ev_cb_call(const Eo *_obj, const void *class_data, va_list *list)
{
Private_Data *pd = (Private_Data *) class_data;
Eo *obj = (Eo *) _obj;
const Eo_Event_Description *desc = va_arg(*list, const Eo_Event_Description *);
void *event_info = va_arg(*list, void *);
Eina_Bool *ret = va_arg(*list, Eina_Bool *);
Eo_Callback_Description *cb;
if (ret) *ret = EINA_TRUE;
if (event_freeze_count || pd->event_freeze_count)
return;
/* FIXME: Change eo_ref to _eo_ref and unref. */
eo_ref(obj);
pd->walking_list++;
EINA_INLIST_FOREACH(pd->callbacks, cb)
{
if (!cb->delete_me && (cb->event == desc))
{
/* Abort callback calling if the func says so. */
if (!cb->func((void *) cb->func_data, obj, desc,
(void *) event_info))
{
if (ret) *ret = EINA_FALSE;
break;
}
}
}
pd->walking_list--;
_eo_callbacks_clear(pd);
eo_unref(obj);
}
static Eina_Bool
_eo_event_forwarder_callback(void *data, Eo *obj, const Eo_Event_Description *desc, void *event_info)
{
(void) obj;
Eo *new_obj = (Eo *) data;
Eina_Bool ret;
eo_do(new_obj, eo_event_callback_call(desc, event_info, &ret));
return ret;
}
/* FIXME: Change default priority? Maybe call later? */
static void
_ev_cb_forwarder_add(Eo *obj, void *class_data EINA_UNUSED, va_list *list)
{
const Eo_Event_Description *desc = va_arg(*list, const Eo_Event_Description *);
Eo *new_obj = va_arg(*list, Eo *);
/* FIXME: Add it EO_MAGIC_RETURN(new_obj, EO_EINA_MAGIC); */
eo_do(obj, eo_event_callback_add(desc, _eo_event_forwarder_callback, new_obj));
}
static void
_ev_cb_forwarder_del(Eo *obj, void *class_data EINA_UNUSED, va_list *list)
{
const Eo_Event_Description *desc = va_arg(*list, const Eo_Event_Description *);
Eo *new_obj = va_arg(*list, Eo *);
/* FIXME: Add it EO_MAGIC_RETURN(new_obj, EO_EINA_MAGIC); */
eo_do(obj, eo_event_callback_del(desc, _eo_event_forwarder_callback, new_obj));
}
static void
_ev_freeze(Eo *obj EINA_UNUSED, void *class_data, va_list *list EINA_UNUSED)
{
Private_Data *pd = (Private_Data *) class_data;
pd->event_freeze_count++;
}
static void
_ev_thaw(Eo *obj, void *class_data, va_list *list EINA_UNUSED)
{
Private_Data *pd = (Private_Data *) class_data;
if (pd->event_freeze_count > 0)
{
pd->event_freeze_count--;
}
else
{
ERR("Events for object %p have already been thawed.", obj);
}
}
static void
_ev_freeze_get(const Eo *obj EINA_UNUSED, const void *class_data, va_list *list)
{
Private_Data *pd = (Private_Data *) class_data;
int *ret = va_arg(*list, int *);
*ret = pd->event_freeze_count;
}
static void
_ev_global_freeze(const Eo_Class *klass EINA_UNUSED, va_list *list EINA_UNUSED)
{
event_freeze_count++;
}
static void
_ev_global_thaw(const Eo_Class *klass EINA_UNUSED, va_list *list EINA_UNUSED)
{
if (event_freeze_count > 0)
{
event_freeze_count--;
}
else
{
ERR("Global events have already been thawed.");
}
}
static void
_ev_global_freeze_get(const Eo_Class *klass EINA_UNUSED, va_list *list)
{
int *ret = va_arg(*list, int *);
*ret = event_freeze_count;
}
/* EOF event callbacks */
/* EO_BASE_CLASS stuff */
#define MY_CLASS EO_BASE_CLASS
/* FIXME: Set proper type descriptions. */
EAPI const Eo_Event_Description _EO_EV_CALLBACK_ADD =
EO_EVENT_DESCRIPTION("callback,add", "A callback was added.");
EAPI const Eo_Event_Description _EO_EV_CALLBACK_DEL =
EO_EVENT_DESCRIPTION("callback,del", "A callback was deleted.");
EAPI const Eo_Event_Description _EO_EV_DEL =
EO_EVENT_DESCRIPTION("del", "Obj is being deleted.");
static void
_constructor(Eo *obj, void *class_data EINA_UNUSED, va_list *list EINA_UNUSED)
{
DBG("%p - %s.", obj, eo_class_name_get(MY_CLASS));
_eo_condtor_done(obj);
}
static void
_destructor(Eo *obj, void *class_data, va_list *list EINA_UNUSED)
{
DBG("%p - %s.", obj, eo_class_name_get(MY_CLASS));
_eo_generic_data_del_all(class_data);
_wref_destruct(class_data);
_eo_callback_remove_all(class_data);
_eo_condtor_done(obj);
}
static void
_class_constructor(Eo_Class *klass)
{
event_freeze_count = 0;
const Eo_Op_Func_Description func_desc[] = {
EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_CONSTRUCTOR), _constructor),
EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_DESTRUCTOR), _destructor),
EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_DATA_SET), _data_set),
EO_OP_FUNC_CONST(EO_BASE_ID(EO_BASE_SUB_ID_DATA_GET), _data_get),
EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_DATA_DEL), _data_del),
EO_OP_FUNC_CONST(EO_BASE_ID(EO_BASE_SUB_ID_WREF_ADD), _wref_add),
EO_OP_FUNC_CONST(EO_BASE_ID(EO_BASE_SUB_ID_WREF_DEL), _wref_del),
EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_EVENT_CALLBACK_PRIORITY_ADD), _ev_cb_priority_add),
EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_EVENT_CALLBACK_DEL), _ev_cb_del),
EO_OP_FUNC_CONST(EO_BASE_ID(EO_BASE_SUB_ID_EVENT_CALLBACK_CALL), _ev_cb_call),
EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_EVENT_CALLBACK_FORWARDER_ADD), _ev_cb_forwarder_add),
EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_EVENT_CALLBACK_FORWARDER_DEL), _ev_cb_forwarder_del),
EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_EVENT_FREEZE), _ev_freeze),
EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_EVENT_THAW), _ev_thaw),
EO_OP_FUNC_CONST(EO_BASE_ID(EO_BASE_SUB_ID_EVENT_FREEZE_GET), _ev_freeze_get),
EO_OP_FUNC_CLASS(EO_BASE_ID(EO_BASE_SUB_ID_EVENT_GLOBAL_FREEZE), _ev_global_freeze),
EO_OP_FUNC_CLASS(EO_BASE_ID(EO_BASE_SUB_ID_EVENT_GLOBAL_THAW), _ev_global_thaw),
EO_OP_FUNC_CLASS(EO_BASE_ID(EO_BASE_SUB_ID_EVENT_GLOBAL_FREEZE_GET), _ev_global_freeze_get),
EO_OP_FUNC_SENTINEL
};
eo_class_funcs_set(klass, func_desc);
}
static const Eo_Op_Description op_desc[] = {
EO_OP_DESCRIPTION(EO_BASE_SUB_ID_CONSTRUCTOR, "Constructor"),
EO_OP_DESCRIPTION(EO_BASE_SUB_ID_DESTRUCTOR, "Destructor"),
EO_OP_DESCRIPTION(EO_BASE_SUB_ID_DATA_SET, "Set data for key."),
EO_OP_DESCRIPTION_CONST(EO_BASE_SUB_ID_DATA_GET, "Get data for key."),
EO_OP_DESCRIPTION(EO_BASE_SUB_ID_DATA_DEL, "Del key."),
EO_OP_DESCRIPTION_CONST(EO_BASE_SUB_ID_WREF_ADD, "Add a weak ref to the object."),
EO_OP_DESCRIPTION_CONST(EO_BASE_SUB_ID_WREF_DEL, "Delete the weak ref."),
EO_OP_DESCRIPTION(EO_BASE_SUB_ID_EVENT_CALLBACK_PRIORITY_ADD, "Add an event callback with a priority."),
EO_OP_DESCRIPTION(EO_BASE_SUB_ID_EVENT_CALLBACK_DEL, "Delete an event callback"),
EO_OP_DESCRIPTION_CONST(EO_BASE_SUB_ID_EVENT_CALLBACK_CALL, "Call the event callbacks for an event."),
EO_OP_DESCRIPTION(EO_BASE_SUB_ID_EVENT_CALLBACK_FORWARDER_ADD, "Add an event forwarder."),
EO_OP_DESCRIPTION(EO_BASE_SUB_ID_EVENT_CALLBACK_FORWARDER_DEL, "Delete an event forwarder."),
EO_OP_DESCRIPTION(EO_BASE_SUB_ID_EVENT_FREEZE, "Freezes events."),
EO_OP_DESCRIPTION(EO_BASE_SUB_ID_EVENT_THAW, "Thaws events."),
EO_OP_DESCRIPTION_CONST(EO_BASE_SUB_ID_EVENT_FREEZE_GET, "Get event freeze counter."),
EO_OP_DESCRIPTION_CLASS(EO_BASE_SUB_ID_EVENT_GLOBAL_FREEZE, "Freezes events globally."),
EO_OP_DESCRIPTION_CLASS(EO_BASE_SUB_ID_EVENT_GLOBAL_THAW, "Thaws events globally."),
EO_OP_DESCRIPTION_CLASS(EO_BASE_SUB_ID_EVENT_GLOBAL_FREEZE_GET, "Get global event freeze counter."),
EO_OP_DESCRIPTION_SENTINEL
};
static const Eo_Event_Description *event_desc[] = {
EO_EV_CALLBACK_ADD,
EO_EV_CALLBACK_DEL,
EO_EV_DEL,
NULL
};
static const Eo_Class_Description class_desc = {
"Eo Base",
EO_CLASS_TYPE_REGULAR_NO_INSTANT,
EO_CLASS_DESCRIPTION_OPS(NULL, op_desc, EO_BASE_SUB_ID_LAST),
event_desc,
sizeof(Private_Data),
_class_constructor,
NULL
};
EO_DEFINE_CLASS_STATIC(eo_base_class_get, EO_BASE_CLASS_ID, &class_desc, NULL, NULL)
|