Debian Codegraph @

tools/x11/xkeymap.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Utility to create mapping from X keycodes to Allegro scancodes.
12 *
13 * By Michael Bukin, modified by Elias Pschernig.
14 *
15 * See readme.txt for copyright information.
16 */
17
18 #include <stdio.h>
19
20 #include <allegro5.h>
21 #include <xalleg.h>
22 #include <allegro/internal/aintern.h>
23
24 #define FILENAME_LENGTH 1024
25
26 char unicode_description[256] = "";
27 static DIALOG main_dialog[];
28 static DIALOG keymap_dialog[];
29
30 static int black = 0;
31 static int white = 1;
32 static int red = 2;
33 static int yellow = 3;
34
35 static volatile int waiting_for_key = 0;
36 static volatile int new_keycode = 0;
37 static int keycode_to_scancode[256];
38
39 static void get_raw_keycode(int pressed, int keycode)
40 {
41 if (pressed && waiting_for_key) {
42 new_keycode = keycode;
43 waiting_for_key = 0;
44 }
45 }
46
47 static const char *keycode_getter(int index, int *list_size)
48 {
49 if (index >= 0) {
50 return scancode_to_name(index + 1);
51 }
52 else {
53 *list_size = KEY_MAX - 1;
54 return NULL;
55 }
56 }
57
58 static DIALOG keymap_dialog[] = {
59 /* 0 */{d_clear_proc, 0, 0, 250, 230, 0, 0, 0, 0, 0, 0, 0, NULL, NULL},
60 /* 1 */{d_ctext_proc, 125, 10, 0, 16, 0, 255, 0, 0, 0, 0, "Select Keycode:", NULL, NULL},
61 /* 2 */{d_list_proc, 0, 32, 230, 92, 0, 255, 0, D_EXIT, 0, 0, (void *)keycode_getter, NULL, NULL},
62 /* 3 */{d_ctext_proc, 125, 142, 0, 16, 0, 255, 0, 0, 0, 0, unicode_description, NULL, NULL},
63 /* 4 */{d_button_proc, 10, 164, 230, 16, 0, 255, 0, D_EXIT, 0, 0, "Define &X Key !", NULL, NULL},
64 /* 5 */{d_button_proc, 10, 186, 110, 16, 0, 255, 27, D_EXIT, 0, 0, "&Cancel", NULL, NULL},
65 /* 6 */{d_button_proc, 130, 186, 110, 16 , 0, 255, 0, D_EXIT, 0, 0, "&Done", NULL, NULL},
66 /* 7 */{d_ctext_proc, 125, 208, 0, 16, 0, 255, 0, 0, 0, 0, "", NULL, NULL},
67 {d_yield_proc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL},
68 {NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL}
69 };
70
71 /* handle the setup command */
72 static void setup_all_keys(void)
73 {
74 int focus = 2;
75 int i;
76
77 /* Prepare dialog. */
78 set_dialog_color(keymap_dialog, black, white);
79 centre_dialog(keymap_dialog);
80
81 /* Parse input. */
82 while (1) {
83 focus = do_dialog(keymap_dialog, focus);
84 switch (focus) {
85 case 2:
86 case 4:
87
88 textprintf_centre_ex (screen, font,
89 keymap_dialog[7].x, keymap_dialog[7].y, red, -1,
90 "Press a key to map to the current scancode");
91 textprintf_centre_ex (screen, font,
92 keymap_dialog[7].x, keymap_dialog[7].y + 8, red, -1,
93 "(or a mouse button to leave it unchanged)");
94
95 /* Wait for new key press. */
96 new_keycode = -1;
97 waiting_for_key = 1;
98
99 do {
100 poll_keyboard();
101 poll_mouse();
102
103 if (mouse_b)
104 waiting_for_key = 0;
105 }
106 while (waiting_for_key);
107
108 /* Save keycode to scancode mapping. */
109 if ((new_keycode >= 0) && (new_keycode < 256)) {
110 keycode_to_scancode[new_keycode] = keymap_dialog[2].d1 + 1;
111 }
112
113 clear_keybuf();
114
115 break;
116 case 5:
117 return;
118 case 6:
119 for (i = 0; i < 256; i++) {
120 if (keycode_to_scancode[i] >= 0)
121 _xwin.keycode_to_scancode[i] = keycode_to_scancode[i];
122 }
123 return;
124 }
125 }
126 }
127
128 static void test_key_map(void)
129 {
130 int i, k, u;
131 static int key_was_pressed[KEY_MAX + 1] = {0};
132 static int key_is_pressed[KEY_MAX + 1] = {0};
133 static char *welcome[] = {
134 "Key that is pressed now is marked with red",
135 "Key that was pressed is marked with yellow",
136 "Press mouse button or Escape to exit test",
137 0
138 };
139
140 /* Clear screen and output prompt. */
141 clear_to_color(screen, white);
142 for (i = 0; welcome[i] != 0; i++)
143 textout_ex(screen, font, welcome[i], 8, i * 8 + 8, black, -1);
144
145 clear_to_color(screen, white);
146 for (i = 0; i < KEY_MAX; i++)
147 textprintf_ex(screen, font, 32 + (i % 4) * 160,
148 32 + (i / 4) * 14, black, -1, "%s", scancode_to_name (i));
149 do {
150 poll_keyboard();
151 poll_mouse();
152 }
153 while ((key[KEY_ESC]) || (mouse_b));
154
155 do {
156
157 while (keypressed()) {
158 u = ureadkey (&k);
159 textprintf_centre_ex (screen, font, SCREEN_W / 2, 8,
160 red, white, "> %c <", u);
161 }
162
163 poll_keyboard();
164 poll_mouse();
165
166 for (i = 0; i < KEY_MAX; i++) {
167 if (key[i])
168 key_was_pressed[i] = key_is_pressed[i] = 1;
169 else
170 key_is_pressed[i] = 0;
171 }
172
173 for (i = 0; i < KEY_MAX; i++) {
174 int x = 16 + (i % 4) * 160;
175 int y = 32 + (i / 4) * 14;
176
177 if (key_is_pressed[i])
178 rectfill(screen, x, y, x + 7, y + 7, red);
179 else if (key_was_pressed[i])
180 rectfill(screen, x, y, x + 7, y + 7, yellow);
181 else
182 rectfill(screen, x, y, x + 7, y + 7, white);
183 }
184
185 rest(1);
186 }
187 while ((!key[KEY_ESC]) && (!mouse_b));
188
189 do {
190 poll_keyboard();
191 poll_mouse();
192 }
193 while ((key[KEY_ESC]) || (mouse_b));
194
195 clear_keybuf();
196 }
197
198 /* handle the save command */
199 static void save_key_map(void)
200 {
201 int i;
202 char *section, *option_format, option[80], tmp1[80], tmp2[80];
203
204 set_config_file("xkeymap.cfg");
205 section = uconvert_ascii("xkeymap", tmp1);
206 option_format = uconvert_ascii("keycode%d", tmp2);
207
208 for (i = 0; i < 256; i++) {
209 if (keycode_to_scancode[i] > 0) {
210 uszprintf(option, sizeof(option), option_format, i);
211 set_config_int(section, option, keycode_to_scancode[i]);
212 }
213 }
214 }
215
216 /* reads a specific keymapping table from the config file */
217 void load_table(unsigned short *table, char *section)
218 {
219 char name[80];
220 int i;
221
222 for (i=0; i<KEY_MAX; i++) {
223 sprintf(name, "key%d", i);
224 table[i] = get_config_int(section, name, table[i]);
225 }
226 }
227
228 static DIALOG main_dialog[] = {
229 /* 0 */{d_clear_proc, 0, 0, 250, 102, 0, 0, 0, 0, 0, 0, 0, NULL, NULL},
230 /* 1 */{d_ctext_proc, 120, 10, 0, 16, 0, 255, 0, 0, 0, 0, "X Key Mapping Utility", NULL, NULL},
231 /* 2 */{d_ctext_proc, 10, 32, 230, 16, 0, 255, 0, D_EXIT, 0, 0, "~", NULL, NULL},
232 /* 3 */{d_button_proc, 10, 54, 230, 16, 0, 255, 'm', D_EXIT, 0, 0, "Setup X key &mappings", NULL, NULL},
233 /* 4 */{d_button_proc, 10, 76, 230, 16, 0, 255, 't', D_EXIT, 0, 0, "&Test X key mappings", NULL, NULL},
234 /* 5 */{d_button_proc, 10, 98, 230, 16, 0, 255, 's', D_EXIT, 0, 0, "&Save and exit", NULL, NULL},
235 /* 6 */{d_button_proc, 10, 120, 230, 16, 0, 255, 27, D_EXIT, 0, 0, "E&xit", NULL, NULL},
236 {d_yield_proc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL},
237 {NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL}
238 };
239
240 static void show_main_dialog(void)
241 {
242 int focus = 1;
243
244 /* Prepare dialog. */
245 set_dialog_color(main_dialog, black, white);
246 centre_dialog(main_dialog);
247
248 /* Parse input. */
249 while (1) {
250 focus = do_dialog(main_dialog, focus);
251 switch (focus) {
252 case 3:
253 /* Setup all key mappings. */
254 setup_all_keys();
255 break;
256 case 4:
257 /* Test key mappings. */
258 test_key_map();
259 break;
260 case 5:
261 /* Save and quit. */
262 save_key_map();
263 return;
264 case 6:
265 /* Just exit. */
266 return;
267 }
268 }
269 }
270
271 int main(void)
272 {
273 int i;
274
275 allegro_init();
276 install_keyboard();
277 install_mouse();
278 install_timer();
279
280 if (set_gfx_mode(GFX_SAFE, 640, 480, 0, 0) != 0) {
281 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
282 allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
283 exit(EXIT_FAILURE);
284 }
285 set_palette(desktop_palette);
286 gui_fg_color = palette_color[255];
287 gui_bg_color = palette_color[16];
288
289 black = makecol(0, 0, 0);
290 white = makecol(255, 255, 255);
291 red = makecol(255, 0, 0);
292 yellow = makecol(255, 255, 0);
293
294 /* Clear key mappings. */
295 for (i = 0; i < 256; i++)
296 keycode_to_scancode[i] = -1;
297
298 /* Hook the X keyboard callback. */
299 _xwin_keyboard_callback = get_raw_keycode;
300
301 show_main_dialog();
302
303 return 0;
304 }
305
306 END_OF_MAIN()
307