xcb: add xcb_error(), a generic describe-an-x-error function

By default, generates an error message with the major/minor opcodes,
the error code and sequence number.

If available, it will use xcb-errors to generate an even better
description.
This commit is contained in:
Daniel Eklöf 2019-01-15 20:44:21 +01:00
parent 7a8ba94a49
commit fae2e5cb18
5 changed files with 67 additions and 44 deletions

35
bar.c
View file

@ -19,10 +19,6 @@
#include <xcb/xcb_event.h> #include <xcb/xcb_event.h>
#include <xcb/xcb_ewmh.h> #include <xcb/xcb_ewmh.h>
#if defined(HAVE_XCB_ERRORS)
#include <xcb/xcb_errors.h>
#endif
#include <cairo.h> #include <cairo.h>
#include <cairo-xcb.h> #include <cairo-xcb.h>
@ -558,11 +554,6 @@ run(struct bar_run_context *run_ctx)
LOG_DBG("all modules started"); 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); int fd = xcb_get_file_descriptor(bar->conn);
while (true) { while (true) {
@ -587,27 +578,9 @@ run(struct bar_run_context *run_ctx)
e = xcb_poll_for_event(bar->conn)) e = xcb_poll_for_event(bar->conn))
{ {
switch (XCB_EVENT_RESPONSE_TYPE(e)) { switch (XCB_EVENT_RESPONSE_TYPE(e)) {
case 0: { case 0:
#if defined(HAVE_XCB_ERRORS) LOG_ERR("%s", xcb_error((const xcb_generic_error_t *)e));
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
break; break;
}
case XCB_EXPOSE: case XCB_EXPOSE:
expose(_bar); expose(_bar);
@ -714,10 +687,6 @@ run(struct bar_run_context *run_ctx)
bar->cursor_name = NULL; 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_gc(bar->conn, bar->gc);
xcb_free_pixmap(bar->conn, bar->pixmap); xcb_free_pixmap(bar->conn, bar->pixmap);
xcb_destroy_window(bar->conn, bar->win); xcb_destroy_window(bar->conn, bar->win);

View file

@ -133,7 +133,7 @@ get_layouts(xcb_connection_t *conn)
conn, cookie, &err); conn, cookie, &err);
if (err != NULL) { 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); free(err);
return (struct layouts){.count = -1}; 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( xcb_get_atom_name_reply_t *atom_name = xcb_get_atom_name_reply(
conn, symbols_name_cookie, &err); conn, symbols_name_cookie, &err);
if (err != NULL) { 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); free(err);
goto err; goto err;
} }
@ -175,7 +175,7 @@ get_layouts(xcb_connection_t *conn)
for (ssize_t i = 0; i < ret.count; i++) { for (ssize_t i = 0; i < ret.count; i++) {
atom_name = xcb_get_atom_name_reply(conn, group_name_cookies[i], &err); atom_name = xcb_get_atom_name_reply(conn, group_name_cookies[i], &err);
if (err != NULL) { 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); free(err);
goto err; goto err;
} }
@ -245,7 +245,7 @@ get_current_layout(xcb_connection_t *conn)
conn, cookie, &err); conn, cookie, &err);
if (err != NULL) { 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; return -1;
} }
@ -278,7 +278,7 @@ register_for_events(xcb_connection_t *conn)
xcb_generic_error_t *err = xcb_request_check(conn, cookie); xcb_generic_error_t *err = xcb_request_check(conn, cookie);
if (err != NULL) { 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; return false;
} }

View file

