Flutter Engine
The Flutter Engine
fl_key_channel_responder.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_key_channel_responder.h"
6
7#include <gtk/gtk.h>
8#include <cinttypes>
9
10#include "flutter/shell/platform/linux/public/flutter_linux/fl_basic_message_channel.h"
11#include "flutter/shell/platform/linux/public/flutter_linux/fl_json_message_codec.h"
12
13static constexpr char kChannelName[] = "flutter/keyevent";
14static constexpr char kTypeKey[] = "type";
15static constexpr char kTypeValueUp[] = "keyup";
16static constexpr char kTypeValueDown[] = "keydown";
17static constexpr char kKeymapKey[] = "keymap";
18static constexpr char kKeyCodeKey[] = "keyCode";
19static constexpr char kScanCodeKey[] = "scanCode";
20static constexpr char kModifiersKey[] = "modifiers";
21static constexpr char kToolkitKey[] = "toolkit";
22static constexpr char kSpecifiedLogicalKey[] = "specifiedLogicalKey";
23static constexpr char kUnicodeScalarValuesKey[] = "unicodeScalarValues";
24
25static constexpr char kGtkToolkit[] = "gtk";
26static constexpr char kLinuxKeymap[] = "linux";
27
28/* Declare and define FlKeyChannelUserData */
29
30/**
31 * FlKeyChannelUserData:
32 * The user_data used when #FlKeyChannelResponder sends message through the
33 * channel.
34 */
35G_DECLARE_FINAL_TYPE(FlKeyChannelUserData,
36 fl_key_channel_user_data,
37 FL,
38 KEY_CHANNEL_USER_DATA,
39 GObject);
40
43
44 // The current responder.
45 FlKeyChannelResponder* responder;
46 // The callback provided by the caller #FlKeyboardManager.
48 // The user_data provided by the caller #FlKeyboardManager.
49 gpointer user_data;
50};
51
52// Definition for FlKeyChannelUserData private class.
53G_DEFINE_TYPE(FlKeyChannelUserData, fl_key_channel_user_data, G_TYPE_OBJECT)
54
55// Dispose method for FlKeyChannelUserData private class.
56static void fl_key_channel_user_data_dispose(GObject* object) {
57 g_return_if_fail(FL_IS_KEY_CHANNEL_USER_DATA(object));
58 FlKeyChannelUserData* self = FL_KEY_CHANNEL_USER_DATA(object);
59 if (self->responder != nullptr) {
60 g_object_remove_weak_pointer(
61 G_OBJECT(self->responder),
62 reinterpret_cast<gpointer*>(&(self->responder)));
63 self->responder = nullptr;
64 }
65}
66
67// Class initialization method for FlKeyChannelUserData private class.
69 FlKeyChannelUserDataClass* klass) {
70 G_OBJECT_CLASS(klass)->dispose = fl_key_channel_user_data_dispose;
71}
72
73// Instance initialization method for FlKeyChannelUserData private class.
74static void fl_key_channel_user_data_init(FlKeyChannelUserData* self) {}
75
76// Creates a new FlKeyChannelUserData private class with all information.
77//
78// The callback and the user_data might be nullptr.
79static FlKeyChannelUserData* fl_key_channel_user_data_new(
80 FlKeyChannelResponder* responder,
82 gpointer user_data) {
83 FlKeyChannelUserData* self = FL_KEY_CHANNEL_USER_DATA(
84 g_object_new(fl_key_channel_user_data_get_type(), nullptr));
85
86 self->responder = responder;
87 // Add a weak pointer so we can know if the key event responder disappeared
88 // while the framework was responding.
89 g_object_add_weak_pointer(G_OBJECT(responder),
90 reinterpret_cast<gpointer*>(&(self->responder)));
91 self->callback = callback;
92 self->user_data = user_data;
93 return self;
94}
95
96/* Define FlKeyChannelResponder */
97
98// Definition of the FlKeyChannelResponder GObject class.
101
102 FlBasicMessageChannel* channel;
103
105};
106
107static void fl_key_channel_responder_iface_init(FlKeyResponderInterface* iface);
108
110 FlKeyChannelResponder,
111 fl_key_channel_responder,
112 G_TYPE_OBJECT,
113 G_IMPLEMENT_INTERFACE(FL_TYPE_KEY_RESPONDER,
115
117 FlKeyResponder* responder,
121 gpointer user_data);
122
124 FlKeyResponderInterface* iface) {
125 iface->handle_event = fl_key_channel_responder_handle_event;
126}
127
128/* Implement FlKeyChannelResponder */
129
130// Handles a response from the method channel to a key event sent to the
131// framework earlier.
132static void handle_response(GObject* object,
133 GAsyncResult* result,
134 gpointer user_data) {
135 g_autoptr(FlKeyChannelUserData) data = FL_KEY_CHANNEL_USER_DATA(user_data);
136
137 // This is true if the weak pointer has been destroyed.
138 if (data->responder == nullptr) {
139 return;
140 }
141
142 FlKeyChannelResponder* self = data->responder;
143
144 g_autoptr(GError) error = nullptr;
145 FlBasicMessageChannel* messageChannel = FL_BASIC_MESSAGE_CHANNEL(object);
148 if (self->mock != nullptr && self->mock->value_converter != nullptr) {
149 message = self->mock->value_converter(message);
150 }
151 bool handled = false;
152 if (error != nullptr) {
153 g_warning("Unable to retrieve framework response: %s", error->message);
154 } else {
155 g_autoptr(FlValue) handled_value =
157 handled = fl_value_get_bool(handled_value);
158 }
159
160 data->callback(handled, data->user_data);
161}
162
163// Disposes of an FlKeyChannelResponder instance.
164static void fl_key_channel_responder_dispose(GObject* object) {
165 FlKeyChannelResponder* self = FL_KEY_CHANNEL_RESPONDER(object);
166
167 g_clear_object(&self->channel);
168
169 G_OBJECT_CLASS(fl_key_channel_responder_parent_class)->dispose(object);
170}
171
172// Initializes the FlKeyChannelResponder class methods.
174 FlKeyChannelResponderClass* klass) {
175 G_OBJECT_CLASS(klass)->dispose = fl_key_channel_responder_dispose;
176}
177
178// Initializes an FlKeyChannelResponder instance.
179static void fl_key_channel_responder_init(FlKeyChannelResponder* self) {}
180
181// Creates a new FlKeyChannelResponder instance, with a messenger used to send
182// messages to the framework, and an FlTextInputPlugin that is used to handle
183// key events that the framework doesn't handle. Mainly for testing purposes, it
184// also takes an optional callback to call when a response is received, and an
185// optional channel name to use when sending messages.
186FlKeyChannelResponder* fl_key_channel_responder_new(
187 FlBinaryMessenger* messenger,
189 g_return_val_if_fail(FL_IS_BINARY_MESSENGER(messenger), nullptr);
190
191 FlKeyChannelResponder* self = FL_KEY_CHANNEL_RESPONDER(
192 g_object_new(fl_key_channel_responder_get_type(), nullptr));
193 self->mock = mock;
194
195 g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
196 const char* channel_name =
197 mock == nullptr ? kChannelName : mock->channel_name;
198 self->channel = fl_basic_message_channel_new(messenger, channel_name,
199 FL_MESSAGE_CODEC(codec));
200
201 return self;
202}
203
204// Sends a key event to the framework.
206 FlKeyResponder* responder,
208 uint64_t specified_logical_key,
210 gpointer user_data) {
211 FlKeyChannelResponder* self = FL_KEY_CHANNEL_RESPONDER(responder);
212 g_return_if_fail(event != nullptr);
213 g_return_if_fail(callback != nullptr);
214
215 const gchar* type = event->is_press ? kTypeValueDown : kTypeValueUp;
216 int64_t scan_code = event->keycode;
217 int64_t unicode_scarlar_values = gdk_keyval_to_unicode(event->keyval);
218
219 // For most modifier keys, GTK keeps track of the "pressed" state of the
220 // modifier keys. Flutter uses this information to keep modifier keys from
221 // being "stuck" when a key-up event is lost because it happens after the app
222 // loses focus.
223 //
224 // For Lock keys (ShiftLock, CapsLock, NumLock), however, GTK keeps track of
225 // the state of the locks themselves, not the "pressed" state of the key.
226 //
227 // Since Flutter expects the "pressed" state of the modifier keys, the lock
228 // state for these keys is discarded here, and it is substituted for the
229 // pressed state of the key.
230 //
231 // This code has the flaw that if a key event is missed due to the app losing
232 // focus, then this state will still think the key is pressed when it isn't,
233 // but that is no worse than for "regular" keys until we implement the
234 // sync/cancel events on app focus changes.
235 //
236 // This is necessary to do here instead of in the framework because Flutter
237 // does modifier key syncing in the framework, and will turn on/off these keys
238 // as being "pressed" whenever the lock is on, which breaks a lot of
239 // interactions (for example, if shift-lock is on, tab traversal is broken).
240
241 // Remove lock states from state mask.
242 guint state = event->state & ~(GDK_LOCK_MASK | GDK_MOD2_MASK);
243
244 static bool shift_lock_pressed = FALSE;
245 static bool caps_lock_pressed = FALSE;
246 static bool num_lock_pressed = FALSE;
247 switch (event->keyval) {
248 case GDK_KEY_Num_Lock:
249 num_lock_pressed = event->is_press;
250 break;
251 case GDK_KEY_Caps_Lock:
252 caps_lock_pressed = event->is_press;
253 break;
254 case GDK_KEY_Shift_Lock:
255 shift_lock_pressed = event->is_press;
256 break;
257 }
258
259 // Add back in the state matching the actual pressed state of the lock keys,
260 // not the lock states.
261 state |= (shift_lock_pressed || caps_lock_pressed) ? GDK_LOCK_MASK : 0x0;
262 state |= num_lock_pressed ? GDK_MOD2_MASK : 0x0;
263
264 g_autoptr(FlValue) message = fl_value_new_map();
274 if (unicode_scarlar_values != 0) {
276 fl_value_new_int(unicode_scarlar_values));
277 }
278
279 if (specified_logical_key != 0) {
282 }
283
284 FlKeyChannelUserData* data =
286 // Send the message off to the framework for handling (or not).
287 fl_basic_message_channel_send(self->channel, message, nullptr,
289}
GLenum type
AtkStateType state
G_MODULE_EXPORT FlBasicMessageChannel * fl_basic_message_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMessageCodec *codec)
G_MODULE_EXPORT FlValue * fl_basic_message_channel_send_finish(FlBasicMessageChannel *self, GAsyncResult *result, GError **error)
G_MODULE_EXPORT void fl_basic_message_channel_send(FlBasicMessageChannel *self, FlValue *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
G_MODULE_EXPORT FlJsonMessageCodec * fl_json_message_codec_new()
static constexpr char kScanCodeKey[]
static void fl_key_channel_responder_handle_event(FlKeyResponder *responder, FlKeyEvent *event, uint64_t specified_logical_key, FlKeyResponderAsyncCallback callback, gpointer user_data)
static constexpr char kToolkitKey[]
G_DEFINE_TYPE_WITH_CODE(FlKeyChannelResponder, fl_key_channel_responder, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(FL_TYPE_KEY_RESPONDER, fl_key_channel_responder_iface_init)) static void fl_key_channel_responder_handle_event(FlKeyResponder *responder
FlKeyEvent uint64_t specified_logical_key
static void fl_key_channel_user_data_dispose(GObject *object)
static constexpr char kTypeKey[]
static void fl_key_channel_responder_init(FlKeyChannelResponder *self)
static constexpr char kGtkToolkit[]
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
static void handle_response(GObject *object, GAsyncResult *result, gpointer user_data)
static void fl_key_channel_user_data_init(FlKeyChannelUserData *self)
static constexpr char kChannelName[]
FlKeyChannelResponder * fl_key_channel_responder_new(FlBinaryMessenger *messenger, FlKeyChannelResponderMock *mock)
G_DECLARE_FINAL_TYPE(FlKeyChannelUserData, fl_key_channel_user_data, FL, KEY_CHANNEL_USER_DATA, GObject)
static constexpr char kModifiersKey[]
static constexpr char kSpecifiedLogicalKey[]
static constexpr char kLinuxKeymap[]
static void fl_key_channel_responder_class_init(FlKeyChannelResponderClass *klass)
static FlKeyChannelUserData * fl_key_channel_user_data_new(FlKeyChannelResponder *responder, FlKeyResponderAsyncCallback callback, gpointer user_data)
static constexpr char kKeyCodeKey[]
static constexpr char kUnicodeScalarValuesKey[]
static void fl_key_channel_responder_iface_init(FlKeyResponderInterface *iface)
FlKeyEvent * event
static constexpr char kKeymapKey[]
static void fl_key_channel_responder_dispose(GObject *object)
static void fl_key_channel_user_data_class_init(FlKeyChannelUserDataClass *klass)
static constexpr char kTypeValueUp[]
static constexpr char kTypeValueDown[]
FlKeyEvent uint64_t FlKeyResponderAsyncCallback gpointer user_data
void(* FlKeyResponderAsyncCallback)(bool handled, gpointer user_data)
#define FL_TYPE_KEY_RESPONDER
const uint8_t uint32_t uint32_t GError ** error
GAsyncResult * result
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:366
G_MODULE_EXPORT void fl_value_set_string_take(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:650
G_MODULE_EXPORT FlValue * fl_value_lookup_string(FlValue *self, const gchar *key)
Definition: fl_value.cc:811
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
Definition: fl_value.cc:262
G_MODULE_EXPORT bool fl_value_get_bool(FlValue *self)
Definition: fl_value.cc:661
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
Win32Message message
return FALSE
FlBasicMessageChannel * channel
FlKeyChannelResponderMock * mock
FlKeyChannelResponder * responder
FlKeyResponderAsyncCallback callback
guint keyval
Definition: fl_key_event.h:30
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63