X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=obt%2Fxevent.c;h=1077165766935301c2ac0551491c92d9dd2c6b53;hb=11bb31d3bc712aa31d73065383d32f1510393fe2;hp=d37c01ae22bbc97c49f1b5fa29bd144931043bba;hpb=9f5296fb1451086350967a0e1f1808e1493a7210;p=chaz%2Fopenbox diff --git a/obt/xevent.c b/obt/xevent.c index d37c01ae..10771657 100644 --- a/obt/xevent.c +++ b/obt/xevent.c @@ -18,6 +18,7 @@ #include "obt/xevent.h" #include "obt/mainloop.h" +#include "obt/util.h" typedef struct _ObtXEventBinding ObtXEventBinding; @@ -26,9 +27,10 @@ struct _ObtXEventHandler gint ref; ObtMainLoop *loop; - /* A hash table where the key is the window, and the value is the - ObtXEventBinding */ - GHashTable *bindings[LASTEvent]; /* LASTEvent comes from X.h */ + /* An array of hash tables where the key is the window, and the value is + the ObtXEventBinding */ + GHashTable **bindings; + gint num_event_types; /* the length of the bindings array */ }; struct _ObtXEventBinding @@ -41,18 +43,15 @@ struct _ObtXEventBinding static void xevent_handler(const XEvent *e, gpointer data); static guint window_hash(Window *w) { return *w; } static gboolean window_comp(Window *w1, Window *w2) { return *w1 == *w2; } +static void binding_free(gpointer b); -ObtXEventHandler* xevent_new() +ObtXEventHandler* xevent_new(void) { ObtXEventHandler *h; - gint i; - h = g_new(ObtXEventHandler, 1); + h = g_slice_new0(ObtXEventHandler); h->ref = 1; - for (i = 0; i < LASTEvent; ++i) - h->bindings[i] = g_hash_table_new_full((GHashFunc)window_hash, - (GEqualFunc)window_comp, - NULL, g_free); + return h; } @@ -64,8 +63,15 @@ void xevent_ref(ObtXEventHandler *h) void xevent_unref(ObtXEventHandler *h) { if (h && --h->ref == 0) { + gint i; + if (h->loop) obt_main_loop_x_remove(h->loop, xevent_handler); + for (i = 0; i < h->num_event_types; ++i) + g_hash_table_destroy(h->bindings[i]); + g_free(h->bindings); + + g_slice_free(ObtXEventHandler, h); } } @@ -80,20 +86,34 @@ void xevent_set_handler(ObtXEventHandler *h, gint type, Window win, { ObtXEventBinding *b; - g_assert(type < LASTEvent); - g_assert(win); g_assert(func); - b = g_new(ObtXEventBinding, 1); + /* make sure we have a spot for the event */ + if (type + 1 < h->num_event_types) { + gint i; + h->bindings = g_renew(GHashTable*, h->bindings, type + 1); + for (i = h->num_event_types; i < type + 1; ++i) + h->bindings[i] = g_hash_table_new_full((GHashFunc)window_hash, + (GEqualFunc)window_comp, + NULL, binding_free); + h->num_event_types = type + 1; + } + + b = g_slice_new(ObtXEventBinding); b->win = win; b->func = func; b->data = data; g_hash_table_replace(h->bindings[type], &b->win, b); } +static void binding_free(gpointer b) +{ + g_slice_free(ObtXEventBinding, b); +} + void xevent_remove_handler(ObtXEventHandler *h, gint type, Window win) { - g_assert(type < LASTEvent); + g_assert(type < h->num_event_types); g_assert(win); g_hash_table_remove(h->bindings[type], &win); @@ -105,6 +125,16 @@ static void xevent_handler(const XEvent *e, gpointer data) ObtXEventBinding *b; h = data; - b = g_hash_table_lookup(h->bindings[e->xany.type], &e->xany.window); - if (b) b->func(e, b->data); + + if (e->type < h->num_event_types) { + const gint all = OBT_XEVENT_ALL_WINDOWS; + /* run the all_windows handler first */ + b = g_hash_table_lookup(h->bindings[e->xany.type], &all); + if (b) b->func(e, b->data); + /* then run the per-window handler */ + b = g_hash_table_lookup(h->bindings[e->xany.type], &e->xany.window); + if (b) b->func(e, b->data); + } + else + g_message("Unhandled X Event type %d", e->xany.type); }