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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
|
#include <curses.h>
#include <malloc.h>
#include <stddef.h>
#include <vsprintf.h>
/** @file
*
* MuCurses: lightweight xcurses implementation for PXE ROMs
*
*/
#define WRAP 0
#define NOWRAP 1
unsigned short _COLS;
unsigned short _LINES;
unsigned short _COLOURS;
unsigned int *_COLOUR_PAIRS; /* basically this is an array, but as its
length is determined only when initscr
is run, I can only think to make it a
pointer and malloc the array into being
... */
struct cursor_pos {
unsigned int y, x;
};
WINDOW _stdscr = {
.attrs = A_DEFAULT,
.ori_y = 0,
.ori_x = 0,
.curs_y = 0,
.curs_x = 0,
.scr = curscr,
};
/*
* Primitives
*/
/**
* Write a single character rendition to a window
*
* @v *win window in which to write
* @v ch character rendition to write
* @v wrap wrap "switch"
*/
static void _wputch ( WINDOW *win, chtype ch, int wrap ) {
/* make sure we set the screen cursor to the right position
first! */
win->scr->movetoyx( win->scr, win->ori_y + win->curs_y,
win->ori_x + win->curs_x );
win->scr->putc(win->scr, ch);
if ( ++(win->curs_x) == win->width ) {
if ( wrap == WRAP ) {
win->curs_x = 0;
/* specification says we should really scroll,
but we have no buffer to scroll with, so we
can only overwrite back at the beginning of
the window */
if ( ++(win->curs_y) == win->height )
win->curs_y = 0;
} else {
(win->curs_x)--;
}
}
}
/**
* Write a chtype string to a window
*
* @v *win window in which to write
* @v *chstr chtype string
* @v wrap wrap "switch"
* @v n write at most n chtypes
*/
static void _wputchstr ( WINDOW *win, const chtype *chstr, int wrap, int n ) {
for ( ; *chstr && n-- ; chstr++ ) {
_wputch(win,*chstr,wrap);
}
}
/**
* Write a standard c-style string to a window
*
* @v *win window in which to write
* @v *str string
* @v wrap wrap "switch"
* @v n write at most n chars from *str
*/
static void _wputstr ( WINDOW *win, const char *str, int wrap, int n ) {
for ( ; *str && n-- ; str++ ) {
_wputch( win, *str | win->attrs, wrap );
}
}
/**
* Restore cursor position from encoded backup variable
*
* @v *win window on which to operate
* @v *pos pointer to struct in which original cursor position is stored
*/
static void _restore_curs_pos ( WINDOW *win, struct cursor_pos *pos ){
win->curs_y = pos->y;
win->curs_x = pos->x;
win->scr->movetoyx ( win->scr, win->curs_y, win->curs_x );
}
/**
* Store cursor position for later restoration
*
* @v *win window on which to operate
* @v *pos pointer to struct in which to store cursor position
*/
static void _store_curs_pos ( WINDOW *win, struct cursor_pos *pos ) {
pos->y = win->curs_y;
pos->x = win->curs_x;
}
/**
* Move a window's cursor to the specified position
*
* @v *win window to be operated on
* @v y Y position
* @v x X position
* @ret rc return status code
*/
int wmove ( WINDOW *win, int y, int x ) {
/* chech for out-of-bounds errors */
if ( ( ( (unsigned)x - win->ori_x ) > win->width ) ||
( ( (unsigned)y - win->ori_y ) > win->height ) ) {
return ERR;
}
win->curs_y = y;
win->curs_x = x;
win->scr->movetoyx( win->scr, win->ori_y + win->curs_y,
win->ori_x + win->curs_x );
return OK;
}
/**
* get terminal baud rate
*
* @ret bps return baud rate in bits per second
*/
int baudrate ( void ) {
return 0;
}
/**
* Audible (or visual) signal
*
* @ret rc return status code
*/
int beep ( void ) {
printf("\a");
return OK;
}
/**
* Draw borders from single-byte characters and renditions around a
* window
*
* @v *win window to be bordered
* @v verch vertical chtype
* @v horch horizontal chtype
* @ret rc return status code
*/
int box ( WINDOW *win, chtype verch, chtype horch ) {
int corner = '+' | win->attrs; /* default corner character */
return wborder( win, verch, verch, horch, horch,
corner, corner, corner, corner );
}
/**
* Indicates whether the underlying terminal device is capable of
* having colours redefined
*
* @ret bool returns boolean
*/
bool can_change_colour ( void ) {
return (bool)TRUE;
}
/**
* Identify the RGB components of a given colour value
*
* @v colour colour value
* @v *red address to store red component
* @v *green address to store green component
* @v *blue address to store blue component
* @ret rc return status code
*/
int colour_content ( short colour, short *red, short *green, short *blue ) {
/* we do not have a particularly large range of colours (3
primary, 3 secondary and black), so let's just put in a
basic switch... */
switch(colour) {
case COLOUR_BLACK:
*red = 0; *green = 0; *blue = 0;
break;
case COLOUR_BLUE:
*red = 0; *green = 0; *blue = 1000;
break;
case COLOUR_GREEN:
*red = 0; *green = 1000; *blue = 0;
break;
case COLOUR_CYAN:
*red = 0; *green = 1000; *blue = 1000;
break;
case COLOUR_RED:
*red = 1000; *green = 0; *blue = 0;
break;
case COLOUR_MAGENTA:
*red = 1000; *green = 0; *blue = 1000;
break;
case COLOUR_YELLOW:
*red = 1000; *green = 1000; *blue = 0;
break;
}
return OK;
}
/**
* Delete a window
*
* @v *win pointer to window being deleted
* @ret rc return status code
*/
int delwin ( WINDOW *win ) {
if ( win == NULL )
goto err;
/* must free descendants first, but I haven't implemented descendants yet
... */
free(win);
return OK;
err:
return ERR;
}
/**
* Get the background rendition attributes for a window
*
* @v *win subject window
* @ret ch chtype rendition representation
*/
inline chtype getbkgd ( WINDOW *win ) {
return win->attrs;
}
/**
* Initialise console environment
*
* @ret *win return pointer to stdscr
*/
WINDOW *initscr ( void ) {
/* determine console size */
/* initialise screen */
stdscr->width = 80;
stdscr->height = 25;
/* set previously unknown window attributes */
/* refresh screen */
return stdscr;
}
/**
* Create new WINDOW
*
* @v nlines number of lines
* @v ncols number of columns
* @v begin_y column origin
* @v begin_x line origin
* @ret *win return pointer to new window
*/
WINDOW *newwin ( int nlines, int ncols, int begin_y, int begin_x ) {
WINDOW *win = calloc( 1, sizeof(WINDOW) );
win->ori_y = begin_y;
win->ori_x = begin_x;
win->height = nlines;
win->width = ncols;
win->scr = stdscr->scr;
win->parent = NULL;
win->child = NULL;
return win;
}
struct printw_context {
struct printf_context ctx;
WINDOW *win;
};
static void _printw_handler ( struct printf_context *ctx, unsigned int c ) {
struct printw_context *wctx =
container_of ( ctx, struct printw_context, ctx );
_wputch( wctx->win, c | wctx->win->attrs, WRAP );
}
/**
* Print formatted output in a window
*
* @v *win subject window
* @v *fmt formatted string
* @v varglist argument list
* @ret rc return status code
*/
int vw_printw ( WINDOW *win, const char *fmt, va_list varglist ) {
struct printw_context wctx = {
.win = win,
.ctx = { .handler = _printw_handler, },
};
vcprintf ( &(wctx.ctx), fmt, varglist );
return OK;
}
/**
* Add a single-byte character and rendition to a window and advance
* the cursor
*
* @v *win window to be rendered in
* @v ch character to be added at cursor
* @ret rc return status code
*/
int waddch ( WINDOW *win, const chtype ch ) {
_wputch( win, ch, WRAP );
return OK;
}
/**
* Add string of single-byte characters and renditions to a window
*
* @v *win window to be rendered in
* @v *chstr pointer to first chtype in "string"
* @v n max number of chars from chstr to render
* @ret rc return status code
*/
int waddchnstr ( WINDOW *win, const chtype *chstr, int n ) {
struct cursor_pos pos;
_store_curs_pos( win, &pos );
_wputchstr( win, chstr, NOWRAP, n );
_restore_curs_pos( win, &pos );
return OK;
}
/**
* Add string of single-byte characters to a window
*
* @v *win window to be rendered in
* @v *str standard c-style string
* @v n max number of chars from string to render
* @ret rc return status code
*/
int waddnstr ( WINDOW *win, const char *str, int n ) {
_wputstr( win, str, WRAP, n );
return OK;
}
/**
* Turn off attributes in a window
*
* @v win subject window
* @v attrs attributes to enable
* @ret rc return status code
*/
int wattroff ( WINDOW *win, int attrs ) {
win->attrs &= ~attrs;
return 0;
}
/**
* Turn on attributes in a window
*
* @v win subject window
* @v attrs attributes to enable
* @ret rc return status code
*/
int wattron ( WINDOW *win, int attrs ) {
win->attrs |= attrs;
return OK;
}
/**
* Set attributes in a window
*
* @v win subject window
* @v attrs attributes to enable
* @ret rc return status code
*/
int wattrset ( WINDOW *win, int attrs ) {
win->attrs = ( attrs | ( win->attrs & A_COLOR ) );
return OK;
}
/**
* Get attributes and colour pair information
*
* @v *win window to obtain information from
* @v *attrs address in which to store attributes
* @v *pair address in which to store colour pair
* @v *opts undefined (for future implementation)
* @ret rc return status cude
*/
int wattr_get ( WINDOW *win, attr_t *attrs, short *pair,
void *opts __unused ) {
*attrs = win->attrs & A_ATTRIBUTES;
*pair = (short)(( win->attrs & A_COLOR ) >> CPAIR_SHIFT);
return OK;
}
/**
* Turn off attributes in a window
*
* @v *win subject window
* @v attrs attributes to toggle
* @v *opts undefined (for future implementation)
* @ret rc return status code
*/
int wattr_off ( WINDOW *win, attr_t attrs,
void *opts __unused ) {
wattroff( win, attrs );
return OK;
}
/**
* Turn on attributes in a window
*
* @v *win subject window
* @v attrs attributes to toggle
* @v *opts undefined (for future implementation)
* @ret rc return status code
*/
int wattr_on ( WINDOW *win, attr_t attrs,
void *opts __unused ) {
wattron( win, attrs );
return OK;
}
/**
* Set attributes and colour pair information in a window
*
* @v *win subject window
* @v attrs attributes to set
* @v cpair colour pair to set
* @v *opts undefined (for future implementation)
* @ret rc return status code
*/
int wattr_set ( WINDOW *win, attr_t attrs, short cpair,
void *opts __unused ) {
wattrset( win, attrs | ( ( (unsigned short)cpair ) << CPAIR_SHIFT ) );
return OK;
}
/**
* Draw borders from single-byte characters and renditions around a
* window
*
* @v *win window to be bordered
* @v ls left side
* @v rs right side
* @v ts top
* @v bs bottom
* @v tl top left corner
* @v tr top right corner
* @v bl bottom left corner
* @v br bottom right corner
* @ret rc return status code
*/
int wborder ( WINDOW *win, chtype ls, chtype rs,
chtype ts, chtype bs, chtype tl,
chtype tr, chtype bl, chtype br ) {
wmove(win,0,0);
_wputch(win,tl,WRAP);
while ( ( win->width - 1 ) - win->curs_x ) {
_wputch(win,ts,WRAP);
}
_wputch(win,tr,WRAP);
while ( ( win->height - 1 ) - win->curs_y ) {
_wputch(win,ls,WRAP);
wmove(win,win->curs_y,(win->width)-1);
_wputch(win,rs,WRAP);
}
_wputch(win,bl,WRAP);
while ( ( win->width -1 ) - win->curs_x ) {
_wputch(win,bs,WRAP);
}
_wputch(win,br,NOWRAP); /* do not wrap last char to leave
cursor in last position */
return OK;
}
/**
* Clear a window to the bottom
*
* @v *win subject window
* @ret rc return status code
*/
int wclrtobot ( WINDOW *win ) {
struct cursor_pos pos;
_store_curs_pos( win, &pos );
do {
_wputch( win, (unsigned)' ', WRAP );
} while ( win->curs_y + win->curs_x );
_restore_curs_pos( win, &pos );
return OK;
}
/**
* Clear a window to the end of the current line
*
* @v *win subject window
* @ret rc return status code
*/
int wclrtoeol ( WINDOW *win ) {
struct cursor_pos pos;
_store_curs_pos( win, &pos );
while ( ( win->curs_y - pos.y ) == 0 ) {
_wputch( win, (unsigned)' ', WRAP );
}
_restore_curs_pos( win, &pos );
return OK;
}
/**
* Set colour pair for a window
*
* @v *win subject window
* @v colour_pair_number colour pair integer
* @v *opts undefined (for future implementation)
* @ret rc return status code
*/
int wcolour_set ( WINDOW *win, short colour_pair_number,
void *opts __unused ) {
if ( ( unsigned short )colour_pair_number > COLORS )
return ERR;
win->attrs = ( (unsigned short)colour_pair_number << CPAIR_SHIFT ) |
( win->attrs & A_ATTRIBUTES );
return OK;
}
/**
* Delete character under the cursor in a window
*
* @v *win subject window
* @ret rc return status code
*/
int wdelch ( WINDOW *win ) {
struct cursor_pos pos;
_store_curs_pos( win, &pos );
_wputch( win, (unsigned)' ', NOWRAP );
_restore_curs_pos( win, &pos );
return OK;
}
/**
* Delete line under a window's cursor
*
* @v *win subject window
* @ret rc return status code
*/
int wdeleteln ( WINDOW *win ) {
/* let's just set the cursor to the beginning of the line and
let wclrtoeol do the work :) */
wmove( win, win->curs_y, 0 );
wclrtoeol( win );
return OK;
}
/**
* Create a horizontal line in a window
*
* @v *win subject window
* @v ch rendition and character
* @v n max number of chars (wide) to render
* @ret rc return status code
*/
int whline ( WINDOW *win, chtype ch, int n ) {
struct cursor_pos pos;
_store_curs_pos ( win, &pos );
while ( ( win->curs_x - win->width ) && n-- ) {
_wputch ( win, ch, NOWRAP );
}
_restore_curs_pos ( win, &pos );
return OK;
}
/**
* Print formatted output to a window
*
* @v *win subject window
* @v *fmt formatted string
* @v ... string arguments
* @ret rc return status code
*/
int wprintw ( WINDOW *win, const char *fmt, ... ) {
va_list args;
int i;
va_start ( args, fmt );
i = vw_printw ( win, fmt, args );
va_end ( args );
return i;
}
/**
* Create a vertical line in a window
*
* @v *win subject window
* @v ch rendition and character
* @v n max number of lines to render
* @ret rc return status code
*/
int wvline ( WINDOW *win, chtype ch, int n ) {
struct cursor_pos pos;
_store_curs_pos ( win, &pos );
while ( ( win->curs_y - win->height ) && n-- ) {
_wputch ( win, ch, NOWRAP );
wmove( win, ++(win->curs_y), pos.x);
}
_restore_curs_pos ( win, &pos );
return OK;
}
|