summaryrefslogtreecommitdiffstats
path: root/keymap.c
blob: 882dbe3447c6548b6a83ffded0c8b6837abc80e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// This file comes from the vncviewer source
// Converted to C from C++ and used in -fromwin version of x2x
// 2003 By Mark Hayter

//  Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
//
//  This file is part of the VNC system.
//
//  The VNC system is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
//  USA.
//
// If the source code for the VNC system is not available from the place 
// whence you received this file, check http://www.uk.research.att.com/vnc or contact
// the authors on vnc@uk.research.att.com for information on obtaining it.

// Thanks to Martin C. Mueller <mcm@itwm.uni-kl.de> for assistance with the
// international keyboard mapping stuff.

// KeyMap.cpp

#ifdef WIN_2_X
#include <stdio.h>
#include <windows.h>
#define XK_MISCELLANY
#define XK_LATIN1
#define XK_XKB_KEYS
#include "keymap.h"

// Define the keymap structure
typedef struct vncKeymapping_struct {
  unsigned int wincode;
  KeySym Xcode;
} vncKeymapping;

static const vncKeymapping keymap[] = {
	{VK_BACK,		XK_BackSpace},
	{VK_TAB,		XK_Tab},
	{VK_CLEAR,		XK_Clear},
	{VK_RETURN,		XK_Return},
	{VK_LSHIFT,		XK_Shift_L},
	{VK_RSHIFT,		XK_Shift_L}, /* XXX mdh - never gen SHIFT_R */
	{VK_SHIFT,      XK_Shift_L},
	{VK_LCONTROL,	XK_Control_L},
	{VK_RCONTROL,	XK_Control_R},
    {VK_CONTROL,	XK_Control_L},
    {VK_LMENU,		XK_Alt_L},
    {VK_RMENU,		XK_Alt_R},
    {VK_MENU,		XK_Alt_L},
    {VK_PAUSE,		XK_Pause},
    {VK_CAPITAL,	XK_Caps_Lock},
    {VK_ESCAPE,		XK_Escape},
    {VK_SPACE,		XK_space},
    {VK_PRIOR,		XK_Page_Up},
    {VK_NEXT,		XK_Page_Down},
    {VK_END,		XK_End},
    {VK_HOME,		XK_Home},
    {VK_LEFT,		XK_Left},
    {VK_UP,			XK_Up},
    {VK_RIGHT,		XK_Right},
    {VK_DOWN,		XK_Down},
    {VK_SELECT,		XK_Select},
    {VK_EXECUTE,	XK_Execute},
    {VK_SNAPSHOT,	XK_Print},
    {VK_INSERT,		XK_Insert},
    {VK_DELETE,		XK_Delete},
    {VK_HELP,		XK_Help},
    {VK_NUMPAD0,	XK_KP_0},
    {VK_NUMPAD1,	XK_KP_1},
    {VK_NUMPAD2,	XK_KP_2},
    {VK_NUMPAD3,	XK_KP_3},
    {VK_NUMPAD4,	XK_KP_4},
    {VK_NUMPAD5,	XK_KP_5},
    {VK_NUMPAD6,	XK_KP_6},
    {VK_NUMPAD7,	XK_KP_7},
    {VK_NUMPAD8,	XK_KP_8},
    {VK_NUMPAD9,	XK_KP_9},
    {VK_MULTIPLY,	XK_KP_Multiply},
    {VK_ADD,		XK_KP_Add},
    {VK_SEPARATOR,	XK_KP_Separator},   // often comma
    {VK_SUBTRACT,	XK_KP_Subtract},
    {VK_DECIMAL,	XK_KP_Decimal},
    {VK_DIVIDE,		XK_KP_Divide},
    {VK_F1,			XK_F1},
    {VK_F2,			XK_F2},
    {VK_F3,			XK_F3},
    {VK_F4,			XK_F4},
    {VK_F5,			XK_F5},
    {VK_F6,			XK_F6},
    {VK_F7,			XK_F7},
    {VK_F8,			XK_F8},
    {VK_F9,			XK_F9},
    {VK_F10,		XK_F10},
    {VK_F11,		XK_F11},
    {VK_F12,		XK_F12},
    {VK_F13,		XK_F13},
    {VK_F14,		XK_F14},
    {VK_F15,		XK_F15},
    {VK_F16,		XK_F16},
    {VK_F17,		XK_F17},
    {VK_F18,		XK_F18},
    {VK_F19,		XK_F19},
    {VK_F20,		XK_F20},
    {VK_F21,		XK_F21},
    {VK_F22,		XK_F22},
    {VK_F23,		XK_F23},
    {VK_F24,		XK_F24},
    {VK_NUMLOCK,	XK_Num_Lock},
    {VK_SCROLL,		XK_Scroll_Lock}
};

static unsigned char buf[4];
static unsigned char keystate[256];

