home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / nethack-3.1 / sys / os2 / os2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-18  |  6.7 KB  |  345 lines

  1. /*    SCCS Id: @(#)os2.c    3.1    93/01/18 */
  2. /*    Copyright (c) Timo Hakulinen, 1990, 1991, 1992, 1993. */
  3. /*    NetHack may be freely redistributed.  See license for details. */
  4.  
  5. /*
  6.  *  OS/2 system functions.
  7.  */
  8.  
  9. #define NEED_VARARGS
  10. #include "hack.h"
  11.  
  12. #ifdef OS2
  13.  
  14. #include "termcap.h"
  15.  
  16. /* OS/2 system definitions */
  17.  
  18. #include "def_os2.h"
  19.  
  20. #include <ctype.h>
  21.  
  22. static char NDECL(DOSgetch);
  23. static char NDECL(BIOSgetch);
  24.  
  25. int
  26. tgetch()
  27. {
  28.     char ch;
  29.  
  30.     /* BIOSgetch can use the numeric key pad on IBM compatibles. */
  31.     if (flags.BIOS)
  32.         ch = BIOSgetch();
  33.     else
  34.         ch = DOSgetch();
  35.     return ((ch == '\r') ? '\n' : ch);
  36. }
  37.  
  38. /*
  39.  *  Keyboard translation tables.
  40.  */
  41. #define KEYPADLO    0x47
  42. #define KEYPADHI    0x53
  43.  
  44. #define PADKEYS     (KEYPADHI - KEYPADLO + 1)
  45. #define iskeypad(x)    (KEYPADLO <= (x) && (x) <= KEYPADHI)
  46.  
  47. /*
  48.  * Keypad keys are translated to the normal values below.
  49.  * When flags.BIOS is active, shifted keypad keys are translated to the
  50.  *    shift values below.
  51.  */
  52. static const struct pad {
  53.     char normal, shift, cntrl;
  54. } keypad[PADKEYS] = {
  55.             {'y', 'Y', C('y')},        /* 7 */
  56.             {'k', 'K', C('k')},        /* 8 */
  57.             {'u', 'U', C('u')},        /* 9 */
  58.             {'m', C('p'), C('p')},        /* - */
  59.             {'h', 'H', C('h')},        /* 4 */
  60.             {'g', 'g', 'g'},        /* 5 */
  61.             {'l', 'L', C('l')},        /* 6 */
  62.             {'p', 'P', C('p')},        /* + */
  63.             {'b', 'B', C('b')},        /* 1 */
  64.             {'j', 'J', C('j')},        /* 2 */
  65.             {'n', 'N', C('n')},        /* 3 */
  66.             {'i', 'I', C('i')},        /* Ins */
  67.             {'.', ':', ':'}            /* Del */
  68. }, numpad[PADKEYS] = {
  69.             {'7', M('7'), '7'},        /* 7 */
  70.             {'8', M('8'), '8'},        /* 8 */
  71.             {'9', M('9'), '9'},        /* 9 */
  72.             {'m', C('p'), C('p')},        /* - */
  73.             {'4', M('4'), '4'},        /* 4 */
  74.             {'g', 'G', 'g'},        /* 5 */
  75.             {'6', M('6'), '6'},        /* 6 */
  76.             {'p', 'P', C('p')},        /* + */
  77.             {'1', M('1'), '1'},        /* 1 */
  78.             {'2', M('2'), '2'},        /* 2 */
  79.             {'3', M('3'), '3'},        /* 3 */
  80.             {'i', 'I', C('i')},        /* Ins */
  81.             {'.', ':', ':'}            /* Del */
  82. };
  83.  
  84. /*
  85.  * Unlike Ctrl-letter, the Alt-letter keystrokes have no specific ASCII
  86.  * meaning unless assigned one by a keyboard conversion table, so the
  87.  * keyboard BIOS normally does not return a character code when Alt-letter
  88.  * is pressed.    So, to interpret unassigned Alt-letters, we must use a
  89.  * scan code table to translate the scan code into a letter, then set the
  90.  * "meta" bit for it.  -3.
  91.  */
  92. #define SCANLO        0x10
  93. #define SCANHI        0x32
  94. #define SCANKEYS    (SCANHI - SCANLO + 1)
  95. #define inmap(x)    (SCANLO <= (x) && (x) <= SCANHI)
  96.  
  97. static const char scanmap[SCANKEYS] = {     /* ... */
  98.     'q','w','e','r','t','y','u','i','o','p','[',']', '\n',
  99.     0, 'a','s','d','f','g','h','j','k','l',';','\'', '`',
  100.     0, '\\', 'z','x','c','v','b','N','m'     /* ... */
  101. };
  102.  
  103. /*
  104.  * BIOSgetch emulates the MSDOS way of getting keys directly with a BIOS call.
  105.  */
  106. #define SHIFT_KEY    (0x1 | 0x2)
  107. #define CTRL_KEY    0x4
  108. #define ALT_KEY        0x8
  109.  
  110. static char
  111. BIOSgetch()
  112. {
  113.     unsigned char scan, shift, ch;
  114.     const struct pad *kpad;
  115.  
  116.     KBDKEYINFO CharData;
  117.     USHORT IOWait = 0;
  118.     HKBD KbdHandle = 0;
  119.  
  120.     KbdCharIn(&CharData,IOWait,KbdHandle);
  121.     ch = CharData.chChar;
  122.     scan = CharData.chScan;
  123.     shift = CharData.fsState;
  124.  
  125.     /* Translate keypad keys */
  126.     if (iskeypad(scan)) {
  127.         kpad = flags.num_pad ? numpad : keypad;
  128.         if (shift & SHIFT_KEY)
  129.             ch = kpad[scan - KEYPADLO].shift;
  130.         else if (shift & CTRL_KEY)
  131.             ch = kpad[scan - KEYPADLO].cntrl;
  132.         else
  133.             ch = kpad[scan - KEYPADLO].normal;
  134.     }
  135.     /* Translate unassigned Alt-letters */
  136.     if ((shift & ALT_KEY) && !ch) {
  137.         if (inmap(scan))
  138.             ch = scanmap[scan - SCANLO];
  139.         return (isprint(ch) ? M(ch) : ch);
  140.     }
  141.     return ch;
  142. }
  143.  
  144. static char
  145. DOSgetch()
  146. {
  147.     KBDKEYINFO CharData;
  148.     USHORT IOWait = 0;
  149.     HKBD KbdHandle = 0;
  150.  
  151.     KbdCharIn(&CharData,IOWait,KbdHandle);
  152.     if (CharData.chChar == 0) {    /* an extended code -- not yet supported */
  153.         KbdCharIn(&CharData,IOWait,KbdHandle);       /* eat the next character */
  154.         CharData.chChar = 0;        /* and return a 0 */
  155.     }
  156.     return (CharData.chChar);
  157. }
  158.  
  159. char
  160. switchar()
  161. {
  162.     return '/';
  163. }
  164.  
  165. int
  166. kbhit()
  167. {
  168.     KBDKEYINFO CharData;
  169.     HKBD KbdHandle = 0;
  170.  
  171.     KbdPeek(&CharData,KbdHandle);
  172.     return (CharData.fbStatus & (1 << 6));
  173. }
  174.  
  175. long
  176. freediskspace(path)
  177. char *path;
  178. {
  179.     FSALLOCATE FSInfoBuf;
  180. #ifdef OS2_32BITAPI
  181.     ULONG
  182. #else
  183.     USHORT
  184. #endif
  185.         DriveNumber, FSInfoLevel = 1, res;
  186.  
  187.     if (path[0] && path[1] == ':')
  188.         DriveNumber = (toupper(path[0]) - 'A') + 1;
  189.     else
  190.         DriveNumber = 0;
  191.     res =
  192. #ifdef OS2_32BITAPI
  193.         DosQueryFSInfo(DriveNumber,FSInfoLevel,(PVOID)&FSInfoBuf,(ULONG)sizeof(FSInfoBuf));
  194. #else
  195.         DosQFSInfo(DriveNumber,FSInfoLevel,(PBYTE)&FSInfoBuf,(USHORT)sizeof(FSInfoBuf));
  196. #endif
  197.     if (res)
  198.         return -1L;        /* error */
  199.     else
  200.         return ((long) FSInfoBuf.cSectorUnit * FSInfoBuf.cUnitAvail *
  201.                    FSInfoBuf.cbSector);
  202. }
  203.  
  204. /*
  205.  * Functions to get filenames using wildcards
  206.  */
  207.  
  208. #ifdef OS2_32BITAPI
  209. static FILEFINDBUF3 ResultBuf;
  210. #else
  211. static FILEFINDBUF ResultBuf;
  212. #endif
  213. static HDIR DirHandle;
  214.  
  215. int
  216. findfirst(path)
  217. char *path;
  218. {
  219. #ifdef OS2_32BITAPI
  220.     ULONG
  221. #else
  222.     USHORT
  223. #endif
  224.         res, SearchCount = 1;
  225.  
  226.     DirHandle = 1;
  227.     res =
  228. #ifdef OS2_32BITAPI
  229.         DosFindFirst((PSZ)path,&DirHandle,0L,(PVOID)&ResultBuf,(ULONG)sizeof(ResultBuf),&SearchCount,1L);
  230. #else
  231.         DosFindFirst((PSZ)path,&DirHandle,0,&ResultBuf,(USHORT)sizeof(ResultBuf),&SearchCount,0L);
  232. #endif
  233.     return(!res);
  234. }
  235.  
  236. int
  237. findnext()
  238. {
  239. #ifdef OS2_32BITAPI
  240.     ULONG
  241. #else
  242.     USHORT
  243. #endif
  244.         res, SearchCount = 1;
  245.  
  246.     res =
  247. #ifdef OS2_32BITAPI
  248.         DosFindNext(DirHandle,(PVOID)&ResultBuf,(ULONG)sizeof(ResultBuf),&SearchCount);
  249. #else
  250.         DosFindNext(DirHandle,&ResultBuf,(USHORT)sizeof(ResultBuf),&SearchCount);
  251. #endif
  252.     return(!res);
  253. }
  254.  
  255. char *
  256. foundfile_buffer()
  257. {
  258.     return(ResultBuf.achName);
  259. }
  260.  
  261. long
  262. filesize(file)
  263. char *file;
  264. {
  265.     if (findfirst(file)) {
  266.         return  (* (long *) (ResultBuf.cbFileAlloc));
  267.     } else
  268.         return -1L;
  269. }
  270.  
  271. /*
  272.  * Chdrive() changes the default drive.
  273.  */
  274. void
  275. chdrive(str)
  276. char *str;
  277. {
  278.     char *ptr;
  279.     char drive;
  280.  
  281.     if ((ptr = index(str, ':')) != NULL) {
  282.         drive = toupper(*(ptr - 1));
  283. #ifdef OS2_32BITAPI
  284.         DosSetDefaultDisk((ULONG)(drive - 'A' + 1));
  285. #else
  286.         DosSelectDisk((USHORT)(drive - 'A' + 1));
  287. #endif
  288.     }
  289. }
  290.  
  291. void
  292. disable_ctrlP()
  293. {
  294.     KBDINFO KbdInfo;
  295.     HKBD KbdHandle = 0;
  296.  
  297.     if (!flags.rawio) return;
  298.     KbdInfo.cb = sizeof(KbdInfo);
  299.     KbdGetStatus(&KbdInfo,KbdHandle);
  300.     KbdInfo.fsMask &= 0xFFF7; /* ASCII off */
  301.     KbdInfo.fsMask |= 0x0004; /* BINARY on */
  302.     KbdSetStatus(&KbdInfo,KbdHandle);
  303. }
  304.  
  305. void
  306. enable_ctrlP()
  307. {
  308.     KBDINFO KbdInfo;
  309.     HKBD KbdHandle = 0;
  310.  
  311.     if (!flags.rawio) return;
  312.     KbdInfo.cb = sizeof(KbdInfo);
  313.     KbdGetStatus(&KbdInfo,KbdHandle);
  314.     KbdInfo.fsMask &= 0xFFFB; /* BINARY off */
  315.     KbdInfo.fsMask |= 0x0008; /* ASCII on */
  316.     KbdSetStatus(&KbdInfo,KbdHandle);
  317. }
  318.  
  319. void
  320. get_scr_size()
  321. {
  322.     VIOMODEINFO ModeInfo;
  323.     HVIO VideoHandle = 0;
  324.  
  325.     ModeInfo.cb = sizeof(ModeInfo);
  326.  
  327.     (void) VioGetMode(&ModeInfo,VideoHandle);
  328.  
  329.     CO = ModeInfo.col;
  330.     LI = ModeInfo.row;
  331. }
  332.  
  333. void
  334. gotoxy(x,y)
  335. int x,y;
  336. {
  337.     HVIO VideoHandle = 0;
  338.  
  339.     x--; y--;            /* (0,0) is upper right corner */
  340.  
  341.     (void) VioSetCurPos(x, y, VideoHandle);
  342. }
  343.  
  344. #endif /* OS2 */
  345.