Flutter Engine
The Flutter Engine
Classes | Macros | Functions | Variables
fl_keyboard_manager.cc File Reference
#include "flutter/shell/platform/linux/fl_keyboard_manager.h"
#include <array>
#include <cinttypes>
#include <memory>
#include <string>
#include "flutter/shell/platform/linux/fl_key_channel_responder.h"
#include "flutter/shell/platform/linux/fl_key_embedder_responder.h"
#include "flutter/shell/platform/linux/key_mapping.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_method_codec.h"

Go to the source code of this file.

Classes

struct  _FlKeyboardPendingEvent
 
struct  _FlKeyboardManagerUserData
 
struct  _FlKeyboardManager
 

Macros

#define DEBUG_PRINT_LAYOUT
 
#define FL_TYPE_KEYBOARD_MANAGER_USER_DATA    fl_keyboard_manager_user_data_get_type()
 

Functions

 G_DECLARE_FINAL_TYPE (FlKeyboardPendingEvent, fl_keyboard_pending_event, FL, KEYBOARD_PENDING_EVENT, GObject)
 
 G_DECLARE_FINAL_TYPE (FlKeyboardManagerUserData, fl_keyboard_manager_user_data, FL, KEYBOARD_MANAGER_USER_DATA, GObject)
 
static uint64_t get_logical_key_from_layout (const FlKeyEvent *event, const DerivedLayout &layout)
 
static void fl_keyboard_pending_event_dispose (GObject *object)
 
static void fl_keyboard_pending_event_class_init (FlKeyboardPendingEventClass *klass)
 
static void fl_keyboard_pending_event_init (FlKeyboardPendingEvent *self)
 
static uint64_t fl_keyboard_manager_get_event_hash (FlKeyEvent *event)
 
static FlKeyboardPendingEvent * fl_keyboard_pending_event_new (std::unique_ptr< FlKeyEvent > event, uint64_t sequence_id, size_t to_reply)
 
 G_DEFINE_TYPE (FlKeyboardManagerUserData, fl_keyboard_manager_user_data, G_TYPE_OBJECT) static void fl_keyboard_manager_user_data_dispose(GObject *object)
 
static void fl_keyboard_manager_user_data_class_init (FlKeyboardManagerUserDataClass *klass)
 
static void fl_keyboard_manager_user_data_init (FlKeyboardManagerUserData *self)
 
static FlKeyboardManagerUserData * fl_keyboard_manager_user_data_new (FlKeyboardManager *manager, uint64_t sequence_id)
 
 G_DEFINE_TYPE (FlKeyboardManager, fl_keyboard_manager, G_TYPE_OBJECT)
 
static void fl_keyboard_manager_dispose (GObject *object)
 
static void fl_keyboard_manager_class_init (FlKeyboardManagerClass *klass)
 
static void fl_keyboard_manager_init (FlKeyboardManager *self)
 
static gboolean g_ptr_array_find_with_equal_func1 (GPtrArray *haystack, gconstpointer needle, GEqualFunc equal_func, guint *index_)
 
static gboolean compare_pending_by_sequence_id (gconstpointer pending, gconstpointer needle_sequence_id)
 
static gboolean compare_pending_by_hash (gconstpointer pending, gconstpointer needle_hash)
 
static bool fl_keyboard_manager_remove_redispatched (FlKeyboardManager *self, uint64_t hash)
 
static void responder_handle_event_callback (bool handled, gpointer user_data_ptr)
 
static uint16_t convert_key_to_char (FlKeyboardViewDelegate *view_delegate, guint keycode, gint group, gint level)
 
static void guarantee_layout (FlKeyboardManager *self, FlKeyEvent *event)
 
FlMethodResponse * get_keyboard_state (FlKeyboardManager *self)
 
static void method_call_handler (FlMethodChannel *channel, FlMethodCall *method_call, gpointer user_data)
 
FlKeyboardManagerfl_keyboard_manager_new (FlBinaryMessenger *messenger, FlKeyboardViewDelegate *view_delegate)
 
static void dispatch_to_responder (gpointer responder_data, gpointer foreach_data_ptr)
 
gboolean fl_keyboard_manager_handle_event (FlKeyboardManager *self, FlKeyEvent *event)
 
gboolean fl_keyboard_manager_is_state_clear (FlKeyboardManager *self)
 
void fl_keyboard_manager_sync_modifier_if_needed (FlKeyboardManager *self, guint state, double event_time)
 
GHashTable * fl_keyboard_manager_get_pressed_state (FlKeyboardManager *self)
 

Variables

static constexpr char kChannelName [] = "flutter/keyboard"
 
