| 1 |
/* |
| 2 |
* Compton - a compositor for X11 |
| 3 |
* |
| 4 |
* Based on `xcompmgr` - Copyright (c) 2003, Keith Packard |
| 5 |
* |
| 6 |
* Copyright (c) 2011-2013, Christopher Jeffrey |
| 7 |
* See LICENSE for more information. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "common.h" |
| 12 |
|
| 13 |
#include <fnmatch.h> |
| 14 |
#include <ctype.h> |
| 15 |
|
| 16 |
// libpcre |
| 17 |
#ifdef CONFIG_REGEX_PCRE |
| 18 |
#include <pcre.h> |
| 19 |
|
| 20 |
// For compatiblity with <libpcre-8.20 |
| 21 |
#ifndef PCRE_STUDY_JIT_COMPILE |
| 22 |
#define PCRE_STUDY_JIT_COMPILE 0 |
| 23 |
#define LPCRE_FREE_STUDY(extra) pcre_free(extra) |
| 24 |
#else |
| 25 |
#define LPCRE_FREE_STUDY(extra) pcre_free_study(extra) |
| 26 |
#endif |
| 27 |
|
| 28 |
#endif |
| 29 |
|
| 30 |
#define C2_MAX_LEVELS 10 |
| 31 |
|
| 32 |
typedef struct _c2_b c2_b_t; |
| 33 |
typedef struct _c2_l c2_l_t; |
| 34 |
|
| 35 |
/// Pointer to a condition tree. |
| 36 |
typedef struct { |
| 37 |
bool isbranch : 1; |
| 38 |
union { |
| 39 |
c2_b_t *b; |
| 40 |
c2_l_t *l; |
| 41 |
}; |
| 42 |
} c2_ptr_t; |
| 43 |
|
| 44 |
/// Initializer for c2_ptr_t. |
| 45 |
#define C2_PTR_INIT { \ |
| 46 |
.isbranch = false, \ |
| 47 |
.l = NULL, \ |
| 48 |
} |
| 49 |
|
| 50 |
const static c2_ptr_t C2_PTR_NULL = C2_PTR_INIT; |
| 51 |
|
| 52 |
/// Operator of a branch element. |
| 53 |
typedef enum { |
| 54 |
C2_B_OUNDEFINED, |
| 55 |
C2_B_OAND, |
| 56 |
C2_B_OOR, |
| 57 |
C2_B_OXOR, |
| 58 |
} c2_b_op_t; |
| 59 |
|
| 60 |
/// Structure for branch element in a window condition |
| 61 |
struct _c2_b { |
| 62 |
bool neg : 1; |
| 63 |
c2_b_op_t op; |
| 64 |
c2_ptr_t opr1; |
| 65 |
c2_ptr_t opr2; |
| 66 |
}; |
| 67 |
|
| 68 |
/// Initializer for c2_b_t. |
| 69 |
#define C2_B_INIT { \ |
| 70 |
.neg = false, \ |
| 71 |
.op = C2_B_OUNDEFINED, \ |
| 72 |
.opr1 = C2_PTR_INIT, \ |
| 73 |
.opr2 = C2_PTR_INIT, \ |
| 74 |
} |
| 75 |
|
| 76 |
/// Structure for leaf element in a window condition |
| 77 |
struct _c2_l { |
| 78 |
bool neg : 1; |
| 79 |
enum { |
| 80 |
C2_L_OEXISTS, |
| 81 |
C2_L_OEQ, |
| 82 |
C2_L_OGT, |
| 83 |
C2_L_OGTEQ, |
| 84 |
C2_L_OLT, |
| 85 |
C2_L_OLTEQ, |
| 86 |
} op : 3; |
| 87 |
enum { |
| 88 |
C2_L_MEXACT, |
| 89 |
C2_L_MSTART, |
| 90 |
C2_L_MCONTAINS, |
| 91 |
C2_L_MWILDCARD, |
| 92 |
C2_L_MPCRE, |
| 93 |
} match : 3; |
| 94 |
bool match_ignorecase : 1; |
| 95 |
char *tgt; |
| 96 |
Atom tgtatom; |
| 97 |
bool tgt_onframe; |
| 98 |
int index; |
| 99 |
enum { |
| 100 |
C2_L_PUNDEFINED, |
| 101 |
C2_L_PID, |
| 102 |
C2_L_PX, |
| 103 |
C2_L_PY, |
| 104 |
C2_L_PX2, |
| 105 |
C2_L_PY2, |
| 106 |
C2_L_PWIDTH, |
| 107 |
C2_L_PHEIGHT, |
| 108 |
C2_L_PWIDTHB, |
| 109 |
C2_L_PHEIGHTB, |
| 110 |
C2_L_PBDW, |
| 111 |
C2_L_PFULLSCREEN, |
| 112 |
C2_L_POVREDIR, |
| 113 |
C2_L_PARGB, |
| 114 |
C2_L_PFOCUSED, |
| 115 |
C2_L_PWMWIN, |
| 116 |
C2_L_PBSHAPED, |
| 117 |
C2_L_PROUNDED, |
| 118 |
C2_L_PCLIENT, |
| 119 |
C2_L_PWINDOWTYPE, |
| 120 |
C2_L_PLEADER, |
| 121 |
C2_L_PNAME, |
| 122 |
C2_L_PCLASSG, |
| 123 |
C2_L_PCLASSI, |
| 124 |
C2_L_PROLE, |
| 125 |
} predef; |
| 126 |
enum c2_l_type { |
| 127 |
C2_L_TUNDEFINED, |
| 128 |
C2_L_TSTRING, |
| 129 |
C2_L_TCARDINAL, |
| 130 |
C2_L_TWINDOW, |
| 131 |
C2_L_TATOM, |
| 132 |
C2_L_TDRAWABLE, |
| 133 |
} type; |
| 134 |
int format; |
| 135 |
enum { |
| 136 |
C2_L_PTUNDEFINED, |
| 137 |
C2_L_PTSTRING, |
| 138 |
C2_L_PTINT, |
| 139 |
} ptntype; |
| 140 |
char *ptnstr; |
| 141 |
long ptnint; |
| 142 |
#ifdef CONFIG_REGEX_PCRE |
| 143 |
pcre *regex_pcre; |
| 144 |
pcre_extra *regex_pcre_extra; |
| 145 |
#endif |
| 146 |
}; |
| 147 |
|
| 148 |
/// Initializer for c2_l_t. |
| 149 |
#define C2_L_INIT { \ |
| 150 |
.neg = false, \ |
| 151 |
.op = C2_L_OEXISTS, \ |
| 152 |
.match = C2_L_MEXACT, \ |
| 153 |
.match_ignorecase = false, \ |
| 154 |
.tgt = NULL, \ |
| 155 |
.tgtatom = 0, \ |
| 156 |
.tgt_onframe = false, \ |
| 157 |
.predef = C2_L_PUNDEFINED, \ |
| 158 |
.index = -1, \ |
| 159 |
.type = C2_L_TUNDEFINED, \ |
| 160 |
.format = 0, \ |
| 161 |
.ptntype = C2_L_PTUNDEFINED, \ |
| 162 |
.ptnstr = NULL, \ |
| 163 |
.ptnint = 0, \ |
| 164 |
} |
| 165 |
|
| 166 |
const static c2_l_t leaf_def = C2_L_INIT; |
| 167 |
|
| 168 |
/// Linked list type of conditions. |
| 169 |
struct _c2_lptr { |
| 170 |
c2_ptr_t ptr; |
| 171 |
void *data; |
| 172 |
struct _c2_lptr *next; |
| 173 |
}; |
| 174 |
|
| 175 |
/// Initializer for c2_lptr_t. |
| 176 |
#define C2_LPTR_INIT { \ |
| 177 |
.ptr = C2_PTR_INIT, \ |
| 178 |
.data = NULL, \ |
| 179 |
.next = NULL, \ |
| 180 |
} |
| 181 |
|
| 182 |
/// Structure representing a predefined target. |
| 183 |
typedef struct { |
| 184 |
const char *name; |
| 185 |
enum c2_l_type type; |
| 186 |
int format; |
| 187 |
} c2_predef_t; |
| 188 |
|
| 189 |
// Predefined targets. |
| 190 |
const static c2_predef_t C2_PREDEFS[] = { |
| 191 |
[C2_L_PID ] = { "id" , C2_L_TCARDINAL , 0 }, |
| 192 |
[C2_L_PX ] = { "x" , C2_L_TCARDINAL , 0 }, |
| 193 |
[C2_L_PY ] = { "y" , C2_L_TCARDINAL , 0 }, |
| 194 |
[C2_L_PX2 ] = { "x2" , C2_L_TCARDINAL , 0 }, |
| 195 |
[C2_L_PY2 ] = { "y2" , C2_L_TCARDINAL , 0 }, |
| 196 |
[C2_L_PWIDTH ] = { "width" , C2_L_TCARDINAL , 0 }, |
| 197 |
[C2_L_PHEIGHT ] = { "height" , C2_L_TCARDINAL , 0 }, |
| 198 |
[C2_L_PWIDTHB ] = { "widthb" , C2_L_TCARDINAL , 0 }, |
| 199 |
[C2_L_PHEIGHTB ] = { "heightb" , C2_L_TCARDINAL , 0 }, |
| 200 |
[C2_L_PBDW ] = { "border_width" , C2_L_TCARDINAL , 0 }, |
| 201 |
[C2_L_PFULLSCREEN ] = { "fullscreen" , C2_L_TCARDINAL , 0 }, |
| 202 |
[C2_L_POVREDIR ] = { "override_redirect" , C2_L_TCARDINAL , 0 }, |
| 203 |
[C2_L_PARGB ] = { "argb" , C2_L_TCARDINAL , 0 }, |
| 204 |
[C2_L_PFOCUSED ] = { "focused" , C2_L_TCARDINAL , 0 }, |
| 205 |
[C2_L_PWMWIN ] = { "wmwin" , C2_L_TCARDINAL , 0 }, |
| 206 |
[C2_L_PBSHAPED ] = { "bounding_shaped" , C2_L_TCARDINAL , 0 }, |
| 207 |
[C2_L_PROUNDED ] = { "rounded_corners" , C2_L_TCARDINAL , 0 }, |
| 208 |
[C2_L_PCLIENT ] = { "client" , C2_L_TWINDOW , 0 }, |
| 209 |
[C2_L_PWINDOWTYPE ] = { "window_type" , C2_L_TSTRING , 0 }, |
| 210 |
[C2_L_PLEADER ] = { "leader" , C2_L_TWINDOW , 0 }, |
| 211 |
[C2_L_PNAME ] = { "name" , C2_L_TSTRING , 0 }, |
| 212 |
[C2_L_PCLASSG ] = { "class_g" , C2_L_TSTRING , 0 }, |
| 213 |
[C2_L_PCLASSI ] = { "class_i" , C2_L_TSTRING , 0 }, |
| 214 |
[C2_L_PROLE ] = { "role" , C2_L_TSTRING , 0 }, |
| 215 |
}; |
| 216 |
|
| 217 |
#define mstrncmp(s1, s2) strncmp((s1), (s2), strlen(s1)) |
| 218 |
|
| 219 |
/** |
| 220 |
* Compare next word in a string with another string. |
| 221 |
*/ |
| 222 |
static inline int |
| 223 |
strcmp_wd(const char *needle, const char *src) { |
| 224 |
int ret = mstrncmp(needle, src); |
| 225 |
if (ret) |
| 226 |
return ret; |
| 227 |
|
| 228 |
char c = src[strlen(needle)]; |
| 229 |
if (isalnum(c) || '_' == c) |
| 230 |
return 1; |
| 231 |
else |
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Return whether a c2_ptr_t is empty. |
| 237 |
*/ |
| 238 |
static inline bool |
| 239 |
c2_ptr_isempty(const c2_ptr_t p) { |
| 240 |
return !(p.isbranch ? (bool) p.b: (bool) p.l); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Reset a c2_ptr_t. |
| 245 |
*/ |
| 246 |
static inline void |
| 247 |
c2_ptr_reset(c2_ptr_t *pp) { |
| 248 |
if (pp) |
| 249 |
memcpy(pp, &C2_PTR_NULL, sizeof(c2_ptr_t)); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Combine two condition trees. |
| 254 |
*/ |
| 255 |
static inline c2_ptr_t |
| 256 |
c2h_comb_tree(c2_b_op_t op, c2_ptr_t p1, c2_ptr_t p2) { |
| 257 |
c2_ptr_t p = { |
| 258 |
.isbranch = true, |
| 259 |
.b = malloc(sizeof(c2_b_t)) |
| 260 |
}; |
| 261 |
|
| 262 |
p.b->opr1 = p1; |
| 263 |
p.b->opr2 = p2; |
| 264 |
p.b->op = op; |
| 265 |
|
| 266 |
return p; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Get the precedence value of a condition branch operator. |
| 271 |
*/ |
| 272 |
static inline int |
| 273 |
c2h_b_opp(c2_b_op_t op) { |
| 274 |
switch (op) { |
| 275 |
case C2_B_OAND: return 2; |
| 276 |
case C2_B_OOR: return 1; |
| 277 |
case C2_B_OXOR: return 1; |
| 278 |
default: break; |
| 279 |
} |
| 280 |
|
| 281 |
assert(0); |
| 282 |
return 0; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Compare precedence of two condition branch operators. |
| 287 |
* |
| 288 |
* Associativity is left-to-right, forever. |
| 289 |
* |
| 290 |
* @return positive number if op1 > op2, 0 if op1 == op2 in precedence, |
| 291 |
* negative number otherwise |
| 292 |
*/ |
| 293 |
static inline int |
| 294 |
c2h_b_opcmp(c2_b_op_t op1, c2_b_op_t op2) { |
| 295 |
return c2h_b_opp(op1) - c2h_b_opp(op2); |
| 296 |
} |
| 297 |
|
| 298 |
static int |
| 299 |
c2_parse_grp(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult, int level); |
| 300 |
|
| 301 |
static int |
| 302 |
c2_parse_target(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult); |
| 303 |
|
| 304 |
static int |
| 305 |
c2_parse_op(const char *pattern, int offset, c2_ptr_t *presult); |
| 306 |
|
| 307 |
static int |
| 308 |
c2_parse_pattern(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult); |
| 309 |
|
| 310 |
static int |
| 311 |
c2_parse_legacy(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult); |
| 312 |
|
| 313 |
static bool |
| 314 |
c2_l_postprocess(session_t *ps, c2_l_t *pleaf); |
| 315 |
|
| 316 |
static void |
| 317 |
c2_free(c2_ptr_t p); |
| 318 |
|
| 319 |
/** |
| 320 |
* Wrapper of c2_free(). |
| 321 |
*/ |
| 322 |
static inline void |
| 323 |
c2_freep(c2_ptr_t *pp) { |
| 324 |
if (pp) { |
| 325 |
c2_free(*pp); |
| 326 |
c2_ptr_reset(pp); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
static const char * |
| 331 |
c2h_dump_str_tgt(const c2_l_t *pleaf); |
| 332 |
|
| 333 |
static const char * |
| 334 |
c2h_dump_str_type(const c2_l_t *pleaf); |
| 335 |
|
| 336 |
static void |
| 337 |
c2_dump_raw(c2_ptr_t p); |
| 338 |
|
| 339 |
/** |
| 340 |
* Wrapper of c2_dump_raw(). |
| 341 |
*/ |
| 342 |
static inline void |
| 343 |
c2_dump(c2_ptr_t p) { |
| 344 |
c2_dump_raw(p); |
| 345 |
printf("\n"); |
| 346 |
fflush(stdout); |
| 347 |
} |
| 348 |
|
| 349 |
static Atom |
| 350 |
c2_get_atom_type(const c2_l_t *pleaf); |
| 351 |
|
| 352 |
static bool |
| 353 |
c2_match_once(session_t *ps, win *w, const c2_ptr_t cond); |
| 354 |
|