Debian Codegraph @

src/main-ibm.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* File: main-ibm.c */
2
3 /*
4 * Copyright (c) 1997 Ben Harrison, and others
5 *
•6 * This work is free software; you can redistribute it and/or modify it
7 * under the terms of either:
8 *
9 * a) the GNU General Public License as published by the Free Software
10 * Foundation, version 2, or
11 *
12 * b) the "Angband licence":
13 * This software may be copied and distributed for educational, research,
14 * and not for profit purposes provided that this copyright and statement
15 * are included in all such copies. Other copyrights may also apply.
16 */
17
18 /* Purpose: Visual Display Support for "z-term.c", for the IBM */
19
20
21 /*
22 * Original code by "Billy Tanksley ([email protected])"
23 * Use "Makefile.ibm" to compile Angband using this file.
24 *
25 * Support for DJGPP v2 by "Scott Egashira ([email protected])"
26 *
27 * Extensive modifications by "Ben Harrison ([email protected])",
28 * including "collation" of the Watcom C/C++ and DOS-286 patches.
29 *
30 * Watcom C/C++ changes by "David Boeren ([email protected])"
31 * Use "Makefile.wat" to compile this file with Watcom C/C++, and
32 * be sure to define "USE_IBM" and "USE_WAT".
33 *
34 * DOS-286 (conio.h) changes by (Roland Jay Roberts ([email protected])
35 * Use "Makefile.286" (not ready) to compile this file for DOS-286,
36 * and be sure to define "USE_IBM", "USE_WAT", and "USE_286". Also,
37 * depending on your compiler, you may need to define "USE_CONIO".
38 *
39 * True color palette support by "Mike Marcelais ([email protected])",
40 * with interface to the "color_table" array by Ben Harrison.
41 *
42 * Both "shift" keys are treated as "identical", and all the modifier keys
43 * (control, shift, alt) are ignored when used with "normal" keys, unless
44 * they modify the underlying "ascii" value of the key. You must use the
45 * new "user pref files" to be able to interact with the keypad and such.
46 *
47 * The "lib/user/pref-ibm.prf" file contains keymap definitions and possible
48 * alternative color set definitions. The "lib/user/font-ibm.prf" contains
49 * attr/char mappings for walls and floors and such.
50 *
51 * Note the "Term_user_ibm()" function hook, which could allow the user
52 * to interact with the "main-ibm.c" visual system. Currently this hook
53 * is unused, but, for example, it could allow the user to toggle "sound"
54 * or "graphics" modes, or to select the number of screen rows, with the
55 * extra screen rows being used for the mirror window.
56 */
57
58
59 #include "angband.h"
60
61
62 #ifdef USE_IBM
63
64 #include "main.h"
65
66 /*
67 * Use a "virtual" screen to "buffer" screen writes.
68 */
69 #define USE_VIRTUAL
70
71
72 #include <bios.h>
73 #include <dos.h>
74
75 #ifdef USE_WAT
76
77 # include <conio.h>
78
79 # ifdef USE_CONIO
80 # else /* USE_CONIO */
81
82 # include <graph.h>
83
84 # define bioskey(C) _bios_keybrd(C)
85
86 # endif /* USE_CONIO */
87
88 # ifndef USE_286
89 # define int86(a,b,c) int386(a,b,c)
90 # endif
91
92 # define inportb(x) inp(x)
93 # define outportb(x,y) outp(x,y)
94
95 #else /* USE_WAT */
96
97 # if __DJGPP__ > 1
98
99 # include <pc.h>
100 # include <osfcn.h>
101 # include <libc/unconst.h>
102 # include <wchar.h>
103 # include <wctype.h>
104
105 # else /* __DJGPP__ > 1 */
106 # ifdef __DJGPP__
107 # error "Upgrade to version 2.0 of DJGPP"
108 # endif /* __DJGPP__ */
109 # endif /* __DJGPP__ > 1 */
110
111 #endif /* USE_WAT */
112
113
114 #ifdef USE_CONIO
115
116 # include <conio.h>
117
118 /*
119 * Hack -- write directly to video card
120 */
121 extern int directvideo = 1;
122
123 /*
124 * Hack -- no virtual screen
125 */
126 # undef USE_VIRTUAL
127
128 #endif /* USE_CONIO */
129
130
131 /*
132 * Keypress input modifier flags (hard-coded by DOS)
133 */
134 #define K_RSHIFT 0 /* Right shift key down */
135 #define K_LSHIFT 1 /* Left shift key down */
136 #define K_CTRL 2 /* Ctrl key down */
137 #define K_ALT 3 /* Alt key down */
138 #define K_SCROLL 4 /* Scroll lock on */
139 #define K_NUM 5 /* Num lock on */
140 #define K_CAPS 6 /* Caps lock on */
141 #define K_INSERT 7 /* Insert on */
142
143
144 /*
145 * Foreground color bits (hard-coded by DOS)
146 */
147 #define VID_BLACK 0x00
148 #define VID_BLUE 0x01
149 #define VID_GREEN 0x02
150 #define VID_CYAN 0x03
151 #define VID_RED 0x04
152 #define VID_MAGENTA 0x05
153 #define VID_YELLOW 0x06
154 #define VID_WHITE 0x07
155
156 /*
157 * Bright text (hard-coded by DOS)
158 */
159 #define VID_BRIGHT 0x08
160
161 /*
162 * Background color bits (hard-coded by DOS)
163 */
164 #define VUD_BLACK 0x00
165 #define VUD_BLUE 0x10
166 #define VUD_GREEN 0x20
167 #define VUD_CYAN 0x30
168 #define VUD_RED 0x40
169 #define VUD_MAGENTA 0x50
170 #define VUD_YELLOW 0x60
171 #define VUD_WHITE 0x70
172
173 /*
174 * Blinking text (hard-coded by DOS)
175 */
176 #define VUD_BRIGHT 0x80
177
178
179 /*
180 * Screen Size
181 */
182 static int rows = 25;
183 static int cols = 80;
184
185
186 /*
187 * Physical Screen
188 */
189 #ifdef USE_286
190 # define PhysicalScreen ((uint8_t *)MK_FP(0xB800,0x0000))
191 #else
192 # define PhysicalScreen ((uint8_t *)(0xB800 << 4))
193 #endif
194
195
196 #ifdef USE_VIRTUAL
197
198 /*
199 * Virtual Screen Contents
200 */
201 static uint8_t *VirtualScreen;
202
203 #else
204
205 /*
206 * Physical screen access
207 */
208 #define VirtualScreen PhysicalScreen
209
210 #endif
211
212
213 /*
214 * Hack -- the cursor "visibility"
215 */
216 static int saved_cur_v;
217 static int saved_cur_high;
218 static int saved_cur_low;
219
220
221 #ifdef USE_CONIO
222 #else /* USE_CONIO */
223
224 /*
225 * This array is used for "wiping" the screen
226 */
227 static uint8_t wiper[160];
228
229 #endif /* USE_CONIO */
230
231
232 /*
233 * The main screen (currently the only screen)
234 */
235 static term term_screen_body;
236
237
238 /*
239 * Choose between the "complex" and "simple" color methods
240 */
241 static uint8_t use_color_complex = false;
242
243
244 /*
245 * The "complex" color set
246 */
247 static long ibm_color_complex[16];
248
249
250 /*
251 * The "simple" color set
252 *
253 * This table is used by the "color" code to instantiate the "approximate"
254 * Angband colors using the only colors available on crappy monitors.
255 *
256 * The entries below are taken from the "color bits" defined above.
257 *
258 * Note that values from 16 to 255 are extremely ugly.
259 *
260 * The values below came from various sources, if you do not like them,
261 * get a better monitor, or edit "pref-ibm.prf" to use different codes.
262 *
263 * Note that many of the choices below suck, but so do crappy monitors.
264 */
265 static uint8_t ibm_color_simple[16] =
266 {
267 VID_BLACK, /* Dark */
268 VID_WHITE, /* White */
269 VID_CYAN, /* Slate XXX */
270 VID_RED | VID_BRIGHT, /* Orange XXX */
271 VID_RED, /* Red */
272 VID_GREEN, /* Green */
273 VID_BLUE, /* Blue */
274 VID_YELLOW, /* Umber XXX */
275 VID_BLACK | VID_BRIGHT, /* Light Dark */
276 VID_CYAN | VID_BRIGHT, /* Light Slate XXX */
277 VID_MAGENTA, /* Violet */
278 VID_YELLOW | VID_BRIGHT, /* Yellow */
279 VID_MAGENTA | VID_BRIGHT, /* Light Red XXX */
280 VID_GREEN | VID_BRIGHT, /* Light Green */
281 VID_BLUE | VID_BRIGHT, /* Light Blue */
282 VID_YELLOW /* Light Umber XXX */
283 };
284
285
286 static uint8_t ext_color_map[32] = {
287 0,
288 1,
289 2,
290 3,
291 4,
292 5,
293 6,
294 7,
295 8,
296 9,
297 10,
298 11,
299 12,
300 13,
301 14,
302 15,
303 10, /* Purple -> Violet */
304 10, /* Violet -> Violet */
305 14, /* Teal -> Light Blue */
306 7, /* Mud -> Umber */
307 15, /* Light Yellow -> Light Umber */
308 10, /* Magenta -> Violet */
309 14, /* Light Teal -> Light Blue */
310 10, /* Light Violet -> Violet */
311 12, /* Light Pink -> Light Red */
312 7, /* Mustard -> Yellow */
313 6, /* Blue Slate -> Blue */
314 14, /* Deep Light Blue -> Light Blue */
315 8 /* Shade -> Light Dark */
316 };
317
318 /*
319 * Activate the "ibm_color_complex" palette information.
320 *
321 * Code by Mike Marcelais, with help from "The programmer's guide
322 * to the EGA and VGA video cards" [Farraro].
323 *
324 * On VGA cards, colors go through a double-indirection when looking
325 * up the `real' color when in 16 color mode. The color value in the
326 * attribute is looked up in the EGA color registers. Then that value
327 * is looked up in the VGA color registers. Then the color is displayed.
328 * This is done for compatability. However, the EGA registers are
329 * initialized by default to 0..5, 14, 7, 38..3F and not 0..F which means
330 * that unless these are reset, the VGA setpalette function will not
331 * update the correct palette register!
332 *
333 * DJGPP's GrSetColor() does _not_ set the EGA palette list, only the
334 * VGA color list.
335 *
336 * Note that the "traditional" method, using "int86(0x10)", is very slow
337 * when called in protected mode, so we use a faster method using video
338 * ports instead.
339 *
340 * On Watcom machines, we could simply use the special "_remapallpalette()"
341 * function, which not only sets both palette lists (see below) but also
342 * checks for legality of the monitor mode, but, if we are doing bitmapped
343 * graphics, that function forgets to set the EGA registers for some reason.
344 */
345 static void activate_color_complex(void)
346 {
347 int i;
348 printf("%c%c%c%c",8,8,8,8);
349
350 #if 1
351
352 /* Edit the EGA palette */
353 inportb(0x3da);
354
355 /* Edit the colors */
356 for (i = 0; i < 16; i++)
357 {
358 /* Set color "i" */
359 outportb(0x3c0, i);
360
361 /* To value "i" */
362 outportb(0x3c0, i);
363 };
364
365 /* Use that EGA palette */
366 outportb(0x3c0, 0x20);
367
368 /* Edit VGA palette, starting at color zero */
369 outportb(0x3c8, 0);
370
371 /* Send the colors */
372 for (i = 0; i < 16; i++)
373 {
374 /* Send the red, green, blue components */
375 outportb(0x3c9, ((ibm_color_complex[i]) & 0xFF));
376 outportb(0x3c9, ((ibm_color_complex[i] >> 8) & 0xFF));
377 outportb(0x3c9, ((ibm_color_complex[i] >> 16) & 0xFF));
378 }
379
380 #else /* 1 */
381
382 /* Set the colors */
383 for (i = 0; i < 16; i++)
384 {
385 union REGS r;
386
387 /* Set EGA color */
388 r.h.ah = 0x10;
389 r.h.al = 0x00;
390
391 /* Set color "i" */
392 r.h.bl = i;
393
394 /* To value "i" */
395 r.h.bh = i;
396
397 /* Do it */
398 int86(0x10, &r, &r);
399
400 /* Set VGA color */
401 r.h.ah = 0x10;
402 r.h.al = 0x10;
403
404 /* Set color "i" */
405 r.h.bh = 0x00;
406 r.h.bl = i;
407
408 /* Use this "green" value */
409 r.h.ch = ((ibm_color_complex[i] >> 8) & 0xFF);
410
411 /* Use this "blue" value */
412 r.h.cl = ((ibm_color_complex[i] >> 16) & 0xFF);
413
414 /* Use this "red" value */
415 r.h.dh = ((ibm_color_complex[i]) & 0xFF);
416
417 /* Do it */
418 int86(0x10, &r, &r);
419 }
420
421 #endif /* 1 */
422
423 }
424
425 static int get_ext_color(int a)
426 {
427 int retval;
428 if (a > -1 && a < 29) {
429 retval = ext_color_map[a];
430 } else {
431 retval = a & 0x0F;
432 }
433 return retval;
434 }
435
436 /*
437 * Note the use of "(x >> 2)" to convert an 8 bit value to a 6 bit value
438 * without losing much precision.
439 */
440 static int Term_xtra_ibm_react(void)
441 {
442 int i;
443
444 /* Complex method */
445 if (use_color_complex)
446 {
447 long rv, gv, bv, code;
448
449 bool change = false;
450
451 /* Save the default colors */
452 for (i = 0; i < 16; i++)
453 {
454 /* Extract desired values */
455 rv = angband_color_table[i][1] >> 2;
456 gv = angband_color_table[i][2] >> 2;
457 bv = angband_color_table[i][3] >> 2;
458
459 /* Extract a full color code */
460 code = ((rv) | (gv << 8) | (bv << 16));
461
462 /* Activate changes */
463 if (ibm_color_complex[i] != code)
464 {
465 /* Note the change */
466 change = true;
467
468 /* Apply the desired color */
469 ibm_color_complex[i] = code;
470 }
471 }
472
473 /* Activate the palette if needed */
474 if (change) activate_color_complex();
475 }
476
477 /* Simple method */
478 else
479 {
480 /* Save the default colors */
481 for (i = 0; i < 16; i++)
482 {
483 /* Simply accept the desired colors */
484 ibm_color_simple[i] = angband_color_table[i][0];
485 }
486 }
487
488 /* Success */
489 return (0);
490 }
491
492
493
494 /*
495 * Hack -- set the cursor "visibility"
496 */
497 static void curs_set(int v)
498 {
499 /* If needed */
500 if (saved_cur_v != v)
501 {
502 union REGS r;
503
504 /* Set cursor */
505 r.h.ah = 1;
506
507 /* Visible */
508 if (v)
509 {
510 /* Use the saved values */
511 r.h.ch = saved_cur_high;
512 r.h.cl = saved_cur_low;
513 }
514
515 /* Invisible */
516 else
517 {
518 /* Make it invisible */
519 r.h.ch = 0x20;
520 r.h.cl = 0x00;
521 }
522
523 /* Make the call */
524 int86(0x10, &r, &r);
525
526 /* Save the cursor state */
527 saved_cur_v = v;
528 }
529 }
530
531
532
533 /*
534 * Process an event (check for a keypress)
535 *
536 * The keypress processing code is often the most system dependant part
537 * of Angband, since sometimes even the choice of compiler is important.
538 *
539 * For the IBM, we divide all keypresses into two catagories, first, the
540 * "normal" keys, including all keys required to play Angband, and second,
541 * the "special" keys, such as keypad keys, function keys, and various keys
542 * used in combination with various modifier keys.
543 *
544 * To simplify this file, we use Angband's "keymap" ability, in
545 * combination with a specialized "pref-ibm.prf" file, to handle most of the
546 * "special" keys, instead of attempting to fully analyze them here. This
547 * file only has to determine when a "special" key has been pressed, and
548 * translate it into a simple string which signals the use of a "special"
549 * key, the set of modifiers used, if any, and the hardware scan code of
550 * the actual key which was pressed. To simplify life for the user, we
551 * treat both "shift" keys as identical modifiers.
552 *
553 * The final encoding is "^_MMMxSS\r", where "MMM" encodes the modifiers
554 * ("C" for control, "S" for shift, "A" for alt, or any ordered combination),
555 * and "SS" encodes the keypress (as the two "digit" hexidecimal encoding of
556 * the scan code of the key that was pressed), and the "^_" and "x" and "\r"
557 * delimit the encoding for recognition by the keymap code.
558 *
559 * Some important facts about scan codes follow. All "normal" keys use
560 * scan codes from 1-58. The "function" keys use 59-68 (and 133-134).
561 * The "keypad" keys use 69-83. Escape uses 1. Enter uses 28. Control
562 * uses 29. Left Shift uses 42. Right Shift uses 54. PrtScrn uses 55.
563 * Alt uses 56. Space uses 57. CapsLock uses 58. NumLock uses 69.
564 * ScrollLock uses 70. The "keypad" keys which use scan codes 71-83
565 * are ordered KP7,KP8,KP9,KP-,KP4,KP5,KP6,KP+,KP1,KP2,KP3,INS,DEL.
566 *
567 * Using "bioskey(0x10)" instead of "bioskey(0)" apparently provides more
568 * information, including better access to the keypad keys in combination
569 * with various modifiers, but only works on "PC's after 6/1/86", and there
570 * is no way to determine if the function is provided on a machine. I have
571 * been told that without it you cannot detect, for example, control-left.
572 * The basic scan code + ascii value pairs returned by the keypad follow,
573 * with values in parentheses only available to "bioskey(0x10)".
574 *
575 * / * - + 1 2 3 4
576 * Norm: 352f 372a 4a2d 4e2b 4f00 5000 5100 4b00
577 * Shft: 352f 372a 4a2d 4e2b 4f31 5032 5133 4b34
578 * Ctrl: (9500) (9600) (8e00) (9000) 7500 (9100) 7600 7300
579 *
580 * 5 6 7 8 9 0 . Enter
581 * Norm: (4c00) 4d00 4700 4800 4900 5200 5300 (e00d)
582 * Shft: 4c35 4d36 4737 4838 4939 5230 532e (e00d)
583 * Ctrl: (8f00) 7400 7700 (8d00) 8400 (9200) (9300) (e00a)
584 *
585 * See "pref-ibm.prf" for the "standard" keymaps for various keys.
586 *
587 * Certain "bizarre" keypad keys (such as "enter") return a "scan code"
588 * of "0xE0", and a "usable" ascii value. These keys should be treated
589 * like the normal keys, see below. XXX XXX XXX Note that these "special"
590 * keys could be prefixed with an optional "ctrl-^" which would allow them
591 * to be used in keymaps without hurting their use in normal situations.
592 */
593 static errr Term_xtra_ibm_event(int v)
594 {
595 int i, k, s;
596 int ch = 0;
597
598 bool mc = false;
599 bool ms = false;
600 bool ma = false;
601 bool kp = false;
602
603 uint8_t mods = 0;
604
605 /* Hack -- Check for a keypress */
606 if (!v && !bioskey(1)) return (1);
607
608 /* Wait for a keypress */
609 k = bioskey(0x10);
610
611 /* Access the "modifiers" */
612 i = bioskey(2);
613
614 /* Extract the "scan code" */
615 s = ((k >> 8) & 0xFF);
616
617 /* Extract the "ascii value" */
618 k = (k & 0xFF);
619
620 /* Process "normal" keys */
621 if ((s <= 58) || (s == 0xE0))
622 {
623 /* Enqueue it */
624 if (k) Term_keypress(k, 0);
625
626 /* Success */
627 return (0);
628 }
629
630 /* Extract the modifier flags */
631 if (i & (1 << K_CTRL)) mc = true;
632 if (i & (1 << K_LSHIFT)) ms = true;
633 if (i & (1 << K_RSHIFT)) ms = true;
634 if (i & (1 << K_ALT)) ma = true;
635
636 switch (s) {
637 case 0x56:
638 /* Hack -- Some foreign keyboards have a special key on the
639 * keyboard, which is used to generate the "<", ">", and
640 * "|" keys (alone, shifted, alt-ed).
641 */
642 if (ms) {
643 ch = '>';
644 } else if (ma) {
645 ch = '|';
646 } else {
647 ch = '<';
648 }
649 break;
650 case 0x35: kp = true; ch = '/'; break;
651 case 0x37: kp = true; ch = '*'; break;
652 case 0x47: kp = true; ch = '7'; break;
653 case 0x48: kp = true; ch = '8'; break;
654 case 0x49: kp = true; ch = '9'; break;
655 case 0x4A: kp = true; ch = '-'; break;
656 case 0x4B: kp = true; ch = '4'; break;
657 case 0x4C: kp = true; ch = '5'; break;
658 case 0x4D: kp = true; ch = '6'; break;
659 case 0x4E: kp = true; ch = '+'; break;
660 case 0x4F: kp = true; ch = '1'; break;
661 case 0x50: kp = true; ch = '2'; break;
662 case 0x51: kp = true; ch = '3'; break;
663 case 0x52: kp = true; ch = '0'; break;
664 case 0x53: kp = true; ch = '.'; break;
665 case 0x95:
666 if (mc) {
667 kp = true;
668 ch = '/';
669 }
670 break;
671 case 0x96:
672 if (mc) {
673 kp = true;
674 ch = '*';
675 }
676 break;
677 case 0x77:
678 if (mc) {
679 kp = true;
680 ch = '7';
681 }
682 break;
683 case 0x8D:
684 if (mc) {
685 kp = true;
686 ch = '8';
687 }
688 break;
689 case 0x84:
690 if (mc) {
691 kp = true;
692 ch = '9';
693 }
694 break;
695 case 0x8E:
696 if (mc) {
697 kp = true;
698 ch = '-';
699 }
700 break;
701 case 0x73:
702 if (mc) {
703 kp = true;
704 ch = '4';
705 }
706 break;
707 case 0x8F:
708 if (mc) {
709 kp = true;
710 ch = '5';
711 }
712 break;
713 case 0x74:
714 if (mc) {
715 kp = true;
716 ch = '6';
717 }
718 break;
719 case 0x90:
720 if (mc) {
721 kp = true;
722 ch = '+';
723 }
724 break;
725 case 0x75:
726 if (mc) {
727 kp = true;
728 ch = '1';
729 }
730 break;
731 case 0x91:
732 if (mc) {
733 kp = true;
734 ch = '2';
735 }
736 break;
737 case 0x76:
738 if (mc) {
739 kp = true;
740 ch = '3';
741 }
742 break;
743 case 0x92:
744 if (mc) {
745 kp = true;
746 ch = '0';
747 }
748 break;
749 case 0x93:
750 if (mc) {
751 kp = true;
752 ch = '.';
753 }
754 break;
755 default: break;
756 }
757
758 if (ch) {
759 if (kp) mods |= KC_MOD_KEYPAD;
760 if (ma) mods |= KC_MOD_ALT;
761 if (ms) mods |= KC_MOD_SHIFT;
762 Term_keypress(ch, mods);
763 }
764
765 /* Success */
766 return (0);
767 }
768
769
770 /*
771 * Handle a "special request"
772 *
773 * The given parameters are "valid".
774 */
775 static errr Term_xtra_ibm(int n, int v)
776 {
777 int i;
778
779 /* Analyze the request */
780 switch (n)
781 {
782 /* Make a "bell" noise */
783 case TERM_XTRA_NOISE:
784 {
785 /* Make a bell noise */
786 (void)write(1, "\007", 1);
787
788 /* Success */
789 return (0);
790 }
791
792 /* Set the cursor shape */
793 case TERM_XTRA_SHAPE:
794 {
795 /* Set cursor shape */
796 curs_set(v);
797
798 /* Success */
799 return (0);
800 }
801
802 /* Flush one line of output */
803 case TERM_XTRA_FROSH:
804 {
805
806 #ifdef USE_VIRTUAL
807
808 # ifdef USE_WAT
809
810 /* Copy the virtual screen to the physical screen */
811 memcpy(PhysicalScreen + (v*160), VirtualScreen + (v*160), 160);
812
813 # else /* USE_WAT */
814
815 /* Apply the virtual screen to the physical screen */
816 ScreenUpdateLine(VirtualScreen + ((v*cols) << 1), v);
817
818 # endif /* USE_WAT */
819
820 #endif /* USE_VIRTUAL */
821
822 /* Success */
823 return (0);
824 }
825
826 /* Clear the screen */
827 case TERM_XTRA_CLEAR:
828 {
829
830 #ifdef USE_CONIO
831
832 /* Clear the screen */
833 clrscr();
834
835 #else /* USE_CONIO */
836
837 /* Clear each line (virtual or physical) */
838 for (i = 0; i < rows; i++)
839 {
840 /* Clear the line */
841 memcpy((VirtualScreen + ((i*cols) << 1)), wiper, (cols << 1));
842 }
843
844 # ifdef USE_VIRTUAL
845
846 # ifdef USE_WAT
847
848 /* Copy the virtual screen to the physical screen */
849 memcpy(PhysicalScreen, VirtualScreen, 25*80*2);
850
851 # else /* USE_WAT */
852
853 /* Erase the physical screen */
854 ScreenClear();
855
856 # endif /* USE_WAT */
857
858 # endif /* USE_VIRTUAL */
859
860 #endif /* USE_CONIO */
861
862 /* Success */
863 return (0);
864 }
865
866 /* Process events */
867 case TERM_XTRA_EVENT:
868 {
869 /* Process one event */
870 return (Term_xtra_ibm_event(v));
871 }
872
873 /* Flush events */
874 case TERM_XTRA_FLUSH:
875 {
876 /* Strip events */
877 while (!Term_xtra_ibm_event(false)) /* loop */;
878
879 /* Success */
880 return (0);
881 }
882
883 /* React to global changes */
884 case TERM_XTRA_REACT:
885 {
886 /* React to "color_table" changes */
887 return (Term_xtra_ibm_react());
888 }
889
890 /* Delay for some milliseconds */
891 case TERM_XTRA_DELAY:
892 {
893 /* Delay if needed */
894 if (v > 0) delay(v);
895
896 /* Success */
897 return (0);
898 }
899 }
900
901 /* Unknown request */
902 return (1);
903 }
904
905
906
907 /*
908 * Move the cursor
909 *
910 * The given parameters are "valid".
911 */
912 static errr Term_curs_ibm(int x, int y)
913 {
914
915 #ifdef USE_WAT
916
917 # ifdef USE_CONIO
918
919 /* Place the cursor */
920 gotoxy(x+1, y+1);
921
922 # else /* USE_CONIO */
923
924 union REGS r;
925
926 r.h.ah = 2;
927 r.h.bh = 0;
928 r.h.dl = x;
929 r.h.dh = y;
930
931 /* Place the cursor */
932 int86(0x10, &r, &r);
933
934 # endif /* USE_CONIO */
935
936 #else /* USE_WAT */
937
938 /* Move the cursor */
939 ScreenSetCursor(y, x);
940
941 #endif /* USE_WAT */
942
943 /* Success */
944 return (0);
945 }
946
947
948 /*
949 * Erase a block of the screen
950 *
951 * The given parameters are "valid".
952 */
953 static errr Term_wipe_ibm(int x, int y, int n)
954 {
955
956 #ifdef USE_CONIO
957
958 /* Wipe the region */
959 window(x+1, y+1, x+n, y+1);
960 clrscr();
961 window(1, 1, cols, rows);
962
963 #else /* USE_CONIO */
964
965 /* Wipe part of the virtual (or physical) screen */
966 memcpy(VirtualScreen + ((cols*y + x)<<1), wiper, n<<1);
967
968 #endif /* USE_CONIO */
969
970 /* Success */
971 return (0);
972 }
973
974
975 /*
976 * Place some text on the screen using an attribute
977 *
978 * The given parameters are "valid". Be careful with "a".
979 *
980 * The string "cp" has length "n" and is NOT null-terminated.
981 */
982 static errr Term_text_ibm(int x, int y, int n, int a, const wchar_t *cp)
983 {
984 register int i;
985 register uint8_t attr;
986 register uint8_t *dest;
987
988 /* Handle "complex" color */
989 if (use_color_complex)
990 {
991 /* Extract a color index */
992 attr = (uint8_t)(get_ext_color(a));
993 }
994
995 /* Handle "simple" color */
996 else
997 {
998 /* Extract a color value */
999 attr = ibm_color_simple[get_ext_color(a)];
1000 }
1001
1002 #ifdef USE_CONIO
1003
1004 /* Place the cursor */
1005 gotoxy(x+1, y+1);
1006
1007 /* Set the attribute */
1008 textattr(attr);
1009
1010 /* Dump the text */
1011 for (i = 0; i < n; i++) {
1012 if (cp[i] < 0x100) {
1013 putch(cp[i]);
1014 }
1015 }
1016
1017 #else /* USE_CONIO */
1018
1019 /* Access the virtual (or physical) screen */
1020 dest = VirtualScreen + (((cols * y) + x) << 1);
1021
1022 /* Save the data */
1023 for (i = 0; i < n; i++)
1024 {
1025 /* Apply */
1026 *dest++ = cp[i];
1027 *dest++ = attr;
1028 }
1029
1030 #endif /* USE_CONIO */
1031
1032 /* Success */
1033 return (0);
1034 }
1035
1036
1037 /*
1038 * Place some attr/char pairs on the screen
1039 *
1040 * The given parameters are "valid".
1041 */
1042 static errr Term_pict_ibm(int x, int y, int n, const int *ap,
1043 const wchar_t *cp, const int *tap, const wchar_t *tcp)
1044 {
1045 register int i;
1046 register uint8_t attr;
1047 register uint8_t *dest;
1048
1049 /* Unused parameters */
1050 (void)tap;
1051 (void)tcp;
1052
1053 #ifdef USE_CONIO
1054
1055 /* Place the cursor */
1056 gotoxy(x+1, y+1);
1057
1058 /* Dump the text */
1059 for (i = 0; i < n; i++)
1060 {
1061 /* Handle "complex" color */
1062 if (use_color_complex)
1063 {
1064 /* Extract a color index */
1065 attr = (uint8_t)(get_ext_color(ap[i]));
1066 }
1067
1068 /* Handle "simple" color */
1069 else
1070 {
1071 /* Extract a color value */
1072 attr = ibm_color_simple[get_ext_color(ap[i])];
1073 }
1074
1075 /* Set the attribute */
1076 textattr(attr);
1077
1078 /* Dump the char */
1079 if (cp[i] < 0x100) {
1080 putch(cp[i]);
1081 }
1082 }
1083
1084 #else /* USE_CONIO */
1085
1086 /* Access the virtual (or physical) screen */
1087 dest = VirtualScreen + (((cols * y) + x) << 1);
1088
1089 /* Save the data */
1090 for (i = 0; i < n; i++)
1091 {
1092 /* Handle "complex" color */
1093 if (use_color_complex)
1094 {
1095 /* Extract a color index */
1096 attr = (uint8_t)(get_ext_color(ap[i]));
1097 }
1098
1099 /* Handle "simple" color */
1100 else
1101 {
1102 /* Extract a color value */
1103 attr = ibm_color_simple[get_ext_color(ap[i])];
1104 }
1105
1106 /* Apply */
1107 *dest++ = cp[i];
1108 *dest++ = attr;
1109 }
1110
1111 #endif /* USE_CONIO */
1112
1113 /* Success */
1114 return (0);
1115 }
1116
1117
1118 /*
1119 * Init a Term
1120 */
1121 static void Term_init_ibm(term *t)
1122 {
1123 /* Unused parameter */
1124 (void)t;
1125
1126 /* XXX Nothing */
1127 }
1128
1129
1130 /*
1131 * Nuke a Term
1132 */
1133 static void Term_nuke_ibm(term *t)
1134 {
1135 #ifdef USE_WAT
1136
1137 /* Nothing */
1138
1139 #else /* USE_WAT */
1140
1141 union REGS r;
1142
1143 #endif /* USE_WAT */
1144
1145 /* Unused parameter */
1146 (void)t;
1147
1148 /* Move the cursor to the bottom of the screen */
1149 Term_curs_ibm(0, rows-1);
1150
1151 #ifdef USE_WAT
1152
1153 /* Restore the original video mode */
1154 _setvideomode(_DEFAULTMODE);
1155
1156 #else /* USE_WAT */
1157
1158 /* Restore the original video mode */
1159 r.h.ah = 0x00;
1160 r.h.al = 0x03;
1161 int86(0x10, &r, &r);
1162
1163 #endif /* USE_WAT */
1164
1165 /* Make the cursor visible */
1166 curs_set(1);
1167 }
1168
1169 int Term_iswprint_ibm(wint_t wc)
1170 {
1171 int retval;
1172 retval = (wc < 0x100);
1173 return retval;
1174 }
1175
1176 wchar_t *Term_wcschr_ibm(const wchar_t *s, wchar_t c) {
1177 wchar_t cc = c;
1178
1179 while (*s) {
1180 if (*s == cc) {
1181 return unconst(s, wchar_t *);
1182 }
1183 s++;
1184 }
1185
1186 if (cc == 0) {
1187 return unconst(s, wchar_t *);
1188 }
1189
1190 return 0;
1191 }
1192
1193 size_t Term_wcslen_ibm(const wchar_t *s) {
1194 const wchar_t *p;
1195
1196 if (s == 0) {
1197 return 0;
1198 }
1199
1200 for (p = s; *p; ++p);
1201
1202 return p - s;
1203 }
1204
1205 #ifdef USE_GRAPHICS
1206
1207 #ifdef USE_286
1208
1209 /*
1210 * In 286 mode we don't need to worry about translating from a 32bit
1211 * pointer to a 16 bit pointer so we just call the interrupt function
1212 *
1213 * Note the use of "intr()" instead of "int86()" so we can pass
1214 * segment registers.
1215 */
1216 void enable_graphic_font(void *font)
1217 {
1218 union REGPACK regs =
1219 {0};
1220
1221 regs.h.ah = 0x11; /* Text font function */
1222 regs.h.bh = 0x10; /* Size of a character -- 16 bytes */
1223 regs.h.cl = 0xFF; /* Last character in font */
1224 regs.x.es = FP_SEG(font); /* Pointer to font */
1225 regs.x.bp = FP_OFF(font);
1226 intr(0x10, &regs);
1227 };
1228
1229 #else /* USE_286 */
1230
1231 #ifdef USE_WAT
1232
1233 /*
1234 * This structure is used by the DMPI function to hold registers when
1235 * doing a real mode interrupt call. (Stolen from the DJGPP <dpmi.h>
1236 * header file).
1237 */
1238
1239 typedef union
1240 {
1241 struct
1242 {
1243 unsigned long edi;
1244 unsigned long esi;
1245 unsigned long ebp;
1246 unsigned long res;
1247 unsigned long ebx;
1248 unsigned long edx;
1249 unsigned long ecx;
1250 unsigned long eax;
1251 } d;
1252 struct
1253 {
1254 unsigned short di, di_hi;
1255 unsigned short si, si_hi;
1256 unsigned short bp, bp_hi;
1257 unsigned short res, res_hi;
1258 unsigned short bx, bx_hi;
1259 unsigned short dx, dx_hi;
1260 unsigned short cx, cx_hi;
1261 unsigned short ax, ax_hi;
1262 unsigned short flags;
1263 unsigned short es;
1264 unsigned short ds;
1265 unsigned short fs;
1266 unsigned short gs;
1267 unsigned short ip;
1268 unsigned short cs;
1269 unsigned short sp;
1270 unsigned short ss;
1271 } x;
1272 struct
1273 {
1274 unsigned char edi[4];
1275 unsigned char esi[4];
1276 unsigned char ebp[4];
1277 unsigned char res[4];
1278 unsigned char bl, bh, ebx_b2, ebx_b3;
1279 unsigned char dl, dh, edx_b2, edx_b3;
1280 unsigned char cl, ch, ecx_b2, ecx_b3;
1281 unsigned char al, ah, eax_b2, eax_b3;
1282 } h;
1283 } __dpmi_regs;
1284
1285 unsigned __dpmi_allocate_dos_memory(int size, unsigned *selector)
1286 {
1287 union REGPACK regs =
1288 {0};
1289
1290 regs.w.ax = 0x100; /* DPMI function -- allocate low memory */
1291 regs.w.bx = size; /* Number of Paragraphs to allocate */
1292 intr(0x31, &regs); /* DPMI interface */
1293
1294 *selector = regs.w.dx;
1295 return (regs.w.ax);
1296 };
1297
1298 void __dpmi_free_dos_memory(unsigned sel)
1299 {
1300 union REGPACK regs =
1301 {0};
1302
1303 regs.w.ax = 0x101; /* DPMI function -- free low memory */
1304 regs.x.edx = sel; /* PM selector for memory block */
1305 intr(0x31, &regs); /* DPMI interface */
1306 };
1307
1308 void __dpmi_int(int intno, __dpmi_regs *dblock)
1309 {
1310 union REGPACK regs =
1311 {0};
1312
1313 regs.w.ax = 0x300; /* DPMI function -- real mode interrupt */
1314 regs.h.bl = intno; /* interrupt 0x10 */
1315 regs.x.edi = FP_OFF(dblock); /* Pointer to dblock (offset and segment) */
1316 regs.x.es = FP_SEG(dblock);
1317 intr(0x31, &regs); /* DPMI interface */
1318 };
1319
1320 unsigned short __dpmi_sel = 0x0000;
1321 #define _farsetsel(x) __dpmi_sel=(x)
1322 extern void _farnspokeb(unsigned long offset, unsigned char value);
1323 #pragma aux _farnspokeb = \
1324 "push fs" \
1325 "mov fs,__dpmi_sel" \
1326 "mov fs:[eax],bl" \
1327 "pop fs" \
1328 parm [eax] [bl];
1329
1330 #else /* USE_WAT */
1331
1332 #include <dpmi.h>
1333 #include <go32.h>
1334 #include <sys/farptr.h>
1335
1336 #endif /* USE_WAT */
1337
1338
1339 /*
1340 * Since you cannot send 32bit pointers to a 16bit interrupt handler
1341 * and the video BIOS wants a (16bit) pointer to the font, we have
1342 * to allocate a block of dos memory, copy the font into it, then
1343 * translate a 32bit pointer into a 16bit pointer to that block.
1344 *
1345 * DPMI - Dos Protected Mode Interface provides functions that let
1346 * us do that.
1347 */
1348 void enable_graphic_font(const char *font)
1349 {
1350 __dpmi_regs dblock = {{0}};
1351
1352 unsigned int seg, i;
1353 int sel;
1354
1355 /*
1356 * Allocate a block of memory 4096 bytes big in `low memory' so a real
1357 * mode interrupt can access it. Real mode pointer is returned as seg:0
1358 * Protected mode pointer is sel:0.
1359 */
1360 seg = __dpmi_allocate_dos_memory(256, &sel);
1361
1362 /* Copy the information into low memory buffer, by copying one byte at
1363 * a time. According to the info in <sys/farptr.h>, the functions
1364 * _farsetsel() and _farnspokeb() will optimise away completely
1365 */
1366 _farsetsel(sel); /* Set the selector to write to */
1367 for (i = 0; i<4096; i++)
1368 {
1369 _farnspokeb(i, *font++); /* Copy 1 byte into low (far) memory */
1370 }
1371
1372 /*
1373 * Now we use DPMI as a jumper to call the real mode interrupt. This
1374 * is needed because loading `es' while in protected mode with a real
1375 * mode pointer will cause an Protection Fault and calling the interrupt
1376 * directly using the protected mode pointer will result in garbage
1377 * being received by the interrupt routine
1378 */
1379 dblock.d.eax = 0x1100; /* BIOS function -- set font */
1380 dblock.d.ebx = 0x1000; /* bh = size of a letter; bl = 0 (reserved) */
1381 dblock.d.ecx = 0x00FF; /* Last character in font */
1382 dblock.x.es = seg; /* Pointer to font segment */
1383 dblock.d.ebp = 0x0000; /* Pointer to font offset */
1384
1385 __dpmi_int(0x10, &dblock);
1386
1387 /* We're done with the low memory, free it */
1388 __dpmi_free_dos_memory(sel);
1389 }
1390
1391 #endif /* USE_286 */
1392
1393 #endif /* ALLOW_GRAPH */
1394
1395
1396 const char help_ibm[] = "IBM Visual Display Support";
1397
1398
1399 /*
1400 * Initialize the IBM "visual module"
1401 *
1402 * Hack -- we assume that "blank space" should be "white space"
1403 * (and not "black space" which might make more sense).
1404 *
1405 * Note the use of "((x << 2) | (x >> 4))" to "expand" a 6 bit value
1406 * into an 8 bit value, without losing much precision, by using the 2
1407 * most significant bits as the least significant bits in the new value.
1408 */
1409 errr init_ibm(int argc, char **argv)
1410 {
1411 int i;
1412 int mode;
1413
1414 term *t = &term_screen_body;
1415
1416 union REGS r;
1417
1418 /* Unused parameters */
1419 (void)argc;
1420 (void)argv;
1421
1422 /* Check for "Windows" */
1423 if (getenv("windir"))
1424 {
1425 r.h.ah = 0x16; /* Windows API Call -- Set device focus */
1426 r.h.al = 0x8B; /* Causes Dos boxes to become fullscreen */
1427 r.h.bh = r.h.bl = 0x00; /* 0x0000 = current Dos box */
1428 int86(0x2F, &r, &r); /* Call the Windows API */
1429 };
1430
1431 /* Initialize "color_table" */
1432 for (i = 0; i < 16; i++)
1433 {
1434 long rv, gv, bv;
1435
1436 /* Extract desired values */
1437 rv = angband_color_table[i][1] >> 2;
1438 gv = angband_color_table[i][2] >> 2;
1439 bv = angband_color_table[i][3] >> 2;
1440
1441 /* Extract the "complex" codes */
1442 ibm_color_complex[i] = ((rv) | (gv << 8) | (bv << 16));
1443
1444 /* Save the "simple" codes */
1445 angband_color_table[i][0] = ibm_color_simple[i];
1446 }
1447
1448 #ifdef USE_WAT
1449
1450 /* Set the video mode */
1451 if (_setvideomode(_VRES16COLOR))
1452 {
1453 mode = 0x13;
1454 }
1455
1456 /* Wimpy monitor */
1457 else
1458 {
1459 mode = 0x03;
1460 }
1461
1462 /* Force 25 line mode */
1463 _setvideomode(_TEXTC80);
1464 _settextrows(25);
1465
1466 #else /* USE_WAT */
1467
1468 /* Set video mode */
1469 r.h.ah = 0x00;
1470 r.h.al = 0x13; /* VGA only mode */
1471 int86(0x10, &r, &r);
1472
1473 /* Get video mode */
1474 r.h.ah = 0x0F;
1475 int86(0x10, &r, &r);
1476 mode = r.h.al;
1477
1478 /* Set video mode */
1479 r.h.ah = 0x00;
1480 r.h.al = 0x03; /* Color text mode */
1481 int86(0x10, &r, &r);
1482
1483 #endif /* USE_WAT */
1484
1485 /* Check video mode */
1486 if (mode == 0x13)
1487 {
1488 /* Remember the mode */
1489 use_color_complex = true;
1490
1491 /* Instantiate the color set */
1492 activate_color_complex();
1493 }
1494
1495 #ifdef USE_GRAPHICS
1496
1497 /* Try to activate bitmap graphics */
1498 if (arg_graphics && use_color_complex)
1499 {
1500 FILE *f;
1501
1502 char buf[4096];
1503
1504 /* Build the filename */
1505 path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA, "angband.fnt");
1506
1507 /* Open the file */
1508 f = fopen(buf, "rb");
1509
1510 /* Okay */
1511 if (f)
1512 {
1513 /* Load the bitmap data */
1514 if (fread(buf, 1, sizeof(buf), f) != 4096)
1515 {
1516 quit("Corrupt 'angband.fnt' file");
1517 }
1518
1519 /* Close the file */
1520 fclose(f);
1521
1522 /* Enable graphics */
1523 enable_graphic_font(buf);
1524
1525 /* Enable colors (again) */
1526 activate_color_complex();
1527
1528 /* Use graphics */
1529 use_graphics = GRAPHICS_ORIGINAL;
1530 }
1531 }
1532
1533 #endif
1534
1535 #ifdef USE_CONIO
1536 #else /* USE_CONIO */
1537
1538 /* Build a "wiper line" */
1539 for (i = 0; i < 80; i++)
1540 {
1541 /* Space */
1542 wiper[2*i] = ' ';
1543
1544 /* Black */
1545 wiper[2*i+1] = COLOUR_WHITE;
1546 }
1547
1548 #endif /* USE_CONIO */
1549
1550
1551 #ifdef USE_VIRTUAL
1552
1553 /* Make the virtual screen */
1554 VirtualScreen = mem_zalloc(rows * cols * 2 * sizeof(uint8_t));
1555
1556 #endif /* USE_VIRTUAL */
1557
1558
1559 /* Erase the screen */
1560 Term_xtra_ibm(TERM_XTRA_CLEAR, 0);
1561
1562
1563 /* Place the cursor */
1564 Term_curs_ibm(0, 0);
1565
1566
1567 /* Access the "default" cursor info */
1568 r.h.ah = 3;
1569 r.h.bh = 0;
1570
1571 /* Make the call */
1572 int86(0x10, &r, &r);
1573
1574 /* Extract the standard cursor info */
1575 saved_cur_v = 1;
1576 saved_cur_high = r.h.ch;
1577 saved_cur_low = r.h.cl;
1578
1579
1580 /* Initialize the term */
1581 term_init(t, 80, 24, 256);
1582
1583 #ifdef USE_CONIO
1584 #else /* USE_CONIO */
1585
1586 /* Always use "Term_pict()" */
1587 t->always_pict = true;
1588
1589 #endif /* USE_CONIO */
1590
1591 /* Prepare the init/nuke hooks */
1592 t->init_hook = Term_init_ibm;
1593 t->nuke_hook = Term_nuke_ibm;
1594
1595 /* Connect the hooks */
1596 t->xtra_hook = Term_xtra_ibm;
1597 t->curs_hook = Term_curs_ibm;
1598 t->wipe_hook = Term_wipe_ibm;
1599 t->text_hook = Term_text_ibm;
1600 t->pict_hook = Term_pict_ibm;
1601 text_iswprint_hook = Term_iswprint_ibm;
1602 text_wcschr_hook = Term_wcschr_ibm;
1603 text_wcslen_hook = Term_wcslen_ibm;
1604
1605 /* Save it */
1606 term_screen = t;
1607
1608 /* Activate it */
1609 Term_activate(term_screen);
1610
1611 /* Success */
1612 return 0;
1613 }
1614
1615
1616 #endif /* USE_IBM */
1617