static constexpr char kGetKeyboardStateMethod [] = "getKeyboardState"
 

Macro Definition Documentation

◆ DEBUG_PRINT_LAYOUT

#define DEBUG_PRINT_LAYOUT

Definition at line 20 of file fl_keyboard_manager.cc.

◆ FL_TYPE_KEYBOARD_MANAGER_USER_DATA

#define FL_TYPE_KEYBOARD_MANAGER_USER_DATA    fl_keyboard_manager_user_data_get_type()

Definition at line 33 of file fl_keyboard_manager.cc.

Function Documentation

◆ compare_pending_by_hash()

static gboolean compare_pending_by_hash ( gconstpointer  pending,
gconstpointer  needle_hash 
)
static

Definition at line 392 of file fl_keyboard_manager.cc.

393 {
394 uint64_t hash = *reinterpret_cast<const uint64_t*>(needle_hash);
395 return static_cast<const FlKeyboardPendingEvent*>(pending)->hash == hash;
396}
static uint32_t hash(const SkShaderBase::GradientInfo &v)

◆ compare_pending_by_sequence_id()

static gboolean compare_pending_by_sequence_id ( gconstpointer  pending,
gconstpointer  needle_sequence_id 
)
static

Definition at line 382 of file fl_keyboard_manager.cc.

384 {
385 uint64_t sequence_id = *reinterpret_cast<const uint64_t*>(needle_sequence_id);
386 return static_cast<const FlKeyboardPendingEvent*>(pending)->sequence_id ==
387 sequence_id;
388}

◆ convert_key_to_char()

static uint16_t convert_key_to_char ( FlKeyboardViewDelegate *  view_delegate,
guint  keycode,
gint  group,
gint  level 
)
static

Definition at line 456 of file fl_keyboard_manager.cc.

459 {
460 GdkKeymapKey key = {keycode, group, level};
461 constexpr int kBmpMax = 0xD7FF;
462 guint origin = fl_keyboard_view_delegate_lookup_key(view_delegate, &key);
463 return origin < kBmpMax ? origin : 0xFFFF;
464}
guint fl_keyboard_view_delegate_lookup_key(FlKeyboardViewDelegate *self, const GdkKeymapKey *key)

◆ dispatch_to_responder()

static void dispatch_to_responder ( gpointer  responder_data,
gpointer  foreach_data_ptr 
)
static

Definition at line 628 of file fl_keyboard_manager.cc.

629 {
630 DispatchToResponderLoopContext* context =
631 reinterpret_cast<DispatchToResponderLoopContext*>(foreach_data_ptr);
632 FlKeyResponder* responder = FL_KEY_RESPONDER(responder_data);
634 responder, context->event, responder_handle_event_callback,
635 context->user_data, context->specified_logical_key);
636}
void fl_key_responder_handle_event(FlKeyResponder *self, FlKeyEvent *event, FlKeyResponderAsyncCallback callback, gpointer user_data, uint64_t specified_logical_key)
static void responder_handle_event_callback(bool handled, gpointer user_data_ptr)

◆ fl_keyboard_manager_class_init()

static void fl_keyboard_manager_class_init ( FlKeyboardManagerClass *  klass)
static

Definition at line 304 of file fl_keyboard_manager.cc.

304 {
305 G_OBJECT_CLASS(klass)->dispose = fl_keyboard_manager_dispose;
306}
static void fl_keyboard_manager_dispose(GObject *object)

◆ fl_keyboard_manager_dispose()

static void fl_keyboard_manager_dispose ( GObject *  object)
static

Definition at line 330 of file fl_keyboard_manager.cc.

330 {
331 FlKeyboardManager* self = FL_KEYBOARD_MANAGER(object);
332
333 if (self->view_delegate != nullptr) {
335 nullptr);
336 g_object_remove_weak_pointer(
337 G_OBJECT(self->view_delegate),
338 reinterpret_cast<gpointer*>(&(self->view_delegate)));
339 self->view_delegate = nullptr;
340 }
341
342 self->derived_layout.reset();
343 self->keycode_to_goals.reset();
344 self->logical_to_mandatory_goals.reset();
345
346 g_ptr_array_free(self->responder_list, TRUE);
347 g_ptr_array_set_free_func(self->pending_responds, g_object_unref);
348 g_ptr_array_free(self->pending_responds, TRUE);
349 g_ptr_array_free(self->pending_redispatches, TRUE);
350
351 G_OBJECT_CLASS(fl_keyboard_manager_parent_class)->dispose(object);
352}
typedefG_BEGIN_DECLS struct _FlKeyboardManager FlKeyboardManager
void fl_keyboard_view_delegate_subscribe_to_layout_change(FlKeyboardViewDelegate *self, KeyboardLayoutNotifier notifier)

