|
| 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 | +#include "flutter/shell/platform/linux/public/flutter_linux/fl_basic_message_channel.h" |
| 7 | +#include "flutter/shell/platform/linux/public/flutter_linux/fl_json_message_codec.h" |
| 8 | + |
| 9 | +static constexpr char kChannelName[] = "flutter/keyevent"; |
| 10 | +static constexpr char kTypeKey[] = "type"; |
| 11 | +static constexpr char kTypeValueUp[] = "keyup"; |
| 12 | +static constexpr char kTypeValueDown[] = "keydown"; |
| 13 | +static constexpr char kKeymapKey[] = "keymap"; |
| 14 | +static constexpr char kKeyCodeKey[] = "keyCode"; |
| 15 | +static constexpr char kScanCodeKey[] = "scanCode"; |
| 16 | +static constexpr char kModifiersKey[] = "modifiers"; |
| 17 | +static constexpr char kToolkitKey[] = "toolkit"; |
| 18 | +static constexpr char kUnicodeScalarValuesKey[] = "unicodeScalarValues"; |
| 19 | + |
| 20 | +struct _FlKeyEventChannel { |
| 21 | + GObject parent_instance; |
| 22 | + |
| 23 | + FlBasicMessageChannel* channel; |
| 24 | +}; |
| 25 | + |
| 26 | +G_DEFINE_TYPE(FlKeyEventChannel, fl_key_event_channel, G_TYPE_OBJECT) |
| 27 | + |
| 28 | +static void fl_key_event_channel_dispose(GObject* object) { |
| 29 | + FlKeyEventChannel* self = FL_KEY_EVENT_CHANNEL(object); |
| 30 | + |
| 31 | + g_clear_object(&self->channel); |
| 32 | + |
| 33 | + G_OBJECT_CLASS(fl_key_event_channel_parent_class)->dispose(object); |
| 34 | +} |
| 35 | + |
| 36 | +static void fl_key_event_channel_class_init(FlKeyEventChannelClass* klass) { |
| 37 | + G_OBJECT_CLASS(klass)->dispose = fl_key_event_channel_dispose; |
| 38 | +} |
| 39 | + |
| 40 | +static void fl_key_event_channel_init(FlKeyEventChannel* self) {} |
| 41 | + |
| 42 | +FlKeyEventChannel* fl_key_event_channel_new(FlBinaryMessenger* messenger) { |
| 43 | + g_return_val_if_fail(FL_IS_BINARY_MESSENGER(messenger), nullptr); |
| 44 | + |
| 45 | + FlKeyEventChannel* self = FL_KEY_EVENT_CHANNEL( |
| 46 | + g_object_new(fl_key_event_channel_get_type(), nullptr)); |
| 47 | + |
| 48 | + g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new(); |
| 49 | + self->channel = fl_basic_message_channel_new(messenger, kChannelName, |
| 50 | + FL_MESSAGE_CODEC(codec)); |
| 51 | + |
| 52 | + return self; |
| 53 | +} |
| 54 | + |
| 55 | +void fl_key_event_channel_send_key_event(FlKeyEventChannel* self, |
| 56 | + FlKeyEventType type, |
| 57 | + const gchar* keymap, |
| 58 | + int64_t scan_code, |
| 59 | + const gchar* toolkit, |
| 60 | + int64_t key_code, |
| 61 | + int64_t modifiers, |
| 62 | + int64_t unicodeScalarValues) { |
| 63 | + g_return_if_fail(FL_IS_KEY_EVENT_CHANNEL(self)); |
| 64 | + g_return_if_fail(type == KEY_EVENT_TYPE_DOWN || type == KEY_EVENT_TYPE_UP); |
| 65 | + g_return_if_fail(keymap != nullptr); |
| 66 | + g_return_if_fail(toolkit != nullptr); |
| 67 | + |
| 68 | + fl_value_set_string_take( |
| 69 | + message, kTypeKey, |
| 70 | + fl_value_new_string(type == KEY_EVENT_TYPE_DOWN ? kTypeValueDown |
| 71 | + : kTypeValueUp)); |
| 72 | + fl_value_set_string_take(message, kKeymapKey, fl_value_new_string(keymap)); |
| 73 | + fl_value_set_string_take(message, kScanCodeKey, fl_value_new_int(scan_code)); |
| 74 | + fl_value_set_string_take(message, kToolkitKey, fl_value_new_string(toolkit)); |
| 75 | + fl_value_set_string_take(message, kKeyCodeKey, fl_value_new_int(key_code)); |
| 76 | + fl_value_set_string_take(message, kModifiersKey, fl_value_new_int(modifiers)); |
| 77 | + if (unicodeScalarValues != 0) |
| 78 | + fl_value_set_string_take(message, kUnicodeScalarValuesKey, |
| 79 | + fl_value_new_int(unicodeScalarValues)); |
| 80 | + |
| 81 | + fl_basic_message_channel_send(self->channel, message, nullptr, nullptr, |
| 82 | + nullptr); |
| 83 | +} |
0 commit comments