forked from Bill-Gray/find_orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmycurses.cpp
618 lines (523 loc) · 13.6 KB
/
mycurses.cpp
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/* mycurses.cpp: really basic DOS Curses
Copyright (C) 2010, Project Pluto
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA. */
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <i86.h>
#define STDSCR_DEFINED 1
#include "mycurses.h"
static int scr_xsize, scr_ysize, echo_on = 0;
static int initial_cursor_x, initial_cursor_y;
static char *backup_screen;
WINDOW *stdscr;
/* At present, I don't see any actual use for curscr in this setup.
Everything goes directly to the screen. If, at some point, I attempt to
make some more full-fledged curses clone, I may have to reconsider. */
#ifdef DEFINE_CURSCR
WINDOW *curscr;
#endif
static int get_cursor_loc( int *row, int *col);
static int set_cursor_loc( const int row, const int col);
static int set_cursor_mode( const int startrow, const int endrow );
#define DISPMEM ((unsigned char *)0xb8000)
WINDOW *resize_window( WINDOW *old, const int ysize, const int xsize)
{
WINDOW *rval = (WINDOW *)calloc( 1,
sizeof( WINDOW) + xsize * ysize * sizeof( attr_t));
if( rval)
{
if( old)
memcpy( rval, old, sizeof( WINDOW));
rval->xsize = xsize;
rval->ysize = ysize;
rval->curr_x = rval->curr_y = 0;
}
if( old)
free( old);
return( rval);
}
/* Once upon a time, I had a graphics card that supported _nine_ different
text modes. My current, theoretically more advanced, card supports only
80-wide with 25, 28, or 50 lines. They call this 'progress'... anyway,
I've included all the data for the nine possible modes. */
#define TOTAL_N_TEXT_MODES 9
static int default_mode_index = 0;
static const unsigned char text_mode_xsizes[TOTAL_N_TEXT_MODES] =
{ 80, 80, 80, 100, 80, 132, 132, 132, 132 };
static const unsigned char text_mode_ysizes[TOTAL_N_TEXT_MODES] =
{ 25, 28, 50, 37, 60, 44, 50, 25, 28 };
static void init_stdscr( void)
{
const char *default_mode_text = getenv( "MYCURSES_MODE");
if( default_mode_text)
default_mode_index = (int)( *default_mode_text - '0');
resize_term( text_mode_ysizes[default_mode_index],
text_mode_xsizes[default_mode_index]);
backup_screen = (char *)malloc( scr_xsize * scr_ysize * 2);
memcpy( backup_screen, DISPMEM, scr_xsize * scr_ysize * 2);
stdscr->wrap_on = 1;
}
WINDOW *initscr( void )
{
get_cursor_loc( &initial_cursor_y, &initial_cursor_x);
init_stdscr( );
return( stdscr);
}
int wmove( WINDOW *w, const int y, const int x)
{
int rval = ERR;
if( x >= 0 && x < w->xsize && y >= 0 && y < w->ysize)
{
w->curr_x = x;
w->curr_y = y;
rval = 0;
}
return( rval);
}
int waddnstr( WINDOW* w, const char *str, int len)
{
attr_t *dataptr = w->data + (w->curr_x + w->curr_y * w->xsize);
int max_out;
if( w->curr_y >= w->ysize)
return( -1);
max_out = w->xsize - w->curr_x;
if( w->wrap_on)
max_out += (w->ysize - w->curr_y - 1) * w->xsize;
if( len < 0)
len = strlen( str);
if( len > max_out)
len = max_out;
w->curr_x += len;
if( w->wrap_on)
while( w->curr_x > w->xsize)
{
w->curr_x -= w->xsize;
w->curr_y++;
}
while( *str && len--)
*dataptr++ = (attr_t)((unsigned char)*str++) | w->attr;
return( 0);
}
int waddch( WINDOW *w, const char c)
{
return( waddnstr( w, &c, 1));
}
int start_color( void )
{
return( 0);
}
int wclear( WINDOW *w)
{
int i = w->xsize * w->ysize;
attr_t *tptr = w->data;
while( i--)
*tptr++ = 0xf20;
return( 0);
}
#ifdef __cplusplus
extern "C" {
#endif
attr_t curses_attrs[COLOR_PAIRS];
#ifdef __cplusplus
}
#endif
int init_pair( const short idx, const short foreground, const short background)
{
if( idx >= 0 && idx < COLOR_PAIRS)
curses_attrs[idx] = ((attr_t)foreground << 8)
| ((attr_t)background << 12);
return( 0);
}
int wattrset( WINDOW *w, const attr_t attr)
{
w->attr = attr;
return( 0);
}
int wattroff( WINDOW *w, const attr_t attr)
{
w->attr = (attr_t)( w->attr & ~attr);
return( 0);
}
int wattron( WINDOW *w, const attr_t attr)
{
w->attr = (attr_t)( w->attr | attr);
return( 0);
}
int wrefresh_rect( const WINDOW *w, int xmin, int ymin, int xmax, int ymax)
{
int y;
if( w->yoffset < 0)
ymin = -w->yoffset;
if( ymax > scr_ysize - w->yoffset)
ymax = scr_ysize - w->yoffset;
if( w->xoffset < 0)
xmin = -w->xoffset;
if( xmax > scr_xsize - w->xoffset)
xmax = scr_xsize - w->xoffset;
if( xmin < xmax)
for( y = ymin; y < ymax; y++)
{
const attr_t *tptr = w->data + y * w->xsize + xmin;
unsigned char *dispmem = DISPMEM
+ ((y + w->yoffset) * scr_xsize + (xmin + w->xoffset)) * 2;
const attr_t *endptr = tptr + xmax - xmin;
while( tptr != endptr)
{
*dispmem++ = (unsigned char)*tptr;
*dispmem++ = (unsigned char)(*tptr++ >> 8);
}
}
return( 0);
}
int wset_rect_attr( WINDOW *w, int xmin, int ymin, int xmax, int ymax)
{
int y;
if( w->yoffset < 0)
ymin = -w->yoffset;
if( ymax > scr_ysize - w->yoffset)
ymax = scr_ysize - w->yoffset;
if( w->xoffset < 0)
xmin = -w->xoffset;
if( xmax > scr_xsize - w->xoffset)
xmax = scr_xsize - w->xoffset;
if( xmin < xmax)
for( y = ymin; y < ymax; y++)
{
attr_t *tptr = w->data + y * w->xsize + xmin;
attr_t *endptr = tptr + (xmax - xmin);
while( tptr != endptr)
{
*tptr = (w->attr | (*tptr & 0xff));
tptr++;
}
}
return( 0);
}
int wrefresh( const WINDOW *w)
{
return( wrefresh_rect( w, 0, 0, w->xsize, w->ysize));
}
int refresh( void)
{
if( !stdscr)
init_stdscr( );
set_cursor_loc( stdscr->curr_y + stdscr->yoffset,
stdscr->curr_x + stdscr->xoffset);
return( wrefresh( stdscr));
}
int noecho( void )
{
echo_on = 0;
return( 0);
}
int echo( void )
{
echo_on = 1;
return( 1);
}
static int set_cursor_mode( const int startrow, const int endrow )
{
union _REGS regset;
regset.h.ah = 0x01;
regset.h.ch = (unsigned char) startrow;
regset.h.cl = (unsigned char) endrow;
int386(0x10, ®set, ®set);
return( 0 );
}
static int set_cursor_loc( const int row, const int col)
{
union _REGS regset;
regset.h.ah = 0x02;
regset.h.bh = 0;
// regset.h.bh = SP->video_page;
regset.h.dh = (unsigned char) row;
regset.h.dl = (unsigned char) col;
int386( 0x10, ®set, ®set);
return( 0);
}
static int get_cursor_loc( int *row, int *col)
{
union _REGS regset;
regset.h.ah = 0x03;
regset.h.bh = 0;
// regset.h.bh = SP->video_page;
int386(0x10, ®set, ®set);
*row = regset.h.dh;
*col = regset.h.dl;
return( 0);
}
int wgetnstr( WINDOW *w, char *str, int max_len)
{
int c = 0;
int len = 0, insert_mode = 0;
set_cursor_mode( 14, 16);
while( len < max_len && c != 13)
{
int x = w->curr_x, y = w->curr_y;
set_cursor_loc( y + w->yoffset, x + w->xoffset);
c = getch( );
if( !c)
c = getch( ) + 256;
if( c == ( 82 + 256)) /* Insert key */
{
insert_mode ^= 1;
set_cursor_mode( 14 - insert_mode * 8, 16);
}
else if( c != 13)
{
if( c != 8)
{
str[len++] = (char)c;
waddch( w, (char)c);
}
else if( len > 0)
{
len--;
str[len] = '\0';
x--;
mvwaddch( w, y, x, ' ');
wmove( w, y, x);
}
if( echo_on)
wrefresh_rect( w, x, y, x + 1, y + 1);
}
}
str[len] = '\0';
if( insert_mode)
set_cursor_mode( 14, 16);
return( 0);
}
int flushinp( void )
{
while( kbhit( ))
getch( );
return( 0);
}
int resize_term( const int ysize, const int xsize)
{
scr_xsize = xsize;
scr_ysize = ysize;
stdscr = resize_window( stdscr, ysize, xsize);
#ifdef DEFINE_CURSCR
curscr = resize_window( curscr, ysize, xsize);
#endif
return( 0);
}
int PDC_set_scrn_mode( const int new_mode)
{
return( 0);
}
int endwin( void )
{
// set_text_mode( TEXT_MODE_80x25);
set_text_mode( default_mode_index);
set_cursor_loc( initial_cursor_y, initial_cursor_x);
if( stdscr)
{
free( stdscr);
stdscr = NULL;
}
#ifdef DEFINE_CURSCR
if( curscr)
{
free( curscr);
curscr = NULL;
}
#endif
if( backup_screen)
{
memcpy( DISPMEM, backup_screen, scr_xsize * scr_ysize * 2);
free( backup_screen);
}
return( 0);
}
int set_text_mode( const unsigned mode)
{
static const unsigned char modes[TOTAL_N_TEXT_MODES] =
{ 3, 3, 3, 42, 38, 34, 34, 34, 34 };
/* for a 16-high font, use 0x14 */
/* for a 14-high font, use 0x11 */
/* for an 8-high font, use 0x12 */
static const unsigned char font_sizes[TOTAL_N_TEXT_MODES] =
{ 0x14, 0x11, 0x12, 0, 0, 0, 0x12, 0x14, 0x11 };
union _REGS regset;
regset.w.ax = modes[mode];
int386( 0x10, ®set, ®set);
if( font_sizes[mode])
{
regset.h.ah = 0x11;
regset.h.al = font_sizes[mode];
regset.h.bl = 0x00;
int386( 0x10, ®set, ®set);
}
resize_term( text_mode_ysizes[mode], text_mode_xsizes[mode]);
return( 0);
}
int get_text_mode_sizes( const int mode, int *ysize, int *xsize)
{
int rval = 0;
if( mode < 0 || mode >= N_TEXT_MODES)
rval = -1;
else
{
if( xsize)
*xsize = (int)text_mode_xsizes[mode];
if( ysize)
*ysize = (int)text_mode_ysizes[mode];
}
return( rval);
}
int putwin(WINDOW *win, FILE *filep)
{
fwrite( win, sizeof( WINDOW), 1, filep);
fwrite( win->data, sizeof( attr_t), win->xsize * win->ysize, filep);
return( OK);
}
static int read_window( WINDOW *win, FILE *filep)
{
fread( win, sizeof( WINDOW), 1, filep);
fread( win->data, sizeof( attr_t), win->xsize * win->ysize, filep);
return( OK);
}
WINDOW *getwin(FILE *filep)
{
WINDOW temp, *rval;
fread( &temp, sizeof( WINDOW), 1, filep);
rval = (WINDOW *)calloc(
sizeof( WINDOW) + temp.xsize * temp.ysize * sizeof( attr_t), 1);
memcpy( rval, &temp, sizeof( WINDOW));
fread( rval->data, sizeof( attr_t), temp.xsize * temp.ysize, filep);
return( rval);
}
int scr_dump(const char *filename)
{
int rval = ERR;
if( stdscr)
{
FILE *ofile = fopen( filename, "wb");
if( ofile)
{
rval = putwin( stdscr, ofile);
fclose( ofile);
}
}
return( rval);
}
int scr_restore(const char *filename)
{
int rval = ERR;
if( stdscr)
{
FILE *ifile = fopen( filename, "rb");
if( ifile)
{
rval = read_window( stdscr, ifile);
fclose( ifile);
}
}
return( rval);
}
char *longname( void)
{
return( "MyCurses");
}
/* Cannibalized, with little modification, from PDCurses. */
int curs_set( const int visibility)
{
union _REGS regs;
int start, end;
switch (visibility)
{
case 0: /* invisible */
start = 32;
end = 0; /* was 32 */
break;
case 2: /* highly visible */
start = 0; /* full-height block */
end = 7;
break;
default: /*' normal' visibility */
start = 0;
end = 31;
break;
}
/* if scrnmode is not set, some BIOSes hang */
regs.h.ah = 0x01;
regs.h.al = (unsigned char)3;
regs.h.ch = (unsigned char)start;
regs.h.cl = (unsigned char)end;
int386(0x10, ®s, ®s);
return 1;
}
/* *inch functions basically copied from PDCurses: */
chtype winch(WINDOW *win)
{
if( !win)
return (chtype)ERR;
return win->data[win->curr_x + win->curr_y * win->xsize];
}
chtype inch(void)
{
return winch(stdscr);
}
chtype mvinch(int y, int x)
{
if (move(y, x) == ERR)
return (chtype)ERR;
return( winch( stdscr));
}
chtype mvwinch(WINDOW *win, int y, int x)
{
if (wmove(win, y, x) == ERR)
return (chtype)ERR;
return( winch( win));
}
int waddchnstr(WINDOW *win, const chtype *ch, int n)
{
attr_t *tptr = win->data + win->curr_x + win->curr_y * win->xsize;
if (n == -1 || n > win->xsize - win->curr_x)
n = win->xsize - win->curr_x;
if( n < 0)
return( ERR);
while( n--)
*tptr++ = (attr_t)*ch++;
return( OK);
}
#ifdef __WATCOMC__
#define ZKEY_CLOCK (*(long *)((long)0x46c))
#else
#define ZKEY_CLOCK (*(long far *)((long)0x46c))
#endif
int napms( int n_milliseconds)
{
const long t_end = ZKEY_CLOCK + (long)n_milliseconds / 55;
long curr_t = 0;
while( curr_t < t_end)
curr_t = ZKEY_CLOCK;
return( 0);
}
int init_color(short color, short red, short green, short blue)
{
return( 0);
}
mmask_t mousemask(mmask_t ignored1, mmask_t *ignored2)
{
return( (mmask_t)0);
}
bool can_change_color( void)
{
return( 0);
}