| Flutter Linux Embedder |

fl_display_monitor.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_display_monitor.h"
6 #include "flutter/shell/platform/linux/fl_engine_private.h"
7
8 struct _FlDisplayMonitor {
9 GObject parent_instance;
10
11 // Engine being updated.
12 GWeakRef engine;
13
14 // Display being monitored.
15 GdkDisplay* display;
16
17 // Mapping of GdkMonitor to display IDs.
18 GHashTable* display_ids_by_monitor;
19
20 // Next ID to assign to a new monitor.
21 FlutterEngineDisplayId next_display_id;
22 };
23
24 G_DEFINE_TYPE(FlDisplayMonitor, fl_display_monitor, G_TYPE_OBJECT)
25
26 // Send the current monitor state to the engine.
27 static void notify_display_update(FlDisplayMonitor* self) {
28 g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
29 if (engine == nullptr) {
30 return;
31 }
32
33 int n_monitors = gdk_display_get_n_monitors(self->display);
34 g_autofree FlutterEngineDisplay* displays =
35 g_new0(FlutterEngineDisplay, n_monitors);
36 for (int i = 0; i < n_monitors; i++) {
37 FlutterEngineDisplay* display = &displays[i];
38
39 GdkMonitor* monitor = gdk_display_get_monitor(self->display, i);
40 FlutterEngineDisplayId display_id = GPOINTER_TO_INT(
41 g_hash_table_lookup(self->display_ids_by_monitor, monitor));
42 if (display_id == 0) {
43 display_id = self->next_display_id;
44 g_hash_table_insert(self->display_ids_by_monitor, g_object_ref(monitor),
45 GINT_TO_POINTER(display_id));
46 self->next_display_id++;
47 }
48
49 GdkRectangle geometry;
50 gdk_monitor_get_geometry(monitor, &geometry);
51
52 display->struct_size = sizeof(FlutterEngineDisplay);
53 display->display_id = display_id;
54 display->single_display = false;
55 display->refresh_rate = gdk_monitor_get_refresh_rate(monitor) / 1000.0;
56 display->width = geometry.width;
57 display->height = geometry.height;
58 display->device_pixel_ratio = gdk_monitor_get_scale_factor(monitor);
59 }
60
61 fl_engine_notify_display_update(engine, displays, n_monitors);
62 }
63
64 static void monitor_added_cb(FlDisplayMonitor* self, GdkMonitor* monitor) {
65 notify_display_update(self);
66 }
67
68 static void monitor_removed_cb(FlDisplayMonitor* self, GdkMonitor* monitor) {
69 g_hash_table_remove(self->display_ids_by_monitor, monitor);
70 notify_display_update(self);
71 }
72
73 static void fl_display_monitor_dispose(GObject* object) {
74 FlDisplayMonitor* self = FL_DISPLAY_MONITOR(object);
75
76 g_weak_ref_clear(&self->engine);
77 g_clear_object(&self->display);
78 g_clear_pointer(&self->display_ids_by_monitor, g_hash_table_unref);
79
80 G_OBJECT_CLASS(fl_display_monitor_parent_class)->dispose(object);
81 }
82
83 static void fl_display_monitor_class_init(FlDisplayMonitorClass* klass) {
84 GObjectClass* object_class = G_OBJECT_CLASS(klass);
85 object_class->dispose = fl_display_monitor_dispose;
86 }
87
88 static void fl_display_monitor_init(FlDisplayMonitor* self) {
89 self->display_ids_by_monitor = g_hash_table_new_full(
90 g_direct_hash, g_direct_equal, g_object_unref, nullptr);
91 self->next_display_id = 1;
92 }
93
94 FlDisplayMonitor* fl_display_monitor_new(FlEngine* engine,
95 GdkDisplay* display) {
96 FlDisplayMonitor* self =
97 FL_DISPLAY_MONITOR(g_object_new(fl_display_monitor_get_type(), nullptr));
98 g_weak_ref_init(&self->engine, engine);
99 self->display = GDK_DISPLAY(g_object_ref(display));
100 return self;
101 }
102
103 void fl_display_monitor_start(FlDisplayMonitor* self) {
104 g_return_if_fail(FL_IS_DISPLAY_MONITOR(self));
105
106 g_signal_connect_object(self->display, "monitor-added",
107 G_CALLBACK(monitor_added_cb), self,
108 G_CONNECT_SWAPPED);
109 g_signal_connect_object(self->display, "monitor-removed",
110 G_CALLBACK(monitor_removed_cb), self,
111 G_CONNECT_SWAPPED);
112 notify_display_update(self);
113 }
114
115 FlutterEngineDisplayId fl_display_monitor_get_display_id(FlDisplayMonitor* self,
116 GdkMonitor* monitor) {
117 g_return_val_if_fail(FL_IS_DISPLAY_MONITOR(self), 0);
118 return GPOINTER_TO_INT(
119 g_hash_table_lookup(self->display_ids_by_monitor, monitor));
120 }
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
g_autoptr
g_autoptr(GMutexLocker) locker
fl_display_monitor_start
void fl_display_monitor_start(FlDisplayMonitor *self)
Definition: fl_display_monitor.cc:103
fl_display_monitor_new
FlDisplayMonitor * fl_display_monitor_new(FlEngine *engine, GdkDisplay *display)
Definition: fl_display_monitor.cc:94
monitor_added_cb
static void monitor_added_cb(FlDisplayMonitor *self, GdkMonitor *monitor)
Definition: fl_display_monitor.cc:64
fl_display_monitor_dispose
static void fl_display_monitor_dispose(GObject *object)
Definition: fl_display_monitor.cc:73
notify_display_update
static void notify_display_update(FlDisplayMonitor *self)
Definition: fl_display_monitor.cc:27
monitor_removed_cb
static void monitor_removed_cb(FlDisplayMonitor *self, GdkMonitor *monitor)
Definition: fl_display_monitor.cc:68
fl_display_monitor_init
static void fl_display_monitor_init(FlDisplayMonitor *self)
Definition: fl_display_monitor.cc:88
fl_display_monitor_get_display_id
FlutterEngineDisplayId fl_display_monitor_get_display_id(FlDisplayMonitor *self, GdkMonitor *monitor)
Definition: fl_display_monitor.cc:115
fl_display_monitor_class_init
static void fl_display_monitor_class_init(FlDisplayMonitorClass *klass)
Definition: fl_display_monitor.cc:83
fl_display_monitor.h
fl_engine_notify_display_update
void fl_engine_notify_display_update(FlEngine *self, const FlutterEngineDisplay *displays, size_t displays_length)
Definition: fl_engine.cc:864
fl_engine_private.h
i
int i
Definition: fl_socket_accessible.cc:18
_FlDisplayMonitor
Definition: fl_display_monitor.cc:8
_FlDisplayMonitor::next_display_id
FlutterEngineDisplayId next_display_id
Definition: fl_display_monitor.cc:21
_FlDisplayMonitor::engine
GWeakRef engine
Definition: fl_display_monitor.cc:12
_FlDisplayMonitor::display_ids_by_monitor
GHashTable * display_ids_by_monitor
Definition: fl_display_monitor.cc:18
_FlDisplayMonitor::display
GdkDisplay * display
Definition: fl_display_monitor.cc:15
_FlDisplayMonitor::parent_instance
GObject parent_instance
Definition: fl_display_monitor.cc:9