Debian Codegraph @

src/inspect.c

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) 2024 Yuxuan Shui <[email protected]>
3
4 #include <X11/Xlib.h>
5 #include <stddef.h>
6 #include <stdint.h>
7 #include <xcb/shape.h>
8 #include <xcb/xcb.h>
9 #include <xcb/xcb_event.h>
10 #include <xcb/xproto.h>
11
12 #include "inspect.h"
13
14 #include "c2.h"
15 #include "common.h"
16 #include "config.h"
17 #include "log.h"
18 #include "utils/console.h"
19 #include "utils/dynarr.h"
20 #include "utils/str.h"
21 #include "wm/defs.h"
22 #include "wm/win.h"
23 #include "x.h"
24
25 xcb_window_t inspect_select_window(struct x_connection *c) {
26 xcb_font_t font = x_new_id(c);
27 xcb_cursor_t cursor = x_new_id(c);
28 const char font_name[] = "cursor";
29 static const uint16_t CROSSHAIR_CHAR = 34;
30 XCB_AWAIT_VOID(xcb_open_font, c, font, sizeof(font_name) - 1, font_name);
31 XCB_AWAIT_VOID(xcb_create_glyph_cursor, c, cursor, font, font, CROSSHAIR_CHAR,
32 CROSSHAIR_CHAR + 1, 0, 0, 0, 0xffff, 0xffff, 0xffff);
33 auto grab_reply = XCB_AWAIT(
34 xcb_grab_pointer, c, false, c->screen_info->root,
35 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE, XCB_GRAB_MODE_SYNC,
36 XCB_GRAB_MODE_ASYNC, c->screen_info->root, cursor, XCB_CURRENT_TIME);
37 if (grab_reply->status != XCB_GRAB_STATUS_SUCCESS) {
38 log_fatal("Failed to grab pointer");
39 return 1;
40 }
41 free(grab_reply);
42
43 // Let the user pick a window by clicking on it, mostly stolen from
44 // xprop
45 xcb_window_t target = XCB_NONE;
46 int buttons_pressed = 0;
47 while ((target == XCB_NONE) || (buttons_pressed > 0)) {
48 XCB_AWAIT_VOID(xcb_allow_events, c, XCB_ALLOW_ASYNC_POINTER, XCB_CURRENT_TIME);
49 xcb_generic_event_t *ev = xcb_wait_for_event(c->c);
50 if (!ev) {
51 log_fatal("Connection to X server lost");
52 return 1;
53 }
54 switch (XCB_EVENT_RESPONSE_TYPE(ev)) {
55 case XCB_BUTTON_PRESS: {
56 xcb_button_press_event_t *e = (xcb_button_press_event_t *)ev;
57 if (target == XCB_NONE) {
58 target = e->child;
59 if (target == XCB_NONE) {
60 target = e->root;
61 }
62 }
63 buttons_pressed++;
64 break;
65 }
66 case XCB_BUTTON_RELEASE: {
67 if (buttons_pressed > 0) {
68 buttons_pressed--;
69 }
70 break;
71 }
72 default: break;
73 }
74 free(ev);
75 }
76 XCB_AWAIT_VOID(xcb_ungrab_pointer, c, XCB_CURRENT_TIME);
77 return target;
78 }
79
80 struct c2_match_state {
81 const struct c2_state *state;
82 const struct win *w;
83 bool print_value;
84 };
85
86 static bool c2_match_and_log(const struct list_node *list, const struct c2_state *state,
87 const struct win *w, bool print_value) {
88 void *rule_data = NULL;
89 c2_condition_list_foreach((struct list_node *)list, i) {
90 printf(" %s ... ", c2_condition_to_str(i));
91 bool matched = c2_match_one(state, w, i, rule_data);
92 printf("%s", matched ? "\033[1;32mmatched\033[0m" : "not matched");
93 if (print_value && matched) {
94 printf("/%lu", (unsigned long)(intptr_t)rule_data);
95 print_value = false;
96 }
97 printf("\n");
98 }
99 return false;
100 }
101
102 void inspect_dump_window(const struct c2_state *state, const struct options *opts,
103 const struct win *w) {
104 if (list_is_empty(&opts->rules)) {
105 printf("Checking " BOLD("transparent-clipping-exclude") ":\n");
106 c2_match_and_log(&opts->transparent_clipping_blacklist, state, w, false);
107 printf("Checking " BOLD("shadow-exclude") ":\n");
108 c2_match_and_log(&opts->shadow_blacklist, state, w, false);
109 printf("Checking " BOLD("fade-exclude") ":\n");
110 c2_match_and_log(&opts->fade_blacklist, state, w, false);
111 printf("Checking " BOLD("clip-shadow-above") ":\n");
112 c2_match_and_log(&opts->shadow_clip_list, state, w, true);
113 printf("Checking " BOLD("focus-exclude") ":\n");
114 c2_match_and_log(&opts->focus_blacklist, state, w, false);
115 printf("Checking " BOLD("invert-color-include") ":\n");
116 c2_match_and_log(&opts->invert_color_list, state, w, false);
117 printf("Checking " BOLD("blur-background-exclude") ":\n");
118 c2_match_and_log(&opts->blur_background_blacklist, state, w, false);
119 printf("Checking " BOLD("unredir-if-possible-exclude") ":\n");
120 c2_match_and_log(&opts->unredir_if_possible_blacklist, state, w, false);
121 printf("Checking " BOLD("rounded-corners-exclude") ":\n");
122 c2_match_and_log(&opts->rounded_corners_blacklist, state, w, false);
123
124 printf("Checking " BOLD("opacity-rule") ":\n");
125 c2_match_and_log(&opts->opacity_rules, state, w, true);
126 printf("Checking " BOLD("corner-radius-rule") ":\n");
127 c2_match_and_log(&opts->corner_radius_rules, state, w, true);
128 }
129
130 printf("\nHere are some rule(s) that match this window:\n");
131 if (w->name != NULL) {
132 printf(" name = '%s'\n", w->name);
133 }
134 if (w->class_instance != NULL) {
135 printf(" class_i = '%s'\n", w->class_instance);
136 }
137 if (w->class_general != NULL) {
138 printf(" class_g = '%s'\n", w->class_general);
139 }
140 if (w->role != NULL) {
141 printf(" role = '%s'\n", w->role);
142 }
143 if (w->window_types != 0) {
144 for (int i = 0; i < NUM_WINTYPES; i++) {
145 if (w->window_types & (1 << i)) {
146 printf(" window_type = '%s'\n", WINTYPES[i].name);
147 }
148 }
149 }
150 printf(" %sfullscreen\n", w->is_fullscreen ? "" : "! ");
151 if (w->bounding_shaped) {
152 printf(" bounding_shaped\n");
153 }
154 printf(" border_width = %d\n", w->g.border_width);
155 }
156
157 void inspect_dump_window_maybe_options(struct window_maybe_options wopts) {
158 bool nothing = true;
159 printf(" Applying:\n");
160 if (wopts.shadow != TRI_UNKNOWN) {
161 printf(" shadow = %s\n", wopts.shadow == TRI_TRUE ? "true" : "false");
162 nothing = false;
163 }
164 if (wopts.fade != TRI_UNKNOWN) {
165 printf(" fade = %s\n", wopts.fade == TRI_TRUE ? "true" : "false");
166 nothing = false;
167 }
168 if (wopts.blur_background != TRI_UNKNOWN) {
169 printf(" blur_background = %s\n",
170 wopts.blur_background == TRI_TRUE ? "true" : "false");
171 nothing = false;
172 }
173 if (wopts.invert_color != TRI_UNKNOWN) {
174 printf(" invert_color = %s\n",
175 wopts.invert_color == TRI_TRUE ? "true" : "false");
176 nothing = false;
177 }
178 if (wopts.clip_shadow_above != TRI_UNKNOWN) {
179 printf(" clip_shadow_above = %s\n",
180 wopts.clip_shadow_above == TRI_TRUE ? "true" : "false");
181 nothing = false;
182 }
183 if (wopts.transparent_clipping != TRI_UNKNOWN) {
184 printf(" transparent_clipping = %s\n",
185 wopts.transparent_clipping == TRI_TRUE ? "true" : "false");
186 nothing = false;
187 }
188 if (wopts.full_shadow != TRI_UNKNOWN) {
189 printf(" full_shadow = %s\n",
190 wopts.full_shadow == TRI_TRUE ? "true" : "false");
191 nothing = false;
192 }
193 if (wopts.unredir != WINDOW_UNREDIR_INVALID) {
194 const char *str = NULL;
195 switch (wopts.unredir) {
196 case WINDOW_UNREDIR_WHEN_POSSIBLE_ELSE_TERMINATE: str = "true"; break;
197 case WINDOW_UNREDIR_TERMINATE: str = "false"; break;
198 case WINDOW_UNREDIR_FORCED: str = "\"forced\""; break;
199 case WINDOW_UNREDIR_PASSIVE: str = "\"passive\""; break;
200 case WINDOW_UNREDIR_WHEN_POSSIBLE: str = "\"preferred\""; break;
201 default: unreachable();
202 }
203 printf(" unredir = %s\n", str);
204 nothing = false;
205 }
206 if (!safe_isnan(wopts.opacity)) {
207 printf(" opacity = %f\n", wopts.opacity);
208 nothing = false;
209 }
210 if (!safe_isnan(wopts.dim)) {
211 printf(" dim = %f\n", wopts.dim);
212 nothing = false;
213 }
214 if (wopts.corner_radius >= 0) {
215 printf(" corner_radius = %d\n", wopts.corner_radius);
216 nothing = false;
217 }
218
219 char **animation_triggers = dynarr_new(char *, 0);
220 for (int i = 0; i < ANIMATION_TRIGGER_COUNT; i++) {
221 if (wopts.animations[i].script != NULL) {
222 char *name = NULL;
223 casprintf(&name, "\"%s\"", animation_trigger_names[i]);
224 dynarr_push(animation_triggers, name);
225 }
226 }
227 if (dynarr_len(animation_triggers) > 0) {
228 char *animation_triggers_str = dynarr_join(animation_triggers, ", ");
229 printf(" animations = { triggers = [%s]; }\n", animation_triggers_str);
230 free(animation_triggers_str);
231 nothing = false;
232 } else {
233 dynarr_free_pod(animation_triggers);
234 }
235
236 if (nothing) {
237 printf(" (nothing)\n");
238 }
239 }