summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafael Antognolli <antognolli@gmail.com>2011-06-29 18:07:34 +0000
committerRafael Antognolli <antognolli@gmail.com>2011-06-29 18:07:34 +0000
commitcd9e094226050d439cf4209dfa9be418ecf68474 (patch)
tree483ffc590dff55ac68097ff865c75af9b1095bbf
parent9cad53125d6e3f0aeefe7665997b8aaa9f29435d (diff)
ecore/poller - Add a poller example and its explanation.
SVN revision: 60829
-rw-r--r--legacy/ecore/doc/examples.dox75
-rw-r--r--legacy/ecore/src/examples/Makefile.am2
-rw-r--r--legacy/ecore/src/examples/ecore_poller_example.c47
3 files changed, 124 insertions, 0 deletions
diff --git a/legacy/ecore/doc/examples.dox b/legacy/ecore/doc/examples.dox
index 4f0922e063..c2980f8393 100644
--- a/legacy/ecore/doc/examples.dox
+++ b/legacy/ecore/doc/examples.dox
@@ -9,6 +9,7 @@
9 * @li @ref ecore_job_example_c 9 * @li @ref ecore_job_example_c
10 * @li @ref ecore_event_example_c 10 * @li @ref ecore_event_example_c
11 * @li @ref ecore_fd_handler_example_c 11 * @li @ref ecore_fd_handler_example_c
12 * @li @ref ecore_poller_example_c
12 * 13 *
13 */ 14 */
14 15
@@ -392,6 +393,74 @@
392 */ 393 */
393 394
394/** 395/**
396 * @page ecore_poller_example_c ecore poller - Repetitive polling tasks
397 * @dontinclude ecore_poller_example.c
398 *
399 * This example show how to setup, and explains how an @ref Ecore_Poller is
400 * called. You can @ref ecore_poller_example.c "see the full source code here".
401 *
402 * In this example we store the initial time of the program just to use as
403 * comparison to the time when the poller callbacks are called. It will be
404 * stored in @c _initial_time :
405 *
406 * @until initial_time
407 *
408 * Then next step is to define the poller callback. This callback assumes that a
409 * @c data pointer is passed to it on creation, and is a string just used to
410 * identify the poller. The callback prints this string and the time since the
411 * program started, and returns @ref ECORE_CALLBACK_RENEW to keep being called.
412 *
413 * @until }
414 *
415 * Now in the main function we initialize Ecore, and save the initial time of
416 * the program, so we can compare it later with the time that the pollers are
417 * being called:
418 *
419 * @until initial_time
420 *
421 * Then we change the poll interval to 0.3 seconds (the default is 0.125
422 * seconds) just to show the API usage.
423 *
424 * Finally, we create two pollers, one that will be called every 4 ticks, and
425 * another one that will be called every 8 ticks. This means the the first
426 * poller interval will be around 1.2 seconds, and the second one will be
427 * around 2.4 seconds. But the most important point is: since the second poller
428 * interval is a multiple of the first one, they will be always synchronized.
429 * Ecore calls pollers that are in the "same tick" together. It doesn't go back
430 * to the main loop and check if there's another poller to execute at this
431 * time, but instead it calls all the pollers registered to this "tick" at the
432 * same time. See the description of ecore_poller_add() for more details. This
433 * is easy to see in the time printed by both of them.
434 *
435 * If instead of two synchronized pollers, we were using two different timers,
436 * one with interval of 1.2 seconds and another one with an interval of 2.4
437 * seconds, there would be no guarantee that they would be totally in sync. Some
438 * delay in the execution of another task, or even in the task called in the
439 * callback, could make them get out of sync, forcing Ecore's main loop to wake
440 * up more than necessary.
441 *
442 * Well, this is the code that create these two pollers and set the poll
443 * interval, then starts the main loop:
444 *
445 * @until ecore_main_loop_begin
446 *
447 * If you hit CTRL-C during the execution of the program, the main loop will
448 * quit, since there are some signal handlers already set by default to do this.
449 * So after the main loop begin call, we change the second poller's interval to
450 * 16 ticks, so it will happen each 4.8 seconds (or each 4 times that the first
451 * poller is called).
452 *
453 * This means: the program is started, the first poller is called each 4 ticks
454 * and the second is called each 8 ticks. After CTRL-C is used, the second
455 * poller will be called each 16 ticks.
456 *
457 * @until }
458 *
459 * The rest of the program is just deleting the pollers and shutting down the
460 * library.
461 */
462
463/**
395 * @example ecore_idler_example.c 464 * @example ecore_idler_example.c
396 * This example shows when @ref Ecore_Idler, @ref Ecore_Idle_Enterer and @ref 465 * This example shows when @ref Ecore_Idler, @ref Ecore_Idle_Enterer and @ref
397 * Ecore_Idle_Exiter are called. See 466 * Ecore_Idle_Exiter are called. See
@@ -423,6 +492,12 @@
423 */ 492 */
424 493
425/** 494/**
495 * @example ecore_poller_example.c
496 * This example shows how to setup and use a poller. See
497 * @ref ecore_poller_example_c "the explanation here".
498 */
499
500/**
426 * @example ecore_event_example.c 501 * @example ecore_event_example.c
427 * This example shows how to setup, change, and delete event handlers. See 502 * This example shows how to setup, change, and delete event handlers. See
428 * @ref ecore_event_example_c "the explanation here". 503 * @ref ecore_event_example_c "the explanation here".
diff --git a/legacy/ecore/src/examples/Makefile.am b/legacy/ecore/src/examples/Makefile.am
index 808a725cd1..cdb4448860 100644
--- a/legacy/ecore/src/examples/Makefile.am
+++ b/legacy/ecore/src/examples/Makefile.am
@@ -13,6 +13,7 @@ LDADD = \
13 13
14SRCS = \ 14SRCS = \
15 ecore_fd_handler_example.c \ 15 ecore_fd_handler_example.c \
16 ecore_poller_example.c \
16 ecore_event_example.c \ 17 ecore_event_example.c \
17 ecore_idler_example.c \ 18 ecore_idler_example.c \
18 ecore_timer_example.c \ 19 ecore_timer_example.c \
@@ -37,6 +38,7 @@ endif
37if EFL_BUILD_EXAMPLES 38if EFL_BUILD_EXAMPLES
38pkglib_PROGRAMS += \ 39pkglib_PROGRAMS += \
39 ecore_fd_handler_example \ 40 ecore_fd_handler_example \
41 ecore_poller_example \
40 ecore_event_example \ 42 ecore_event_example \
41 ecore_idler_example \ 43 ecore_idler_example \
42 ecore_job_example \ 44 ecore_job_example \
diff --git a/legacy/ecore/src/examples/ecore_poller_example.c b/legacy/ecore/src/examples/ecore_poller_example.c
new file mode 100644
index 0000000000..a66c069d1d
--- /dev/null
+++ b/legacy/ecore/src/examples/ecore_poller_example.c
@@ -0,0 +1,47 @@
1#include <Ecore.h>
2#include <unistd.h>
3
4static double _initial_time = 0;
5
6static Eina_Bool
7_poller_print_cb(void *data)
8{
9 char *str = data;
10 printf("Ecore Poller '%s' callback called after %0.3f seconds.\n",
11 str, ecore_time_get() - _initial_time);
12
13 return ECORE_CALLBACK_RENEW;
14}
15
16int main(int argc, char **argv)
17{
18 double interval = 0.3; // tick each 0.3 seconds
19 Ecore_Poller *poller1, *poller2;
20 char *str1 = "poller1";
21 char *str2 = "poller2";
22
23 if (!ecore_init())
24 {
25 printf("ERROR: Cannot init Ecore!\n");
26 return -1;
27 }
28
29 _initial_time = ecore_time_get();
30
31 ecore_poller_poll_interval_set(ECORE_POLLER_CORE, interval);
32
33 poller1 = ecore_poller_add(ECORE_POLLER_CORE, 4, _poller_print_cb, str1);
34 poller2 = ecore_poller_add(ECORE_POLLER_CORE, 8, _poller_print_cb, str2);
35
36 ecore_main_loop_begin();
37
38 printf("changing poller2 interval to 16\n");
39
40 ecore_poller_poller_interval_set(poller2, 16);
41 ecore_main_loop_begin();
42
43 ecore_poller_del(poller1);
44 ecore_poller_del(poller2);
45
46 ecore_shutdown();
47}