◆ fl_keyboard_manager_get_event_hash()

static uint64_t fl_keyboard_manager_get_event_hash ( FlKeyEvent event)
static

Definition at line 168 of file fl_keyboard_manager.cc.

168 {
169 // Combine the event timestamp, the type of event, and the hardware keycode
170 // (scan code) of the event to come up with a unique id for this event that
171 // can be derived solely from the event data itself, so that we can identify
172 // whether or not we have seen this event already.
173 guint64 type =
174 static_cast<uint64_t>(event->is_press ? GDK_KEY_PRESS : GDK_KEY_RELEASE);
175 guint64 keycode = static_cast<uint64_t>(event->keycode);
176 return (event->time & 0xffffffff) | ((type & 0xffff) << 32) |
177 ((keycode & 0xffff) << 48);
178}
GLenum type
FlKeyEvent * event
guint32 time
Definition: fl_key_event.h:24

◆ fl_keyboard_manager_get_pressed_state()

GHashTable * fl_keyboard_manager_get_pressed_state ( FlKeyboardManager manager)

fl_keyboard_manager_get_pressed_state: @manager: the FlKeyboardManager self.

Returns the keyboard pressed state. The hash table contains one entry per pressed keys, mapping from the logical key to the physical key.*

Definition at line 688 of file fl_keyboard_manager.cc.

688 {
689 g_return_val_if_fail(FL_IS_KEYBOARD_MANAGER(self), nullptr);
690
691 // The embedder responder is the first element in
692 // FlKeyboardManager.responder_list.
693 FlKeyEmbedderResponder* responder =
694 FL_KEY_EMBEDDER_RESPONDER(g_ptr_array_index(self->responder_list, 0));
696}
GHashTable * fl_key_embedder_responder_get_pressed_state(FlKeyEmbedderResponder *self)

◆ fl_keyboard_manager_handle_event()

gboolean fl_keyboard_manager_handle_event ( FlKeyboardManager manager,
FlKeyEvent event 
)

fl_keyboard_manager_handle_event: @manager: the FlKeyboardManager self. @event: the event to be dispatched. It is usually a wrap of a GdkEventKey. This event will be managed and released by FlKeyboardManager.

Make the manager process a system key event. This might eventually send messages to the framework, trigger text input effects, or redispatch the event back to the system.

Definition at line 638 of file fl_keyboard_manager.cc.

639 {
640 g_return_val_if_fail(FL_IS_KEYBOARD_MANAGER(self), FALSE);
641 g_return_val_if_fail(event != nullptr, FALSE);
642 g_return_val_if_fail(self->view_delegate != nullptr, FALSE);
643
645
646 uint64_t incoming_hash = fl_keyboard_manager_get_event_hash(event);
647 if (fl_keyboard_manager_remove_redispatched(self, incoming_hash)) {
648 return FALSE;
649 }
650
651 FlKeyboardPendingEvent* pending = fl_keyboard_pending_event_new(
652 std::unique_ptr<FlKeyEvent>(event), ++self->last_sequence_id,
653 self->responder_list->len);
654
655 g_ptr_array_add(self->pending_responds, pending);
656 FlKeyboardManagerUserData* user_data =
657 fl_keyboard_manager_user_data_new(self, pending->sequence_id);
658 DispatchToResponderLoopContext data{
659 .event = event,
660 .specified_logical_key =
661 get_logical_key_from_layout(event, *self->derived_layout),
662 .user_data = user_data,
663 };
664 g_ptr_array_foreach(self->responder_list, dispatch_to_responder, &data);
665
666 return TRUE;
667}
static FlKeyboardPendingEvent * fl_keyboard_pending_event_new(std::unique_ptr< FlKeyEvent > event, uint64_t sequence_id, size_t to_reply)
static uint64_t get_logical_key_from_layout(const FlKeyEvent *event, const DerivedLayout &layout)
static uint64_t fl_keyboard_manager_get_event_hash(FlKeyEvent *event)
static void guarantee_layout(FlKeyboardManager *self, FlKeyEvent *event)
static FlKeyboardManagerUserData * fl_keyboard_manager_user_data_new(FlKeyboardManager *manager, uint64_t sequence_id)
static void dispatch_to_responder(gpointer responder_data, gpointer foreach_data_ptr)
static bool fl_keyboard_manager_remove_redispatched(FlKeyboardManager *self, uint64_t hash)
return FALSE
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
void * user_data

