Codebergdnkl

foot

master
ply5 git calls31 ms56 KiB4 cachedprotocol v2
4374 lines133 KiBRaw

Showing 4000 of 4374 lines.

1#include "config.h"
3#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6#include <stdbool.h>
7#include <ctype.h>
8#include <unistd.h>
9#include <errno.h>
10#include <pwd.h>
11#include <fcntl.h>
13#include <sys/types.h>
14#include <sys/stat.h>
16#include <linux/input-event-codes.h>
17#include <xkbcommon/xkbcommon.h>
18#include <fontconfig/fontconfig.h>
20#define LOG_MODULE "config"
21#define LOG_ENABLE_DBG 0
22#include "log.h"
23#include "char32.h"
24#include "debug.h"
25#include "input.h"
26#include "key-binding.h"
27#include "macros.h"
28#include "tokenize.h"
29#include "util.h"
30#include "xmalloc.h"
31#include "xsnprintf.h"
33static const uint32_t default_foreground = 0xffffff;
34static const uint32_t default_background = 0x242424;
36static const size_t min_csd_border_width = 5;
38#define cube6(r, g) \
39 r|g|0x00, r|g|0x5f, r|g|0x87, r|g|0xaf, r|g|0xd7, r|g|0xff
41#define cube36(r) \
42 cube6(r, 0x0000), \
43 cube6(r, 0x5f00), \
44 cube6(r, 0x8700), \
45 cube6(r, 0xaf00), \
46 cube6(r, 0xd700), \
47 cube6(r, 0xff00)
49static const uint32_t default_color_table[256] = {
50 // Regular
51 0x242424,
52 0xf62b5a,
53 0x47b413,
54 0xe3c401,
55 0x24acd4,
56 0xf2affd,
57 0x13c299,
58 0xe6e6e6,
60 // Bright
61 0x616161,
62 0xff4d51,
63 0x35d450,
64 0xe9e836,
65 0x5dc5f8,
66 0xfeabf2,
67 0x24dfc4,
68 0xffffff,
70 // 6x6x6 RGB cube
71 // (color channels = i ? i*40+55 : 0, where i = 0..5)
72 cube36(0x000000),
73 cube36(0x5f0000),
74 cube36(0x870000),
75 cube36(0xaf0000),
76 cube36(0xd70000),
77 cube36(0xff0000),
79 // 24 shades of gray
80 // (color channels = i*10+8, where i = 0..23)
81 0x080808, 0x121212, 0x1c1c1c, 0x262626,
82 0x303030, 0x3a3a3a, 0x444444, 0x4e4e4e,
83 0x585858, 0x626262, 0x6c6c6c, 0x767676,
84 0x808080, 0x8a8a8a, 0x949494, 0x9e9e9e,
85 0xa8a8a8, 0xb2b2b2, 0xbcbcbc, 0xc6c6c6,
86 0xd0d0d0, 0xdadada, 0xe4e4e4, 0xeeeeee
87};
89/* VT330/VT340 Programmer Reference Manual - Table 2-3 VT340 Default Color Map */
90static const uint32_t default_sixel_colors[16] = {
91 0xff000000,
92 0xff3333cc,
93 0xffcc2121,
94 0xff33cc33,
95 0xffcc33cc,
96 0xff33cccc,
97 0xffcccc33,
98 0xff878787,
99 0xff424242,
100 0xff545499,
101 0xff994242,
102 0xff549954,
103 0xff995499,
104 0xff549999,
105 0xff999954,
106 0xffcccccc,
107};
109static const char *const default_url_regex_string =
110 "("
111 "("
112 "(https?://|mailto:|ftp://|file:|ssh:|ssh://|git://|tel:|magnet:|ipfs://|ipns://|gemini://|gopher://|news:)"
113 "|"
114 "www\\."
115 ")"
116 "("
117 /* Safe + reserved + some unsafe characters parenthesis and double quotes omitted (we only allow them when balanced) */
118 "[0-9a-zA-Z:/?#@!$&*+,;=.~_%^'\\-]+"
119 "|"
120 /* Balanced "(...)". Content is same as above, plus all _other_ characters we require to be balanced */
121 "\\([]\\[\"0-9a-zA-Z:/?#@!$&'*+,;=.~_%^\\-]+\\)"
122 "|"
123 /* Balanced "[...]". Content is same as above, plus all _other_ characters we require to be balanced */
124 "\\[[\\(\\)\"0-9a-zA-Z:/?#@!$&'*+,;=.~_%^\\-]+\\]"
125 "|"
126 /* Balanced '"..."'. Content is same as above, plus all _other_ characters we require to be balanced */
127 "\"[]\\[\\(\\)0-9a-zA-Z:/?#@!$&'*+,;=.~_%^\\-]+\""
128 "|"
129 /* Balanced "'...'". Content is same as above, plus all _other_ characters we require to be balanced */
130 "'[]\\[\\(\\)0-9a-zA-Z:/?#@!$&*+,;=.~_%^\\-]+'"
131 ")+"
132 "("
133 /* Same as above, except :?!,;.' are excluded */
134 "[0-9a-zA-Z/#@$&*+=~_%^\\-]"
135 "|"
136 /* Balanced "(...)". Content is same as above, plus all _other_ characters we require to be balanced */
137 "\\([]\\[\"0-9a-zA-Z:/?#@!$&'*+,;=.~_%^\\-]+\\)"
138 "|"
139 /* Balanced "[...]". Content is same as above, plus all _other_ characters we require to be balanced */
140 "\\[[\\(\\)\"0-9a-zA-Z:/?#@!$&'*+,;=.~_%^\\-]+\\]"
141 "|"
142 /* Balanced '"..."'. Content is same as above, plus all _other_ characters we require to be balanced */
143 "\"[]\\[\\(\\)0-9a-zA-Z:/?#@!$&'*+,;=.~_%^\\-]+\""
144 "|"
145 /* Balanced "'...'". Content is same as above, plus all _other_ characters we require to be balanced */
146 "'[]\\[\\(\\)0-9a-zA-Z:/?#@!$&*+,;=.~_%^\\-]+'"
147 ")"
148 ")";
150static const char *const binding_action_map[] = {
151 [BIND_ACTION_NONE] = NULL,
152 [BIND_ACTION_NOOP] = "noop",
153 [BIND_ACTION_SCROLLBACK_UP_PAGE] = "scrollback-up-page",
154 [BIND_ACTION_SCROLLBACK_UP_HALF_PAGE] = "scrollback-up-half-page",
155 [BIND_ACTION_SCROLLBACK_UP_LINE] = "scrollback-up-line",
156 [BIND_ACTION_SCROLLBACK_DOWN_PAGE] = "scrollback-down-page",
157 [BIND_ACTION_SCROLLBACK_DOWN_HALF_PAGE] = "scrollback-down-half-page",
158 [BIND_ACTION_SCROLLBACK_DOWN_LINE] = "scrollback-down-line",
159 [BIND_ACTION_SCROLLBACK_HOME] = "scrollback-home",
160 [BIND_ACTION_SCROLLBACK_END] = "scrollback-end",
161 [BIND_ACTION_CLIPBOARD_COPY] = "clipboard-copy",
162 [BIND_ACTION_CLIPBOARD_PASTE] = "clipboard-paste",
163 [BIND_ACTION_PRIMARY_PASTE] = "primary-paste",
164 [BIND_ACTION_SEARCH_START] = "search-start",
165 [BIND_ACTION_FONT_SIZE_UP] = "font-increase",
166 [BIND_ACTION_FONT_SIZE_DOWN] = "font-decrease",
167 [BIND_ACTION_FONT_SIZE_RESET] = "font-reset",
168 [BIND_ACTION_SPAWN_TERMINAL] = "spawn-terminal",
169 [BIND_ACTION_MINIMIZE] = "minimize",
170 [BIND_ACTION_MAXIMIZE] = "maximize",
171 [BIND_ACTION_FULLSCREEN] = "fullscreen",
172 [BIND_ACTION_PIPE_SCROLLBACK] = "pipe-scrollback",
173 [BIND_ACTION_PIPE_VIEW] = "pipe-visible",
174 [BIND_ACTION_PIPE_SELECTED] = "pipe-selected",
175 [BIND_ACTION_PIPE_COMMAND_OUTPUT] = "pipe-command-output",
176 [BIND_ACTION_SHOW_URLS_COPY] = "show-urls-copy",
177 [BIND_ACTION_SHOW_URLS_LAUNCH] = "show-urls-launch",
178 [BIND_ACTION_SHOW_URLS_PERSISTENT] = "show-urls-persistent",
179 [BIND_ACTION_TEXT_BINDING] = "text-binding",
180 [BIND_ACTION_PROMPT_PREV] = "prompt-prev",
181 [BIND_ACTION_PROMPT_NEXT] = "prompt-next",
182 [BIND_ACTION_UNICODE_INPUT] = "unicode-input",
183 [BIND_ACTION_QUIT] = "quit",
184 [BIND_ACTION_REGEX_LAUNCH] = "regex-launch",
185 [BIND_ACTION_REGEX_COPY] = "regex-copy",
186 [BIND_ACTION_THEME_SWITCH_DARK] = "color-theme-switch-dark",
187 [BIND_ACTION_THEME_SWITCH_LIGHT] = "color-theme-switch-light",
188 [BIND_ACTION_THEME_TOGGLE] = "color-theme-toggle",
190 /* Mouse-specific actions */
191 [BIND_ACTION_SCROLLBACK_UP_MOUSE] = "scrollback-up-mouse",
192 [BIND_ACTION_SCROLLBACK_DOWN_MOUSE] = "scrollback-down-mouse",
193 [BIND_ACTION_SELECT_BEGIN] = "select-begin",
194 [BIND_ACTION_SELECT_BEGIN_BLOCK] = "select-begin-block",
195 [BIND_ACTION_SELECT_EXTEND] = "select-extend",
196 [BIND_ACTION_SELECT_EXTEND_CHAR_WISE] = "select-extend-character-wise",
197 [BIND_ACTION_SELECT_WORD] = "select-word",
198 [BIND_ACTION_SELECT_WORD_WS] = "select-word-whitespace",
199 [BIND_ACTION_SELECT_QUOTE] = "select-quote",
200 [BIND_ACTION_SELECT_ROW] = "select-row",
201};
203static const char *const search_binding_action_map[] = {
204 [BIND_ACTION_SEARCH_NONE] = NULL,
205 [BIND_ACTION_SEARCH_SCROLLBACK_UP_PAGE] = "scrollback-up-page",
206 [BIND_ACTION_SEARCH_SCROLLBACK_UP_HALF_PAGE] = "scrollback-up-half-page",
207 [BIND_ACTION_SEARCH_SCROLLBACK_UP_LINE] = "scrollback-up-line",
208 [BIND_ACTION_SEARCH_SCROLLBACK_DOWN_PAGE] = "scrollback-down-page",
209 [BIND_ACTION_SEARCH_SCROLLBACK_DOWN_HALF_PAGE] = "scrollback-down-half-page",
210 [BIND_ACTION_SEARCH_SCROLLBACK_DOWN_LINE] = "scrollback-down-line",
211 [BIND_ACTION_SEARCH_SCROLLBACK_HOME] = "scrollback-home",
212 [BIND_ACTION_SEARCH_SCROLLBACK_END] = "scrollback-end",
213 [BIND_ACTION_SEARCH_CANCEL] = "cancel",
214 [BIND_ACTION_SEARCH_COMMIT] = "commit",
215 [BIND_ACTION_SEARCH_FIND_PREV] = "find-prev",
216 [BIND_ACTION_SEARCH_FIND_NEXT] = "find-next",
217 [BIND_ACTION_SEARCH_EDIT_LEFT] = "cursor-left",
218 [BIND_ACTION_SEARCH_EDIT_LEFT_WORD] = "cursor-left-word",
219 [BIND_ACTION_SEARCH_EDIT_RIGHT] = "cursor-right",
220 [BIND_ACTION_SEARCH_EDIT_RIGHT_WORD] = "cursor-right-word",
221 [BIND_ACTION_SEARCH_EDIT_HOME] = "cursor-home",
222 [BIND_ACTION_SEARCH_EDIT_END] = "cursor-end",
223 [BIND_ACTION_SEARCH_DELETE_PREV] = "delete-prev",
224 [BIND_ACTION_SEARCH_DELETE_PREV_WORD] = "delete-prev-word",
225 [BIND_ACTION_SEARCH_DELETE_NEXT] = "delete-next",
226 [BIND_ACTION_SEARCH_DELETE_NEXT_WORD] = "delete-next-word",
227 [BIND_ACTION_SEARCH_DELETE_TO_START] = "delete-to-start",
228 [BIND_ACTION_SEARCH_DELETE_TO_END] = "delete-to-end",
229 [BIND_ACTION_SEARCH_EXTEND_CHAR] = "extend-char",
230 [BIND_ACTION_SEARCH_EXTEND_WORD] = "extend-to-word-boundary",
231 [BIND_ACTION_SEARCH_EXTEND_WORD_WS] = "extend-to-next-whitespace",
232 [BIND_ACTION_SEARCH_EXTEND_LINE_DOWN] = "extend-line-down",
233 [BIND_ACTION_SEARCH_EXTEND_BACKWARD_CHAR] = "extend-backward-char",
234 [BIND_ACTION_SEARCH_EXTEND_BACKWARD_WORD] = "extend-backward-to-word-boundary",
235 [BIND_ACTION_SEARCH_EXTEND_BACKWARD_WORD_WS] = "extend-backward-to-next-whitespace",
236 [BIND_ACTION_SEARCH_EXTEND_LINE_UP] = "extend-line-up",
237 [BIND_ACTION_SEARCH_CLIPBOARD_PASTE] = "clipboard-paste",
238 [BIND_ACTION_SEARCH_PRIMARY_PASTE] = "primary-paste",
239 [BIND_ACTION_SEARCH_UNICODE_INPUT] = "unicode-input",
240};
242static const char *const url_binding_action_map[] = {
243 [BIND_ACTION_URL_NONE] = NULL,
244 [BIND_ACTION_URL_CANCEL] = "cancel",
245 [BIND_ACTION_URL_TOGGLE_URL_ON_JUMP_LABEL] = "toggle-url-visible",
246};
248static_assert(ALEN(binding_action_map) == BIND_ACTION_COUNT,
249 "binding action map size mismatch");
250static_assert(ALEN(search_binding_action_map) == BIND_ACTION_SEARCH_COUNT,
251 "search binding action map size mismatch");
252static_assert(ALEN(url_binding_action_map) == BIND_ACTION_URL_COUNT,
253 "URL binding action map size mismatch");
255struct context {
256 struct config *conf;
257 const char *section;
258 const char *section_suffix;
259 const char *key;
260 const char *value;
262 const char *path;
263 unsigned lineno;
265 bool errors_are_fatal;
266};
268static const enum user_notification_kind log_class_to_notify_kind[LOG_CLASS_COUNT] = {
269 [LOG_CLASS_WARNING] = USER_NOTIFICATION_WARNING,
270 [LOG_CLASS_ERROR] = USER_NOTIFICATION_ERROR,
271};
273static void NOINLINE VPRINTF(5)
274log_and_notify_va(struct config *conf, enum log_class log_class,
275 const char *file, int lineno, const char *fmt, va_list va)
277 xassert(log_class < ALEN(log_class_to_notify_kind));
278 enum user_notification_kind kind = log_class_to_notify_kind[log_class];
280 if (kind == 0) {
281 BUG("unsupported log class: %d", (int)log_class);
282 return;
283 }
285 char *formatted_msg = xvasprintf(fmt, va);
286 log_msg(log_class, LOG_MODULE, file, lineno, "%s", formatted_msg);
287 user_notification_add(&conf->notifications, kind, formatted_msg);
290static void NOINLINE PRINTF(5)
291log_and_notify(struct config *conf, enum log_class log_class,
292 const char *file, int lineno, const char *fmt, ...)
294 va_list va;
295 va_start(va, fmt);
296 log_and_notify_va(conf, log_class, file, lineno, fmt, va);
297 va_end(va);
300static void NOINLINE PRINTF(5)
301log_contextual(struct context *ctx, enum log_class log_class,
302 const char *file, int lineno, const char *fmt, ...)
304 va_list va;
305 va_start(va, fmt);
306 char *formatted_msg = xvasprintf(fmt, va);
307 va_end(va);
309 const bool print_dot = ctx->key != NULL;
310 const bool print_colon = ctx->value != NULL;
311 const bool print_section_suffix = ctx->section_suffix != NULL;
313 if (!print_dot)
314 ctx->key = "";
316 if (!print_colon)
317 ctx->value = "";
319 if (!print_section_suffix)
320 ctx->section_suffix = "";
322 log_and_notify(
323 ctx->conf, log_class, file, lineno, "%s:%d: [%s%s%s]%s%s%s%s: %s",
324 ctx->path, ctx->lineno, ctx->section,
325 print_section_suffix ? ":" : "", ctx->section_suffix,
326 print_dot ? "." : "", ctx->key, print_colon ? ": " : "",
327 ctx->value, formatted_msg);
328 free(formatted_msg);
332static void NOINLINE VPRINTF(4)
333log_and_notify_errno_va(struct config *conf, const char *file, int lineno,
334 const char *fmt, va_list va)
336 int errno_copy = errno;
337 char *formatted_msg = xvasprintf(fmt, va);
338 log_and_notify(
339 conf, LOG_CLASS_ERROR, file, lineno,
340 "%s: %s", formatted_msg, strerror(errno_copy));
341 free(formatted_msg);
344static void NOINLINE PRINTF(4)
345log_and_notify_errno(struct config *conf, const char *file, int lineno,
346 const char *fmt, ...)
348 va_list va;
349 va_start(va, fmt);
350 log_and_notify_errno_va(conf, file, lineno, fmt, va);
351 va_end(va);
354static void NOINLINE PRINTF(4)
355log_contextual_errno(struct context *ctx, const char *file, int lineno,
356 const char *fmt, ...)
358 va_list va;
359 va_start(va, fmt);
360 char *formatted_msg = xvasprintf(fmt, va);
361 va_end(va);
363 bool print_dot = ctx->key != NULL;
364 bool print_colon = ctx->value != NULL;
366 if (!print_dot)
367 ctx->key = "";
369 if (!print_colon)
370 ctx->value = "";
372 log_and_notify_errno(
373 ctx->conf, file, lineno, "%s:%d: [%s]%s%s%s%s: %s",
374 ctx->path, ctx->lineno, ctx->section, print_dot ? "." : "",
375 ctx->key, print_colon ? ": " : "", ctx->value, formatted_msg);
377 free(formatted_msg);
380#define LOG_CONTEXTUAL_ERR(...) \
381 log_contextual(ctx, LOG_CLASS_ERROR, __FILE__, __LINE__, __VA_ARGS__)
383#define LOG_CONTEXTUAL_WARN(...) \
384 log_contextual(ctx, LOG_CLASS_WARNING, __FILE__, __LINE__, __VA_ARGS__)
386#define LOG_CONTEXTUAL_ERRNO(...) \
387 log_contextual_errno(ctx, __FILE__, __LINE__, __VA_ARGS__)
389#define LOG_AND_NOTIFY_ERR(...) \
390 log_and_notify(conf, LOG_CLASS_ERROR, __FILE__, __LINE__, __VA_ARGS__)
392#define LOG_AND_NOTIFY_WARN(...) \
393 log_and_notify(conf, LOG_CLASS_WARNING, __FILE__, __LINE__, __VA_ARGS__)
395#define LOG_AND_NOTIFY_ERRNO(...) \
396 log_and_notify_errno(conf, __FILE__, __LINE__, __VA_ARGS__)
398static char *
399get_shell(void)
401 const char *shell = getenv("SHELL");
403 if (shell == NULL) {
404 struct passwd *passwd = getpwuid(getuid());
405 if (passwd == NULL) {
406 LOG_ERRNO("failed to lookup user: falling back to 'sh'");
407 shell = "sh";
408 } else
409 shell = passwd->pw_shell;
410 }
412 LOG_DBG("user's shell: %s", shell);
413 return xstrdup(shell);
416struct config_file {
417 char *path; /* Full, absolute, path */
418 int fd; /* FD of file, O_RDONLY */
419};
421static struct config_file
422open_config(void)
424 char *path = NULL;
425 struct config_file ret = {.path = NULL, .fd = -1};
427 const char *xdg_config_home = getenv("XDG_CONFIG_HOME");
428 const char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
429 const char *home_dir = getenv("HOME");
430 char *xdg_config_dirs_copy = NULL;
432 /* First, check XDG_CONFIG_HOME (or .config, if unset) */
433 if (xdg_config_home != NULL && xdg_config_home[0] != '\0')
434 path = xstrjoin(xdg_config_home, "/foot/foot.ini");
435 else if (home_dir != NULL)
436 path = xstrjoin(home_dir, "/.config/foot/foot.ini");
438 if (path != NULL) {
439 LOG_DBG("checking for %s", path);
440 int fd = open(path, O_RDONLY | O_CLOEXEC);
442 if (fd >= 0) {
443 ret = (struct config_file) {.path = path, .fd = fd};
444 path = NULL;
445 goto done;
446 }
447 }
449 xdg_config_dirs_copy = xdg_config_dirs != NULL && xdg_config_dirs[0] != '\0'
450 ? strdup(xdg_config_dirs)
451 : strdup("/etc/xdg");
453 if (xdg_config_dirs_copy == NULL || xdg_config_dirs_copy[0] == '\0')
454 goto done;
456 for (const char *conf_dir = strtok(xdg_config_dirs_copy, ":");
457 conf_dir != NULL;
458 conf_dir = strtok(NULL, ":"))
459 {
460 free(path);
461 path = xstrjoin(conf_dir, "/foot/foot.ini");
463 LOG_DBG("checking for %s", path);
464 int fd = open(path, O_RDONLY | O_CLOEXEC);
465 if (fd >= 0) {
466 ret = (struct config_file){.path = path, .fd = fd};
467 path = NULL;
468 goto done;
469 }
470 }
472done:
473 free(xdg_config_dirs_copy);
474 free(path);
475 return ret;
478static bool
479str_has_prefix(const char *str, const char *prefix)
481 return strncmp(str, prefix, strlen(prefix)) == 0;
484static bool NOINLINE
485value_to_bool(struct context *ctx, bool *res)
487 static const char *const yes[] = {"on", "true", "yes", "1"};
488 static const char *const no[] = {"off", "false", "no", "0"};
490 for (size_t i = 0; i < ALEN(yes); i++) {
491 if (strcasecmp(ctx->value, yes[i]) == 0) {
492 *res = true;
493 return true;
494 }
495 }
497 for (size_t i = 0; i < ALEN(no); i++) {
498 if (strcasecmp(ctx->value, no[i]) == 0) {
499 *res = false;
500 return true;
501 }
502 }
504 LOG_CONTEXTUAL_ERR("invalid boolean value");
505 return false;
509static bool NOINLINE
510str_to_ulong(const char *s, int base, unsigned long *res)
512 if (s == NULL)
513 return false;
515 errno = 0;
516 char *end = NULL;
518 unsigned long v = strtoul(s, &end, base);
519 if (!(errno == 0 && *end == '\0'))
520 return false;
522 *res = v;
523 return true;
526static bool NOINLINE
527str_to_uint32(const char *s, int base, uint32_t *res)
529 unsigned long v;
530 bool ret = str_to_ulong(s, base, &v);
531 if (v > UINT32_MAX)
532 return false;
533 *res = v;
534 return ret;
537static bool NOINLINE
538str_to_uint16(const char *s, int base, uint16_t *res)
540 unsigned long v;
541 bool ret = str_to_ulong(s, base, &v);
542 if (v > UINT16_MAX)
543 return false;
544 *res = v;
545 return ret;
548static bool NOINLINE
549value_to_uint16(struct context *ctx, int base, uint16_t *res)
551 if (!str_to_uint16(ctx->value, base, res)) {
552 LOG_CONTEXTUAL_ERR(
553 "invalid integer value, or outside range 0-%u", UINT16_MAX);
554 return false;
555 }
556 return true;
559static bool NOINLINE
560value_to_uint32(struct context *ctx, int base, uint32_t *res)
562 if (!str_to_uint32(ctx->value, base, res)){
563 LOG_CONTEXTUAL_ERR(
564 "invalid integer value, or outside range 0-%u", UINT32_MAX);
565 return false;
566 }
567 return true;
570static bool NOINLINE
571value_to_dimensions(struct context *ctx, uint32_t *x, uint32_t *y)
573 if (sscanf(ctx->value, "%ux%u", x, y) != 2) {
574 LOG_CONTEXTUAL_ERR("invalid dimensions (must be in the form AxB)");
575 return false;
576 }
578 return true;
581static bool NOINLINE
582value_to_float(struct context *ctx, float *res)
584 const char *s = ctx->value;
586 if (s == NULL)
587 return false;
589 errno = 0;
590 char *end = NULL;
592 float v = strtof(s, &end);
593 if (!(errno == 0 && *end == '\0')) {
594 LOG_CONTEXTUAL_ERR("invalid decimal value");
595 return false;
596 }
598 *res = v;
599 return true;
602static bool NOINLINE
603value_to_str(struct context *ctx, char **res)
605 char *copy = xstrdup(ctx->value);
606 char *end = copy + strlen(copy) - 1;
608 /* Un-quote
609 *
610 * Note: this is very simple; we only support the *entire* value
611 * being quoted. That is, no mid-value quotes. Both double and
612 * single quotes are supported.
613 *
614 * - key="value" OK
615 * - key=abc "quote" def NOT OK
616 * - key='value' OK
617 *
618 * Finally, we support escaping the quote character, and the
619 * escape character itself:
620 *
621 * - key="value \"quotes\""
622 * - key="backslash: \\"
623 *
624 * ONLY the "current" quote character can be escaped:
625 *
626 * key="value \'" NOt OK (both backslash and single quote is kept)
627 */
629 if ((copy[0] == '"' && *end == '"') ||
630 (copy[0] == '\'' && *end == '\''))
631 {
632 const char quote = copy[0];
633 *end = '\0';
635 memmove(copy, copy + 1, end - copy);
637 /* Un-escape */
638 for (char *p = copy; *p != '\0'; p++) {
639 if (p[0] == '\\' && (p[1] == '\\' || p[1] == quote)) {
640 memmove(p, p + 1, end - p);
641 }
642 }
643 }
645 free(*res);
646 *res = copy;
647 return true;
650static bool NOINLINE
651value_to_wchars(struct context *ctx, char32_t **res)
653 char32_t *s = ambstoc32(ctx->value);
654 if (s == NULL) {
655 LOG_CONTEXTUAL_ERR("not a valid string value");
656 return false;
657 }
659 free(*res);
660 *res = s;
661 return true;
664static bool NOINLINE
665value_to_enum(struct context *ctx, const char **value_map, int *res)
667 size_t str_len = 0;
668 size_t count = 0;
670 for (; value_map[count] != NULL; count++) {
671 if (strcasecmp(value_map[count], ctx->value) == 0) {
672 *res = count;
673 return true;
674 }
675 str_len += strlen(value_map[count]);
676 }
678 const size_t size = str_len + count * 4 + 1;
679 char valid_values[512];
680 size_t idx = 0;
681 xassert(size < sizeof(valid_values));
683 for (size_t i = 0; i < count; i++)
684 idx += xsnprintf(&valid_values[idx], size - idx, "'%s', ", value_map[i]);
686 if (count > 0)
687 valid_values[idx - 2] = '\0';
689 LOG_CONTEXTUAL_ERR("not one of %s", valid_values);
690 return false;
693static bool NOINLINE
694value_to_color(struct context *ctx, uint32_t *result, bool allow_alpha)
696 uint32_t color;
697 const size_t len = strlen(ctx->value);
698 const size_t component_count = len / 2;
700 if (!(len == 6 || (allow_alpha && len == 8)) ||
701 !str_to_uint32(ctx->value, 16, &color))
702 {
703 if (allow_alpha) {
704 LOG_CONTEXTUAL_ERR("color must be in either RGB or ARGB format");
705 } else {
706 LOG_CONTEXTUAL_ERR("color must be in RGB format");
707 }
709 return false;
710 }
712 if (allow_alpha && component_count == 3) {
713 /* If user left out the alpha component, assume non-transparency */
714 color |= 0xff000000;
715 }
717 *result = color;
718 return true;
721static bool NOINLINE
722value_to_two_colors(struct context *ctx,
723 uint32_t *first, uint32_t *second, bool allow_alpha)
725 bool ret = false;
726 const char *original_value = ctx->value;
728 /* TODO: do this without strdup() */
729 char *value_copy = xstrdup(ctx->value);
730 const char *first_as_str = strtok(value_copy, " ");
731 const char *second_as_str = strtok(NULL, " ");
733 if (first_as_str == NULL || second_as_str == NULL) {
734 LOG_CONTEXTUAL_ERR("invalid double color value");
735 goto out;
736 }
738 uint32_t a, b;
740 ctx->value = first_as_str;
741 if (!value_to_color(ctx, &a, allow_alpha))
742 goto out;
744 ctx->value = second_as_str;
745 if (!value_to_color(ctx, &b, allow_alpha))
746 goto out;
748 *first = a;
749 *second = b;
750 ret = true;
752out:
753 free(value_copy);
754 ctx->value = original_value;
755 return ret;
758static bool NOINLINE
759value_to_pt_or_px(struct context *ctx, struct pt_or_px *res)
761 const char *s = ctx->value;
763 size_t len = s != NULL ? strlen(s) : 0;
764 if (len >= 2 && s[len - 2] == 'p' && s[len - 1] == 'x') {
765 errno = 0;
766 char *end = NULL;
768 long value = strtol(s, &end, 10);
769 if (!(len > 2 && errno == 0 && end == s + len - 2)) {
770 LOG_CONTEXTUAL_ERR("invalid px value (must be in the form 12px)");
771 return false;
772 }
773 res->pt = 0;
774 res->px = value;
775 } else {
776 float value;
777 if (!value_to_float(ctx, &value))
778 return false;
779 res->pt = value;
780 res->px = 0;
781 }
783 return true;
786static struct config_font_list NOINLINE
787value_to_fonts(struct context *ctx)
789 size_t count = 0;
790 size_t size = 0;
791 struct config_font *fonts = NULL;
793 char *copy = xstrdup(ctx->value);
794 for (const char *font = strtok(copy, ",");
795 font != NULL;
796 font = strtok(NULL, ","))
797 {
798 /* Trim spaces, strictly speaking not necessary, but looks nice :) */
799 while (isspace(font[0]))
800 font++;
802 if (font[0] == '\0')
803 continue;
805 struct config_font font_data;
806 if (!config_font_parse(font, &font_data)) {
807 ctx->value = font;
808 LOG_CONTEXTUAL_ERR("invalid font specification");
809 goto err;
810 }
812 if (count + 1 > size) {
813 size += 4;
814 fonts = xrealloc(fonts, size * sizeof(fonts[0]));
815 }
817 xassert(count + 1 <= size);
818 fonts[count++] = font_data;
819 }
821 free(copy);
822 return (struct config_font_list){.arr = fonts, .count = count};
824err:
825 free(copy);
826 free(fonts);
827 return (struct config_font_list){.arr = NULL, .count = 0};
830static void NOINLINE
831free_argv(struct argv *argv)
833 if (argv->args == NULL)
834 return;
835 for (char **a = argv->args; *a != NULL; a++)
836 free(*a);
837 free(argv->args);
838 argv->args = NULL;
841static void NOINLINE
842clone_argv(struct argv *dst, const struct argv *src)
844 if (src->args == NULL) {
845 dst->args = NULL;
846 return;
847 }
849 size_t count = 0;
850 for (char **args = src->args; *args != NULL; args++)
851 count++;
853 dst->args = xmalloc((count + 1) * sizeof(dst->args[0]));
854 for (char **args_src = src->args, **args_dst = dst->args;
855 *args_src != NULL; args_src++,
856 args_dst++)
857 {
858 *args_dst = xstrdup(*args_src);
859 }
860 dst->args[count] = NULL;
863static void
864spawn_template_free(struct config_spawn_template *template)
866 free_argv(&template->argv);
869static void
870spawn_template_clone(struct config_spawn_template *dst,
871 const struct config_spawn_template *src)
873 clone_argv(&dst->argv, &src->argv);
876static bool NOINLINE
877value_to_spawn_template(struct context *ctx,
878 struct config_spawn_template *template)
880 spawn_template_free(template);
882 char **argv = NULL;
884 if (ctx->value[0] == '"' && ctx->value[1] == '"' && ctx->value[2] == '\0') {
885 template->argv.args = NULL;
886 return true;
887 }
889 if (!tokenize_cmdline(ctx->value, &argv)) {
890 LOG_CONTEXTUAL_ERR("syntax error in command line");
891 return false;
892 }
894 template->argv.args = argv;
895 return true;
898static bool parse_config_file(
899 FILE *f, struct config *conf, const char *path, bool errors_are_fatal);
901static bool
902parse_section_main(struct context *ctx)
904 struct config *conf = ctx->conf;
905 const char *key = ctx->key;
906 const char *value = ctx->value;
907 bool errors_are_fatal = ctx->errors_are_fatal;
909 if (streq(key, "include")) {
910 char *_include_path = NULL;
911 const char *include_path = NULL;
913 if (value[0] == '~' && value[1] == '/') {
914 const char *home_dir = getenv("HOME");
916 if (home_dir == NULL) {
917 LOG_CONTEXTUAL_ERRNO("failed to expand '~'");
918 return false;
919 }
921 _include_path = xstrjoin3(home_dir, "/", value + 2);
922 include_path = _include_path;
923 } else
924 include_path = value;
926 if (include_path[0] != '/') {
927 LOG_CONTEXTUAL_ERR("not an absolute path");
928 free(_include_path);
929 return false;
930 }
932 FILE *include = fopen(include_path, "r");
934 if (include == NULL) {
935 LOG_CONTEXTUAL_ERRNO("failed to open");
936 free(_include_path);
937 return false;
938 }
940 bool ret = parse_config_file(
941 include, conf, include_path, errors_are_fatal);
942 fclose(include);
944 LOG_INFO("imported sub-configuration from %s", include_path);
945 free(_include_path);
946 return ret;
947 }
949 else if (streq(key, "term"))
950 return value_to_str(ctx, &conf->term);
952 else if (streq(key, "shell"))
953 return value_to_str(ctx, &conf->shell);
955 else if (streq(key, "login-shell"))
956 return value_to_bool(ctx, &conf->login_shell);
958 else if (streq(key, "title"))
959 return value_to_str(ctx, &conf->title);
961 else if (streq(key, "locked-title"))
962 return value_to_bool(ctx, &conf->locked_title);
964 else if (streq(key, "app-id"))
965 return value_to_str(ctx, &conf->app_id);
967 else if (streq(key, "toplevel-tag"))
968 return value_to_str(ctx, &conf->toplevel_tag);
970 else if (streq(key, "initial-window-size-pixels")) {
971 if (!value_to_dimensions(ctx, &conf->size.width, &conf->size.height))
972 return false;
974 conf->size.type = CONF_SIZE_PX;
975 return true;
976 }
978 else if (streq(key, "initial-window-size-chars")) {
979 if (!value_to_dimensions(ctx, &conf->size.width, &conf->size.height))
980 return false;
982 conf->size.type = CONF_SIZE_CELLS;
983 return true;
984 }
986 else if (streq(key, "pad")) {
987 unsigned x, y, left, top, right, bottom;
988 char mode[64] = {0};
989 int ret = sscanf(value, "%ux%ux%ux%u %63s", &left, &top, &right, &bottom, mode);
990 enum center_when center = CENTER_NEVER;
992 if (ret == 5) {
993 if (strcasecmp(mode, "center") == 0)
994 center = CENTER_ALWAYS;
995 else if (strcasecmp(mode, "center-when-fullscreen") == 0)
996 center = CENTER_FULLSCREEN;
997 else if (strcasecmp(mode, "center-when-maximized-and-fullscreen") == 0)
998 center = CENTER_MAXIMIZED_AND_FULLSCREEN;
999 else
1000 center = CENTER_INVALID;
1001 } else if (ret < 4) {
1002 ret = sscanf(value, "%ux%u %63s", &x, &y, mode);
1003 if (ret >= 2) {
1004 left = right = x;
1005 top = bottom = y;
1006 if (ret == 3) {
1007 if (strcasecmp(mode, "center") == 0)
1008 center = CENTER_ALWAYS;
1009 else if (strcasecmp(mode, "center-when-fullscreen") == 0)
1010 center = CENTER_FULLSCREEN;
1011 else if (strcasecmp(mode, "center-when-maximized-and-fullscreen") == 0)
1012 center = CENTER_MAXIMIZED_AND_FULLSCREEN;
1013 else
1014 center = CENTER_INVALID;
1019 if ((ret < 2 || ret > 5) || center == CENTER_INVALID) {
1020 LOG_CONTEXTUAL_ERR(
1021 "invalid padding (must be in the form RIGHTxTOPxLEFTxBOTTOM or XxY "
1022 "[center|"
1023 "center-when-fullscreen|"
1024 "center-when-maximized-and-fullscreen])");
1025 return false;
1028 conf->pad_left = left;
1029 conf->pad_top = top;
1030 conf->pad_right = right;
1031 conf->pad_bottom = bottom;
1032 conf->center_when = (ret == 4 || ret == 2) ? CENTER_NEVER : center;
1033 return true;
1036 else if (streq(key, "resize-delay-ms"))
1037 return value_to_uint16(ctx, 10, &conf->resize_delay_ms);
1039 else if (streq(key, "resize-by-cells"))
1040 return value_to_bool(ctx, &conf->resize_by_cells);
1042 else if (streq(key, "resize-keep-grid"))
1043 return value_to_bool(ctx, &conf->resize_keep_grid);
1045 else if (streq(key, "bold-text-in-bright")) {
1046 if (streq(value, "palette-based")) {
1047 conf->bold_in_bright.enabled = true;
1048 conf->bold_in_bright.palette_based = true;
1049 } else {
1050 if (!value_to_bool(ctx, &conf->bold_in_bright.enabled))
1051 return false;
1052 conf->bold_in_bright.palette_based = false;
1054 return true;
1057 else if (streq(key, "initial-window-mode")) {
1058 _Static_assert(sizeof(conf->startup_mode) == sizeof(int),
1059 "enum is not 32-bit");
1061 return value_to_enum(
1062 ctx,
1063 (const char *[]){"windowed", "maximized", "fullscreen", NULL},
1064 (int *)&conf->startup_mode);
1067 else if (streq(key, "font") ||
1068 streq(key, "font-bold") ||
1069 streq(key, "font-italic") ||
1070 streq(key, "font-bold-italic"))
1073 size_t idx =
1074 streq(key, "font") ? 0 :
1075 streq(key, "font-bold") ? 1 :
1076 streq(key, "font-italic") ? 2 : 3;
1078 struct config_font_list new_list = value_to_fonts(ctx);
1079 if (new_list.arr == NULL)
1080 return false;
1082 config_font_list_destroy(&conf->fonts[idx]);
1083 conf->fonts[idx] = new_list;
1084 return true;
1087 else if (streq(key, "font-size-adjustment")) {
1088 const size_t len = strlen(ctx->value);
1089 if (len >= 1 && ctx->value[len - 1] == '%') {
1090 errno = 0;
1091 char *end = NULL;
1093 float percent = strtof(ctx->value, &end);
1094 if (!(len > 1 && errno == 0 && end == ctx->value + len - 1)) {
1095 LOG_CONTEXTUAL_ERR(
1096 "invalid percent value (must be in the form 10.5%%)");
1097 return false;
1100 conf->font_size_adjustment.percent = percent / 100.;
1101 conf->font_size_adjustment.pt_or_px.pt = 0;
1102 conf->font_size_adjustment.pt_or_px.px = 0;
1103 return true;
1104 } else {
1105 bool ret = value_to_pt_or_px(ctx, &conf->font_size_adjustment.pt_or_px);
1106 if (ret)
1107 conf->font_size_adjustment.percent = 0.;
1108 return ret;
1112 else if (streq(key, "line-height"))
1113 return value_to_pt_or_px(ctx, &conf->line_height);
1115 else if (streq(key, "letter-spacing"))
1116 return value_to_pt_or_px(ctx, &conf->letter_spacing);
1118 else if (streq(key, "horizontal-letter-offset"))
1119 return value_to_pt_or_px(ctx, &conf->horizontal_letter_offset);
1121 else if (streq(key, "vertical-letter-offset"))
1122 return value_to_pt_or_px(ctx, &conf->vertical_letter_offset);
1124 else if (streq(key, "underline-offset")) {
1125 if (!value_to_pt_or_px(ctx, &conf->underline_offset))
1126 return false;
1127 conf->use_custom_underline_offset = true;
1128 return true;
1131 else if (streq(key, "underline-thickness"))
1132 return value_to_pt_or_px(ctx, &conf->underline_thickness);
1134 else if (streq(key, "strikeout-thickness"))
1135 return value_to_pt_or_px(ctx, &conf->strikeout_thickness);
1137 else if (streq(key, "dpi-aware"))
1138 return value_to_bool(ctx, &conf->dpi_aware);
1140 else if (streq(key, "workers"))
1141 return value_to_uint16(ctx, 10, &conf->render_worker_count);
1143 else if (streq(key, "word-delimiters"))
1144 return value_to_wchars(ctx, &conf->word_delimiters);
1146 else if (streq(key, "selection-target")) {
1147 _Static_assert(sizeof(conf->selection_target) == sizeof(int),
1148 "enum is not 32-bit");
1150 return value_to_enum(
1151 ctx,
1152 (const char *[]){"none", "primary", "clipboard", "both", NULL},
1153 (int *)&conf->selection_target);
1156 else if (streq(key, "box-drawings-uses-font-glyphs"))
1157 return value_to_bool(ctx, &conf->box_drawings_uses_font_glyphs);
1159 else if (streq(key, "utmp-helper")) {
1160 if (!value_to_str(ctx, &conf->utmp_helper_path))
1161 return false;
1163 if (streq(conf->utmp_helper_path, "none")) {
1164 free(conf->utmp_helper_path);
1165 conf->utmp_helper_path = NULL;
1168 return true;
1171 else if (streq(key, "gamma-correct-blending"))
1172 return value_to_bool(ctx, &conf->gamma_correct);
1174 else if (streq(key, "initial-color-theme")) {
1175 _Static_assert(
1176 sizeof(conf->initial_color_theme) == sizeof(int),
1177 "enum is not 32-bit");
1179 return value_to_enum(ctx, (const char*[]){
1180 "dark", "light", NULL}, (int *)&conf->initial_color_theme);
1183 else if (streq(key, "uppercase-regex-insert"))
1184 return value_to_bool(ctx, &conf->uppercase_regex_insert);
1186 else {
1187 LOG_CONTEXTUAL_ERR("not a valid option: %s", key);
1188 return false;
1192static bool
1193parse_section_security(struct context *ctx)
1195 struct config *conf = ctx->conf;
1196 const char *key = ctx->key;
1198 if (streq(key, "osc52")) {
1199 _Static_assert(sizeof(conf->security.osc52) == sizeof(int),
1200 "enum is not 32-bit");
1201 return value_to_enum(
1202 ctx,
1203 (const char *[]){"disabled", "copy-enabled", "paste-enabled", "enabled", NULL},
1204 (int *)&conf->security.osc52);
1205 } else {
1206 LOG_CONTEXTUAL_ERR("not a valid option: %s", key);
1207 return false;
1211static bool
1212parse_section_bell(struct context *ctx)
1214 struct config *conf = ctx->conf;
1215 const char *key = ctx->key;
1217 if (streq(key, "urgent"))
1218 return value_to_bool(ctx, &conf->bell.urgent);
1219 else if (streq(key, "notify"))
1220 return value_to_bool(ctx, &conf->bell.notify);
1221 else if (streq(key, "system"))
1222 return value_to_bool(ctx, &conf->bell.system_bell);
1223 else if (streq(key, "visual"))
1224 return value_to_bool(ctx, &conf->bell.flash);
1225 else if (streq(key, "command"))
1226 return value_to_spawn_template(ctx, &conf->bell.command);
1227 else if (streq(key, "command-focused"))
1228 return value_to_bool(ctx, &conf->bell.command_focused);
1229 else {
1230 LOG_CONTEXTUAL_ERR("not a valid option: %s", key);
1231 return false;
1235static bool
1236parse_section_desktop_notifications(struct context *ctx)
1238 struct config *conf = ctx->conf;
1239 const char *key = ctx->key;
1241 if (streq(key, "command"))
1242 return value_to_spawn_template(
1243 ctx, &conf->desktop_notifications.command);
1244 else if (streq(key, "command-action-argument"))
1245 return value_to_spawn_template(
1246 ctx, &conf->desktop_notifications.command_action_arg);
1247 else if (streq(key, "close"))
1248 return value_to_spawn_template(
1249 ctx, &conf->desktop_notifications.close);
1250 else if (streq(key, "inhibit-when-focused"))
1251 return value_to_bool(
1252 ctx, &conf->desktop_notifications.inhibit_when_focused);
1253 else {
1254 LOG_CONTEXTUAL_ERR("not a valid option: %s", key);
1255 return false;
1259static bool
1260parse_section_scrollback(struct context *ctx)
1262 struct config *conf = ctx->conf;
1263 const char *key = ctx->key;
1264 const char *value = ctx->value;
1266 if (streq(key, "lines"))
1267 return value_to_uint32(ctx, 10, &conf->scrollback.lines);
1269 else if (streq(key, "indicator-position")) {
1270 _Static_assert(
1271 sizeof(conf->scrollback.indicator.position) == sizeof(int),
1272 "enum is not 32-bit");
1274 return value_to_enum(
1275 ctx,
1276 (const char *[]){"none", "fixed", "relative", NULL},
1277 (int *)&conf->scrollback.indicator.position);
1280 else if (streq(key, "indicator-format")) {
1281 if (streq(value, "percentage")) {
1282 conf->scrollback.indicator.format
1283 = SCROLLBACK_INDICATOR_FORMAT_PERCENTAGE;
1284 return true;
1285 } else if (streq(value, "line")) {
1286 conf->scrollback.indicator.format
1287 = SCROLLBACK_INDICATOR_FORMAT_LINENO;
1288 return true;
1289 } else
1290 return value_to_wchars(ctx, &conf->scrollback.indicator.text);
1293 else if (streq(key, "multiplier"))
1294 return value_to_float(ctx, &conf->scrollback.multiplier);
1296 else {
1297 LOG_CONTEXTUAL_ERR("not a valid option: %s", key);
1298 return false;
1302static bool
1303parse_section_url(struct context *ctx)
1305 struct config *conf = ctx->conf;
1306 const char *key = ctx->key;
1308 if (streq(key, "launch"))
1309 return value_to_spawn_template(ctx, &conf->url.launch);
1311 else if (streq(key, "label-letters"))
1312 return value_to_wchars(ctx, &conf->url.label_letters);
1314 else if (streq(key, "style")) {
1315 _Static_assert(sizeof(conf->url.style) == sizeof(int),
1316 "enum is not 32-bit");
1317 return value_to_enum(
1318 ctx, (const char *[]){"none", "single", "double", "curly", "dotted", "dashed", NULL},
1319 (int *)&conf->url.style);
1322 else if (streq(key, "osc8-underline")) {
1323 _Static_assert(sizeof(conf->url.osc8_underline) == sizeof(int),
1324 "enum is not 32-bit");
1326 return value_to_enum(
1327 ctx,
1328 (const char *[]){"url-mode", "always", NULL},
1329 (int *)&conf->url.osc8_underline);
1332 else if (streq(key, "regex")) {
1333 const char *regex = ctx->value;
1334 regex_t preg;
1336 int r = regcomp(&preg, regex, REG_EXTENDED);
1338 if (r != 0) {
1339 char err_buf[128];
1340 regerror(r, &preg, err_buf, sizeof(err_buf));
1341 LOG_CONTEXTUAL_ERR("invalid regex: %s", err_buf);
1342 return false;
1345 if (preg.re_nsub == 0) {
1346 LOG_CONTEXTUAL_ERR("invalid regex: no marked subexpression(s)");
1347 regfree(&preg);
1348 return false;
1351 regfree(&conf->url.preg);
1352 free(conf->url.regex);
1354 conf->url.regex = xstrdup(regex);
1355 conf->url.preg = preg;
1356 return true;
1359 else {
1360 LOG_CONTEXTUAL_ERR("not a valid option: %s", key);
1361 return false;
1365static bool
1366parse_section_regex(struct context *ctx)
1368 struct config *conf = ctx->conf;
1369 const char *key = ctx->key;
1371 const char *regex_name =
1372 ctx->section_suffix != NULL ? ctx->section_suffix : "";
1374 struct custom_regex *regex = NULL;
1375 tll_foreach(conf->custom_regexes, it) {
1376 if (streq(it->item.name, regex_name)) {
1377 regex = &it->item;
1378 break;
1382 if (streq(key, "regex")) {
1383 const char *regex_string = ctx->value;
1384 regex_t preg;
1386 int r = regcomp(&preg, regex_string, REG_EXTENDED);
1388 if (r != 0) {
1389 char err_buf[128];
1390 regerror(r, &preg, err_buf, sizeof(err_buf));
1391 LOG_CONTEXTUAL_ERR("invalid regex: %s", err_buf);
1392 return false;
1395 if (preg.re_nsub == 0) {
1396 LOG_CONTEXTUAL_ERR("invalid regex: no marked subexpression(s)");
1397 regfree(&preg);
1398 return false;
1401 if (regex == NULL) {
1402 tll_push_back(conf->custom_regexes,
1403 ((struct custom_regex){.name = xstrdup(regex_name)}));
1404 regex = &tll_back(conf->custom_regexes);
1407 regfree(&regex->preg);
1408 free(regex->regex);
1410 regex->regex = xstrdup(regex_string);
1411 regex->preg = preg;
1412 return true;
1415 else if (streq(key, "launch")) {
1416 struct config_spawn_template launch = {NULL};
1417 if (!value_to_spawn_template(ctx, &launch))
1418 return false;
1420 if (regex == NULL) {
1421 tll_push_back(conf->custom_regexes,
1422 ((struct custom_regex){.name = xstrdup(regex_name)}));
1423 regex = &tll_back(conf->custom_regexes);
1426 spawn_template_free(&regex->launch);
1427 regex->launch = launch;
1428 return true;
1431 else {
1432 LOG_CONTEXTUAL_ERR("not a valid option: %s", key);
1433 return false;
1437static bool NOINLINE
1438parse_color_theme(struct context *ctx, struct color_theme *theme)
1440 const char *key = ctx->key;
1442 size_t key_len = strlen(key);
1443 uint8_t last_digit = (unsigned char)key[key_len - 1] - '0';
1444 uint32_t *color = NULL;
1446 if (isdigit(key[0])) {
1447 unsigned long index;
1448 if (!str_to_ulong(key, 0, &index) || index >= ALEN(theme->table)) {
1449 LOG_CONTEXTUAL_ERR(
1450 "invalid color palette index: %s (not in range 0-%zu)",
1451 key, ALEN(theme->table));
1452 return false;
1454 color = &theme->table[index];
1457 else if (key_len == 8 && str_has_prefix(key, "regular") && last_digit < 8)
1458 color = &theme->table[last_digit];
1460 else if (key_len == 7 && str_has_prefix(key, "bright") && last_digit < 8)
1461 color = &theme->table[8 + last_digit];
1463 else if (key_len == 4 && str_has_prefix(key, "dim") && last_digit < 8) {
1464 if (!value_to_color(ctx, &theme->dim[last_digit], false))
1465 return false;
1467 theme->use_custom.dim |= 1 << last_digit;
1468 return true;
1471 else if (str_has_prefix(key, "sixel") &&
1472 ((key_len == 6 && last_digit < 10) ||
1473 (key_len == 7 && key[5] == '1' && last_digit < 6)))
1475 size_t idx = key_len == 6 ? last_digit : 10 + last_digit;
1476 return value_to_color(ctx, &theme->sixel[idx], false);
1479 else if (streq(key, "flash")) color = &theme->flash;
1480 else if (streq(key, "foreground")) color = &theme->fg;
1481 else if (streq(key, "background")) color = &theme->bg;
1482 else if (streq(key, "selection-foreground")) color = &theme->selection_fg;
1483 else if (streq(key, "selection-background")) color = &theme->selection_bg;
1485 else if (streq(key, "jump-labels")) {
1486 if (!value_to_two_colors(
1487 ctx,
1488 &theme->jump_label.fg,
1489 &theme->jump_label.bg,
1490 false))
1492 return false;
1495 theme->use_custom.jump_label = true;
1496 return true;
1499 else if (streq(key, "scrollback-indicator")) {
1500 if (!value_to_two_colors(
1501 ctx,
1502 &theme->scrollback_indicator.fg,
1503 &theme->scrollback_indicator.bg,
1504 false))
1506 return false;
1509 theme->use_custom.scrollback_indicator = true;
1510 return true;
1513 else if (streq(key, "search-box-no-match")) {
1514 if (!value_to_two_colors(
1515 ctx,
1516 &theme->search_box.no_match.fg,
1517 &theme->search_box.no_match.bg,
1518 false))
1520 return false;
1523 theme->use_custom.search_box_no_match = true;
1524 return true;
1527 else if (streq(key, "search-box-match")) {
1528 if (!value_to_two_colors(
1529 ctx,
1530 &theme->search_box.match.fg,
1531 &theme->search_box.match.bg,
1532 false))
1534 return false;
1537 theme->use_custom.search_box_match = true;
1538 return true;
1541 else if (streq(key, "cursor")) {
1542 if (!value_to_two_colors(
1543 ctx,
1544 &theme->cursor.text,
1545 &theme->cursor.cursor,
1546 false))
1548 return false;
1551 theme->use_custom.cursor = true;
1552 return true;
1555 else if (streq(key, "urls")) {
1556 if (!value_to_color(ctx, &theme->url, false))
1557 return false;
1559 theme->use_custom.url = true;
1560 return true;
1563 else if (streq(key, "alpha")) {
1564 float alpha;
1565 if (!value_to_float(ctx, &alpha))
1566 return false;
1568 if (alpha < 0. || alpha > 1.) {
1569 LOG_CONTEXTUAL_ERR("not in range 0.0-1.0");
1570 return false;
1573 theme->alpha = alpha * 65535.;
1574 return true;
1577 else if (streq(key, "flash-alpha")) {
1578 float alpha;
1579 if (!value_to_float(ctx, &alpha))
1580 return false;
1582 if (alpha < 0. || alpha > 1.) {
1583 LOG_CONTEXTUAL_ERR("not in range 0.0-1.0");
1584 return false;
1587 theme->flash_alpha = alpha * 65535.;
1588 return true;
1591 else if (streq(key, "alpha-mode")) {
1592 _Static_assert(sizeof(theme->alpha_mode) == sizeof(int),
1593 "enum is not 32-bit");
1595 return value_to_enum(
1596 ctx,
1597 (const char *[]){"default", "matching", "all", NULL},
1598 (int *)&theme->alpha_mode);
1601 else if (streq(key, "dim-blend-towards")) {
1602 _Static_assert(sizeof(theme->dim_blend_towards) == sizeof(int),
1603 "enum is not 32-bit");
1605 return value_to_enum(
1606 ctx,
1607 (const char *[]){"black", "white", NULL},
1608 (int *)&theme->dim_blend_towards);
1611 else if (streq(key, "blur"))
1612 return value_to_bool(ctx, &theme->blur);
1614 else {
1615 LOG_CONTEXTUAL_ERR("not valid option");
1616 return false;
1619 uint32_t color_value;
1620 if (!value_to_color(ctx, &color_value, false))
1621 return false;
1623 *color = color_value;
1624 return true;
1627static bool
1628parse_section_colors_dark(struct context *ctx)
1630 return parse_color_theme(ctx, &ctx->conf->colors_dark);
1633static bool
1634parse_section_colors_light(struct context *ctx)
1636 return parse_color_theme(ctx, &ctx->conf->colors_light);
1639static bool
1640parse_section_cursor(struct context *ctx)
1642 struct config *conf = ctx->conf;
1643 const char *key = ctx->key;
1645 if (streq(key, "style")) {
1646 _Static_assert(sizeof(conf->cursor.style) == sizeof(int),
1647 "enum is not 32-bit");
1649 return value_to_enum(
1650 ctx,
1651 (const char *[]){"block", "underline", "beam", "hollow", NULL},
1652 (int *)&conf->cursor.style);
1655 else if (streq(key, "unfocused-style")) {
1656 _Static_assert(sizeof(conf->cursor.unfocused_style) == sizeof(int),
1657 "enum is not 32-bit");
1659 return value_to_enum(
1660 ctx,
1661 (const char *[]){"unchanged", "hollow", "none", NULL},
1662 (int *)&conf->cursor.unfocused_style);
1665 else if (streq(key, "blink"))
1666 return value_to_bool(ctx, &conf->cursor.blink.enabled);
1668 else if (streq(key, "blink-rate"))
1669 return value_to_uint32(ctx, 10, &conf->cursor.blink.rate_ms);
1671 else if (streq(key, "beam-thickness"))
1672 return value_to_pt_or_px(ctx, &conf->cursor.beam_thickness);
1674 else if (streq(key, "underline-thickness"))
1675 return value_to_pt_or_px(ctx, &conf->cursor.underline_thickness);
1677 else {
1678 LOG_CONTEXTUAL_ERR("not a valid option: %s", key);
1679 return false;
1683static bool
1684parse_section_mouse(struct context *ctx)
1686 struct config *conf = ctx->conf;
1687 const char *key = ctx->key;
1689 if (streq(key, "hide-when-typing"))
1690 return value_to_bool(ctx, &conf->mouse.hide_when_typing);
1692 else if (streq(key, "alternate-scroll-mode"))
1693 return value_to_bool(ctx, &conf->mouse.alternate_scroll_mode);
1695 else {
1696 LOG_CONTEXTUAL_ERR("not a valid option: %s", key);
1697 return false;
1701static bool
1702parse_section_csd(struct context *ctx)
1704 struct config *conf = ctx->conf;
1705 const char *key = ctx->key;
1707 if (streq(key, "preferred")) {
1708 _Static_assert(sizeof(conf->csd.preferred) == sizeof(int),
1709 "enum is not 32-bit");
1711 return value_to_enum(
1712 ctx,
1713 (const char *[]){"none", "server", "client", NULL},
1714 (int *)&conf->csd.preferred);
1717 else if (streq(key, "font")) {
1718 struct config_font_list new_list = value_to_fonts(ctx);
1719 if (new_list.arr == NULL)
1720 return false;
1722 config_font_list_destroy(&conf->csd.font);
1723 conf->csd.font = new_list;
1724 return true;
1727 else if (streq(key, "color")) {
1728 uint32_t color;
1729 if (!value_to_color(ctx, &color, true))
1730 return false;
1732 conf->csd.color.title_set = true;
1733 conf->csd.color.title = color;
1734 return true;
1737 else if (streq(key, "size"))
1738 return value_to_uint16(ctx, 10, &conf->csd.title_height);
1740 else if (streq(key, "button-width"))
1741 return value_to_uint16(ctx, 10, &conf->csd.button_width);
1743 else if (streq(key, "button-color")) {
1744 if (!value_to_color(ctx, &conf->csd.color.buttons, true))
1745 return false;
1747 conf->csd.color.buttons_set = true;
1748 return true;
1751 else if (streq(key, "button-minimize-color")) {
1752 if (!value_to_color(ctx, &conf->csd.color.minimize, true))
1753 return false;
1755 conf->csd.color.minimize_set = true;
1756 return true;
1759 else if (streq(key, "button-maximize-color")) {
1760 if (!value_to_color(ctx, &conf->csd.color.maximize, true))
1761 return false;
1763 conf->csd.color.maximize_set = true;
1764 return true;
1767 else if (streq(key, "button-close-color")) {
1768 if (!value_to_color(ctx, &conf->csd.color.quit, true))
1769 return false;
1771 conf->csd.color.close_set = true;
1772 return true;
1775 else if (streq(key, "border-color")) {
1776 if (!value_to_color(ctx, &conf->csd.color.border, true))
1777 return false;
1779 conf->csd.color.border_set = true;
1780 return true;
1783 else if (streq(key, "border-width"))
1784 return value_to_uint16(ctx, 10, &conf->csd.border_width_visible);
1786 else if (streq(key, "hide-when-maximized"))
1787 return value_to_bool(ctx, &conf->csd.hide_when_maximized);
1789 else if (streq(key, "double-click-to-maximize"))
1790 return value_to_bool(ctx, &conf->csd.double_click_to_maximize);
1792 else {
1793 LOG_CONTEXTUAL_ERR("not a valid action: %s", key);
1794 return false;
1798static void
1799free_binding_aux(struct binding_aux *aux)
1801 if (!aux->master_copy)
1802 return;
1804 switch (aux->type) {
1805 case BINDING_AUX_NONE: break;
1806 case BINDING_AUX_PIPE: free_argv(&aux->pipe); break;
1807 case BINDING_AUX_TEXT: free(aux->text.data); break;
1808 case BINDING_AUX_REGEX: free(aux->regex_name); break;
1812static void
1813free_key_binding(struct config_key_binding *binding)
1815 free_binding_aux(&binding->aux);
1816 tll_free_and_free(binding->modifiers, free);
1819static void NOINLINE
1820free_key_binding_list(struct config_key_binding_list *bindings)
1822 struct config_key_binding *binding = &bindings->arr[0];
1824 for (size_t i = 0; i < bindings->count; i++, binding++)
1825 free_key_binding(binding);
1826 free(bindings->arr);
1828 bindings->arr = NULL;
1829 bindings->count = 0;
1832static void NOINLINE
1833parse_modifiers(const char *text, size_t len, config_modifier_list_t *modifiers)
1835 tll_free_and_free(*modifiers, free);
1837 /* Handle "none" separately because e.g. none+shift is nonsense */
1838 if (strncmp(text, "none", len) == 0)
1839 return;
1841 char *copy = xstrndup(text, len);
1843 for (char *ctx = NULL, *key = strtok_r(copy, "+", &ctx);
1844 key != NULL;
1845 key = strtok_r(NULL, "+", &ctx))
1847 tll_push_back(*modifiers, xstrdup(key));
1850 free(copy);
1851 tll_sort(*modifiers, strcmp);
1854static int NOINLINE
1855argv_compare(const struct argv *argv1, const struct argv *argv2)
1857 if (argv1->args == NULL && argv2->args == NULL)
1858 return 0;
1860 if (argv1->args == NULL)
1861 return -1;
1862 if (argv2->args == NULL)
1863 return 1;
1865 for (size_t i = 0; ; i++) {
1866 if (argv1->args[i] == NULL && argv2->args[i] == NULL)
1867 return 0;
1868 if (argv1->args[i] == NULL)
1869 return -1;
1870 if (argv2->args[i] == NULL)
1871 return 1;
1873 int ret = strcmp(argv1->args[i], argv2->args[i]);
1874 if (ret != 0)
1875 return ret;
1878 BUG("unexpected loop break");
1879 return 1;
1882static bool NOINLINE
1883binding_aux_equal(const struct binding_aux *a,
1884 const struct binding_aux *b)
1886 if (a->type != b->type)
1887 return false;
1889 switch (a->type) {
1890 case BINDING_AUX_NONE:
1891 return true;
1893 case BINDING_AUX_PIPE:
1894 return argv_compare(&a->pipe, &b->pipe) == 0;
1896 case BINDING_AUX_TEXT:
1897 return a->text.len == b->text.len &&
1898 memcmp(a->text.data, b->text.data, a->text.len) == 0;
1900 case BINDING_AUX_REGEX:
1901 return streq(a->regex_name, b->regex_name);
1904 BUG("invalid AUX type: %d", a->type);
1905 return false;
1908static void NOINLINE
1909remove_from_key_bindings_list(struct config_key_binding_list *bindings,
1910 int action, const struct binding_aux *aux)
1912 size_t remove_first_idx = 0;
1913 size_t remove_count = 0;
1915 for (size_t i = 0; i < bindings->count; i++) {
1916 struct config_key_binding *binding = &bindings->arr[i];
1918 if (binding->action != action)
1919 continue;
1921 if (binding_aux_equal(&binding->aux, aux)) {
1922 if (remove_count++ == 0)
1923 remove_first_idx = i;
1925 xassert(remove_first_idx + remove_count - 1 == i);
1926 free_key_binding(binding);
1930 if (remove_count == 0)
1931 return;
1933 size_t move_count = bindings->count - (remove_first_idx + remove_count);
1935 memmove(
1936 &bindings->arr[remove_first_idx],
1937 &bindings->arr[remove_first_idx + remove_count],
1938 move_count * sizeof(bindings->arr[0]));
1939 bindings->count -= remove_count;
1942static const struct {
1943 const char *name;
1944 int code;
1945} button_map[] = {
1946 /* System defined */
1947 {"BTN_LEFT", BTN_LEFT},
1948 {"BTN_RIGHT", BTN_RIGHT},
1949 {"BTN_MIDDLE", BTN_MIDDLE},
1950 {"BTN_SIDE", BTN_SIDE},
1951 {"BTN_EXTRA", BTN_EXTRA},
1952 {"BTN_FORWARD", BTN_FORWARD},
1953 {"BTN_BACK", BTN_BACK},
1954 {"BTN_TASK", BTN_TASK},
1956 /* Foot custom, to be able to map scroll events to mouse bindings */
1957 {"BTN_WHEEL_BACK", BTN_WHEEL_BACK},
1958 {"BTN_WHEEL_FORWARD", BTN_WHEEL_FORWARD},
1959 {"BTN_WHEEL_LEFT", BTN_WHEEL_LEFT},
1960 {"BTN_WHEEL_RIGHT", BTN_WHEEL_RIGHT},
1963static int
1964mouse_button_name_to_code(const char *name)
1966 for (size_t i = 0; i < ALEN(button_map); i++) {
1967 if (streq(button_map[i].name, name))
1968 return button_map[i].code;
1970 return -1;
1973static const char*
1974mouse_button_code_to_name(int code)
1976 for (size_t i = 0; i < ALEN(button_map); i++) {
1977 if (code == button_map[i].code)
1978 return button_map[i].name;
1981 return NULL;
1984static bool NOINLINE
1985value_to_key_combos(struct context *ctx, int action,
1986 struct binding_aux *aux,
1987 struct config_key_binding_list *bindings,
1988 enum key_binding_type type)
1990 if (strcasecmp(ctx->value, "none") == 0) {
1991 remove_from_key_bindings_list(bindings, action, aux);
1992 return true;
1995 /* Count number of combinations */
1996 size_t combo_count = 1;
1997 size_t used_combos = 1; /* For error handling */
1998 for (const char *p = strchr(ctx->value, ' ');
1999 p != NULL;
2000 p = strchr(p + 1, ' '))
2002 combo_count++;
2005 struct config_key_binding new_combos[combo_count];
2007 char *copy = xstrdup(ctx->value);
2008 size_t idx = 0;
2010 for (char *tok_ctx = NULL, *combo = strtok_r(copy, " ", &tok_ctx);
2011 combo != NULL;
2012 combo = strtok_r(NULL, " ", &tok_ctx),
2013 idx++, used_combos++)
2015 struct config_key_binding *new_combo = &new_combos[idx];
2016 new_combo->action = action;
2017 new_combo->aux = *aux;
2018 new_combo->aux.master_copy = idx == 0;
2019#if 0
2020 new_combo->aux.type = BINDING_AUX_PIPE;
2021 new_combo->aux.master_copy = idx == 0;
2022 new_combo->aux.pipe = *argv;
2023#endif
2024 memset(&new_combo->modifiers, 0, sizeof(new_combo->modifiers));
2025 new_combo->path = ctx->path;
2026 new_combo->lineno = ctx->lineno;
2028 char *key = strrchr(combo, '+');
2030 if (key == NULL) {
2031 /* No modifiers */
2032 key = combo;
2033 } else {
2034 *key = '\0';
2035 parse_modifiers(combo, key - combo, &new_combo->modifiers);
2036 key++; /* Skip past the '+' */
2039 switch (type) {
2040 case KEY_BINDING:
2041 /* Translate key name to symbol */
2042 new_combo->k.sym = xkb_keysym_from_name(key, 0);
2043 if (new_combo->k.sym == XKB_KEY_NoSymbol) {
2044 LOG_CONTEXTUAL_ERR("not a valid XKB key name: %s", key);
2045 goto err;
2047 break;
2049 case MOUSE_BINDING: {
2050 new_combo->m.count = 1;
2052 char *_count = strrchr(key, '-');
2053 if (_count != NULL) {
2054 *_count = '\0';
2055 _count++;
2057 errno = 0;
2058 char *end;
2059 unsigned long value = strtoul(_count, &end, 10);
2060 if (_count[0] == '\0' || *end != '\0' || errno != 0) {
2061 if (errno != 0)
2062 LOG_CONTEXTUAL_ERRNO("invalid click count: %s", _count);
2063 else
2064 LOG_CONTEXTUAL_ERR("invalid click count: %s", _count);
2065 goto err;
2068 new_combo->m.count = value;
2071 new_combo->m.button = mouse_button_name_to_code(key);
2072 if (new_combo->m.button < 0) {
2073 LOG_CONTEXTUAL_ERR("invalid mouse button name: %s", key);
2074 goto err;
2077 break;
2083 if (idx == 0) {
2084 LOG_CONTEXTUAL_ERR(
2085 "empty binding not allowed (set to 'none' to unmap)");
2086 free(copy);
2087 return false;
2090 remove_from_key_bindings_list(bindings, action, aux);
2092 bindings->arr = xrealloc(
2093 bindings->arr,
2094 (bindings->count + combo_count) * sizeof(bindings->arr[0]));
2096 memcpy(&bindings->arr[bindings->count],
2097 new_combos,
2098 combo_count * sizeof(bindings->arr[0]));
2099 bindings->count += combo_count;
2101 free(copy);
2102 return true;
2104err:
2105 for (size_t i = 0; i < used_combos; i++)
2106 free_key_binding(&new_combos[i]);
2107 free(copy);
2108 return false;
2111static bool
2112modifiers_equal(const config_modifier_list_t *mods1,
2113 const config_modifier_list_t *mods2)
2115 if (tll_length(*mods1) != tll_length(*mods2))
2116 return false;
2118 size_t count = 0;
2119 tll_foreach(*mods1, it1) {
2120 size_t skip = count;
2121 tll_foreach(*mods2, it2) {
2122 if (skip > 0) {
2123 skip--;
2124 continue;
2127 if (strcmp(it1->item, it2->item) != 0)
2128 return false;
2129 break;
2132 count++;
2135 return true;
2136 /*
2137 * bool shift = mods1->shift == mods2->shift;
2138 * bool alt = mods1->alt == mods2->alt;
2139 * bool ctrl = mods1->ctrl == mods2->ctrl;
2140 * bool super = mods1->super == mods2->super;
2141 * return shift && alt && ctrl && super;
2142 */
2145UNITTEST
2147 config_modifier_list_t mods1 = tll_init();
2148 config_modifier_list_t mods2 = tll_init();
2150 tll_push_back(mods1, xstrdup("foo"));
2151 tll_push_back(mods1, xstrdup("bar"));
2153 tll_push_back(mods2, xstrdup("foo"));
2154 xassert(!modifiers_equal(&mods1, &mods2));
2156 tll_push_back(mods2, xstrdup("zoo"));
2157 xassert(!modifiers_equal(&mods1, &mods2));
2159 free(tll_pop_back(mods2));
2160 tll_push_back(mods2, xstrdup("bar"));
2161 xassert(modifiers_equal(&mods1, &mods2));
2163 tll_free_and_free(mods1, free);
2164 tll_free_and_free(mods2, free);
2167static bool
2168modifiers_disjoint(const config_modifier_list_t *mods1,
2169 const config_modifier_list_t *mods2)
2171 size_t count = 0;
2172 tll_foreach(*mods1, it1) {
2173 /*
2174 * Both sets are sorted. Thus, if we're still here in
2175 * iteration 2..N, then we can skip the first x items in set
2176 * #2; we've already checked them against the previous items
2177 * in set #1.
2178 */
2179 size_t skip = count;
2180 tll_foreach(*mods2, it2) {
2182 if (skip > 0) {
2183 --skip;
2184 continue;
2187 int r = strcmp(it1->item, it2->item);
2188 if (r == 0)
2189 return false;
2190 if (r < 0)
2191 break;
2193 ++count;
2196 return true;
2199UNITTEST
2201 config_modifier_list_t mods1 = tll_init();
2202 config_modifier_list_t mods2 = tll_init();
2204 /* Both empty counts as disjoint */
2205 xassert(modifiers_disjoint(&mods1, &mods2));
2207 /* Second set being empty means it's disjoint with the first, non-empty set */
2208 tll_push_back(mods1, xstrdup("foo"));
2209 tll_push_back(mods1, xstrdup("bar"));
2210 tll_sort(mods1, strcmp);
2211 tll_sort(mods2, strcmp);
2212 xassert(modifiers_disjoint(&mods1, &mods2));
2213 tll_free_and_free(mods1, free);
2214 tll_free_and_free(mods2, free);
2216 /* "foo" in both sets -> not disjoint */
2217 tll_push_back(mods1, xstrdup("foo"));
2218 tll_push_back(mods1, xstrdup("bar"));
2219 tll_push_back(mods2, xstrdup("foo"));
2220 tll_sort(mods1, strcmp);
2221 tll_sort(mods2, strcmp);
2222 xassert(!modifiers_disjoint(&mods1, &mods2));
2223 tll_free_and_free(mods1, free);
2224 tll_free_and_free(mods2, free);
2226 /* "bar in both sets -> not disjoint */
2227 tll_push_back(mods1, xstrdup("foo"));
2228 tll_push_back(mods1, xstrdup("bar"));
2229 tll_push_back(mods2, xstrdup("bar"));
2230 tll_sort(mods1, strcmp);
2231 tll_sort(mods2, strcmp);
2232 xassert(!modifiers_disjoint(&mods1, &mods2));
2233 tll_free_and_free(mods1, free);
2234 tll_free_and_free(mods2, free);
2236 /* "foo" and "bar" in both sets -> not disjoint */
2237 tll_push_back(mods1, xstrdup("foo"));
2238 tll_push_back(mods1, xstrdup("bar"));
2239 tll_push_back(mods2, xstrdup("foo"));
2240 tll_push_back(mods2, xstrdup("bar"));
2241 tll_sort(mods1, strcmp);
2242 tll_sort(mods2, strcmp);
2243 xassert(!modifiers_disjoint(&mods1, &mods2));
2244 tll_free_and_free(mods1, free);
2245 tll_free_and_free(mods2, free);
2247 /*
2248 * Disjoint, with set #2 sorting most of it's items before the
2249 * items in set #1. This tests the count/skip optimization in
2250 * modifiers_disjoint().
2251 */
2252 tll_push_back(mods1, xstrdup("ddd"));
2253 tll_push_back(mods1, xstrdup("eee"));
2254 tll_push_back(mods1, xstrdup("fff"));
2255 tll_push_back(mods2, xstrdup("aaa"));
2256 tll_push_back(mods2, xstrdup("bbb"));
2257 tll_push_back(mods2, xstrdup("ccc"));
2258 tll_push_back(mods2, xstrdup("ggg"));
2259 tll_sort(mods1, strcmp);
2260 tll_sort(mods2, strcmp);
2261 xassert(modifiers_disjoint(&mods1, &mods2));
2262 tll_free_and_free(mods1, free);
2263 tll_free_and_free(mods2, free);
2266static char * NOINLINE
2267modifiers_to_str(const config_modifier_list_t *mods, bool strip_last_plus)
2269 size_t len = tll_length(*mods); /* '+' separator */
2270 tll_foreach(*mods, it)
2271 len += strlen(it->item);
2273 char *ret = xmalloc(len + 1);
2274 size_t idx = 0;
2275 tll_foreach(*mods, it) {
2276 idx += snprintf(&ret[idx], len - idx, "%s", it->item);
2277 ret[idx++] = '+';
2280 if (strip_last_plus)
2281 idx--;
2283 ret[idx] = '\0';
2284 return ret;
2288 * Parses a key binding value in the form
2289 * "[cmd-to-exec arg1 arg2] Mods+Key"
2291 * and extracts 'cmd-to-exec' and its arguments.
2293 * Input:
2294 * - value: raw string, in the form mentioned above
2295 * - cmd: pointer to string to will be allocated and filled with
2296 * 'cmd-to-exec arg1 arg2'
2297 * - argv: point to array of string. Array will be allocated. Will be
2298 * filled with {'cmd-to-exec', 'arg1', 'arg2', NULL}
2300 * Returns:
2301 * - ssize_t, number of bytes that were stripped from 'value' to remove the '[]'
2302 * enclosed cmd and its arguments, including any subsequent
2303 * whitespace characters. I.e. if 'value' is "[cmd] BTN_RIGHT", the
2304 * return value is 6 (strlen("[cmd] ")).
2305 * - cmd: allocated string containing "cmd arg1 arg2...". Caller frees.
2306 * - argv: allocated array containing {"cmd", "arg1", "arg2", NULL}. Caller frees.
2307 */
2308static ssize_t NOINLINE
2309pipe_argv_from_value(struct context *ctx, struct argv *argv)
2311 argv->args = NULL;
2313 if (ctx->value[0] != '[')
2314 return 0;
2316 const char *pipe_cmd_end = strrchr(ctx->value, ']');
2317 if (pipe_cmd_end == NULL) {
2318 LOG_CONTEXTUAL_ERR("unclosed '['");
2319 return -1;
2322 size_t pipe_len = pipe_cmd_end - ctx->value - 1;
2323 char *cmd = xstrndup(&ctx->value[1], pipe_len);
2325 if (!tokenize_cmdline(cmd, &argv->args)) {
2326 LOG_CONTEXTUAL_ERR("syntax error in command line");
2327 free(cmd);
2328 return -1;
2331 ssize_t remove_len = pipe_cmd_end + 1 - ctx->value;
2332 ctx->value = pipe_cmd_end + 1;
2333 while (isspace(*ctx->value)) {
2334 ctx->value++;
2335 remove_len++;
2338 free(cmd);
2339 return remove_len;
2342static ssize_t NOINLINE
2343regex_name_from_value(struct context *ctx, char **regex_name)
2345 *regex_name = NULL;
2347 if (ctx->value[0] != '[')
2348 return 0;
2350 const char *regex_end = strrchr(ctx->value, ']');
2351 if (regex_end == NULL) {
2352 LOG_CONTEXTUAL_ERR("unclosed '['");
2353 return -1;
2356 size_t regex_len = regex_end - ctx->value - 1;
2357 *regex_name = xstrndup(&ctx->value[1], regex_len);
2359 ssize_t remove_len = regex_end + 1 - ctx->value;
2360 ctx->value = regex_end + 1;
2361 while (isspace(*ctx->value)) {
2362 ctx->value++;
2363 remove_len++;
2366 return remove_len;
2370static bool NOINLINE
2371parse_key_binding_section(struct context *ctx,
2372 int action_count,
2373 const char *const action_map[static action_count],
2374 struct config_key_binding_list *bindings)
2376 for (int action = 0; action < action_count; action++) {
2377 if (action_map[action] == NULL)
2378 continue;
2380 if (!streq(ctx->key, action_map[action]))
2381 continue;
2383 struct binding_aux aux = {.type = BINDING_AUX_NONE, .master_copy = true};
2385 /* TODO: this is ugly... */
2386 if (action_map == binding_action_map &&
2387 action >= BIND_ACTION_PIPE_SCROLLBACK &&
2388 action <= BIND_ACTION_PIPE_COMMAND_OUTPUT)
2390 ssize_t pipe_remove_len = pipe_argv_from_value(ctx, &aux.pipe);
2391 if (pipe_remove_len <= 0)
2392 return false;
2394 aux.type = BINDING_AUX_PIPE;
2395 aux.master_copy = true;
2396 } else if (action_map == binding_action_map &&
2397 action >= BIND_ACTION_REGEX_LAUNCH &&
2398 action <= BIND_ACTION_REGEX_COPY)
2400 char *regex_name = NULL;
2401 ssize_t regex_remove_len = regex_name_from_value(ctx, &regex_name);
2402 if (regex_remove_len <= 0)
2403 return false;
2405 aux.type = BINDING_AUX_REGEX;
2406 aux.master_copy = true;
2407 aux.regex_name = regex_name;
2410 if (!value_to_key_combos(ctx, action, &aux, bindings, KEY_BINDING)) {
2411 free_binding_aux(&aux);
2412 return false;
2415 return true;
2418 LOG_CONTEXTUAL_ERR("not a valid action: %s", ctx->key);
2419 return false;
2422UNITTEST
2424 enum test_actions {
2425 TEST_ACTION_NONE,
2426 TEST_ACTION_FOO,
2427 TEST_ACTION_BAR,
2428 TEST_ACTION_COUNT,
2429 };
2431 const char *const map[] = {
2432 [TEST_ACTION_NONE] = NULL,
2433 [TEST_ACTION_FOO] = "foo",
2434 [TEST_ACTION_BAR] = "bar",
2435 };
2437 struct config conf = {0};
2438 struct config_key_binding_list bindings = {0};
2440 struct context ctx = {
2441 .conf = &conf,
2442 .section = "",
2443 .key = "foo",
2444 .value = "Escape",
2445 .path = "",
2446 };
2448 /*
2449 * ADD foo=Escape
2451 * This verifies we can bind a single key combo to an action.
2452 */
2453 xassert(parse_key_binding_section(&ctx, ALEN(map), map, &bindings));
2454 xassert(bindings.count == 1);
2455 xassert(bindings.arr[0].action == TEST_ACTION_FOO);
2456 xassert(bindings.arr[0].k.sym == XKB_KEY_Escape);
2458 /*
2459 * ADD bar=Control+g Control+Shift+x
2461 * This verifies we can bind multiple key combos to an action.
2462 */
2463 ctx.key = "bar";
2464 ctx.value = "Control+g Control+Shift+x";
2465 xassert(parse_key_binding_section(&ctx, ALEN(map), map, &bindings));
2466 xassert(bindings.count == 3);
2467 xassert(bindings.arr[0].action == TEST_ACTION_FOO);
2468 xassert(bindings.arr[1].action == TEST_ACTION_BAR);
2469 xassert(bindings.arr[1].k.sym == XKB_KEY_g);
2470 xassert(tll_length(bindings.arr[1].modifiers) == 1);
2471 xassert(strcmp(tll_front(bindings.arr[1].modifiers), XKB_MOD_NAME_CTRL) == 0);
2472 xassert(bindings.arr[2].action == TEST_ACTION_BAR);
2473 xassert(bindings.arr[2].k.sym == XKB_KEY_x);
2474 xassert(tll_length(bindings.arr[2].modifiers) == 2);
2475 xassert(strcmp(tll_front(bindings.arr[2].modifiers), XKB_MOD_NAME_CTRL) == 0);
2476 xassert(strcmp(tll_back(bindings.arr[2].modifiers), XKB_MOD_NAME_SHIFT) == 0);
2478 /*
2479 * REPLACE foo with foo=Mod+v Shift+q
2481 * This verifies we can update a single-combo action with multiple
2482 * key combos.
2483 */
2484 ctx.key = "foo";
2485 ctx.value = "Mod1+v Shift+q";
2486 xassert(parse_key_binding_section(&ctx, ALEN(map), map, &bindings));
2487 xassert(bindings.count == 4);
2488 xassert(bindings.arr[0].action == TEST_ACTION_BAR);
2489 xassert(bindings.arr[1].action == TEST_ACTION_BAR);
2490 xassert(bindings.arr[2].action == TEST_ACTION_FOO);
2491 xassert(bindings.arr[2].k.sym == XKB_KEY_v);
2492 xassert(tll_length(bindings.arr[2].modifiers) == 1);
2493 xassert(strcmp(tll_front(bindings.arr[2].modifiers), XKB_MOD_NAME_ALT) == 0);
2494 xassert(bindings.arr[3].action == TEST_ACTION_FOO);
2495 xassert(bindings.arr[3].k.sym == XKB_KEY_q);
2496 xassert(tll_length(bindings.arr[3].modifiers) == 1);
2497 xassert(strcmp(tll_front(bindings.arr[3].modifiers), XKB_MOD_NAME_SHIFT) == 0);
2499 /*
2500 * REMOVE bar
2501 */
2502 ctx.key = "bar";
2503 ctx.value = "none";
2504 xassert(parse_key_binding_section(&ctx, ALEN(map), map, &bindings));
2505 xassert(bindings.count == 2);
2506 xassert(bindings.arr[0].action == TEST_ACTION_FOO);
2507 xassert(bindings.arr[1].action == TEST_ACTION_FOO);
2509 /*
2510 * REMOVE foo
2511 */
2512 ctx.key = "foo";
2513 ctx.value = "none";
2514 xassert(parse_key_binding_section(&ctx, ALEN(map), map, &bindings));
2515 xassert(bindings.count == 0);
2517 free(bindings.arr);
2520static bool
2521parse_section_key_bindings(struct context *ctx)
2523 return parse_key_binding_section(
2524 ctx,
2525 BIND_ACTION_KEY_COUNT, binding_action_map,
2526 &ctx->conf->bindings.key);
2529static bool
2530parse_section_search_bindings(struct context *ctx)
2532 return parse_key_binding_section(
2533 ctx,
2534 BIND_ACTION_SEARCH_COUNT, search_binding_action_map,
2535 &ctx->conf->bindings.search);
2538static bool
2539parse_section_url_bindings(struct context *ctx)
2541 return parse_key_binding_section(
2542 ctx,
2543 BIND_ACTION_URL_COUNT, url_binding_action_map,
2544 &ctx->conf->bindings.url);
2547static bool NOINLINE
2548resolve_key_binding_collisions(struct config *conf, const char *section_name,
2549 const char *const action_map[],
2550 struct config_key_binding_list *bindings,
2551 enum key_binding_type type)
2553 bool ret = true;
2555 for (size_t i = 1; i < bindings->count; i++) {
2556 enum {COLLISION_NONE,
2557 COLLISION_OVERRIDE,
2558 COLLISION_BINDING} collision_type = COLLISION_NONE;
2559 const struct config_key_binding *collision_binding = NULL;
2561 struct config_key_binding *binding1 = &bindings->arr[i];
2562 xassert(binding1->action != BIND_ACTION_NONE);
2564 const config_modifier_list_t *mods1 = &binding1->modifiers;
2566 /* Does our modifiers collide with the selection override mods? */
2567 if (type == MOUSE_BINDING &&
2568 !modifiers_disjoint(
2569 mods1, &conf->mouse.selection_override_modifiers))
2571 collision_type = COLLISION_OVERRIDE;
2574 /* Does our binding collide with another binding? */
2575 for (ssize_t j = i - 1;
2576 collision_type == COLLISION_NONE && j >= 0;
2577 j--)
2579 const struct config_key_binding *binding2 = &bindings->arr[j];
2580 xassert(binding2->action != BIND_ACTION_NONE);
2582 if (binding2->action == binding1->action &&
2583 binding_aux_equal(&binding1->aux, &binding2->aux))
2585 continue;
2588 const config_modifier_list_t *mods2 = &binding2->modifiers;
2590 bool mods_equal = modifiers_equal(mods1, mods2);
2591 bool sym_equal;
2593 switch (type) {
2594 case KEY_BINDING:
2595 sym_equal = binding1->k.sym == binding2->k.sym;
2596 break;
2598 case MOUSE_BINDING:
2599 sym_equal = (binding1->m.button == binding2->m.button &&
2600 binding1->m.count == binding2->m.count);
2601 break;
2603 default:
2604 BUG("unhandled key binding type");
2607 if (!mods_equal || !sym_equal)
2608 continue;
2610 collision_binding = binding2;
2611 collision_type = COLLISION_BINDING;
2612 break;
2615 if (collision_type != COLLISION_NONE) {
2616 char *modifier_names = modifiers_to_str(mods1, false);
2617 char sym_name[64];
2619 switch (type){
2620 case KEY_BINDING:
2621 xkb_keysym_get_name(binding1->k.sym, sym_name, sizeof(sym_name));
2622 break;
2624 case MOUSE_BINDING: {
2625 const char *button_name =
2626 mouse_button_code_to_name(binding1->m.button);
2628 if (binding1->m.count > 1) {
2629 snprintf(sym_name, sizeof(sym_name), "%s-%d",
2630 button_name, binding1->m.count);
2631 } else
2632 strcpy(sym_name, button_name);
2633 break;
2637 switch (collision_type) {
2638 case COLLISION_NONE:
2639 break;
2641 case COLLISION_BINDING: {
2642 bool has_pipe = collision_binding->aux.type == BINDING_AUX_PIPE;
2643 LOG_AND_NOTIFY_ERR(
2644 "%s:%d: [%s].%s: %s%s already mapped to '%s%s%s%s'",
2645 binding1->path, binding1->lineno, section_name,
2646 action_map[binding1->action],
2647 modifier_names, sym_name,
2648 action_map[collision_binding->action],
2649 has_pipe ? " [" : "",
2650 has_pipe ? collision_binding->aux.pipe.args[0] : "",
2651 has_pipe ? "]" : "");
2652 ret = false;
2653 break;
2656 case COLLISION_OVERRIDE: {
2657 char *override_names = modifiers_to_str(
2658 &conf->mouse.selection_override_modifiers, true);
2660 LOG_AND_NOTIFY_ERR(
2661 "%s:%d: [%s].%s: %s%s: "
2662 "modifiers conflict with 'selection-override-modifiers=%s'",
2663 binding1->path != NULL ? binding1->path : "(default)",
2664 binding1->lineno, section_name,
2665 action_map[binding1->action],
2666 modifier_names, sym_name, override_names);
2667 ret = false;
2668 free(override_names);
2669 break;
2673 free(modifier_names);
2675 if (binding1->aux.master_copy && i + 1 < bindings->count) {
2676 struct config_key_binding *next = &bindings->arr[i + 1];
2678 if (next->action == binding1->action &&
2679 binding_aux_equal(&binding1->aux, &next->aux))
2681 /* Transfer ownership to next binding */
2682 next->aux.master_copy = true;
2683 binding1->aux.master_copy = false;
2687 free_key_binding(binding1);
2689 /* Remove the most recent binding */
2690 size_t move_count = bindings->count - (i + 1);
2691 memmove(&bindings->arr[i], &bindings->arr[i + 1],
2692 move_count * sizeof(bindings->arr[0]));
2693 bindings->count--;
2695 i--;
2699 return ret;
2702static bool
2703parse_section_mouse_bindings(struct context *ctx)
2705 struct config *conf = ctx->conf;
2706 const char *key = ctx->key;
2707 const char *value = ctx->value;
2709 if (streq(key, "selection-override-modifiers")) {
2710 parse_modifiers(
2711 ctx->value, strlen(value),
2712 &conf->mouse.selection_override_modifiers);
2713 return true;
2716 struct binding_aux aux;
2718 ssize_t pipe_remove_len = pipe_argv_from_value(ctx, &aux.pipe);
2719 if (pipe_remove_len < 0)
2720 return false;
2722 aux.type = pipe_remove_len == 0 ? BINDING_AUX_NONE : BINDING_AUX_PIPE;
2723 aux.master_copy = true;
2725 for (enum bind_action_normal action = 0;
2726 action < BIND_ACTION_COUNT;
2727 action++)
2729 if (binding_action_map[action] == NULL)
2730 continue;
2732 if (!streq(key, binding_action_map[action]))
2733 continue;
2735 if (!value_to_key_combos(
2736 ctx, action, &aux, &conf->bindings.mouse, MOUSE_BINDING))
2738 free_binding_aux(&aux);
2739 return false;
2742 return true;
2745 LOG_CONTEXTUAL_ERR("not a valid option: %s", key);
2746 free_binding_aux(&aux);
2747 return false;
2750static bool
2751parse_section_text_bindings(struct context *ctx)
2753 struct config *conf = ctx->conf;
2754 const char *key = ctx->key;
2756 const size_t key_len = strlen(key);
2758 uint8_t *data = xmalloc(key_len + 1);
2759 size_t data_len = 0;
2760 bool esc = false;
2762 for (size_t i = 0; i < key_len; i++) {
2763 if (key[i] == '\\') {
2764 if (i + 1 >= key_len) {
2765 ctx->value = "";
2766 LOG_CONTEXTUAL_ERR("trailing backslash");
2767 goto err;
2770 esc = true;
2773 else if (esc) {
2774 if (key[i] != 'x') {
2775 ctx->value = "";
2776 LOG_CONTEXTUAL_ERR("invalid escaped character: %c", key[i]);
2777 goto err;
2779 if (i + 2 >= key_len) {
2780 ctx->value = "";
2781 LOG_CONTEXTUAL_ERR("\\x sequence too short");
2782 goto err;
2785 const uint8_t nib1 = hex2nibble(key[i + 1]);
2786 const uint8_t nib2 = hex2nibble(key[i + 2]);
2788 if (nib1 >= HEX_DIGIT_INVALID || nib2 >= HEX_DIGIT_INVALID) {
2789 ctx->value = "";
2790 LOG_CONTEXTUAL_ERR("invalid \\x sequence: \\x%c%c",
2791 key[i + 1], key[i + 2]);
2792 goto err;
2795 data[data_len++] = nib1 << 4 | nib2;
2796 esc = false;
2797 i += 2;
2800 else
2801 data[data_len++] = key[i];
2804 struct binding_aux aux = {
2805 .type = BINDING_AUX_TEXT,
2806 .text = {
2807 .data = data, /* data is now owned by value_to_key_combos() */
2808 .len = data_len,
2809 },
2810 };
2812 if (!value_to_key_combos(ctx, BIND_ACTION_TEXT_BINDING, &aux,
2813 &conf->bindings.key, KEY_BINDING))
2815 /* Do *not* free(data) - it is handled by value_to_key_combos() */
2816 return false;
2819 return true;
2821err:
2822 free(data);
2823 return false;
2826static bool
2827parse_section_environment(struct context *ctx)
2829 struct config *conf = ctx->conf;
2830 const char *key = ctx->key;
2832 /* Check for pre-existing env variable */
2833 tll_foreach(conf->env_vars, it) {
2834 if (streq(it->item.name, key))
2835 return value_to_str(ctx, &it->item.value);
2838 /*
2839 * No pre-existing variable - allocate a new one
2840 */
2842 char *value = NULL;
2843 if (!value_to_str(ctx, &value))
2844 return false;
2846 tll_push_back(conf->env_vars, ((struct env_var){xstrdup(key), value}));
2847 return true;
2850static bool
2851parse_section_tweak(struct context *ctx)
2853 struct config *conf = ctx->conf;
2854 const char *key = ctx->key;
2856 if (streq(key, "scaling-filter")) {
2857 static const char *filters[] = {
2858 [FCFT_SCALING_FILTER_NONE] = "none",
2859 [FCFT_SCALING_FILTER_NEAREST] = "nearest",
2860 [FCFT_SCALING_FILTER_BILINEAR] = "bilinear",
2862 [FCFT_SCALING_FILTER_IMPULSE] = "impulse",
2863 [FCFT_SCALING_FILTER_BOX] = "box",
2864 [FCFT_SCALING_FILTER_LINEAR] = "linear",
2865 [FCFT_SCALING_FILTER_CUBIC] = "cubic",
2866 [FCFT_SCALING_FILTER_GAUSSIAN] = "gaussian",
2867 [FCFT_SCALING_FILTER_LANCZOS2] = "lanczos2",
2868 [FCFT_SCALING_FILTER_LANCZOS3] = "lanczos3",
2869 [FCFT_SCALING_FILTER_LANCZOS3_STRETCHED] = "lanczos3-stretched",
2870 NULL,
2871 };
2873 _Static_assert(sizeof(conf->tweak.fcft_filter) == sizeof(int),
2874 "enum is not 32-bit");
2876 return value_to_enum(ctx, filters, (int *)&conf->tweak.fcft_filter);
2879 else if (streq(key, "overflowing-glyphs"))
2880 return value_to_bool(ctx, &conf->tweak.overflowing_glyphs);
2882 else if (streq(key, "damage-whole-window"))
2883 return value_to_bool(ctx, &conf->tweak.damage_whole_window);
2885 else if (streq(key, "grapheme-shaping")) {
2886 if (!value_to_bool(ctx, &conf->tweak.grapheme_shaping))
2887 return false;
2889#if !defined(FOOT_GRAPHEME_CLUSTERING)
2890 if (conf->tweak.grapheme_shaping) {
2891 LOG_CONTEXTUAL_WARN(
2892 "foot was not compiled with support for grapheme shaping");
2893 conf->tweak.grapheme_shaping = false;
2895#endif
2897 if (conf->tweak.grapheme_shaping && !conf->can_shape_grapheme) {
2898 LOG_WARN(
2899 "fcft was not compiled with support for grapheme shaping");
2901 /* Keep it enabled though - this will cause us to do
2902 * grapheme-clustering at least */
2905 return true;
2908 else if (streq(key, "grapheme-width-method")) {
2909 _Static_assert(sizeof(conf->tweak.grapheme_width_method) == sizeof(int),
2910 "enum is not 32-bit");
2912 return value_to_enum(
2913 ctx,
2914 (const char *[]){"wcswidth", "double-width", "max", NULL},
2915 (int *)&conf->tweak.grapheme_width_method);
2918 else if (streq(key, "render-timer")) {
2919 _Static_assert(sizeof(conf->tweak.render_timer) == sizeof(int),
2920 "enum is not 32-bit");
2922 return value_to_enum(
2923 ctx,
2924 (const char *[]){"none", "osd", "log", "both", NULL},
2925 (int *)&conf->tweak.render_timer);
2928 else if (streq(key, "delayed-render-lower")) {
2929 uint32_t ns;
2930 if (!value_to_uint32(ctx, 10, &ns))
2931 return false;
2933 if (ns > 16666666) {
2934 LOG_CONTEXTUAL_ERR("timeout must not exceed 16ms");
2935 return false;
2938 conf->tweak.delayed_render_lower_ns = ns;
2939 return true;
2942 else if (streq(key, "delayed-render-upper")) {
2943 uint32_t ns;
2944 if (!value_to_uint32(ctx, 10, &ns))
2945 return false;
2947 if (ns > 16666666) {
2948 LOG_CONTEXTUAL_ERR("timeout must not exceed 16ms");
2949 return false;
2952 conf->tweak.delayed_render_upper_ns = ns;
2953 return true;
2956 else if (streq(key, "max-shm-pool-size-mb")) {
2957 uint32_t mb;
2958 if (!value_to_uint32(ctx, 10, &mb))
2959 return false;
2961 conf->tweak.max_shm_pool_size = min((int32_t)mb * 1024 * 1024, INT32_MAX);
2962 return true;
2965 else if (streq(key, "box-drawing-base-thickness"))
2966 return value_to_float(ctx, &conf->tweak.box_drawing_base_thickness);
2968 else if (streq(key, "box-drawing-solid-shades"))
2969 return value_to_bool(ctx, &conf->tweak.box_drawing_solid_shades);
2971 else if (streq(key, "font-monospace-warn"))
2972 return value_to_bool(ctx, &conf->tweak.font_monospace_warn);
2974 else if (streq(key, "sixel"))
2975 return value_to_bool(ctx, &conf->tweak.sixel);
2977 else if (streq(key, "dim-amount"))
2978 return value_to_float(ctx, &conf->dim.amount);
2980 else if (streq(key, "bold-text-in-bright-amount"))
2981 return value_to_float(ctx, &conf->bold_in_bright.amount);
2983 else if (streq(key, "surface-bit-depth")) {
2984 _Static_assert(sizeof(conf->tweak.surface_bit_depth) == sizeof(int),
2985 "enum is not 32-bit");
2987#if defined(HAVE_PIXMAN_RGBA_16)
2988 return value_to_enum(
2989 ctx,
2990 (const char *[]){"auto", "8-bit", "10-bit", "16-bit", NULL},
2991 (int *)&conf->tweak.surface_bit_depth);
2992#else
2993 return value_to_enum(
2994 ctx,
2995 (const char *[]){"auto", "8-bit", "10-bit", NULL},
2996 (int *)&conf->tweak.surface_bit_depth);
2997#endif
3000 else if (streq(key, "min-stride-alignment"))
3001 return value_to_uint32(ctx, 10, &conf->tweak.min_stride_alignment);
3003 else if (streq(key, "pre-apply-damage"))
3004 return value_to_bool(ctx, &conf->tweak.preapply_damage);
3006 else {
3007 LOG_CONTEXTUAL_ERR("not a valid option: %s", key);
3008 return false;
3012static bool
3013parse_section_touch(struct context *ctx) {
3014 struct config *conf = ctx->conf;
3015 const char *key = ctx->key;
3017 if (streq(key, "long-press-delay"))
3018 return value_to_uint32(ctx, 10, &conf->touch.long_press_delay);
3020 else {
3021 LOG_CONTEXTUAL_ERR("not a valid option: %s", key);
3022 return false;
3026static bool
3027parse_key_value(char *kv, char **section, const char **key, const char **value)
3029 bool section_is_needed = section != NULL;
3031 /* Strip leading whitespace */
3032 while (isspace(kv[0]))
3033 ++kv;
3035 if (section_is_needed)
3036 *section = "main";
3038 if (kv[0] == '=')
3039 return false;
3041 *key = kv;
3042 *value = NULL;
3044 size_t kvlen = strlen(kv);
3046 /* Strip trailing whitespace */
3047 while (isspace(kv[kvlen - 1]))
3048 kvlen--;
3049 kv[kvlen] = '\0';
3051 for (size_t i = 0; i < kvlen; ++i) {
3052 if (kv[i] == '.' && section_is_needed) {
3053 section_is_needed = false;
3054 *section = kv;
3055 kv[i] = '\0';
3056 if (i == kvlen - 1 || kv[i + 1] == '=') {
3057 *key = NULL;
3058 return false;
3060 *key = &kv[i + 1];
3061 } else if (kv[i] == '=') {
3062 kv[i] = '\0';
3063 if (i != kvlen - 1)
3064 *value = &kv[i + 1];
3065 break;
3069 if (*value == NULL)
3070 return false;
3072 /* Strip trailing whitespace from key (leading stripped earlier) */
3074 xassert(!isspace(*key[0]));
3076 char *end = (char *)*key + strlen(*key) - 1;
3077 while (isspace(end[0]))
3078 end--;
3079 end[1] = '\0';
3082 /* Strip leading whitespace from value (trailing stripped earlier) */
3083 while (isspace(*value[0]))
3084 ++*value;
3086 return true;
3089enum section {
3090 SECTION_MAIN,
3091 SECTION_SECURITY,
3092 SECTION_BELL,
3093 SECTION_DESKTOP_NOTIFICATIONS,
3094 SECTION_SCROLLBACK,
3095 SECTION_URL,
3096 SECTION_REGEX,
3097 SECTION_COLORS_DARK,
3098 SECTION_COLORS_LIGHT,
3099 SECTION_CURSOR,
3100 SECTION_MOUSE,
3101 SECTION_CSD,
3102 SECTION_KEY_BINDINGS,
3103 SECTION_SEARCH_BINDINGS,
3104 SECTION_URL_BINDINGS,
3105 SECTION_MOUSE_BINDINGS,
3106 SECTION_TEXT_BINDINGS,
3107 SECTION_ENVIRONMENT,
3108 SECTION_TWEAK,
3109 SECTION_TOUCH,
3111 SECTION_COUNT,
3114/* Function pointer, called for each key/value line */
3115typedef bool (*parser_fun_t)(struct context *ctx);
3117static const struct {
3118 parser_fun_t fun;
3119 const char *name;
3120 bool allow_colon_suffix;
3121} section_info[] = {
3122 [SECTION_MAIN] = {&parse_section_main, "main"},
3123 [SECTION_SECURITY] = {&parse_section_security, "security"},
3124 [SECTION_BELL] = {&parse_section_bell, "bell"},
3125 [SECTION_DESKTOP_NOTIFICATIONS] = {&parse_section_desktop_notifications, "desktop-notifications"},
3126 [SECTION_SCROLLBACK] = {&parse_section_scrollback, "scrollback"},
3127 [SECTION_URL] = {&parse_section_url, "url"},
3128 [SECTION_REGEX] = {&parse_section_regex, "regex", true},
3129 [SECTION_COLORS_DARK] = {&parse_section_colors_dark, "colors-dark"},
3130 [SECTION_COLORS_LIGHT] = {&parse_section_colors_light, "colors-light"},
3131 [SECTION_CURSOR] = {&parse_section_cursor, "cursor"},
3132 [SECTION_MOUSE] = {&parse_section_mouse, "mouse"},
3133 [SECTION_CSD] = {&parse_section_csd, "csd"},
3134 [SECTION_KEY_BINDINGS] = {&parse_section_key_bindings, "key-bindings"},
3135 [SECTION_SEARCH_BINDINGS] = {&parse_section_search_bindings, "search-bindings"},
3136 [SECTION_URL_BINDINGS] = {&parse_section_url_bindings, "url-bindings"},
3137 [SECTION_MOUSE_BINDINGS] = {&parse_section_mouse_bindings, "mouse-bindings"},
3138 [SECTION_TEXT_BINDINGS] = {&parse_section_text_bindings, "text-bindings"},
3139 [SECTION_ENVIRONMENT] = {&parse_section_environment, "environment"},
3140 [SECTION_TWEAK] = {&parse_section_tweak, "tweak"},
3141 [SECTION_TOUCH] = {&parse_section_touch, "touch"},
3144static_assert(ALEN(section_info) == SECTION_COUNT, "section info array size mismatch");
3146static enum section
3147str_to_section(char *str, char **suffix)
3149 *suffix = NULL;
3151 for (enum section section = SECTION_MAIN; section < SECTION_COUNT; ++section) {
3152 const char *name = section_info[section].name;
3154 if (streq(str, name))
3155 return section;
3157 else if (section_info[section].allow_colon_suffix) {
3158 const size_t str_len = strlen(str);
3159 const size_t name_len = strlen(name);
3161 /* At least "section:" chars? */
3162 if (str_len > name_len + 1) {
3163 if (strncmp(str, name, name_len) == 0 && str[name_len] == ':') {
3164 str[name_len] = '\0';
3165 *suffix = &str[name_len + 1];
3166 return section;
3171 return SECTION_COUNT;
3174static bool
3175parse_config_file(FILE *f, struct config *conf, const char *path, bool errors_are_fatal)
3177 enum section section = SECTION_MAIN;
3179 char *_line = NULL;
3180 size_t count = 0;
3181 bool ret = true;
3183#define error_or_continue() \
3184 { \
3185 if (errors_are_fatal) { \
3186 ret = false; \
3187 goto done; \
3188 } else \
3189 continue; \
3192 char *section_name = xstrdup("main");
3193 char *section_suffix = NULL;
3195 struct context context = {
3196 .conf = conf,
3197 .section = section_name,
3198 .section_suffix = section_suffix,
3199 .path = path,
3200 .lineno = 0,
3201 .errors_are_fatal = errors_are_fatal,
3202 };
3203 struct context *ctx = &context; /* For LOG_AND_*() */
3205 errno = 0;
3206 ssize_t len;
3208 while ((len = getline(&_line, &count, f)) != -1) {
3209 context.key = NULL;
3210 context.value = NULL;
3211 context.lineno++;
3213 char *line = _line;
3215 /* Strip leading whitespace */
3216 while (isspace(line[0])) {
3217 line++;
3218 len--;
3221 /* Empty line, or comment */
3222 if (line[0] == '\0' || line[0] == '#')
3223 continue;
3225 /* Strip the trailing newline - may be absent on the last line */
3226 if (line[len - 1] == '\n')
3227 line[--len] = '\0';
3229 /* Split up into key/value pair + trailing comment separated by blank */
3230 char *key_value = line;
3231 char *kv_trailing = &line[len - 1];
3232 char *comment = &line[1];
3233 while (comment[1] != '\0') {
3234 if (isblank(comment[0]) && comment[1] == '#') {
3235 comment[1] = '\0'; /* Terminate key/value pair */
3236 kv_trailing = comment++;
3237 break;
3239 comment++;
3241 comment++;
3243 /* Strip trailing whitespace */
3244 while (isspace(kv_trailing[0]))
3245 kv_trailing--;
3246 kv_trailing[1] = '\0';
3248 /* Check for new section */
3249 if (key_value[0] == '[') {
3250 key_value++;
3252 if (key_value[0] == ']') {
3253 LOG_CONTEXTUAL_ERR("empty section name");
3254 section = SECTION_COUNT;
3255 error_or_continue();
3258 char *end = strchr(key_value, ']');
3260 if (end == NULL) {
3261 context.section = key_value;
3262 LOG_CONTEXTUAL_ERR("syntax error: no closing ']'");
3263 context.section = section_name;
3264 section = SECTION_COUNT;
3265 error_or_continue();
3268 end[0] = '\0';
3270 if (end[1] != '\0') {
3271 context.section = key_value;
3272 LOG_CONTEXTUAL_ERR("section declaration contains trailing "
3273 "characters");
3274 context.section = section_name;
3275 section = SECTION_COUNT;
3276 error_or_continue();
3279 char *maybe_section_suffix;
3280 section = str_to_section(key_value, &maybe_section_suffix);
3281 if (section == SECTION_COUNT) {
3282 context.section = key_value;
3283 LOG_CONTEXTUAL_ERR("invalid section name: %s", key_value);
3284 context.section = section_name;
3285 error_or_continue();
3288 free(section_name);
3289 free(section_suffix);
3290 section_name = xstrdup(key_value);
3291 section_suffix = maybe_section_suffix != NULL ? xstrdup(maybe_section_suffix) : NULL;
3292 context.section = section_name;
3293 context.section_suffix = section_suffix;
3295 /* Process next line */
3296 continue;
3299 if (section >= SECTION_COUNT) {
3300 /* Last section name was invalid; ignore all keys in it */
3301 continue;
3304 if (!parse_key_value(key_value, NULL, &context.key, &context.value)) {
3305 LOG_CONTEXTUAL_ERR("syntax error: key/value pair has no %s",
3306 context.key == NULL ? "key" : "value");
3307 error_or_continue();
3310 LOG_DBG("section=%s, key='%s', value='%s', comment='%s'",
3311 section_info[section].name, context.key, context.value, comment);
3313 xassert(section >= 0 && section < SECTION_COUNT);
3315 parser_fun_t section_parser = section_info[section].fun;
3316 xassert(section_parser != NULL);
3318 if (!section_parser(ctx))
3319 error_or_continue();
3321 /* For next iteration of getline() */
3322 errno = 0;
3325 if (errno != 0) {
3326 LOG_AND_NOTIFY_ERRNO("failed to read from configuration");
3327 if (errors_are_fatal)
3328 ret = false;
3331done:
3332 free(section_name);
3333 free(section_suffix);
3334 free(_line);
3335 return ret;
3338static char *
3339get_server_socket_path(void)
3341 const char *xdg_runtime = getenv("XDG_RUNTIME_DIR");
3342 if (xdg_runtime == NULL)
3343 return xstrdup("/tmp/foot.sock");
3345 const char *wayland_display = getenv("WAYLAND_DISPLAY");
3346 if (wayland_display == NULL) {
3347 return xstrjoin(xdg_runtime, "/foot.sock");
3350 return xasprintf("%s/foot-%s.sock", xdg_runtime, wayland_display);
3353static config_modifier_list_t
3354m(const char *text)
3356 config_modifier_list_t ret = tll_init();
3357 parse_modifiers(text, strlen(text), &ret);
3358 return ret;
3361static void
3362add_default_key_bindings(struct config *conf)
3364 const struct config_key_binding bindings[] = {
3365 {BIND_ACTION_SCROLLBACK_UP_PAGE, m(XKB_MOD_NAME_SHIFT), {{XKB_KEY_Prior}}},
3366 {BIND_ACTION_SCROLLBACK_UP_PAGE, m(XKB_MOD_NAME_SHIFT), {{XKB_KEY_KP_Prior}}},
3367 {BIND_ACTION_SCROLLBACK_DOWN_PAGE, m(XKB_MOD_NAME_SHIFT), {{XKB_KEY_Next}}},
3368 {BIND_ACTION_SCROLLBACK_DOWN_PAGE, m(XKB_MOD_NAME_SHIFT), {{XKB_KEY_KP_Next}}},
3369 {BIND_ACTION_CLIPBOARD_COPY, m(XKB_MOD_NAME_CTRL "+" XKB_MOD_NAME_SHIFT), {{XKB_KEY_c}}},
3370 {BIND_ACTION_CLIPBOARD_COPY, m("none"), {{XKB_KEY_XF86Copy}}},
3371 {BIND_ACTION_CLIPBOARD_PASTE, m(XKB_MOD_NAME_CTRL "+" XKB_MOD_NAME_SHIFT), {{XKB_KEY_v}}},
3372 {BIND_ACTION_CLIPBOARD_PASTE, m("none"), {{XKB_KEY_XF86Paste}}},
3373 {BIND_ACTION_PRIMARY_PASTE, m(XKB_MOD_NAME_SHIFT), {{XKB_KEY_Insert}}},
3374 {BIND_ACTION_SEARCH_START, m(XKB_MOD_NAME_CTRL "+" XKB_MOD_NAME_SHIFT), {{XKB_KEY_r}}},
3375 {BIND_ACTION_FONT_SIZE_UP, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_plus}}},
3376 {BIND_ACTION_FONT_SIZE_UP, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_equal}}},
3377 {BIND_ACTION_FONT_SIZE_UP, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_KP_Add}}},
3378 {BIND_ACTION_FONT_SIZE_DOWN, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_minus}}},
3379 {BIND_ACTION_FONT_SIZE_DOWN, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_KP_Subtract}}},
3380 {BIND_ACTION_FONT_SIZE_RESET, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_0}}},
3381 {BIND_ACTION_FONT_SIZE_RESET, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_KP_0}}},
3382 {BIND_ACTION_SPAWN_TERMINAL, m(XKB_MOD_NAME_CTRL "+" XKB_MOD_NAME_SHIFT), {{XKB_KEY_n}}},
3383 {BIND_ACTION_SHOW_URLS_LAUNCH, m(XKB_MOD_NAME_CTRL "+" XKB_MOD_NAME_SHIFT), {{XKB_KEY_o}}},
3384 {BIND_ACTION_UNICODE_INPUT, m(XKB_MOD_NAME_CTRL "+" XKB_MOD_NAME_SHIFT), {{XKB_KEY_u}}},
3385 {BIND_ACTION_PROMPT_PREV, m(XKB_MOD_NAME_CTRL "+" XKB_MOD_NAME_SHIFT), {{XKB_KEY_z}}},
3386 {BIND_ACTION_PROMPT_NEXT, m(XKB_MOD_NAME_CTRL "+" XKB_MOD_NAME_SHIFT), {{XKB_KEY_x}}},
3387 };
3389 conf->bindings.key.count = ALEN(bindings);
3390 conf->bindings.key.arr = xmemdup(bindings, sizeof(bindings));
3394static void
3395add_default_search_bindings(struct config *conf)
3397 const struct config_key_binding bindings[] = {
3398 {BIND_ACTION_SEARCH_SCROLLBACK_UP_PAGE, m(XKB_MOD_NAME_SHIFT), {{XKB_KEY_Prior}}},
3399 {BIND_ACTION_SEARCH_SCROLLBACK_UP_PAGE, m(XKB_MOD_NAME_SHIFT), {{XKB_KEY_KP_Prior}}},
3400 {BIND_ACTION_SEARCH_SCROLLBACK_DOWN_PAGE, m(XKB_MOD_NAME_SHIFT), {{XKB_KEY_Next}}},
3401 {BIND_ACTION_SEARCH_SCROLLBACK_DOWN_PAGE, m(XKB_MOD_NAME_SHIFT), {{XKB_KEY_KP_Next}}},
3402 {BIND_ACTION_SEARCH_CANCEL, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_c}}},
3403 {BIND_ACTION_SEARCH_CANCEL, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_g}}},
3404 {BIND_ACTION_SEARCH_CANCEL, m("none"), {{XKB_KEY_Escape}}},
3405 {BIND_ACTION_SEARCH_COMMIT, m("none"), {{XKB_KEY_Return}}},
3406 {BIND_ACTION_SEARCH_COMMIT, m("none"), {{XKB_KEY_KP_Enter}}},
3407 {BIND_ACTION_SEARCH_FIND_PREV, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_r}}},
3408 {BIND_ACTION_SEARCH_FIND_NEXT, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_s}}},
3409 {BIND_ACTION_SEARCH_EDIT_LEFT, m("none"), {{XKB_KEY_Left}}},
3410 {BIND_ACTION_SEARCH_EDIT_LEFT, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_b}}},
3411 {BIND_ACTION_SEARCH_EDIT_LEFT_WORD, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_Left}}},
3412 {BIND_ACTION_SEARCH_EDIT_LEFT_WORD, m(XKB_MOD_NAME_ALT), {{XKB_KEY_b}}},
3413 {BIND_ACTION_SEARCH_EDIT_RIGHT, m("none"), {{XKB_KEY_Right}}},
3414 {BIND_ACTION_SEARCH_EDIT_RIGHT, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_f}}},
3415 {BIND_ACTION_SEARCH_EDIT_RIGHT_WORD, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_Right}}},
3416 {BIND_ACTION_SEARCH_EDIT_RIGHT_WORD, m(XKB_MOD_NAME_ALT), {{XKB_KEY_f}}},
3417 {BIND_ACTION_SEARCH_EDIT_HOME, m("none"), {{XKB_KEY_Home}}},
3418 {BIND_ACTION_SEARCH_EDIT_HOME, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_a}}},
3419 {BIND_ACTION_SEARCH_EDIT_END, m("none"), {{XKB_KEY_End}}},
3420 {BIND_ACTION_SEARCH_EDIT_END, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_e}}},
3421 {BIND_ACTION_SEARCH_DELETE_PREV, m("none"), {{XKB_KEY_BackSpace}}},
3422 {BIND_ACTION_SEARCH_DELETE_PREV_WORD, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_BackSpace}}},
3423 {BIND_ACTION_SEARCH_DELETE_PREV_WORD, m(XKB_MOD_NAME_ALT), {{XKB_KEY_BackSpace}}},
3424 {BIND_ACTION_SEARCH_DELETE_NEXT, m("none"), {{XKB_KEY_Delete}}},
3425 {BIND_ACTION_SEARCH_DELETE_NEXT_WORD, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_Delete}}},
3426 {BIND_ACTION_SEARCH_DELETE_NEXT_WORD, m(XKB_MOD_NAME_ALT), {{XKB_KEY_d}}},
3427 {BIND_ACTION_SEARCH_DELETE_TO_START, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_u}}},
3428 {BIND_ACTION_SEARCH_DELETE_TO_END, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_k}}},
3429 {BIND_ACTION_SEARCH_EXTEND_CHAR, m(XKB_MOD_NAME_SHIFT), {{XKB_KEY_Right}}},
3430 {BIND_ACTION_SEARCH_EXTEND_WORD, m(XKB_MOD_NAME_CTRL "+" XKB_MOD_NAME_SHIFT), {{XKB_KEY_Right}}},
3431 {BIND_ACTION_SEARCH_EXTEND_WORD, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_w}}},
3432 {BIND_ACTION_SEARCH_EXTEND_WORD_WS, m(XKB_MOD_NAME_CTRL "+" XKB_MOD_NAME_SHIFT), {{XKB_KEY_w}}},
3433 {BIND_ACTION_SEARCH_EXTEND_LINE_DOWN, m(XKB_MOD_NAME_SHIFT), {{XKB_KEY_Down}}},
3434 {BIND_ACTION_SEARCH_EXTEND_BACKWARD_CHAR, m(XKB_MOD_NAME_SHIFT), {{XKB_KEY_Left}}},
3435 {BIND_ACTION_SEARCH_EXTEND_BACKWARD_WORD, m(XKB_MOD_NAME_CTRL "+" XKB_MOD_NAME_SHIFT), {{XKB_KEY_Left}}},
3436 {BIND_ACTION_SEARCH_EXTEND_LINE_UP, m(XKB_MOD_NAME_SHIFT), {{XKB_KEY_Up}}},
3437 {BIND_ACTION_SEARCH_CLIPBOARD_PASTE, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_v}}},
3438 {BIND_ACTION_SEARCH_CLIPBOARD_PASTE, m(XKB_MOD_NAME_CTRL "+" XKB_MOD_NAME_SHIFT), {{XKB_KEY_v}}},
3439 {BIND_ACTION_SEARCH_CLIPBOARD_PASTE, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_y}}},
3440 {BIND_ACTION_SEARCH_CLIPBOARD_PASTE, m("none"), {{XKB_KEY_XF86Paste}}},
3441 {BIND_ACTION_SEARCH_PRIMARY_PASTE, m(XKB_MOD_NAME_SHIFT), {{XKB_KEY_Insert}}},
3442 };
3444 conf->bindings.search.count = ALEN(bindings);
3445 conf->bindings.search.arr = xmemdup(bindings, sizeof(bindings));
3448static void
3449add_default_url_bindings(struct config *conf)
3451 const struct config_key_binding bindings[] = {
3452 {BIND_ACTION_URL_CANCEL, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_c}}},
3453 {BIND_ACTION_URL_CANCEL, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_g}}},
3454 {BIND_ACTION_URL_CANCEL, m(XKB_MOD_NAME_CTRL), {{XKB_KEY_d}}},
3455 {BIND_ACTION_URL_CANCEL, m("none"), {{XKB_KEY_Escape}}},
3456 {BIND_ACTION_URL_TOGGLE_URL_ON_JUMP_LABEL, m("none"), {{XKB_KEY_t}}},
3457 };
3459 conf->bindings.url.count = ALEN(bindings);
3460 conf->bindings.url.arr = xmemdup(bindings, sizeof(bindings));
3463static void
3464add_default_mouse_bindings(struct config *conf)
3466 const struct config_key_binding bindings[] = {
3467 {BIND_ACTION_SCROLLBACK_UP_MOUSE, m("none"), {.m = {BTN_WHEEL_BACK, 1}}},
3468 {BIND_ACTION_SCROLLBACK_DOWN_MOUSE, m("none"), {.m = {BTN_WHEEL_FORWARD, 1}}},
3469 {BIND_ACTION_PRIMARY_PASTE, m("none"), {.m = {BTN_MIDDLE, 1}}},
3470 {BIND_ACTION_SELECT_BEGIN, m("none"), {.m = {BTN_LEFT, 1}}},
3471 {BIND_ACTION_SELECT_BEGIN_BLOCK, m(XKB_MOD_NAME_CTRL), {.m = {BTN_LEFT, 1}}},
3472 {BIND_ACTION_SELECT_EXTEND, m("none"), {.m = {BTN_RIGHT, 1}}},
3473 {BIND_ACTION_SELECT_EXTEND_CHAR_WISE, m(XKB_MOD_NAME_CTRL), {.m = {BTN_RIGHT, 1}}},
3474 {BIND_ACTION_SELECT_WORD, m("none"), {.m = {BTN_LEFT, 2}}},
3475 {BIND_ACTION_SELECT_WORD_WS, m(XKB_MOD_NAME_CTRL), {.m = {BTN_LEFT, 2}}},
3476 {BIND_ACTION_SELECT_QUOTE, m("none"), {.m = {BTN_LEFT, 3}}},
3477 {BIND_ACTION_SELECT_ROW, m("none"), {.m = {BTN_LEFT, 4}}},
3478 {BIND_ACTION_FONT_SIZE_UP, m("Control"), {.m = {BTN_WHEEL_BACK, 1}}},
3479 {BIND_ACTION_FONT_SIZE_DOWN, m("Control"), {.m = {BTN_WHEEL_FORWARD, 1}}},
3480 };
3482 conf->bindings.mouse.count = ALEN(bindings);
3483 conf->bindings.mouse.arr = xmemdup(bindings, sizeof(bindings));
3486static void NOINLINE
3487config_font_list_clone(struct config_font_list *dst,
3488 const struct config_font_list *src)
3490 dst->count = src->count;
3491 dst->arr = xmalloc(dst->count * sizeof(dst->arr[0]));
3493 for (size_t j = 0; j < dst->count; j++) {
3494 dst->arr[j].pt_size = src->arr[j].pt_size;
3495 dst->arr[j].px_size = src->arr[j].px_size;
3496 dst->arr[j].pattern = xstrdup(src->arr[j].pattern);
3500bool
3501config_load(struct config *conf, const char *conf_path,
3502 user_notifications_t *initial_user_notifications,
3503 config_override_t *overrides, bool errors_are_fatal,
3504 bool as_server)
3506 bool ret = true;
3507 enum fcft_capabilities fcft_caps = fcft_capabilities();
3509 *conf = (struct config) {
3510 .conf_path = (conf_path ? xstrdup(conf_path) : NULL),
3511 .term = xstrdup(FOOT_DEFAULT_TERM),
3512 .shell = get_shell(),
3513 .title = xstrdup("foot"),
3514 .app_id = (as_server ? xstrdup("footclient") : xstrdup("foot")),
3515 .toplevel_tag = xstrdup(""),
3516 .word_delimiters = xc32dup(U",│`|:\"'()[]{}<>"),
3517 .size = {
3518 .type = CONF_SIZE_PX,
3519 .width = 700,
3520 .height = 500,
3521 },
3522 .pad_left = 0,
3523 .pad_top = 0,
3524 .pad_right = 0,
3525 .pad_bottom = 0,
3526 .center_when = CENTER_MAXIMIZED_AND_FULLSCREEN,
3527 .resize_by_cells = true,
3528 .resize_keep_grid = true,
3529 .resize_delay_ms = 100,
3530 .dim = { .amount = 1.5 },
3531 .bold_in_bright = {
3532 .enabled = false,
3533 .palette_based = false,
3534 .amount = 1.3,
3535 },
3536 .startup_mode = STARTUP_WINDOWED,
3537 .fonts = {{0}},
3538 .font_size_adjustment = {.percent = 0., .pt_or_px = {.pt = 0.5, .px = 0}},
3539 .line_height = {.pt = 0, .px = -1},
3540 .letter_spacing = {.pt = 0, .px = 0},
3541 .horizontal_letter_offset = {.pt = 0, .px = 0},
3542 .vertical_letter_offset = {.pt = 0, .px = 0},
3543 .use_custom_underline_offset = false,
3544 .box_drawings_uses_font_glyphs = false,
3545 .underline_thickness = {.pt = 0., .px = -1},
3546 .strikeout_thickness = {.pt = 0., .px = -1},
3547 .dpi_aware = false,
3548 .gamma_correct = false,
3549 .uppercase_regex_insert = true,
3550 .security = {
3551 .osc52 = OSC52_ENABLED,
3552 },
3553 .bell = {
3554 .urgent = false,
3555 .notify = false,
3556 .flash = false,
3557 .system_bell = true,
3558 .command = {
3559 .argv = {.args = NULL},
3560 },
3561 .command_focused = false,
3562 },
3563 .url = {
3564 .label_letters = xc32dup(U"sadfjklewcmpgh"),
3565 .osc8_underline = OSC8_UNDERLINE_URL_MODE,
3566 .style = UNDERLINE_DOTTED,
3567 },
3568 .custom_regexes = tll_init(),
3569 .can_shape_grapheme = fcft_caps & FCFT_CAPABILITY_GRAPHEME_SHAPING,
3570 .scrollback = {
3571 .lines = 1000,
3572 .indicator = {
3573 .position = SCROLLBACK_INDICATOR_POSITION_RELATIVE,
3574 .format = SCROLLBACK_INDICATOR_FORMAT_TEXT,
3575 .text = xc32dup(U""),
3576 },
3577 .multiplier = 3.,
3578 },
3579 .colors_dark = {
3580 .fg = default_foreground,
3581 .bg = default_background,
3582 .flash = 0x7f7f00,
3583 .flash_alpha = 0x7fff,
3584 .alpha = 0xffff,
3585 .alpha_mode = ALPHA_MODE_DEFAULT,
3586 .dim_blend_towards = DIM_BLEND_TOWARDS_BLACK,
3587 .selection_fg = 0x80000000, /* Use default bg */
3588 .selection_bg = 0x80000000, /* Use default fg */
3589 .cursor = {
3590 .text = 0,
3591 .cursor = 0,
3592 },
3593 .use_custom = {
3594 .jump_label = false,
3595 .scrollback_indicator = false,
3596 .url = false,
3597 },
3598 .blur = false,
3599 },
3600 .initial_color_theme = COLOR_THEME_DARK,
3601 .cursor = {
3602 .style = CURSOR_BLOCK,
3603 .unfocused_style = CURSOR_UNFOCUSED_HOLLOW,
3604 .blink = {
3605 .enabled = false,
3606 .rate_ms = 500,
3607 },
3608 .beam_thickness = {.pt = 1.5},
3609 .underline_thickness = {.pt = 0., .px = -1},
3610 },
3611 .mouse = {
3612 .hide_when_typing = false,
3613 .alternate_scroll_mode = true,
3614 .selection_override_modifiers = tll_init(),
3615 },
3616 .csd = {
3617 .preferred = CONF_CSD_PREFER_SERVER,
3618 .font = {0},
3619 .hide_when_maximized = false,
3620 .double_click_to_maximize = true,
3621 .title_height = 26,
3622 .border_width = 5,
3623 .border_width_visible = 0,
3624 .button_width = 26,
3625 },
3627 .render_worker_count = sysconf(_SC_NPROCESSORS_ONLN),
3628 .server_socket_path = get_server_socket_path(),
3629 .presentation_timings = false,
3630 .selection_target = SELECTION_TARGET_PRIMARY,
3631 .hold_at_exit = false,
3632 .desktop_notifications = {
3633 .command = {
3634 .argv = {.args = NULL},
3635 },
3636 .command_action_arg = {
3637 .argv = {.args = NULL},
3638 },
3639 .close = {
3640 .argv = {.args = NULL},
3641 },
3642 .inhibit_when_focused = true,
3643 },
3645 .tweak = {
3646 .fcft_filter = FCFT_SCALING_FILTER_LANCZOS3,
3647 .overflowing_glyphs = true,
3648#if defined(FOOT_GRAPHEME_CLUSTERING) && FOOT_GRAPHEME_CLUSTERING
3649 .grapheme_shaping = fcft_caps & FCFT_CAPABILITY_GRAPHEME_SHAPING,
3650#endif
3651 .grapheme_width_method = GRAPHEME_WIDTH_DOUBLE,
3652 .delayed_render_lower_ns = 500000, /* 0.5ms */
3653 .delayed_render_upper_ns = 16666666 / 2, /* half a frame period (60Hz) */
3654 .max_shm_pool_size = 512 * 1024 * 1024,
3655 .render_timer = RENDER_TIMER_NONE,
3656 .damage_whole_window = false,
3657 .box_drawing_base_thickness = 0.04,
3658 .box_drawing_solid_shades = true,
3659 .font_monospace_warn = true,
3660 .sixel = true,
3661 .surface_bit_depth = SHM_BITS_AUTO,
3662 .min_stride_alignment = 256,
3663 .preapply_damage = true,
3664 },
3666 .touch = {
3667 .long_press_delay = 400,
3668 },
3670 .env_vars = tll_init(),
3671#if defined(UTMP_DEFAULT_HELPER_PATH)
3672 .utmp_helper_path = ((strlen(UTMP_DEFAULT_HELPER_PATH) > 0 &&
3673 access(UTMP_DEFAULT_HELPER_PATH, X_OK) == 0)
3674 ? xstrdup(UTMP_DEFAULT_HELPER_PATH)
3675 : NULL),
3676#endif
3677 .notifications = tll_init(),
3678 };
3680 memcpy(conf->colors_dark.table, default_color_table, sizeof(default_color_table));
3681 memcpy(conf->colors_dark.sixel, default_sixel_colors, sizeof(default_sixel_colors));
3682 memcpy(&conf->colors_light, &conf->colors_dark, sizeof(conf->colors_dark));
3683 conf->colors_light.dim_blend_towards = DIM_BLEND_TOWARDS_WHITE;
3685 parse_modifiers(XKB_MOD_NAME_SHIFT, 5, &conf->mouse.selection_override_modifiers);
3687 tokenize_cmdline(
3688 "notify-send --wait --app-name ${app-id} --icon ${app-id} --category ${category} --urgency ${urgency} --expire-time ${expire-time} --hint STRING:image-path:${icon} --hint BOOLEAN:suppress-sound:${muted} --hint STRING:sound-name:${sound-name} --replace-id ${replace-id} ${action-argument} --print-id -- ${title} ${body}",
3689 &conf->desktop_notifications.command.argv.args);
3690 tokenize_cmdline("--action ${action-name}=${action-label}", &conf->desktop_notifications.command_action_arg.argv.args);
3691 tokenize_cmdline("xdg-open ${url}", &conf->url.launch.argv.args);
3695 int r = regcomp(&conf->url.preg, default_url_regex_string, REG_EXTENDED);
3696 xassert(r == 0);
3697 conf->url.regex = xstrdup(default_url_regex_string);
3698 xassert(conf->url.preg.re_nsub >= 1);
3701 tll_foreach(*initial_user_notifications, it) {
3702 tll_push_back(conf->notifications, it->item);
3703 tll_remove(*initial_user_notifications, it);
3706 add_default_key_bindings(conf);
3707 add_default_search_bindings(conf);
3708 add_default_url_bindings(conf);
3709 add_default_mouse_bindings(conf);
3711 struct config_file conf_file = {.path = NULL, .fd = -1};
3712 if (conf_path != NULL) {
3713 int fd = open(conf_path, O_RDONLY);
3714 if (fd < 0) {
3715 LOG_AND_NOTIFY_ERRNO("%s: failed to open", conf_path);
3716 ret = !errors_are_fatal;
3717 } else {
3718 conf_file.path = xstrdup(conf_path);
3719 conf_file.fd = fd;
3721 } else {
3722 conf_file = open_config();
3723 if (conf_file.fd < 0) {
3724 LOG_WARN("no configuration found, using defaults");
3725 ret = !errors_are_fatal;
3729 if (conf_file.path && conf_file.fd >= 0) {
3730 LOG_INFO("loading configuration from %s", conf_file.path);
3732 FILE *f = fdopen(conf_file.fd, "r");
3733 if (f == NULL) {
3734 LOG_AND_NOTIFY_ERRNO("%s: failed to open", conf_file.path);
3735 ret = !errors_are_fatal;
3736 } else {
3737 if (!parse_config_file(f, conf, conf_file.path, errors_are_fatal))
3738 ret = !errors_are_fatal;
3740 fclose(f);
3741 conf_file.fd = -1;
3745 if (!config_override_apply(conf, overrides, errors_are_fatal))
3746 ret = !errors_are_fatal;
3748 if (ret && conf->fonts[0].count == 0) {
3749 struct config_font font;
3750 if (!config_font_parse("monospace", &font)) {
3751 LOG_ERR("failed to load font 'monospace' - no fonts installed?");
3752 ret = false;
3753 } else {
3754 conf->fonts[0].count = 1;
3755 conf->fonts[0].arr = xmalloc(sizeof(font));
3756 conf->fonts[0].arr[0] = font;
3760 if (ret && conf->csd.font.count == 0)
3761 config_font_list_clone(&conf->csd.font, &conf->fonts[0]);
3763#if defined(_DEBUG)
3764 for (size_t i = 0; i < conf->bindings.key.count; i++)
3765 xassert(conf->bindings.key.arr[i].action != BIND_ACTION_NONE);
3766 for (size_t i = 0; i < conf->bindings.search.count; i++)
3767 xassert(conf->bindings.search.arr[i].action != BIND_ACTION_SEARCH_NONE);
3768 for (size_t i = 0; i < conf->bindings.url.count; i++)
3769 xassert(conf->bindings.url.arr[i].action != BIND_ACTION_URL_NONE);
3770#endif
3772 free(conf_file.path);
3773 if (conf_file.fd >= 0)
3774 close(conf_file.fd);
3776 return ret;
3779bool
3780config_override_apply(struct config *conf, config_override_t *overrides,
3781 bool errors_are_fatal)
3783 char *section_name = NULL;
3785 struct context context = {
3786 .conf = conf,
3787 .path = "override",
3788 .lineno = 0,
3789 .errors_are_fatal = errors_are_fatal,
3790 };
3791 struct context *ctx = &context;
3793 tll_foreach(*overrides, it) {
3794 context.lineno++;
3796 if (!parse_key_value(it->item, &section_name, &context.key, &context.value))
3798 LOG_CONTEXTUAL_ERR("syntax error: key/value pair has no %s",
3799 context.key == NULL ? "key" : "value");
3800 if (errors_are_fatal)
3801 return false;
3802 continue;
3805 if (section_name[0] == '\0') {
3806 LOG_CONTEXTUAL_ERR("empty section name");
3807 if (errors_are_fatal)
3808 return false;
3809 continue;
3812 char *maybe_section_suffix = NULL;
3813 enum section section = str_to_section(section_name, &maybe_section_suffix);
3815 context.section = section_name;
3816 context.section_suffix = maybe_section_suffix;
3818 if (section == SECTION_COUNT) {
3819 LOG_CONTEXTUAL_ERR("invalid section name: %s", section_name);
3820 if (errors_are_fatal)
3821 return false;
3822 continue;
3825 parser_fun_t section_parser = section_info[section].fun;
3826 xassert(section_parser != NULL);
3828 if (!section_parser(ctx)) {
3829 if (errors_are_fatal)
3830 return false;
3831 continue;
3835 conf->csd.border_width = max(
3836 min_csd_border_width, conf->csd.border_width_visible);
3838 return
3839 resolve_key_binding_collisions(
3840 conf, section_info[SECTION_KEY_BINDINGS].name,
3841 binding_action_map, &conf->bindings.key, KEY_BINDING) &&
3842 resolve_key_binding_collisions(
3843 conf, section_info[SECTION_SEARCH_BINDINGS].name,
3844 search_binding_action_map, &conf->bindings.search, KEY_BINDING) &&
3845 resolve_key_binding_collisions(
3846 conf, section_info[SECTION_URL_BINDINGS].name,
3847 url_binding_action_map, &conf->bindings.url, KEY_BINDING) &&
3848 resolve_key_binding_collisions(
3849 conf, section_info[SECTION_MOUSE_BINDINGS].name,
3850 binding_action_map, &conf->bindings.mouse, MOUSE_BINDING);
3853static void NOINLINE
3854key_binding_list_clone(struct config_key_binding_list *dst,
3855 const struct config_key_binding_list *src)
3857 struct argv *last_master_argv = NULL;
3858 uint8_t *last_master_text_data = NULL;
3859 size_t last_master_text_len = 0;
3860 char *last_master_regex_name = NULL;
3862 dst->count = src->count;
3863 dst->arr = xmalloc(src->count * sizeof(dst->arr[0]));
3865 for (size_t i = 0; i < src->count; i++) {
3866 const struct config_key_binding *old = &src->arr[i];
3867 struct config_key_binding *new = &dst->arr[i];
3869 *new = *old;
3870 memset(&new->modifiers, 0, sizeof(new->modifiers));
3871 tll_foreach(old->modifiers, it)
3872 tll_push_back(new->modifiers, xstrdup(it->item));
3874 switch (old->aux.type) {
3875 case BINDING_AUX_NONE:
3876 last_master_argv = NULL;
3877 last_master_text_data = NULL;
3878 last_master_text_len = 0;
3879 break;
3881 case BINDING_AUX_PIPE:
3882 if (old->aux.master_copy) {
3883 clone_argv(&new->aux.pipe, &old->aux.pipe);
3884 last_master_argv = &new->aux.pipe;
3885 } else {
3886 xassert(last_master_argv != NULL);
3887 new->aux.pipe = *last_master_argv;
3889 last_master_text_data = NULL;
3890 last_master_text_len = 0;
3891 break;
3893 case BINDING_AUX_TEXT:
3894 if (old->aux.master_copy) {
3895 const size_t len = old->aux.text.len;
3896 new->aux.text.len = len;
3897 new->aux.text.data = xmemdup(old->aux.text.data, len);
3899 last_master_text_len = len;
3900 last_master_text_data = new->aux.text.data;
3901 } else {
3902 xassert(last_master_text_data != NULL);
3903 new->aux.text.len = last_master_text_len;
3904 new->aux.text.data = last_master_text_data;
3906 last_master_argv = NULL;
3907 break;
3909 case BINDING_AUX_REGEX:
3910 if (old->aux.master_copy) {
3911 new->aux.regex_name = xstrdup(old->aux.regex_name);
3912 last_master_regex_name = new->aux.regex_name;
3913 } else {
3914 xassert(last_master_regex_name != NULL);
3915 new->aux.regex_name = last_master_regex_name;
3917 break;
3922struct config *
3923config_clone(const struct config *old)
3925 struct config *conf = xmalloc(sizeof(*conf));
3926 *conf = *old;
3928 conf->conf_path = (old->conf_path ? xstrdup(old->conf_path) : NULL);
3929 conf->term = xstrdup(old->term);
3930 conf->shell = xstrdup(old->shell);
3931 conf->title = xstrdup(old->title);
3932 conf->app_id = xstrdup(old->app_id);
3933 conf->toplevel_tag = xstrdup(old->toplevel_tag);
3934 conf->word_delimiters = xc32dup(old->word_delimiters);
3935 conf->scrollback.indicator.text = xc32dup(old->scrollback.indicator.text);
3936 conf->server_socket_path = xstrdup(old->server_socket_path);
3937 spawn_template_clone(&conf->bell.command, &old->bell.command);
3938 spawn_template_clone(&conf->desktop_notifications.command,
3939 &old->desktop_notifications.command);
3940 spawn_template_clone(&conf->desktop_notifications.command_action_arg,
3941 &old->desktop_notifications.command_action_arg);
3942 spawn_template_clone(&conf->desktop_notifications.close,
3943 &old->desktop_notifications.close);
3945 for (size_t i = 0; i < ALEN(conf->fonts); i++)
3946 config_font_list_clone(&conf->fonts[i], &old->fonts[i]);
3947 config_font_list_clone(&conf->csd.font, &old->csd.font);
3949 conf->url.label_letters = xc32dup(old->url.label_letters);
3950 spawn_template_clone(&conf->url.launch, &old->url.launch);
3951 conf->url.regex = xstrdup(old->url.regex);
3952 regcomp(&conf->url.preg, conf->url.regex, REG_EXTENDED);
3954 memset(&conf->custom_regexes, 0, sizeof(conf->custom_regexes));
3955 tll_foreach(old->custom_regexes, it) {
3956 const struct custom_regex *old_regex = &it->item;
3958 tll_push_back(conf->custom_regexes,
3959 ((struct custom_regex){.name = xstrdup(old_regex->name),
3960 .regex = xstrdup(old_regex->regex)}));
3963 struct custom_regex *new_regex = &tll_back(conf->custom_regexes);
3964 regcomp(&new_regex->preg, new_regex->regex, REG_EXTENDED);
3965 spawn_template_clone(&new_regex->launch, &old_regex->launch);
3968 key_binding_list_clone(&conf->bindings.key, &old->bindings.key);
3969 key_binding_list_clone(&conf->bindings.search, &old->bindings.search);
3970 key_binding_list_clone(&conf->bindings.url, &old->bindings.url);
3971 key_binding_list_clone(&conf->bindings.mouse, &old->bindings.mouse);
3973 conf->env_vars.length = 0;
3974 conf->env_vars.head = conf->env_vars.tail = NULL;
3976 memset(&conf->mouse.selection_override_modifiers, 0, sizeof(conf->mouse.selection_override_modifiers));
3977 tll_foreach(old->mouse.selection_override_modifiers, it)
3978 tll_push_back(conf->mouse.selection_override_modifiers, xstrdup(it->item));
3980 tll_foreach(old->env_vars, it) {
3981 struct env_var copy = {
3982 .name = xstrdup(it->item.name),
3983 .value = xstrdup(it->item.value),
3984 };
3985 tll_push_back(conf->env_vars, copy);
3988 conf->utmp_helper_path =
3989 old->utmp_helper_path != NULL ? xstrdup(old->utmp_helper_path) : NULL;
3991 conf->notifications.length = 0;
3992 conf->notifications.head = conf->notifications.tail = 0;
3993 tll_foreach(old->notifications, it) {
3994 char *text = xstrdup(it->item.text);
3995 user_notification_add(&conf->notifications, it->item.kind, text);
3998 return conf;
Diagnostics5 git calls · 31 ms
CallStatusSizeTime031 ms
discover200178 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=178
git.server.agent=git/2.56.1-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=178
git.pkt.payload.max=35
ls-refs20010 KiB · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=10322
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=108
git.pkt.data=107
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=10322
git.pkt.payload.max=106
git.ls_refs.count=107
git.ls_refs.head=refs/heads/master
fetchcombine:blob:none+tree:2 · deepen 12008.6 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=8830
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=8830
git.pkt.payload.max=8730
git.sideband.band1.bytes=8730
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=14
git.pack.bytes=8730
git.fetch.wide.objects=14
git.fetch.wide.extra_bytes=9909
git.fetch.wide.blobs=0
fetchtree:0 · deepen 102009.3 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=26
git.response.bytes=9489
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=9489
git.pkt.payload.max=9389
git.sideband.band1.bytes=9389
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=10
git.pack.bytes=9389
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=de930681b6073ba5b599a0f956bfbd114762ad5a
git.pack.full=10
Open pack map
fetch20028 KiB · hit26 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=28239
git.response.cache=HIT
git.response.duration_ms=26
git.fetch.wants=221258ddce0254d0660e86c5ee82385c00ec1231
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=28239
git.pkt.payload.max=28212
git.sideband.band1.bytes=28212
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://codeberg.org/dnkl/foot
Ref
refs/heads/master
Commit
c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
cf-ray
a5e249967fdbee28
Request
c1d29cfd…
Protocol
v2 · sha1
Agent
git/2.56.1-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format