Tizen 2.1 base
[framework/uifw/ecore.git] / src / examples / ecore_idler_example.c
1 #include <Ecore.h>
2 #include <unistd.h>
3
4 struct context   // helper struct to give some context to the callbacks
5 {
6    int                  count;
7    Ecore_Idle_Enterer  *enterer;
8    Ecore_Idler         *idler;
9    Ecore_Idle_Exiter   *exiter;
10    Ecore_Event_Handler *handler;
11    Ecore_Timer         *timer;
12 };
13
14 static _event_type = 0; // a new type of event will be defined and stored here
15
16 static Eina_Bool
17 _enterer_cb(void *data) // the idle enterer callback
18 {
19    printf("IDLE ENTERER: Ecore entering in idle state.\n");
20
21    return ECORE_CALLBACK_RENEW; // same as EINA_TRUE
22 }
23
24 static Eina_Bool
25 _exiter_cb(void *data) // the idle exiter callback
26 {
27    printf("IDLE EXITER: Ecore exiting idle state.\n");
28
29    return ECORE_CALLBACK_RENEW; // same as EINA_TRUE
30 }
31
32 static Eina_Bool
33 _idler_cb(void *data) // the idler callback - ran while the mainloop is idle
34 {
35    struct context *ctxt = data;
36    printf("IDLER: executing idler callback while in idle state.\n");
37
38    ctxt->count++;
39
40    /* each 10 times that the callback gets called, generate an event that
41     * will wake up the main loop, triggering idle enterers, exiters, etc. */
42    if ((ctxt->count % 10) == 0)
43      ecore_event_add(_event_type, NULL, NULL, NULL);
44
45    return ECORE_CALLBACK_RENEW; // same as EINA_TRUE
46 }
47
48 static Eina_Bool
49 _event_handler_cb(void *data, int type, void *event) // event callback
50 {
51    struct context *ctxt = data;
52
53    printf("EVENT: processing callback for the event received.\n");
54
55    if (ctxt->count > 100)
56      {
57         ecore_idle_enterer_del(ctxt->enterer);
58         ecore_idle_exiter_del(ctxt->exiter);
59         ecore_idler_del(ctxt->idler);
60
61         ctxt->enterer = NULL;
62         ctxt->exiter = NULL;
63         ctxt->idler = NULL;
64
65         if (ctxt->timer)
66           {
67              ecore_timer_del(ctxt->timer);
68              ctxt->timer = NULL;
69           }
70
71         ecore_main_loop_quit();
72      }
73
74    return ECORE_CALLBACK_DONE; // same as EINA_FALSE
75 }
76
77 static Eina_Bool
78 _timer_cb(void *data)
79 {
80    struct context *ctxt = data;
81    printf("TIMER: timer callback called.\n");
82
83    if (ctxt->timer)
84      ctxt->timer = NULL;
85
86    return ECORE_CALLBACK_CANCEL; // same as EINA_FALSE
87 }
88
89 int
90 main(int argc, char **argv)
91 {
92    struct context ctxt = {0};
93
94    if (!ecore_init())
95      {
96         printf("ERROR: Cannot init Ecore!\n");
97         return -1;
98      }
99
100    _event_type = ecore_event_type_new();
101
102    ctxt.enterer = ecore_idle_enterer_add(_enterer_cb, &ctxt);
103    ctxt.exiter = ecore_idle_exiter_add(_exiter_cb, &ctxt);
104    ctxt.idler = ecore_idler_add(_idler_cb, &ctxt);
105    ctxt.handler = ecore_event_handler_add(_event_type,
106                                           _event_handler_cb,
107                                           &ctxt);
108    ctxt.timer = ecore_timer_add(0.0005, _timer_cb, &ctxt);
109
110    ecore_main_loop_begin();
111    ecore_shutdown();
112
113    return 0;
114 }
115