1 #ifndef _SWAY_CONFIG_H
2 #define _SWAY_CONFIG_H
3 #include <libinput.h>
4 #include <stdint.h>
5 #include <string.h>
6 #include <time.h>
7 #include <wlr/interfaces/wlr_switch.h>
8 #include <wlr/types/wlr_box.h>
9 #include <xkbcommon/xkbcommon.h>
10 #include "../include/config.h"
11 #include "list.h"
12 #include "swaynag.h"
13 #include "tree/container.h"
14 #include "sway/tree/root.h"
15 #include "wlr-layer-shell-unstable-v1-protocol.h"
16 
17 // TODO: Refactor this shit
18 
19 /**
20  * Describes a variable created via the `set` command.
21  */
22 struct sway_variable {
23 	char *name;
24 	char *value;
25 };
26 
27 enum binding_input_type {
28 	BINDING_KEYCODE,
29 	BINDING_KEYSYM,
30 	BINDING_MOUSECODE,
31 	BINDING_MOUSESYM,
32 	BINDING_SWITCH
33 };
34 
35 enum binding_flags {
36 	BINDING_RELEASE = 1 << 0,
37 	BINDING_LOCKED = 1 << 1, // keyboard only
38 	BINDING_BORDER = 1 << 2, // mouse only; trigger on container border
39 	BINDING_CONTENTS = 1 << 3, // mouse only; trigger on container contents
40 	BINDING_TITLEBAR = 1 << 4, // mouse only; trigger on container titlebar
41 	BINDING_CODE = 1 << 5, // keyboard only; convert keysyms into keycodes
42 	BINDING_RELOAD = 1 << 6, // switch only; (re)trigger binding on reload
43 	BINDING_INHIBITED = 1 << 7, // keyboard only: ignore shortcut inhibitor
44 	BINDING_NOREPEAT = 1 << 8, // keyboard only; do not trigger when repeating a held key
45 };
46 
47 /**
48  * A key binding and an associated command.
49  */
50 struct sway_binding {
51 	enum binding_input_type type;
52 	int order;
53 	char *input;
54 	uint32_t flags;
55 	list_t *keys; // sorted in ascending order
56 	list_t *syms; // sorted in ascending order; NULL if BINDING_CODE is not set
57 	uint32_t modifiers;
58 	xkb_layout_index_t group;
59 	char *command;
60 };
61 
62 /**
63  * A mouse binding and an associated command.
64  */
65 struct sway_mouse_binding {
66 	uint32_t button;
67 	char *command;
68 };
69 
70 /**
71  * A laptop switch binding and an associated command.
72  */
73 struct sway_switch_binding {
74 	enum wlr_switch_type type;
75 	enum wlr_switch_state state;
76 	uint32_t flags;
77 	char *command;
78 };
79 
80 /**
81  * Focus on window activation.
82  */
83 enum sway_fowa {
84 	FOWA_SMART,
85 	FOWA_URGENT,
86 	FOWA_FOCUS,
87 	FOWA_NONE,
88 };
89 
90 /**
91  * A "mode" of keybindings created via the `mode` command.
92  */
93 struct sway_mode {
94 	char *name;
95 	list_t *keysym_bindings;
96 	list_t *keycode_bindings;
97 	list_t *mouse_bindings;
98 	list_t *switch_bindings;
99 	bool pango;
100 };
101 
102 struct input_config_mapped_from_region {
103 	double x1, y1;
104 	double x2, y2;
105 	bool mm;
106 };
107 
108 struct calibration_matrix {
109 	bool configured;
110 	float matrix[6];
111 };
112 
113 enum input_config_mapped_to {
114 	MAPPED_TO_DEFAULT,
115 	MAPPED_TO_OUTPUT,
116 	MAPPED_TO_REGION,
117 };
118 
119 /**
120  * options for input devices
121  */
122 struct input_config {
123 	char *identifier;
124 	const char *input_type;
125 
126 	int accel_profile;
127 	struct calibration_matrix calibration_matrix;
128 	int click_method;
129 	int drag;
130 	int drag_lock;
131 	int dwt;
132 	int left_handed;
133 	int middle_emulation;
134 	int natural_scroll;
135 	float pointer_accel;
136 	float scroll_factor;
137 	int repeat_delay;
138 	int repeat_rate;
139 	int scroll_button;
140 	int scroll_method;
141 	int send_events;
142 	int tap;
143 	int tap_button_map;
144 
145 	char *xkb_layout;
146 	char *xkb_model;
147 	char *xkb_options;
148 	char *xkb_rules;
149 	char *xkb_variant;
150 	char *xkb_file;
151 
152 	bool xkb_file_is_set;
153 
154 	int xkb_numlock;
155 	int xkb_capslock;
156 
157 	struct input_config_mapped_from_region *mapped_from_region;
158 
159 	enum input_config_mapped_to mapped_to;
160 	char *mapped_to_output;
161 	struct wlr_box *mapped_to_region;
162 
163 	bool capturable;
164 	struct wlr_box region;
165 };
166 
167 /**
168  * Options for misc device configurations that happen in the seat block
169  */
170 struct seat_attachment_config {
171 	char *identifier;
172 	// TODO other things are configured here for some reason
173 };
174 
175 enum seat_config_allow_constrain {
176 	CONSTRAIN_DEFAULT, // the default is currently enabled
177 	CONSTRAIN_ENABLE,
178 	CONSTRAIN_DISABLE,
179 };
180 
181 enum seat_config_shortcuts_inhibit {
182 	SHORTCUTS_INHIBIT_DEFAULT, // the default is currently enabled
183 	SHORTCUTS_INHIBIT_ENABLE,
184 	SHORTCUTS_INHIBIT_DISABLE,
185 };
186 
187 enum seat_keyboard_grouping {
188 	KEYBOARD_GROUP_DEFAULT, // the default is currently smart
189 	KEYBOARD_GROUP_NONE,
190 	KEYBOARD_GROUP_SMART, // keymap and repeat info
191 };
192 
193 enum sway_input_idle_source {
194 	IDLE_SOURCE_KEYBOARD = 1 << 0,
195 	IDLE_SOURCE_POINTER = 1 << 1,
196 	IDLE_SOURCE_TOUCH = 1 << 2,
197 	IDLE_SOURCE_TABLET_PAD = 1 << 3,
198 	IDLE_SOURCE_TABLET_TOOL = 1 << 4,
199 	IDLE_SOURCE_SWITCH = 1 << 5,
200 };
201 
202 /**
203  * Options for multiseat and other misc device configurations
204  */
205 struct seat_config {
206 	char *name;
207 	int fallback; // -1 means not set
208 	list_t *attachments; // list of seat_attachment configs
209 	int hide_cursor_timeout;
210 	enum seat_config_allow_constrain allow_constrain;
211 	enum seat_config_shortcuts_inhibit shortcuts_inhibit;
212 	enum seat_keyboard_grouping keyboard_grouping;
213 	uint32_t idle_inhibit_sources, idle_wake_sources;
214 	struct {
215 		char *name;
216 		int size;
217 	} xcursor_theme;
218 };
219 
220 enum config_dpms {
221 	DPMS_IGNORE,
222 	DPMS_ON,
223 	DPMS_OFF,
224 };
225 
226 enum scale_filter_mode {
227 	SCALE_FILTER_DEFAULT, // the default is currently smart
228 	SCALE_FILTER_LINEAR,
229 	SCALE_FILTER_NEAREST,
230 	SCALE_FILTER_SMART,
231 };
232 
233 /**
234  * Size and position configuration for a particular output.
235  *
236  * This is set via the `output` command.
237  */
238 struct output_config {
239 	char *name;
240 	int enabled;
241 	int width, height;
242 	float refresh_rate;
243 	int custom_mode;
244 	int x, y;
245 	float scale;
246 	enum scale_filter_mode scale_filter;
247 	int32_t transform;
248 	enum wl_output_subpixel subpixel;
249 	int max_render_time; // In milliseconds
250 	int adaptive_sync;
251 
252 	char *background;
253 	char *background_option;
254 	char *background_fallback;
255 	enum config_dpms dpms_state;
256 };
257 
258 /**
259  * Stores size of gaps for each side
260  */
261 struct side_gaps {
262 	int top;
263 	int right;
264 	int bottom;
265 	int left;
266 };
267 
268 /**
269  * Stores configuration for a workspace, regardless of whether the workspace
270  * exists.
271  */
272 struct workspace_config {
273 	char *workspace;
274 	list_t *outputs;
275 	int gaps_inner;
276 	struct side_gaps gaps_outer;
277 };
278 
279 struct bar_config {
280 	char *swaybar_command;
281 	struct wl_client *client;
282 	struct wl_listener client_destroy;
283 
284 	/**
285 	 * One of "dock", "hide", "invisible"
286 	 *
287 	 * Always visible in dock mode. Visible only when modifier key is held in hide mode.
288 	 * Never visible in invisible mode.
289 	 */
290 	char *mode;
291 	/**
292 	 * One of "show" or "hide".
293 	 *
294 	 * In "show" mode, it will always be shown on top of the active workspace.
295 	 */
296 	char *hidden_state;
297 	bool visible_by_modifier; // only relevant in "hide" mode
298 	/**
299 	 * Id name used to identify the bar through IPC.
300 	 *
301 	 * Defaults to bar-x, where x corresponds to the position of the
302 	 * embedding bar block in the config file (bar-0, bar-1, ...).
303 	 */
304 	char *id;
305 	uint32_t modifier;
306 	list_t *outputs;
307 	char *position;
308 	list_t *bindings;
309 	char *status_command;
310 	bool pango_markup;
311 	char *font;
312 	int height; // -1 not defined
313 	bool workspace_buttons;
314 	bool wrap_scroll;
315 	char *separator_symbol;
316 	bool strip_workspace_numbers;
317 	bool strip_workspace_name;
318 	bool binding_mode_indicator;
319 	bool verbose;
320 	struct side_gaps gaps;
321 	int status_padding;
322 	int status_edge_padding;
323 	struct {
324 		char *background;
325 		char *statusline;
326 		char *separator;
327 		char *focused_background;
328 		char *focused_statusline;
329 		char *focused_separator;
330 		char *focused_workspace_border;
331 		char *focused_workspace_bg;
332 		char *focused_workspace_text;
333 		char *active_workspace_border;
334 		char *active_workspace_bg;
335 		char *active_workspace_text;
336 		char *inactive_workspace_border;
337 		char *inactive_workspace_bg;
338 		char *inactive_workspace_text;
339 		char *urgent_workspace_border;
340 		char *urgent_workspace_bg;
341 		char *urgent_workspace_text;
342 		char *binding_mode_border;
343 		char *binding_mode_bg;
344 		char *binding_mode_text;
345 	} colors;
346 
347 #if HAVE_TRAY
348 	char *icon_theme;
349 	struct wl_list tray_bindings; // struct tray_binding::link
350 	list_t *tray_outputs; // char *
351 	int tray_padding;
352 #endif
353 };
354 
355 struct bar_binding {
356 	uint32_t button;
357 	bool release;
358 	char *command;
359 };
360 
361 #if HAVE_TRAY
362 struct tray_binding {
363 	uint32_t button;
364 	const char *command;
365 	struct wl_list link; // struct tray_binding::link
366 };
367 #endif
368 
369 struct border_colors {
370 	float border[4];
371 	float background[4];
372 	float text[4];
373 	float indicator[4];
374 	float child_border[4];
375 };
376 
377 enum edge_border_types {
378 	E_NONE, /**< Don't hide edge borders */
379 	E_VERTICAL, /**< hide vertical edge borders */
380 	E_HORIZONTAL, /**< hide horizontal edge borders */
381 	E_BOTH, /**< hide vertical and horizontal edge borders */
382 };
383 
384 enum edge_border_smart_types {
385 	ESMART_OFF,
386 	ESMART_ON, /**< hide edges if precisely one window is present in workspace */
387 	ESMART_NO_GAPS, /**< hide edges if one window and gaps to edge is zero */
388 };
389 
390 enum sway_popup_during_fullscreen {
391 	POPUP_SMART,
392 	POPUP_IGNORE,
393 	POPUP_LEAVE,
394 };
395 
396 enum command_context {
397 	CONTEXT_CONFIG = 1 << 0,
398 	CONTEXT_BINDING = 1 << 1,
399 	CONTEXT_IPC = 1 << 2,
400 	CONTEXT_CRITERIA = 1 << 3,
401 	CONTEXT_ALL = 0xFFFFFFFF,
402 };
403 
404 enum focus_follows_mouse_mode {
405 	FOLLOWS_NO,
406 	FOLLOWS_YES,
407 	FOLLOWS_ALWAYS,
408 };
409 
410 enum focus_wrapping_mode {
411 	WRAP_NO,
412 	WRAP_YES,
413 	WRAP_FORCE,
414 	WRAP_WORKSPACE,
415 };
416 
417 enum mouse_warping_mode {
418 	WARP_NO,
419 	WARP_OUTPUT,
420 	WARP_CONTAINER,
421 };
422 
423 enum alignment {
424 	ALIGN_LEFT,
425 	ALIGN_CENTER,
426 	ALIGN_RIGHT,
427 };
428 
429 enum xwayland_mode {
430 	XWAYLAND_MODE_DISABLED,
431 	XWAYLAND_MODE_LAZY,
432 	XWAYLAND_MODE_IMMEDIATE,
433 };
434 
435 /**
436  * The configuration struct. The result of loading a config file.
437  */
438 struct sway_config {
439 	char *swaynag_command;
440 	struct swaynag_instance swaynag_config_errors;
441 	list_t *symbols;
442 	list_t *modes;
443 	list_t *bars;
444 	list_t *cmd_queue;
445 	list_t *workspace_configs;
446 	list_t *output_configs;
447 	list_t *input_configs;
448 	list_t *input_type_configs;
449 	list_t *seat_configs;
450 	list_t *criteria;
451 	list_t *no_focus;
452 	list_t *active_bar_modifiers;
453 	struct sway_mode *current_mode;
454 	struct bar_config *current_bar;
455 	uint32_t floating_mod;
456 	bool floating_mod_inverse;
457 	uint32_t dragging_key;
458 	uint32_t resizing_key;
459 	char *floating_scroll_up_cmd;
460 	char *floating_scroll_down_cmd;
461 	char *floating_scroll_left_cmd;
462 	char *floating_scroll_right_cmd;
463 	enum sway_container_layout default_orientation;
464 	enum sway_container_layout default_layout;
465 	char *font;
466 	size_t font_height;
467 	size_t font_baseline;
468 	bool pango_markup;
469 	int titlebar_border_thickness;
470 	int titlebar_h_padding;
471 	int titlebar_v_padding;
472 	size_t urgent_timeout;
473 	enum sway_fowa focus_on_window_activation;
474 	enum sway_popup_during_fullscreen popup_during_fullscreen;
475 	enum xwayland_mode xwayland;
476 	int32_t xwayland_scale;
477 
478 	// swaybg
479 	char *swaybg_command;
480 	struct wl_client *swaybg_client;
481 	struct wl_listener swaybg_client_destroy;
482 
483 	// Flags
484 	enum focus_follows_mouse_mode focus_follows_mouse;
485 	enum mouse_warping_mode mouse_warping;
486 	enum focus_wrapping_mode focus_wrapping;
487 	bool active;
488 	bool failed;
489 	bool reloading;
490 	bool reading;
491 	bool validating;
492 	bool auto_back_and_forth;
493 	bool show_marks;
494 	enum alignment title_align;
495 
496 	bool tiling_drag;
497 	int tiling_drag_threshold;
498 
499 	bool smart_gaps;
500 	int gaps_inner;
501 	struct side_gaps gaps_outer;
502 
503 	list_t *config_chain;
504 	bool user_config_path;
505 	const char *current_config_path;
506 	const char *current_config;
507 	int current_config_line_number;
508 	char *current_config_line;
509 
510 	enum sway_container_border border;
511 	enum sway_container_border floating_border;
512 	int border_thickness;
513 	int floating_border_thickness;
514 	enum edge_border_types hide_edge_borders;
515 	enum edge_border_smart_types hide_edge_borders_smart;
516 	bool hide_lone_tab;
517 
518 	// border colors
519 	struct {
520 		struct border_colors focused;
521 		struct border_colors focused_inactive;
522 		struct border_colors unfocused;
523 		struct border_colors urgent;
524 		struct border_colors placeholder;
525 		float background[4];
526 	} border_colors;
527 
528 	// floating view
529 	int32_t floating_maximum_width;
530 	int32_t floating_maximum_height;
531 	int32_t floating_minimum_width;
532 	int32_t floating_minimum_height;
533 
534 	// The keysym to keycode translation
535 	struct xkb_state *keysym_translation_state;
536 
537 	// Context for command handlers
538 	struct {
539 		struct input_config *input_config;
540 		struct output_config *output_config;
541 		struct seat_config *seat_config;
542 		struct sway_seat *seat;
543 		struct sway_node *node;
544 		struct sway_container *container;
545 		struct sway_workspace *workspace;
546 		bool using_criteria;
547 		struct {
548 			int argc;
549 			char **argv;
550 		} leftovers;
551 	} handler_context;
552 };
553 
554 /**
555  * Loads the main config from the given path. is_active should be true when
556  * reloading the config.
557  */
558 bool load_main_config(const char *path, bool is_active, bool validating);
559 
560 /**
561  * Loads an included config. Can only be used after load_main_config.
562  */
563 void load_include_configs(const char *path, struct sway_config *config,
564 		struct swaynag_instance *swaynag);
565 
566 /**
567  * Reads the config from the given FILE.
568  */
569 bool read_config(FILE *file, struct sway_config *config,
570 		struct swaynag_instance *swaynag);
571 
572 /**
573  * Run the commands that were deferred when reading the config file.
574  */
575 void run_deferred_commands(void);
576 
577 /**
578  * Run the binding commands that were deferred when initializing the inputs
579  */
580 void run_deferred_bindings(void);
581 
582 /**
583  * Adds a warning entry to the swaynag instance used for errors.
584  */
585 void config_add_swaynag_warning(char *fmt, ...);
586 
587 /**
588  * Free config struct
589  */
590 void free_config(struct sway_config *config);
591 
592 void free_sway_variable(struct sway_variable *var);
593 
594 /**
595  * Does variable replacement for a string based on the config's currently loaded variables.
596  */
597 char *do_var_replacement(char *str);
598 
599 int input_identifier_cmp(const void *item, const void *data);
600 
601 struct input_config *new_input_config(const char* identifier);
602 
603 void merge_input_config(struct input_config *dst, struct input_config *src);
604 
605 struct input_config *store_input_config(struct input_config *ic, char **error);
606 
607 void input_config_fill_rule_names(struct input_config *ic,
608 		struct xkb_rule_names *rules);
609 
610 void free_input_config(struct input_config *ic);
611 
612 int seat_name_cmp(const void *item, const void *data);
613 
614 struct seat_config *new_seat_config(const char* name);
615 
616 void merge_seat_config(struct seat_config *dst, struct seat_config *src);
617 
618 struct seat_config *copy_seat_config(struct seat_config *seat);
619 
620 void free_seat_config(struct seat_config *ic);
621 
622 struct seat_attachment_config *seat_attachment_config_new(void);
623 
624 struct seat_attachment_config *seat_config_get_attachment(
625 		struct seat_config *seat_config, char *identifier);
626 
627 struct seat_config *store_seat_config(struct seat_config *seat);
628 
629 int output_name_cmp(const void *item, const void *data);
630 
631 void output_get_identifier(char *identifier, size_t len,
632 	struct sway_output *output);
633 
634 const char *sway_output_scale_filter_to_string(enum scale_filter_mode scale_filter);
635 
636 struct output_config *new_output_config(const char *name);
637 
638 void merge_output_config(struct output_config *dst, struct output_config *src);
639 
640 bool apply_output_config(struct output_config *oc, struct sway_output *output);
641 
642 bool test_output_config(struct output_config *oc, struct sway_output *output);
643 
644 struct output_config *store_output_config(struct output_config *oc);
645 
646 struct output_config *find_output_config(struct sway_output *output);
647 
648 void apply_output_config_to_outputs(struct output_config *oc);
649 
650 void reset_outputs(void);
651 
652 void free_output_config(struct output_config *oc);
653 
654 bool spawn_swaybg(void);
655 
656 int workspace_output_cmp_workspace(const void *a, const void *b);
657 
658 void free_sway_binding(struct sway_binding *sb);
659 
660 void free_switch_binding(struct sway_switch_binding *binding);
661 
662 void seat_execute_command(struct sway_seat *seat, struct sway_binding *binding);
663 
664 void load_swaybar(struct bar_config *bar);
665 
666 void load_swaybars(void);
667 
668 struct bar_config *default_bar_config(void);
669 
670 void free_bar_config(struct bar_config *bar);
671 
672 void free_bar_binding(struct bar_binding *binding);
673 
674 void free_workspace_config(struct workspace_config *wsc);
675 
676 /**
677  * Updates the value of config->font_height based on the max title height
678  * reported by each container. If recalculate is true, the containers will
679  * recalculate their heights before reporting.
680  *
681  * If the height has changed, all containers will be rearranged to take on the
682  * new size.
683  */
684 void config_update_font_height(bool recalculate);
685 
686 /**
687  * Convert bindsym into bindcode using the first configured layout.
688  * Return false in case the conversion is unsuccessful.
689  */
690 bool translate_binding(struct sway_binding *binding);
691 
692 void translate_keysyms(struct input_config *input_config);
693 
694 void binding_add_translated(struct sway_binding *binding, list_t *bindings);
695 
696 /* Global config singleton. */
697 extern struct sway_config *config;
698 
699 #endif
700