1 |
/* |
2 |
rdesktop: A Remote Desktop Protocol client. |
3 |
User interface services - X keyboard mapping |
4 |
Copyright (C) Matthew Chapman 1999-2001 |
5 |
|
6 |
This program is free software; you can redistribute it and/or modify |
7 |
it under the terms of the GNU General Public License as published by |
8 |
the Free Software Foundation; either version 2 of the License, or |
9 |
(at your option) any later version. |
10 |
|
11 |
This program 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 this program; if not, write to the Free Software |
18 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
20 |
|
21 |
#include <X11/Xlib.h> |
22 |
#include <X11/keysym.h> |
23 |
#include <stdio.h> |
24 |
#include <stdlib.h> |
25 |
#include <string.h> |
26 |
#include "rdesktop.h" |
27 |
|
28 |
#define KEYMAP_SIZE 4096 |
29 |
#define KEYMAP_MASK (KEYMAP_SIZE - 1) |
30 |
|
31 |
extern char keymapname[16]; |
32 |
extern int keylayout; |
33 |
|
34 |
static uint8 keymap[KEYMAP_SIZE]; |
35 |
static unsigned int min_keycode; |
36 |
|
37 |
static BOOL xkeymap_read(char *mapname) |
38 |
{ |
39 |
FILE *fp; |
40 |
char line[256], path[256]; |
41 |
char *keyname, *p; |
42 |
KeySym keysym; |
43 |
unsigned char keycode; |
44 |
unsigned int mapcode = 0; |
45 |
|
46 |
strcpy(path, KEYMAP_PATH); |
47 |
strncat(path, mapname, sizeof(path) - sizeof(KEYMAP_PATH)); |
48 |
|
49 |
fp = fopen(path, "r"); |
50 |
if (fp == NULL) |
51 |
{ |
52 |
error("Failed to open keymap %s\n", path); |
53 |
return False; |
54 |
} |
55 |
|
56 |
while (fgets(line, sizeof(line), fp) != NULL) |
57 |
{ |
58 |
p = strchr(line, '\n'); |
59 |
if (p != NULL) |
60 |
*p = 0; |
61 |
|
62 |
keycode = strtol(line, &keyname, 16); |
63 |
if ((keycode != 0) && (*keyname == ' ')) |
64 |
{ |
65 |
do |
66 |
{ |
67 |
keyname++; |
68 |
p = strchr(keyname, ' '); |
69 |
if (p != NULL) |
70 |
*p = 0; |
71 |
|
72 |
keysym = XStringToKeysym(keyname); |
73 |
if (keysym == NoSymbol) |
74 |
error("Bad keysym %s in keymap %s\n", keyname, mapname); |
75 |
|
76 |
keymap[keysym & KEYMAP_MASK] = keycode; |
77 |
keyname = p; |
78 |
|
79 |
} while (keyname != NULL); |
80 |
} |
81 |
else if (strncmp(line, "include ", 8) == 0) |
82 |
{ |
83 |
if (!xkeymap_read(line+8)) |
84 |
return False; |
85 |
} |
86 |
else if (strncmp(line, "map ", 4) == 0) |
87 |
{ |
88 |
keylayout = strtol(line+4, NULL, 16); |
89 |
} |
90 |
else if (line[0] != '#') |
91 |
{ |
92 |
error("Malformed line in keymap %s\n", mapname); |
93 |
} |
94 |
} |
95 |
|
96 |
fclose(fp); |
97 |
return True; |
98 |
} |
99 |
|
100 |
void xkeymap_init(Display *display) |
101 |
{ |
102 |
unsigned int max_keycode; |
103 |
|
104 |
XDisplayKeycodes(display, &min_keycode, &max_keycode); |
105 |
|
106 |
if (strcmp(keymapname, "none")) |
107 |
xkeymap_read(keymapname); |
108 |
} |
109 |
|
110 |
uint8 xkeymap_translate_key(KeySym keysym, unsigned int keycode) |
111 |
{ |
112 |
uint8 scancode; |
113 |
|
114 |
scancode = keymap[keysym & KEYMAP_MASK]; |
115 |
if (scancode != 0) |
116 |
return scancode; |
117 |
|
118 |
/* not in keymap, try to interpret the raw scancode */ |
119 |
|
120 |
if ((keycode >= min_keycode) && (keycode <= 0x60)) |
121 |
return (uint8)(keycode - min_keycode); |
122 |
|
123 |
switch (keycode) |
124 |
{ |
125 |
case 0x61: /* home */ |
126 |
return 0x47 | 0x80; |
127 |
case 0x62: /* up arrow */ |
128 |
return 0x48 | 0x80; |
129 |
case 0x63: /* page up */ |
130 |
return 0x49 | 0x80; |
131 |
case 0x64: /* left arrow */ |
132 |
return 0x4b | 0x80; |
133 |
case 0x66: /* right arrow */ |
134 |
return 0x4d | 0x80; |
135 |
case 0x67: /* end */ |
136 |
return 0x4f | 0x80; |
137 |
case 0x68: /* down arrow */ |
138 |
return 0x50 | 0x80; |
139 |
case 0x69: /* page down */ |
140 |
return 0x51 | 0x80; |
141 |
case 0x6a: /* insert */ |
142 |
return 0x52 | 0x80; |
143 |
case 0x6b: /* delete */ |
144 |
return 0x53 | 0x80; |
145 |
case 0x6c: /* keypad enter */ |
146 |
return 0x1c | 0x80; |
147 |
case 0x6d: /* right ctrl */ |
148 |
return 0x1d | 0x80; |
149 |
case 0x6f: /* ctrl - print screen */ |
150 |
return 0x37 | 0x80; |
151 |
case 0x70: /* keypad '/' */ |
152 |
return 0x35 | 0x80; |
153 |
case 0x71: /* right alt */ |
154 |
return 0x38 | 0x80; |
155 |
case 0x72: /* ctrl break */ |
156 |
return 0x46 | 0x80; |
157 |
case 0x73: /* left window key */ |
158 |
return 0x5b | 0x80; |
159 |
case 0x74: /* right window key */ |
160 |
return 0x5c | 0x80; |
161 |
case 0x75: /* menu key */ |
162 |
return 0x5d | 0x80; |
163 |
} |
164 |
|
165 |
return 0; |
166 |
} |
167 |
|
168 |
uint16 xkeymap_translate_button(unsigned int button) |
169 |
{ |
170 |
switch (button) |
171 |
{ |
172 |
case Button1: /* left */ |
173 |
return MOUSE_FLAG_BUTTON1; |
174 |
case Button2: /* middle */ |
175 |
return MOUSE_FLAG_BUTTON3; |
176 |
case Button3: /* right */ |
177 |
return MOUSE_FLAG_BUTTON2; |
178 |
} |
179 |
|
180 |
return 0; |
181 |
} |