2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
22 #include "event_glib.h"
27 static GMainLoop *loop;
29 static void event_glib_main_loop_run(void)
31 loop = g_main_loop_new (NULL, TRUE);
32 if (g_main_loop_is_running (loop))
34 g_main_loop_run (loop);
38 static void event_glib_main_loop_quit(void)
41 g_main_loop_quit(loop);
50 event_glib_call_watch(GIOChannel * iochan, GIOCondition condition, gpointer t)
52 struct callback *cb=t;
57 static struct event_watch *
58 event_glib_add_watch(void *fd, enum event_watch_cond cond, struct callback *cb)
60 struct event_watch *ret=g_new0(struct event_watch, 1);
62 ret->iochan = g_io_channel_unix_new(GPOINTER_TO_INT(fd));
64 case event_watch_cond_read:
67 case event_watch_cond_write:
70 case event_watch_cond_except:
71 flags=G_IO_ERR|G_IO_HUP;
74 ret->source = g_io_add_watch(ret->iochan, flags, event_glib_call_watch, (gpointer)cb);
79 event_glib_remove_watch(struct event_watch *ev)
83 g_source_remove(ev->source);
84 g_io_channel_unref(ev->iochan);
88 struct event_timeout {
94 event_glib_call_timeout_single(struct event_timeout *ev)
96 callback_call_0(ev->cb);
102 event_glib_call_timeout_multi(struct event_timeout *ev)
104 callback_call_0(ev->cb);
109 static struct event_timeout *
110 event_glib_add_timeout(int timeout, int multi, struct callback *cb)
112 struct event_timeout *ret=g_new0(struct event_timeout, 1);
114 ret->source = g_timeout_add(timeout, multi ? (GSourceFunc)event_glib_call_timeout_multi : (GSourceFunc)event_glib_call_timeout_single, (gpointer)ret);
120 event_glib_remove_timeout(struct event_timeout *ev)
124 g_source_remove(ev->source);
134 event_glib_call_idle(struct event_idle *ev)
136 callback_call_0(ev->cb);
140 static struct event_idle *
141 event_glib_add_idle(int priority, struct callback *cb)
143 struct event_idle *ret=g_new0(struct event_idle, 1);
145 ret->source = g_idle_add_full(priority+100, (GSourceFunc)event_glib_call_idle, (gpointer)ret, NULL);
150 event_glib_remove_idle(struct event_idle *ev)
154 g_source_remove(ev->source);
159 event_glib_call_callback(struct callback_list *cb)
162 Idea for implementation:
163 Create a pipe then use add_watch
164 add callback to a queue
165 from here write to the pipe to wakeup the pool
166 then from the gui thread process the callback queue
170 static struct event_methods event_glib_methods = {
171 event_glib_main_loop_run,
172 event_glib_main_loop_quit,
173 event_glib_add_watch,
174 event_glib_remove_watch,
175 event_glib_add_timeout,
176 event_glib_remove_timeout,
178 event_glib_remove_idle,
179 event_glib_call_callback,
185 static struct event_priv*
186 event_glib_new(struct event_methods *meth)
188 *meth=event_glib_methods;
189 return (struct event_priv *)event_glib_new;
193 event_glib_init(void)
195 plugin_register_event_type("glib", event_glib_new);