6 # define WIN32_LEAN_AND_MEAN
8 # undef WIN32_LEAN_AND_MEAN
9 # ifndef USER_TIMER_MINIMUM
10 # define USER_TIMER_MINIMUM 0x0a
22 #include <sys/types.h>
27 # include <sys/time.h>
37 # include <sys/param.h>
49 #include "ecore_private.h"
51 #ifdef HAVE_SYS_EPOLL_H
53 # include <sys/epoll.h>
56 #ifdef USE_G_MAIN_LOOP
60 struct _Ecore_Fd_Handler
65 Ecore_Fd_Handler_Flags flags;
70 Ecore_Fd_Prep_Cb prep_func;
73 Eina_Bool read_active : 1;
74 Eina_Bool write_active : 1;
75 Eina_Bool error_active : 1;
76 Eina_Bool delete_me : 1;
77 #if defined(USE_G_MAIN_LOOP) && !defined(HAVE_EPOLL)
83 struct _Ecore_Win32_Handler
88 Ecore_Fd_Win32_Cb func;
91 Eina_Bool delete_me : 1;
96 static int _ecore_main_select(double timeout);
97 static void _ecore_main_prepare_handlers(void);
98 static void _ecore_main_fd_handlers_cleanup(void);
100 static void _ecore_main_fd_handlers_bads_rem(void);
102 static void _ecore_main_fd_handlers_call(void);
103 static int _ecore_main_fd_handlers_buf_call(void);
104 #ifndef USE_G_MAIN_LOOP
105 static void _ecore_main_loop_iterate_internal(int once_only);
109 static int _ecore_main_win32_select(int nfds, fd_set *readfds, fd_set *writefds,
110 fd_set *exceptfds, struct timeval *timeout);
111 static void _ecore_main_win32_handlers_cleanup(void);
114 static int in_main_loop = 0;
115 static int do_quit = 0;
116 static Ecore_Fd_Handler *fd_handlers = NULL;
117 static Ecore_Fd_Handler *fd_handler_current = NULL;
118 static Eina_List *fd_handlers_with_prep = NULL;
119 static Eina_List *fd_handlers_with_buffer = NULL;
120 static Eina_List *fd_handlers_to_delete = NULL;
122 static Eina_List *fd_handlers_to_call = NULL;
123 static Eina_List *fd_handlers_to_call_current;
124 static Eina_List *fd_handlers_to_call_current_next;
127 static Ecore_Win32_Handler *win32_handlers = NULL;
128 static Ecore_Win32_Handler *win32_handler_current = NULL;
129 static Eina_Bool win32_handlers_delete_me = EINA_FALSE;
133 static Ecore_Select_Function main_loop_select = _ecore_main_win32_select;
135 static Ecore_Select_Function main_loop_select = select;
138 static double t1 = 0.0;
139 static double t2 = 0.0;
142 static int epoll_fd = -1;
143 static pid_t epoll_pid;
146 #ifdef USE_G_MAIN_LOOP
148 static GPollFD ecore_epoll_fd;
150 static GSource *ecore_glib_source;
151 static guint ecore_glib_source_id;
152 static GMainLoop* ecore_main_loop;
153 static gboolean ecore_idling;
154 static gboolean ecore_fds_ready;
159 _ecore_get_epoll_fd(void)
161 if (epoll_pid && epoll_pid != getpid())
164 _ecore_main_loop_shutdown();
166 if (epoll_pid == 0 && epoll_fd < 0)
168 _ecore_main_loop_init();
174 _ecore_epoll_add(int efd, int fd, int events, void *ptr)
176 struct epoll_event ev;
178 memset(&ev, 0, sizeof (ev));
181 INF("adding poll on %d %08x", fd, events);
182 return epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev);
186 _ecore_poll_events_from_fdh(Ecore_Fd_Handler *fdh)
189 if (fdh->flags & ECORE_FD_READ) events |= EPOLLIN;
190 if (fdh->flags & ECORE_FD_WRITE) events |= EPOLLOUT;
191 if (fdh->flags & ECORE_FD_ERROR) events |= EPOLLERR;
196 _ecore_poll_events_from_fdh(Ecore_Fd_Handler *fdh __UNUSED__)
202 #ifdef USE_G_MAIN_LOOP
204 _gfd_events_from_fdh(Ecore_Fd_Handler *fdh)
207 if (fdh->flags & ECORE_FD_READ) events |= G_IO_IN;
208 if (fdh->flags & ECORE_FD_WRITE) events |= G_IO_OUT;
209 if (fdh->flags & ECORE_FD_ERROR) events |= G_IO_ERR;
215 _ecore_main_fdh_poll_add(Ecore_Fd_Handler *fdh)
219 r = _ecore_epoll_add(_ecore_get_epoll_fd(), fdh->fd,
220 _ecore_poll_events_from_fdh(fdh), fdh);
221 #elif USE_G_MAIN_LOOP
222 fdh->gfd.fd = fdh->fd;
223 fdh->gfd.events = _gfd_events_from_fdh(fdh);
224 fdh->gfd.revents = 0;
225 INF("adding gpoll on %d %08x", fdh->fd, fdh->gfd.events);
226 g_source_add_poll(ecore_glib_source, &fdh->gfd);
228 if (!ECORE_MAGIC_CHECK(fdh, ECORE_MAGIC_FD_HANDLER))
230 ECORE_MAGIC_FAIL(fdh, ECORE_MAGIC_FD_HANDLER,
231 "_ecore_main_fdh_poll_add");
238 _ecore_main_fdh_poll_del(Ecore_Fd_Handler *fdh)
241 struct epoll_event ev;
242 int efd = _ecore_get_epoll_fd();
244 memset(&ev, 0, sizeof (ev));
245 INF("removing poll on %d", fdh->fd);
246 /* could get an EBADF if somebody closed the FD before removing it */
247 if ((epoll_ctl(efd, EPOLL_CTL_DEL, fdh->fd, &ev) < 0))
251 WRN("fd %d was closed, can't remove from epoll - reinit!",
253 _ecore_main_loop_shutdown();
254 _ecore_main_loop_init();
258 ERR("Failed to delete epoll fd %d! (errno=%d)", fdh->fd, errno);
261 #elif USE_G_MAIN_LOOP
262 fdh->gfd.fd = fdh->fd;
263 fdh->gfd.events = _gfd_events_from_fdh(fdh);
264 fdh->gfd.revents = 0;
265 INF("adding gpoll on %d %08x", fdh->fd, fdh->gfd.events);
266 g_source_add_poll(ecore_glib_source, &fdh->gfd);
268 if (!ECORE_MAGIC_CHECK(fdh, ECORE_MAGIC_FD_HANDLER))
270 ECORE_MAGIC_FAIL(fdh, ECORE_MAGIC_FD_HANDLER,
271 "_ecore_main_fdh_poll_del");
277 _ecore_main_fdh_poll_modify(Ecore_Fd_Handler *fdh)
281 struct epoll_event ev;
282 int efd = _ecore_get_epoll_fd();
284 memset(&ev, 0, sizeof (ev));
285 ev.events = _ecore_poll_events_from_fdh(fdh);
287 INF("modifing epoll on %d to %08x", fdh->fd, ev.events);
288 r = epoll_ctl(efd, EPOLL_CTL_MOD, fdh->fd, &ev);
289 #elif USE_G_MAIN_LOOP
290 fdh->gfd.fd = fdh->fd;
291 fdh->gfd.events = _gfd_events_from_fdh(fdh);
292 fdh->gfd.revents = 0;
293 INF("modifing gpoll on %d to %08x", fdh->fd, fdh->gfd.events);
295 if (!ECORE_MAGIC_CHECK(fdh, ECORE_MAGIC_FD_HANDLER))
297 ECORE_MAGIC_FAIL(fdh, ECORE_MAGIC_FD_HANDLER,
298 "_ecore_main_fdh_poll_modify");
305 static inline int _ecore_main_fdh_poll_mark_active(void)
307 struct epoll_event ev[32];
309 int efd = _ecore_get_epoll_fd();
311 memset(&ev, 0, sizeof (ev));
312 ret = epoll_wait(efd, ev, sizeof(ev) / sizeof(struct epoll_event), 0);
315 if (errno == EINTR) return -1;
316 ERR("epoll_wait failed %d", errno);
320 for (i = 0; i < ret; i++)
322 Ecore_Fd_Handler *fdh;
325 fdh = ev[i].data.ptr;
326 if (!ECORE_MAGIC_CHECK(fdh, ECORE_MAGIC_FD_HANDLER))
328 ECORE_MAGIC_FAIL(fdh, ECORE_MAGIC_FD_HANDLER,
329 "_ecore_main_fdh_poll_mark_active");
334 ERR("deleted fd in epoll");
337 pst = st = fdh->read_active | fdh->write_active | fdh->error_active;
338 if ((ev[i].events & EPOLLIN) && (!fdh->read_active))
339 st = fdh->read_active = EINA_TRUE;
340 if ((ev[i].events & EPOLLOUT) && (!fdh->write_active))
341 st = fdh->write_active = EINA_TRUE;
342 if ((ev[i].events & EPOLLERR) && (!fdh->error_active))
343 st = fdh->error_active = EINA_TRUE;
345 fd_handlers_to_call = eina_list_append(fd_handlers_to_call, fdh);
351 #elif USE_G_MAIN_LOOP
353 static inline int _ecore_main_fdh_poll_mark_active(void)
355 Ecore_Fd_Handler *fdh;
359 /* call the prepare callback for all handlers */
360 EINA_INLIST_FOREACH(fd_handlers, fdh)
365 pst = st = fdh->read_active | fdh->write_active | fdh->error_active;
366 if ((fdh->gfd.revents & G_IO_IN) && (!fdh->read_active))
367 st = fdh->read_active = EINA_TRUE;
368 if ((fdh->gfd.revents & G_IO_OUT) && (!fdh->write_active))
369 st = fdh->write_active = EINA_TRUE;
370 if ((fdh->gfd.revents & G_IO_ERR) && (!fdh->error_active))
371 st = fdh->error_active = EINA_TRUE;
373 fd_handlers_to_call = eina_list_append(fd_handlers_to_call, fdh);
374 if (fdh->gfd.revents & (G_IO_IN|G_IO_OUT|G_IO_ERR)) ret++;
377 INF("found %d active fds", ret);
384 #ifdef USE_G_MAIN_LOOP
386 /* like we are about to enter main_loop_select in _ecore_main_select */
388 _ecore_main_gsource_prepare(GSource *source, gint *next_time)
390 double t = _ecore_timer_next_get();
393 INF("enter, next timeout in %.1f", t);
398 while (_ecore_timer_call(_ecore_time_loop_time));
399 _ecore_timer_cleanup();
401 /* when idling, busy loop checking the fds only */
402 if (!ecore_idling) _ecore_idle_enterer_call();
405 /* don't check fds if somebody quit */
406 running = g_main_loop_is_running(ecore_main_loop);
409 /* only set idling state in dispatch */
410 if (ecore_idling && !_ecore_idler_exist())
412 if (_ecore_timers_exists())
414 double t = _ecore_timer_next_get();
415 *next_time = (t / 1000.0);
423 if (fd_handlers_with_prep)
424 _ecore_main_prepare_handlers();
428 INF("leave, timeout = %d", *next_time);
430 /* ready if we're not running (about to quit) */
435 _ecore_main_gsource_check(GSource *source)
440 ecore_fds_ready = (_ecore_main_fdh_poll_mark_active() > 0);
441 _ecore_main_fd_handlers_cleanup();
443 _ecore_time_loop_time = ecore_time_get();
444 _ecore_timer_enable_new();
449 return TRUE; /* always dispatch */
452 /* like we just came out of main_loop_select in _ecore_main_select */
454 _ecore_main_gsource_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
456 gboolean events_ready, timers_ready, idlers_ready, signals_ready;
457 double next_time = _ecore_timer_next_get();
459 events_ready = _ecore_event_exist();
460 timers_ready = _ecore_timers_exists() && (0.0 <= next_time);
461 idlers_ready = _ecore_idler_exist();
462 signals_ready = (_ecore_signal_count_get() > 0);
465 INF("enter idling=%d fds=%d events=%d signals=%d timers=%d (next=%.2f) idlers=%d",
466 ecore_idling, ecore_fds_ready, events_ready, signals_ready,
467 _ecore_timers_exists(), next_time, idlers_ready);
469 if (ecore_idling && events_ready)
471 INF("calling idle exiters");
472 _ecore_idle_exiter_call();
475 else if (!ecore_idling && !events_ready)
483 INF("calling idler");
486 events_ready = _ecore_event_exist();
487 timers_ready = _ecore_timers_exists() && (0.0 <= next_time);
488 idlers_ready = _ecore_idler_exist();
490 if ((ecore_fds_ready || events_ready || timers_ready || idlers_ready || signals_ready))
492 INF("calling idle exiters");
493 _ecore_idle_exiter_call();
502 _ecore_main_fd_handlers_call();
503 if (fd_handlers_with_buffer)
504 _ecore_main_fd_handlers_buf_call();
505 while (_ecore_signal_count_get()) _ecore_signal_call();
507 _ecore_main_fd_handlers_cleanup();
514 return TRUE; /* what should be returned here? */
518 _ecore_main_gsource_finalize(GSource *source)
523 static GSourceFuncs ecore_gsource_funcs =
525 .prepare = _ecore_main_gsource_prepare,
526 .check = _ecore_main_gsource_check,
527 .dispatch = _ecore_main_gsource_dispatch,
528 .finalize = _ecore_main_gsource_finalize,
534 _ecore_main_loop_init(void)
538 epoll_fd = epoll_create(1);
540 CRIT("Failed to create epoll fd!");
541 epoll_pid = getpid();
543 /* add polls on all our file descriptors */
544 Ecore_Fd_Handler *fdh;
545 EINA_INLIST_FOREACH(fd_handlers, fdh)
549 _ecore_epoll_add(epoll_fd, fdh->fd,
550 _ecore_poll_events_from_fdh(fdh), fdh);
551 _ecore_main_fdh_poll_add(fdh);
556 #ifdef USE_G_MAIN_LOOP
557 ecore_glib_source = g_source_new(&ecore_gsource_funcs, sizeof (GSource));
558 if (!ecore_glib_source)
559 CRIT("Failed to create glib source for epoll!");
563 ecore_epoll_fd.fd = epoll_fd;
564 ecore_epoll_fd.events = G_IO_IN;
565 ecore_epoll_fd.revents = 0;
566 g_source_add_poll(ecore_glib_source, &ecore_epoll_fd);
568 ecore_glib_source_id = g_source_attach(ecore_glib_source, NULL);
569 if (ecore_glib_source_id <= 0)
570 CRIT("Failed to attach glib source to default context");
577 _ecore_main_loop_shutdown(void)
579 #ifdef USE_G_MAIN_LOOP
580 if (ecore_glib_source)
582 g_source_destroy(ecore_glib_source);
583 ecore_glib_source = NULL;
599 * @addtogroup Ecore_Group Ecore - Main Loop and Job Functions.
605 * @addtogroup Ecore_Main_Loop_Group Ecore Main Loop functions
607 * These functions control the Ecore event handling loop. This loop is
608 * designed to work on embedded systems all the way to large and
609 * powerful mutli-cpu workstations.
611 * It serialises all system signals and events into a single event
612 * queue, that can be easily processed without needing to worry about
613 * concurrency. A properly written, event-driven program using this
614 * kind of programming does not need threads. It makes the program very
615 * robust and easy to follow.
617 * Here is an example of simple program and its basic event loop flow:
618 * @image html prog_flow.png
620 * For examples of setting up and using a main loop, see
621 * @ref event_handler_example.c and @ref timer_example.c.
627 * Runs a single iteration of the main loop to process everything on the
631 ecore_main_loop_iterate(void)
633 #ifndef USE_G_MAIN_LOOP
634 _ecore_main_loop_iterate_internal(1);
636 g_main_context_iteration(NULL, 1);
641 * Runs the application main loop.
643 * This function will not return until @ref ecore_main_loop_quit is called.
647 ecore_main_loop_begin(void)
649 #ifndef USE_G_MAIN_LOOP
651 while (do_quit == 0) _ecore_main_loop_iterate_internal(0);
655 ecore_main_loop = g_main_loop_new(NULL, FALSE);
656 g_main_loop_run(ecore_main_loop);
661 * Quits the main loop once all the events currently on the queue have
665 ecore_main_loop_quit(void)
667 #ifndef USE_G_MAIN_LOOP
671 g_main_loop_quit(ecore_main_loop);
677 * Sets the function to use when monitoring multiple file descriptors,
678 * and waiting until one of more of the file descriptors before ready
679 * for some class of I/O operation.
681 * This function will be used instead of the system call select and
682 * could possible be used to integrate the Ecore event loop with an
683 * external event loop.
685 * @warning you don't know how to use, don't even try to use it.
689 ecore_main_loop_select_func_set(Ecore_Select_Function func)
691 main_loop_select = func;
695 * Gets the select function set by ecore_select_func_set(),
696 * or the native select function if none was set.
700 ecore_main_loop_select_func_get(void)
702 return main_loop_select;
706 * @defgroup Ecore_FD_Handler_Group File Event Handling Functions
708 * Functions that deal with file descriptor handlers.
712 * Adds a callback for activity on the given file descriptor.
714 * @p func will be called during the execution of @ref ecore_main_loop_begin
715 * when the file descriptor is available for reading, or writing, or both.
717 * Normally the return value from the @p func is "zero means this handler is
718 * finished and can be deleted" as is usual for handler callbacks. However,
719 * if the @p buf_func is supplied, then the return value from the @p func is
720 * "non zero means the handler should be called again in a tight loop".
722 * @p buf_func is called during event loop handling to check if data that has
723 * been read from the file descriptor is in a buffer and is available to
724 * read. Some systems (notably xlib) handle their own buffering, and would
725 * otherwise not work with select(). These systems should use a @p buf_func.
726 * This is a most annoying hack, only ecore_x uses it, so refer to that for
727 * an example. NOTE - @p func should probably return "one" always if
728 * @p buf_func is used, to avoid confusion with the other return value
731 * @param fd The file descriptor to watch.
732 * @param flags To watch it for read (@c ECORE_FD_READ) and/or
733 * (@c ECORE_FD_WRITE) write ability. @c ECORE_FD_ERROR
735 * @param func The callback function.
736 * @param data The data to pass to the callback.
737 * @param buf_func The function to call to check if any data has been
738 * buffered and already read from the fd. Can be @c NULL.
739 * @param buf_data The data to pass to the @p buf_func function.
740 * @return A fd handler handle if successful. @c NULL otherwise.
741 * @ingroup Ecore_FD_Handler_Group
743 EAPI Ecore_Fd_Handler *
744 ecore_main_fd_handler_add(int fd, Ecore_Fd_Handler_Flags flags, Ecore_Fd_Cb func, const void *data,
745 Ecore_Fd_Cb buf_func, const void *buf_data)
747 Ecore_Fd_Handler *fdh;
749 if ((fd < 0) || (flags == 0) || (!func)) return NULL;
751 fdh = calloc(1, sizeof(Ecore_Fd_Handler));
752 if (!fdh) return NULL;
753 ECORE_MAGIC_SET(fdh, ECORE_MAGIC_FD_HANDLER);
756 if (_ecore_main_fdh_poll_add(fdh) < 0)
758 ERR("Failed to add poll on fd %d (errno = %d)!", fd, errno);
762 fdh->read_active = EINA_FALSE;
763 fdh->write_active = EINA_FALSE;
764 fdh->error_active = EINA_FALSE;
765 fdh->delete_me = EINA_FALSE;
767 fdh->data = (void *)data;
768 fdh->buf_func = buf_func;
770 fd_handlers_with_buffer = eina_list_append(fd_handlers_with_buffer, fdh);
771 fdh->buf_data = (void *)buf_data;
772 fd_handlers = (Ecore_Fd_Handler *)
773 eina_inlist_append(EINA_INLIST_GET(fd_handlers),
774 EINA_INLIST_GET(fdh));
779 EAPI Ecore_Win32_Handler *
780 ecore_main_win32_handler_add(void *h, Ecore_Fd_Win32_Cb func, const void *data)
782 Ecore_Win32_Handler *wh;
784 if (!h || !func) return NULL;
786 wh = calloc(1, sizeof(Ecore_Win32_Handler));
787 if (!wh) return NULL;
788 ECORE_MAGIC_SET(wh, ECORE_MAGIC_WIN32_HANDLER);
790 wh->delete_me = EINA_FALSE;
792 wh->data = (void *)data;
793 win32_handlers = (Ecore_Win32_Handler *)
794 eina_inlist_append(EINA_INLIST_GET(win32_handlers),
795 EINA_INLIST_GET(wh));
799 EAPI Ecore_Win32_Handler *
800 ecore_main_win32_handler_add(void *h __UNUSED__, Ecore_Fd_Win32_Cb func __UNUSED__,
801 const void *data __UNUSED__)
808 * Deletes the given FD handler.
809 * @param fd_handler The given FD handler.
810 * @return The data pointer set using @ref ecore_main_fd_handler_add,
811 * for @p fd_handler on success. @c NULL otherwise.
812 * @ingroup Ecore_FD_Handler_Group
814 * Beware that if the fd is already closed, ecore may complain if it uses
815 * epoll internally, and that in some rare cases this may be able to cause
816 * crashes and instability. Remember to delete your fd handlers before the
817 * fd's they listen to are closed.
820 ecore_main_fd_handler_del(Ecore_Fd_Handler *fd_handler)
822 if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
824 ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
825 "ecore_main_fd_handler_del");
828 if (fd_handler->delete_me)
829 /* FIXME: should this return NULL instead? */
830 return fd_handler->data;
832 fd_handler->delete_me = EINA_TRUE;
833 _ecore_main_fdh_poll_del(fd_handler);
834 fd_handlers_to_delete = eina_list_append(fd_handlers_to_delete, fd_handler);
835 if (fd_handler->prep_func && fd_handlers_with_prep)
836 fd_handlers_with_prep = eina_list_remove(fd_handlers_with_prep, fd_handler);
837 if (fd_handler->buf_func && fd_handlers_with_buffer)
838 fd_handlers_with_buffer = eina_list_remove(fd_handlers_with_buffer, fd_handler);
839 return fd_handler->data;
844 ecore_main_win32_handler_del(Ecore_Win32_Handler *win32_handler)
846 if (!ECORE_MAGIC_CHECK(win32_handler, ECORE_MAGIC_WIN32_HANDLER))
848 ECORE_MAGIC_FAIL(win32_handler, ECORE_MAGIC_WIN32_HANDLER,
849 "ecore_main_win32_handler_del");
852 win32_handler->delete_me = EINA_TRUE;
853 win32_handlers_delete_me = EINA_TRUE;
854 return win32_handler->data;
858 ecore_main_win32_handler_del(Ecore_Win32_Handler *win32_handler __UNUSED__)
865 * @brief Set the prepare callback with data for a given #Ecore_Fd_Handler
866 * @param fd_handler The fd handler
867 * @param func The prep function
868 * @param data The data to pass to the prep function
869 * This function will be called prior to the the fd handler's callback function.
872 ecore_main_fd_handler_prepare_callback_set(Ecore_Fd_Handler *fd_handler, Ecore_Fd_Prep_Cb func, const void *data)
874 if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
876 ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
877 "ecore_main_fd_handler_prepare_callback_set");
880 fd_handler->prep_func = func;
881 fd_handler->prep_data = (void *)data;
882 if (fd_handlers_with_prep && (!eina_list_data_find(fd_handlers_with_prep, fd_handler)))
883 /* FIXME: THIS WILL NOT SCALE WITH LOTS OF PREP FUNCTIONS!!! */
884 fd_handlers_with_prep = eina_list_append(fd_handlers_with_prep, fd_handler);
888 * Retrieves the file descriptor that the given handler is handling.
889 * @param fd_handler The given FD handler.
890 * @return The file descriptor the handler is watching.
891 * @ingroup Ecore_FD_Handler_Group
894 ecore_main_fd_handler_fd_get(Ecore_Fd_Handler *fd_handler)
896 if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
898 ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
899 "ecore_main_fd_handler_fd_get");
902 return fd_handler->fd;
906 * Return if read, write or error, or a combination thereof, is active on the
907 * file descriptor of the given FD handler.
908 * @param fd_handler The given FD handler.
909 * @param flags The flags, @c ECORE_FD_READ, @c ECORE_FD_WRITE or
910 * @c ECORE_FD_ERROR to query.
911 * @return #EINA_TRUE if any of the given flags are active. #EINA_FALSE otherwise.
912 * @ingroup Ecore_FD_Handler_Group
915 ecore_main_fd_handler_active_get(Ecore_Fd_Handler *fd_handler, Ecore_Fd_Handler_Flags flags)
917 int ret = EINA_FALSE;
919 if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
921 ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
922 "ecore_main_fd_handler_active_get");
925 if ((flags & ECORE_FD_READ) && (fd_handler->read_active)) ret = EINA_TRUE;
926 if ((flags & ECORE_FD_WRITE) && (fd_handler->write_active)) ret = EINA_TRUE;
927 if ((flags & ECORE_FD_ERROR) && (fd_handler->error_active)) ret = EINA_TRUE;
932 * Set what active streams the given FD handler should be monitoring.
933 * @param fd_handler The given FD handler.
934 * @param flags The flags to be watching.
935 * @ingroup Ecore_FD_Handler_Group
938 ecore_main_fd_handler_active_set(Ecore_Fd_Handler *fd_handler, Ecore_Fd_Handler_Flags flags)
940 if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
942 ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
943 "ecore_main_fd_handler_active_set");
946 fd_handler->flags = flags;
947 if (_ecore_main_fdh_poll_modify(fd_handler) < 0)
949 ERR("Failed to mod epoll fd %d!", fd_handler->fd);
962 _ecore_main_shutdown(void)
967 "*** ECORE WARINING: Calling ecore_shutdown() while still in the main loop.\n"
968 "*** Program may crash or behave strangely now.");
973 Ecore_Fd_Handler *fdh;
976 fd_handlers = (Ecore_Fd_Handler *) eina_inlist_remove(EINA_INLIST_GET(fd_handlers),
977 EINA_INLIST_GET(fdh));
978 ECORE_MAGIC_SET(fdh, ECORE_MAGIC_NONE);
981 if (fd_handlers_with_buffer)
982 fd_handlers_with_buffer = eina_list_free(fd_handlers_with_buffer);
983 if (fd_handlers_with_prep)
984 fd_handlers_with_prep = eina_list_free(fd_handlers_with_prep);
985 if (fd_handlers_to_delete)
986 fd_handlers_to_delete = eina_list_free(fd_handlers_to_delete);
987 if (fd_handlers_to_call)
988 fd_handlers_to_call = eina_list_free(fd_handlers_to_call);
990 fd_handlers_to_call_current = NULL;
991 fd_handlers_to_delete = NULL;
992 fd_handler_current = NULL;
995 while (win32_handlers)
997 Ecore_Win32_Handler *wh;
1000 win32_handlers = (Ecore_Win32_Handler *) eina_inlist_remove(EINA_INLIST_GET(win32_handlers),
1001 EINA_INLIST_GET(wh));
1002 ECORE_MAGIC_SET(wh, ECORE_MAGIC_NONE);
1005 win32_handlers_delete_me = EINA_FALSE;
1006 win32_handler_current = NULL;
1011 _ecore_main_prepare_handlers(void)
1013 Ecore_Fd_Handler *fdh;
1016 /* call the prepare callback for all handlers with prep functions */
1017 EINA_LIST_FOREACH_SAFE(fd_handlers_with_prep, l, l2, fdh)
1021 fd_handlers_with_prep = eina_list_remove_list(l, fd_handlers_with_prep);
1024 if (!fdh->delete_me && fdh->prep_func)
1027 fdh->prep_func(fdh->prep_data, fdh);
1031 fd_handlers_with_prep = eina_list_remove_list(fd_handlers_with_prep, l);
1036 _ecore_main_select(double timeout)
1038 struct timeval tv, *t;
1039 fd_set rfds, wfds, exfds;
1043 Ecore_Fd_Handler *fdh;
1047 if ((!finite(timeout)) || (timeout == 0.0)) /* finite() tests for NaN, too big, too small, and infinity. */
1053 else if (timeout > 0.0)
1058 timeout += (0.5 / HZ);
1060 usec = (int)((timeout - (double)sec) * 1000000);
1063 usec = (int)((timeout - (double)sec) * 1000000);
1074 /* call the prepare callback for all handlers */
1075 if (fd_handlers_with_prep)
1076 _ecore_main_prepare_handlers();
1078 EINA_INLIST_FOREACH(fd_handlers, fdh)
1080 if (!fdh->delete_me)
1082 if (fdh->flags & ECORE_FD_READ)
1084 FD_SET(fdh->fd, &rfds);
1085 if (fdh->fd > max_fd) max_fd = fdh->fd;
1087 if (fdh->flags & ECORE_FD_WRITE)
1089 FD_SET(fdh->fd, &wfds);
1090 if (fdh->fd > max_fd) max_fd = fdh->fd;
1092 if (fdh->flags & ECORE_FD_ERROR)
1094 FD_SET(fdh->fd, &exfds);
1095 if (fdh->fd > max_fd) max_fd = fdh->fd;
1099 #else /* HAVE_EPOLL */
1100 /* polling on the epoll fd will wake when an fd in the epoll set is active */
1101 max_fd = _ecore_get_epoll_fd();
1102 FD_SET(max_fd, &rfds);
1103 #endif /* HAVE_EPOLL */
1105 if (_ecore_signal_count_get()) return -1;
1107 ret = main_loop_select(max_fd + 1, &rfds, &wfds, &exfds, t);
1109 _ecore_time_loop_time = ecore_time_get();
1113 if (errno == EINTR) return -1;
1114 else if (errno == EBADF) _ecore_main_fd_handlers_bads_rem();
1120 _ecore_main_fdh_poll_mark_active();
1121 #else /* HAVE_EPOLL */
1122 Ecore_Fd_Handler *fdh;
1124 EINA_INLIST_FOREACH(fd_handlers, fdh)
1126 if (!fdh->delete_me)
1129 pst = st = fdh->read_active | fdh->write_active | fdh->error_active;
1130 if ((FD_ISSET(fdh->fd, &rfds)) && (!fdh->read_active))
1131 st = fdh->read_active = EINA_TRUE;
1132 if ((FD_ISSET(fdh->fd, &wfds)) && (!fdh->write_active))
1133 st = fdh->write_active = EINA_TRUE;
1134 if ((FD_ISSET(fdh->fd, &exfds)) && (!fdh->error_active))
1135 st = fdh->error_active = EINA_TRUE;
1137 fd_handlers_to_call = eina_list_append(fd_handlers_to_call, fdh);
1140 #endif /* HAVE_EPOLL */
1141 _ecore_main_fd_handlers_cleanup();
1143 _ecore_main_win32_handlers_cleanup();
1152 _ecore_main_fd_handlers_bads_rem(void)
1154 Ecore_Fd_Handler *fdh;
1158 ERR("Removing bad fds");
1159 for (l = EINA_INLIST_GET(fd_handlers); l; )
1161 fdh = (Ecore_Fd_Handler *) l;
1165 if ((fcntl(fdh->fd, F_GETFD) < 0) && (errno == EBADF))
1167 ERR("Found bad fd at index %d", fdh->fd);
1168 if (fdh->flags & ECORE_FD_ERROR)
1170 ERR("Fd set for error! calling user");
1172 if (!fdh->func(fdh->data, fdh))
1174 ERR("Fd function err returned 0, remove it");
1175 if (!fdh->delete_me)
1177 fdh->delete_me = EINA_TRUE;
1178 fd_handlers_to_delete = eina_list_append(fd_handlers_to_delete, fdh);
1186 ERR("Problematic fd found at %d! setting it for delete", fdh->fd);
1187 if (!fdh->delete_me)
1189 fdh->delete_me = EINA_TRUE;
1190 fd_handlers_to_delete = eina_list_append(fd_handlers_to_delete, fdh);
1200 ERR("No bad fd found. Maybe a foreign fd from glib?");
1202 ERR("No bad fd found. EEEK!");
1205 _ecore_main_fd_handlers_cleanup();
1210 _ecore_main_fd_handlers_cleanup(void)
1212 Ecore_Fd_Handler *fdh;
1215 if (!fd_handlers_to_delete) return;
1216 EINA_LIST_FOREACH_SAFE(fd_handlers_to_delete, l, l2, fdh)
1220 fd_handlers_to_delete = eina_list_remove_list(l, fd_handlers_to_delete);
1223 /* fdh->delete_me should be set for all fdhs at the start of the list */
1224 if (fdh->references)
1226 if (fdh->buf_func && fd_handlers_with_buffer)
1227 fd_handlers_with_buffer = eina_list_remove(fd_handlers_with_buffer, fdh);
1228 if (fdh->prep_func && fd_handlers_with_prep)
1229 fd_handlers_with_prep = eina_list_remove(fd_handlers_with_prep, fdh);
1230 fd_handlers = (Ecore_Fd_Handler *)
1231 eina_inlist_remove(EINA_INLIST_GET(fd_handlers), EINA_INLIST_GET(fdh));
1232 ECORE_MAGIC_SET(fdh, ECORE_MAGIC_NONE);
1234 fd_handlers_to_delete = eina_list_remove_list(fd_handlers_to_delete, l);
1240 _ecore_main_win32_handlers_cleanup(void)
1242 Ecore_Win32_Handler *wh;
1244 int deleted_in_use = 0;
1246 if (!win32_handlers_delete_me) return;
1247 for (l = EINA_INLIST_GET(win32_handlers); l; )
1249 wh = (Ecore_Win32_Handler *)l;
1260 win32_handlers = (Ecore_Win32_Handler *)
1261 eina_inlist_remove(EINA_INLIST_GET(win32_handlers),
1262 EINA_INLIST_GET(wh));
1263 ECORE_MAGIC_SET(wh, ECORE_MAGIC_NONE);
1267 if (!deleted_in_use) win32_handlers_delete_me = EINA_FALSE;
1272 _ecore_main_fd_handlers_call(void)
1274 if (!fd_handlers_to_call_current)
1276 /* regular main loop, start from head */
1277 fd_handlers_to_call_current = fd_handlers_to_call;
1278 fd_handlers_to_call_current_next = eina_list_next(fd_handlers_to_call_current);
1282 /* recursive main loop, continue from where we were */
1283 fd_handlers_to_call_current = fd_handlers_to_call_current_next;
1284 fd_handlers_to_call_current_next = eina_list_next(fd_handlers_to_call_current);
1287 while (fd_handlers_to_call_current)
1289 Ecore_Fd_Handler *fdh = fd_handlers_to_call_current->data;
1291 if (!fdh->delete_me)
1293 if ((fdh->read_active) ||
1294 (fdh->write_active) ||
1295 (fdh->error_active))
1298 if (!fdh->func(fdh->data, fdh))
1300 if (!fdh->delete_me)
1302 fdh->delete_me = EINA_TRUE;
1303 fd_handlers_to_delete = eina_list_append(fd_handlers_to_delete, fdh);
1309 fdh->read_active = EINA_FALSE;
1310 fdh->write_active = EINA_FALSE;
1311 fdh->error_active = EINA_FALSE;
1315 fd_handlers_to_call = eina_list_remove_list(fd_handlers_to_call, fd_handlers_to_call_current);
1316 fd_handlers_to_call_current = fd_handlers_to_call_current_next;
1317 fd_handlers_to_call_current_next = eina_list_next(fd_handlers_to_call_current_next);
1322 _ecore_main_fd_handlers_buf_call(void)
1324 Ecore_Fd_Handler *fdh;
1329 EINA_LIST_FOREACH_SAFE(fd_handlers_with_buffer, l, l2, fdh)
1333 fd_handlers_with_buffer = eina_list_remove_list(l, fd_handlers_with_buffer);
1336 if ((!fdh->delete_me) && fdh->buf_func)
1339 if (fdh->buf_func(fdh->buf_data, fdh))
1341 ret |= fdh->func(fdh->data, fdh);
1342 if (!fdh->read_active)
1344 fdh->read_active = EINA_TRUE;
1345 if ((!fdh->write_active) && (!fdh->error_active))
1346 fd_handlers_to_call = eina_list_append(fd_handlers_to_call, fdh);
1352 fd_handlers_with_buffer = eina_list_remove_list(fd_handlers_with_buffer, l);
1357 #ifndef USE_G_MAIN_LOOP
1359 _ecore_main_loop_iterate_internal(int once_only)
1361 double next_time = -1.0;
1366 /* expire any timers */
1367 while (_ecore_timer_call(_ecore_time_loop_time));
1368 _ecore_timer_cleanup();
1370 /* process signals into events .... */
1371 while (_ecore_signal_count_get()) _ecore_signal_call();
1372 if (_ecore_event_exist())
1374 _ecore_idle_enterer_call();
1376 _ecore_main_select(0.0);
1377 _ecore_time_loop_time = ecore_time_get();
1378 _ecore_timer_enable_new();
1379 goto process_events;
1381 /* call idle enterers ... */
1382 if (!once_only) _ecore_idle_enterer_call();
1385 have_event = have_signal = 0;
1387 if (_ecore_main_select(0.0) > 0) have_event = 1;
1388 if (_ecore_signal_count_get() > 0) have_signal = 1;
1389 if (have_signal || have_event)
1391 _ecore_time_loop_time = ecore_time_get();
1392 _ecore_timer_enable_new();
1393 goto process_events;
1397 /* if these calls caused any buffered events to appear - deal with them */
1398 if (fd_handlers_with_buffer)
1399 _ecore_main_fd_handlers_buf_call();
1401 /* if there are any - jump to processing them */
1402 if (_ecore_event_exist())
1405 _ecore_main_select(0.0);
1406 _ecore_time_loop_time = ecore_time_get();
1407 _ecore_timer_enable_new();
1408 goto process_events;
1412 _ecore_idle_enterer_call();
1414 _ecore_time_loop_time = ecore_time_get();
1415 _ecore_timer_enable_new();
1419 if (_ecore_fps_debug)
1421 t2 = ecore_time_get();
1422 if ((t1 > 0.0) && (t2 > 0.0))
1423 _ecore_fps_debug_runtime_add(t2 - t1);
1426 /* any timers re-added as a result of these are allowed to go */
1427 _ecore_timer_enable_new();
1430 _ecore_time_loop_time = ecore_time_get();
1432 _ecore_timer_enable_new();
1435 if (!_ecore_event_exist())
1438 have_event = have_signal = 0;
1439 next_time = _ecore_timer_next_get();
1444 if (!_ecore_idler_exist())
1446 if (_ecore_main_select(-1.0) > 0) have_event = 1;
1453 if (!_ecore_idler_call()) goto start_loop;
1454 if (_ecore_event_exist()) break;
1455 if (_ecore_main_select(0.0) > 0) have_event = 1;
1456 if (_ecore_signal_count_get() > 0) have_signal = 1;
1457 if (have_event || have_signal) break;
1458 if (_ecore_timers_exists()) goto start_loop;
1467 if (!_ecore_idler_exist())
1469 if (_ecore_main_select(next_time) > 0) have_event = 1;
1476 if (!_ecore_idler_call()) goto start_loop;
1477 if (_ecore_event_exist()) break;
1478 if (_ecore_main_select(0.0) > 0) have_event = 1;
1479 if (_ecore_signal_count_get() > 0) have_signal = 1;
1480 if (have_event || have_signal) break;
1481 next_time = _ecore_timer_next_get();
1482 if (next_time <= 0) break;
1487 _ecore_time_loop_time = ecore_time_get();
1489 if (_ecore_fps_debug) t1 = ecore_time_get();
1490 /* we came out of our "wait state" so idle has exited */
1491 if (!once_only) _ecore_idle_exiter_call();
1492 /* call the fd handler per fd that became alive... */
1493 /* this should read or write any data to the monitored fd and then */
1494 /* post events onto the ecore event pipe if necessary */
1496 _ecore_main_fd_handlers_call();
1497 if (fd_handlers_with_buffer)
1498 _ecore_main_fd_handlers_buf_call();
1499 /* process signals into events .... */
1500 while (_ecore_signal_count_get()) _ecore_signal_call();
1501 /* handle events ... */
1502 _ecore_event_call();
1503 _ecore_main_fd_handlers_cleanup();
1505 if (once_only) _ecore_idle_enterer_call();
1512 _ecore_main_win32_select(int nfds __UNUSED__, fd_set *readfds, fd_set *writefds,
1513 fd_set *exceptfds, struct timeval *tv)
1515 HANDLE objects[MAXIMUM_WAIT_OBJECTS];
1516 int sockets[MAXIMUM_WAIT_OBJECTS];
1517 Ecore_Fd_Handler *fdh;
1518 Ecore_Win32_Handler *wh;
1519 unsigned int objects_nbr = 0;
1520 unsigned int handles_nbr = 0;
1521 unsigned int events_nbr = 0;
1528 /* Create an event object per socket */
1529 EINA_INLIST_FOREACH(fd_handlers, fdh)
1535 if (FD_ISSET(fdh->fd, readfds))
1536 network_event |= FD_READ;
1537 if (FD_ISSET(fdh->fd, writefds))
1538 network_event |= FD_WRITE;
1539 if (FD_ISSET(fdh->fd, exceptfds))
1540 network_event |= FD_OOB;
1544 event = WSACreateEvent();
1545 WSAEventSelect(fdh->fd, event, network_event);
1546 objects[objects_nbr] = event;
1547 sockets[events_nbr] = fdh->fd;
1553 /* store the HANDLEs in the objects to wait for */
1554 EINA_INLIST_FOREACH(win32_handlers, wh)
1556 objects[objects_nbr] = wh->h;
1561 /* Empty the queue before waiting */
1562 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
1564 TranslateMessage(&msg);
1565 DispatchMessage(&msg);
1568 /* Wait for any message sent or posted to this queue */
1569 /* or for one of the passed handles be set to signaled. */
1573 timeout = (DWORD)((tv->tv_sec * 1000.0) + (tv->tv_usec / 1000.0));
1575 if (timeout == 0) return 0;
1577 result = MsgWaitForMultipleObjects(objects_nbr, (const HANDLE *)objects, EINA_FALSE,
1578 timeout, QS_ALLINPUT);
1584 /* The result tells us the type of event we have. */
1585 if (result == WAIT_FAILED)
1589 msg = evil_last_error_get();
1590 ERR(" * %s\n", msg);
1594 else if (result == WAIT_TIMEOUT)
1596 /* ERR("time out\n"); */
1599 else if (result == (WAIT_OBJECT_0 + objects_nbr))
1601 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
1603 TranslateMessage(&msg);
1604 DispatchMessage(&msg);
1609 else if ((result >= 0) && (result < WAIT_OBJECT_0 + events_nbr))
1611 WSANETWORKEVENTS network_event;
1613 WSAEnumNetworkEvents(sockets[result], objects[result], &network_event);
1615 if (network_event.lNetworkEvents & FD_READ)
1616 FD_SET(sockets[result], readfds);
1617 if (network_event.lNetworkEvents & FD_WRITE)
1618 FD_SET(sockets[result], writefds);
1619 if (network_event.lNetworkEvents & FD_OOB)
1620 FD_SET(sockets[result], exceptfds);
1624 else if ((result >= (WAIT_OBJECT_0 + events_nbr)) &&
1625 (result < (WAIT_OBJECT_0 + objects_nbr)))
1627 if (!win32_handler_current)
1629 /* regular main loop, start from head */
1630 win32_handler_current = win32_handlers;
1634 /* recursive main loop, continue from where we were */
1635 win32_handler_current = (Ecore_Win32_Handler *)EINA_INLIST_GET(win32_handler_current)->next;
1638 while (win32_handler_current)
1640 wh = win32_handler_current;
1642 if (objects[result - WAIT_OBJECT_0] == wh->h)
1647 if (!wh->func(wh->data, wh))
1649 wh->delete_me = EINA_TRUE;
1650 win32_handlers_delete_me = EINA_TRUE;
1655 if (win32_handler_current) /* may have changed in recursive main loops */
1656 win32_handler_current = (Ecore_Win32_Handler *)EINA_INLIST_GET(win32_handler_current)->next;
1662 ERR("unknown result...\n");
1666 /* Remove event objects again */
1667 for (i = 0; i < events_nbr; i++) WSACloseEvent(objects[i]);