-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtextonly.c
2380 lines (2185 loc) · 69.2 KB
/
textonly.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
/*
** This file is part of the Matrix Brandy Basic VI Interpreter.
** Copyright (C) 2000-2014 David Daniels
** Copyright (C) 2018-2024 Michael McConnell and contributors
**
** Brandy is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2, or (at your option)
** any later version.
**
** Brandy is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Brandy; see the file COPYING. If not, write to
** the Free Software Foundation, 59 Temple Place - Suite 330,
** Boston, MA 02111-1307, USA.
**
**
** This file contains the VDU driver emulation for the interpreter.
** This version of the code is used on targets which do not
** support graphics
**
** 06-Dec-2018 JGH: DEL expects 0 command bytes.
** 28-Dec-2018 JGH: Corrected physical colour numbers.
**
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <math.h>
#include <unistd.h>
#include "common.h"
#include "target.h"
#include "errors.h"
#include "basicdefs.h"
#include "scrcommon.h"
#include "screen.h"
#include "keyboard.h"
#include "iostate.h"
#ifdef TARGET_DOSWIN
#include "conio.h"
#else
#define USE_ANSI /* Have to use ANSI control sequences, not conio */
#endif
#if defined(TARGET_MINGW)
#include <windows.h>
#include <conio.h>
#endif
#if defined(TARGET_UNIX) | defined(TARGET_DJGPP)
#include <sys/ioctl.h>
#include <sys/types.h>
#include <unistd.h>
#include <termios.h>
#endif
/*
** Notes
** -----
** This is one of the four versions of the VDU driver emulation.
** It is used by versions of the interpreter where only text
** output is possible. All graphics commands are flagged as errors
** by this code.
**
** The four versions of the VDU driver code are in:
** riscos.c
** graphsdl.c
** textonly.c
** simpletext.c
**
** The most important function is 'emulate_vdu'. All text output
** and any VDU commands go via this function. It corresponds to the
** SWI OS_WriteC.
**
** The code supports two different methods for such things as
** positioning the cursor. It can use either ANSI control
** sequences or the functions in the 'conio' library. 'conio'
** is used by the DOS version of the program and ANSI sequences
** by the Linux and NetBSD ones. Conio is better in that it
** supports a greater range of facilities and allows the program
** to more closely emulate what the RISC OS VDU drivers can do.
** On the other hand, some features such as the text window are
** not implemented properly using ANSI control sequences. This
** probably can be improved.
** If 'USE_ANSI' is defined then ANSI control sequences are used;
** otherwise the code uses conio.
**
** The code takes special action if stdout is being redirected.
** In this case the control sequences to carry out such functions
** as changing the text output colour are either ignored or their
** use flagged as an error. This allows output to be sent to a
** file, for example, without the file being polluted with the
** characters sent as escape sequences.
*/
/*
** Note: SCRHEIGHT is really used to flag to indicate that that height
** of the screen is not known. The screen height can be found when
** using the conio library under DOS or when running under NetBSD and
** Linux but this code allows for it to be left unspecified
*/
#define SCRWIDTH 80 /* Assumed width of normal text screen */
#define SCRHEIGHT 0 /* Pretend height of normal text screen */
#ifdef USE_ANSI
/*
** ANSI colour numbers. The ANSI colour table maps RISC OS physical to ANSI
** colour numbers in 2, 4 and 16 colour modes
*/
#define ANSI_BLACK 0
#define ANSI_RED 1
#define ANSI_GREEN 2
#define ANSI_YELLOW 3
#define ANSI_BLUE 4
#define ANSI_MAGENTA 5
#define ANSI_CYAN 6
#define ANSI_WHITE 7
/*
** In the SGR ANSI escape sequence:
** colour number+30 = change foreground
** colour number+40 = change background
*/
#define ANSI_FOREGROUND 30
#define ANSI_BACKGROUND 40
#define ANSI_BOLD 1
#define ANSI_BLINK 5
#define ANSI_REVERSE 7
static byte colourmap [] = {
ANSI_BLACK, ANSI_RED, ANSI_GREEN, ANSI_YELLOW, ANSI_BLUE, ANSI_MAGENTA,
ANSI_CYAN, ANSI_WHITE, ANSI_BLACK, ANSI_RED, ANSI_GREEN, ANSI_YELLOW,
ANSI_BLUE, ANSI_MAGENTA, ANSI_CYAN, ANSI_WHITE
};
#else
/*
** Conio colour numbers. The conio colour table maps the RISC OS physical
** colour numbers to those used by conio in 2, 4 and 16 colour modes
*/
#ifdef TARGET_MINGW
static void gotoxy(int32, int32);
static int wherex(void);
static int wherey(void);
#define FG_TEXT_ATTRIB_SHIFT 0
#define BG_TEXT_ATTRIB_SHIFT 4
#define BLACK 0
#if (FOREGROUND_RED << BG_TEXT_ATTRIB_SHIFT) != (BACKGROUND_RED)
#error "Check assumption about foreground & background"
#endif
static byte colourmap [] = {
BLACK, FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_RED | FOREGROUND_GREEN, FOREGROUND_BLUE,
FOREGROUND_RED | FOREGROUND_BLUE, FOREGROUND_GREEN | FOREGROUND_BLUE,
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
FOREGROUND_INTENSITY, FOREGROUND_RED | FOREGROUND_INTENSITY, FOREGROUND_GREEN | FOREGROUND_INTENSITY,
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY, FOREGROUND_BLUE | FOREGROUND_INTENSITY,
FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY
};
#else
static byte colourmap [] = {
BLACK, RED, GREEN, BROWN, BLUE, MAGENTA, CYAN, LIGHTGRAY,
DARKGRAY, LIGHTRED, LIGHTGREEN, YELLOW, LIGHTBLUE, LIGHTMAGENTA, LIGHTCYAN, WHITE
};
#endif
#endif
/* Tektronix variables */
static int32
xgupp = 2, /* RISC OS graphic units per pixel in X direction */
ygupp = 2, /* RISC OS graphic units per pixel in Y direction */
xlast, /* Graphics X coordinate of last point visited */
ylast, /* Graphics Y coordinate of last point visited */
xlast2, /* Graphics X coordinate of last-but-one point visited */
ylast2, /* Graphics Y coordinate of last-but-one point visited */
xorigin, /* X coordinate of graphics origin */
yorigin, /* Y coordinate of graphics origin */
graphicurs; /* Text at graphics cursor */
#define MAX_XRES 16384
#define MAX_YRES 16384
#define FAST_2_MUL(x) ((x)<<1)
#define FAST_3_MUL(x) (((x)<<1)+x)
#define FAST_4_MUL(x) ((x)<<2)
#define FAST_4_DIV(x) ((x)>>2)
static int32 geom_left[MAX_YRES], geom_right[MAX_YRES];
static unsigned int vduflag(unsigned int flags) {
return (vduflags & flags) ? 1 : 0;
}
static void write_vduflag(unsigned int flags, int yesno) {
vduflags = yesno ? vduflags | flags : vduflags & ~flags;
}
static void tekvdu(int chr) {
putchar(chr);
fflush(stdout);
if (matrixflags.tekspeed > 0) usleep(9000000/matrixflags.tekspeed);
}
#ifdef USE_ANSI
/*
** 'find_cursor' reads the position of the cursor on the text
** screen. It can only do this if input is coming from the
** keyboard and output is going to the screen, that is, stdin
** and stdout have not been redirected
** -- ANSI --
*/
void find_cursor(void) {
int column, row;
column = row = 0;
if (!basicvars.runflags.outredir && !basicvars.runflags.inredir) {
int ch;
// set_esc_key(_POSIX_VDISABLE); /* Disable INTR */
printf("\033[6n"); /* ANSI/VTxxx sequence to find the position of the cursor */
fflush(stdout);
ch = kbd_inkey(50); /* Sequence expected back is ' ESC[<no>;<no>R' */
if (ch!='\033') return;
ch = read_key();
if (ch!='[') return;
ch = read_key();
while (isdigit(ch)) {
row = row*10+ch-'0';
ch = read_key();
}
if (ch!=';') return;
ch = read_key();
while (isdigit(ch)) {
column = column*10+ch-'0';
ch = read_key();
}
if (ch!='R') return;
xtext = column-1; /* The programs wants RISC OS text coordinates, */
ytext = row-1; /* not ANSI ones */
if (xtext<twinleft) /* Ensure that the cursor lies within the text window */
xtext = twinleft;
else if (xtext>twinright)
xtext = twinright;
else if (ytext<twintop)
ytext = twintop;
else if (SCRHEIGHT!=0 && ytext>twinbottom) {
ytext = twinbottom;
}
// set_esc_key(27); /* Put INTR back on ESC key */
}
}
/*
** 'reset_screen' is called to carry out anything needed to reset
** the screen to its default settings
** -- ANSI --
*/
static void reset_screen(void) {
if (vduflag(VDU_FLAG_TEXTWIN)) printf("\033[%d;%dr", 1, textheight); /* Set scrolling region to whole screen */
}
/*
** The following functions duplicate the actions of functions
** in the 'conio' text output library. They are used in versions
** of the code where conio is not available and ANSI control
** sequences have to be used
*/
/*
** 'putch' displays a character.
** -- ANSI --
*/
static void putch(int32 ch) {
putchar(ch);
if (vduflag(VDU_FLAG_ECHO)) fflush(stdout);
}
/*
** 'gotoxy' moves the text cursor to column 'x' row 'y' on the screen.
** The top left-hand corner of the screen is (1, 1). This function is
** called with RISC OS screen coordinates, which start at (0, 0). It
** is up to the calling code to allow for this.
** -- ANSI --
*/
static void gotoxy(int32 x, int32 y) {
printf("\033[%d;%dH", y, x); /* VTxxx/ANSI sequence to move cursor */
fflush(stdout);
}
/*
** 'scroll_text' is called to move the text window up or down a line.
** This version of the code can only scroll the entire screen up or
** down. It does not support the text window
** -- ANSI --
*/
static void scroll_text(updown direction) {
if (vduflag(VDU_FLAG_TEXTWIN)) return;
if (direction==SCROLL_UP) /* Move screen up - Output a linefeed */
printf("\n\033[%d;%dH", ytext+1, xtext+1); /* Move screen up and move cursor to correct place */
else { /* Move screen down */
printf("\033[L");
}
fflush(stdout);
}
/*
** 'textcolor' sets the text foreground colour
** -- ANSI --
*/
static void textcolor(int32 colour) {
printf("\033[");
if (colour & 8) printf("1;"); /* bright */
else printf("11;"); /* non-bright */
printf("%dm", colour+ANSI_FOREGROUND); /* VTxxx/ANSI sequence to set the foreground colour */
}
/*
** 'textbackground' sets the text background colour
** -- ANSI --
*/
static void textbackground(int32 colour) {
printf("\033[%dm", colour+ANSI_BACKGROUND); /* VTxxx/ANSI sequence to set the background colour */
}
/*
** 'clrscr' clears the screen and sends the cursor to the top
** left-hand corner of the screen
** -- ANSI --
*/
static void clrscr(void) {
printf("\033[2J\033[H"); /* VTxxx/ANSI sequence for clearing the screen and to 'home' the cursor */
fflush(stdout);
}
/*
** 'set_cursor' sets the type of the text cursor used on the graphic
** screen to either a block or an underline. There is no ANSI
** equivalent of this so the function is just a no-op
** -- ANSI --
*/
void set_cursor(boolean underline) {
}
/*
** 'echo_on' turns on the immediate echo of characters to the screen
** -- ANSI --
*/
void echo_on(void) {
write_vduflag(VDU_FLAG_ECHO,1);
fflush(stdout);
}
/*
** 'echo_off' turns off the immediate echo of characters to the screen.
** -- ANSI --
*/
void echo_off(void) {
write_vduflag(VDU_FLAG_ECHO,0);
}
#else
/* ========== conio functions ========== */
/*
** 'find_cursor' locates the cursor on the text screen and ensures that
** its position is valid, that is, that it lies within the text window
** -- conio --
*/
void find_cursor(void) {
if (!basicvars.runflags.outredir) {
xtext = wherex()-1;
ytext = wherey()-1;
if (xtext<twinleft)
xtext = twinleft;
else if (xtext>twinright)
xtext = twinright;
else if (ytext<twintop)
ytext = twintop;
else if (ytext>twinbottom) {
ytext = twinbottom;
}
gotoxy(xtext+1, ytext+1);
}
}
/*
** 'set_cursor' sets the type of the text cursor used on the graphic
** screen to either a block or an underline. 'underline' is set to
** TRUE if an underline is required. Underline is used by the program
** when keyboard input is in 'insert' mode and a block when it is in
** 'overwrite'. Transitioning to block display overrides any VDU23 hidden
** state.
** -- conio --
*/
void set_cursor(boolean underline) {
if (!basicvars.runflags.outredir) {
#ifdef TARGET_MINGW
CONSOLE_CURSOR_INFO cursor;
cursmode = underline ? UNDERLINE : BLOCK;
cursor.dwSize = underline ? 1 : 100; /* Percent */
cursor.bVisible = (cursorstate != HIDDEN);
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
#else
cursmode = underline ? UNDERLINE : BLOCK;
if (cursmode==UNDERLINE)
_setcursortype((cursorstate == HIDDEN) ? _NOCURSOR : _NORMALCURSOR);
else {
_setcursortype((cursorstate == HIDDEN) ? _NOCURSOR : _SOLIDCURSOR);
}
#endif
}
}
/*
** 'reset_screen' is called to carry out anything needed to reset
** the screen to its default settings
** -- conio --
*/
static void reset_screen(void) {
}
#ifdef TARGET_MINGW
/*
** 'gotoxy' moves the text cursor to column 'x' row 'y' on the screen.
** This function is called with RISC OS screen coordinates, which start
** at (0, 0).
** -- conio (Win32) --
*/
static void gotoxy(int32 x, int32 y) {
COORD pos;
pos.X = x - 1;
pos.Y = y - 1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
/*
** 'wherex' returns the cursor position with the origin at (1, 1).
** This function is replicated here for 32 bit console apps.
** -- conio (Win32) --
*/
static int wherex(void) {
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
return info.dwCursorPosition.X;
}
/*
** 'wherey' returns the cursor position with the origin at (1, 1).
** This function is replicated here for 32 bit console apps.
** -- conio (Win32) --
*/
static int wherey(void) {
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
return info.dwCursorPosition.Y;
}
/*
** 'clrscr' clears the screen and sends the cursor to the top
** left-hand corner of the screen
** -- conio (Win32) --
*/
void clrscr(void) {
#ifdef CYGWINBUILD
COORD coordScreen = {0,0};
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole;
hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) return;
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
if (!FillConsoleOutputCharacter(hConsole,(TCHAR)' ',dwConSize,coordScreen,&cCharsWritten)) return;
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) return;
if (!FillConsoleOutputAttribute(hConsole,csbi.wAttributes,dwConSize,coordScreen,&cCharsWritten)) return;
SetConsoleCursorPosition(hConsole,coordScreen);
#else
system("cls");
#endif
}
/*
** 'textcolor' and
** 'textbackground' set the foreground and background colours, though as the console text
** attributes are a bitmask of both these functions just mix the parameter with the
** global for the opposite of their own name
** -- conio (Win32) --
*/
void textcolor(int32 colour) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (colour << FG_TEXT_ATTRIB_SHIFT) |
(text_physbackcol << BG_TEXT_ATTRIB_SHIFT));
}
void textbackground(int32 colour) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (text_physforecol << FG_TEXT_ATTRIB_SHIFT) |
(colour << BG_TEXT_ATTRIB_SHIFT));
}
#endif
/*
** 'scroll_text' is called to move the text window up or down a line.
** Note that the coordinates here are in RISC OS text coordinates which
** start at (0, 0) whereas conio's start with (1, 1) at the top left-hand
** corner of the screen.
** -- conio --
*/
static void scroll_text(updown direction) {
#ifndef TARGET_MINGW
int n;
#endif
if (!vduflag(VDU_FLAG_TEXTWIN) && direction==SCROLL_UP) /* Text window is the whole screen and scrolling is upwards */
putch('\n'); /* Output a linefeed */
else { /* Writing to a text window */
#ifdef TARGET_MINGW
SMALL_RECT scroll;
SMALL_RECT clip;
COORD dest;
CHAR_INFO clear;
scroll.Left = clip.Left = twinleft;
scroll.Right = clip.Right = twinright;
scroll.Top = clip.Top = twintop;
scroll.Bottom = clip.Bottom = twinbottom;
dest.X = twinleft;
dest.Y = twintop - 1;
clear.Char.AsciiChar = ' '; /* No foreground colour needed as it's cleared with space */
clear.Attributes = text_physbackcol << BG_TEXT_ATTRIB_SHIFT;
ScrollConsoleScreenBuffer(GetStdHandle(STD_OUTPUT_HANDLE), &scroll, &clip, dest, &clear);
#else
if (twintop!=twinbottom) { /* Text window is more than one line high */
if (direction==SCROLL_UP) /* Scroll text up a line */
(void) movetext(twinleft+1, twintop+2, twinright+1, twinbottom+1, twinleft+1, twintop+1);
else { /* Scroll text down a line */
(void) movetext(twinleft+1, twintop+1, twinright+1, twinbottom, twinleft+1, twintop+2);
}
}
gotoxy(twinleft+1, ytext+1);
echo_off();
for (n=twinleft; n<=twinright; n++) putch(' '); /* Clear the vacated line of the window */
echo_on();
#endif
}
gotoxy(xtext+1, ytext+1); /* Put the cursor back where it should be */
}
/*
** 'echo_on' turns on the immediate echo of characters to the screen
** -- conio --
*/
void echo_on(void) {
}
/*
** 'echo_off' turns off the immediate echo of characters to the screen.
** -- conio --
*/
void echo_off(void) {
}
#endif
/*
** 'vdu_2317' deals with various flavours of the sequence VDU 23,17,...
*/
static void vdu_2317(void) {
int32 temp;
switch (vduqueue[1]) { /* vduqueue[1] is the byte after the '17' and says what to change */
case TINT_FORETEXT: /* Foreground text colour tint */
text_foretint = (vduqueue[2] & TINTMASK)>>TINTSHIFT; /* Third byte in queue is new TINT value */
if (colourdepth==256) text_physforecol = (text_forecol<<COL256SHIFT)+text_foretint;
break;
case TINT_BACKTEXT: /* Background text colour tint */
text_backtint = (vduqueue[2] & TINTMASK)>>TINTSHIFT;
if (colourdepth==256) text_physbackcol = (text_backcol<<COL256SHIFT)+text_backtint;
break;
case TINT_FOREGRAPH: /* Foreground and background graphics colour tints - Just ignore these two */
case TINT_BACKGRAPH:
break;
case EXCH_TEXTCOLS: /* Exchange text foreground and background colours */
temp = text_forecol; text_forecol = text_backcol; text_backcol = temp;
temp = text_physforecol; text_physforecol = text_physbackcol; text_physbackcol = temp;
temp = text_foretint; text_foretint = text_backtint; text_backtint = temp;
break;
default: /* Ignore bad value */
break;
}
}
/*
** 'vdu_23command' emulates some of the VDU 23 command sequences
*/
static void vdu_23command(void) {
switch (vduqueue[0]) { /* First byte in VDU queue gives the command type */
case 1: /* Control the appear of the text cursor */
if (vduqueue[1]==0) {
cursorstate = HIDDEN; /* 0 = hide, 1 = show */
set_cursor(cursmode);
}
if (vduqueue[1]==1 && cursorstate!=NOCURSOR) {
cursorstate = ONSCREEN;
set_cursor(cursmode); /* Unhiding always starts as an underscore */
}
break;
case 8: /* Clear part of the text window */
break;
case 17: /* Set the tint value for a colour in 256 colour modes, etc */
vdu_2317();
}
}
/*
** 'move_cursor' sends the text cursor to the position (column, row)
** on the screen. It updates the cursor position as well.
** The column and row are given in RISC OS text coordinates, that
** is, (0,0) is the top left-hand corner of the screen. These values
** are the true coordinates on the screen. The code that uses this
** function has to allow for the text window.
*/
static void move_cursor(int32 column, int32 row) {
xtext = column;
ytext = row;
gotoxy(column+1, row+1);
}
/*
** 'map_colour' for non-graphics (text only) operation. The number
** of colours available is limited to at most sixteen. 256 colour
** modes are dealt with by taking the six bit colour number and
** interpreting it as a bit pattern of the form 'bbggrr' where
** 'bb', 'gg' and 'rr' are two bit colour component values. It
** uses the most significant bit of each component to form a
** RISC OS physical colour number. It is a hack but it does the
** job
*/
static int32 map_colour(int32 colour) {
int32 temp=0;
if (colourdepth<=16) {
temp=colourmap[logtophys[colour]];
if (temp) temp=temp | (((colourdepth<16)&1)*8);
return temp;
} else { /* Map a 256-colour colour number to a sixteen-bit one */
if (colour & C256_REDBIT) temp+=VDU_RED;
if (colour & C256_GREENBIT) temp+=VDU_GREEN;
if (colour & C256_BLUEBIT) temp+=VDU_BLUE;
return colourmap[temp];
}
}
/*
** 'vdu_setpalette' changes one of the logical to physical colour map
** entries (VDU 19).
** Note that this function should have the side effect of changing all
** pixels of logical colour number 'logcol' to the physical colour
** given by 'mode' but the code does not do this.
*/
static void vdu_setpalette(void) {
int32 logcol, mode;
logcol = vduqueue[0] & colourmask;
mode = vduqueue[1];
if (mode>15) {
if (basicvars.runflags.flag_cosmetic) error(ERR_UNSUPPORTED);
return; /* Silently ignore command as it is 'cosmetic' */
}
if (colourdepth<=16) logtophys[logcol] = mode;
}
#ifdef USE_ANSI
/* ========== ANSI ========== */
/*
** 'printer_char' sends the character in the VDU command queue to the printer
** stream if connected, or to the output stream. This is used to provide an
** escape mechanism to allow any character to be sent rather than having
** characters with codes less than 32 treated as VDU commands. VDU 1 is
** hijacked to do this.
** -- ANSI --
*/
static void printer_char(void) {
if (matrixflags.printer) {
fputc(vduqueue[0], matrixflags.printer);
} else {
putchar(vduqueue[0]);
if (vduflag(VDU_FLAG_ECHO)) fflush(stdout);
}
}
/*
** 'move_curback' moves the cursor back one character on the screen (VDU 8).
** -- ANSI --
*/
static void move_curback(void) {
xtext--;
if (xtext>=twinleft) /* Cursor is still within the text window */
printf("\033[D"); /* ANSI sequence to move the cursor back one char */
else { /* Cursor is at left-hand edge of text window so move up a line */
xtext = twinright;
ytext--;
if (ytext>=twintop) /* Cursor is still within the confines of the text window */
printf("\033[A\033[%dG", xtext+1); /* Move cursor up and to last column */
else { /* Cursor is now above the top of the window */
ytext++; /* Scroll window down a line */
scroll_text(SCROLL_DOWN);
printf("\033[%dG", xtext+1); /* Move cursor to last column */
}
}
fflush(stdout);
}
/*
** 'move_curforward' moves the cursor forwards one character on the
** screen (VDU 9)
** -- ANSI --
*/
static void move_curforward(void) {
xtext++;
if (xtext<=twinright) /* Cursor is still within the text window */
printf("\033[C"); /* ANSI sequence to move the cursor forwards one char */
else { /* Cursor is at right-hand edge of text window so move down a line */
xtext = twinleft;
ytext++;
printf("\n\033[%dG", xtext+1); /* Move cursor down and to first column */
}
fflush(stdout);
}
/*
** 'move_curdown' moves the cursor down the screen, that is, it
** performs the linefeed operation (VDU 10)
** -- ANSI --
*/
static void move_curdown(void) {
ytext++;
printf("\n\033[%dG", xtext+1);
fflush(stdout);
}
/*
** 'move_curup' moves the cursor up a line on the screen (VDU 11)
** -- ANSI --
*/
static void move_curup(void) {
ytext--;
if (ytext>=twintop) /* Cursor is still within the window */
printf("\033[A"); /* ANSI sequence to move the cursor up one line */
else { /* Cursor is above the top of the window */
ytext++; /* Scroll window down a line */
scroll_text(SCROLL_DOWN);
}
fflush(stdout);
}
/*
** 'vdu_cleartext' clears the text window. Normally this is the
** entire screen (VDU 12)
** -- ANSI --
*/
static void vdu_cleartext(void) {
if (vduflag(VDU_FLAG_TEXTWIN)) { /* Text window defined that does not occupy the whole screen */
int32 row;
for (row = twintop; row<=twinbottom; row++) {
printf("\033[%d;%dH\033[%dX", row+1, twinleft+1, twinright-twinleft+1); /* Clear the line */
}
fflush(stdout);
move_cursor(twinleft, twintop); /* Send cursor to home position in window */
}
else { /* No text window has been defined */
clrscr();
xtext = twinleft;
ytext = twintop;
}
}
/*
** 'vdu_return' deals with the carriage return character (VDU 13).
** -- ANSI --
*/
static void vdu_return(void) {
printf("\033[%dG", twinleft+1);
fflush(stdout);
xtext = twinleft;
}
/*
** 'vdu_textwind' defines a text window (VDU 28)
** -- ANSI --
*/
static void vdu_textwind(void) {
int32 left, right, top, bottom;
left = vduqueue[0];
bottom = vduqueue[1];
right = vduqueue[2];
top = vduqueue[3];
if (left>right) { /* Ensure right column number > left */
int32 temp = left;
left = right;
right = temp;
}
if (bottom<top) { /* Ensure bottom line number > top */
int32 temp = bottom;
bottom = top;
top = temp;
}
if (left>=textwidth || (SCRHEIGHT!=0 && top>=textheight)) return; /* Ignore bad parameters */
twinleft = left;
twinright = right;
twintop = top;
twinbottom = bottom;
/* Set flag to say if text window occupies only a part of the screen */
write_vduflag(VDU_FLAG_TEXTWIN,(left>0 || right<textwidth-1 || top>0 || bottom<textheight-1));
/*
** If the text window is the full width of the screen then it is
** possible to set a 'scrolling region' for that part of the screen
** using an ANSI control sequence so that the contents of the window
** can be scrolled up or down
*/
if (vduflag(VDU_FLAG_TEXTWIN) && left==0 && right==textwidth-1) printf("\033[%d;%dr", twintop+1, twinbottom+1);
move_cursor(twinleft, twintop); /* Move text cursor to home position in new window */
}
#else
/* ========== conio ========== */
static void printer_char(void) {
if (matrixflags.printer) fputc(vduqueue[0], matrixflags.printer);
}
/*
** 'move_curback' moves the cursor back one character on the screen (VDU 8).
** -- conio --
*/
static void move_curback(void) {
xtext--;
if (xtext>=twinleft) /* Cursor is still within the text window */
putch('\b');
else { /* Cursor is at left-hand edge of text window so move up a line */
xtext = twinright;
ytext--;
if (ytext>=twintop) /* Cursor is still within the confines of the text window */
gotoxy(xtext+1, ytext+1);
else { /* Cursor is now above the top of the window */
ytext++;
scroll_text(SCROLL_DOWN); /* Scroll the window down a line */
}
}
}
/*
** 'move_curforward' moves the cursor forwards one character on the
** screen (VDU 9)
** -- conio --
*/
static void move_curforward(void) {
xtext++;
if (xtext<=twinright) /* Cursor is still within the text window */
gotoxy(xtext+1, ytext+1);
else { /* Cursor is at right-hand edge of text window so move down a line */
xtext = twinleft;
ytext++;
if (ytext<=twinbottom) /* Text cursor is still within the confines of the window */
gotoxy(xtext+1, ytext+1);
else { /* Cursor has moved below bottom of window, so scroll screen up a line */
ytext--;
scroll_text(SCROLL_UP);
}
}
}
/*
** 'move_curdown' moves the cursor down the screen, that is, it
** performs the linefeed operation (VDU 10)
** -- conio --
*/
static void move_curdown(void) {
ytext++;
if (ytext<=twinbottom)
gotoxy(xtext+1, ytext+1);
else { /* At bottom of window. Move window up a line */
ytext--;
scroll_text(SCROLL_UP);
}
}
/*
** 'move_curup' moves the cursor up a line on the screen (VDU 11)
** -- conio --
*/
static void move_curup(void) {
ytext--;
if (ytext>=twintop) /* Cursor is still within the window */
gotoxy(xtext+1, ytext+1);
else { /* Cursor is above the top of the window */
ytext++;
scroll_text(SCROLL_DOWN); /* Scroll the window down a line */
}
}
/*
** 'vdu_cleartext' clears the text window. Normally this is the
** entire screen (VDU 12)
** -- conio --
*/
static void vdu_cleartext(void) {
if (vduflag(VDU_FLAG_TEXTWIN)) { /* Text window defined that does not occupy the whole screen */
int32 column, row;
for (row = twintop; row<=twinbottom; row++) {
gotoxy(twinleft+1, row+1); /* Go to start of line on screen */
for (column = twinleft; column<=twinright; column++) putch(' ');
}
move_cursor(twinleft, twintop); /* Send cursor to home position in window */
}
else { /* No text window has been defined */
clrscr();
xtext = twinleft;
ytext = twintop;
}
}
/*
** 'vdu_return' deals with the carriage return character (VDU 13).
** -- conio --
*/
static void vdu_return(void) {
move_cursor(twinleft, ytext);
basicvars.xtab = 0;
}
/*
** 'vdu_textwind' defines a text window (VDU 28)
** -- conio --
*/
static void vdu_textwind(void) {
int32 left, right, top, bottom;
left = vduqueue[0];
bottom = vduqueue[1];
right = vduqueue[2];
top = vduqueue[3];
if (left>right) { /* Ensure right column number > left */
int32 temp = left;
left = right;
right = temp;
}
if (bottom<top) { /* Ensure bottom line number > top */
int32 temp = bottom;
bottom = top;
top = temp;
}
if (left>=textwidth || top>=textheight) return; /* Ignore bad parameters */
twinleft = left;
twinright = right;
twintop = top;
twinbottom = bottom;
/* Set flag to say if text window occupies only a part of the screen */
write_vduflag(VDU_FLAG_TEXTWIN,(left>0 || right<textwidth-1 || top>0 || bottom<textheight-1));
move_cursor(twinleft, twintop); /* Move text cursor to home position in new window */
}
#endif
/*
** 'vdu_textcol' changes the text colour to the value in the VDU queue
** (VDU 17). It handles both foreground and background colours at any
** colour depth. The RISC OS physical colour number is mapped to either
** a conio or an ANSI equivalent. In the case of the ANSI colour numbers,
** the 'bold' attribute is also sent in the escape sequence otherwise the
** colours are too dark
*/
static void vdu_textcol(void) {
int32 colnumber;
colnumber = vduqueue[0];
if (colnumber<128) { /* Setting foreground colour */
text_forecol = colnumber & colourmask;
text_physforecol = map_colour(text_forecol);
textcolor(text_physforecol);
}
else { /* Setting background colour */
text_backcol = (colnumber-128) & colourmask;
text_physbackcol = map_colour(text_backcol);
textbackground(text_physbackcol);
}
}