| 1 | /* |
| 2 | * Copyright (C) 2010, Intel Corporation. |
| 3 | * |
| 4 | * Author: Raymond Liu <[email protected]> |
| 5 | * |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public License |
| 9 | * version 2.1 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 19 | * 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include "debug.h" |
| 23 | #include <QEvent> |
| 24 | #include <QKeyEvent> |
| 25 | |
| 26 | #include "qt-keysym-map.h" |
| 27 | |
| 28 | // To workaround glib >= 2.25 where signals is used, while QT already define it as C++ keyword: protected |
| 29 | #ifdef signals |
| 30 | #undef signals |
| 31 | #endif |
| 32 | #include "qt-gtk-translate.h" |
| 33 | |
| 34 | |
| 35 | GdkEventKey * |
| 36 | compose_gdk_keyevent(GdkEventType type, guint keyval, guint state, GdkWindow *window) |
| 37 | { |
| 38 | GdkEventKey *event = NULL; |
| 39 | #if GTK_CHECK_VERSION (3, 0, 0) |
| 40 | GdkDisplay *display; |
| 41 | GdkDeviceManager *device_manager; |
| 42 | GdkDevice *client_pointer; |
| 43 | #endif |
| 44 | if ((type != GDK_KEY_PRESS) && (type != GDK_KEY_RELEASE)) |
| 45 | return NULL; |
| 46 | |
| 47 | event = (GdkEventKey *)(gdk_event_new(type)); |
| 48 | event->length = 0; |
| 49 | event->string = 0; |
| 50 | event->is_modifier = 0; |
| 51 | event->time = GDK_CURRENT_TIME; |
| 52 | event->state = state; |
| 53 | |
| 54 | #if GTK_CHECK_VERSION (3, 0, 0) |
| 55 | display = gdk_window_get_display (window); |
| 56 | device_manager = gdk_display_get_device_manager (display); |
| 57 | client_pointer = gdk_device_manager_get_client_pointer (device_manager); |
| 58 | |
| 59 | gdk_event_set_device ((GdkEvent *)event, |
| 60 | gdk_device_get_associated_device (client_pointer)); |
| 61 | #endif |
| 62 | |
| 63 | if (type == GDK_KEY_RELEASE) |
| 64 | event->state |= GDK_RELEASE_MASK; |
| 65 | event->keyval = keyval; |
| 66 | event->window = window; |
| 67 | |
| 68 | if (event->window) { |
| 69 | GdkKeymap *key_map = gdk_keymap_get_default(); |
| 70 | GdkKeymapKey *keys; |
| 71 | gint n; |
| 72 | |
| 73 | g_object_ref(event->window); // seems when event is freed, the event->window will be unref |
| 74 | |
| 75 | if (gdk_keymap_get_entries_for_keyval(key_map, event->keyval, &keys, &n)) { |
| 76 | event->hardware_keycode = keys[0].keycode; |
| 77 | event->group = keys[0].group; |
| 78 | } else { |
| 79 | event->hardware_keycode = 0; |
| 80 | event->group = 0; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | DBG("event type=0x%x, state=0x%x, keyval=0x%x, keycode=0x%x, group=%d", |
| 85 | event->type, event->state, event->keyval, event->hardware_keycode, event->group); |
| 86 | |
| 87 | return event; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | GdkEventKey * |
| 93 | qt_key_event_to_gdk(int type, int key, int modifiers, const char *text, GdkWindow *window) |
| 94 | { |
| 95 | Q_UNUSED(text); |
| 96 | guint state = 0; |
| 97 | guint keyval; |
| 98 | |
| 99 | STEP(); |
| 100 | if ((type != QEvent::KeyPress) && (type != QEvent::KeyRelease)) |
| 101 | return NULL; |
| 102 | |
| 103 | if (modifiers & Qt::ShiftModifier) |
| 104 | state |= GDK_SHIFT_MASK; |
| 105 | if (modifiers & Qt::ControlModifier) |
| 106 | state |= GDK_CONTROL_MASK; |
| 107 | if (modifiers & Qt::AltModifier) |
| 108 | state |= GDK_MOD1_MASK; |
| 109 | |
| 110 | keyval = QtKeyToXKeySym(key); |
| 111 | |
| 112 | if (type == QEvent::KeyPress) { |
| 113 | return compose_gdk_keyevent(GDK_KEY_PRESS, keyval, state, window); |
| 114 | } else { |
| 115 | return compose_gdk_keyevent(GDK_KEY_RELEASE, keyval, state, window); |
| 116 | } |
| 117 | |
| 118 | } |
| 119 | |
| 120 | |
| 121 | gboolean |
| 122 | gdk_key_event_to_qt(GdkEventKey *event, int *type, int *key, int *modifier) |
| 123 | { |
| 124 | |
| 125 | switch (event->type) { |
| 126 | case GDK_KEY_PRESS: |
| 127 | *type = QEvent::KeyPress; |
| 128 | break; |
| 129 | case GDK_KEY_RELEASE: |
| 130 | *type = QEvent::KeyRelease; |
| 131 | break; |
| 132 | default: |
| 133 | return FALSE; |
| 134 | } |
| 135 | |
| 136 | *key = XKeySymToQTKey(event->keyval); |
| 137 | if (*key == Qt::Key_unknown) { |
| 138 | qWarning("Unkonwn key"); |
| 139 | return FALSE; |
| 140 | } |
| 141 | |
| 142 | *modifier = Qt::NoModifier; |
| 143 | if (event->state & GDK_SHIFT_MASK) |
| 144 | *modifier |= Qt::ShiftModifier; |
| 145 | if (event->state & GDK_CONTROL_MASK) |
| 146 | *modifier |= Qt::ControlModifier; |
| 147 | if (event->state & GDK_MOD1_MASK) |
| 148 | *modifier |= Qt::AltModifier; |
| 149 | if (event->state & GDK_META_MASK) |
| 150 | *modifier |= Qt::MetaModifier; |
| 151 | |
| 152 | DBG("qtkey type =%d, qtkey=0x%x, modifier=0x%x", *type, *key, *modifier); |
| 153 | |
| 154 | return TRUE; |
| 155 | } |