-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathosgen3.c
5460 lines (4601 loc) · 156 KB
/
osgen3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef RCSID
static char RCSid[] =
"$Header: d:/cvsroot/tads/TADS2/OSGEN.C,v 1.3 1999/07/11 00:46:30 MJRoberts Exp $";
#endif
/*
* Copyright (c) 1990, 2002 Michael J. Roberts. All Rights Reserved.
*
* Please see the accompanying license file, LICENSE.TXT, for information
* on using and copying this software.
*/
/*
Name
osgen3 - Operating System dependent functions, general implementation
TADS 3 Version, with new "banner" interface
Function
This module contains certain OS-dependent functions that are common
to many character-mode platforms. ("Character mode" means that the
display is organized as a rectangular grid of characters in a monospaced
font. This module isn't usable for most GUI systems, because it doesn't
have any support for variable-pitch fonts - GUI ports generally need to
provide their own custom versions of the os_xxx() functions this module
module provides. On GUI systems you should simply omit this entire
module from the build, and instead substitute a new module of your own
creation that defines your custom versions of the necessary os_xxx()
functions.)
Some routines in this file are selectively enabled according to macros
defined in os.h, since some ports that use this file will want to provide
their own custom versions of these routines instead of the ones defined
here. The macros and associated functions are:
USE_STDIO - implement os_print, os_flush, os_gets with stdio functions
USE_DOSEXT - implement os_remext, os_defext using MSDOS-like filename
conventions
USE_NULLINIT - implement os_init and os_term as do-nothing routines
USE_NULLPAUSE - implement os_expause as a do-nothing routine
USE_EXPAUSE - use an os_expause that prints a 'strike any key' message
and calls os_waitc
USE_TIMERAND - implement os_rand using localtime() as a seed
USE_NULLSTAT - use a do-nothing os_status function
USE_NULLSCORE - use a do-nothing os_score function
RUNTIME - enable character-mode console implementation
USE_STATLINE - implement os_status and os_score using character-mode
status line implementation
USE_OVWCHK - implements default saved file overwrite check
USE_NULLSTYPE - use a dummy os_settype routine
USE_NULL_SET_TITLE - use an empty os_set_title() implementation
If USE_STDIO is defined, we'll implicitly define USE_STDIO_INPDLG.
If USE_STATLINE is defined, certain subroutines must be provided for
your platform that handle the character-mode console:
ossclr - clears a portion of the screen
ossdsp - displays text in a given color at a given location
ossscr - scroll down (i.e., moves a block of screen up)
ossscu - scroll up (i.e., moves a block of screen down)
ossloc - locate cursor
If USE_STATLINE is defined, certain sub-options can be enabled:
USE_SCROLLBACK - include output buffer capture in console system
USE_HISTORY - include command editing and history in console system
Notes
Modified
01/01/98 MJRoberts - moved certain osgen.c routines to osnoui.c
04/24/93 JEras - add os_locate() for locating tads-related files
04/12/92 MJRoberts - add os_strsc (string score) function
03/26/92 MJRoberts - add os_setcolor function
09/26/91 MJRoberts - os/2 user exit support
09/04/91 MJRoberts - stop reading resources if we find '$eof' resource
08/28/91 MJRoberts - debugger bug fix
08/01/91 MJRoberts - make runstat work correctly
07/30/91 MJRoberts - add debug active/inactive visual cue
05/23/91 MJRoberts - add user exit reader
04/08/91 MJRoberts - add full-screen debugging support
03/10/91 MJRoberts - integrate John's qa-scripter mods
11/27/90 MJRoberts - use time() not localtime() in os_rand; cast time_t
11/15/90 MJRoberts - created (split off from os.c)
*/
#define OSGEN_INIT
# include "os.h"
#undef OSGEN_INIT
#include "osgen.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <assert.h>
#include "run.h"
#if defined(TURBO) || defined(DJGPP)
#include "io.h"
#endif
#include "lib.h"
#include "tio.h"
/*
* Flag: use "plain" mode. If this is set, we'll use plain stdio output
* rather than our window-oriented display.
*/
int os_f_plain = 0;
#ifdef RUNTIME
# ifdef USE_SCROLLBACK
/*
* Screen size variables. The underlying system-specific "oss" code must
* initialize these during startup and must keep them up-to-date if the
* screen size ever changes.
*/
int G_oss_screen_width = 80;
int G_oss_screen_height = 24;
# endif /* USE_SCROLLBACK */
#endif /* RUNTIME */
/*
* The special character codes for controlling color.
*/
/*
* set text attributes: this is followed by one byte giving the new
* attribute codes
*/
#define OSGEN_ATTR 1
/*
* Set text color: this is followed by two bytes giving the foreground and
* background colors as OSGEN_COLOR_xxx codes.
*
* Note well that the colors encoded in this escape sequence are
* OSGEN_COLOR_xxx values, not os_color_t values. The latter require 32
* bits because they can store 24-bit RGB values plus some special
* parameter codes, while our internal OSGEN_COLOR_xxx values are only a
* byte each.
*/
#define OSGEN_COLOR 2
/*
* If this port is to use the default saved file overwrite check, define
* USE_OVWCHK. This routine tries to open the file; if successful, the
* file is closed and we ask the user if they're sure they want to overwrite
* the file.
*/
#ifdef USE_OVWCHK
int os_chkovw(char *filename)
{
FILE *fp;
if ((fp = fopen( filename, "r" )) != 0)
{
char buf[128];
fclose(fp);
os_printz("That file already exists. Overwrite it? (y/n) >");
os_gets((uchar *)buf, sizeof(buf));
if (buf[0] != 'y' && buf[0] != 'Y')
return 1;
}
return 0;
}
#endif /* USE_OVWCHK */
/*
* non-stop mode does nothing in character-mode implementations, since the
* portable console layer handles MORE mode
*/
void os_nonstop_mode(int flag)
{
}
/* ------------------------------------------------------------------------ */
/*
* Ports can implement os_flush and os_gets as calls to the stdio routines
* of the same name, and os_print and os_printz using the fputs() to
* stdout, by defining USE_STDIO. These definitions can be used for any
* port for which the standard C run-time library is available.
*/
#ifdef USE_STDIO
/*
* os_printz works just like fputs() to stdout: we write a null-terminated
* string to the standard output.
*/
void os_printz(const char *str)
{
fputs(str, stdout);
}
/*
* os_puts works like fputs() to stdout, except that we are given a
* separate length, and the string might not be null-terminated
*/
void os_print(const char *str, size_t len)
{
printf("%.*s", (int)len, str);
}
/*
* os_flush forces output of anything buffered for standard output. It
* is generally used prior to waiting for a key (so the normal flushing
* may not occur, as it does when asking for a line of input).
*/
void os_flush(void)
{
fflush(stdout);
}
/*
* update the display - since we're using text mode, there's nothing we
* need to do
*/
void os_update_display(void)
{
}
/*
* os_gets performs the same function as gets(). It should get a
* string from the keyboard, echoing it and allowing any editing
* appropriate to the system, and return the null-terminated string as
* the function's value. The closing newline should NOT be included in
* the string.
*/
uchar *os_gets(uchar *s, size_t bufl)
{
/* make sure everything we've displayed so far is actually displayed */
fflush(stdout);
/* get a line of input from the standard input */
return((uchar *)fgets((char *)s, bufl, stdin));
}
/*
* The default stdio implementation does not support reading a line of
* text with timeout.
*/
int os_gets_timeout(unsigned char *buf, size_t bufl,
unsigned long timeout, int resume_editing)
{
/* tell the caller this operation is not supported */
return OS_EVT_NOTIMEOUT;
}
/*
* since we don't support os_gets_timeout(), we don't need to do anything
* in the cancel routine
*/
void os_gets_cancel(int reset)
{
/* os_gets_timeout doesn't do anything, so neither do we */
}
/*
* Get an event - stdio version. This version does not accept a timeout
* value, and can only get a keystroke.
*/
int os_get_event(unsigned long timeout, int use_timeout,
os_event_info_t *info)
{
/* if there's a timeout, return an error indicating we don't allow it */
if (use_timeout)
return OS_EVT_NOTIMEOUT;
/* get a key the normal way */
info->key[0] = os_getc();
/* if it's an extended key, get the other key */
if (info->key[0] == 0)
{
/* get the extended key code */
info->key[1] = os_getc();
/* if it's EOF, return an EOF event rather than a key event */
if (info->key[1] == CMD_EOF)
return OS_EVT_EOF;
}
/* return the keyboard event */
return OS_EVT_KEY;
}
#endif /* USE_STDIO */
/******************************************************************************
* Ports without any special initialization/termination requirements can define
* USE_NULLINIT to pick up the default definitions below. These do nothing, so
* ports requiring special handling at startup and/or shutdown time must define
* their own versions of these routines.
******************************************************************************/
#ifdef USE_NULLINIT
/* os_init returns 0 for success, 1 for failure. The arguments are &argc, the
* address of the count of arguments to the program, and argv, the address of
* an array of up to 10 pointers to those arguments. For systems which don't
* pass a standard command line (such as the Mac Finder), the arguments should
* be read here using some alternate mechanism (an alert box, for instance),
* and the values of argc and argv[] updated accordingly. Note that a maximum
* of 10 arguments are allowed to be stored in the argv[] array. The command
* line itself can be stored in buf, which is a buffer passed by the caller
* guaranteed to be bufsiz bytes long.
*
* Unix conventions are followed, so argc is 1 when no arguments are present.
* The final argument is a prompt string which can be used to ask the user for
* a command line; its use is not required, but may be desirable for producing
* a relevant prompt message. See the Mac implementation for a detailed
* example of how this mechanism is used.
*/
int os_init(int *argc, char *argv[], const char *prompt,
char *buf, int bufsiz)
{
return 0;
}
/*
* uninitialize
*/
void os_uninit(void)
{
}
/*
* os_term should perform any necessary cleaning up, then terminate the
* program. The int argument is a return code to be passed to the
* caller, generally 0 for success and other for failure.
*/
void os_term(int rc)
{
exit(rc);
}
#endif /* USE_NULLINIT */
/* ------------------------------------------------------------------------ */
/*
* Ports can define USE_NULLPAUSE if no pause is required on exit.
*
* Ports needing an exit pause, and can simply print a message (with
* os_print) and wait for a key (with os_getc) can define USE_EXPAUSE.
*/
#ifdef USE_NULLPAUSE
void os_expause(void)
{
/* does nothing */
}
#endif /* USE_NULLPAUSE */
#ifdef USE_EXPAUSE
void os_expause(void)
{
os_printz("(Strike any key to exit...)");
os_flush();
os_waitc();
}
#endif /* USE_EXPAUSE */
#ifdef USE_NULLSTAT
/*
* USE_NULLSTAT defines a do-nothing version of os_status.
*/
void os_status(int stat)
{
/* ignore the new status */
}
int os_get_status()
{
return 0;
}
#endif /* USE_NULLSTAT */
#ifdef USE_NULLSCORE
/*
* USE_NULLSCORE defines a do-nothing version of os_score.
*/
void os_score(int cur, int turncount)
{
/* ignore the score information */
}
void os_strsc(const char *p)
{
/* ignore */
}
#endif /* USE_NULLSCORE */
/* ------------------------------------------------------------------------ */
/*
* Scrollback
*/
/* forward-declare the window control structure type */
typedef struct osgen_win_t osgen_win_t;
/*
* We can be compiled with or without scrollback. The first version
* defines the scrollback implementation; the second just defines some
* dummy functions for the non-scrollback implementation.
*/
# ifdef USE_SCROLLBACK
/* ------------------------------------------------------------------------ */
/*
* Character color structure
*/
typedef struct osgen_charcolor_t osgen_charcolor_t;
struct osgen_charcolor_t
{
/* foreground color, as an OSGEN_COLOR_xxx value */
char fg;
/* background color, as an OSGEN_COLOR_xxx value */
char bg;
};
/*
* Window control structure. Each on-screen window is represented by one
* of these structures.
*/
struct osgen_win_t
{
/* type of the window - this is an OS_BANNER_TYPE_xxx code */
int win_type;
/* flags */
unsigned int flags;
/* next window in window list */
osgen_win_t *nxt;
/*
* Parent window. We take our display area out of the parent window's
* allocated space at the time we lay out this window.
*/
osgen_win_t *parent;
/* head of list of children of this window */
osgen_win_t *first_child;
/*
* The window's alignment type - this determines where the window goes
* relative to the main window. This is an OS_BANNER_ALIGN_xxx
* alignment type code.
*/
int alignment;
/* size type (as an OS_BANNER_SIZE_xxx value) */
int size_type;
/*
* The window's size. If size_type is OS_BANNER_SIZE_ABS, this is the
* size of the window in character cells. If size_type is
* OS_BANNER_SIZE_PCT, this is given as a percentage of the full screen
* size.
*/
int size;
/* upper-left corner position of window on screen */
int winx;
int winy;
/* size of window on screen */
size_t wid;
size_t ht;
/*
* Cursor position (location of next output). These are given in
* "document coordinates", which is to say that they're relative to
* the start of the text in the buffer.
*
* To translate to "window coordinates", simply subtract the scrolling
* offsets, which give the document coordinates of the first character
* displayed at the upper left corner of the window.
*
* To translate to absolute screen coordinates, first subtract the
* scrolling offsets to get window coordinates, then add the window's
* screen position (winx,winy).
*/
int x;
int y;
/*
* maximum row and line where we've actually written any text (this
* can be used for purposes like setting horizontal scrollbar limits
* and sizing a window horizontally to its contents)
*/
int xmax;
int ymax;
/* scrolling offset of text displayed in window */
int scrollx;
int scrolly;
/*
* current text foreground and background colors (as OSGEN_COLOR_xxx
* values)
*/
char txtfg;
char txtbg;
/* current text attributes */
int txtattr;
/* window fill color (and oss color code for same) */
char fillcolor;
int oss_fillcolor;
};
/*
* Ordinary text stream window. This is a subclass of the basic
* osgen_win_t window type, used when win_type is OS_BANNER_TYPE_TEXT.
*
* Ordinary text windows keep a circular buffer of scrollback text. We
* optimize space by using escape codes embedded in the saved text stream
* to select colors and attributes.
*
* In the circular text buffer, each line ends with a null byte. We keep
* an array of line-start pointers to make it fast and easy to find the
* first byte of a particular line. The line-start array is also
* circular, and is organized in ascending order of row number.
*/
typedef struct osgen_txtwin_t osgen_txtwin_t;
struct osgen_txtwin_t
{
/* embed the base class */
osgen_win_t base;
/* text color and attributes at start of latest line */
int solfg;
int solbg;
int solattr;
/* window text buffer, and size of the buffer */
char *txtbuf;
size_t txtbufsiz;
/* next free byte of window text buffer */
char *txtfree;
/* circular array of line-start pointers */
char **line_ptr;
size_t line_ptr_cnt;
/* index of first line-start pointer in use */
size_t first_line;
/* number of lines of text stored in the buffer */
size_t line_count;
};
/*
* Text Grid window. This is a subclass of the basic window type,
* osgen_win_t, used when win_type is OS_BANNER_TYPE_TEXTGRID.
*
* A text grid window keeps a simple rectangular array of text, and a
* corresponding array of the color of each character. The size of each
* array is at least as large as the window's actual area on the screen;
* when we resize the window, we'll reallocate the arrays at a larger size
* if the window has expanded beyond the stored size. We don't keep any
* scrollback information in a text grid; we only keep enough to cover
* what's actually on the screen.
*/
typedef struct osgen_gridwin_t osgen_gridwin_t;
struct osgen_gridwin_t
{
/* embed the base class */
osgen_win_t base;
/* width and height of the text and color arrays */
size_t grid_wid;
size_t grid_ht;
/* text array */
char *grid_txt;
/* color array */
osgen_charcolor_t *grid_color;
};
/*
* Window flags
*/
/* keep the cursor visible when adding text to the window */
#define OSGEN_AUTO_VSCROLL 0x0001
/* the window buffer is full and we're allowing nothing more to enter it */
#define OSGEN_FULL 0x0002
/* the window is in deferred-redraw mode */
#define OSGEN_DEFER_REDRAW 0x0004
/*
* MORE mode in the banner. Note that we keep track of this only so that
* we can indicate it on queries for the banner style; we count on the
* caller to handle the actual prompting for us.
*/
#define OSGEN_MOREMODE 0x0008
/* child banner "strut" flags */
#define OSGEN_VSTRUT 0x0010
#define OSGEN_HSTRUT 0x0020
/*
* The main text area window. This window is special, because it's the
* root of the window tree.
*/
static osfar_t osgen_txtwin_t *S_main_win = 0;
/* default status line window */
static osfar_t osgen_txtwin_t *S_status_win = 0;
/* default input/output window (for os_print, os_gets, etc) */
static osfar_t osgen_txtwin_t *S_default_win = 0;
/* current scrollback-mode window */
static osfar_t osgen_txtwin_t *S_sbmode_win = 0;
/* scrollback mode settings */
static osfar_t int S_sbmode_orig_scrolly;
static osfar_t int S_sbmode_orig_x;
static osfar_t char **S_sbmode_orig_last_line;
static osfar_t char *S_sbmode_orig_txtfree;
/*
* flag: we're using a special cursor position; we use this to override our
* normal default cursor position
*/
static osfar_t int S_special_cursor_pos = FALSE;
static osfar_t int S_special_cursor_x = 0;
static osfar_t int S_special_cursor_y = 0;
/*
* Flag: deferred redraw required. This indicates that something happened
* that requires redrawing the screen, but we didn't bother actually doing
* the redrawing immediately in case other things that would also require
* redrawing were to occur shortly.
*/
static osfar_t int S_deferred_redraw = FALSE;
/*
* Input buffer state. This information is defined statically because
* os_gets_timeout() can carry the information from invocation to
* invocation when input editing is interrupted by a tmieout.
*/
static osfar_t char S_gets_internal_buf[256]; /* internal save buffer */
static osfar_t char *S_gets_buf = S_gets_internal_buf; /* current save buf */
static osfar_t char *S_gets_buf_end = 0; /* end of input buffer */
static osfar_t size_t S_gets_buf_siz = sizeof(S_gets_internal_buf); /* size */
static osfar_t int S_gets_ofs; /* offset in buffer of insertion point */
static osfar_t char *S_gets_curhist; /* current history pointer */
static osfar_t int S_gets_x, S_gets_y; /* saved cursor position */
# ifdef USE_HISTORY
/* save buffer for line being edited before history recall began */
static osfar_t char S_hist_sav_internal[256];
static osfar_t char *S_hist_sav = S_hist_sav_internal;
static osfar_t size_t S_hist_sav_siz = sizeof(S_hist_sav_internal);
# endif /* USE_HISTORY */
/* strcpy with destination buffer size limit */
extern void safe_strcpy(char *dst, size_t dstlen, const char *src);
/*
* Flag: input is already in progress. When os_gets_timeout() returns
* with OS_EVT_TIMEOUT, it sets this flag to true. os_gets_cancel() sets
* this flag to false.
*
* When os_gets_timeout() is called again, it checks this flag to see if
* the input session was cancelled; if not, the routine knows that the
* partially-edited input line is already displayed where it left off,
* because the display has not been modified since the interrupted call to
* os_gets_timeout() returned.
*/
static osfar_t int S_gets_in_progress = FALSE;
/* forward declarations */
static void osssb_add_color_code(osgen_txtwin_t *win);
static void osgen_gridwin_clear(osgen_gridwin_t *win, size_t ofs, size_t len);
static void osgen_redraw_win(osgen_win_t *win);
static void osgen_scrdisp(osgen_win_t *win, int x, int y, int len);
static void osgen_gets_redraw_cmdline(void);
/*
* Delete a window
*/
static void osgen_delete_win(osgen_win_t *win)
{
osgen_win_t *prv;
osgen_win_t *cur;
osgen_txtwin_t *twin;
osgen_gridwin_t *gwin;
/* if we have a parent, remove ourselves from our parent's child list */
if (win->parent != 0)
{
/* scan our parent's child list */
for (prv = 0, cur = win->parent->first_child ;
cur != 0 && cur != win ;
prv = cur, cur = cur->nxt) ;
/* if we found it, unlink it */
if (cur != 0)
{
/* set the previous item's forward pointer */
if (prv == 0)
win->parent->first_child = win->nxt;
else
prv->nxt = win->nxt;
}
}
/*
* Remove the parent reference from each child of this window. We're
* going to be deleted, so we can't keep references from our children
* to us.
*/
for (cur = win->first_child ; cur != 0 ; cur = cur->nxt)
cur->parent = 0;
/* delete the window according to its type */
switch(win->win_type)
{
case OS_BANNER_TYPE_TEXT:
/* get the text window subclass data */
twin = (osgen_txtwin_t *)win;
/* delete its scrollback buffer, if it has one */
if (twin->txtbuf != 0)
osfree(twin->txtbuf);
/* delete the line pointers */
if (twin->line_ptr != 0)
osfree(twin->line_ptr);
break;
case OS_BANNER_TYPE_TEXTGRID:
/* get the grid window subclass data */
gwin = (osgen_gridwin_t *)win;
/* delete the character and color arrays */
if (gwin->grid_txt != 0)
osfree(gwin->grid_txt);
if (gwin->grid_color != 0)
osfree(gwin->grid_color);
break;
}
/* delete the window itself */
osfree(win);
}
/*
* Delete a window and all of its children
*/
static void osgen_delete_win_tree(osgen_win_t *win)
{
osgen_win_t *chi;
osgen_win_t *nxt;
/* delete the children first */
for (chi = win->first_child ; chi != 0 ; chi = nxt)
{
/* remember the next one before we delete the current one */
nxt = chi->nxt;
/* delete this one */
osgen_delete_win_tree(chi);
}
/* delete this window */
osgen_delete_win(win);
}
/*
* Create a window and link it into our list. We initialize the window
* and allocate its display buffer, but we do NOT set the window's size or
* position on the screen.
*/
static osgen_win_t *osgen_create_win(int win_type, int where, void *other,
osgen_win_t *parent)
{
osgen_win_t *win;
osgen_win_t *prv;
osgen_win_t *cur;
size_t struct_siz;
/* figure the structure size based on the window type */
switch(win_type)
{
case OS_BANNER_TYPE_TEXT:
struct_siz = sizeof(osgen_txtwin_t);
break;
case OS_BANNER_TYPE_TEXTGRID:
struct_siz = sizeof(osgen_gridwin_t);
break;
default:
/* unrecognized type - return failure */
return 0;
}
/* create the window object */
win = (osgen_win_t *)osmalloc(struct_siz);
if (win == 0)
return 0;
/* remember the type */
win->win_type = win_type;
/* it's not in a window list yet */
win->nxt = 0;
/* initialize with default colors */
win->txtfg = OSGEN_COLOR_TEXT;
win->txtbg = OSGEN_COLOR_TRANSPARENT;
win->txtattr = 0;
win->fillcolor = OSGEN_COLOR_TEXTBG;
/* cache the oss translation of the fill color */
win->oss_fillcolor = ossgetcolor(OSGEN_COLOR_TEXT, OSGEN_COLOR_TEXTBG,
0, 0);
/* start at the upper left corner */
win->x = 0;
win->y = 0;
win->scrollx = 0;
win->scrolly = 0;
/*
* the window's position on screen will eventually be set by
* osgen_recalc_layout(), but initialize the position to a reasonable
* value for now in case anyone looks at it before then
*/
win->winx = 0;
win->winy = 0;
/* we haven't seen any text in the window yet */
win->xmax = 0;
win->ymax = 0;
/* clear the flags */
win->flags = 0;
/* remember our parent */
win->parent = parent;
/* we have no children yet */
win->first_child = 0;
/* if there's a parent, insert the window into the parent's child list */
if (parent != 0)
{
/* insert into the parent's child list at the proper point */
switch(where)
{
case OS_BANNER_FIRST:
/* link it at the head of the list */
win->nxt = parent->first_child;
parent->first_child = win;
break;
case OS_BANNER_LAST:
default:
/* find the end of the parent's list */
for (cur = parent->first_child ; cur != 0 && cur->nxt != 0 ;
cur = cur->nxt) ;
/* link it after the last element */
win->nxt = 0;
if (cur != 0)
cur->nxt = win;
else
parent->first_child = win;
/* done */
break;
case OS_BANNER_BEFORE:
case OS_BANNER_AFTER:
/* scan the parent's child list, looking for 'other' */
for (prv = 0, cur = parent->first_child ;
cur != 0 && cur != other ;
prv = cur, cur = cur->nxt) ;
/*
* if we didn't find 'other', link the new window at the tail
* of the list by default; since 'prv' will be the last item if
* we didn't find 'other', we can simply set the link mode to
* 'before' to link before the placeholder null at the end of
* the list
*/
if (cur == 0)
where = OS_BANNER_BEFORE;
/* if we're linking after 'cur', advance one position */
if (where == OS_BANNER_AFTER)
{
prv = cur;
cur = cur->nxt;
}
/* link before 'cur', which is right after 'prv' */
win->nxt = cur;
if (prv != 0)
prv->nxt = win;
else
parent->first_child = win;
/* done */
break;
}
}
/* return the new window */
return win;
}
/*
* Create a text window
*/
static osgen_txtwin_t *osgen_create_txtwin(int where, void *other,
void *parent,
unsigned int buf_size,
unsigned int buf_lines)
{
osgen_txtwin_t *win;
/* create the base window */
win = (osgen_txtwin_t *)osgen_create_win(OS_BANNER_TYPE_TEXT,
where, other, parent);
/* if that failed, give up now */
if (win == 0)
return 0;
/* allocate the window's buffer */
win->txtbufsiz = buf_size;
win->txtbuf = (char *)osmalloc(buf_size);
/* allocate the line starts */
win->line_ptr_cnt = buf_lines;
win->line_ptr = (char **)osmalloc(buf_lines * sizeof(char *));
/* make sure we allocated everything properly */
if (win->txtbuf == 0 || win->line_ptr == 0)
{
/* free anything we allocated */
osgen_delete_win(&win->base);
/* return failure */
return 0;
}
/* set up the buffer free pointer */
win->txtfree = win->txtbuf;
/* start out with a single line in the buffer */
win->line_count = 1;
/* set up the first line start */
win->first_line = 0;
win->line_ptr[0] = win->txtbuf;
/* initialize the start-of-line colors */
win->solfg = win->base.txtfg;
win->solbg = win->base.txtbg;
win->solattr = win->base.txtattr;
/* start the new first line with the current text color */
osssb_add_color_code(win);
/* return the new window */
return win;
}
/*
* Create a text grid window
*/
static osgen_gridwin_t *osgen_create_gridwin(int where, void *other,
void *parent,
int grid_wid, int grid_ht)
{
osgen_gridwin_t *win;
/* create the base window */
win = (osgen_gridwin_t *)osgen_create_win(OS_BANNER_TYPE_TEXTGRID,
where, other, parent);