X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fopenbox;a=blobdiff_plain;f=obt%2Fxqueue.c;h=22a3de23578be77b060fd2b7019fec4b8fc6c9e9;hp=e5d2da317de43419a677e592d110f7b598dcf5b6;hb=fd77a0a7b3f892925f203287b8b46c6ec9be94ea;hpb=2e94af28e4c12166cb74233526bb79f50877903c 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; + } + } +}