aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/ecore/ecore_events.c
blob: 01a4c3d019871d544e0f2266c99648d4b7017881 (plain) (blame)
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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>

#include "Ecore.h"
#include "ecore_private.h"

typedef struct _Ecore_Future_Schedule_Entry
{
   Eina_Future_Schedule_Entry base;
   Eina_Future_Scheduler_Cb cb;
   Eina_Future *future;
   Eina_Value value;
} Ecore_Future_Schedule_Entry;

//////
// XXX: still using legacy ecore events
static Eina_Bool            shutting_down              = EINA_FALSE;
static Eina_Mempool        *mp_future_schedule_entry   = NULL;
//
//////

static Ecore_Event_Message_Handler *_event_msg_handler = NULL;

EAPI Ecore_Event_Handler *
ecore_event_handler_add(int                    type,
                        Ecore_Event_Handler_Cb func,
                        const void            *data)
{
   return ecore_event_message_handler_add(_event_msg_handler,
                                          type, func, (void *)data);
}

EAPI Ecore_Event_Handler *
ecore_event_handler_prepend(int                    type,
                        Ecore_Event_Handler_Cb func,
                        const void            *data)
{
   return ecore_event_message_handler_prepend(_event_msg_handler,
                                          type, func, (void *)data);
}

EAPI void *
ecore_event_handler_del(Ecore_Event_Handler *event_handler)
{
   return ecore_event_message_handler_del(_event_msg_handler,
                                          event_handler);
}

EAPI void *
ecore_event_handler_data_get(Ecore_Event_Handler *eh)
{
   return ecore_event_message_handler_data_get(_event_msg_handler, eh);
}

EAPI void *
ecore_event_handler_data_set(Ecore_Event_Handler *eh,
                             const void          *data)
{
   return ecore_event_message_handler_data_set(_event_msg_handler, eh,
                                               (void *)data);
}

EAPI Ecore_Event *
ecore_event_add(int          type,
                void        *ev,
                Ecore_End_Cb func_free,
                void        *data)
{
   Ecore_Event_Message *msg;
   if (type <= ECORE_EVENT_NONE) return NULL;

   msg = ecore_event_message_handler_message_type_add(_event_msg_handler);
   if (msg)
     {
        ecore_event_message_data_set(msg, type, ev, func_free, data);
        efl_loop_message_handler_message_send(_event_msg_handler, msg);
     }
   return (Ecore_Event *)msg;
}

EAPI void *
ecore_event_del(Ecore_Event *event)
{
   void *data = NULL;
   if (!event) return data;
   ecore_event_message_data_get((Eo *)event, NULL, NULL, NULL, &data);
   _efl_loop_message_unsend((Eo *)event);
   return data;
}

EAPI int
ecore_event_type_new(void)
{
   return ecore_event_message_handler_type_new(_event_msg_handler);
}

EAPI Ecore_Event_Filter *
ecore_event_filter_add(Ecore_Data_Cb   func_start,
                       Ecore_Filter_Cb func_filter,
                       Ecore_End_Cb    func_end,
                       const void     *data)
{
   return ecore_event_message_handler_filter_add(_event_msg_handler,
                                                 func_start, func_filter,
                                                 func_end, (void *)data);
}

EAPI void *
ecore_event_filter_del(Ecore_Event_Filter *ef)
{
   return ecore_event_message_handler_filter_del(_event_msg_handler, ef);
}

EAPI int
ecore_event_current_type_get(void)
{
   return ecore_event_message_handler_current_type_get(_event_msg_handler);
}

EAPI void *
ecore_event_current_event_get(void)
{
   return ecore_event_message_handler_current_event_get(_event_msg_handler);
}

static void _future_dispatch_cb(void *data, const Efl_Event *ev EINA_UNUSED);
static void _event_del_cb(void *data, const Efl_Event *ev);

EFL_CALLBACKS_ARRAY_DEFINE(ecore_future_callbacks,
                           { EFL_LOOP_EVENT_IDLE_ENTER, _future_dispatch_cb },
                           { EFL_LOOP_EVENT_IDLE, _future_dispatch_cb },
                           { EFL_EVENT_DEL, _event_del_cb });

