code / bb

Lines2.7K C1.8K Shell331 YAML273 Markdown197 make44
(139 lines)
1 //
2 // terminal.h
3 // Copyright 2020 Bruce Hill
4 // Released under the MIT License
5 //
6 // Definitions of some basic terminal stuff, like reading keys and some
7 // terminal escape sequences.
8 //
10 #ifndef FILE__TERMINAL_H
11 #define FILE__TERMINAL_H
13 #include <stdio.h>
14 #include <unistd.h>
16 // Maximum time in milliseconds between double clicks
17 #define DOUBLECLICK_THRESHOLD 200
19 typedef enum {
20 // ASCII chars:
21 KEY_CTRL_AT = 0x00,
22 KEY_CTRL_A,
23 KEY_CTRL_B,
24 KEY_CTRL_C,
25 KEY_CTRL_D,
26 KEY_CTRL_E,
27 KEY_CTRL_F,
28 KEY_CTRL_G,
29 KEY_CTRL_H,
30 KEY_CTRL_I,
31 KEY_CTRL_J,
32 KEY_CTRL_K,
33 KEY_CTRL_L,
34 KEY_CTRL_M,
35 KEY_CTRL_N,
36 KEY_CTRL_O,
37 KEY_CTRL_P,
38 KEY_CTRL_Q,
39 KEY_CTRL_R,
40 KEY_CTRL_S,
41 KEY_CTRL_T,
42 KEY_CTRL_U,
43 KEY_CTRL_V,
44 KEY_CTRL_W,
45 KEY_CTRL_X,
46 KEY_CTRL_Y,
47 KEY_CTRL_Z,
48 KEY_CTRL_LSQ_BRACKET,
49 KEY_CTRL_BACKSLASH,
50 KEY_CTRL_RSQ_BRACKET,
51 KEY_CTRL_CARET,
52 KEY_CTRL_UNDERSCORE,
53 KEY_SPACE,
54 // Printable chars would be here
55 KEY_BACKSPACE2 = 0x7F,
57 // Non-ascii multi-byte keys:
58 KEY_F0,
59 KEY_F1,
60 KEY_F2,
61 KEY_F3,
62 KEY_F4,
63 KEY_F5,
64 KEY_F6,
65 KEY_F7,
66 KEY_F8,
67 KEY_F9,
68 KEY_F10,
69 KEY_F11,
70 KEY_F12,
71 KEY_INSERT,
72 KEY_DELETE,
73 KEY_HOME,
74 KEY_END,
75 KEY_PGUP,
76 KEY_PGDN,
77 KEY_ARROW_UP,
78 KEY_ARROW_DOWN,
79 KEY_ARROW_LEFT,
80 KEY_ARROW_RIGHT,
81 MOUSE_LEFT_PRESS,
82 MOUSE_RIGHT_PRESS,
83 MOUSE_MIDDLE_PRESS,
84 MOUSE_LEFT_DRAG,
85 MOUSE_RIGHT_DRAG,
86 MOUSE_MIDDLE_DRAG,
87 MOUSE_LEFT_RELEASE,
88 MOUSE_RIGHT_RELEASE,
89 MOUSE_MIDDLE_RELEASE,
90 MOUSE_LEFT_DOUBLE,
91 MOUSE_RIGHT_DOUBLE,
92 MOUSE_MIDDLE_DOUBLE,
93 MOUSE_WHEEL_RELEASE,
94 MOUSE_WHEEL_PRESS,
95 } bkey_t;
97 #define MOD_BITSHIFT 9
98 #define MOD_META (1 << (MOD_BITSHIFT + 0))
99 #define MOD_CTRL (1 << (MOD_BITSHIFT + 1))
100 #define MOD_ALT (1 << (MOD_BITSHIFT + 2))
101 #define MOD_SHIFT (1 << (MOD_BITSHIFT + 3))
103 // Overlapping key codes:
104 #define KEY_CTRL_BACKTICK 0x00 /* clash with ^@ */
105 #define KEY_CTRL_2 0x00 /* clash with ^@ */
106 #define KEY_BACKSPACE 0x08 /* clash with ^H */
107 #define KEY_TAB 0x09 /* clash with ^I */
108 #define KEY_ENTER 0x0D /* clash with ^M */
109 #define KEY_ESC 0x1B /* clash with ^[ */
110 #define KEY_CTRL_3 0x1B /* clash with ^[ */
111 #define KEY_CTRL_4 0x1C /* clash with ^\ */
112 #define KEY_CTRL_5 0x1D /* clash with ^] */
113 #define KEY_CTRL_TILDE 0x1E /* clash with ^^ */
114 #define KEY_CTRL_6 0x1E /* clash with ^^ */
115 #define KEY_CTRL_7 0x1F /* clash with ^_ */
116 #define KEY_CTRL_SLASH 0x1F /* clash with ^_ */
117 #define KEY_SPACE 0x20
118 #define KEY_BACKSPACE2 0x7F
119 #define KEY_CTRL_8 0x7F /* clash with 'BACKSPACE2' */
121 // Terminal escape sequences:
122 #define T_WRAP "7"
123 #define T_SHOW_CURSOR "25"
124 #define T_MOUSE_XY "1000"
125 #define T_MOUSE_CELL "1002"
126 #define T_MOUSE_SGR "1006"
127 #define T_ALT_SCREEN "1049"
128 #define T_ON(opt) "\033[?" opt "h"
129 #define T_OFF(opt) "\033[?" opt "l"
131 #define move_cursor(f, x, y) fprintf((f), "\033[%d;%dH", (int)(y) + 1, (int)(x) + 1)
132 #define move_cursor_col(f, x) fprintf((f), "\033[%d`", (int)(x) + 1)
134 int bgetkey(FILE *in, int *mouse_x, int *mouse_y);
135 char *bkeyname(int key, char *buf);
136 int bkeywithname(const char *name);
138 #endif
139 // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0