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);
42 g_main_loop_unref(loop);
53 event_glib_call_watch(GIOChannel * iochan, GIOCondition condition, gpointer t)
55 struct callback *cb=t;
60 static struct event_watch *
61 event_glib_add_watch(void *fd, enum event_watch_cond cond, struct callback *cb)
63 struct event_watch *ret=g_new0(struct event_watch, 1);
65 ret->iochan = g_io_channel_unix_new(GPOINTER_TO_INT(fd));
67 case event_watch_cond_read:
70 case event_watch_cond_write:
73 case event_watch_cond_except:
74 flags=G_IO_ERR|G_IO_HUP;
77 ret->source = g_io_add_watch(ret->iochan, flags, event_glib_call_watch, (gpointer)cb);
82 event_glib_remove_watch(struct event_watch *ev)
86 g_source_remove(ev->source);
87 g_io_channel_unref(ev->iochan);
91 struct event_timeout {
97 event_glib_call_timeout_single(struct event_timeout *ev)
99 callback_call_0(ev->cb);
105 event_glib_call_timeout_multi(struct event_timeout *ev)
107 callback_call_0(ev->cb);
112 static struct event_timeout *
113 event_glib_add_timeout(int timeout, int multi, struct callback *cb)
115 struct event_timeout *ret=g_new0(struct event_timeout, 1);
117 ret->source = g_timeout_add(timeout, multi ? (GSourceFunc)event_glib_call_timeout_multi : (GSourceFunc)event_glib_call_timeout_single, (gpointer)ret);
123 event_glib_remove_timeout(struct event_timeout *ev)
127 g_source_remove(ev->source);
137 event_glib_call_idle(struct event_idle *ev)
139 callback_call_0(ev->cb);
143 static struct event_idle *
144 event_glib_add_idle(int priority, struct callback *cb)
146 struct event_idle *ret=g_new0(struct event_idle, 1);
148 ret->source = g_idle_add_full(priority+100, (GSourceFunc)event_glib_call_idle, (gpointer)ret, NULL);
153 event_glib_remove_idle(struct event_idle *ev)
157 g_source_remove(ev->source);
162 event_glib_call_callback(struct callback_list *cb)
165 Idea for implementation:
166 Create a pipe then use add_watch
167 add callback to a queue
168 from here write to the pipe to wakeup the pool
169 then from the gui thread process the callback queue
173 static struct event_methods event_glib_methods = {
174 event_glib_main_loop_run,
175 event_glib_main_loop_quit,
176 event_glib_add_watch,
177 event_glib_remove_watch,
178 event_glib_add_timeout,
179 event_glib_remove_timeout,
181 event_glib_remove_idle,
182 event_glib_call_callback,
188 static struct event_priv*
189 event_glib_new(struct event_methods *meth)
191 *meth=event_glib_methods;
192 return (struct event_priv *)event_glib_new;
196 event_glib_init(void)
198 plugin_register_event_type("glib", event_glib_new);