-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcodes.c
63 lines (55 loc) · 1.69 KB
/
codes.c
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
/*************************************************************************\
| Title: codes.c
| Author: Peter Sussman
| Description: Modified from original codes.c by Chris Hall. A lightweight
| interface for chorded interaction directly through a UNIX
| terminal.
\*************************************************************************/
#include <unistd.h> /* tcgetattr and tcsetattr */
#include <stdio.h>
#include <sys/ioctl.h> /* TCIOCGWINSZ */
#include <string.h> /* strlen */
#include "codes.h"
/** Terminal operations **/
int t_getstate(tstate *state){
if (tcgetattr(1, state)) {
return -1;
}
return 0;
}
tstate t_initstate(const tstate *state){
tstate nstate = *state;
nstate.c_lflag = 0; /* set non-canonical */
nstate.c_cc[VTIME] = 0; /* set timeout */
nstate.c_cc[VMIN] = 1; /* set read to return after it has one char available */
return nstate;
}
int t_setstate(const tstate *state){
if (tcsetattr(1, TCSANOW, state)) {
return -1;
}
return 0;
}
int t_clear() {
return fputs("[2J", stdout);
}
int t_getwidth() {
/* do I need to take TCIOCGSIZE into account? if so http://www.linuxquestions.org/questions/programming-9/get-width-height-of-a-terminal-window-in-c-810739/ */
struct winsize ts;
if (ioctl(0, TIOCGWINSZ, &ts)) {
return -1;
}
return ts.ws_col;
}
int t_getheight() {
/* do I need to take TCIOCGSIZE into account? if so http://www.linuxquestions.org/questions/programming-9/get-width-height-of-a-terminal-window-in-c-810739/ */
struct winsize ts;
if(ioctl(0, TIOCGWINSZ, &ts)) {
return -1;
}
return ts.ws_row;
}
int t_read(char *c, size_t len) {
memset(c, 0, len*sizeof(char));
return read(0, c, len);
}