◆ fl_keyboard_manager_init()

static void fl_keyboard_manager_init ( FlKeyboardManager self)
static

Definition at line 308 of file fl_keyboard_manager.cc.

308 {
309 self->derived_layout = std::make_unique<DerivedLayout>();
310
311 self->keycode_to_goals =
312 std::make_unique<std::map<uint16_t, const LayoutGoal*>>();
313 self->logical_to_mandatory_goals =
314 std::make_unique<std::map<uint64_t, const LayoutGoal*>>();
315 for (const LayoutGoal& goal : layout_goals) {
316 (*self->keycode_to_goals)[goal.keycode] = &goal;
317 if (goal.mandatory) {
318 (*self->logical_to_mandatory_goals)[goal.logical_key] = &goal;
319 }
320 }
321
322 self->responder_list = g_ptr_array_new_with_free_func(g_object_unref);
323
324 self->pending_responds = g_ptr_array_new();
325 self->pending_redispatches = g_ptr_array_new_with_free_func(g_object_unref);
326
327 self->last_sequence_id = 1;
328}
if(end==-1)
const std::vector< LayoutGoal > layout_goals

◆ fl_keyboard_manager_is_state_clear()

gboolean fl_keyboard_manager_is_state_clear ( FlKeyboardManager manager)

fl_keyboard_manager_is_state_clear: @manager: the FlKeyboardManager self.

A debug-only method that queries whether the manager's various states are cleared, i.e. no pending events for redispatching or for responding.

Returns: true if the manager's various states are cleared.

Definition at line 669 of file fl_keyboard_manager.cc.

669 {
670 g_return_val_if_fail(FL_IS_KEYBOARD_MANAGER(self), FALSE);
671 return self->pending_responds->len == 0 &&
672 self->pending_redispatches->len == 0;
673}

◆ fl_keyboard_manager_new()

FlKeyboardManager * fl_keyboard_manager_new ( FlBinaryMessenger *  messenger,
FlKeyboardViewDelegate *  view_delegate 
)

FlKeyboardManager:

Processes keyboard events and cooperate with TextInputPlugin.

A keyboard event goes through a few sections, each can choose to handle the event, and only unhandled events can move to the next section:

  • Keyboard: Dispatch to the embedder responder and the channel responder simultaneously. After both responders have responded (asynchronously), the event is considered handled if either responder handles it.
  • Text input: Events are sent to IM filter (usually owned by TextInputPlugin) and are handled synchronously.
  • Redispatching: Events are inserted back to the system for redispatching. fl_keyboard_manager_new: @view_delegate: An interface that the manager requires to communicate with the platform. Usually implemented by FlView.

Create a new FlKeyboardManager.

Returns: a new FlKeyboardManager.

Definition at line 585 of file fl_keyboard_manager.cc.

