Clean up static variables in event_handle

This function needs the event numbers for events from some extensions. These are
assigned dynamically by the server. Right now, this is done by having a bunch of
static variables that are initialized when needed.

Refactor this to have a function event_init() instead that sets variable in
globalconf (where all of our state should be saved). Also, a preprocessor macro
is introduced to handle event dispatch which also looks a bit nicer.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2016-04-09 15:44:54 +02:00
parent 6e8337e26a
commit 80a6f2f510
4 changed files with 32 additions and 41 deletions

View File

@ -637,6 +637,8 @@ main(int argc, char **argv)
query = xcb_get_extension_data(globalconf.connection, &xcb_shape_id); query = xcb_get_extension_data(globalconf.connection, &xcb_shape_id);
globalconf.have_shape = query->present; globalconf.have_shape = query->present;
event_init();
/* Allocate the key symbols */ /* Allocate the key symbols */
globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection); globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);

61
event.c
View File

@ -976,49 +976,34 @@ void event_handle(xcb_generic_event_t *event)
#undef EVENT #undef EVENT
} }
static uint8_t randr_screen_change_notify = 0; #define EXTENSION_EVENT(base, offset, callback) \
static uint8_t randr_output_change_notify = 0; if (globalconf.event_base_ ## base != 0 \
static uint8_t shape_notify = 0; && response_type == globalconf.event_base_ ## base + (offset)) \
static uint8_t xkb_notify = 0; callback((void *) event)
EXTENSION_EVENT(randr, XCB_RANDR_SCREEN_CHANGE_NOTIFY, event_handle_randr_screen_change_notify);
EXTENSION_EVENT(randr, XCB_RANDR_NOTIFY, event_handle_randr_output_change_notify);
EXTENSION_EVENT(shape, XCB_SHAPE_NOTIFY, event_handle_shape_notify);
EXTENSION_EVENT(xkb, 0, event_handle_xkb_notify);
#undef EXTENSION_EVENT
}
if(randr_screen_change_notify == 0 || randr_output_change_notify == 0) void event_init(void)
{ {
/* check for randr extension */ const xcb_query_extension_reply_t *reply;
const xcb_query_extension_reply_t *randr_query;
randr_query = xcb_get_extension_data(globalconf.connection, &xcb_randr_id); reply = xcb_get_extension_data(globalconf.connection, &xcb_randr_id);
if(randr_query->present) { if (reply && reply->present) {
xcb_randr_select_input(globalconf.connection, globalconf.screen->root, XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE); xcb_randr_select_input(globalconf.connection, globalconf.screen->root, XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE);
randr_screen_change_notify = randr_query->first_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY; globalconf.event_base_randr = reply->first_event;
randr_output_change_notify = randr_query->first_event + XCB_RANDR_NOTIFY;
}
} }
if(shape_notify == 0) reply = xcb_get_extension_data(globalconf.connection, &xcb_shape_id);
{ if (reply && reply->present)
/* check for shape extension */ globalconf.event_base_shape = reply->first_event;
const xcb_query_extension_reply_t *shape_query;
shape_query = xcb_get_extension_data(globalconf.connection, &xcb_shape_id);
if(shape_query->present)
shape_notify = shape_query->first_event + XCB_SHAPE_NOTIFY;
}
if(xkb_notify == 0) reply = xcb_get_extension_data(globalconf.connection, &xcb_xkb_id);
{ if (reply && reply->present)
/* check for xkb extension */ globalconf.event_base_xkb = reply->first_event;
const xcb_query_extension_reply_t *xkb_query;
xkb_query = xcb_get_extension_data(globalconf.connection, &xcb_xkb_id);
if(xkb_query->present)
xkb_notify = xkb_query->first_event;
}
if (response_type == randr_screen_change_notify)
event_handle_randr_screen_change_notify((void *) event);
if (response_type == randr_output_change_notify)
event_handle_randr_output_change_notify((void *) event);
if (response_type == shape_notify)
event_handle_shape_notify((void *) event);
if (response_type == xkb_notify)
event_handle_xkb_notify((void *) event);
} }
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -50,6 +50,7 @@ awesome_refresh(void)
return xcb_flush(globalconf.connection); return xcb_flush(globalconf.connection);
} }
void event_init(void);
void event_handle(xcb_generic_event_t *); void event_handle(xcb_generic_event_t *);
void event_drawable_under_mouse(lua_State *, int); void event_drawable_under_mouse(lua_State *, int);

View File

@ -82,8 +82,6 @@ typedef struct
screen_array_t screens; screen_array_t screens;
/** The primary screen, access through screen_get_primary() */ /** The primary screen, access through screen_get_primary() */
screen_t *primary_screen; screen_t *primary_screen;
/** Do we have RandR 1.3 or newer? */
bool have_randr_13;
/** Root window key bindings */ /** Root window key bindings */
key_array_t keys; key_array_t keys;
/** Root window mouse bindings */ /** Root window mouse bindings */
@ -92,10 +90,15 @@ typedef struct
xcb_atom_t selection_atom; xcb_atom_t selection_atom;
/** Window owning the WM_Sn selection */ /** Window owning the WM_Sn selection */
xcb_window_t selection_owner_window; xcb_window_t selection_owner_window;
/** Do we have RandR 1.3 or newer? */
bool have_randr_13;
/** Check for XTest extension */ /** Check for XTest extension */
bool have_xtest; bool have_xtest;
/** Check for SHAPE extension */ /** Check for SHAPE extension */
bool have_shape; bool have_shape;
uint8_t event_base_shape;
uint8_t event_base_xkb;
uint8_t event_base_randr;
/** Clients list */ /** Clients list */
client_array_t clients; client_array_t clients;
/** Embedded windows */ /** Embedded windows */