| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <stdalign.h> |
| 5 |
#include <stdbool.h> |
| 6 |
#include <stdlib.h> |
| 7 |
|
| 8 |
#include <X11/Xlib-xcb.h> |
| 9 |
#include <X11/Xutil.h> |
| 10 |
#include <X11/extensions/syncconst.h> |
| 11 |
#include <pixman.h> |
| 12 |
#include <xcb/composite.h> |
| 13 |
#include <xcb/damage.h> |
| 14 |
#include <xcb/glx.h> |
| 15 |
#include <xcb/present.h> |
| 16 |
#include <xcb/randr.h> |
| 17 |
#include <xcb/render.h> |
| 18 |
#include <xcb/shm.h> |
| 19 |
#include <xcb/sync.h> |
| 20 |
#include <xcb/xcb.h> |
| 21 |
#include <xcb/xcb_aux.h> |
| 22 |
#include <xcb/xcb_renderutil.h> |
| 23 |
#include <xcb/xcb_util.h> |
| 24 |
#include <xcb/xcbext.h> |
| 25 |
#include <xcb/xfixes.h> |
| 26 |
#include <xcb/xproto.h> |
| 27 |
|
| 28 |
#include "atom.h" |
| 29 |
#include "common.h" |
| 30 |
#include "compiler.h" |
| 31 |
#include "log.h" |
| 32 |
#include "region.h" |
| 33 |
#include "utils/kernel.h" |
| 34 |
#include "utils/misc.h" |
| 35 |
#include "x.h" |
| 36 |
|
| 37 |
static inline uint64_t x_widen_sequence(struct x_connection *c, uint32_t sequence) { |
| 38 |
if (sequence < c->last_sequence) { |
| 39 |
|
| 40 |
return (uint64_t)sequence + UINT32_MAX + 1; |
| 41 |
} |
| 42 |
return (uint64_t)sequence; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
static const char *x_error_code_to_string(struct x_connection *c, unsigned long serial, |
| 52 |
uint8_t major, uint16_t minor, uint8_t error_code) { |
| 53 |
|
| 54 |
int o = 0; |
| 55 |
const char *name = "Unknown"; |
| 56 |
|
| 57 |
#define CASESTRRET(s) \ |
| 58 |
case s: name = #s; break |
| 59 |
|
| 60 |
#define CASESTRRET2(s) \ |
| 61 |
case XCB_##s: name = #s; break |
| 62 |
|
| 63 |
o = error_code - c->e.fixes_error; |
| 64 |
switch (o) { CASESTRRET2(XFIXES_BAD_REGION); } |
| 65 |
|
| 66 |
o = error_code - c->e.damage_error; |
| 67 |
switch (o) { CASESTRRET2(DAMAGE_BAD_DAMAGE); } |
| 68 |
|
| 69 |
o = error_code - c->e.shm_error; |
| 70 |
switch (o) { CASESTRRET2(SHM_BAD_SEG); } |
| 71 |
|
| 72 |
o = error_code - c->e.render_error; |
| 73 |
switch (o) { |
| 74 |
CASESTRRET2(RENDER_PICT_FORMAT); |
| 75 |
CASESTRRET2(RENDER_PICTURE); |
| 76 |
CASESTRRET2(RENDER_PICT_OP); |
| 77 |
CASESTRRET2(RENDER_GLYPH_SET); |
| 78 |
CASESTRRET2(RENDER_GLYPH); |
| 79 |
} |
| 80 |
|
| 81 |
if (c->e.has_glx) { |
| 82 |
o = error_code - c->e.glx_error; |
| 83 |
switch (o) { |
| 84 |
CASESTRRET2(GLX_BAD_CONTEXT); |
| 85 |
CASESTRRET2(GLX_BAD_CONTEXT_STATE); |
| 86 |
CASESTRRET2(GLX_BAD_DRAWABLE); |
| 87 |
CASESTRRET2(GLX_BAD_PIXMAP); |
| 88 |
CASESTRRET2(GLX_BAD_CONTEXT_TAG); |
| 89 |
CASESTRRET2(GLX_BAD_CURRENT_WINDOW); |
| 90 |
CASESTRRET2(GLX_BAD_RENDER_REQUEST); |
| 91 |
CASESTRRET2(GLX_BAD_LARGE_REQUEST); |
| 92 |
CASESTRRET2(GLX_UNSUPPORTED_PRIVATE_REQUEST); |
| 93 |
CASESTRRET2(GLX_BAD_FB_CONFIG); |
| 94 |
CASESTRRET2(GLX_BAD_PBUFFER); |
| 95 |
CASESTRRET2(GLX_BAD_CURRENT_DRAWABLE); |
| 96 |
CASESTRRET2(GLX_BAD_WINDOW); |
| 97 |
CASESTRRET2(GLX_GLX_BAD_PROFILE_ARB); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
if (c->e.has_sync) { |
| 102 |
o = error_code - c->e.sync_error; |
| 103 |
switch (o) { |
| 104 |
CASESTRRET(XSyncBadCounter); |
| 105 |
CASESTRRET(XSyncBadAlarm); |
| 106 |
CASESTRRET(XSyncBadFence); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
switch (error_code) { |
| 111 |
CASESTRRET2(ACCESS); |
| 112 |
CASESTRRET2(ALLOC); |
| 113 |
CASESTRRET2(ATOM); |
| 114 |
CASESTRRET2(COLORMAP); |
| 115 |
CASESTRRET2(CURSOR); |
| 116 |
CASESTRRET2(DRAWABLE); |
| 117 |
CASESTRRET2(FONT); |
| 118 |
CASESTRRET2(G_CONTEXT); |
| 119 |
CASESTRRET2(ID_CHOICE); |
| 120 |
CASESTRRET2(IMPLEMENTATION); |
| 121 |
CASESTRRET2(LENGTH); |
| 122 |
CASESTRRET2(MATCH); |
| 123 |
CASESTRRET2(NAME); |
| 124 |
CASESTRRET2(PIXMAP); |
| 125 |
CASESTRRET2(REQUEST); |
| 126 |
CASESTRRET2(VALUE); |
| 127 |
CASESTRRET2(WINDOW); |
| 128 |
} |
| 129 |
|
| 130 |
#undef CASESTRRET |
| 131 |
#undef CASESTRRET2 |
| 132 |
|
| 133 |
thread_local static char buffer[256]; |
| 134 |
snprintf(buffer, sizeof(buffer), "X error %d %s request %d minor %d serial %lu", |
| 135 |
error_code, name, major, minor, serial); |
| 136 |
return buffer; |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
void x_print_error_impl(struct x_connection *c, unsigned long serial, uint8_t major, |
| 141 |
uint16_t minor, uint8_t error_code, const char *func) { |
| 142 |
if (unlikely(LOG_LEVEL_DEBUG >= log_get_level_tls())) { |
| 143 |
log_printf(tls_logger, LOG_LEVEL_DEBUG, func, "%s", |
| 144 |
x_error_code_to_string(c, serial, major, minor, error_code)); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
struct x_generic_async_request { |
| 149 |
struct x_async_request_base base; |
| 150 |
enum x_error_action error_action; |
| 151 |
const char *func; |
| 152 |
const char *file; |
| 153 |
int line; |
| 154 |
}; |
| 155 |
|
| 156 |
static void |
| 157 |
x_generic_async_callback(struct x_connection *c, struct x_async_request_base *req_base, |
| 158 |
const xcb_raw_generic_event_t *reply_or_error) { |
| 159 |
auto req = (struct x_generic_async_request *)req_base; |
| 160 |
auto error_action = req->error_action; |
| 161 |
auto func = req->func == NULL ? "(unknown)" : req->func; |
| 162 |
auto file = req->file == NULL ? "(unknown)" : req->file; |
| 163 |
auto line = req->line; |
| 164 |
free(req); |
| 165 |
|
| 166 |
if (reply_or_error == NULL || reply_or_error->response_type != 0) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
auto error = (xcb_generic_error_t *)reply_or_error; |
| 171 |
if (error_action != PENDING_REPLY_ACTION_IGNORE) { |
| 172 |
log_error("X error for request in %s at %s:%d: %s", func, file, line, |
| 173 |
x_error_code_to_string(c, error->full_sequence, error->major_code, |
| 174 |
error->minor_code, error->error_code)); |
| 175 |
} else { |
| 176 |
log_debug("Expected X error for request in %s at %s:%d: %s", func, file, line, |
| 177 |
x_error_code_to_string(c, error->full_sequence, error->major_code, |
| 178 |
error->minor_code, error->error_code)); |
| 179 |
} |
| 180 |
switch (error_action) { |
| 181 |
case PENDING_REPLY_ACTION_ABORT: |
| 182 |
log_fatal("An unrecoverable X error occurred, " |
| 183 |
"aborting..."); |
| 184 |
abort(); |
| 185 |
case PENDING_REPLY_ACTION_DEBUG_ABORT: assert(false); break; |
| 186 |
case PENDING_REPLY_ACTION_IGNORE: break; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
void x_set_error_action(struct x_connection *c, uint32_t sequence, enum x_error_action action, |
| 191 |
const char *func, const char *file, int line) { |
| 192 |
auto req = ccalloc(1, struct x_generic_async_request); |
| 193 |
req->func = func; |
| 194 |
req->file = file; |
| 195 |
req->line = line; |
| 196 |
req->error_action = action; |
| 197 |
req->base.sequence = sequence; |
| 198 |
req->base.callback = x_generic_async_callback; |
| 199 |
req->base.no_reply = true; |
| 200 |
x_await_request(c, &req->base); |
| 201 |
} |
| 202 |
|
| 203 |
static bool x_feed_event(struct x_connection *c, xcb_generic_event_t *e); |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
static int xerror(Display attr_unused *dpy, XErrorEvent *ev) { |
| 209 |
if (!ps_g) { |
| 210 |
|
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
xcb_generic_error_t xcb_err = {}; |
| 216 |
xcb_err.full_sequence = (uint32_t)ev->serial; |
| 217 |
xcb_err.major_code = ev->request_code; |
| 218 |
xcb_err.minor_code = ev->minor_code; |
| 219 |
xcb_err.error_code = ev->error_code; |
| 220 |
x_feed_event(&ps_g->c, (xcb_generic_event_t *)&xcb_err); |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
bool x_extensions_init(struct x_connection *c) { |
| 230 |
xcb_prefetch_extension_data(c->c, &xcb_composite_id); |
| 231 |
xcb_prefetch_extension_data(c->c, &xcb_damage_id); |
| 232 |
xcb_prefetch_extension_data(c->c, &xcb_xfixes_id); |
| 233 |
xcb_prefetch_extension_data(c->c, &xcb_glx_id); |
| 234 |
xcb_prefetch_extension_data(c->c, &xcb_present_id); |
| 235 |
xcb_prefetch_extension_data(c->c, &xcb_randr_id); |
| 236 |
xcb_prefetch_extension_data(c->c, &xcb_render_id); |
| 237 |
xcb_prefetch_extension_data(c->c, &xcb_shape_id); |
| 238 |
xcb_prefetch_extension_data(c->c, &xcb_sync_id); |
| 239 |
xcb_prefetch_extension_data(c->c, &xcb_shm_id); |
| 240 |
|
| 241 |
|
| 242 |
auto extension = xcb_get_extension_data(c->c, &xcb_composite_id); |
| 243 |
if (!extension || !extension->present) { |
| 244 |
log_fatal("The X server doesn't have the X Composite extension."); |
| 245 |
|
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
auto composite = xcb_composite_query_version_reply( |
| 251 |
c->c, |
| 252 |
xcb_composite_query_version(c->c, XCB_COMPOSITE_MAJOR_VERSION, |
| 253 |
XCB_COMPOSITE_MINOR_VERSION), |
| 254 |
NULL); |
| 255 |
if (!composite || (composite->major_version == 0 && composite->minor_version < 2)) { |
| 256 |
log_fatal("The X server doesn't support the X Composite extension v0.2."); |
| 257 |
|
| 258 |
if (composite) { |
| 259 |
free(composite); |
| 260 |
} |
| 261 |
|
| 262 |
return false; |
| 263 |
} |
| 264 |
|
| 265 |
free(composite); |
| 266 |
|
| 267 |
|
| 268 |
extension = xcb_get_extension_data(c->c, &xcb_damage_id); |
| 269 |
if (!extension || !extension->present) { |
| 270 |
log_fatal("The X server doesn't have the X Damage extension."); |
| 271 |
|
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
c->e.damage_event = extension->first_event; |
| 276 |
c->e.damage_error = extension->first_error; |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
xcb_discard_reply(c->c, xcb_damage_query_version(c->c, XCB_DAMAGE_MAJOR_VERSION, |
| 283 |
XCB_DAMAGE_MINOR_VERSION) |
| 284 |
.sequence); |
| 285 |
|
| 286 |
|
| 287 |
extension = xcb_get_extension_data(c->c, &xcb_xfixes_id); |
| 288 |
if (!extension || !extension->present) { |
| 289 |
log_fatal("The X server doesn't have the X Fixes extension."); |
| 290 |
|
| 291 |
return false; |
| 292 |
} |
| 293 |
|
| 294 |
c->e.fixes_error = extension->first_error; |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
xcb_discard_reply(c->c, xcb_xfixes_query_version(c->c, XCB_XFIXES_MAJOR_VERSION, |
| 300 |
XCB_XFIXES_MINOR_VERSION) |
| 301 |
.sequence); |
| 302 |
|
| 303 |
|
| 304 |
extension = xcb_get_extension_data(c->c, &xcb_shm_id); |
| 305 |
if (!extension || !extension->present) { |
| 306 |
log_fatal("This X server is missing the MIT-SHM extension."); |
| 307 |
return false; |
| 308 |
} |
| 309 |
|
| 310 |
c->e.shm_event = extension->first_event; |
| 311 |
c->e.shm_error = extension->first_error; |
| 312 |
|
| 313 |
auto shm_info = XCB_AWAIT(xcb_shm_query_version, c); |
| 314 |
if (!shm_info) { |
| 315 |
log_fatal("Failed to query version of the MIT-SHM."); |
| 316 |
return false; |
| 317 |
} |
| 318 |
if (shm_info->major_version < 1 || |
| 319 |
(shm_info->major_version == 1 && shm_info->minor_version < 2)) { |
| 320 |
log_fatal("This X server's MIT-SHM extension is too old (expected at " |
| 321 |
"least 1.2, got %d.%d).", |
| 322 |
shm_info->major_version, shm_info->minor_version); |
| 323 |
return false; |
| 324 |
} |
| 325 |
free(shm_info); |
| 326 |
|
| 327 |
|
| 328 |
extension = xcb_get_extension_data(c->c, &xcb_glx_id); |
| 329 |
if (extension && extension->present) { |
| 330 |
c->e.has_glx = true; |
| 331 |
c->e.glx_error = extension->first_error; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
extension = xcb_get_extension_data(c->c, &xcb_present_id); |
| 336 |
if (extension && extension->present) { |
| 337 |
c->e.has_present = true; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
extension = xcb_get_extension_data(c->c, &xcb_randr_id); |
| 342 |
if (extension && extension->present) { |
| 343 |
c->e.has_randr = true; |
| 344 |
c->e.randr_event = extension->first_event; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
extension = xcb_get_extension_data(c->c, &xcb_render_id); |
| 349 |
if (!extension || !extension->present) { |
| 350 |
log_fatal("The X server doesn't have the X Render extension."); |
| 351 |
|
| 352 |
return false; |
| 353 |
} |
| 354 |
|
| 355 |
c->e.render_error = extension->first_error; |
| 356 |
|
| 357 |
|
| 358 |
extension = xcb_get_extension_data(c->c, &xcb_shape_id); |
| 359 |
if (extension && extension->present) { |
| 360 |
c->e.has_shape = true; |
| 361 |
c->e.shape_event = extension->first_event; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
extension = xcb_get_extension_data(c->c, &xcb_sync_id); |
| 366 |
if (extension && extension->present) { |
| 367 |
|
| 368 |
auto sync = xcb_sync_initialize_reply( |
| 369 |
c->c, |
| 370 |
xcb_sync_initialize(c->c, XCB_SYNC_MAJOR_VERSION, XCB_SYNC_MINOR_VERSION), |
| 371 |
NULL); |
| 372 |
if (sync && (sync->major_version > 3 || |
| 373 |
(sync->major_version == 3 && sync->minor_version >= 1))) { |
| 374 |
c->e.has_sync = true; |
| 375 |
c->e.sync_event = extension->first_event; |
| 376 |
c->e.sync_error = extension->first_error; |
| 377 |
} |
| 378 |
|
| 379 |
if (sync) { |
| 380 |
free(sync); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
return true; |
| 385 |
} |
| 386 |
|
| 387 |
static void x_connection_init_inner(struct x_connection *c) { |
| 388 |
list_init_head(&c->pending_x_requests); |
| 389 |
c->previous_xerror_handler = XSetErrorHandler(xerror); |
| 390 |
|
| 391 |
c->screen_info = xcb_aux_get_screen(c->c, c->screen); |
| 392 |
|
| 393 |
|
| 394 |
auto cookie = xcb_get_input_focus(c->c); |
| 395 |
free(xcb_get_input_focus_reply(c->c, cookie, NULL)); |
| 396 |
c->last_sequence = cookie.sequence; |
| 397 |
} |
| 398 |
|
| 399 |
void x_connection_init(struct x_connection *c, Display *dpy) { |
| 400 |
c->dpy = dpy; |
| 401 |
c->c = XGetXCBConnection(dpy); |
| 402 |
c->screen = DefaultScreen(dpy); |
| 403 |
x_connection_init_inner(c); |
| 404 |
} |
| 405 |
|
| 406 |
void x_connection_init_xcb(struct x_connection *c, xcb_connection_t *conn, int screen) { |
| 407 |
c->c = conn; |
| 408 |
c->dpy = NULL; |
| 409 |
c->screen = screen; |
| 410 |
x_connection_init_inner(c); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
winprop_t x_get_prop_with_offset(const struct x_connection *c, xcb_window_t w, xcb_atom_t atom, |
| 429 |
int offset, int length, xcb_atom_t rtype, int rformat) { |
| 430 |
xcb_get_property_reply_t *r = xcb_get_property_reply( |
| 431 |
c->c, |
| 432 |
xcb_get_property(c->c, 0, w, atom, rtype, to_u32_checked(offset), |
| 433 |
to_u32_checked(length)), |
| 434 |
NULL); |
| 435 |
|
| 436 |
if (r && xcb_get_property_value_length(r) && |
| 437 |
(rtype == XCB_GET_PROPERTY_TYPE_ANY || r->type == rtype) && |
| 438 |
(!rformat || r->format == rformat) && |
| 439 |
(r->format == 8 || r->format == 16 || r->format == 32)) { |
| 440 |
auto len = xcb_get_property_value_length(r); |
| 441 |
return (winprop_t){ |
| 442 |
.ptr = xcb_get_property_value(r), |
| 443 |
.nitems = (ulong)(len / (r->format / 8)), |
| 444 |
.type = r->type, |
| 445 |
.format = r->format, |
| 446 |
.r = r, |
| 447 |
}; |
| 448 |
} |
| 449 |
|
| 450 |
free(r); |
| 451 |
return (winprop_t){ |
| 452 |
.ptr = NULL, .nitems = 0, .type = XCB_GET_PROPERTY_TYPE_ANY, .format = 0}; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
xcb_window_t wid_get_prop_window(struct x_connection *c, xcb_window_t wid, |
| 461 |
xcb_atom_t aprop, bool *exists) { |
| 462 |
|
| 463 |
xcb_window_t p = XCB_NONE; |
| 464 |
winprop_t prop = x_get_prop(c, wid, aprop, 1L, XCB_ATOM_WINDOW, 32); |
| 465 |
|
| 466 |
|
| 467 |
if (prop.nitems) { |
| 468 |
*exists = true; |
| 469 |
p = (xcb_window_t)*prop.p32; |
| 470 |
} else { |
| 471 |
*exists = false; |
| 472 |
} |
| 473 |
|
| 474 |
free_winprop(&prop); |
| 475 |
|
| 476 |
return p; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
bool wid_get_text_prop(struct x_connection *c, struct atom *atoms, xcb_window_t wid, |
| 483 |
xcb_atom_t prop, char ***pstrlst, int *pnstr) { |
| 484 |
xcb_generic_error_t *e = NULL; |
| 485 |
auto r = xcb_get_property_reply( |
| 486 |
c->c, xcb_get_property(c->c, 0, wid, prop, XCB_ATOM_ANY, 0, UINT_MAX), &e); |
| 487 |
if (!r) { |
| 488 |
log_debug_x_error(c, e, "Failed to get window property for %#010x", wid); |
| 489 |
free(e); |
| 490 |
return false; |
| 491 |
} |
| 492 |
|
| 493 |
if (r->type == XCB_ATOM_NONE) { |
| 494 |
free(r); |
| 495 |
return false; |
| 496 |
} |
| 497 |
|
| 498 |
if (!x_is_type_string(atoms, r->type)) { |
| 499 |
log_warn("Text property %d of window %#010x has unsupported type: %d", |
| 500 |
prop, wid, r->type); |
| 501 |
free(r); |
| 502 |
return false; |
| 503 |
} |
| 504 |
|
| 505 |
if (r->format != 8) { |
| 506 |
log_warn("Text property %d of window %#010x has unexpected format: %d", |
| 507 |
prop, wid, r->format); |
| 508 |
free(r); |
| 509 |
return false; |
| 510 |
} |
| 511 |
|
| 512 |
uint32_t length = to_u32_checked(xcb_get_property_value_length(r)); |
| 513 |
void *data = xcb_get_property_value(r); |
| 514 |
unsigned int nstr = 0; |
| 515 |
uint32_t current_offset = 0; |
| 516 |
while (current_offset < length) { |
| 517 |
current_offset += |
| 518 |
(uint32_t)strnlen(data + current_offset, length - current_offset) + 1; |
| 519 |
nstr += 1; |
| 520 |
} |
| 521 |
|
| 522 |
if (nstr == 0) { |
| 523 |
|
| 524 |
|
| 525 |
char **strlst = malloc(sizeof(char *)); |
| 526 |
strlst[0] = ""; |
| 527 |
*pnstr = 1; |
| 528 |
*pstrlst = strlst; |
| 529 |
free(r); |
| 530 |
return true; |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
void *buf = NULL; |
| 535 |
if (posix_memalign(&buf, alignof(char *), length + sizeof(char *) * nstr + 1) != 0) { |
| 536 |
abort(); |
| 537 |
} |
| 538 |
|
| 539 |
char *strlst = buf + sizeof(char *) * nstr; |
| 540 |
memcpy(strlst, xcb_get_property_value(r), length); |
| 541 |
strlst[length] = '\0'; |
| 542 |
|
| 543 |
char **ret = buf; |
| 544 |
current_offset = 0; |
| 545 |
nstr = 0; |
| 546 |
while (current_offset < length) { |
| 547 |
ret[nstr] = strlst + current_offset; |
| 548 |
current_offset += (uint32_t)strlen(strlst + current_offset) + 1; |
| 549 |
nstr += 1; |
| 550 |
} |
| 551 |
|
| 552 |
*pnstr = to_int_checked(nstr); |
| 553 |
*pstrlst = ret; |
| 554 |
free(r); |
| 555 |
return true; |
| 556 |
} |
| 557 |
|
| 558 |
bool wid_get_opacity_prop(struct x_connection *c, struct atom *atoms, xcb_window_t wid, |
| 559 |
opacity_t def, opacity_t *out) { |
| 560 |
bool ret = false; |
| 561 |
*out = def; |
| 562 |
|
| 563 |
winprop_t prop = |
| 564 |
x_get_prop(c, wid, atoms->a_NET_WM_WINDOW_OPACITY, 1L, XCB_ATOM_CARDINAL, 32); |
| 565 |
|
| 566 |
if (prop.nitems) { |
| 567 |
*out = *prop.c32; |
| 568 |
ret = true; |
| 569 |
} |
| 570 |
|
| 571 |
free_winprop(&prop); |
| 572 |
|
| 573 |
return ret; |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
static thread_local xcb_render_query_pict_formats_reply_t *g_pictfmts = NULL; |
| 579 |
|
| 580 |
static inline void x_get_server_pictfmts(struct x_connection *c) { |
| 581 |
if (g_pictfmts) { |
| 582 |
return; |
| 583 |
} |
| 584 |
xcb_generic_error_t *e = NULL; |
| 585 |
|
| 586 |
g_pictfmts = xcb_render_query_pict_formats_reply( |
| 587 |
c->c, xcb_render_query_pict_formats(c->c), &e); |
| 588 |
if (e || !g_pictfmts) { |
| 589 |
log_fatal("failed to get pict formats\n"); |
| 590 |
abort(); |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
const xcb_render_pictforminfo_t * |
| 595 |
x_get_pictform_for_visual(struct x_connection *c, xcb_visualid_t visual) { |
| 596 |
x_get_server_pictfmts(c); |
| 597 |
|
| 598 |
xcb_render_pictvisual_t *pv = xcb_render_util_find_visual_format(g_pictfmts, visual); |
| 599 |
for (xcb_render_pictforminfo_iterator_t i = |
| 600 |
xcb_render_query_pict_formats_formats_iterator(g_pictfmts); |
| 601 |
i.rem; xcb_render_pictforminfo_next(&i)) { |
| 602 |
if (i.data->id == pv->format) { |
| 603 |
return i.data; |
| 604 |
} |
| 605 |
} |
| 606 |
return NULL; |
| 607 |
} |
| 608 |
|
| 609 |
static xcb_visualid_t attr_pure x_get_visual_for_pictfmt(xcb_render_query_pict_formats_reply_t *r, |
| 610 |
xcb_render_pictformat_t fmt) { |
| 611 |
for (auto screen = xcb_render_query_pict_formats_screens_iterator(r); screen.rem; |
| 612 |
xcb_render_pictscreen_next(&screen)) { |
| 613 |
for (auto depth = xcb_render_pictscreen_depths_iterator(screen.data); |
| 614 |
depth.rem; xcb_render_pictdepth_next(&depth)) { |
| 615 |
for (auto pv = xcb_render_pictdepth_visuals_iterator(depth.data); |
| 616 |
pv.rem; xcb_render_pictvisual_next(&pv)) { |
| 617 |
if (pv.data->format == fmt) { |
| 618 |
return pv.data->visual; |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
return XCB_NONE; |
| 624 |
} |
| 625 |
|
| 626 |
xcb_visualid_t x_get_visual_for_standard(struct x_connection *c, xcb_pict_standard_t std) { |
| 627 |
x_get_server_pictfmts(c); |
| 628 |
|
| 629 |
auto pictfmt = xcb_render_util_find_standard_format(g_pictfmts, std); |
| 630 |
|
| 631 |
return x_get_visual_for_pictfmt(g_pictfmts, pictfmt->id); |
| 632 |
} |
| 633 |
|
| 634 |
xcb_visualid_t x_get_visual_for_depth(xcb_screen_t *screen, uint8_t depth) { |
| 635 |
xcb_depth_iterator_t depth_it = xcb_screen_allowed_depths_iterator(screen); |
| 636 |
for (; depth_it.rem; xcb_depth_next(&depth_it)) { |
| 637 |
if (depth_it.data->depth == depth) { |
| 638 |
return xcb_depth_visuals_iterator(depth_it.data).data->visual_id; |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
return XCB_NONE; |
| 643 |
} |
| 644 |
|
| 645 |
xcb_render_pictformat_t |
| 646 |
x_get_pictfmt_for_standard(struct x_connection *c, xcb_pict_standard_t std) { |
| 647 |
x_get_server_pictfmts(c); |
| 648 |
|
| 649 |
auto pictfmt = xcb_render_util_find_standard_format(g_pictfmts, std); |
| 650 |
|
| 651 |
return pictfmt->id; |
| 652 |
} |
| 653 |
|
| 654 |
xcb_render_picture_t |
| 655 |
x_create_picture_with_pictfmt_and_pixmap(struct x_connection *c, xcb_render_pictformat_t pictfmt, |
| 656 |
xcb_pixmap_t pixmap, uint32_t valuemask, |
| 657 |
const xcb_render_create_picture_value_list_t *attr) { |
| 658 |
void *buf = NULL; |
| 659 |
if (attr) { |
| 660 |
xcb_render_create_picture_value_list_serialize(&buf, valuemask, attr); |
| 661 |
if (!buf) { |
| 662 |
log_error("failed to serialize picture attributes"); |
| 663 |
return XCB_NONE; |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
xcb_render_picture_t tmp_picture = x_new_id(c); |
| 668 |
xcb_generic_error_t *e = |
| 669 |
xcb_request_check(c->c, xcb_render_create_picture_checked( |
| 670 |
c->c, tmp_picture, pixmap, pictfmt, valuemask, buf)); |
| 671 |
free(buf); |
| 672 |
if (e) { |
| 673 |
log_error_x_error(c, e, "failed to create picture"); |
| 674 |
free(e); |
| 675 |
abort(); |
| 676 |
return XCB_NONE; |
| 677 |
} |
| 678 |
return tmp_picture; |
| 679 |
} |
| 680 |
|
| 681 |
xcb_render_picture_t |
| 682 |
x_create_picture_with_visual_and_pixmap(struct x_connection *c, xcb_visualid_t visual, |
| 683 |
xcb_pixmap_t pixmap, uint32_t valuemask, |
| 684 |
const xcb_render_create_picture_value_list_t *attr) { |
| 685 |
const xcb_render_pictforminfo_t *pictfmt = x_get_pictform_for_visual(c, visual); |
| 686 |
return x_create_picture_with_pictfmt_and_pixmap(c, pictfmt->id, pixmap, valuemask, attr); |
| 687 |
} |
| 688 |
|
| 689 |
xcb_render_picture_t |
| 690 |
x_create_picture_with_standard_and_pixmap(struct x_connection *c, xcb_pict_standard_t standard, |
| 691 |
xcb_pixmap_t pixmap, uint32_t valuemask, |
| 692 |
const xcb_render_create_picture_value_list_t *attr) { |
| 693 |
x_get_server_pictfmts(c); |
| 694 |
|
| 695 |
auto pictfmt = xcb_render_util_find_standard_format(g_pictfmts, standard); |
| 696 |
assert(pictfmt); |
| 697 |
return x_create_picture_with_pictfmt_and_pixmap(c, pictfmt->id, pixmap, valuemask, attr); |
| 698 |
} |
| 699 |
|
| 700 |
xcb_render_picture_t |
| 701 |
x_create_picture_with_standard(struct x_connection *c, int w, int h, |
| 702 |
xcb_pict_standard_t standard, uint32_t valuemask, |
| 703 |
const xcb_render_create_picture_value_list_t *attr) { |
| 704 |
x_get_server_pictfmts(c); |
| 705 |
|
| 706 |
auto pictfmt = xcb_render_util_find_standard_format(g_pictfmts, standard); |
| 707 |
assert(pictfmt); |
| 708 |
return x_create_picture_with_pictfmt(c, w, h, pictfmt->id, pictfmt->depth, |
| 709 |
valuemask, attr); |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
xcb_render_picture_t |
| 716 |
x_create_picture_with_pictfmt(struct x_connection *c, int w, int h, |
| 717 |
xcb_render_pictformat_t pictfmt, uint8_t depth, uint32_t valuemask, |
| 718 |
const xcb_render_create_picture_value_list_t *attr) { |
| 719 |
|
| 720 |
xcb_pixmap_t tmp_pixmap = x_create_pixmap(c, depth, w, h); |
| 721 |
if (!tmp_pixmap) { |
| 722 |
return XCB_NONE; |
| 723 |
} |
| 724 |
|
| 725 |
xcb_render_picture_t picture = x_create_picture_with_pictfmt_and_pixmap( |
| 726 |
c, pictfmt, tmp_pixmap, valuemask, attr); |
| 727 |
|
| 728 |
x_set_error_action_abort(c, xcb_free_pixmap(c->c, tmp_pixmap)); |
| 729 |
|
| 730 |
return picture; |
| 731 |
} |
| 732 |
|
| 733 |
xcb_render_picture_t |
| 734 |
x_create_picture_with_visual(struct x_connection *c, int w, int h, xcb_visualid_t visual, |
| 735 |
uint32_t valuemask, |
| 736 |
const xcb_render_create_picture_value_list_t *attr) { |
| 737 |
auto pictfmt = x_get_pictform_for_visual(c, visual); |
| 738 |
return x_create_picture_with_pictfmt(c, w, h, pictfmt->id, pictfmt->depth, |
| 739 |
valuemask, attr); |
| 740 |
} |
| 741 |
|
| 742 |
bool x_fetch_region(struct x_connection *c, xcb_xfixes_region_t r, pixman_region32_t *res) { |
| 743 |
xcb_generic_error_t *e = NULL; |
| 744 |
xcb_xfixes_fetch_region_reply_t *xr = |
| 745 |
xcb_xfixes_fetch_region_reply(c->c, xcb_xfixes_fetch_region(c->c, r), &e); |
| 746 |
if (!xr) { |
| 747 |
log_error_x_error(c, e, "Failed to fetch rectangles"); |
| 748 |
return false; |
| 749 |
} |
| 750 |
|
| 751 |
int nrect = xcb_xfixes_fetch_region_rectangles_length(xr); |
| 752 |
auto b = ccalloc(nrect, pixman_box32_t); |
| 753 |
xcb_rectangle_t *xrect = xcb_xfixes_fetch_region_rectangles(xr); |
| 754 |
for (int i = 0; i < nrect; i++) { |
| 755 |
b[i] = (pixman_box32_t){.x1 = xrect[i].x, |
| 756 |
.y1 = xrect[i].y, |
| 757 |
.x2 = xrect[i].x + xrect[i].width, |
| 758 |
.y2 = xrect[i].y + xrect[i].height}; |
| 759 |
} |
| 760 |
bool ret = pixman_region32_init_rects(res, b, nrect); |
| 761 |
free(b); |
| 762 |
free(xr); |
| 763 |
return ret; |
| 764 |
} |
| 765 |
|
| 766 |
bool x_set_region(struct x_connection *c, xcb_xfixes_region_t dst, const region_t *src) { |
| 767 |
if (!src || dst == XCB_NONE) { |
| 768 |
return false; |
| 769 |
} |
| 770 |
|
| 771 |
int32_t nrects = 0; |
| 772 |
const rect_t *rects = pixman_region32_rectangles((region_t *)src, &nrects); |
| 773 |
if (!rects || nrects < 1) { |
| 774 |
return false; |
| 775 |
} |
| 776 |
|
| 777 |
xcb_rectangle_t *xrects = ccalloc(nrects, xcb_rectangle_t); |
| 778 |
for (int32_t i = 0; i < nrects; i++) { |
| 779 |
xrects[i] = |
| 780 |
(xcb_rectangle_t){.x = to_i16_checked(rects[i].x1), |
| 781 |
.y = to_i16_checked(rects[i].y1), |
| 782 |
.width = to_u16_checked(rects[i].x2 - rects[i].x1), |
| 783 |
.height = to_u16_checked(rects[i].y2 - rects[i].y1)}; |
| 784 |
} |
| 785 |
|
| 786 |
bool success = |
| 787 |
XCB_AWAIT_VOID(xcb_xfixes_set_region, c, dst, to_u32_checked(nrects), xrects); |
| 788 |
|
| 789 |
free(xrects); |
| 790 |
|
| 791 |
return success; |
| 792 |
} |
| 793 |
|
| 794 |
uint32_t x_create_region(struct x_connection *c, const region_t *reg) { |
| 795 |
if (!reg) { |
| 796 |
return XCB_NONE; |
| 797 |
} |
| 798 |
|
| 799 |
int nrects; |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
const pixman_box32_t *rects = pixman_region32_rectangles((region_t *)reg, &nrects); |
| 804 |
auto xrects = ccalloc(nrects, xcb_rectangle_t); |
| 805 |
for (int i = 0; i < nrects; i++) { |
| 806 |
xrects[i] = |
| 807 |
(xcb_rectangle_t){.x = to_i16_checked(rects[i].x1), |
| 808 |
.y = to_i16_checked(rects[i].y1), |
| 809 |
.width = to_u16_checked(rects[i].x2 - rects[i].x1), |
| 810 |
.height = to_u16_checked(rects[i].y2 - rects[i].y1)}; |
| 811 |
} |
| 812 |
|
| 813 |
xcb_xfixes_region_t ret = x_new_id(c); |
| 814 |
bool success = |
| 815 |
XCB_AWAIT_VOID(xcb_xfixes_create_region, c, ret, to_u32_checked(nrects), xrects); |
| 816 |
free(xrects); |
| 817 |
if (!success) { |
| 818 |
return XCB_NONE; |
| 819 |
} |
| 820 |
return ret; |
| 821 |
} |
| 822 |
|
| 823 |
void x_async_change_window_attributes(struct x_connection *c, xcb_window_t wid, |
| 824 |
uint32_t mask, const uint32_t *values, |
| 825 |
struct x_async_request_base *req) { |
| 826 |
req->sequence = xcb_change_window_attributes(c->c, wid, mask, values).sequence; |
| 827 |
req->no_reply = true; |
| 828 |
x_await_request(c, req); |
| 829 |
} |
| 830 |
|
| 831 |
void x_async_query_tree(struct x_connection *c, xcb_window_t wid, |
| 832 |
struct x_async_request_base *req) { |
| 833 |
req->sequence = xcb_query_tree(c->c, wid).sequence; |
| 834 |
x_await_request(c, req); |
| 835 |
} |
| 836 |
|
| 837 |
void x_async_get_property(struct x_connection *c, xcb_window_t wid, xcb_atom_t atom, |
| 838 |
xcb_atom_t type, uint32_t long_offset, uint32_t long_length, |
| 839 |
struct x_async_request_base *req) { |
| 840 |
req->sequence = |
| 841 |
xcb_get_property(c->c, 0, wid, atom, type, long_offset, long_length).sequence; |
| 842 |
x_await_request(c, req); |
| 843 |
} |
| 844 |
|
| 845 |
void x_destroy_region(struct x_connection *c, xcb_xfixes_region_t r) { |
| 846 |
if (r != XCB_NONE) { |
| 847 |
x_set_error_action_debug_abort(c, xcb_xfixes_destroy_region(c->c, r)); |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
void x_set_picture_clip_region(struct x_connection *c, xcb_render_picture_t pict, |
| 852 |
int16_t clip_x_origin, int16_t clip_y_origin, |
| 853 |
const region_t *reg) { |
| 854 |
int nrects; |
| 855 |
const rect_t *rects = pixman_region32_rectangles((region_t *)reg, &nrects); |
| 856 |
auto xrects = ccalloc(nrects, xcb_rectangle_t); |
| 857 |
for (int i = 0; i < nrects; i++) { |
| 858 |
xrects[i] = (xcb_rectangle_t){ |
| 859 |
.x = to_i16_checked(rects[i].x1), |
| 860 |
.y = to_i16_checked(rects[i].y1), |
| 861 |
.width = to_u16_checked(rects[i].x2 - rects[i].x1), |
| 862 |
.height = to_u16_checked(rects[i].y2 - rects[i].y1), |
| 863 |
}; |
| 864 |
} |
| 865 |
|
| 866 |
xcb_generic_error_t *e = |
| 867 |
xcb_request_check(c->c, xcb_render_set_picture_clip_rectangles_checked( |
| 868 |
c->c, pict, clip_x_origin, clip_y_origin, |
| 869 |
to_u32_checked(nrects), xrects)); |
| 870 |
if (e) { |
| 871 |
log_error_x_error(c, e, "Failed to set clip region"); |
| 872 |
free(e); |
| 873 |
} |
| 874 |
free(xrects); |
| 875 |
} |
| 876 |
|
| 877 |
void x_clear_picture_clip_region(struct x_connection *c, xcb_render_picture_t pict) { |
| 878 |
assert(pict != XCB_NONE); |
| 879 |
xcb_render_change_picture_value_list_t v = {.clipmask = XCB_NONE}; |
| 880 |
xcb_generic_error_t *e = xcb_request_check( |
| 881 |
c->c, xcb_render_change_picture_checked(c->c, pict, XCB_RENDER_CP_CLIP_MASK, &v)); |
| 882 |
if (e) { |
| 883 |
log_error_x_error(c, e, "failed to clear clip region"); |
| 884 |
free(e); |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
void x_free_picture(struct x_connection *c, xcb_render_picture_t p) { |
| 894 |
assert(p != XCB_NONE); |
| 895 |
auto cookie = xcb_render_free_picture(c->c, p); |
| 896 |
x_set_error_action_debug_abort(c, cookie); |
| 897 |
} |
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
const char *x_strerror(struct x_connection *c, const xcb_generic_error_t *e) { |
| 906 |
if (!e) { |
| 907 |
return "No error"; |
| 908 |
} |
| 909 |
return x_error_code_to_string(c, e->full_sequence, e->major_code, e->minor_code, |
| 910 |
e->error_code); |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
xcb_pixmap_t x_create_pixmap(struct x_connection *c, uint8_t depth, int width, int height) { |
| 917 |
xcb_pixmap_t pix = x_new_id(c); |
| 918 |
xcb_void_cookie_t cookie = |
| 919 |
xcb_create_pixmap_checked(c->c, depth, pix, c->screen_info->root, |
| 920 |
to_u16_checked(width), to_u16_checked(height)); |
| 921 |
xcb_generic_error_t *err = xcb_request_check(c->c, cookie); |
| 922 |
if (err == NULL) { |
| 923 |
return pix; |
| 924 |
} |
| 925 |
|
| 926 |
log_error_x_error(c, err, "Failed to create pixmap"); |
| 927 |
free(err); |
| 928 |
return XCB_NONE; |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
xcb_pixmap_t x_get_root_back_pixmap(struct x_connection *c, struct atom *atoms) { |
| 944 |
xcb_pixmap_t pixmap = XCB_NONE; |
| 945 |
|
| 946 |
xcb_atom_t root_back_pixmap_atoms[] = {atoms->a_XROOTPMAP_ID, atoms->aESETROOT_PMAP_ID}; |
| 947 |
for (size_t i = 0; i < ARR_SIZE(root_back_pixmap_atoms); i++) { |
| 948 |
winprop_t prop = |
| 949 |
x_get_prop(c, c->screen_info->root, root_back_pixmap_atoms[i], 1, |
| 950 |
XCB_ATOM_PIXMAP, 32); |
| 951 |
if (prop.nitems) { |
| 952 |
pixmap = (xcb_pixmap_t)*prop.p32; |
| 953 |
free_winprop(&prop); |
| 954 |
break; |
| 955 |
} |
| 956 |
free_winprop(&prop); |
| 957 |
} |
| 958 |
|
| 959 |
return pixmap; |
| 960 |
} |
| 961 |
|
| 962 |
bool x_is_root_back_pixmap_atom(struct atom *atoms, xcb_atom_t atom) { |
| 963 |
return atom == atoms->a_XROOTPMAP_ID || atom == atoms->aESETROOT_PMAP_ID || |
| 964 |
atom == atoms->a_XSETROOT_ID; |
| 965 |
} |
| 966 |
|
| 967 |
void x_request_vblank_event(struct x_connection *c, xcb_window_t window, uint64_t msc) { |
| 968 |
auto cookie = xcb_present_notify_msc(c->c, window, 0, msc, 1, 0); |
| 969 |
x_set_error_action_abort(c, cookie); |
| 970 |
} |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
void x_create_convolution_kernel(const conv *kernel, double center, |
| 985 |
struct x_convolution_kernel **ret) { |
| 986 |
assert(ret); |
| 987 |
if (!*ret || (*ret)->capacity < kernel->w * kernel->h + 2) { |
| 988 |
free(*ret); |
| 989 |
*ret = |
| 990 |
cvalloc(sizeof(struct x_convolution_kernel) + |
| 991 |
(size_t)(kernel->w * kernel->h + 2) * sizeof(xcb_render_fixed_t)); |
| 992 |
(*ret)->capacity = kernel->w * kernel->h + 2; |
| 993 |
} |
| 994 |
|
| 995 |
(*ret)->size = kernel->w * kernel->h + 2; |
| 996 |
|
| 997 |
auto buf = (*ret)->kernel; |
| 998 |
buf[0] = DOUBLE_TO_XFIXED(kernel->w); |
| 999 |
buf[1] = DOUBLE_TO_XFIXED(kernel->h); |
| 1000 |
|
| 1001 |
double sum = center; |
| 1002 |
bool has_neg = false; |
| 1003 |
for (int i = 0; i < kernel->w * kernel->h; i++) { |
| 1004 |
if (i == kernel->w * kernel->h / 2) { |
| 1005 |
|
| 1006 |
continue; |
| 1007 |
} |
| 1008 |
sum += kernel->data[i]; |
| 1009 |
if (kernel->data[i] < 0 && !has_neg) { |
| 1010 |
has_neg = true; |
| 1011 |
log_warn("A X convolution kernel with negative values may not " |
| 1012 |
"work properly."); |
| 1013 |
} |
| 1014 |
} |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
double factor = sum != 0 ? 1.0 / sum : 1; |
| 1019 |
for (int i = 0; i < kernel->w * kernel->h; i++) { |
| 1020 |
buf[i + 2] = DOUBLE_TO_XFIXED(kernel->data[i] * factor); |
| 1021 |
} |
| 1022 |
|
| 1023 |
buf[kernel->h / 2 * kernel->w + kernel->w / 2 + 2] = |
| 1024 |
DOUBLE_TO_XFIXED(center * factor); |
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
struct xvisual_info x_get_visual_info(struct x_connection *c, xcb_visualid_t visual) { |
| 1030 |
auto pictfmt = x_get_pictform_for_visual(c, visual); |
| 1031 |
auto depth = xcb_aux_get_depth_of_visual(c->screen_info, visual); |
| 1032 |
if (!pictfmt || depth == 0) { |
| 1033 |
log_error("Invalid visual %#03x", visual); |
| 1034 |
return (struct xvisual_info){-1, -1, -1, -1, -1, 0}; |
| 1035 |
} |
| 1036 |
if (pictfmt->type != XCB_RENDER_PICT_TYPE_DIRECT) { |
| 1037 |
log_error("We cannot handle non-DirectColor visuals. Report an " |
| 1038 |
"issue if you see this error message."); |
| 1039 |
return (struct xvisual_info){-1, -1, -1, -1, -1, 0}; |
| 1040 |
} |
| 1041 |
|
| 1042 |
int red_size = popcntul(pictfmt->direct.red_mask), |
| 1043 |
blue_size = popcntul(pictfmt->direct.blue_mask), |
| 1044 |
green_size = popcntul(pictfmt->direct.green_mask), |
| 1045 |
alpha_size = popcntul(pictfmt->direct.alpha_mask); |
| 1046 |
|
| 1047 |
return (struct xvisual_info){ |
| 1048 |
.red_size = red_size, |
| 1049 |
.green_size = green_size, |
| 1050 |
.blue_size = blue_size, |
| 1051 |
.alpha_size = alpha_size, |
| 1052 |
.visual_depth = depth, |
| 1053 |
.visual = visual, |
| 1054 |
}; |
| 1055 |
} |
| 1056 |
|
| 1057 |
struct x_update_monitors_request { |
| 1058 |
struct x_async_request_base base; |
| 1059 |
struct x_monitors *monitors; |
| 1060 |
}; |
| 1061 |
|
| 1062 |
static void x_handle_update_monitors_reply(struct x_connection *c, |
| 1063 |
struct x_async_request_base *req_base, |
| 1064 |
const xcb_raw_generic_event_t *reply_or_error) { |
| 1065 |
auto m = ((struct x_update_monitors_request *)req_base)->monitors; |
| 1066 |
free(req_base); |
| 1067 |
|
| 1068 |
if (reply_or_error == NULL) { |
| 1069 |
|
| 1070 |
return; |
| 1071 |
} |
| 1072 |
|
| 1073 |
if (reply_or_error->response_type == 0) { |
| 1074 |
log_warn("Failed to get monitor information using RandR: %s", |
| 1075 |
x_strerror(c, (xcb_generic_error_t *)reply_or_error)); |
| 1076 |
return; |
| 1077 |
} |
| 1078 |
|
| 1079 |
x_free_monitor_info(m); |
| 1080 |
|
| 1081 |
auto reply = (const xcb_randr_get_monitors_reply_t *)reply_or_error; |
| 1082 |
|
| 1083 |
m->count = xcb_randr_get_monitors_monitors_length(reply); |
| 1084 |
m->regions = ccalloc(m->count, region_t); |
| 1085 |
xcb_randr_monitor_info_iterator_t monitor_info_it = |
| 1086 |
xcb_randr_get_monitors_monitors_iterator(reply); |
| 1087 |
for (int i = 0; monitor_info_it.rem; xcb_randr_monitor_info_next(&monitor_info_it)) { |
| 1088 |
xcb_randr_monitor_info_t *mi = monitor_info_it.data; |
| 1089 |
pixman_region32_init_rect(&m->regions[i++], mi->x, mi->y, mi->width, mi->height); |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
void x_update_monitors_async(struct x_connection *c, struct x_monitors *m) { |
| 1094 |
auto req = ccalloc(1, struct x_update_monitors_request); |
| 1095 |
req->base.callback = x_handle_update_monitors_reply; |
| 1096 |
req->base.sequence = xcb_randr_get_monitors(c->c, c->screen_info->root, 1).sequence; |
| 1097 |
req->monitors = m; |
| 1098 |
x_await_request(c, &req->base); |
| 1099 |
} |
| 1100 |
|
| 1101 |
void x_free_monitor_info(struct x_monitors *m) { |
| 1102 |
if (m->regions) { |
| 1103 |
for (int i = 0; i < m->count; i++) { |
| 1104 |
pixman_region32_fini(&m->regions[i]); |
| 1105 |
} |
| 1106 |
free(m->regions); |
| 1107 |
m->regions = NULL; |
| 1108 |
} |
| 1109 |
m->count = 0; |
| 1110 |
} |
| 1111 |
|
| 1112 |
static inline void x_ingest_event(struct x_connection *c, xcb_generic_event_t *event) { |
| 1113 |
if (event != NULL) { |
| 1114 |
assert(event->response_type != 1); |
| 1115 |
c->last_sequence = event->full_sequence; |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
static const xcb_raw_generic_event_t no_reply_success = {.response_type = 1}; |
| 1120 |
|
| 1121 |
|
| 1122 |
static void x_complete_async_requests(struct x_connection *c, xcb_generic_event_t *e) { |
| 1123 |
auto seq = x_widen_sequence(c, e->full_sequence); |
| 1124 |
list_foreach_safe(struct x_async_request_base, i, &c->pending_x_requests, siblings) { |
| 1125 |
auto head_seq = x_widen_sequence(c, i->sequence); |
| 1126 |
if (head_seq > seq) { |
| 1127 |
break; |
| 1128 |
} |
| 1129 |
if (head_seq == seq && e->response_type == 0) { |
| 1130 |
|
| 1131 |
break; |
| 1132 |
} |
| 1133 |
auto reply_or_error = &no_reply_success; |
| 1134 |
if (!i->no_reply) { |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
xcb_generic_error_t *err = NULL; |
| 1140 |
auto has_reply = xcb_poll_for_reply( |
| 1141 |
c->c, i->sequence, (void **)&reply_or_error, &err); |
| 1142 |
BUG_ON(has_reply == 0); |
| 1143 |
if (reply_or_error == NULL) { |
| 1144 |
reply_or_error = (xcb_raw_generic_event_t *)err; |
| 1145 |
} |
| 1146 |
} |
| 1147 |
c->latest_completed_request = i->sequence; |
| 1148 |
list_remove(&i->siblings); |
| 1149 |
i->callback(c, i, reply_or_error); |
| 1150 |
if (reply_or_error != &no_reply_success) { |
| 1151 |
free((void *)reply_or_error); |
| 1152 |
} |
| 1153 |
} |
| 1154 |
} |
| 1155 |
|
| 1156 |
static bool x_feed_event(struct x_connection *c, xcb_generic_event_t *e) { |
| 1157 |
x_complete_async_requests(c, e); |
| 1158 |
x_ingest_event(c, e); |
| 1159 |
|
| 1160 |
if (e->response_type != 0) { |
| 1161 |
return true; |
| 1162 |
} |
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
struct x_async_request_base *head = NULL; |
| 1167 |
xcb_generic_error_t *error = (xcb_generic_error_t *)e; |
| 1168 |
if (!list_is_empty(&c->pending_x_requests)) { |
| 1169 |
head = list_entry(c->pending_x_requests.next, struct x_async_request_base, |
| 1170 |
siblings); |
| 1171 |
} |
| 1172 |
if (head != NULL && error->full_sequence == head->sequence) { |
| 1173 |
|
| 1174 |
c->latest_completed_request = head->sequence; |
| 1175 |
list_remove(&head->siblings); |
| 1176 |
head->callback(c, head, (xcb_raw_generic_event_t *)e); |
| 1177 |
} else { |
| 1178 |
log_warn("Stray X error: %s", |
| 1179 |
x_error_code_to_string(c, error->full_sequence, error->major_code, |
| 1180 |
error->minor_code, error->error_code)); |
| 1181 |
} |
| 1182 |
return false; |
| 1183 |
} |
| 1184 |
|
| 1185 |
bool x_prepare_for_sleep(struct x_connection *c) { |
| 1186 |
if (!list_is_empty(&c->pending_x_requests)) { |
| 1187 |
auto last = list_entry(c->pending_x_requests.prev, |
| 1188 |
struct x_async_request_base, siblings); |
| 1189 |
if (c->event_sync != last->sequence) { |
| 1190 |
|
| 1191 |
|
| 1192 |
auto cookie = xcb_free_pixmap(c->c, XCB_NONE); |
| 1193 |
c->event_sync = cookie.sequence; |
| 1194 |
x_set_error_action_ignore(c, cookie); |
| 1195 |
log_trace("Sending event sync request to catch response to " |
| 1196 |
"pending request, last sequence: %u, event sync: %u", |
| 1197 |
last->sequence, c->event_sync); |
| 1198 |
} |
| 1199 |
} |
| 1200 |
XFlush(c->dpy); |
| 1201 |
xcb_flush(c->c); |
| 1202 |
return true; |
| 1203 |
} |
| 1204 |
|
| 1205 |
xcb_generic_event_t *x_poll_for_event(struct x_connection *c, bool queued) { |
| 1206 |
while (true) { |
| 1207 |
auto e = queued ? xcb_poll_for_queued_event(c->c) : xcb_poll_for_event(c->c); |
| 1208 |
if (e == NULL) { |
| 1209 |
break; |
| 1210 |
} |
| 1211 |
if (x_feed_event(c, e)) { |
| 1212 |
return e; |
| 1213 |
} |
| 1214 |
free(e); |
| 1215 |
} |
| 1216 |
return NULL; |
| 1217 |
} |