aboutsummaryrefslogtreecommitdiffstats
path: root/pages/develop/efl/ecore_idlers.txt
blob: 1fa3b269323cad9af7bf023530627c74ffe5f2eb (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
~~Title: Idlers~~
~~CODE-c~~

{{page>index}}

=== Idlers ===

When your application is sleeping and waiting on something to wake it up, it is in the //Idle// state. It will normally sit here and consume no CPU cycles (in the [[mainloop]] during this time, waiting to wake up). There is an option to set up functions to run in a tight loop during this time, instead of sleeping. These are known as //Idlers//

Idlers are rarely needed, and if you find yourself using them, be careful. If you do things like change the state of a canvas (widgets, other UI objects), such changes will not be automatically updated because the [[mainloop]] doesn't even know it should wake up and begin its usual processing, at the end of which before going idle again, it will begin UI updates. If you find yourself needing to do this, you will need to wake up the main loop. You could do this in various ways, by adding a [[ecore_timers|timer]] for 0.0 seconds from now (with a dummy function that does nothing), or a dummy callback for an [[ecore_jobs|job]] for example.

Every Idler registered will be called, one after the other, and when each idler callback returns, the [[mainloop]] will check if it should wake up from the idle state and process as normal, so keep your idler callbacks as short as possible to ensure the latency for a wake-up event is as low as possible.

You add an idler with ecore_idler_add() which will return a handle to the idler object and take a callback with data pointer to pass to it as parameters. The return value of the function determines if it is deleted or not. If you return EINA_FALSE, then the idler will be deleted automatically for you, otherwise returning EINA_TRUE means it will keep being called until it is deleted with ecore_idler_del(). As follows:

<code c>
Ecore_Idler *my_idler;
double start_time = 0.0;

static Eina_Bool
on_idle(void *data)
{
   const char *string = data;
   
   printf("String passed in is '%s'\n", string);
   if ((ecore_time_get() - start_time) > 0.5)
     {
        my_idler = NULL;
        return EINA_FALSE;
     }
   return EINA_TRUE;
}

static void
my_init(void)
{
   static const char *string = "Hello World";

   start_time = ecore_time_get();
   my_idler = ecore_idler_add(on_idle, string);
   if ((rand() % 4) == 0)
     {
        ecore_idler_del(my_idler);
        my_idler = NULL;
     }
}
</code>

Note that the first (and only) parameter to the callback function is the ''data'' parameter which can be a pointer to anything at all. Exactly this pointer is passed to your callback every time it is called. If this no longer points to valid data, it is your job to ensure it is not used or the idler is no longer called before that happens. Pointers are simply numbers with an address in memory (for all intents and purposes) and so thus imagine your pointer first says "Fred is at 213 Jones St.", but in the meantime Fred moves to "27 Bank St.". If the callback looks for Fred at 213 Jones St. - it may find someone else there or nothing at all but a pile of garbage. So be aware of what you pass into callbacks as data pointers and "do the right thing".

----

~~DISCUSSIONS~~