KeyActionSpec PCtoX(UINT virtkey, DWORD keyData) { 
  int numkeys = 0;
  int mapsize, i;
  int extended;
  int ret;

  KeyActionSpec kas;
  kas.releaseModifiers = 0;

  extended = ((keyData & 0x1000000) != 0);
#ifdef DEBUG
  printf(" keyData %04x ", (unsigned int) keyData);
#endif

  if (extended) { 
#ifdef DEBUG
    printf(" (extended) ");
#endif
    switch (virtkey) {
    case VK_MENU :
      virtkey = VK_RMENU;
      break;
    case VK_CONTROL:
      virtkey = VK_RCONTROL;
      break;
    }
  }
    
  // We try looking it up in our table
  mapsize = sizeof(keymap) / sizeof(vncKeymapping);
        
  // Look up the desired code in the table
  for (i = 0; i < mapsize; i++) {
    if (keymap[i].wincode == virtkey) {
      kas.keycodes[numkeys++] = keymap[i].Xcode;
      break;
    }
  }
    
  if (numkeys != 0) {
#ifdef DEBUG
    UINT key = kas.keycodes[numkeys-1];
    printf("keymap gives %u (%x) ", key, key);
#endif
  } else {
    // not found in table
#ifdef DEBUG
    printf("not in special keymap, ");
#endif
    // we try a simple conversion to Ascii, using the current keyboard mapping
    GetKeyboardState(keystate);

    ret = ToAscii(virtkey, 0, keystate, (WORD *) buf, 0);

    // If Left Ctrl & Alt both pressed and ToAscii gives a valid keysym
    // This is for AltGr on international keyboards  (= LCtrl-Alt).
    // e.g. Ctrl-Alt-Q gives @ on German keyboards
    if (  ((keystate[VK_MENU] & 0x80) != 0) &&
	  ((keystate[VK_CONTROL] & 0x80) != 0) ) {

      // If the key means anything in this keyboard layout
      if  ( (ret >= 1) && 
	    ( ( (*buf >= 32) && (*buf <= 126) ) ||
	      ( (*buf >= 160) && (*buf <= 255) ) ) 
	    ) {

	// Send the modifiers up, then the keystroke, then mods down
	// We don't release the right control; this allows German users
	// to use it for doing Ctl-@ etc. (though not under Win95 --
	// see below)

	if (GetKeyState(VK_LCONTROL) & 0x8000) 
	  kas.releaseModifiers |= KEYMAP_LCONTROL;

	if (GetKeyState(VK_LMENU)    & 0x8000) 
	  kas.releaseModifiers |= KEYMAP_LALT;

	if (GetKeyState(VK_RMENU)    & 0x8000) 
	  kas.releaseModifiers |= KEYMAP_RALT;

	// This is for windows 95, and possibly other systems.  
	// The above GetKeyState calls don't work in 95 - they always return 0.
	// But if we're here at all we know that control and alt are pressed, so let's
	// raise all Control and Alt keys if we haven't registered any yet.
	if (kas.releaseModifiers == 0)
	  kas.releaseModifiers = KEYMAP_LCONTROL | KEYMAP_LALT | KEYMAP_RALT;

#ifdef DEBUG
	printf("Ctrl-Alt pressed ToAscii(without modifiers)returns %d bytes: ",
	       ret);
#endif
	for (i = 0; i < ret; i++) {
	  kas.keycodes[numkeys++] = *(buf+i);
#ifdef DEBUG
	  printf("%02x (%c) ", *(buf+i) , *(buf+i));
#endif
	}
#ifdef DEBUG
	printf("\n");
#endif
      } 

    } 
        
    // If not a ctrl-alt key
    if (numkeys == 0) {

      // There are no keysyms corresponding to control characters 
      // Eg Ctrl F.  The server already knows whether the control 
      // key is pressed. So we are interested in the key that would be 
      // there if the Ctrl were not pressed.
      keystate[VK_CONTROL] = keystate[VK_LCONTROL] = keystate[VK_RCONTROL] = 0;

      ret = ToAscii(virtkey, 0, keystate, (WORD *) buf, 0);
      if (ret < 0) {
	switch (*buf) {
	case '`' :
	  kas.keycodes[numkeys++] = XK_dead_grave; 
	  break;
	case '\'' :
	  kas.keycodes[numkeys++] = XK_dead_acute; 
	  break;
	case '~' :
	  kas.keycodes[numkeys++] = XK_dead_tilde;
	  break;
	case '^':
	  kas.keycodes[numkeys++] = XK_dead_circumflex;
	  break;
	}
      }
      // if this works, and it's a regular printable character, just send that
      if (ret >= 1) {
#ifdef DEBUG
	printf("ToAscii (without ctrl) returns %d byte(s): ", ret);
#endif
	for (i = 0; i < ret; i++) {
	  kas.keycodes[numkeys++] = *(buf+i);
#ifdef DEBUG
	  printf("%02x (%c) ", *(buf+i) , *(buf+i));
#endif
	}
      }
    }
  }

  kas.keycodes[numkeys] = VoidKeyCode;
  return kas;
}

#endif