@ -88,6 +88,7 @@ update_application(struct module *mod)
xcb_get_property_reply_t *r = xcb_get_property_reply(m->conn, c, &e); xcb_get_property_reply_t *r = xcb_get_property_reply(m->conn, c, &e);
if (e != NULL) { if (e != NULL) {
LOG_ERR("failed to get _NET_WM_PID");
free(e); free(e);
free(r); free(r);
return; return;
@ -236,6 +237,9 @@ run(struct module *mod)
_e = xcb_poll_for_event(m->conn)) _e = xcb_poll_for_event(m->conn))
{ {
switch (XCB_EVENT_RESPONSE_TYPE(_e)) { switch (XCB_EVENT_RESPONSE_TYPE(_e)) {
case 0:
LOG_ERR("%s", xcb_error((const xcb_generic_error_t *)_e));
case XCB_PROPERTY_NOTIFY: { case XCB_PROPERTY_NOTIFY: {
xcb_property_notify_event_t *e = (xcb_property_notify_event_t *)_e; xcb_property_notify_event_t *e = (xcb_property_notify_event_t *)_e;
if (e->atom == _NET_ACTIVE_WINDOW || if (e->atom == _NET_ACTIVE_WINDOW ||
@ -256,8 +260,6 @@ run(struct module *mod)
} }
break; break;
} }
case 0: break; /* error */
} }
free(_e); free(_e);

58
xcb.c
View file

@ -1,5 +1,6 @@
#include "xcb.h" #include "xcb.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
@ -8,6 +9,10 @@
#include <xcb/randr.h> #include <xcb/randr.h>
#include <xcb/render.h> #include <xcb/render.h>
#if defined(HAVE_XCB_ERRORS)
#include <xcb/xcb_errors.h>
#endif
#define LOG_MODULE "xcb" #define LOG_MODULE "xcb"
#define LOG_ENABLE_DBG 0 #define LOG_ENABLE_DBG 0
#include "log.h" #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_VISIBLE_NAME;
xcb_atom_t _NET_WM_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 bool
xcb_init(void) xcb_init(void)
{ {
@ -36,6 +53,10 @@ xcb_init(void)
return false; return false;
} }
#if defined(HAVE_XCB_ERRORS)
xcb_errors_context_new(conn, &err_context);
#endif
#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG #if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
const xcb_setup_t *setup = xcb_get_setup(conn); 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_t *randr_version =
xcb_randr_query_version_reply(conn, randr_cookie, &e); xcb_randr_query_version_reply(conn, randr_cookie, &e);
if (e != NULL) { 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); free(e);
xcb_disconnect(conn); xcb_disconnect(conn);
return false; return false;
@ -92,7 +113,7 @@ xcb_init(void)
xcb_render_query_version_reply_t *render_version = xcb_render_query_version_reply_t *render_version =
xcb_render_query_version_reply(conn, render_cookie, &e); xcb_render_query_version_reply(conn, render_cookie, &e);
if (e != NULL) { 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); free(e);
xcb_disconnect(conn); xcb_disconnect(conn);
return false; return false;
@ -137,7 +158,7 @@ get_atom(xcb_connection_t *conn, const char *name)
&e); &e);
if (e != NULL) { 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(e);
free(reply); 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); conn, xcb_get_atom_name(conn, atom), &e);
if (e != NULL) { if (e != NULL) {
LOG_ERR("failed to get atom name"); LOG_ERR("failed to get atom name: %s", xcb_error(e));
free(e); free(e);
free(reply); free(reply);
return NULL; return NULL;
@ -180,3 +201,32 @@ get_atom_name(xcb_connection_t *conn, xcb_atom_t atom)
free(reply); free(reply);
return name; 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;
}

2
xcb.h
View file

@ -8,6 +8,8 @@ bool xcb_init(void);
xcb_atom_t get_atom(xcb_connection_t *conn, const char *name); xcb_atom_t get_atom(xcb_connection_t *conn, const char *name);
char *get_atom_name(xcb_connection_t *conn, xcb_atom_t atom); char *get_atom_name(xcb_connection_t *conn, xcb_atom_t atom);
const char *xcb_error(const xcb_generic_error_t *error);
/* Cached atoms */ /* Cached atoms */
xcb_atom_t UTF8_STRING; xcb_atom_t UTF8_STRING;
xcb_atom_t _NET_WM_PID; xcb_atom_t _NET_WM_PID;