diff options
| author | Dana Jansens <danakj@orodu.net> | 2010-06-08 17:50:23 -0400 |
|---|---|---|
| committer | Dana Jansens <danakj@orodu.net> | 2010-06-14 12:19:01 -0400 |
| commit | fd77a0a7b3f892925f203287b8b46c6ec9be94ea (patch) | |
| tree | a3e61c8c17969a41279ba21592be7543a262fcd5 /obt/xqueue.c | |
| parent | 2e94af28e4c12166cb74233526bb79f50877903c (diff) | |
Use GMainLoop instead of ObtMainLoop
Diffstat (limited to 'obt/xqueue.c')
| -rw-r--r-- | obt/xqueue.c | 66 |
1 files changed, 66 insertions, 0 deletions
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; + } + } +} |
