Debian Codegraph @

src/options.c

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui <[email protected]>
3
4 #include <getopt.h>
5 #include <locale.h>
6 #include <stdbool.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/ioctl.h>
11 #include <termios.h>
12 #include <unistd.h>
13 #include <xcb/render.h> // for xcb_render_fixed_t, XXX
14
15 #include "backend/backend.h"
16 #include "c2.h"
17 #include "config.h"
18 #include "log.h"
19 #include "options.h"
20 #include "transition/script.h"
21 #include "utils/dynarr.h"
22 #include "utils/misc.h"
23 #include "utils/str.h"
24 #include "x.h"
25
26 #pragma GCC diagnostic error "-Wunused-parameter"
27
28 struct picom_option;
29
30 struct picom_arg {
31 const char *name;
32 ptrdiff_t offset;
33
34 const void *user_data;
35 bool (*handler)(const struct picom_option *, const struct picom_arg *,
36 const char *optarg, void *output);
37 };
38
39 struct picom_arg_parser {
40 int (*parse)(const char *);
41 int invalid_value;
42 };
43
44 struct picom_rules_parser {
45 void *(*parse_prefix)(const char *, const char **end, void *data);
46 void (*free_value)(void *);
47 void *user_data;
48 };
49
50 struct picom_deprecated_arg {
51 const char *message;
52 struct picom_arg inner;
53 bool error;
54 };
55
56 struct picom_option {
57 const char *long_name;
58 int has_arg;
59 struct picom_arg arg;
60 const char *help;
61 const char *argv0;
62 };
63
64 static bool set_flag(const struct picom_option * /*opt*/, const struct picom_arg *arg,
65 const char * /*arg_str*/, void *output) {
66 *(bool *)(output + arg->offset) = true;
67 return true;
68 }
69
70 static bool set_rule_flag(const struct picom_option *arg_opt, const struct picom_arg *arg,
71 const char * /*arg_str*/, void *output) {
72 auto opt = (struct options *)output;
73 if (!list_is_empty(&opt->rules)) {
74 log_warn_both_style_of_rules(opt, arg_opt->long_name);
75 return true;
76 }
77 *(bool *)(output + arg->offset) = true;
78 return true;
79 }
80 static bool unset_flag(const struct picom_option * /*opt*/, const struct picom_arg *arg,
81 const char * /*arg_str*/, void *output) {
82 *(bool *)(output + arg->offset) = false;
83 return true;
84 }
85
86 static bool parse_with(const struct picom_option *opt, const struct picom_arg *arg,
87 const char *arg_str, void *output) {
88 const struct picom_arg_parser *parser = arg->user_data;
89 int *dst = (int *)(output + arg->offset);
90 *dst = parser->parse(arg_str);
91 if (*dst == parser->invalid_value) {
92 log_error("Invalid argument for option `--%s`: %s", opt->long_name, arg_str);
93 return false;
94 }
95 return true;
96 }
97
98 static bool store_float(const struct picom_option *opt, const struct picom_arg *arg,
99 const char *arg_str, void *output) {
100 double *dst = (double *)(output + arg->offset);
101 const double *minmax = (const double *)arg->user_data;
102 const char *endptr = NULL;
103 *dst = strtod_simple(arg_str, &endptr);
104 if (!endptr || *endptr != '\0') {
105 log_error("Argument for option `--%s` is not a valid float number: %s",
106 opt->long_name, arg_str);
107 return false;
108 }
109 *dst = max2(minmax[0], min2(*dst, minmax[1]));
110 return true;
111 }
112
113 static bool store_rule_float(const struct picom_option *arg_opt, const struct picom_arg *arg,
114 const char *arg_str, void *output) {
115 auto opt = (struct options *)output;
116 if (!list_is_empty(&opt->rules)) {
117 log_warn_both_style_of_rules(opt, arg_opt->long_name);
118 return true;
119 }
120 return store_float(arg_opt, arg, arg_str, output);
121 }
122
123 static bool store_int(const struct picom_option *opt, const struct picom_arg *arg,
124 const char *arg_str, void *output) {
125 const int *minmax = (const int *)arg->user_data;
126 int *dst = (int *)(output + arg->offset);
127 if (!parse_int(arg_str, dst)) {
128 log_error("Argument for option `--%s` is not a valid integer: %s",
129 opt->long_name, arg_str);
130 return false;
131 }
132 *dst = max2(minmax[0], min2(*dst, minmax[1]));
133 return true;
134 }
135
136 static bool store_string(const struct picom_option * /*opt*/, const struct picom_arg *arg,
137 const char *arg_str, void *output) {
138 char **dst = (char **)(output + arg->offset);
139 free(*dst);
140 *dst = strdup(arg_str);
141 return true;
142 }
143
144 static bool store_shader(const struct picom_option *opt, const struct picom_arg *arg,
145 const char *arg_str, void *output) {
146 scoped_charp cwd = getcwd(NULL, 0);
147 scoped_charp full_path = locate_auxiliary_file("shaders", arg_str, cwd);
148 if (!full_path) {
149 log_error("Couldn't find shader file \"%s\" for %s", arg_str, opt->long_name);
150 return false;
151 }
152
153 auto dst = (struct shader_specification **)(output + arg->offset);
154 free(*dst);
155 *dst = shader_spec_from_path(full_path);
156 return true;
157 }
158
159 static bool
160 store_fixed_string(const struct picom_option * /*opt*/, const struct picom_arg *arg,
161 const char * /*arg_str*/, void *output) {
162 char **dst = (char **)(output + arg->offset);
163 free(*dst);
164 *dst = strdup((const char *)arg->user_data);
165 return true;
166 }
167
168 static bool store_rules(const struct picom_option *arg_opt, const struct picom_arg *arg,
169 const char *arg_str, void *output) {
170 const struct picom_rules_parser *parser = arg->user_data;
171 struct options *opt = (struct options *)output;
172 if (!list_is_empty(&opt->rules)) {
173 log_warn_both_style_of_rules(opt, arg_opt->long_name);
174 return true;
175 }
176 auto rules = (struct list_node *)(output + arg->offset);
177 bool deprecated = false, succeeded;
178 if (!parser->parse_prefix) {
179 succeeded = c2_parse(rules, arg_str, NULL, &deprecated) != NULL;
180 } else {
181 succeeded = c2_parse_with_prefix(rules, arg_str, parser->parse_prefix,
182 parser->free_value, parser->user_data,
183 &deprecated);
184 }
185 if (deprecated) {
186 log_warn("Rule option --%s contains deprecated syntax (see above).",
187 arg_opt->long_name);
188 record_problematic_option(opt, arg_opt->long_name);
189 }
190 return succeeded;
191 }
192
193 static bool store_fixed_enum(const struct picom_option * /*opt*/, const struct picom_arg *arg,
194 const char * /*arg_str*/, void *output) {
195 const int *value = (const int *)arg->user_data;
196 *(int *)(output + arg->offset) = *value;
197 return true;
198 }
199
200 static bool noop(const struct picom_option * /*opt*/, const struct picom_arg * /*arg*/,
201 const char * /*arg_str*/, void * /*output*/) {
202 return true;
203 }
204
205 static bool reject(const struct picom_option * /*opt*/, const struct picom_arg * /*arg*/,
206 const char * /*arg_str*/, void * /*output*/) {
207 return false;
208 }
209
210 static bool say_deprecated(const struct picom_option *opt, const struct picom_arg *arg,
211 const char *arg_str, void *output) {
212 const struct picom_deprecated_arg *deprecation = arg->user_data;
213 report_deprecated_option(output, opt->long_name, deprecation->error);
214 return deprecation->inner.handler(opt, &deprecation->inner, arg_str, output);
215 }
216
217 #define OFFSET(member) offsetof(struct options, member)
218
219 #define ENABLE(member) \
220 no_argument, { \
221 .offset = OFFSET(member), .handler = set_flag, \
222 }
223
224 /// A true or false option that functions like a window rule. Which is superseded by the
225 /// `rules` option.
226 #define ENABLE_RULE(member) \
227 no_argument, { \
228 .offset = OFFSET(member), .handler = set_rule_flag, \
229 }
230
231 #define DISABLE(member) \
232 no_argument, { \
233 .offset = OFFSET(member), .handler = unset_flag, \
234 }
235
236 #define IGNORE(has_arg) \
237 has_arg, { \
238 .handler = noop, \
239 }
240
241 #define REJECT(has_arg) \
242 has_arg, { \
243 .handler = reject, \
244 }
245
246 #define DO(fn) \
247 required_argument, { \
248 .handler = (fn), \
249 }
250
251 #define PARSE_WITH(fn, invalid, member) \
252 required_argument, { \
253 .offset = OFFSET(member), .handler = parse_with, \
254 .user_data = (struct picom_arg_parser[]){{ \
255 .invalid_value = (invalid), \
256 .parse = (fn), \
257 }}, \
258 }
259
260 #define FLOAT(member, min, max) \
261 required_argument, { \
262 .offset = OFFSET(member), .handler = store_float, \
263 .user_data = (double[]){min, max}, \
264 }
265
266 /// A float option that functions like a window rule. Which is superseded by the `rules`
267 /// option.
268 #define FLOAT_RULE(member, min, max) \
269 required_argument, { \
270 .offset = OFFSET(member), .handler = store_rule_float, \
271 .user_data = (double[]){min, max}, \
272 }
273
274 #define INTEGER(member, min, max) \
275 required_argument, { \
276 .offset = OFFSET(member), .handler = store_int, \
277 .user_data = (int[]){min, max}, \
278 }
279
280 #define NAMED_STRING(member, name_) \
281 required_argument, { \
282 .offset = OFFSET(member), .handler = store_string, .name = (name_) \
283 }
284
285 #define SHADER(member) \
286 required_argument, { \
287 .offset = OFFSET(member), .handler = store_shader, .name = "PATH", \
288 }
289
290 #define STRING(member) NAMED_STRING(member, NULL)
291
292 #define NAMED_RULES(member, name_, ...) \
293 required_argument, { \
294 .offset = OFFSET(member), .handler = store_rules, .name = (name_), \
295 .user_data = (struct picom_rules_parser[]) { \
296 __VA_ARGS__ \
297 } \
298 }
299 #define NUMERIC_RULES(member, value, min, max) \
300 NAMED_RULES(member, value ":COND", \
301 {.parse_prefix = parse_numeric_prefix, .user_data = (int[]){min, max}})
302 #define RULES(member) NAMED_RULES(member, "COND", {})
303
304 #define FIXED(member, value) \
305 no_argument, { \
306 .offset = OFFSET(member), .handler = store_fixed_enum, \
307 .user_data = (int[]){value}, \
308 }
309 #define FIXED_STR(member, str) \
310 no_argument, { \
311 .offset = OFFSET(member), .handler = store_fixed_string, \
312 .user_data = (void *)(str) \
313 }
314
315 #define SAY_DEPRECATED_(error_, msg, has_arg, ...) \
316 has_arg, { \
317 .handler = say_deprecated, .user_data = (struct picom_deprecated_arg[]) { \
318 {.message = (msg), .inner = __VA_ARGS__, .error = error_}, \
319 } \
320 }
321 #define SAY_DEPRECATED(error_, msg, ...) SAY_DEPRECATED_(error_, msg, __VA_ARGS__)
322
323 #define WARN_DEPRECATED(...) \
324 SAY_DEPRECATED_(false, \
325 "If you encounter problems without this feature, please " \
326 "feel free to open a bug report.", \
327 __VA_ARGS__)
328
329 #define WARN_DEPRECATED_ENABLED(...) \
330 SAY_DEPRECATED_(false, "Its functionality will always be enabled. ", __VA_ARGS__)
331
332 #define ERROR_DEPRECATED(has_arg) SAY_DEPRECATED(true, "", REJECT(has_arg))
333
334 static bool
335 store_shadow_color(const struct picom_option * /*opt*/, const struct picom_arg * /*arg*/,
336 const char *arg_str, void *output) {
337 struct options *opt = (struct options *)output;
338 struct color rgb;
339 rgb = hex_to_rgb(arg_str);
340 opt->shadow_red = rgb.red;
341 opt->shadow_green = rgb.green;
342 opt->shadow_blue = rgb.blue;
343 return true;
344 }
345
346 static bool
347 store_blur_kern(const struct picom_option * /*opt*/, const struct picom_arg * /*arg*/,
348 const char *arg_str, void *output) {
349 struct options *opt = (struct options *)output;
350 opt->blur_kerns = parse_blur_kern_lst(arg_str, &opt->blur_kernel_count);
351 return opt->blur_kerns != NULL;
352 }
353
354 static bool
355 store_benchmark_wid(const struct picom_option * /*opt*/, const struct picom_arg * /*arg*/,
356 const char *arg_str, void *output) {
357 struct options *opt = (struct options *)output;
358 const char *endptr = NULL;
359 opt->benchmark_wid = (xcb_window_t)strtoul(arg_str, (char **)&endptr, 0);
360 if (!endptr || *endptr != '\0') {
361 log_error("Invalid window ID for `--benchmark-wid`: %s", arg_str);
362 return false;
363 }
364 return true;
365 }
366
367 static bool store_backend(const struct picom_option * /*opt*/, const struct picom_arg * /*arg*/,
368 const char *arg_str, void *output) {
369 struct options *opt = (struct options *)output;
370 opt->backend = backend_find(arg_str);
371 if (opt->backend == NULL) {
372 log_error("Invalid backend: %s", arg_str);
373 return false;
374 }
375 return true;
376 }
377
378 #define WINDOW_SHADER_RULE \
379 {.parse_prefix = parse_window_shader_prefix_with_cwd, .free_value = free}
380
381 #ifdef CONFIG_OPENGL
382 #define BACKENDS "xrender, glx"
383 #else
384 #define BACKENDS "xrender"
385 #endif
386
387 // clang-format off
388 static const struct option *longopts = NULL;
389 static const struct picom_option picom_options[] = {
390 // As you can see, aligning this table is difficult...
391
392 // Rejected options, we shouldn't be able to reach `get_cfg` when these are set
393 ['h'] = {"help" , REJECT(no_argument), "Print this help message and exit."},
394 [318] = {"version", REJECT(no_argument), "Print version number and exit."},
395
396 // Ignored options, these are already handled by `get_early_cfg`
397 [314] = {"show-all-xerrors", IGNORE(no_argument)},
398 ['b'] = {"daemon" , IGNORE(no_argument) , "Daemonize process."},
399 [256] = {"config" , IGNORE(required_argument), "Path to the configuration file."},
400 [307] = {"plugins" , IGNORE(required_argument), "Plugins to load. Can be specified multiple times, each time with a single plugin."},
401
402 // "Rule-like" options
403 [262] = {"mark-wmwin-focused" , ENABLE_RULE(mark_wmwin_focused) , "Try to detect WM windows and mark them as active."},
404 [264] = {"mark-ovredir-focused" , ENABLE_RULE(mark_ovredir_focused) , "Mark windows that have no WM frame as active."},
405 [266] = {"shadow-ignore-shaped" , ENABLE_RULE(shadow_ignore_shaped) , "Do not paint shadows on shaped windows. (Deprecated, use --shadow-exclude "
406 "\'bounding_shaped\' or --shadow-exclude \'bounding_shaped && "
407 "!rounded_corners\' instead.)"},
408 [260] = {"inactive-opacity-override", ENABLE_RULE(inactive_opacity_override), "Inactive opacity set by -i overrides value of _NET_WM_WINDOW_OPACITY."},
409 [297] = {"active-opacity" , FLOAT_RULE(active_opacity, 0, 1) , "Default opacity for active windows. (0.0 - 1.0)"},
410 [261] = {"inactive-dim" , FLOAT_RULE(inactive_dim, 0, 1) , "Dim inactive windows. (0.0 - 1.0, defaults to 0)"},
411 ['i'] = {"inactive-opacity" , FLOAT_RULE(inactive_opacity, 0, 1) , "Opacity of inactive windows. (0.0 - 1.0)"},
412
413 // Simple flags
414 ['c'] = {"shadow" , ENABLE(shadow_enable) , "Enabled client-side shadows on windows."},
415 ['f'] = {"fading" , ENABLE(fading_enable) , "Fade windows in/out when opening/closing and when opacity changes, "
416 "unless --no-fading-openclose is used."},
417 [265] = {"no-fading-openclose" , ENABLE(no_fading_openclose) , "Do not fade on window open/close."},
418 [268] = {"detect-client-opacity" , ENABLE(detect_client_opacity) , "Detect _NET_WM_WINDOW_OPACITY on client windows, useful for window "
419 "managers not passing _NET_WM_WINDOW_OPACITY of client windows to frame"},
420 [270] = {"vsync" , ENABLE(vsync) , "Enable VSync"},
421 [271] = {"crop-shadow-to-monitor" , ENABLE(crop_shadow_to_monitor) , "Crop shadow of a window fully on a particular monitor to that monitor. "
422 "This is currently implemented using the X RandR extension"},
423 [276] = {"use-ewmh-active-win" , ENABLE(use_ewmh_active_win) , "Use _NET_WM_ACTIVE_WINDOW on the root window to determine which window is "
424 "focused instead of using FocusIn/Out events"},
425 [278] = {"unredir-if-possible" , ENABLE(unredir_if_possible) , "Unredirect all windows if a full-screen opaque window is detected, to "
426 "maximize performance for full-screen applications."},
427 [280] = {"inactive-dim-fixed" , ENABLE(inactive_dim_fixed) , "Use fixed inactive dim value."},
428 [281] = {"detect-transient" , ENABLE(detect_transient) , "Use WM_TRANSIENT_FOR to group windows, and consider windows in the same "
429 "group focused at the same time."},
430 [282] = {"detect-client-leader" , ENABLE(detect_client_leader) , "Use WM_CLIENT_LEADER to group windows, and consider windows in the same group "
431 "focused at the same time. This usually means windows from the same application "
432 "will be considered focused or unfocused at the same time. WM_TRANSIENT_FOR has "
433 "higher priority if --detect-transient is enabled, too."},
434 [284] = {"blur-background-frame" , ENABLE(blur_background_frame) , "Blur background of windows when the window frame is not opaque. Implies "
435 "--blur-background."},
436 [285] = {"blur-background-fixed" , ENABLE(blur_background_fixed) , "Use fixed blur strength instead of adjusting according to window opacity."},
437 #ifdef CONFIG_DBUS
438 [286] = {"dbus" , ENABLE(dbus) , "Enable remote control via D-Bus. See the D-BUS API section in the man page "
439 "for more details."},
440 #endif
441 [311] = {"vsync-use-glfinish" , ENABLE(vsync_use_glfinish)},
442 [313] = {"xrender-sync-fence" , ENABLE(xrender_sync_fence) , "Additionally use X Sync fence to sync clients' draw calls. Needed on "
443 "nvidia-drivers with GLX backend for some users."},
444 [315] = {"no-fading-destroyed-argb" , ENABLE(no_fading_destroyed_argb) , "Do not fade destroyed ARGB windows with WM frame. Workaround bugs in Openbox, "
445 "Fluxbox, etc."},
446 [316] = {"force-win-blend" , ENABLE(force_win_blend) , "Force all windows to be painted with blending. Useful if you have a custom "
447 "shader that could turn opaque pixels transparent."},
448 [319] = {"no-x-selection" , ENABLE(no_x_selection)},
449 [323] = {"use-damage" , ENABLE(use_damage) , "Render only the damaged (changed) part of the screen"},
450 [324] = {"no-use-damage" , DISABLE(use_damage) , "Disable the use of damage information. This cause the whole screen to be"
451 "redrawn every time, instead of the part of the screen that has actually "
452 "changed. Potentially degrades the performance, but might fix some artifacts."},
453 [267] = {"detect-rounded-corners" , ENABLE(detect_rounded_corners) , "Try to detect windows with rounded corners and don't consider them shaped "
454 "windows. Affects --shadow-ignore-shaped, --unredir-if-possible, and "
455 "possibly others. You need to turn this on manually if you want to match "
456 "against rounded_corners in conditions."},
457 [298] = {"glx-no-rebind-pixmap" , WARN_DEPRECATED(ENABLE(glx_no_rebind_pixmap))},
458 [291] = {"glx-no-stencil" , WARN_DEPRECATED(ENABLE(glx_no_stencil))},
459 [325] = {"no-vsync" , DISABLE(vsync) , "Disable VSync"},
460 [327] = {"transparent-clipping" , ENABLE(transparent_clipping) , "Make transparent windows clip other windows like non-transparent windows do, "
461 "instead of blending on top of them"},
462 [339] = {"dithered-present" , ENABLE(dithered_present) , "Use higher precision during rendering, and apply dither when presenting the "
463 "rendered screen. Reduces banding artifacts, but might cause performance "
464 "degradation. Only works with OpenGL."},
465 [341] = {"no-frame-pacing" , DISABLE(frame_pacing) , "Disable frame pacing. This might increase the latency."},
466 [733] = {"legacy-backends" , WARN_DEPRECATED(ENABLE(use_legacy_backends)), NULL},
467 [800] = {"monitor-repaint" , ENABLE(monitor_repaint) , "Highlight the updated area of the screen. For debugging."},
468 [801] = {"diagnostics" , ENABLE(print_diagnostics) , "Print diagnostic information"},
469 [802] = {"debug-mode" , ENABLE(debug_mode) , "Render into a separate window, and don't take over the screen. Useful when "
470 "you want to attach a debugger to picom"},
471 [803] = {"no-ewmh-fullscreen" , ENABLE(no_ewmh_fullscreen) , "Do not use EWMH to detect fullscreen windows. Reverts to checking if a "
472 "window is fullscreen based only on its size and coordinates."},
473 [804] = {"realtime" , ENABLE(use_realtime_scheduling) , "Enable realtime scheduling. This might reduce latency, but might also cause "
474 "other issues. Disable this if you see the compositor being killed."},
475 [805] = {"monitor" , ENABLE(inspect_monitor) , "For picom-inspect, run in a loop and dump information every time something "
476 "changed about a window.", "picom-inspect"},
477
478 // Flags that takes an argument
479 ['r'] = {"shadow-radius" , INTEGER(shadow_radius, 0, INT_MAX) , "The blur radius for shadows. (default 12)"},
480 ['o'] = {"shadow-opacity" , FLOAT(shadow_opacity, 0, 1) , "The translucency for shadows. (default .75)"},
481 ['l'] = {"shadow-offset-x" , INTEGER(shadow_offset_x, INT_MIN, INT_MAX) , "The left offset for shadows. (default -15)"},
482 ['t'] = {"shadow-offset-y" , INTEGER(shadow_offset_y, INT_MIN, INT_MAX) , "The top offset for shadows. (default -15)"},
483 ['I'] = {"fade-in-step" , FLOAT(fade_in_step, 0, 1) , "Opacity change between steps while fading in. (default 0.028)"},
484 ['O'] = {"fade-out-step" , FLOAT(fade_out_step, 0, 1) , "Opacity change between steps while fading out. (default 0.03)"},
485 ['D'] = {"fade-delta" , INTEGER(fade_delta, 1, INT_MAX) , "The time between steps in a fade in milliseconds. (default 10)"},
486 ['e'] = {"frame-opacity" , FLOAT(frame_opacity, 0, 1) , "Opacity of window titlebars and borders. (0.0 - 1.0)"},
487 [257] = {"shadow-red" , FLOAT(shadow_red, 0, 1) , "Red color value of shadow (0.0 - 1.0, defaults to 0)."},
488 [258] = {"shadow-green" , FLOAT(shadow_green, 0, 1) , "Green color value of shadow (0.0 - 1.0, defaults to 0)."},
489 [259] = {"shadow-blue" , FLOAT(shadow_blue, 0, 1) , "Blue color value of shadow (0.0 - 1.0, defaults to 0)."},
490 [283] = {"blur-background" , FIXED(blur_method, BLUR_METHOD_KERNEL) , "Blur background of semi-transparent / ARGB windows. May impact performance"},
491 [290] = {"backend" , DO(store_backend) , "Backend. Possible values are: " BACKENDS},
492 [293] = {"benchmark" , INTEGER(benchmark, 0, INT_MAX) , "Benchmark mode. Repeatedly paint until reaching the specified cycles."},
493 [302] = {"resize-damage" , WARN_DEPRECATED(INTEGER(resize_damage, INT_MIN, INT_MAX))}, // only used by legacy backends
494 [309] = {"unredir-if-possible-delay" , INTEGER(unredir_if_possible_delay, 0, INT_MAX) , "Delay before unredirecting the window, in milliseconds. Defaults to 0."},
495 [310] = {"write-pid-path" , NAMED_STRING(write_pid_path, "PATH") , "Write process ID to a file."},
496 [322] = {"log-file" , STRING(logpath) , "Path to the log file."},
497 [326] = {"max-brightness" , FLOAT(max_brightness, 0, 1) , "Dims windows which average brightness is above this threshold. Requires "
498 "--no-use-damage. (default: 1.0, meaning no dimming)"},
499 [329] = {"blur-size" , INTEGER(blur_radius, 0, INT_MAX) , "The radius of the blur kernel for 'box' and 'gaussian' blur method."},
500 [330] = {"blur-deviation" , FLOAT(blur_deviation, 0, INFINITY) , "The standard deviation for the 'gaussian' blur method."},
501 [331] = {"blur-strength" , INTEGER(blur_strength, 0, INT_MAX) , "The strength level of the 'dual_kawase' blur method."},
502 [333] = {"corner-radius" , INTEGER(corner_radius, 0, INT_MAX) , "Sets the radius of rounded window corners. When > 0, the compositor will "
503 "round the corners of windows. (defaults to 0)."},
504 [336] = {"window-shader-fg" , SHADER(window_shader_fg) , "Specify GLSL fragment shader path for rendering window contents. See man "
505 "page for more details."},
506 [342] = {"root-pixmap-shader" , SHADER(root_pixmap_shader) , "Specify GLSL fragment shader path for rendering the root pixmap."},
507 [294] = {"benchmark-wid" , DO(store_benchmark_wid) , "Specify window ID to repaint in benchmark mode. If omitted or is 0, the whole"
508 " screen is repainted."},
509 [301] = {"blur-kern" , DO(store_blur_kern) , "Specify the blur convolution kernel, see man page for more details"},
510 [332] = {"shadow-color" , DO(store_shadow_color) , "Color of shadow, as a hex RGB string (defaults to #000000)"},
511
512 // Rules
513 [263] = {"shadow-exclude" , RULES(shadow_blacklist) , "Exclude conditions for shadows."},
514 [279] = {"focus-exclude" , RULES(focus_blacklist) , "Specify a list of conditions of windows that should always be considered focused."},
515 [288] = {"invert-color-include" , RULES(invert_color_list) , "Specify a list of conditions of windows that should be painted with "
516 "inverted color."},
517 [296] = {"blur-background-exclude" , RULES(blur_background_blacklist) , "Exclude conditions for background blur."},
518 [300] = {"fade-exclude" , RULES(fade_blacklist) , "Exclude conditions for fading."},
519 [306] = {"paint-exclude" , RULES(paint_blacklist) , NULL},
520 [308] = {"unredir-if-possible-exclude" , RULES(unredir_if_possible_blacklist) , "Conditions of windows that shouldn't be considered full-screen for "
521 "unredirecting screen."},
522 [334] = {"rounded-corners-exclude" , RULES(rounded_corners_blacklist) , "Exclude conditions for rounded corners."},
523 [335] = {"clip-shadow-above" , RULES(shadow_clip_list) , "Specify a list of conditions of windows to not paint a shadow over, such "
524 "as a dock window."},
525 [338] = {"transparent-clipping-exclude", RULES(transparent_clipping_blacklist), "Specify a list of conditions of windows that should never have "
526 "transparent clipping applied. Useful for screenshot tools, where you "
527 "need to be able to see through transparent parts of the window."},
528
529 // Rules that are too long to fit in one line
530 [304] = {"opacity-rule" , NUMERIC_RULES(opacity_rules, "OPACITY", 0, 100),
531 "Specify a list of opacity rules, see man page for more details"},
532 [337] = {"window-shader-fg-rule" , NAMED_RULES(window_shader_fg_rules, "PATH", WINDOW_SHADER_RULE),
533 "Specify GLSL fragment shader path for rendering window contents using patterns. Pattern should be "
534 "in the format of SHADER_PATH:PATTERN, similar to --opacity-rule. SHADER_PATH can be \"default\", "
535 "in which case the default shader will be used. See man page for more details"},
536 [340] = {"corner-radius-rules" , NUMERIC_RULES(corner_radius_rules, "RADIUS", 0, INT_MAX),
537 "Window rules for specific rounded corner radii."},
538
539 // Options that are too long to fit in one line
540 [321] = {"log-level" , PARSE_WITH(string_to_log_level, LOG_LEVEL_INVALID, log_level),
541 "Log level, possible values are: trace, debug, info, warn, error"},
542 [328] = {"blur-method", PARSE_WITH(parse_blur_method, BLUR_METHOD_INVALID, blur_method),
543 "The algorithm used for background bluring. Available choices are: 'none' to disable, 'gaussian', "
544 "'box' or 'kernel' for custom convolution blur with --blur-kern."},
545
546 // Deprecated options
547 [269] = {"refresh-rate" , WARN_DEPRECATED(IGNORE(required_argument))},
548 [272] = {"xinerama-shadow-crop", SAY_DEPRECATED(false, "Use --crop-shadow-to-monitor instead.", ENABLE(crop_shadow_to_monitor))},
549 [287] = {"logpath" , SAY_DEPRECATED(false, "Use --log-file instead." , STRING(logpath))},
550 [289] = {"opengl" , SAY_DEPRECATED(false, "Use --backend=glx instead." , FIXED_STR(backend, "glx"))},
551 [305] = {"shadow-exclude-reg" , SAY_DEPRECATED(true, "Use --clip-shadow-above instead." , REJECT(required_argument))},
552 };
553 // clang-format on
554
555 static void setup_longopts(void) {
556 auto opts = ccalloc(ARR_SIZE(picom_options) + 1, struct option);
557 int option_count = 0;
558 for (size_t i = 0; i < ARR_SIZE(picom_options); i++) {
559 if (picom_options[i].arg.handler == NULL) {
560 continue;
561 }
562 opts[option_count].name = picom_options[i].long_name;
563 opts[option_count].has_arg = picom_options[i].has_arg;
564 opts[option_count].flag = NULL;
565 opts[option_count].val = (int)i;
566 option_count++;
567 }
568 longopts = opts;
569 }
570
571 void print_help(const char *help, size_t indent, size_t curr_indent, size_t line_wrap,
572 FILE *f) {
573 if (curr_indent > indent) {
574 fputs("\n", f);
575 curr_indent = 0;
576 }
577
578 if (line_wrap - indent <= 1) {
579 line_wrap = indent + 2;
580 }
581
582 size_t pos = 0;
583 size_t len = strlen(help);
584 while (pos < len) {
585 fprintf(f, "%*s", (int)(indent - curr_indent), "");
586 curr_indent = 0;
587 size_t towrite = line_wrap - indent;
588 while (help[pos] == ' ') {
589 pos++;
590 }
591 if (pos + towrite > len) {
592 towrite = len - pos;
593 fwrite(help + pos, 1, towrite, f);
594 } else {
595 auto space_break = towrite;
596 while (space_break > 0 && help[pos + space_break - 1] != ' ') {
597 space_break--;
598 }
599
600 bool print_hyphen = false;
601 if (space_break == 0) {
602 print_hyphen = true;
603 towrite--;
604 } else {
605 towrite = space_break;
606 }
607
608 fwrite(help + pos, 1, towrite, f);
609
610 if (print_hyphen) {
611 fputs("-", f);
612 }
613 }
614
615 fputs("\n", f);
616 pos += towrite;
617 }
618 }
619
620 /**
621 * Print usage text.
622 */
623 static void usage(const char *argv0, int ret) {
624 FILE *f = (ret ? stderr : stdout);
625 fprintf(f, "picom " PICOM_FULL_VERSION "\n");
626 fprintf(f, "Standalone X11 compositor\n");
627 fprintf(f, "Please report bugs to https://github.com/yshui/picom\n\n");
628
629 fprintf(f, "Usage: %s [OPTION]...\n\n", argv0);
630 fprintf(f, "OPTIONS:\n");
631
632 int line_wrap = 80;
633 struct winsize window_size = {0};
634 if (ioctl(fileno(f), TIOCGWINSZ, &window_size) != -1) {
635 line_wrap = window_size.ws_col;
636 }
637
638 const char *basename = strrchr(argv0, '/') ? strrchr(argv0, '/') + 1 : argv0;
639
640 size_t help_indent = 0;
641 for (size_t i = 0; i < ARR_SIZE(picom_options); i++) {
642 if (picom_options[i].help == NULL) {
643 // Hide options with no help message.
644 continue;
645 }
646 if (picom_options[i].argv0 != NULL &&
647 strcmp(picom_options[i].argv0, basename) != 0) {
648 // Hide options that are not for this program.
649 continue;
650 }
651 auto option_len = strlen(picom_options[i].long_name) + 2 + 4;
652 if (picom_options[i].arg.name) {
653 option_len += strlen(picom_options[i].arg.name) + 1;
654 }
655 if (option_len > help_indent && option_len < 30) {
656 help_indent = option_len;
657 }
658 }
659 help_indent += 6;
660
661 for (size_t i = 0; i < ARR_SIZE(picom_options); i++) {
662 if (picom_options[i].help == NULL) {
663 continue;
664 }
665 if (picom_options[i].argv0 != NULL &&
666 strcmp(picom_options[i].argv0, basename) != 0) {
667 // Hide options that are not for this program.
668 continue;
669 }
670 size_t option_len = 8;
671 fprintf(f, " ");
672 if ((i > 'a' && i < 'z') || (i > 'A' && i < 'Z')) {
673 fprintf(f, "-%c, ", (char)i);
674 } else {
675 fprintf(f, " ");
676 }
677 fprintf(f, "--%s", picom_options[i].long_name);
678 option_len += strlen(picom_options[i].long_name) + 2;
679 if (picom_options[i].arg.name) {
680 fprintf(f, "=%s", picom_options[i].arg.name);
681 option_len += strlen(picom_options[i].arg.name) + 1;
682 }
683 fprintf(f, " ");
684 option_len += 2;
685 print_help(picom_options[i].help, help_indent, option_len,
686 (size_t)line_wrap, f);
687 }
688 }
689
690 static void set_default_winopts(options_t *opt) {
691 auto mask = opt->wintype_option_mask;
692 // Apply default wintype options.
693 if (!mask[WINTYPE_DESKTOP].shadow) {
694 // Desktop windows are always drawn without shadow by default.
695 mask[WINTYPE_DESKTOP].shadow = true;
696 opt->wintype_option[WINTYPE_DESKTOP].shadow = false;
697 }
698
699 // Focused/unfocused state only apply to a few window types, all other windows
700 // are always considered focused.
701 const wintype_t nofocus_type[] = {WINTYPE_UNKNOWN, WINTYPE_NORMAL, WINTYPE_UTILITY};
702 for (unsigned long i = 0; i < ARR_SIZE(nofocus_type); i++) {
703 if (!mask[nofocus_type[i]].focus) {
704 mask[nofocus_type[i]].focus = true;
705 opt->wintype_option[nofocus_type[i]].focus = false;
706 }
707 }
708 for (unsigned long i = 0; i < NUM_WINTYPES; i++) {
709 if (!mask[i].shadow) {
710 mask[i].shadow = true;
711 opt->wintype_option[i].shadow = opt->shadow_enable;
712 }
713 if (!mask[i].fade) {
714 mask[i].fade = true;
715 opt->wintype_option[i].fade = opt->fading_enable;
716 }
717 if (!mask[i].focus) {
718 mask[i].focus = true;
719 opt->wintype_option[i].focus = true;
720 }
721 if (!mask[i].blur_background) {
722 mask[i].blur_background = true;
723 opt->wintype_option[i].blur_background =
724 opt->blur_method != BLUR_METHOD_NONE;
725 }
726 if (!mask[i].full_shadow) {
727 mask[i].full_shadow = true;
728 opt->wintype_option[i].full_shadow = false;
729 }
730 if (!mask[i].redir_ignore) {
731 mask[i].redir_ignore = true;
732 opt->wintype_option[i].redir_ignore = false;
733 }
734 if (!mask[i].opacity) {
735 mask[i].opacity = true;
736 // Opacity is not set to a concrete number here because the
737 // opacity logic is complicated, and needs an "unset" state
738 opt->wintype_option[i].opacity = NAN;
739 }
740 if (!mask[i].clip_shadow_above) {
741 mask[i].clip_shadow_above = true;
742 opt->wintype_option[i].clip_shadow_above = false;
743 }
744 }
745 }
746
747 static const char *shortopts = "D:I:O:r:o:m:l:t:i:e:hcfCzGb";
748
749 /// Get config options that are needed to parse the rest of the options
750 /// Return true if we should quit
751 bool get_early_config(int argc, char *const *argv, char **config_file, bool *all_xerrors,
752 bool *fork, int *exit_code) {
753 setup_longopts();
754
755 scoped_charp current_working_dir = getcwd(NULL, 0);
756 int o = 0, longopt_idx = -1;
757
758 // Pre-parse the command line arguments to check for --config and invalid
759 // switches
760 // Must reset optind to 0 here in case we reread the command line
761 // arguments
762 optind = 1;
763 *config_file = NULL;
764 *exit_code = 0;
765 while (-1 != (o = getopt_long(argc, argv, shortopts, longopts, &longopt_idx))) {
766 if (o == 256) {
767 *config_file = strdup(optarg);
768 } else if (o == 'h') {
769 usage(argv[0], 0);
770 return true;
771 } else if (o == 'b') {
772 *fork = true;
773 } else if (o == 314) {
774 *all_xerrors = true;
775 } else if (o == 318) {
776 printf(PICOM_FULL_VERSION "\n");
777 return true;
778 } else if (o == 307) {
779 // --plugin
780 if (!load_plugin(optarg, current_working_dir)) {
781 log_error("Failed to load plugin %s", optarg);
782 goto err;
783 }
784 } else if (o == '?' || o == ':') {
785 usage(argv[0], 1);
786 goto err;
787 }
788 // TODO(yshui) maybe log-level should be handled here.
789 }
790
791 // Check for abundant positional arguments
792 if (optind < argc) {
793 // log is not initialized here yet
794 fprintf(stderr, "picom doesn't accept positional arguments.\n");
795 goto err;
796 }
797
798 return false;
799 err:
800 *exit_code = 1;
801 return true;
802 }
803
804 static void script_ptr_deinit(struct script **ptr) {
805 if (*ptr) {
806 script_free(*ptr);
807 *ptr = NULL;
808 }
809 }
810
811 static bool sanitize_options(struct options *opt) {
812 if (opt->backend == NULL) {
813 log_error("Backend not specified. You must choose one "
814 "explicitly. Valid ones are: ");
815 for (auto i = backend_iter(); i; i = backend_iter_next(i)) {
816 log_error("\t%s", backend_name(i));
817 }
818 return false;
819 }
820
821 if (opt->max_brightness < 1.0 && opt->use_damage) {
822 log_warn("--max-brightness requires --no-use-damage. "
823 "Falling back to 1.0.");
824 opt->max_brightness = 1.0;
825 }
826
827 if (opt->write_pid_path && *opt->write_pid_path != '/') {
828 log_warn("--write-pid-path is not an absolute path");
829 }
830
831 // Sanitize parameters for dual-filter kawase blur
832 if (opt->blur_method == BLUR_METHOD_DUAL_KAWASE) {
833 if (opt->blur_strength <= 0 && opt->blur_radius > 500) {
834 log_warn("Blur radius >500 not supported by dual_kawase "
835 "method, "
836 "capping to 500.");
837 opt->blur_radius = 500;
838 }
839 if (opt->blur_strength > 20) {
840 log_warn("Blur strength >20 not supported by dual_kawase "
841 "method, "
842 "capping to 20.");
843 opt->blur_strength = 20;
844 }
845 }
846
847 if (opt->resize_damage < 0) {
848 log_warn("Negative --resize-damage will not work correctly.");
849 }
850
851 if (opt->has_both_style_of_rules) {
852 log_warn("You have set both \"rules\", as well as old-style rule options "
853 "in your configuration. The old-style rule options will have no "
854 "effect. It is recommended that you remove the old-style rule "
855 "options, and use only \"rules\" for all your window rules. If "
856 "you do genuinely need to use the old-style rule options, you "
857 "must not set \"rules\".");
858 }
859
860 return true;
861 }
862
863 /**
864 * Process arguments and configuration files.
865 */
866 bool get_cfg(options_t *opt, int argc, char *const *argv) {
867 int o = 0, longopt_idx = -1;
868 bool failed = false;
869 optind = 1;
870 const char *basename = strrchr(argv[0], '/') ? strrchr(argv[0], '/') + 1 : argv[0];
871 while (-1 != (o = getopt_long(argc, argv, shortopts, longopts, &longopt_idx))) {
872 if (o == '?' || o == ':' || picom_options[o].arg.handler == NULL) {
873 usage(argv[0], 1);
874 failed = true;
875 } else if (picom_options[o].argv0 != NULL &&
876 strcmp(picom_options[o].argv0, basename) != 0) {
877 log_error("Invalid option %s", argv[optind - 1]);
878 failed = true;
879 } else if (!picom_options[o].arg.handler(
880 &picom_options[o], &picom_options[o].arg, optarg, opt)) {
881 failed = true;
882 }
883
884 if (failed) {
885 // Parsing this option has failed, break the loop
886 break;
887 }
888 }
889
890 if (failed) {
891 return false;
892 }
893
894 log_set_level_tls(opt->log_level);
895
896 if (!sanitize_options(opt)) {
897 return false;
898 }
899
900 // --blur-background-frame implies --blur-background
901 if (opt->blur_background_frame && opt->blur_method == BLUR_METHOD_NONE) {
902 opt->blur_method = BLUR_METHOD_KERNEL;
903 }
904
905 // Apply default wintype options that are dependent on global options
906 set_default_winopts(opt);
907
908 // Determine whether we track window grouping
909 if (opt->detect_transient || opt->detect_client_leader) {
910 opt->track_leader = true;
911 }
912
913 // Fill default blur kernel
914 if (opt->blur_method == BLUR_METHOD_KERNEL &&
915 (!opt->blur_kerns || !opt->blur_kerns[0])) {
916 opt->blur_kerns = parse_blur_kern_lst("3x3box", &opt->blur_kernel_count);
917 CHECK(opt->blur_kerns);
918 CHECK(opt->blur_kernel_count);
919 }
920
921 if (opt->fading_enable) {
922 generate_fading_config(opt);
923 }
924 return true;
925 }
926
927 void options_postprocess_c2_lists(struct c2_state *state, struct x_connection *c,
928 struct options *option) {
929 if (!list_is_empty(&option->rules)) {
930 if (!c2_list_postprocess(state, c->c, &option->rules)) {
931 log_error("Post-processing of rules failed, some of your rules "
932 "might not work");
933 }
934 return;
935 }
936
937 if (!(c2_list_postprocess(state, c->c, &option->unredir_if_possible_blacklist) &&
938 c2_list_postprocess(state, c->c, &option->paint_blacklist) &&
939 c2_list_postprocess(state, c->c, &option->shadow_blacklist) &&
940 c2_list_postprocess(state, c->c, &option->shadow_clip_list) &&
941 c2_list_postprocess(state, c->c, &option->fade_blacklist) &&
942 c2_list_postprocess(state, c->c, &option->blur_background_blacklist) &&
943 c2_list_postprocess(state, c->c, &option->invert_color_list) &&
944 c2_list_postprocess(state, c->c, &option->window_shader_fg_rules) &&
945 c2_list_postprocess(state, c->c, &option->opacity_rules) &&
946 c2_list_postprocess(state, c->c, &option->rounded_corners_blacklist) &&
947 c2_list_postprocess(state, c->c, &option->corner_radius_rules) &&
948 c2_list_postprocess(state, c->c, &option->focus_blacklist) &&
949 c2_list_postprocess(state, c->c, &option->transparent_clipping_blacklist))) {
950 log_error("Post-processing of conditionals failed, some of your "
951 "rules might not work");
952 }
953 }
954
955 static void free_window_maybe_options(void *data) {
956 auto wopts = (struct window_maybe_options *)data;
957 if (wopts) {
958 free((void *)wopts->shader);
959 free(wopts);
960 }
961 }
962
963 void options_destroy(struct options *options) {
964 // Free blacklists
965 c2_list_free(&options->shadow_blacklist, NULL);
966 c2_list_free(&options->shadow_clip_list, NULL);
967 c2_list_free(&options->fade_blacklist, NULL);
968 c2_list_free(&options->focus_blacklist, NULL);
969 c2_list_free(&options->invert_color_list, NULL);
970 c2_list_free(&options->blur_background_blacklist, NULL);
971 c2_list_free(&options->opacity_rules, NULL);
972 c2_list_free(&options->paint_blacklist, NULL);
973 c2_list_free(&options->unredir_if_possible_blacklist, NULL);
974 c2_list_free(&options->rounded_corners_blacklist, NULL);
975 c2_list_free(&options->corner_radius_rules, NULL);
976 c2_list_free(&options->window_shader_fg_rules, free);
977 c2_list_free(&options->transparent_clipping_blacklist, NULL);
978 c2_list_free(&options->rules, free_window_maybe_options);
979
980 free(options->config_file_path);
981 free(options->write_pid_path);
982 free(options->logpath);
983 free(options->window_shader_fg);
984 free(options->root_pixmap_shader);
985
986 for (int i = 0; i < options->blur_kernel_count; ++i) {
987 free(options->blur_kerns[i]);
988 }
989 free(options->blur_kerns);
990
991 dynarr_free(options->all_scripts, script_ptr_deinit);
992 memset(options->animations, 0, sizeof(options->animations));
993
994 list_foreach_safe(struct included_config_file, i, &options->included_config_files,
995 siblings) {
996 free(i->path);
997 list_remove(&i->siblings);
998 free(i);
999 }
1000
1001 struct option_name *bad_opt, *next_bad_opt;
1002 HASH_ITER(hh, options->problematic_options, bad_opt, next_bad_opt) {
1003 HASH_DEL(options->problematic_options, bad_opt);
1004 free(bad_opt);
1005 }
1006 }
1007
1008 // vim: set noet sw=8 ts=8 :