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