Main Loop Menu


File Descriptors

Ecore provides an infrastructure to monitor file descriptors, so that files do not have to be blocked or polled to read or write on them. Instead, monitor sockets, pipes, or other streams are used to get a file descriptor.

Table of Contents

To manage the file descriptors

To set a callback

Use the _my_cb_func() function. Its first parameter is the data passed to it (optional), and the second one is the Ecore file descriptor handler. Its return value is, as in most Ecore callbacks, ECORE_CALLBACK_RENEW or ECORE_CALLBACK_CANCEL. It tells Ecore whether it wants to be called again or whether its treatment is finished.

To listen to events

Use the ecore_main_fd_handler_add() function.

To wait for incoming data on the file descriptor

To wait for incoming data (that is, to read data) on the my_fd file descriptor, passing my_data:

Eina_Bool my_fd_cb(void *data, Ecore_Fd_Handler *handler)
{
   int fd;
   fd = ecore_main_fd_handler_fd_get(handler);
   count = read(fd, buf, sizeof(buf)); // This is guaranteed not to block
 
   return ECORE_CALLBACK_RENEW;
}
ecore_main_fd_handler_add(my_fd, ECORE_FD_READ, my_fd_cb, my_data, NULL, NULL);
To delete a file descriptor handler

Use the ecore_main_fd_handler_del() function. This does not close the file descriptor. Always delete the handlers before closing the actual file descriptors.

To get the handler's file descriptor

Use the ecore_main_fd_handler_fd_get() function.

To select whether a flag is active on a handler

Use the ecore_main_fd_handler_active_get() function. For example, the handler is set to monitor both ECORE_FD_READ and ECORE_FD_ERROR. The following example finds out whether the function was called because of an error:

Eina_Bool my_fd_cb(void *data, Ecore_Fd_Handler *handler)
{
   int fd;
   fd = ecore_main_fd_handler_fd_get(handler);
   if (ecore_main_fd_handler_active_get(handler, ECORE_FD_ERROR) == EINA_TRUE)
     {
        // We have an error!
 
        return ECORE_CALLBACK_CANCEL;
     }
   count = read(fd, buf, sizeof(buf)); // This is guaranteed not to block
 
   return ECORE_CALLBACK_RENEW;
}
ecore_main_fd_handler_add(my_fd, ECORE_FD_READ | ECORE_FD_ERROR, my_fd_cb, my_data, NULL, NULL);
To change the flags the handler is monitoring

Use the ecore_main_fd_handler_active_set() function.


Main Loop Menu