| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <assert.h> |
| 6 |
#include <ctype.h> |
| 7 |
#include <fnmatch.h> |
| 8 |
#include <inttypes.h> |
| 9 |
#include <stddef.h> |
| 10 |
#include <stdint.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <string.h> |
| 13 |
#include <uthash.h> |
| 14 |
|
| 15 |
|
| 16 |
#ifdef CONFIG_REGEX_PCRE |
| 17 |
#define PCRE2_CODE_UNIT_WIDTH 8 |
| 18 |
#include <pcre2.h> |
| 19 |
|
| 20 |
#endif |
| 21 |
|
| 22 |
#include <X11/Xlib.h> |
| 23 |
#include <xcb/xcb.h> |
| 24 |
|
| 25 |
#include "atom.h" |
| 26 |
#include "common.h" |
| 27 |
#include "compiler.h" |
| 28 |
#include "log.h" |
| 29 |
#include "test.h" |
| 30 |
#include "utils/str.h" |
| 31 |
#include "utils/uthash_extra.h" |
| 32 |
#include "wm/win.h" |
| 33 |
#include "x.h" |
| 34 |
|
| 35 |
#include "c2.h" |
| 36 |
|
| 37 |
#pragma GCC diagnostic error "-Wunused-parameter" |
| 38 |
|
| 39 |
#define C2_MAX_LEVELS 10 |
| 40 |
|
| 41 |
typedef struct c2_condition_node_branch c2_condition_node_branch; |
| 42 |
typedef struct c2_condition_node_leaf c2_condition_node_leaf; |
| 43 |
|
| 44 |
enum c2_condition_node_type { |
| 45 |
C2_NODE_TYPE_BRANCH, |
| 46 |
C2_NODE_TYPE_LEAF, |
| 47 |
C2_NODE_TYPE_TRUE, |
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
typedef struct { |
| 52 |
enum c2_condition_node_type type; |
| 53 |
union { |
| 54 |
struct c2_condition_node_branch *b; |
| 55 |
struct c2_condition_node_leaf *l; |
| 56 |
}; |
| 57 |
bool neg : 1; |
| 58 |
} c2_condition_node_ptr; |
| 59 |
|
| 60 |
struct c2_tracked_property_key { |
| 61 |
xcb_atom_t property; |
| 62 |
bool is_on_client; |
| 63 |
char padding[3]; |
| 64 |
}; |
| 65 |
static_assert(sizeof(struct c2_tracked_property_key) == 8, "Padding bytes in " |
| 66 |
"c2_tracked_property_key"); |
| 67 |
|
| 68 |
struct c2_tracked_property { |
| 69 |
UT_hash_handle hh; |
| 70 |
struct c2_tracked_property_key key; |
| 71 |
unsigned int id; |
| 72 |
}; |
| 73 |
|
| 74 |
struct c2_state { |
| 75 |
struct c2_tracked_property *tracked_properties; |
| 76 |
struct atom *atoms; |
| 77 |
xcb_get_property_cookie_t *cookies; |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
struct c2_property_value { |
| 82 |
union { |
| 83 |
struct { |
| 84 |
char *string; |
| 85 |
|
| 86 |
unsigned int string_capacity; |
| 87 |
}; |
| 88 |
struct { |
| 89 |
int64_t numbers[4]; |
| 90 |
}; |
| 91 |
struct { |
| 92 |
int64_t *array; |
| 93 |
|
| 94 |
unsigned int array_capacity; |
| 95 |
}; |
| 96 |
}; |
| 97 |
|
| 98 |
|
| 99 |
uint32_t length; |
| 100 |
enum { |
| 101 |
C2_PROPERTY_TYPE_STRING, |
| 102 |
C2_PROPERTY_TYPE_NUMBER, |
| 103 |
C2_PROPERTY_TYPE_ATOM, |
| 104 |
C2_PROPERTY_TYPE_NONE, |
| 105 |
} type; |
| 106 |
bool valid; |
| 107 |
bool needs_update; |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
static const c2_condition_node_ptr C2_NODE_PTR_INIT = { |
| 112 |
.type = C2_NODE_TYPE_LEAF, |
| 113 |
.l = NULL, |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
typedef enum { |
| 118 |
C2_B_OUNDEFINED, |
| 119 |
C2_B_OAND, |
| 120 |
C2_B_OOR, |
| 121 |
C2_B_OXOR, |
| 122 |
} c2_b_op_t; |
| 123 |
|
| 124 |
|
| 125 |
struct c2_condition_node_branch { |
| 126 |
c2_b_op_t op; |
| 127 |
c2_condition_node_ptr opr1; |
| 128 |
c2_condition_node_ptr opr2; |
| 129 |
}; |
| 130 |
|
| 131 |
|
| 132 |
#define C2_B_INIT \ |
| 133 |
{.neg = false, .op = C2_B_OUNDEFINED, .opr1 = C2_PTR_INIT, .opr2 = C2_PTR_INIT} |
| 134 |
|
| 135 |
|
| 136 |
struct c2_condition_node_leaf { |
| 137 |
enum { |
| 138 |
C2_L_OEXISTS = 0, |
| 139 |
C2_L_OEQ, |
| 140 |
C2_L_OGT, |
| 141 |
C2_L_OGTEQ, |
| 142 |
C2_L_OLT, |
| 143 |
C2_L_OLTEQ, |
| 144 |
} op : 3; |
| 145 |
enum { |
| 146 |
C2_L_MEXACT, |
| 147 |
C2_L_MSTART, |
| 148 |
C2_L_MCONTAINS, |
| 149 |
C2_L_MWILDCARD, |
| 150 |
C2_L_MPCRE, |
| 151 |
} match : 3; |
| 152 |
bool match_ignorecase : 1; |
| 153 |
bool target_on_client : 1; |
| 154 |
char *tgt; |
| 155 |
unsigned int target_id; |
| 156 |
xcb_atom_t tgtatom; |
| 157 |
int index; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
enum { |
| 163 |
C2_L_PUNDEFINED = -1, |
| 164 |
C2_L_PID = 0, |
| 165 |
C2_L_PX, |
| 166 |
C2_L_PY, |
| 167 |
C2_L_PX2, |
| 168 |
C2_L_PY2, |
| 169 |
C2_L_PWIDTH, |
| 170 |
C2_L_PHEIGHT, |
| 171 |
C2_L_PWIDTHB, |
| 172 |
C2_L_PHEIGHTB, |
| 173 |
C2_L_PBDW, |
| 174 |
C2_L_PFULLSCREEN, |
| 175 |
C2_L_POVREDIR, |
| 176 |
C2_L_PARGB, |
| 177 |
C2_L_PFOCUSED, |
| 178 |
C2_L_PGROUPFOCUSED, |
| 179 |
C2_L_PWMWIN, |
| 180 |
C2_L_PBSHAPED, |
| 181 |
C2_L_PROUNDED, |
| 182 |
C2_L_PCLIENT, |
| 183 |
C2_L_PWINDOWTYPE, |
| 184 |
C2_L_PLEADER, |
| 185 |
C2_L_PNAME, |
| 186 |
C2_L_PCLASSG, |
| 187 |
C2_L_PCLASSI, |
| 188 |
C2_L_PROLE, |
| 189 |
C2_L_PURGENT, |
| 190 |
} predef; |
| 191 |
enum { |
| 192 |
C2_L_PTUNDEFINED, |
| 193 |
C2_L_PTSTRING, |
| 194 |
C2_L_PTINT, |
| 195 |
} ptntype; |
| 196 |
char *ptnstr; |
| 197 |
long ptnint; |
| 198 |
#ifdef CONFIG_REGEX_PCRE |
| 199 |
pcre2_code *regex_pcre; |
| 200 |
pcre2_match_data *regex_pcre_match; |
| 201 |
#endif |
| 202 |
}; |
| 203 |
|
| 204 |
static const unsigned int C2_L_INVALID_TARGET_ID = UINT_MAX; |
| 205 |
|
| 206 |
static const c2_condition_node_leaf C2_LEAF_NODE_INIT = { |
| 207 |
.op = C2_L_OEXISTS, |
| 208 |
.match = C2_L_MEXACT, |
| 209 |
.match_ignorecase = false, |
| 210 |
.tgt = NULL, |
| 211 |
.tgtatom = 0, |
| 212 |
.target_on_client = false, |
| 213 |
.predef = C2_L_PUNDEFINED, |
| 214 |
.index = 0, |
| 215 |
.ptntype = C2_L_PTUNDEFINED, |
| 216 |
.ptnstr = NULL, |
| 217 |
.ptnint = 0, |
| 218 |
.target_id = C2_L_INVALID_TARGET_ID, |
| 219 |
}; |
| 220 |
|
| 221 |
|
| 222 |
struct c2_condition { |
| 223 |
c2_condition_node_ptr root; |
| 224 |
void *data; |
| 225 |
struct list_node siblings; |
| 226 |
}; |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
static const struct { |
| 231 |
const char *name; |
| 232 |
bool is_string; |
| 233 |
bool deprecated; |
| 234 |
} C2_PREDEFS[] = { |
| 235 |
[C2_L_PID] = { "id", false, true }, |
| 236 |
[C2_L_PX] = { "x", false, }, |
| 237 |
[C2_L_PY] = { "y", false, }, |
| 238 |
[C2_L_PX2] = { "x2", false, }, |
| 239 |
[C2_L_PY2] = { "y2", false, }, |
| 240 |
[C2_L_PWIDTH] = { "width", false, }, |
| 241 |
[C2_L_PHEIGHT] = { "height", false, }, |
| 242 |
[C2_L_PWIDTHB] = { "widthb", false, }, |
| 243 |
[C2_L_PHEIGHTB] = { "heightb", false, }, |
| 244 |
[C2_L_PBDW] = { "border_width", false, }, |
| 245 |
[C2_L_PFULLSCREEN] = { "fullscreen", false, }, |
| 246 |
[C2_L_POVREDIR] = { "override_redirect", false, }, |
| 247 |
[C2_L_PARGB] = { "argb", false, }, |
| 248 |
[C2_L_PFOCUSED] = { "focused", false, }, |
| 249 |
[C2_L_PGROUPFOCUSED] = { "group_focused", false, }, |
| 250 |
[C2_L_PWMWIN] = { "wmwin", false, }, |
| 251 |
[C2_L_PBSHAPED] = { "bounding_shaped", false, }, |
| 252 |
[C2_L_PROUNDED] = { "rounded_corners", false, }, |
| 253 |
[C2_L_PCLIENT] = { "client", false, true }, |
| 254 |
[C2_L_PWINDOWTYPE] = { "window_type", true, }, |
| 255 |
[C2_L_PLEADER] = { "leader", false, true }, |
| 256 |
[C2_L_PNAME] = { "name", true, }, |
| 257 |
[C2_L_PCLASSG] = { "class_g", true, }, |
| 258 |
[C2_L_PCLASSI] = { "class_i", true, }, |
| 259 |
[C2_L_PROLE] = { "role", true, }, |
| 260 |
[C2_L_PURGENT] = { "urgent", false }, |
| 261 |
}; |
| 262 |
|
| 263 |
|
| 264 |
static const char *const c2_pattern_type_names[] = { |
| 265 |
[C2_L_PTUNDEFINED] = "undefined", |
| 266 |
[C2_L_PTSTRING] = "string", |
| 267 |
[C2_L_PTINT] = "number", |
| 268 |
}; |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
static inline int strcmp_wd(const char *needle, const char *src) { |
| 274 |
int ret = mstrncmp(needle, src); |
| 275 |
if (ret) { |
| 276 |
return ret; |
| 277 |
} |
| 278 |
|
| 279 |
char c = src[strlen(needle)]; |
| 280 |
if (isalnum((unsigned char)c) || '_' == c) { |
| 281 |
return 1; |
| 282 |
} |
| 283 |
|
| 284 |
return 0; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
static inline c2_condition_node_ptr |
| 291 |
c2h_comb_tree(c2_b_op_t op, c2_condition_node_ptr p1, c2_condition_node_ptr p2) { |
| 292 |
c2_condition_node_ptr p = {.type = C2_NODE_TYPE_BRANCH, .b = NULL}; |
| 293 |
p.b = cmalloc(struct c2_condition_node_branch); |
| 294 |
|
| 295 |
p.b->op = op; |
| 296 |
p.b->opr1 = p1; |
| 297 |
p.b->opr2 = p2; |
| 298 |
|
| 299 |
return p; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
static inline int c2h_b_opp(c2_b_op_t op) { |
| 306 |
switch (op) { |
| 307 |
case C2_B_OAND: return 2; |
| 308 |
case C2_B_OOR: |
| 309 |
case C2_B_OXOR: return 1; |
| 310 |
default: break; |
| 311 |
} |
| 312 |
|
| 313 |
assert(0); |
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
static inline int c2h_b_opcmp(c2_b_op_t op1, c2_b_op_t op2) { |
| 326 |
return c2h_b_opp(op1) - c2h_b_opp(op2); |
| 327 |
} |
| 328 |
|
| 329 |
static int c2_parse_group(const char *pattern, int offset, c2_condition_node_ptr *presult, |
| 330 |
int level, bool *deprecated); |
| 331 |
static int c2_parse_target(const char *pattern, int offset, c2_condition_node_ptr *presult); |
| 332 |
static int c2_parse_op(const char *pattern, int offset, c2_condition_node_ptr *presult); |
| 333 |
static int c2_parse_pattern(const char *pattern, int offset, c2_condition_node_ptr *presult); |
| 334 |
static int c2_parse_legacy(const char *pattern, int offset, c2_condition_node_ptr *presult); |
| 335 |
static void c2_free(c2_condition_node_ptr p); |
| 336 |
static size_t c2_condition_node_to_str(c2_condition_node_ptr p, char *output, size_t len); |
| 337 |
static const char *c2_condition_node_to_str2(c2_condition_node_ptr ptr); |
| 338 |
static bool |
| 339 |
c2_tree_postprocess(struct c2_state *state, xcb_connection_t *c, c2_condition_node_ptr node); |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
static inline void c2_freep(c2_condition_node_ptr *pp) { |
| 345 |
if (pp) { |
| 346 |
c2_free(*pp); |
| 347 |
*pp = C2_NODE_PTR_INIT; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
struct c2_condition * |
| 355 |
c2_parse(struct list_node *list, const char *pattern, void *data, bool *deprecated) { |
| 356 |
if (!pattern) { |
| 357 |
return NULL; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
auto result = C2_NODE_PTR_INIT; |
| 362 |
int offset = -1; |
| 363 |
|
| 364 |
if (strlen(pattern) >= 2 && ':' == pattern[1]) { |
| 365 |
offset = c2_parse_legacy(pattern, 0, &result); |
| 366 |
} else { |
| 367 |
offset = c2_parse_group(pattern, 0, &result, 0, deprecated); |
| 368 |
} |
| 369 |
|
| 370 |
if (offset < 0) { |
| 371 |
c2_freep(&result); |
| 372 |
return NULL; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
{ |
| 377 |
auto plptr = cmalloc(struct c2_condition); |
| 378 |
*plptr = (struct c2_condition){ |
| 379 |
.root = result, |
| 380 |
.data = data, |
| 381 |
}; |
| 382 |
list_init_head(&plptr->siblings); |
| 383 |
if (list) { |
| 384 |
list_insert_after(list, &plptr->siblings); |
| 385 |
} |
| 386 |
|
| 387 |
#ifdef DEBUG_C2 |
| 388 |
log_trace("(\"%s\"): ", pattern); |
| 389 |
c2_dump(plptr->ptr); |
| 390 |
putchar('\n'); |
| 391 |
#endif |
| 392 |
|
| 393 |
return plptr; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
c2_condition * |
| 401 |
c2_parse_with_prefix(struct list_node *list, const char *pattern, |
| 402 |
void *(*parse_prefix)(const char *input, const char **end, void *), |
| 403 |
void (*free_value)(void *), void *user_data, bool *deprecated) { |
| 404 |
char *pattern_start = NULL; |
| 405 |
void *val = parse_prefix(pattern, (const char **)&pattern_start, user_data); |
| 406 |
if (pattern_start == NULL) { |
| 407 |
return NULL; |
| 408 |
} |
| 409 |
auto ret = c2_parse(list, pattern_start, val, deprecated); |
| 410 |
if (!ret && free_value) { |
| 411 |
free_value(val); |
| 412 |
} |
| 413 |
return ret; |
| 414 |
} |
| 415 |
|
| 416 |
TEST_CASE(c2_parse) { |
| 417 |
char str[1024]; |
| 418 |
bool deprecated = false; |
| 419 |
struct c2_condition *cond = c2_parse(NULL, "name = \"xterm\"", NULL, &deprecated); |
| 420 |
struct atom *atoms = init_mock_atoms(); |
| 421 |
struct c2_state *state = c2_state_new(atoms); |
| 422 |
TEST_NOTEQUAL(cond, NULL); |
| 423 |
TEST_EQUAL(!cond->root.type, C2_NODE_TYPE_BRANCH); |
| 424 |
TEST_NOTEQUAL(cond->root.l, NULL); |
| 425 |
TEST_EQUAL(cond->root.l->op, C2_L_OEQ); |
| 426 |
TEST_EQUAL(cond->root.l->ptntype, C2_L_PTSTRING); |
| 427 |
TEST_STREQUAL(cond->root.l->ptnstr, "xterm"); |
| 428 |
|
| 429 |
size_t len = c2_condition_node_to_str(cond->root, str, sizeof(str)); |
| 430 |
TEST_STREQUAL3(str, "name = \"xterm\"", len); |
| 431 |
|
| 432 |
struct wm *wm = wm_new(); |
| 433 |
struct wm_ref *node = wm_new_mock_window(wm, 1); |
| 434 |
|
| 435 |
struct win test_win = { |
| 436 |
.name = "xterm", |
| 437 |
.tree_ref = node, |
| 438 |
}; |
| 439 |
TEST_TRUE(c2_match_one(state, &test_win, cond, NULL)); |
| 440 |
c2_tree_postprocess(state, NULL, cond->root); |
| 441 |
TEST_EQUAL(HASH_COUNT(state->tracked_properties), 0); |
| 442 |
c2_state_free(state); |
| 443 |
destroy_atoms(atoms); |
| 444 |
c2_free_condition(cond, NULL); |
| 445 |
|
| 446 |
cond = c2_parse(NULL, "argb", NULL, &deprecated); |
| 447 |
TEST_NOTEQUAL(cond, NULL); |
| 448 |
TEST_NOTEQUAL(cond->root.type, C2_NODE_TYPE_BRANCH); |
| 449 |
TEST_EQUAL(cond->root.l->ptntype, C2_L_PTINT); |
| 450 |
c2_free_condition(cond, NULL); |
| 451 |
|
| 452 |
cond = c2_parse(NULL, "argb = 'b'", NULL, &deprecated); |
| 453 |
TEST_EQUAL(cond, NULL); |
| 454 |
|
| 455 |
cond = c2_parse(NULL, "_GTK_FRAME_EXTENTS@:c", NULL, &deprecated); |
| 456 |
TEST_NOTEQUAL(cond, NULL); |
| 457 |
TEST_NOTEQUAL(cond->root.type, C2_NODE_TYPE_BRANCH); |
| 458 |
TEST_NOTEQUAL(cond->root.l, NULL); |
| 459 |
TEST_EQUAL(cond->root.l->op, C2_L_OEXISTS); |
| 460 |
TEST_EQUAL(cond->root.l->match, C2_L_MEXACT); |
| 461 |
TEST_EQUAL(cond->root.l->predef, C2_L_PUNDEFINED); |
| 462 |
TEST_TRUE(cond->root.l->target_on_client); |
| 463 |
TEST_NOTEQUAL(cond->root.l->tgt, NULL); |
| 464 |
TEST_STREQUAL(cond->root.l->tgt, "_GTK_FRAME_EXTENTS"); |
| 465 |
|
| 466 |
atoms = init_mock_atoms(); |
| 467 |
state = c2_state_new(atoms); |
| 468 |
c2_tree_postprocess(state, NULL, cond->root); |
| 469 |
TEST_EQUAL(HASH_COUNT(state->tracked_properties), 1); |
| 470 |
HASH_ITER2(state->tracked_properties, prop) { |
| 471 |
TEST_EQUAL(prop->key.property, |
| 472 |
get_atom_with_nul(state->atoms, "_GTK_FRAME_EXTENTS", NULL)); |
| 473 |
} |
| 474 |
c2_state_free(state); |
| 475 |
destroy_atoms(atoms); |
| 476 |
|
| 477 |
len = c2_condition_node_to_str(cond->root, str, sizeof(str)); |
| 478 |
TEST_STREQUAL3(str, "_GTK_FRAME_EXTENTS@[0]", len); |
| 479 |
c2_free_condition(cond, NULL); |
| 480 |
|
| 481 |
cond = c2_parse(NULL, "!(name != \"xterm\" && class_g *= \"XTerm\") || !name != \"yterm\"", |
| 482 |
NULL, &deprecated); |
| 483 |
TEST_NOTEQUAL(cond, NULL); |
| 484 |
TEST_STREQUAL(c2_condition_to_str(cond), "(!(name != \"xterm\" && class_g *= " |
| 485 |
"\"XTerm\") || name = \"yterm\")"); |
| 486 |
c2_free_condition(cond, NULL); |
| 487 |
|
| 488 |
cond = c2_parse(NULL, "name = \"xterm\" && class_g *= \"XTerm\"", NULL, &deprecated); |
| 489 |
TEST_NOTEQUAL(cond, NULL); |
| 490 |
TEST_EQUAL(cond->root.type, C2_NODE_TYPE_BRANCH); |
| 491 |
TEST_NOTEQUAL(cond->root.b, NULL); |
| 492 |
TEST_EQUAL(cond->root.b->op, C2_B_OAND); |
| 493 |
TEST_NOTEQUAL(cond->root.b->opr1.l, NULL); |
| 494 |
TEST_NOTEQUAL(cond->root.b->opr2.l, NULL); |
| 495 |
TEST_EQUAL(cond->root.b->opr1.l->op, C2_L_OEQ); |
| 496 |
TEST_EQUAL(cond->root.b->opr1.l->match, C2_L_MEXACT); |
| 497 |
TEST_EQUAL(cond->root.b->opr1.l->ptntype, C2_L_PTSTRING); |
| 498 |
TEST_EQUAL(cond->root.b->opr2.l->op, C2_L_OEQ); |
| 499 |
TEST_EQUAL(cond->root.b->opr2.l->match, C2_L_MCONTAINS); |
| 500 |
TEST_EQUAL(cond->root.b->opr2.l->ptntype, C2_L_PTSTRING); |
| 501 |
TEST_STREQUAL(cond->root.b->opr1.l->tgt, "name"); |
| 502 |
TEST_EQUAL(cond->root.b->opr1.l->predef, C2_L_PNAME); |
| 503 |
TEST_STREQUAL(cond->root.b->opr2.l->tgt, "class_g"); |
| 504 |
TEST_EQUAL(cond->root.b->opr2.l->predef, C2_L_PCLASSG); |
| 505 |
|
| 506 |
atoms = init_mock_atoms(); |
| 507 |
state = c2_state_new(atoms); |
| 508 |
len = c2_condition_node_to_str(cond->root, str, sizeof(str)); |
| 509 |
TEST_STREQUAL3(str, "(name = \"xterm\" && class_g *= \"XTerm\")", len); |
| 510 |
test_win.class_general = "XTerm"; |
| 511 |
TEST_TRUE(c2_match_one(state, &test_win, cond, NULL)); |
| 512 |
test_win.class_general = "asdf"; |
| 513 |
TEST_TRUE(!c2_match_one(state, &test_win, cond, NULL)); |
| 514 |
c2_free_condition(cond, NULL); |
| 515 |
c2_state_free(state); |
| 516 |
destroy_atoms(atoms); |
| 517 |
|
| 518 |
cond = c2_parse(NULL, "_NET_WM_STATE[1]:32a *='_NET_WM_STATE_HIDDEN'", NULL, |
| 519 |
&deprecated); |
| 520 |
TEST_EQUAL(cond->root.l->index, 1); |
| 521 |
TEST_STREQUAL(cond->root.l->tgt, "_NET_WM_STATE"); |
| 522 |
TEST_STREQUAL(cond->root.l->ptnstr, "_NET_WM_STATE_HIDDEN"); |
| 523 |
|
| 524 |
len = c2_condition_node_to_str(cond->root, str, sizeof(str)); |
| 525 |
TEST_STREQUAL3(str, "_NET_WM_STATE[1] *= \"_NET_WM_STATE_HIDDEN\"", len); |
| 526 |
c2_free_condition(cond, NULL); |
| 527 |
|
| 528 |
cond = c2_parse(NULL, "_NET_WM_STATE[*]:32a*='_NET_WM_STATE_HIDDEN'", NULL, &deprecated); |
| 529 |
TEST_EQUAL(cond->root.l->index, -1); |
| 530 |
|
| 531 |
len = c2_condition_node_to_str(cond->root, str, sizeof(str)); |
| 532 |
TEST_STREQUAL3(str, "_NET_WM_STATE[*] *= \"_NET_WM_STATE_HIDDEN\"", len); |
| 533 |
c2_free_condition(cond, NULL); |
| 534 |
|
| 535 |
cond = c2_parse(NULL, "!class_i:0s", NULL, &deprecated); |
| 536 |
TEST_NOTEQUAL(cond, NULL); |
| 537 |
len = c2_condition_node_to_str(cond->root, str, sizeof(str)); |
| 538 |
TEST_STREQUAL3(str, "!class_i", len); |
| 539 |
c2_free_condition(cond, NULL); |
| 540 |
|
| 541 |
cond = c2_parse(NULL, "_NET_WM_STATE = '_NET_WM_STATE_HIDDEN'", NULL, &deprecated); |
| 542 |
TEST_NOTEQUAL(cond, NULL); |
| 543 |
c2_free_condition(cond, NULL); |
| 544 |
|
| 545 |
cond = c2_parse(NULL, "1A:\n1111111111111ar1", NULL, &deprecated); |
| 546 |
TEST_EQUAL(cond, NULL); |
| 547 |
|
| 548 |
cond = c2_parse(NULL, "N [4444444444444: \n", NULL, &deprecated); |
| 549 |
TEST_EQUAL(cond, NULL); |
| 550 |
|
| 551 |
cond = c2_parse(NULL, " x:a=\"b\377\\xCCCCC", NULL, &deprecated); |
| 552 |
TEST_EQUAL(cond, NULL); |
| 553 |
|
| 554 |
cond = c2_parse(NULL, "!!!!!!!((((((!(((((,", NULL, &deprecated); |
| 555 |
TEST_EQUAL(cond, NULL); |
| 556 |
|
| 557 |
const char *rule = "(((role = \"\\\\tg^\\n\\n[\\t\" && role ~?= \"\") && " |
| 558 |
"role ~?= \"\\n\\n\\n\\b\\n^\\n*0bon\") && role ~?= " |
| 559 |
"\"\\n\\n\\x8a\\b\\n^\\n*0\\n[\\n[\\n\\n\\b\\n\")"; |
| 560 |
cond = c2_parse(NULL, rule, NULL, &deprecated); |
| 561 |
TEST_NOTEQUAL(cond, NULL); |
| 562 |
len = c2_condition_node_to_str(cond->root, str, sizeof(str)); |
| 563 |
TEST_STREQUAL3(str, rule, len); |
| 564 |
c2_free_condition(cond, NULL); |
| 565 |
|
| 566 |
wm_free_mock_window(wm, test_win.tree_ref); |
| 567 |
wm_free(wm); |
| 568 |
} |
| 569 |
|
| 570 |
#define c2_error(format, ...) \ |
| 571 |
do { \ |
| 572 |
log_error("Pattern \"%s\" pos %d: " format, pattern, offset, ##__VA_ARGS__); \ |
| 573 |
goto fail; \ |
| 574 |
} while (0) |
| 575 |
|
| 576 |
|
| 577 |
#define C2H_SKIP_SPACES() \ |
| 578 |
{ \ |
| 579 |
while (isspace((unsigned char)pattern[offset])) \ |
| 580 |
++offset; \ |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
static int c2_parse_group(const char *pattern, int offset, c2_condition_node_ptr *presult, |
| 589 |
int level, bool *deprecated) { |
| 590 |
if (!pattern) { |
| 591 |
return -1; |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
const char endchar = (offset ? ')' : '\0'); |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
c2_b_op_t ops[3] = {}; |
| 604 |
|
| 605 |
c2_condition_node_ptr eles[2] = {C2_NODE_PTR_INIT, C2_NODE_PTR_INIT}; |
| 606 |
|
| 607 |
int elei = 0; |
| 608 |
|
| 609 |
c2_condition_node_ptr *pele = eles; |
| 610 |
|
| 611 |
bool neg = false; |
| 612 |
|
| 613 |
|
| 614 |
bool next_expected = true; |
| 615 |
|
| 616 |
|
| 617 |
if (level > C2_MAX_LEVELS) { |
| 618 |
c2_error("Exceeded maximum recursion levels."); |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
for (; pattern[offset]; ++offset) { |
| 623 |
assert(elei <= 2); |
| 624 |
|
| 625 |
|
| 626 |
if (isspace((unsigned char)pattern[offset])) { |
| 627 |
continue; |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
if (')' == pattern[offset]) { |
| 632 |
break; |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
if ('!' == pattern[offset]) { |
| 637 |
if (!next_expected) { |
| 638 |
c2_error("Unexpected \"!\"."); |
| 639 |
} |
| 640 |
|
| 641 |
neg = !neg; |
| 642 |
continue; |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
if ('&' == pattern[offset] || '|' == pattern[offset]) { |
| 647 |
if (next_expected) { |
| 648 |
c2_error("Unexpected logical operator."); |
| 649 |
} |
| 650 |
|
| 651 |
next_expected = true; |
| 652 |
if (mstrncmp("&&", pattern + offset) == 0) { |
| 653 |
ops[elei] = C2_B_OAND; |
| 654 |
++offset; |
| 655 |
} else if (mstrncmp("||", pattern + offset) == 0) { |
| 656 |
ops[elei] = C2_B_OOR; |
| 657 |
++offset; |
| 658 |
} else { |
| 659 |
c2_error("Illegal logical operator."); |
| 660 |
} |
| 661 |
|
| 662 |
continue; |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
if (!next_expected) { |
| 667 |
c2_error("Unexpected expression."); |
| 668 |
} |
| 669 |
|
| 670 |
assert(!elei || ops[elei]); |
| 671 |
|
| 672 |
|
| 673 |
if (elei == 2) { |
| 674 |
--elei; |
| 675 |
|
| 676 |
|
| 677 |
if (c2h_b_opcmp(ops[1], ops[2]) >= 0) { |
| 678 |
eles[0] = c2h_comb_tree(ops[1], eles[0], eles[1]); |
| 679 |
eles[1] = C2_NODE_PTR_INIT; |
| 680 |
pele = &eles[elei]; |
| 681 |
ops[1] = ops[2]; |
| 682 |
} |
| 683 |
|
| 684 |
else { |
| 685 |
eles[1] = c2h_comb_tree(ops[2], eles[1], C2_NODE_PTR_INIT); |
| 686 |
assert(eles[1].type == C2_NODE_TYPE_BRANCH); |
| 687 |
pele = &eles[1].b->opr2; |
| 688 |
} |
| 689 |
|
| 690 |
ops[2] = C2_B_OUNDEFINED; |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
if ('(' == pattern[offset]) { |
| 695 |
if ((offset = c2_parse_group(pattern, offset + 1, pele, level + 1, |
| 696 |
deprecated)) < 0) { |
| 697 |
goto fail; |
| 698 |
} |
| 699 |
} else { |
| 700 |
|
| 701 |
if ((offset = c2_parse_target(pattern, offset, pele)) < 0) { |
| 702 |
goto fail; |
| 703 |
} |
| 704 |
|
| 705 |
assert(pele->type != C2_NODE_TYPE_BRANCH && pele->l != NULL); |
| 706 |
|
| 707 |
if ((offset = c2_parse_op(pattern, offset, pele)) < 0) { |
| 708 |
goto fail; |
| 709 |
} |
| 710 |
|
| 711 |
if ((offset = c2_parse_pattern(pattern, offset, pele)) < 0) { |
| 712 |
goto fail; |
| 713 |
} |
| 714 |
|
| 715 |
if (pele->l->predef != C2_L_PUNDEFINED) { |
| 716 |
typeof(pele->l->ptntype) predef_type = |
| 717 |
C2_PREDEFS[pele->l->predef].is_string ? C2_L_PTSTRING |
| 718 |
: C2_L_PTINT; |
| 719 |
if (C2_PREDEFS[pele->l->predef].deprecated) { |
| 720 |
log_warn("Predefined target \"%s\" is " |
| 721 |
"deprecated. Matching against it will " |
| 722 |
"always fail. Offending pattern is " |
| 723 |
"\"%s\"", |
| 724 |
pele->l->tgt, pattern); |
| 725 |
*deprecated = true; |
| 726 |
} |
| 727 |
if (pele->l->op == C2_L_OEXISTS) { |
| 728 |
pele->l->ptntype = predef_type; |
| 729 |
} else if (pele->l->ptntype != predef_type) { |
| 730 |
c2_error("Predefined target %s is a %s, but you " |
| 731 |
"are comparing it with a %s", |
| 732 |
C2_PREDEFS[pele->l->predef].name, |
| 733 |
c2_pattern_type_names[predef_type], |
| 734 |
c2_pattern_type_names[pele->l->ptntype]); |
| 735 |
} |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
--offset; |
| 740 |
|
| 741 |
|
| 742 |
if (neg) { |
| 743 |
neg = false; |
| 744 |
pele->neg = !pele->neg; |
| 745 |
} |
| 746 |
|
| 747 |
next_expected = false; |
| 748 |
++elei; |
| 749 |
pele = &eles[elei]; |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
if (pattern[offset] && !endchar) { |
| 754 |
c2_error("Expected end of string but found '%c'.", pattern[offset]); |
| 755 |
} |
| 756 |
if (!pattern[offset] && endchar) { |
| 757 |
c2_error("Expected '%c' but found end of string.", endchar); |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
if (!elei) { |
| 762 |
c2_error("Empty group."); |
| 763 |
} else if (next_expected) { |
| 764 |
c2_error("Missing rule before end of group."); |
| 765 |
} else if (elei > 1) { |
| 766 |
assert(2 == elei); |
| 767 |
assert(ops[1]); |
| 768 |
eles[0] = c2h_comb_tree(ops[1], eles[0], eles[1]); |
| 769 |
eles[1] = C2_NODE_PTR_INIT; |
| 770 |
} |
| 771 |
|
| 772 |
*presult = eles[0]; |
| 773 |
|
| 774 |
if (')' == pattern[offset]) { |
| 775 |
++offset; |
| 776 |
} |
| 777 |
|
| 778 |
return offset; |
| 779 |
|
| 780 |
fail: |
| 781 |
c2_freep(&eles[0]); |
| 782 |
c2_freep(&eles[1]); |
| 783 |
|
| 784 |
return -1; |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
static int c2_parse_target(const char *pattern, int offset, c2_condition_node_ptr *presult) { |
| 791 |
|
| 792 |
presult->type = C2_NODE_TYPE_LEAF; |
| 793 |
presult->neg = false; |
| 794 |
presult->l = cmalloc(c2_condition_node_leaf); |
| 795 |
|
| 796 |
auto const pleaf = presult->l; |
| 797 |
*pleaf = C2_LEAF_NODE_INIT; |
| 798 |
|
| 799 |
|
| 800 |
while ('!' == pattern[offset]) { |
| 801 |
presult->neg = !presult->neg; |
| 802 |
++offset; |
| 803 |
C2H_SKIP_SPACES(); |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
int tgtlen = 0; |
| 808 |
for (; pattern[offset] && (isalnum((unsigned char)pattern[offset]) || |
| 809 |
'_' == pattern[offset] || '.' == pattern[offset]); |
| 810 |
++offset) { |
| 811 |
++tgtlen; |
| 812 |
} |
| 813 |
if (!tgtlen) { |
| 814 |
c2_error("Empty target."); |
| 815 |
} |
| 816 |
pleaf->tgt = strndup(&pattern[offset - tgtlen], (size_t)tgtlen); |
| 817 |
|
| 818 |
|
| 819 |
static const int npredefs = (int)(sizeof(C2_PREDEFS) / sizeof(C2_PREDEFS[0])); |
| 820 |
for (int i = 0; i < npredefs; ++i) { |
| 821 |
if (strcmp(C2_PREDEFS[i].name, pleaf->tgt) == 0) { |
| 822 |
pleaf->predef = i; |
| 823 |
break; |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
C2H_SKIP_SPACES(); |
| 828 |
|
| 829 |
|
| 830 |
if ('@' == pattern[offset]) { |
| 831 |
pleaf->target_on_client = true; |
| 832 |
++offset; |
| 833 |
C2H_SKIP_SPACES(); |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
if ('[' == pattern[offset]) { |
| 838 |
if (pleaf->predef != C2_L_PUNDEFINED) { |
| 839 |
c2_error("Predefined targets can't have index."); |
| 840 |
} |
| 841 |
|
| 842 |
offset++; |
| 843 |
|
| 844 |
C2H_SKIP_SPACES(); |
| 845 |
|
| 846 |
long index = -1; |
| 847 |
const char *endptr = NULL; |
| 848 |
|
| 849 |
if ('*' == pattern[offset]) { |
| 850 |
index = -1; |
| 851 |
endptr = pattern + offset + 1; |
| 852 |
} else { |
| 853 |
index = strtol(pattern + offset, (char **)&endptr, 0); |
| 854 |
if (index < 0) { |
| 855 |
c2_error("Index number invalid."); |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
if (!endptr || pattern + offset == endptr) { |
| 860 |
c2_error("No index number found after bracket."); |
| 861 |
} |
| 862 |
if (index > INT_MAX) { |
| 863 |
c2_error("Index %ld too large.", index); |
| 864 |
} |
| 865 |
|
| 866 |
pleaf->index = (int)index; |
| 867 |
offset = to_int_checked(endptr - pattern); |
| 868 |
|
| 869 |
C2H_SKIP_SPACES(); |
| 870 |
|
| 871 |
if (pattern[offset] != ']') { |
| 872 |
c2_error("Index end marker not found."); |
| 873 |
} |
| 874 |
|
| 875 |
++offset; |
| 876 |
|
| 877 |
C2H_SKIP_SPACES(); |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
if (':' == pattern[offset]) { |
| 882 |
++offset; |
| 883 |
C2H_SKIP_SPACES(); |
| 884 |
|
| 885 |
|
| 886 |
bool hasformat = false; |
| 887 |
long format = 0; |
| 888 |
char *endptr = NULL; |
| 889 |
format = strtol(pattern + offset, &endptr, 0); |
| 890 |
assert(endptr); |
| 891 |
hasformat = endptr && endptr != pattern + offset; |
| 892 |
if (hasformat) { |
| 893 |
offset = to_int_checked(endptr - pattern); |
| 894 |
} |
| 895 |
C2H_SKIP_SPACES(); |
| 896 |
|
| 897 |
|
| 898 |
switch (pattern[offset]) { |
| 899 |
case 'w': |
| 900 |
case 'd': |
| 901 |
case 'c': |
| 902 |
case 's': |
| 903 |
case 'a': break; |
| 904 |
default: c2_error("Invalid type character."); |
| 905 |
} |
| 906 |
|
| 907 |
log_warn("Type specifier is deprecated. Type \"%c\" specified on target " |
| 908 |
"\"%s\" will be ignored, you can remove it.", |
| 909 |
pattern[offset], pleaf->tgt); |
| 910 |
|
| 911 |
offset++; |
| 912 |
C2H_SKIP_SPACES(); |
| 913 |
|
| 914 |
|
| 915 |
if (hasformat) { |
| 916 |
log_warn("Format specifier is deprecated. Format \"%ld\" " |
| 917 |
"specified on target \"%s\" will be ignored, you can " |
| 918 |
"remove it.", |
| 919 |
format, pleaf->tgt); |
| 920 |
if (format && format != 8 && format != 16 && format != 32) { |
| 921 |
c2_error("Invalid format %ld.", format); |
| 922 |
} |
| 923 |
} |
| 924 |
} |
| 925 |
return offset; |
| 926 |
|
| 927 |
fail: |
| 928 |
return -1; |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
static int c2_parse_op(const char *pattern, int offset, c2_condition_node_ptr *presult) { |
| 935 |
auto const pleaf = presult->l; |
| 936 |
|
| 937 |
|
| 938 |
C2H_SKIP_SPACES(); |
| 939 |
while ('!' == pattern[offset]) { |
| 940 |
presult->neg = !presult->neg; |
| 941 |
++offset; |
| 942 |
C2H_SKIP_SPACES(); |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
if ('*' == pattern[offset] || '^' == pattern[offset] || '%' == pattern[offset] || |
| 947 |
'~' == pattern[offset]) { |
| 948 |
switch (pattern[offset]) { |
| 949 |
case '*': pleaf->match = C2_L_MCONTAINS; break; |
| 950 |
case '^': pleaf->match = C2_L_MSTART; break; |
| 951 |
case '%': pleaf->match = C2_L_MWILDCARD; break; |
| 952 |
case '~': pleaf->match = C2_L_MPCRE; break; |
| 953 |
default: assert(0); |
| 954 |
} |
| 955 |
++offset; |
| 956 |
C2H_SKIP_SPACES(); |
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
while ('?' == pattern[offset]) { |
| 961 |
pleaf->match_ignorecase = true; |
| 962 |
++offset; |
| 963 |
C2H_SKIP_SPACES(); |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
while ('=' == pattern[offset] || '>' == pattern[offset] || '<' == pattern[offset]) { |
| 968 |
if ('=' == pattern[offset] && C2_L_OGT == pleaf->op) { |
| 969 |
pleaf->op = C2_L_OGTEQ; |
| 970 |
} else if ('=' == pattern[offset] && C2_L_OLT == pleaf->op) { |
| 971 |
pleaf->op = C2_L_OLTEQ; |
| 972 |
} else if (pleaf->op) { |
| 973 |
c2_error("Duplicate operator."); |
| 974 |
} else { |
| 975 |
switch (pattern[offset]) { |
| 976 |
case '=': pleaf->op = C2_L_OEQ; break; |
| 977 |
case '>': pleaf->op = C2_L_OGT; break; |
| 978 |
case '<': pleaf->op = C2_L_OLT; break; |
| 979 |
default: assert(0); |
| 980 |
} |
| 981 |
} |
| 982 |
++offset; |
| 983 |
C2H_SKIP_SPACES(); |
| 984 |
} |
| 985 |
|
| 986 |
|
| 987 |
if (C2_L_OEQ != pleaf->op && (pleaf->match || pleaf->match_ignorecase)) { |
| 988 |
c2_error("Exists/greater-than/less-than operators cannot have a " |
| 989 |
"qualifier."); |
| 990 |
} |
| 991 |
|
| 992 |
return offset; |
| 993 |
|
| 994 |
fail: |
| 995 |
return -1; |
| 996 |
} |
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
static int c2_parse_pattern(const char *pattern, int offset, c2_condition_node_ptr *presult) { |
| 1002 |
auto const pleaf = presult->l; |
| 1003 |
|
| 1004 |
|
| 1005 |
if (!pleaf->op) { |
| 1006 |
return offset; |
| 1007 |
} |
| 1008 |
|
| 1009 |
C2H_SKIP_SPACES(); |
| 1010 |
|
| 1011 |
char *endptr = NULL; |
| 1012 |
if (!strcmp_wd("true", &pattern[offset])) { |
| 1013 |
pleaf->ptntype = C2_L_PTINT; |
| 1014 |
pleaf->ptnint = true; |
| 1015 |
offset += 4; |
| 1016 |
} else if (!strcmp_wd("false", &pattern[offset])) { |
| 1017 |
pleaf->ptntype = C2_L_PTINT; |
| 1018 |
pleaf->ptnint = false; |
| 1019 |
offset += 5; |
| 1020 |
} else if (pleaf->ptnint = strtol(pattern + offset, &endptr, 0), |
| 1021 |
pattern + offset != endptr) { |
| 1022 |
pleaf->ptntype = C2_L_PTINT; |
| 1023 |
offset = to_int_checked(endptr - pattern); |
| 1024 |
|
| 1025 |
if (isalnum((unsigned char)pattern[offset])) { |
| 1026 |
c2_error("Trailing characters after a numeric pattern."); |
| 1027 |
} |
| 1028 |
} else { |
| 1029 |
|
| 1030 |
bool raw = false; |
| 1031 |
char delim = '\0'; |
| 1032 |
|
| 1033 |
|
| 1034 |
if (tolower((unsigned char)pattern[offset]) == 'r') { |
| 1035 |
raw = true; |
| 1036 |
++offset; |
| 1037 |
C2H_SKIP_SPACES(); |
| 1038 |
} |
| 1039 |
|
| 1040 |
if (raw == true) { |
| 1041 |
log_warn("Raw string patterns has been deprecated. pos %d", offset); |
| 1042 |
} |
| 1043 |
|
| 1044 |
|
| 1045 |
if (pattern[offset] == '\"' || pattern[offset] == '\'') { |
| 1046 |
pleaf->ptntype = C2_L_PTSTRING; |
| 1047 |
delim = pattern[offset]; |
| 1048 |
++offset; |
| 1049 |
} |
| 1050 |
|
| 1051 |
if (pleaf->ptntype != C2_L_PTSTRING) { |
| 1052 |
c2_error("Invalid pattern type."); |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
auto tptnstr = ccalloc((strlen(pattern + offset) + 1), char); |
| 1060 |
char *ptptnstr = tptnstr; |
| 1061 |
pleaf->ptnstr = tptnstr; |
| 1062 |
for (; pattern[offset] && delim != pattern[offset]; ++offset) { |
| 1063 |
|
| 1064 |
if ('\\' == pattern[offset] && !raw) { |
| 1065 |
switch (pattern[++offset]) { |
| 1066 |
case '\\': *(ptptnstr++) = '\\'; break; |
| 1067 |
case '\'': *(ptptnstr++) = '\''; break; |
| 1068 |
case '\"': *(ptptnstr++) = '\"'; break; |
| 1069 |
case 'a': *(ptptnstr++) = '\a'; break; |
| 1070 |
case 'b': *(ptptnstr++) = '\b'; break; |
| 1071 |
case 'f': *(ptptnstr++) = '\f'; break; |
| 1072 |
case 'n': *(ptptnstr++) = '\n'; break; |
| 1073 |
case 'r': *(ptptnstr++) = '\r'; break; |
| 1074 |
case 't': *(ptptnstr++) = '\t'; break; |
| 1075 |
case 'v': *(ptptnstr++) = '\v'; break; |
| 1076 |
case 'o': |
| 1077 |
case 'x': { |
| 1078 |
scoped_charp tstr = strndup(pattern + offset + 1, 2); |
| 1079 |
char *pstr = NULL; |
| 1080 |
long val = strtol( |
| 1081 |
tstr, &pstr, ('o' == pattern[offset] ? 8 : 16)); |
| 1082 |
if (pstr != &tstr[2] || val <= 0) { |
| 1083 |
c2_error("Invalid octal/hex escape " |
| 1084 |
"sequence."); |
| 1085 |
} |
| 1086 |
if (val > 255) { |
| 1087 |
c2_error("Octal/hex escape sequence out " |
| 1088 |
"of ASCII range."); |
| 1089 |
} |
| 1090 |
if (val > 127) { |
| 1091 |
|
| 1092 |
val -= 256; |
| 1093 |
} |
| 1094 |
*(ptptnstr++) = (char)val; |
| 1095 |
offset += 2; |
| 1096 |
break; |
| 1097 |
} |
| 1098 |
default: c2_error("Invalid escape sequence."); |
| 1099 |
} |
| 1100 |
} else { |
| 1101 |
*(ptptnstr++) = pattern[offset]; |
| 1102 |
} |
| 1103 |
} |
| 1104 |
if (!pattern[offset]) { |
| 1105 |
c2_error("Premature end of pattern string."); |
| 1106 |
} |
| 1107 |
++offset; |
| 1108 |
*ptptnstr = '\0'; |
| 1109 |
pleaf->ptnstr = strdup(tptnstr); |
| 1110 |
free(tptnstr); |
| 1111 |
} |
| 1112 |
|
| 1113 |
C2H_SKIP_SPACES(); |
| 1114 |
|
| 1115 |
if (!pleaf->ptntype) { |
| 1116 |
c2_error("Invalid pattern type."); |
| 1117 |
} |
| 1118 |
|
| 1119 |
if (pleaf->ptntype == C2_L_PTINT) { |
| 1120 |
if (pleaf->match) { |
| 1121 |
c2_error("Integer/boolean pattern cannot have operator " |
| 1122 |
"qualifiers."); |
| 1123 |
} |
| 1124 |
if (pleaf->match_ignorecase) { |
| 1125 |
c2_error("Integer/boolean pattern cannot have flags."); |
| 1126 |
} |
| 1127 |
} |
| 1128 |
|
| 1129 |
if (C2_L_PTSTRING == pleaf->ptntype && |
| 1130 |
(C2_L_OGT == pleaf->op || C2_L_OGTEQ == pleaf->op || C2_L_OLT == pleaf->op || |
| 1131 |
C2_L_OLTEQ == pleaf->op)) { |
| 1132 |
c2_error("String pattern cannot have an arithmetic operator."); |
| 1133 |
} |
| 1134 |
|
| 1135 |
return offset; |
| 1136 |
|
| 1137 |
fail: |
| 1138 |
return -1; |
| 1139 |
} |
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
static int c2_parse_legacy(const char *pattern, int offset, c2_condition_node_ptr *presult) { |
| 1145 |
if (strlen(pattern + offset) < 4 || pattern[offset + 1] != ':' || |
| 1146 |
!strchr(pattern + offset + 2, ':')) { |
| 1147 |
c2_error("Legacy parser: Invalid format."); |
| 1148 |
} |
| 1149 |
|
| 1150 |
|
| 1151 |
auto pleaf = cmalloc(c2_condition_node_leaf); |
| 1152 |
presult->type = C2_NODE_TYPE_LEAF; |
| 1153 |
presult->l = pleaf; |
| 1154 |
presult->neg = false; |
| 1155 |
*pleaf = C2_LEAF_NODE_INIT; |
| 1156 |
pleaf->op = C2_L_OEQ; |
| 1157 |
pleaf->ptntype = C2_L_PTSTRING; |
| 1158 |
|
| 1159 |
|
| 1160 |
switch (pattern[offset]) { |
| 1161 |
case 'n': pleaf->predef = C2_L_PNAME; break; |
| 1162 |
case 'i': pleaf->predef = C2_L_PCLASSI; break; |
| 1163 |
case 'g': pleaf->predef = C2_L_PCLASSG; break; |
| 1164 |
case 'r': pleaf->predef = C2_L_PROLE; break; |
| 1165 |
default: c2_error("Target \"%c\" invalid.\n", pattern[offset]); |
| 1166 |
} |
| 1167 |
|
| 1168 |
offset += 2; |
| 1169 |
|
| 1170 |
|
| 1171 |
switch (pattern[offset]) { |
| 1172 |
case 'e': pleaf->match = C2_L_MEXACT; break; |
| 1173 |
case 'a': pleaf->match = C2_L_MCONTAINS; break; |
| 1174 |
case 's': pleaf->match = C2_L_MSTART; break; |
| 1175 |
case 'w': pleaf->match = C2_L_MWILDCARD; break; |
| 1176 |
case 'p': pleaf->match = C2_L_MPCRE; break; |
| 1177 |
default: c2_error("Type \"%c\" invalid.\n", pattern[offset]); |
| 1178 |
} |
| 1179 |
++offset; |
| 1180 |
|
| 1181 |
|
| 1182 |
while (':' != pattern[offset]) { |
| 1183 |
switch (pattern[offset]) { |
| 1184 |
case 'i': pleaf->match_ignorecase = true; break; |
| 1185 |
default: c2_error("Flag \"%c\" invalid.", pattern[offset]); |
| 1186 |
} |
| 1187 |
++offset; |
| 1188 |
} |
| 1189 |
++offset; |
| 1190 |
|
| 1191 |
|
| 1192 |
pleaf->ptnstr = strdup(pattern + offset); |
| 1193 |
|
| 1194 |
return offset; |
| 1195 |
|
| 1196 |
fail: |
| 1197 |
return -1; |
| 1198 |
} |
| 1199 |
|
| 1200 |
#undef c2_error |
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
static bool |
| 1206 |
c2_l_postprocess(struct c2_state *state, xcb_connection_t *c, c2_condition_node_leaf *pleaf) { |
| 1207 |
|
| 1208 |
if (pleaf->predef == C2_L_PUNDEFINED) { |
| 1209 |
pleaf->tgtatom = get_atom_with_nul(state->atoms, pleaf->tgt, c); |
| 1210 |
if (!pleaf->tgtatom) { |
| 1211 |
log_error("Failed to get atom for target \"%s\".", pleaf->tgt); |
| 1212 |
return false; |
| 1213 |
} |
| 1214 |
} else if (pleaf->predef == C2_L_PURGENT) { |
| 1215 |
pleaf->tgtatom = state->atoms->aWM_HINTS; |
| 1216 |
pleaf->target_on_client = true; |
| 1217 |
pleaf->index = 0; |
| 1218 |
} |
| 1219 |
|
| 1220 |
|
| 1221 |
if (pleaf->tgtatom) { |
| 1222 |
struct c2_tracked_property *property; |
| 1223 |
struct c2_tracked_property_key key = { |
| 1224 |
.property = pleaf->tgtatom, |
| 1225 |
.is_on_client = pleaf->target_on_client, |
| 1226 |
}; |
| 1227 |
HASH_FIND(hh, state->tracked_properties, &key, sizeof(key), property); |
| 1228 |
if (property == NULL) { |
| 1229 |
property = cmalloc(struct c2_tracked_property); |
| 1230 |
property->key = key; |
| 1231 |
property->id = HASH_COUNT(state->tracked_properties); |
| 1232 |
HASH_ADD_KEYPTR(hh, state->tracked_properties, &property->key, |
| 1233 |
sizeof(property->key), property); |
| 1234 |
} |
| 1235 |
pleaf->target_id = property->id; |
| 1236 |
} |
| 1237 |
|
| 1238 |
|
| 1239 |
if (pleaf->predef == C2_L_PUNDEFINED) { |
| 1240 |
for (const char *pc = pleaf->tgt; *pc; ++pc) { |
| 1241 |
if (islower((unsigned char)*pc)) { |
| 1242 |
log_warn("Lowercase character in target name \"%s\".", |
| 1243 |
pleaf->tgt); |
| 1244 |
break; |
| 1245 |
} |
| 1246 |
} |
| 1247 |
} |
| 1248 |
|
| 1249 |
|
| 1250 |
if (C2_L_PTSTRING == pleaf->ptntype && C2_L_MPCRE == pleaf->match) { |
| 1251 |
#ifdef CONFIG_REGEX_PCRE |
| 1252 |
int errorcode = 0; |
| 1253 |
PCRE2_SIZE erroffset = 0; |
| 1254 |
unsigned int options = 0; |
| 1255 |
|
| 1256 |
|
| 1257 |
if (pleaf->match_ignorecase) { |
| 1258 |
options |= PCRE2_CASELESS; |
| 1259 |
} |
| 1260 |
|
| 1261 |
|
| 1262 |
pleaf->regex_pcre = |
| 1263 |
pcre2_compile((PCRE2_SPTR)pleaf->ptnstr, PCRE2_ZERO_TERMINATED, |
| 1264 |
options, &errorcode, &erroffset, NULL); |
| 1265 |
if (pleaf->regex_pcre == NULL) { |
| 1266 |
PCRE2_UCHAR buffer[256]; |
| 1267 |
pcre2_get_error_message(errorcode, buffer, sizeof(buffer)); |
| 1268 |
log_error("Pattern \"%s\": PCRE regular expression " |
| 1269 |
"parsing failed on offset %zu: %s", |
| 1270 |
pleaf->ptnstr, erroffset, buffer); |
| 1271 |
return false; |
| 1272 |
} |
| 1273 |
pleaf->regex_pcre_match = |
| 1274 |
pcre2_match_data_create_from_pattern(pleaf->regex_pcre, NULL); |
| 1275 |
#else |
| 1276 |
log_error("PCRE regular expression support not compiled in."); |
| 1277 |
return false; |
| 1278 |
#endif |
| 1279 |
} |
| 1280 |
|
| 1281 |
return true; |
| 1282 |
} |
| 1283 |
|
| 1284 |
static bool |
| 1285 |
c2_tree_postprocess(struct c2_state *state, xcb_connection_t *c, c2_condition_node_ptr node) { |
| 1286 |
switch (node.type) { |
| 1287 |
case C2_NODE_TYPE_TRUE: return true; |
| 1288 |
case C2_NODE_TYPE_LEAF: return c2_l_postprocess(state, c, node.l); |
| 1289 |
case C2_NODE_TYPE_BRANCH: |
| 1290 |
return c2_tree_postprocess(state, c, node.b->opr1) && |
| 1291 |
c2_tree_postprocess(state, c, node.b->opr2); |
| 1292 |
default: unreachable(); |
| 1293 |
} |
| 1294 |
} |
| 1295 |
|
| 1296 |
bool c2_list_postprocess(struct c2_state *state, xcb_connection_t *c, struct list_node *list) { |
| 1297 |
list_foreach(c2_condition, i, list, siblings) { |
| 1298 |
if (!c2_tree_postprocess(state, c, i->root)) { |
| 1299 |
return false; |
| 1300 |
} |
| 1301 |
} |
| 1302 |
return true; |
| 1303 |
} |
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
static void c2_free(c2_condition_node_ptr p) { |
| 1308 |
|
| 1309 |
switch (p.type) { |
| 1310 |
case C2_NODE_TYPE_BRANCH: |
| 1311 |
if (p.b == NULL) { |
| 1312 |
return; |
| 1313 |
} |
| 1314 |
|
| 1315 |
c2_free(p.b->opr1); |
| 1316 |
c2_free(p.b->opr2); |
| 1317 |
free(p.b); |
| 1318 |
return; |
| 1319 |
case C2_NODE_TYPE_LEAF: |
| 1320 |
|
| 1321 |
if (!p.l) { |
| 1322 |
return; |
| 1323 |
} |
| 1324 |
|
| 1325 |
free(p.l->tgt); |
| 1326 |
free(p.l->ptnstr); |
| 1327 |
#ifdef CONFIG_REGEX_PCRE |
| 1328 |
pcre2_code_free(p.l->regex_pcre); |
| 1329 |
pcre2_match_data_free(p.l->regex_pcre_match); |
| 1330 |
#endif |
| 1331 |
free(p.l); |
| 1332 |
default: return; |
| 1333 |
} |
| 1334 |
} |
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
void c2_free_condition(c2_condition *lp, c2_userdata_free f) { |
| 1340 |
if (!lp) { |
| 1341 |
return; |
| 1342 |
} |
| 1343 |
|
| 1344 |
if (f) { |
| 1345 |
f(lp->data); |
| 1346 |
} |
| 1347 |
lp->data = NULL; |
| 1348 |
c2_free(lp->root); |
| 1349 |
free(lp); |
| 1350 |
} |
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
static const char *c2h_dump_str_tgt(const c2_condition_node_leaf *pleaf) { |
| 1356 |
if (pleaf->predef != C2_L_PUNDEFINED) { |
| 1357 |
return C2_PREDEFS[pleaf->predef].name; |
| 1358 |
} |
| 1359 |
return pleaf->tgt; |
| 1360 |
} |
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
static size_t |
| 1369 |
c2_condition_node_to_str(const c2_condition_node_ptr p, char *output, size_t len) { |
| 1370 |
#define push_char(c) \ |
| 1371 |
if (offset < len) \ |
| 1372 |
output[offset] = (c); \ |
| 1373 |
offset++ |
| 1374 |
#define push_str(str) \ |
| 1375 |
do { \ |
| 1376 |
if (offset < len) { \ |
| 1377 |
size_t slen = strlen(str); \ |
| 1378 |
if (offset + slen > len) \ |
| 1379 |
slen = len - offset; \ |
| 1380 |
memcpy(output + offset, str, slen); \ |
| 1381 |
} \ |
| 1382 |
offset += strlen(str); \ |
| 1383 |
} while (false) |
| 1384 |
size_t offset = 0; |
| 1385 |
char number[128]; |
| 1386 |
switch (p.type) { |
| 1387 |
case C2_NODE_TYPE_BRANCH: |
| 1388 |
|
| 1389 |
if (p.b == NULL) { |
| 1390 |
return 0; |
| 1391 |
} |
| 1392 |
|
| 1393 |
if (p.neg) { |
| 1394 |
push_char('!'); |
| 1395 |
} |
| 1396 |
|
| 1397 |
push_char('('); |
| 1398 |
if (len > offset) { |
| 1399 |
offset += c2_condition_node_to_str(p.b->opr1, output + offset, |
| 1400 |
len - offset); |
| 1401 |
} else { |
| 1402 |
offset += c2_condition_node_to_str(p.b->opr1, NULL, 0); |
| 1403 |
} |
| 1404 |
|
| 1405 |
switch (p.b->op) { |
| 1406 |
case C2_B_OAND: push_str(" && "); break; |
| 1407 |
case C2_B_OOR: push_str(" || "); break; |
| 1408 |
case C2_B_OXOR: push_str(" XOR "); break; |
| 1409 |
default: assert(0); break; |
| 1410 |
} |
| 1411 |
|
| 1412 |
if (len > offset) { |
| 1413 |
offset += c2_condition_node_to_str(p.b->opr2, output + offset, |
| 1414 |
len - offset); |
| 1415 |
} else { |
| 1416 |
offset += c2_condition_node_to_str(p.b->opr2, NULL, 0); |
| 1417 |
} |
| 1418 |
push_str(")"); |
| 1419 |
break; |
| 1420 |
case C2_NODE_TYPE_LEAF: |
| 1421 |
|
| 1422 |
if (!p.l) { |
| 1423 |
return 0; |
| 1424 |
} |
| 1425 |
|
| 1426 |
if (C2_L_OEXISTS == p.l->op && p.neg) { |
| 1427 |
push_char('!'); |
| 1428 |
} |
| 1429 |
|
| 1430 |
|
| 1431 |
const char *target_str = c2h_dump_str_tgt(p.l); |
| 1432 |
push_str(target_str); |
| 1433 |
if (p.l->target_on_client) { |
| 1434 |
push_char('@'); |
| 1435 |
} |
| 1436 |
if (p.l->predef == C2_L_PUNDEFINED) { |
| 1437 |
if (p.l->index < 0) { |
| 1438 |
push_str("[*]"); |
| 1439 |
} else { |
| 1440 |
sprintf(number, "[%d]", p.l->index); |
| 1441 |
push_str(number); |
| 1442 |
} |
| 1443 |
} |
| 1444 |
|
| 1445 |
if (C2_L_OEXISTS == p.l->op) { |
| 1446 |
return offset; |
| 1447 |
} |
| 1448 |
|
| 1449 |
|
| 1450 |
push_char(' '); |
| 1451 |
|
| 1452 |
if (C2_L_OEXISTS != p.l->op && p.neg) { |
| 1453 |
push_char('!'); |
| 1454 |
} |
| 1455 |
|
| 1456 |
switch (p.l->match) { |
| 1457 |
case C2_L_MEXACT: break; |
| 1458 |
case C2_L_MCONTAINS: push_char('*'); break; |
| 1459 |
case C2_L_MSTART: push_char('^'); break; |
| 1460 |
case C2_L_MPCRE: push_char('~'); break; |
| 1461 |
case C2_L_MWILDCARD: push_char('%'); break; |
| 1462 |
} |
| 1463 |
|
| 1464 |
if (p.l->match_ignorecase) { |
| 1465 |
push_char('?'); |
| 1466 |
} |
| 1467 |
|
| 1468 |
switch (p.l->op) { |
| 1469 |
case C2_L_OEXISTS: break; |
| 1470 |
case C2_L_OEQ: push_str("="); break; |
| 1471 |
case C2_L_OGT: push_str(">"); break; |
| 1472 |
case C2_L_OGTEQ: push_str(">="); break; |
| 1473 |
case C2_L_OLT: push_str("<"); break; |
| 1474 |
case C2_L_OLTEQ: push_str("<="); break; |
| 1475 |
} |
| 1476 |
|
| 1477 |
|
| 1478 |
push_char(' '); |
| 1479 |
switch (p.l->ptntype) { |
| 1480 |
case C2_L_PTINT: |
| 1481 |
sprintf(number, "%ld", p.l->ptnint); |
| 1482 |
push_str(number); |
| 1483 |
break; |
| 1484 |
case C2_L_PTSTRING: |
| 1485 |
|
| 1486 |
push_char('"'); |
| 1487 |
for (int i = 0; p.l->ptnstr[i]; i++) { |
| 1488 |
switch (p.l->ptnstr[i]) { |
| 1489 |
case '\\': push_str("\\\\"); break; |
| 1490 |
case '"': push_str("\\\""); break; |
| 1491 |
case '\a': push_str("\\a"); break; |
| 1492 |
case '\b': push_str("\\b"); break; |
| 1493 |
case '\f': push_str("\\f"); break; |
| 1494 |
case '\r': push_str("\\r"); break; |
| 1495 |
case '\v': push_str("\\v"); break; |
| 1496 |
case '\t': push_str("\\t"); break; |
| 1497 |
case '\n': push_str("\\n"); break; |
| 1498 |
default: |
| 1499 |
if (isprint(p.l->ptnstr[i])) { |
| 1500 |
push_char(p.l->ptnstr[i]); |
| 1501 |
} else { |
| 1502 |
sprintf(number, "\\x%02x", |
| 1503 |
(unsigned char)p.l->ptnstr[i]); |
| 1504 |
push_str(number); |
| 1505 |
} |
| 1506 |
break; |
| 1507 |
} |
| 1508 |
} |
| 1509 |
push_char('"'); |
| 1510 |
break; |
| 1511 |
default: assert(0); break; |
| 1512 |
} |
| 1513 |
break; |
| 1514 |
case C2_NODE_TYPE_TRUE: push_str("(default)"); break; |
| 1515 |
default: unreachable(); |
| 1516 |
} |
| 1517 |
#undef push_char |
| 1518 |
#undef push_str |
| 1519 |
return offset; |
| 1520 |
} |
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
static const char *c2_condition_node_to_str2(c2_condition_node_ptr ptr) { |
| 1526 |
static thread_local char buf[4096]; |
| 1527 |
auto len = c2_condition_node_to_str(ptr, buf, sizeof(buf)); |
| 1528 |
if (len >= sizeof(buf)) { |
| 1529 |
|
| 1530 |
buf[sizeof(buf) - 1] = '\0'; |
| 1531 |
} else { |
| 1532 |
buf[len] = '\0'; |
| 1533 |
} |
| 1534 |
return buf; |
| 1535 |
} |
| 1536 |
|
| 1537 |
const char *c2_condition_to_str(const c2_condition *ptr) { |
| 1538 |
return c2_condition_node_to_str2(ptr->root); |
| 1539 |
} |
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
static inline const int64_t * |
| 1547 |
c2_values_get_number_targets(const struct c2_property_value *values, int index, size_t *n) { |
| 1548 |
auto storage = values->numbers; |
| 1549 |
if (values->length > ARR_SIZE(values->numbers)) { |
| 1550 |
storage = values->array; |
| 1551 |
} |
| 1552 |
if (index < 0) { |
| 1553 |
*n = values->length; |
| 1554 |
return storage; |
| 1555 |
} |
| 1556 |
if ((size_t)index < values->length) { |
| 1557 |
*n = 1; |
| 1558 |
return &storage[index]; |
| 1559 |
} |
| 1560 |
*n = 0; |
| 1561 |
return NULL; |
| 1562 |
} |
| 1563 |
|
| 1564 |
static inline bool c2_int_op(const c2_condition_node_leaf *leaf, int64_t target) { |
| 1565 |
switch (leaf->op) { |
| 1566 |
case C2_L_OEXISTS: return leaf->predef != C2_L_PUNDEFINED ? target : true; |
| 1567 |
case C2_L_OEQ: return target == leaf->ptnint; |
| 1568 |
case C2_L_OGT: return target > leaf->ptnint; |
| 1569 |
case C2_L_OGTEQ: return target >= leaf->ptnint; |
| 1570 |
case C2_L_OLT: return target < leaf->ptnint; |
| 1571 |
case C2_L_OLTEQ: return target <= leaf->ptnint; |
| 1572 |
} |
| 1573 |
unreachable(); |
| 1574 |
} |
| 1575 |
|
| 1576 |
|
| 1577 |
typedef enum { |
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
XCB_ICCCM_WM_HINT_X_URGENCY = (1L << 8) |
| 1588 |
} xcb_icccm_wm_t; |
| 1589 |
|
| 1590 |
static const int64_t * |
| 1591 |
c2_window_state_get_number_for_leaf(const struct win *w, |
| 1592 |
const c2_condition_node_leaf *leaf, size_t *ntargets) { |
| 1593 |
*ntargets = 0; |
| 1594 |
if (leaf->target_id == C2_L_INVALID_TARGET_ID) { |
| 1595 |
log_debug("Leaf target ID is invalid, skipping. Most likely a list " |
| 1596 |
"postprocessing failure."); |
| 1597 |
return NULL; |
| 1598 |
} |
| 1599 |
auto values = &w->c2_state.values[leaf->target_id]; |
| 1600 |
assert(!values->needs_update); |
| 1601 |
if (!values->valid) { |
| 1602 |
log_verbose("Property %s not found on %s window %#010x (%s)", leaf->tgt, |
| 1603 |
leaf->target_on_client ? "the client window of" : "", |
| 1604 |
wm_ref_win_id(w->tree_ref), w->name); |
| 1605 |
return NULL; |
| 1606 |
} |
| 1607 |
|
| 1608 |
if (values->type == C2_PROPERTY_TYPE_STRING) { |
| 1609 |
log_error("Property %s is not an integer", leaf->tgt); |
| 1610 |
return NULL; |
| 1611 |
} |
| 1612 |
|
| 1613 |
return c2_values_get_number_targets(values, leaf->index, ntargets); |
| 1614 |
} |
| 1615 |
|
| 1616 |
static bool c2_match_once_leaf_int(const struct win *w, const c2_condition_node_leaf *leaf) { |
| 1617 |
size_t ntargets; |
| 1618 |
const int64_t *targets; |
| 1619 |
|
| 1620 |
if (leaf->predef != C2_L_PUNDEFINED) { |
| 1621 |
|
| 1622 |
int64_t predef_target = 0; |
| 1623 |
if (C2_PREDEFS[leaf->predef].deprecated) { |
| 1624 |
return false; |
| 1625 |
} |
| 1626 |
|
| 1627 |
switch (leaf->predef) { |
| 1628 |
case C2_L_PX: predef_target = w->g.x; break; |
| 1629 |
case C2_L_PY: predef_target = w->g.y; break; |
| 1630 |
case C2_L_PX2: predef_target = w->g.x + w->widthb; break; |
| 1631 |
case C2_L_PY2: predef_target = w->g.y + w->heightb; break; |
| 1632 |
case C2_L_PWIDTH: predef_target = w->g.width; break; |
| 1633 |
case C2_L_PHEIGHT: predef_target = w->g.height; break; |
| 1634 |
case C2_L_PWIDTHB: predef_target = w->widthb; break; |
| 1635 |
case C2_L_PHEIGHTB: predef_target = w->heightb; break; |
| 1636 |
case C2_L_PBDW: predef_target = w->g.border_width; break; |
| 1637 |
case C2_L_PFULLSCREEN: predef_target = w->is_fullscreen; break; |
| 1638 |
case C2_L_PURGENT: |
| 1639 |
targets = c2_window_state_get_number_for_leaf(w, leaf, &ntargets); |
| 1640 |
if (!targets) { |
| 1641 |
return false; |
| 1642 |
} |
| 1643 |
assert(ntargets == 1); |
| 1644 |
predef_target = (*targets & XCB_ICCCM_WM_HINT_X_URGENCY) != 0; |
| 1645 |
break; |
| 1646 |
case C2_L_PARGB: predef_target = win_has_alpha(w); break; |
| 1647 |
case C2_L_PFOCUSED: |
| 1648 |
predef_target = |
| 1649 |
w->a.map_state == XCB_MAP_STATE_VIEWABLE && w->is_focused; |
| 1650 |
break; |
| 1651 |
case C2_L_PGROUPFOCUSED: |
| 1652 |
predef_target = w->a.map_state == XCB_MAP_STATE_VIEWABLE && |
| 1653 |
w->is_group_focused; |
| 1654 |
break; |
| 1655 |
case C2_L_PWMWIN: predef_target = win_is_wmwin(w); break; |
| 1656 |
case C2_L_PBSHAPED: predef_target = w->bounding_shaped; break; |
| 1657 |
case C2_L_PROUNDED: predef_target = w->rounded_corners; break; |
| 1658 |
case C2_L_POVREDIR: |
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
predef_target = w->a.override_redirect && |
| 1665 |
wm_ref_client_of(w->tree_ref) == NULL; |
| 1666 |
break; |
| 1667 |
default: unreachable(); |
| 1668 |
} |
| 1669 |
return c2_int_op(leaf, predef_target); |
| 1670 |
} |
| 1671 |
|
| 1672 |
|
| 1673 |
targets = c2_window_state_get_number_for_leaf(w, leaf, &ntargets); |
| 1674 |
for (size_t i = 0; i < ntargets; i++) { |
| 1675 |
if (c2_int_op(leaf, targets[i])) { |
| 1676 |
return true; |
| 1677 |
} |
| 1678 |
} |
| 1679 |
return false; |
| 1680 |
} |
| 1681 |
|
| 1682 |
static bool c2_string_op(const c2_condition_node_leaf *leaf, const char *target) { |
| 1683 |
if (leaf->op == C2_L_OEXISTS) { |
| 1684 |
return true; |
| 1685 |
} |
| 1686 |
if (leaf->op != C2_L_OEQ) { |
| 1687 |
log_error("Unsupported operator %d for string comparison.", leaf->op); |
| 1688 |
assert(leaf->op == C2_L_OEQ); |
| 1689 |
return false; |
| 1690 |
} |
| 1691 |
if (leaf->match == C2_L_MPCRE) { |
| 1692 |
#ifdef CONFIG_REGEX_PCRE |
| 1693 |
assert(strlen(target) <= INT_MAX); |
| 1694 |
assert(leaf->regex_pcre); |
| 1695 |
return (pcre2_match(leaf->regex_pcre, (PCRE2_SPTR)target, strlen(target), |
| 1696 |
0, 0, leaf->regex_pcre_match, NULL) > 0); |
| 1697 |
#else |
| 1698 |
log_error("PCRE regular expression support not compiled in."); |
| 1699 |
assert(leaf->match != C2_L_MPCRE); |
| 1700 |
return false; |
| 1701 |
#endif |
| 1702 |
} |
| 1703 |
if (leaf->match_ignorecase) { |
| 1704 |
switch (leaf->match) { |
| 1705 |
case C2_L_MEXACT: return strcasecmp(target, leaf->ptnstr) == 0; |
| 1706 |
case C2_L_MCONTAINS: return strcasestr(target, leaf->ptnstr); |
| 1707 |
case C2_L_MSTART: |
| 1708 |
return strncasecmp(target, leaf->ptnstr, strlen(leaf->ptnstr)) == 0; |
| 1709 |
case C2_L_MWILDCARD: return !fnmatch(leaf->ptnstr, target, FNM_CASEFOLD); |
| 1710 |
default: unreachable(); |
| 1711 |
} |
| 1712 |
} else { |
| 1713 |
switch (leaf->match) { |
| 1714 |
case C2_L_MEXACT: return strcmp(target, leaf->ptnstr) == 0; |
| 1715 |
case C2_L_MCONTAINS: return strstr(target, leaf->ptnstr); |
| 1716 |
case C2_L_MSTART: |
| 1717 |
return strncmp(target, leaf->ptnstr, strlen(leaf->ptnstr)) == 0; |
| 1718 |
case C2_L_MWILDCARD: return !fnmatch(leaf->ptnstr, target, 0); |
| 1719 |
default: unreachable(); |
| 1720 |
} |
| 1721 |
} |
| 1722 |
unreachable(); |
| 1723 |
} |
| 1724 |
|
| 1725 |
static bool c2_match_once_leaf_string(struct atom *atoms, const struct win *w, |
| 1726 |
const c2_condition_node_leaf *leaf) { |
| 1727 |
|
| 1728 |
const char *predef_target = NULL; |
| 1729 |
if (leaf->predef != C2_L_PUNDEFINED) { |
| 1730 |
if (leaf->predef == C2_L_PWINDOWTYPE) { |
| 1731 |
for (unsigned i = 0; i < NUM_WINTYPES; i++) { |
| 1732 |
if (w->window_types & (1 << i) && |
| 1733 |
c2_string_op(leaf, WINTYPES[i].name)) { |
| 1734 |
return true; |
| 1735 |
} |
| 1736 |
} |
| 1737 |
return false; |
| 1738 |
} |
| 1739 |
|
| 1740 |
switch (leaf->predef) { |
| 1741 |
case C2_L_PNAME: predef_target = w->name; break; |
| 1742 |
case C2_L_PCLASSG: predef_target = w->class_general; break; |
| 1743 |
case C2_L_PCLASSI: predef_target = w->class_instance; break; |
| 1744 |
case C2_L_PROLE: predef_target = w->role; break; |
| 1745 |
case C2_L_PWINDOWTYPE: |
| 1746 |
default: unreachable(); |
| 1747 |
} |
| 1748 |
if (!predef_target) { |
| 1749 |
return false; |
| 1750 |
} |
| 1751 |
log_verbose("Matching against predefined target %s", predef_target); |
| 1752 |
return c2_string_op(leaf, predef_target); |
| 1753 |
} |
| 1754 |
|
| 1755 |
if (leaf->target_id == C2_L_INVALID_TARGET_ID) { |
| 1756 |
log_debug("Leaf target ID is invalid, skipping. Most likely a list " |
| 1757 |
"postprocessing failure."); |
| 1758 |
return false; |
| 1759 |
} |
| 1760 |
auto values = &w->c2_state.values[leaf->target_id]; |
| 1761 |
assert(!values->needs_update); |
| 1762 |
if (!values->valid) { |
| 1763 |
log_verbose("Property %s not found on window %#010x, client %#010x (%s)", |
| 1764 |
leaf->tgt, win_id(w), win_client_id(w, false), w->name); |
| 1765 |
return false; |
| 1766 |
} |
| 1767 |
|
| 1768 |
if (values->type == C2_PROPERTY_TYPE_ATOM) { |
| 1769 |
size_t ntargets = 0; |
| 1770 |
auto targets = c2_values_get_number_targets(values, leaf->index, &ntargets); |
| 1771 |
|
| 1772 |
for (size_t i = 0; i < ntargets; ++i) { |
| 1773 |
auto atom = (xcb_atom_t)targets[i]; |
| 1774 |
const char *atom_name = get_atom_name_cached(atoms, atom); |
| 1775 |
log_verbose("(%zu/%zu) Atom %u is %s", i, ntargets, atom, atom_name); |
| 1776 |
assert(atom_name != NULL); |
| 1777 |
if (atom_name && c2_string_op(leaf, atom_name)) { |
| 1778 |
return true; |
| 1779 |
} |
| 1780 |
} |
| 1781 |
return false; |
| 1782 |
} |
| 1783 |
|
| 1784 |
if (values->type != C2_PROPERTY_TYPE_STRING) { |
| 1785 |
log_verbose("Property %s is not a string", leaf->tgt); |
| 1786 |
return false; |
| 1787 |
} |
| 1788 |
|
| 1789 |
|
| 1790 |
if (leaf->index < 0) { |
| 1791 |
size_t offset = 0; |
| 1792 |
while (offset < values->length) { |
| 1793 |
if (c2_string_op(leaf, values->string + offset)) { |
| 1794 |
return true; |
| 1795 |
} |
| 1796 |
offset += strlen(values->string + offset) + 1; |
| 1797 |
} |
| 1798 |
return false; |
| 1799 |
} |
| 1800 |
size_t offset = 0; |
| 1801 |
int index = leaf->index; |
| 1802 |
while (offset < values->length && index != 0) { |
| 1803 |
offset += strlen(values->string + offset) + 1; |
| 1804 |
index -= 1; |
| 1805 |
} |
| 1806 |
if (index != 0 || values->length == 0) { |
| 1807 |
|
| 1808 |
return false; |
| 1809 |
} |
| 1810 |
return c2_string_op(leaf, values->string + offset); |
| 1811 |
} |
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
static inline bool c2_match_once_leaf(const struct c2_state *state, const struct win *w, |
| 1819 |
const c2_condition_node_ptr leaf) { |
| 1820 |
assert(leaf.type == C2_NODE_TYPE_LEAF); |
| 1821 |
assert(leaf.l); |
| 1822 |
|
| 1823 |
const xcb_window_t wid = |
| 1824 |
(leaf.l->target_on_client ? win_client_id(w, true) |
| 1825 |
: win_id(w)); |
| 1826 |
|
| 1827 |
|
| 1828 |
if (leaf.l->predef == C2_L_PUNDEFINED && !wid) { |
| 1829 |
log_debug("Window ID missing."); |
| 1830 |
return false; |
| 1831 |
} |
| 1832 |
|
| 1833 |
log_verbose("Matching window %#010x (%s) against condition %s", wid, w->name, |
| 1834 |
c2_condition_node_to_str2(leaf)); |
| 1835 |
|
| 1836 |
unsigned int pattern_type = leaf.l->ptntype; |
| 1837 |
if (pattern_type == C2_L_PTUNDEFINED) { |
| 1838 |
if (leaf.l->target_id == C2_L_INVALID_TARGET_ID) { |
| 1839 |
log_debug("Leaf target ID is invalid, skipping. Most likely a " |
| 1840 |
"list postprocessing failure."); |
| 1841 |
return false; |
| 1842 |
} |
| 1843 |
auto values = &w->c2_state.values[leaf.l->target_id]; |
| 1844 |
if (values->type == C2_PROPERTY_TYPE_STRING) { |
| 1845 |
pattern_type = C2_L_PTSTRING; |
| 1846 |
} else { |
| 1847 |
pattern_type = C2_L_PTINT; |
| 1848 |
} |
| 1849 |
} |
| 1850 |
|
| 1851 |
switch (pattern_type) { |
| 1852 |
|
| 1853 |
case C2_L_PTINT: return c2_match_once_leaf_int(w, leaf.l); |
| 1854 |
|
| 1855 |
case C2_L_PTSTRING: return c2_match_once_leaf_string(state->atoms, w, leaf.l); |
| 1856 |
default: unreachable(); |
| 1857 |
} |
| 1858 |
} |
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
static bool c2_match_once(const struct c2_state *state, const struct win *w, |
| 1866 |
const c2_condition_node_ptr node) { |
| 1867 |
bool result = false; |
| 1868 |
|
| 1869 |
switch (node.type) { |
| 1870 |
case C2_NODE_TYPE_BRANCH: |
| 1871 |
|
| 1872 |
if (!node.b) { |
| 1873 |
return false; |
| 1874 |
} |
| 1875 |
|
| 1876 |
log_verbose("Matching window %#010x (%s) against condition %s", win_id(w), |
| 1877 |
w->name, c2_condition_node_to_str2(node)); |
| 1878 |
|
| 1879 |
switch (node.b->op) { |
| 1880 |
case C2_B_OAND: |
| 1881 |
result = (c2_match_once(state, w, node.b->opr1) && |
| 1882 |
c2_match_once(state, w, node.b->opr2)); |
| 1883 |
break; |
| 1884 |
case C2_B_OOR: |
| 1885 |
result = (c2_match_once(state, w, node.b->opr1) || |
| 1886 |
c2_match_once(state, w, node.b->opr2)); |
| 1887 |
break; |
| 1888 |
case C2_B_OXOR: |
| 1889 |
result = (c2_match_once(state, w, node.b->opr1) != |
| 1890 |
c2_match_once(state, w, node.b->opr2)); |
| 1891 |
break; |
| 1892 |
default: unreachable(); |
| 1893 |
} |
| 1894 |
|
| 1895 |
log_debug("(%#010x): branch: result = %d, pattern = %s", win_id(w), |
| 1896 |
result, c2_condition_node_to_str2(node)); |
| 1897 |
break; |
| 1898 |
case C2_NODE_TYPE_TRUE: return true; |
| 1899 |
case C2_NODE_TYPE_LEAF: |
| 1900 |
|
| 1901 |
if (node.l == NULL) { |
| 1902 |
return false; |
| 1903 |
} |
| 1904 |
|
| 1905 |
result = c2_match_once_leaf(state, w, node); |
| 1906 |
|
| 1907 |
log_debug("(%#010x): leaf: result = %d, client = %#010x, pattern = %s", |
| 1908 |
win_id(w), result, win_client_id(w, false), |
| 1909 |
c2_condition_node_to_str2(node)); |
| 1910 |
break; |
| 1911 |
default: unreachable(); |
| 1912 |
} |
| 1913 |
|
| 1914 |
|
| 1915 |
if (node.neg) { |
| 1916 |
result = !result; |
| 1917 |
} |
| 1918 |
|
| 1919 |
return result; |
| 1920 |
} |
| 1921 |
|
| 1922 |
c2_condition *c2_new_true(struct list_node *list) { |
| 1923 |
auto ret = ccalloc(1, c2_condition); |
| 1924 |
ret->root = (c2_condition_node_ptr){.type = C2_NODE_TYPE_TRUE}; |
| 1925 |
if (list) { |
| 1926 |
list_insert_after(list, &ret->siblings); |
| 1927 |
} |
| 1928 |
return ret; |
| 1929 |
} |
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
|
| 1937 |
|
| 1938 |
bool c2_match(struct c2_state *state, const struct win *w, |
| 1939 |
const struct list_node *conditions, void **pdata) { |
| 1940 |
|
| 1941 |
list_foreach(c2_condition, i, conditions, siblings) { |
| 1942 |
if (c2_match_once(state, w, i->root)) { |
| 1943 |
if (pdata) { |
| 1944 |
*pdata = i->data; |
| 1945 |
} |
| 1946 |
return true; |
| 1947 |
} |
| 1948 |
} |
| 1949 |
|
| 1950 |
return false; |
| 1951 |
} |
| 1952 |
|
| 1953 |
|
| 1954 |
bool c2_match_one(const struct c2_state *state, const struct win *w, |
| 1955 |
const c2_condition *condition, void **pdata) { |
| 1956 |
if (!condition) { |
| 1957 |
return false; |
| 1958 |
} |
| 1959 |
if (c2_match_once(state, w, condition->root)) { |
| 1960 |
if (pdata) { |
| 1961 |
*pdata = condition->data; |
| 1962 |
} |
| 1963 |
return true; |
| 1964 |
} |
| 1965 |
return false; |
| 1966 |
} |
| 1967 |
|
| 1968 |
|
| 1969 |
void *c2_condition_get_data(const c2_condition *condition) { |
| 1970 |
return condition->data; |
| 1971 |
} |
| 1972 |
|
| 1973 |
void *c2_condition_set_data(c2_condition *condition, void *data) { |
| 1974 |
void *old = condition->data; |
| 1975 |
condition->data = data; |
| 1976 |
return old; |
| 1977 |
} |
| 1978 |
|
| 1979 |
c2_condition *c2_condition_list_entry(struct list_node *list) { |
| 1980 |
return list == NULL ? NULL : list_entry(list, c2_condition, siblings); |
| 1981 |
} |
| 1982 |
|
| 1983 |
c2_condition *c2_condition_list_next(struct list_node *list, c2_condition *condition) { |
| 1984 |
if (condition == NULL) { |
| 1985 |
return NULL; |
| 1986 |
} |
| 1987 |
if (condition->siblings.next == list) { |
| 1988 |
return NULL; |
| 1989 |
} |
| 1990 |
return c2_condition_list_entry(condition->siblings.next); |
| 1991 |
} |
| 1992 |
|
| 1993 |
c2_condition *c2_condition_list_prev(struct list_node *list, c2_condition *condition) { |
| 1994 |
if (condition == NULL) { |
| 1995 |
return NULL; |
| 1996 |
} |
| 1997 |
if (condition->siblings.prev == list) { |
| 1998 |
return NULL; |
| 1999 |
} |
| 2000 |
return c2_condition_list_entry(condition->siblings.prev); |
| 2001 |
} |
| 2002 |
|
| 2003 |
struct c2_state *c2_state_new(struct atom *atoms) { |
| 2004 |
auto ret = ccalloc(1, struct c2_state); |
| 2005 |
ret->atoms = atoms; |
| 2006 |
return ret; |
| 2007 |
} |
| 2008 |
|
| 2009 |
void c2_state_free(struct c2_state *state) { |
| 2010 |
struct c2_tracked_property *property, *tmp; |
| 2011 |
HASH_ITER(hh, state->tracked_properties, property, tmp) { |
| 2012 |
HASH_DEL(state->tracked_properties, property); |
| 2013 |
free(property); |
| 2014 |
} |
| 2015 |
free(state->cookies); |
| 2016 |
free(state); |
| 2017 |
} |
| 2018 |
|
| 2019 |
void c2_window_state_init(const struct c2_state *state, struct c2_window_state *window_state) { |
| 2020 |
auto property_count = HASH_COUNT(state->tracked_properties); |
| 2021 |
window_state->values = ccalloc(property_count, struct c2_property_value); |
| 2022 |
for (size_t i = 0; i < property_count; i++) { |
| 2023 |
window_state->values[i].needs_update = true; |
| 2024 |
window_state->values[i].valid = false; |
| 2025 |
} |
| 2026 |
} |
| 2027 |
|
| 2028 |
void c2_window_state_destroy(const struct c2_state *state, |
| 2029 |
struct c2_window_state *window_state) { |
| 2030 |
size_t property_count = HASH_COUNT(state->tracked_properties); |
| 2031 |
for (size_t i = 0; i < property_count; i++) { |
| 2032 |
auto values = &window_state->values[i]; |
| 2033 |
if (values->type == C2_PROPERTY_TYPE_STRING) { |
| 2034 |
free(window_state->values[i].string); |
| 2035 |
} else if (values->length > ARR_SIZE(values->numbers)) { |
| 2036 |
free(window_state->values[i].array); |
| 2037 |
} |
| 2038 |
} |
| 2039 |
free(window_state->values); |
| 2040 |
} |
| 2041 |
|
| 2042 |
void c2_window_state_mark_dirty(const struct c2_state *state, |
| 2043 |
struct c2_window_state *window_state, xcb_atom_t property, |
| 2044 |
bool is_on_client) { |
| 2045 |
struct c2_tracked_property *p; |
| 2046 |
struct c2_tracked_property_key key = { |
| 2047 |
.property = property, |
| 2048 |
.is_on_client = is_on_client, |
| 2049 |
}; |
| 2050 |
HASH_FIND(hh, state->tracked_properties, &key, sizeof(key), p); |
| 2051 |
if (p) { |
| 2052 |
window_state->values[p->id].needs_update = true; |
| 2053 |
} |
| 2054 |
} |
| 2055 |
|
| 2056 |
void c2_window_state_mark_dirty_for_client_change(const struct c2_state *state, |
| 2057 |
struct c2_window_state *window_state) { |
| 2058 |
struct c2_tracked_property *i, *next; |
| 2059 |
HASH_ITER(hh, state->tracked_properties, i, next) { |
| 2060 |
if (!i->key.is_on_client) { |
| 2061 |
continue; |
| 2062 |
} |
| 2063 |
window_state->values[i->id].needs_update = true; |
| 2064 |
} |
| 2065 |
} |
| 2066 |
|
| 2067 |
static void |
| 2068 |
c2_window_state_update_one_from_reply(struct c2_state *state, |
| 2069 |
struct c2_property_value *value, xcb_atom_t property, |
| 2070 |
xcb_get_property_reply_t *reply, xcb_connection_t *c) { |
| 2071 |
auto len = to_u32_checked(xcb_get_property_value_length(reply)); |
| 2072 |
void *data = xcb_get_property_value(reply); |
| 2073 |
bool property_is_string = x_is_type_string(state->atoms, reply->type); |
| 2074 |
unsigned int external_capacity = 0; |
| 2075 |
char *external_storage = NULL; |
| 2076 |
value->needs_update = false; |
| 2077 |
value->valid = false; |
| 2078 |
if (reply->type == XCB_ATOM_NONE) { |
| 2079 |
|
| 2080 |
log_verbose("Property %s doesn't exist on this window", |
| 2081 |
get_atom_name_cached(state->atoms, property)); |
| 2082 |
return; |
| 2083 |
} |
| 2084 |
if ((property_is_string && reply->format != 8) || |
| 2085 |
(reply->format != 8 && reply->format != 16 && reply->format != 32)) { |
| 2086 |
log_error("Invalid property type and format combination: property %s, " |
| 2087 |
"type: %s, format: %d.", |
| 2088 |
get_atom_name_cached(state->atoms, property), |
| 2089 |
get_atom_name_cached(state->atoms, reply->type), reply->format); |
| 2090 |
return; |
| 2091 |
} |
| 2092 |
|
| 2093 |
if (value->type == C2_PROPERTY_TYPE_STRING) { |
| 2094 |
external_capacity = value->string_capacity; |
| 2095 |
external_storage = value->string; |
| 2096 |
} else if (value->type == C2_PROPERTY_TYPE_NUMBER && |
| 2097 |
value->length > ARR_SIZE(value->numbers)) { |
| 2098 |
external_capacity = value->array_capacity; |
| 2099 |
external_storage = (char *)value->array; |
| 2100 |
} |
| 2101 |
log_verbose("Updating property %s, length = %u, format = %d", |
| 2102 |
get_atom_name_cached(state->atoms, property), len, reply->format); |
| 2103 |
value->valid = true; |
| 2104 |
if (len == 0) { |
| 2105 |
value->length = 0; |
| 2106 |
value->type = C2_PROPERTY_TYPE_NONE; |
| 2107 |
} else if (property_is_string) { |
| 2108 |
bool nul_terminated = ((char *)data)[len - 1] == '\0'; |
| 2109 |
value->length = len; |
| 2110 |
value->type = C2_PROPERTY_TYPE_STRING; |
| 2111 |
if (!nul_terminated) { |
| 2112 |
value->length += 1; |
| 2113 |
} |
| 2114 |
if (value->length > external_capacity) { |
| 2115 |
value->string = crealloc(external_storage, value->length); |
| 2116 |
value->string_capacity = value->length; |
| 2117 |
} else { |
| 2118 |
value->string = external_storage; |
| 2119 |
value->string_capacity = external_capacity; |
| 2120 |
} |
| 2121 |
external_capacity = 0; |
| 2122 |
external_storage = NULL; |
| 2123 |
memcpy(value->string, data, len); |
| 2124 |
if (!nul_terminated) { |
| 2125 |
value->string[len] = '\0'; |
| 2126 |
} |
| 2127 |
} else { |
| 2128 |
unsigned step = reply->format / 8; |
| 2129 |
bool is_signed = reply->type == XCB_ATOM_INTEGER; |
| 2130 |
value->length = len / step; |
| 2131 |
if (reply->type == XCB_ATOM_ATOM) { |
| 2132 |
value->type = C2_PROPERTY_TYPE_ATOM; |
| 2133 |
} else { |
| 2134 |
value->type = C2_PROPERTY_TYPE_NUMBER; |
| 2135 |
} |
| 2136 |
|
| 2137 |
int64_t *storage = value->numbers; |
| 2138 |
if (value->length > ARR_SIZE(value->numbers)) { |
| 2139 |
const unsigned size = value->length * sizeof(*value->array); |
| 2140 |
if (external_capacity < size) { |
| 2141 |
value->array = (int64_t *)crealloc(external_storage, size); |
| 2142 |
value->array_capacity = size; |
| 2143 |
} else { |
| 2144 |
value->array = (int64_t *)external_storage; |
| 2145 |
value->array_capacity = external_capacity; |
| 2146 |
} |
| 2147 |
external_capacity = 0; |
| 2148 |
external_storage = NULL; |
| 2149 |
storage = value->array; |
| 2150 |
} |
| 2151 |
for (uint32_t i = 0; i < value->length; i++) { |
| 2152 |
auto item = (char *)data + (size_t)i * step; |
| 2153 |
if (is_signed) { |
| 2154 |
switch (reply->format) { |
| 2155 |
case 8: storage[i] = *(int8_t *)item; break; |
| 2156 |
case 16: storage[i] = *(int16_t *)item; break; |
| 2157 |
case 32: storage[i] = *(int32_t *)item; break; |
| 2158 |
default: unreachable(); |
| 2159 |
} |
| 2160 |
} else { |
| 2161 |
switch (reply->format) { |
| 2162 |
case 8: storage[i] = *(uint8_t *)item; break; |
| 2163 |
case 16: storage[i] = *(uint16_t *)item; break; |
| 2164 |
case 32: storage[i] = *(uint32_t *)item; break; |
| 2165 |
default: unreachable(); |
| 2166 |
} |
| 2167 |
} |
| 2168 |
log_verbose("Property %s[%d] = %" PRId64, |
| 2169 |
get_atom_name_cached(state->atoms, property), i, |
| 2170 |
storage[i]); |
| 2171 |
if (reply->type == XCB_ATOM_ATOM) { |
| 2172 |
|
| 2173 |
|
| 2174 |
get_atom_name(state->atoms, (xcb_atom_t)storage[i], c); |
| 2175 |
} |
| 2176 |
} |
| 2177 |
} |
| 2178 |
|
| 2179 |
free(external_storage); |
| 2180 |
} |
| 2181 |
|
| 2182 |
static void c2_window_state_update_from_replies(struct c2_state *state, |
| 2183 |
struct c2_window_state *window_state, |
| 2184 |
xcb_connection_t *c, xcb_window_t client_win, |
| 2185 |
xcb_window_t frame_win) { |
| 2186 |
HASH_ITER2(state->tracked_properties, p) { |
| 2187 |
if (!window_state->values[p->id].needs_update) { |
| 2188 |
continue; |
| 2189 |
} |
| 2190 |
xcb_window_t window = p->key.is_on_client ? client_win : frame_win; |
| 2191 |
xcb_get_property_reply_t *reply = |
| 2192 |
xcb_get_property_reply(c, state->cookies[p->id], NULL); |
| 2193 |
if (!reply) { |
| 2194 |
log_warn("Failed to get property %d for window %#010x, some " |
| 2195 |
"window rules might not work.", |
| 2196 |
p->id, window); |
| 2197 |
window_state->values[p->id].valid = false; |
| 2198 |
window_state->values[p->id].needs_update = false; |
| 2199 |
continue; |
| 2200 |
} |
| 2201 |
c2_window_state_update_one_from_reply(state, &window_state->values[p->id], |
| 2202 |
p->key.property, reply, c); |
| 2203 |
free(reply); |
| 2204 |
} |
| 2205 |
} |
| 2206 |
|
| 2207 |
void c2_window_state_update(struct c2_state *state, struct c2_window_state *window_state, |
| 2208 |
xcb_connection_t *c, xcb_window_t client_win, |
| 2209 |
xcb_window_t frame_win) { |
| 2210 |
size_t property_count = HASH_COUNT(state->tracked_properties); |
| 2211 |
if (!state->cookies) { |
| 2212 |
state->cookies = ccalloc(property_count, xcb_get_property_cookie_t); |
| 2213 |
} |
| 2214 |
memset(state->cookies, 0, property_count * sizeof(xcb_get_property_cookie_t)); |
| 2215 |
|
| 2216 |
log_verbose("Updating c2 window state for window %#010x (frame %#010x)", |
| 2217 |
client_win, frame_win); |
| 2218 |
|
| 2219 |
HASH_ITER2(state->tracked_properties, p) { |
| 2220 |
if (!window_state->values[p->id].needs_update) { |
| 2221 |
continue; |
| 2222 |
} |
| 2223 |
|
| 2224 |
xcb_window_t window = p->key.is_on_client ? client_win : frame_win; |
| 2225 |
|
| 2226 |
|
| 2227 |
state->cookies[p->id] = xcb_get_property( |
| 2228 |
c, 0, window, p->key.property, XCB_GET_PROPERTY_TYPE_ANY, 0, UINT32_MAX); |
| 2229 |
} |
| 2230 |
|
| 2231 |
c2_window_state_update_from_replies(state, window_state, c, client_win, frame_win); |
| 2232 |
} |
| 2233 |
|
| 2234 |
bool c2_state_is_property_tracked(struct c2_state *state, xcb_atom_t property) { |
| 2235 |
struct c2_tracked_property *p; |
| 2236 |
struct c2_tracked_property_key key = { |
| 2237 |
.property = property, |
| 2238 |
.is_on_client = true, |
| 2239 |
}; |
| 2240 |
HASH_FIND(hh, state->tracked_properties, &key, sizeof(key), p); |
| 2241 |
if (p != NULL) { |
| 2242 |
return true; |
| 2243 |
} |
| 2244 |
key.is_on_client = false; |
| 2245 |
HASH_FIND(hh, state->tracked_properties, &key, sizeof(key), p); |
| 2246 |
return p != NULL; |
| 2247 |
} |