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"
26 static GMainLoop *loop;
28 static void event_glib_main_loop_run(void)
30 loop = g_main_loop_new (NULL, TRUE);
31 if (g_main_loop_is_running (loop))
33 g_main_loop_run (loop);
37 static void event_glib_main_loop_quit(void)
40 g_main_loop_quit(loop);
49 event_glib_call_watch(GIOChannel * iochan, GIOCondition condition, gpointer t)
51 struct callback *cb=t;
56 static struct event_watch *
57 event_glib_add_watch(int fd, int w, struct callback *cb)
59 struct event_watch *ret=g_new0(struct event_watch, 1);
60 ret->iochan = g_io_channel_unix_new(fd);
61 ret->source = g_io_add_watch(ret->iochan, G_IO_IN | G_IO_ERR | G_IO_HUP, event_glib_call_watch, (gpointer)cb);
66 event_glib_remove_watch(struct event_watch *ev)
69 g_source_remove(ev->source);
70 g_io_channel_shutdown(ev->iochan, 0, &error);
74 struct event_timeout {
79 event_glib_call_timeout_single(gpointer t)
81 struct callback *cb=t;
87 event_glib_call_timeout_multi(gpointer t)
89 struct callback *cb=t;
95 static struct event_timeout *
96 event_glib_add_timeout(int timeout, int multi, struct callback *cb)
98 struct event_timeout *ret=g_new0(struct event_timeout, 1);
99 ret->source = g_timeout_add(timeout, multi ? (GSourceFunc)event_glib_call_timeout_multi : (GSourceFunc)event_glib_call_timeout_single, (gpointer)cb);
105 event_glib_remove_timeout(struct event_timeout *ev)
107 g_source_remove(ev->source);
111 static struct event_idle *
112 event_glib_add_idle(struct callback *cb)
118 event_glib_remove_idle(struct event_idle *ev)
122 static struct event_methods event_glib_methods = {
123 event_glib_main_loop_run,
124 event_glib_main_loop_quit,
125 event_glib_add_watch,
126 event_glib_remove_watch,
127 event_glib_add_timeout,
128 event_glib_remove_timeout,
130 event_glib_remove_idle,
134 event_glib_new(struct event_methods *meth)
136 *meth=event_glib_methods;
140 event_glib_init(void)
142 plugin_register_event_type("glib", event_glib_new);