| Flutter Linux Embedder |

fl_key_event_channel.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_event_channel.h"
6
7 #include "flutter/shell/platform/linux/public/flutter_linux/fl_basic_message_channel.h"
8 #include "flutter/shell/platform/linux/public/flutter_linux/fl_json_message_codec.h"
9
10 static constexpr char kChannelName[] = "flutter/keyevent";
11 static constexpr char kTypeKey[] = "type";
12 static constexpr char kTypeValueUp[] = "keyup";
13 static constexpr char kTypeValueDown[] = "keydown";
14 static constexpr char kKeymapKey[] = "keymap";
15 static constexpr char kKeyCodeKey[] = "keyCode";
16 static constexpr char kScanCodeKey[] = "scanCode";
17 static constexpr char kModifiersKey[] = "modifiers";
18 static constexpr char kToolkitKey[] = "toolkit";
19 static constexpr char kSpecifiedLogicalKey[] = "specifiedLogicalKey";
20 static constexpr char kUnicodeScalarValuesKey[] = "unicodeScalarValues";
21
22 static constexpr char kGtkToolkit[] = "gtk";
23 static constexpr char kLinuxKeymap[] = "linux";
24
25 static constexpr int64_t kUnicodeScalarValuesUnset = 0;
26 static constexpr int64_t kSpecifiedLogicalKeyUnset = 0;
27
28 struct _FlKeyEventChannel {
29 GObject parent_instance;
30
31 FlBasicMessageChannel* channel;
32 };
33
34 G_DEFINE_TYPE(FlKeyEventChannel, fl_key_event_channel, G_TYPE_OBJECT)
35
36 static void fl_key_event_channel_dispose(GObject* object) {
37 FlKeyEventChannel* self = FL_KEY_EVENT_CHANNEL(object);
38
39 g_clear_object(&self->channel);
40
41 G_OBJECT_CLASS(fl_key_event_channel_parent_class)->dispose(object);
42 }
43
44 static void fl_key_event_channel_class_init(FlKeyEventChannelClass* klass) {
45 G_OBJECT_CLASS(klass)->dispose = fl_key_event_channel_dispose;
46 }
47
48 static void fl_key_event_channel_init(FlKeyEventChannel* self) {}
49
50 FlKeyEventChannel* fl_key_event_channel_new(FlBinaryMessenger* messenger) {
51 g_return_val_if_fail(FL_IS_BINARY_MESSENGER(messenger), nullptr);
52
53 FlKeyEventChannel* self = FL_KEY_EVENT_CHANNEL(
54 g_object_new(fl_key_event_channel_get_type(), nullptr));
55
56 g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
57 self->channel = fl_basic_message_channel_new(messenger, kChannelName,
58 FL_MESSAGE_CODEC(codec));
59
60 return self;
61 }
62
63 void fl_key_event_channel_send(FlKeyEventChannel* self,
64 FlKeyEventType type,
65 int64_t scan_code,
66 int64_t key_code,
67 int64_t modifiers,
68 int64_t unicode_scalar_values,
69 int64_t specified_logical_key,
70 GCancellable* cancellable,
71 GAsyncReadyCallback callback,
72 gpointer user_data) {
73 g_return_if_fail(FL_IS_KEY_EVENT_CHANNEL(self));
74
75 const gchar* type_string;
76 switch (type) {
77 case FL_KEY_EVENT_TYPE_KEYUP:
78 type_string = kTypeValueUp;
79 break;
80 case FL_KEY_EVENT_TYPE_KEYDOWN:
81 type_string = kTypeValueDown;
82 break;
83 default:
84 g_assert_not_reached();
85 }
86
87 g_autoptr(FlValue) message = fl_value_new_map();
88 fl_value_set_string_take(message, kTypeKey, fl_value_new_string(type_string));
89 fl_value_set_string_take(message, kKeymapKey,
90 fl_value_new_string(kLinuxKeymap));
91 fl_value_set_string_take(message, kScanCodeKey, fl_value_new_int(scan_code));
92 fl_value_set_string_take(message, kToolkitKey,
93 fl_value_new_string(kGtkToolkit));
94 fl_value_set_string_take(message, kKeyCodeKey, fl_value_new_int(key_code));
95 fl_value_set_string_take(message, kModifiersKey, fl_value_new_int(modifiers));
96 if (unicode_scalar_values != kUnicodeScalarValuesUnset) {
97 fl_value_set_string_take(message, kUnicodeScalarValuesKey,
98 fl_value_new_int(unicode_scalar_values));
99 }
100 if (specified_logical_key != kSpecifiedLogicalKeyUnset) {
101 fl_value_set_string_take(message, kSpecifiedLogicalKey,
102 fl_value_new_int(specified_logical_key));
103 }
104 fl_basic_message_channel_send(self->channel, message, cancellable, callback,
105 user_data);
106 }
107
108 gboolean fl_key_event_channel_send_finish(GObject* object,
109 GAsyncResult* result,
110 gboolean* handled,
111 GError** error) {
112 g_autoptr(FlValue) message = fl_basic_message_channel_send_finish(
113 FL_BASIC_MESSAGE_CHANNEL(object), result, error);
114 if (message == nullptr) {
115 return FALSE;
116 }
117
118 FlValue* handled_value = fl_value_lookup_string(message, "handled");
119 *handled = fl_value_get_bool(handled_value);
120
121 return TRUE;
122 }
fl_basic_message_channel_send_finish
G_MODULE_EXPORT FlValue * fl_basic_message_channel_send_finish(FlBasicMessageChannel *self, GAsyncResult *result, GError **error)
Definition: fl_basic_message_channel.cc:253
fl_basic_message_channel_new
G_MODULE_EXPORT FlBasicMessageChannel * fl_basic_message_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMessageCodec *codec)
Definition: fl_basic_message_channel.cc:154
fl_basic_message_channel_send
G_MODULE_EXPORT void fl_basic_message_channel_send(FlBasicMessageChannel *self, FlValue *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
Definition: fl_basic_message_channel.cc:226
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_basic_message_channel.h
user_data
G_BEGIN_DECLS G_MODULE_EXPORT FlValue gpointer user_data
Definition: fl_event_channel.h:90
fl_json_message_codec_new
G_MODULE_EXPORT FlJsonMessageCodec * fl_json_message_codec_new()
Definition: fl_json_message_codec.cc:306
fl_json_message_codec.h
kScanCodeKey
static constexpr char kScanCodeKey[]
Definition: fl_key_event_channel.cc:16
fl_key_event_channel_class_init
static void fl_key_event_channel_class_init(FlKeyEventChannelClass *klass)
Definition: fl_key_event_channel.cc:44
kToolkitKey
static constexpr char kToolkitKey[]
Definition: fl_key_event_channel.cc:18
kSpecifiedLogicalKeyUnset
static constexpr int64_t kSpecifiedLogicalKeyUnset
Definition: fl_key_event_channel.cc:26
kTypeKey
static constexpr char kTypeKey[]
Definition: fl_key_event_channel.cc:11
fl_key_event_channel_init
static void fl_key_event_channel_init(FlKeyEventChannel *self)
Definition: fl_key_event_channel.cc:48
kGtkToolkit
static constexpr char kGtkToolkit[]
Definition: fl_key_event_channel.cc:22
kUnicodeScalarValuesUnset
static constexpr int64_t kUnicodeScalarValuesUnset
Definition: fl_key_event_channel.cc:25
fl_key_event_channel_new
FlKeyEventChannel * fl_key_event_channel_new(FlBinaryMessenger *messenger)
Definition: fl_key_event_channel.cc:50
kChannelName
static constexpr char kChannelName[]
Definition: fl_key_event_channel.cc:10
fl_key_event_channel_send_finish
gboolean fl_key_event_channel_send_finish(GObject *object, GAsyncResult *result, gboolean *handled, GError **error)
Definition: fl_key_event_channel.cc:108
kModifiersKey
static constexpr char kModifiersKey[]
Definition: fl_key_event_channel.cc:17
kSpecifiedLogicalKey
static constexpr char kSpecifiedLogicalKey[]
Definition: fl_key_event_channel.cc:19
kLinuxKeymap
static constexpr char kLinuxKeymap[]
Definition: fl_key_event_channel.cc:23
fl_key_event_channel_dispose
static void fl_key_event_channel_dispose(GObject *object)
Definition: fl_key_event_channel.cc:36
kKeyCodeKey
static constexpr char kKeyCodeKey[]
Definition: fl_key_event_channel.cc:15
kUnicodeScalarValuesKey
static constexpr char kUnicodeScalarValuesKey[]
Definition: fl_key_event_channel.cc:20
kKeymapKey
static constexpr char kKeymapKey[]
Definition: fl_key_event_channel.cc:14
fl_key_event_channel_send
void fl_key_event_channel_send(FlKeyEventChannel *self, FlKeyEventType type, int64_t scan_code, int64_t key_code, int64_t modifiers, int64_t unicode_scalar_values, int64_t specified_logical_key, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
Definition: fl_key_event_channel.cc:63
kTypeValueUp
static constexpr char kTypeValueUp[]
Definition: fl_key_event_channel.cc:12
kTypeValueDown
static constexpr char kTypeValueDown[]
Definition: fl_key_event_channel.cc:13
fl_key_event_channel.h
FlKeyEventType
FlKeyEventType
Definition: fl_key_event_channel.h:25
FL_KEY_EVENT_TYPE_KEYDOWN
@ FL_KEY_EVENT_TYPE_KEYDOWN
Definition: fl_key_event_channel.h:27
FL_KEY_EVENT_TYPE_KEYUP
@ FL_KEY_EVENT_TYPE_KEYUP
Definition: fl_key_event_channel.h:26
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
type
uint8_t type
Definition: fl_standard_message_codec_test.cc:1117
fl_value_lookup_string
G_MODULE_EXPORT FlValue * fl_value_lookup_string(FlValue *self, const gchar *key)
Definition: fl_value.cc:811
fl_value_set_string_take
G_MODULE_EXPORT void fl_value_set_string_take(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:650
fl_value_new_string
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276
fl_value_get_bool
G_MODULE_EXPORT bool fl_value_get_bool(FlValue *self)
Definition: fl_value.cc:661
fl_value_new_int
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
Definition: fl_value.cc:262
fl_value_new_map
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:366
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
_FlKeyEventChannel
Definition: fl_key_event_channel.cc:28
_FlKeyEventChannel::parent_instance
GObject parent_instance
Definition: fl_key_event_channel.cc:29
_FlKeyEventChannel::channel
FlBasicMessageChannel * channel
Definition: fl_key_event_channel.cc:31