summaryrefslogtreecommitdiff
path: root/libterm/term.c
blob: 81d967e26fb694ab8d2d7e9d89b7bdea88c7896d (plain) (blame)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "term.h"

#include "debug.h"

#define T_ESC "\x1b"

//setup initial terminal stuff
//this coulc act differently, becouse allways there is 
//different terminal setting that should be default
int term_init( term_screen *term )
{
	int ret=0;

	memset( term, 0, sizeof( term_screen ));

	if ( !isatty(STDIN_FILENO) )
	{
		ERROR("isatty failed\n")
		goto exit_error;
	}
	//!!!!!!
	//register atexit

	term->ifd = STDIN_FILENO;
	term->ofd = STDOUT_FILENO;

	//if you whant raw mode then you should set it man
	if ( tcgetattr( term->ifd, &term->orig_i ) == -1 ) goto exit_error;
	term->raw_i = term->orig_i;
	if ( tcgetattr( term->ofd, &term->orig_o ) == -1 ) goto exit_error;
	term->raw_o = term->orig_o;

	term->mode = SCREEN_MODE_80x25; 

	return ret;

exit_error:
	errno = ENOTTY;
	return -1;
}

//set terminal speed return 0 if OK and 1 if not
//im trust to input arguments that they are ok
int term_set_speed( term_screen *ts, speed_t speed)
{
	int ret = cfsetospeed( &ts->raw_o, speed );
	ret = tcsetattr( ts->ofd, TCSANOW, &ts->raw_o );
	if ( ret != 0 )
		return 1;

	//if baudrate set to there then input speed same as 
	//output speed
	cfsetispeed( &ts->raw_i, B0);
	ret = tcsetattr( ts->ifd, TCSANOW, &ts->raw_i );
	if ( ret != 0 )
		return 1;
	return 0;
}

//get maximal number of columns setting up cursor to 999 column
//and getting on with place terminal have placed cursor and getting
//column on with terminal putted cursor
int term_get_maxcol( term_screen *ts )
{
	int ret=-1;
	int orig_c;
	int cur_c;

	/* get initial cursor position */
	if ( (orig_c = term_cur_get_c( ts )) == -1 )
		goto exit_error;


	/* go to right marging and get position */
	if ( write( ts->ofd, T_ESC "[999C", 6 ) != 6 )
		goto exit_error;
	if ( (cur_c = term_cur_get_c( ts )) == -1 )
		goto exit_error;
	ret = cur_c;

	/* restore position */
	{
		char buf[32];
		snprintf( buf, 32, T_ESC "[" "%d" "D", cur_c-orig_c);
		write( ts->ofd, buf, strlen(buf) );
	}

	return ret;

exit_error:
	return -1;

}

//try to setup far away line after that read position where
//terminal have putted cursor and read line of that postion
int term_get_maxrow( term_screen *ts )
{
	int ret=-1;
	int orig_r;
	int cur_r;

	/* get initial cursor position */
	if ( (orig_r = term_cur_get_r( ts )) == -1 )
		goto exit_error;


	/* go to right marging and get position */
	if ( write( ts->ofd, T_ESC "[999B", 6 ) != 6 )
		goto exit_error;
	if ( (cur_r = term_cur_get_r( ts )) == -1 )
		goto exit_error;
	ret = cur_r;

	/* restore position */
	{
		char buf[32];
		snprintf( buf, 32, T_ESC "[" "%d" "A", cur_r-orig_r);
		write( ts->ofd, buf, strlen(buf) );
	}

	return ret;

exit_error:
	return -1;
}



int term_cur_get_c( term_screen *ts )
{
	unsigned int i;
	int row, col;
	char buf[32];
	
	if ( write( ts->ofd, "\x1b[6n", 4 ) != 4 ) goto exit_error;
	
	i = 0;
	while (i < sizeof(buf)-1)
	{
		if ( read( ts->ifd, buf+i,1 ) != 1 ) break;
		if (buf[i] == 'R') break;
		i++;
	}
	buf[i] = '\0';
	
	/* Parse terminal response */
	if ( buf[0] != '\x1b' || buf[1] != '[' ) goto exit_error;
	if ( sscanf( buf+2, "%d;%d", &row, &col) != 2 ) goto exit_error;

	return col;
exit_error:
	return -1;
}


