1 /*****************************************************************************/
2 /* */
3 /* KBD.CC */
4 /* */
5 /* (C) 1996 Ullrich von Bassewitz */
6 /* Wacholderweg 14 */
7 /* D-70597 Stuttgart */
8 /* EMail: uz@ibb.schwaben.com */
9 /* */
10 /*****************************************************************************/
11
12
13
14 // $Id$
15 //
16 // $Log$
17 //
18 //
19
20
21
22 #include <process.h>
23 #include <conio.h>
24
25 #include "msgid.h"
26 #include "program.h"
27 #include "kbd.h"
28
29
30
31 /*****************************************************************************/
32 /* Data */
33 /*****************************************************************************/
34
35
36
37 // The one and only keyboard instance
38 Keyboard* Kbd;
39
40
41
42 // Mapper table from virtual to extended keys. This table is fixed for DOS/OS2
43 static struct {
44 Key EK;
45 Key VK;
46 } VirtualMap [] = {
47 { kbEsc, vkAbort },
48 { kbF1, vkHelp },
49 { kbF10, vkAccept },
50 { kbPgUp, vkPgUp },
51 { kbPgDn, vkPgDn },
52 { kbCtrlPgUp, vkCtrlPgUp },
53 { kbCtrlPgDn, vkCtrlPgDn },
54 { kbUp, vkUp },
55 { kbDown, vkDown },
56 { kbLeft, vkLeft },
57 { kbRight, vkRight },
58 { kbIns, vkIns },
59 { kbDel, vkDel },
60 { kbHome, vkHome },
61 { kbEnd, vkEnd },
62 { kbCtrlUp, vkCtrlUp },
63 { kbCtrlDown, vkCtrlDown },
64 { kbCtrlLeft, vkCtrlLeft },
65 { kbCtrlRight, vkCtrlRight },
66 { kbCtrlIns, vkCtrlIns },
67 { kbCtrlDel, vkCtrlDel },
68 { kbCtrlHome, vkCtrlHome },
69 { kbCtrlEnd, vkCtrlEnd },
70 { kbF5, vkZoom },
71 { kbMetaF3, vkClose },
72 { kbF3, vkOpen },
73 { kbF2, vkSave },
74 { kbCtrlF5, vkResize },
75 { kbMetaX, vkQuit },
76
77 // Secondary mappings follow
78 { kbCtrlR, vkPgUp },
79 { kbCtrlC, vkPgDn },
80 { kbCtrlE, vkUp },
81 { kbCtrlX, vkDown },
82 { kbCtrlS, vkLeft },
83 { kbCtrlD, vkRight },
84 { kbCtrlV, vkIns },
85 { kbCtrlG, vkDel },
86 { kbCtrlW, vkCtrlUp },
87 { kbCtrlZ, vkCtrlDown },
88 { kbCtrlA, vkCtrlLeft },
89 { kbCtrlF, vkCtrlRight },
90
91 };
92
93
94
95 /*****************************************************************************/
96 /* Code */
97 /*****************************************************************************/
98
99
100
KbdCharAvail()101 static int KbdCharAvail ()
102 // Return true if a char is available from the os
103 {
104 return kbhit ();
105 }
106
107
108
KbdMapExtended(Key K)109 static Key KbdMapExtended (Key K)
110 // Map an extended key to a virtual key. Return the virtual key if a map
111 // exists, otherwise return the key unchanged.
112 {
113 for (int I = 0; I < sizeof (VirtualMap) / sizeof (VirtualMap [0]); I++) {
114 if (VirtualMap [I].EK == K) {
115 return VirtualMap [I].VK;
116 }
117 }
118 return K;
119 }
120
121
122
KbdMapVirtual(Key K)123 static Key KbdMapVirtual (Key K)
124 // Map a virtual key to an extended key. Return the extended key if a map
125 // exists, otherwise return the key unchanged.
126 {
127 for (int I = 0; I < sizeof (VirtualMap) / sizeof (VirtualMap [0]); I++) {
128 if (VirtualMap [I].VK == K) {
129 return VirtualMap [I].EK;
130 }
131 }
132 return K;
133 }
134
135
136
137 /*****************************************************************************/
138 /* Class Keyboard */
139 /*****************************************************************************/
140
141
142
Keyboard()143 Keyboard::Keyboard ():
144 Console (1),
145 TransTable (NULL)
146 {
147 }
148
149
150
~Keyboard()151 Keyboard::~Keyboard ()
152 {
153 delete [] TransTable;
154 }
155
156
157
RawKey()158 Key Keyboard::RawKey ()
159 // Get a raw (unmapped) key from the os
160 {
161 // Wait for a key calling repeatedly App->Idle ()
162 while (KbdCharAvail () == 0) {
163
164 // Allow idle processing
165 CurThread () -> Idle ();
166
167 // Allow other threads to run
168 ThreadSwitch ();
169 }
170
171 // Get the key
172 Key K = getch ();
173 if (K == 0) {
174 // Extended key
175 return (0x0100 | (Key) getch ());
176 } else {
177 return (K & 0x00FF);
178 }
179 }
180
181
182
GetMappedKey(int Wait)183 void Keyboard::GetMappedKey (int Wait)
184 // Read keys until the needed key is not found in the mapper or until
185 // a valid sequence is found. If Wait is zero, return immediately if
186 // no match is found and no more keys are available.
187 {
188 // If waiting is not wanted, check if there are keys available before
189 // grabbing them
190 if (Wait == 0 && KbdCharAvail () == 0) {
191 // No keys available, bail out
192 return;
193 }
194
195 // Get a key, remap it and put it into the buffer
196 KeyBuf.Put (KbdMapExtended (Translate (RawKey ())));
197 }
198
199
200
GetKeyName(Key K)201 String Keyboard::GetKeyName (Key K)
202 // Return a string describing the give key
203 {
204 if (IsVirtualKey (K)) {
205 // It is a virtual key, remap it to it's extended form
206 K = KbdMapVirtual (K);
207 }
208
209 // Now return the key description
210 return App->LoadMsg (MSGBASE_KBD + K);
211 }
212
213
214