From fd77a0a7b3f892925f203287b8b46c6ec9be94ea Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Tue, 8 Jun 2010 17:50:23 -0400 Subject: Use GMainLoop instead of ObtMainLoop --- obt/xqueue.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'obt/xqueue.c') diff --git a/obt/xqueue.c b/obt/xqueue.c index e5d2da31..22a3de23 100644 --- a/obt/xqueue.c +++ b/obt/xqueue.c @@ -318,3 +318,69 @@ gboolean xqueue_pending_local(void) if (!qnum) read_events(FALSE); return qnum != 0; } + +typedef struct _ObtXQueueCB { + ObtXQueueFunc func; + gpointer data; +} ObtXQueueCB; + +static ObtXQueueCB *callbacks = NULL; +static guint n_callbacks = 0; + +static gboolean event_read(GIOChannel *s, GIOCondition cond, gpointer data) +{ + XEvent ev; + + while (xqueue_next_local(&ev)) { + guint i; + for (i = 0; i < n_callbacks; ++i) + callbacks[i].func(&ev, callbacks[i].data); + } + + return TRUE; /* repeat */ +} + +void xqueue_listen(void) +{ + GIOChannel *ch; + + g_assert(obt_display != NULL); + + ch = g_io_channel_unix_new(ConnectionNumber(obt_display)); + g_io_add_watch(ch, G_IO_IN, event_read, NULL); + g_io_channel_unref(ch); +} + +void xqueue_add_callback(ObtXQueueFunc f, gpointer data) +{ + guint i; + + g_return_if_fail(f != NULL); + + for (i = 0; i < n_callbacks; ++i) + if (callbacks[i].func == f && callbacks[i].data == data) + return; + + callbacks = g_renew(ObtXQueueCB, callbacks, n_callbacks + 1); + callbacks[n_callbacks].func = f; + callbacks[n_callbacks].data = data; + ++n_callbacks; +} + +void xqueue_remove_callback(ObtXQueueFunc f, gpointer data) +{ + guint i; + + g_return_if_fail(f != NULL); + + for (i = 0; i < n_callbacks; ++i) { + if (callbacks[i].func == f && callbacks[i].data == data) { + /* remove it */ + for (; i < n_callbacks - 1; ++i) + callbacks[i] = callbacks[i+1]; + callbacks = g_renew(ObtXQueueCB, callbacks, n_callbacks - 1); + --n_callbacks; + break; + } + } +} -- cgit v1.2.3