Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Functions | Variables
fl_key_channel_responder.cc File Reference
#include "flutter/shell/platform/linux/fl_key_channel_responder.h"
#include <gtk/gtk.h>
#include <cinttypes>
#include "flutter/shell/platform/linux/public/flutter_linux/fl_basic_message_channel.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_json_message_codec.h"

Go to the source code of this file.

Classes

struct  _FlKeyChannelUserData
 
struct  _FlKeyChannelResponder
 

Functions

 G_DECLARE_FINAL_TYPE (FlKeyChannelUserData, fl_key_channel_user_data, FL, KEY_CHANNEL_USER_DATA, GObject)
 
static void fl_key_channel_user_data_dispose (GObject *object)
 
static void fl_key_channel_user_data_class_init (FlKeyChannelUserDataClass *klass)
 
static void fl_key_channel_user_data_init (FlKeyChannelUserData *self)
 
static FlKeyChannelUserData * fl_key_channel_user_data_new (FlKeyChannelResponder *responder, FlKeyResponderAsyncCallback callback, gpointer user_data)
 
static void fl_key_channel_responder_iface_init (FlKeyResponderInterface *iface)
 
 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
 
static void handle_response (GObject *object, GAsyncResult *result, gpointer user_data)
 
static void fl_key_channel_responder_dispose (GObject *object)
 
static void fl_key_channel_responder_class_init (FlKeyChannelResponderClass *klass)
 
static void fl_key_channel_responder_init (FlKeyChannelResponder *self)
 
FlKeyChannelResponder * fl_key_channel_responder_new (FlBinaryMessenger *messenger, FlKeyChannelResponderMock *mock)
 
static void fl_key_channel_responder_handle_event (FlKeyResponder *responder, FlKeyEvent *event, uint64_t specified_logical_key, FlKeyResponderAsyncCallback callback, gpointer user_data)
 

Variables

static constexpr char kChannelName [] = "flutter/keyevent"
 
static constexpr char kTypeKey [] = "type"
 
static constexpr char kTypeValueUp [] = "keyup"
 
static constexpr char kTypeValueDown [] = "keydown"
 
static constexpr char kKeymapKey [] = "keymap"
 
static constexpr char kKeyCodeKey [] = "keyCode"
 
static constexpr char kScanCodeKey [] = "scanCode"
 
static constexpr char kModifiersKey [] = "modifiers"
 
static constexpr char kToolkitKey [] = "toolkit"
 
static constexpr char kSpecifiedLogicalKey [] = "specifiedLogicalKey"
 
static constexpr char kUnicodeScalarValuesKey [] = "unicodeScalarValues"
 
static constexpr char kGtkToolkit [] = "gtk"
 
static constexpr char kLinuxKeymap [] = "linux"
 
FlKeyEventevent
 
FlKeyEvent uint64_t specified_logical_key
 
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
 
FlKeyEvent uint64_t FlKeyResponderAsyncCallback gpointer user_data
 

Function Documentation

◆ fl_key_channel_responder_class_init()

static void fl_key_channel_responder_class_init ( FlKeyChannelResponderClass *  klass)
static

Definition at line 173 of file fl_key_channel_responder.cc.

174 {
175 G_OBJECT_CLASS(klass)->dispose = fl_key_channel_responder_dispose;
176}
static void fl_key_channel_responder_dispose(GObject *object)

◆ fl_key_channel_responder_dispose()

static void fl_key_channel_responder_dispose ( GObject *  object)
static

Definition at line 164 of file fl_key_channel_responder.cc.

164 {
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}

◆ fl_key_channel_responder_handle_event()

static void fl_key_channel_responder_handle_event ( FlKeyResponder *  responder,
FlKeyEvent event,
uint64_t  specified_logical_key,
FlKeyResponderAsyncCallback  callback,
gpointer  user_data 
)
static

Definition at line 205 of file fl_key_channel_responder.cc.