static void
_future_dispatch_cb(void *data, const Efl_Event *ev EINA_UNUSED)
{
   Efl_Loop_Future_Scheduler *loopsched = data;
   Eina_List *entries = loopsched->future_entries;
   Ecore_Future_Schedule_Entry *entry;

   loopsched->future_entries = NULL;
   efl_event_callback_array_del((Eo *) loopsched->loop, ecore_future_callbacks(), loopsched);

   EINA_LIST_FREE(entries, entry)
     {
        entry->cb(entry->future, entry->value);
        eina_mempool_free(mp_future_schedule_entry, entry);
     }
}

static void
_event_del_cb(void *data, const Efl_Event *ev EINA_UNUSED)
{
   Efl_Loop_Future_Scheduler *loopsched = data;
   Eina_List *entries = loopsched->future_entries;
   Ecore_Future_Schedule_Entry *entry;

   loopsched->future_entries = NULL;
   efl_event_callback_array_del((Eo *) loopsched->loop, ecore_future_callbacks(), loopsched);

   EINA_LIST_FREE(entries, entry)
     {
        eina_future_cancel(entry->future);
        eina_value_flush(&entry->value);
        eina_mempool_free(mp_future_schedule_entry, entry);
     }
}

static Eina_Future_Schedule_Entry *
ecore_future_schedule(Eina_Future_Scheduler *sched,
                      Eina_Future_Scheduler_Cb cb,
                      Eina_Future *future,
                      Eina_Value value)
{
   Efl_Loop_Future_Scheduler *loopsched = (Efl_Loop_Future_Scheduler *)sched;
   Ecore_Future_Schedule_Entry *entry;

   entry = eina_mempool_malloc(mp_future_schedule_entry, sizeof(*entry));
   EINA_SAFETY_ON_NULL_RETURN_VAL(entry, NULL);
   entry->base.scheduler = sched;
   entry->cb = cb;
   entry->future = future;
   entry->value = value;

   if (!loopsched->future_entries)
     efl_event_callback_array_add((Eo *) loopsched->loop, ecore_future_callbacks(), loopsched);

   loopsched->future_entries = eina_list_append(loopsched->future_entries, entry);
   return &entry->base;
}

static void
ecore_future_recall(Eina_Future_Schedule_Entry *s_entry)
{
   Ecore_Future_Schedule_Entry *entry = (Ecore_Future_Schedule_Entry *)s_entry;
   Efl_Loop_Future_Scheduler *loopsched;
   Eina_List *lookup;

   if (shutting_down) return;

   loopsched = (Efl_Loop_Future_Scheduler *) entry->base.scheduler;

   lookup = eina_list_data_find_list(loopsched->future_entries, entry);
   if (!lookup) return;

   loopsched->future_entries = eina_list_remove_list(loopsched->future_entries, lookup);
   if (!loopsched->future_entries)
     efl_event_callback_array_del((Eo *) loopsched->loop, ecore_future_callbacks(), loopsched);

   eina_value_flush(&entry->value);
   eina_mempool_free(mp_future_schedule_entry, entry);

}

static Eina_Future_Scheduler ecore_future_scheduler = {
   .schedule = ecore_future_schedule,
   .recall = ecore_future_recall,
};

Eina_Future_Scheduler *
_ecore_event_future_scheduler_get(void)
{
   return &ecore_future_scheduler;
}

