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>
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;
80 struct _Ecore_Win32_Handler
85 Ecore_Fd_Win32_Cb func;
88 Eina_Bool delete_me : 1;
93 static int _ecore_main_select(double timeout);
94 static void _ecore_main_prepare_handlers(void);
95 static void _ecore_main_fd_handlers_cleanup(void);
97 static void _ecore_main_fd_handlers_bads_rem(void);
99 static void _ecore_main_fd_handlers_call(void);
100 static int _ecore_main_fd_handlers_buf_call(void);
101 #ifndef USE_G_MAIN_LOOP
102 static void _ecore_main_loop_iterate_internal(int once_only);
106 static int _ecore_main_win32_select(int nfds, fd_set *readfds, fd_set *writefds,
107 fd_set *exceptfds, struct timeval *timeout);
108 static void _ecore_main_win32_handlers_cleanup(void);
111 static int in_main_loop = 0;
112 static int do_quit = 0;
113 static Ecore_Fd_Handler *fd_handlers = NULL;
114 static Ecore_Fd_Handler *fd_handler_current = NULL;
115 static int fd_handlers_delete_me = 0;
117 static Ecore_Win32_Handler *win32_handlers = NULL;
118 static Ecore_Win32_Handler *win32_handler_current = NULL;
119 static int win32_handlers_delete_me = 0;
123 static Ecore_Select_Function main_loop_select = _ecore_main_win32_select;
125 static Ecore_Select_Function main_loop_select = select;
128 static double t1 = 0.0;
129 static double t2 = 0.0;
132 static int epoll_fd = -1;
135 #ifdef USE_G_MAIN_LOOP
136 static GSource *ecore_epoll_source;
137 static GPollFD ecore_epoll_fd;
138 static guint ecore_epoll_id;
139 static GMainLoop* ecore_main_loop;
140 static gboolean ecore_idling;
141 static gboolean ecore_fds_ready;
144 static inline int _ecore_poll_events_from_fdh(Ecore_Fd_Handler *fdh)
148 if (fdh->flags & ECORE_FD_READ) events |= EPOLLIN;
149 if (fdh->flags & ECORE_FD_WRITE) events |= EPOLLOUT;
150 if (fdh->flags & ECORE_FD_ERROR) events |= EPOLLERR;
155 static inline int _ecore_main_fdh_epoll_add(Ecore_Fd_Handler *fdh)
159 struct epoll_event ev = {0};
161 ev.events = _ecore_poll_events_from_fdh(fdh);
163 INF("adding poll on %d %08x", fdh->fd, ev.events);
164 r = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fdh->fd, &ev);
169 static inline void _ecore_main_fdh_epoll_del(Ecore_Fd_Handler *fdh)
172 struct epoll_event ev = {0};
174 INF("removing poll on %d", fdh->fd);
175 /* could get an EBADF if somebody closed the FD before removing it */
176 if ((epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fdh->fd, &ev) < 0) &&
179 ERR("Failed to delete epoll fd %d! (errno=%d)", fdh->fd, errno);
184 static inline int _ecore_main_fdh_epoll_modify(Ecore_Fd_Handler *fdh)
188 struct epoll_event ev = {0};
190 ev.events = _ecore_poll_events_from_fdh(fdh);
192 INF("modifing epoll on %d to %08x", fdh->fd, ev.events);
193 r = epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fdh->fd, &ev);
199 static inline int _ecore_main_fdh_epoll_mark_active(void)
201 struct epoll_event ev[32] = {0};
204 ret = epoll_wait(epoll_fd, ev, sizeof(ev) / sizeof(struct epoll_event), 0);
207 if (errno == EINTR) return -1;
208 ERR("epoll_wait failed %d", errno);
212 for (i = 0; i < ret; i++)
214 Ecore_Fd_Handler *fdh;
216 fdh = ev[i].data.ptr;
217 if (!ECORE_MAGIC_CHECK(fdh, ECORE_MAGIC_FD_HANDLER))
219 ECORE_MAGIC_FAIL(fdh, ECORE_MAGIC_FD_HANDLER,
220 "_ecore_main_fdh_epoll_mark_active");
225 ERR("deleted fd in epoll");
228 if (ev->events & EPOLLIN)
229 fdh->read_active = 1;
230 if (ev->events & EPOLLOUT)
231 fdh->write_active = 1;
232 if (ev->events & EPOLLERR)
233 fdh->error_active = 1;
240 #ifdef USE_G_MAIN_LOOP
242 /* like we are about to enter main_loop_select in _ecore_main_select */
244 _ecore_main_gsource_prepare(GSource *source, gint *next_time)
246 double t = _ecore_timer_next_get();
249 INF("enter, next timeout in %.1f", t);
254 while (_ecore_timer_call(_ecore_loop_time));
255 _ecore_timer_cleanup();
257 /* when idling, busy loop checking the fds only */
258 if (!ecore_idling) _ecore_idle_enterer_call();
261 /* don't check fds if somebody quit */
262 running = g_main_loop_is_running(ecore_main_loop);
265 /* only set idling state in dispatch */
266 if (ecore_idling && !_ecore_idler_exist())
268 if (_ecore_timers_exists())
270 double t = _ecore_timer_next_get();
271 *next_time = (t / 1000.0);
279 _ecore_main_prepare_handlers();
283 INF("leave, timeout = %d", *next_time);
285 /* ready if we're not running (about to quit) */
290 _ecore_main_gsource_check(GSource *source)
295 ecore_fds_ready = (_ecore_main_fdh_epoll_mark_active() > 0);
296 _ecore_main_fd_handlers_cleanup();
298 _ecore_loop_time = ecore_time_get();
299 _ecore_timer_enable_new();
304 return TRUE; /* always dispatch */
307 /* like we just came out of main_loop_select in _ecore_main_select */
309 _ecore_main_gsource_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
311 gboolean events_ready, timers_ready, idlers_ready, signals_ready;
312 double next_time = _ecore_timer_next_get();
314 events_ready = _ecore_event_exist();
315 timers_ready = _ecore_timers_exists() && (0.0 <= next_time);
316 idlers_ready = _ecore_idler_exist();
317 signals_ready = (_ecore_signal_count_get() > 0);
320 INF("enter idling=%d fds=%d events=%d signals=%d timers=%d (next=%.2f) idlers=%d",
321 ecore_idling, ecore_fds_ready, events_ready, signals_ready,
322 _ecore_timers_exists(), next_time, idlers_ready);
324 if (ecore_idling && events_ready)
326 INF("calling idle exiters");
327 _ecore_idle_exiter_call();
330 else if (!ecore_idling && !events_ready)
338 INF("calling idler");
341 events_ready = _ecore_event_exist();
342 timers_ready = _ecore_timers_exists() && (0.0 <= next_time);
343 idlers_ready = _ecore_idler_exist();
345 if ((ecore_fds_ready || events_ready || timers_ready || idlers_ready || signals_ready))
347 INF("calling idle exiters");
348 _ecore_idle_exiter_call();
357 _ecore_main_fd_handlers_call();
358 _ecore_main_fd_handlers_buf_call();
359 while (_ecore_signal_count_get()) _ecore_signal_call();
361 _ecore_main_fd_handlers_cleanup();
368 return TRUE; /* what should be returned here? */
372 _ecore_main_gsource_finalize(GSource *source)
377 static GSourceFuncs ecore_gsource_funcs = {
378 .prepare = _ecore_main_gsource_prepare,
379 .check = _ecore_main_gsource_check,
380 .dispatch = _ecore_main_gsource_dispatch,
381 .finalize = _ecore_main_gsource_finalize,
387 _ecore_main_loop_init(void)
391 epoll_fd = epoll_create(1);
393 CRIT("Failed to create epoll fd!");
396 #ifdef USE_G_MAIN_LOOP
397 ecore_epoll_source = g_source_new(&ecore_gsource_funcs, sizeof (GSource));
398 if (!ecore_epoll_source)
399 CRIT("Failed to create glib source for epoll!");
400 ecore_epoll_fd.fd = epoll_fd;
401 ecore_epoll_fd.events = G_IO_IN;
402 ecore_epoll_fd.revents = 0;
403 g_source_add_poll(ecore_epoll_source, &ecore_epoll_fd);
404 ecore_epoll_id = g_source_attach(ecore_epoll_source, NULL);
405 if (ecore_epoll_id <= 0)
406 CRIT("Failed to attach glib source to default context");
412 _ecore_main_loop_shutdown(void)
414 #ifdef USE_G_MAIN_LOOP
415 g_source_destroy(ecore_epoll_source);
425 * @defgroup Ecore_Main_Loop_Group Main Loop Functions
427 * These functions control the Ecore event handling loop. This loop is
428 * designed to work on embedded systems all the way to large and
429 * powerful mutli-cpu workstations.
431 * It serialises all system signals and events into a single event
432 * queue, that can be easily processed without needing to worry about
433 * concurrency. A properly written, event-driven program using this
434 * kind of programming does not need threads. It makes the program very
435 * robust and easy to follow.
437 * Here is an example of simple program and its basic event loop flow:
438 * @image html prog_flow.png
440 * For examples of setting up and using a main loop, see
441 * @ref event_handler_example.c and @ref timer_example.c.
445 * Runs a single iteration of the main loop to process everything on the
447 * @ingroup Ecore_Main_Loop_Group
450 ecore_main_loop_iterate(void)
452 #ifndef USE_G_MAIN_LOOP
453 _ecore_main_loop_iterate_internal(1);
455 g_main_context_iteration(NULL, 1);
460 * Runs the application main loop.
462 * This function will not return until @ref ecore_main_loop_quit is called.
464 * @ingroup Ecore_Main_Loop_Group
467 ecore_main_loop_begin(void)
469 #ifndef USE_G_MAIN_LOOP
471 while (do_quit == 0) _ecore_main_loop_iterate_internal(0);
475 ecore_main_loop = g_main_loop_new(NULL, FALSE);
476 g_main_loop_run(ecore_main_loop);
481 * Quits the main loop once all the events currently on the queue have
483 * @ingroup Ecore_Main_Loop_Group
486 ecore_main_loop_quit(void)
488 #ifndef USE_G_MAIN_LOOP
492 g_main_loop_quit(ecore_main_loop);
498 * Sets the function to use when monitoring multiple file descriptors,
499 * and waiting until one of more of the file descriptors before ready
500 * for some class of I/O operation.
502 * This function will be used instead of the system call select and
503 * could possible be used to integrate the Ecore event loop with an
504 * external event loop.
506 * @warning you don't know how to use, don't even try to use it.
508 * @ingroup Ecore_Main_Loop_Group
511 ecore_main_loop_select_func_set(Ecore_Select_Function func)
513 main_loop_select = func;
517 * Gets the select function set by ecore_select_func_set(),
518 * or the native select function if none was set.
520 * @ingroup Ecore_Main_Loop_Group
523 ecore_main_loop_select_func_get(void)
525 return main_loop_select;
529 * @defgroup Ecore_FD_Handler_Group File Event Handling Functions
531 * Functions that deal with file descriptor handlers.
535 * Adds a callback for activity on the given file descriptor.
537 * @p func will be called during the execution of @ref ecore_main_loop_begin
538 * when the file descriptor is available for reading, or writing, or both.
540 * Normally the return value from the @p func is "zero means this handler is
541 * finished and can be deleted" as is usual for handler callbacks. However,
542 * if the @p buf_func is supplied, then the return value from the @p func is
543 * "non zero means the handler should be called again in a tight loop".
545 * @p buf_func is called during event loop handling to check if data that has
546 * been read from the file descriptor is in a buffer and is available to
547 * read. Some systems (notably xlib) handle their own buffering, and would
548 * otherwise not work with select(). These systems should use a @p buf_func.
549 * This is a most annoying hack, only ecore_x uses it, so refer to that for
550 * an example. NOTE - @p func should probably return "one" always if
551 * @p buf_func is used, to avoid confusion with the other return value
554 * @param fd The file descriptor to watch.
555 * @param flags To watch it for read (@c ECORE_FD_READ) and/or
556 * (@c ECORE_FD_WRITE) write ability. @c ECORE_FD_ERROR
558 * @param func The callback function.
559 * @param data The data to pass to the callback.
560 * @param buf_func The function to call to check if any data has been
561 * buffered and already read from the fd. Can be @c NULL.
562 * @param buf_data The data to pass to the @p buf_func function.
563 * @return A fd handler handle if successful. @c NULL otherwise.
564 * @ingroup Ecore_FD_Handler_Group
566 EAPI Ecore_Fd_Handler *
567 ecore_main_fd_handler_add(int fd, Ecore_Fd_Handler_Flags flags, Ecore_Fd_Cb func, const void *data,
568 Ecore_Fd_Cb buf_func, const void *buf_data)
570 Ecore_Fd_Handler *fdh;
572 if ((fd < 0) || (flags == 0) || (!func)) return NULL;
574 fdh = calloc(1, sizeof(Ecore_Fd_Handler));
575 if (!fdh) return NULL;
576 ECORE_MAGIC_SET(fdh, ECORE_MAGIC_FD_HANDLER);
579 if (0 > _ecore_main_fdh_epoll_add(fdh))
581 ERR("Failed to add epoll fd %d (errno = %d)!", fd, errno);
585 fdh->read_active = 0;
586 fdh->write_active = 0;
587 fdh->error_active = 0;
590 fdh->data = (void *)data;
591 fdh->buf_func = buf_func;
592 fdh->buf_data = (void *)buf_data;
593 fd_handlers = (Ecore_Fd_Handler *)
594 eina_inlist_append(EINA_INLIST_GET(fd_handlers),
595 EINA_INLIST_GET(fdh));
600 EAPI Ecore_Win32_Handler *
601 ecore_main_win32_handler_add(void *h, Ecore_Fd_Win32_Cb func, const void *data)
603 Ecore_Win32_Handler *wh;
605 if (!h || !func) return NULL;
607 wh = calloc(1, sizeof(Ecore_Win32_Handler));
608 if (!wh) return NULL;
609 ECORE_MAGIC_SET(wh, ECORE_MAGIC_WIN32_HANDLER);
613 wh->data = (void *)data;
614 win32_handlers = (Ecore_Win32_Handler *)
615 eina_inlist_append(EINA_INLIST_GET(win32_handlers),
616 EINA_INLIST_GET(wh));
620 EAPI Ecore_Win32_Handler *
621 ecore_main_win32_handler_add(void *h __UNUSED__, Ecore_Fd_Win32_Cb func __UNUSED__,
622 const void *data __UNUSED__)
629 * Deletes the given FD handler.
630 * @param fd_handler The given FD handler.
631 * @return The data pointer set using @ref ecore_main_fd_handler_add,
632 * for @p fd_handler on success. @c NULL otherwise.
633 * @ingroup Ecore_FD_Handler_Group
635 * Beware that if the fd is already closed, ecore may complain if it uses
636 * epoll internally, and that in some rare cases this may be able to cause
637 * crashes and instability. Remember to delete your fd handlers before the
638 * fd's they listen to are closed.
641 ecore_main_fd_handler_del(Ecore_Fd_Handler *fd_handler)
643 if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
645 ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
646 "ecore_main_fd_handler_del");
649 fd_handler->delete_me = 1;
650 fd_handlers_delete_me = 1;
651 _ecore_main_fdh_epoll_del(fd_handler);
652 return fd_handler->data;
657 ecore_main_win32_handler_del(Ecore_Win32_Handler *win32_handler)
659 if (!ECORE_MAGIC_CHECK(win32_handler, ECORE_MAGIC_WIN32_HANDLER))
661 ECORE_MAGIC_FAIL(win32_handler, ECORE_MAGIC_WIN32_HANDLER,
662 "ecore_main_win32_handler_del");
665 win32_handler->delete_me = 1;
666 win32_handlers_delete_me = 1;
667 return win32_handler->data;
671 ecore_main_win32_handler_del(Ecore_Win32_Handler *win32_handler __UNUSED__)
678 ecore_main_fd_handler_prepare_callback_set(Ecore_Fd_Handler *fd_handler, Ecore_Fd_Prep_Cb func, const void *data)
680 if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
682 ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
683 "ecore_main_fd_handler_prepare_callback_set");
686 fd_handler->prep_func = func;
687 fd_handler->prep_data = (void *) data;
691 * Retrieves the file descriptor that the given handler is handling.
692 * @param fd_handler The given FD handler.
693 * @return The file descriptor the handler is watching.
694 * @ingroup Ecore_FD_Handler_Group
697 ecore_main_fd_handler_fd_get(Ecore_Fd_Handler *fd_handler)
699 if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
701 ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
702 "ecore_main_fd_handler_fd_get");
705 return fd_handler->fd;
709 * Return if read, write or error, or a combination thereof, is active on the
710 * file descriptor of the given FD handler.
711 * @param fd_handler The given FD handler.
712 * @param flags The flags, @c ECORE_FD_READ, @c ECORE_FD_WRITE or
713 * @c ECORE_FD_ERROR to query.
714 * @return @c 1 if any of the given flags are active. @c 0 otherwise.
715 * @ingroup Ecore_FD_Handler_Group
718 ecore_main_fd_handler_active_get(Ecore_Fd_Handler *fd_handler, Ecore_Fd_Handler_Flags flags)
722 if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
724 ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
725 "ecore_main_fd_handler_active_get");
729 if ((flags & ECORE_FD_READ) && (fd_handler->read_active)) ret = 1;
730 if ((flags & ECORE_FD_WRITE) && (fd_handler->write_active)) ret = 1;
731 if ((flags & ECORE_FD_ERROR) && (fd_handler->error_active)) ret = 1;
736 * Set what active streams the given FD handler should be monitoring.
737 * @param fd_handler The given FD handler.
738 * @param flags The flags to be watching.
739 * @ingroup Ecore_FD_Handler_Group
742 ecore_main_fd_handler_active_set(Ecore_Fd_Handler *fd_handler, Ecore_Fd_Handler_Flags flags)
744 if (!ECORE_MAGIC_CHECK(fd_handler, ECORE_MAGIC_FD_HANDLER))
746 ECORE_MAGIC_FAIL(fd_handler, ECORE_MAGIC_FD_HANDLER,
747 "ecore_main_fd_handler_active_set");
750 fd_handler->flags = flags;
751 if (0 > _ecore_main_fdh_epoll_modify(fd_handler))
753 ERR("Failed to mod epoll fd %d!", fd_handler->fd);
758 _ecore_main_shutdown(void)
763 "*** ECORE WARINING: Calling ecore_shutdown() while still in the main loop.\n"
764 "*** Program may crash or behave strangely now.");
769 Ecore_Fd_Handler *fdh;
772 fd_handlers = (Ecore_Fd_Handler *) eina_inlist_remove(EINA_INLIST_GET(fd_handlers),
773 EINA_INLIST_GET(fdh));
774 ECORE_MAGIC_SET(fdh, ECORE_MAGIC_NONE);
777 fd_handlers_delete_me = 0;
778 fd_handler_current = NULL;
781 while (win32_handlers)
783 Ecore_Win32_Handler *wh;
786 win32_handlers = (Ecore_Win32_Handler *) eina_inlist_remove(EINA_INLIST_GET(win32_handlers),
787 EINA_INLIST_GET(wh));
788 ECORE_MAGIC_SET(wh, ECORE_MAGIC_NONE);
791 win32_handlers_delete_me = 0;
792 win32_handler_current = NULL;
797 _ecore_main_prepare_handlers(void)
799 Ecore_Fd_Handler *fdh;
801 /* call the prepare callback for all handlers */
802 EINA_INLIST_FOREACH(fd_handlers, fdh)
804 if (!fdh->delete_me && fdh->prep_func)
807 fdh->prep_func (fdh->prep_data, fdh);
814 _ecore_main_select(double timeout)
816 struct timeval tv, *t;
817 fd_set rfds, wfds, exfds;
822 if ((!finite(timeout)) || (timeout == 0.0)) /* finite() tests for NaN, too big, too small, and infinity. */
828 else if (timeout > 0.0)
833 timeout += (0.5 / HZ);
835 usec = (int)((timeout - (double)sec) * 1000000);
838 usec = (int)((timeout - (double)sec) * 1000000);
849 /* call the prepare callback for all handlers */
850 _ecore_main_prepare_handlers();
852 Ecore_Fd_Handler *fdh;
854 EINA_INLIST_FOREACH(fd_handlers, fdh)
858 if (fdh->flags & ECORE_FD_READ)
860 FD_SET(fdh->fd, &rfds);
861 if (fdh->fd > max_fd) max_fd = fdh->fd;
863 if (fdh->flags & ECORE_FD_WRITE)
865 FD_SET(fdh->fd, &wfds);
866 if (fdh->fd > max_fd) max_fd = fdh->fd;
868 if (fdh->flags & ECORE_FD_ERROR)
870 FD_SET(fdh->fd, &exfds);
871 if (fdh->fd > max_fd) max_fd = fdh->fd;
875 #else /* HAVE_EPOLL */
876 /* polling on the epoll fd will wake when an fd in the epoll set is active */
877 FD_SET(epoll_fd, &rfds);
879 #endif /* HAVE_EPOLL */
881 if (_ecore_signal_count_get()) return -1;
883 ret = main_loop_select(max_fd + 1, &rfds, &wfds, &exfds, t);
885 _ecore_loop_time = ecore_time_get();
889 if (errno == EINTR) return -1;
890 else if (errno == EBADF) _ecore_main_fd_handlers_bads_rem();
896 _ecore_main_fdh_epoll_mark_active();
897 #else /* HAVE_EPOLL */
898 Ecore_Fd_Handler *fdh;
900 EINA_INLIST_FOREACH(fd_handlers, fdh)
904 if (FD_ISSET(fdh->fd, &rfds))
905 fdh->read_active = 1;
906 if (FD_ISSET(fdh->fd, &wfds))
907 fdh->write_active = 1;
908 if (FD_ISSET(fdh->fd, &exfds))
909 fdh->error_active = 1;
912 #endif /* HAVE_EPOLL */
913 _ecore_main_fd_handlers_cleanup();
915 _ecore_main_win32_handlers_cleanup();
924 _ecore_main_fd_handlers_bads_rem(void)
926 Ecore_Fd_Handler *fdh;
930 ERR("Removing bad fds");
931 for (l = EINA_INLIST_GET(fd_handlers); l; )
933 fdh = (Ecore_Fd_Handler *) l;
937 if ((fcntl(fdh->fd, F_GETFD) < 0) && (errno == EBADF))
939 ERR("Found bad fd at index %d", fdh->fd);
940 if (fdh->flags & ECORE_FD_ERROR)
942 ERR("Fd set for error! calling user");
944 if (!fdh->func(fdh->data, fdh))
946 ERR("Fd function err returned 0, remove it");
948 fd_handlers_delete_me = 1;
955 ERR("Problematic fd found at %d! setting it for delete", fdh->fd);
957 fd_handlers_delete_me = 1;
965 ERR("No bad fd found. Maybe a foreign fd from glib?");
967 ERR("No bad fd found. EEEK!");
970 _ecore_main_fd_handlers_cleanup();
975 _ecore_main_fd_handlers_cleanup(void)
977 Ecore_Fd_Handler *fdh;
979 int deleted_in_use = 0;
981 if (!fd_handlers_delete_me) return;
982 for (l = EINA_INLIST_GET(fd_handlers); l; )
984 fdh = (Ecore_Fd_Handler *) l;
995 fd_handlers = (Ecore_Fd_Handler *)
996 eina_inlist_remove(EINA_INLIST_GET(fd_handlers),
997 EINA_INLIST_GET(fdh));
998 ECORE_MAGIC_SET(fdh, ECORE_MAGIC_NONE);
1002 if (!deleted_in_use) fd_handlers_delete_me = 0;
1007 _ecore_main_win32_handlers_cleanup(void)
1009 Ecore_Win32_Handler *wh;
1011 int deleted_in_use = 0;
1013 if (!win32_handlers_delete_me) return;
1014 for (l = EINA_INLIST_GET(win32_handlers); l; )
1016 wh = (Ecore_Win32_Handler *)l;
1027 win32_handlers = (Ecore_Win32_Handler *)
1028 eina_inlist_remove(EINA_INLIST_GET(win32_handlers),
1029 EINA_INLIST_GET(wh));
1030 ECORE_MAGIC_SET(wh, ECORE_MAGIC_NONE);
1034 if (!deleted_in_use) win32_handlers_delete_me = 0;
1039 _ecore_main_fd_handlers_call(void)
1041 if (!fd_handler_current)
1043 /* regular main loop, start from head */
1044 fd_handler_current = fd_handlers;
1048 /* recursive main loop, continue from where we were */
1049 fd_handler_current = (Ecore_Fd_Handler *)EINA_INLIST_GET(fd_handler_current)->next;
1052 while (fd_handler_current)
1054 Ecore_Fd_Handler *fdh = fd_handler_current;
1056 if (!fdh->delete_me)
1058 if ((fdh->read_active) ||
1059 (fdh->write_active) ||
1060 (fdh->error_active))
1063 if (!fdh->func(fdh->data, fdh))
1066 fd_handlers_delete_me = 1;
1070 fdh->read_active = 0;
1071 fdh->write_active = 0;
1072 fdh->error_active = 0;
1076 if (fd_handler_current) /* may have changed in recursive main loops */
1077 fd_handler_current = (Ecore_Fd_Handler *)EINA_INLIST_GET(fd_handler_current)->next;
1082 _ecore_main_fd_handlers_buf_call(void)
1084 Ecore_Fd_Handler *fdh;
1088 EINA_INLIST_FOREACH(fd_handlers, fdh)
1090 if (!fdh->delete_me)
1095 if (fdh->buf_func(fdh->buf_data, fdh))
1097 ret |= fdh->func(fdh->data, fdh);
1098 fdh->read_active = 1;
1107 #ifndef USE_G_MAIN_LOOP
1109 _ecore_main_loop_iterate_internal(int once_only)
1111 double next_time = -1.0;
1116 /* expire any timers */
1117 while (_ecore_timer_call(_ecore_loop_time));
1118 _ecore_timer_cleanup();
1120 /* process signals into events .... */
1121 while (_ecore_signal_count_get()) _ecore_signal_call();
1122 if (_ecore_event_exist())
1124 _ecore_idle_enterer_call();
1126 _ecore_main_select(0.0);
1127 _ecore_loop_time = ecore_time_get();
1128 _ecore_timer_enable_new();
1129 goto process_events;
1131 /* call idle enterers ... */
1132 if (!once_only) _ecore_idle_enterer_call();
1135 have_event = have_signal = 0;
1137 if (_ecore_main_select(0.0) > 0) have_event = 1;
1138 if (_ecore_signal_count_get() > 0) have_signal = 1;
1139 if (have_signal || have_event)
1141 _ecore_loop_time = ecore_time_get();
1142 _ecore_timer_enable_new();
1143 goto process_events;
1147 /* if these calls caused any buffered events to appear - deal with them */
1148 _ecore_main_fd_handlers_buf_call();
1150 /* if ther are any - jump to processing them */
1151 if (_ecore_event_exist())
1154 _ecore_main_select(0.0);
1155 _ecore_loop_time = ecore_time_get();
1156 _ecore_timer_enable_new();
1157 goto process_events;
1161 _ecore_idle_enterer_call();
1163 _ecore_loop_time = ecore_time_get();
1164 _ecore_timer_enable_new();
1168 if (_ecore_fps_debug)
1170 t2 = ecore_time_get();
1171 if ((t1 > 0.0) && (t2 > 0.0))
1172 _ecore_fps_debug_runtime_add(t2 - t1);
1175 /* any timers re-added as a result of these are allowed to go */
1176 _ecore_timer_enable_new();
1179 _ecore_loop_time = ecore_time_get();
1181 _ecore_timer_enable_new();
1184 if (!_ecore_event_exist())
1187 have_event = have_signal = 0;
1188 next_time = _ecore_timer_next_get();
1193 if (!_ecore_idler_exist())
1195 if (_ecore_main_select(-1.0) > 0) have_event = 1;
1202 if (!_ecore_idler_call()) goto start_loop;
1203 if (_ecore_event_exist()) break;
1204 if (_ecore_main_select(0.0) > 0) have_event = 1;
1205 if (_ecore_signal_count_get() > 0) have_signal = 1;
1206 if (have_event || have_signal) break;
1207 if (_ecore_timers_exists()) goto start_loop;
1216 if (!_ecore_idler_exist())
1218 if (_ecore_main_select(next_time) > 0) have_event = 1;
1225 if (!_ecore_idler_call()) goto start_loop;
1226 if (_ecore_event_exist()) break;
1227 if (_ecore_main_select(0.0) > 0) have_event = 1;
1228 if (_ecore_signal_count_get() > 0) have_signal = 1;
1229 if (have_event || have_signal) break;
1230 next_time = _ecore_timer_next_get();
1231 if (next_time <= 0) break;
1236 _ecore_loop_time = ecore_time_get();
1238 if (_ecore_fps_debug) t1 = ecore_time_get();
1239 /* we came out of our "wait state" so idle has exited */
1240 if (!once_only) _ecore_idle_exiter_call();
1241 /* call the fd handler per fd that became alive... */
1242 /* this should read or write any data to the monitored fd and then */
1243 /* post events onto the ecore event pipe if necessary */
1245 _ecore_main_fd_handlers_call();
1246 _ecore_main_fd_handlers_buf_call();
1247 /* process signals into events .... */
1248 while (_ecore_signal_count_get()) _ecore_signal_call();
1249 /* handle events ... */
1250 _ecore_event_call();
1251 _ecore_main_fd_handlers_cleanup();
1253 if (once_only) _ecore_idle_enterer_call();
1260 _ecore_main_win32_select(int nfds __UNUSED__, fd_set *readfds, fd_set *writefds,
1261 fd_set *exceptfds, struct timeval *tv)
1263 HANDLE objects[MAXIMUM_WAIT_OBJECTS];
1264 int sockets[MAXIMUM_WAIT_OBJECTS];
1265 Ecore_Fd_Handler *fdh;
1266 Ecore_Win32_Handler *wh;
1267 unsigned int objects_nbr = 0;
1268 unsigned int handles_nbr = 0;
1269 unsigned int events_nbr = 0;
1276 /* Create an event object per socket */
1277 EINA_INLIST_FOREACH(fd_handlers, fdh)
1283 if (FD_ISSET(fdh->fd, readfds))
1284 network_event |= FD_READ;
1285 if (FD_ISSET(fdh->fd, writefds))
1286 network_event |= FD_WRITE;
1287 if (FD_ISSET(fdh->fd, exceptfds))
1288 network_event |= FD_OOB;
1292 event = WSACreateEvent();
1293 WSAEventSelect(fdh->fd, event, network_event);
1294 objects[objects_nbr] = event;
1295 sockets[events_nbr] = fdh->fd;
1301 /* store the HANDLEs in the objects to wait for */
1302 EINA_INLIST_FOREACH(win32_handlers, wh)
1304 objects[objects_nbr] = wh->h;
1309 /* Empty the queue before waiting */
1310 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
1312 TranslateMessage(&msg);
1313 DispatchMessage(&msg);
1316 /* Wait for any message sent or posted to this queue */
1317 /* or for one of the passed handles be set to signaled. */
1321 timeout = (DWORD)((tv->tv_sec * 1000.0) + (tv->tv_usec / 1000.0));
1323 if (timeout == 0) return 0;
1325 result = MsgWaitForMultipleObjects(objects_nbr, (const HANDLE *)objects, EINA_FALSE,
1326 timeout, QS_ALLINPUT);
1332 /* The result tells us the type of event we have. */
1333 if (result == WAIT_FAILED)
1337 msg = evil_last_error_get();
1338 ERR(" * %s\n", msg);
1342 else if (result == WAIT_TIMEOUT)
1347 else if (result == (WAIT_OBJECT_0 + objects_nbr))
1349 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
1351 TranslateMessage(&msg);
1352 DispatchMessage(&msg);
1357 else if ((result >= 0) && (result < WAIT_OBJECT_0 + events_nbr))
1359 WSANETWORKEVENTS network_event;
1361 WSAEnumNetworkEvents(sockets[result], objects[result], &network_event);
1363 if (network_event.lNetworkEvents & FD_READ)
1364 FD_SET(sockets[result], readfds);
1365 if (network_event.lNetworkEvents & FD_WRITE)
1366 FD_SET(sockets[result], writefds);
1367 if (network_event.lNetworkEvents & FD_OOB)
1368 FD_SET(sockets[result], exceptfds);
1372 else if ((result >= (WAIT_OBJECT_0 + events_nbr)) &&
1373 (result < (WAIT_OBJECT_0 + objects_nbr)))
1375 if (!win32_handler_current)
1377 /* regular main loop, start from head */
1378 win32_handler_current = win32_handlers;
1382 /* recursive main loop, continue from where we were */
1383 win32_handler_current = (Ecore_Win32_Handler *)EINA_INLIST_GET(win32_handler_current)->next;
1386 while (win32_handler_current)
1388 wh = win32_handler_current;
1390 if (objects[result - WAIT_OBJECT_0] == wh->h)
1395 if (!wh->func(wh->data, wh))
1398 win32_handlers_delete_me = 1;
1403 if (win32_handler_current) /* may have changed in recursive main loops */
1404 win32_handler_current = (Ecore_Win32_Handler *)EINA_INLIST_GET(win32_handler_current)->next;
1410 ERR("unknown result...\n");
1414 /* Remove event objects again */
1415 for (i = 0; i < events_nbr; i++) WSACloseEvent(objects[i]);