-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconio.c
439 lines (383 loc) · 8.54 KB
/
conio.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* KallistiOS ##version##
conio.c
Copyright (C) 2002 Megan Potter
Adapted from Kosh, Copyright (C) 2000 Jordan DeLong
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <kos/thread.h>
#include <kos/sem.h>
#include <dc/maple/keyboard.h>
#include <dc/scif.h>
#include <arch/arch.h>
#include "conio.h"
/* the cursor */
conio_cursor_t conio_cursor;
/* the virtual screen */
char conio_virtscr[CONIO_NUM_ROWS][CONIO_NUM_COLS];
/* Our modes */
int conio_ttymode = CONIO_TTY_NONE, conio_inputmode = CONIO_INPUT_NONE;
int conio_theme = CONIO_THEME_PLAIN;
/* freeze/thaw mutex */
static semaphore_t ft_mutex;
/* File handle for serial tty */
file_t conio_serial_fd;
/* scroll everything up a line */
void conio_scroll() {
int i;
switch (conio_ttymode) {
case CONIO_TTY_PVR:
memmove(conio_virtscr, conio_virtscr[1], (CONIO_NUM_ROWS - 1) * CONIO_NUM_COLS);
for (i = 0; i < CONIO_NUM_COLS; i++)
conio_virtscr[CONIO_NUM_ROWS - 1][i] = ' ';
conio_cursor.row--;
break;
case CONIO_TTY_SERIAL:
scif_write_buffer((const uint8 *)"\x1b[M", 3, 1);
break;
case CONIO_TTY_STDIO:
fs_write(conio_serial_fd, (void *)"\x1b[M", 3);
break;
case CONIO_TTY_DBGIO:
dbgio_write_buffer((const uint8 *)"\x1b[M", 3);
break;
}
}
/* move the cursor back, don't scroll (we can't) */
void conio_deadvance_cursor() {
switch (conio_ttymode) {
case CONIO_TTY_PVR:
conio_cursor.col--;
if (conio_cursor.col < 0) {
if (conio_cursor.row != 0) {
conio_cursor.col = CONIO_NUM_COLS - 1;
conio_cursor.row--;
} else {
conio_cursor.col = 0;
}
}
break;
case CONIO_TTY_SERIAL:
scif_write(8);
scif_write(32);
scif_write(8);
break;
case CONIO_TTY_STDIO:
fs_write(conio_serial_fd, (void *)"\x8\x20\x8", 3);
break;
case CONIO_TTY_DBGIO:
dbgio_write(8);
dbgio_write(32);
dbgio_write(8);
break;
}
}
/* move the cursor ahead, scroll if we need to */
void conio_advance_cursor() {
switch (conio_ttymode) {
case CONIO_TTY_PVR:
conio_cursor.col++;
if (conio_cursor.col >= CONIO_NUM_COLS) {
conio_cursor.col = 0;
conio_cursor.row++;
if (conio_cursor.row >= CONIO_NUM_ROWS)
conio_scroll();
}
break;
case CONIO_TTY_SERIAL:
scif_write_buffer((const uint8 *)"\x1b[1C", 4, 1);
break;
case CONIO_TTY_STDIO:
fs_write(conio_serial_fd, (void *)"\x1b[1C", 4);
break;
case CONIO_TTY_DBGIO:
dbgio_write_buffer((const uint8 *)"\x1b[1C", 4);
break;
}
}
/* move the cursor */
void conio_gotoxy(int x, int y) {
switch (conio_ttymode) {
case CONIO_TTY_PVR:
conio_cursor.col = x;
conio_cursor.row = y;
break;
case CONIO_TTY_SERIAL: {
char tmp[256];
sprintf(tmp, "\x1b[%d;%df", x, y);
scif_write_buffer((unsigned char *)tmp, strlen(tmp), 1);
break;
}
case CONIO_TTY_STDIO: {
char tmp[256];
sprintf(tmp, "\x1b[%d;%df", x, y);
fs_write(conio_serial_fd, tmp, strlen(tmp));
break;
}
case CONIO_TTY_DBGIO: {
char tmp[256];
sprintf(tmp, "\x1b[%d;%df", x, y);
dbgio_write_buffer((unsigned char *)tmp, strlen(tmp));
break;
}
}
}
/* blocking call for a character */
int conio_getch() {
int key = -1;
uint8 b;
switch (conio_ttymode) {
case CONIO_TTY_PVR:
#ifdef GFX
while ((key = kbd_get_key()) == -1) { thd_pass(); }
#endif
break;
case CONIO_TTY_SERIAL: {
while ((key = scif_read()) == -1) { thd_pass(); }
if (key == 3)
arch_exit();
break;
}
case CONIO_TTY_STDIO: {
int i;
i = fs_read(conio_serial_fd, &b, 1);
if (i <= 0)
return -1;
key = b;
if (key == '\n')
return conio_getch();
break;
}
case CONIO_TTY_DBGIO: {
while ((key = dbgio_read()) == -1) {
thd_sleep(1000);
}
if (key == 3)
arch_exit();
break;
}
}
return key;
}
/* Check to see if a key has been pressed */
int conio_check_getch() {
int key = -1;
uint8 b;
switch (conio_ttymode) {
case CONIO_TTY_PVR:
#ifdef GFX
key = kbd_get_key();
#endif
break;
case CONIO_TTY_SERIAL: {
key = scif_read();
if (key == 3)
arch_exit();
break;
}
case CONIO_TTY_STDIO:
if (fs_total(conio_serial_fd) > 0) {
if (fs_read(conio_serial_fd, &b, 1) == 1)
key = b;
}
if (key == '\n')
key = -1;
break;
case CONIO_TTY_DBGIO: {
key = dbgio_read();
if (key == 3)
arch_exit();
break;
}
}
return key;
}
/* set current char to ch, w/o advancing the cursor */
void conio_setch(int ch) {
switch (conio_ttymode) {
case CONIO_TTY_PVR:
switch (ch) {
case '\n':
case '\r':
break;
default:
conio_virtscr[conio_cursor.row][conio_cursor.col] = ch;
}
break;
case CONIO_TTY_SERIAL:
case CONIO_TTY_STDIO:
case CONIO_TTY_DBGIO:
conio_deadvance_cursor();
conio_putch(ch);
break;
}
}
/* put a character at the cursor and move the cursor */
void conio_putch(int ch) {
switch (conio_ttymode) {
case CONIO_TTY_PVR:
switch (ch) {
case '\r':
conio_cursor.col = 0;
break;
case '\n':
conio_cursor.row++;
conio_cursor.col = 0;
if (conio_cursor.row >= CONIO_NUM_ROWS)
conio_scroll();
break;
default:
conio_virtscr[conio_cursor.row][conio_cursor.col] = ch;
conio_advance_cursor();
}
break;
case CONIO_TTY_SERIAL:
if (ch == '\n')
scif_write('\r');
else if (ch == '\r')
break;
scif_write(ch);
break;
case CONIO_TTY_STDIO:
if (ch == '\n')
fs_write(conio_serial_fd, "\r", 1);
else if (ch == '\r')
break;
fs_write(conio_serial_fd, &ch, 1);
break;
case CONIO_TTY_DBGIO:
dbgio_write(ch);
break;
}
}
/* put a string of characters */
void conio_putstr(char *str) {
while (*str != '\0') {
conio_putch(*str++);
}
}
/* a printfish function */
int conio_printf(const char *fmt, ...) {
char buff[512]; /* buffer overflow waiting to happen.... I'll add a vsnprintf func later */
va_list args;
int i;
va_start(args, fmt);
i = vsprintf(buff, fmt, args);
if (conio_ttymode != CONIO_TTY_NONE)
conio_putstr(buff);
va_end(args);
return i;
}
/* clear the screen */
void conio_clear() {
int row, col;
switch (conio_ttymode) {
case CONIO_TTY_PVR:
/* fill screen with spaces */
for (row = 0; row < CONIO_NUM_ROWS; row++)
for (col = 0; col < CONIO_NUM_COLS; col++)
conio_virtscr[row][col] = ' ';
break;
case CONIO_TTY_SERIAL:
scif_write_buffer((uint8 *)"\x1b[2J", 4, 1);
break;
case CONIO_TTY_STDIO:
fs_write(conio_serial_fd, (void *)"\x1b[2J", 4);
break;
case CONIO_TTY_DBGIO:
dbgio_write_buffer((uint8 *)"\x1b[2J", 4);
break;
}
}
/* conio freeze (for sub-process taking over TA) */
void conio_freeze() {
sem_wait(&ft_mutex);
}
/* conio thaw */
void conio_thaw() {
sem_signal(&ft_mutex);
}
/* set theme */
void conio_set_theme(int theme) {
assert( theme >= CONIO_THEME_PLAIN && theme <= CONIO_THEME_C64 );
conio_theme = theme;
}
/* ptr to old printf */
/* static int (*oldprintf)(const char *fmt, ...) = NULL; */
static volatile int conio_entered = 0;
static volatile int conio_exit = 0;
/* the drawing/keyboard polling thread */
static void *conio_thread(void *param) {
conio_entered = 1;
while (!conio_exit) {
sem_wait(&ft_mutex);
conio_input_frame();
if (conio_ttymode == CONIO_TTY_PVR) {
#ifdef GFX
conio_draw_frame();
#endif
} else {
if (conio_ttymode == CONIO_TTY_SERIAL)
scif_flush();
thd_sleep(1000/60); /* Simulate frame delay */
}
sem_signal(&ft_mutex);
}
conio_exit = -1;
return NULL;
}
/* initialize the console I/O stuffs */
int conio_init(int ttymode, int inputmode) {
conio_ttymode = ttymode;
conio_inputmode = inputmode;
switch (conio_ttymode) {
case CONIO_TTY_PVR:
#ifdef GFX
conio_draw_init();
#endif
break;
case CONIO_TTY_STDIO:
break;
case CONIO_TTY_SERIAL:
conio_serial_fd = 1;
break;
case CONIO_TTY_DBGIO:
break;
default:
assert_msg( 0, "Unknown CONIO TTY mode" );
conio_ttymode = CONIO_TTY_PVR;
break;
}
assert_msg( conio_inputmode == CONIO_INPUT_LINE, "Non-Line input modes not supported yet" );
conio_input_init();
if (conio_ttymode == CONIO_TTY_PVR) {
conio_clear();
conio_gotoxy(0, 0);
}
sem_init(&ft_mutex, 1);
/* create the conio thread */
conio_exit = 0;
if (thd_create(1, conio_thread, 0) < 0)
return -1;
/* Wait for it to actually start */
while (!conio_entered)
thd_pass();
return 0;
}
int conio_shutdown() {
/* shutup our thread */
conio_exit = 1;
while (conio_exit != -1)
;
/* Delete the sempahore */
sem_destroy(&ft_mutex);
#ifdef GFX
if (conio_ttymode == CONIO_TTY_PVR)
conio_draw_shutdown();
#endif
conio_input_shutdown();
conio_ttymode = CONIO_TTY_NONE;
conio_inputmode = CONIO_INPUT_NONE;
return 0;
}