ecore_event_example_02.c

This example shows how to setup, change, and delete event handlers. See the explanation here.

//Compile with:
// gcc -g -Wall -o ecore_event_example_02 ecore_event_example_02.c `pkg-config --cflags --libs ecore`
#include <Ecore.h>
#include <unistd.h>
struct context // helper struct to give some context to the callbacks
{
const char *str1, *str2;
Ecore_Event_Handler *handler1;
Ecore_Event_Handler *handler2;
};
static int _event_type = 0; // a new type of event will be defined and stored here
static Eina_Bool
_event_handler1_cb(void *data, int type EINA_UNUSED, void *event)
{
int *number = event;
const char *str = data;
printf("event_handler1: number=%d, data=\"%s\".\n", *number, str);
if ((*number % 2) == 0)
return ECORE_CALLBACK_DONE;
return ECORE_CALLBACK_PASS_ON;
}
static Eina_Bool
_event_handler2_cb(void *data, int type EINA_UNUSED, void *event) // event callback
{
struct context *ctxt = data;
int *number = event;
printf("event_handler2: number=%d.\n", *number);
if (*number == 5)
{
const char *old = NULL;
old = ecore_event_handler_data_set(ctxt->handler1, (void *)ctxt->str2);
printf("changed handler1 data from \"%s\" to \"%s\".\n",
old, ctxt->str2);
}
else if (*number >= 10)
{
printf("finish main loop.\n");
ecore_main_loop_quit();
}
return ECORE_CALLBACK_DONE; // same as EINA_FALSE
}
int
main(void)
{
struct context ctxt = {0};
int i;
ctxt.str1 = "dataone";
ctxt.str2 = "datatwo";
if (!ecore_init())
{
printf("ERROR: Cannot init Ecore!\n");
return -1;
}
_event_type = ecore_event_type_new();
ctxt.handler1 = ecore_event_handler_add(_event_type,
_event_handler1_cb,
ctxt.str1);
ctxt.handler2 = ecore_event_handler_add(_event_type,
_event_handler2_cb,
&ctxt);
for (i = 0; i <= 15; i++)
{
int *event_data = malloc(sizeof(*event_data));
*event_data = i;
ecore_event_add(_event_type, event_data, NULL, NULL);
}
printf("start the main loop.\n");
ecore_main_loop_begin();
ecore_shutdown();
return 0;
}
Ecore_Event_Handler
struct _Ecore_Event_Handler Ecore_Event_Handler
A handle for an event handler.
Definition: Ecore_Common.h:576
ecore_event_handler_data_set
void * ecore_event_handler_data_set(Ecore_Event_Handler *eh, const void *data)
Sets the data associated with an Ecore_Event_Handler.
Definition: ecore_events.c:44
ecore_event_type_new
int ecore_event_type_new(void)
Allocates a new event type id sensibly and returns the new id.
Definition: ecore_events.c:80
ecore_event_add
Ecore_Event * ecore_event_add(int type, void *ev, Ecore_End_Cb func_free, void *data)
Adds an event to the event queue.
Definition: ecore_events.c:52
ecore_event_handler_add
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
ecore_shutdown
EAPI int ecore_shutdown(void)
Shuts down connections, signal handlers sockets etc.
Definition: ecore.c:371
ecore_init
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition: ecore.c:230
ECORE_CALLBACK_PASS_ON
#define ECORE_CALLBACK_PASS_ON
Return value to pass event to next handler.
Definition: Ecore_Common.h:155
ECORE_CALLBACK_DONE
#define ECORE_CALLBACK_DONE
Return value to stop event handling.
Definition: Ecore_Common.h:156
ecore_main_loop_quit
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
ecore_main_loop_begin
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1311
Eina_Bool
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
EINA_UNUSED
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339