Flutter Linux Embedder
fl_key_event.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 
6 
7 struct _FlKeyEvent {
8  GObject parent_instance;
9 
10  // Time in milliseconds.
11  guint32 time;
12 
13  // True if is a press event, otherwise a release event.
14  gboolean is_press;
15 
16  // Hardware keycode.
17  guint16 keycode;
18 
19  // Keyval.
20  guint keyval;
21 
22  // Modifier state.
23  GdkModifierType state;
24 
25  // Keyboard group.
26  guint8 group;
27 
28  // The original event.
29  GdkEvent* origin;
30 };
31 
32 G_DEFINE_TYPE(FlKeyEvent, fl_key_event, G_TYPE_OBJECT)
33 
34 FlKeyEvent* fl_key_event_new(guint32 time,
35  gboolean is_press,
36  guint16 keycode,
37  guint keyval,
38  GdkModifierType state,
39  guint8 group) {
40  FlKeyEvent* self =
41  FL_KEY_EVENT(g_object_new(fl_key_event_get_type(), nullptr));
42 
43  self->time = time;
44  self->is_press = is_press;
45  self->keycode = keycode;
46  self->keyval = keyval;
47  self->state = state;
48  self->group = group;
49 
50  return self;
51 }
52 
53 FlKeyEvent* fl_key_event_new_from_gdk_event(GdkEvent* event) {
54  FlKeyEvent* self =
55  FL_KEY_EVENT(g_object_new(fl_key_event_get_type(), nullptr));
56 
57  GdkEventType type = gdk_event_get_event_type(event);
58  g_return_val_if_fail(type == GDK_KEY_PRESS || type == GDK_KEY_RELEASE,
59  nullptr);
60 
61  guint16 keycode = 0;
62  gdk_event_get_keycode(event, &keycode);
63  guint keyval = 0;
64  gdk_event_get_keyval(event, &keyval);
65  GdkModifierType state = static_cast<GdkModifierType>(0);
66  gdk_event_get_state(event, &state);
67 
68  self->time = gdk_event_get_time(event);
69  self->is_press = type == GDK_KEY_PRESS;
70  self->keycode = keycode;
71  self->keyval = keyval;
72  self->state = state;
73  self->group = event->key.group;
74  self->origin = event;
75 
76  return self;
77 }
78 
79 guint32 fl_key_event_get_time(FlKeyEvent* self) {
80  g_return_val_if_fail(FL_IS_KEY_EVENT(self), 0);
81  return self->time;
82 }
83 
84 gboolean fl_key_event_get_is_press(FlKeyEvent* self) {
85  g_return_val_if_fail(FL_IS_KEY_EVENT(self), FALSE);
86  return self->is_press;
87 }
88 
89 guint16 fl_key_event_get_keycode(FlKeyEvent* self) {
90  g_return_val_if_fail(FL_IS_KEY_EVENT(self), 0);
91  return self->keycode;
92 }
93 
94 guint fl_key_event_get_keyval(FlKeyEvent* self) {
95  g_return_val_if_fail(FL_IS_KEY_EVENT(self), 0);
96  return self->keyval;
97 }
98 
99 GdkModifierType fl_key_event_get_state(FlKeyEvent* self) {
100  g_return_val_if_fail(FL_IS_KEY_EVENT(self), static_cast<GdkModifierType>(0));
101  return self->state;
102 }
103 
104 guint8 fl_key_event_get_group(FlKeyEvent* self) {
105  g_return_val_if_fail(FL_IS_KEY_EVENT(self), 0);
106  return self->group;
107 }
108 
109 GdkEvent* fl_key_event_get_origin(FlKeyEvent* self) {
110  g_return_val_if_fail(FL_IS_KEY_EVENT(self), nullptr);
111  return self->origin;
112 }
113 
114 uint64_t fl_key_event_hash(FlKeyEvent* self) {
115  g_return_val_if_fail(FL_IS_KEY_EVENT(self), 0);
116 
117  // Combine the event timestamp, the type of event, and the hardware keycode
118  // (scan code) of the event to come up with a unique id for this event that
119  // can be derived solely from the event data itself, so that we can identify
120  // whether or not we have seen this event already.
121  guint64 type =
122  static_cast<uint64_t>(self->is_press ? GDK_KEY_PRESS : GDK_KEY_RELEASE);
123  guint64 keycode = static_cast<uint64_t>(self->keycode);
124  return (self->time & 0xffffffff) | ((type & 0xffff) << 32) |
125  ((keycode & 0xffff) << 48);
126 }
127 
128 static void fl_key_event_dispose(GObject* object) {
129  FlKeyEvent* self = FL_KEY_EVENT(object);
130 
131  g_clear_pointer(&self->origin, gdk_event_free);
132 
133  G_OBJECT_CLASS(fl_key_event_parent_class)->dispose(object);
134 }
135 
136 static void fl_key_event_class_init(FlKeyEventClass* klass) {
137  G_OBJECT_CLASS(klass)->dispose = fl_key_event_dispose;
138 }
139 
140 static void fl_key_event_init(FlKeyEvent* self) {}
_FlKeyEvent::time
guint32 time
Definition: fl_key_event.cc:11
_FlKeyEvent::group
guint8 group
Definition: fl_key_event.cc:26
event
FlKeyEvent * event
Definition: fl_key_channel_responder.cc:118
fl_key_event_new_from_gdk_event
FlKeyEvent * fl_key_event_new_from_gdk_event(GdkEvent *event)
Definition: fl_key_event.cc:53
fl_key_event_get_group
guint8 fl_key_event_get_group(FlKeyEvent *self)
Definition: fl_key_event.cc:104
type
uint8_t type
Definition: fl_standard_message_codec_test.cc:1115
fl_key_event_dispose
static void fl_key_event_dispose(GObject *object)
Definition: fl_key_event.cc:128
_FlKeyEvent::origin
GdkEvent * origin
Definition: fl_key_event.cc:29
fl_key_event_init
static void fl_key_event_init(FlKeyEvent *self)
Definition: fl_key_event.cc:140
_FlKeyEvent
Definition: fl_key_event.cc:7
state
AtkStateType state
Definition: fl_accessible_node.cc:10
fl_key_event_get_origin
GdkEvent * fl_key_event_get_origin(FlKeyEvent *self)
Definition: fl_key_event.cc:109
fl_key_event_get_keyval
guint fl_key_event_get_keyval(FlKeyEvent *self)
Definition: fl_key_event.cc:94
_FlKeyEvent::state
GdkModifierType state
Definition: fl_key_event.cc:23
_FlKeyEvent::keycode
guint16 keycode
Definition: fl_key_event.cc:17
_FlKeyEvent::parent_instance
GObject parent_instance
Definition: fl_key_event.cc:8
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
_FlKeyEvent::is_press
gboolean is_press
Definition: fl_key_event.cc:14
_FlKeyEvent::keyval
guint keyval
Definition: fl_key_event.cc:20
fl_key_event_get_time
guint32 fl_key_event_get_time(FlKeyEvent *self)
Definition: fl_key_event.cc:79
fl_key_event_get_state
GdkModifierType fl_key_event_get_state(FlKeyEvent *self)
Definition: fl_key_event.cc:99
fl_key_event_hash
uint64_t fl_key_event_hash(FlKeyEvent *self)
Definition: fl_key_event.cc:114
fl_key_event_get_keycode
guint16 fl_key_event_get_keycode(FlKeyEvent *self)
Definition: fl_key_event.cc:89
fl_key_event.h
fl_key_event_class_init
static void fl_key_event_class_init(FlKeyEventClass *klass)
Definition: fl_key_event.cc:136
fl_key_event_get_is_press
gboolean fl_key_event_get_is_press(FlKeyEvent *self)
Definition: fl_key_event.cc:84
fl_key_event_new
FlKeyEvent * fl_key_event_new(guint32 time, gboolean is_press, guint16 keycode, guint keyval, GdkModifierType state, guint8 group)
Definition: fl_key_event.cc:34