8 #include "ecore_private.h"
13 static Eina_Bool _ecore_glib_active = EINA_FALSE;
14 static Ecore_Select_Function _ecore_glib_select_original;
15 static GCond *_ecore_glib_cond = NULL;
16 static GPollFD *_ecore_glib_fds = NULL;
17 static size_t _ecore_glib_fds_size = 0;
18 static const size_t ECORE_GLIB_FDS_INITIAL = 128;
19 static const size_t ECORE_GLIB_FDS_STEP = 8;
20 static const size_t ECORE_GLIB_FDS_MAX_FREE = 256;
23 _ecore_glib_fds_resize(size_t size)
25 void *tmp = realloc(_ecore_glib_fds, sizeof(GPollFD) * size);
29 ERR("Could not realloc from %zu to %zu buckets.",
30 _ecore_glib_fds_size, size);
34 _ecore_glib_fds = tmp;
35 _ecore_glib_fds_size = size;
40 _ecore_glib_context_query(GMainContext *ctx, int priority, int *p_timer)
44 if (_ecore_glib_fds_size == 0)
46 if (!_ecore_glib_fds_resize(ECORE_GLIB_FDS_INITIAL)) return -1;
53 reqfds = g_main_context_query
54 (ctx, priority, p_timer, _ecore_glib_fds, _ecore_glib_fds_size);
55 if (reqfds <= (int)_ecore_glib_fds_size) break;
57 size = (1 + reqfds / ECORE_GLIB_FDS_STEP) * ECORE_GLIB_FDS_STEP;
58 if (!_ecore_glib_fds_resize(size)) return -1;
61 if (reqfds + ECORE_GLIB_FDS_MAX_FREE < _ecore_glib_fds_size)
65 size = (1 + reqfds / ECORE_GLIB_FDS_MAX_FREE) * ECORE_GLIB_FDS_MAX_FREE;
66 _ecore_glib_fds_resize(size);
73 _ecore_glib_context_poll_from(const GPollFD *pfds, int count, fd_set *rfds, fd_set *wfds, fd_set *efds)
75 const GPollFD *itr = pfds, *itr_end = pfds + count;
78 for (; itr < itr_end; itr++)
80 if (glib_fds < itr->fd)
83 if (itr->events & G_IO_IN)
84 FD_SET(itr->fd, rfds);
85 if (itr->events & G_IO_OUT)
86 FD_SET(itr->fd, wfds);
87 if (itr->events & (G_IO_HUP | G_IO_ERR))
88 FD_SET(itr->fd, efds);
95 _ecore_glib_context_poll_to(GPollFD *pfds, int count, const fd_set *rfds, const fd_set *wfds, const fd_set *efds, int ready)
97 GPollFD *itr = pfds, *itr_end = pfds + count;
99 for (; itr < itr_end && ready > 0; itr++)
102 if (FD_ISSET(itr->fd, rfds))
104 itr->revents |= G_IO_IN;
107 if (FD_ISSET(itr->fd, wfds))
109 itr->revents |= G_IO_OUT;
112 if (FD_ISSET(itr->fd, efds))
114 itr->revents |= G_IO_ERR;
122 _ecore_glib_select__locked(GMainContext *ctx, int ecore_fds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *ecore_timeout)
124 int priority, maxfds, glib_fds, reqfds, reqtimeout, ret;
125 struct timeval *timeout, glib_timeout;
127 g_main_context_prepare(ctx, &priority);
128 reqfds = _ecore_glib_context_query(ctx, priority, &reqtimeout);
129 if (reqfds < 0) goto error;
131 glib_fds = _ecore_glib_context_poll_from
132 (_ecore_glib_fds, reqfds, rfds, wfds, efds);
134 if (reqtimeout == -1)
135 timeout = ecore_timeout;
138 glib_timeout.tv_sec = reqtimeout / 1000;
139 glib_timeout.tv_usec = (reqtimeout % 1000) * 1000;
141 if (!ecore_timeout || timercmp(ecore_timeout, &glib_timeout, >))
142 timeout = &glib_timeout;
144 timeout = ecore_timeout;
147 maxfds = (ecore_fds >= glib_fds) ? ecore_fds : glib_fds;
148 ret = _ecore_glib_select_original(maxfds, rfds, wfds, efds, timeout);
150 ret = _ecore_glib_context_poll_to
151 (_ecore_glib_fds, reqfds, rfds, wfds, efds, ret);
153 if (g_main_context_check(ctx, priority, _ecore_glib_fds, reqfds))
154 g_main_context_dispatch(ctx);
159 return _ecore_glib_select_original
160 (ecore_fds, rfds, wfds, efds, ecore_timeout);
164 _ecore_glib_select(int ecore_fds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *ecore_timeout)
166 GStaticMutex lock = G_STATIC_MUTEX_INIT;
167 GMutex *mutex = g_static_mutex_get_mutex(&lock);
168 GMainContext *ctx = g_main_context_default();
171 if (g_main_context_acquire(ctx))
175 if (!_ecore_glib_cond)
176 _ecore_glib_cond = g_cond_new();
178 while (!g_main_context_wait(ctx, _ecore_glib_cond, mutex))
182 ret = _ecore_glib_select__locked
183 (ctx, ecore_fds, rfds, wfds, efds, ecore_timeout);
185 g_mutex_unlock(mutex);
186 g_main_context_release(ctx);
193 _ecore_glib_init(void)
198 _ecore_glib_shutdown(void)
201 if (!_ecore_glib_active) return;
202 _ecore_glib_active = EINA_FALSE;
204 if (ecore_main_loop_select_func_get() == _ecore_glib_select)
205 ecore_main_loop_select_func_set(_ecore_glib_select_original);
209 free(_ecore_glib_fds);
210 _ecore_glib_fds = NULL;
212 _ecore_glib_fds_size = 0;
214 if (_ecore_glib_cond)
216 g_cond_free(_ecore_glib_cond);
217 _ecore_glib_cond = NULL;
223 * Request ecore to integrate GLib's main loop.
225 * This will add a small overhead during every main loop interaction
226 * by checking glib's default main context (used by its main loop). If
227 * it have events to be checked (timers, file descriptors or idlers),
228 * then these will be polled alongside with Ecore's own events, then
229 * dispatched before Ecore's. This is done by calling
230 * ecore_main_loop_select_func_set().
232 * This will cooperate with previously set
233 * ecore_main_loop_select_func_set() by calling the old
234 * function. Similarly, if you want to override
235 * ecore_main_loop_select_func_set() after main loop is integrated,
236 * call the new select function set by this call (get it by calling
237 * ecore_main_loop_select_func_get() right after
238 * ecore_main_loop_glib_integrate()).
240 * This is useful to use GMainLoop libraries, like GTK, GUPnP,
241 * LibSoup, GConf and more. Adobe Flash plugin and other plugins
242 * systems depend on this as well.
244 * Once initialized/integrated, it will be valid until Ecore is
245 * completely shut down.
247 * @note this is only available if Ecore was compiled with GLib support.
249 * @return @c EINA_TRUE on success of @c EINA_FALSE if it failed,
250 * likely no GLib support in Ecore.
253 ecore_main_loop_glib_integrate(void)
258 if (_ecore_glib_active) return EINA_TRUE;
259 func = ecore_main_loop_select_func_get();
260 if (func == _ecore_glib_select) return EINA_TRUE;
261 _ecore_glib_select_original = func;
262 ecore_main_loop_select_func_set(_ecore_glib_select);
263 _ecore_glib_active = EINA_TRUE;
266 fputs("ERROR: no glib support in ecore.\n", stderr);
271 Eina_Bool _ecore_glib_always_integrate = 1;
274 * Disable always integrating glib
276 * If ecore is compiled with --enable-glib-integration-always (to always
277 * call ecore_main_loop_glib_integrate() when ecore_init() is called), then
278 * calling this before calling ecore_init() will disable the integration.
279 * This is for apps that explicitly do not want this to happen for whatever
280 * reasons they may have.
283 ecore_main_loop_glib_always_integrate_disable(void)
285 _ecore_glib_always_integrate = 0;