| Flutter Linux Embedder |

fl_application.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_application.h"
6
7 #include <gtk/gtk.h>
8 #ifdef GDK_WINDOWING_X11
9 #include <gdk/gdkx.h>
10 #endif
11
12 #include "flutter/shell/platform/linux/fl_engine_private.h"
13 #include "flutter/shell/platform/linux/public/flutter_linux/fl_dart_project.h"
14 #include "flutter/shell/platform/linux/public/flutter_linux/fl_plugin_registry.h"
15 #include "flutter/shell/platform/linux/public/flutter_linux/fl_view.h"
16
17 struct FlApplicationPrivate {
18 // Arguments to pass to Dart.
19 gchar** dart_entrypoint_arguments;
20 };
21
22 #define FL_APPLICATION_GET_PRIVATE(app) \
23 ((FlApplicationPrivate*)fl_application_get_instance_private( \
24 FL_APPLICATION(app)))
25
26 enum { SIGNAL_REGISTER_PLUGINS, SIGNAL_CREATE_WINDOW, LAST_SIGNAL };
27
28 static guint fl_application_signals[LAST_SIGNAL];
29
30 G_DEFINE_TYPE_WITH_CODE(FlApplication,
31 fl_application,
32 GTK_TYPE_APPLICATION,
33 G_ADD_PRIVATE(FlApplication))
34
35 // Called when the platform creates a window.
36 static GtkWindow* create_window_cb(FlApplication* self, FlView* view) {
37 GtkWindow* window;
38 g_signal_emit(self, fl_application_signals[SIGNAL_CREATE_WINDOW], 0, view,
39 &window);
40
41 return window;
42 }
43
44 // Called when the first frame is received.
45 static void first_frame_cb(FlApplication* self, FlView* view) {
46 GtkWidget* window = gtk_widget_get_toplevel(GTK_WIDGET(view));
47
48 // Show the main window.
49 if (window != nullptr && GTK_IS_WINDOW(window)) {
50 gtk_window_present(GTK_WINDOW(window));
51 }
52 }
53
54 // Default implementation of FlApplication::register_plugins
55 static void fl_application_register_plugins(FlApplication* self,
56 FlPluginRegistry* registry) {}
57
58 // Default implementation of FlApplication::create_window
59 static GtkWindow* fl_application_create_window(FlApplication* self,
60 FlView* view) {
61 GtkApplicationWindow* window =
62 GTK_APPLICATION_WINDOW(gtk_application_window_new(GTK_APPLICATION(self)));
63
64 // Use a header bar when running in GNOME as this is the common style used
65 // by applications and is the setup most users will be using (e.g. Ubuntu
66 // desktop).
67 // If running on X and not using GNOME then just use a traditional title bar
68 // in case the window manager does more exotic layout, e.g. tiling.
69 // If running on Wayland assume the header bar will work (may need changing
70 // if future cases occur).
71 gboolean use_header_bar = TRUE;
72 #ifdef GDK_WINDOWING_X11
73 GdkScreen* screen = gtk_window_get_screen(GTK_WINDOW(window));
74 if (GDK_IS_X11_SCREEN(screen)) {
75 const gchar* wm_name = gdk_x11_screen_get_window_manager_name(screen);
76 if (g_strcmp0(wm_name, "GNOME Shell") != 0) {
77 use_header_bar = FALSE;
78 }
79 }
80 #endif
81 if (use_header_bar) {
82 GtkHeaderBar* header_bar = GTK_HEADER_BAR(gtk_header_bar_new());
83 gtk_widget_show(GTK_WIDGET(header_bar));
84 gtk_header_bar_set_show_close_button(header_bar, TRUE);
85 gtk_window_set_titlebar(GTK_WINDOW(window), GTK_WIDGET(header_bar));
86 }
87
88 gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view));
89
90 return GTK_WINDOW(window);
91 }
92
93 // Implements GApplication::activate.
94 static void fl_application_activate(GApplication* application) {
95 FlApplication* self = FL_APPLICATION(application);
96 FlApplicationPrivate* priv = FL_APPLICATION_GET_PRIVATE(self);
97
98 g_autoptr(FlDartProject) project = fl_dart_project_new();
99 fl_dart_project_set_dart_entrypoint_arguments(
100 project, priv->dart_entrypoint_arguments);
101
102 FlView* view = fl_view_new(project);
103 g_signal_connect_swapped(view, "first-frame", G_CALLBACK(first_frame_cb),
104 self);
105 gtk_widget_show(GTK_WIDGET(view));
106
107 FlWindowingHandler* windowing_handler =
108 fl_engine_get_windowing_handler(fl_view_get_engine(view));
109 g_signal_connect_swapped(windowing_handler, "create_window",
110 G_CALLBACK(create_window_cb), self);
111
112 GtkWindow* window;
113 g_signal_emit(self, fl_application_signals[SIGNAL_CREATE_WINDOW], 0, view,
114 &window);
115
116 // Make the resources for the view so rendering can start.
117 // We'll show the view when we have the first frame.
118 gtk_widget_realize(GTK_WIDGET(view));
119
120 g_signal_emit(self, fl_application_signals[SIGNAL_REGISTER_PLUGINS], 0,
121 FL_PLUGIN_REGISTRY(view));
122 }
123
124 // Implements GApplication::local_command_line.
125 static gboolean fl_application_local_command_line(GApplication* application,
126 gchar*** arguments,
127 int* exit_status) {
128 FlApplication* self = FL_APPLICATION(application);
129 FlApplicationPrivate* priv = FL_APPLICATION_GET_PRIVATE(self);
130
131 // Strip out the first argument as it is the binary name.
132 priv->dart_entrypoint_arguments = g_strdupv(*arguments + 1);
133
134 g_autoptr(GError) error = nullptr;
135 if (!g_application_register(application, nullptr, &error)) {
136 g_warning("Failed to register: %s", error->message);
137 *exit_status = 1;
138 return TRUE;
139 }
140
141 // This will only run on the primary instance or this instance with
142 // G_APPLICATION_NON_UNIQUE
143 g_application_activate(application);
144 *exit_status = 0;
145
146 return TRUE;
147 }
148
149 // Implements GObject::dispose.
150 static void fl_application_dispose(GObject* object) {
151 FlApplication* self = FL_APPLICATION(object);
152 FlApplicationPrivate* priv = FL_APPLICATION_GET_PRIVATE(self);
153
154 g_clear_pointer(&priv->dart_entrypoint_arguments, g_strfreev);
155
156 G_OBJECT_CLASS(fl_application_parent_class)->dispose(object);
157 }
158
159 static void fl_application_class_init(FlApplicationClass* klass) {
160 G_APPLICATION_CLASS(klass)->activate = fl_application_activate;
161 G_APPLICATION_CLASS(klass)->local_command_line =
162 fl_application_local_command_line;
163 G_OBJECT_CLASS(klass)->dispose = fl_application_dispose;
164
165 klass->register_plugins = fl_application_register_plugins;
166 klass->create_window = fl_application_create_window;
167
168 fl_application_signals[SIGNAL_REGISTER_PLUGINS] = g_signal_new(
169 "register-plugins", fl_application_get_type(), G_SIGNAL_RUN_LAST,
170 G_STRUCT_OFFSET(FlApplicationClass, register_plugins), nullptr, nullptr,
171 nullptr, G_TYPE_NONE, 1, fl_plugin_registry_get_type());
172 fl_application_signals[SIGNAL_CREATE_WINDOW] = g_signal_new(
173 "create-window", fl_application_get_type(), G_SIGNAL_RUN_LAST,
174 G_STRUCT_OFFSET(FlApplicationClass, create_window),
175 g_signal_accumulator_first_wins, nullptr, nullptr, GTK_TYPE_WINDOW, 1,
176 fl_view_get_type());
177 }
178
179 static void fl_application_init(FlApplication* self) {}
180
181 G_MODULE_EXPORT
182 FlApplication* fl_application_new(const gchar* application_id,
183 GApplicationFlags flags) {
184 return FL_APPLICATION(g_object_new(fl_application_get_type(),
185 "application-id", application_id, "flags",
186 flags, nullptr));
187 }
flags
FlutterSemanticsFlag flags
Definition: fl_accessible_node.cc:109
view
FlView * view
Definition: fl_application.cc:36
fl_application_dispose
static void fl_application_dispose(GObject *object)
Definition: fl_application.cc:150
first_frame_cb
static void first_frame_cb(FlApplication *self, FlView *view)
Definition: fl_application.cc:45
fl_application_init
static void fl_application_init(FlApplication *self)
Definition: fl_application.cc:179
fl_application_signals
static guint fl_application_signals[LAST_SIGNAL]
Definition: fl_application.cc:28
fl_application_local_command_line
static gboolean fl_application_local_command_line(GApplication *application, gchar ***arguments, int *exit_status)
Definition: fl_application.cc:125
LAST_SIGNAL
@ LAST_SIGNAL
Definition: fl_application.cc:26
SIGNAL_CREATE_WINDOW
@ SIGNAL_CREATE_WINDOW
Definition: fl_application.cc:26
SIGNAL_REGISTER_PLUGINS
@ SIGNAL_REGISTER_PLUGINS
Definition: fl_application.cc:26
fl_application_create_window
static GtkWindow * fl_application_create_window(FlApplication *self, FlView *view)
Definition: fl_application.cc:59
fl_application_new
G_MODULE_EXPORT FlApplication * fl_application_new(const gchar *application_id, GApplicationFlags flags)
Definition: fl_application.cc:182
FL_APPLICATION_GET_PRIVATE
#define FL_APPLICATION_GET_PRIVATE(app)
Definition: fl_application.cc:22
fl_application_register_plugins
static void fl_application_register_plugins(FlApplication *self, FlPluginRegistry *registry)
Definition: fl_application.cc:55
g_signal_emit
g_signal_emit(self, fl_application_signals[SIGNAL_CREATE_WINDOW], 0, view, &window)
G_DEFINE_TYPE_WITH_CODE
G_DEFINE_TYPE_WITH_CODE(FlApplication, fl_application, GTK_TYPE_APPLICATION, G_ADD_PRIVATE(FlApplication)) static GtkWindow *create_window_cb(FlApplication *self
window
return window
Definition: fl_application.cc:41
fl_application_class_init
static void fl_application_class_init(FlApplicationClass *klass)
Definition: fl_application.cc:159
fl_application_activate
static void fl_application_activate(GApplication *application)
Definition: fl_application.cc:94
fl_application.h
fl_dart_project_new
G_MODULE_EXPORT FlDartProject * fl_dart_project_new()
Definition: fl_dart_project.cc:50
fl_dart_project_set_dart_entrypoint_arguments
G_MODULE_EXPORT void fl_dart_project_set_dart_entrypoint_arguments(FlDartProject *self, char **argv)
Definition: fl_dart_project.cc:110
fl_dart_project.h
fl_engine_get_windowing_handler
FlWindowingHandler * fl_engine_get_windowing_handler(FlEngine *self)
Definition: fl_engine.cc:1319
fl_engine_private.h
priv
FlPixelBufferTexturePrivate * priv
Definition: fl_pixel_buffer_texture.cc:30
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
fl_plugin_registry.h
fl_view_new
G_MODULE_EXPORT FlView * fl_view_new(FlDartProject *project)
Definition: fl_view.cc:751
fl_view_get_engine
G_MODULE_EXPORT FlEngine * fl_view_get_engine(FlView *self)
Definition: fl_view.cc:787
fl_view.h
FlApplicationPrivate
Definition: fl_application.cc:17
FlApplicationPrivate::dart_entrypoint_arguments
gchar ** dart_entrypoint_arguments
Definition: fl_application.cc:19