| 1 |
/* |
| 2 |
* Clutter. |
| 3 |
* |
| 4 |
* An OpenGL based 'interactive canvas' library. |
| 5 |
* |
| 6 |
* Copyright (C) 2010 Intel Corporation. |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Lesser General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public |
| 19 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
* Authors: |
| 22 |
* Kristian Høgsberg |
| 23 |
* Damien Lespiau <[email protected]> |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "clutter-keysyms.h" |
| 27 |
#include "clutter-event-private.h" |
| 28 |
#include "clutter-xkb-utils.h" |
| 29 |
|
| 30 |
/* |
| 31 |
* _clutter_event_new_from_evdev: Create a new Clutter ClutterKeyEvent |
| 32 |
* @device: a ClutterInputDevice |
| 33 |
* @stage: the stage the event should be delivered to |
| 34 |
* @xkb: XKB rules to translate the event |
| 35 |
* @_time: timestamp of the event |
| 36 |
* @key: a key code coming from a Linux input device |
| 37 |
* @state: TRUE if a press event, FALSE if a release event |
| 38 |
* @modifer_state: in/out |
| 39 |
* |
| 40 |
* Translate @key to a #ClutterKeyEvent using rules from xbbcommon. |
| 41 |
* |
| 42 |
* Return value: the new #ClutterEvent |
| 43 |
*/ |
| 44 |
ClutterEvent * |
| 45 |
_clutter_key_event_new_from_evdev (ClutterInputDevice *device, |
| 46 |
ClutterInputDevice *core_device, |
| 47 |
ClutterStage *stage, |
| 48 |
struct xkb_state *xkb_state, |
| 49 |
uint32_t button_state, |
| 50 |
uint32_t _time, |
| 51 |
xkb_keycode_t key, |
| 52 |
uint32_t state) |
| 53 |
{ |
| 54 |
ClutterEvent *event; |
| 55 |
xkb_keysym_t sym; |
| 56 |
const xkb_keysym_t *syms; |
| 57 |
char buffer[8]; |
| 58 |
int n; |
| 59 |
|
| 60 |
if (state) |
| 61 |
event = clutter_event_new (CLUTTER_KEY_PRESS); |
| 62 |
else |
| 63 |
event = clutter_event_new (CLUTTER_KEY_RELEASE); |
| 64 |
|
| 65 |
/* We use a fixed offset of 8 because evdev starts KEY_* numbering from |
| 66 |
* 0, whereas X11's minimum keycode, for really stupid reasons, is 8. |
| 67 |
* So the evdev XKB rules are based on the keycodes all being shifted |
| 68 |
* upwards by 8. */ |
| 69 |
key += 8; |
| 70 |
|
| 71 |
n = xkb_key_get_syms (xkb_state, key, &syms); |
| 72 |
if (n == 1) |
| 73 |
sym = syms[0]; |
| 74 |
else |
| 75 |
sym = XKB_KEY_NoSymbol; |
| 76 |
|
| 77 |
event->key.device = core_device; |
| 78 |
event->key.stage = stage; |
| 79 |
event->key.time = _time; |
| 80 |
_clutter_xkb_translate_state (event, xkb_state, button_state); |
| 81 |
event->key.hardware_keycode = key; |
| 82 |
event->key.keyval = sym; |
| 83 |
clutter_event_set_source_device (event, device); |
| 84 |
|
| 85 |
n = xkb_keysym_to_utf8 (sym, buffer, sizeof (buffer)); |
| 86 |
|
| 87 |
if (n == 0) |
| 88 |
{ |
| 89 |
/* not printable */ |
| 90 |
event->key.unicode_value = (gunichar) '\0'; |
| 91 |
} |
| 92 |
else |
| 93 |
{ |
| 94 |
event->key.unicode_value = g_utf8_get_char_validated (buffer, n); |
| 95 |
if (event->key.unicode_value == -1 || event->key.unicode_value == -2) |
| 96 |
event->key.unicode_value = (gunichar) '\0'; |
| 97 |
} |
| 98 |
|
| 99 |
return event; |
| 100 |
} |
| 101 |
|
| 102 |
void |
| 103 |
_clutter_xkb_translate_state (ClutterEvent *event, |
| 104 |
struct xkb_state *state, |
| 105 |
uint32_t button_state) |
| 106 |
{ |
| 107 |
_clutter_event_set_state_full (event, |
| 108 |
button_state, |
| 109 |
xkb_state_serialize_mods (state, XKB_STATE_MODS_DEPRESSED), |
| 110 |
xkb_state_serialize_mods (state, XKB_STATE_MODS_LATCHED), |
| 111 |
xkb_state_serialize_mods (state, XKB_STATE_MODS_LOCKED), |
| 112 |
xkb_state_serialize_mods (state, XKB_STATE_MODS_EFFECTIVE) | button_state); |
| 113 |
} |