dte test coverage


Directory: ./
File: src/terminal/key.h
Date: 2025-07-13 15:27:15
Exec Total Coverage
Lines: 2 2 100.0%
Functions: 1 1 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 #ifndef TERMINAL_KEY_H
2 #define TERMINAL_KEY_H
3
4 #include <stddef.h>
5 #include <stdint.h>
6 #include "util/macros.h"
7
8 enum {
9 // Buffer size for keycode_to_str()
10 KEYCODE_STR_BUFSIZE = 32 // next_pow2(sizeof("QUERY REPLY; 0x12345678"))
11 };
12
13 // NOLINTNEXTLINE(readability-enum-initial-value,cert-int09-c)
14 enum {
15 KEY_NONE = 0,
16 KEY_TAB = '\t',
17 KEY_ENTER = '\n',
18 KEY_SPACE = ' ',
19
20 // This is the maximum Unicode codepoint allowed by RFC 3629.
21 // When stored in a 32-bit integer, it only requires the first
22 // 21 low-order bits, leaving 11 high-order bits available to
23 // be used as bit flags.
24 KEY_UNICODE_MAX = 0x10FFFF, // UNICODE_MAX_VALID_CODEPOINT
25 KEY_MASK = 0x1FFFFF, // (1 << 21) - 1
26
27 // In addition to the 11 unused, high-order bits, there are also
28 // 983,042 unused values in the range from KEY_UNICODE_MAX + 1 to
29 // KEY_MASK, which can be used to represent special keys.
30 KEY_SPECIAL_MIN = KEY_UNICODE_MAX + 1,
31
32 // Note: these must be kept in sync with the array of names in key.c
33 KEY_ESCAPE = KEY_SPECIAL_MIN,
34 KEY_BACKSPACE,
35 KEY_INSERT,
36 KEY_DELETE,
37 KEY_HOME,
38 KEY_END,
39 KEY_PAGE_UP,
40 KEY_PAGE_DOWN,
41 KEY_UP,
42 KEY_DOWN,
43 KEY_RIGHT,
44 KEY_LEFT,
45 KEY_BEGIN, // Keypad '5'
46 KEY_SCROLL_LOCK,
47 KEY_PRINT_SCREEN,
48 KEY_PAUSE,
49 KEY_MENU,
50 KEY_F1,
51 KEY_F2,
52 KEY_F3,
53 KEY_F4,
54 KEY_F5,
55 KEY_F6,
56 KEY_F7,
57 KEY_F8,
58 KEY_F9,
59 KEY_F10,
60 KEY_F11,
61 KEY_F12,
62 KEY_F13,
63 KEY_F14,
64 KEY_F15,
65 KEY_F16,
66 KEY_F17,
67 KEY_F18,
68 KEY_F19,
69 KEY_F20,
70
71 KEY_SPECIAL_MAX = KEY_F20,
72 NR_SPECIAL_KEYS = KEY_SPECIAL_MAX - KEY_SPECIAL_MIN + 1,
73
74 // In-band signalling for non-key events
75 KEYCODE_DETECTED_PASTE = KEY_SPECIAL_MAX + 1,
76 KEYCODE_BRACKETED_PASTE,
77 KEY_IGNORE,
78
79 // Modifier bit flags (as described above)
80 // See also: decode_modifiers()
81 KEYCODE_MODIFIER_OFFSET = 21,
82 MOD_SHIFT = 1 << KEYCODE_MODIFIER_OFFSET,
83 MOD_META = 2 << KEYCODE_MODIFIER_OFFSET,
84 MOD_CTRL = 4 << KEYCODE_MODIFIER_OFFSET,
85 MOD_SUPER = 8 << KEYCODE_MODIFIER_OFFSET,
86 MOD_HYPER = 16 << KEYCODE_MODIFIER_OFFSET,
87 MOD_MASK = MOD_SHIFT | MOD_META | MOD_CTRL | MOD_SUPER | MOD_HYPER,
88
89 // If this bit is set, all other bits correspond to TermFeatureFlags.
90 // This is used by the functions in query.c to communicate replies to
91 // term_read_input(), although such values are handled entirely within
92 // that function and are never present in KeyCode values it returns.
93 KEYCODE_QUERY_REPLY_BIT = 1u << 30,
94 };
95
96 typedef uint32_t KeyCode;
97
98 575 static inline KeyCode keycode_get_key(KeyCode k)
99 {
100 575 return k & KEY_MASK;
101 }
102
103 static inline KeyCode keycode_get_modifiers(KeyCode k)
104 {
105 return k & MOD_MASK;
106 }
107
108 KeyCode keycode_from_str(const char *str) NONNULL_ARGS WARN_UNUSED_RESULT;
109 size_t keycode_to_str(KeyCode key, char buf[KEYCODE_STR_BUFSIZE]) NONNULL_ARGS;
110
111 #endif
112