2 * Copyright (C) 2007 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
23 #include "swfdec_gtk_keys.h"
26 #include <gdk/gdkkeysyms.h>
28 static const guint8 flash_keycodes_old
[] = {
29 /* 0 */ 0, 0, 0, 0, 0,
30 /* 5 */ 0, 0, 0, 0, SWFDEC_KEY_ESCAPE
,
31 /* 10 */ SWFDEC_KEY_1
, SWFDEC_KEY_2
, SWFDEC_KEY_3
, SWFDEC_KEY_4
, SWFDEC_KEY_5
,
32 /* 15 */ SWFDEC_KEY_6
, SWFDEC_KEY_7
, SWFDEC_KEY_8
, SWFDEC_KEY_9
, SWFDEC_KEY_0
,
33 /* 20 */ SWFDEC_KEY_MINUS
, SWFDEC_KEY_EQUAL
, SWFDEC_KEY_BACKSPACE
, SWFDEC_KEY_TAB
, SWFDEC_KEY_Q
,
34 /* 25 */ SWFDEC_KEY_W
, SWFDEC_KEY_E
, SWFDEC_KEY_R
, SWFDEC_KEY_T
, SWFDEC_KEY_Y
,
35 /* 30 */ SWFDEC_KEY_U
, SWFDEC_KEY_I
, SWFDEC_KEY_O
, SWFDEC_KEY_P
, SWFDEC_KEY_LEFT_BRACKET
,
36 /* 35 */ SWFDEC_KEY_RIGHT_BRACKET
, SWFDEC_KEY_ENTER
, SWFDEC_KEY_CONTROL
, SWFDEC_KEY_A
, SWFDEC_KEY_S
,
37 /* 40 */ SWFDEC_KEY_D
, SWFDEC_KEY_F
, SWFDEC_KEY_G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_KEY_F12
, SWFDEC_KEY_HOME
, SWFDEC_KEY_UP
, SWFDEC_KEY_PAGE_UP
,
49 /* 100 */ SWFDEC_KEY_LEFT
, 0, SWFDEC_KEY_RIGHT
, SWFDEC_KEY_END
, SWFDEC_KEY_DOWN
,
50 /* 105 */ SWFDEC_KEY_PAGE_DOWN
, SWFDEC_KEY_INSERT
, SWFDEC_KEY_DELETE
, SWFDEC_KEY_ENTER
, SWFDEC_KEY_CONTROL
,
51 /* 110 */ 0, 0, SWFDEC_KEY_NUMPAD_DIVIDE
, SWFDEC_KEY_ALT
, 0,
52 /* 115 */ 0, 0, 0, 0, 0
60 static const guint8 flash_keycodes_evdev
[] = {
61 /* 0 */ 0, 0, 0, 0, 0,
62 /* 5 */ 0, 0, 0, 0, SWFDEC_KEY_ESCAPE
,
63 /* 10 */ SWFDEC_KEY_1
, SWFDEC_KEY_2
, SWFDEC_KEY_3
, SWFDEC_KEY_4
, SWFDEC_KEY_5
,
64 /* 15 */ SWFDEC_KEY_6
, SWFDEC_KEY_7
, SWFDEC_KEY_8
, SWFDEC_KEY_9
, SWFDEC_KEY_0
,
65 /* 20 */ SWFDEC_KEY_MINUS
, SWFDEC_KEY_EQUAL
, SWFDEC_KEY_BACKSPACE
, SWFDEC_KEY_TAB
, SWFDEC_KEY_Q
,
66 /* 25 */ SWFDEC_KEY_W
, SWFDEC_KEY_E
, SWFDEC_KEY_R
, SWFDEC_KEY_T
, SWFDEC_KEY_Y
,
67 /* 30 */ SWFDEC_KEY_U
, SWFDEC_KEY_I
, SWFDEC_KEY_O
, SWFDEC_KEY_P
, SWFDEC_KEY_LEFT_BRACKET
,
68 /* 35 */ SWFDEC_KEY_RIGHT_BRACKET
, SWFDEC_KEY_ENTER
, SWFDEC_KEY_CONTROL
, SWFDEC_KEY_A
, SWFDEC_KEY_S
,
69 /* 40 */ SWFDEC_KEY_D
, SWFDEC_KEY_F
, SWFDEC_KEY_G
, SWFDEC_KEY_H
, SWFDEC_KEY_J
,
70 /*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0, 0, 0,
82 /* 105 */ SWFDEC_KEY_CONTROL
, SWFDEC_KEY_NUMPAD_DIVIDE
, 0, SWFDEC_KEY_ALT
, 0,
83 /* 110 */ SWFDEC_KEY_HOME
, SWFDEC_KEY_UP
, SWFDEC_KEY_PAGE_UP
, SWFDEC_KEY_LEFT
, SWFDEC_KEY_RIGHT
,
84 /* 115 */ SWFDEC_KEY_END
, SWFDEC_KEY_DOWN
, SWFDEC_KEY_PAGE_DOWN
, SWFDEC_KEY_INSERT
, SWFDEC_KEY_DELETE
88 compute_keycodes (void)
90 /* This is a huge hack, but a proper solution is probably very complicated
92 * We need a proper way to convert X keycodes to Windows virtual keycodes
93 * and according to the people in #xorg-devel noone else has succeeded there
95 static const GdkKeymapKey test
= { 105, 0, 0 };
96 guint keyval
= gdk_keymap_lookup_key (gdk_keymap_get_default (), &test
);
99 g_assert (G_N_ELEMENTS (flash_keycodes_old
) == G_N_ELEMENTS (flash_keycodes_evdev
));
101 if (keyval
== GDK_Page_Down
) {
102 return flash_keycodes_old
;
103 } else if (keyval
!= GDK_Control_R
) {
104 g_printerr ("FIXME: Failed to detect proper keyboard layout, assuming evdev.\n");
107 return flash_keycodes_evdev
;
111 * swfdec_gtk_keycode_from_hardware_keycode:
112 * @hardware_keycode: a hardware keycode sent from the X server
114 * Tries to transform an X hardware keycode to the corresponding #SwfdecKey.
115 * This is a utility function for cases where key events need to be transformed
118 * Returns: the corresponding key code as used in Flash.
121 swfdec_gtk_keycode_from_hardware_keycode (guint hardware_keycode
)
123 static const guint8
*keycodes
= NULL
;
125 if (G_UNLIKELY (keycodes
== NULL
))
126 keycodes
= compute_keycodes ();
128 if (hardware_keycode
>= G_N_ELEMENTS (flash_keycodes_evdev
))
130 return keycodes
[hardware_keycode
];