-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtermio.h
231 lines (181 loc) · 4.27 KB
/
termio.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#ifndef ZAKAROUF__ZTYPES_IMP_TERMIO_H
#define ZAKAROUF__ZTYPES_IMP_TERMIO_H
#include "string.h"
/**
* Get Size of the Current Terminal
*/
void z__termio_get_term_size(z__u32 * _Nonnull x, z__u32 * _Nonnull y);
/**
* Get a key input as char (blocking)
*/
z__u8 z__termio_getchar(void);
/**
* Get a key input as char (non blocking)
*/
z__u8 z__termio_getchar_nowait(void);
/**
* Get Key Hit
*/
int z__termio_kbhit(void);
/**
* Block Terminal io
*/
void z__termio_block(void);
/**
* Un-Block Terminal io
*/
void z__termio_nonblock(void);
/**
* Enable/Disable Key Echo
*/
void z__termio_echo(int tog);
/**
* Put Char on terminal
*/
void z__termio_putchar(z__char c);
/**
* Put a string on terminal
*/
void z__termio_putString(const z__String * _Nonnull str);
#ifdef Z__IMPLEMENTATION
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include "std/primitives.h"
#include "string.h"
void z__termio_nonblock(void)
{
struct termios ttystate;
//get the terminal state
tcgetattr(STDIN_FILENO, &ttystate);
//turn off canonical mode
ttystate.c_lflag &= ~ICANON;
//minimum of number input read.
ttystate.c_cc[VMIN] = 1;
//set the terminal attributes.
tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
}
void z__termio_block(void)
{
struct termios ttystate;
//get the terminal state
tcgetattr(STDIN_FILENO, &ttystate);
//turn on canonical mode
ttystate.c_lflag |= ICANON;
//set the terminal attributes.
tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
}
void z__termio_echo(int tog)
{
struct termios ttystate;
tcgetattr(STDIN_FILENO, &ttystate);
if(tog) ttystate.c_lflag |= ECHO;
else ttystate.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
}
int z__termio_kbhit(void)
{
struct timeval tv;
fd_set fds;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
return FD_ISSET(STDIN_FILENO, &fds);
}
z__u8 z__termio_getchar(void)
{
char buf = 0;
struct termios old = {0};
fflush(stdout);
tcgetattr(0, &old);
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &old);
read(0, &buf, 1);
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
tcsetattr(0, TCSADRAIN, &old);
return buf;
}
#define k(n) z__termio_key_##n
enum z__termio_key {
k(null) = 0
, k(soh)
, k(space) = 32
, k(tilde) = 126
, k(delete) = 127
, k(arrow_up) = 128
};
z__u32 z__termio_getkey(void)
{
char buf = 0;
struct termios old = {0};
fflush(stdout);
tcgetattr(0, &old);
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &old);
read(0, &buf, 1);
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
tcsetattr(0, TCSADRAIN, &old);
return buf;
}
z__u8 z__termio_getchar_nowait(void)
{
struct termios ttystate;
//get the terminal state
tcgetattr(STDIN_FILENO, &ttystate);
//turn off canonical mode
ttystate.c_lflag &= ~ICANON;
//minimum of number input read.
ttystate.c_cc[VMIN] = 1;
//set the terminal attributes.
tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
z__u8 c = 0;
if(z__termio_kbhit()) {
c = fgetc(stdin);
}
//get the terminal state
tcgetattr(STDIN_FILENO, &ttystate);
//turn on canonical mode
ttystate.c_lflag |= ICANON;
//set the terminal attributes.
tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
return c;
}
void z__termio_get_term_size(z__u32 *x, z__u32 *y)
{
struct winsize ws;
ioctl(1, TIOCGWINSZ, &ws);
*x = ws.ws_col;
*y = ws.ws_row;
}
void z__termio_getln_b(z__String *str)
{
while(true) {
int key = z__termio_getkey();
if (key == '\n') { return; }
z__String_pushChar(str, key);
}
}
z__String z__termio_getln(void)
{
z__String str = z__String_new(64);
z__termio_getln_b(&str);
return str;
}
void z__termio_putString(z__String const * const str)
{
fwrite(str->data, sizeof *str->data, str->len, stdout);
}
#endif //Z__IMPLEMENTATION
#endif