x: add function for converting x error to string

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2020-03-27 23:07:19 +00:00
parent 574eca3c25
commit 58582eb639
No known key found for this signature in database
GPG Key ID: 37C999F617EA1A47
2 changed files with 36 additions and 8 deletions

32
src/x.c
View File

@ -324,11 +324,13 @@ enum { XSyncBadCounter = 0,
};
/**
* X11 error handler function.
* Convert a X11 error to string
*
* XXX consider making this error to string
* @return a pointer to a string. this pointer shouldn NOT be freed, same buffer is used
* for multiple calls to this function,
*/
void x_print_error(unsigned long serial, uint8_t major, uint16_t minor, uint8_t error_code) {
static const char *
_x_strerror(unsigned long serial, uint8_t major, uint16_t minor, uint8_t error_code) {
session_t *const ps = ps_g;
int o = 0;
@ -337,6 +339,7 @@ void x_print_error(unsigned long serial, uint8_t major, uint16_t minor, uint8_t
#define CASESTRRET2(s) \
case s: name = #s; break
// TODO separate error code out from session_t
o = error_code - ps->xfixes_error;
switch (o) { CASESTRRET2(XCB_XFIXES_BAD_REGION); }
@ -398,8 +401,27 @@ void x_print_error(unsigned long serial, uint8_t major, uint16_t minor, uint8_t
#undef CASESTRRET2
log_debug("X error %d %s request %d minor %d serial %lu", error_code, name, major,
minor, serial);
thread_local static char buffer[256];
snprintf(buffer, sizeof(buffer), "X error %d %s request %d minor %d serial %lu",
error_code, name, major, minor, serial);
return buffer;
}
/**
* Log a X11 error
*/
void x_print_error(unsigned long serial, uint8_t major, uint16_t minor, uint8_t error_code) {
log_debug("%s", _x_strerror(serial, major, minor, error_code));
}
/*
* Convert a xcb_generic_error_t to a string that describes the error
*
* @return a pointer to a string. this pointer shouldn NOT be freed, same buffer is used
* for multiple calls to this function,
*/
const char *x_strerror(xcb_generic_error_t *e) {
return _x_strerror(e->full_sequence, e->major_code, e->minor_code, e->error_code);
}
/**

12
src/x.h
View File

@ -191,12 +191,18 @@ void x_set_picture_clip_region(xcb_connection_t *, xcb_render_picture_t, int16_t
void x_clear_picture_clip_region(xcb_connection_t *, xcb_render_picture_t pict);
/**
* X11 error handler function.
*
* XXX consider making this error to string
* Log a X11 error
*/
void x_print_error(unsigned long serial, uint8_t major, uint16_t minor, uint8_t error_code);
/*
* Convert a xcb_generic_error_t to a string that describes the error
*
* @return a pointer to a string. this pointer shouldn NOT be freed, same buffer is used
* for multiple calls to this function,
*/
const char *x_strerror(xcb_generic_error_t *e);
xcb_pixmap_t x_create_pixmap(xcb_connection_t *, uint8_t depth, xcb_drawable_t drawable,
int width, int height);