1 |
matthewc |
38 |
/* |
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 |
matthewc |
39 |
#include <limits.h> |
27 |
matthewc |
38 |
#include "rdesktop.h" |
28 |
|
|
|
29 |
|
|
#define KEYMAP_SIZE 4096 |
30 |
|
|
#define KEYMAP_MASK (KEYMAP_SIZE - 1) |
31 |
|
|
|
32 |
matthewc |
50 |
extern Display *display; |
33 |
matthewc |
38 |
extern char keymapname[16]; |
34 |
|
|
extern int keylayout; |
35 |
|
|
|
36 |
|
|
static uint8 keymap[KEYMAP_SIZE]; |
37 |
|
|
static unsigned int min_keycode; |
38 |
|
|
|
39 |
astrand |
64 |
static BOOL |
40 |
|
|
xkeymap_read(char *mapname) |
41 |
matthewc |
38 |
{ |
42 |
|
|
FILE *fp; |
43 |
matthewc |
39 |
char line[PATH_MAX], path[PATH_MAX]; |
44 |
matthewc |
38 |
char *keyname, *p; |
45 |
|
|
KeySym keysym; |
46 |
|
|
unsigned char keycode; |
47 |
|
|
|
48 |
|
|
strcpy(path, KEYMAP_PATH); |
49 |
|
|
strncat(path, mapname, sizeof(path) - sizeof(KEYMAP_PATH)); |
50 |
|
|
|
51 |
|
|
fp = fopen(path, "r"); |
52 |
|
|
if (fp == NULL) |
53 |
|
|
{ |
54 |
|
|
error("Failed to open keymap %s\n", path); |
55 |
|
|
return False; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
while (fgets(line, sizeof(line), fp) != NULL) |
59 |
|
|
{ |
60 |
|
|
p = strchr(line, '\n'); |
61 |
|
|
if (p != NULL) |
62 |
|
|
*p = 0; |
63 |
|
|
|
64 |
|
|
keycode = strtol(line, &keyname, 16); |
65 |
|
|
if ((keycode != 0) && (*keyname == ' ')) |
66 |
|
|
{ |
67 |
|
|
do |
68 |
|
|
{ |
69 |
|
|
keyname++; |
70 |
|
|
p = strchr(keyname, ' '); |
71 |
|
|
if (p != NULL) |
72 |
|
|
*p = 0; |
73 |
|
|
|
74 |
|
|
keysym = XStringToKeysym(keyname); |
75 |
|
|
if (keysym == NoSymbol) |
76 |
astrand |
64 |
error("Bad keysym %s in keymap %s\n", |
77 |
|
|
keyname, mapname); |
78 |
matthewc |
38 |
|
79 |
|
|
keymap[keysym & KEYMAP_MASK] = keycode; |
80 |
|
|
keyname = p; |
81 |
|
|
|
82 |
astrand |
64 |
} |
83 |
|
|
while (keyname != NULL); |
84 |
matthewc |
38 |
} |
85 |
|
|
else if (strncmp(line, "include ", 8) == 0) |
86 |
|
|
{ |
87 |
astrand |
64 |
if (!xkeymap_read(line + 8)) |
88 |
matthewc |
38 |
return False; |
89 |
|
|
} |
90 |
|
|
else if (strncmp(line, "map ", 4) == 0) |
91 |
|
|
{ |
92 |
astrand |
64 |
keylayout = strtol(line + 4, NULL, 16); |
93 |
matthewc |
38 |
} |
94 |
|
|
else if (line[0] != '#') |
95 |
|
|
{ |
96 |
|
|
error("Malformed line in keymap %s\n", mapname); |
97 |
|
|
} |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
fclose(fp); |
101 |
|
|
return True; |
102 |
|
|
} |
103 |
|
|
|
104 |
astrand |
64 |
void |
105 |
|
|
xkeymap_init(void) |
106 |
matthewc |
38 |
{ |
107 |
|
|
unsigned int max_keycode; |
108 |
|
|
|
109 |
|
|
XDisplayKeycodes(display, &min_keycode, &max_keycode); |
110 |
|
|
|
111 |
|
|
if (strcmp(keymapname, "none")) |
112 |
|
|
xkeymap_read(keymapname); |
113 |
|
|
} |
114 |
|
|
|
115 |
astrand |
64 |
uint8 |
116 |
|
|
xkeymap_translate_key(unsigned int keysym, unsigned int keycode, |
117 |
|
|
uint16 * flags) |
118 |
matthewc |
38 |
{ |
119 |
|
|
uint8 scancode; |
120 |
|
|
|
121 |
|
|
scancode = keymap[keysym & KEYMAP_MASK]; |
122 |
|
|
if (scancode != 0) |
123 |
matthewc |
50 |
{ |
124 |
|
|
if (scancode & 0x80) |
125 |
|
|
*flags |= KBD_FLAG_EXT; |
126 |
matthewc |
38 |
|
127 |
matthewc |
50 |
return (scancode & 0x7f); |
128 |
|
|
} |
129 |
|
|
|
130 |
matthewc |
38 |
/* not in keymap, try to interpret the raw scancode */ |
131 |
|
|
|
132 |
|
|
if ((keycode >= min_keycode) && (keycode <= 0x60)) |
133 |
astrand |
64 |
return (uint8) (keycode - min_keycode); |
134 |
matthewc |
38 |
|
135 |
matthewc |
50 |
*flags |= KBD_FLAG_EXT; |
136 |
|
|
|
137 |
matthewc |
38 |
switch (keycode) |
138 |
|
|
{ |
139 |
|
|
case 0x61: /* home */ |
140 |
matthewc |
50 |
return 0x47; |
141 |
matthewc |
38 |
case 0x62: /* up arrow */ |
142 |
matthewc |
50 |
return 0x48; |
143 |
matthewc |
38 |
case 0x63: /* page up */ |
144 |
matthewc |
50 |
return 0x49; |
145 |
matthewc |
38 |
case 0x64: /* left arrow */ |
146 |
matthewc |
50 |
return 0x4b; |
147 |
matthewc |
38 |
case 0x66: /* right arrow */ |
148 |
matthewc |
50 |
return 0x4d; |
149 |
matthewc |
38 |
case 0x67: /* end */ |
150 |
matthewc |
50 |
return 0x4f; |
151 |
matthewc |
38 |
case 0x68: /* down arrow */ |
152 |
matthewc |
50 |
return 0x50; |
153 |
matthewc |
38 |
case 0x69: /* page down */ |
154 |
matthewc |
50 |
return 0x51; |
155 |
matthewc |
38 |
case 0x6a: /* insert */ |
156 |
matthewc |
50 |
return 0x52; |
157 |
matthewc |
38 |
case 0x6b: /* delete */ |
158 |
matthewc |
50 |
return 0x53; |
159 |
matthewc |
38 |
case 0x6c: /* keypad enter */ |
160 |
matthewc |
50 |
return 0x1c; |
161 |
matthewc |
38 |
case 0x6d: /* right ctrl */ |
162 |
matthewc |
50 |
return 0x1d; |
163 |
matthewc |
38 |
case 0x6f: /* ctrl - print screen */ |
164 |
matthewc |
50 |
return 0x37; |
165 |
matthewc |
38 |
case 0x70: /* keypad '/' */ |
166 |
matthewc |
50 |
return 0x35; |
167 |
matthewc |
38 |
case 0x71: /* right alt */ |
168 |
matthewc |
50 |
return 0x38; |
169 |
matthewc |
38 |
case 0x72: /* ctrl break */ |
170 |
matthewc |
50 |
return 0x46; |
171 |
matthewc |
38 |
case 0x73: /* left window key */ |
172 |
matthewc |
50 |
return 0x5b; |
173 |
matthewc |
38 |
case 0x74: /* right window key */ |
174 |
matthewc |
50 |
return 0x5c; |
175 |
matthewc |
38 |
case 0x75: /* menu key */ |
176 |
matthewc |
50 |
return 0x5d; |
177 |
matthewc |
38 |
} |
178 |
|
|
|
179 |
|
|
return 0; |
180 |
|
|
} |
181 |
|
|
|
182 |
astrand |
64 |
uint16 |
183 |
|
|
xkeymap_translate_button(unsigned int button) |
184 |
matthewc |
38 |
{ |
185 |
|
|
switch (button) |
186 |
|
|
{ |
187 |
astrand |
64 |
case Button1: /* left */ |
188 |
matthewc |
38 |
return MOUSE_FLAG_BUTTON1; |
189 |
astrand |
64 |
case Button2: /* middle */ |
190 |
matthewc |
38 |
return MOUSE_FLAG_BUTTON3; |
191 |
astrand |
64 |
case Button3: /* right */ |
192 |
matthewc |
38 |
return MOUSE_FLAG_BUTTON2; |
193 |
astrand |
64 |
case Button4: /* wheel up */ |
194 |
jsorg71 |
56 |
return MOUSE_FLAG_BUTTON4; |
195 |
astrand |
64 |
case Button5: /* wheel down */ |
196 |
jsorg71 |
56 |
return MOUSE_FLAG_BUTTON5; |
197 |
matthewc |
38 |
} |
198 |
|
|
|
199 |
|
|
return 0; |
200 |
|
|
} |