blob: ee5b85eb44932fd44e54e442ba7962a98a38bddc [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19#include <map>
20
21#define XK_MISCELLANY
22#define XK_LATIN1
23#define XK_CURRENCY
24#include <rfb/keysymdef.h>
25
26#include <rfb_win32/CKeyboard.h>
27#include <rfb/LogWriter.h>
28#include <rfb_win32/OSVersion.h>
29#include "keymap.h"
30
31using namespace rfb;
32
33static LogWriter vlog("CKeyboard");
34
35
36// Client-side RFB keyboard event sythesis
37
38class CKeymapper {
39
40public:
41 CKeymapper()
42 {
Peter Åstrand6ac5b732010-02-10 07:22:55 +000043 for (unsigned int i = 0; i < sizeof(keymap) / sizeof(keymap_t); i++) {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000044 int extendedVkey = keymap[i].vk + (keymap[i].extended ? 256 : 0);
45 if (keysymMap.find(extendedVkey) == keysymMap.end()) {
46 keysymMap[extendedVkey] = keymap[i].keysym;
47 }
48 }
49 }
50
51 // lookup() tries to find a match for vkey with the extended flag. We check
52 // first for an exact match including the extended flag, then try without the
53 // extended flag.
54 rdr::U32 lookup(int extendedVkey) {
Peter Åstrand6978fc02009-01-15 11:51:39 +000055
56 // There's no real definition of the meaning of
57 // VK_SEPARATOR/XK_KP_Separator or VK_DECIMAL/XK_KP_Decimal. As
58 // http://blogs.msdn.com/michkap/archive/2006/09/13/752377.aspx
59 // puts it: "As for what is actually assigned to VK_DECIMAL, that
60 // is something that for every keyboard is either defined in a
61 // standard or decided by the person/people who submitted the
62 // keyboard layout. It may match the locale's preferences or it
63 // may not". In a VNC context, we are considering a SEPARATOR to
64 // be a comma and a DECIMAL to be a dot.
65 if (extendedVkey == VK_DECIMAL || extendedVkey == VK_SEPARATOR) {
66 char buf[4];
Adam Tkac8517ea52009-10-08 11:49:12 +000067 if (!GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, (LPSTR) buf, sizeof(buf))) {
Peter Åstrand6978fc02009-01-15 11:51:39 +000068 vlog.debug("failed to retrieve LOCALE_SDECIMAL");
69 } else {
70 switch (buf[0]) {
71 case ',':
72 extendedVkey = VK_SEPARATOR;
73 break;
74 case '.':
75 extendedVkey = VK_DECIMAL;
76 break;
77 }
78 }
79 }
80
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000081 if (keysymMap.find(extendedVkey) != keysymMap.end())
82 return keysymMap[extendedVkey];
83 if (keysymMap.find(extendedVkey ^ 256) != keysymMap.end())
84 return keysymMap[extendedVkey ^ 256];
85 return 0;
86 }
87
88private:
89 std::map<int,rdr::U32> keysymMap;
90} ckeymapper;
91
92
93class ModifierKeyReleaser {
94public:
95 ModifierKeyReleaser(InputHandler* writer_, int vkCode, bool extended)
96 : writer(writer_), extendedVkey(vkCode + (extended ? 256 : 0)),
97 keysym(0)
98 {}
99 void release(std::map<int,rdr::U32>* downKeysym) {
100 if (downKeysym->find(extendedVkey) != downKeysym->end()) {
101 keysym = (*downKeysym)[extendedVkey];
102 vlog.debug("fake release extendedVkey 0x%x, keysym 0x%x",
103 extendedVkey, keysym);
104 writer->keyEvent(keysym, false);
105 }
106 }
107 ~ModifierKeyReleaser() {
108 if (keysym) {
109 vlog.debug("fake press extendedVkey 0x%x, keysym 0x%x",
110 extendedVkey, keysym);
111 writer->keyEvent(keysym, true);
112 }
113 }
114 InputHandler* writer;
115 int extendedVkey;
116 rdr::U32 keysym;
117};
118
119// IS_PRINTABLE_LATIN1 tests if a character is either a printable latin1
120// character, or 128, which is the Euro symbol on Windows.
121#define IS_PRINTABLE_LATIN1(c) (((c) >= 32 && (c) <= 126) || (c) == 128 || \
122 ((c) >= 160 && (c) <= 255))
123
124void win32::CKeyboard::keyEvent(InputHandler* writer, rdr::U8 vkey,
125 rdr::U32 flags, bool down)
126{
127 bool extended = (flags & 0x1000000);
128 int extendedVkey = vkey + (extended ? 256 : 0);
129
130 // If it's a release, just release whichever keysym corresponded to the same
131 // key being pressed, regardless of how it would be interpreted in the
132 // current keyboard state.
133 if (!down) {
134 releaseKey(writer, extendedVkey);
135 return;
136 }
137
138 // We should always pass every down event to ToAscii() otherwise it can get
139 // out of sync.
140
141 // XXX should we pass CapsLock, ScrollLock or NumLock to ToAscii - they
142 // actually alter the lock state on the keyboard?
143
144 BYTE keystate[256];
145 GetKeyboardState(keystate);
146 rdr::U8 chars[2];
147
148 int nchars = ToAscii(vkey, 0, keystate, (WORD*)&chars, 0);
149
150 // See if it's in the Windows VK code -> X keysym map. We do this before
151 // looking at the result of ToAscii so that e.g. we recognise that it's
152 // XK_KP_Add rather than '+'.
153
154 rdr::U32 keysym = ckeymapper.lookup(extendedVkey);
155 if (keysym) {
156 vlog.debug("mapped key: extendedVkey 0x%x", extendedVkey);
157 pressKey(writer, extendedVkey, keysym);
158 return;
159 }
160
161 if (nchars < 0) {
162 // Dead key - the next call to ToAscii() will give us either the accented
163 // character or two characters.
164 vlog.debug("ToAscii dead key (1): extendedVkey 0x%x", extendedVkey);
165 return;
166 }
167
168 if (nchars > 0 && IS_PRINTABLE_LATIN1(chars[0])) {
169 // Got a printable latin1 character. We must release Control and Alt
170 // (AltGr) if they were both pressed, so that the latin1 character is seen
171 // without them by the VNC server.
172 ModifierKeyReleaser lctrl(writer, VK_CONTROL, 0);
173 ModifierKeyReleaser rctrl(writer, VK_CONTROL, 1);
174 ModifierKeyReleaser lalt(writer, VK_MENU, 0);
175 ModifierKeyReleaser ralt(writer, VK_MENU, 1);
176
177 if ((keystate[VK_CONTROL] & 0x80) && (keystate[VK_MENU] & 0x80)) {
178 lctrl.release(&downKeysym);
179 rctrl.release(&downKeysym);
180 lalt.release(&downKeysym);
181 ralt.release(&downKeysym);
182 }
183
184 for (int i = 0; i < nchars; i++) {
185 vlog.debug("ToAscii key (1): extendedVkey 0x%x", extendedVkey);
186 if (chars[i] == 128) { // special hack for euro!
187 pressKey(writer, extendedVkey, XK_EuroSign);
188 } else {
189 pressKey(writer, extendedVkey, chars[i]);
190 }
191 }
192 return;
193 }
194
195 // Either no chars were generated, or something outside the printable
196 // character range. Try ToAscii() without the Control and Alt keys down to
197 // see if that yields an ordinary character.
198
199 keystate[VK_CONTROL] = keystate[VK_LCONTROL] = keystate[VK_RCONTROL] = 0;
200 keystate[VK_MENU] = keystate[VK_LMENU] = keystate[VK_RMENU] = 0;
201
202 nchars = ToAscii(vkey, 0, keystate, (WORD*)&chars, 0);
203
204 if (nchars < 0) {
205 // So it would be a dead key if neither control nor alt were pressed.
206 // However, we know that at least one of control and alt must be pressed.
207 // We can't leave it at this stage otherwise the next call to ToAscii()
208 // with a valid character will get wrongly interpreted in the context of
209 // this bogus dead key. Working on the assumption that a dead key followed
210 // by space usually returns the dead character itself, try calling ToAscii
211 // with VK_SPACE.
212 vlog.debug("ToAscii dead key (2): extendedVkey 0x%x", extendedVkey);
213 nchars = ToAscii(VK_SPACE, 0, keystate, (WORD*)&chars, 0);
214 if (nchars < 0) {
215 vlog.debug("ToAscii dead key (3): extendedVkey 0x%x - giving up!",
216 extendedVkey);
217 return;
218 }
219 }
220
221 if (nchars > 0 && IS_PRINTABLE_LATIN1(chars[0])) {
222 for (int i = 0; i < nchars; i++) {
223 vlog.debug("ToAscii key (2) (no ctrl/alt): extendedVkey 0x%x",
224 extendedVkey);
225 if (chars[i] == 128) { // special hack for euro!
226 pressKey(writer, extendedVkey, XK_EuroSign);
227 } else {
228 pressKey(writer, extendedVkey, chars[i]);
229 }
230 }
231 return;
232 }
233
234 vlog.debug("no chars regardless of control and alt: extendedVkey 0x%x",
235 extendedVkey);
236}
237
238// releaseAllKeys() - write key release events to the server for all keys
239// that are currently regarded as being down.
240void win32::CKeyboard::releaseAllKeys(InputHandler* writer) {
241 std::map<int,rdr::U32>::iterator i, next_i;
242 for (i=downKeysym.begin(); i!=downKeysym.end(); i=next_i) {
243 next_i = i; next_i++;
244 writer->keyEvent((*i).second, false);
245 downKeysym.erase(i);
246 }
247}
248
249// releaseKey() - write a key up event to the server, but only if we've
250// actually sent a key down event for the given key. The key up event always
251// contains the same keysym we used in the key down event, regardless of what
252// it would look up as using the current keyboard state.
253void win32::CKeyboard::releaseKey(InputHandler* writer, int extendedVkey)
254{
255 if (downKeysym.find(extendedVkey) != downKeysym.end()) {
256 vlog.debug("release extendedVkey 0x%x, keysym 0x%x",
257 extendedVkey, downKeysym[extendedVkey]);
258 writer->keyEvent(downKeysym[extendedVkey], false);
259 downKeysym.erase(extendedVkey);
260 }
261}
262
263// pressKey() - write a key down event to the server, and record which keysym
264// was sent as corresponding to the given extendedVkey. The only tricky bit is
265// that if we are trying to press an extendedVkey which is already marked as
266// down but with a different keysym, then we need to release the old keysym
267// first. This can happen in two cases: (a) when a single key press results in
268// more than one character, and (b) when shift is released while another key is
269// autorepeating.
270void win32::CKeyboard::pressKey(InputHandler* writer, int extendedVkey,
271 rdr::U32 keysym)
272{
273 if (downKeysym.find(extendedVkey) != downKeysym.end()) {
274 if (downKeysym[extendedVkey] != keysym) {
275 vlog.debug("release extendedVkey 0x%x, keysym 0x%x",
276 extendedVkey, downKeysym[extendedVkey]);
277 writer->keyEvent(downKeysym[extendedVkey], false);
278 }
279 }
280 vlog.debug("press extendedVkey 0x%x, keysym 0x%x",
281 extendedVkey, keysym);
282 writer->keyEvent(keysym, true);
283 downKeysym[extendedVkey] = keysym;
284}