-
Notifications
You must be signed in to change notification settings - Fork 17
/
console.c
357 lines (288 loc) · 5.49 KB
/
console.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
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
/* CONSOLE INTERFACE */
#ifndef WIN32
#include <ctype.h>
#include <termios.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
#include "config.h"
#include "cfg.h"
#include "console.h"
#include "kbd.h"
#include "error.h"
#define SCREEN_BUFFERING
/* ALL COORDINATES START FROM 0 */
int console_ok=1;
struct termios term_setting;
int screen_width;
#ifdef SCREEN_BUFFERING
char *screen_buffer;
int screen_buffer_pos=0;
int screen_buffer_size;
#endif
#ifdef HAVE_INLINE
inline void
#else
void
#endif
my_putc(char c)
{
#ifdef SCREEN_BUFFERING
if (screen_buffer_pos+1>screen_buffer_size)
{
screen_buffer_size<<=1;
screen_buffer=mem_realloc(screen_buffer,screen_buffer_size);
if (!screen_buffer){c_shutdown();fprintf(stderr,"Error: Can't reallocate screen buffer.\n");EXIT(1);}
}
screen_buffer[screen_buffer_pos]=c;
screen_buffer_pos++;
#else
fputc(c,stdout);
#endif
}
#ifdef HAVE_INLINE
inline
#endif
void my_print_l(char *str, int l)
{
#ifdef SCREEN_BUFFERING
if (screen_buffer_pos+l>screen_buffer_size)
{
while(screen_buffer_pos+l>screen_buffer_size)
screen_buffer_size<<=1;
screen_buffer=mem_realloc(screen_buffer,screen_buffer_size);
if (!screen_buffer){c_shutdown();fprintf(stderr,"Error: Can't reallocate screen buffer.\n");EXIT(1);}
}
memcpy(screen_buffer+screen_buffer_pos,str,l);
screen_buffer_pos+=l;
#else
fputs(str,stdout);
#endif
}
#ifdef HAVE_INLINE
inline
#endif
void my_print(char *str)
{
my_print_l(str,
#ifdef SCREEN_BUFFERING
strlen(str)
#else
0
#endif
);
}
void c_refresh(void)
{
#ifdef SCREEN_BUFFERING
ssize_t ret = 0;
ret = write(1,screen_buffer,screen_buffer_pos);
screen_buffer_pos=0;
#else
fflush(stdout);
#endif
}
/* initialize console */
void c_init(int w,int h)
{
struct termios t;
console_ok=0;
#ifdef SCREEN_BUFFERING
screen_buffer_size=w*h;
if (!(screen_buffer=mem_alloc(screen_buffer_size))){fprintf(stderr,"Error: Not enough memory for screen buffer.\n");EXIT(1);}
#else
w=w;h=h;
#endif
tcgetattr(0,&term_setting);
memcpy(&t, &term_setting, sizeof(struct termios));
t.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
t.c_oflag &= ~OPOST;
t.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
t.c_cflag &= ~(CSIZE|PARENB);
t.c_cflag |= CS8;
tcsetattr(0, TCSANOW, &t);
kbd_init();
c_cls();
my_print("\033[;H");
c_refresh();
}
/* close console */
void c_shutdown(void)
{
kbd_close();
tcsetattr(0,TCSANOW,&term_setting);
c_cursor(C_NORMAL);
c_setcolor(7);
c_cls();
my_print("\033[;H");
c_refresh();
#ifdef SCREEN_BUFFERING
mem_free(screen_buffer);
#endif
console_ok=1;
}
/* move cursor to [x,y] */
void c_goto(int x,int y)
{
char txt[16];
#ifdef TRI_D
if (TRI_D_ON&&tri_d)x+=screen_width;
#endif
snprintf(txt,16,"\033[%d;%dH",y+1,x+1);
my_print(txt);
}
/* set complete foreground color */
void c_setcolor(unsigned char a)
{
char txt[16];
a&=15;
snprintf(txt,16,"\033[%c;%dm",(a>>3)+'0',30+(a&7));
my_print(txt);
}
/* set complete foreground color and background color */
void c_setcolor_bg(unsigned char fg,unsigned char bg)
{
char txt[16];
fg&=15;
if (!(fg>>3)&&!bg) snprintf(txt,16,"\033[%c;%dm",(fg>>3)+'0',30+(fg&7));
else snprintf(txt,16,"\033[%c;%d;%dm",(fg>>3)+'0',40+(bg&7),30+(fg&7));
my_print(txt);
}
/* set background color */
void c_setbgcolor(unsigned char a)
{
char txt[16];
snprintf(txt,16,"\033[%dm",40+(a&7));
my_print(txt);
}
/* set highlight color */
void c_sethlt(unsigned char a)
{
my_print(a?"\033[1m":"\033[0m");
}
/* set highlight and background color */
void c_sethlt_bg(unsigned char hlt,unsigned char bg)
{
char txt[16];
snprintf(txt,16,"\033[%d;%dm",hlt&1,40+(bg&7));
my_print(txt);
}
/* set 3 bit foreground color */
void c_setcolor_3b(unsigned char a)
{
char txt[8];
snprintf(txt,sizeof(txt),"\033[%dm",30+(a&7));
my_print(txt);
}
/* set 3 bit foreground color and background color*/
void c_setcolor_3b_bg(unsigned char fg,unsigned char bg)
{
char txt[16];
snprintf(txt,16,"\033[%d;%dm",30+(fg&7),40+(bg&7));
my_print(txt);
}
/* print on the cursor position */
void c_print(char * text)
{
my_print(text);
}
void c_print_l(char * text, int len)
{
my_print_l(text, len);
}
/* print char on the cursor position */
void c_putc(char c)
{
my_putc(c);
}
/* clear the screen */
void c_cls(void)
{
my_print("\033[2J");
}
/* clear rectangle on the screen */
/* presumtions: x2>=x1 && y2>=y1 */
void c_clear(int x1,int y1,int x2,int y2)
{
char *line;
int y;
line=mem_alloc(x2-x1+1);
if (!line){fprintf(stderr,"Error: Not enough memory.\n");EXIT(1);}
for (y=0;y<x2-x1+1;y++)
line[y]=' ';
line[y]=0;
for(y=y1;y<=y2;y++)
{
c_goto(x1,y);c_print(line);
}
mem_free(line);
}
void c_update_kbd(void)
{
kbd_update();
}
int c_pressed(int k)
{
return kbd_is_pressed(k);
}
int c_was_pressed(int k)
{
return kbd_was_pressed(k);
}
void c_wait_for_key(void)
{
kbd_wait_for_key();
}
/* set cursor shape */
void c_cursor(int type)
{
switch (type)
{
case C_NORMAL:
my_print("\033[?25h");
break;
case C_HIDE:
my_print("\033[?25l");
break;
}
}
/* ring the bell */
void c_bell(void)
{
my_print("\007");
}
/* get screen dimensions */
void c_get_size(int *x, int *y)
{
#ifndef __EMX__
struct winsize ws;
if (ioctl(1, TIOCGWINSZ, &ws) != -1)
{
*x=ws.ws_col;
*y=ws.ws_row;
}
else
{
*x=80;
*y=24;
}
#ifdef TRI_D
if (TRI_D_ON)
(*x)>>=1;
screen_width=*x;
#endif
return;
#else
int s[2];
_scrsize(s);
*x = s[0];
*y = s[1];
#endif
}
#else
#include "winconsole.c"
#endif