diff --git a/bar.c b/bar.c index 71deef6..334763c 100644 --- a/bar.c +++ b/bar.c @@ -19,10 +19,6 @@ #include #include -#if defined(HAVE_XCB_ERRORS) - #include -#endif - #include #include @@ -558,11 +554,6 @@ run(struct bar_run_context *run_ctx) LOG_DBG("all modules started"); -#if defined(HAVE_XCB_ERRORS) - xcb_errors_context_t *err_context; - xcb_errors_context_new(bar->conn, &err_context); -#endif - int fd = xcb_get_file_descriptor(bar->conn); while (true) { @@ -587,27 +578,9 @@ run(struct bar_run_context *run_ctx) e = xcb_poll_for_event(bar->conn)) { switch (XCB_EVENT_RESPONSE_TYPE(e)) { - case 0: { -#if defined(HAVE_XCB_ERRORS) - const xcb_value_error_t *error = (void *)e; - const char *major = xcb_errors_get_name_for_major_code( - err_context, error->major_opcode); - const char *minor = xcb_errors_get_name_for_minor_code( - err_context, error->major_opcode, error->minor_opcode); - - const char *extension; - const char *name = xcb_errors_get_name_for_error( - err_context, error->error_code, &extension); - - LOG_ERR("XCB error: %s (%s), code %s (%s), sequence %u, value %u", - major, minor != NULL ? minor : "no minor", - name, extension != NULL ? extension : "no extension", - error->sequence, error->bad_value); -#else - LOG_ERR(" XCB error: TODO"); -#endif + case 0: + LOG_ERR("%s", xcb_error((const xcb_generic_error_t *)e)); break; - } case XCB_EXPOSE: expose(_bar); @@ -714,10 +687,6 @@ run(struct bar_run_context *run_ctx) bar->cursor_name = NULL; } -#if defined(HAVE_XCB_ERRORS) - xcb_errors_context_free(err_context); -#endif - xcb_free_gc(bar->conn, bar->gc); xcb_free_pixmap(bar->conn, bar->pixmap); xcb_destroy_window(bar->conn, bar->win); diff --git a/modules/xkb.c b/modules/xkb.c index b2856c2..133ea57 100644 --- a/modules/xkb.c +++ b/modules/xkb.c @@ -133,7 +133,7 @@ get_layouts(xcb_connection_t *conn) conn, cookie, &err); if (err != NULL) { - LOG_ERR("failed to get group names and symbols (%d)", err->error_code); + LOG_ERR("failed to get group names and symbols: %s", xcb_error(err)); free(err); return (struct layouts){.count = -1}; } @@ -162,7 +162,7 @@ get_layouts(xcb_connection_t *conn) xcb_get_atom_name_reply_t *atom_name = xcb_get_atom_name_reply( conn, symbols_name_cookie, &err); if (err != NULL) { - LOG_ERR("failed to get atom name (%d)", err->error_code); + LOG_ERR("failed to get atom name: %s", xcb_error(err)); free(err); goto err; } @@ -175,7 +175,7 @@ get_layouts(xcb_connection_t *conn) for (ssize_t i = 0; i < ret.count; i++) { atom_name = xcb_get_atom_name_reply(conn, group_name_cookies[i], &err); if (err != NULL) { - LOG_ERR("failed to get atom name (%d)", err->error_code); + LOG_ERR("failed to get atom name: %s", xcb_error(err)); free(err); goto err; } @@ -245,7 +245,7 @@ get_current_layout(xcb_connection_t *conn) conn, cookie, &err); if (err != NULL) { - LOG_ERR("failed to get XKB state (%d)", err->error_code); + LOG_ERR("failed to get XKB state: %s", xcb_error(err)); return -1; } @@ -278,7 +278,7 @@ register_for_events(xcb_connection_t *conn) xcb_generic_error_t *err = xcb_request_check(conn, cookie); if (err != NULL) { - LOG_ERR("failed to register for events (%d)", err->error_code); + LOG_ERR("failed to register for events: %s", xcb_error(err)); return false; } diff --git a/modules/xwindow.c b/modules/xwindow.c index c6a6fe3..138a748 100644 --- a/modules/xwindow.c +++ b/modules/xwindow.c @@ -88,6 +88,7 @@ update_application(struct module *mod) xcb_get_property_reply_t *r = xcb_get_property_reply(m->conn, c, &e); if (e != NULL) { + LOG_ERR("failed to get _NET_WM_PID"); free(e); free(r); return; @@ -236,6 +237,9 @@ run(struct module *mod) _e = xcb_poll_for_event(m->conn)) { switch (XCB_EVENT_RESPONSE_TYPE(_e)) { + case 0: + LOG_ERR("%s", xcb_error((const xcb_generic_error_t *)_e)); + case XCB_PROPERTY_NOTIFY: { xcb_property_notify_event_t *e = (xcb_property_notify_event_t *)_e; if (e->atom == _NET_ACTIVE_WINDOW || @@ -256,8 +260,6 @@ run(struct module *mod) } break; } - - case 0: break; /* error */ } free(_e); diff --git a/xcb.c b/xcb.c index 33d7dd9..17435e5 100644 --- a/xcb.c +++ b/xcb.c @@ -1,5 +1,6 @@ #include "xcb.h" +#include #include #include #include @@ -8,6 +9,10 @@ #include #include +#if defined(HAVE_XCB_ERRORS) + #include +#endif + #define LOG_MODULE "xcb" #define LOG_ENABLE_DBG 0 #include "log.h" @@ -27,6 +32,18 @@ xcb_atom_t _NET_CURRENT_DESKTOP; xcb_atom_t _NET_WM_VISIBLE_NAME; xcb_atom_t _NET_WM_NAME; +#if defined(HAVE_XCB_ERRORS) +static xcb_errors_context_t *err_context; +#endif + +static void __attribute__((destructor)) +fini(void) +{ +#if defined(HAVE_XCB_ERRORS) + xcb_errors_context_free(err_context); +#endif +} + bool xcb_init(void) { @@ -36,6 +53,10 @@ xcb_init(void) return false; } +#if defined(HAVE_XCB_ERRORS) + xcb_errors_context_new(conn, &err_context); +#endif + #if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG const xcb_setup_t *setup = xcb_get_setup(conn); @@ -83,7 +104,7 @@ xcb_init(void) xcb_randr_query_version_reply_t *randr_version = xcb_randr_query_version_reply(conn, randr_cookie, &e); if (e != NULL) { - LOG_ERR("failed to query RANDR version: %d", e->error_code); + LOG_ERR("failed to query RANDR version: %s", xcb_error(e)); free(e); xcb_disconnect(conn); return false; @@ -92,7 +113,7 @@ xcb_init(void) xcb_render_query_version_reply_t *render_version = xcb_render_query_version_reply(conn, render_cookie, &e); if (e != NULL) { - LOG_ERR("failed to query RENDER version: %d", e->error_code); + LOG_ERR("failed to query RENDER version: %s", xcb_error(e)); free(e); xcb_disconnect(conn); return false; @@ -137,7 +158,7 @@ get_atom(xcb_connection_t *conn, const char *name) &e); if (e != NULL) { - LOG_ERR("failed to get atom for %s", name); + LOG_ERR("%s: failed to get atom for %s", name, xcb_error(e)); free(e); free(reply); @@ -164,7 +185,7 @@ get_atom_name(xcb_connection_t *conn, xcb_atom_t atom) conn, xcb_get_atom_name(conn, atom), &e); if (e != NULL) { - LOG_ERR("failed to get atom name"); + LOG_ERR("failed to get atom name: %s", xcb_error(e)); free(e); free(reply); return NULL; @@ -180,3 +201,32 @@ get_atom_name(xcb_connection_t *conn, xcb_atom_t atom) free(reply); return name; } + +const char * +xcb_error(const xcb_generic_error_t *error) +{ + static char msg[1024]; + +#if defined(HAVE_XCB_ERRORS) + const char *major = xcb_errors_get_name_for_major_code( + err_context, error->major_code); + const char *minor = xcb_errors_get_name_for_minor_code( + err_context, error->major_code, error->minor_code); + + const char *extension; + const char *name = xcb_errors_get_name_for_error( + err_context, error->error_code, &extension); + + snprintf(msg, sizeof(msg), + "XCB: %s (%s), code %s (%s), sequence %u", + major, minor != NULL ? minor : "no minor", + name, extension != NULL ? extension : "no extension", + error->sequence); +#else + snprintf(msg, sizeof(msg), "XCB: op %hhu:%hu, code %hhu, sequence %hu", + error->major_code, error->minor_code, error->error_code, + error->sequence); +#endif + + return msg; +} diff --git a/xcb.h b/xcb.h index ce84759..d55932c 100644 --- a/xcb.h +++ b/xcb.h @@ -8,6 +8,8 @@ bool xcb_init(void); xcb_atom_t get_atom(xcb_connection_t *conn, const char *name); char *get_atom_name(xcb_connection_t *conn, xcb_atom_t atom); +const char *xcb_error(const xcb_generic_error_t *error); + /* Cached atoms */ xcb_atom_t UTF8_STRING; xcb_atom_t _NET_WM_PID;