| Flutter Linux Embedder |

fl_keyboard_layout.cc
Go to the documentation of this file.
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_layout.h"
6
7 // The maxiumum keycode in a derived layout.
8 //
9 // Although X supports higher keycodes, Flutter only cares about standard keys,
10 // which are below this.
11 constexpr size_t kLayoutSize = 128;
12
13 struct _FlKeyboardLayout {
14 GObject parent_instance;
15
16 // Each keycode->logical key mapping per group.
17 GHashTable* groups;
18 };
19
20 G_DEFINE_TYPE(FlKeyboardLayout, fl_keyboard_layout, G_TYPE_OBJECT)
21
22 static void fl_keyboard_layout_dispose(GObject* object) {
23 FlKeyboardLayout* self = FL_KEYBOARD_LAYOUT(object);
24
25 g_clear_pointer(&self->groups, g_hash_table_unref);
26
27 G_OBJECT_CLASS(fl_keyboard_layout_parent_class)->dispose(object);
28 }
29
30 static void fl_keyboard_layout_class_init(FlKeyboardLayoutClass* klass) {
31 G_OBJECT_CLASS(klass)->dispose = fl_keyboard_layout_dispose;
32 }
33
34 static void fl_keyboard_layout_init(FlKeyboardLayout* self) {
35 self->groups = g_hash_table_new_full(
36 g_direct_hash, g_direct_equal, nullptr,
37 reinterpret_cast<GDestroyNotify>(g_hash_table_unref));
38 }
39
40 FlKeyboardLayout* fl_keyboard_layout_new() {
41 return FL_KEYBOARD_LAYOUT(
42 g_object_new(fl_keyboard_layout_get_type(), nullptr));
43 }
44
45 gboolean fl_keyboard_layout_has_group(FlKeyboardLayout* self, uint8_t group) {
46 return g_hash_table_lookup(self->groups, GINT_TO_POINTER(group)) != nullptr;
47 }
48
49 void fl_keyboard_layout_set_logical_key(FlKeyboardLayout* self,
50 uint8_t group,
51 uint16_t keycode,
52 uint64_t logical_key) {
53 GHashTable* group_layout = static_cast<GHashTable*>(
54 g_hash_table_lookup(self->groups, GINT_TO_POINTER(group)));
55 if (group_layout == nullptr) {
56 group_layout =
57 g_hash_table_new_full(g_direct_hash, g_direct_equal, nullptr, nullptr);
58 g_hash_table_insert(self->groups, GINT_TO_POINTER(group), group_layout);
59 }
60
61 g_hash_table_insert(group_layout, GINT_TO_POINTER(keycode),
62 GINT_TO_POINTER(logical_key));
63 }
64
65 uint64_t fl_keyboard_layout_get_logical_key(FlKeyboardLayout* self,
66 uint8_t group,
67 uint16_t keycode) {
68 if (keycode >= kLayoutSize) {
69 return 0;
70 }
71
72 GHashTable* group_layout = static_cast<GHashTable*>(
73 g_hash_table_lookup(self->groups, GINT_TO_POINTER(group)));
74 if (group_layout == nullptr) {
75 return 0;
76 }
77
78 return GPOINTER_TO_INT(
79 g_hash_table_lookup(group_layout, GINT_TO_POINTER(keycode)));
80 }
G_DEFINE_TYPE
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
Definition: fl_basic_message_channel.cc:37
fl_keyboard_layout_get_logical_key
uint64_t fl_keyboard_layout_get_logical_key(FlKeyboardLayout *self, uint8_t group, uint16_t keycode)
Definition: fl_keyboard_layout.cc:65
kLayoutSize
constexpr size_t kLayoutSize
Definition: fl_keyboard_layout.cc:11
fl_keyboard_layout_set_logical_key
void fl_keyboard_layout_set_logical_key(FlKeyboardLayout *self, uint8_t group, uint16_t keycode, uint64_t logical_key)
Definition: fl_keyboard_layout.cc:49
fl_keyboard_layout_class_init
static void fl_keyboard_layout_class_init(FlKeyboardLayoutClass *klass)
Definition: fl_keyboard_layout.cc:30
fl_keyboard_layout_dispose
static void fl_keyboard_layout_dispose(GObject *object)
Definition: fl_keyboard_layout.cc:22
fl_keyboard_layout_init
static void fl_keyboard_layout_init(FlKeyboardLayout *self)
Definition: fl_keyboard_layout.cc:34
fl_keyboard_layout_new
FlKeyboardLayout * fl_keyboard_layout_new()
Definition: fl_keyboard_layout.cc:40
fl_keyboard_layout_has_group
gboolean fl_keyboard_layout_has_group(FlKeyboardLayout *self, uint8_t group)
Definition: fl_keyboard_layout.cc:45
fl_keyboard_layout.h
_FlKeyboardLayout
Definition: fl_keyboard_layout.cc:13
_FlKeyboardLayout::groups
GHashTable * groups
Definition: fl_keyboard_layout.cc:17
_FlKeyboardLayout::parent_instance
GObject parent_instance
Definition: fl_keyboard_layout.cc:14