1Ecore_Main_Loop_Page(3) Library Functions Manual Ecore_Main_Loop_Page(3)
2
3
4
6 Ecore_Main_Loop_Page - The Ecore Main Loop
7
9 Ecore is a clean and tiny event loop library with many modules to do
10 lots of convenient things for a programmer, to save time and effort.
11
12 It's small and lean, designed to work on embedded systems all the way
13 to large and powerful multi-cpu workstations. It serialises all system
14 signals, events etc. into a single event queue, that is easily
15 processed without needing to worry about concurrency. A properly
16 written, event-driven program using this kind of programming doesn't
17 need threads, nor has to worry about concurrency. It turns a program
18 into a state machine, and makes it very robust and easy to follow.
19
20 Ecore gives you other handy primitives, such as timers to tick over for
21 you and call specified functions at particular times so the programmer
22 can use this to do things, like animate, or time out on connections or
23 tasks that take too long etc.
24
25 Idle handlers are provided too, as well as calls on entering an idle
26 state (often a very good time to update the state of the program). All
27 events that enter the system are passed to specific callback functions
28 that the program sets up to handle those events. Handling them is
29 simple and other Ecore modules produce more events on the queue, coming
30 from other sources such as file descriptors etc.
31
32 Ecore also lets you have functions called when file descriptors become
33 active for reading or writing, allowing for streamlined, non-blocking
34 IO.
35
36 Here is an exmaple of a simple program and its basic event loop flow:
37
39 Ecore is very easy to learn and use. All the function calls are
40 designed to be easy to remember, explicit in describing what they do,
41 and heavily name-spaced. Ecore programs can start and be very simple.
42
43 For example:
44
45 #include <Ecore.h>
46
47 int main(int argc, const char **argv)
48 {
49 ecore_init();
50 ecore_app_args_set(argc, argv);
51 ecore_main_loop_begin();
52 ecore_shutdown();
53 return 0;
54 }
55
56 This program is very simple and does't check for errors, but it does
57 start up and begin a main loop waiting for events or timers to tick
58 off. This program doesn't set up any, but now we can expand on this
59 simple program a little more by adding some event handlers and timers.
60
61 #include <Ecore.h>
62
63 Ecore_Timer *timer1 = NULL;
64 Ecore_Event_Handler *handler1 = NULL;
65 double start_time = 0.0;
66
67 int timer_func(void *data)
68 {
69 printf('Tick timer. Sec: %3.2f0, ecore_time_get() - start_time);
70 return 1;
71 }
72
73 int exit_func(void *data, int ev_type, void *ev)
74 {
75 Ecore_Event_Signal_Exit *e;
76
77 e = (Ecore_Event_Signal_Exit *)ev;
78 if (e->interrupt) printf('Exit: interrupt0);
79 else if (e->quit) printf('Exit: quit0);
80 else if (e->terminate) printf('Exit: terminate0);
81 ecore_main_loop_quit();
82 return 1;
83 }
84
85 int main(int argc, const char **argv)
86 {
87 ecore_init();
88 ecore_app_args_set(argc, argv);
89 start_time = ecore_time_get();
90 handler1 = ecore_event_handler_add(ECORE_EVENT_SIGNAL_EXIT, exit_func, NULL);
91 timer1 = ecore_timer_add(0.5, timer_func, NULL);
92 ecore_main_loop_begin();
93 ecore_shutdown();
94 return 0;
95 }
96
97 In the previous example, we initialize our application and get the time
98 at which our program has started so we can calculate an offset. We set
99 up a timer to tick off in 0.5 seconds, and since it returns 1, will
100 keep ticking off every 0.5 seconds until it returns 0, or is deleted by
101 hand. An event handler is set up to call a function - exit_func(),
102 whenever an event of type ECORE_EVENT_SIGNAL_EXIT is received (CTRL-C
103 on the command line will cause such an event to happen). If this event
104 occurs it tells you what kind of exit signal was received, and asks the
105 main loop to quit when it is finished by calling
106 ecore_main_loop_quit().
107
108 The handles returned by ecore_timer_add() and ecore_event_handler_add()
109 are only stored here as an example. If you don't need to address the
110 timer or event handler again you don't need to store the result, so
111 just call the function, and don't assign the result to any variable.
112
113 This program looks slightly more complex than needed to do these simple
114 things, but in principle, programs don't get any more complex. You add
115 more event handlers, for more events, will have more timers and such,
116 BUT it all follows the same principles as shown in this example.
117
118
119
120Ecore 2 Jul 2010 Ecore_Main_Loop_Page(3)