587 {
588 g_return_val_if_fail(FL_IS_KEYBOARD_VIEW_DELEGATE(view_delegate), nullptr);
589
590 FlKeyboardManager* self = FL_KEYBOARD_MANAGER(
591 g_object_new(fl_keyboard_manager_get_type(), nullptr));
592
593 self->view_delegate = view_delegate;
594 g_object_add_weak_pointer(
595 G_OBJECT(view_delegate),
596 reinterpret_cast<gpointer*>(&(self->view_delegate)));
597
598 // The embedder responder must be added before the channel responder.
599 g_ptr_array_add(
600 self->responder_list,
601 FL_KEY_RESPONDER(fl_key_embedder_responder_new(
603 void* callback_user_data, void* send_key_event_user_data) {
605 FL_KEYBOARD_MANAGER(send_key_event_user_data);
606 g_return_if_fail(self->view_delegate != nullptr);
608 self->view_delegate, event, callback, callback_user_data);
609 },
610 self)));
611 g_ptr_array_add(self->responder_list,
612 FL_KEY_RESPONDER(fl_key_channel_responder_new(
614
616 self->view_delegate, [self]() { self->derived_layout->clear(); });
617
618 // Setup the flutter/keyboard channel.
619 g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
620 self->channel =
621 fl_method_channel_new(messenger, kChannelName, FL_METHOD_CODEC(codec));
623 self, nullptr);
624 return self;
625}
void(* FlutterKeyEventCallback)(bool, void *)
Definition: embedder.h:1155
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
FlKeyChannelResponder * fl_key_channel_responder_new(FlBinaryMessenger *messenger, FlKeyChannelResponderMock *mock)
FlKeyEmbedderResponder * fl_key_embedder_responder_new(EmbedderSendKeyEvent send_key_event, void *send_key_event_user_data)
static void method_call_handler(FlMethodChannel *channel, FlMethodCall *method_call, gpointer user_data)
static constexpr char kChannelName[]
void fl_keyboard_view_delegate_send_key_event(FlKeyboardViewDelegate *self, const FlutterKeyEvent *event, FlutterKeyEventCallback callback, void *user_data)
FlBinaryMessenger * fl_keyboard_view_delegate_get_messenger(FlKeyboardViewDelegate *self)
G_MODULE_EXPORT FlMethodChannel * fl_method_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMethodCodec *codec)
G_MODULE_EXPORT void fl_method_channel_set_method_call_handler(FlMethodChannel *self, FlMethodChannelMethodCallHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
G_MODULE_EXPORT FlStandardMethodCodec * fl_standard_method_codec_new()

◆ fl_keyboard_manager_remove_redispatched()

static bool fl_keyboard_manager_remove_redispatched ( FlKeyboardManager self,
uint64_t  hash 
)
static

Definition at line 402 of file fl_keyboard_manager.cc.

403 {
404 guint result_index;
405 gboolean found = g_ptr_array_find_with_equal_func1(
406 self->pending_redispatches, static_cast<const uint64_t*>(&hash),
407 compare_pending_by_hash, &result_index);
408 if (found) {
409 // The removed object is freed due to `pending_redispatches`'s free_func.
410 g_ptr_array_remove_index_fast(self->pending_redispatches, result_index);
411 return TRUE;
412 } else {
413 return FALSE;
414 }
415}
static gboolean g_ptr_array_find_with_equal_func1(GPtrArray *haystack, gconstpointer needle, GEqualFunc equal_func, guint *index_)
static gboolean compare_pending_by_hash(gconstpointer pending, gconstpointer needle_hash)

◆ fl_keyboard_manager_sync_modifier_if_needed()

void fl_keyboard_manager_sync_modifier_if_needed ( FlKeyboardManager manager,
guint  state,
double  event_time 
)

fl_keyboard_manager_sync_modifier_if_needed: @manager: the FlKeyboardManager self. @state: the state of the modifiers mask. @event_time: the time attribute of the incoming GDK event.

If needed, synthesize modifier keys up and down event by comparing their current pressing states with the given modifiers mask.

Definition at line 675 of file fl_keyboard_manager.cc.

677 {
678 g_return_if_fail(FL_IS_KEYBOARD_MANAGER(self));
679
680 // The embedder responder is the first element in
681 // FlKeyboardManager.responder_list.
682 FlKeyEmbedderResponder* responder =
683 FL_KEY_EMBEDDER_RESPONDER(g_ptr_array_index(self->responder_list, 0));
685 event_time);
686}
AtkStateType state
void fl_key_embedder_responder_sync_modifiers_if_needed(FlKeyEmbedderResponder *responder, guint state, double event_time)

◆ fl_keyboard_manager_user_data_class_init()

static void fl_keyboard_manager_user_data_class_init ( FlKeyboardManagerUserDataClass *  klass)
static

Definition at line 229 of file fl_keyboard_manager.cc.

230 {
231 G_OBJECT_CLASS(klass)->dispose = fl_keyboard_manager_user_data_dispose;
232}

◆ fl_keyboard_manager_user_data_init()

static void fl_keyboard_manager_user_data_init ( FlKeyboardManagerUserData *  self)
static

Definition at line 234 of file fl_keyboard_manager.cc.

235 {}

◆ fl_keyboard_manager_user_data_new()

static FlKeyboardManagerUserData * fl_keyboard_manager_user_data_new ( FlKeyboardManager manager,
uint64_t  sequence_id 
)
static

Definition at line 238 of file fl_keyboard_manager.cc.

240 {
241 FlKeyboardManagerUserData* self = FL_KEYBOARD_MANAGER_USER_DATA(
242 g_object_new(fl_keyboard_manager_user_data_get_type(), nullptr));
243
244 self->manager = manager;
245 // Add a weak pointer so we can know if the key event responder disappeared
246 // while the framework was responding.
247 g_object_add_weak_pointer(G_OBJECT(manager),
248 reinterpret_cast<gpointer*>(&(self->manager)));
249 self->sequence_id = sequence_id;
250 return self;
251}
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font manager
Definition: switches.h:218

◆ fl_keyboard_pending_event_class_init()

static void fl_keyboard_pending_event_class_init ( FlKeyboardPendingEventClass *  klass)
static

Definition at line 159 of file fl_keyboard_manager.cc.

