2018-10-03 17:14:51 -04:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) 2018 Yuxuan Shui <yshuiv7@gmail.com>
|
2018-09-29 14:07:39 -04:00
|
|
|
#include <stdbool.h>
|
2019-01-20 16:15:20 -05:00
|
|
|
#include <stdlib.h>
|
2018-09-29 14:07:39 -04:00
|
|
|
|
2018-12-30 03:00:22 -05:00
|
|
|
#include <X11/Xutil.h>
|
2019-03-10 08:34:37 -04:00
|
|
|
#include <pixman.h>
|
2019-01-20 16:15:20 -05:00
|
|
|
#include <xcb/composite.h>
|
|
|
|
#include <xcb/damage.h>
|
|
|
|
#include <xcb/render.h>
|
2019-03-10 08:34:37 -04:00
|
|
|
#include <xcb/sync.h>
|
|
|
|
#include <xcb/xcb.h>
|
|
|
|
#include <xcb/xcb_renderutil.h>
|
|
|
|
#include <xcb/xfixes.h>
|
2018-09-06 14:17:26 -04:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
#include "backend/gl/glx.h"
|
2018-09-06 14:17:26 -04:00
|
|
|
#include "common.h"
|
2019-03-10 08:34:37 -04:00
|
|
|
#include "compiler.h"
|
2019-02-17 18:47:46 -05:00
|
|
|
#include "kernel.h"
|
2018-12-15 12:53:17 -05:00
|
|
|
#include "log.h"
|
2019-03-10 08:34:37 -04:00
|
|
|
#include "region.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "x.h"
|
2018-09-06 14:17:26 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific attribute of a window.
|
|
|
|
*
|
|
|
|
* Returns a blank structure if the returned type and format does not
|
|
|
|
* match the requested type and format.
|
|
|
|
*
|
|
|
|
* @param ps current session
|
|
|
|
* @param w window
|
|
|
|
* @param atom atom of attribute to fetch
|
|
|
|
* @param length length to read
|
|
|
|
* @param rtype atom of the requested type
|
|
|
|
* @param rformat requested format
|
|
|
|
* @return a <code>winprop_t</code> structure containing the attribute
|
|
|
|
* and number of items. A blank one on failure.
|
|
|
|
*/
|
2019-03-10 08:34:37 -04:00
|
|
|
winprop_t wid_get_prop_adv(const session_t *ps, xcb_window_t w, xcb_atom_t atom,
|
2019-03-30 05:07:21 -04:00
|
|
|
int offset, int length, xcb_atom_t rtype, int rformat) {
|
2019-03-10 08:34:37 -04:00
|
|
|
xcb_get_property_reply_t *r = xcb_get_property_reply(
|
2019-03-30 05:07:21 -04:00
|
|
|
ps->c,
|
|
|
|
xcb_get_property(ps->c, 0, w, atom, rtype, to_u32_checked(offset),
|
|
|
|
to_u32_checked(length)),
|
|
|
|
NULL);
|
2019-03-10 08:34:37 -04:00
|
|
|
|
|
|
|
if (r && xcb_get_property_value_length(r) &&
|
|
|
|
(rtype == XCB_GET_PROPERTY_TYPE_ANY || r->type == rtype) &&
|
|
|
|
(!rformat || r->format == rformat) &&
|
|
|
|
(r->format == 8 || r->format == 16 || r->format == 32)) {
|
2019-03-30 05:07:21 -04:00
|
|
|
auto len = xcb_get_property_value_length(r);
|
2019-03-10 08:34:37 -04:00
|
|
|
return (winprop_t){
|
|
|
|
.ptr = xcb_get_property_value(r),
|
2019-03-30 05:07:21 -04:00
|
|
|
.nitems = (ulong)(len / (r->format / 8)),
|
2019-03-10 08:34:37 -04:00
|
|
|
.type = r->type,
|
|
|
|
.format = r->format,
|
|
|
|
.r = r,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
free(r);
|
|
|
|
return (winprop_t){
|
|
|
|
.ptr = NULL, .nitems = 0, .type = XCB_GET_PROPERTY_TYPE_ANY, .format = 0};
|
2018-09-06 14:17:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-27 15:45:38 -05:00
|
|
|
* Get the value of a type-<code>xcb_window_t</code> property of a window.
|
2018-09-06 14:17:26 -04:00
|
|
|
*
|
|
|
|
* @return the value if successful, 0 otherwise
|
|
|
|
*/
|
2019-03-10 08:34:37 -04:00
|
|
|
xcb_window_t wid_get_prop_window(session_t *ps, xcb_window_t wid, xcb_atom_t aprop) {
|
|
|
|
// Get the attribute
|
|
|
|
xcb_window_t p = XCB_NONE;
|
|
|
|
winprop_t prop = wid_get_prop(ps, wid, aprop, 1L, XCB_ATOM_WINDOW, 32);
|
|
|
|
|
|
|
|
// Return it
|
|
|
|
if (prop.nitems) {
|
2019-03-30 05:07:21 -04:00
|
|
|
p = (xcb_window_t)*prop.p32;
|
2019-03-10 08:34:37 -04:00
|
|
|
}
|
2018-09-06 14:17:26 -04:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
free_winprop(&prop);
|
2018-09-06 14:17:26 -04:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
return p;
|
2018-09-06 14:17:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the value of a text property of a window.
|
|
|
|
*/
|
2019-03-10 08:34:37 -04:00
|
|
|
bool wid_get_text_prop(session_t *ps, xcb_window_t wid, xcb_atom_t prop, char ***pstrlst,
|
|
|
|
int *pnstr) {
|
|
|
|
XTextProperty text_prop = {NULL, XCB_NONE, 0, 0};
|
|
|
|
|
|
|
|
if (!(XGetTextProperty(ps->dpy, wid, &text_prop, prop) && text_prop.value))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Success != XmbTextPropertyToTextList(ps->dpy, &text_prop, pstrlst, pnstr) ||
|
|
|
|
!*pnstr) {
|
|
|
|
*pnstr = 0;
|
|
|
|
if (*pstrlst)
|
|
|
|
XFreeStringList(*pstrlst);
|
|
|
|
cxfree(text_prop.value);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cxfree(text_prop.value);
|
|
|
|
return true;
|
2018-09-06 14:17:26 -04:00
|
|
|
}
|
2018-09-23 14:10:46 -04:00
|
|
|
|
2019-02-03 10:14:14 -05:00
|
|
|
// A cache of pict formats. We assume they don't change during the lifetime
|
|
|
|
// of compton
|
|
|
|
static thread_local xcb_render_query_pict_formats_reply_t *g_pictfmts = NULL;
|
|
|
|
|
|
|
|
static inline void x_get_server_pictfmts(xcb_connection_t *c) {
|
2019-03-10 08:34:37 -04:00
|
|
|
if (g_pictfmts)
|
|
|
|
return;
|
|
|
|
xcb_generic_error_t *e = NULL;
|
|
|
|
// Get window picture format
|
|
|
|
g_pictfmts =
|
|
|
|
xcb_render_query_pict_formats_reply(c, xcb_render_query_pict_formats(c), &e);
|
|
|
|
if (e || !g_pictfmts) {
|
|
|
|
log_fatal("failed to get pict formats\n");
|
|
|
|
abort();
|
|
|
|
}
|
2018-09-23 14:10:46 -04:00
|
|
|
}
|
|
|
|
|
2019-02-06 19:18:47 -05:00
|
|
|
const xcb_render_pictforminfo_t *
|
|
|
|
x_get_pictform_for_visual(xcb_connection_t *c, xcb_visualid_t visual) {
|
2019-03-10 08:34:37 -04:00
|
|
|
x_get_server_pictfmts(c);
|
|
|
|
|
|
|
|
xcb_render_pictvisual_t *pv = xcb_render_util_find_visual_format(g_pictfmts, visual);
|
|
|
|
for (xcb_render_pictforminfo_iterator_t i =
|
|
|
|
xcb_render_query_pict_formats_formats_iterator(g_pictfmts);
|
|
|
|
i.rem; xcb_render_pictforminfo_next(&i)) {
|
|
|
|
if (i.data->id == pv->format) {
|
|
|
|
return i.data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2018-09-23 14:10:46 -04:00
|
|
|
}
|
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
static xcb_visualid_t attr_pure x_get_visual_for_pictfmt(xcb_render_query_pict_formats_reply_t *r,
|
|
|
|
xcb_render_pictformat_t fmt) {
|
|
|
|
for (auto screen = xcb_render_query_pict_formats_screens_iterator(r); screen.rem;
|
|
|
|
xcb_render_pictscreen_next(&screen)) {
|
|
|
|
for (auto depth = xcb_render_pictscreen_depths_iterator(screen.data);
|
|
|
|
depth.rem; xcb_render_pictdepth_next(&depth)) {
|
|
|
|
for (auto pv = xcb_render_pictdepth_visuals_iterator(depth.data);
|
|
|
|
pv.rem; xcb_render_pictvisual_next(&pv)) {
|
|
|
|
if (pv.data->format == fmt) {
|
|
|
|
return pv.data->visual;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return XCB_NONE;
|
2019-02-26 18:52:37 -05:00
|
|
|
}
|
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
xcb_visualid_t x_get_visual_for_standard(xcb_connection_t *c, xcb_pict_standard_t std) {
|
|
|
|
x_get_server_pictfmts(c);
|
2019-02-26 18:52:37 -05:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
auto pictfmt = xcb_render_util_find_standard_format(g_pictfmts, std);
|
2019-02-26 18:52:37 -05:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
return x_get_visual_for_pictfmt(g_pictfmts, pictfmt->id);
|
2019-02-26 18:52:37 -05:00
|
|
|
}
|
|
|
|
|
2019-02-03 11:48:52 -05:00
|
|
|
int x_get_visual_depth(xcb_connection_t *c, xcb_visualid_t visual) {
|
2019-03-10 08:34:37 -04:00
|
|
|
auto setup = xcb_get_setup(c);
|
|
|
|
for (auto screen = xcb_setup_roots_iterator(setup); screen.rem;
|
|
|
|
xcb_screen_next(&screen)) {
|
|
|
|
for (auto depth = xcb_screen_allowed_depths_iterator(screen.data);
|
|
|
|
depth.rem; xcb_depth_next(&depth)) {
|
|
|
|
const int len = xcb_depth_visuals_length(depth.data);
|
|
|
|
const xcb_visualtype_t *visuals = xcb_depth_visuals(depth.data);
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
if (visual == visuals[i].visual_id) {
|
|
|
|
return depth.data->depth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
2019-02-03 11:48:52 -05:00
|
|
|
}
|
|
|
|
|
2018-09-23 14:10:46 -04:00
|
|
|
xcb_render_picture_t
|
2019-03-10 08:34:37 -04:00
|
|
|
x_create_picture_with_pictfmt_and_pixmap(xcb_connection_t *c,
|
|
|
|
const xcb_render_pictforminfo_t *pictfmt,
|
2019-03-30 05:07:21 -04:00
|
|
|
xcb_pixmap_t pixmap, uint32_t valuemask,
|
2019-03-10 08:34:37 -04:00
|
|
|
const xcb_render_create_picture_value_list_t *attr) {
|
|
|
|
void *buf = NULL;
|
|
|
|
if (attr) {
|
|
|
|
xcb_render_create_picture_value_list_serialize(&buf, valuemask, attr);
|
|
|
|
if (!buf) {
|
|
|
|
log_error("failed to serialize picture attributes");
|
|
|
|
return XCB_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 00:49:39 -04:00
|
|
|
xcb_render_picture_t tmp_picture = x_new_id(c);
|
2019-03-10 08:34:37 -04:00
|
|
|
xcb_generic_error_t *e =
|
|
|
|
xcb_request_check(c, xcb_render_create_picture_checked(
|
|
|
|
c, tmp_picture, pixmap, pictfmt->id, valuemask, buf));
|
|
|
|
free(buf);
|
|
|
|
if (e) {
|
|
|
|
x_print_error(e->full_sequence, e->major_code, e->minor_code, e->error_code);
|
|
|
|
log_error("failed to create picture");
|
|
|
|
return XCB_NONE;
|
|
|
|
}
|
|
|
|
return tmp_picture;
|
2018-09-23 14:10:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
xcb_render_picture_t
|
2019-03-10 08:34:37 -04:00
|
|
|
x_create_picture_with_visual_and_pixmap(xcb_connection_t *c, xcb_visualid_t visual,
|
2019-03-30 05:07:21 -04:00
|
|
|
xcb_pixmap_t pixmap, uint32_t valuemask,
|
2019-03-10 08:34:37 -04:00
|
|
|
const xcb_render_create_picture_value_list_t *attr) {
|
|
|
|
const xcb_render_pictforminfo_t *pictfmt = x_get_pictform_for_visual(c, visual);
|
|
|
|
return x_create_picture_with_pictfmt_and_pixmap(c, pictfmt, pixmap, valuemask, attr);
|
2018-09-23 14:10:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
xcb_render_picture_t
|
2019-03-10 08:34:37 -04:00
|
|
|
x_create_picture_with_standard_and_pixmap(xcb_connection_t *c, xcb_pict_standard_t standard,
|
2019-03-30 05:07:21 -04:00
|
|
|
xcb_pixmap_t pixmap, uint32_t valuemask,
|
2019-03-10 08:34:37 -04:00
|
|
|
const xcb_render_create_picture_value_list_t *attr) {
|
|
|
|
x_get_server_pictfmts(c);
|
|
|
|
|
|
|
|
auto pictfmt = xcb_render_util_find_standard_format(g_pictfmts, standard);
|
|
|
|
assert(pictfmt);
|
|
|
|
return x_create_picture_with_pictfmt_and_pixmap(c, pictfmt, pixmap, valuemask, attr);
|
2018-09-23 14:10:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an picture.
|
|
|
|
*/
|
|
|
|
xcb_render_picture_t
|
2019-03-30 05:07:21 -04:00
|
|
|
x_create_picture_with_pictfmt(xcb_connection_t *c, xcb_drawable_t d, int w, int h,
|
|
|
|
const xcb_render_pictforminfo_t *pictfmt, uint32_t valuemask,
|
2019-03-10 08:34:37 -04:00
|
|
|
const xcb_render_create_picture_value_list_t *attr) {
|
2019-03-30 05:07:21 -04:00
|
|
|
uint8_t depth = pictfmt->depth;
|
2018-09-23 14:10:46 -04:00
|
|
|
|
2019-03-30 05:07:21 -04:00
|
|
|
xcb_pixmap_t tmp_pixmap = x_create_pixmap(c, depth, d, w, h);
|
2019-03-10 08:34:37 -04:00
|
|
|
if (!tmp_pixmap)
|
|
|
|
return XCB_NONE;
|
2018-09-23 14:10:46 -04:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
xcb_render_picture_t picture = x_create_picture_with_pictfmt_and_pixmap(
|
|
|
|
c, pictfmt, tmp_pixmap, valuemask, attr);
|
2018-09-30 15:53:52 -04:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
xcb_free_pixmap(c, tmp_pixmap);
|
2018-09-23 14:10:46 -04:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
return picture;
|
2018-09-23 14:10:46 -04:00
|
|
|
}
|
2018-09-29 18:36:53 -04:00
|
|
|
|
2018-12-21 18:44:42 -05:00
|
|
|
xcb_render_picture_t
|
2019-02-26 19:30:27 -05:00
|
|
|
x_create_picture_with_visual(xcb_connection_t *c, xcb_drawable_t d, int w, int h,
|
2019-03-30 05:07:21 -04:00
|
|
|
xcb_visualid_t visual, uint32_t valuemask,
|
2019-03-10 08:34:37 -04:00
|
|
|
const xcb_render_create_picture_value_list_t *attr) {
|
|
|
|
auto pictfmt = x_get_pictform_for_visual(c, visual);
|
|
|
|
return x_create_picture_with_pictfmt(c, d, w, h, pictfmt, valuemask, attr);
|
2018-12-21 18:44:42 -05:00
|
|
|
}
|
|
|
|
|
2019-02-03 10:59:31 -05:00
|
|
|
bool x_fetch_region(xcb_connection_t *c, xcb_xfixes_region_t r, pixman_region32_t *res) {
|
2019-03-10 08:34:37 -04:00
|
|
|
xcb_generic_error_t *e = NULL;
|
|
|
|
xcb_xfixes_fetch_region_reply_t *xr =
|
|
|
|
xcb_xfixes_fetch_region_reply(c, xcb_xfixes_fetch_region(c, r), &e);
|
|
|
|
if (!xr) {
|
|
|
|
log_error("Failed to fetch rectangles");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nrect = xcb_xfixes_fetch_region_rectangles_length(xr);
|
|
|
|
auto b = ccalloc(nrect, pixman_box32_t);
|
|
|
|
xcb_rectangle_t *xrect = xcb_xfixes_fetch_region_rectangles(xr);
|
|
|
|
for (int i = 0; i < nrect; i++) {
|
|
|
|
b[i] = (pixman_box32_t){.x1 = xrect[i].x,
|
|
|
|
.y1 = xrect[i].y,
|
|
|
|
.x2 = xrect[i].x + xrect[i].width,
|
|
|
|
.y2 = xrect[i].y + xrect[i].height};
|
|
|
|
}
|
|
|
|
bool ret = pixman_region32_init_rects(res, b, nrect);
|
|
|
|
free(b);
|
|
|
|
free(xr);
|
|
|
|
return ret;
|
2018-09-29 18:36:53 -04:00
|
|
|
}
|
|
|
|
|
2019-02-03 10:59:31 -05:00
|
|
|
void x_set_picture_clip_region(xcb_connection_t *c, xcb_render_picture_t pict,
|
2019-03-30 05:07:21 -04:00
|
|
|
int16_t clip_x_origin, int16_t clip_y_origin,
|
|
|
|
const region_t *reg) {
|
2019-03-10 08:34:37 -04:00
|
|
|
int nrects;
|
|
|
|
const rect_t *rects = pixman_region32_rectangles((region_t *)reg, &nrects);
|
|
|
|
auto xrects = ccalloc(nrects, xcb_rectangle_t);
|
|
|
|
for (int i = 0; i < nrects; i++)
|
|
|
|
xrects[i] = (xcb_rectangle_t){
|
2019-03-30 05:07:21 -04:00
|
|
|
.x = to_i16_checked(rects[i].x1),
|
|
|
|
.y = to_i16_checked(rects[i].y1),
|
|
|
|
.width = to_u16_checked(rects[i].x2 - rects[i].x1),
|
|
|
|
.height = to_u16_checked(rects[i].y2 - rects[i].y1),
|
2019-03-10 08:34:37 -04:00
|
|
|
};
|
|
|
|
|
2019-03-30 05:07:21 -04:00
|
|
|
xcb_generic_error_t *e = xcb_request_check(
|
|
|
|
c, xcb_render_set_picture_clip_rectangles_checked(
|
|
|
|
c, pict, clip_x_origin, clip_y_origin, to_u32_checked(nrects), xrects));
|
2019-03-10 08:34:37 -04:00
|
|
|
if (e)
|
|
|
|
log_error("Failed to set clip region");
|
|
|
|
free(e);
|
|
|
|
free(xrects);
|
|
|
|
return;
|
2018-09-29 18:36:53 -04:00
|
|
|
}
|
2018-09-30 09:37:21 -04:00
|
|
|
|
2019-02-03 10:59:31 -05:00
|
|
|
void x_clear_picture_clip_region(xcb_connection_t *c, xcb_render_picture_t pict) {
|
2019-03-10 08:34:37 -04:00
|
|
|
xcb_render_change_picture_value_list_t v = {.clipmask = XCB_NONE};
|
|
|
|
xcb_generic_error_t *e = xcb_request_check(
|
|
|
|
c, xcb_render_change_picture(c, pict, XCB_RENDER_CP_CLIP_MASK, &v));
|
|
|
|
if (e)
|
|
|
|
log_error("failed to clear clip region");
|
|
|
|
free(e);
|
|
|
|
return;
|
2018-12-21 18:44:42 -05:00
|
|
|
}
|
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
enum { XSyncBadCounter = 0,
|
|
|
|
XSyncBadAlarm = 1,
|
|
|
|
XSyncBadFence = 2,
|
2018-12-30 02:06:47 -05:00
|
|
|
};
|
|
|
|
|
2018-09-30 09:37:21 -04:00
|
|
|
/**
|
|
|
|
* X11 error handler function.
|
|
|
|
*
|
|
|
|
* XXX consider making this error to string
|
|
|
|
*/
|
2019-03-30 05:07:21 -04:00
|
|
|
void x_print_error(unsigned long serial, uint8_t major, uint16_t minor, uint8_t error_code) {
|
2019-03-10 08:34:37 -04:00
|
|
|
session_t *const ps = ps_g;
|
|
|
|
|
|
|
|
int o = 0;
|
|
|
|
const char *name = "Unknown";
|
|
|
|
|
|
|
|
if (major == ps->composite_opcode && minor == XCB_COMPOSITE_REDIRECT_SUBWINDOWS) {
|
|
|
|
log_fatal("Another composite manager is already running "
|
|
|
|
"(and does not handle _NET_WM_CM_Sn correctly)");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CASESTRRET2(s) \
|
|
|
|
case s: name = #s; break
|
|
|
|
|
|
|
|
o = error_code - ps->xfixes_error;
|
|
|
|
switch (o) { CASESTRRET2(XCB_XFIXES_BAD_REGION); }
|
|
|
|
|
|
|
|
o = error_code - ps->damage_error;
|
|
|
|
switch (o) { CASESTRRET2(XCB_DAMAGE_BAD_DAMAGE); }
|
|
|
|
|
|
|
|
o = error_code - ps->render_error;
|
|
|
|
switch (o) {
|
|
|
|
CASESTRRET2(XCB_RENDER_PICT_FORMAT);
|
|
|
|
CASESTRRET2(XCB_RENDER_PICTURE);
|
|
|
|
CASESTRRET2(XCB_RENDER_PICT_OP);
|
|
|
|
CASESTRRET2(XCB_RENDER_GLYPH_SET);
|
|
|
|
CASESTRRET2(XCB_RENDER_GLYPH);
|
|
|
|
}
|
2018-09-30 09:37:21 -04:00
|
|
|
|
|
|
|
#ifdef CONFIG_OPENGL
|
2019-03-10 08:34:37 -04:00
|
|
|
if (ps->glx_exists) {
|
|
|
|
o = error_code - ps->glx_error;
|
|
|
|
switch (o) {
|
|
|
|
CASESTRRET2(GLX_BAD_SCREEN);
|
|
|
|
CASESTRRET2(GLX_BAD_ATTRIBUTE);
|
|
|
|
CASESTRRET2(GLX_NO_EXTENSION);
|
|
|
|
CASESTRRET2(GLX_BAD_VISUAL);
|
|
|
|
CASESTRRET2(GLX_BAD_CONTEXT);
|
|
|
|
CASESTRRET2(GLX_BAD_VALUE);
|
|
|
|
CASESTRRET2(GLX_BAD_ENUM);
|
|
|
|
}
|
|
|
|
}
|
2018-09-30 09:37:21 -04:00
|
|
|
#endif
|
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
if (ps->xsync_exists) {
|
|
|
|
o = error_code - ps->xsync_error;
|
|
|
|
switch (o) {
|
|
|
|
CASESTRRET2(XSyncBadCounter);
|
|
|
|
CASESTRRET2(XSyncBadAlarm);
|
|
|
|
CASESTRRET2(XSyncBadFence);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (error_code) {
|
|
|
|
CASESTRRET2(BadAccess);
|
|
|
|
CASESTRRET2(BadAlloc);
|
|
|
|
CASESTRRET2(BadAtom);
|
|
|
|
CASESTRRET2(BadColor);
|
|
|
|
CASESTRRET2(BadCursor);
|
|
|
|
CASESTRRET2(BadDrawable);
|
|
|
|
CASESTRRET2(BadFont);
|
|
|
|
CASESTRRET2(BadGC);
|
|
|
|
CASESTRRET2(BadIDChoice);
|
|
|
|
CASESTRRET2(BadImplementation);
|
|
|
|
CASESTRRET2(BadLength);
|
|
|
|
CASESTRRET2(BadMatch);
|
|
|
|
CASESTRRET2(BadName);
|
|
|
|
CASESTRRET2(BadPixmap);
|
|
|
|
CASESTRRET2(BadRequest);
|
|
|
|
CASESTRRET2(BadValue);
|
|
|
|
CASESTRRET2(BadWindow);
|
|
|
|
}
|
2018-09-30 09:37:21 -04:00
|
|
|
|
|
|
|
#undef CASESTRRET2
|
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
log_debug("X error %d %s request %d minor %d serial %lu", error_code, name, major,
|
|
|
|
minor, serial);
|
2018-09-30 09:37:21 -04:00
|
|
|
}
|
2018-09-30 15:53:52 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a pixmap and check that creation succeeded.
|
|
|
|
*/
|
2019-03-10 08:34:37 -04:00
|
|
|
xcb_pixmap_t x_create_pixmap(xcb_connection_t *c, uint8_t depth, xcb_drawable_t drawable,
|
2019-03-30 05:07:21 -04:00
|
|
|
int width, int height) {
|
2019-04-08 00:49:39 -04:00
|
|
|
xcb_pixmap_t pix = x_new_id(c);
|
2019-03-30 05:07:21 -04:00
|
|
|
xcb_void_cookie_t cookie = xcb_create_pixmap_checked(
|
|
|
|
c, depth, pix, drawable, to_u16_checked(width), to_u16_checked(height));
|
2019-03-10 08:34:37 -04:00
|
|
|
xcb_generic_error_t *err = xcb_request_check(c, cookie);
|
|
|
|
if (err == NULL)
|
|
|
|
return pix;
|
|
|
|
|
|
|
|
log_error("Failed to create pixmap:");
|
|
|
|
x_print_error(err->sequence, err->major_code, err->minor_code, err->error_code);
|
|
|
|
free(err);
|
|
|
|
return XCB_NONE;
|
2018-09-30 15:53:52 -04:00
|
|
|
}
|
2018-12-15 16:11:41 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate a pixmap.
|
|
|
|
*
|
|
|
|
* Detect whether the pixmap is valid with XGetGeometry. Well, maybe there
|
|
|
|
* are better ways.
|
|
|
|
*/
|
2019-03-10 08:34:37 -04:00
|
|
|
bool x_validate_pixmap(xcb_connection_t *c, xcb_pixmap_t pixmap) {
|
|
|
|
if (pixmap == XCB_NONE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto r = xcb_get_geometry_reply(c, xcb_get_geometry(c, pixmap), NULL);
|
|
|
|
if (!r) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = r->width && r->height;
|
|
|
|
free(r);
|
|
|
|
return ret;
|
2018-12-15 16:11:41 -05:00
|
|
|
}
|
2018-12-21 18:44:42 -05:00
|
|
|
/// Names of root window properties that could point to a pixmap of
|
|
|
|
/// background.
|
|
|
|
static const char *background_props_str[] = {
|
2019-03-10 08:34:37 -04:00
|
|
|
"_XROOTPMAP_ID",
|
|
|
|
"_XSETROOT_ID",
|
|
|
|
0,
|
2018-12-21 18:44:42 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
xcb_pixmap_t x_get_root_back_pixmap(session_t *ps) {
|
2019-03-10 08:34:37 -04:00
|
|
|
xcb_pixmap_t pixmap = XCB_NONE;
|
|
|
|
|
|
|
|
// Get the values of background attributes
|
|
|
|
for (int p = 0; background_props_str[p]; p++) {
|
|
|
|
xcb_atom_t prop_atom = get_atom(ps, background_props_str[p]);
|
|
|
|
winprop_t prop =
|
|
|
|
wid_get_prop(ps, ps->root, prop_atom, 1, XCB_ATOM_PIXMAP, 32);
|
|
|
|
if (prop.nitems) {
|
2019-03-30 05:07:21 -04:00
|
|
|
pixmap = (xcb_pixmap_t)*prop.p32;
|
2019-03-10 08:34:37 -04:00
|
|
|
free_winprop(&prop);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free_winprop(&prop);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pixmap;
|
2018-12-21 18:44:42 -05:00
|
|
|
}
|
|
|
|
|
2019-02-06 19:30:32 -05:00
|
|
|
bool x_is_root_back_pixmap_atom(session_t *ps, xcb_atom_t atom) {
|
2019-03-10 08:34:37 -04:00
|
|
|
for (int p = 0; background_props_str[p]; p++) {
|
|
|
|
xcb_atom_t prop_atom = get_atom(ps, background_props_str[p]);
|
|
|
|
if (prop_atom == atom)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-12-21 18:44:42 -05:00
|
|
|
}
|
2018-12-30 02:06:47 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Synchronizes a X Render drawable to ensure all pending painting requests
|
|
|
|
* are completed.
|
|
|
|
*/
|
2019-02-03 10:59:31 -05:00
|
|
|
bool x_fence_sync(xcb_connection_t *c, xcb_sync_fence_t f) {
|
2019-03-10 08:34:37 -04:00
|
|
|
// TODO(richardgv): If everybody just follows the rules stated in X Sync
|
|
|
|
// prototype, we need only one fence per screen, but let's stay a bit
|
|
|
|
// cautious right now
|
|
|
|
|
|
|
|
auto e = xcb_request_check(c, xcb_sync_trigger_fence_checked(c, f));
|
|
|
|
if (e) {
|
|
|
|
log_error("Failed to trigger the fence.");
|
|
|
|
free(e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
e = xcb_request_check(c, xcb_sync_await_fence_checked(c, 1, &f));
|
|
|
|
if (e) {
|
|
|
|
log_error("Failed to await on a fence.");
|
|
|
|
free(e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
e = xcb_request_check(c, xcb_sync_reset_fence_checked(c, f));
|
|
|
|
if (e) {
|
|
|
|
log_error("Failed to reset the fence.");
|
|
|
|
free(e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-12-30 02:06:47 -05:00
|
|
|
}
|
2019-02-17 18:47:46 -05:00
|
|
|
|
|
|
|
// xcb-render specific macros
|
2019-03-10 08:34:37 -04:00
|
|
|
#define XFIXED_TO_DOUBLE(value) (((double)(value)) / 65536)
|
|
|
|
#define DOUBLE_TO_XFIXED(value) ((xcb_render_fixed_t)(((double)(value)) * 65536))
|
2019-02-17 18:47:46 -05:00
|
|
|
|
|
|
|
/**
|
2019-02-20 11:43:42 -05:00
|
|
|
* Convert a struct conv to a X picture convolution filter, normalizing the kernel
|
|
|
|
* in the process. Allow the caller to specify the element at the center of the kernel,
|
|
|
|
* for compatibility with legacy code.
|
2019-02-17 18:47:46 -05:00
|
|
|
*
|
2019-02-20 11:43:42 -05:00
|
|
|
* @param[in] kernel the convolution kernel
|
|
|
|
* @param[in] center the element to put at the center of the matrix
|
|
|
|
* @param[inout] ret pointer to an array of `size`, if `size` is too small, more space
|
|
|
|
* will be allocated, and `*ret` will be updated
|
|
|
|
* @param[inout] size size of the array pointed to by `ret`, in number of elements
|
|
|
|
* @return number of elements filled into `*ret`
|
2019-02-17 18:47:46 -05:00
|
|
|
*/
|
2019-03-30 05:07:21 -04:00
|
|
|
int x_picture_filter_from_conv(const conv *kernel, double center,
|
|
|
|
xcb_render_fixed_t **ret, size_t *size) {
|
2019-03-10 08:34:37 -04:00
|
|
|
if (*size < (size_t)(kernel->w * kernel->h + 2)) {
|
2019-03-30 05:07:21 -04:00
|
|
|
*size = (size_t)(kernel->w * kernel->h + 2);
|
2019-03-10 08:34:37 -04:00
|
|
|
*ret = crealloc(*ret, *size);
|
|
|
|
}
|
|
|
|
auto buf = *ret;
|
|
|
|
buf[0] = DOUBLE_TO_XFIXED(kernel->w);
|
|
|
|
buf[1] = DOUBLE_TO_XFIXED(kernel->h);
|
|
|
|
double sum = center;
|
|
|
|
for (int i = 0; i < kernel->w * kernel->h; i++) {
|
|
|
|
sum += kernel->data[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note for floating points a / b != a * (1 / b), but this shouldn't have any real
|
|
|
|
// impact on the result
|
|
|
|
double factor = sum != 0 ? 1.0 / sum : 1;
|
|
|
|
for (int i = 0; i < kernel->w * kernel->h; i++) {
|
|
|
|
buf[i + 2] = DOUBLE_TO_XFIXED(kernel->data[i] * factor);
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[kernel->h / 2 * kernel->w + kernel->w / 2 + 2] =
|
|
|
|
DOUBLE_TO_XFIXED(center * factor);
|
|
|
|
return kernel->w * kernel->h + 2;
|
2019-02-17 18:47:46 -05:00
|
|
|
}
|
2019-02-26 18:52:37 -05:00
|
|
|
|
|
|
|
/// Generate a search criteria for fbconfig from a X visual.
|
2019-03-30 05:07:21 -04:00
|
|
|
/// Returns {-1, -1, -1, -1, -1, 0} on failure
|
2019-03-10 08:34:37 -04:00
|
|
|
struct xvisual_info x_get_visual_info(xcb_connection_t *c, xcb_visualid_t visual) {
|
2019-02-26 18:52:37 -05:00
|
|
|
auto pictfmt = x_get_pictform_for_visual(c, visual);
|
|
|
|
auto depth = x_get_visual_depth(c, visual);
|
|
|
|
if (!pictfmt || depth == -1) {
|
|
|
|
log_error("Invalid visual %#03x", visual);
|
2019-03-30 05:07:21 -04:00
|
|
|
return (struct xvisual_info){-1, -1, -1, -1, -1, 0};
|
2019-02-26 18:52:37 -05:00
|
|
|
}
|
|
|
|
if (pictfmt->type != XCB_RENDER_PICT_TYPE_DIRECT) {
|
|
|
|
log_error("compton cannot handle non-DirectColor visuals. Report an "
|
|
|
|
"issue if you see this error message.");
|
2019-03-30 05:07:21 -04:00
|
|
|
return (struct xvisual_info){-1, -1, -1, -1, -1, 0};
|
2019-02-26 18:52:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int red_size = popcountl(pictfmt->direct.red_mask),
|
|
|
|
blue_size = popcountl(pictfmt->direct.blue_mask),
|
|
|
|
green_size = popcountl(pictfmt->direct.green_mask),
|
|
|
|
alpha_size = popcountl(pictfmt->direct.alpha_mask);
|
|
|
|
|
|
|
|
return (struct xvisual_info){
|
|
|
|
.red_size = red_size,
|
|
|
|
.green_size = green_size,
|
|
|
|
.blue_size = blue_size,
|
|
|
|
.alpha_size = alpha_size,
|
|
|
|
.visual_depth = depth,
|
|
|
|
.visual = visual,
|
|
|
|
};
|
|
|
|
}
|
2019-03-30 05:07:21 -04:00
|
|
|
|
|
|
|
xcb_screen_t *x_screen_of_display(xcb_connection_t *c, int screen) {
|
|
|
|
xcb_screen_iterator_t iter;
|
|
|
|
|
|
|
|
iter = xcb_setup_roots_iterator(xcb_get_setup(c));
|
|
|
|
for (; iter.rem; --screen, xcb_screen_next(&iter))
|
|
|
|
if (screen == 0)
|
|
|
|
return iter.data;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|