Flutter Engine
The Flutter Engine
fl_view.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/public/flutter_linux/fl_view.h"
6
7#include "flutter/shell/platform/linux/fl_view_private.h"
8
9#include <atk/atk.h>
10#include <gtk/gtk-a11y.h>
11
12#include <cstring>
13
14#include "flutter/shell/platform/linux/fl_accessible_node.h"
15#include "flutter/shell/platform/linux/fl_backing_store_provider.h"
16#include "flutter/shell/platform/linux/fl_engine_private.h"
17#include "flutter/shell/platform/linux/fl_key_event.h"
18#include "flutter/shell/platform/linux/fl_keyboard_manager.h"
19#include "flutter/shell/platform/linux/fl_keyboard_view_delegate.h"
20#include "flutter/shell/platform/linux/fl_mouse_cursor_plugin.h"
21#include "flutter/shell/platform/linux/fl_platform_plugin.h"
22#include "flutter/shell/platform/linux/fl_plugin_registrar_private.h"
23#include "flutter/shell/platform/linux/fl_renderer_gdk.h"
24#include "flutter/shell/platform/linux/fl_scrolling_manager.h"
25#include "flutter/shell/platform/linux/fl_scrolling_view_delegate.h"
26#include "flutter/shell/platform/linux/fl_socket_accessible.h"
27#include "flutter/shell/platform/linux/fl_text_input_plugin.h"
28#include "flutter/shell/platform/linux/fl_text_input_view_delegate.h"
29#include "flutter/shell/platform/linux/fl_view_accessible.h"
30#include "flutter/shell/platform/linux/public/flutter_linux/fl_engine.h"
31#include "flutter/shell/platform/linux/public/flutter_linux/fl_plugin_registry.h"
32
33static constexpr int kMicrosecondsPerMillisecond = 1000;
34
35struct _FlView {
37
38 // Project being run.
39 FlDartProject* project;
40
41 // Rendering output.
42 FlRendererGdk* renderer;
43
44 // Engine running @project.
45 FlEngine* engine;
46
47 // Pointer button state recorded for sending status updates.
48 int64_t button_state;
49
50 // Current state information for the window associated with this view.
51 GdkWindowState window_state;
52
53 // Flutter system channel handlers.
55 FlScrollingManager* scrolling_manager;
56 FlTextInputPlugin* text_input_plugin;
57 FlMouseCursorPlugin* mouse_cursor_plugin;
58 FlPlatformPlugin* platform_plugin;
59
60 GtkWidget* event_box;
61 GtkGLArea* gl_area;
62
63 // Tracks whether mouse pointer is inside the view.
65
66 /* FlKeyboardViewDelegate related properties */
68 GdkKeymap* keymap;
69 gulong keymap_keys_changed_cb_id; // Signal connection ID for
70 // keymap-keys-changed
71 gulong window_state_cb_id; // Signal connection ID for window-state-changed
72
73 // Accessible tree from Flutter, exposed as an AtkPlug.
74 FlViewAccessible* view_accessible;
75};
76
78
80 FlPluginRegistryInterface* iface);
81
83 FlKeyboardViewDelegateInterface* iface);
84
86 FlScrollingViewDelegateInterface* iface);
87
89 FlTextInputViewDelegateInterface* iface);
90
92 FlView,
93 fl_view,
94 GTK_TYPE_BOX,
95 G_IMPLEMENT_INTERFACE(fl_plugin_registry_get_type(),
97 G_IMPLEMENT_INTERFACE(fl_keyboard_view_delegate_get_type(),
99 G_IMPLEMENT_INTERFACE(fl_scrolling_view_delegate_get_type(),
101 G_IMPLEMENT_INTERFACE(fl_text_input_view_delegate_get_type(),
103
104// Signal handler for GtkWidget::delete-event
105static gboolean window_delete_event_cb(FlView* self) {
107 // Stop the event from propagating.
108 return TRUE;
109}
110
111// Initialize keyboard manager.
112static void init_keyboard(FlView* self) {
113 FlBinaryMessenger* messenger = fl_engine_get_binary_messenger(self->engine);
114
115 GdkWindow* window =
116 gtk_widget_get_window(gtk_widget_get_toplevel(GTK_WIDGET(self)));
117 g_return_if_fail(GDK_IS_WINDOW(window));
118 g_autoptr(GtkIMContext) im_context = gtk_im_multicontext_new();
119 gtk_im_context_set_client_window(im_context, window);
120
121 g_clear_object(&self->text_input_plugin);
122 self->text_input_plugin = fl_text_input_plugin_new(
123 messenger, im_context, FL_TEXT_INPUT_VIEW_DELEGATE(self));
124 g_clear_object(&self->keyboard_manager);
125 self->keyboard_manager =
126 fl_keyboard_manager_new(messenger, FL_KEYBOARD_VIEW_DELEGATE(self));
127}
128
129static void init_scrolling(FlView* self) {
130 g_clear_object(&self->scrolling_manager);
131 self->scrolling_manager =
132 fl_scrolling_manager_new(FL_SCROLLING_VIEW_DELEGATE(self));
133}
134
136 GdkDevice* device = gdk_event_get_source_device(event);
137 GdkInputSource source = gdk_device_get_source(device);
138 switch (source) {
139 case GDK_SOURCE_PEN:
140 case GDK_SOURCE_ERASER:
141 case GDK_SOURCE_CURSOR:
142 case GDK_SOURCE_TABLET_PAD:
144 case GDK_SOURCE_TOUCHSCREEN:
146 case GDK_SOURCE_TOUCHPAD: // trackpad device type is reserved for gestures
147 case GDK_SOURCE_TRACKPOINT:
148 case GDK_SOURCE_KEYBOARD:
149 case GDK_SOURCE_MOUSE:
151 }
152}
153
154// Converts a GDK button event into a Flutter event and sends it to the engine.
155static gboolean send_pointer_button_event(FlView* self, GdkEvent* event) {
156 guint event_time = gdk_event_get_time(event);
157 GdkEventType event_type = gdk_event_get_event_type(event);
158 GdkModifierType event_state = static_cast<GdkModifierType>(0);
159 gdk_event_get_state(event, &event_state);
160 guint event_button = 0;
161 gdk_event_get_button(event, &event_button);
162 gdouble event_x = 0.0, event_y = 0.0;
163 gdk_event_get_coords(event, &event_x, &event_y);
164
165 int64_t button;
166 switch (event_button) {
167 case 1:
169 break;
170 case 2:
172 break;
173 case 3:
175 break;
176 default:
177 return FALSE;
178 }
179 int old_button_state = self->button_state;
181 if (event_type == GDK_BUTTON_PRESS) {
182 // Drop the event if Flutter already thinks the button is down.
183 if ((self->button_state & button) != 0) {
184 return FALSE;
185 }
186 self->button_state ^= button;
187
188 phase = old_button_state == 0 ? kDown : kMove;
189 } else if (event_type == GDK_BUTTON_RELEASE) {
190 // Drop the event if Flutter already thinks the button is up.
191 if ((self->button_state & button) == 0) {
192 return FALSE;
193 }
194 self->button_state ^= button;
195
196 phase = self->button_state == 0 ? kUp : kMove;
197 }
198
199 if (self->engine == nullptr) {
200 return FALSE;
201 }
202
203 gint scale_factor = gtk_widget_get_scale_factor(GTK_WIDGET(self));
205 self->scrolling_manager, event_x * scale_factor, event_y * scale_factor);
207 event_state, event_time);
209 self->engine, phase, event_time * kMicrosecondsPerMillisecond,
210 event_x * scale_factor, event_y * scale_factor,
211 get_device_kind((GdkEvent*)event), 0, 0, self->button_state);
212
213 return TRUE;
214}
215
216// Generates a mouse pointer event if the pointer appears inside the window.
217static void check_pointer_inside(FlView* self, GdkEvent* event) {
218 if (!self->pointer_inside) {
219 self->pointer_inside = TRUE;
220
221 gdouble x, y;
222 if (gdk_event_get_coords(event, &x, &y)) {
223 gint scale_factor = gtk_widget_get_scale_factor(GTK_WIDGET(self));
224
226 self->engine, kAdd,
227 gdk_event_get_time(event) * kMicrosecondsPerMillisecond,
228 x * scale_factor, y * scale_factor, get_device_kind(event), 0, 0,
229 self->button_state);
230 }
231 }
232}
233
234// Updates the engine with the current window metrics.
235static void handle_geometry_changed(FlView* self) {
236 GtkAllocation allocation;
237 gtk_widget_get_allocation(GTK_WIDGET(self), &allocation);
238 gint scale_factor = gtk_widget_get_scale_factor(GTK_WIDGET(self));
240 self->engine, allocation.width * scale_factor,
241 allocation.height * scale_factor, scale_factor);
242
243 // Make sure the view has been realized and its size has been allocated before
244 // waiting for a frame. `fl_view_realize()` and `fl_view_size_allocate()` may
245 // be called in either order depending on the order in which the window is
246 // shown and the view is added to a container in the app runner.
247 //
248 // Note: `gtk_widget_init()` initializes the size allocation to 1x1.
249 if (allocation.width > 1 && allocation.height > 1 &&
250 gtk_widget_get_realized(GTK_WIDGET(self))) {
251 fl_renderer_wait_for_frame(FL_RENDERER(self->renderer),
252 allocation.width * scale_factor,
253 allocation.height * scale_factor);
254 }
255}
256
257// Called when the engine updates accessibility.
258static void update_semantics_cb(FlEngine* engine,
260 gpointer user_data) {
261 FlView* self = FL_VIEW(user_data);
262
264}
265
266// Invoked by the engine right before the engine is restarted.
267//
268// This method should reset states to be as if the engine had just been started,
269// which usually indicates the user has requested a hot restart (Shift-R in the
270// Flutter CLI.)
271static void on_pre_engine_restart_cb(FlEngine* engine, gpointer user_data) {
272 FlView* self = FL_VIEW(user_data);
273
276}
277
278// Implements FlPluginRegistry::get_registrar_for_plugin.
279static FlPluginRegistrar* fl_view_get_registrar_for_plugin(
280 FlPluginRegistry* registry,
281 const gchar* name) {
282 FlView* self = FL_VIEW(registry);
283
287}
288
290 FlPluginRegistryInterface* iface) {
291 iface->get_registrar_for_plugin = fl_view_get_registrar_for_plugin;
292}
293
295 FlKeyboardViewDelegateInterface* iface) {
296 iface->send_key_event =
297 [](FlKeyboardViewDelegate* view_delegate, const FlutterKeyEvent* event,
299 FlView* self = FL_VIEW(view_delegate);
300 if (self->engine != nullptr) {
302 };
303 };
304
305 iface->text_filter_key_press = [](FlKeyboardViewDelegate* view_delegate,
306 FlKeyEvent* event) {
307 FlView* self = FL_VIEW(view_delegate);
308 return fl_text_input_plugin_filter_keypress(self->text_input_plugin, event);
309 };
310
311 iface->get_messenger = [](FlKeyboardViewDelegate* view_delegate) {
312 FlView* self = FL_VIEW(view_delegate);
313 return fl_engine_get_binary_messenger(self->engine);
314 };
315
316 iface->redispatch_event = [](FlKeyboardViewDelegate* view_delegate,
317 std::unique_ptr<FlKeyEvent> in_event) {
318 FlKeyEvent* event = in_event.release();
319 GdkEventType event_type = gdk_event_get_event_type(event->origin);
320 g_return_if_fail(event_type == GDK_KEY_PRESS ||
321 event_type == GDK_KEY_RELEASE);
322 gdk_event_put(event->origin);
324 };
325
326 iface->subscribe_to_layout_change = [](FlKeyboardViewDelegate* view_delegate,
327 KeyboardLayoutNotifier notifier) {
328 FlView* self = FL_VIEW(view_delegate);
329 self->keyboard_layout_notifier = std::move(notifier);
330 };
331
332 iface->lookup_key = [](FlKeyboardViewDelegate* view_delegate,
333 const GdkKeymapKey* key) -> guint {
334 FlView* self = FL_VIEW(view_delegate);
335 g_return_val_if_fail(self->keymap != nullptr, 0);
336 return gdk_keymap_lookup_key(self->keymap, key);
337 };
338
339 iface->get_keyboard_state =
340 [](FlKeyboardViewDelegate* view_delegate) -> GHashTable* {
341 FlView* self = FL_VIEW(view_delegate);
343 };
344}
345
347 FlScrollingViewDelegateInterface* iface) {
348 iface->send_mouse_pointer_event =
349 [](FlScrollingViewDelegate* view_delegate, FlutterPointerPhase phase,
350 size_t timestamp, double x, double y,
351 FlutterPointerDeviceKind device_kind, double scroll_delta_x,
352 double scroll_delta_y, int64_t buttons) {
353 FlView* self = FL_VIEW(view_delegate);
354 if (self->engine != nullptr) {
355 fl_engine_send_mouse_pointer_event(self->engine, phase, timestamp, x,
356 y, device_kind, scroll_delta_x,
357 scroll_delta_y, buttons);
358 }
359 };
360 iface->send_pointer_pan_zoom_event =
361 [](FlScrollingViewDelegate* view_delegate, size_t timestamp, double x,
362 double y, FlutterPointerPhase phase, double pan_x, double pan_y,
363 double scale, double rotation) {
364 FlView* self = FL_VIEW(view_delegate);
365 if (self->engine != nullptr) {
366 fl_engine_send_pointer_pan_zoom_event(self->engine, timestamp, x, y,
367 phase, pan_x, pan_y, scale,
368 rotation);
369 };
370 };
371}
372
374 FlTextInputViewDelegateInterface* iface) {
375 iface->translate_coordinates = [](FlTextInputViewDelegate* delegate,
376 gint view_x, gint view_y, gint* window_x,
377 gint* window_y) {
378 FlView* self = FL_VIEW(delegate);
379 gtk_widget_translate_coordinates(GTK_WIDGET(self),
380 gtk_widget_get_toplevel(GTK_WIDGET(self)),
381 view_x, view_y, window_x, window_y);
382 };
383}
384
385// Signal handler for GtkWidget::button-press-event
386static gboolean button_press_event_cb(FlView* self,
387 GdkEventButton* button_event) {
388 GdkEvent* event = reinterpret_cast<GdkEvent*>(button_event);
389
390 // Flutter doesn't handle double and triple click events.
391 GdkEventType event_type = gdk_event_get_event_type(event);
392 if (event_type == GDK_DOUBLE_BUTTON_PRESS ||
393 event_type == GDK_TRIPLE_BUTTON_PRESS) {
394 return FALSE;
395 }
396
398
400}
401
402// Signal handler for GtkWidget::button-release-event
403static gboolean button_release_event_cb(FlView* self,
404 GdkEventButton* button_event) {
405 GdkEvent* event = reinterpret_cast<GdkEvent*>(button_event);
407}
408
409// Signal handler for GtkWidget::scroll-event
410static gboolean scroll_event_cb(FlView* self, GdkEventScroll* event) {
411 // TODO(robert-ancell): Update to use GtkEventControllerScroll when we can
412 // depend on GTK 3.24.
413
415 self->scrolling_manager, event,
416 gtk_widget_get_scale_factor(GTK_WIDGET(self)));
417 return TRUE;
418}
419
420// Signal handler for GtkWidget::motion-notify-event
421static gboolean motion_notify_event_cb(FlView* self,
422 GdkEventMotion* motion_event) {
423 GdkEvent* event = reinterpret_cast<GdkEvent*>(motion_event);
424
425 if (self->engine == nullptr) {
426 return FALSE;
427 }
428
429 guint event_time = gdk_event_get_time(event);
430 GdkModifierType event_state = static_cast<GdkModifierType>(0);
431 gdk_event_get_state(event, &event_state);
432 gdouble event_x = 0.0, event_y = 0.0;
433 gdk_event_get_coords(event, &event_x, &event_y);
434
436
437 gint scale_factor = gtk_widget_get_scale_factor(GTK_WIDGET(self));
438
440 event_state, event_time);
442 self->engine, self->button_state != 0 ? kMove : kHover,
443 event_time * kMicrosecondsPerMillisecond, event_x * scale_factor,
444 event_y * scale_factor, get_device_kind((GdkEvent*)event), 0, 0,
445 self->button_state);
446
447 return TRUE;
448}
449
450// Signal handler for GtkWidget::enter-notify-event
451static gboolean enter_notify_event_cb(FlView* self,
452 GdkEventCrossing* crossing_event) {
453 GdkEvent* event = reinterpret_cast<GdkEvent*>(crossing_event);
454
455 if (self->engine == nullptr) {
456 return FALSE;
457 }
458
460
461 return TRUE;
462}
463
464// Signal handler for GtkWidget::leave-notify-event
465static gboolean leave_notify_event_cb(FlView* self,
466 GdkEventCrossing* crossing_event) {
467 GdkEvent* event = reinterpret_cast<GdkEvent*>(crossing_event);
468
469 guint event_time = gdk_event_get_time(event);
470 gdouble event_x = 0.0, event_y = 0.0;
471 gdk_event_get_coords(event, &event_x, &event_y);
472
473 if (crossing_event->mode != GDK_CROSSING_NORMAL) {
474 return FALSE;
475 }
476
477 if (self->engine == nullptr) {
478 return FALSE;
479 }
480
481 // Don't remove pointer while button is down; In case of dragging outside of
482 // window with mouse grab active Gtk will send another leave notify on
483 // release.
484 if (self->pointer_inside && self->button_state == 0) {
485 gint scale_factor = gtk_widget_get_scale_factor(GTK_WIDGET(self));
487 self->engine, kRemove, event_time * kMicrosecondsPerMillisecond,
488 event_x * scale_factor, event_y * scale_factor,
489 get_device_kind((GdkEvent*)event), 0, 0, self->button_state);
490 self->pointer_inside = FALSE;
491 }
492
493 return TRUE;
494}
495
496static void keymap_keys_changed_cb(FlView* self) {
497 if (self->keyboard_layout_notifier == nullptr) {
498 return;
499 }
500
501 self->keyboard_layout_notifier();
502}
503
504static void gesture_rotation_begin_cb(FlView* self) {
506}
507
509 gdouble rotation,
510 gdouble delta) {
512 rotation);
513}
514
515static void gesture_rotation_end_cb(FlView* self) {
517}
518
519static void gesture_zoom_begin_cb(FlView* self) {
521}
522
523static void gesture_zoom_update_cb(FlView* self, gdouble scale) {
525}
526
527static void gesture_zoom_end_cb(FlView* self) {
528 fl_scrolling_manager_handle_zoom_end(self->scrolling_manager);
529}
530
531static gboolean window_state_event_cb(FlView* self, GdkEvent* event) {
532 g_return_val_if_fail(FL_IS_ENGINE(self->engine), FALSE);
533
534 GdkWindowState state = event->window_state.new_window_state;
535 GdkWindowState previous_state = self->window_state;
536 self->window_state = state;
537 bool was_visible = !((previous_state & GDK_WINDOW_STATE_WITHDRAWN) ||
538 (previous_state & GDK_WINDOW_STATE_ICONIFIED));
539 bool is_visible = !((state & GDK_WINDOW_STATE_WITHDRAWN) ||
540 (state & GDK_WINDOW_STATE_ICONIFIED));
541 bool was_focused = (previous_state & GDK_WINDOW_STATE_FOCUSED);
542 bool is_focused = (state & GDK_WINDOW_STATE_FOCUSED);
543 if (was_visible != is_visible || was_focused != is_focused) {
544 if (self->engine != nullptr) {
545 fl_engine_send_window_state_event(FL_ENGINE(self->engine), is_visible,
546 is_focused);
547 }
548 }
549 return FALSE;
550}
551
552static GdkGLContext* create_context_cb(FlView* self) {
553 self->renderer =
554 fl_renderer_gdk_new(gtk_widget_get_parent_window(GTK_WIDGET(self)));
555 self->engine = fl_engine_new(self->project, FL_RENDERER(self->renderer));
557 self, nullptr);
559 self->engine, on_pre_engine_restart_cb, self, nullptr);
560
561 // Must initialize the keymap before the keyboard.
562 self->keymap = gdk_keymap_get_for_display(gdk_display_get_default());
563 self->keymap_keys_changed_cb_id = g_signal_connect_swapped(
564 self->keymap, "keys-changed", G_CALLBACK(keymap_keys_changed_cb), self);
565
566 // Create system channel handlers.
567 FlBinaryMessenger* messenger = fl_engine_get_binary_messenger(self->engine);
569 self->mouse_cursor_plugin = fl_mouse_cursor_plugin_new(messenger, self);
570 self->platform_plugin = fl_platform_plugin_new(messenger);
571
572 g_autoptr(GError) error = nullptr;
573 if (!fl_renderer_gdk_create_contexts(self->renderer, &error)) {
574 gtk_gl_area_set_error(self->gl_area, error);
575 return nullptr;
576 }
577
578 return GDK_GL_CONTEXT(
579 g_object_ref(fl_renderer_gdk_get_context(self->renderer)));
580}
581
582static void realize_cb(FlView* self) {
583 g_autoptr(GError) error = nullptr;
584
585 fl_renderer_make_current(FL_RENDERER(self->renderer));
586
587 GError* gl_error = gtk_gl_area_get_error(self->gl_area);
588 if (gl_error != NULL) {
589 g_warning("Failed to initialize GLArea: %s", gl_error->message);
590 return;
591 }
592
593 fl_renderer_setup(FL_RENDERER(self->renderer));
594
595 // Handle requests by the user to close the application.
596 GtkWidget* toplevel_window = gtk_widget_get_toplevel(GTK_WIDGET(self));
597
598 // Listen to window state changes.
599 self->window_state_cb_id =
600 g_signal_connect_swapped(toplevel_window, "window-state-event",
601 G_CALLBACK(window_state_event_cb), self);
602 self->window_state =
603 gdk_window_get_state(gtk_widget_get_window(toplevel_window));
604
605 g_signal_connect_swapped(toplevel_window, "delete-event",
606 G_CALLBACK(window_delete_event_cb), self);
607
609
610 fl_renderer_start(FL_RENDERER(FL_RENDERER(self->renderer)), self);
611
612 if (!fl_engine_start(self->engine, &error)) {
613 g_warning("Failed to start Flutter engine: %s", error->message);
614 return;
615 }
616
618
619 self->view_accessible = fl_view_accessible_new(self->engine);
621 FL_SOCKET_ACCESSIBLE(gtk_widget_get_accessible(GTK_WIDGET(self))),
622 atk_plug_get_id(ATK_PLUG(self->view_accessible)));
623}
624
625static gboolean render_cb(FlView* self, GdkGLContext* context) {
626 if (gtk_gl_area_get_error(self->gl_area) != NULL) {
627 return FALSE;
628 }
629
630 int width = gtk_widget_get_allocated_width(GTK_WIDGET(self->gl_area));
631 int height = gtk_widget_get_allocated_height(GTK_WIDGET(self->gl_area));
632 gint scale_factor = gtk_widget_get_scale_factor(GTK_WIDGET(self->gl_area));
633 fl_renderer_render(FL_RENDERER(self->renderer), width * scale_factor,
634 height * scale_factor);
635
636 return TRUE;
637}
638
639static void unrealize_cb(FlView* self) {
640 g_autoptr(GError) error = nullptr;
641
642 fl_renderer_make_current(FL_RENDERER(self->renderer));
643
644 GError* gl_error = gtk_gl_area_get_error(self->gl_area);
645 if (gl_error != NULL) {
646 g_warning("Failed to uninitialize GLArea: %s", gl_error->message);
647 return;
648 }
649
650 fl_renderer_cleanup(FL_RENDERER(self->renderer));
651}
652
653static void size_allocate_cb(FlView* self) {
655}
656
657static void fl_view_set_property(GObject* object,
658 guint prop_id,
659 const GValue* value,
660 GParamSpec* pspec) {
661 FlView* self = FL_VIEW(object);
662
663 switch (prop_id) {
665 g_set_object(&self->project,
666 static_cast<FlDartProject*>(g_value_get_object(value)));
667 break;
668 default:
669 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
670 break;
671 }
672}
673
674static void fl_view_get_property(GObject* object,
675 guint prop_id,
676 GValue* value,
677 GParamSpec* pspec) {
678 FlView* self = FL_VIEW(object);
679
680 switch (prop_id) {
682 g_value_set_object(value, self->project);
683 break;
684 default:
685 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
686 break;
687 }
688}
689
690static void fl_view_notify(GObject* object, GParamSpec* pspec) {
691 FlView* self = FL_VIEW(object);
692
693 if (strcmp(pspec->name, "scale-factor") == 0) {
695 }
696
697 if (G_OBJECT_CLASS(fl_view_parent_class)->notify != nullptr) {
698 G_OBJECT_CLASS(fl_view_parent_class)->notify(object, pspec);
699 }
700}
701
702static void fl_view_dispose(GObject* object) {
703 FlView* self = FL_VIEW(object);
704
705 if (self->engine != nullptr) {
706 fl_engine_set_update_semantics_handler(self->engine, nullptr, nullptr,
707 nullptr);
708 fl_engine_set_on_pre_engine_restart_handler(self->engine, nullptr, nullptr,
709 nullptr);
710 }
711
712 if (self->window_state_cb_id != 0) {
713 GtkWidget* toplevel_window = gtk_widget_get_toplevel(GTK_WIDGET(self));
714 g_signal_handler_disconnect(toplevel_window, self->window_state_cb_id);
715 self->window_state_cb_id = 0;
716 }
717
718 g_clear_object(&self->project);
719 g_clear_object(&self->renderer);
720 g_clear_object(&self->engine);
721 g_clear_object(&self->keyboard_manager);
722 if (self->keymap_keys_changed_cb_id != 0) {
723 g_signal_handler_disconnect(self->keymap, self->keymap_keys_changed_cb_id);
724 self->keymap_keys_changed_cb_id = 0;
725 }
726 g_clear_object(&self->mouse_cursor_plugin);
727 g_clear_object(&self->platform_plugin);
728 g_clear_object(&self->view_accessible);
729
730 G_OBJECT_CLASS(fl_view_parent_class)->dispose(object);
731}
732
733// Implements GtkWidget::key_press_event.
734static gboolean fl_view_key_press_event(GtkWidget* widget, GdkEventKey* event) {
735 FlView* self = FL_VIEW(widget);
736
738 self->keyboard_manager, fl_key_event_new_from_gdk_event(gdk_event_copy(
739 reinterpret_cast<GdkEvent*>(event))));
740}
741
742// Implements GtkWidget::key_release_event.
743static gboolean fl_view_key_release_event(GtkWidget* widget,
744 GdkEventKey* event) {
745 FlView* self = FL_VIEW(widget);
747 self->keyboard_manager, fl_key_event_new_from_gdk_event(gdk_event_copy(
748 reinterpret_cast<GdkEvent*>(event))));
749}
750
751static void fl_view_class_init(FlViewClass* klass) {
752 GObjectClass* object_class = G_OBJECT_CLASS(klass);
753 object_class->set_property = fl_view_set_property;
754 object_class->get_property = fl_view_get_property;
755 object_class->notify = fl_view_notify;
756 object_class->dispose = fl_view_dispose;
757
758 GtkWidgetClass* widget_class = GTK_WIDGET_CLASS(klass);
759 widget_class->key_press_event = fl_view_key_press_event;
760 widget_class->key_release_event = fl_view_key_release_event;
761
762 g_object_class_install_property(
763 G_OBJECT_CLASS(klass), kPropFlutterProject,
764 g_param_spec_object(
765 "flutter-project", "flutter-project", "Flutter project in use",
766 fl_dart_project_get_type(),
767 static_cast<GParamFlags>(G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
768 G_PARAM_STATIC_STRINGS)));
769
770 gtk_widget_class_set_accessible_type(GTK_WIDGET_CLASS(klass),
771 fl_socket_accessible_get_type());
772}
773
774static void fl_view_init(FlView* self) {
775 gtk_widget_set_can_focus(GTK_WIDGET(self), TRUE);
776
777 self->event_box = gtk_event_box_new();
778 gtk_widget_set_hexpand(self->event_box, TRUE);
779 gtk_widget_set_vexpand(self->event_box, TRUE);
780 gtk_container_add(GTK_CONTAINER(self), self->event_box);
781 gtk_widget_show(self->event_box);
782 gtk_widget_add_events(self->event_box,
783 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
784 GDK_BUTTON_RELEASE_MASK | GDK_SCROLL_MASK |
785 GDK_SMOOTH_SCROLL_MASK);
786
787 g_signal_connect_swapped(self->event_box, "button-press-event",
788 G_CALLBACK(button_press_event_cb), self);
789 g_signal_connect_swapped(self->event_box, "button-release-event",
790 G_CALLBACK(button_release_event_cb), self);
791 g_signal_connect_swapped(self->event_box, "scroll-event",
792 G_CALLBACK(scroll_event_cb), self);
793 g_signal_connect_swapped(self->event_box, "motion-notify-event",
794 G_CALLBACK(motion_notify_event_cb), self);
795 g_signal_connect_swapped(self->event_box, "enter-notify-event",
796 G_CALLBACK(enter_notify_event_cb), self);
797 g_signal_connect_swapped(self->event_box, "leave-notify-event",
798 G_CALLBACK(leave_notify_event_cb), self);
799 GtkGesture* zoom = gtk_gesture_zoom_new(self->event_box);
800 g_signal_connect_swapped(zoom, "begin", G_CALLBACK(gesture_zoom_begin_cb),
801 self);
802 g_signal_connect_swapped(zoom, "scale-changed",
803 G_CALLBACK(gesture_zoom_update_cb), self);
804 g_signal_connect_swapped(zoom, "end", G_CALLBACK(gesture_zoom_end_cb), self);
805 GtkGesture* rotate = gtk_gesture_rotate_new(self->event_box);
806 g_signal_connect_swapped(rotate, "begin",
807 G_CALLBACK(gesture_rotation_begin_cb), self);
808 g_signal_connect_swapped(rotate, "angle-changed",
809 G_CALLBACK(gesture_rotation_update_cb), self);
810 g_signal_connect_swapped(rotate, "end", G_CALLBACK(gesture_rotation_end_cb),
811 self);
812
813 self->gl_area = GTK_GL_AREA(gtk_gl_area_new());
814 gtk_widget_show(GTK_WIDGET(self->gl_area));
815 gtk_container_add(GTK_CONTAINER(self->event_box), GTK_WIDGET(self->gl_area));
816
817 g_signal_connect_swapped(self->gl_area, "create-context",
818 G_CALLBACK(create_context_cb), self);
819 g_signal_connect_swapped(self->gl_area, "realize", G_CALLBACK(realize_cb),
820 self);
821 g_signal_connect_swapped(self->gl_area, "render", G_CALLBACK(render_cb),
822 self);
823 g_signal_connect_swapped(self->gl_area, "unrealize", G_CALLBACK(unrealize_cb),
824 self);
825
826 g_signal_connect_swapped(self, "size-allocate", G_CALLBACK(size_allocate_cb),
827 self);
828}
829
830G_MODULE_EXPORT FlView* fl_view_new(FlDartProject* project) {
831 return static_cast<FlView*>(
832 g_object_new(fl_view_get_type(), "flutter-project", project, nullptr));
833}
834
835G_MODULE_EXPORT FlEngine* fl_view_get_engine(FlView* self) {
836 g_return_val_if_fail(FL_IS_VIEW(self), nullptr);
837 return self->engine;
838}
839
840void fl_view_redraw(FlView* self) {
841 g_return_if_fail(FL_IS_VIEW(self));
842 gtk_widget_queue_draw(GTK_WIDGET(self->gl_area));
843}
844
845GHashTable* fl_view_get_keyboard_state(FlView* self) {
846 g_return_val_if_fail(FL_IS_VIEW(self), nullptr);
847 return fl_keyboard_manager_get_pressed_state(self->keyboard_manager);
848}
static bool rotate(const SkDCubic &cubic, int zero, int index, SkDCubic &rotPath)
ax::mojom::Event event_type
FlutterPointerPhase
The phase of the pointer event.
Definition: embedder.h:965
@ kHover
The pointer moved while up.
Definition: embedder.h:997
@ kUp
Definition: embedder.h:973
@ kRemove
Definition: embedder.h:995
@ kDown
Definition: embedder.h:980
@ kAdd
Definition: embedder.h:990
@ kMove
Definition: embedder.h:985
@ kFlutterPointerButtonMousePrimary
Definition: embedder.h:1017
@ kFlutterPointerButtonMouseMiddle
Definition: embedder.h:1019
@ kFlutterPointerButtonMouseSecondary
Definition: embedder.h:1018
void(* FlutterKeyEventCallback)(bool, void *)
Definition: embedder.h:1155
FlutterPointerDeviceKind
The device type that created a pointer event.
Definition: embedder.h:1007
@ kFlutterPointerDeviceKindTouch
Definition: embedder.h:1009
@ kFlutterPointerDeviceKindStylus
Definition: embedder.h:1010
@ kFlutterPointerDeviceKindMouse
Definition: embedder.h:1008
GLFWwindow * window
Definition: main.cc:45
VkDevice device
Definition: main.cc:53
FlutterEngine engine
Definition: main.cc:68
SkBitmap source
Definition: examples.cpp:28
AtkStateType state
void fl_engine_send_key_event(FlEngine *self, const FlutterKeyEvent *event, FlutterKeyEventCallback callback, void *user_data)
Definition: fl_engine.cc:860
FlEngine * fl_engine_new(FlDartProject *project, FlRenderer *renderer)
Definition: fl_engine.cc:455
void fl_engine_send_pointer_pan_zoom_event(FlEngine *self, size_t timestamp, double x, double y, FlutterPointerPhase phase, double pan_x, double pan_y, double scale, double rotation)
Definition: fl_engine.cc:826
void fl_engine_set_on_pre_engine_restart_handler(FlEngine *self, FlEngineOnPreEngineRestartHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
Definition: fl_engine.cc:638
G_MODULE_EXPORT FlTextureRegistrar * fl_engine_get_texture_registrar(FlEngine *self)
Definition: fl_engine.cc:931
void fl_engine_set_update_semantics_handler(FlEngine *self, FlEngineUpdateSemanticsHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
Definition: fl_engine.cc:621
void fl_engine_send_window_state_event(FlEngine *self, gboolean visible, gboolean focused)
Definition: fl_engine.cc:756
void fl_engine_send_window_metrics_event(FlEngine *self, size_t width, size_t height, double pixel_ratio)
Definition: fl_engine.cc:768
G_MODULE_EXPORT FlBinaryMessenger * fl_engine_get_binary_messenger(FlEngine *self)
Definition: fl_engine.cc:915
gboolean fl_engine_start(FlEngine *self, GError **error)
Definition: fl_engine.cc:471
void fl_engine_send_mouse_pointer_event(FlEngine *self, FlutterPointerPhase phase, size_t timestamp, double x, double y, FlutterPointerDeviceKind device_kind, double scroll_delta_x, double scroll_delta_y, int64_t buttons)
Definition: fl_engine.cc:790
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
FlKeyEvent * event
void fl_key_event_dispose(FlKeyEvent *event)
Definition: fl_key_event.cc:32
FlKeyEvent * fl_key_event_new_from_gdk_event(GdkEvent *event)
Definition: fl_key_event.cc:7
typedefG_BEGIN_DECLS struct _FlKeyboardManager FlKeyboardManager
gboolean fl_keyboard_manager_handle_event(FlKeyboardManager *self, FlKeyEvent *event)
GHashTable * fl_keyboard_manager_get_pressed_state(FlKeyboardManager *self)
FlKeyboardManager * fl_keyboard_manager_new(FlBinaryMessenger *messenger, FlKeyboardViewDelegate *view_delegate)
void fl_keyboard_manager_sync_modifier_if_needed(FlKeyboardManager *self, guint state, double event_time)
std::function< void()> KeyboardLayoutNotifier
FlMouseCursorPlugin * fl_mouse_cursor_plugin_new(FlBinaryMessenger *messenger, FlView *view)
const uint8_t uint32_t uint32_t GError ** error
FlPlatformPlugin * fl_platform_plugin_new(FlBinaryMessenger *messenger)
void fl_platform_plugin_request_app_exit(FlPlatformPlugin *self)
FlPluginRegistrar * fl_plugin_registrar_new(FlView *view, FlBinaryMessenger *messenger, FlTextureRegistrar *texture_registrar)
gboolean fl_renderer_start(FlRenderer *self, FlView *view)
Definition: fl_renderer.cc:125
void fl_renderer_make_current(FlRenderer *self)
Definition: fl_renderer.cc:141
void fl_renderer_render(FlRenderer *self, int width, int height)
Definition: fl_renderer.cc:316
void fl_renderer_cleanup(FlRenderer *self)
Definition: fl_renderer.cc:388
void fl_renderer_setup(FlRenderer *self)
Definition: fl_renderer.cc:274
void fl_renderer_wait_for_frame(FlRenderer *self, int target_width, int target_height)
Definition: fl_renderer.cc:207
GdkGLContext * fl_renderer_gdk_get_context(FlRendererGdk *self)
gboolean fl_renderer_gdk_create_contexts(FlRendererGdk *self, GError **error)
FlRendererGdk * fl_renderer_gdk_new(GdkWindow *window)
void fl_scrolling_manager_handle_rotation_begin(FlScrollingManager *self)
void fl_scrolling_manager_set_last_mouse_position(FlScrollingManager *self, gdouble x, gdouble y)
void fl_scrolling_manager_handle_rotation_end(FlScrollingManager *self)
void fl_scrolling_manager_handle_scroll_event(FlScrollingManager *self, GdkEventScroll *scroll_event, gint scale_factor)
FlScrollingManager * fl_scrolling_manager_new(FlScrollingViewDelegate *view_delegate)
void fl_scrolling_manager_handle_zoom_update(FlScrollingManager *self, gdouble scale)
void fl_scrolling_manager_handle_rotation_update(FlScrollingManager *self, gdouble rotation)
void fl_scrolling_manager_handle_zoom_begin(FlScrollingManager *self)
void fl_scrolling_manager_handle_zoom_end(FlScrollingManager *self)
void fl_socket_accessible_embed(FlSocketAccessible *self, gchar *id)
uint8_t value
guint prop_id
guint const GValue GParamSpec * pspec
gboolean fl_text_input_plugin_filter_keypress(FlTextInputPlugin *self, FlKeyEvent *event)
FlTextInputPlugin * fl_text_input_plugin_new(FlBinaryMessenger *messenger, GtkIMContext *im_context, FlTextInputViewDelegate *view_delegate)
static void gesture_zoom_begin_cb(FlView *self)
Definition: fl_view.cc:519
static void init_scrolling(FlView *self)
Definition: fl_view.cc:129
static gboolean button_release_event_cb(FlView *self, GdkEventButton *button_event)
Definition: fl_view.cc:403
static void check_pointer_inside(FlView *self, GdkEvent *event)
Definition: fl_view.cc:217
static void handle_geometry_changed(FlView *self)
Definition: fl_view.cc:235
static void gesture_rotation_end_cb(FlView *self)
Definition: fl_view.cc:515
static void gesture_rotation_update_cb(FlView *self, gdouble rotation, gdouble delta)
Definition: fl_view.cc:508
static gboolean button_press_event_cb(FlView *self, GdkEventButton *button_event)
Definition: fl_view.cc:386
static void gesture_zoom_end_cb(FlView *self)
Definition: fl_view.cc:527
static void fl_view_dispose(GObject *object)
Definition: fl_view.cc:702
static gboolean fl_view_key_press_event(GtkWidget *widget, GdkEventKey *event)
Definition: fl_view.cc:734
static gboolean render_cb(FlView *self, GdkGLContext *context)
Definition: fl_view.cc:625
static void on_pre_engine_restart_cb(FlEngine *engine, gpointer user_data)
Definition: fl_view.cc:271
static GdkGLContext * create_context_cb(FlView *self)
Definition: fl_view.cc:552
static void size_allocate_cb(FlView *self)
Definition: fl_view.cc:653
static void fl_view_notify(GObject *object, GParamSpec *pspec)
Definition: fl_view.cc:690
static gboolean motion_notify_event_cb(FlView *self, GdkEventMotion *motion_event)
Definition: fl_view.cc:421
static void fl_view_init(FlView *self)
Definition: fl_view.cc:774
static gboolean enter_notify_event_cb(FlView *self, GdkEventCrossing *crossing_event)
Definition: fl_view.cc:451
static void fl_view_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
Definition: fl_view.cc:657
static void gesture_rotation_begin_cb(FlView *self)
Definition: fl_view.cc:504
GHashTable * fl_view_get_keyboard_state(FlView *self)
Definition: fl_view.cc:845
static void fl_view_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
Definition: fl_view.cc:674
G_MODULE_EXPORT FlView * fl_view_new(FlDartProject *project)
Definition: fl_view.cc:830
static void fl_view_class_init(FlViewClass *klass)
Definition: fl_view.cc:751
static void init_keyboard(FlView *self)
Definition: fl_view.cc:112
G_MODULE_EXPORT FlEngine * fl_view_get_engine(FlView *self)
Definition: fl_view.cc:835
static gboolean send_pointer_button_event(FlView *self, GdkEvent *event)
Definition: fl_view.cc:155
static void fl_view_text_input_delegate_iface_init(FlTextInputViewDelegateInterface *iface)
Definition: fl_view.cc:373
static constexpr int kMicrosecondsPerMillisecond
Definition: fl_view.cc:33
static void update_semantics_cb(FlEngine *engine, const FlutterSemanticsUpdate2 *update, gpointer user_data)
Definition: fl_view.cc:258
static gboolean window_state_event_cb(FlView *self, GdkEvent *event)
Definition: fl_view.cc:531
G_DEFINE_TYPE_WITH_CODE(FlView, fl_view, GTK_TYPE_BOX, G_IMPLEMENT_INTERFACE(fl_plugin_registry_get_type(), fl_view_plugin_registry_iface_init) G_IMPLEMENT_INTERFACE(fl_keyboard_view_delegate_get_type(), fl_view_keyboard_delegate_iface_init) G_IMPLEMENT_INTERFACE(fl_scrolling_view_delegate_get_type(), fl_view_scrolling_delegate_iface_init) G_IMPLEMENT_INTERFACE(fl_text_input_view_delegate_get_type(), fl_view_text_input_delegate_iface_init)) static gboolean window_delete_event_cb(FlView *self)
Definition: fl_view.cc:91
static gboolean fl_view_key_release_event(GtkWidget *widget, GdkEventKey *event)
Definition: fl_view.cc:743
static void fl_view_keyboard_delegate_iface_init(FlKeyboardViewDelegateInterface *iface)
Definition: fl_view.cc:294
@ kPropLast
Definition: fl_view.cc:77
@ kPropFlutterProject
Definition: fl_view.cc:77
static gboolean scroll_event_cb(FlView *self, GdkEventScroll *event)
Definition: fl_view.cc:410
static void fl_view_plugin_registry_iface_init(FlPluginRegistryInterface *iface)
Definition: fl_view.cc:289
static void fl_view_scrolling_delegate_iface_init(FlScrollingViewDelegateInterface *iface)
Definition: fl_view.cc:346
void fl_view_redraw(FlView *self)
Definition: fl_view.cc:840
static void gesture_zoom_update_cb(FlView *self, gdouble scale)
Definition: fl_view.cc:523
static void realize_cb(FlView *self)
Definition: fl_view.cc:582
static FlutterPointerDeviceKind get_device_kind(GdkEvent *event)
Definition: fl_view.cc:135
static void keymap_keys_changed_cb(FlView *self)
Definition: fl_view.cc:496
static gboolean leave_notify_event_cb(FlView *self, GdkEventCrossing *crossing_event)
Definition: fl_view.cc:465
static FlPluginRegistrar * fl_view_get_registrar_for_plugin(FlPluginRegistry *registry, const gchar *name)
Definition: fl_view.cc:279
static void unrealize_cb(FlView *self)
Definition: fl_view.cc:639
FlViewAccessible * fl_view_accessible_new(FlEngine *engine)
void fl_view_accessible_handle_update_semantics(FlViewAccessible *self, const FlutterSemanticsUpdate2 *update)
return FALSE
double y
double x
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
Definition: update.py:1
int32_t height
int32_t width
const Scalar scale
A batch of updates to semantics nodes and custom actions.
Definition: embedder.h:1504
GdkEvent * origin
Definition: fl_key_event.h:36
FlEngine * engine
Definition: fl_view.cc:45
gboolean pointer_inside
Definition: fl_view.cc:64
FlScrollingManager * scrolling_manager
Definition: fl_view.cc:55
gulong keymap_keys_changed_cb_id
Definition: fl_view.cc:69
FlPlatformPlugin * platform_plugin
Definition: fl_view.cc:58
GdkKeymap * keymap
Definition: fl_view.cc:68
GdkWindowState window_state
Definition: fl_view.cc:51
int64_t button_state
Definition: fl_view.cc:48
KeyboardLayoutNotifier keyboard_layout_notifier
Definition: fl_view.cc:67
GtkGLArea * gl_area
Definition: fl_view.cc:61
FlMouseCursorPlugin * mouse_cursor_plugin
Definition: fl_view.cc:57
GtkBox parent_instance
Definition: fl_view.cc:36
FlDartProject * project
Definition: fl_view.cc:39
gulong window_state_cb_id
Definition: fl_view.cc:71
FlRendererGdk * renderer
Definition: fl_view.cc:42
GtkWidget * event_box
Definition: fl_view.cc:60
FlKeyboardManager * keyboard_manager
Definition: fl_view.cc:54
FlTextInputPlugin * text_input_plugin
Definition: fl_view.cc:56
FlViewAccessible * view_accessible
Definition: fl_view.cc:74
void * user_data