source: vendor/emx/current/src/os2/keyboard.c

Last change on this file was 18, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 3.8 KB
Line 
1/* keyboard.c -- Read characters from the keyboard
2 Copyright (c) 1994-1995 by Eberhard Mattes
3
4This file is part of emx.
5
6emx is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11emx is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with emx; see the file COPYING. If not, write to
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA.
20
21As special exception, emx.dll can be distributed without source code
22unless it has been changed. If you modify emx.dll, this exception
23no longer applies and you must remove this paragraph from all source
24files for emx.dll. */
25
26
27#define INCL_KBD
28#include <os2emx.h>
29#include "emxdll.h"
30#include "clib.h"
31
32
33static UCHAR kbd_input_more;
34static UCHAR kbd_input_next;
35static KBDINFO kbd_info; /* Must not cross a 64K boundary */
36static KBDKEYINFO kbd_inp; /* Must not cross a 64K boundary */
37static 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
43int 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
120void 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}
Note: See TracBrowser for help on using the repository browser.