| 1 | /* keyboard.c -- Read characters from the keyboard
|
|---|
| 2 | Copyright (c) 1994-1995 by Eberhard Mattes
|
|---|
| 3 |
|
|---|
| 4 | This file is part of emx.
|
|---|
| 5 |
|
|---|
| 6 | emx is free software; you can redistribute it and/or modify it
|
|---|
| 7 | under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | emx is distributed in the hope that it will be useful,
|
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 14 | GNU General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with emx; see the file COPYING. If not, write to
|
|---|
| 18 | the Free Software Foundation, 59 Temple Place - Suite 330,
|
|---|
| 19 | Boston, MA 02111-1307, USA.
|
|---|
| 20 |
|
|---|
| 21 | As special exception, emx.dll can be distributed without source code
|
|---|
| 22 | unless it has been changed. If you modify emx.dll, this exception
|
|---|
| 23 | no longer applies and you must remove this paragraph from all source
|
|---|
| 24 | files for emx.dll. */
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 | #define INCL_KBD
|
|---|
| 28 | #include <os2emx.h>
|
|---|
| 29 | #include "emxdll.h"
|
|---|
| 30 | #include "clib.h"
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | static UCHAR kbd_input_more;
|
|---|
| 34 | static UCHAR kbd_input_next;
|
|---|
| 35 | static KBDINFO kbd_info; /* Must not cross a 64K boundary */
|
|---|
| 36 | static KBDKEYINFO kbd_inp; /* Must not cross a 64K boundary */
|
|---|
| 37 | static UCHAR cgets_buf[256]; /* Must not cross a 64K boundary */
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | #define MASK1 (KEYBOARD_BINARY_MODE | KEYBOARD_ASCII_MODE)
|
|---|
| 41 | #define MASK2 (MASK1 | KEYBOARD_ECHO_ON | KEYBOARD_ECHO_OFF)
|
|---|
| 42 |
|
|---|
| 43 | int kbd_input (UCHAR *dst, int binary_p, int echo_p, int wait_p,
|
|---|
| 44 | int check_avail_p)
|
|---|
| 45 | {
|
|---|
| 46 | int mask, old_mask = 0, status_changed, result;
|
|---|
| 47 |
|
|---|
| 48 | if (kbd_input_more)
|
|---|
| 49 | {
|
|---|
| 50 | if (check_avail_p)
|
|---|
| 51 | return TRUE;
|
|---|
| 52 | kbd_input_more = FALSE;
|
|---|
| 53 | if (dst != NULL) *dst = kbd_input_next;
|
|---|
| 54 | return TRUE;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | kbd_info.cb = sizeof (kbd_info);
|
|---|
| 58 | status_changed = FALSE;
|
|---|
| 59 | if (KbdGetStatus (&kbd_info, 0) == 0)
|
|---|
| 60 | {
|
|---|
| 61 | old_mask = kbd_info.fsMask;
|
|---|
| 62 | mask = (binary_p ? KEYBOARD_BINARY_MODE : KEYBOARD_ASCII_MODE);
|
|---|
| 63 | if ((old_mask & MASK1) != mask)
|
|---|
| 64 | {
|
|---|
| 65 | status_changed = TRUE;
|
|---|
| 66 | kbd_info.fsMask = (kbd_info.fsMask & ~MASK2) | mask;
|
|---|
| 67 | kbd_info.cb = sizeof (kbd_info);
|
|---|
| 68 | KbdSetStatus (&kbd_info, 0);
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | do
|
|---|
| 73 | {
|
|---|
| 74 | if (check_avail_p)
|
|---|
| 75 | KbdPeek (&kbd_inp, 0);
|
|---|
| 76 | else
|
|---|
| 77 | KbdCharIn (&kbd_inp, (wait_p ? IO_WAIT : IO_NOWAIT), 0);
|
|---|
| 78 | break;
|
|---|
| 79 | } while (!(check_avail_p || !wait_p
|
|---|
| 80 | || (kbd_inp.fbStatus & KBDTRF_FINAL_CHAR_IN)));
|
|---|
| 81 |
|
|---|
| 82 | if (kbd_inp.fbStatus & KBDTRF_FINAL_CHAR_IN)
|
|---|
| 83 | {
|
|---|
| 84 | result = TRUE;
|
|---|
| 85 | if ((kbd_inp.chChar == 0 || kbd_inp.chChar == 0xe0)
|
|---|
| 86 | && (kbd_inp.fbStatus & KBDTRF_EXTENDED_CODE))
|
|---|
| 87 | {
|
|---|
| 88 | kbd_inp.chChar = 0; /* Override 0xe0 */
|
|---|
| 89 | kbd_input_next = kbd_inp.chScan;
|
|---|
| 90 | kbd_input_more = TRUE;
|
|---|
| 91 | }
|
|---|
| 92 | else if (echo_p)
|
|---|
| 93 | {
|
|---|
| 94 | UCHAR c;
|
|---|
| 95 | ULONG nwritten;
|
|---|
| 96 |
|
|---|
| 97 | c = kbd_inp.chChar;
|
|---|
| 98 | DosWrite (1, &c, 1, &nwritten);
|
|---|
| 99 | }
|
|---|
| 100 | if (dst != NULL) *dst = kbd_inp.chChar;
|
|---|
| 101 | }
|
|---|
| 102 | else
|
|---|
| 103 | result = FALSE;
|
|---|
| 104 |
|
|---|
| 105 | if (status_changed)
|
|---|
| 106 | {
|
|---|
| 107 | kbd_info.cb = sizeof (kbd_info);
|
|---|
| 108 | if (KbdGetStatus (&kbd_info, 0) == 0)
|
|---|
| 109 | {
|
|---|
| 110 | kbd_info.fsMask = (kbd_info.fsMask & ~MASK2) | (old_mask & MASK1);
|
|---|
| 111 | kbd_info.cb = sizeof (kbd_info);
|
|---|
| 112 | KbdSetStatus (&kbd_info, 0);
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | return result;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 | void conin_string (UCHAR *buf)
|
|---|
| 121 | {
|
|---|
| 122 | int i, buf_len;
|
|---|
| 123 | STRINGINBUF sib;
|
|---|
| 124 | USHORT rc;
|
|---|
| 125 |
|
|---|
| 126 | buf_len = buf[0];
|
|---|
| 127 | memcpy (cgets_buf, buf + 2, buf_len);
|
|---|
| 128 | for (i = 0; i < buf_len; ++i)
|
|---|
| 129 | if (cgets_buf[i] == 0 || cgets_buf[i] == '\r')
|
|---|
| 130 | break;
|
|---|
| 131 | if (i >= buf_len)
|
|---|
| 132 | i = 0;
|
|---|
| 133 | cgets_buf[i] = '\r';
|
|---|
| 134 | sib.cchIn = (USHORT)i;
|
|---|
| 135 | sib.cb = (USHORT)buf_len;
|
|---|
| 136 | rc = KbdStringIn (cgets_buf, &sib, IO_WAIT, 0);
|
|---|
| 137 | i = (rc == 0 ? sib.cchIn : 0);
|
|---|
| 138 | buf[1] = (UCHAR)i;
|
|---|
| 139 | memcpy (buf + 2, cgets_buf, buf_len);
|
|---|
| 140 | buf[i + 2] = '\r';
|
|---|
| 141 | }
|
|---|