int term_cur_get_r( term_screen *ts )
{
	unsigned int i;
	int row, col;
	char buf[32];
	
	if ( write( ts->ofd, "\x1b[6n", 4 ) != 4 ) goto exit_error;
	
	i = 0;
	while (i < sizeof(buf)-1)
	{
		if ( read( ts->ifd, buf+i,1 ) != 1 ) break;
		if (buf[i] == 'R') break;
		i++;
	}
	buf[i] = '\0';
	
	/* Parse terminal response */
	if ( buf[0] != '\x1b' || buf[1] != '[' ) goto exit_error;
	if ( sscanf( buf+2, "%d;%d", &row, &col) != 2 ) goto exit_error;

	return row;
exit_error:
	return -1;
}

//set cursor column position
int term_cur_set_c( term_screen *ts, unsigned int pc )
{
	int ret = 0;
	int cur_r;

	/* go to right marging and get position */
	if ( (cur_r = term_cur_get_r( ts )) == -1 )
		goto exit_error;
	/* set position */
	{
		char buf[32];
		int l;

		snprintf( buf, 32, T_ESC "[%d;%dH", cur_r, pc);
		l = strlen( buf );
		if ( write( ts->ofd, buf, l ) != l)
			goto exit_error;
	}

	return ret;

exit_error:
	return -1;
}

//set cursor row/line position
int term_cur_set_r( term_screen *ts, unsigned int pr )
{
	int ret = 0;
	int cur_c;

	/* go to right marging and get position */
	if ( (cur_c = term_cur_get_c( ts )) == -1 )
		goto exit_error;
	/* set position */
	{
		char buf[32];
		int l;

		snprintf( buf, 32, T_ESC "[%d;%dH", pr, cur_c);
		l = strlen( buf );
		if ( write( ts->ofd, buf, l ) != l)
			goto exit_error;
	}

	return ret;

exit_error:
	return -1;
}

int term_cur_set_cr( term_screen *ts, unsigned int pc, unsigned int pr )
{
	int ret = 0;

	/* set position */
	{
		char buf[32];
		int l;

		snprintf( buf, 32, T_ESC "[%d;%dH", pr, pc);
		l = strlen( buf );
		if ( write( ts->ofd, buf, l ) != l)
			goto exit_error;
	}
	return ret;

exit_error:
	return -1;

}


//clean terminal with escape command 
int term_clr_scr( term_screen *ts )
{
	int ret = 0;

	if ( write( ts->ofd, T_ESC "[H" T_ESC "[2J", 7 ) <= 0 ){};

	return ret;
}

//set terminal default input/output behavior
int term_set_raw_mode( term_screen *ts )
{
	int ret = 0;

	if ( tcgetattr( ts->ifd, &ts->orig_i ) == -1 )
	{
		ERROR("Cannot get input terminal attributes\n");
		goto exit_error;
	}

	ts->raw_i = ts->orig_i; /* modify the original mode */
	/* input modes: no break, no CR to NL, no parity check, no strip char,
	 * no start/stop output control. */
	ts->raw_i.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
	/* output modes - disable post raw */
	ts->raw_i.c_oflag &= ~(OPOST);
	/* control modes - set 8 bit chars */
	ts->raw_i.c_cflag |= (CS8);
	/* local modes - choing off, canonical off, no extended functions,
	 * no signal chars (^Z,^C) */
	ts->raw_i.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
	/* control chars - set return condition: min number of bytes and timer.
	 * We want read to return every single byte, without timeout. */
	ts->raw_i.c_cc[VMIN] = 1; 
	ts->raw_i.c_cc[VTIME] = 0; /* 1 byte, no timer */

	/* put terminal in raw mode after flushing */
	if (tcsetattr( ts->ifd, TCSAFLUSH, &ts->raw_i) < 0)
	{
		ERROR("Cannot set new terminal input attribures\n");
		goto exit_error;
	}

	return ret;

exit_error:
	errno = ENOTTY;
	return -1;

}


//if there is no mode with some rows/columns , then just show that no mode setet
// up and user should decide by his own what to do
int term_mode_rows( term_screen *ts )
{
	int ret = -1;

	if ( ts == NULL) return -1;

	switch ( ts->mode )
	{
		//---------------------
		case SCREEN_MODE_80x25:
			ret = 25;
			break;
		//--------------------
		case SCREEN_MODE_NONE:
		default:
			ret = -1;
	}

	return ret;
}


//if there is no mode with some rows/columns , then just show that no mode setet
// up and user should decide by his own what to do
int term_mode_columns( term_screen *ts )
{
	int ret = -1;

	if ( ts == NULL) return -1;

	switch ( ts->mode )
	{
		//---------------------
		case SCREEN_MODE_80x25:
			ret = 80;
			break;
		//--------------------
		case SCREEN_MODE_NONE:
		default:
			ret = -1;
	}

	return ret;
}


void term_set_orig_mode( term_screen *ts )
{
	tcsetattr( ts->ifd, TCSAFLUSH, &ts->orig_i );
}