Make Ogresmasher grant 25 constitution
[aNetHack.git] / sys / os2 / os2.c
blob83798968eb2bedf1fac145409d9361abdaaf4bf3
1 /* NetHack 3.6 os2.c $NHDT-Date: 1432512793 2015/05/25 00:13:13 $ $NHDT-Branch: master $:$NHDT-Revision: 1.9 $ */
2 /* Copyright (c) Timo Hakulinen, 1990, 1991, 1992, 1993, 1996. */
3 /* NetHack may be freely redistributed. See license for details. */
5 /*
6 * OS/2 system functions.
7 */
9 #define NEED_VARARGS
10 #include "hack.h"
12 #ifdef OS2
14 #include "tcap.h"
16 /* OS/2 system definitions */
18 #ifdef __EMX__
19 #undef CLR_BLACK
20 #undef CLR_WHITE
21 #undef CLR_BLUE
22 #undef CLR_RED
23 #undef CLR_GREEN
24 #undef CLR_CYAN
25 #undef CLR_YELLOW
26 #undef CLR_BROWN
27 #endif
29 #include "def_os2.h"
31 #include <ctype.h>
33 static char NDECL(DOSgetch);
34 static char NDECL(BIOSgetch);
36 int
37 tgetch()
39 char ch;
41 /* BIOSgetch can use the numeric key pad on IBM compatibles. */
42 if (iflags.BIOS)
43 ch = BIOSgetch();
44 else
45 ch = DOSgetch();
46 return ((ch == '\r') ? '\n' : ch);
50 * Keyboard translation tables.
52 #define KEYPADLO 0x47
53 #define KEYPADHI 0x53
55 #define PADKEYS (KEYPADHI - KEYPADLO + 1)
56 #define iskeypad(x) (KEYPADLO <= (x) && (x) <= KEYPADHI)
59 * Keypad keys are translated to the normal values below.
60 * When iflags.BIOS is active, shifted keypad keys are translated to the
61 * shift values below.
63 static const struct pad {
64 char normal, shift, cntrl;
65 } keypad[PADKEYS] =
67 { 'y', 'Y', C('y') }, /* 7 */
68 { 'k', 'K', C('k') }, /* 8 */
69 { 'u', 'U', C('u') }, /* 9 */
70 { 'm', C('p'), C('p') }, /* - */
71 { 'h', 'H', C('h') }, /* 4 */
72 { 'g', 'g', 'g' }, /* 5 */
73 { 'l', 'L', C('l') }, /* 6 */
74 { 'p', 'P', C('p') }, /* + */
75 { 'b', 'B', C('b') }, /* 1 */
76 { 'j', 'J', C('j') }, /* 2 */
77 { 'n', 'N', C('n') }, /* 3 */
78 { 'i', 'I', C('i') }, /* Ins */
79 { '.', ':', ':' } /* Del */
81 numpad[PADKEYS] = {
82 { '7', M('7'), '7' }, /* 7 */
83 { '8', M('8'), '8' }, /* 8 */
84 { '9', M('9'), '9' }, /* 9 */
85 { 'm', C('p'), C('p') }, /* - */
86 { '4', M('4'), '4' }, /* 4 */
87 { 'g', 'G', 'g' }, /* 5 */
88 { '6', M('6'), '6' }, /* 6 */
89 { 'p', 'P', C('p') }, /* + */
90 { '1', M('1'), '1' }, /* 1 */
91 { '2', M('2'), '2' }, /* 2 */
92 { '3', M('3'), '3' }, /* 3 */
93 { 'i', 'I', C('i') }, /* Ins */
94 { '.', ':', ':' } /* Del */
98 * Unlike Ctrl-letter, the Alt-letter keystrokes have no specific ASCII
99 * meaning unless assigned one by a keyboard conversion table, so the
100 * keyboard BIOS normally does not return a character code when Alt-letter
101 * is pressed. So, to interpret unassigned Alt-letters, we must use a
102 * scan code table to translate the scan code into a letter, then set the
103 * "meta" bit for it. -3.
105 #define SCANLO 0x10
106 #define SCANHI 0x32
107 #define SCANKEYS (SCANHI - SCANLO + 1)
108 #define inmap(x) (SCANLO <= (x) && (x) <= SCANHI)
110 static const char scanmap[SCANKEYS] = {
111 /* ... */
112 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', 0, 'a',
113 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0, '\\', 'z', 'x',
114 'c', 'v', 'b', 'N', 'm' /* ... */
118 * BIOSgetch emulates the MSDOS way of getting keys directly with a BIOS call.
120 #define SHIFT_KEY (0x1 | 0x2)
121 #define CTRL_KEY 0x4
122 #define ALT_KEY 0x8
124 static char
125 BIOSgetch()
127 unsigned char scan, shift, ch;
128 const struct pad *kpad;
130 KBDKEYINFO CharData;
131 USHORT IOWait = 0;
132 HKBD KbdHandle = 0;
134 KbdCharIn(&CharData, IOWait, KbdHandle);
135 ch = CharData.chChar;
136 scan = CharData.chScan;
137 shift = CharData.fsState;
139 /* Translate keypad keys */
140 if (iskeypad(scan)) {
141 kpad = iflags.num_pad ? numpad : keypad;
142 if (shift & SHIFT_KEY)
143 ch = kpad[scan - KEYPADLO].shift;
144 else if (shift & CTRL_KEY)
145 ch = kpad[scan - KEYPADLO].cntrl;
146 else
147 ch = kpad[scan - KEYPADLO].normal;
149 /* Translate unassigned Alt-letters */
150 if ((shift & ALT_KEY) && !ch) {
151 if (inmap(scan))
152 ch = scanmap[scan - SCANLO];
153 return (isprint(ch) ? M(ch) : ch);
155 return ch;
158 static char
159 DOSgetch()
161 KBDKEYINFO CharData;
162 USHORT IOWait = 0;
163 HKBD KbdHandle = 0;
165 KbdCharIn(&CharData, IOWait, KbdHandle);
166 if (CharData.chChar == 0) { /* an extended code -- not yet supported */
167 KbdCharIn(&CharData, IOWait, KbdHandle); /* eat the next character */
168 CharData.chChar = 0; /* and return a 0 */
170 return (CharData.chChar);
173 char
174 switchar()
176 return '/';
180 kbhit()
182 KBDKEYINFO CharData;
183 HKBD KbdHandle = 0;
185 KbdPeek(&CharData, KbdHandle);
186 return (CharData.fbStatus & (1 << 6));
189 long
190 freediskspace(path)
191 char *path;
193 FSALLOCATE FSInfoBuf;
194 #ifdef OS2_32BITAPI
195 ULONG
196 #else
197 USHORT
198 #endif
199 DriveNumber, FSInfoLevel = 1, res;
201 if (path[0] && path[1] == ':')
202 DriveNumber = (toupper(path[0]) - 'A') + 1;
203 else
204 DriveNumber = 0;
205 res =
206 #ifdef OS2_32BITAPI
207 DosQueryFSInfo(DriveNumber, FSInfoLevel, (PVOID) &FSInfoBuf,
208 (ULONG) sizeof(FSInfoBuf));
209 #else
210 DosQFSInfo(DriveNumber, FSInfoLevel, (PBYTE) &FSInfoBuf,
211 (USHORT) sizeof(FSInfoBuf));
212 #endif
213 if (res)
214 return -1L; /* error */
215 else
216 return ((long) FSInfoBuf.cSectorUnit * FSInfoBuf.cUnitAvail
217 * FSInfoBuf.cbSector);
221 * Functions to get filenames using wildcards
224 #ifdef OS2_32BITAPI
225 static FILEFINDBUF3 ResultBuf;
226 #else
227 static FILEFINDBUF ResultBuf;
228 #endif
229 static HDIR DirHandle;
232 findfirst(path)
233 char *path;
235 #ifdef OS2_32BITAPI
236 ULONG
237 #else
238 USHORT
239 #endif
240 res, SearchCount = 1;
242 DirHandle = 1;
243 res =
244 #ifdef OS2_32BITAPI
245 DosFindFirst((PSZ) path, &DirHandle, 0L, (PVOID) &ResultBuf,
246 (ULONG) sizeof(ResultBuf), &SearchCount, 1L);
247 #else
248 DosFindFirst((PSZ) path, &DirHandle, 0, &ResultBuf,
249 (USHORT) sizeof(ResultBuf), &SearchCount, 0L);
250 #endif
251 return (!res);
255 findnext()
257 #ifdef OS2_32BITAPI
258 ULONG
259 #else
260 USHORT
261 #endif
262 res, SearchCount = 1;
264 res =
265 #ifdef OS2_32BITAPI
266 DosFindNext(DirHandle, (PVOID) &ResultBuf, (ULONG) sizeof(ResultBuf),
267 &SearchCount);
268 #else
269 DosFindNext(DirHandle, &ResultBuf, (USHORT) sizeof(ResultBuf),
270 &SearchCount);
271 #endif
272 return (!res);
275 char *
276 foundfile_buffer()
278 return (ResultBuf.achName);
281 long
282 filesize(file)
283 char *file;
285 if (findfirst(file)) {
286 return (*(long *) (ResultBuf.cbFileAlloc));
287 } else
288 return -1L;
292 * Chdrive() changes the default drive.
294 void
295 chdrive(str)
296 char *str;
298 char *ptr;
299 char drive;
301 if ((ptr = index(str, ':')) != (char *) 0) {
302 drive = toupper(*(ptr - 1));
303 #ifdef OS2_32BITAPI
304 DosSetDefaultDisk((ULONG)(drive - 'A' + 1));
305 #else
306 DosSelectDisk((USHORT)(drive - 'A' + 1));
307 #endif
311 void
312 disable_ctrlP()
314 KBDINFO KbdInfo;
315 HKBD KbdHandle = 0;
317 if (!iflags.rawio)
318 return;
319 KbdInfo.cb = sizeof(KbdInfo);
320 KbdGetStatus(&KbdInfo, KbdHandle);
321 KbdInfo.fsMask &= 0xFFF7; /* ASCII off */
322 KbdInfo.fsMask |= 0x0004; /* BINARY on */
323 KbdSetStatus(&KbdInfo, KbdHandle);
326 void
327 enable_ctrlP()
329 KBDINFO KbdInfo;
330 HKBD KbdHandle = 0;
332 if (!iflags.rawio)
333 return;
334 KbdInfo.cb = sizeof(KbdInfo);
335 KbdGetStatus(&KbdInfo, KbdHandle);
336 KbdInfo.fsMask &= 0xFFFB; /* BINARY off */
337 KbdInfo.fsMask |= 0x0008; /* ASCII on */
338 KbdSetStatus(&KbdInfo, KbdHandle);
341 void
342 get_scr_size()
344 VIOMODEINFO ModeInfo;
345 HVIO VideoHandle = 0;
347 ModeInfo.cb = sizeof(ModeInfo);
349 (void) VioGetMode(&ModeInfo, VideoHandle);
351 CO = ModeInfo.col;
352 LI = ModeInfo.row;
355 void
356 gotoxy(x, y)
357 int x, y;
359 HVIO VideoHandle = 0;
361 x--;
362 y--; /* (0,0) is upper right corner */
364 (void) VioSetCurPos(x, y, VideoHandle);
367 char *
368 get_username(lan_username_size)
369 int *lan_username_size;
371 return (char *) 0;
373 #ifdef X11_GRAPHICS
374 int errno;
375 #endif
376 #endif /* OS2 */