| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
#include <X11/Xlib.h> |
| 6 |
#include <stdbool.h> |
| 7 |
#include <stdint.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <xcb/render.h> |
| 10 |
#include <xcb/sync.h> |
| 11 |
#include <xcb/xcb.h> |
| 12 |
#include <xcb/xcb_renderutil.h> |
| 13 |
#include <xcb/xfixes.h> |
| 14 |
|
| 15 |
#include "atom.h" |
| 16 |
#include "compiler.h" |
| 17 |
#include "log.h" |
| 18 |
#include "region.h" |
| 19 |
#include "utils/kernel.h" |
| 20 |
#include "utils/list.h" |
| 21 |
|
| 22 |
typedef struct session session_t; |
| 23 |
struct atom; |
| 24 |
|
| 25 |
|
| 26 |
typedef struct winprop { |
| 27 |
union { |
| 28 |
void *ptr; |
| 29 |
int8_t *p8; |
| 30 |
int16_t *p16; |
| 31 |
int32_t *p32; |
| 32 |
uint32_t *c32; |
| 33 |
xcb_atom_t *atom; |
| 34 |
}; |
| 35 |
unsigned long nitems; |
| 36 |
xcb_atom_t type; |
| 37 |
int format; |
| 38 |
|
| 39 |
xcb_get_property_reply_t *r; |
| 40 |
} winprop_t; |
| 41 |
|
| 42 |
enum x_error_action { |
| 43 |
PENDING_REPLY_ACTION_IGNORE, |
| 44 |
PENDING_REPLY_ACTION_ABORT, |
| 45 |
PENDING_REPLY_ACTION_DEBUG_ABORT, |
| 46 |
}; |
| 47 |
|
| 48 |
struct x_extensions { |
| 49 |
|
| 50 |
int damage_event; |
| 51 |
|
| 52 |
int damage_error; |
| 53 |
|
| 54 |
int fixes_error; |
| 55 |
|
| 56 |
int shm_error; |
| 57 |
|
| 58 |
int shm_event; |
| 59 |
|
| 60 |
bool has_glx; |
| 61 |
|
| 62 |
int glx_error; |
| 63 |
|
| 64 |
bool has_present; |
| 65 |
|
| 66 |
bool has_randr; |
| 67 |
|
| 68 |
int randr_event; |
| 69 |
|
| 70 |
int render_error; |
| 71 |
|
| 72 |
bool has_shape; |
| 73 |
|
| 74 |
int shape_event; |
| 75 |
|
| 76 |
bool has_sync; |
| 77 |
|
| 78 |
int sync_event; |
| 79 |
|
| 80 |
int sync_error; |
| 81 |
}; |
| 82 |
|
| 83 |
struct x_connection { |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
xcb_connection_t *c; |
| 89 |
|
| 90 |
Display *dpy; |
| 91 |
|
| 92 |
int screen; |
| 93 |
|
| 94 |
struct x_extensions e; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
struct list_node pending_x_requests; |
| 100 |
|
| 101 |
XErrorHandler previous_xerror_handler; |
| 102 |
|
| 103 |
xcb_screen_t *screen_info; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
uint32_t last_sequence; |
| 108 |
|
| 109 |
uint32_t latest_completed_request; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
uint32_t event_sync; |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
struct x_monitors { |
| 135 |
int count; |
| 136 |
region_t *regions; |
| 137 |
}; |
| 138 |
|
| 139 |
#define XCB_AWAIT_VOID(func, conn, ...) \ |
| 140 |
\ |
| 141 |
({ \ |
| 142 |
bool __success = true; \ |
| 143 |
__auto_type __e = \ |
| 144 |
xcb_request_check((conn)->c, func##_checked((conn)->c, __VA_ARGS__)); \ |
| 145 |
if (__e) { \ |
| 146 |
x_print_error(conn, __e->sequence, __e->major_code, \ |
| 147 |
__e->minor_code, __e->error_code); \ |
| 148 |
free(__e); \ |
| 149 |
__success = false; \ |
| 150 |
} \ |
| 151 |
__success; \ |
| 152 |
}) |
| 153 |
|
| 154 |
#define XCB_AWAIT(func, conn, ...) \ |
| 155 |
({ \ |
| 156 |
xcb_generic_error_t *__e = NULL; \ |
| 157 |
__auto_type __r = \ |
| 158 |
func##_reply((conn)->c, func((conn)->c, ##__VA_ARGS__), &__e); \ |
| 159 |
if (__e) { \ |
| 160 |
x_print_error(conn, __e->sequence, __e->major_code, \ |
| 161 |
__e->minor_code, __e->error_code); \ |
| 162 |
free(__e); \ |
| 163 |
} \ |
| 164 |
__r; \ |
| 165 |
}) |
| 166 |
|
| 167 |
#define log_debug_x_error(c, e, fmt, ...) \ |
| 168 |
LOG(DEBUG, fmt " (%s)", ##__VA_ARGS__, x_strerror(c, e)) |
| 169 |
#define log_error_x_error(c, e, fmt, ...) \ |
| 170 |
LOG(ERROR, fmt " (%s)", ##__VA_ARGS__, x_strerror(c, e)) |
| 171 |
#define log_fatal_x_error(c, e, fmt, ...) \ |
| 172 |
LOG(FATAL, fmt " (%s)", ##__VA_ARGS__, x_strerror(c, e)) |
| 173 |
|
| 174 |
|
| 175 |
#define XFIXED_TO_DOUBLE(value) (((double)(value)) / 65536) |
| 176 |
#define DOUBLE_TO_XFIXED(value) ((xcb_render_fixed_t)(((double)(value)) * 65536)) |
| 177 |
|
| 178 |
|
| 179 |
static inline uint32_t x_new_id(struct x_connection *c) { |
| 180 |
auto ret = xcb_generate_id(c->c); |
| 181 |
if (ret == (uint32_t)-1) { |
| 182 |
log_fatal("We seems to have run of XIDs. This is either a bug in the X " |
| 183 |
"server, or a resource leakage in the compositor. Please open " |
| 184 |
"an issue about this problem. The compositor will die."); |
| 185 |
abort(); |
| 186 |
} |
| 187 |
return ret; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
void x_set_error_action(struct x_connection *c, uint32_t sequence, enum x_error_action action, |
| 196 |
const char *func, const char *file, int line); |
| 197 |
|
| 198 |
|
| 199 |
#define x_set_error_action_ignore(c, cookie) \ |
| 200 |
x_set_error_action(c, (cookie).sequence, PENDING_REPLY_ACTION_IGNORE, __func__, \ |
| 201 |
__FILE__, __LINE__) |
| 202 |
|
| 203 |
|
| 204 |
#define x_set_error_action_abort(c, cookie) \ |
| 205 |
x_set_error_action(c, (cookie).sequence, PENDING_REPLY_ACTION_ABORT, __func__, \ |
| 206 |
__FILE__, __LINE__) |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
#ifndef NDEBUG |
| 211 |
#define x_set_error_action_debug_abort(c, cookie) \ |
| 212 |
x_set_error_action(c, (cookie).sequence, PENDING_REPLY_ACTION_DEBUG_ABORT, \ |
| 213 |
__func__, __FILE__, __LINE__) |
| 214 |
#else |
| 215 |
#define x_set_error_action_debug_abort(c, cookie) \ |
| 216 |
((void)(c)); \ |
| 217 |
((void)(cookie)) |
| 218 |
#endif |
| 219 |
|
| 220 |
struct x_async_request_base { |
| 221 |
struct list_node siblings; |
| 222 |
|
| 223 |
|
| 224 |
void (*callback)(struct x_connection *, struct x_async_request_base *, |
| 225 |
const xcb_raw_generic_event_t *reply_or_error); |
| 226 |
|
| 227 |
unsigned int sequence; |
| 228 |
|
| 229 |
|
| 230 |
bool no_reply; |
| 231 |
}; |
| 232 |
|
| 233 |
static inline void attr_unused free_x_connection(struct x_connection *c) { |
| 234 |
list_foreach_safe(struct x_async_request_base, i, &c->pending_x_requests, siblings) { |
| 235 |
list_remove(&i->siblings); |
| 236 |
i->callback(c, i, NULL); |
| 237 |
} |
| 238 |
|
| 239 |
XSetErrorHandler(c->previous_xerror_handler); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
bool x_extensions_init(struct x_connection *c); |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
void x_connection_init(struct x_connection *c, Display *dpy); |
| 254 |
void x_connection_init_xcb(struct x_connection *c, xcb_connection_t *conn, int screen); |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
winprop_t x_get_prop_with_offset(const struct x_connection *c, xcb_window_t w, xcb_atom_t atom, |
| 272 |
int offset, int length, xcb_atom_t rtype, int rformat); |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
static inline winprop_t |
| 278 |
x_get_prop(const struct x_connection *c, xcb_window_t wid, xcb_atom_t atom, int length, |
| 279 |
xcb_atom_t rtype, int rformat) { |
| 280 |
return x_get_prop_with_offset(c, wid, atom, 0L, length, rtype, rformat); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
xcb_window_t wid_get_prop_window(struct x_connection *c, xcb_window_t wid, |
| 289 |
xcb_atom_t aprop, bool *exists); |
| 290 |
bool wid_get_opacity_prop(struct x_connection *c, struct atom *atoms, xcb_window_t wid, |
| 291 |
opacity_t def, opacity_t *out); |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
bool wid_get_text_prop(struct x_connection *c, struct atom *atoms, xcb_window_t wid, |
| 301 |
xcb_atom_t prop, char ***pstrlst, int *pnstr); |
| 302 |
|
| 303 |
static inline bool x_is_type_string(struct atom *atoms, xcb_atom_t type) { |
| 304 |
return type == XCB_ATOM_STRING || type == atoms->aUTF8_STRING || |
| 305 |
type == atoms->aC_STRING; |
| 306 |
} |
| 307 |
|
| 308 |
const xcb_render_pictforminfo_t * |
| 309 |
x_get_pictform_for_visual(struct x_connection *, xcb_visualid_t); |
| 310 |
|
| 311 |
xcb_render_picture_t |
| 312 |
x_create_picture_with_pictfmt_and_pixmap(struct x_connection *, xcb_render_pictformat_t pictfmt, |
| 313 |
xcb_pixmap_t pixmap, uint32_t valuemask, |
| 314 |
const xcb_render_create_picture_value_list_t *attr) |
| 315 |
attr_nonnull(1); |
| 316 |
|
| 317 |
xcb_render_picture_t |
| 318 |
x_create_picture_with_visual_and_pixmap(struct x_connection *, xcb_visualid_t visual, |
| 319 |
xcb_pixmap_t pixmap, uint32_t valuemask, |
| 320 |
const xcb_render_create_picture_value_list_t *attr) |
| 321 |
attr_nonnull(1); |
| 322 |
|
| 323 |
xcb_render_picture_t |
| 324 |
x_create_picture_with_standard_and_pixmap(struct x_connection *, xcb_pict_standard_t standard, |
| 325 |
xcb_pixmap_t pixmap, uint32_t valuemask, |
| 326 |
const xcb_render_create_picture_value_list_t *attr) |
| 327 |
attr_nonnull(1); |
| 328 |
|
| 329 |
xcb_render_picture_t |
| 330 |
x_create_picture_with_standard(struct x_connection *c, int w, int h, |
| 331 |
xcb_pict_standard_t standard, uint32_t valuemask, |
| 332 |
const xcb_render_create_picture_value_list_t *attr) |
| 333 |
attr_nonnull(1); |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
xcb_render_picture_t |
| 339 |
x_create_picture_with_pictfmt(struct x_connection *, int w, int h, |
| 340 |
xcb_render_pictformat_t pictfmt, uint8_t depth, uint32_t valuemask, |
| 341 |
const xcb_render_create_picture_value_list_t *attr) |
| 342 |
attr_nonnull(1); |
| 343 |
|
| 344 |
xcb_render_picture_t |
| 345 |
x_create_picture_with_visual(struct x_connection *, int w, int h, xcb_visualid_t visual, |
| 346 |
uint32_t valuemask, |
| 347 |
const xcb_render_create_picture_value_list_t *attr) |
| 348 |
attr_nonnull(1); |
| 349 |
|
| 350 |
|
| 351 |
bool x_fetch_region(struct x_connection *, xcb_xfixes_region_t r, region_t *res); |
| 352 |
|
| 353 |
|
| 354 |
bool x_set_region(struct x_connection *c, xcb_xfixes_region_t dst, const region_t *src); |
| 355 |
|
| 356 |
|
| 357 |
uint32_t x_create_region(struct x_connection *c, const region_t *reg); |
| 358 |
|
| 359 |
void x_async_change_window_attributes(struct x_connection *c, xcb_window_t wid, |
| 360 |
uint32_t mask, const uint32_t *values, |
| 361 |
struct x_async_request_base *req); |
| 362 |
void x_async_query_tree(struct x_connection *c, xcb_window_t wid, |
| 363 |
struct x_async_request_base *req); |
| 364 |
void x_async_get_property(struct x_connection *c, xcb_window_t wid, xcb_atom_t atom, |
| 365 |
xcb_atom_t type, uint32_t long_offset, uint32_t long_length, |
| 366 |
struct x_async_request_base *req); |
| 367 |
|
| 368 |
|
| 369 |
void x_destroy_region(struct x_connection *c, uint32_t region); |
| 370 |
|
| 371 |
void x_set_picture_clip_region(struct x_connection *, xcb_render_picture_t, |
| 372 |
int16_t clip_x_origin, int16_t clip_y_origin, const region_t *); |
| 373 |
|
| 374 |
void x_clear_picture_clip_region(struct x_connection *, xcb_render_picture_t pict); |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
void x_free_picture(struct x_connection *c, xcb_render_picture_t p); |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
void x_print_error_impl(struct x_connection *c, unsigned long serial, uint8_t major, |
| 387 |
uint16_t minor, uint8_t error_code, const char *func); |
| 388 |
#define x_print_error(c, serial, major, minor, error_code) \ |
| 389 |
x_print_error_impl(c, serial, major, minor, error_code, __func__) |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
const char *x_strerror(struct x_connection *c, const xcb_generic_error_t *e); |
| 398 |
|
| 399 |
xcb_pixmap_t x_create_pixmap(struct x_connection *, uint8_t depth, int width, int height); |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
static inline void free_winprop(winprop_t *pprop) { |
| 407 |
|
| 408 |
if (pprop->r) { |
| 409 |
free(pprop->r); |
| 410 |
} |
| 411 |
pprop->ptr = NULL; |
| 412 |
pprop->r = NULL; |
| 413 |
pprop->nitems = 0; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
xcb_pixmap_t x_get_root_back_pixmap(struct x_connection *c, struct atom *atoms); |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
bool x_is_root_back_pixmap_atom(struct atom *atoms, xcb_atom_t atom); |
| 422 |
|
| 423 |
struct x_convolution_kernel { |
| 424 |
int size; |
| 425 |
int capacity; |
| 426 |
xcb_render_fixed_t kernel[]; |
| 427 |
}; |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
void attr_nonnull(1, 3) x_create_convolution_kernel(const conv *kernel, double center, |
| 441 |
struct x_convolution_kernel **ret); |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
struct xvisual_info x_get_visual_info(struct x_connection *c, xcb_visualid_t visual); |
| 446 |
|
| 447 |
xcb_visualid_t x_get_visual_for_standard(struct x_connection *c, xcb_pict_standard_t std); |
| 448 |
|
| 449 |
xcb_visualid_t x_get_visual_for_depth(xcb_screen_t *screen, uint8_t depth); |
| 450 |
|
| 451 |
xcb_render_pictformat_t |
| 452 |
x_get_pictfmt_for_standard(struct x_connection *c, xcb_pict_standard_t std); |
| 453 |
|
| 454 |
|
| 455 |
void x_update_monitors_async(struct x_connection *, struct x_monitors *); |
| 456 |
|
| 457 |
void x_free_monitor_info(struct x_monitors *); |
| 458 |
|
| 459 |
uint32_t attr_deprecated xcb_generate_id(xcb_connection_t *c); |
| 460 |
|
| 461 |
|
| 462 |
void x_request_vblank_event(struct x_connection *c, xcb_window_t window, uint64_t msc); |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
static inline void x_await_request(struct x_connection *c, struct x_async_request_base *req) { |
| 470 |
list_insert_before(&c->pending_x_requests, &req->siblings); |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
bool x_prepare_for_sleep(struct x_connection *c); |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
xcb_generic_event_t *x_poll_for_event(struct x_connection *c, bool queued); |