summaryrefslogtreecommitdiff
path: root/reference/c/core/src/core_event.c
blob: f62f25c4a05a36eca99b3f5d9597b23b64a97349 (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
#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.
 */


static void
_del_obj_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
   printf("  Delete Object %s\n", efl_name_get(event->object));
}

static void
_custom_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
   printf("  Custom event called on %s\n", efl_name_get(event->object));
}

static void
_poll_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
   printf("  Poll from %s\n", efl_name_get(event->object));
}

static void
_freezethaw_cb(void *data, const Efl_Event *event)
{
   Efl_Loop *polled = data;
   static int called = 0;

   switch (called)
     {
        case 0:
          printf("  Freeze %s from %s\n", efl_name_get(polled), efl_name_get(event->object));
          efl_event_freeze(polled);
          break;
        case 1:
          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);
     }

   called++;
}

static void
_events_freeze(Efl_Loop *mainloop)
{
   printf("Test 2:\n");

   // Set up a new poll on the main loop that we will interupt with freeze.
   efl_event_callback_add(mainloop, 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, mainloop));
}

static void
_timer_cb(void *data EINA_UNUSED, const Efl_Event *event)
{
   Efl_Loop *mainloop = (Efl_Loop *)data;

   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_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 CUSTOM_EVENT = EFL_EVENT_DESCRIPTION("custom-event");

   printf("Test 1:\n");

   // 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);

   // 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_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 timer1 to call back...\n");
}

EAPI_MAIN void
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()