ecore_fd_handler_example.c

- Main Page
- Related Pages
- Modules
- Data Structures
- Files
- Examples

ecore_fd_handler_example.c

This example shows how to setup and use an fd_handler.

This example shows how to setup and use an fd_handler. See the explanation here.

/*
* gcc -o ecore_fd_handler_example ecore_fd_handler_example.c `pkg-config --cflags --libs ecore`
*/
#include <Ecore.h>
#include <unistd.h>
struct context
{
Ecore_Fd_Handler *handler;
Ecore_Timer *timer;
};
static void
_fd_prepare_cb(void *data EINA_UNUSED, Ecore_Fd_Handler *handler EINA_UNUSED)
{
printf("prepare_cb called.\n");
}
static Eina_Bool
_fd_handler_cb(void *data, Ecore_Fd_Handler *handler)
{
struct context *ctxt = data;
char buf[1024];
size_t nbytes;
int fd;
if (ecore_main_fd_handler_active_get(handler, ECORE_FD_ERROR))
{
printf("An error has occurred. Stop watching this fd and quit.\n");
ecore_main_loop_quit();
ctxt->handler = NULL;
return ECORE_CALLBACK_CANCEL;
}
fd = ecore_main_fd_handler_fd_get(handler);
nbytes = read(fd, buf, sizeof(buf));
if (nbytes == 0)
{
printf("Nothing to read, exiting...\n");
ecore_main_loop_quit();
ctxt->handler = NULL;
return ECORE_CALLBACK_CANCEL;
}
buf[nbytes - 1] = '\0';
printf("Read %zd bytes from input: \"%s\"\n", nbytes - 1, buf);
return ECORE_CALLBACK_RENEW;
}
static Eina_Bool
_timer_cb(void *data EINA_UNUSED)
{
printf("Timer expired after 5 seconds...\n");
return ECORE_CALLBACK_RENEW;
}
int
main(void)
{
struct context ctxt = {0};
if (!ecore_init())
{
printf("ERROR: Cannot init Ecore!\n");
return -1;
}
ctxt.handler = ecore_main_fd_handler_add(STDIN_FILENO,
ECORE_FD_READ | ECORE_FD_ERROR,
_fd_handler_cb,
&ctxt, NULL, NULL);
ecore_main_fd_handler_prepare_callback_set(ctxt.handler, _fd_prepare_cb, &ctxt);
ctxt.timer = ecore_timer_add(5, _timer_cb, &ctxt);
printf("Starting the main loop. Type anything and hit <enter> to "
"activate the fd_handler callback, or CTRL+d to shutdown.\n");
ecore_main_loop_begin();
if (ctxt.handler)
ecore_main_fd_handler_del(ctxt.handler);
if (ctxt.timer)
ecore_timer_del(ctxt.timer);
ecore_shutdown();
return 0;
}
ecore_main_fd_handler_prepare_callback_set
void ecore_main_fd_handler_prepare_callback_set(Ecore_Fd_Handler *fd_handler, Ecore_Fd_Prep_Cb func, const void *data)
Sets the prepare callback with data for a given Ecore_Fd_Handler.
Definition: ecore_main.c:1602
Ecore_Fd_Handler
struct _Ecore_Fd_Handler Ecore_Fd_Handler
A handle for Fd handlers.
Definition: Ecore_Common.h:1380
ecore_main_fd_handler_del
void * ecore_main_fd_handler_del(Ecore_Fd_Handler *fd_handler)
Marks an FD handler for deletion.
Definition: ecore_main.c:1480
ecore_main_fd_handler_fd_get
int ecore_main_fd_handler_fd_get(Ecore_Fd_Handler *fd_handler)
Retrieves the file descriptor that the given handler is handling.
Definition: ecore_main.c:1627
ecore_main_fd_handler_active_get
Eina_Bool ecore_main_fd_handler_active_get(Ecore_Fd_Handler *fd_handler, Ecore_Fd_Handler_Flags flags)
Gets which flags are active on an FD handler.
Definition: ecore_main.c:1643
ecore_main_fd_handler_add
Ecore_Fd_Handler * ecore_main_fd_handler_add(int fd, Ecore_Fd_Handler_Flags flags, Ecore_Fd_Cb func, const void *data, Ecore_Fd_Cb buf_func, const void *buf_data)
Adds a callback for activity on the given file descriptor.
Definition: ecore_main.c:1449
ECORE_FD_READ
@ ECORE_FD_READ
Fd Read mask.
Definition: Ecore_Common.h:1388
ECORE_FD_ERROR
@ ECORE_FD_ERROR
Fd Error mask.
Definition: Ecore_Common.h:1390
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_RENEW
#define ECORE_CALLBACK_RENEW
Return value to keep a callback.
Definition: Ecore_Common.h:153
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_CALLBACK_CANCEL
#define ECORE_CALLBACK_CANCEL
Return value to remove a callback.
Definition: Ecore_Common.h:152
ecore_main_loop_begin
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1311
ecore_timer_del
void * ecore_timer_del(Ecore_Timer *timer)
Deletes the specified timer from the timer list.
Definition: ecore_timer.c:238
ecore_timer_add
Ecore_Timer * ecore_timer_add(double in, Ecore_Task_Cb func, const void *data)
Creates a timer to call the given function in the given period of time.
Definition: ecore_timer.c:189
Ecore_Timer
Eo Ecore_Timer
A handle for timers.
Definition: Ecore_Common.h:3079
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