2 Unix SMB/CIFS implementation.
4 main select loop and event handling
6 plugin for using a gtk application's event loop
8 Copyright (C) Stefan Metzmacher 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "lib/events/events.h"
27 #include "lib/events/events_internal.h"
29 #include "gtk/common/select.h"
31 /* as gtk_main() doesn't take a parameter nor return one,
32 we need to have a global event context structure for our
35 static struct event_context *gtk_event_context_global;
37 static int gtk_event_context_destructor(struct event_context *ev)
39 gtk_event_context_global = NULL;
44 create a gtk_event_context structure.
46 static int gtk_event_context_init(struct event_context *ev)
48 talloc_set_destructor(ev, gtk_event_context_destructor);
59 static gboolean gtk_event_fd_handler(GIOChannel *source, GIOCondition condition, gpointer data)
61 struct fd_event *fde = talloc_get_type(data, struct fd_event);
62 struct gtk_fd_event *gtk_fd = talloc_get_type(fde->additional_data,
66 if (condition & (G_IO_IN|G_IO_PRI|G_IO_ERR|G_IO_HUP))
67 flags |= EVENT_FD_READ;
68 if (condition & G_IO_OUT)
69 flags |= EVENT_FD_WRITE;
71 gtk_fd->running = True;
72 fde->handler(fde->event_ctx, fde, flags, fde->private_data);
73 gtk_fd->running = False;
75 if (gtk_fd->free_after_run) {
86 static int gtk_event_fd_destructor(struct fd_event *fde)
88 struct gtk_fd_event *gtk_fd = talloc_get_type(fde->additional_data,
91 if (gtk_fd->running) {
92 /* the event is running reject the talloc_free()
93 as it's done by the gtk_event_timed_handler()
95 gtk_fd->free_after_run = True;
100 /* only if any flag is set we have really registered an event */
101 g_source_remove(gtk_fd->fd_id);
103 g_io_channel_unref(gtk_fd->channel);
110 return NULL on failure (memory allocation error)
112 static struct fd_event *gtk_event_add_fd(struct event_context *ev, TALLOC_CTX *mem_ctx,
113 int fd, uint16_t flags,
114 event_fd_handler_t handler,
117 struct fd_event *fde;
118 struct gtk_fd_event *gtk_fd;
121 GIOCondition condition = 0;
123 fde = talloc(mem_ctx?mem_ctx:ev, struct fd_event);
124 if (!fde) return NULL;
126 gtk_fd = talloc(fde, struct gtk_fd_event);
127 if (gtk_fd == NULL) {
135 fde->handler = handler;
136 fde->private_data = private_data;
137 fde->additional_flags = 0;
138 fde->additional_data = gtk_fd;
140 channel = g_io_channel_unix_new(fde->fd);
141 if (channel == NULL) {
146 if (fde->flags & EVENT_FD_READ)
147 condition |= (G_IO_IN | G_IO_ERR | G_IO_HUP);
148 if (fde->flags & EVENT_FD_WRITE)
149 condition |= G_IO_OUT;
152 /* only register the event when at least one flag is set
153 as condition == 0 means wait for any event and is not the same
156 fd_id = g_io_add_watch(channel, condition, gtk_event_fd_handler, fde);
159 gtk_fd->running = False;
160 gtk_fd->free_after_run = False;
161 gtk_fd->channel = channel;
162 gtk_fd->fd_id = fd_id;
164 talloc_set_destructor(fde, gtk_event_fd_destructor);
170 return the fd event flags
172 static uint16_t gtk_event_get_fd_flags(struct fd_event *fde)
178 set the fd event flags
180 static void gtk_event_set_fd_flags(struct fd_event *fde, uint16_t flags)
182 struct gtk_fd_event *gtk_fd = talloc_get_type(fde->additional_data,
183 struct gtk_fd_event);
184 GIOCondition condition = 0;
186 if (fde->flags == flags) return;
188 if (flags & EVENT_FD_READ)
189 condition |= (G_IO_IN | G_IO_ERR | G_IO_HUP);
190 if (flags & EVENT_FD_WRITE)
191 condition |= G_IO_OUT;
193 /* only register the event when at least one flag is set
194 as condition == 0 means wait for any event and is not the same
198 g_source_remove(gtk_fd->fd_id);
201 gtk_fd->fd_id = g_io_add_watch(gtk_fd->channel, condition, gtk_event_fd_handler, fde);
207 struct gtk_timed_event {
212 destroy a timed event
214 static int gtk_event_timed_destructor(struct timed_event *te)
216 struct gtk_timed_event *gtk_te = talloc_get_type(te->additional_data,
217 struct gtk_timed_event);
219 g_source_remove(gtk_te->te_id);
224 static int gtk_event_timed_deny_destructor(struct timed_event *te)
229 static gboolean gtk_event_timed_handler(gpointer data)
231 struct timed_event *te = talloc_get_type(data, struct timed_event);
232 struct timeval t = timeval_current();
234 /* deny the handler to free the event */
235 talloc_set_destructor(te, gtk_event_timed_deny_destructor);
236 te->handler(te->event_ctx, te, t, te->private_data);
238 talloc_set_destructor(te, gtk_event_timed_destructor);
241 /* return FALSE mean this event should be removed */
247 return NULL on failure (memory allocation error)
249 static struct timed_event *gtk_event_add_timed(struct event_context *ev, TALLOC_CTX *mem_ctx,
250 struct timeval next_event,
251 event_timed_handler_t handler,
254 struct timed_event *te;
255 struct gtk_timed_event *gtk_te;
256 struct timeval cur_tv, diff_tv;
259 te = talloc(mem_ctx?mem_ctx:ev, struct timed_event);
260 if (te == NULL) return NULL;
262 gtk_te = talloc(te, struct gtk_timed_event);
263 if (gtk_te == NULL) {
269 te->next_event = next_event;
270 te->handler = handler;
271 te->private_data = private_data;
272 te->additional_data = gtk_te;
274 cur_tv = timeval_current();
275 diff_tv = timeval_until(&cur_tv, &next_event);
276 timeout = ((diff_tv.tv_usec+999)/1000)+(diff_tv.tv_sec*1000);
278 gtk_te->te_id = g_timeout_add(timeout, gtk_event_timed_handler, te);
280 talloc_set_destructor(te, gtk_event_timed_destructor);
286 do a single event loop
288 static int gtk_event_loop_once(struct event_context *ev)
291 * gtk_main_iteration ()
293 * gboolean gtk_main_iteration (void);
295 * Runs a single iteration of the mainloop. If no events
296 * are waiting to be processed GTK+ will block until the
297 * next event is noticed. If you don't want to block look
298 * at gtk_main_iteration_do() or check if any events are
299 * pending with gtk_events_pending() first.
301 * Returns : TRUE if gtk_main_quit() has been called for the innermost mainloop.
305 ret = gtk_main_iteration();
306 if (ret == gtk_true()) {
316 static int gtk_event_loop_wait(struct event_context *ev)
321 * void gtk_main (void);
323 * Runs the main loop until gtk_main_quit() is called.
324 * You can nest calls to gtk_main(). In that case
325 * gtk_main_quit() will make the innermost invocation
326 * of the main loop return.
332 static const struct event_ops gtk_event_ops = {
333 .context_init = gtk_event_context_init,
334 .add_fd = gtk_event_add_fd,
335 .get_fd_flags = gtk_event_get_fd_flags,
336 .set_fd_flags = gtk_event_set_fd_flags,
337 .add_timed = gtk_event_add_timed,
338 .loop_once = gtk_event_loop_once,
339 .loop_wait = gtk_event_loop_wait,
342 int gtk_event_loop(void)
346 event_register_backend("gtk", >k_event_ops);
348 gtk_event_context_global = event_context_init_byname(NULL, "gtk");
349 if (!gtk_event_context_global) return -1;
351 ret = event_loop_wait(gtk_event_context_global);
353 talloc_free(gtk_event_context_global);
358 struct event_context *gtk_event_context(void)
360 return gtk_event_context_global;