160 {
161 G_OBJECT_CLASS(klass)->dispose = fl_keyboard_pending_event_dispose;
162}
static void fl_keyboard_pending_event_dispose(GObject *object)

◆ fl_keyboard_pending_event_dispose()

static void fl_keyboard_pending_event_dispose ( GObject *  object)
static

Definition at line 147 of file fl_keyboard_manager.cc.

147 {
148 // Redundant, but added so that we don't get a warning about unused function
149 // for FL_IS_KEYBOARD_PENDING_EVENT.
150 g_return_if_fail(FL_IS_KEYBOARD_PENDING_EVENT(object));
151
152 FlKeyboardPendingEvent* self = FL_KEYBOARD_PENDING_EVENT(object);
153 if (self->event != nullptr) {
154 fl_key_event_dispose(self->event.release());
155 }
156 G_OBJECT_CLASS(fl_keyboard_pending_event_parent_class)->dispose(object);
157}
void fl_key_event_dispose(FlKeyEvent *event)
Definition: fl_key_event.cc:32

◆ fl_keyboard_pending_event_init()

static void fl_keyboard_pending_event_init ( FlKeyboardPendingEvent *  self)
static

Definition at line 164 of file fl_keyboard_manager.cc.

164{}

◆ fl_keyboard_pending_event_new()

static FlKeyboardPendingEvent * fl_keyboard_pending_event_new ( std::unique_ptr< FlKeyEvent event,
uint64_t  sequence_id,
size_t  to_reply 
)
static

Definition at line 184 of file fl_keyboard_manager.cc.

187 {
188 FlKeyboardPendingEvent* self = FL_KEYBOARD_PENDING_EVENT(
189 g_object_new(fl_keyboard_pending_event_get_type(), nullptr));
190
191 self->event = std::move(event);
192 self->sequence_id = sequence_id;
193 self->unreplied = to_reply;
194 self->any_handled = false;
195 self->hash = fl_keyboard_manager_get_event_hash(self->event.get());
196 return self;
197}

◆ G_DECLARE_FINAL_TYPE() [1/2]

G_DECLARE_FINAL_TYPE ( FlKeyboardManagerUserData  ,
fl_keyboard_manager_user_data  ,
FL  ,
KEYBOARD_MANAGER_USER_DATA  ,
GObject   
)

◆ G_DECLARE_FINAL_TYPE() [2/2]

G_DECLARE_FINAL_TYPE ( FlKeyboardPendingEvent  ,
fl_keyboard_pending_event  ,
FL  ,
KEYBOARD_PENDING_EVENT  ,
GObject   
)

◆ G_DEFINE_TYPE() [1/2]

G_DEFINE_TYPE ( FlKeyboardManager  ,
fl_keyboard_manager  ,
G_TYPE_OBJECT   
)

◆ G_DEFINE_TYPE() [2/2]

G_DEFINE_TYPE ( FlKeyboardManagerUserData  ,
fl_keyboard_manager_user_data  ,
G_TYPE_OBJECT   
)

Definition at line 215 of file fl_keyboard_manager.cc.

219 {
220 g_return_if_fail(FL_IS_KEYBOARD_MANAGER_USER_DATA(object));
221 FlKeyboardManagerUserData* self = FL_KEYBOARD_MANAGER_USER_DATA(object);
222 if (self->manager != nullptr) {
223 g_object_remove_weak_pointer(G_OBJECT(self->manager),
224 reinterpret_cast<gpointer*>(&(self->manager)));
225 self->manager = nullptr;
226 }
227}

◆ g_ptr_array_find_with_equal_func1()

static gboolean g_ptr_array_find_with_equal_func1 ( GPtrArray *  haystack,
gconstpointer  needle,
GEqualFunc  equal_func,
guint *  index_ 
)
static

Definition at line 359 of file fl_keyboard_manager.cc.

362 {
363 guint i;
364 g_return_val_if_fail(haystack != NULL, FALSE);
365 if (equal_func == NULL) {
366 equal_func = g_direct_equal;
367 }
368 for (i = 0; i < haystack->len; i++) {
369 if (equal_func(g_ptr_array_index(haystack, i), needle)) {
370 if (index_ != NULL) {
371 *index_ = i;
372 }
373 return TRUE;
374 }
375 }
376
377 return FALSE;
378}

◆ get_keyboard_state()

FlMethodResponse * get_keyboard_state ( FlKeyboardManager self)

Definition at line 544 of file fl_keyboard_manager.cc.