210 {
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,
288 handle_response, data);
289}
AtkStateType state
G_MODULE_EXPORT void fl_basic_message_channel_send(FlBasicMessageChannel *self, FlValue *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
static constexpr char kScanCodeKey[]
static constexpr char kToolkitKey[]
FlKeyEvent uint64_t specified_logical_key
static constexpr char kTypeKey[]
static constexpr char kGtkToolkit[]
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
static void handle_response(GObject *object, GAsyncResult *result, gpointer user_data)
static constexpr char kModifiersKey[]
static constexpr char kSpecifiedLogicalKey[]
static constexpr char kLinuxKeymap[]
static FlKeyChannelUserData * fl_key_channel_user_data_new(FlKeyChannelResponder *responder, FlKeyResponderAsyncCallback callback, gpointer user_data)
static constexpr char kKeyCodeKey[]
static constexpr char kUnicodeScalarValuesKey[]
FlKeyEvent * event
static constexpr char kKeymapKey[]
static constexpr char kTypeValueUp[]
static constexpr char kTypeValueDown[]
FlKeyEvent uint64_t FlKeyResponderAsyncCallback gpointer user_data
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:649
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
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition fl_value.h:42
Win32Message message
return FALSE
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switches.h:41

◆ fl_key_channel_responder_iface_init()

static void fl_key_channel_responder_iface_init ( FlKeyResponderInterface *  iface)
static

Definition at line 123 of file fl_key_channel_responder.cc.

124 {
125 iface->handle_event = fl_key_channel_responder_handle_event;
126}
static void fl_key_channel_responder_handle_event(FlKeyResponder *responder, FlKeyEvent *event, uint64_t specified_logical_key, FlKeyResponderAsyncCallback callback, gpointer user_data)

◆ fl_key_channel_responder_init()

static void fl_key_channel_responder_init ( FlKeyChannelResponder *  self)
static

Definition at line 179 of file fl_key_channel_responder.cc.

179{}

◆ fl_key_channel_responder_new()

FlKeyChannelResponder * fl_key_channel_responder_new ( FlBinaryMessenger *  messenger,
FlKeyChannelResponderMock mock = nullptr 
)

FlKeyChannelResponder:

A #FlKeyResponder that handles events by sending the raw event data in JSON through the message channel.

This class communicates with the RawKeyboard API in the framework. fl_key_channel_responder_new: @messenger: the messenger that the message channel should be built on. @mock: options to mock several functionalities. Only used in unittests.

Creates a new #FlKeyChannelResponder.

Returns: a new #FlKeyChannelResponder.

Definition at line 186 of file fl_key_channel_responder.cc.

188 {
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}
G_MODULE_EXPORT FlBasicMessageChannel * fl_basic_message_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMessageCodec *codec)
G_MODULE_EXPORT FlJsonMessageCodec * fl_json_message_codec_new()
static constexpr char kChannelName[]

◆ fl_key_channel_user_data_class_init()

static void fl_key_channel_user_data_class_init ( FlKeyChannelUserDataClass *  klass)
static

Definition at line 68 of file fl_key_channel_responder.cc.

69 {
70 G_OBJECT_CLASS(klass)->dispose = fl_key_channel_user_data_dispose;
71}
static void fl_key_channel_user_data_dispose(GObject *object)

◆ fl_key_channel_user_data_dispose()

static void fl_key_channel_user_data_dispose ( GObject *  object)
static

Definition at line 56 of file fl_key_channel_responder.cc.

56 {
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}

◆ fl_key_channel_user_data_init()

static void fl_key_channel_user_data_init ( FlKeyChannelUserData *  self)
static

Definition at line 74 of file fl_key_channel_responder.cc.

74{}

◆ fl_key_channel_user_data_new()

static FlKeyChannelUserData * fl_key_channel_user_data_new ( FlKeyChannelResponder *  responder,
FlKeyResponderAsyncCallback  callback,
gpointer  user_data 
)
static

Definition at line 79 of file fl_key_channel_responder.cc.

82 {
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}
g_object_add_weak_pointer(G_OBJECT(self), reinterpret_cast< gpointer * >(&self->engine))

