Handling events example

This example shows the simplest possible way to register a handler for an ecore event, this way we can focus on the important aspects.

The example will start the main loop and quit it when it receives the ECORE_EVENT_SIGNAL_EXIT event. This event is triggered by a SIGTERM(pressing ctrl+c).

So let's start with the function we want called when we receive the event, instead of just stopping the main loop we'll also print a message, that's just so it's clear that it got called:

static Eina_Bool
_quitter(void *data EINA_UNUSED, int ev_type EINA_UNUSED, void *event EINA_UNUSED)
{
printf("Leaving already?\n");
}
#define ECORE_CALLBACK_DONE
Return value to stop event handling.
Definition: Ecore_Common.h:156
void ecore_main_loop_quit(void)
Quits the main loop once all the events currently on the queue have been processed.
Definition: ecore_main.c:1321
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
Note
We return ECORE_CALLBACK_DONE because we don't want any other handlers for this event to be called, the program is quitting after all.

We then have our main function and the obligatory initialization of ecore:

int
main(void)
{
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition: ecore.c:230

We then get to the one line of our example that makes everything work, the registering of the callback:

#define ECORE_EVENT_SIGNAL_EXIT
Exit signal event.
Definition: Ecore_Common.h:565
Ecore_Event_Handler * ecore_event_handler_add(int type, Ecore_Event_Handler_Cb func, const void *data)
Adds an event handler.
Definition: ecore_events.c:13
Note
The NULL there is because there is no need to pass data to the callback.

And the all that is left to do is start the main loop:

return 0;
}
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1311

Full source code for this example: ecore_event_example_01.c.