examples/reference/c/core/src/core_event.c

157 lines
3.6 KiB
C
Raw Normal View History

2017-12-05 07:07:32 -08:00
#define EFL_EO_API_SUPPORT 1
#define EFL_BETA_API_SUPPORT 1
#include <stdio.h>
#include <Eina.h>
#include <Efl_Core.h>
/*
* Efl Core Event examples.
*
* This example shows the various ways of adding callbacks for standard events.
* It also demonstrates the creation of a custom event and how to freeze and
* thaw events on an object.
*/
Efl_Event_Description *_CUSTOM_EVENT;
static void
_add_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
{
printf(" Add\n");
}
static void
_del_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
{
printf(" Delete Callback\n");
}
static void
_del_obj_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
{
printf(" Delete Object\n");
}
static void
_custom_cb(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
{
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;
static int called = 0;
polled = data;
switch (called)
{
case 0:
printf(" Freeze\n");
efl_event_freeze(polled);
break;
case 1:
printf(" Thaw\n");
efl_event_thaw(polled);
break;
default:
efl_exit(0);
}
called++;
}
static void
_events_freeze(Efl_Loop *mainloop)
{
Efl_Loop *polled;
printf("Test 2:\n");
polled = efl_add(EFL_LOOP_CLASS, mainloop);
efl_event_callback_add(polled, EFL_LOOP_EVENT_POLL_HIGH, _fast_cb, NULL);
efl_add(EFL_LOOP_TIMER_CLASS, mainloop,
efl_loop_timer_interval_set(efl_added, .1),
efl_event_callback_add(efl_added, EFL_LOOP_TIMER_EVENT_TICK, _freezethaw_cb, polled));
}
static void
_timer_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
Efl_Loop *mainloop;
mainloop = (Efl_Loop *)data;
_events_freeze(mainloop);
// cancel this timer
efl_del(event->object);
}
EFL_CALLBACKS_ARRAY_DEFINE(_callback_array,
{ EFL_LOOP_EVENT_POLL_MEDIUM, _poll_cb },
{ EFL_EVENT_CALLBACK_DEL, _del_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");
_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);
// 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);
// we will exit from this timer
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));
printf(" Waiting for timer to call back (10 seconds)...\n");
}
EAPI_MAIN void
efl_main(void *data EINA_UNUSED, const Efl_Event *ev)
{
Efl_Loop *mainloop;
mainloop = ev->object;
_events_demo(mainloop);
}
EFL_MAIN()