2 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
12 #include "ecore_private.h"
17 static Eina_Bool _ecore_glib_active = EINA_FALSE;
18 static int (*_ecore_glib_select_original)(int, fd_set*, fd_set*, fd_set*, struct timeval *);
19 static GCond *_ecore_glib_cond = NULL;
20 static GPollFD *_ecore_glib_fds = NULL;
21 static size_t _ecore_glib_fds_size = 0;
22 static const size_t ECORE_GLIB_FDS_INITIAL = 128;
23 static const size_t ECORE_GLIB_FDS_STEP = 8;
24 static const size_t ECORE_GLIB_FDS_MAX_FREE = 256;
27 _ecore_glib_fds_resize(size_t size)
29 void *tmp = realloc(_ecore_glib_fds, sizeof(GPollFD) * size);
33 ERR("Could not realloc from %zu to %zu buckets.",
34 _ecore_glib_fds_size, size);
38 _ecore_glib_fds = tmp;
39 _ecore_glib_fds_size = size;
44 _ecore_glib_context_query(GMainContext *ctx, int priority, int *p_timer)
48 if (_ecore_glib_fds_size == 0)
50 if (!_ecore_glib_fds_resize(ECORE_GLIB_FDS_INITIAL)) return -1;
57 reqfds = g_main_context_query
58 (ctx, priority, p_timer, _ecore_glib_fds, _ecore_glib_fds_size);
59 if (reqfds <= (int)_ecore_glib_fds_size) break;
61 size = (1 + reqfds / ECORE_GLIB_FDS_STEP) * ECORE_GLIB_FDS_STEP;
62 if (!_ecore_glib_fds_resize(size)) return -1;
65 if (reqfds + ECORE_GLIB_FDS_MAX_FREE < _ecore_glib_fds_size)
69 size = (1 + reqfds / ECORE_GLIB_FDS_MAX_FREE) * ECORE_GLIB_FDS_MAX_FREE;
70 _ecore_glib_fds_resize(size);
77 _ecore_glib_context_poll_from(const GPollFD *pfds, int count, fd_set *rfds, fd_set *wfds, fd_set *efds)
79 const GPollFD *itr = pfds, *itr_end = pfds + count;
82 for (; itr < itr_end; itr++)
84 if (glib_fds < itr->fd)
87 if (itr->events & G_IO_IN)
88 FD_SET(itr->fd, rfds);
89 if (itr->events & G_IO_OUT)
90 FD_SET(itr->fd, wfds);
91 if (itr->events & (G_IO_HUP | G_IO_ERR))
92 FD_SET(itr->fd, efds);
99 _ecore_glib_context_poll_to(GPollFD *pfds, int count, const fd_set *rfds, const fd_set *wfds, const fd_set *efds, int ready)
101 GPollFD *itr = pfds, *itr_end = pfds + count;
103 for (; itr < itr_end && ready > 0; itr++)
106 if (FD_ISSET(itr->fd, rfds))
108 itr->revents |= G_IO_IN;
111 if (FD_ISSET(itr->fd, wfds))
113 itr->revents |= G_IO_OUT;
116 if (FD_ISSET(itr->fd, efds))
118 itr->revents |= G_IO_ERR;
126 _ecore_glib_select__locked(GMainContext *ctx, int ecore_fds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *ecore_timeout)
128 int priority, maxfds, glib_fds, reqfds, reqtimeout, ret;
129 struct timeval *timeout, glib_timeout;
131 g_main_context_prepare(ctx, &priority);
132 reqfds = _ecore_glib_context_query(ctx, priority, &reqtimeout);
133 if (reqfds < 0) goto error;
135 glib_fds = _ecore_glib_context_poll_from
136 (_ecore_glib_fds, reqfds, rfds, wfds, efds);
138 if (reqtimeout == -1)
139 timeout = ecore_timeout;
142 glib_timeout.tv_sec = reqtimeout / 1000;
143 glib_timeout.tv_usec = (reqtimeout % 1000) * 1000;
145 if (!ecore_timeout || timercmp(ecore_timeout, &glib_timeout, >))
146 timeout = &glib_timeout;
148 timeout = ecore_timeout;
151 maxfds = (ecore_fds >= glib_fds) ? ecore_fds : glib_fds;
152 ret = _ecore_glib_select_original(maxfds, rfds, wfds, efds, timeout);
154 ret = _ecore_glib_context_poll_to
155 (_ecore_glib_fds, reqfds, rfds, wfds, efds, ret);
157 if (g_main_context_check(ctx, priority, _ecore_glib_fds, reqfds))
158 g_main_context_dispatch(ctx);
163 return _ecore_glib_select_original
164 (ecore_fds, rfds, wfds, efds, ecore_timeout);
168 _ecore_glib_select(int ecore_fds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *ecore_timeout)
170 GStaticMutex lock = G_STATIC_MUTEX_INIT;
171 GMutex *mutex = g_static_mutex_get_mutex(&lock);
172 GMainContext *ctx = g_main_context_default();
175 if (g_main_context_acquire(ctx))
179 if (!_ecore_glib_cond)
180 _ecore_glib_cond = g_cond_new();
182 while (!g_main_context_wait(ctx, _ecore_glib_cond, mutex))
186 ret = _ecore_glib_select__locked
187 (ctx, ecore_fds, rfds, wfds, efds, ecore_timeout);
189 g_mutex_unlock(mutex);
190 g_main_context_release(ctx);
197 _ecore_glib_init(void)
202 _ecore_glib_shutdown(void)
205 if (!_ecore_glib_active) return;
206 _ecore_glib_active = EINA_FALSE;
208 if (ecore_main_loop_select_func_get() == _ecore_glib_select)
209 ecore_main_loop_select_func_set(_ecore_glib_select_original);
213 free(_ecore_glib_fds);
214 _ecore_glib_fds = NULL;
216 _ecore_glib_fds_size = 0;
218 if (_ecore_glib_cond)
220 g_cond_free(_ecore_glib_cond);
221 _ecore_glib_cond = NULL;
227 * Request ecore to integrate GLib's main loop.
229 * This will add a small overhead during every main loop interaction
230 * by checking glib's default main context (used by its main loop). If
231 * it have events to be checked (timers, file descriptors or idlers),
232 * then these will be polled alongside with Ecore's own events, then
233 * dispatched before Ecore's. This is done by calling
234 * ecore_main_loop_select_func_set().
236 * This will cooperate with previously set
237 * ecore_main_loop_select_func_set() by calling the old
238 * function. Similarly, if you want to override
239 * ecore_main_loop_select_func_set() after main loop is integrated,
240 * call the new select function set by this call (get it by calling
241 * ecore_main_loop_select_func_get() right after
242 * ecore_main_loop_glib_integrate()).
244 * This is useful to use GMainLoop libraries, like GTK, GUPnP,
245 * LibSoup, GConf and more. Adobe Flash plugin and other plugins
246 * systems depend on this as well.
248 * Once initialized/integrated, it will be valid until Ecore is
249 * completely shut down.
251 * @note this is only available if Ecore was compiled with GLib support.
253 * @return @c EINA_TRUE on success of @c EINA_FALSE if it failed,
254 * likely no GLib support in Ecore.
257 ecore_main_loop_glib_integrate(void)
262 if (_ecore_glib_active) return EINA_TRUE;
263 func = ecore_main_loop_select_func_get();
264 if (func == _ecore_glib_select) return EINA_TRUE;
265 _ecore_glib_select_original = func;
266 ecore_main_loop_select_func_set(_ecore_glib_select);
267 _ecore_glib_active = EINA_TRUE;
270 fputs("ERROR: no glib support in ecore.\n", stderr);
275 Eina_Bool _ecore_glib_always_integrate = 1;
278 * Disable always integrating glib
280 * If ecore is compiled with --enable-glib-integration-always (to always
281 * call ecore_main_loop_glib_integrate() when ecore_init() is called), then
282 * calling this before calling ecore_init() will disable the integration.
283 * This is for apps that explicitly do not want this to happen for whatever
284 * reasons they may have.
287 ecore_main_loop_glib_always_integrate_disable(void)
289 _ecore_glib_always_integrate = 0;