Debian Codegraph @

terps/tads/tads2/osgen3.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/OSGEN.C,v 1.3 1999/07/11 00:46:30 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1990, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 osgen3 - Operating System dependent functions, general implementation
15 TADS 3 Version, with new "banner" interface
16 Function
17 This module contains certain OS-dependent functions that are common
18 to many character-mode platforms. ("Character mode" means that the
19 display is organized as a rectangular grid of characters in a monospaced
20 font. This module isn't usable for most GUI systems, because it doesn't
21 have any support for variable-pitch fonts - GUI ports generally need to
22 provide their own custom versions of the os_xxx() functions this module
23 module provides. On GUI systems you should simply omit this entire
24 module from the build, and instead substitute a new module of your own
25 creation that defines your custom versions of the necessary os_xxx()
26 functions.)
27
28 Some routines in this file are selectively enabled according to macros
29 defined in os.h, since some ports that use this file will want to provide
30 their own custom versions of these routines instead of the ones defined
31 here. The macros and associated functions are:
32
33 USE_STDIO - implement os_print, os_flush, os_gets with stdio functions
34 USE_DOSEXT - implement os_remext, os_defext using MSDOS-like filename
35 conventions
36 USE_NULLINIT - implement os_init and os_term as do-nothing routines
37 USE_NULLPAUSE - implement os_expause as a do-nothing routine
38 USE_EXPAUSE - use an os_expause that prints a 'strike any key' message
39 and calls os_waitc
40 USE_TIMERAND - implement os_rand using localtime() as a seed
41 USE_NULLSTAT - use a do-nothing os_status function
42 USE_NULLSCORE - use a do-nothing os_score function
43 RUNTIME - enable character-mode console implementation
44 USE_STATLINE - implement os_status and os_score using character-mode
45 status line implementation
46 USE_OVWCHK - implements default saved file overwrite check
47 USE_NULLSTYPE - use a dummy os_settype routine
48 USE_NULL_SET_TITLE - use an empty os_set_title() implementation
49
50 If USE_STDIO is defined, we'll implicitly define USE_STDIO_INPDLG.
51
52 If USE_STATLINE is defined, certain subroutines must be provided for
53 your platform that handle the character-mode console:
54 ossclr - clears a portion of the screen
55 ossdsp - displays text in a given color at a given location
56 ossscr - scroll down (i.e., moves a block of screen up)
57 ossscu - scroll up (i.e., moves a block of screen down)
58 ossloc - locate cursor
59
60 If USE_STATLINE is defined, certain sub-options can be enabled:
61 USE_SCROLLBACK - include output buffer capture in console system
62 USE_HISTORY - include command editing and history in console system
63 Notes
64
65 Modified
66 01/01/98 MJRoberts - moved certain osgen.c routines to osnoui.c
67 04/24/93 JEras - add os_locate() for locating tads-related files
68 04/12/92 MJRoberts - add os_strsc (string score) function
69 03/26/92 MJRoberts - add os_setcolor function
70 09/26/91 MJRoberts - os/2 user exit support
71 09/04/91 MJRoberts - stop reading resources if we find '$eof' resource
72 08/28/91 MJRoberts - debugger bug fix
73 08/01/91 MJRoberts - make runstat work correctly
74 07/30/91 MJRoberts - add debug active/inactive visual cue
75 05/23/91 MJRoberts - add user exit reader
76 04/08/91 MJRoberts - add full-screen debugging support
77 03/10/91 MJRoberts - integrate John's qa-scripter mods
78 11/27/90 MJRoberts - use time() not localtime() in os_rand; cast time_t
79 11/15/90 MJRoberts - created (split off from os.c)
80 */
81
82 #define OSGEN_INIT
83 # include "os.h"
84 #undef OSGEN_INIT
85
86 #include "osgen.h"
87
88 #include <stdio.h>
89 #include <stdlib.h>
90 #include <string.h>
91 #include <stdarg.h>
92 #include <ctype.h>
93 #include <assert.h>
94
95 #include "run.h"
96
97 #if defined(TURBO) || defined(DJGPP)
98 #include "io.h"
99 #endif
100
101 #include "lib.h"
102 #include "tio.h"
103
104 /*
105 * Flag: use "plain" mode. If this is set, we'll use plain stdio output
106 * rather than our window-oriented display.
107 */
108 int os_f_plain = 0;
109
110 #ifdef RUNTIME
111 # ifdef USE_SCROLLBACK
112
113 /*
114 * Screen size variables. The underlying system-specific "oss" code must
115 * initialize these during startup and must keep them up-to-date if the
116 * screen size ever changes.
117 */
118 int G_oss_screen_width = 80;
119 int G_oss_screen_height = 24;
120
121
122 # endif /* USE_SCROLLBACK */
123 #endif /* RUNTIME */
124
125 /*
126 * The special character codes for controlling color.
127 */
128
129 /*
130 * set text attributes: this is followed by one byte giving the new
131 * attribute codes
132 */
133 #define OSGEN_ATTR 1
134
135 /*
136 * Set text color: this is followed by two bytes giving the foreground and
137 * background colors as OSGEN_COLOR_xxx codes.
138 *
139 * Note well that the colors encoded in this escape sequence are
140 * OSGEN_COLOR_xxx values, not os_color_t values. The latter require 32
141 * bits because they can store 24-bit RGB values plus some special
142 * parameter codes, while our internal OSGEN_COLOR_xxx values are only a
143 * byte each.
144 */
145 #define OSGEN_COLOR 2
146
147
148 /*
149 * If this port is to use the default saved file overwrite check, define
150 * USE_OVWCHK. This routine tries to open the file; if successful, the
151 * file is closed and we ask the user if they're sure they want to overwrite
152 * the file.
153 */
154 #ifdef USE_OVWCHK
155 int os_chkovw(char *filename)
156 {
157 FILE *fp;
158
159 if ((fp = fopen( filename, "r" )) != 0)
160 {
161 char buf[128];
162
163 fclose(fp);
164 os_printz("That file already exists. Overwrite it? (y/n) >");
165 os_gets((uchar *)buf, sizeof(buf));
166 if (buf[0] != 'y' && buf[0] != 'Y')
167 return 1;
168 }
169 return 0;
170 }
171 #endif /* USE_OVWCHK */
172
173 /*
174 * non-stop mode does nothing in character-mode implementations, since the
175 * portable console layer handles MORE mode
176 */
177 void os_nonstop_mode(int flag)
178 {
179 }
180
181 /* ------------------------------------------------------------------------ */
182 /*
183 * Ports can implement os_flush and os_gets as calls to the stdio routines
184 * of the same name, and os_print and os_printz using the fputs() to
185 * stdout, by defining USE_STDIO. These definitions can be used for any
186 * port for which the standard C run-time library is available.
187 */
188
189 #ifdef USE_STDIO
190
191 /*
192 * os_printz works just like fputs() to stdout: we write a null-terminated
193 * string to the standard output.
194 */
195 void os_printz(const char *str)
196 {
197 fputs(str, stdout);
198 }
199
200 /*
201 * os_puts works like fputs() to stdout, except that we are given a
202 * separate length, and the string might not be null-terminated
203 */
204 void os_print(const char *str, size_t len)
205 {
206 printf("%.*s", (int)len, str);
207 }
208
209 /*
210 * os_flush forces output of anything buffered for standard output. It
211 * is generally used prior to waiting for a key (so the normal flushing
212 * may not occur, as it does when asking for a line of input).
213 */
214 void os_flush(void)
215 {
216 fflush(stdout);
217 }
218
219 /*
220 * update the display - since we're using text mode, there's nothing we
221 * need to do
222 */
223 void os_update_display(void)
224 {
225 }
226
227 /*
228 * os_gets performs the same function as gets(). It should get a
229 * string from the keyboard, echoing it and allowing any editing
230 * appropriate to the system, and return the null-terminated string as
231 * the function's value. The closing newline should NOT be included in
232 * the string.
233 */
234 uchar *os_gets(uchar *s, size_t bufl)
235 {
236 /* make sure everything we've displayed so far is actually displayed */
237 fflush(stdout);
238
239 /* get a line of input from the standard input */
240 return((uchar *)fgets((char *)s, bufl, stdin));
241 }
242
243 /*
244 * The default stdio implementation does not support reading a line of
245 * text with timeout.
246 */
247 int os_gets_timeout(unsigned char *buf, size_t bufl,
248 unsigned long timeout, int resume_editing)
249 {
250 /* tell the caller this operation is not supported */
251 return OS_EVT_NOTIMEOUT;
252 }
253
254 /*
255 * since we don't support os_gets_timeout(), we don't need to do anything
256 * in the cancel routine
257 */
258 void os_gets_cancel(int reset)
259 {
260 /* os_gets_timeout doesn't do anything, so neither do we */
261 }
262
263 /*
264 * Get an event - stdio version. This version does not accept a timeout
265 * value, and can only get a keystroke.
266 */
267 int os_get_event(unsigned long timeout, int use_timeout,
268 os_event_info_t *info)
269 {
270 /* if there's a timeout, return an error indicating we don't allow it */
271 if (use_timeout)
272 return OS_EVT_NOTIMEOUT;
273
274 /* get a key the normal way */
275 info->key[0] = os_getc();
276
277 /* if it's an extended key, get the other key */
278 if (info->key[0] == 0)
279 {
280 /* get the extended key code */
281 info->key[1] = os_getc();
282
283 /* if it's EOF, return an EOF event rather than a key event */
284 if (info->key[1] == CMD_EOF)
285 return OS_EVT_EOF;
286 }
287
288 /* return the keyboard event */
289 return OS_EVT_KEY;
290 }
291
292 #endif /* USE_STDIO */
293
294 /******************************************************************************
295 * Ports without any special initialization/termination requirements can define
296 * USE_NULLINIT to pick up the default definitions below. These do nothing, so
297 * ports requiring special handling at startup and/or shutdown time must define
298 * their own versions of these routines.
299 ******************************************************************************/
300
301 #ifdef USE_NULLINIT
302 /* os_init returns 0 for success, 1 for failure. The arguments are &argc, the
303 * address of the count of arguments to the program, and argv, the address of
304 * an array of up to 10 pointers to those arguments. For systems which don't
305 * pass a standard command line (such as the Mac Finder), the arguments should
306 * be read here using some alternate mechanism (an alert box, for instance),
307 * and the values of argc and argv[] updated accordingly. Note that a maximum
308 * of 10 arguments are allowed to be stored in the argv[] array. The command
309 * line itself can be stored in buf, which is a buffer passed by the caller
310 * guaranteed to be bufsiz bytes long.
311 *
312 * Unix conventions are followed, so argc is 1 when no arguments are present.
313 * The final argument is a prompt string which can be used to ask the user for
314 * a command line; its use is not required, but may be desirable for producing
315 * a relevant prompt message. See the Mac implementation for a detailed
316 * example of how this mechanism is used.
317 */
318 int os_init(int *argc, char *argv[], const char *prompt,
319 char *buf, int bufsiz)
320 {
321 return 0;
322 }
323
324 /*
325 * uninitialize
326 */
327 void os_uninit(void)
328 {
329 }
330
331 /*
332 * os_term should perform any necessary cleaning up, then terminate the
333 * program. The int argument is a return code to be passed to the
334 * caller, generally 0 for success and other for failure.
335 */
336 void os_term(int rc)
337 {
338 exit(rc);
339 }
340 #endif /* USE_NULLINIT */
341
342 /* ------------------------------------------------------------------------ */
343 /*
344 * Ports can define USE_NULLPAUSE if no pause is required on exit.
345 *
346 * Ports needing an exit pause, and can simply print a message (with
347 * os_print) and wait for a key (with os_getc) can define USE_EXPAUSE.
348 */
349
350 #ifdef USE_NULLPAUSE
351 void os_expause(void)
352 {
353 /* does nothing */
354 }
355 #endif /* USE_NULLPAUSE */
356
357 #ifdef USE_EXPAUSE
358 void os_expause(void)
359 {
360 os_printz("(Strike any key to exit...)");
361 os_flush();
362 os_waitc();
363 }
364 #endif /* USE_EXPAUSE */
365
366
367 #ifdef USE_NULLSTAT
368 /*
369 * USE_NULLSTAT defines a do-nothing version of os_status.
370 */
371 void os_status(int stat)
372 {
373 /* ignore the new status */
374 }
375
376 int os_get_status()
377 {
378 return 0;
379 }
380 #endif /* USE_NULLSTAT */
381
382 #ifdef USE_NULLSCORE
383 /*
384 * USE_NULLSCORE defines a do-nothing version of os_score.
385 */
386 void os_score(int cur, int turncount)
387 {
388 /* ignore the score information */
389 }
390
391 void os_strsc(const char *p)
392 {
393 /* ignore */
394 }
395 #endif /* USE_NULLSCORE */
396
397 /* ------------------------------------------------------------------------ */
398 /*
399 * Scrollback
400 */
401
402 /* forward-declare the window control structure type */
403 typedef struct osgen_win_t osgen_win_t;
404
405 /*
406 * We can be compiled with or without scrollback. The first version
407 * defines the scrollback implementation; the second just defines some
408 * dummy functions for the non-scrollback implementation.
409 */
410 # ifdef USE_SCROLLBACK
411
412 /* ------------------------------------------------------------------------ */
413 /*
414 * Character color structure
415 */
416 typedef struct osgen_charcolor_t osgen_charcolor_t;
417 struct osgen_charcolor_t
418 {
419 /* foreground color, as an OSGEN_COLOR_xxx value */
420 char fg;
421
422 /* background color, as an OSGEN_COLOR_xxx value */
423 char bg;
424 };
425
426 /*
427 * Window control structure. Each on-screen window is represented by one
428 * of these structures.
429 */
430 struct osgen_win_t
431 {
432 /* type of the window - this is an OS_BANNER_TYPE_xxx code */
433 int win_type;
434
435 /* flags */
436 unsigned int flags;
437
438 /* next window in window list */
439 osgen_win_t *nxt;
440
441 /*
442 * Parent window. We take our display area out of the parent window's
443 * allocated space at the time we lay out this window.
444 */
445 osgen_win_t *parent;
446
447 /* head of list of children of this window */
448 osgen_win_t *first_child;
449
450 /*
451 * The window's alignment type - this determines where the window goes
452 * relative to the main window. This is an OS_BANNER_ALIGN_xxx
453 * alignment type code.
454 */
455 int alignment;
456
457 /* size type (as an OS_BANNER_SIZE_xxx value) */
458 int size_type;
459
460 /*
461 * The window's size. If size_type is OS_BANNER_SIZE_ABS, this is the
462 * size of the window in character cells. If size_type is
463 * OS_BANNER_SIZE_PCT, this is given as a percentage of the full screen
464 * size.
465 */
466 int size;
467
468 /* upper-left corner position of window on screen */
469 int winx;
470 int winy;
471
472 /* size of window on screen */
473 size_t wid;
474 size_t ht;
475
476 /*
477 * Cursor position (location of next output). These are given in
478 * "document coordinates", which is to say that they're relative to
479 * the start of the text in the buffer.
480 *
481 * To translate to "window coordinates", simply subtract the scrolling
482 * offsets, which give the document coordinates of the first character
483 * displayed at the upper left corner of the window.
484 *
485 * To translate to absolute screen coordinates, first subtract the
486 * scrolling offsets to get window coordinates, then add the window's
487 * screen position (winx,winy).
488 */
489 int x;
490 int y;
491
492 /*
493 * maximum row and line where we've actually written any text (this
494 * can be used for purposes like setting horizontal scrollbar limits
495 * and sizing a window horizontally to its contents)
496 */
497 int xmax;
498 int ymax;
499
500 /* scrolling offset of text displayed in window */
501 int scrollx;
502 int scrolly;
503
504 /*
505 * current text foreground and background colors (as OSGEN_COLOR_xxx
506 * values)
507 */
508 char txtfg;
509 char txtbg;
510
511 /* current text attributes */
512 int txtattr;
513
514 /* window fill color (and oss color code for same) */
515 char fillcolor;
516 int oss_fillcolor;
517 };
518
519 /*
520 * Ordinary text stream window. This is a subclass of the basic
521 * osgen_win_t window type, used when win_type is OS_BANNER_TYPE_TEXT.
522 *
523 * Ordinary text windows keep a circular buffer of scrollback text. We
524 * optimize space by using escape codes embedded in the saved text stream
525 * to select colors and attributes.
526 *
527 * In the circular text buffer, each line ends with a null byte. We keep
528 * an array of line-start pointers to make it fast and easy to find the
529 * first byte of a particular line. The line-start array is also
530 * circular, and is organized in ascending order of row number.
531 */
532 typedef struct osgen_txtwin_t osgen_txtwin_t;
533 struct osgen_txtwin_t
534 {
535 /* embed the base class */
536 osgen_win_t base;
537
538 /* text color and attributes at start of latest line */
539 int solfg;
540 int solbg;
541 int solattr;
542
543 /* window text buffer, and size of the buffer */
544 char *txtbuf;
545 size_t txtbufsiz;
546
547 /* next free byte of window text buffer */
548 char *txtfree;
549
550 /* circular array of line-start pointers */
551 char **line_ptr;
552 size_t line_ptr_cnt;
553
554 /* index of first line-start pointer in use */
555 size_t first_line;
556
557 /* number of lines of text stored in the buffer */
558 size_t line_count;
559 };
560
561 /*
562 * Text Grid window. This is a subclass of the basic window type,
563 * osgen_win_t, used when win_type is OS_BANNER_TYPE_TEXTGRID.
564 *
565 * A text grid window keeps a simple rectangular array of text, and a
566 * corresponding array of the color of each character. The size of each
567 * array is at least as large as the window's actual area on the screen;
568 * when we resize the window, we'll reallocate the arrays at a larger size
569 * if the window has expanded beyond the stored size. We don't keep any
570 * scrollback information in a text grid; we only keep enough to cover
571 * what's actually on the screen.
572 */
573 typedef struct osgen_gridwin_t osgen_gridwin_t;
574 struct osgen_gridwin_t
575 {
576 /* embed the base class */
577 osgen_win_t base;
578
579 /* width and height of the text and color arrays */
580 size_t grid_wid;
581 size_t grid_ht;
582
583 /* text array */
584 char *grid_txt;
585
586 /* color array */
587 osgen_charcolor_t *grid_color;
588 };
589
590 /*
591 * Window flags
592 */
593
594 /* keep the cursor visible when adding text to the window */
595 #define OSGEN_AUTO_VSCROLL 0x0001
596
597 /* the window buffer is full and we're allowing nothing more to enter it */
598 #define OSGEN_FULL 0x0002
599
600 /* the window is in deferred-redraw mode */
601 #define OSGEN_DEFER_REDRAW 0x0004
602
603 /*
604 * MORE mode in the banner. Note that we keep track of this only so that
605 * we can indicate it on queries for the banner style; we count on the
606 * caller to handle the actual prompting for us.
607 */
608 #define OSGEN_MOREMODE 0x0008
609
610 /* child banner "strut" flags */
611 #define OSGEN_VSTRUT 0x0010
612 #define OSGEN_HSTRUT 0x0020
613
614 /*
615 * The main text area window. This window is special, because it's the
616 * root of the window tree.
617 */
618 static osfar_t osgen_txtwin_t *S_main_win = 0;
619
620 /* default status line window */
621 static osfar_t osgen_txtwin_t *S_status_win = 0;
622
623 /* default input/output window (for os_print, os_gets, etc) */
624 static osfar_t osgen_txtwin_t *S_default_win = 0;
625
626 /* current scrollback-mode window */
627 static osfar_t osgen_txtwin_t *S_sbmode_win = 0;
628
629 /* scrollback mode settings */
630 static osfar_t int S_sbmode_orig_scrolly;
631 static osfar_t int S_sbmode_orig_x;
632 static osfar_t char **S_sbmode_orig_last_line;
633 static osfar_t char *S_sbmode_orig_txtfree;
634
635 /*
636 * flag: we're using a special cursor position; we use this to override our
637 * normal default cursor position
638 */
639 static osfar_t int S_special_cursor_pos = FALSE;
640 static osfar_t int S_special_cursor_x = 0;
641 static osfar_t int S_special_cursor_y = 0;
642
643 /*
644 * Flag: deferred redraw required. This indicates that something happened
645 * that requires redrawing the screen, but we didn't bother actually doing
646 * the redrawing immediately in case other things that would also require
647 * redrawing were to occur shortly.
648 */
649 static osfar_t int S_deferred_redraw = FALSE;
650
651 /*
652 * Input buffer state. This information is defined statically because
653 * os_gets_timeout() can carry the information from invocation to
654 * invocation when input editing is interrupted by a tmieout.
655 */
656 static osfar_t char S_gets_internal_buf[256]; /* internal save buffer */
657 static osfar_t char *S_gets_buf = S_gets_internal_buf; /* current save buf */
658 static osfar_t char *S_gets_buf_end = 0; /* end of input buffer */
659 static osfar_t size_t S_gets_buf_siz = sizeof(S_gets_internal_buf); /* size */
660 static osfar_t int S_gets_ofs; /* offset in buffer of insertion point */
661 static osfar_t char *S_gets_curhist; /* current history pointer */
662 static osfar_t int S_gets_x, S_gets_y; /* saved cursor position */
663
664 # ifdef USE_HISTORY
665 /* save buffer for line being edited before history recall began */
666 static osfar_t char S_hist_sav_internal[256];
667 static osfar_t char *S_hist_sav = S_hist_sav_internal;
668 static osfar_t size_t S_hist_sav_siz = sizeof(S_hist_sav_internal);
669 # endif /* USE_HISTORY */
670
671 /* strcpy with destination buffer size limit */
672 extern void safe_strcpy(char *dst, size_t dstlen, const char *src);
673
674 /*
675 * Flag: input is already in progress. When os_gets_timeout() returns
676 * with OS_EVT_TIMEOUT, it sets this flag to true. os_gets_cancel() sets
677 * this flag to false.
678 *
679 * When os_gets_timeout() is called again, it checks this flag to see if
680 * the input session was cancelled; if not, the routine knows that the
681 * partially-edited input line is already displayed where it left off,
682 * because the display has not been modified since the interrupted call to
683 * os_gets_timeout() returned.
684 */
685 static osfar_t int S_gets_in_progress = FALSE;
686
687 /* forward declarations */
688 static void osssb_add_color_code(osgen_txtwin_t *win);
689 static void osgen_gridwin_clear(osgen_gridwin_t *win, size_t ofs, size_t len);
690 static void osgen_redraw_win(osgen_win_t *win);
691 static void osgen_scrdisp(osgen_win_t *win, int x, int y, int len);
692 static void osgen_gets_redraw_cmdline(void);
693
694 /*
695 * Delete a window
696 */
697 static void osgen_delete_win(osgen_win_t *win)
698 {
699 osgen_win_t *prv;
700 osgen_win_t *cur;
701 osgen_txtwin_t *twin;
702 osgen_gridwin_t *gwin;
703
704 /* if we have a parent, remove ourselves from our parent's child list */
705 if (win->parent != 0)
706 {
707 /* scan our parent's child list */
708 for (prv = 0, cur = win->parent->first_child ;
709 cur != 0 && cur != win ;
710 prv = cur, cur = cur->nxt) ;
711
712 /* if we found it, unlink it */
713 if (cur != 0)
714 {
715 /* set the previous item's forward pointer */
716 if (prv == 0)
717 win->parent->first_child = win->nxt;
718 else
719 prv->nxt = win->nxt;
720 }
721 }
722
723 /*
724 * Remove the parent reference from each child of this window. We're
725 * going to be deleted, so we can't keep references from our children
726 * to us.
727 */
728 for (cur = win->first_child ; cur != 0 ; cur = cur->nxt)
729 cur->parent = 0;
730
731 /* delete the window according to its type */
732 switch(win->win_type)
733 {
734 case OS_BANNER_TYPE_TEXT:
735 /* get the text window subclass data */
736 twin = (osgen_txtwin_t *)win;
737
738 /* delete its scrollback buffer, if it has one */
739 if (twin->txtbuf != 0)
740 osfree(twin->txtbuf);
741
742 /* delete the line pointers */
743 if (twin->line_ptr != 0)
744 osfree(twin->line_ptr);
745 break;
746
747 case OS_BANNER_TYPE_TEXTGRID:
748 /* get the grid window subclass data */
749 gwin = (osgen_gridwin_t *)win;
750
751 /* delete the character and color arrays */
752 if (gwin->grid_txt != 0)
753 osfree(gwin->grid_txt);
754 if (gwin->grid_color != 0)
755 osfree(gwin->grid_color);
756 break;
757 }
758
759 /* delete the window itself */
760 osfree(win);
761 }
762
763 /*
764 * Delete a window and all of its children
765 */
766 static void osgen_delete_win_tree(osgen_win_t *win)
767 {
768 osgen_win_t *chi;
769 osgen_win_t *nxt;
770
771 /* delete the children first */
772 for (chi = win->first_child ; chi != 0 ; chi = nxt)
773 {
774 /* remember the next one before we delete the current one */
775 nxt = chi->nxt;
776
777 /* delete this one */
778 osgen_delete_win_tree(chi);
779 }
780
781 /* delete this window */
782 osgen_delete_win(win);
783 }
784
785 /*
786 * Create a window and link it into our list. We initialize the window
787 * and allocate its display buffer, but we do NOT set the window's size or
788 * position on the screen.
789 */
790 static osgen_win_t *osgen_create_win(int win_type, int where, void *other,
791 osgen_win_t *parent)
792 {
793 osgen_win_t *win;
794 osgen_win_t *prv;
795 osgen_win_t *cur;
796 size_t struct_siz;
797
798 /* figure the structure size based on the window type */
799 switch(win_type)
800 {
801 case OS_BANNER_TYPE_TEXT:
802 struct_siz = sizeof(osgen_txtwin_t);
803 break;
804
805 case OS_BANNER_TYPE_TEXTGRID:
806 struct_siz = sizeof(osgen_gridwin_t);
807 break;
808
809 default:
810 /* unrecognized type - return failure */
811 return 0;
812 }
813
814 /* create the window object */
815 win = (osgen_win_t *)osmalloc(struct_siz);
816 if (win == 0)
817 return 0;
818
819 /* remember the type */
820 win->win_type = win_type;
821
822 /* it's not in a window list yet */
823 win->nxt = 0;
824
825 /* initialize with default colors */
826 win->txtfg = OSGEN_COLOR_TEXT;
827 win->txtbg = OSGEN_COLOR_TRANSPARENT;
828 win->txtattr = 0;
829 win->fillcolor = OSGEN_COLOR_TEXTBG;
830
831 /* cache the oss translation of the fill color */
832 win->oss_fillcolor = ossgetcolor(OSGEN_COLOR_TEXT, OSGEN_COLOR_TEXTBG,
833 0, 0);
834
835 /* start at the upper left corner */
836 win->x = 0;
837 win->y = 0;
838 win->scrollx = 0;
839 win->scrolly = 0;
840
841 /*
842 * the window's position on screen will eventually be set by
843 * osgen_recalc_layout(), but initialize the position to a reasonable
844 * value for now in case anyone looks at it before then
845 */
846 win->winx = 0;
847 win->winy = 0;
848
849 /* we haven't seen any text in the window yet */
850 win->xmax = 0;
851 win->ymax = 0;
852
853 /* clear the flags */
854 win->flags = 0;
855
856 /* remember our parent */
857 win->parent = parent;
858
859 /* we have no children yet */
860 win->first_child = 0;
861
862 /* if there's a parent, insert the window into the parent's child list */
863 if (parent != 0)
864 {
865 /* insert into the parent's child list at the proper point */
866 switch(where)
867 {
868 case OS_BANNER_FIRST:
869 /* link it at the head of the list */
870 win->nxt = parent->first_child;
871 parent->first_child = win;
872 break;
873
874 case OS_BANNER_LAST:
875 default:
876 /* find the end of the parent's list */
877 for (cur = parent->first_child ; cur != 0 && cur->nxt != 0 ;
878 cur = cur->nxt) ;
879
880 /* link it after the last element */
881 win->nxt = 0;
882 if (cur != 0)
883 cur->nxt = win;
884 else
885 parent->first_child = win;
886
887 /* done */
888 break;
889
890 case OS_BANNER_BEFORE:
891 case OS_BANNER_AFTER:
892 /* scan the parent's child list, looking for 'other' */
893 for (prv = 0, cur = parent->first_child ;
894 cur != 0 && cur != other ;
895 prv = cur, cur = cur->nxt) ;
896
897 /*
898 * if we didn't find 'other', link the new window at the tail
899 * of the list by default; since 'prv' will be the last item if
900 * we didn't find 'other', we can simply set the link mode to
901 * 'before' to link before the placeholder null at the end of
902 * the list
903 */
904 if (cur == 0)
905 where = OS_BANNER_BEFORE;
906
907 /* if we're linking after 'cur', advance one position */
908 if (where == OS_BANNER_AFTER)
909 {
910 prv = cur;
911 cur = cur->nxt;
912 }
913
914 /* link before 'cur', which is right after 'prv' */
915 win->nxt = cur;
916 if (prv != 0)
917 prv->nxt = win;
918 else
919 parent->first_child = win;
920
921 /* done */
922 break;
923 }
924 }
925
926 /* return the new window */
927 return win;
928 }
929
930 /*
931 * Create a text window
932 */
933 static osgen_txtwin_t *osgen_create_txtwin(int where, void *other,
934 void *parent,
935 unsigned int buf_size,
936 unsigned int buf_lines)
937 {
938 osgen_txtwin_t *win;
939
940 /* create the base window */
941 win = (osgen_txtwin_t *)osgen_create_win(OS_BANNER_TYPE_TEXT,
942 where, other, parent);
943
944 /* if that failed, give up now */
945 if (win == 0)
946 return 0;
947
948 /* allocate the window's buffer */
949 win->txtbufsiz = buf_size;
950 win->txtbuf = (char *)osmalloc(buf_size);
951
952 /* allocate the line starts */
953 win->line_ptr_cnt = buf_lines;
954 win->line_ptr = (char **)osmalloc(buf_lines * sizeof(char *));
955
956 /* make sure we allocated everything properly */
957 if (win->txtbuf == 0 || win->line_ptr == 0)
958 {
959 /* free anything we allocated */
960 osgen_delete_win(&win->base);
961
962 /* return failure */
963 return 0;
964 }
965
966 /* set up the buffer free pointer */
967 win->txtfree = win->txtbuf;
968
969 /* start out with a single line in the buffer */
970 win->line_count = 1;
971
972 /* set up the first line start */
973 win->first_line = 0;
974 win->line_ptr[0] = win->txtbuf;
975
976 /* initialize the start-of-line colors */
977 win->solfg = win->base.txtfg;
978 win->solbg = win->base.txtbg;
979 win->solattr = win->base.txtattr;
980
981 /* start the new first line with the current text color */
982 osssb_add_color_code(win);
983
984 /* return the new window */
985 return win;
986 }
987
988 /*
989 * Create a text grid window
990 */
991 static osgen_gridwin_t *osgen_create_gridwin(int where, void *other,
992 void *parent,
993 int grid_wid, int grid_ht)
994 {
995 osgen_gridwin_t *win;
996
997 /* create the base window */
998 win = (osgen_gridwin_t *)osgen_create_win(OS_BANNER_TYPE_TEXTGRID,
999 where, other, parent);
1000
1001 /* if that failed, give up now */
1002 if (win == 0)
1003 return 0;
1004
1005 /* allocate the grid to the requested size */
1006 win->grid_wid = grid_wid;
1007 win->grid_ht = grid_ht;
1008 win->grid_txt = (char *)osmalloc(grid_wid * grid_ht);
1009 win->grid_color = (osgen_charcolor_t *)osmalloc(
1010 grid_wid * grid_ht * sizeof(osgen_charcolor_t));
1011
1012 /* if we failed to allocate, delete the window and abort */
1013 if (win->grid_txt == 0 || win->grid_color == 0)
1014 {
1015 /* free anything we allocated */
1016 osgen_delete_win(&win->base);
1017
1018 /* return failure */
1019 return 0;
1020 }
1021
1022 /* clear the grid text and color arrays */
1023 osgen_gridwin_clear(win, 0, win->grid_wid * win->grid_ht);
1024
1025 /* return the new window */
1026 return win;
1027 }
1028
1029 /*
1030 * Clear a window
1031 */
1032 static void osgen_clear_win(osgen_win_t *win)
1033 {
1034 osgen_txtwin_t *twin;
1035 osgen_gridwin_t *gwin;
1036
1037 /* check the type */
1038 switch(win->win_type)
1039 {
1040 case OS_BANNER_TYPE_TEXT:
1041 /* get the text window subclass data */
1042 twin = (osgen_txtwin_t *)win;
1043
1044 /* reset the window's scrollback buffer */
1045 twin->txtfree = twin->txtbuf;
1046 twin->line_count = 1;
1047 twin->first_line = 0;
1048 twin->line_ptr[0] = twin->txtbuf;
1049
1050 /* start the new first line with the current text color */
1051 osssb_add_color_code(twin);
1052
1053 /* done */
1054 break;
1055
1056 case OS_BANNER_TYPE_TEXTGRID:
1057 /* get the grid window subclass data */
1058 gwin = (osgen_gridwin_t *)win;
1059
1060 /* clear the entire grid */
1061 osgen_gridwin_clear(gwin, 0, gwin->grid_wid * gwin->grid_ht);
1062
1063 /* done */
1064 break;
1065 }
1066
1067 /* put the cursor at the top left of the window */
1068 win->x = 0;
1069 win->y = 0;
1070
1071 /* set the scrolling position to the top of the window */
1072 win->scrollx = 0;
1073 win->scrolly = 0;
1074
1075 /* the content size is now zero */
1076 win->xmax = 0;
1077 win->ymax = 0;
1078
1079 /* if the buffer was full, it's full no longer */
1080 win->flags &= ~OSGEN_FULL;
1081
1082 /*
1083 * If this window is not auto-scrolling, don't actually clear it
1084 * visually right now, but just set a deferred-redraw on the window;
1085 * clearing a window that doesn't automatically scroll usually means
1086 * that we're updating some live status information, so we use deferred
1087 * drawing on these windows to reduce flicker during updates. If the
1088 * window automatically scrolls, then the caller presumably is
1089 * displaying more sizable data, and thus would want the user to see
1090 * the text output as it occurs.
1091 *
1092 * Note that both approaches (deferred vs immediate redraw) yield the
1093 * same results in the end, because we always stop deferring redraws
1094 * when we stop to ask for input or simply pause for a time delay; the
1095 * only difference is that deferred redrawing reduces flicker by
1096 * gathering all the updates into a single operation, while immediate
1097 * update might flicker more but shows individual bits of text output
1098 * as they occur.
1099 */
1100 if (!(win->flags & OSGEN_AUTO_VSCROLL))
1101 {
1102 /* defer redrawing until we need to */
1103 win->flags |= OSGEN_DEFER_REDRAW;
1104 }
1105 else
1106 {
1107 /* clear the window's on-screen area with its fill color */
1108 ossclr(win->winy, win->winx,
1109 win->winy + win->ht - 1, win->winx + win->wid - 1,
1110 win->oss_fillcolor);
1111 }
1112 }
1113
1114 /*
1115 * Display text in a window, all in a single color, truncating the display
1116 * if we start before the left edge of the window or go past the right
1117 * edge of the window. The x,y coordinates are given in window
1118 * coordinates.
1119 */
1120 static void osgen_disp_trunc(osgen_win_t *win, int y, int x, int oss_color,
1121 const char *p)
1122 {
1123 char buf[64];
1124 size_t chars_rem;
1125 size_t wid_rem;
1126
1127 /* if we have deferred redrawing pending, don't bother drawing now */
1128 if (S_deferred_redraw || (win->flags & OSGEN_DEFER_REDRAW) != 0)
1129 return;
1130
1131 /* get the number of characters to be displayed */
1132 chars_rem = strlen(p);
1133
1134 /* calculate the amount of space we have to work with on the screen */
1135 wid_rem = win->wid - (x >= 0 ? x : 0);
1136
1137 /* make sure at least some of the text overlaps the window */
1138 if (y < 0 || y >= (int)win->ht
1139 || x + (int)chars_rem <= 0 || x >= (int)win->wid)
1140 return;
1141
1142 /*
1143 * if we're starting to the left of the window, skip characters up to
1144 * the first character visible in the window
1145 */
1146 if (x < 0)
1147 {
1148 /*
1149 * get the number of characters to skip - this is simply the
1150 * negative of the x position, since x position zero is the first
1151 * column to display
1152 */
1153 x = -x;
1154
1155 /*
1156 * if we don't have enough characters to reach the left edge of
1157 * the window, we have nothing to do
1158 */
1159 if (chars_rem <= (size_t)x)
1160 return;
1161
1162 /* skip the desired number of characters */
1163 chars_rem -= x;
1164 p += x;
1165
1166 /* we've skipped up to column zero, so proceed from there */
1167 x = 0;
1168 }
1169
1170 /* if the text entirely fits, display it and we're done */
1171 if (chars_rem < wid_rem)
1172 {
1173 /* display the entire string */
1174 ossdsp(win->winy + y, win->winx + x, oss_color, p);
1175
1176 /* done */
1177 return;
1178 }
1179
1180 /*
1181 * we have too much to display, so display as much as will fit - keep
1182 * going until we run out of space for the display (we know we'll run
1183 * out of space before we run out of text, because we know we have too
1184 * much text to fit)
1185 */
1186 while (wid_rem != 0)
1187 {
1188 size_t cur;
1189
1190 /* display as much as will fit, up to our buffer length */
1191 cur = wid_rem;
1192 if (cur > sizeof(buf) - 1)
1193 cur = sizeof(buf) - 1;
1194
1195 /* copy this portion to our buffer and null-terminate it */
1196 memcpy(buf, p, cur);
1197 buf[cur] = '\0';
1198
1199 /* display it */
1200 ossdsp(win->winy + y, win->winx + x, oss_color, buf);
1201
1202 /* advance our screen position */
1203 x += cur;
1204
1205 /* advance past the source material we just displayed */
1206 p += cur;
1207
1208 /* deduct the on-screen space we consumed from the remaining space */
1209 wid_rem -= cur;
1210 }
1211 }
1212
1213 /* ------------------------------------------------------------------------ */
1214 /*
1215 * Initialize the scrollback buffer. This initializes the main text area
1216 * window.
1217 *
1218 * The oss code should call this during initialization to set up the
1219 * osgen3-layer display management; note that this routine must be called
1220 * from the oss code AFTER the screen size has been determined and stored
1221 * in the global variables G_oss_screen_width and G_oss_screen_height.
1222 */
1223 void osssbini(unsigned int size)
1224 {
1225 osgen_txtwin_t *win;
1226
1227 /* allocate our main window if we're not in 'plain' mode */
1228 if (!os_f_plain)
1229 {
1230 /*
1231 * Initialize the main window. The main window has no parent,
1232 * since it's the root of the window tree.
1233 */
1234 S_main_win = win = osgen_create_txtwin(
1235 OS_BANNER_FIRST, 0, 0, size, size/40);
1236 if (win == 0)
1237 {
1238 /* show a message and give up */
1239 printf("There is not enough memory to run this program.\n");
1240 exit(1);
1241 }
1242
1243 /* make this the default window */
1244 S_default_win = win;
1245
1246 /* scroll to keep the cursor visible when writing to this window */
1247 win->base.flags |= OSGEN_AUTO_VSCROLL;
1248
1249 /* initially give the window the entire screen */
1250 win->base.winy = 0;
1251 win->base.winx = 0;
1252 win->base.wid = G_oss_screen_width;
1253 win->base.ht = G_oss_screen_height;
1254
1255 /* set the initial page size */
1256 G_os_linewidth = win->base.wid;
1257 G_os_pagelength = (win->base.ht > 2
1258 ? win->base.ht - 2
1259 : win->base.ht > 1
1260 ? win->base.ht - 1 : win->base.ht);
1261
1262 /* set the initial color scheme to the default text color */
1263 os_set_text_color(OS_COLOR_P_TEXT, OS_COLOR_P_TRANSPARENT);
1264 os_set_screen_color(OS_COLOR_P_TEXTBG);
1265
1266 /* clear the window */
1267 osgen_clear_win(&win->base);
1268 }
1269 else
1270 {
1271 /* use the entire available display */
1272 G_os_linewidth = G_oss_screen_width;
1273 G_os_pagelength = G_oss_screen_height - 2;
1274 }
1275 }
1276
1277 /*
1278 * Delete the scrollback buffer. The oss code should call this during
1279 * program termination to free memory allocated by the osgen3 layer.
1280 */
1281 void osssbdel(void)
1282 {
1283 /* delete the main window and its children */
1284 if (S_main_win != 0)
1285 osgen_delete_win_tree(&S_main_win->base);
1286 }
1287
1288 /* ------------------------------------------------------------------------ */
1289 /*
1290 * Scrollback operations. Scrollback applies only to ordinary text
1291 * windows.
1292 */
1293
1294 /*
1295 * advance a pointer in a scrollback buffer
1296 */
1297 static char *ossadvsp(osgen_txtwin_t *win, char *p)
1298 {
1299 /* move to the next byte */
1300 ++p;
1301
1302 /* if we've passed the end of the buffer, wrap to the beginning */
1303 if (p >= win->txtbuf + win->txtbufsiz)
1304 p = win->txtbuf;
1305
1306 /* return the pointer */
1307 return p;
1308 }
1309
1310 /*
1311 * decrement a scrollback buffer pointer
1312 */
1313 static char *ossdecsp(osgen_txtwin_t *win, char *p)
1314 {
1315 /* if we're at the start of the buffer, wrap to just past the end */
1316 if (p == win->txtbuf)
1317 p = win->txtbuf + win->txtbufsiz;
1318
1319 /* move to the previous byte */
1320 --p;
1321
1322 /* return the pointer */
1323 return p;
1324 }
1325
1326 /*
1327 * Get the line pointer for the given line
1328 */
1329 static char **osgen_get_line_ptr(osgen_txtwin_t *win, int y)
1330 {
1331 size_t idx;
1332
1333 /* if the line doesn't exist, return null */
1334 if (y < 0 || (size_t)y >= win->line_count)
1335 return 0;
1336
1337 /* add the line number to the base index to get the linear index */
1338 idx = y + win->first_line;
1339
1340 /* wrap back to the start of the array if necessary */
1341 if (idx >= win->line_ptr_cnt)
1342 idx -= win->line_ptr_cnt;
1343
1344 /* return the line at this index */
1345 return &win->line_ptr[idx];
1346 }
1347
1348 /*
1349 * Get a pointer to the text of the line in the given window at the given
1350 * document y coordinate.
1351 */
1352 static char *osgen_get_line(osgen_txtwin_t *win, int y)
1353 {
1354 char **line_ptr;
1355
1356 /* get the line pointer for the given line */
1357 line_ptr = osgen_get_line_ptr(win, y);
1358
1359 /* return the text it points to, or null if there's no such line */
1360 return (line_ptr != 0 ? *line_ptr : 0);
1361 }
1362
1363 /*
1364 * Add a byte to the scrollback buffer. Returns true if the byte was
1365 * successfully added, false if not.
1366 */
1367 static int osssb_add_byte(osgen_txtwin_t *win, char c)
1368 {
1369 char *nxtfree;
1370
1371 /* if the window is full, indicate failure */
1372 if ((win->base.flags & OSGEN_FULL) != 0)
1373 return FALSE;
1374
1375 /*
1376 * If there's no room, and we're not in auto-vscroll mode, simply
1377 * discard the text. A window that is not in auto-vscroll mode is
1378 * designed to always show the oldest text, so if we overflow the
1379 * buffer, then we want to start discarding the newest test.
1380 */
1381 nxtfree = ossadvsp(win, win->txtfree);
1382 if (nxtfree == win->line_ptr[win->first_line]
1383 && !(win->base.flags & OSGEN_AUTO_VSCROLL))
1384 {
1385 /*
1386 * The buffer is full, and we're not in auto-vscroll mode -
1387 * discard the new text. Before we do, set the current last byte
1388 * to null, to terminate the current line at this point.
1389 */
1390 *win->txtfree = '\0';
1391
1392 /* set the 'full' flag */
1393 win->base.flags |= OSGEN_FULL;
1394
1395 /* indicate failure */
1396 return FALSE;
1397 }
1398
1399 /* add the character to the buffer */
1400 *win->txtfree = c;
1401
1402 /* advance the free pointer */
1403 win->txtfree = nxtfree;
1404
1405 /*
1406 * if the free pointer has just collided with the start of the oldest
1407 * line, delete the oldest line
1408 */
1409 if (win->txtfree == win->line_ptr[win->first_line])
1410 {
1411 /* delete the oldest line to make room for the new data */
1412 win->first_line++;
1413 if (win->first_line >= win->line_ptr_cnt)
1414 win->first_line = 0;
1415
1416 /* we have one less line in the buffer */
1417 win->line_count--;
1418
1419 /* adjust our document cooordinates for the loss of a line */
1420 win->base.y--;
1421
1422 /*
1423 * Adjust the scrolling position for the loss of a line. If the
1424 * window is displaying the line that we just deleted (i.e., the
1425 * window's vertical scroll offset is zero), we must redraw the
1426 * window; otherwise, the change won't affect the display, so we
1427 * can merely adjust the scrolling offset.
1428 */
1429 if (win->base.scrolly == 0)
1430 win->base.flags |= OSGEN_DEFER_REDRAW;
1431 else
1432 win->base.scrolly--;
1433 }
1434
1435 /* we successfully added the byte */
1436 return TRUE;
1437 }
1438
1439 /*
1440 * Add a given number of bytes. The bytes will be added as a unit - if we
1441 * can't add all of the bytes, we won't add any of them.
1442 */
1443 static int osssb_add_bytes(osgen_txtwin_t *win, const char *p, size_t len)
1444 {
1445 char *orig_free;
1446
1447 /* note the original free pointer */
1448 orig_free = win->txtfree;
1449
1450 /* add the bytes */
1451 for ( ; len != 0 ; --len)
1452 {
1453 /* try adding this byte */
1454 if (!osssb_add_byte(win, *p++))
1455 {
1456 /*
1457 * failure - forget everything we've added by resetting the
1458 * free pointer to its original value on entry
1459 */
1460 win->txtfree = orig_free;
1461
1462 /* indicate failure */
1463 return FALSE;
1464 }
1465 }
1466
1467 /* we added all of the bytes successfully */
1468 return TRUE;
1469 }
1470
1471 /*
1472 * Add a "safety" null byte. This adds a null byte at the end of the
1473 * buffer, in case we need to inspect the current line before we add any
1474 * more text.
1475 */
1476 static void osssb_add_safety_null(osgen_txtwin_t *win)
1477 {
1478 /* add the null */
1479 if (osssb_add_byte(win, '\0'))
1480 {
1481 /*
1482 * we added the null - decrement the free pointer so we overwrite
1483 * the null byte if we add any more text to the current line
1484 */
1485 win->txtfree = ossdecsp(win, win->txtfree);
1486 }
1487 }
1488
1489 /*
1490 * Add an appropriate color code to the scrollback buffer to yield the
1491 * current color setting.
1492 */
1493 static void osssb_add_color_code(osgen_txtwin_t *win)
1494 {
1495 char buf[10];
1496
1497 /* if we have any attributes, add an attribute code */
1498 if (win->base.txtattr != 0)
1499 {
1500 /* add the attribute code sequence */
1501 buf[0] = OSGEN_ATTR;
1502 buf[1] = (char)win->base.txtattr;
1503 osssb_add_bytes(win, buf, 2);
1504 }
1505
1506 /* if we're using the plain text color, add a color code */
1507 if (win->base.txtfg != OSGEN_COLOR_TEXT
1508 || win->base.txtbg != OSGEN_COLOR_TRANSPARENT)
1509 {
1510 /* add the explicit color code */
1511 buf[0] = OSGEN_COLOR;
1512 buf[1] = win->base.txtfg;
1513 buf[2] = win->base.txtbg;
1514 osssb_add_bytes(win, buf, 3);
1515 }
1516
1517 /* add a safety null terminator */
1518 osssb_add_safety_null(win);
1519 }
1520
1521 /*
1522 * Start a new line in the scrollback buffer. Returns true on success,
1523 * false if there's no space to add the newline character.
1524 */
1525 static int osssb_new_line(osgen_txtwin_t *win)
1526 {
1527 char **line_ptr;
1528
1529 /* add a null byte to mark the end of the current line */
1530 if (!osssb_add_byte(win, '\0'))
1531 return FALSE;
1532
1533 /* if the window is full, indicate failure */
1534 if ((win->base.flags & OSGEN_FULL) != 0)
1535 return FALSE;
1536
1537 /*
1538 * If the line buffer is full, and this window doesn't automatically
1539 * scroll vertically, drop the new text. When we're not in
1540 * auto-vscroll mode, we drop text from the end of the buffer rather
1541 * than from the beginning.
1542 */
1543 if (win->line_count + 1 > win->line_ptr_cnt
1544 && !(win->base.flags & OSGEN_AUTO_VSCROLL))
1545 {
1546 /* set the 'full' flag */
1547 win->base.flags |= OSGEN_FULL;
1548
1549 /* indicate failure */
1550 return FALSE;
1551 }
1552
1553 /* move to the left column */
1554 win->base.x = 0;
1555
1556 /* add a line start */
1557 if (win->line_count >= win->line_ptr_cnt)
1558 {
1559 /* forget the original first line */
1560 win->first_line++;
1561 if (win->first_line == win->line_ptr_cnt)
1562 win->first_line = 0;
1563
1564 /*
1565 * Since we took away one line and we're adding another, the total
1566 * line count and the document 'y' position aren't changing - but
1567 * the line at the document 'y' position now belongs to the new
1568 * last line.
1569 *
1570 * Adjust the scrolling position for the loss of a line at the
1571 * start of the buffer. This doesn't change what we're displaying
1572 * on the screen, because we're simply reducing the amount of text
1573 * in the buffer before the top of the screen, so we must
1574 * compensate by decrementing the document coordinate of the top
1575 * line in the window. However, if the line we're taking away is
1576 * showing in the window, then we do need to redraw the contents,
1577 * because the change will be visible; set the deferred redraw
1578 * flag for the window in this case so that we eventually redraw
1579 * the whole thing.
1580 */
1581 if (win->base.scrolly == 0)
1582 win->base.flags |= OSGEN_DEFER_REDRAW;
1583 else
1584 win->base.scrolly--;
1585 }
1586 else
1587 {
1588 /* count the new line */
1589 win->line_count++;
1590
1591 /* note the highest output 'y' position if this is it */
1592 if (win->base.y > win->base.ymax)
1593 win->base.ymax = win->base.y;
1594
1595 /* increase the document y position for the new line */
1596 win->base.y++;
1597 }
1598
1599 /* get the line pointer for the new last line */
1600 line_ptr = osgen_get_line_ptr(win, win->base.y);
1601
1602 /*
1603 * set the new line pointer to point to the first free byte of the
1604 * buffer
1605 */
1606 *line_ptr = win->txtfree;
1607
1608 /* add a color code to the start of the new line, if necessary */
1609 osssb_add_color_code(win);
1610
1611 /* remember the text color at the start of this line */
1612 win->solfg = win->base.txtfg;
1613 win->solbg = win->base.txtbg;
1614 win->solattr = win->base.txtattr;
1615
1616 /* add a safety null terminator */
1617 osssb_add_safety_null(win);
1618
1619 /* success */
1620 return TRUE;
1621 }
1622
1623 /*
1624 * Scroll the window forward by the given number of lines
1625 */
1626 static void osgen_scroll_win_fwd(osgen_txtwin_t *win, size_t lines)
1627 {
1628 /* limit the scrolling to the available number of lines */
1629 if (lines > win->line_count - win->base.scrolly - win->base.ht)
1630 lines = win->line_count - win->base.scrolly - win->base.ht;
1631
1632 /*
1633 * If we're scrolling more than a few lines, or more than the entire
1634 * window's height, just redraw the entire window. If it's a small
1635 * number of lines, scroll the screen one line at a time, so that we
1636 * don't have to redraw as much.
1637 */
1638 if (lines > 3 || (int)lines >= win->base.ht)
1639 {
1640 /* adjust the scroll position */
1641 win->base.scrolly += lines;
1642
1643 /* redraw the entire window */
1644 osgen_redraw_win(&win->base);
1645 }
1646 else
1647 {
1648 /* scroll one line at a time */
1649 for ( ; lines != 0 ; --lines)
1650 {
1651 /* scroll the window's on-screen area one line */
1652 ossscr(win->base.winy, win->base.winx,
1653 win->base.winy + win->base.ht - 1,
1654 win->base.winx + win->base.wid - 1,
1655 win->base.oss_fillcolor);
1656
1657 /* adjust the scroll position */
1658 win->base.scrolly++;
1659
1660 /* redraw the bottom line */
1661 osgen_scrdisp(&win->base, 0, win->base.ht - 1, win->base.wid);
1662 }
1663 }
1664 }
1665
1666 /*
1667 * Scroll the window back by the given number of lines
1668 */
1669 static void osgen_scroll_win_back(osgen_txtwin_t *win, int lines)
1670 {
1671 /* limit scrolling to the available number of lines above the window */
1672 if (lines > win->base.scrolly)
1673 lines = win->base.scrolly;
1674
1675 /*
1676 * If we're scrolling more than a few lines, or by the full window
1677 * height or more, just redraw the entire window. Otherwise, scroll a
1678 * line at a time to minimize redrawing.
1679 */
1680 if (lines > 3)
1681 {
1682 /* adjust the scroll position */
1683 win->base.scrolly -= lines;
1684
1685 /* redraw the entire window */
1686 osgen_redraw_win(&win->base);
1687 }
1688 else
1689 {
1690 /* scroll up by one line at a time */
1691 for ( ; lines != 0 ; --lines)
1692 {
1693 /* scroll the window's on-screen area one line */
1694 ossscu(win->base.winy, win->base.winx,
1695 win->base.winy + win->base.ht - 1,
1696 win->base.winx + win->base.wid - 1,
1697 win->base.oss_fillcolor);
1698
1699 /* adjust the scroll position */
1700 win->base.scrolly--;
1701
1702 /* redraw the top line */
1703 osgen_scrdisp(&win->base, 0, 0, win->base.wid);
1704 }
1705 }
1706 }
1707
1708 /*
1709 * If a window is in auto-vscroll mode, bring the cursor into view in the
1710 * window.
1711 */
1712 static void osgen_auto_vscroll(osgen_txtwin_t *win)
1713 {
1714 /* if the window isn't in auto-vscroll mode, ignore this */
1715 if (!(win->base.flags & OSGEN_AUTO_VSCROLL))
1716 return;
1717
1718 /*
1719 * scroll the window forward if the cursor is outside the visible area
1720 * of the window
1721 */
1722 if (win->base.y >= win->base.scrolly + (int)win->base.ht)
1723 osgen_scroll_win_fwd(win, win->base.y
1724 - (win->base.scrolly + win->base.ht) + 1);
1725 }
1726
1727 /*
1728 * Add text to the scrollback buffer.
1729 */
1730 static void ossaddsb(osgen_txtwin_t *win, const char *p, size_t len, int draw)
1731 {
1732 int startx;
1733 int starty;
1734 int line_len;
1735
1736 /* if there's no scrollback buffer, ignore it */
1737 if (win->txtbuf == 0)
1738 return;
1739
1740 /* note the starting x,y position, for redrawing purposes */
1741 startx = win->base.x - win->base.scrollx;
1742 starty = win->base.y - win->base.scrolly;
1743
1744 /* we haven't added any characters to the line yet */
1745 line_len = 0;
1746
1747 /*
1748 * Copy the text into the screen buffer, respecting the circular
1749 * nature of the screen buffer. If the given text wraps lines,
1750 * enter an explicit carriage return into the text to ensure that
1751 * users can correctly count lines.
1752 */
1753 while (len != 0)
1754 {
1755 /* check what we have */
1756 switch(*p)
1757 {
1758 case OSGEN_ATTR:
1759 /* switch to the new attributes */
1760 win->base.txtattr = (unsigned char)*(p+1);
1761
1762 /*
1763 * if we're at the start of the line, this is the start-of-line
1764 * attribute - this will ensure that if we back up with '\r',
1765 * we'll re-apply this attribute change
1766 */
1767 if (win->base.x == 0)
1768 win->solattr = win->base.txtattr;
1769
1770 /* add the two-byte sequence */
1771 osssb_add_bytes(win, p, 2);
1772 p += 2;
1773 len -= 2;
1774 break;
1775
1776 case OSGEN_COLOR:
1777 /* switch to the new colors */
1778 win->base.txtfg = (unsigned char)*(p+1);
1779 win->base.txtbg = (unsigned char)*(p+2);
1780
1781 /*
1782 * if we're at the start of the line, this is the new
1783 * start-of-line color
1784 */
1785 if (win->base.x == 0)
1786 {
1787 win->solfg = win->base.txtfg;
1788 win->solbg = win->base.txtbg;
1789 }
1790
1791 /* add and skip the three-byte color sequence */
1792 osssb_add_bytes(win, p, 3);
1793 p += 3;
1794 len -= 3;
1795 break;
1796
1797 case '\n':
1798 /* add the new line */
1799 osssb_new_line(win);
1800
1801 /* draw the part of the line we added */
1802 if (draw)
1803 osgen_scrdisp(&win->base, startx, starty, line_len);
1804
1805 /* skip this character */
1806 ++p;
1807 --len;
1808
1809 /* reset to the next line */
1810 startx = win->base.x - win->base.scrollx;
1811 ++starty;
1812 line_len = 0;
1813
1814 /* bring the cursor onto the screen if auto-vscrolling */
1815 if (draw)
1816 osgen_auto_vscroll(win);
1817
1818 /* done */
1819 break;
1820
1821 case '\r':
1822 /*
1823 * We have a plain carriage return, which indicates that we
1824 * should go back to the start of the current line and
1825 * overwrite it. (This is most likely to occur for the
1826 * "[More]" prompt.)
1827 */
1828
1829 /* set the free pointer back to the start of the current line */
1830 win->txtfree = osgen_get_line(win, win->base.y);
1831
1832 /* switch back to the color as of the start of the line */
1833 win->base.txtfg = win->solfg;
1834 win->base.txtbg = win->solbg;
1835 win->base.txtattr = win->solattr;
1836
1837 /*
1838 * since we're back at the start of the line, add a color code
1839 * if necessary
1840 */
1841 osssb_add_color_code(win);
1842
1843 /* clear the window area of the line we're overwriting */
1844 if (draw && starty >= 0 && starty < (int)win->base.ht)
1845 ossclr(win->base.winy + starty, win->base.winx,
1846 win->base.winy + starty,
1847 win->base.winx + win->base.wid - 1,
1848 win->base.oss_fillcolor);
1849
1850 /* reset to the start of the line */
1851 line_len = 0;
1852 startx = 0;
1853
1854 /* move to the left column */
1855 win->base.x = 0;
1856
1857 /* skip this character */
1858 ++p;
1859 --len;
1860
1861 /* done */
1862 break;
1863
1864 default:
1865 /* for anything else, simply store the byte in the buffer */
1866 if (osssb_add_byte(win, *p))
1867 {
1868 /* note the maximum output position if appropriate */
1869 if (win->base.x > win->base.xmax)
1870 win->base.xmax = win->base.x;
1871
1872 /* adjust the output x position */
1873 win->base.x++;
1874
1875 /* count the ordinary character in the display length */
1876 ++line_len;
1877 }
1878
1879 /* skip the character */
1880 ++p;
1881 --len;
1882
1883 /* done */
1884 break;
1885 }
1886 }
1887
1888 /*
1889 * Add a safety null terminator after each addition, in case we need
1890 * to look at the buffer before this line is finished.
1891 */
1892 osssb_add_safety_null(win);
1893
1894 /* display the added text */
1895 if (draw)
1896 {
1897 /* show the added text */
1898 osgen_scrdisp(&win->base, startx, starty, line_len);
1899
1900 /* bring the cursor onto the screen if auto-vscrolling */
1901 osgen_auto_vscroll(win);
1902 }
1903 }
1904
1905 /*
1906 * Add an input line to the scrollback buffer.
1907 */
1908 void ossaddsb_input(osgen_txtwin_t *win, char *p, int add_nl)
1909 {
1910 size_t chars_rem;
1911
1912 /* get the number of characters in the input to add */
1913 chars_rem = strlen(p);
1914
1915 /*
1916 * add the input in chunks, wrapping whenever we reach the right edge
1917 * of the window
1918 */
1919 while (chars_rem != 0)
1920 {
1921 size_t wid_rem;
1922 size_t cur;
1923
1924 /* if we're already past the edge, add a newline */
1925 if (win->base.x >= (int)win->base.wid)
1926 osssb_new_line(win);
1927
1928 /* figure out how much space we have */
1929 wid_rem = win->base.wid - win->base.x;
1930
1931 /* add as much as we can, up to the width remaining */
1932 cur = chars_rem;
1933 if (cur > wid_rem)
1934 cur = wid_rem;
1935
1936 /* add the text */
1937 ossaddsb(win, p, cur, FALSE);
1938
1939 /* skip this chunk */
1940 p += cur;
1941 chars_rem -= cur;
1942 }
1943
1944 /* add a newline after the input if desired */
1945 if (add_nl)
1946 osssb_new_line(win);
1947 else
1948 osssb_add_safety_null(win);
1949 }
1950
1951 /* ------------------------------------------------------------------------ */
1952 /*
1953 * End scrollback mode.
1954 */
1955 static void osgen_sb_mode_end()
1956 {
1957 osgen_txtwin_t *win = S_sbmode_win;
1958
1959 /* restore our original scroll position */
1960 if (win->base.scrolly > S_sbmode_orig_scrolly)
1961 osgen_scroll_win_back(win, win->base.scrolly - S_sbmode_orig_scrolly);
1962 else if (win->base.scrolly < S_sbmode_orig_scrolly)
1963 osgen_scroll_win_fwd(win, S_sbmode_orig_scrolly - win->base.scrolly);
1964
1965 /*
1966 * adjust the input editing cursor position if the scrolling position
1967 * doesn't match the original position
1968 */
1969 S_gets_y += S_sbmode_orig_scrolly - win->base.scrolly;
1970
1971 /*
1972 * restore our full window area, adding back in the top line that we
1973 * removed to make room for the mode line
1974 */
1975 win->base.winy -= 1;
1976 win->base.ht += 1;
1977 win->base.scrolly -= 1;
1978
1979 /* redraw the top line, where we drew the mode line */
1980 ossclr(win->base.winy, win->base.winx,
1981 win->base.winy, win->base.winx + win->base.wid - 1,
1982 win->base.oss_fillcolor);
1983 osgen_scrdisp(&win->base, 0, 0, win->base.wid);
1984
1985 /*
1986 * Delete the temporary copy of the input from the scrollback buffer.
1987 * To do this, restore the x,y position and free pointer, then delete
1988 * extra lines from the end of the buffer until the last line points
1989 * to the same text it did before we added the command input text.
1990 */
1991 win->base.x = S_sbmode_orig_x;
1992 win->txtfree = S_sbmode_orig_txtfree;
1993 while (win->line_count > 1
1994 && osgen_get_line_ptr(win, win->base.y) != S_sbmode_orig_last_line)
1995 {
1996 /* this is an added line - delete the line */
1997 win->line_count--;
1998 win->base.y--;
1999 }
2000
2001 /* we're no longer in scrollback mode */
2002 S_sbmode_win = 0;
2003 }
2004
2005 /*
2006 * Draw the scrollback mode status line. This is a line we draw at the top
2007 * of the window to give a visual indication that we're in scrollback mode.
2008 * (Actually, we draw this one line above the current top of the window,
2009 * because we shrink the window vertically by one line while it's in
2010 * scrollback mode specifically to make room for the mode line.)
2011 */
2012 static void osgen_draw_sb_mode_line(void)
2013 {
2014 osgen_txtwin_t *win;
2015 int oss_color;
2016
2017 /* get the current scrollback window */
2018 win = S_sbmode_win;
2019
2020 /* there's nothing to do if there's no scrollback window */
2021 if (win == 0)
2022 return;
2023
2024 /* get the color - use the reverse of the window's current color scheme */
2025 oss_color = ossgetcolor(win->base.fillcolor, win->base.txtfg, 0,
2026 OSGEN_COLOR_TRANSPARENT);
2027
2028 /* clear the area of the mode line */
2029 ossclr(win->base.winy - 1, win->base.winx,
2030 win->base.winy - 1, win->base.winx + win->base.wid - 1, oss_color);
2031
2032 /* momentarily bring the mode line back into the window's area */
2033 win->base.winy -= 1;
2034 win->base.ht += 1;
2035
2036 /* draw the mode line text */
2037 osgen_disp_trunc(&win->base, 0, 0, oss_color, OS_SBSTAT);
2038
2039 /* undo our temporary adjustment of the window size */
2040 win->base.winy += 1;
2041 win->base.ht -= 1;
2042 }
2043
2044 /*
2045 * Process the event in scrollback mode. If we're not already in
2046 * scrollback mode, we'll enter scrollback mode; otherwise, we'll process
2047 * the event in the already running scrollback.
2048 *
2049 * Returns true if we processed the event, false if we have terminated
2050 * scrollback mode and want the caller to process the event as it normally
2051 * would outside of scrollback mode.
2052 */
2053 static int osgen_sb_mode(osgen_txtwin_t *win, int event_type,
2054 os_event_info_t *event_info)
2055 {
2056 int ret;
2057 char c;
2058 int just_started;
2059
2060 /* if we were doing scrollback in a different window, cancel it */
2061 if (S_sbmode_win != 0 && S_sbmode_win != win)
2062 osgen_sb_mode_end();
2063
2064 /* note if we're just entering scrollback mode */
2065 just_started = (S_sbmode_win == 0);
2066
2067 /* if scrollback mode isn't already in progress, set it up */
2068 if (just_started)
2069 {
2070 /* if there's no buffer, we can't enter scrollback mode */
2071 if (win->txtbuf == 0)
2072 return FALSE;
2073
2074 /*
2075 * If the number of lines in the scrollback buffer doesn't exceed
2076 * the number of lines displayed in the window, then don't bother
2077 * entering scrollback mode. If the window isn't at least two
2078 * lines high, don't allow it either, as we need space to draw our
2079 * mode line.
2080 */
2081 if (win->line_count <= win->base.ht || win->base.ht < 2)
2082 return FALSE;
2083
2084 /* remember the window involved in scrollback */
2085 S_sbmode_win = win;
2086
2087 /*
2088 * Shrink the window one line at the top, to make room for the mode
2089 * line. Compensate by bumping the scrolling position forward a
2090 * line, to keep the lines that will remain visible in the rest of
2091 * the window at the same place on the screen.
2092 */
2093 win->base.winy += 1;
2094 win->base.ht -= 1;
2095 win->base.scrolly += 1;
2096
2097 /*
2098 * Temporarily add the command buffer to the scrollback buffer, so
2099 * that it shows up when we're scrolling. To facilitate removing
2100 * the added text later, remember the current free pointer, last
2101 * line pointer, x document position.
2102 */
2103 S_sbmode_orig_x = win->base.x;
2104 S_sbmode_orig_txtfree = win->txtfree;
2105 S_sbmode_orig_last_line = osgen_get_line_ptr(win, win->base.y);
2106 ossaddsb_input(win, S_gets_buf, FALSE);
2107
2108 /* remember the original scrolling position */
2109 S_sbmode_orig_scrolly = win->base.scrolly;
2110
2111 /* draw the mode line */
2112 osgen_draw_sb_mode_line();
2113 }
2114
2115 /* presume we'll handle the event */
2116 ret = TRUE;
2117
2118 /* check the event */
2119 switch(event_type)
2120 {
2121 case OS_EVT_KEY:
2122 /*
2123 * If it's a regular key, exit scrollback mode immediately, and
2124 * let the caller handle the event normally. This allows the user
2125 * to resume typing even while scrollback is active; we'll simply
2126 * cancel the scrollback and resume editing with the event that
2127 * canceled the scrollback.
2128 */
2129 c = event_info->key[0];
2130 if ((unsigned char)c >= 32 || c == '\n' || c == '\r' || c == 8)
2131 {
2132 /* it's a regular key - terminate scrollback mode */
2133 osgen_sb_mode_end();
2134
2135 /* let the caller handle the event */
2136 ret = FALSE;
2137 }
2138
2139 /* if it's the escape key, we're done */
2140 if (c == 27)
2141 {
2142 osgen_sb_mode_end();
2143 break;
2144 }
2145
2146 /* if it's not a special keystroke, we're done with it */
2147 if (c != '\0')
2148 break;
2149
2150 /* it's a special keystroke - check the command ID */
2151 switch(event_info->key[1])
2152 {
2153 case CMD_SCR:
2154 /* if we're not just toggling in to scrollback mode, toggle out */
2155 if (!just_started)
2156 osgen_sb_mode_end();
2157 break;
2158
2159 case CMD_KILL:
2160 case CMD_EOF:
2161 /* it's a termination key - cancel scrollback mode */
2162 osgen_sb_mode_end();
2163 break;
2164
2165 case CMD_UP:
2166 /* scroll back a line */
2167 osgen_scroll_win_back(win, 1);
2168 break;
2169
2170 case CMD_DOWN:
2171 /* scroll forward a line */
2172 osgen_scroll_win_fwd(win, 1);
2173 break;
2174
2175 case CMD_PGUP:
2176 /* move back by one window height */
2177 osgen_scroll_win_back(win, win->base.ht > 1
2178 ? win->base.ht - 1 : 1);
2179 break;
2180
2181 case CMD_PGDN:
2182 /* move forward by one window height */
2183 osgen_scroll_win_fwd(win, win->base.ht > 1
2184 ? win->base.ht - 1 : 1);
2185 break;
2186 }
2187
2188 /* done with the key */
2189 break;
2190
2191 default:
2192 /*
2193 * ignore any other events we receive, but consider them processed
2194 * - even though we're ignoring the event, we're deliberately
2195 * ignoring the event as our way of processing it
2196 */
2197 break;
2198 }
2199
2200 /* return the 'processed' indication to the caller */
2201 return ret;
2202 }
2203
2204 /*
2205 * Display a line of text in the given ordinary text window
2206 */
2207 static void osgen_scrdisp_txt(osgen_txtwin_t *win, int x, int y, int len)
2208 {
2209 int fg;
2210 int bg;
2211 int attr;
2212 int oss_color;
2213 char buf[64];
2214 char *bufp;
2215 char *p;
2216 int scanx;
2217
2218 /* start out in ordinary text mode */
2219 fg = OSGEN_COLOR_TEXT;
2220 bg = OSGEN_COLOR_TRANSPARENT;
2221 attr = 0;
2222 oss_color = ossgetcolor(fg, bg, attr, win->base.fillcolor);
2223
2224 /* get a pointer to the start of the desired line */
2225 p = osgen_get_line(win, y + win->base.scrolly);
2226 if (p == 0)
2227 return;
2228
2229 /* get the window-relative x coordinate of the start of the line */
2230 scanx = -win->base.scrollx;
2231
2232 /*
2233 * Scan the line. We must scan from the start of the line, even if we
2234 * don't want to display from the start of the line, to make sure we
2235 * take into account any color and attribute settings stored in the
2236 * line.
2237 */
2238 for (bufp = buf ; *p != '\0' && len != 0 ; p = ossadvsp(win, p))
2239 {
2240 /* check for special escape codes */
2241 switch(*p)
2242 {
2243 case OSGEN_ATTR:
2244 /* skip the escape byte */
2245 p = ossadvsp(win, p);
2246
2247 /* get the new attribute code */
2248 attr = *p;
2249
2250 /* set the new colors */
2251 goto change_color;
2252
2253 case OSGEN_COLOR:
2254 /* skip the escape byte */
2255 p = ossadvsp(win, p);
2256
2257 /* get the foreground color and skip it */
2258 fg = *p;
2259 p = ossadvsp(win, p);
2260
2261 /* get the background color */
2262 bg = *p;
2263 goto change_color;
2264
2265 change_color:
2266 /* flush the buffer */
2267 if (bufp != buf)
2268 {
2269 /* display the contents of the buffer */
2270 *bufp = '\0';
2271 osgen_disp_trunc(&win->base, y, x, oss_color, buf);
2272
2273 /* adjust the column position for the display */
2274 x += bufp - buf;
2275
2276 /* reset the buffer */
2277 bufp = buf;
2278 }
2279
2280 /* translate the new text color */
2281 oss_color = ossgetcolor(fg, bg, attr, win->base.fillcolor);
2282 break;
2283
2284 default:
2285 /* if the buffer is full, flush it */
2286 if (bufp == buf + sizeof(buf) - 1)
2287 {
2288 /* display the buffer */
2289 *bufp = '\0';
2290 osgen_disp_trunc(&win->base, y, x, oss_color, buf);
2291
2292 /* adjust the column position for the display */
2293 x += bufp - buf;
2294
2295 /* empty the buffer */
2296 bufp = buf;
2297 }
2298
2299 /*
2300 * if we've reached the starting x coordinate, add this
2301 * character to the buffer; if we haven't, we can ignore the
2302 * character, since in that case we're just scanning for
2303 * escape codes in the line before the part we want to display
2304 */
2305 if (scanx >= x)
2306 {
2307 /* add it to the buffer */
2308 *bufp++ = *p;
2309
2310 /* count it against the length remaining to be displayed */
2311 --len;
2312 }
2313
2314 /* adjust the x coordinate of our scan */
2315 ++scanx;
2316
2317 /* done */
2318 break;
2319 }
2320 }
2321
2322 /* display the last portion of the line if anything's left */
2323 if (bufp != buf)
2324 {
2325 /* null-terminate at the current point */
2326 *bufp = '\0';
2327
2328 /* display it */
2329 osgen_disp_trunc(&win->base, y, x, oss_color, buf);
2330 }
2331 }
2332
2333
2334 /* ------------------------------------------------------------------------ */
2335 /*
2336 * Special routines for Text Grid windows
2337 */
2338
2339 /*
2340 * Clear the text and color arrays for a section of a grid window. Sets
2341 * each cleared character's text to a space, and sets the color to
2342 * text/transparent.
2343 */
2344 static void osgen_gridwin_clear_ptr(char *txtp, osgen_charcolor_t *colorp,
2345 size_t len)
2346 {
2347 /* loop through the grid from the starting offset */
2348 for ( ; len != 0 ; --len, ++txtp, ++colorp)
2349 {
2350 /* set this character's text to a space */
2351 *txtp = ' ';
2352
2353 /* set this character's color to text/transparent */
2354 colorp->fg = OSGEN_COLOR_TEXT;
2355 colorp->bg = OSGEN_COLOR_TRANSPARENT;
2356 }
2357 }
2358
2359 /*
2360 * Clear the text and color arrays for a section of a grid window. Starts
2361 * at the given character offset in the grid arrays, and clears for the
2362 * given number of character positions. Sets each cleared character's
2363 * text to a space, and sets the color to text/transparent.
2364 */
2365 static void osgen_gridwin_clear(osgen_gridwin_t *win, size_t ofs, size_t len)
2366 {
2367 /* clear our array in the selected areas */
2368 osgen_gridwin_clear_ptr(win->grid_txt + ofs, win->grid_color + ofs, len);
2369 }
2370
2371
2372 /*
2373 * Resize a text grid window. This must be called whenever the on-screen
2374 * size of the window is changed, or we need to write outside the current
2375 * bounds of the array, so that we can expand our internal size allocation
2376 * if the window is now bigger than our internal text/color arrays.
2377 */
2378 static void osgen_gridwin_resize(osgen_gridwin_t *win,
2379 size_t new_wid, size_t new_ht)
2380 {
2381 /* if the window's size is now bigger than the grid, expand the grid */
2382 if (new_wid > win->grid_wid || new_ht > win->grid_ht)
2383 {
2384 char *new_txt;
2385 osgen_charcolor_t *new_color;
2386 char *tsrc, *tdst;
2387 osgen_charcolor_t *csrc, *cdst;
2388 size_t y;
2389
2390 /*
2391 * use the larger of the old size and the new size, so that the
2392 * window only expands (this somewhat simplifies copying the old
2393 * to the new)
2394 */
2395 if (win->grid_wid > new_wid)
2396 new_wid = win->grid_wid;
2397 if (win->grid_ht > new_ht)
2398 new_ht = win->grid_ht;
2399
2400 /* allocate the new arrays */
2401 new_txt = (char *)osmalloc(new_wid * new_ht);
2402 new_color = (osgen_charcolor_t *)osmalloc(
2403 new_wid * new_ht * sizeof(new_color[0]));
2404
2405 /* copy the old grid to the new grid */
2406 tsrc = win->grid_txt;
2407 csrc = win->grid_color;
2408 tdst = new_txt;
2409 cdst = new_color;
2410 for (y = 0 ; y < win->grid_ht ; ++y)
2411 {
2412 /* copy the old text and color data */
2413 memcpy(tdst, tsrc, win->grid_wid);
2414 memcpy(cdst, csrc, win->grid_wid * sizeof(cdst[0]));
2415
2416 /* clear the rest of the line if expanding the width */
2417 osgen_gridwin_clear_ptr(tdst + win->grid_wid,
2418 cdst + win->grid_wid,
2419 new_wid - win->grid_wid);
2420
2421 /* advance the pointers */
2422 tsrc += win->grid_wid;
2423 csrc += win->grid_wid;
2424 tdst += new_wid;
2425 cdst += new_wid;
2426 }
2427
2428 /* clear all remaining lines */
2429 for ( ; y < new_ht ; ++y, tdst += new_wid, cdst += new_wid)
2430 osgen_gridwin_clear_ptr(tdst, cdst, new_wid);
2431
2432 /* delete the old buffers */
2433 osfree(win->grid_txt);
2434 osfree(win->grid_color);
2435
2436 /* set the new buffers and sizes */
2437 win->grid_txt = new_txt;
2438 win->grid_color = new_color;
2439 win->grid_wid = new_wid;
2440 win->grid_ht = new_ht;
2441 }
2442 }
2443
2444 /*
2445 * Display a line of text in the given grid window
2446 */
2447 static void osgen_scrdisp_grid(osgen_gridwin_t *win, int x, int y, int len)
2448 {
2449 char *txtp;
2450 osgen_charcolor_t *colorp;
2451 size_t ofs;
2452 char buf[64];
2453 char *dst;
2454 char fg, bg;
2455 int oss_color;
2456
2457 /*
2458 * calculate the offset into our arrays: get the document coordinates
2459 * (by adjusting for the scrolling offset), multiply the document row
2460 * number by the row width, and add the document column number
2461 */
2462 ofs = ((y - win->base.scrolly) * win->grid_wid) + (x - win->base.scrollx);
2463
2464 /* start at the calculated offset */
2465 txtp = win->grid_txt + ofs;
2466 colorp = win->grid_color + ofs;
2467
2468 /* start with the first character's color */
2469 fg = colorp->fg;
2470 bg = colorp->bg;
2471
2472 /* calculate the starting oss-level color */
2473 oss_color = ossgetcolor(fg, bg, 0, win->base.fillcolor);
2474
2475 /*
2476 * scan the text, flushing when we fill up the buffer or encounter a
2477 * new text color
2478 */
2479 for (dst = buf ; ; --len, ++txtp, ++colorp)
2480 {
2481 /*
2482 * if we have a color change, or we've exhausted the requested
2483 * length, or the buffer is full, display what we have in the
2484 * buffer so far
2485 */
2486 if (len == 0
2487 || dst == buf + sizeof(buf) - 1
2488 || colorp->fg != fg || colorp->bg != bg)
2489 {
2490 /* null-terminate the buffer and display it */
2491 *dst = '\0';
2492 osgen_disp_trunc(&win->base, y, x, oss_color, buf);
2493
2494 /* count the output column change */
2495 x += dst - buf;
2496
2497 /* reset the buffer write pointer */
2498 dst = buf;
2499
2500 /* if we have a color change, calculate the new color */
2501 if (colorp->fg != fg || colorp->bg != bg)
2502 {
2503 /* remember the new color */
2504 fg = colorp->fg;
2505 bg = colorp->bg;
2506
2507 /* calculate the new oss-level color */
2508 oss_color = ossgetcolor(fg, bg, 0, win->base.fillcolor);
2509 }
2510 }
2511
2512 /* if we've exhausted the request, we're done */
2513 if (len == 0)
2514 break;
2515
2516 /* add this character from the text grid to our output buffer */
2517 *dst++ = *txtp;
2518 }
2519 }
2520
2521 /*
2522 * Write text into a grid window
2523 */
2524 static void osgen_gridwin_write(osgen_gridwin_t *win,
2525 const char *txt, size_t len)
2526 {
2527 const char *p;
2528 size_t rem;
2529 int xmax, ymax;
2530 int x, y;
2531 int startx;
2532 int winx;
2533 size_t ofs;
2534 char *tdst;
2535 osgen_charcolor_t *cdst;
2536
2537 /*
2538 * First, scan the text to check for writing beyond the end of the
2539 * current text array. If we write anything beyond the bounds of the
2540 * current array, we'll need to expand the array accordingly.
2541 */
2542 xmax = x = win->base.x;
2543 ymax = y = win->base.y;
2544 for (p = txt, rem = len ; rem != 0 ; ++p, --rem)
2545 {
2546 /* check what we have */
2547 switch(*p)
2548 {
2549 case '\n':
2550 /* move to the start of the next line */
2551 x = 0;
2552 ++y;
2553 break;
2554
2555 case '\r':
2556 /* move to the start of the current line */
2557 x = 0;
2558 break;
2559
2560 default:
2561 /*
2562 * everything else takes up a character cell; if this is the
2563 * highest x/y position so far, note it
2564 */
2565 if (y > ymax)
2566 ymax = y;
2567 if (x > xmax)
2568 xmax = x;
2569
2570 /* move the cursor right */
2571 ++x;
2572 break;
2573 }
2574 }
2575
2576 /* expand our text array if we're writing outside the current bounds */
2577 if (xmax >= (int)win->grid_wid || ymax >= (int)win->grid_ht)
2578 osgen_gridwin_resize(win, xmax + 1, ymax + 1);
2579
2580 /* start at the current window coordinates */
2581 x = win->base.x;
2582 y = win->base.y;
2583
2584 /* calculate the starting offset in the array for writing */
2585 ofs = y * win->grid_wid + x;
2586 tdst = win->grid_txt + ofs;
2587 cdst = win->grid_color + ofs;
2588
2589 /* note the new maximum write positions */
2590 if (xmax > win->base.xmax)
2591 win->base.xmax = xmax;
2592 if (ymax > win->base.ymax)
2593 win->base.ymax = ymax;
2594
2595 /* start at the current column */
2596 startx = x;
2597 winx = x - win->base.scrollx;
2598
2599 /* now scan the text again, writing it to the grid and displaying it */
2600 for (p = txt, rem = len ; ; ++p, --rem)
2601 {
2602 /*
2603 * if we're at a newline or the end of the text to display,
2604 * display the section of the current line that we just built
2605 */
2606 if (rem == 0 || *p == '\n' || *p == '\r')
2607 {
2608 /* if we're not in deferred redraw mode, draw this section */
2609 if (!S_deferred_redraw
2610 && !(win->base.flags & OSGEN_DEFER_REDRAW)
2611 && x != startx)
2612 osgen_scrdisp(&win->base, winx, y, x - startx);
2613
2614 /* if we're out of text to display, we're done */
2615 if (rem == 0)
2616 break;
2617
2618 /* apply any newline */
2619 if (*p == '\n')
2620 {
2621 /* move to the next line */
2622 x = 0;
2623 ++y;
2624 }
2625 else if (*p == '\r')
2626 {
2627 /* move to the start of the current line */
2628 x = 0;
2629 }
2630
2631 /* note the starting coordinates of the next chunk */
2632 startx = x;
2633 winx = x - win->base.scrollx;
2634
2635 /* calculate the new array output offset */
2636 ofs = y * win->grid_wid + x;
2637 tdst = win->grid_txt + ofs;
2638 cdst = win->grid_color + ofs;
2639 }
2640 else
2641 {
2642 /* add this character to our array */
2643 *tdst = *p;
2644 cdst->fg = win->base.txtfg;
2645 cdst->bg = win->base.txtbg;
2646
2647 /* ajust the output pointers */
2648 ++tdst;
2649 ++cdst;
2650
2651 /* adjust the column counter */
2652 ++x;
2653 }
2654 }
2655
2656 /* set the new output position in the window */
2657 win->base.x = x;
2658 win->base.y = y;
2659 }
2660
2661
2662 /* ------------------------------------------------------------------------ */
2663 /*
2664 * Generic banner window routines
2665 */
2666
2667 /*
2668 * Display a window's text from the given starting position for the given
2669 * number of characters (counting only displayed characters; escape
2670 * sequences don't count). The starting position is given in
2671 * window-relative coordinates.
2672 */
2673 static void osgen_scrdisp(osgen_win_t *win, int x, int y, int len)
2674 {
2675 /*
2676 * if the text is entirely outside the window's display area, there's
2677 * nothing to display, so ignore the request
2678 */
2679 if (y < 0 || (size_t)y >= win->ht
2680 || x + len <= 0 || (size_t)x >= win->wid)
2681 return;
2682
2683 /* draw according to window type */
2684 switch(win->win_type)
2685 {
2686 case OS_BANNER_TYPE_TEXT:
2687 osgen_scrdisp_txt((osgen_txtwin_t *)win, x, y, len);
2688 break;
2689
2690 case OS_BANNER_TYPE_TEXTGRID:
2691 osgen_scrdisp_grid((osgen_gridwin_t *)win, x, y, len);
2692 break;
2693 }
2694 }
2695
2696
2697 /*
2698 * redraw a window
2699 */
2700 static void osgen_redraw_win(osgen_win_t *win)
2701 {
2702 size_t y;
2703
2704 /* clear the window's area on the screen */
2705 ossclr(win->winy, win->winx,
2706 win->winy + win->ht - 1, win->winx + win->wid - 1,
2707 win->oss_fillcolor);
2708
2709 /* display each line in the window */
2710 for (y = 0 ; y < win->ht ; ++y)
2711 osgen_scrdisp(win, 0, y, win->wid);
2712 }
2713
2714 /* redraw the entire screen */
2715 void os_redraw(void)
2716 {
2717 /*
2718 * force a redraw of the entire screen by setting the global
2719 * pending-redraw flag
2720 */
2721 S_deferred_redraw = TRUE;
2722
2723 /* go redraw it */
2724 osssb_redraw_if_needed();
2725
2726 }
2727
2728 /* redraw a window, if it needs redrawing */
2729 static void osgen_redraw_win_if_needed(int global_deferred, osgen_win_t *win)
2730 {
2731 osgen_win_t *chi;
2732
2733 /*
2734 * if this window needs redrawing, or we have a global deferred redraw,
2735 * redraw the window
2736 */
2737 if (global_deferred || (win->flags & OSGEN_DEFER_REDRAW) != 0)
2738 {
2739 /* clear the window-specific deferred-redraw flag */
2740 win->flags &= ~OSGEN_DEFER_REDRAW;
2741
2742 /* redraw the window */
2743 osgen_redraw_win(win);
2744 }
2745
2746 /* redraw this window's children if necessary */
2747 for (chi = win->first_child ; chi != 0 ; chi = chi->nxt)
2748 osgen_redraw_win_if_needed(global_deferred, chi);
2749 }
2750
2751 /* redraw the screen if necessary */
2752 void osssb_redraw_if_needed()
2753 {
2754 int global_deferred = S_deferred_redraw;
2755
2756 /* we're explicitly redrawing, so cancel any pending deferred redraw */
2757 S_deferred_redraw = FALSE;
2758
2759 /* redraw the root window, which will redraw its children */
2760 if (S_main_win != 0)
2761 osgen_redraw_win_if_needed(global_deferred, &S_main_win->base);
2762
2763 /* if the redraw is global, redraw other global features */
2764 if (global_deferred)
2765 {
2766 /* redraw the scrollback mode line, if appropriate */
2767 osgen_draw_sb_mode_line();
2768
2769 /* redraw any command line under construction */
2770 osgen_gets_redraw_cmdline();
2771 }
2772 }
2773
2774 /* move the cursor to the default position */
2775 void osssb_cursor_to_default_pos(void)
2776 {
2777 osgen_win_t *win;
2778
2779 /* if we're in plain mode, ignore it */
2780 if (os_f_plain)
2781 return;
2782
2783 /* if we're using a special cursor position, do nothing */
2784 if (S_sbmode_win != 0)
2785 {
2786 /* locate at the scrollback mode line */
2787 ossloc(S_sbmode_win->base.winy - 1, S_sbmode_win->base.winx);
2788 }
2789 else if (S_special_cursor_pos)
2790 {
2791 /* locate at the special cursor position */
2792 ossloc(S_special_cursor_y, S_special_cursor_x);
2793 }
2794 else
2795 {
2796 /*
2797 * if we have a default window, put the cursor at the last text
2798 * position in the default window
2799 */
2800 if ((win = &S_default_win->base) != 0)
2801 ossloc(win->winy + win->y - win->scrolly,
2802 win->winx + win->x - win->scrollx);
2803 }
2804 }
2805
2806 /*
2807 * Lay out the given window
2808 */
2809 static void oss_lay_out_window(osgen_win_t *win)
2810 {
2811 osgen_win_t *chi;
2812
2813 /*
2814 * if we have a parent, take space from our parent window; otherwise,
2815 * assume that the caller has already laid out our main area, in which
2816 * case we just have to adjust for our children
2817 */
2818 if (win->parent != 0)
2819 {
2820 int siz;
2821 int base_size;
2822 int x, y, wid, ht;
2823
2824 /*
2825 * our size and area will be taken from our parent's, so get the
2826 * parent window's current area
2827 */
2828 x = win->parent->winx;
2829 y = win->parent->winy;
2830 wid = win->parent->wid;
2831 ht = win->parent->ht;
2832
2833 /* get the window's size in character cells */
2834 switch(win->size_type)
2835 {
2836 case OS_BANNER_SIZE_ABS:
2837 /* the size is given in character cells */
2838 siz = win->size;
2839 break;
2840
2841 case OS_BANNER_SIZE_PCT:
2842 /*
2843 * the size is given as a percentage of the parent's size - get
2844 * the appropriate dimension from the parent's size
2845 */
2846 base_size = (win->alignment == OS_BANNER_ALIGN_LEFT
2847 || win->alignment == OS_BANNER_ALIGN_RIGHT
2848 ? wid : ht);
2849
2850 /* calculate the percentage of the full size */
2851 siz = (win->size * base_size) / 100;
2852 break;
2853 }
2854
2855 /* allocate space to the window according to its alignment */
2856 switch(win->alignment)
2857 {
2858 case OS_BANNER_ALIGN_TOP:
2859 /*
2860 * assign the window the full width of the parent, and give it
2861 * the requested height, up to the available height
2862 */
2863 win->wid = wid;
2864 win->winx = x;
2865 win->ht = (siz <= ht ? siz : ht);
2866 win->winy = y;
2867
2868 /* take the window's space away from the top of the parent */
2869 ht -= win->ht;
2870 y += win->ht;
2871 break;
2872
2873 case OS_BANNER_ALIGN_BOTTOM:
2874 /* give the window space at the bottom of the parent window */
2875 win->wid = wid;
2876 win->winx = x;
2877 win->ht = (siz <= ht ? siz : ht);
2878 win->winy = y + ht - win->ht;
2879
2880 /* deduct the window from the parent area */
2881 ht -= win->ht;
2882 break;
2883
2884 case OS_BANNER_ALIGN_LEFT:
2885 /* give the window space at the left of the parent area */
2886 win->wid = (siz <= wid ? siz : wid);
2887 win->winx = x;
2888 win->ht = ht;
2889 win->winy = y;
2890
2891 /* deduct the window from the parent area */
2892 wid -= win->wid;
2893 x += win->wid;
2894 break;
2895
2896 case OS_BANNER_ALIGN_RIGHT:
2897 /* give the window space at the right of the remaining area */
2898 win->wid = (siz <= wid ? siz : wid);
2899 win->winx = x + wid - win->wid;
2900 win->ht = ht;
2901 win->winy = y;
2902
2903 /* deduct the window from the parent area */
2904 wid -= win->wid;
2905 break;
2906 }
2907
2908 /* adjust our parent's area for the removal of our area */
2909 win->parent->winx = x;
2910 win->parent->winy = y;
2911 win->parent->wid = wid;
2912 win->parent->ht = ht;
2913 }
2914
2915 /* lay out our children */
2916 for (chi = win->first_child ; chi != 0 ; chi = chi->nxt)
2917 oss_lay_out_window(chi);
2918
2919 /* make any necessary adjustments */
2920 switch(win->win_type)
2921 {
2922 case OS_BANNER_TYPE_TEXT:
2923 /*
2924 * If this is the current active scrollback-mode window, we must
2925 * make the same adjustment that we make on entering scrollback
2926 * mode: shrink the window one line from the top, to make room for
2927 * the scrollback-mode status line.
2928 */
2929 if (win == &S_sbmode_win->base)
2930 {
2931 /* it's the scrollback window - take out the mode line */
2932 win->winy += 1;
2933 win->ht -= 1;
2934 }
2935 break;
2936
2937 case OS_BANNER_TYPE_TEXTGRID:
2938 /* make sure the window's text/color buffers are large enough */
2939 osgen_gridwin_resize((osgen_gridwin_t *)win, win->wid, win->ht);
2940 break;
2941 }
2942 }
2943
2944 /*
2945 * Recalculate the window layout and redraw the screen
2946 */
2947 static void osgen_recalc_layout()
2948 {
2949 /* start at the root window */
2950 if (S_main_win != 0)
2951 {
2952 int ht;
2953
2954 /* start by giving the entire screen to the main window */
2955 S_main_win->base.winx = 0;
2956 S_main_win->base.winy = 0;
2957 S_main_win->base.wid = G_oss_screen_width;
2958 S_main_win->base.ht = G_oss_screen_height;
2959
2960 /* lay out the main window and its children */
2961 oss_lay_out_window(&S_main_win->base);
2962
2963 /* recalculate the main window's page size */
2964 G_os_linewidth = S_main_win->base.wid;
2965 ht = S_main_win->base.ht;
2966 G_os_pagelength = (ht > 2 ? ht - 2 : ht > 1 ? ht - 1 : ht);
2967 }
2968
2969 /* schedule a redraw of the entire screen */
2970 S_deferred_redraw = TRUE;
2971 }
2972
2973 /*
2974 * Receive notification that the screen was resized. We'll recalculate
2975 * the banner window layout.
2976 */
2977 void osssb_on_resize_screen()
2978 {
2979 /* recalculate the window layout */
2980 osgen_recalc_layout();
2981
2982 /* immediately update the screen */
2983 osssb_redraw_if_needed();
2984
2985 /* set the cursor back to the default position */
2986 osssb_cursor_to_default_pos();
2987 }
2988
2989 # else /* USE_SCROLLBACK */
2990
2991 /*
2992 * for the non-scrollback version, there's nothing we need to do on
2993 * resizing the screen, as we don't do anything fancy with the layout
2994 */
2995 void osssb_on_resize_screen()
2996 {
2997 /* do nothing */
2998 }
2999
3000 # endif /* USE_SCROLLBACK */
3001
3002 /* ------------------------------------------------------------------------ */
3003
3004 #ifdef USE_STATLINE
3005
3006 /* current statusline score-area string */
3007 static osfar_t char S_scorebuf[135];
3008
3009 /* statusline left-string reset point */
3010 static osfar_t char *S_stat_reset_free;
3011 static osfar_t int S_stat_reset_x;
3012
3013 /* update the right portion of the statusline */
3014 static void osgen_update_stat_right()
3015 {
3016 osgen_txtwin_t *win;
3017 int wid_rem;
3018 size_t char_len;
3019
3020 /*
3021 * if there's no statusline window, or no reset point, we can't do
3022 * anything right now
3023 */
3024 if ((win = S_status_win) == 0 || S_stat_reset_free == 0)
3025 return;
3026
3027 /*
3028 * set the statusline position back to where it was when we finished
3029 * with the statusline itself
3030 */
3031 win->txtfree = S_stat_reset_free;
3032 win->base.x = S_stat_reset_x;
3033
3034 /* if there's no space, don't draw anything */
3035 if (win->base.x > (int)win->base.wid)
3036 return;
3037
3038 /* figure out how much space in the window we have */
3039 wid_rem = win->base.wid - win->base.x;
3040
3041 /* figure out how much we're adding */
3042 char_len = strlen(S_scorebuf);
3043
3044 /*
3045 * if we're adding more than we have room for, add a space and then
3046 * add the buffer contents
3047 */
3048 if ((int)char_len + 1 >= wid_rem)
3049 {
3050 /* add a space */
3051 ossaddsb(win, " ", 1, TRUE);
3052
3053 /* add the right-half string */
3054 ossaddsb(win, S_scorebuf, strlen(S_scorebuf), TRUE);
3055 }
3056 else
3057 {
3058 /* add spaces to right-align the scorebuf text */
3059 while (wid_rem > (int)char_len + 1)
3060 {
3061 char buf[64];
3062 size_t cur;
3063
3064 /* add the remaining spaces, up to a buffer-full */
3065 cur = wid_rem - char_len - 1;
3066 if (cur > sizeof(buf) - 1)
3067 cur = sizeof(buf) - 1;
3068
3069 /* make a buffer-full of spaces */
3070 memset(buf, ' ', cur);
3071
3072 /* display it */
3073 ossaddsb(win, buf, cur, TRUE);
3074
3075 /* deduct the amount we displayed from the width remaining */
3076 wid_rem -= cur;
3077 }
3078
3079 /* add the right-half string */
3080 ossaddsb(win, S_scorebuf, strlen(S_scorebuf), TRUE);
3081 }
3082 }
3083
3084 /*
3085 * Set the status mode. In status mode 0, text displayed via os_print and
3086 * the like will display to the main window; in mode 1, text displayed will
3087 * go to the default status line window, until we reach a newline, at which
3088 * point we'll switch to status mode 2 and text will go nowhere.
3089 */
3090 void os_status(int stat)
3091 {
3092 /* if we're in 'plain' mode, suppress all statusline output */
3093 if (os_f_plain)
3094 {
3095 /* check the requested mode */
3096 switch(stat)
3097 {
3098 case 0:
3099 /* switching to main text mode */
3100 status_mode = 0;
3101 break;
3102
3103 case 1:
3104 /* switching to statusline mode - suppress all output */
3105 status_mode = 2;
3106 break;
3107
3108 case 2:
3109 /* suppress mode */
3110 status_mode = 2;
3111 break;
3112 }
3113
3114 /* done */
3115 return;
3116 }
3117
3118 /* if there's no statusline window, create one */
3119 if (S_status_win == 0)
3120 {
3121 /* create the statusline window as a child of the main window */
3122 S_status_win = osgen_create_txtwin(OS_BANNER_FIRST, 0, S_main_win,
3123 512, 1);
3124
3125 /* if that succeeded, set up the window */
3126 if (S_status_win != 0)
3127 {
3128 /* set up the statusline window at the top of the screen */
3129 S_status_win->base.alignment = OS_BANNER_ALIGN_TOP;
3130 S_status_win->base.size = 1;
3131 S_status_win->base.size_type = OS_BANNER_SIZE_ABS;
3132
3133 /* use the default statusline color in this window */
3134 S_status_win->base.txtfg = OSGEN_COLOR_STATUSLINE;
3135 S_status_win->base.txtbg = OSGEN_COLOR_TRANSPARENT;
3136 S_status_win->base.fillcolor = OSGEN_COLOR_STATUSBG;
3137
3138 /* cache the oss translations of the colors */
3139 S_status_win->base.oss_fillcolor =
3140 ossgetcolor(OSGEN_COLOR_STATUSLINE, OSGEN_COLOR_STATUSBG,
3141 0, 0);
3142
3143 /* recalculate the window layout */
3144 osgen_recalc_layout();
3145 }
3146 }
3147
3148 /* if entering status mode 1, clear the statusline */
3149 if (stat == 1 && status_mode != 1 && S_status_win != 0)
3150 {
3151 /* switch the default window to the status line */
3152 S_default_win = S_status_win;
3153
3154 /* clear the statusline window */
3155 osgen_clear_win(&S_status_win->base);
3156
3157 /* forget the score reset point */
3158 S_stat_reset_free = 0;
3159 }
3160
3161 /* if we're leaving status mode 1, finish the statusline */
3162 if (status_mode == 1 && stat != 1 && S_status_win != 0)
3163 {
3164 /*
3165 * remember the current statusline window settings as the "reset"
3166 * point - this is where we reset the buffer contents whenever we
3167 * want to add the right-half string
3168 */
3169 S_stat_reset_free = S_status_win->txtfree;
3170 S_stat_reset_x = S_status_win->base.x;
3171
3172 /* update the right-half string */
3173 osgen_update_stat_right();
3174 }
3175
3176 /* switch to the new mode */
3177 status_mode = stat;
3178
3179 /* check the mode */
3180 if (status_mode == 0)
3181 {
3182 /* switching to the main window */
3183 S_default_win = S_main_win;
3184 }
3185 else if (status_mode == 2)
3186 {
3187 /*
3188 * entering post-status mode - ignore everything, so set the
3189 * default window to null
3190 */
3191 S_default_win = 0;
3192 }
3193 }
3194
3195 int os_get_status()
3196 {
3197 return status_mode;
3198 }
3199
3200 /* Set score to a string value provided by the caller */
3201 void os_strsc(const char *p)
3202 {
3203 size_t copy_len;
3204
3205 /* copy the score, if a value was given */
3206 if (p != 0)
3207 {
3208 /* limit the copying length to our buffer size */
3209 copy_len = strlen(p);
3210 if (copy_len > sizeof(S_scorebuf) - 1)
3211 copy_len = sizeof(S_scorebuf) - 1;
3212
3213 /* copy the text and null-terminate it */
3214 memcpy(S_scorebuf, p, copy_len);
3215 S_scorebuf[copy_len] = '\0';
3216 }
3217
3218 /* update the statusline window */
3219 osgen_update_stat_right();
3220 }
3221
3222 /*
3223 * Set the score. If cur == -1, the LAST score set with a non-(-1) cur is
3224 * displayed; this is used to refresh the status line without providing a
3225 * new score (for example, after exiting scrollback mode). Otherwise, the
3226 * given current score (cur) and turncount are displayed, and saved in
3227 * case cur==-1 on the next call.
3228 */
3229 void os_score(int cur, int turncount)
3230 {
3231 char buf[20];
3232
3233 /* check for the special -1 turn count */
3234 if (turncount == -1)
3235 {
3236 /* it's turn "-1" - we're simply redrawing the score */
3237 os_strsc((char *)0);
3238 }
3239 else
3240 {
3241 /* format the score */
3242 sprintf(buf, "%d/%d", cur, turncount);
3243
3244 /* display the score string */
3245 os_strsc(buf);
3246 }
3247 }
3248
3249
3250 /* ------------------------------------------------------------------------ */
3251 /*
3252 * Set the terminal into 'plain' mode: disables status line,
3253 * scrollback, command editing.
3254 */
3255 void os_plain(void)
3256 {
3257 /* set the 'plain' mode flag */
3258 os_f_plain = 1;
3259
3260 /*
3261 * if we're running without a stdin, turn off pagination - since the
3262 * user won't be able to respond to [more] prompts, there's no reason
3263 * to show them
3264 */
3265 if (oss_eof_on_stdin())
3266 G_os_moremode = FALSE;
3267 }
3268
3269 /*
3270 * display text to the default window
3271 */
3272 void os_printz(const char *str)
3273 {
3274 /* write using the base counted-length routine */
3275 os_print(str, strlen(str));
3276 }
3277
3278 /*
3279 * display text to the default window
3280 */
3281 void os_print(const char *str, size_t len)
3282 {
3283 osgen_txtwin_t *win;
3284 const char *p;
3285 const char *startp;
3286 size_t rem;
3287
3288 /* determine what to do based on the status mode */
3289 switch(status_mode)
3290 {
3291 case 2:
3292 /* we're in the post-status-line mode - suppress all output */
3293 break;
3294
3295 case 0:
3296 /* we're showing the text in the default window */
3297 if (os_f_plain)
3298 {
3299 /* plain mode - simply write it to stdout */
3300 printf("%.*s", (int)len, str);
3301 }
3302 else
3303 {
3304 /* normal mode - write to the default window, if there is one */
3305 win = S_default_win;
3306 if (win != 0)
3307 {
3308 /* write the text to the window buffer */
3309 ossaddsb(win, str, len, TRUE);
3310
3311 /*
3312 * move the cursor to the new location, if we're not hiding
3313 * updates for the moment
3314 */
3315 if (!S_deferred_redraw
3316 && !(win->base.flags & OSGEN_DEFER_REDRAW))
3317 ossloc(win->base.winy + win->base.y - win->base.scrolly,
3318 win->base.winx + win->base.x - win->base.scrollx);
3319 }
3320 }
3321
3322 /* done */
3323 break;
3324
3325 case 1:
3326 /*
3327 * Status line contents. Ignore the status line in 'plain' mode
3328 * or if there's no statusline window.
3329 */
3330 if (os_f_plain || (win = S_status_win) == 0)
3331 break;
3332
3333 /*
3334 * Skip leading newlines at the start of the statusline output.
3335 * Only do this if we don't already have anything buffered, since
3336 * a newline after some other text indicates the end of the status
3337 * line and thus can't be ignored.
3338 */
3339 p = str;
3340 rem = len;
3341 if (win->base.winy == 0 || win->base.winx == 0)
3342 {
3343 /* the buffer is empty, so skip leading newlines */
3344 for ( ; rem != 0 && *p == '\n' ; ++p, --rem) ;
3345
3346 /* if that leaves nothing, we're done */
3347 if (rem == 0)
3348 break;
3349
3350 /*
3351 * add a space before the first character, so that we always
3352 * have a space at the left edge of the status line
3353 */
3354 ossaddsb(win, " ", 1, TRUE);
3355 }
3356
3357 /* scan for a newline; if we find one, it's the end of the status */
3358 for (startp = p ; rem != 0 && *p != '\n' ; ++p, --rem)
3359 {
3360 /* skip escapes */
3361 switch (*p)
3362 {
3363 case OSGEN_ATTR:
3364 /* skip the extra byte */
3365 p += 1;
3366 rem -= 1;
3367 break;
3368
3369 case OSGEN_COLOR:
3370 /* skip the extra two bytes */
3371 p += 2;
3372 rem -= 2;
3373 break;
3374
3375 default:
3376 /* everything else is one byte long */
3377 break;
3378 }
3379 }
3380
3381 /* add this text to the statusline window */
3382 ossaddsb(win, startp, p - startp, TRUE);
3383
3384 /* finish up if we found a newline */
3385 if (*p == '\n')
3386 {
3387 /* switch to status mode 2 */
3388 os_status(2);
3389 }
3390
3391 /* done */
3392 break;
3393 }
3394 }
3395
3396 #ifndef FROBTADS /* hack - should refactor to avoid need for ifdef */
3397 void os_flush(void)
3398 {
3399 /*
3400 * we don't buffer output ourselves, so there's normally nothing to do
3401 * here; but if we're in 'plain' mode, let stdio know about the flush,
3402 * since it might be buffering output on our behalf
3403 */
3404 if (os_f_plain)
3405 fflush(stdout);
3406 }
3407 #endif
3408
3409 void os_update_display(void)
3410 {
3411 /* there's nothing we need to do */
3412 }
3413
3414 # ifdef USE_HISTORY
3415 /*
3416 * For command line history, we must have some buffer space to store
3417 * past command lines. We will use a circular buffer: when we move
3418 * the pointer past the end of the buffer, it wraps back to the start
3419 * of the buffer. A "tail" indicates the oldest line in the buffer;
3420 * when we need more room for new text, we advance the tail and thereby
3421 * lose the oldest text in the buffer.
3422 */
3423 static osfar_t char *histbuf = 0;
3424 static osfar_t char *histhead = 0;
3425 static osfar_t char *histtail = 0;
3426
3427 /*
3428 * ossadvhp advances a history pointer, and returns the new pointer.
3429 * This function takes the circular nature of the buffer into account
3430 * by wrapping back to the start of the buffer when it hits the end.
3431 */
3432 char *ossadvhp(char *p)
3433 {
3434 if (++p >= histbuf + HISTBUFSIZE)
3435 p = histbuf;
3436 return p;
3437 }
3438
3439 /*
3440 * ossdechp decrements a history pointer, wrapping the pointer back
3441 * to the top of the buffer when it reaches the bottom.
3442 */
3443 char *ossdechp(char *p)
3444 {
3445 if (p == histbuf)
3446 p = histbuf + HISTBUFSIZE;
3447 return p - 1;
3448 }
3449
3450 /*
3451 * osshstcpy copies from a history buffer into a contiguous destination
3452 * buffer, wrapping the history pointer if need be. One null-terminated
3453 * string is copied.
3454 */
3455 void osshstcpy(char *dst, char *hst)
3456 {
3457 while (*hst != '\0')
3458 {
3459 *dst++ = *hst;
3460 hst = ossadvhp(hst);
3461 }
3462 *dst = '\0';
3463 }
3464
3465 /*
3466 * ossprvcmd returns a pointer to the previous history command, given
3467 * a pointer to a history command. It returns a null pointer if the
3468 * given history command is the first in the buffer.
3469 */
3470 char *ossprvcmd(char *hst)
3471 {
3472 /* check to see if we're already at the fist command */
3473 if (hst == histtail)
3474 return 0;
3475
3476 /* back up to the previous null byte */
3477 hst = ossdechp(hst);
3478
3479 /* scan back to the previous line */
3480 do
3481 {
3482 hst = ossdechp(hst);
3483 } while (*hst && hst != histtail);
3484
3485 /* step over the null byte to the start of the following line */
3486 if (*hst == 0)
3487 hst = ossadvhp(hst);
3488
3489 /* return the result */
3490 return hst;
3491 }
3492
3493 /*
3494 * ossnxtcmd returns a pointer to the next history command, given
3495 * a pointer to a history command. It returns a null pointer if the
3496 * given command is already past the last command.
3497 */
3498 char *ossnxtcmd(char *hst)
3499 {
3500 /* check to see if we're already past the last line */
3501 if (hst == histhead)
3502 return 0;
3503
3504 /* scan forward to the next null byte */
3505 while(*hst != '\0')
3506 hst = ossadvhp(hst);
3507
3508 /* scan past the null onto the new command */
3509 hst = ossadvhp(hst);
3510
3511 /* return the pointer */
3512 return hst;
3513 }
3514 # endif /* USE_HISTORY */
3515
3516 /* ------------------------------------------------------------------------ */
3517 /*
3518 * Display an input line under construction from the given character
3519 * position. If 'delta_yscroll' is non-null, then we'll scroll the window
3520 * vertically if necessary and fill in '*delta_yscroll' with the number of
3521 * lines we scrolled by.
3522 */
3523 static void ossdsp_str(osgen_win_t *win, int y, int x, int color,
3524 char *str, size_t len, int *delta_yscroll)
3525 {
3526 /* presume we won't scroll */
3527 if (delta_yscroll != 0)
3528 *delta_yscroll = 0;
3529
3530 /* keep going until we exhaust the string */
3531 while (len != 0)
3532 {
3533 size_t cur;
3534 unsigned char oldc;
3535
3536 /* display as much as will fit on the current line before wrapping */
3537 cur = win->winx + win->wid - x;
3538 if (cur > len)
3539 cur = len;
3540
3541 /* null-terminate the chunk, but save the original character */
3542 oldc = str[cur];
3543 str[cur] = '\0';
3544
3545 /* display this chunk */
3546 ossdsp(y, x, color, (char *)str);
3547
3548 /* restore the character where we put our null */
3549 str[cur] = oldc;
3550
3551 /* move our string counters past this chunk */
3552 str += cur;
3553 len -= cur;
3554
3555 /* advance the x position */
3556 x += cur;
3557
3558 /* if we've reached the right edge of the window, wrap the line */
3559 if (x >= win->winx + (int)win->wid)
3560 {
3561 /* wrap to the left edge of the window */
3562 x = win->winx;
3563
3564 /* advance to the next line */
3565 ++y;
3566
3567 /*
3568 * if this puts us past the bottom of the window, and we're
3569 * allowed to scroll the window, do so
3570 */
3571 if (y >= win->winy + (int)win->ht && delta_yscroll != 0)
3572 {
3573 /* scroll by one line */
3574 ossscr(win->winy, win->winx,
3575 win->winy + win->ht - 1, win->winx + win->wid - 1,
3576 win->oss_fillcolor);
3577
3578 /* adjust the scroll position of the window */
3579 win->scrolly++;
3580
3581 /* count the scrolling */
3582 ++(*delta_yscroll);
3583
3584 /* move back a line */
3585 --y;
3586 }
3587 }
3588 }
3589 }
3590
3591 /*
3592 * Move the cursor left by the number of characters.
3593 */
3594 static void oss_gets_csrleft(osgen_txtwin_t *win, int *y, int *x, size_t len)
3595 {
3596 for ( ; len != 0 ; --len)
3597 {
3598 /* move left one character, wrapping at the end of the line */
3599 if (--*x < win->base.winx)
3600 {
3601 /* move up a line */
3602 --*y;
3603
3604 /* move to the end of the line */
3605 *x = win->base.winx + win->base.wid - 1;
3606 }
3607 }
3608 }
3609
3610 /*
3611 * Move the cursor right by the number of characters.
3612 */
3613 static void oss_gets_csrright(osgen_txtwin_t *win, int *y, int *x, size_t len)
3614 {
3615 for ( ; len != 0 ; --len)
3616 {
3617 /* move right one character, wrapping at the end of the line */
3618 if (++*x >= win->base.winx + (int)win->base.wid)
3619 {
3620 /* move down a line */
3621 ++*y;
3622
3623 /* move to the left column */
3624 *x = win->base.winx;
3625 }
3626 }
3627 }
3628
3629 /*
3630 * clear an area of the display formerly occupied by some input text
3631 */
3632 static void oss_gets_clear(osgen_txtwin_t *win, int y, int x, size_t len)
3633 {
3634 /* if we don't even reach the left edge of the window, ignore it */
3635 if (x + (int)len <= win->base.winx)
3636 return;
3637
3638 /* skip anything to the left of the window */
3639 if (x < win->base.winx)
3640 {
3641 /* deduct the unseen part from the length */
3642 len -= (win->base.winx - x);
3643
3644 /* start at the left edge */
3645 x = win->base.winx;
3646 }
3647
3648 /* clear one line at a time */
3649 while (len != 0)
3650 {
3651 size_t cur;
3652
3653 /* calculate how much we have left to the right edge of the window */
3654 cur = win->base.winx + win->base.wid - x;
3655
3656 /* limit the clearing to the requested length */
3657 if (cur > len)
3658 cur = len;
3659
3660 /* clear this chunk */
3661 ossclr(y, x, y, x + cur - 1, win->base.oss_fillcolor);
3662
3663 /* deduct this chunk from the remaining length */
3664 len -= cur;
3665
3666 /* move to the start of the next line */
3667 x = win->base.winx;
3668 ++y;
3669
3670 /* if we're past the bottom of the window, stop */
3671 if (y >= win->base.winy + (int)win->base.ht)
3672 break;
3673 }
3674 }
3675
3676 /*
3677 * Delete a character in the buffer, updating the display.
3678 */
3679 static void oss_gets_delchar(osgen_txtwin_t *win,
3680 char *buf, char *p, char **eol, int x, int y)
3681 {
3682 int color;
3683
3684 /* get the oss color for the current text in the window */
3685 color = ossgetcolor(win->base.txtfg, win->base.txtbg,
3686 win->base.txtattr, win->base.fillcolor);
3687
3688 /* if the character is within the buffer, delete it */
3689 if (p < *eol)
3690 {
3691 /* delete the character and close the gap */
3692 --*eol;
3693 if (p != *eol)
3694 memmove(p, p + 1, *eol - p);
3695
3696 /* null-terminate the shortened buffer */
3697 **eol = '\0';
3698
3699 /* re-display the changed part of the string */
3700 ossdsp_str(&win->base, y, x, color, p, *eol - p, 0);
3701
3702 /* move to the position of the former last character */
3703 oss_gets_csrright(win, &y, &x, *eol - p);
3704
3705 /* clear the screen area where the old last character was displayed */
3706 ossclr(y, x, y, x, win->base.oss_fillcolor);
3707 }
3708 }
3709
3710 /*
3711 * Backspace in the buffer, updating the display and adjusting the cursor
3712 * position.
3713 */
3714 static void oss_gets_backsp(osgen_txtwin_t *win,
3715 char *buf, char **p,
3716 char **eol, int *x, int *y)
3717 {
3718 int color;
3719
3720 /* get the oss color for the current text in the window */
3721 color = ossgetcolor(win->base.txtfg, win->base.txtbg,
3722 win->base.txtattr, win->base.fillcolor);
3723
3724 /* if we can back up, do so */
3725 if (*p > buf)
3726 {
3727 int tmpy;
3728 int tmpx;
3729
3730 /* move our insertion point back one position */
3731 --*p;
3732
3733 /* the line is now one character shorter */
3734 --*eol;
3735
3736 /* shift all of the characters down one position */
3737 if (*p != *eol)
3738 memmove(*p, *p + 1, *eol - *p);
3739
3740 /* move the cursor back, wrapping if at the first column */
3741 if (--*x < win->base.winx)
3742 {
3743 *x = win->base.winx + win->base.wid - 1;
3744 --*y;
3745 }
3746
3747 /* null-terminate the shortened buffer */
3748 **eol = '\0';
3749
3750 /*
3751 * display the string from the current position, so that we update
3752 * the display for the moved characters
3753 */
3754 tmpy = *y;
3755 tmpx = *x;
3756 ossdsp_str(&win->base, tmpy, tmpx, color, *p, *eol - *p, 0);
3757
3758 /* clear the screen area where the old last character was shown */
3759 oss_gets_csrright(win, &tmpy, &tmpx, *eol - *p);
3760 ossclr(tmpy, tmpx, tmpy, tmpx, win->base.oss_fillcolor);
3761 }
3762 }
3763
3764 /*
3765 * Redraw any command line under construction
3766 */
3767 static void osgen_gets_redraw_cmdline(void)
3768 {
3769 osgen_txtwin_t *win;
3770 int x, y;
3771 int color;
3772
3773 /*
3774 * get the default window; if we don't have one, or there's no command
3775 * line editing in progress, there's nothing to do
3776 */
3777 if ((win = S_default_win) == 0 || !S_gets_in_progress)
3778 return;
3779
3780 /* set up at the current cursor position */
3781 x = S_gets_x;
3782 y = S_gets_y;
3783
3784 /* move to the start of the command */
3785 oss_gets_csrleft(win, &y, &x, S_gets_ofs);
3786
3787 /* get the color of the command text */
3788 color = ossgetcolor(win->base.txtfg, win->base.txtbg,
3789 win->base.txtattr, win->base.fillcolor);
3790
3791 /* draw the command line */
3792 ossdsp_str(&win->base, y, x, color, S_gets_buf, strlen(S_gets_buf), 0);
3793 }
3794
3795 /* ------------------------------------------------------------------------ */
3796 /*
3797 * cancel interrupted input
3798 */
3799 void os_gets_cancel(int reset)
3800 {
3801 int x, y;
3802
3803 /* if we're doing scrollback, cancel it */
3804 if (S_sbmode_win != 0)
3805 osgen_sb_mode_end();
3806
3807 /* do any deferred redrawing */
3808 osssb_redraw_if_needed();
3809
3810 /*
3811 * if we interrupted a previous line, apply display effects as though
3812 * the user had pressed return
3813 */
3814 if (S_gets_in_progress)
3815 {
3816 osgen_txtwin_t *win;
3817
3818 /* use the main window */
3819 win = S_main_win;
3820
3821 /* move to the end of the input line */
3822 x = S_gets_x;
3823 y = S_gets_y;
3824 oss_gets_csrright(win, &y, &x, strlen(S_gets_buf + S_gets_ofs));
3825
3826 /* set the cursor to the new position */
3827 ossloc(y, x);
3828
3829 /* copy the buffer to the screen save buffer, adding a newline */
3830 ossaddsb_input(win, (char *)S_gets_buf, TRUE);
3831
3832 /* we no longer have an input in progress */
3833 S_gets_in_progress = FALSE;
3834 }
3835
3836 /* if we're resetting, clear our saved buffer */
3837 if (reset)
3838 S_gets_buf[0] = '\0';
3839 }
3840
3841 /* ------------------------------------------------------------------------ */
3842 /*
3843 * Initialize input line editing mode. Returns true if we can
3844 * successfully set up input line editing mode, false if not.
3845 */
3846 int os_gets_begin(size_t max_line_len)
3847 {
3848 osgen_txtwin_t *win;
3849
3850 /* if there's no default window, there's nothing we can do */
3851 if ((win = S_default_win) == 0)
3852 return FALSE;
3853
3854 /* do any deferred redrawing */
3855 osssb_redraw_if_needed();
3856
3857 /* if the cursor if off the screen vertically, scroll to show it */
3858 if (win->base.y >= win->base.scrolly + (int)win->base.ht)
3859 osgen_scroll_win_fwd(win, win->base.y
3860 - (win->base.scrolly + win->base.ht) + 1);
3861
3862 /* if we're horizontally scrolled, scroll to the left edge */
3863 if (win->base.scrollx != 0)
3864 {
3865 /* scroll to the left edge */
3866 win->base.scrollx = 0;
3867
3868 /* redraw the window */
3869 osgen_redraw_win(&win->base);
3870 }
3871
3872 # ifdef USE_HISTORY
3873 /* allocate the history buffer if it's not already allocated */
3874 if (histbuf == 0)
3875 {
3876 histbuf = (char *)osmalloc(HISTBUFSIZE);
3877 histhead = histtail = histbuf;
3878 S_gets_curhist = histhead;
3879 }
3880 # endif /* USE_HISTORY */
3881
3882 /*
3883 * If we have saved input state from a previous interrupted call,
3884 * restore it now. Otherwise, initialize everything.
3885 */
3886 if (S_gets_buf[0] != '\0' || S_gets_in_progress)
3887 {
3888 /*
3889 * if we cancelled the previous input, we must re-display the
3890 * buffer under construction, since we have displayed something
3891 * else in between and have re-displayed the prompt
3892 */
3893 if (!S_gets_in_progress)
3894 {
3895 int x;
3896 int y;
3897 int deltay;
3898 int color;
3899
3900 /* set up at the window's output position, in screen coords */
3901 x = win->base.winx + win->base.x - win->base.scrollx;
3902 y = win->base.winy + win->base.y - win->base.scrolly;
3903
3904 /* get the current color in the window */
3905 color = ossgetcolor(win->base.txtfg, win->base.txtbg,
3906 win->base.txtattr, win->base.fillcolor);
3907
3908 /* re-display the buffer */
3909 ossdsp_str(&win->base, y, x, color,
3910 S_gets_buf, strlen(S_gets_buf), &deltay);
3911
3912 /* adjust our y position for any scrolling we just did */
3913 y -= deltay;
3914
3915 /* limit the initial offset to the available buffer length */
3916 if (S_gets_ofs > (int)max_line_len)
3917 S_gets_ofs = max_line_len;
3918
3919 /* move back to the original insertion point */
3920 oss_gets_csrright(win, &y, &x, S_gets_ofs);
3921
3922 /* note the current position as the new editing position */
3923 S_gets_x = x;
3924 S_gets_y = y;
3925 }
3926 }
3927 else
3928 {
3929 /* initialize our history recall pointer */
3930 S_gets_curhist = histhead;
3931
3932 /* set up at the window's output position, in screen coords */
3933 S_gets_x = win->base.winx + win->base.x - win->base.scrollx;
3934 S_gets_y = win->base.winy + win->base.y - win->base.scrolly;
3935
3936 /* we're at offset zero in the input line */
3937 S_gets_ofs = 0;
3938 }
3939
3940 /*
3941 * set the buffer end pointer to limit input to the maximum size the
3942 * caller has requested, or the maximum size of our internal buffer,
3943 * whichever is smaller
3944 */
3945 if (max_line_len > S_gets_buf_siz)
3946 S_gets_buf_end = S_gets_buf + S_gets_buf_siz - 1;
3947 else
3948 S_gets_buf_end = S_gets_buf + max_line_len - 1;
3949
3950 /* override the default cursor position logic while reading */
3951 S_special_cursor_pos = TRUE;
3952 S_special_cursor_x = S_gets_x;
3953 S_special_cursor_y = S_gets_y;
3954
3955 /* note that input is in progress */
3956 S_gets_in_progress = TRUE;
3957
3958 /* successfully set up */
3959 return TRUE;
3960 }
3961
3962
3963 /* ------------------------------------------------------------------------ */
3964 /*
3965 * Process an event in input line editing mode. Returns true if the user
3966 * explicitly ended input line editing by pressing Return or something
3967 * similar, false if input line editing mode remains active.
3968 */
3969 int os_gets_process(int event_type, os_event_info_t *event_info)
3970 {
3971 unsigned char c;
3972 char *p;
3973 char *eol;
3974 char *buf;
3975 size_t bufsiz;
3976 int x;
3977 int y;
3978 osgen_txtwin_t *win;
3979 int color;
3980
3981 /* use the default window */
3982 if ((win = S_default_win) == 0)
3983 return TRUE;
3984
3985 /* if it's a keystroke event, convert from "raw" to a CMD_xxx code */
3986 if (event_type == OS_EVT_KEY)
3987 oss_raw_key_to_cmd(event_info);
3988
3989 /*
3990 * if we're in scrollback mode, run the event through the scrollback
3991 * handler rather than handling it directly
3992 */
3993 if (S_sbmode_win != 0)
3994 {
3995 /* run the event through scrollback mode */
3996 if (osgen_sb_mode(win, event_type, event_info))
3997 {
3998 /*
3999 * scrollback mode fully handled the event, so we're done;
4000 * tell the caller we didn't finish command input editing
4001 */
4002 return FALSE;
4003 }
4004 }
4005
4006 /* ignore everything except keystroke events */
4007 if (event_type != OS_EVT_KEY)
4008 return FALSE;
4009
4010 /* get the key from the event */
4011 c = (unsigned char)event_info->key[0];
4012
4013 /* set up at the current position */
4014 x = S_gets_x;
4015 y = S_gets_y;
4016
4017 /* set up our buffer pointers */
4018 p = S_gets_buf + S_gets_ofs;
4019 buf = S_gets_buf;
4020 bufsiz = S_gets_buf_siz;
4021 eol = p + strlen(p);
4022
4023 /* get the current color in the window */
4024 color = ossgetcolor(win->base.txtfg, win->base.txtbg,
4025 win->base.txtattr, win->base.fillcolor);
4026
4027 /*
4028 * Check the character we got. Note that we must interpret certain
4029 * control characters explicitly, because os_get_event() returns raw
4030 * keycodes (untranslated into CMD_xxx codes) for control characters.
4031 */
4032 switch(c)
4033 {
4034 case 8:
4035 /* backspace one character */
4036 oss_gets_backsp(win, buf, &p, &eol, &x, &y);
4037 break;
4038
4039 case 13:
4040 /* Return/Enter key - we're done. Null-terminate the input. */
4041 *eol = '\0';
4042
4043 /* move to the end of the line */
4044 oss_gets_csrright(win, &y, &x, eol - p);
4045 p = eol;
4046
4047 # ifdef USE_HISTORY
4048 /*
4049 * Save the line in our history buffer. If we don't have enough
4050 * room, lose some old text by advancing the tail pointer far
4051 * enough. Don't save it if it's a blank line, though, or if it
4052 * duplicates the most recent previous command.
4053 */
4054 if (strlen(buf) != 0)
4055 {
4056 char *q;
4057 int advtail;
4058 int saveflag = 1; /* assume we will be saving it */
4059
4060 if ((q = ossprvcmd(histhead)) != 0)
4061 {
4062 char *p = buf;
4063
4064 while (*p == *q && *p != '\0' && *q != '\0')
4065 {
4066 ++p;
4067 q = ossadvhp(q);
4068 }
4069 if (*p == *q) /* is this a duplicate command? */
4070 saveflag = 0; /* if so, don't save it */
4071 }
4072
4073 if (saveflag)
4074 {
4075 for (q = buf, advtail = 0 ; q <= eol ; ++q)
4076 {
4077 *histhead = *q;
4078 histhead = ossadvhp(histhead);
4079 if (histhead == histtail)
4080 {
4081 histtail = ossadvhp(histtail);
4082 advtail = 1;
4083 }
4084 }
4085
4086 /*
4087 * If we have encroached on space that was already
4088 * occupied, throw away the entire command we have
4089 * partially trashed; to do so, advance the tail pointer
4090 * to the next null byte.
4091 */
4092 if (advtail)
4093 {
4094 while(*histtail)
4095 histtail = ossadvhp(histtail);
4096 histtail = ossadvhp(histtail);
4097 }
4098 }
4099 }
4100 # endif /* USE_HISTORY */
4101
4102 /* add the text to the scrollback buffer */
4103 ossaddsb_input(win, buf, TRUE);
4104
4105 /* done with the special cursor position */
4106 S_special_cursor_pos = FALSE;
4107
4108 /*
4109 * immediately scroll the window if necessary, and make sure the
4110 * cursor is showing at the right location
4111 */
4112 osgen_auto_vscroll(win);
4113 osssb_cursor_to_default_pos();
4114
4115 /* input is no longer in progress */
4116 S_gets_in_progress = FALSE;
4117
4118 /* tell the caller we're done */
4119 return TRUE;
4120
4121 case 0:
4122 /* extended key code - get the second half of the code */
4123 c = (unsigned char)event_info->key[1];
4124
4125 /* handle the command key code */
4126 switch(c)
4127 {
4128 # ifdef USE_SCROLLBACK
4129 case CMD_SCR:
4130 case CMD_PGUP:
4131 case CMD_PGDN:
4132 /* run the event through scrollback mode */
4133 osgen_sb_mode(win, event_type, event_info);
4134
4135 /* done */
4136 break;
4137 # endif /* USE_SCROLLBACK */
4138
4139 case CMD_LEFT:
4140 /* move the cursor left */
4141 if (p > buf)
4142 {
4143 --p;
4144 oss_gets_csrleft(win, &y, &x, 1);
4145 }
4146 break;
4147
4148 case CMD_WORD_LEFT:
4149 /*
4150 * Move back one word. This moves the cursor back a
4151 * character, then seeks back until we're on a non-space
4152 * character, then seeks back until we're on a character
4153 * preceded by a space character.
4154 */
4155 if (p > buf)
4156 {
4157 /* back up one character */
4158 --p;
4159 oss_gets_csrleft(win, &y, &x, 1);
4160
4161 /* back up until we're on a non-space character */
4162 while (p > buf && t_isspace(*p) && !t_isspace(*(p-1)))
4163 {
4164 --p;
4165 oss_gets_csrleft(win, &y, &x, 1);
4166 }
4167
4168 /*
4169 * back up again until we're on a character preceded by a
4170 * space character
4171 */
4172 while (p > buf && !t_isspace(*(p-1)))
4173 {
4174 --p;
4175 oss_gets_csrleft(win, &y, &x, 1);
4176 }
4177 }
4178 break;
4179
4180 case CMD_RIGHT:
4181 /* move the cursor right */
4182 if (p < eol)
4183 {
4184 ++p;
4185 oss_gets_csrright(win, &y, &x, 1);
4186 }
4187 break;
4188
4189 case CMD_WORD_RIGHT:
4190 /*
4191 * Move right one word. This moves the cursor right until
4192 * we're on a space character, then moves the cursor right
4193 * again until we're on a non-space character. First, move
4194 * right until we're between words (i.e., until we're on a
4195 * space character).
4196 */
4197 while (p < eol && !t_isspace(*p))
4198 {
4199 ++p;
4200 oss_gets_csrright(win, &y, &x, 1);
4201 }
4202
4203 /* now move right until we're on a non-space character */
4204 while (p < eol && t_isspace(*p))
4205 {
4206 ++p;
4207 oss_gets_csrright(win, &y, &x, 1);
4208 }
4209 break;
4210
4211 case CMD_DEL:
4212 /* delete a character */
4213 oss_gets_delchar(win, buf, p, &eol, x, y);
4214 break;
4215
4216 #ifdef UNIX
4217 case CMD_WORDKILL:
4218 {
4219 /* remove spaces preceding word */
4220 while (p >= buf && *p <= ' ')
4221 oss_gets_backsp(win, buf, &p, &eol, &x, &y);
4222
4223 /* remove previous word (i.e., until we get a space) */
4224 while (p >= buf && *p > ' ')
4225 oss_gets_backsp(win, buf, &p, &eol, &x, &y);
4226
4227 /* that's it */
4228 break;
4229 }
4230 #endif /* UNIX */
4231
4232 case CMD_KILL:
4233 case CMD_HOME:
4234 # ifdef USE_HISTORY
4235 case CMD_UP:
4236 case CMD_DOWN:
4237 /*
4238 * Home, Kill (delete entire line), History Up, History Down -
4239 * what these all have in common is that we move to the start
4240 * of the line before doing anything else.
4241 */
4242
4243 /* if 'up', make sure we have more history to traverse */
4244 if (c == CMD_UP && !ossprvcmd(S_gets_curhist))
4245 break;
4246
4247 /* if 'down', make sure there's more history to traverse */
4248 if (c == CMD_DOWN && !ossnxtcmd(S_gets_curhist))
4249 break;
4250
4251 /*
4252 * if this is the first 'up', save the current buffer, so that
4253 * we can reinstate it if we traverse back 'down' until we're
4254 * back at the original buffer (the active buffer essentially
4255 * becomes a temporary history entry that we can recover by
4256 * history-scrolling back down to it)
4257 */
4258 if (c == CMD_UP && !ossnxtcmd(S_gets_curhist))
4259 safe_strcpy(S_hist_sav, S_hist_sav_siz, buf);
4260
4261 # endif /* USE_HISTORY */
4262
4263 /* move to the start of the line */
4264 if (p != buf)
4265 {
4266 /* move the cursor */
4267 oss_gets_csrleft(win, &y, &x, p - buf);
4268
4269 /* move the insertion pointer */
4270 p = buf;
4271 }
4272
4273 /* if it was just a 'home' command, we're done */
4274 if (c == CMD_HOME)
4275 break;
4276
4277 /*
4278 * We're at the start of the line now; fall through for
4279 * KILL, UP, and DOWN to the code which deletes to the end
4280 * of the line.
4281 */
4282
4283 case CMD_DEOL:
4284 /* clear the remainder of the line on the display */
4285 oss_gets_clear(win, y, x, eol - p);
4286
4287 /* truncate the buffer at the insertion point */
4288 eol = p;
4289 *p = '\0';
4290
4291 # ifdef USE_HISTORY
4292 if (c == CMD_UP)
4293 {
4294 S_gets_curhist = ossprvcmd(S_gets_curhist);
4295 osshstcpy(buf, S_gets_curhist);
4296 }
4297 else if (c == CMD_DOWN)
4298 {
4299 if (!ossnxtcmd(S_gets_curhist))
4300 break; /* no more */
4301 S_gets_curhist = ossnxtcmd(S_gets_curhist);
4302 if (ossnxtcmd(S_gets_curhist)) /* on a valid command */
4303 osshstcpy(buf, S_gets_curhist); /* ... so use it */
4304 else
4305 {
4306 /* no more history - restore original line */
4307 safe_strcpy(buf, bufsiz, S_hist_sav);
4308 }
4309 }
4310 if ((c == CMD_UP || c == CMD_DOWN)
4311 && strlen(buf) != 0)
4312 {
4313 int deltay;
4314
4315 /* get the end pointer based on null termination */
4316 eol = buf + strlen(buf);
4317
4318 /* display the string */
4319 ossdsp_str(&win->base, y, x, color, p, eol - p, &deltay);
4320 y -= deltay;
4321
4322 /* move to the end of the line */
4323 oss_gets_csrright(win, &y, &x, eol - p);
4324 p = eol;
4325 }
4326 # endif /* USE_HISTORY */
4327 break;
4328 case CMD_END:
4329 while (p < eol)
4330 {
4331 ++p;
4332 if (++x >= win->base.winx + (int)win->base.wid)
4333 {
4334 x = win->base.winx;
4335 ++y;
4336 }
4337 }
4338 break;
4339 }
4340 break;
4341
4342 default:
4343 if (c >= ' ' && eol < S_gets_buf_end)
4344 {
4345 int deltay;
4346
4347 /* open up the line and insert the character */
4348 if (p != eol)
4349 memmove(p + 1, p, eol - p);
4350 ++eol;
4351 *p = (char)c;
4352 *eol = '\0';
4353
4354 /* write the updated part of the line */
4355 ossdsp_str(&win->base, y, x, color, p, eol - p, &deltay);
4356 y -= deltay;
4357
4358 /* move the cursor right one character */
4359 oss_gets_csrright(win, &y, &x, 1);
4360
4361 /* advance the buffer pointer one character */
4362 ++p;
4363 }
4364 break;
4365 }
4366
4367 /* remember the current editing position */
4368 S_special_cursor_x = S_gets_x = x;
4369 S_special_cursor_y = S_gets_y = y;
4370 S_gets_ofs = p - S_gets_buf;
4371
4372 /* we didn't finish editing */
4373 return FALSE;
4374 }
4375
4376 /* ------------------------------------------------------------------------ */
4377 /*
4378 * Common routine to read a command from the keyboard. This
4379 * implementation provides command editing and history, as well as timeout
4380 * capabilities.
4381 */
4382 int os_gets_timeout(unsigned char *buf, size_t bufl,
4383 unsigned long timeout, int use_timeout)
4384 {
4385 long end_time;
4386 osgen_txtwin_t *win;
4387
4388 /* if we're in 'plain' mode, simply use stdio input */
4389 if (os_f_plain)
4390 {
4391 size_t len;
4392
4393 /* make sure the standard output is flushed */
4394 fflush(stdout);
4395
4396 /* we don't support the timeout feature in plain mode */
4397 if (use_timeout)
4398 return OS_EVT_NOTIMEOUT;
4399
4400 /*
4401 * get input from stdio, and translate the result code - if gets()
4402 * returns null, it indicates an error of some kind, so return an
4403 * end-of-file indication
4404 */
4405 if (fgets((char *)buf, bufl, stdin) == 0)
4406 return OS_EVT_EOF;
4407
4408 /* remove the trailing newline from the buffer, if present */
4409 if ((len = strlen((char *)buf)) != 0 && buf[len-1] == '\n')
4410 buf[len-1] = '\0';
4411
4412 /* indicate that we read a line */
4413 return OS_EVT_LINE;
4414 }
4415
4416 /* begin input editing mode */
4417 os_gets_begin(bufl);
4418
4419 /*
4420 * If we have a timeout, calculate the system clock time at which the
4421 * timeout expires. This is simply the current system clock time plus
4422 * the timeout interval. Since we might need to process a series of
4423 * events, we'll need to know how much time remains at each point we
4424 * get a new event.
4425 */
4426 end_time = os_get_sys_clock_ms() + timeout;
4427
4428 /* use the default window for input */
4429 if ((win = S_default_win) == 0)
4430 return OS_EVT_EOF;
4431
4432 /* process keystrokes until we're done entering the command */
4433 for ( ;; )
4434 {
4435 int event_type;
4436 os_event_info_t event_info;
4437
4438 /* if we're using a timeout, check for expiration */
4439 if (use_timeout)
4440 {
4441 long cur_clock;
4442
4443 /* note the current system clock time */
4444 cur_clock = os_get_sys_clock_ms();
4445
4446 /*
4447 * if we're past the timeout expiration time already,
4448 * interrupt with timeout now
4449 */
4450 if (cur_clock >= end_time)
4451 goto timeout_expired;
4452
4453 /* note the interval remaining to the timeout expiration */
4454 timeout = end_time - cur_clock;
4455 }
4456
4457 /* move to the proper position on the screen before pausing */
4458 if (S_sbmode_win != 0)
4459 ossloc(S_sbmode_win->base.winy, S_sbmode_win->base.winx);
4460 else
4461 ossloc(S_gets_y, S_gets_x);
4462
4463 /* get an event */
4464 event_type = os_get_event(timeout, use_timeout, &event_info);
4465
4466 /* handle the event according to the event type */
4467 switch(event_type)
4468 {
4469 case OS_EVT_TIMEOUT:
4470 timeout_expired:
4471 /* done with the overridden cursor position */
4472 S_special_cursor_pos = FALSE;
4473
4474 /* return the timeout status to the caller */
4475 return OS_EVT_TIMEOUT;
4476
4477 case OS_EVT_NOTIMEOUT:
4478 /*
4479 * we can't handle events with timeouts, so we can't provide
4480 * line reading with timeouts, either
4481 */
4482 S_special_cursor_pos = FALSE;
4483 return OS_EVT_NOTIMEOUT;
4484
4485 case OS_EVT_EOF:
4486 /* end of file - end input and return the EOF to our caller */
4487 S_special_cursor_pos = FALSE;
4488 S_gets_in_progress = FALSE;
4489 return OS_EVT_EOF;
4490
4491 default:
4492 /* process anything else through the input line editor */
4493 if (os_gets_process(event_type, &event_info))
4494 {
4495 /*
4496 * Copy the result to the caller's buffer. Note that we
4497 * know the result will fit, because we always limit the
4498 * editing process to the caller's buffer size.
4499 */
4500 strcpy((char *)buf, S_gets_buf);
4501
4502 /* clear the input buffer */
4503 S_gets_buf[0] = '\0';
4504
4505 /* input is no longer in progress */
4506 S_gets_in_progress = FALSE;
4507
4508 /* return success */
4509 return OS_EVT_LINE;
4510 }
4511 break;
4512 }
4513 }
4514 }
4515
4516 /*
4517 * Read a line of input. We implement this in terms of the timeout input
4518 * line reader, passing an infinite timeout to that routine.
4519 */
4520 uchar *os_gets(unsigned char *buf, size_t bufl)
4521 {
4522 int evt;
4523
4524 /* cancel any previous input, clearing the buffer */
4525 os_gets_cancel(TRUE);
4526
4527 /* get a line of input, with no timeout */
4528 evt = os_gets_timeout(buf, bufl, 0, FALSE);
4529
4530 /* translate the event code to the appropriate return value */
4531 switch(evt)
4532 {
4533 case OS_EVT_LINE:
4534 /* we got a line of input - return a pointer to our buffer */
4535 return buf;
4536
4537 case OS_EVT_EOF:
4538 /* end of file - return null */
4539 return 0;
4540
4541 default:
4542 /* we don't expect any other results */
4543 assert(FALSE);
4544 return 0;
4545 }
4546 }
4547
4548 #else /* USE_STATLINE */
4549
4550 #endif /* USE_STATLINE */
4551
4552 /* ------------------------------------------------------------------------ */
4553 /*
4554 * Highlighting and colors
4555 */
4556
4557 #ifdef STD_OS_HILITE
4558
4559 #ifdef RUNTIME
4560 /*
4561 * Set text attributes
4562 */
4563 void os_set_text_attr(int attr)
4564 {
4565 osgen_txtwin_t *win;
4566
4567 /* if there's no default output window, do nothing */
4568 if ((win = S_default_win) == 0)
4569 return;
4570
4571 /*
4572 * if the attributes are different from the old attributes, add an
4573 * attribute-change sequence to the display buffer
4574 */
4575 if (attr != win->base.txtattr)
4576 {
4577 char buf[3];
4578
4579 /* set up the attribute-change sequence */
4580 buf[0] = OSGEN_ATTR;
4581 buf[1] = (char)attr;
4582 ossaddsb(win, buf, 2, TRUE);
4583 }
4584 }
4585
4586 /*
4587 * Translate a color from the os_color_t encoding to an OSGEN_xxx color.
4588 */
4589 static char osgen_xlat_color_t(os_color_t color)
4590 {
4591 size_t i;
4592 struct color_map_t
4593 {
4594 /* the OSGEN_COLOR_xxx value */
4595 char id;
4596
4597 /* the RGB components for the color */
4598 unsigned char rgb[3];
4599 };
4600 struct color_map_t *p;
4601 struct color_map_t *bestp;
4602 static struct color_map_t color_map[] =
4603 {
4604 { OSGEN_COLOR_BLACK, { 0x00, 0x00, 0x00 } },
4605 { OSGEN_COLOR_WHITE, { 0xFF, 0xFF, 0xFF } },
4606 { OSGEN_COLOR_RED, { 0xFF, 0x00, 0x00 } },
4607 { OSGEN_COLOR_BLUE, { 0x00, 0x00, 0xFF } },
4608 { OSGEN_COLOR_GREEN, { 0x00, 0x80, 0x00 } },
4609 { OSGEN_COLOR_YELLOW, { 0xFF, 0xFF, 0x00 } },
4610 { OSGEN_COLOR_CYAN, { 0x00, 0xFF, 0xFF } },
4611 { OSGEN_COLOR_SILVER, { 0xC0, 0xC0, 0xC0 } },
4612 { OSGEN_COLOR_GRAY, { 0x80, 0x80, 0x80 } },
4613 { OSGEN_COLOR_MAROON, { 0x80, 0x00, 0x00 } },
4614 { OSGEN_COLOR_PURPLE, { 0x80, 0x00, 0x80 } },
4615 { OSGEN_COLOR_MAGENTA, { 0xFF, 0x00, 0xFF } },
4616 { OSGEN_COLOR_LIME, { 0x00, 0xFF, 0x00 } },
4617 { OSGEN_COLOR_OLIVE, { 0x80, 0x80, 0x00 } },
4618 { OSGEN_COLOR_NAVY, { 0x00, 0x00, 0x80 } },
4619 { OSGEN_COLOR_TEAL, { 0x00, 0x80, 0x80 } }
4620 };
4621 unsigned char r, g, b;
4622 unsigned long best_dist;
4623
4624 /*
4625 * If it's parameterized, map it by shifting the parameter code (in
4626 * the high-order 8 bits of the os_color_t) to our single-byte code,
4627 * which is defined as exactly the same code as the os_color_t values
4628 * but shifted into the low-order 8 bits.
4629 */
4630 if (os_color_is_param(color))
4631 return (char)((color >> 24) & 0xFF);
4632
4633 /* break the color into its components */
4634 r = os_color_get_r(color);
4635 g = os_color_get_g(color);
4636 b = os_color_get_b(color);
4637
4638 /* search for the closest match among our 16 ANSI colors */
4639 for (i = 0, p = color_map, bestp = 0, best_dist = 0xFFFFFFFF ;
4640 i < sizeof(color_map)/sizeof(color_map[0]) ; ++i, ++p)
4641 {
4642 unsigned long dist;
4643 int rd, gd, bd;
4644
4645 /* calculate the delta for each component */
4646 rd = r - p->rgb[0];
4647 gd = g - p->rgb[1];
4648 bd = b - p->rgb[2];
4649
4650 /* calculate the "distance" in RGB space */
4651 dist = rd*rd + gd*gd + bd*bd;
4652
4653 /* if it's an exact match, we need look no further */
4654 if (dist == 0)
4655 return p->id;
4656
4657 /* if it's the smallest distance so far, note it */
4658 if (dist < best_dist)
4659 {
4660 best_dist = dist;
4661 bestp = p;
4662 }
4663 }
4664
4665 /* return the OSGEN_COLOR_xxx ID of the best match we found */
4666 return bestp->id;
4667 }
4668
4669 /*
4670 * Set the text colors.
4671 *
4672 * The foreground and background colors apply to subsequent characters
4673 * displayed via os_print(). If the background color is set to zero, it
4674 * indicates "transparent" drawing: subsequent text is displayed with the
4675 * "screen" color.
4676 */
4677 void os_set_text_color(os_color_t fg, os_color_t bg)
4678 {
4679 char buf[4];
4680
4681 /* if we're in plain mode, ignore it */
4682 if (os_f_plain || S_default_win == 0)
4683 return;
4684
4685 /* add the color sequence to the default window's scrollback buffer */
4686 buf[0] = OSGEN_COLOR;
4687 buf[1] = osgen_xlat_color_t(fg);
4688 buf[2] = osgen_xlat_color_t(bg);
4689 ossaddsb(S_default_win, buf, 3, TRUE);
4690 }
4691
4692 /*
4693 * Set the screen color
4694 */
4695 void os_set_screen_color(os_color_t color)
4696 {
4697 /* if we're in plain mode, ignore it */
4698 if (os_f_plain || S_default_win == 0)
4699 return;
4700
4701 /* set the new background color in the default buffer */
4702 S_default_win->base.fillcolor = osgen_xlat_color_t(color);
4703 S_default_win->base.oss_fillcolor =
4704 ossgetcolor(OSGEN_COLOR_TEXT, osgen_xlat_color_t(color), 0, 0);
4705
4706 /* redraw the window if we don't have a scheduled redraw already */
4707 if (!S_deferred_redraw
4708 && !(S_default_win->base.flags & OSGEN_DEFER_REDRAW))
4709 osgen_redraw_win(&S_default_win->base);
4710 }
4711
4712 /* ------------------------------------------------------------------------ */
4713 /*
4714 * Banners
4715 */
4716
4717 /*
4718 * create a banner window
4719 */
4720 void *os_banner_create(void *parent, int where, void *other, int wintype,
4721 int align, int siz, int siz_units, unsigned long style)
4722 {
4723 osgen_win_t *win;
4724
4725 /* we don't support banners in plain mode */
4726 if (os_f_plain)
4727 return 0;
4728
4729 /* if the parent is null, it means that it's a child of the main window */
4730 if (parent == 0)
4731 parent = &S_main_win->base;
4732
4733 /* check for a supported window type */
4734 switch(wintype)
4735 {
4736 case OS_BANNER_TYPE_TEXT:
4737 /*
4738 * Create a text window. We don't support scrollback in the UI in
4739 * banner windows, so we only need enough for what's on the screen
4740 * for redrawing. Overallocate by a bit, though, to be safe in
4741 * case the screen grows later.
4742 */
4743 win = (osgen_win_t *)osgen_create_txtwin(where, other, parent,
4744 G_oss_screen_height * G_oss_screen_width * 2,
4745 G_oss_screen_height * 2);
4746 break;
4747
4748 case OS_BANNER_TYPE_TEXTGRID:
4749 /*
4750 * Create a text grid window. Make it ten lines high at the
4751 * current screen width; we'll automatically expand this
4752 * allocation as needed later, so this size doesn't have to be a
4753 * perfect guess; but the closer we get the better, as it's more
4754 * efficient to avoid reallocating if possible.
4755 */
4756 win = (osgen_win_t *)osgen_create_gridwin(where, other, parent,
4757 G_oss_screen_width, 10);
4758 break;
4759
4760 default:
4761 /* unsupported type - return failure */
4762 return 0;
4763 }
4764
4765 /* if that failed, return null */
4766 if (win == 0)
4767 return 0;
4768
4769 /* set the alignment */
4770 win->alignment = align;
4771
4772 /*
4773 * Start out width a zero size in the settable dimension, and the
4774 * current main text area size in the constrained dimension.
4775 */
4776 if (align == OS_BANNER_ALIGN_LEFT || align == OS_BANNER_ALIGN_RIGHT)
4777 {
4778 /* the width is the settable dimension for a left/right banner */
4779 win->wid = 0;
4780 win->ht = (S_main_win != 0
4781 ? S_main_win->base.ht : G_oss_screen_height);
4782 }
4783 else
4784 {
4785 /* the height is the settable dimension for a top/bottom banner */
4786 win->wid = (S_main_win != 0
4787 ? S_main_win->base.wid : G_oss_screen_width);
4788 win->ht = 0;
4789 }
4790
4791 /* set auto-vscroll mode if they want it */
4792 if ((style & OS_BANNER_STYLE_AUTO_VSCROLL) != 0)
4793 win->flags |= OSGEN_AUTO_VSCROLL;
4794
4795 /*
4796 * Note the MORE mode style, if specified. MORE mode implies
4797 * auto-vscroll, so add that style as well if MORE mode is requested.
4798 */
4799 if ((style & OS_BANNER_STYLE_MOREMODE) != 0)
4800 win->flags |= OSGEN_AUTO_VSCROLL | OSGEN_MOREMODE;
4801
4802 /* note the "strut" style flags, if specified */
4803 if ((style & OS_BANNER_STYLE_VSTRUT) != 0)
4804 win->flags |= OSGEN_VSTRUT;
4805 if ((style & OS_BANNER_STYLE_HSTRUT) != 0)
4806 win->flags |= OSGEN_HSTRUT;
4807
4808 /* remember the requested size */
4809 win->size = siz;
4810 win->size_type = siz_units;
4811
4812 /*
4813 * if the window has a non-zero size, recalculate the layout; if the
4814 * size is zero, we don't have to bother, since the layout won't affect
4815 * anything on the display
4816 */
4817 if (siz != 0)
4818 osgen_recalc_layout();
4819
4820 /* return the window */
4821 return win;
4822 }
4823
4824 /*
4825 * delete a banner
4826 */
4827 void os_banner_delete(void *banner_handle)
4828 {
4829 osgen_win_t *win = (osgen_win_t *)banner_handle;
4830
4831 /* delete the window */
4832 osgen_delete_win(win);
4833
4834 /* recalculate the display layout */
4835 osgen_recalc_layout();
4836 }
4837
4838 /*
4839 * orphan a banner - treat this exactly like delete
4840 */
4841 void os_banner_orphan(void *banner_handle)
4842 {
4843 os_banner_delete(banner_handle);
4844 }
4845
4846 /*
4847 * get information on the banner
4848 */
4849 int os_banner_getinfo(void *banner_handle, os_banner_info_t *info)
4850 {
4851 osgen_win_t *win = (osgen_win_t *)banner_handle;
4852
4853 /* set the alignment */
4854 info->align = win->alignment;
4855
4856 /* set the flags */
4857 info->style = 0;
4858 if ((win->flags & OSGEN_AUTO_VSCROLL) != 0)
4859 info->style |= OS_BANNER_STYLE_AUTO_VSCROLL;
4860 if ((win->flags & OSGEN_MOREMODE) != 0)
4861 info->style |= OS_BANNER_STYLE_MOREMODE;
4862 if ((win->flags & OSGEN_HSTRUT) != 0)
4863 info->style |= OS_BANNER_STYLE_HSTRUT;
4864 if ((win->flags & OSGEN_VSTRUT) != 0)
4865 info->style |= OS_BANNER_STYLE_VSTRUT;
4866
4867 /* set the character size */
4868 info->rows = win->ht;
4869 info->columns = win->wid;
4870
4871 /* we're a character-mode platform, so we don't have a pixel size */
4872 info->pix_width = 0;
4873 info->pix_height = 0;
4874
4875 /*
4876 * We are designed for fixed-pitch character-mode displays only, so we
4877 * support <TAB> alignment by virtue of our fixed pitch.
4878 */
4879 info->style |= OS_BANNER_STYLE_TAB_ALIGN;
4880
4881 /* we do not do our own line wrapping */
4882 info->os_line_wrap = FALSE;
4883
4884 /* indicate success */
4885 return TRUE;
4886 }
4887
4888 /*
4889 * clear the contents of a banner
4890 */
4891 void os_banner_clear(void *banner_handle)
4892 {
4893 osgen_win_t *win = (osgen_win_t *)banner_handle;
4894
4895 /* clear the window */
4896 osgen_clear_win(win);
4897 }
4898
4899 /*
4900 * display text in a banner
4901 */
4902 void os_banner_disp(void *banner_handle, const char *txt, size_t len)
4903 {
4904 osgen_win_t *win = (osgen_win_t *)banner_handle;
4905
4906 /* write the text according to the window type */
4907 switch(win->win_type)
4908 {
4909 case OS_BANNER_TYPE_TEXT:
4910 /* normal text window - write the text into the scrollback buffer */
4911 ossaddsb((osgen_txtwin_t *)win, txt, len, TRUE);
4912 break;
4913
4914 case OS_BANNER_TYPE_TEXTGRID:
4915 /* text grid - write the text into the grid */
4916 osgen_gridwin_write((osgen_gridwin_t *)win, txt, len);
4917 break;
4918 }
4919 }
4920
4921 /*
4922 * set the text attributes in a banner
4923 */
4924 void os_banner_set_attr(void *banner_handle, int attr)
4925 {
4926 osgen_win_t *win = (osgen_win_t *)banner_handle;
4927 char buf[3];
4928
4929 /* if the attributes aren't changing, ignore it */
4930 if (attr == win->txtattr)
4931 return;
4932
4933 /* set the attribute according to the window type */
4934 switch(win->win_type)
4935 {
4936 case OS_BANNER_TYPE_TEXT:
4937 /* add the color sequence to the window's scrollback buffer */
4938 buf[0] = OSGEN_ATTR;
4939 buf[1] = (char)attr;
4940 ossaddsb((osgen_txtwin_t *)win, buf, 2, TRUE);
4941 break;
4942
4943 case OS_BANNER_TYPE_TEXTGRID:
4944 /* text grid windows don't use attributes - ignore it */
4945 break;
4946 }
4947 }
4948
4949 /*
4950 * set the text color in a banner
4951 */
4952 void os_banner_set_color(void *banner_handle, os_color_t fg, os_color_t bg)
4953 {
4954 osgen_win_t *win = (osgen_win_t *)banner_handle;
4955 char buf[4];
4956
4957 /* set the color according to the window type */
4958 switch(win->win_type)
4959 {
4960 case OS_BANNER_TYPE_TEXT:
4961 /* add the color sequence to the window's scrollback buffer */
4962 buf[0] = OSGEN_COLOR;
4963 buf[1] = osgen_xlat_color_t(fg);
4964 buf[2] = osgen_xlat_color_t(bg);
4965 ossaddsb((osgen_txtwin_t *)win, buf, 3, TRUE);
4966 break;
4967
4968 case OS_BANNER_TYPE_TEXTGRID:
4969 /* simply set the current color in the window */
4970 win->txtfg = osgen_xlat_color_t(fg);
4971 win->txtbg = osgen_xlat_color_t(bg);
4972 break;
4973 }
4974 }
4975
4976 /*
4977 * set the window color in a banner
4978 */
4979 void os_banner_set_screen_color(void *banner_handle, os_color_t color)
4980 {
4981 osgen_win_t *win = (osgen_win_t *)banner_handle;
4982
4983 /* set the new background color in the window */
4984 win->fillcolor = osgen_xlat_color_t(color);
4985 win->oss_fillcolor = ossgetcolor(OSGEN_COLOR_TEXT, win->fillcolor, 0, 0);
4986
4987 /* redraw the window if we don't have a redraw scheduled already */
4988 if (!S_deferred_redraw && !(win->flags & OSGEN_DEFER_REDRAW))
4989 osgen_redraw_win(win);
4990 }
4991
4992 /*
4993 * flush text in a banner
4994 */
4995 void os_banner_flush(void *banner_handle)
4996 {
4997 osgen_win_t *win = (osgen_win_t *)banner_handle;
4998
4999 /* if we're deferring redrawing, redraw now */
5000 osgen_redraw_win_if_needed(FALSE, win);
5001 }
5002
5003 /*
5004 * set the size
5005 */
5006 void os_banner_set_size(void *banner_handle, int siz, int siz_units,
5007 int is_advisory)
5008 {
5009 osgen_win_t *win = (osgen_win_t *)banner_handle;
5010
5011 /* if the size isn't changing, do nothing */
5012 if (win->size == siz && win->size_type == siz_units)
5013 return;
5014
5015 /*
5016 * if the size is only advisory, ignore it, since we do implement
5017 * size-to-contents
5018 */
5019 if (is_advisory)
5020 return;
5021
5022 /* set the new size */
5023 win->size = siz;
5024 win->size_type = siz_units;
5025
5026 /* recalculate the layout */
5027 osgen_recalc_layout();
5028 }
5029
5030 /*
5031 * calculate the content height, for os_banner_size_to_contents()
5032 */
5033 static size_t oss_get_content_height(osgen_win_t *win)
5034 {
5035 size_t y;
5036 osgen_win_t *chi;
5037
5038 /* start with our own maximum 'y' size */
5039 y = win->ymax + 1;
5040
5041 /* scan children for vertical strut styles, and include any we find */
5042 for (chi = win->first_child ; chi != 0 ; chi = chi->nxt)
5043 {
5044 /* if this is a vertical strut, include its height as well */
5045 if ((chi->flags & OSGEN_VSTRUT) != 0)
5046 {
5047 /* calculate the child height */
5048 size_t chi_y = oss_get_content_height(chi);
5049
5050 /*
5051 * if the child is horizontal, add its height to the parent's;
5052 * otherwise, it shares the same height, so use the larger of
5053 * the parent's natural height or the child's natural height
5054 */
5055 if (chi->alignment == OS_BANNER_ALIGN_TOP
5056 || chi->alignment == OS_BANNER_ALIGN_BOTTOM)
5057 {
5058 /* it's horizontal - add its height to the parent's */
5059 y += chi_y;
5060 }
5061 else
5062 {
5063 /* it's vertical - they share a common height */
5064 if (y < chi_y)
5065 y = chi_y;
5066 }
5067 }
5068 }
5069
5070 /* return the result */
5071 return y;
5072 }
5073
5074 /*
5075 * calculate the content width, for os_banner_size_to_contents()
5076 */
5077 static size_t oss_get_content_width(osgen_win_t *win)
5078 {
5079 size_t x;
5080 osgen_win_t *chi;
5081
5082 /* start with the maximum 'x' size we've seen in the window */
5083 x = win->xmax + 1;
5084
5085 /* scan children for horizontal strut styles, and include any we find */
5086 for (chi = win->first_child ; chi != 0 ; chi = chi->nxt)
5087 {
5088 /* if this is a horizontal strut, include its height as well */
5089 if ((chi->flags & OSGEN_HSTRUT) != 0)
5090 {
5091 /* calculate the child width */
5092 size_t chi_x = oss_get_content_width(chi);
5093
5094 /*
5095 * if the child is vertical, add its width to the parent's;
5096 * otherwise, it shares the same width, so use the larger of
5097 * the parent's natural width or the child's natural width
5098 */
5099 if (chi->alignment == OS_BANNER_ALIGN_LEFT
5100 || chi->alignment == OS_BANNER_ALIGN_RIGHT)
5101 {
5102 /* it's vertical - add its width to the parent's */
5103 x += chi_x;
5104 }
5105 else
5106 {
5107 /* it's horizontal - they share a common width */
5108 if (x < chi_x)
5109 x = chi_x;
5110 }
5111 }
5112 }
5113
5114 /* return the result */
5115 return x;
5116 }
5117
5118 /*
5119 * size a banner to its contents
5120 */
5121 void os_banner_size_to_contents(void *banner_handle)
5122 {
5123 osgen_win_t *win = (osgen_win_t *)banner_handle;
5124
5125 /* the sizing depends on the window's alignment */
5126 if (win->alignment == OS_BANNER_ALIGN_TOP
5127 || win->alignment == OS_BANNER_ALIGN_BOTTOM)
5128 {
5129 size_t newy;
5130
5131 /* calculate the new height */
5132 newy = oss_get_content_height(win);
5133
5134 /*
5135 * if this is the same as the current height, there's no need to
5136 * redraw anything
5137 */
5138 if (win->ht == newy)
5139 return;
5140
5141 /* set the new size as a fixed character-cell size */
5142 win->size = newy;
5143 win->size_type = OS_BANNER_SIZE_ABS;
5144 }
5145 else
5146 {
5147 size_t newx;
5148
5149 /* calculate the new width */
5150 newx = oss_get_content_width(win);
5151
5152 /* if the size isn't changing, there's no need to redraw */
5153 if (win->wid == newx)
5154 return;
5155
5156 /* set the new size as a fixed character-cell size */
5157 win->size = newx;
5158 win->size_type = OS_BANNER_SIZE_ABS;
5159 }
5160
5161 /* recalculate the window layout */
5162 osgen_recalc_layout();
5163 }
5164
5165 /*
5166 * get the width, in characters, of the banner window
5167 */
5168 int os_banner_get_charwidth(void *banner_handle)
5169 {
5170 /* return the current width from the window */
5171 return ((osgen_win_t *)banner_handle)->wid;
5172 }
5173
5174 /*
5175 * get the height, in characters, of the banner window
5176 */
5177 int os_banner_get_charheight(void *banner_handle)
5178 {
5179 /* return the current height from the window */
5180 return ((osgen_win_t *)banner_handle)->ht;
5181 }
5182
5183 /*
5184 * start HTML mode in a banner
5185 */
5186 void os_banner_start_html(void *banner_handle)
5187 {
5188 /* we don't support HTML mode, so there's nothing to do */
5189 }
5190
5191 /*
5192 * end HTML mode in a banner
5193 */
5194 void os_banner_end_html(void *banner_handle)
5195 {
5196 /* we don't support HTML mode, so there's nothing to do */
5197 }
5198
5199 /*
5200 * set the output position in a text grid window
5201 */
5202 void os_banner_goto(void *banner_handle, int row, int col)
5203 {
5204 osgen_win_t *win = (osgen_win_t *)banner_handle;
5205
5206 /* check the window type */
5207 switch(win->win_type)
5208 {
5209 case OS_BANNER_TYPE_TEXTGRID:
5210 /* it's a text grid - move the output position */
5211 win->y = row;
5212 win->x = col;
5213 break;
5214
5215 default:
5216 /*
5217 * this operation is meaningless with other window types - simply
5218 * ignore it
5219 */
5220 break;
5221 }
5222 }
5223
5224 /* ------------------------------------------------------------------------ */
5225 /*
5226 * Non-RUNTIME version
5227 */
5228 #else /* RUNTIME */
5229
5230 void os_set_text_attr(int attr)
5231 {
5232 /* attributes aren't supported in non-RUNTIME mode - ignore it */
5233 }
5234
5235 void os_set_text_color(os_color_t fg, os_color_t bg)
5236 {
5237 /* colors aren't supported in non-RUNTIME mode - ignore it */
5238 }
5239
5240 void os_set_screen_color(os_color_t color)
5241 {
5242 /* colors aren't supported in non-RUNTIME mode - ignore it */
5243 }
5244
5245 /*
5246 * Banners aren't supported in plain mode
5247 */
5248
5249 void *os_banner_create(void *parent, int where, void *other, int wintype,
5250 int align, int siz, int siz_units, unsigned long style)
5251 {
5252 return 0;
5253 }
5254
5255 void os_banner_delete(void *banner_handle)
5256 {
5257 }
5258
5259 void os_banner_orphan(void *banner_handle)
5260 {
5261 }
5262
5263 int os_banner_getinfo(void *banner_handle, os_banner_info_t *info)
5264 {
5265 return FALSE;
5266 }
5267
5268 void os_banner_clear(void *banner_handle)
5269 {
5270 }
5271
5272 int os_banner_get_charwidth(void *banner_handle)
5273 {
5274 return 0;
5275 }
5276
5277 int os_banner_get_charheight(void *banner_handle)
5278 {
5279 return 0;
5280 }
5281
5282 void os_banner_disp(void *banner_handle, const char *txt, size_t len)
5283 {
5284 }
5285
5286 void os_banner_set_color(void *banner_handle, os_color_t fg, os_color_t bg)
5287 {
5288 }
5289
5290 void os_banner_set_screen_color(void *banner_handle, os_color_t color)
5291 {
5292 }
5293
5294 void os_banner_flush(void *banner_handle)
5295 {
5296 }
5297
5298 void os_banner_set_size(void *banner_handle, int siz, int siz_units,
5299 int is_advisory)
5300 {
5301 }
5302
5303 void os_banner_size_to_contents(void *banner_handle)
5304 {
5305 }
5306
5307 void os_banner_start_html(void *banner_handle)
5308 {
5309 }
5310
5311 void os_banner_end_html(void *banner_handle)
5312 {
5313 }
5314
5315 void os_banner_goto(void *banner_handle, int row, int col)
5316 {
5317 }
5318
5319 #endif /* RUNTIME */
5320
5321 #endif /* STD_OS_HILITE */
5322
5323 /* ------------------------------------------------------------------------ */
5324 /*
5325 * clear the screen, deleting all scrollback information
5326 */
5327 #ifdef STD_OSCLS
5328
5329 void oscls(void)
5330 {
5331 #ifdef RUNTIME
5332 /* do nothing in 'plain' mode */
5333 if (os_f_plain || S_default_win == 0)
5334 return;
5335
5336 /* clear the default window */
5337 osgen_clear_win(&S_default_win->base);
5338 #endif
5339 }
5340
5341 #endif /* STD_OSCLS */
5342
5343 /* ------------------------------------------------------------------------ */
5344 /*
5345 * Simple implementation of os_get_sysinfo. This can be used for any
5346 * non-HTML version of the system, since all sysinfo codes currently
5347 * pertain to HTML features. Note that new sysinfo codes may be added
5348 * in the future which may be relevant to non-html versions, so the
5349 * sysinfo codes should be checked from time to time to ensure that new
5350 * codes relevant to this system version are handled correctly here.
5351 */
5352 int os_get_sysinfo(int code, void *param, long *result)
5353 {
5354 #ifdef RUNTIME
5355 /* if the oss layer recognizes the code, defer to its judgment */
5356 if (oss_get_sysinfo(code, param, result))
5357 return TRUE;
5358 #endif
5359
5360 /* check the type of information they're requesting */
5361 switch(code)
5362 {
5363 case SYSINFO_HTML:
5364 case SYSINFO_JPEG:
5365 case SYSINFO_PNG:
5366 case SYSINFO_WAV:
5367 case SYSINFO_MIDI:
5368 case SYSINFO_WAV_MIDI_OVL:
5369 case SYSINFO_WAV_OVL:
5370 case SYSINFO_MPEG:
5371 case SYSINFO_MPEG1:
5372 case SYSINFO_MPEG2:
5373 case SYSINFO_MPEG3:
5374 case SYSINFO_PREF_IMAGES:
5375 case SYSINFO_PREF_SOUNDS:
5376 case SYSINFO_PREF_MUSIC:
5377 case SYSINFO_PREF_LINKS:
5378 case SYSINFO_LINKS_HTTP:
5379 case SYSINFO_LINKS_FTP:
5380 case SYSINFO_LINKS_NEWS:
5381 case SYSINFO_LINKS_MAILTO:
5382 case SYSINFO_LINKS_TELNET:
5383 case SYSINFO_PNG_TRANS:
5384 case SYSINFO_PNG_ALPHA:
5385 case SYSINFO_OGG:
5386 case SYSINFO_MNG:
5387 case SYSINFO_MNG_TRANS:
5388 case SYSINFO_MNG_ALPHA:
5389 /*
5390 * we don't support any of these features - set the result to 0
5391 * to indicate this
5392 */
5393 *result = 0;
5394
5395 /* return true to indicate that we recognized the code */
5396 return TRUE;
5397
5398 case SYSINFO_INTERP_CLASS:
5399 /* we're a text-only character-mode interpreter */
5400 *result = SYSINFO_ICLASS_TEXT;
5401 return TRUE;
5402
5403 #ifdef RUNTIME
5404
5405 case SYSINFO_BANNERS:
5406 /*
5407 * we support the os_banner_xxx() interfaces, as long as we're not
5408 * in "plain" mode
5409 */
5410 *result = !os_f_plain;
5411 return TRUE;
5412
5413 #endif /* RUNTIME */
5414
5415 default:
5416 /* not recognized */
5417 return FALSE;
5418 }
5419 }
5420
5421 /* ------------------------------------------------------------------------ */
5422 /*
5423 * Set the saved-game extension. Most platforms don't need to do
5424 * anything with this information, and in fact most platforms won't even
5425 * have a way of letting the game author set the saved game extension,
5426 * so this trivial implementation is suitable for most systems.
5427 *
5428 * The purpose of setting a saved game extension is to support platforms
5429 * (such as Windows) where the filename suffix is used to associate
5430 * document files with applications. Each stand-alone executable
5431 * generated on such platforms must have a unique saved game extension,
5432 * so that the system can associate each game's saved position files
5433 * with that game's executable.
5434 */
5435 void os_set_save_ext(const char *ext)
5436 {
5437 /* ignore the setting */
5438 }
5439
5440 const char *os_get_save_ext()
5441 {
5442 /* we ignore the setting, so always return null */
5443 return 0;
5444 }
5445
5446 /* ------------------------------------------------------------------------ */
5447 /*
5448 * Set the game title. Most platforms have no use for this information,
5449 * so they'll just ignore it. This trivial implementation simply
5450 * ignores the title.
5451 */
5452 #ifdef USE_NULL_SET_TITLE
5453
5454 void os_set_title(const char *title)
5455 {
5456 /* ignore the information */
5457 }
5458
5459 #endif /* USE_NULL_SET_TITLE */
5460