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
64 Ecore_Fd_Handler *next_ready;
66 Ecore_Fd_Handler_Flags flags;
71 Ecore_Fd_Prep_Cb prep_func;
74 Eina_Bool read_active : 1;
75 Eina_Bool write_active : 1;
76 Eina_Bool error_active : 1;
77 Eina_Bool delete_me : 1;
78 #if defined(USE_G_MAIN_LOOP) && !defined(HAVE_EPOLL)
84 struct _Ecore_Win32_Handler
89 Ecore_Win32_Handle_Cb func;
92 Eina_Bool delete_me : 1;
97 static int _ecore_main_select(double timeout);
98 static void _ecore_main_prepare_handlers(void);
99 static void _ecore_main_fd_handlers_cleanup(void);
101 static void _ecore_main_fd_handlers_bads_rem(void);
103 static void _ecore_main_fd_handlers_call(void);
104 static int _ecore_main_fd_handlers_buf_call(void);
105 #ifndef USE_G_MAIN_LOOP
106 static void _ecore_main_loop_iterate_internal(int once_only);
110 static int _ecore_main_win32_select(int nfds, fd_set *readfds, fd_set *writefds,
111 fd_set *exceptfds, struct timeval *timeout);
112 static void _ecore_main_win32_handlers_cleanup(void);
115 static int in_main_loop = 0;
116 static int do_quit = 0;
117 static Ecore_Fd_Handler *fd_handlers = NULL;
118 static Ecore_Fd_Handler *fd_handler_current = NULL;
119 static Eina_List *fd_handlers_with_prep = NULL;
120 static Eina_List *fd_handlers_with_buffer = NULL;
121 static Eina_List *fd_handlers_to_delete = NULL;
123 /* single linked list of ready fdhs, terminated by loop to self */
124 static Ecore_Fd_Handler *fd_handlers_to_call;
125 static Ecore_Fd_Handler *fd_handlers_to_call_current;
128 static Ecore_Win32_Handler *win32_handlers = NULL;
129 static Ecore_Win32_Handler *win32_handler_current = NULL;
130 static Eina_Bool win32_handlers_delete_me = EINA_FALSE;
134 static Ecore_Select_Function main_loop_select = _ecore_main_win32_select;
136 static Ecore_Select_Function main_loop_select = select;
139 static double t1 = 0.0;
140 static double t2 = 0.0;
143 static int epoll_fd = -1;
144 static pid_t epoll_pid;
147 #ifdef USE_G_MAIN_LOOP
149 static GPollFD ecore_epoll_fd;
151 static GSource *ecore_glib_source;
152 static guint ecore_glib_source_id;
153 static GMainLoop* ecore_main_loop;
154 static gboolean ecore_idling;
155 static gboolean ecore_fds_ready;
159 _ecore_fd_valid(void)
162 if (fcntl(epoll_fd, F_GETFD) < 0)
164 ERR("arghhh you caught me! report a backtrace to edevel!");
171 _ecore_try_add_to_call_list(Ecore_Fd_Handler *fdh)
173 /* check if this fdh is already in the list */
176 if (fdh->read_active || fdh->write_active || fdh->error_active)
179 * make sure next_ready is non-null by pointing to ourselves
180 * use that to indicate this fdh is in the ready list
181 * insert at the head of the list to avoid trouble
183 fdh->next_ready = fd_handlers_to_call ? fd_handlers_to_call : fdh;
184 fd_handlers_to_call = fdh;
190 _ecore_get_epoll_fd(void)
192 if (epoll_pid && epoll_pid != getpid())
195 _ecore_main_loop_shutdown();
197 if (epoll_pid == 0 && epoll_fd < 0)
199 _ecore_main_loop_init();
205 _ecore_epoll_add(int efd, int fd, int events, void *ptr)
207 struct epoll_event ev;
209 memset(&ev, 0, sizeof (ev));
212 INF("adding poll on %d %08x", fd, events);
213 return epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev);
217 _ecore_poll_events_from_fdh(Ecore_Fd_Handler *fdh)
220 if (fdh->flags & ECORE_FD_READ) events |= EPOLLIN;
221 if (fdh->flags & ECORE_FD_WRITE) events |= EPOLLOUT;
222 if (fdh->flags & ECORE_FD_ERROR) events |= EPOLLERR;
227 _ecore_poll_events_from_fdh(Ecore_Fd_Handler *fdh __UNUSED__)
233 #ifdef USE_G_MAIN_LOOP
235 _gfd_events_from_fdh(Ecore_Fd_Handler *fdh)
238 if (fdh->flags & ECORE_FD_READ) events |= G_IO_IN;
239 if (fdh->flags & ECORE_FD_WRITE) events |= G_IO_OUT;
240 if (fdh->flags & ECORE_FD_ERROR) events |= G_IO_ERR;
246 _ecore_main_fdh_poll_add(Ecore_Fd_Handler *fdh)
250 r = _ecore_epoll_add(_ecore_get_epoll_fd(), fdh->fd,
251 _ecore_poll_events_from_fdh(fdh), fdh);
252 #elif USE_G_MAIN_LOOP
253 fdh->gfd.fd = fdh->fd;
254 fdh->gfd.events = _gfd_events_from_fdh(fdh);
255 fdh->gfd.revents = 0;
256 INF("adding gpoll on %d %08x", fdh->fd, fdh->gfd.events);
257 g_source_add_poll(ecore_glib_source, &fdh->gfd);
259 if (!ECORE_MAGIC_CHECK(fdh, ECORE_MAGIC_FD_HANDLER))
261 ECORE_MAGIC_FAIL(fdh, ECORE_MAGIC_FD_HANDLER,
262 "_ecore_main_fdh_poll_add");
269 _ecore_main_fdh_poll_del(Ecore_Fd_Handler *fdh)
272 struct epoll_event ev;
273 int efd = _ecore_get_epoll_fd();
275 memset(&ev, 0, sizeof (ev));
276 INF("removing poll on %d", fdh->fd);
277 /* could get an EBADF if somebody closed the FD before removing it */
278 if ((epoll_ctl(efd, EPOLL_CTL_DEL, fdh->fd, &ev) < 0))
282 WRN("fd %d was closed, can't remove from epoll - reinit!",
284 _ecore_main_loop_shutdown();
285 _ecore_main_loop_init();
289 ERR("Failed to delete epoll fd %d! (errno=%d)", fdh->fd, errno);
292 #elif USE_G_MAIN_LOOP
293 fdh->gfd.fd = fdh->fd;
294 fdh->gfd.events = _gfd_events_from_fdh(fdh);
295 fdh->gfd.revents = 0;
296 INF("adding gpoll on %d %08x", fdh->fd, fdh->gfd.events);
297 g_source_add_poll(ecore_glib_source, &fdh->gfd);
299 if (!ECORE_MAGIC_CHECK(fdh, ECORE_MAGIC_FD_HANDLER))
301 ECORE_MAGIC_FAIL(fdh, ECORE_MAGIC_FD_HANDLER,
302 "_ecore_main_fdh_poll_del");
308 _ecore_main_fdh_poll_modify(Ecore_Fd_Handler *fdh)
312 struct epoll_event ev;
313 int efd = _ecore_get_epoll_fd();
315 memset(&ev, 0, sizeof (ev));
316 ev.events = _ecore_poll_events_from_fdh(fdh);
318 INF("modifing epoll on %d to %08x", fdh->fd, ev.events);
319 r = epoll_ctl(efd, EPOLL_CTL_MOD, fdh->fd, &ev);
320 #elif USE_G_MAIN_LOOP
321 fdh->gfd.fd = fdh->fd;
322 fdh->gfd.events = _gfd_events_from_fdh(fdh);
323 fdh->gfd.revents = 0;
324 INF("modifing gpoll on %d to %08x", fdh->fd, fdh->gfd.events);
326 if (!ECORE_MAGIC_CHECK(fdh, ECORE_MAGIC_FD_HANDLER))
328 ECORE_MAGIC_FAIL(fdh, ECORE_MAGIC_FD_HANDLER,
329 "_ecore_main_fdh_poll_modify");
336 static inline int _ecore_main_fdh_poll_mark_active(void)
338 struct epoll_event ev[32];
340 int efd = _ecore_get_epoll_fd();
342 memset(&ev, 0, sizeof (ev));
343 ret = epoll_wait(efd, ev, sizeof(ev) / sizeof(struct epoll_event), 0);
346 if (errno == EINTR) return -1;
347 ERR("epoll_wait failed %d", errno);
351 for (i = 0; i < ret; i++)
353 Ecore_Fd_Handler *fdh;
355 fdh = ev[i].data.ptr;
356 if (!ECORE_MAGIC_CHECK(fdh, ECORE_MAGIC_FD_HANDLER))
358 ECORE_MAGIC_FAIL(fdh, ECORE_MAGIC_FD_HANDLER,
359 "_ecore_main_fdh_poll_mark_active");
364 ERR("deleted fd in epoll");
368 if (ev[i].events & EPOLLIN)
369 fdh->read_active = EINA_TRUE;
370 if (ev[i].events & EPOLLOUT)
371 fdh->write_active = EINA_TRUE;
372 if (ev[i].events & EPOLLERR)
373 fdh->error_active = EINA_TRUE;
375 _ecore_try_add_to_call_list(fdh);
381 #elif USE_G_MAIN_LOOP
383 static inline int _ecore_main_fdh_poll_mark_active(void)
385 Ecore_Fd_Handler *fdh;
388 /* call the prepare callback for all handlers */
389 EINA_INLIST_FOREACH(fd_handlers, fdh)
394 if (fdh->gfd.revents & G_IO_IN)
395 fdh->read_active = EINA_TRUE;
396 if (fdh->gfd.revents & G_IO_OUT)
397 fdh->write_active = EINA_TRUE;
398 if (fdh->gfd.revents & G_IO_ERR)
399 fdh->error_active = EINA_TRUE;
401 _ecore_try_add_to_call_list(fdh);
403 if (fdh->gfd.revents & (G_IO_IN|G_IO_OUT|G_IO_ERR)) ret++;
406 INF("found %d active fds", ret);
413 #ifdef USE_G_MAIN_LOOP
415 /* like we are about to enter main_loop_select in _ecore_main_select */
417 _ecore_main_gsource_prepare(GSource *source, gint *next_time)
419 double t = _ecore_timer_next_get();
422 INF("enter, next timeout in %.1f", t);
427 while (_ecore_timer_call(_ecore_time_loop_time));
428 _ecore_timer_cleanup();
430 /* when idling, busy loop checking the fds only */
431 if (!ecore_idling) _ecore_idle_enterer_call();
434 /* don't check fds if somebody quit */
435 running = g_main_loop_is_running(ecore_main_loop);
438 /* only set idling state in dispatch */
439 if (ecore_idling && !_ecore_idler_exist())
441 if (_ecore_timers_exists())
443 double t = _ecore_timer_next_get();
444 *next_time = (t / 1000.0);
452 if (fd_handlers_with_prep)
453 _ecore_main_prepare_handlers();
457 INF("leave, timeout = %d", *next_time);
459 /* ready if we're not running (about to quit) */
464 _ecore_main_gsource_check(GSource *source)
469 ecore_fds_ready = (_ecore_main_fdh_poll_mark_active() > 0);
470 _ecore_main_fd_handlers_cleanup();
472 _ecore_time_loop_time = ecore_time_get();
473 _ecore_timer_enable_new();
478 return TRUE; /* always dispatch */
481 /* like we just came out of main_loop_select in _ecore_main_select */
483 _ecore_main_gsource_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
485 gboolean events_ready, timers_ready, idlers_ready, signals_ready;
486 double next_time = _ecore_timer_next_get();
488 events_ready = _ecore_event_exist();
489 timers_ready = _ecore_timers_exists() && (0.0 <= next_time);
490 idlers_ready = _ecore_idler_exist();
491 signals_ready = (_ecore_signal_count_get() > 0);
494 INF("enter idling=%d fds=%d events=%d signals=%d timers=%d (next=%.2f) idlers=%d",
495 ecore_idling, ecore_fds_ready, events_ready, signals_ready,
496 _ecore_timers_exists(), next_time, idlers_ready);
498 if (ecore_idling && events_ready)
500 INF("calling idle exiters");
501 _ecore_idle_exiter_call();
504 else if (!ecore_idling && !events_ready)
512 INF("calling idler");
515 events_ready = _ecore_event_exist();
516 timers_ready = _ecore_timers_exists() && (0.0 <= next_time);
517 idlers_ready = _ecore_idler_exist();
519 if ((ecore_fds_ready || events_ready || timers_ready || idlers_ready || signals_ready))
521 INF("calling idle exiters");
522 _ecore_idle_exiter_call();
531 _ecore_main_fd_handlers_call();
532 if (fd_handlers_with_buffer)
533 _ecore_main_fd_handlers_buf_call();
534 while (_ecore_signal_count_get()) _ecore_signal_call();
536 _ecore_main_fd_handlers_cleanup();
543 return TRUE; /* what should be returned here? */
547 _ecore_main_gsource_finalize(GSource *source)
552 static GSourceFuncs ecore_gsource_funcs =
554 .prepare = _ecore_main_gsource_prepare,
555 .check = _ecore_main_gsource_check,
556 .dispatch = _ecore_main_gsource_dispatch,
557 .finalize = _ecore_main_gsource_finalize,
563 _ecore_main_loop_init(void)
567 epoll_fd = epoll_create(1);
569 CRIT("Failed to create epoll fd!");
570 epoll_pid = getpid();
572 /* add polls on all our file descriptors */
573 Ecore_Fd_Handler *fdh;
574 EINA_INLIST_FOREACH(fd_handlers, fdh)
578 _ecore_epoll_add(epoll_fd, fdh->fd,
579 _ecore_poll_events_from_fdh(fdh), fdh);
580 _ecore_main_fdh_poll_add(fdh);
585 #ifdef USE_G_MAIN_LOOP
586 ecore_glib_source = g_source_new(&ecore_gsource_funcs, sizeof (GSource));
587 if (!ecore_glib_source)
588 CRIT("Failed to create glib source for epoll!");
592 ecore_epoll_fd.fd = epoll_fd;
593 ecore_epoll_fd.events = G_IO_IN;
594 ecore_epoll_fd.revents = 0;
595 g_source_add_poll(ecore_glib_source, &ecore_epoll_fd);
597 ecore_glib_source_id = g_source_attach(ecore_glib_source, NULL);
598 if (ecore_glib_source_id <= 0)
599 CRIT("Failed to attach glib source to default context");
606 _ecore_main_loop_shutdown(void)
608 #ifdef USE_G_MAIN_LOOP
609 if (ecore_glib_source)
611 g_source_destroy(ecore_glib_source);
612 ecore_glib_source = NULL;
628 * @addtogroup Ecore_Group Ecore - Main Loop and Job Functions.
634 * @addtogroup Ecore_Main_Loop_Group Ecore Main Loop functions
636 * These functions control the Ecore event handling loop. This loop is
637 * designed to work on embedded systems all the way to large and
638 * powerful mutli-cpu workstations.
640 * It serialises all system signals and events into a single event
641 * queue, that can be easily processed without needing to worry about
642 * concurrency. A properly written, event-driven program using this
643 * kind of programming does not need threads. It makes the program very
644 * robust and easy to follow.
646 * Here is an example of simple program and its basic event loop flow:
647 * @image html prog_flow.png
649 * For examples of setting up and using a main loop, see
650 * @ref event_handler_example.c and @ref timer_example.c.
656 * Runs a single iteration of the main loop to process everything on the
660 ecore_main_loop_iterate(void)
662 #ifndef USE_G_MAIN_LOOP
663 _ecore_main_loop_iterate_internal(1);
665 g_main_context_iteration(NULL, 1);
670 * Runs the application main loop.
672 * This function will not return until @ref ecore_main_loop_quit is called.
676 ecore_main_loop_begin(void)
678 #ifndef USE_G_MAIN_LOOP
680 while (do_quit == 0) _ecore_main_loop_iterate_internal(0);
684 ecore_main_loop = g_main_loop_new(NULL, FALSE);
685 g_main_loop_run(ecore_main_loop);
690 * Quits the main loop once all the events currently on the queue have
694 ecore_main_loop_quit(void)
696 #ifndef USE_G_MAIN_LOOP
700 g_main_loop_quit(ecore_main_loop);
706 * Sets the function to use when monitoring multiple file descriptors,
707 * and waiting until one of more of the file descriptors before ready
708 * for some class of I/O operation.
710 * This function will be used instead of the system call select and
711 * could possible be used to integrate the Ecore event loop with an
712 * external event loop.
714 * @warning you don't know how to use, don't even try to use it.
718 ecore_main_loop_select_func_set(Ecore_Select_Function func)
720 main_loop_select = func;
724 * Gets the select function set by ecore_select_func_set(),
725 * or the native select function if none was set.
728 EAPI Ecore_Select_Function
729 ecore_main_loop_select_func_get(void)
731 return main_loop_select;
735 * @defgroup Ecore_FD_Handler_Group File Event Handling Functions
737 * Functions that deal with file descriptor handlers.
741 * Adds a callback for activity on the given file descriptor.
743 * @p func will be called during the execution of @ref ecore_main_loop_begin
744 * when the file descriptor is available for reading, or writing, or both.
746 * Normally the return value from the @p func is "zero means this handler is
747 * finished and can be deleted" as is usual for handler callbacks. However,
748 * if the @p buf_func is supplied, then the return value from the @p func is
749 * "non zero means the handler should be called again in a tight loop".
751 * @p buf_func is called during event loop handling to check if data that has
752 * been read from the file descriptor is in a buffer and is available to
753 * read. Some systems (notably xlib) handle their own buffering, and would
754 * otherwise not work with select(). These systems should use a @p buf_func.
755 * This is a most annoying hack, only ecore_x uses it, so refer to that for
756 * an example. NOTE - @p func should probably return "one" always if
757 * @p buf_func is used, to avoid confusion with the other return value
760 * @param fd The file descriptor to watch.
761 * @param flags To watch it for read (@c ECORE_FD_READ) and/or
762 * (@c ECORE_FD_WRITE) write ability. @c ECORE_FD_ERROR
764 * @param func The callback function.
765 * @param data The data to pass to the callback.
766 * @param buf_func The function to call to check if any data has been
767 * buffered and already read from the fd. Can be @c NULL.
768 * @param buf_data The data to pass to the @p buf_func function.
769 * @return A fd handler handle if successful. @c NULL otherwise.
770 * @ingroup Ecore_FD_Handler_Group
772 EAPI Ecore_Fd_Handler *
773 ecore_main_fd_handler_add(int fd, Ecore_Fd_Handler_Flags flags, Ecore_Fd_Cb func, const void *data,
774 Ecore_Fd_Cb buf_func, const void *buf_data)
776 Ecore_Fd_Handler *fdh;
778 if ((fd < 0) || (flags == 0) || (!func)) return NULL;
780 fdh = calloc(1, sizeof(Ecore_Fd_Handler));
781 if (!fdh) return NULL;
782 ECORE_MAGIC_SET(fdh, ECORE_MAGIC_FD_HANDLER);
783 fdh->next_ready = NULL;
786 if (_ecore_main_fdh_poll_add(fdh) < 0)
789 ERR("Failed to add poll on fd %d (errno = %d: %s)!", fd, err, strerror(err));
793 fdh->read_active = EINA_FALSE;
794 fdh->write_active = EINA_FALSE;
795 fdh->error_active = EINA_FALSE;
796 fdh->delete_me = EINA_FALSE;
798 fdh->data = (void *)data;
799 fdh->buf_func = buf_func;
801 fd_handlers_with_buffer = eina_list_append(fd_handlers_with_buffer, fdh);
802 fdh->buf_data = (void *)buf_data;
803 fd_handlers = (Ecore_Fd_Handler *)
804 eina_inlist_append(EINA_INLIST_GET(fd_handlers),
805 EINA_INLIST_GET(fdh));
810 EAPI Ecore_Win32_Handler *
811 ecore_main_win32_handler_add(void *h, Ecore_Win32_Handle_Cb func, const void *data)
813 Ecore_Win32_Handler *wh;
815 if (!h || !func) return NULL;
817 wh = calloc(1, sizeof(Ecore_Win32_Handler));
818 if (!wh) return NULL;
819 ECORE_MAGIC_SET(wh, ECORE_MAGIC_WIN32_HANDLER);
821 wh->delete_me = EINA_FALSE;
823 wh->data = (void *)data;
824 win32_handlers = (Ecore_Win32_Handler *)
825 eina_inlist_append(EINA_INLIST_GET(win32_handlers),
826 EINA_INLIST_GET(wh));
830 EAPI Ecore_Win32_Handler *
831 ecore_main_win32_handler_add(void *h __UNUSED__, Ecore_Win32_Handle_Cb func __UNUSED__,
832 const void *data __UNUSED__)
839 * Deletes the given FD handler.
840 * @param fd_handler The given FD handler.
841 * @return The data pointer set using @ref ecore_main_fd_handler_add,
842 * for @p fd_handler on success. @c NULL otherwise.
843 * @ingroup Ecore_FD_Handler_Group
845 * Beware that if the fd is already closed, ecore may complain if it uses
846 * epoll internally, and that in some rare cases this may be able to cause
847 * crashes and instability. Remember to delete your fd handlers before the
848 * fd's they listen to are closed.
851 ecore_main_fd_handler_del(Ecore_Fd_Handler *fd_handler)
853 if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
855 ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
856 "ecore_main_fd_handler_del");
859 if (fd_handler->delete_me)
861 ERR("fdh %p deleted twice", fd_handler);
865 fd_handler->delete_me = EINA_TRUE;
866 _ecore_main_fdh_poll_del(fd_handler);
867 fd_handlers_to_delete = eina_list_append(fd_handlers_to_delete, fd_handler);
868 if (fd_handler->prep_func && fd_handlers_with_prep)
869 fd_handlers_with_prep = eina_list_remove(fd_handlers_with_prep, fd_handler);
870 if (fd_handler->buf_func && fd_handlers_with_buffer)
871 fd_handlers_with_buffer = eina_list_remove(fd_handlers_with_buffer, fd_handler);
872 return fd_handler->data;
877 ecore_main_win32_handler_del(Ecore_Win32_Handler *win32_handler)
879 if (!ECORE_MAGIC_CHECK(win32_handler, ECORE_MAGIC_WIN32_HANDLER))
881 ECORE_MAGIC_FAIL(win32_handler, ECORE_MAGIC_WIN32_HANDLER,
882 "ecore_main_win32_handler_del");
885 win32_handler->delete_me = EINA_TRUE;
886 win32_handlers_delete_me = EINA_TRUE;
887 return win32_handler->data;
891 ecore_main_win32_handler_del(Ecore_Win32_Handler *win32_handler __UNUSED__)
898 * @brief Set the prepare callback with data for a given #Ecore_Fd_Handler
899 * @param fd_handler The fd handler
900 * @param func The prep function
901 * @param data The data to pass to the prep function
902 * This function will be called prior to the the fd handler's callback function.
905 ecore_main_fd_handler_prepare_callback_set(Ecore_Fd_Handler *fd_handler, Ecore_Fd_Prep_Cb func, const void *data)
907 if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
909 ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
910 "ecore_main_fd_handler_prepare_callback_set");
913 fd_handler->prep_func = func;
914 fd_handler->prep_data = (void *)data;
915 if (fd_handlers_with_prep && (!eina_list_data_find(fd_handlers_with_prep, fd_handler)))
916 /* FIXME: THIS WILL NOT SCALE WITH LOTS OF PREP FUNCTIONS!!! */
917 fd_handlers_with_prep = eina_list_append(fd_handlers_with_prep, fd_handler);
921 * Retrieves the file descriptor that the given handler is handling.
922 * @param fd_handler The given FD handler.
923 * @return The file descriptor the handler is watching.
924 * @ingroup Ecore_FD_Handler_Group
927 ecore_main_fd_handler_fd_get(Ecore_Fd_Handler *fd_handler)
929 if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
931 ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
932 "ecore_main_fd_handler_fd_get");
935 return fd_handler->fd;
939 * Return if read, write or error, or a combination thereof, is active on the
940 * file descriptor of the given FD handler.
941 * @param fd_handler The given FD handler.
942 * @param flags The flags, @c ECORE_FD_READ, @c ECORE_FD_WRITE or
943 * @c ECORE_FD_ERROR to query.
944 * @return #EINA_TRUE if any of the given flags are active. #EINA_FALSE otherwise.
945 * @ingroup Ecore_FD_Handler_Group
948 ecore_main_fd_handler_active_get(Ecore_Fd_Handler *fd_handler, Ecore_Fd_Handler_Flags flags)
950 int ret = EINA_FALSE;
952 if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
954 ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
955 "ecore_main_fd_handler_active_get");
958 if ((flags & ECORE_FD_READ) && (fd_handler->read_active)) ret = EINA_TRUE;
959 if ((flags & ECORE_FD_WRITE) && (fd_handler->write_active)) ret = EINA_TRUE;
960 if ((flags & ECORE_FD_ERROR) && (fd_handler->error_active)) ret = EINA_TRUE;
965 * Set what active streams the given FD handler should be monitoring.
966 * @param fd_handler The given FD handler.
967 * @param flags The flags to be watching.
968 * @ingroup Ecore_FD_Handler_Group
971 ecore_main_fd_handler_active_set(Ecore_Fd_Handler *fd_handler, Ecore_Fd_Handler_Flags flags)
975 if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
977 ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
978 "ecore_main_fd_handler_active_set");
981 fd_handler->flags = flags;
982 ret = _ecore_main_fdh_poll_modify(fd_handler);
985 ERR("Failed to mod epoll fd %d: %s!", fd_handler->fd, strerror(ret));
998 _ecore_main_shutdown(void)
1003 "*** ECORE WARINING: Calling ecore_shutdown() while still in the main loop.\n"
1004 "*** Program may crash or behave strangely now.");
1009 Ecore_Fd_Handler *fdh;
1012 fd_handlers = (Ecore_Fd_Handler *) eina_inlist_remove(EINA_INLIST_GET(fd_handlers),
1013 EINA_INLIST_GET(fdh));
1014 ECORE_MAGIC_SET(fdh, ECORE_MAGIC_NONE);
1017 if (fd_handlers_with_buffer)
1018 fd_handlers_with_buffer = eina_list_free(fd_handlers_with_buffer);
1019 if (fd_handlers_with_prep)
1020 fd_handlers_with_prep = eina_list_free(fd_handlers_with_prep);
1021 if (fd_handlers_to_delete)
1022 fd_handlers_to_delete = eina_list_free(fd_handlers_to_delete);
1024 fd_handlers_to_call = NULL;
1025 fd_handlers_to_call_current = NULL;
1026 fd_handlers_to_delete = NULL;
1027 fd_handler_current = NULL;
1030 while (win32_handlers)
1032 Ecore_Win32_Handler *wh;
1034 wh = win32_handlers;
1035 win32_handlers = (Ecore_Win32_Handler *) eina_inlist_remove(EINA_INLIST_GET(win32_handlers),
1036 EINA_INLIST_GET(wh));
1037 ECORE_MAGIC_SET(wh, ECORE_MAGIC_NONE);
1040 win32_handlers_delete_me = EINA_FALSE;
1041 win32_handler_current = NULL;
1046 _ecore_main_prepare_handlers(void)
1048 Ecore_Fd_Handler *fdh;
1051 /* call the prepare callback for all handlers with prep functions */
1052 EINA_LIST_FOREACH_SAFE(fd_handlers_with_prep, l, l2, fdh)
1056 fd_handlers_with_prep = eina_list_remove_list(l, fd_handlers_with_prep);
1059 if (!fdh->delete_me && fdh->prep_func)
1062 fdh->prep_func(fdh->prep_data, fdh);
1066 fd_handlers_with_prep = eina_list_remove_list(fd_handlers_with_prep, l);
1071 _ecore_main_select(double timeout)
1073 struct timeval tv, *t;
1074 fd_set rfds, wfds, exfds;
1078 Ecore_Fd_Handler *fdh;
1082 if ((!finite(timeout)) || (timeout == 0.0)) /* finite() tests for NaN, too big, too small, and infinity. */
1088 else if (timeout > 0.0)
1093 timeout += (0.5 / HZ);
1095 usec = (int)((timeout - (double)sec) * 1000000);
1098 usec = (int)((timeout - (double)sec) * 1000000);
1109 /* call the prepare callback for all handlers */
1110 if (fd_handlers_with_prep)
1111 _ecore_main_prepare_handlers();
1113 EINA_INLIST_FOREACH(fd_handlers, fdh)
1115 if (!fdh->delete_me)
1117 if (fdh->flags & ECORE_FD_READ)
1119 FD_SET(fdh->fd, &rfds);
1120 if (fdh->fd > max_fd) max_fd = fdh->fd;
1122 if (fdh->flags & ECORE_FD_WRITE)
1124 FD_SET(fdh->fd, &wfds);
1125 if (fdh->fd > max_fd) max_fd = fdh->fd;
1127 if (fdh->flags & ECORE_FD_ERROR)
1129 FD_SET(fdh->fd, &exfds);
1130 if (fdh->fd > max_fd) max_fd = fdh->fd;
1134 #else /* HAVE_EPOLL */
1135 /* polling on the epoll fd will wake when an fd in the epoll set is active */
1136 max_fd = _ecore_get_epoll_fd();
1137 FD_SET(max_fd, &rfds);
1138 #endif /* HAVE_EPOLL */
1140 if (_ecore_signal_count_get()) return -1;
1142 ret = main_loop_select(max_fd + 1, &rfds, &wfds, &exfds, t);
1144 _ecore_time_loop_time = ecore_time_get();
1148 if (errno == EINTR) return -1;
1149 else if (errno == EBADF) _ecore_main_fd_handlers_bads_rem();
1155 _ecore_main_fdh_poll_mark_active();
1156 #else /* HAVE_EPOLL */
1157 Ecore_Fd_Handler *fdh;
1159 EINA_INLIST_FOREACH(fd_handlers, fdh)
1161 if (!fdh->delete_me)
1163 if (FD_ISSET(fdh->fd, &rfds))
1164 fdh->read_active = EINA_TRUE;
1165 if (FD_ISSET(fdh->fd, &wfds))
1166 fdh->write_active = EINA_TRUE;
1167 if (FD_ISSET(fdh->fd, &exfds))
1168 fdh->error_active = EINA_TRUE;
1169 _ecore_try_add_to_call_list(fdh);
1172 #endif /* HAVE_EPOLL */
1173 _ecore_main_fd_handlers_cleanup();
1175 _ecore_main_win32_handlers_cleanup();
1184 _ecore_main_fd_handlers_bads_rem(void)
1186 Ecore_Fd_Handler *fdh;
1190 ERR("Removing bad fds");
1191 for (l = EINA_INLIST_GET(fd_handlers); l; )
1193 fdh = (Ecore_Fd_Handler *) l;
1197 if ((fcntl(fdh->fd, F_GETFD) < 0) && (errno == EBADF))
1199 ERR("Found bad fd at index %d", fdh->fd);
1200 if (fdh->flags & ECORE_FD_ERROR)
1202 ERR("Fd set for error! calling user");
1204 if (!fdh->func(fdh->data, fdh))
1206 ERR("Fd function err returned 0, remove it");
1207 if (!fdh->delete_me)
1209 fdh->delete_me = EINA_TRUE;
1210 fd_handlers_to_delete = eina_list_append(fd_handlers_to_delete, fdh);
1218 ERR("Problematic fd found at %d! setting it for delete", fdh->fd);
1219 if (!fdh->delete_me)
1221 fdh->delete_me = EINA_TRUE;
1222 fd_handlers_to_delete = eina_list_append(fd_handlers_to_delete, fdh);
1232 ERR("No bad fd found. Maybe a foreign fd from glib?");
1234 ERR("No bad fd found. EEEK!");
1237 _ecore_main_fd_handlers_cleanup();
1242 _ecore_main_fd_handlers_cleanup(void)
1244 Ecore_Fd_Handler *fdh;
1247 if (!fd_handlers_to_delete) return;
1248 EINA_LIST_FOREACH_SAFE(fd_handlers_to_delete, l, l2, fdh)
1252 fd_handlers_to_delete = eina_list_remove_list(l, fd_handlers_to_delete);
1255 /* fdh->delete_me should be set for all fdhs at the start of the list */
1256 if (fdh->references)
1258 if (fdh->buf_func && fd_handlers_with_buffer)
1259 fd_handlers_with_buffer = eina_list_remove(fd_handlers_with_buffer, fdh);
1260 if (fdh->prep_func && fd_handlers_with_prep)
1261 fd_handlers_with_prep = eina_list_remove(fd_handlers_with_prep, fdh);
1262 fd_handlers = (Ecore_Fd_Handler *)
1263 eina_inlist_remove(EINA_INLIST_GET(fd_handlers), EINA_INLIST_GET(fdh));
1264 ECORE_MAGIC_SET(fdh, ECORE_MAGIC_NONE);
1266 fd_handlers_to_delete = eina_list_remove_list(fd_handlers_to_delete, l);
1272 _ecore_main_win32_handlers_cleanup(void)
1274 Ecore_Win32_Handler *wh;
1276 int deleted_in_use = 0;
1278 if (!win32_handlers_delete_me) return;
1279 for (l = EINA_INLIST_GET(win32_handlers); l; )
1281 wh = (Ecore_Win32_Handler *)l;
1292 win32_handlers = (Ecore_Win32_Handler *)
1293 eina_inlist_remove(EINA_INLIST_GET(win32_handlers),
1294 EINA_INLIST_GET(wh));
1295 ECORE_MAGIC_SET(wh, ECORE_MAGIC_NONE);
1299 if (!deleted_in_use) win32_handlers_delete_me = EINA_FALSE;
1304 _ecore_main_fd_handlers_call(void)
1306 /* grab a new list */
1307 if (!fd_handlers_to_call_current)
1309 fd_handlers_to_call_current = fd_handlers_to_call;
1310 fd_handlers_to_call = NULL;
1313 while (fd_handlers_to_call_current)
1315 Ecore_Fd_Handler *fdh = fd_handlers_to_call_current;
1317 if (!fdh->delete_me)
1319 if ((fdh->read_active) ||
1320 (fdh->write_active) ||
1321 (fdh->error_active))
1324 if (!fdh->func(fdh->data, fdh))
1326 if (!fdh->delete_me)
1328 fdh->delete_me = EINA_TRUE;
1329 fd_handlers_to_delete = eina_list_append(fd_handlers_to_delete, fdh);
1336 fdh->read_active = EINA_FALSE;
1337 fdh->write_active = EINA_FALSE;
1338 fdh->error_active = EINA_FALSE;
1342 /* stop when we point to ourselves */
1343 if (fdh->next_ready == fdh)
1345 fdh->next_ready = NULL;
1346 fd_handlers_to_call_current = NULL;
1350 fd_handlers_to_call_current = fdh->next_ready;
1351 fdh->next_ready = NULL;
1356 _ecore_main_fd_handlers_buf_call(void)
1358 Ecore_Fd_Handler *fdh;
1363 EINA_LIST_FOREACH_SAFE(fd_handlers_with_buffer, l, l2, fdh)
1367 fd_handlers_with_buffer = eina_list_remove_list(l, fd_handlers_with_buffer);
1370 if ((!fdh->delete_me) && fdh->buf_func)
1373 if (fdh->buf_func(fdh->buf_data, fdh))
1375 ret |= fdh->func(fdh->data, fdh);
1376 fdh->read_active = EINA_TRUE;
1377 _ecore_try_add_to_call_list(fdh);
1382 fd_handlers_with_buffer = eina_list_remove_list(fd_handlers_with_buffer, l);
1387 #ifndef USE_G_MAIN_LOOP
1389 _ecore_main_loop_iterate_internal(int once_only)
1391 double next_time = -1.0;
1396 /* expire any timers */
1397 while (_ecore_timer_call(_ecore_time_loop_time));
1398 _ecore_timer_cleanup();
1400 /* process signals into events .... */
1401 while (_ecore_signal_count_get()) _ecore_signal_call();
1402 if (_ecore_event_exist())
1404 _ecore_idle_enterer_call();
1406 _ecore_main_select(0.0);
1407 _ecore_time_loop_time = ecore_time_get();
1408 _ecore_timer_enable_new();
1409 goto process_events;
1411 /* call idle enterers ... */
1412 if (!once_only) _ecore_idle_enterer_call();
1415 have_event = have_signal = 0;
1417 if (_ecore_main_select(0.0) > 0) have_event = 1;
1418 if (_ecore_signal_count_get() > 0) have_signal = 1;
1419 if (have_signal || have_event)
1421 _ecore_time_loop_time = ecore_time_get();
1422 _ecore_timer_enable_new();
1423 goto process_events;
1427 /* if these calls caused any buffered events to appear - deal with them */
1428 if (fd_handlers_with_buffer)
1429 _ecore_main_fd_handlers_buf_call();
1431 /* if there are any - jump to processing them */
1432 if (_ecore_event_exist())
1435 _ecore_main_select(0.0);
1436 _ecore_time_loop_time = ecore_time_get();
1437 _ecore_timer_enable_new();
1438 goto process_events;
1442 _ecore_idle_enterer_call();
1444 _ecore_time_loop_time = ecore_time_get();
1445 _ecore_timer_enable_new();
1449 if (_ecore_fps_debug)
1451 t2 = ecore_time_get();
1452 if ((t1 > 0.0) && (t2 > 0.0))
1453 _ecore_fps_debug_runtime_add(t2 - t1);
1456 /* any timers re-added as a result of these are allowed to go */
1457 _ecore_timer_enable_new();
1460 _ecore_time_loop_time = ecore_time_get();
1462 _ecore_timer_enable_new();
1465 if (!_ecore_event_exist())
1468 have_event = have_signal = 0;
1469 next_time = _ecore_timer_next_get();
1474 if (!_ecore_idler_exist())
1476 if (_ecore_main_select(-1.0) > 0) have_event = 1;
1483 if (!_ecore_idler_call()) goto start_loop;
1484 if (_ecore_main_select(0.0) > 0) have_event = 1;
1485 if (_ecore_event_exist()) break;
1486 if (_ecore_signal_count_get() > 0) have_signal = 1;
1487 if (have_event || have_signal) break;
1488 if (_ecore_timers_exists()) goto start_loop;
1497 if (!_ecore_idler_exist())
1499 if (_ecore_main_select(next_time) > 0) have_event = 1;
1506 if (!_ecore_idler_call()) goto start_loop;
1507 if (_ecore_main_select(0.0) > 0) have_event = 1;
1508 if (_ecore_event_exist()) break;
1509 if (_ecore_signal_count_get() > 0) have_signal = 1;
1510 if (have_event || have_signal) break;
1511 next_time = _ecore_timer_next_get();
1512 if (next_time <= 0) break;
1517 _ecore_time_loop_time = ecore_time_get();
1519 if (_ecore_fps_debug) t1 = ecore_time_get();
1520 /* we came out of our "wait state" so idle has exited */
1521 if (!once_only) _ecore_idle_exiter_call();
1522 /* call the fd handler per fd that became alive... */
1523 /* this should read or write any data to the monitored fd and then */
1524 /* post events onto the ecore event pipe if necessary */
1526 _ecore_main_fd_handlers_call();
1527 if (fd_handlers_with_buffer)
1528 _ecore_main_fd_handlers_buf_call();
1529 /* process signals into events .... */
1530 while (_ecore_signal_count_get()) _ecore_signal_call();
1531 /* handle events ... */
1532 _ecore_event_call();
1533 _ecore_main_fd_handlers_cleanup();
1535 if (once_only) _ecore_idle_enterer_call();
1542 _ecore_main_win32_select(int nfds __UNUSED__, fd_set *readfds, fd_set *writefds,
1543 fd_set *exceptfds, struct timeval *tv)
1545 HANDLE objects[MAXIMUM_WAIT_OBJECTS];
1546 int sockets[MAXIMUM_WAIT_OBJECTS];
1547 Ecore_Fd_Handler *fdh;
1548 Ecore_Win32_Handler *wh;
1549 unsigned int objects_nbr = 0;
1550 unsigned int handles_nbr = 0;
1551 unsigned int events_nbr = 0;
1558 /* Create an event object per socket */
1559 EINA_INLIST_FOREACH(fd_handlers, fdh)
1565 if (FD_ISSET(fdh->fd, readfds))
1566 network_event |= FD_READ;
1567 if (FD_ISSET(fdh->fd, writefds))
1568 network_event |= FD_WRITE;
1569 if (FD_ISSET(fdh->fd, exceptfds))
1570 network_event |= FD_OOB;
1574 event = WSACreateEvent();
1575 WSAEventSelect(fdh->fd, event, network_event);
1576 objects[objects_nbr] = event;
1577 sockets[events_nbr] = fdh->fd;
1583 /* store the HANDLEs in the objects to wait for */
1584 EINA_INLIST_FOREACH(win32_handlers, wh)
1586 objects[objects_nbr] = wh->h;
1591 /* Empty the queue before waiting */
1592 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
1594 TranslateMessage(&msg);
1595 DispatchMessage(&msg);
1598 /* Wait for any message sent or posted to this queue */
1599 /* or for one of the passed handles be set to signaled. */
1603 timeout = (DWORD)((tv->tv_sec * 1000.0) + (tv->tv_usec / 1000.0));
1605 if (timeout == 0) return 0;
1607 result = MsgWaitForMultipleObjects(objects_nbr, (const HANDLE *)objects, EINA_FALSE,
1608 timeout, QS_ALLINPUT);
1614 /* The result tells us the type of event we have. */
1615 if (result == WAIT_FAILED)
1619 msg = evil_last_error_get();
1620 ERR(" * %s\n", msg);
1624 else if (result == WAIT_TIMEOUT)
1626 /* ERR("time out\n"); */
1629 else if (result == (WAIT_OBJECT_0 + objects_nbr))
1631 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
1633 TranslateMessage(&msg);
1634 DispatchMessage(&msg);
1639 else if ((result >= 0) && (result < WAIT_OBJECT_0 + events_nbr))
1641 WSANETWORKEVENTS network_event;
1643 WSAEnumNetworkEvents(sockets[result], objects[result], &network_event);
1645 if (network_event.lNetworkEvents & FD_READ)
1646 FD_SET(sockets[result], readfds);
1647 if (network_event.lNetworkEvents & FD_WRITE)
1648 FD_SET(sockets[result], writefds);
1649 if (network_event.lNetworkEvents & FD_OOB)
1650 FD_SET(sockets[result], exceptfds);
1654 else if ((result >= (WAIT_OBJECT_0 + events_nbr)) &&
1655 (result < (WAIT_OBJECT_0 + objects_nbr)))
1657 if (!win32_handler_current)
1659 /* regular main loop, start from head */
1660 win32_handler_current = win32_handlers;
1664 /* recursive main loop, continue from where we were */
1665 win32_handler_current = (Ecore_Win32_Handler *)EINA_INLIST_GET(win32_handler_current)->next;
1668 while (win32_handler_current)
1670 wh = win32_handler_current;
1672 if (objects[result - WAIT_OBJECT_0] == wh->h)
1677 if (!wh->func(wh->data, wh))
1679 wh->delete_me = EINA_TRUE;
1680 win32_handlers_delete_me = EINA_TRUE;
1685 if (win32_handler_current) /* may have changed in recursive main loops */
1686 win32_handler_current = (Ecore_Win32_Handler *)EINA_INLIST_GET(win32_handler_current)->next;
1692 ERR("unknown result...\n");
1696 /* Remove event objects again */
1697 for (i = 0; i < events_nbr; i++) WSACloseEvent(objects[i]);