From 884867654044a324d84fdc011e4c701385baf78b Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sun, 30 Sep 2018 09:31:22 +0200 Subject: [PATCH] Add a function that allocates a pixmap and checks success Signed-off-by: Uli Schlachter --- src/common.h | 22 ++++++++++++++++++++++ src/compton.c | 26 ++++++++++++++------------ src/x.c | 4 +--- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/common.h b/src/common.h index 04c2279..6f1c6c8 100644 --- a/src/common.h +++ b/src/common.h @@ -1251,6 +1251,9 @@ extern session_t *ps_g; static inline void print_timestamp(session_t *ps); +void +ev_xcb_error(session_t *ps, xcb_generic_error_t *err); + #ifdef DEBUG_BACKTRACE #include @@ -2530,3 +2533,22 @@ free_pixmap(session_t *ps, Pixmap *p) { *p = None; } } + +/** + * Create a pixmap and check that creation succeeded. + */ +static inline xcb_pixmap_t +create_pixmap(session_t *ps, uint8_t depth, xcb_drawable_t drawable, uint16_t width, uint16_t height) +{ + xcb_connection_t *c = XGetXCBConnection(ps->dpy); + xcb_pixmap_t pix = xcb_generate_id(c); + xcb_void_cookie_t cookie = xcb_create_pixmap_checked(c, depth, pix, drawable, width, height); + xcb_generic_error_t *err = xcb_request_check(c, cookie); + if (err == NULL) + return pix; + + printf_err("Failed to create pixmap:"); + ev_xcb_error(ps, err); + free(err); + return XCB_NONE; +} diff --git a/src/compton.c b/src/compton.c index a8fdefe..be62eb5 100644 --- a/src/compton.c +++ b/src/compton.c @@ -628,10 +628,8 @@ win_build_shadow(session_t *ps, win *w, double opacity) { return None; } - shadow_pixmap = xcb_generate_id(c); - shadow_pixmap_argb = xcb_generate_id(c); - xcb_create_pixmap(c, 8, shadow_pixmap, ps->root, shadow_image->width, shadow_image->height); - xcb_create_pixmap(c, 32, shadow_pixmap_argb, ps->root, shadow_image->width, shadow_image->height); + shadow_pixmap = create_pixmap(ps, 8, ps->root, shadow_image->width, shadow_image->height); + shadow_pixmap_argb = create_pixmap(ps, 32, ps->root, shadow_image->width, shadow_image->height); if (!shadow_pixmap || !shadow_pixmap_argb) { printf_errf("(): failed to create shadow pixmaps"); @@ -701,9 +699,8 @@ solid_picture(session_t *ps, bool argb, double a, xcb_rectangle_t rect; xcb_connection_t *c = XGetXCBConnection(ps->dpy); - pixmap = xcb_generate_id(c); + pixmap = create_pixmap(ps, argb ? 32 : 8, ps->root, 1, 1); if (!pixmap) return None; - xcb_create_pixmap(c, argb ? 32 : 8, pixmap, ps->root, 1, 1); pa.repeat = True; picture = x_create_picture_with_standard_and_pixmap(ps, @@ -892,8 +889,11 @@ get_root_tile(session_t *ps) { // Create a pixmap if there isn't any if (!pixmap) { - pixmap = xcb_generate_id(c); - xcb_create_pixmap(c, ps->depth, pixmap, ps->root, 1, 1); + pixmap = create_pixmap(ps, ps->depth, ps->root, 1, 1); + if (pixmap == XCB_NONE) { + fprintf(stderr, "Failed to create some pixmap\n"); + exit(1); + } fill = true; } @@ -1710,9 +1710,11 @@ paint_all(session_t *ps, XserverRegion region, XserverRegion region_real, win *t else { if (!ps->tgt_buffer.pixmap) { free_paint(ps, &ps->tgt_buffer); - ps->tgt_buffer.pixmap = xcb_generate_id(c); - xcb_create_pixmap(c, ps->depth, ps->tgt_buffer.pixmap, - ps->root, ps->root_width, ps->root_height); + ps->tgt_buffer.pixmap = create_pixmap(ps, ps->depth, ps->root, ps->root_width, ps->root_height); + if (ps->tgt_buffer.pixmap == XCB_NONE) { + fprintf(stderr, "Failed to allocate a screen-sized pixmap\n"); + exit(1); + } } if (BKEND_GLX != ps->o.backend) @@ -2612,7 +2614,7 @@ xerror(Display __attribute__((unused)) *dpy, XErrorEvent *ev) { /** * XCB error handler function. */ -inline static void +void ev_xcb_error(session_t __attribute__((unused)) *ps, xcb_generic_error_t *err) { xerror_common(err->sequence, err->major_code, err->minor_code, err->error_code); } diff --git a/src/x.c b/src/x.c index cb9c149..0b8cd30 100644 --- a/src/x.c +++ b/src/x.c @@ -193,11 +193,9 @@ x_create_picture(session_t *ps, int wid, int hei, int depth = pictfmt->depth; - xcb_connection_t *c = XGetXCBConnection(ps->dpy); - Pixmap tmp_pixmap = xcb_generate_id(c); + Pixmap tmp_pixmap = create_pixmap(ps, depth, ps->root, wid, hei); if (!tmp_pixmap) return None; - xcb_create_pixmap(c, depth, tmp_pixmap, ps->root, wid, hei); xcb_render_picture_t picture = x_create_picture_with_pictfmt_and_pixmap(ps, pictfmt, tmp_pixmap, valuemask, attr);