summaryrefslogtreecommitdiff
path: root/src/tty.c
blob: 0a7b7fca0205a571df983217241fc09c21cdf1c5 (plain)
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
231
232

/* --------------------------------------------------------------------
   tty code for my 68HC11 devtools project

   Copyright (C) 2004 Dominic Radermacher <dominic.radermacher@gmail.com>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
         
   http://www.apache.org/licenses/LICENSE-2.0
                
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
   -------------------------------------------------------------------- */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>		/* strncpy() */
#include <termios.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/types.h>

#include "tty.h"

struct _tty_keyseq_struct {
	char *seq;
	int code;
} tty_keyseqs[] = {
	{"[[A", KEY_F1}, {"OP", KEY_F1},
	{"[[B", KEY_F2}, {"OQ", KEY_F2},
	{"[[C", KEY_F3}, {"OR", KEY_F3},
	{"[[D", KEY_F4}, {"OS", KEY_F4},
	{"[[E", KEY_F5}, {"[15~", KEY_F5},
	{"[17~", KEY_F6},
	{"[18~", KEY_F7},
	{"[19~", KEY_F8},
	{"[20~", KEY_F9},
	{"[21~", KEY_F10},
	{"[23~", KEY_F11},
	{"[24~", KEY_F12},
	{"[25~", KEY_F13},
	{"[A", KEY_C_UP},
	{"[B", KEY_C_DOWN},
	{"[C", KEY_C_RIGHT},
	{"[D", KEY_C_LEFT},
	{"[2~", KEY_INS},
	{"[3~", KEY_DEL},
	{"[1~", KEY_HOME}, {"[H", KEY_HOME},
	{"[5~", KEY_PG_UP},
	{"[6~", KEY_PG_DOWN},
	{"[4~", KEY_END}, {"[F", KEY_END},
	{"[P", KEY_BREAK},
	{0, 0}
};

/* --------------------------------------------------------------------
	Function	: Initialisation of the tty.
	Input value	: Structure of screen.
	Output value	: Structure of screen with height and width
   -------------------------------------------------------------------- */

int tty_init(void)
{
	struct termios tios;

	if(tcgetattr(0, &tios) == 0) {
		tios.c_lflag &= ~ICANON;	/* Turn char buffering off */
		tios.c_lflag &= ~ECHO;		/* Turn char displaying off */
		tios.c_cflag |= CRTSCTS;
		if(tcsetattr(0, 0, &tios) != 0) {
			fprintf(stderr, "TTY Error\n");
		}
	}
	printf("\033[r\033[999;999f\033[6n");
	scanf("\033[%i;%i", &(my_scr.height), &(my_scr.width));
	return 0;
}

int _tty_getkeycode(int t)
{
	fd_set fds;
	struct timeval TimeOut;
	unsigned char b[1];
	TimeOut.tv_sec = t;
	TimeOut.tv_usec = 1000;		/* Mikrosekunden !! */
  
	FD_ZERO(&fds);
	FD_SET(0, &fds);		/* 0 means socket stdin */
	if(select(1, &fds, NULL, NULL, &TimeOut) != 0) {
		read(0, b, 1);
		return 0x0000+b[0];
	}	/* else ... */
	return KEY_TIMEOUT;
}

/* ---------------------------------------------------------------------
	Function	: Wait for key until timeout, if a key is pressed
			  return its code.
	Input value	: Timeout in seconds.
	Output value	: 0x00xx, xx=keycode, if normal keycode
			  0x01xx  xx=keycode with ALT modifier
			  0x02xx  xx=special ESC sequence code
			  0xffff  if timeout occurs.
   --------------------------------------------------------------------- */

int tty_getkey(void)
{
	int count, k;
	char b[8];

	if((k=_tty_getkeycode(1)) == KEY_TIMEOUT) {
		return KEY_TIMEOUT;
	} else if(k != 0x1b) {
		return k;
	} else {
		for(count=0; ((k=_tty_getkeycode(0)) != KEY_TIMEOUT); count++) {
			if(count < 7) {
				b[count]=(k & 0xff);
			}
		}
		if(count == 0) {
			return 0x001b;
		} else if(count == 1) {		/* ESC <key> = Alt + <key> */
			return 0x0100 + b[0];
		}				/* all others must be looked up */
		for(k=0; tty_keyseqs[k].seq != NULL; k++) {
			if(strncmp(tty_keyseqs[k].seq, b, count) == 0) {
				return tty_keyseqs[k].code;
			}
		}
//		printf("%i ESC %02x %02x %02x %02x\n", count, b[0], b[1], b[2], b[3]);
		return KEY_UNKNOWN;
	}
}

/* --------------------------------------------------------------------
	Function	: Turns display of chars on or off.
	Input value	: Mode, can be 0 (off) or 1 (on)
	Output value	: None.
   -------------------------------------------------------------------- */

int tty_setechomode(int mode)
{
	struct termios tios;

	if(tcgetattr(0, &tios) != 0) {
		return -1;
	}
	if(mode == 0) {
		tios.c_lflag &= ~ECHO;	/* Char displaying off */
	} else {
		tios.c_lflag |= ECHO;	/* Char displaying on */
	}
	if(tcsetattr(0, 0, &tios) != 0) {
		return -1;
	}
	return 0;
}

inline int tty_width()
{
	return my_scr.width;
}

inline int tty_height()
{
	return my_scr.height;
}

inline void tty_cls(void)
{
	printf("\033[2J");
}

inline void tty_clreol(void)
{
	printf("\033[0K");	/* clear from cursor to end of line */
}

void tty_gotoxy(int x, int y)
{
	if(x < 1) x=1;
	if(x > my_scr.width) x=my_scr.width;
	if(y < 1) y=1;
	if(y > my_scr.height) y=my_scr.height;
	printf("\033[%i;%if", y, x);
}

int tty_setscrollregion(int from, int to)
{
	if((from < 1) || (from >= my_scr.height) || (to < from) || (to > my_scr.height)) {
		return -1;
	}
	printf("\033[%i;%ir", from, to);	/* Set scrolling region */
	return 0;
}

void tty_setmode(int mode)
{
	printf("\033[%im", mode);
}

void tty_setcolor(int bg, int fg)
{
	if((bg < 0) || (bg > 7) || (fg < 0) || (fg > 7)) {
		return;
	}
	printf("\033[%i;%im", 40+bg, 30+fg);
}

inline void tty_savecursorpos(void)
{
	printf("\0337");			/* Save cursor position */
}

inline void tty_restorecursorpos(void)
{
	printf("\0338");			/* Restore cursor */
}

void tty_reset(void)
{
//	tty_setscrollregion(1, my_scr.height);
	tty_setechomode(1);			/* is NOT included in full reset */
	printf("\033c");			/* Full reset */
}