summaryrefslogtreecommitdiff
path: root/navit/event_glib.c
blob: 69e6358d2cc491cd4e5fb8cca543861f1b2f27fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
 * Navit, a modular navigation system.
 * Copyright (C) 2005-2008 Navit Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include <glib.h>
#include "event.h"
#include "event_glib.h"
#include "debug.h"
#include "callback.h"

static GMainLoop *loop;

static void event_glib_main_loop_run(void)
{
	loop = g_main_loop_new (NULL, TRUE);
        if (g_main_loop_is_running (loop))
        {
		g_main_loop_run (loop);
	}
}

static void event_glib_main_loop_quit(void)
{
	if (loop)
		g_main_loop_quit(loop);
}

struct event_watch {
	GIOChannel *iochan;
	guint source;
};

static gboolean
event_glib_call_watch(GIOChannel * iochan, GIOCondition condition, gpointer t)
{
	struct callback *cb=t;
	callback_call_0(cb);
	return TRUE;
}

static struct event_watch *
event_glib_add_watch(int fd, int w, struct callback *cb)
{
	struct event_watch *ret=g_new0(struct event_watch, 1);
	ret->iochan = g_io_channel_unix_new(fd);
	ret->source = g_io_add_watch(ret->iochan, G_IO_IN | G_IO_ERR | G_IO_HUP, event_glib_call_watch, (gpointer)cb);
	return ret;
}

static void
event_glib_remove_watch(struct event_watch *ev)
{
	GError *error = NULL;
	g_source_remove(ev->source);
	g_io_channel_shutdown(ev->iochan, 0, &error);
	g_free(ev);
}

struct event_timeout {
	guint source;
};

static gboolean
event_glib_call_timeout_single(gpointer t)
{
	struct callback *cb=t;
	callback_call_0(cb);
	return FALSE;
}

static gboolean
event_glib_call_timeout_multi(gpointer t)
{
	struct callback *cb=t;
	callback_call_0(cb);
	return TRUE;
}


static struct event_timeout *
event_glib_add_timeout(int timeout, int multi, struct callback *cb)
{
	struct event_timeout *ret=g_new0(struct event_timeout, 1);
	ret->source = g_timeout_add(timeout, multi ? (GSourceFunc)event_glib_call_timeout_multi : (GSourceFunc)event_glib_call_timeout_single, (gpointer)cb);

	return ret;
}

static void
event_glib_remove_timeout(struct event_timeout *ev)
{
	g_source_remove(ev->source);
	g_free(ev);
}

static struct event_idle *
event_glib_add_idle(struct callback *cb)
{
	return NULL;
}

static void
event_glib_remove_idle(struct event_idle *ev)
{
}

static struct event_methods event_glib_methods = {
	event_glib_main_loop_run,
	event_glib_main_loop_quit,
	event_glib_add_watch,
	event_glib_remove_watch,
	event_glib_add_timeout,
	event_glib_remove_timeout,
	event_glib_add_idle,
	event_glib_remove_idle,
};

static void
event_glib_new(struct event_methods *meth)
{
	*meth=event_glib_methods;
}

void
event_glib_init(void)
{
	plugin_register_event_type("glib", event_glib_new);
}