-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathacursesio.cpp
More file actions
578 lines (470 loc) · 12.7 KB
/
acursesio.cpp
File metadata and controls
578 lines (470 loc) · 12.7 KB
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
* Arduino version of curses library
*/
#include "ztypes.h"
#include <mcurses.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
ztheme_t themes[] = {
{"TRS-80 Black", (F_BLACK|B_WHITE), (F_WHITE|B_BLACK)},
{"Lisa White", (F_WHITE|B_BLACK), (F_BLACK|B_WHITE)},
{"Compaq Green", (F_BLACK|B_GREEN), (F_GREEN|B_BLACK)},
{"IBM XT Amber", (F_BLACK|B_YELLOW|A_DIM), (F_YELLOW|B_BLACK|A_DIM)},
{"Amiga Blue", (F_BLUE|B_WHITE), (F_WHITE|B_BLUE)},
{"Amstrad Blue and Gold", (F_YELLOW|B_BLACK|A_BOLD), (F_YELLOW|B_BLUE|A_BOLD)}
};
//int themecount = 5;
int themecount = sizeof(themes)/sizeof(themes[0]);
extern int theme;
#define EXTENDED 1
#define PLAIN 2
#ifdef HARD_COLORS
static ZINT16 current_fg;
static ZINT16 current_bg;
#endif
extern ZINT16 default_fg;
extern ZINT16 default_bg;
extern int hist_buf_size;
extern int use_bg_color;
/* new stuff for command editing */
int BUFFER_SIZE;
char *commands;
int space_avail;
static int ptr1, ptr2 = 0;
static int end_ptr = 0;
static int row, head_col;
static int keypad_avail = 1;
/* done with editing global info */
static int current_row = 0;
static int current_col = 0;
static int saved_row;
static int saved_col;
static int status_row = 0;
static int status_col = 0;
static int text_col = 0;
static int cursor_saved = OFF;
static char tcbuf[1024];
static char cmbuf[1024];
static char *cmbufp;
static ZINT16 current_fg;
static ZINT16 current_bg;
extern ZINT16 default_fg;
extern ZINT16 default_bg;
static void display_string( char * );
static int read_char( int timeout );
void Arduino_putchar(uint8_t c)
{
Serial.write(c);
}
char Arduino_getchar()
{
while (!Serial.available()){yield();};
return Serial.read();
}
static int inc( uint32_t timeout = 0 )
{
uint32_t timer = millis();
while(!Serial.available() && ((timeout == 0) || (timeout > 0 && (timer + timeout*100 > millis())))){yield();};
if(timeout > 0 && ((timer + timeout*100) <= millis()))
return -1;
int c = Serial.read();
if ( c == -1 )
{
fatal("acursesio inc: error in Serial.read!");
}
if((int) c != 127) // is this a backspace key? will print it later
Serial.write(c);
if(c == '\r')
Serial.write('\n');
return c;
}
static int uninc( int c )
{
// not supported right now
//return ungetc( c, stdin );
}
static int outc( int c )
{
Serial.print(String((char) c));
return c;
}
void initialize_screen( )
{
int row, col;
setFunction_putchar(Arduino_putchar); // tell the library which output channel shall be used
setFunction_getchar(Arduino_getchar); // tell the library which input channel shall be used
/* initialize the command buffer */
cmbufp = cmbuf;
/* start the curses environment */
if ( !initscr( ) )
{
fatal( "initialize_screen(): Couldn't init curses." );
}
/* COLS and LINES set by curses */
screen_cols = COLS;
screen_rows = LINES;
attrset(themes[theme].text_attr);
clear_screen( );
/* Last release (2.0.1g) claimed DEC tops 20. I'm a sadist. Sue me. */
h_interpreter = INTERP_MSDOS;
JTERP = INTERP_UNIX;
// command history not implemented
/*
commands = ( char * ) malloc( hist_buf_size * sizeof ( char ) );
if ( commands == NULL )
fatal( "initialize_screen(): Couldn't allocate history buffer." );
BUFFER_SIZE = hist_buf_size;
space_avail = hist_buf_size - 1;
*/
interp_initialized = 1;
} /* initialize_screen */
void restart_screen( )
{
cursor_saved = OFF;
} /* restart_screen */
void reset_screen( )
{
/* only do this stuff on exit when called AFTER initialize_screen */
if ( interp_initialized )
{
display_string( "\r\n[Hit any key to exit.]" );
Arduino_getchar();
delete_status_window( );
select_text_window( );
//printf( "[0m" );
erase( );
//set_cbreak_mode( 0 );
}
display_string( "\r\n" );
} /* reset_screen */
void clear_screen( )
{
erase( ); /* clear screen */
current_row = 1;
current_col = 1;
} /* clear_screen */
void select_status_window( )
{
save_cursor_position( );
} /* select_status_window */
void select_text_window( )
{
restore_cursor_position( );
} /* select_text_window */
void create_status_window( )
{
int row, col;
get_cursor_position( &row, &col );
/* set up a software scrolling region */
/*
setscrreg(status_size, screen_rows-1);
*/
move_cursor( row, col );
} /* create_status_window */
void delete_status_window( )
{
int row, col;
get_cursor_position( &row, &col );
/* set up a software scrolling region */
setscrreg(0, screen_rows-1);
move_cursor( row, col );
} /* delete_status_window */
void clear_line( )
{
clrtoeol( );
} /* clear_line */
void clear_text_window( )
{
int i, row, col;
get_cursor_position( &row, &col );
for ( i = status_size + 1; i <= screen_rows; i++ )
{
move_cursor( i, 1 );
clear_line( );
}
move_cursor( row, col );
} /* clear_text_window */
void clear_status_window( )
{
int i, row, col;
get_cursor_position( &row, &col );
for ( i = status_size; i; i-- )
{
move_cursor( i, 1 );
clear_line( );
}
move_cursor( row, col );
} /* clear_status_window */
void move_cursor( int row, int col )
{
move( row - 1, col - 1 );
current_row = row;
current_col = col;
} /* move_cursor */
void get_cursor_position( int *row, int *col )
{
*row = current_row;
*col = current_col;
} /* get_cursor_position */
void save_cursor_position( )
{
if ( cursor_saved == OFF )
{
get_cursor_position( &saved_row, &saved_col );
cursor_saved = ON;
}
} /* save_cursor_position */
void restore_cursor_position( )
{
if ( cursor_saved == ON )
{
move_cursor( saved_row, saved_col );
cursor_saved = OFF;
}
} /* restore_cursor_position */
void set_attribute( int attribute )
{
static int emph = 0, rev = 0;
if ( attribute == NORMAL )
{
// this is the text part of the window
attrset(themes[theme].text_attr);
}
if ( attribute & REVERSE )
{
// this is the status part of the window
attrset(themes[theme].status_attr);
}
if ( attribute & BOLD )
{
}
if ( attribute & EMPHASIS )
{
}
if ( attribute & FIXED_FONT )
{
}
} /* set_attribute */
static void display_string( char *s )
{
while ( *s )
display_char( *s++ );
} /* display_string */
void display_char( int c )
{
outc( c );
if ( ++current_col > screen_cols )
current_col = screen_cols;
} /* display_char */
void scroll_line( )
{
int row, col;
display_char( '\r' );
display_char( '\n');
get_cursor_position( &row, &col );
if ( row < screen_rows )
{
display_char( '\r' );
display_char( '\n');
}
else
{
setscrreg( status_size, screen_rows - 1 );
display_char( '\r' );
//display_char( '\n');
}
current_col = 1;
if ( ++current_row > screen_rows )
current_row = screen_rows;
} /* scroll_line */
int input_line( int buflen, char *buffer, int timeout, int *read_size )
{
int c;
yield();
*read_size = 0;
// while ( ( c = read_char( ) ) != '\n' )
while ( ( c = read_char(timeout) ) != '\r' ) // use for Arduino line feed
{
if(c == 127) // backspace pressed?
{
if(*read_size > 0)
{
buffer[(*read_size)] = '\0';
(*read_size)--;
Serial.print((char) c); // OK to backspace, characters still on the left side
}
}
else if ( *read_size < buflen )
buffer[( *read_size )++] = c;
}
text_col = 0;
//Serial.print("");
//delay(1000);
yield();
return c;
} /* input_line */
#define COMMAND_LEN 20
static int read_char( int timeout = 0 )
{
static int input_is_at_eol = TRUE;
int c, n;
char command[COMMAND_LEN + 1];
for ( ;; )
{
if ( ( c = inc(timeout) ) == '\\' )
{
c = inc(timeout);
if ( c == '\\' )
break;
uninc( c );
/* Read a command. */
for ( n = 0; n < COMMAND_LEN; n++ )
{
command[n] = inc(timeout);
//if ( command[n] == '\n' )
if ( command[n] == '\r' )
break;
}
command[n] = '\0';
/* If line was too long, flush input to the end of it. */
if ( n == COMMAND_LEN )
//while ( inc(timeout) != '\n' )
while ( inc(timeout) != '\r' )
;
continue;
}
break;
}
input_is_at_eol = ( c == '\r' );
return c;
} /* read_char */
int input_character( int timeout )
{
int c = read_char( timeout );
/* Bureaucracy expects CR, not NL. */
return ( ( c == '\n' ) ? '\r' : c );
} /* input_character */
static int read_key( int mode )
{
int c;
if ( mode == PLAIN )
{
do
{
c = Arduino_getchar();
if ( c == 4 )
{
reset_screen( );
exit( 0 );
} /* CTRL-D (EOF) */
}
while ( !( c == 10 || c == 13 || c == 8 ) && ( c < 32 || c > 127 ) );
}
else if ( mode == EXTENDED )
{ /* also pass ESC character back for editor */
do
{
c = Arduino_getchar();
if ( c == 4 )
{
reset_screen( );
exit( 0 );
} /* CTRL-D (EOF) */
}
while ( !( c == 27 || c == 10 || c == 13 || c == 8 ) && ( c < 32 || c > 127 ) );
}
if ( c == 127 )
c = '\b';
else if ( c == 10 )
c = 13;
return ( c );
} /* read_key */
static void rundown( )
{
unload_cache( );
close_story( );
close_script( );
reset_screen( );
} /* rundown */
void set_colours( zword_t foreground, zword_t background )
{
// not implemented
}
/* Zcolors:
* BLACK 0 BLUE 4 GREEN 2 CYAN 6 RED 1 MAGENTA 5 BROWN 3 WHITE 7
* ANSI Colors (foreground over background):
* BLACK 30 BLUE 34 GREEN 32 CYAN 36 RED 31 MAGENTA 35 BROWN 33 WHITE 37
* BLACK 40 BLUE 44 GREEN 42 CYAN 46 RED 41 MAGENTA 45 BROWN 43 WHITE 47
*/
void set_colours_( zword_t foreground, zword_t background )
{
return;
int fg, bg;
int fg_colour_map[] = { F_BLACK, F_BLUE, F_GREEN, F_CYAN, F_RED, F_MAGENTA, F_BROWN, F_WHITE };
int bg_colour_map[] = { B_BLACK, B_BLUE, B_GREEN, B_CYAN, B_RED, B_MAGENTA, B_BROWN, B_WHITE };
/* Translate from Z-code colour values to natural colour values */
if ( ( ZINT16 ) foreground >= 1 && ( ZINT16 ) foreground <= 9 )
{
fg = ( foreground == 1 ) ? ( fg_colour_map[default_fg] ) : fg_colour_map[foreground];
}
if ( ( ZINT16 ) background >= 1 && ( ZINT16 ) background <= 9 )
{
bg = ( background == 1 ) ? ( bg_colour_map[default_bg] ) : bg_colour_map[background];
}
current_fg = ( ZINT16 ) fg;
current_bg = ( ZINT16 ) bg;
if( monochrome)
{
attrset ( F_WHITE | B_BLACK );
}
else
{
attrset (TEXT_ATTR);
}
}
/*
* codes_to_text
*
* Translate Z-code characters to machine specific characters. These characters
* include line drawing characters and international characters.
*
* The routine takes one of the Z-code characters from the following table and
* writes the machine specific text replacement. The target replacement buffer
* is defined by MAX_TEXT_SIZE in ztypes.h. The replacement text should be in a
* normal C, zero terminated, string.
*
* Return 0 if a translation was available, otherwise 1.
*
* Line drawing characters (0xb3 - 0xda):
*
* 0xb3 vertical line (|)
* 0xba double vertical line (#)
* 0xc4 horizontal line (-)
* 0xcd double horizontal line (=)
* all other are corner pieces (+)
*/
int codes_to_text( int c, char *s )
{
/* German characters need translation */
if ( c > 154 && c < 224 )
{
s[0] = zscii2latin1[c - 155];
if ( c == 220 )
{
s[1] = 'e';
s[2] = '\0';
}
else if ( c == 221 )
{
s[1] = 'E';
s[2] = '\0';
}
else
{
s[1] = '\0';
}
return 0;
}
return 1;
} /* codes_to_text */