544 {
545 g_autoptr(FlValue) result = fl_value_new_map();
546
547 GHashTable* pressing_records =
549
550 g_hash_table_foreach(
551 pressing_records,
552 [](gpointer key, gpointer value, gpointer user_data) {
553 int64_t physical_key = reinterpret_cast<int64_t>(key);
554 int64_t logical_key = reinterpret_cast<int64_t>(value);
555 FlValue* fl_value_map = reinterpret_cast<FlValue*>(user_data);
556
557 fl_value_set_take(fl_value_map, fl_value_new_int(physical_key),
558 fl_value_new_int(logical_key));
559 },
560 result);
561 return FL_METHOD_RESPONSE(fl_method_success_response_new(result));
562}
GHashTable * fl_keyboard_view_delegate_get_keyboard_state(FlKeyboardViewDelegate *self)
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
uint8_t value
GAsyncResult * result
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:366
G_MODULE_EXPORT void fl_value_set_take(FlValue *self, FlValue *key, FlValue *value)
Definition: fl_value.cc:618
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

◆ get_logical_key_from_layout()

static uint64_t get_logical_key_from_layout ( const FlKeyEvent event,
const DerivedLayout &  layout 
)
static

Definition at line 97 of file fl_keyboard_manager.cc.

98 {
99 guint8 group = event->group;
100 guint16 keycode = event->keycode;
101 if (keycode >= kLayoutSize) {
102 return 0;
103 }
104
105 auto found_group_layout = layout.find(group);
106 if (found_group_layout != layout.end()) {
107 return found_group_layout->second[keycode];
108 }
109 return 0;
110}

◆ guarantee_layout()

static void guarantee_layout ( FlKeyboardManager self,
FlKeyEvent event 
)
static

Definition at line 468 of file fl_keyboard_manager.cc.

468 {
469 guint8 group = event->group;
470 if (self->derived_layout->find(group) != self->derived_layout->end()) {
471 return;
472 }
473 if (self->keycode_to_goals->find(event->keycode) ==
474 self->keycode_to_goals->end()) {
475 return;
476 }
477
478 DerivedGroupLayout& layout = (*self->derived_layout)[group];
479
480 // Clone all mandatory goals. Each goal is removed from this cloned map when
481 // fulfilled, and the remaining ones will be assigned to a default position.
482 std::map<uint64_t, const LayoutGoal*> remaining_mandatory_goals =
483 *self->logical_to_mandatory_goals;
484
485#ifdef DEBUG_PRINT_LAYOUT
486 std::string debug_layout_data;
487 for (uint16_t keycode = 0; keycode < 128; keycode += 1) {
488 std::vector<uint16_t> this_key_clues = {
489 convert_key_to_char(self->view_delegate, keycode, group, 0),
490 convert_key_to_char(self->view_delegate, keycode, group, 1), // Shift
491 };
492 debug_format_layout_data(debug_layout_data, keycode, this_key_clues[0],
493 this_key_clues[1]);
494 }
495#endif
496
497 // It's important to only traverse layout goals instead of all keycodes.
498 // Some key codes outside of the standard keyboard also gives alpha-numeric
499 // letters, and will therefore take over mandatory goals from standard
500 // keyboard keys if they come first. Example: French keyboard digit 1.
501 for (const LayoutGoal& keycode_goal : layout_goals) {
502 uint16_t keycode = keycode_goal.keycode;
503 std::vector<uint16_t> this_key_clues = {
504 convert_key_to_char(self->view_delegate, keycode, group, 0),
505 convert_key_to_char(self->view_delegate, keycode, group, 1), // Shift
506 };
507
508 // The logical key should be the first available clue from below:
509 //
510 // - Mandatory goal, if it matches any clue. This ensures that all alnum
511 // keys can be found somewhere.
512 // - US layout, if neither clue of the key is EASCII. This ensures that
513 // there are no non-latin logical keys.
514 // - A value derived on the fly from keycode & keyval.
515 for (uint16_t clue : this_key_clues) {
516 auto matching_goal = remaining_mandatory_goals.find(clue);
517 if (matching_goal != remaining_mandatory_goals.end()) {
518 // Found a key that produces a mandatory char. Use it.
519 g_return_if_fail(layout[keycode] == 0);
520 layout[keycode] = clue;
521 remaining_mandatory_goals.erase(matching_goal);
522 break;
523 }
524 }
525 bool has_any_eascii =
526 is_eascii(this_key_clues[0]) || is_eascii(this_key_clues[1]);
527 // See if any produced char meets the requirement as a logical key.
528 if (layout[keycode] == 0 && !has_any_eascii) {
529 auto found_us_layout = self->keycode_to_goals->find(keycode);
530 if (found_us_layout != self->keycode_to_goals->end()) {
531 layout[keycode] = found_us_layout->second->logical_key;
532 }
533 }
534 }
535
536 // Ensure all mandatory goals are assigned.
537 for (const auto mandatory_goal_iter : remaining_mandatory_goals) {
538 const LayoutGoal* goal = mandatory_goal_iter.second;
539 layout[goal->keycode] = goal->logical_key;
540 }
541}
static uint16_t convert_key_to_char(FlKeyboardViewDelegate *view_delegate, guint keycode, gint group, gint level)
uint16_t keycode
Definition: key_mapping.h:42
uint64_t logical_key
Definition: key_mapping.h:45
guint16 keycode
Definition: fl_key_event.h:28