Eina_Bool
_ecore_event_init(void)
{
   const char *choice = getenv("EINA_MEMPOOL");
   if ((!choice) || (!choice[0])) choice = "chained_mempool";

   _event_msg_handler = efl_add(ECORE_EVENT_MESSAGE_HANDLER_CLASS, _mainloop_singleton);
   efl_loop_register(_mainloop_singleton, ECORE_EVENT_MESSAGE_HANDLER_CLASS, _event_msg_handler);

   if (!_event_msg_handler)
     {
        ERR("Cannot create legacy ecore event message handler");
        return EINA_FALSE;
     }
   // init some core legacy event types in t he same order and numbering as before
   // ECORE_EVENT_NONE                     0
   // no need to do as ev types start at 1

   // ECORE_EVENT_SIGNAL_USER              1
   ecore_event_message_handler_type_new(_event_msg_handler);
   // ECORE_EVENT_SIGNAL_HUP               2
   ecore_event_message_handler_type_new(_event_msg_handler);
   // ECORE_EVENT_SIGNAL_EXIT              3
   ecore_event_message_handler_type_new(_event_msg_handler);
   // ECORE_EVENT_SIGNAL_POWER             4
   ecore_event_message_handler_type_new(_event_msg_handler);
   // ECORE_EVENT_SIGNAL_REALTIME          5
   ecore_event_message_handler_type_new(_event_msg_handler);
   // ECORE_EVENT_MEMORY_STATE             6
   ecore_event_message_handler_type_new(_event_msg_handler);
   // ECORE_EVENT_POWER_STATE              7
   ecore_event_message_handler_type_new(_event_msg_handler);
   // ECORE_EVENT_LOCALE_CHANGED           8
   ecore_event_message_handler_type_new(_event_msg_handler);
   // ECORE_EVENT_HOSTNAME_CHANGED         9
   ecore_event_message_handler_type_new(_event_msg_handler);
   // ECORE_EVENT_SYSTEM_TIMEDATE_CHANGED  10
   ecore_event_message_handler_type_new(_event_msg_handler);
   // ECORE_EVENT_COUNT                    11
   // no need to do as it was a count, nto an event

   //////
   // XXX: ecore future still using legacy...
   shutting_down = EINA_FALSE;
//   ECORE_EV_FUTURE_ID = ecore_event_type_new();
//   future_handler = ecore_event_handler_add(ECORE_EV_FUTURE_ID, ecore_future_dispatched, NULL);
//   EINA_SAFETY_ON_NULL_GOTO(future_handler, err_handler);
   //FIXME: Is 512 too high?
   if (!mp_future_schedule_entry)
     {
        mp_future_schedule_entry = eina_mempool_add
          (choice, "Ecore_Future_Event", NULL,
           sizeof(Ecore_Future_Schedule_Entry), 512);
        EINA_SAFETY_ON_NULL_GOTO(mp_future_schedule_entry, err_pool);
     }
   //
   //////

   return EINA_TRUE;

 err_pool:
// XXX:
//   ecore_event_handler_del(future_handler);
//   future_handler = NULL;
// err_handler:
//   ECORE_EV_FUTURE_ID = -1;
   return EINA_FALSE;
}

void
_ecore_event_shutdown(void)
{
   shutting_down = EINA_TRUE;

   //////
   // XXX: ecore future still using legacy...
//   ecore_event_handler_del(future_handler);
//   future_handler = NULL;
//   ECORE_EV_FUTURE_ID = -1;
   //
   //////

   efl_loop_message_handler_message_clear(_event_msg_handler);
   _event_msg_handler = NULL;
}

void *
_ecore_event_signal_user_new(void)
{
   return calloc(1, sizeof(Ecore_Event_Signal_User));
}

void *
_ecore_event_signal_hup_new(void)
{
   return calloc(1, sizeof(Ecore_Event_Signal_Hup));
}

void *
_ecore_event_signal_exit_new(void)
{
   return calloc(1, sizeof(Ecore_Event_Signal_Exit));
}

void *
_ecore_event_signal_power_new(void)
{
   return calloc(1, sizeof(Ecore_Event_Signal_Power));
}

void *
_ecore_event_signal_realtime_new(void)
{
   return calloc(1, sizeof(Ecore_Event_Signal_Realtime));
}

EAPI void
ecore_event_type_flush_internal(int type, ...)
{
   va_list args;

   if (type == ECORE_EVENT_NONE) return;
   ecore_event_message_handler_type_flush(_event_msg_handler, type);

   va_start(args, type);
   for (;;)
     {
        type = va_arg(args, int);
        if (type == ECORE_EVENT_NONE) break;
        ecore_event_message_handler_type_flush(_event_msg_handler, type);
     }
   va_end(args);
}