◆ G_DECLARE_FINAL_TYPE()

G_DECLARE_FINAL_TYPE ( FlKeyChannelUserData  ,
fl_key_channel_user_data  ,
FL  ,
KEY_CHANNEL_USER_DATA  ,
GObject   
)

FlKeyChannelUserData: The user_data used when #FlKeyChannelResponder sends message through the channel.

◆ G_DEFINE_TYPE_WITH_CODE()

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  
)

◆ handle_response()

static void handle_response ( GObject *  object,
GAsyncResult *  result,
gpointer  user_data 
)
static

Definition at line 132 of file fl_key_channel_responder.cc.

134 {
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}
G_MODULE_EXPORT FlValue * fl_basic_message_channel_send_finish(FlBasicMessageChannel *self, GAsyncResult *result, GError **error)
const uint8_t uint32_t uint32_t GError ** error
GAsyncResult * result
G_MODULE_EXPORT FlValue * fl_value_lookup_string(FlValue *self, const gchar *key)
Definition fl_value.cc:810
G_MODULE_EXPORT bool fl_value_get_bool(FlValue *self)
Definition fl_value.cc:660

Variable Documentation

◆ callback

Definition at line 120 of file fl_key_channel_responder.cc.

◆ event

FlKeyEvent* event

Definition at line 118 of file fl_key_channel_responder.cc.

◆ kChannelName

constexpr char kChannelName[] = "flutter/keyevent"
staticconstexpr

Definition at line 13 of file fl_key_channel_responder.cc.

◆ kGtkToolkit

constexpr char kGtkToolkit[] = "gtk"
staticconstexpr

Definition at line 25 of file fl_key_channel_responder.cc.

◆ kKeyCodeKey

constexpr char kKeyCodeKey[] = "keyCode"
staticconstexpr

Definition at line 18 of file fl_key_channel_responder.cc.

◆ kKeymapKey

constexpr char kKeymapKey[] = "keymap"
staticconstexpr

Definition at line 17 of file fl_key_channel_responder.cc.

◆ kLinuxKeymap

constexpr char kLinuxKeymap[] = "linux"
staticconstexpr

Definition at line 26 of file fl_key_channel_responder.cc.

◆ kModifiersKey

constexpr char kModifiersKey[] = "modifiers"
staticconstexpr

Definition at line 20 of file fl_key_channel_responder.cc.

◆ kScanCodeKey

constexpr char kScanCodeKey[] = "scanCode"
staticconstexpr

Definition at line 19 of file fl_key_channel_responder.cc.

◆ kSpecifiedLogicalKey

constexpr char kSpecifiedLogicalKey[] = "specifiedLogicalKey"
staticconstexpr

Definition at line 22 of file fl_key_channel_responder.cc.

◆ kToolkitKey

constexpr char kToolkitKey[] = "toolkit"
staticconstexpr

Definition at line 21 of file fl_key_channel_responder.cc.

◆ kTypeKey

constexpr char kTypeKey[] = "type"
staticconstexpr

Definition at line 14 of file fl_key_channel_responder.cc.

◆ kTypeValueDown

constexpr char kTypeValueDown[] = "keydown"
staticconstexpr

Definition at line 16 of file fl_key_channel_responder.cc.

◆ kTypeValueUp

constexpr char kTypeValueUp[] = "keyup"
staticconstexpr

Definition at line 15 of file fl_key_channel_responder.cc.

◆ kUnicodeScalarValuesKey

constexpr char kUnicodeScalarValuesKey[] = "unicodeScalarValues"
staticconstexpr

Definition at line 23 of file fl_key_channel_responder.cc.

◆ specified_logical_key

FlKeyEvent uint64_t specified_logical_key

Definition at line 119 of file fl_key_channel_responder.cc.

◆ user_data

FlKeyEvent uint64_t FlKeyResponderAsyncCallback gpointer user_data

Definition at line 121 of file fl_key_channel_responder.cc.