|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "flutter/shell/platform/linux/fl_keyboard_manager.h" |
| 6 | + |
| 7 | +#include <gtk/gtk.h> |
| 8 | +#include <glib.h> |
| 9 | + |
| 10 | +#include "flutter/shell/platform/linux/keyboard_map_private.h" |
| 11 | + |
| 12 | +// Ensures that a uint64_t can be safely fit into a gpointer. |
| 13 | +// |
| 14 | +// Flutter keyboard keys, values of uint64_t, are used by GHashTable by being |
| 15 | +// reinterpreted as gpointers. This greatly simplifies the implementation |
| 16 | +// (otherwise all keys and values needs allocation), but requires pointers to |
| 17 | +// be at least 64-bit long. |
| 18 | +// |
| 19 | +// Reinterpreting integers as gpointers for GHashTable is officially supported |
| 20 | +// by GTK, but natively it only supports 32-bit, probably due to crossplatform. |
| 21 | +static_assert(sizeof(gpointer) >= sizeof(uint64_t)); |
| 22 | + |
| 23 | +static constexpr uint64_t kLinuxKeyIdPlane = 0x00400000000; |
| 24 | +static constexpr uint64_t kAutogeneratedMask = 0x10000000000; |
| 25 | +// // The `gdk_keyval_to_unicode` returns a guint32 character, which should not |
| 26 | +// // exceed this length (including \0) after being converted to UTF-8. |
| 27 | +// static constexpr size_t kCharacterPoolSize = 5; |
| 28 | +static constexpr double kMicrosecondsPerMillisecond = 1000; |
| 29 | + |
| 30 | +struct _FlKeyboardManager { |
| 31 | + gchar* character_to_free; |
| 32 | + |
| 33 | + GObject parent_instance; |
| 34 | + |
| 35 | + // Stores pressed keys, mapping their Flutter physical key to Flutter logical key. |
| 36 | + // |
| 37 | + // Both keys and values are directly stored uint64s. |
| 38 | + GHashTable* pressing_records; |
| 39 | + |
| 40 | + // A static map from XKB to Flutter's physical key code |
| 41 | + GHashTable* xkb_to_physical_key; |
| 42 | + |
| 43 | + // A static map from GTK keyval to Flutter's logical key code |
| 44 | + GHashTable* keyval_to_logical_key; |
| 45 | +}; |
| 46 | + |
| 47 | +G_DEFINE_TYPE(FlKeyboardManager, fl_keyboard_manager, G_TYPE_OBJECT) |
| 48 | + |
| 49 | +static uint64_t event_to_physical_key(const GdkEventKey* event, GHashTable* table) { |
| 50 | + gpointer record = g_hash_table_lookup(table, GUINT_TO_POINTER(event->hardware_keycode)); |
| 51 | + if (record != nullptr) { |
| 52 | + return GPOINTER_TO_UINT(record); |
| 53 | + } |
| 54 | + // Auto-generate key |
| 55 | + return kAutogeneratedMask | kLinuxKeyIdPlane | event->hardware_keycode; |
| 56 | +} |
| 57 | + |
| 58 | +static uint64_t event_to_logical_key(const GdkEventKey* event, GHashTable* table) { |
| 59 | + guint keyval = event->keyval; |
| 60 | + gpointer record = g_hash_table_lookup(table, GUINT_TO_POINTER(keyval)); |
| 61 | + if (record != nullptr) { |
| 62 | + return GPOINTER_TO_UINT(record); |
| 63 | + } |
| 64 | + // ASCII // TODO |
| 65 | + if (keyval < 256) { |
| 66 | + return keyval; |
| 67 | + } |
| 68 | + // Auto-generate key |
| 69 | + return kAutogeneratedMask | kLinuxKeyIdPlane | keyval; |
| 70 | +} |
| 71 | + |
| 72 | +static uint64_t event_to_timestamp(const GdkEventKey* event) { |
| 73 | + return kMicrosecondsPerMillisecond * (double)event->time; |
| 74 | +} |
| 75 | + |
| 76 | +// Returns a newly accocated UTF-8 string from event->keyval that must be |
| 77 | +// freed later with g_free(). |
| 78 | +static char* event_to_character(const GdkEventKey* event) { |
| 79 | + gunichar unicodeChar = gdk_keyval_to_unicode(event->keyval); |
| 80 | + glong items_written; |
| 81 | + gchar* result = g_ucs4_to_utf8(&unicodeChar, 1, NULL, &items_written, NULL); |
| 82 | + if (items_written == 0) { |
| 83 | + if (result != NULL) |
| 84 | + g_free(result); |
| 85 | + return nullptr; |
| 86 | + } |
| 87 | + return result; |
| 88 | +} |
| 89 | + |
| 90 | +static uint64_t gpointerToUint64(gpointer pointer) { |
| 91 | + return pointer == nullptr ? 0 : reinterpret_cast<uint64_t>(pointer); |
| 92 | +} |
| 93 | + |
| 94 | +static gpointer uint64ToGpointer(uint64_t number) { |
| 95 | + return reinterpret_cast<gpointer>(number); |
| 96 | +} |
| 97 | + |
| 98 | +// Return the logical key corresponding to the physical key. |
| 99 | +// |
| 100 | +// Returns 0 if not found. |
| 101 | +static uint64_t pressed_logical_for_physical(GHashTable* pressing_records, uint64_t physical) { |
| 102 | + return gpointerToUint64(g_hash_table_lookup(pressing_records, uint64ToGpointer(physical))); |
| 103 | +} |
| 104 | + |
| 105 | +size_t fl_keyboard_manager_convert_key_event(FlKeyboardManager* self, |
| 106 | + const GdkEventKey* event, |
| 107 | + FlKeyDatum* results) { |
| 108 | + printf("===START=== state %d\n", event->state); |
| 109 | + if (self->character_to_free != nullptr) { |
| 110 | + g_free(self->character_to_free); |
| 111 | + self->character_to_free = nullptr; |
| 112 | + } |
| 113 | + uint64_t physical_key = event_to_physical_key(event, self->xkb_to_physical_key); |
| 114 | + uint64_t logical_key = event_to_logical_key(event, self->keyval_to_logical_key); |
| 115 | + bool is_physical_down = event->type == GDK_KEY_PRESS; |
| 116 | + |
| 117 | + uint64_t last_logical_record = pressed_logical_for_physical(self->pressing_records, physical_key); |
| 118 | + uint64_t next_logical_record = is_physical_down ? last_logical_record : 0; |
| 119 | + |
| 120 | + char* character_to_free = nullptr; |
| 121 | + |
| 122 | + printf("last %lu next %lu down %d type %d\n", last_logical_record, next_logical_record, is_physical_down, event->type); |
| 123 | + fflush(stdout); |
| 124 | + |
| 125 | + FlKeyDatum* base_event = results + 0; |
| 126 | + base_event->physical = physical_key; |
| 127 | + base_event->timestamp = event_to_timestamp(event); |
| 128 | + base_event->synthesized = false; |
| 129 | + |
| 130 | + if (is_physical_down) { |
| 131 | + character_to_free = event_to_character(event); // Might be null |
| 132 | + base_event->character = character_to_free; |
| 133 | + |
| 134 | + if (last_logical_record) { |
| 135 | + // GTK doesn't report repeat events separatedly, therefore we can't |
| 136 | + // distinguish a repeat event from a down event after a missed up event. |
| 137 | + base_event->kind = kFlKeyDataKindRepeat; |
| 138 | + base_event->logical = last_logical_record; |
| 139 | + } else { |
| 140 | + base_event->kind = kFlKeyDataKindDown; |
| 141 | + base_event->logical = logical_key; |
| 142 | + } |
| 143 | + } else { // is_physical_down false |
| 144 | + base_event->character = nullptr; |
| 145 | + base_event->kind = kFlKeyDataKindUp; |
| 146 | + base_event->logical = logical_key; |
| 147 | + } |
| 148 | + |
| 149 | + return 1; |
| 150 | +} |
| 151 | + |
| 152 | +static void fl_keyboard_manager_dispose(GObject* object) { |
| 153 | + FlKeyboardManager* self = FL_KEYBOARD_MANAGER(object); |
| 154 | + |
| 155 | + g_clear_pointer(&self->pressing_records, g_hash_table_unref); |
| 156 | + g_clear_pointer(&self->xkb_to_physical_key, g_hash_table_unref); |
| 157 | + g_clear_pointer(&self->keyval_to_logical_key, g_hash_table_unref); |
| 158 | + if (self->character_to_free != nullptr) { |
| 159 | + g_free(self->character_to_free); |
| 160 | + } |
| 161 | + |
| 162 | + G_OBJECT_CLASS(fl_keyboard_manager_parent_class)->dispose(object); |
| 163 | +} |
| 164 | + |
| 165 | +static void fl_keyboard_manager_class_init(FlKeyboardManagerClass* klass) { |
| 166 | + G_OBJECT_CLASS(klass)->dispose = fl_keyboard_manager_dispose; |
| 167 | +} |
| 168 | + |
| 169 | +static void fl_keyboard_manager_init(FlKeyboardManager* self) { |
| 170 | +} |
| 171 | + |
| 172 | +FlKeyboardManager* fl_keyboard_manager_new() { |
| 173 | + FlKeyboardManager* self = FL_KEYBOARD_MANAGER( |
| 174 | + g_object_new(fl_keyboard_manager_get_type(), nullptr)); |
| 175 | + |
| 176 | + self->pressing_records = g_hash_table_new(g_direct_hash, g_direct_equal); |
| 177 | + self->xkb_to_physical_key = g_hash_table_new(g_direct_hash, g_direct_equal); |
| 178 | + initialize_xkb_to_physical_key(self->xkb_to_physical_key); |
| 179 | + self->keyval_to_logical_key = g_hash_table_new(g_direct_hash, g_direct_equal); |
| 180 | + initialize_gtk_keyval_to_logical_key(self->keyval_to_logical_key); |
| 181 | + self->character_to_free = nullptr; |
| 182 | + |
| 183 | + return self; |
| 184 | +} |
0 commit comments