summaryrefslogtreecommitdiff
path: root/src/main-emx.c
blob: 6d048e795e38bf5e26a945148936f1a5000c7c8d (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
/* File: main-emx.c */

/*
 * Copyright (c) 1997 Ben Harrison, Ekkehard Kraemer, and others
 *
 * This software may be copied and distributed for educational, research,
 * and not for profit purposes provided that this copyright and statement
 * are included in all such copies.
 */

/* Purpose: Support for OS/2 EMX Angband */

/* Author: ekraemer@pluto.camelot.de (Ekkehard Kraemer) */

/* Current maintainer: silasd@psyber.com (Silas Dunsmore) */
/* Unless somebody else wants it.... */

#ifdef USE_EMX

/*
 * === Instructions for using Angband 2.7.X with OS/2 ===
 *
 * The patches (file "main-emx.c") to compile Angband 2.7.X under OS/2
 * were written by ekraemer@pluto.camelot.de (Ekkehard Kraemer).
 *
 * TO COMPILE:
 *
 * - untar the archive into /angband (or /games/angband or whatever)
 * - change directory to /angband/src
 * - run "dmake -B -r -f makefile.emx" (not gmake or make)
 *
 * TO INSTALL:
 *
 * - change directory to /angband/src
 * - run "dmake -B -r -f makefile.emx install" (not gmake or make)
 * - copy your old savefile into ./lib/save and your old pref.prf into ./lib/user
 * - start /angband/angband.exe for one single window
 * - start /angband/startwnd.cmd for multiple windows
 *
 * TO REMOVE TEMPORARY FILES:
 *
 * - run 'dmake -B -r -f makefile.emx clean'
 *
 *
 * I use EMX 0.9b, but every EMX compiler since 0.8g or so should work
 * fine. EMX is available at ftp-os2.cdrom.com ("Hobbes"), as is dmake.
 *
 *  dmake:    ftp://ftp-os2.cdrom.com/all/program/dmake38X.zip
 *  EMX:      ftp://ftp-os2.cdrom.com/2_x/unix/emx???/   (most probably)
 *
 * Old savefiles must be renamed to follow the new "savefile" naming
 * conventions.  Either rename the savefile to "PLAYER", or start the
 * program with the "-uName" flag.  See "main.c" for details.  The
 * savefiles are stores in "./lib/save" if you forgot the old names...
 *
 *  Changes
 *  =======
 *
 *  When       By     Version   What
 *  -------------------------------------------------------------------
 *
 *  18.11.95   EK      2.7.8    Added window/pipe code
 *                              Introduced __EMX__CLIENT__ hack
 *
 *  15.12.95   EK      2.7.9    Updated for 2.7.9
 *                     beta     Added third view
 *                              Added number of line support in aclient
 *
 *  25.12.95   EK      2.7.9    Added 'install' target
 *                    non-beta  Updated installation hints
 *                              Uploaded binary to export.andrew.cmu.edu
 *
 *  25.01.96   EK      2.7.9    Updated for 2.7.9v3
 *                      v3      Removed (improved) keyboard hack
 *                              Introduced pref-emx.prf
 *                              Phew... Makefile.emx grows! (patches, export)
 *                              Uploaded binary to export.andrew.cmu.edu
 *
 *  26.01.96   EK               Added files.uue target
 *
 *  22.02.96   EK               Added PM support
 *
 *   2.03.96   EK      2.7.9    Uploaded binaries to export.andrew.cmu.edu
 *                      v4
 *
 *   9.03.96   EK      2.7.9    Adjustable background color (PM)
 *                      v5      Added map window
 
 *  3 Dec 97   SWD      282     Brought key-handling, macros in sync with DOS.
 *                              Hacked on sub-window code -- it compiles, but
 *                              doesn't link.
 *
 * 23 Jan 98   SWD      282     Hacked more on sub-windows.  Now links, with
 *                              warnings.  Seems to work.
 *
 * 01 Nov 98   AGA      2.8.3	Adjusted for 2.8.3 sources, typos corrected
 *
 * 01 Nov 98   AGA	Z214b	Corrections for ZAngband 2.1.4 beta
 *
 */

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/kbdscan.h>
#include <io.h>
#define INCL_KBD 1
#include <os2.h>
#include <sys/video.h>

#include "angband.h"


/*
 * Maximum windows
 */
#define MAX_TERM_DATA 8


/*
 * Keypress input modifier flags (copied from main-ibm.c)
 *
 * SWD: these could be changed to the definitions in <os2.h>, which are
 *      direct bitmasks instead of shift-counts.
 */
#define K_RSHIFT	0	/* Right shift key down */
#define K_LSHIFT	1	/* Left shift key down */
#define K_CTRL		2	/* Ctrl key down */
#define K_ALT		3	/* Alt key down */
#define K_SCROLL	4	/* Scroll lock on */
#define K_NUM		5	/* Num lock on */
#define K_CAPS		6	/* Caps lock on */
#define K_INSERT	7	/* Insert on */


/*
 * Prototypes!
 */
static errr Term_curs_emx(int x, int y);
static errr Term_wipe_emx(int x, int y, int n);
static errr Term_text_emx(int x, int y, int n, unsigned char a, cptr s);
static void Term_init_emx(term *t);
static void Term_nuke_emx(term *t);

#ifndef EMXPM

/*
 * termPipe* is sometimes cast to term* and vice versa,
 * so "term t;" must be the first line
 */

typedef struct
{
	term t;
	FILE *out;                   /* used by the ..._pipe_emx stuff */
}
termPipe;

/*
 * Communication between server and client
 */

enum
{
	PIP_INIT,
	PIP_NUKE,
	PIP_XTRA,
	PIP_CURS,
	PIP_WIPE,
	PIP_TEXT,
};


/*
 * Current cursor "size"
 */
static int curs_start = 0;
static int curs_end = 0;

/*
 * Angband color conversion table
 *
 * Note that "Light Green"/"Yellow"/"Red" are used for
 * "good"/"fair"/"awful" stats and conditions.
 *
 * But "brown"/"light brown"/"orange" are really only used
 * for "objects" (such as doors and spellbooks), and these
 * values can in fact be re-defined.
 *
 * Future versions of Angband will probably allow the
 * "use" of more that 16 colors, so "extra" entries can be
 * made at the end of this table in preparation.
 */
static int colors[16] =
{
	F_BLACK,                     /* Black */
	F_WHITE | INTENSITY,           /* White */
	F_WHITE,                     /* XXX Gray */
	F_RED | INTENSITY,             /* Orange */
	F_RED,                       /* Red */
	F_GREEN,                     /* Green */
	F_BLUE,                      /* Blue */
	F_BROWN,                     /* Brown */
	F_BLACK | INTENSITY,           /* Dark-grey */
	F_WHITE,                     /* XXX Light gray */
	F_MAGENTA,                   /* Purple */
	F_YELLOW | INTENSITY,          /* Yellow */
	F_RED | INTENSITY,             /* Light Red */
	F_GREEN | INTENSITY,           /* Light Green */
	F_BLUE | INTENSITY,            /* Light Blue */
	F_BROWN | INTENSITY           /* Light brown */
};

/*
 * Display a cursor, on top of a given attr/char
 */
static errr Term_curs_emx(int x, int y)
{
	v_gotoxy(x, y);
	v_ctype(curs_start, curs_end);

	return (0);
}

/*
 * Erase a grid of space (as if spaces were printed)
 */
static errr Term_wipe_emx(int x, int y, int n)
{
	v_gotoxy(x, y);
	v_putn(' ', n);

	return (0);
}

/*
 * Draw some text, wiping behind it first
 */
static errr Term_text_emx(int x, int y, int n, unsigned char a, cptr s)
{
	/* Convert the color and put the text */
	v_attrib(colors[a & 0x0F]);
	v_gotoxy(x, y);
	v_putm(s, n);

	return (0);
}

/*
 * EMX initialization
 */
static void Term_init_emx(term *t)
{
	struct _KBDINFO kbdinfo; 	/* see structure description ?somewhere? */

	v_init();
	v_getctype(&curs_start, &curs_end);
	/* hide cursor (?) XXX XXX XXX */
	v_clear();

	/* the documentation I (SWD) have implies, in passing, that setting */
	/* "binary mode" on the keyboard device will prevent the O/S from */
	/* acting on keys such as ^S (pause) and ^P (printer echo). */

	/* note also that "KbdSetStatus is ignored for a Vio-windowed application." */
	/* so there may well be problems with running this in a window.  Damnit. */

	/* this is kind of a nasty structure, as you can't just flip a bit */
	/* to change binary/ASCII mode, or echo on/off mode... nor can you */
	/* clear the whole thing -- certain bits need to be preserved. */

	KbdGetStatus(&kbdinfo, (HKBD)0);
	kbdinfo.fsMask &= ~ (KEYBOARD_ECHO_ON | 	/* clear lowest four bits */
	                     KEYBOARD_ECHO_OFF | KEYBOARD_BINARY_MODE | KEYBOARD_ASCII_MODE);
	kbdinfo.fsMask |= (KEYBOARD_BINARY_MODE); 	/* set bit two */
	KbdSetStatus(&kbdinfo, (HKBD)0);

#if 1 /* turn off for debug */
	signal(SIGHUP, SIG_IGN);
	signal(SIGINT, SIG_IGN);
	signal(SIGQUIT, SIG_IGN);
	/*  signal(SIGILL,SIG_IGN);  */
	/*  signal(SIGTRAP,SIG_IGN); */
	/*  signal(SIGABRT,SIG_IGN); */
	/*  signal(SIGEMT,SIG_IGN);  */
	/*  signal(SIGFPE,SIG_IGN);  */
	/*  signal(SIGBUS,SIG_IGN);  */
	/*  signal(SIGSEGV,SIG_IGN); */
	/*  signal(SIGSYS,SIG_IGN);  */
	signal(SIGPIPE, SIG_IGN);
	signal(SIGALRM, SIG_IGN);
	/*  signal(SIGTERM,SIG_IGN); */
	signal(SIGUSR1, SIG_IGN);
	signal(SIGUSR2, SIG_IGN);
	signal(SIGCHLD, SIG_IGN);
	signal(SIGBREAK, SIG_IGN);
#endif

}

/*
 * EMX shutdown
 */
static void Term_nuke_emx(term *t)
{
	/* Move the cursor to bottom of screen */
	v_gotoxy(0, 23);

	/* Restore the cursor (not necessary) */
	v_ctype(curs_start, curs_end);

	/* Set attribute to gray on black */
	v_attrib(F_WHITE);

	/* Clear the screen */
	v_clear();
}


#ifndef __EMX__CLIENT__

/*
 * Oh no, more prototypes!
 */
static errr CheckEvents(int returnImmediately);
static errr Term_xtra_pipe_emx(int n, int v);
static errr Term_curs_pipe_emx(int x, int y);
static errr Term_wipe_pipe_emx(int x, int y, int n);
static errr Term_text_pipe_emx(int x, int y, int n, unsigned char a, cptr s);
static void Term_init_pipe_emx(term *t);
static void Term_nuke_pipe_emx(term *t);
static FILE *initPipe(const char *name);
static void initPipeTerm(termPipe *pipe, const char *name, term **term);

/*
 * Main initialization function
 */
errr init_emx(void);

/*
 * The screens
 */
static termPipe term_screen_aga[MAX_TERM_DATA];


/*
 * Check for events -- called by "Term_scan_emx()"
 *
 * Note -- this is probably NOT the most efficient way
 * to "wait" for a keypress (TERM_XTRA_EVENT).
 *
 *
 * This code was ripped from "main-ibm.c" -- consult it to
 * figure out what's going on.
 *
 * See "main-ibm.c" for more information about "macro encoding".
 *
 *
 * The following documentation was cut&pasted from
 * the OS/2 Programming Reference, PRCP.INF
 * <ftp://hobbes.nmsu.edu/pub/os2/dev/16-bit/inf16bit.zip>
-------------------------------------------------------------------------------
 
 
 This call returns a character data record from the keyboard.
 
  KbdCharIn    (CharData, IOWait, KbdHandle)
 
  CharData (PKBDKEYINFO) - output
     Address of the character data structure:
 
     asciicharcode (UCHAR)
	ASCII character code. The scan code received from the keyboard is
	translated to the ASCII character code.
 
     scancode (UCHAR)
	Code received from the keyboard.  The scan code received from the
	keyboard is translated to the ASCII character code.
 
     status (UCHAR)
	State of the keystroke event:
 
	Bit       Description
 
	7-6       00 = Undefined
 
		  01 = Final character, interim character flag off
 
		  10 = Interim character
 
		  11 = Final character, interim character flag on.
 
	5         1 = Immediate conversion requested.
 
	4-2       Reserved.
 
	1         0 = Scan code is a character.
 
		  1 = Scan code is not a character; is an extended key code
		  from the keyboard.
 
	0         1 = Shift status returned without character.
 
     reserved (UCHAR)
	NLS shift status.  Reserved, set to zero.
 
     shiftkeystat (USHORT)
	Shift key status.
 
	Bit       Description
	15        SysReq key down
	14        CapsLock key down
	13        NumLock key down
	12        ScrollLock key down
	11        Right Alt key down
	10        Right Ctrl key down
	9         Left Alt key down
	8         Left Ctrl key down
	7         Insert on
	6         CapsLock on
	5         NumLock on
	4         ScrollLock on
	3         Either Alt key down
	2         Either Ctrl key down
	1         Left Shift key down
	0         Right Shift key down
 
     time (ULONG)
	Time stamp indicating when a key was pressed.  It is specified in
	milliseconds from the time the system was started.
 
  IOWait (USHORT) - input
     Wait if a character is not available.
 
     Value     Definition
     0         Requestor waits for a character if one is not available.
     1         Requestor gets an immediate return if no character is
	       available.
 
  KbdHandle (HKBD) - input
     Default keyboard or the logical keyboard.
 
  rc (USHORT) - return
     Return code descriptions are:
 
     0         NO_ERROR
     375       ERROR_KBD_INVALID_IOWAIT
     439       ERROR_KBD_INVALID_HANDLE
     445       ERROR_KBD_FOCUS_REQUIRED
     447       ERROR_KBD_KEYBOARD_BUSY
     464       ERROR_KBD_DETACHED
     504       ERROR_KBD_EXTENDED_SG
 
  Remarks
 
  On an enhanced keyboard, the secondary enter key returns the normal
  character 0DH and a scan code of E0H.
 
  Double-byte character codes (DBCS) require two function calls to obtain
  the entire code.
 
  If shift report is set with KbdSetStatus, the CharData record returned
  reflects changed shift information only.
 
  Extended ASCII codes are identified with the status byte, bit 1 on and the
  ASCII character code being either 00H or E0H. Both conditions must be
  satisfied for the character to be an extended keystroke.  For extended
  ASCII codes, the scan code byte returned is the second code (extended
  code). Usually the extended ASCII code is the scan code of the primary
  key that was pressed.
 
  A thread in the foreground session that repeatedly polls the keyboard
  with KbdCharIn (with no wait), can prevent all regular priority class
  threads from executing.  If polling must be used and a minimal amount of
  other processing is being performed, the thread should periodically yield to
  the CPU by issuing a DosSleep call for an interval of at least 5
  milliseconds.
 
 
  Family API Considerations
 
  Some options operate differently in the DOS mode than in the OS /2 mode.
  Therefore, the following restrictions apply to KbdCharIn when coding in
  the DOS mode:
 
  o The CharData structure includes everything except the time stamp.
  o Interim character is not supported
  o Status can be 0 or 40H
  o KbdHandle is ignored.
 
 
-------------------------------------------------------------------------------
 
 
   typedef struct _KBDKEYINFO {   / * kbci * /
     UCHAR    chChar;             / * ASCII character code * /
     UCHAR    chScan;             / * Scan Code * /
     UCHAR    fbStatus;           / * State of the character * /
     UCHAR    bNlsShift;          / * Reserved (set to zero) * /
     USHORT   fsState;            / * State of the shift keys * /
     ULONG    time;               / * Time stamp of keystroke (ms since ipl) * /
   }KBDKEYINFO;
 
   #define INCL_KBD
 
   USHORT  rc = KbdCharIn(CharData, IOWait, KbdHandle);
 
   PKBDKEYINFO      CharData;      / * Buffer for data * /
   USHORT           IOWait;        / * Indicate if wait * /
   HKBD             KbdHandle;     / * Keyboard handle * /
 
   USHORT           rc;            / * return code * /
 
 
-------------------------------------------------------------------------------
 *
 */
static errr CheckEvents(int returnImmediately)
{
	int i, k, s;

	bool mc = FALSE;
	bool ms = FALSE;
	bool ma = FALSE;

	/* start OS/2 specific section */

	struct _KBDKEYINFO keyinfo; 	/* see structure description above */

	/* Check (and possibly wait) for a keypress */
	/* see function description above */
	KbdCharIn( &keyinfo, returnImmediately, (HKBD)0 );

#if 0
	printf("AC:%x SC:%x ST:%x R1:%x SH:%x TI:%ld\n", 	/* OS/2 debug */
	       keyinfo.chChar,
	       keyinfo.chScan,
	       keyinfo.fbStatus,
	       keyinfo.bNlsShift,
	       keyinfo.fsState,
	       keyinfo.time );
#endif

	/* If there wasn't a key, leave now. */
	if ((keyinfo.fbStatus & 0xC0) == 0) return (1);


	/* by a set of lucky coincidences, the data maps directly over. */
	k = keyinfo.chChar;
	s = keyinfo.chScan;
	i = (keyinfo.fsState & 0xFF);

	/* end OS/2 specific section */


	/* Process "normal" keys */
	if ( k != 0 && ((s <= 58) || (s == 0xE0)) )	/* Tweak: allow for ALT-keys */
	{
		/* Enqueue it */
		Term_keypress(k);

		/* Success */
		return (0);
	}

	/* Extract the modifier flags */
	if (i & (1 << K_CTRL)) mc = TRUE;
	if (i & (1 << K_LSHIFT)) ms = TRUE;
	if (i & (1 << K_RSHIFT)) ms = TRUE;
	if (i & (1 << K_ALT)) ma = TRUE;


	/* Begin a "macro trigger" */
	Term_keypress(31);

	/* Hack -- Send the modifiers */
	if (mc) Term_keypress('C');
	if (ms) Term_keypress('S');
	if (ma) Term_keypress('A');

	/* Introduce the hexidecimal scan code */
	Term_keypress('x');

	/* Encode the hexidecimal scan code */
	Term_keypress(hexsym[s / 16]);
	Term_keypress(hexsym[s % 16]);

	/* End the "macro trigger" */
	Term_keypress(13);

	/* Success */
	return (0);
}



/*
 * Do a special thing (beep, flush, etc)
 */
static errr Term_xtra_emx(int n, int v)
{
	switch (n)
	{
	case TERM_XTRA_SHAPE:
		if (v)
		{
			v_ctype(curs_start, curs_end);
		}
		else
		{
			v_hidecursor();
		}
		return (0);

	case TERM_XTRA_NOISE:
		DosBeep(440, 50);
		return (0);

	case TERM_XTRA_FLUSH:
		while (!CheckEvents(TRUE));
		return 0;

	case TERM_XTRA_EVENT:

		/* Process an event */
		return (CheckEvents(!v));

		/* Success */
		return (0);

	case TERM_XTRA_CLEAR:
		v_clear();
		return (0);
	}

	return (1);
}

static errr Term_xtra_pipe_emx(int n, int v)
{
	termPipe *tp = (termPipe*)Term;

	switch (n)
	{
	case TERM_XTRA_NOISE:
		DosBeep(440, 50);
		return (0);

	case TERM_XTRA_SHAPE:
		return (0);

	case TERM_XTRA_EVENT:
		return (CheckEvents(FALSE));

	case TERM_XTRA_CLEAR:

		if (!tp->out) return -1;

		fputc(PIP_XTRA, tp->out);
		fwrite(&n, sizeof(n), 1, tp->out);
		fwrite(&v, sizeof(v), 1, tp->out);
		fflush(tp->out);

		return (0);
	}

	return (1);
}

static errr Term_curs_pipe_emx(int x, int y)
{
	termPipe *tp = (termPipe*)Term;

	if (!tp->out) return -1;

	fputc(PIP_CURS, tp->out);
	fwrite(&x, sizeof(x), 1, tp->out);
	fwrite(&y, sizeof(y), 1, tp->out);
	fflush(tp->out);

	return (0);
}


static errr Term_wipe_pipe_emx(int x, int y, int n)
{
	termPipe *tp = (termPipe*)Term;

	if (!tp->out) return -1;

	fputc(PIP_WIPE, tp->out);
	fwrite(&x, sizeof(x), 1, tp->out);
	fwrite(&y, sizeof(y), 1, tp->out);
	fwrite(&n, sizeof(n), 1, tp->out);
	fflush(tp->out);

	return (0);
}


static errr Term_text_pipe_emx(int x, int y, int n, unsigned char a, cptr s)
{
	termPipe *tp = (termPipe*)Term;

	if (!tp->out) return -1;

	fputc(PIP_TEXT, tp->out);
	fwrite(&x, sizeof(x), 1, tp->out);
	fwrite(&y, sizeof(y), 1, tp->out);
	fwrite(&n, sizeof(n), 1, tp->out);
	fwrite(&a, sizeof(a), 1, tp->out);
	fwrite(s, n, 1, tp->out);
	fflush(tp->out);

	return (0);
}


static void Term_init_pipe_emx(term *t)
{
	termPipe *tp = (termPipe*)t;

	if (tp->out)
	{
		fputc(PIP_INIT, tp->out);
		fflush(tp->out);
	}
}


static void Term_nuke_pipe_emx(term *t)
{
	termPipe *tp = (termPipe*)t;

	if (tp->out)
	{
		fputc(PIP_NUKE, tp->out);  /* Terminate client */
		fflush(tp->out);
		fclose(tp->out);          /* Close Pipe */
		tp->out = NULL;             /* Paranoia */
	}
}

static void initPipeTerm(termPipe *pipe, const char *name, term **termTarget)
{
	term *t;

	t = &pipe->t;

	if ((pipe->out = initPipe(name)) != NULL)
	{
		/* Initialize the term */
		term_init(t, 80, 24, 1);

		/* Special hooks */
		t->init_hook = Term_init_pipe_emx;
		t->nuke_hook = Term_nuke_pipe_emx;

		/* Add the hooks */
		t->text_hook = Term_text_pipe_emx;
		t->wipe_hook = Term_wipe_pipe_emx;
		t->curs_hook = Term_curs_pipe_emx;
		t->xtra_hook = Term_xtra_pipe_emx;

		/* Save it */
		*termTarget = t;

		/* Activate it */
		Term_activate(t);
	}
}

/*
 * Prepare "term.c" to use "USE_EMX" built-in video library
 */
errr init_emx(void)
{
	int i;

	term *t;

	/* Initialize the pipe windows */
	for (i = MAX_TERM_DATA - 1; i > 0; --i)
	{
		const char *name = angband_term_name[i];
		initPipeTerm(&term_screen_aga[i], name, &angband_term[i]);
	}

	/* Initialize main window */
	t = &term_screen_aga[0].t;

	/* Initialize the term -- big key buffer */
	term_init(t, 80, 24, 1024);

	/* Special hooks */
	t->init_hook = Term_init_emx;
	t->nuke_hook = Term_nuke_emx;

	/* Add the hooks */
	t->text_hook = Term_text_emx;
	t->wipe_hook = Term_wipe_emx;
	t->curs_hook = Term_curs_emx;
	t->xtra_hook = Term_xtra_emx;

	/* Save it */
	angband_term[0] = t;

	/* Activate it */
	Term_activate(t);

	/* Success */
	return (0);
}

static FILE *initPipe(const char *name)
{
	char buf[256];
	FILE *fi;

	sprintf(buf, "\\pipe\\angband\\%s", name);    /* Name of pipe */
	fi = fopen(buf, "wb");                         /* Look for server */
	return fi;
}

#else /* __EMX__CLIENT__ */

int main(int argc, char **argv)
{
	int c, end = 0, lines = 25;
	int x, y, h, n, v;

	FILE *in = NULL;
	char a;
	char buf[160];
	HPIPE pipe;
	APIRET rc;
	char *target;

	/* Check command line */
	if (argc != 2 && argc != 3)
	{
		printf("Usage: %s Mirror|Recall|Choice|Term-4|...|Term-7 [number of lines]\n"
		       "Start this before angband.exe\n", argv[0]);
		exit(1);
	}

	if (argc == 3) lines = atoi(argv[2]);
	if (lines <= 0) lines = 25;

	printf("Looking for Angband... press ^C to abort\n");

	target = strdup(argv[1]);
	for (c = 0; c < strlen(target); c++) target[c] = tolower(target[c]);

	sprintf(buf, "\\pipe\\angband\\%s", target);

	do
	{
		rc = DosCreateNPipe((PSZ)buf,           /* Create pipe */
		                    &pipe,
		                    NP_ACCESS_INBOUND,
		                    NP_WAIT | NP_TYPE_BYTE | NP_READMODE_BYTE | 1,
		                    1,                  /* No output buffer */
		                    1,                  /* No input buffer */
		                    -1);

		if (rc)                              /* Pipe not created */
		{
			printf("DosCreateNPipe: rc=%ld, pipe=%ld\n", (long)rc, (long)pipe);
			break;
		}

		do
		{
			rc = DosConnectNPipe(pipe);         /* Wait for angband to connect */
			if (!rc) break;
			_sleep2(500);                     /* Sleep for 0.5s  */
		}
		while (_read_kbd(0, 0, 0) == -1);       /* Until key pressed */

		if (rc) break;

		h = _imphandle(pipe);                   /* Register handle with io */
		setmode(h, O_BINARY);                  /* Make it binary */
		in = fdopen(h, "rb");                    /* Register handle with stdio */

	}
	while (0);            /* We don't need no stinking exception handling <g> */

	if (!in)
	{
		printf("Sorry, the pipe connection to Angband could not be established.\n");
		exit(1);
	}

	printf("Connected.\n");

	sprintf(buf, "mode co80,%d", lines);
	system(buf);

	/* Infinite loop */
	while (!end)
	{
		/* Get command */
		c = fgetc(in);

		switch (c)
		{
		case PIP_XTRA:
			if (!fread(&n, sizeof(x), 1, in) ||
			                !fread(&v, sizeof(y), 1, in))
				abort();

			/* This hack prevents another hack */
			switch (n)
			{
			case TERM_XTRA_CLEAR:
				v_clear();
				break;

			default:
				printf("Sorry, angband.exe and aclient.exe don't fit together.\n");
				exit(1);
			}

			break;

		case PIP_CURS:
			if (!fread(&x, sizeof(x), 1, in) ||
			                !fread(&y, sizeof(y), 1, in))
				abort();
			Term_curs_emx(x, y);
			break;

		case PIP_WIPE:
			if (!fread(&x, sizeof(x), 1, in) ||
			                !fread(&y, sizeof(y), 1, in) ||
			                !fread(&n, sizeof(n), 1, in))
				abort();
			Term_wipe_emx(x, y, n);
			break;

		case PIP_TEXT:
			if (!fread(&x, sizeof(x), 1, in) ||
			                !fread(&y, sizeof(y), 1, in) ||
			                !fread(&n, sizeof(n), 1, in) ||
			                !fread(&a, sizeof(a), 1, in) || (n > 160) ||
			                !fread(buf, n, 1, in))
				abort();
			Term_text_emx(x, y, n, a, buf);
			break;

		case PIP_INIT:
			Term_init_emx(NULL);
			break;

		case PIP_NUKE:
		case EOF:
		default:
			Term_nuke_emx(NULL);
			end = 1;
			break;
		}
	}

	return 0;
}

#endif /* __EMX__CLIENT__ */

#else /* EMXPM */

void emx_endPM(const char *reason);
int emx_options(char **ANGBAND_DIR_USER,
                char **ANGBAND_DIR_SAVE,
                char **ANGBAND_DIR_INFO,
                char *arg_force_roguelike,
                char *arg_force_original,
                char *arg_fiddle,
                char *arg_wizard,
                char player_name[32]);

void emx_init_window(void **instance, void *main_instance, int n);

errr emx_curs(void *instance, int x, int y);
errr emx_wipe(void *instance, int x, int y, int n);
errr emx_text(void *instance, int x, int y, int n, unsigned char a, cptr s);
void emx_init(void *instance);
void emx_nuke(void *instance);
int emx_read_kbd(void *instance, int wait);
void emx_clear(void *instance);
void emx_hidecursor(void *instance);
void emx_showcursor(void *instance);

/*
* termWindow* is sometimes cast to term* and vice versa,
* so "term t;" must be the first line
*/

typedef struct
{
	term t;
	void *instance;                           /* Pointer to window */
}
termWindow;

/*
* Display a cursor, on top of a given attr/char
*/
static errr Term_curs_emx(int x, int y)
{
	return emx_curs(((termWindow*)Term)->instance, x, y);
}

/*
* Erase a grid of space (as if spaces were printed)
*/
static errr Term_wipe_emx(int x, int y, int n)
{
	return emx_wipe(((termWindow*)Term)->instance, x, y, n);
}

/*
* Draw some text, wiping behind it first
*/
static errr Term_text_emx(int x, int y, int n, unsigned char a, cptr s)
{
	return emx_text(((termWindow*)Term)->instance, x, y, n, a, s);
}

/*
* EMX initialization
*/
static void Term_init_emx(term *t)
{
	return emx_init(((termWindow*)t)->instance);
}

/*
* EMX shutdown
*/
static void Term_nuke_emx(term *t)
{}

/*
* Oh no, more prototypes!
*/
static errr CheckEvents(int returnImmediately);

/*
* Main initialization function
*/
errr init_emx(void);

/*
* The screens
*/
static termWindow term_screen_aga[MAX_TERM_DATA];

/*
* Check for events -- called by "Term_scan_emx()"
*/
static errr CheckEvents(int returnImmediately)
{
	/* Get key - Macro triggers are generated by emx_read_kbd() */
	int k = emx_read_kbd(((termWindow*)Term)->instance, returnImmediately ? 0 : 1);

	/* Nothing ready */
	if (k < 0) return (1);

	/* Enqueue the key */
	Term_keypress(k);

	/* Success */
	return (0);
}

/*
* Do a special thing (beep, flush, etc)
*/
static errr Term_xtra_emx(int n, int v)
{
	void *instance = ((termWindow*)Term)->instance;

	switch (n)
	{
	case TERM_XTRA_SHAPE:
		if (v)
		{
			emx_showcursor(instance);
		}
		else
		{
			emx_hidecursor(instance);
		}
		return (0);

	case TERM_XTRA_NOISE:
		DosBeep(440, 50);
		return (0);

	case TERM_XTRA_FLUSH:
		while (!CheckEvents(TRUE));
		return 0;

	case TERM_XTRA_EVENT:
		return (CheckEvents(!v));

	case TERM_XTRA_CLEAR:
		emx_clear(instance);
		return (0);

	case TERM_XTRA_DELAY:
		if (v > 0) _sleep2(v);
		return (0);
	}

	return (1);
}

void emx_init_term(termWindow *t, void *main_instance, term **angTerm, int n)
{
	term *te = (term*)t;

	/* Initialize window */
	emx_init_window(&t->instance, main_instance, n);

	*angTerm = te;

	/* Initialize the term -- big key buffer */
	term_init(te, 80, 24, 1024);

	/* Special hooks */
	te->init_hook = Term_init_emx;
	te->nuke_hook = Term_nuke_emx;

	/* Add the hooks */
	te->text_hook = Term_text_emx;
	te->wipe_hook = Term_wipe_emx;
	te->curs_hook = Term_curs_emx;
	te->xtra_hook = Term_xtra_emx;
}

/*
* Prepare "term.c" to use "USE_EMX" built-in faked video library
*/
errr init_emx(void)
{
	int i;

	/* Initialize the windows */
	emx_init_term(&term_screen_aga[0], NULL, &angband_term[0], 0);

	for (i = 1; i < MAX_TERM_DATA; ++i)
	{
		emx_init_term(&term_screen_aga[i], term_screen_aga[0].instance, &angband_term[i], i);
	}

	/* Activate main window */
	Term_activate(angband_term[0]);

	/* Success */
	return (0);
}

static void init_stuff(void)
{
	char path[1024];
	cptr tail;

	/* Get the environment variable */
	tail = getenv("ANGBAND_PATH");

	/* Use the angband_path, or a default */
	strcpy(path, tail ? tail : DEFAULT_PATH);

	/* Hack -- Add a path separator (only if needed) */
	if (!suffix(path, PATH_SEP)) strcat(path, PATH_SEP);

	/* Initialize */
	init_file_paths(path);
}

static void quit_hook(cptr s)
{
	int i;

	for (i = MAX_TERM_DATA - 1; i >= 0; --i)
	{
		/* Shut down the term windows */
		if (angband_term[i])
		{
			term_nuke(angband_term[i]);
			emx_nuke(((termWindow*)angband_term[i])->instance);
		}
	}

	/* Shut down window system - doesn't return */
	emx_endPM(s);
}


void angbandThread(void *arg)
{
	bool new_game = FALSE;

	int show_score = 0;

	char player_name_aga[32];

	/* Save the "program name" */
	argv0 = (char*)arg;

	/* Use the "main-emx.c" support */
	init_emx();
	ANGBAND_SYS = "ibm";

	/* Get the file paths */
	init_stuff();

	if (!emx_options((char**)&ANGBAND_DIR_USER,
	                 (char**)&ANGBAND_DIR_SAVE,
	                 (char**)&ANGBAND_DIR_INFO,
	                 &arg_force_roguelike,
	                 &arg_force_original,
	                 &arg_fiddle,
	                 &arg_wizard,
	                 player_name_aga)) quit(NULL);

	/* XXX XXX XXX (?) */
	strcpy(player_name, player_name_aga);

	/* Process the player name */
	process_player_name(TRUE);

	/* Tell "quit()" to call "Term_nuke()" */
	quit_aux = quit_hook;

	/* If requested, display scores and quit */
	if (show_score > 0) display_scores(0, show_score);

	/* Catch nasty signals */
	signals_init();

	/* Initialize */
	init_angband();

	/* Wait for response */
	pause_line(23);

	/* Play the game */
	play_game(new_game);

	/* Quit */
	quit(NULL);
}

#endif /* EMXPM */

#endif /* USE_EMX */

/*
 * Local Variables:
 * comment-column: 45
 * End:
 *
 */