Revamp events example

Removed callback,add/del which are confusing events
Added lots of object name printfs
Used a single polling callback
Used array event_add on the timer instead of the main loop for simplicity
This commit is contained in:
Xavi Artigas 2017-12-11 13:51:57 +01:00
parent afae017240
commit afb4aa07ca
1 changed files with 41 additions and 59 deletions

View File

@ -14,70 +14,43 @@
* thaw events on an object.
*/
Efl_Event_Description *_CUSTOM_EVENT;
static void
_add_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
_del_obj_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
printf(" Add\n");
printf(" Delete Object %s\n", efl_name_get(event->object));
}
static void
_del_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
_custom_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
printf(" Delete Callback\n");
printf(" Custom event called on %s\n", efl_name_get(event->object));
}
static void
_del_obj_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
_poll_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
printf(" Delete Object\n");
printf(" Poll from %s\n", efl_name_get(event->object));
}
static void
_custom_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
_freezethaw_cb(void *data, const Efl_Event *event)
{
printf(" Custom\n");
}
static void
_poll_cb(void *data, const Efl_Event *event EINA_UNUSED)
{
Efl_Loop *mainloop;
mainloop = (Efl_Loop *)data;
printf(" Poll\n");
efl_event_callback_del(mainloop, _CUSTOM_EVENT, _custom_cb, NULL);
}
static void
_fast_cb(void *data, const Efl_Event *event EINA_UNUSED)
{
Efl_Loop *mainloop;
mainloop = (Efl_Loop *)data;
printf(" Running\n");
}
static void
_freezethaw_cb(void *data, const Efl_Event *event EINA_UNUSED)
{
Efl_Loop *polled;
Efl_Loop *polled = data;
static int called = 0;
polled = data;
switch (called)
{
case 0:
printf(" Freeze\n");
printf(" Freeze %s from %s\n", efl_name_get(polled), efl_name_get(event->object));
efl_event_freeze(polled);
break;
case 1:
printf(" Thaw\n");
printf(" Thaw %s from %s\n", efl_name_get(polled), efl_name_get(event->object));
efl_event_thaw(polled);
break;
default:
printf(" %s expired: exitting\n", efl_name_get(event->object));
efl_exit(0);
}
@ -91,10 +64,16 @@ _events_freeze(Efl_Loop *mainloop)
printf("Test 2:\n");
polled = efl_add(EFL_LOOP_CLASS, mainloop);
efl_event_callback_add(polled, EFL_LOOP_EVENT_POLL_HIGH, _fast_cb, NULL);
// we add a secondary loop to experiment with freeze/thaw of events.
// if we did these experiments on the main loop, we could not receive
// the timer events used to control the test.
polled = efl_add(EFL_LOOP_CLASS, mainloop,
efl_name_set(efl_added, "subloop"));
efl_event_callback_add(polled, EFL_LOOP_EVENT_POLL_HIGH, _poll_cb, NULL);
// notify every 0.1 seconds
efl_add(EFL_LOOP_TIMER_CLASS, mainloop,
efl_name_set(efl_added, "timer2"),
efl_loop_timer_interval_set(efl_added, .1),
efl_event_callback_add(efl_added, EFL_LOOP_TIMER_EVENT_TICK, _freezethaw_cb, polled));
}
@ -102,46 +81,48 @@ _events_freeze(Efl_Loop *mainloop)
static void
_timer_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
Efl_Loop *mainloop;
Efl_Loop *mainloop = (Efl_Loop *)data;
mainloop = (Efl_Loop *)data;
_events_freeze(mainloop);
printf(" Timer reached for %s\n", efl_name_get(event->object));
// stop listening to polling events
efl_event_callback_del(mainloop, EFL_LOOP_EVENT_POLL_HIGH, _poll_cb, NULL);
// cancel this timer
efl_del(event->object);
// setup second test
_events_freeze(mainloop);
}
EFL_CALLBACKS_ARRAY_DEFINE(_callback_array,
{ EFL_LOOP_EVENT_POLL_MEDIUM, _poll_cb },
{ EFL_EVENT_CALLBACK_DEL, _del_cb },
{ EFL_LOOP_TIMER_EVENT_TICK, _timer_cb },
{ EFL_EVENT_DEL, _del_obj_cb })
static void
_events_demo(Efl_Loop *mainloop)
{
// this is a definition of a custom event, for our app
Efl_Event_Description desc = EFL_EVENT_DESCRIPTION("custom-event");
Efl_Event_Description CUSTOM_EVENT = EFL_EVENT_DESCRIPTION("custom-event");
_CUSTOM_EVENT = &desc;
printf("Test 1:\n");
// add a singe callback that monitors for new callbacks added
efl_event_callback_add(mainloop, EFL_EVENT_CALLBACK_ADD, _add_cb, NULL);
// add an array of callbacks (defined above)
// this is more efficient if you have multiple callbacks to add
efl_event_callback_array_add(mainloop, _callback_array(), mainloop);
// add a single callback that gets called continuously
efl_event_callback_add(mainloop, EFL_LOOP_EVENT_POLL_HIGH, _poll_cb, NULL);
// here we add a custom callback and call it immediately
efl_event_callback_add(mainloop, _CUSTOM_EVENT, _custom_cb, NULL);
efl_event_callback_call(mainloop, _CUSTOM_EVENT, NULL);
efl_event_callback_add(mainloop, &CUSTOM_EVENT, _custom_cb, NULL);
efl_event_callback_call(mainloop, &CUSTOM_EVENT, NULL);
// we will exit from this timer
// we will exit from this timer after 0.1 seconds
// add an array of callbacks (defined above)
// this is more efficient if you have multiple callbacks to add
efl_add(EFL_LOOP_TIMER_CLASS, mainloop,
efl_loop_timer_interval_set(efl_added, 10),
efl_event_callback_add(efl_added, EFL_LOOP_TIMER_EVENT_TICK, _timer_cb, mainloop));
efl_name_set(efl_added, "timer1"),
efl_loop_timer_interval_set(efl_added, 0.1),
efl_event_callback_array_add(efl_added, _callback_array(), mainloop));
printf(" Waiting for timer to call back (10 seconds)...\n");
printf(" Waiting for timer1 to call back...\n");
}
EAPI_MAIN void
@ -150,6 +131,7 @@ efl_main(void *data EINA_UNUSED, const Efl_Event *ev)
Efl_Loop *mainloop;
mainloop = ev->object;
efl_name_set(mainloop, "mainloop");
_events_demo(mainloop);
}
EFL_MAIN()