Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/extern/isocline/src/tty.h
2727 views
1
/* ----------------------------------------------------------------------------
2
Copyright (c) 2021, Daan Leijen
3
This is free software; you can redistribute it and/or modify it
4
under the terms of the MIT License. A copy of the license can be
5
found in the "LICENSE" file at the root of this distribution.
6
-----------------------------------------------------------------------------*/
7
#pragma once
8
#ifndef IC_TTY_H
9
#define IC_TTY_H
10
11
#include "common.h"
12
13
//-------------------------------------------------------------
14
// TTY/Keyboard input
15
//-------------------------------------------------------------
16
17
// Key code
18
typedef uint32_t code_t;
19
20
// TTY interface
21
struct tty_s;
22
typedef struct tty_s tty_t;
23
24
25
ic_private tty_t* tty_new(alloc_t* mem, int fd_in);
26
ic_private void tty_free(tty_t* tty);
27
28
ic_private bool tty_is_utf8(const tty_t* tty);
29
ic_private bool tty_start_raw(tty_t* tty);
30
ic_private void tty_end_raw(tty_t* tty);
31
ic_private code_t tty_read(tty_t* tty);
32
ic_private bool tty_read_timeout(tty_t* tty, long timeout_ms, code_t* c );
33
34
ic_private void tty_code_pushback( tty_t* tty, code_t c );
35
ic_private bool code_is_ascii_char(code_t c, char* chr );
36
ic_private bool code_is_unicode(code_t c, unicode_t* uchr);
37
ic_private bool code_is_virt_key(code_t c );
38
39
ic_private bool tty_term_resize_event(tty_t* tty); // did the terminal resize?
40
ic_private bool tty_async_stop(const tty_t* tty); // unblock the read asynchronously
41
ic_private void tty_set_esc_delay(tty_t* tty, long initial_delay_ms, long followup_delay_ms);
42
43
// shared between tty.c and tty_esc.c: low level character push
44
ic_private void tty_cpush_char(tty_t* tty, uint8_t c);
45
ic_private bool tty_cpop(tty_t* tty, uint8_t* c);
46
ic_private bool tty_readc_noblock(tty_t* tty, uint8_t* c, long timeout_ms);
47
ic_private code_t tty_read_esc(tty_t* tty, long esc_initial_timeout, long esc_timeout); // in tty_esc.c
48
49
// used by term.c to read back ANSI escape responses
50
ic_private bool tty_read_esc_response(tty_t* tty, char esc_start, bool final_st, char* buf, ssize_t buflen );
51
52
53
//-------------------------------------------------------------
54
// Key codes: a code_t is 32 bits.
55
// we use the bottom 24 (nah, 21) bits for unicode (up to x0010FFFF)
56
// The codes after x01000000 are for virtual keys
57
// and events use x02000000.
58
// The top 4 bits are used for modifiers.
59
//-------------------------------------------------------------
60
61
static inline code_t key_char( char c ) {
62
// careful about signed character conversion (negative char ~> 0x80 - 0xFF)
63
return ((uint8_t)c);
64
}
65
66
static inline code_t key_unicode( unicode_t u ) {
67
return u;
68
}
69
70
71
#define KEY_MOD_SHIFT (0x10000000U)
72
#define KEY_MOD_ALT (0x20000000U)
73
#define KEY_MOD_CTRL (0x40000000U)
74
75
#define KEY_NO_MODS(k) (k & 0x0FFFFFFFU)
76
#define KEY_MODS(k) (k & 0xF0000000U)
77
78
#define WITH_SHIFT(x) (x | KEY_MOD_SHIFT)
79
#define WITH_ALT(x) (x | KEY_MOD_ALT)
80
#define WITH_CTRL(x) (x | KEY_MOD_CTRL)
81
82
#define KEY_NONE (0)
83
#define KEY_CTRL_A (1)
84
#define KEY_CTRL_B (2)
85
#define KEY_CTRL_C (3)
86
#define KEY_CTRL_D (4)
87
#define KEY_CTRL_E (5)
88
#define KEY_CTRL_F (6)
89
#define KEY_BELL (7)
90
#define KEY_BACKSP (8)
91
#define KEY_TAB (9)
92
#define KEY_LINEFEED (10) // ctrl/shift + enter is considered KEY_LINEFEED
93
#define KEY_CTRL_K (11)
94
#define KEY_CTRL_L (12)
95
#define KEY_ENTER (13)
96
#define KEY_CTRL_N (14)
97
#define KEY_CTRL_O (15)
98
#define KEY_CTRL_P (16)
99
#define KEY_CTRL_Q (17)
100
#define KEY_CTRL_R (18)
101
#define KEY_CTRL_S (19)
102
#define KEY_CTRL_T (20)
103
#define KEY_CTRL_U (21)
104
#define KEY_CTRL_V (22)
105
#define KEY_CTRL_W (23)
106
#define KEY_CTRL_X (24)
107
#define KEY_CTRL_Y (25)
108
#define KEY_CTRL_Z (26)
109
#define KEY_ESC (27)
110
#define KEY_SPACE (32)
111
#define KEY_RUBOUT (127) // always translated to KEY_BACKSP
112
#define KEY_UNICODE_MAX (0x0010FFFFU)
113
114
115
#define KEY_VIRT (0x01000000U)
116
#define KEY_UP (KEY_VIRT+0)
117
#define KEY_DOWN (KEY_VIRT+1)
118
#define KEY_LEFT (KEY_VIRT+2)
119
#define KEY_RIGHT (KEY_VIRT+3)
120
#define KEY_HOME (KEY_VIRT+4)
121
#define KEY_END (KEY_VIRT+5)
122
#define KEY_DEL (KEY_VIRT+6)
123
#define KEY_PAGEUP (KEY_VIRT+7)
124
#define KEY_PAGEDOWN (KEY_VIRT+8)
125
#define KEY_INS (KEY_VIRT+9)
126
127
#define KEY_F1 (KEY_VIRT+11)
128
#define KEY_F2 (KEY_VIRT+12)
129
#define KEY_F3 (KEY_VIRT+13)
130
#define KEY_F4 (KEY_VIRT+14)
131
#define KEY_F5 (KEY_VIRT+15)
132
#define KEY_F6 (KEY_VIRT+16)
133
#define KEY_F7 (KEY_VIRT+17)
134
#define KEY_F8 (KEY_VIRT+18)
135
#define KEY_F9 (KEY_VIRT+19)
136
#define KEY_F10 (KEY_VIRT+20)
137
#define KEY_F11 (KEY_VIRT+21)
138
#define KEY_F12 (KEY_VIRT+22)
139
#define KEY_F(n) (KEY_F1 + (n) - 1)
140
141
#define KEY_EVENT_BASE (0x02000000U)
142
#define KEY_EVENT_RESIZE (KEY_EVENT_BASE+1)
143
#define KEY_EVENT_AUTOTAB (KEY_EVENT_BASE+2)
144
#define KEY_EVENT_STOP (KEY_EVENT_BASE+3)
145
146
// Convenience
147
#define KEY_CTRL_UP (WITH_CTRL(KEY_UP))
148
#define KEY_CTRL_DOWN (WITH_CTRL(KEY_DOWN))
149
#define KEY_CTRL_LEFT (WITH_CTRL(KEY_LEFT))
150
#define KEY_CTRL_RIGHT (WITH_CTRL(KEY_RIGHT))
151
#define KEY_CTRL_HOME (WITH_CTRL(KEY_HOME))
152
#define KEY_CTRL_END (WITH_CTRL(KEY_END))
153
#define KEY_CTRL_DEL (WITH_CTRL(KEY_DEL))
154
#define KEY_CTRL_PAGEUP (WITH_CTRL(KEY_PAGEUP))
155
#define KEY_CTRL_PAGEDOWN (WITH_CTRL(KEY_PAGEDOWN)))
156
#define KEY_CTRL_INS (WITH_CTRL(KEY_INS))
157
158
#define KEY_SHIFT_TAB (WITH_SHIFT(KEY_TAB))
159
160
#endif // IC_TTY_H
161
162