◆ method_call_handler()

static void method_call_handler ( FlMethodChannel *  channel,
FlMethodCall *  method_call,
gpointer  user_data 
)
static

Definition at line 565 of file fl_keyboard_manager.cc.

567 {
568 FlKeyboardManager* self = FL_KEYBOARD_MANAGER(user_data);
569
570 const gchar* method = fl_method_call_get_name(method_call);
571
572 g_autoptr(FlMethodResponse) response = nullptr;
573 if (strcmp(method, kGetKeyboardStateMethod) == 0) {
574 response = get_keyboard_state(self);
575 } else {
576 response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
577 }
578
579 g_autoptr(GError) error = nullptr;
580 if (!fl_method_call_respond(method_call, response, &error)) {
581 g_warning("Failed to send method call response: %s", error->message);
582 }
583}
FlMethodResponse * get_keyboard_state(FlKeyboardManager *self)
static constexpr char kGetKeyboardStateMethod[]
G_MODULE_EXPORT const gchar * fl_method_call_get_name(FlMethodCall *self)
G_MODULE_EXPORT gboolean fl_method_call_respond(FlMethodCall *self, FlMethodResponse *response, GError **error)
G_BEGIN_DECLS G_MODULE_EXPORT FlMethodCall * method_call
G_MODULE_EXPORT FlMethodNotImplementedResponse * fl_method_not_implemented_response_new()
const uint8_t uint32_t uint32_t GError ** error

◆ responder_handle_event_callback()

static void responder_handle_event_callback ( bool  handled,
gpointer  user_data_ptr 
)
static

Definition at line 418 of file fl_keyboard_manager.cc.

419 {
420 g_return_if_fail(FL_IS_KEYBOARD_MANAGER_USER_DATA(user_data_ptr));
421 FlKeyboardManagerUserData* user_data =
422 FL_KEYBOARD_MANAGER_USER_DATA(user_data_ptr);
423 FlKeyboardManager* self = user_data->manager;
424 g_return_if_fail(self->view_delegate != nullptr);
425
426 guint result_index = -1;
427 gboolean found = g_ptr_array_find_with_equal_func1(
428 self->pending_responds, &user_data->sequence_id,
429 compare_pending_by_sequence_id, &result_index);
430 g_return_if_fail(found);
431 FlKeyboardPendingEvent* pending = FL_KEYBOARD_PENDING_EVENT(
432 g_ptr_array_index(self->pending_responds, result_index));
433 g_return_if_fail(pending != nullptr);
434 g_return_if_fail(pending->unreplied > 0);
435 pending->unreplied -= 1;
436 pending->any_handled = pending->any_handled || handled;
437 // All responders have replied.
438 if (pending->unreplied == 0) {
439 g_object_unref(user_data_ptr);
440 gpointer removed =
441 g_ptr_array_remove_index_fast(self->pending_responds, result_index);
442 g_return_if_fail(removed == pending);
443 bool should_redispatch = !pending->any_handled &&
445 self->view_delegate, pending->event.get());
446 if (should_redispatch) {
447 g_ptr_array_add(self->pending_redispatches, pending);
449 std::move(pending->event));
450 } else {
451 g_object_unref(pending);
452 }
453 }
454}
static gboolean compare_pending_by_sequence_id(gconstpointer pending, gconstpointer needle_sequence_id)
gboolean fl_keyboard_view_delegate_text_filter_key_press(FlKeyboardViewDelegate *self, FlKeyEvent *event)
void fl_keyboard_view_delegate_redispatch_event(FlKeyboardViewDelegate *self, std::unique_ptr< FlKeyEvent > event)

Variable Documentation

◆ kChannelName

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

Definition at line 22 of file fl_keyboard_manager.cc.

◆ kGetKeyboardStateMethod

constexpr char kGetKeyboardStateMethod[] = "getKeyboardState"
staticconstexpr

Definition at line 23 of file fl_keyboard_manager.cc.