Robert Ancell | 7b458e5 | 2022-12-08 08:44:11 +1300 | [diff] [blame] | 1 | // Copyright 2014 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 "my_application.h" |
| 6 | |
| 7 | #include <flutter_linux/flutter_linux.h> |
| 8 | #include <math.h> |
| 9 | #include <upower.h> |
| 10 | #ifdef GDK_WINDOWING_X11 |
| 11 | #include <gdk/gdkx.h> |
| 12 | #endif |
| 13 | |
| 14 | #include "flutter/generated_plugin_registrant.h" |
| 15 | |
| 16 | struct _MyApplication { |
| 17 | GtkApplication parent_instance; |
| 18 | char** dart_entrypoint_arguments; |
| 19 | |
| 20 | FlView* view; |
| 21 | |
| 22 | // Connection to UPower. |
| 23 | UpClient* up_client; |
| 24 | GPtrArray* battery_devices; |
| 25 | |
| 26 | // Channel for Dart code to request the battery information. |
| 27 | FlMethodChannel* battery_channel; |
| 28 | |
| 29 | // Channel to send updates to Dart code about battery charging state. |
| 30 | FlEventChannel* charging_channel; |
| 31 | gchar* last_charge_event; |
| 32 | bool emit_charge_events; |
| 33 | }; |
| 34 | |
| 35 | G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION) |
| 36 | |
| 37 | // Checks the charging state and emits an event if necessary. |
| 38 | static void update_charging_state(MyApplication* self) { |
| 39 | if (!self->emit_charge_events) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | const gchar* charge_event = "discharging"; |
| 44 | for (guint i = 0; i < self->battery_devices->len; i++) { |
| 45 | UpDevice* device = |
| 46 | static_cast<UpDevice*>(g_ptr_array_index(self->battery_devices, i)); |
| 47 | |
| 48 | guint state; |
| 49 | g_object_get(device, "state", &state, nullptr); |
| 50 | if (state == UP_DEVICE_STATE_CHARGING || |
| 51 | state == UP_DEVICE_STATE_FULLY_CHARGED) { |
| 52 | charge_event = "charging"; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | if (g_strcmp0(charge_event, self->last_charge_event) != 0) { |
| 57 | g_autoptr(GError) error = nullptr; |
| 58 | g_autoptr(FlValue) value = fl_value_new_string(charge_event); |
| 59 | if (!fl_event_channel_send(self->charging_channel, value, nullptr, |
| 60 | &error)) { |
| 61 | g_warning("Failed to send charging event: %s", error->message); |
| 62 | return; |
| 63 | } |
| 64 | g_free(self->last_charge_event); |
| 65 | self->last_charge_event = g_strdup(charge_event); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Called when a UPower device changes state. |
| 70 | static void up_device_state_changed_cb(MyApplication* self, GParamSpec* pspec, |
| 71 | UpDevice* device) { |
| 72 | update_charging_state(self); |
| 73 | } |
| 74 | |
| 75 | // Called when UPower devices are added. |
| 76 | static void up_device_added_cb(MyApplication* self, UpDevice* device) { |
| 77 | // Listen for state changes from battery_devices. |
| 78 | guint kind; |
| 79 | g_object_get(device, "kind", &kind, nullptr); |
| 80 | if (kind == UP_DEVICE_KIND_BATTERY) { |
| 81 | g_ptr_array_add(self->battery_devices, g_object_ref(device)); |
| 82 | g_signal_connect_swapped(device, "notify::state", |
| 83 | G_CALLBACK(up_device_state_changed_cb), self); |
| 84 | up_device_state_changed_cb(self, nullptr, device); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Called when UPower devices are removed. |
| 89 | static void up_device_removed_cb(MyApplication* self, UpDevice* device) { |
| 90 | g_ptr_array_remove(self->battery_devices, device); |
| 91 | g_signal_handlers_disconnect_matched( |
| 92 | device, G_SIGNAL_MATCH_FUNC, 0, 0, nullptr, |
| 93 | reinterpret_cast<GClosure*>(up_device_state_changed_cb), nullptr); |
| 94 | } |
| 95 | |
| 96 | // Gets the current battery level. |
| 97 | static FlMethodResponse* get_battery_level(MyApplication* self) { |
| 98 | // Find the first available battery and use that. |
| 99 | for (guint i = 0; i < self->battery_devices->len; i++) { |
| 100 | UpDevice* device = |
| 101 | static_cast<UpDevice*>(g_ptr_array_index(self->battery_devices, i)); |
| 102 | |
| 103 | double percentage; |
| 104 | g_object_get(device, "percentage", &percentage, nullptr); |
| 105 | g_autoptr(FlValue) result = |
| 106 | fl_value_new_int(static_cast<int64_t>(round(percentage))); |
| 107 | return FL_METHOD_RESPONSE(fl_method_success_response_new(result)); |
| 108 | } |
| 109 | |
| 110 | return FL_METHOD_RESPONSE(fl_method_error_response_new( |
| 111 | "NO_BATTERY", "Device does not have a battery.", nullptr)); |
| 112 | } |
| 113 | |
| 114 | // Called when the Dart code requests battery information. |
| 115 | static void battery_method_call_cb(FlMethodChannel* channel, |
| 116 | FlMethodCall* method_call, |
| 117 | gpointer user_data) { |
| 118 | MyApplication* self = static_cast<MyApplication*>(user_data); |
| 119 | |
| 120 | g_autoptr(FlMethodResponse) response = nullptr; |
| 121 | if (strcmp(fl_method_call_get_name(method_call), "getBatteryLevel") == 0) { |
| 122 | response = get_battery_level(self); |
| 123 | } else { |
| 124 | response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new()); |
| 125 | } |
| 126 | |
| 127 | g_autoptr(GError) error = nullptr; |
| 128 | if (!fl_method_call_respond(method_call, response, &error)) { |
| 129 | g_warning("Failed to send response: %s", error->message); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Called when the Dart code starts listening for charging events. |
| 134 | static FlMethodErrorResponse* charging_listen_cb(FlEventChannel* channel, |
| 135 | FlValue* args, |
| 136 | gpointer user_data) { |
| 137 | MyApplication* self = static_cast<MyApplication*>(user_data); |
| 138 | |
| 139 | self->emit_charge_events = true; |
| 140 | update_charging_state(self); |
| 141 | |
| 142 | return nullptr; |
| 143 | } |
| 144 | |
| 145 | // Called when the Dart code stops listening for charging events. |
| 146 | static FlMethodErrorResponse* charging_cancel_cb(FlEventChannel* channel, |
| 147 | FlValue* args, |
| 148 | gpointer user_data) { |
| 149 | MyApplication* self = static_cast<MyApplication*>(user_data); |
| 150 | |
| 151 | self->emit_charge_events = false; |
| 152 | |
| 153 | return nullptr; |
| 154 | } |
| 155 | |
| 156 | // Creates the platform channels this application provides. |
| 157 | static void create_channels(MyApplication* self) { |
| 158 | FlEngine* engine = fl_view_get_engine(self->view); |
| 159 | FlBinaryMessenger* messenger = fl_engine_get_binary_messenger(engine); |
| 160 | g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new(); |
| 161 | |
| 162 | self->battery_channel = fl_method_channel_new( |
| 163 | messenger, "samples.flutter.io/battery", FL_METHOD_CODEC(codec)); |
| 164 | fl_method_channel_set_method_call_handler( |
| 165 | self->battery_channel, battery_method_call_cb, self, nullptr); |
| 166 | |
| 167 | self->charging_channel = fl_event_channel_new( |
| 168 | messenger, "samples.flutter.io/charging", FL_METHOD_CODEC(codec)); |
| 169 | fl_event_channel_set_stream_handlers(self->charging_channel, |
| 170 | charging_listen_cb, charging_cancel_cb, |
| 171 | self, nullptr); |
| 172 | } |
| 173 | |
| 174 | // Implements GApplication::activate. |
| 175 | static void my_application_activate(GApplication* application) { |
| 176 | MyApplication* self = MY_APPLICATION(application); |
| 177 | GtkWindow* window = |
| 178 | GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(application))); |
| 179 | |
| 180 | // Use a header bar when running in GNOME as this is the common style used |
| 181 | // by applications and is the setup most users will be using (e.g. Ubuntu |
| 182 | // desktop). |
| 183 | // If running on X and not using GNOME then just use a traditional title bar |
| 184 | // in case the window manager does more exotic layout, e.g. tiling. |
| 185 | // If running on Wayland assume the header bar will work (may need changing |
| 186 | // if future cases occur). |
| 187 | gboolean use_header_bar = TRUE; |
| 188 | #ifdef GDK_WINDOWING_X11 |
| 189 | GdkScreen* screen = gtk_window_get_screen(window); |
| 190 | if (GDK_IS_X11_SCREEN(screen)) { |
| 191 | const gchar* wm_name = gdk_x11_screen_get_window_manager_name(screen); |
| 192 | if (g_strcmp0(wm_name, "GNOME Shell") != 0) { |
| 193 | use_header_bar = FALSE; |
| 194 | } |
| 195 | } |
| 196 | #endif |
| 197 | if (use_header_bar) { |
| 198 | GtkHeaderBar* header_bar = GTK_HEADER_BAR(gtk_header_bar_new()); |
| 199 | gtk_widget_show(GTK_WIDGET(header_bar)); |
| 200 | gtk_header_bar_set_title(header_bar, "platform_channel"); |
| 201 | gtk_header_bar_set_show_close_button(header_bar, TRUE); |
| 202 | gtk_window_set_titlebar(window, GTK_WIDGET(header_bar)); |
| 203 | } else { |
| 204 | gtk_window_set_title(window, "platform_channel"); |
| 205 | } |
| 206 | |
| 207 | gtk_window_set_default_size(window, 1280, 720); |
| 208 | gtk_widget_show(GTK_WIDGET(window)); |
| 209 | |
| 210 | g_autoptr(FlDartProject) project = fl_dart_project_new(); |
| 211 | fl_dart_project_set_dart_entrypoint_arguments( |
| 212 | project, self->dart_entrypoint_arguments); |
| 213 | |
| 214 | // Connect to UPower. |
| 215 | self->up_client = up_client_new(); |
| 216 | g_signal_connect_swapped(self->up_client, "device-added", |
| 217 | G_CALLBACK(up_device_added_cb), self); |
| 218 | g_signal_connect_swapped(self->up_client, "device-removed", |
| 219 | G_CALLBACK(up_device_removed_cb), self); |
| 220 | g_autoptr(GPtrArray) devices = up_client_get_devices(self->up_client); |
| 221 | for (guint i = 0; i < devices->len; i++) { |
| 222 | g_autoptr(UpDevice) device = |
| 223 | static_cast<UpDevice*>(g_ptr_array_index(devices, i)); |
| 224 | up_device_added_cb(self, device); |
| 225 | } |
| 226 | |
| 227 | self->view = fl_view_new(project); |
| 228 | gtk_widget_show(GTK_WIDGET(self->view)); |
| 229 | gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(self->view)); |
| 230 | |
| 231 | fl_register_plugins(FL_PLUGIN_REGISTRY(self->view)); |
| 232 | |
| 233 | // Create application specific platform channels. |
| 234 | create_channels(self); |
| 235 | |
| 236 | gtk_widget_grab_focus(GTK_WIDGET(self->view)); |
| 237 | } |
| 238 | |
| 239 | // Implements GApplication::local_command_line. |
| 240 | static gboolean my_application_local_command_line(GApplication* application, |
| 241 | gchar*** arguments, |
| 242 | int* exit_status) { |
| 243 | MyApplication* self = MY_APPLICATION(application); |
| 244 | // Strip out the first argument as it is the binary name. |
| 245 | self->dart_entrypoint_arguments = g_strdupv(*arguments + 1); |
| 246 | |
| 247 | g_autoptr(GError) error = nullptr; |
| 248 | if (!g_application_register(application, nullptr, &error)) { |
| 249 | g_warning("Failed to register: %s", error->message); |
| 250 | *exit_status = 1; |
| 251 | return TRUE; |
| 252 | } |
| 253 | |
| 254 | g_application_activate(application); |
| 255 | *exit_status = 0; |
| 256 | |
| 257 | return TRUE; |
| 258 | } |
| 259 | |
| 260 | // Implements GObject::dispose. |
| 261 | static void my_application_dispose(GObject* object) { |
| 262 | MyApplication* self = MY_APPLICATION(object); |
| 263 | |
| 264 | for (guint i = 0; i < self->battery_devices->len; i++) { |
| 265 | UpDevice* device = |
| 266 | static_cast<UpDevice*>(g_ptr_array_index(self->battery_devices, i)); |
| 267 | g_signal_handlers_disconnect_matched(device, G_SIGNAL_MATCH_DATA, 0, 0, |
| 268 | nullptr, nullptr, self); |
| 269 | } |
| 270 | g_signal_handlers_disconnect_matched(self->up_client, G_SIGNAL_MATCH_DATA, 0, |
| 271 | 0, nullptr, nullptr, self); |
| 272 | |
| 273 | g_clear_pointer(&self->dart_entrypoint_arguments, g_strfreev); |
| 274 | g_clear_object(&self->up_client); |
| 275 | g_clear_object(&self->battery_devices); |
| 276 | g_clear_object(&self->battery_channel); |
| 277 | g_clear_object(&self->charging_channel); |
| 278 | g_clear_pointer(&self->last_charge_event, g_free); |
| 279 | G_OBJECT_CLASS(my_application_parent_class)->dispose(object); |
| 280 | } |
| 281 | |
| 282 | static void my_application_class_init(MyApplicationClass* klass) { |
| 283 | G_APPLICATION_CLASS(klass)->activate = my_application_activate; |
| 284 | G_APPLICATION_CLASS(klass)->local_command_line = |
| 285 | my_application_local_command_line; |
| 286 | G_OBJECT_CLASS(klass)->dispose = my_application_dispose; |
| 287 | } |
| 288 | |
| 289 | static void my_application_init(MyApplication* self) { |
| 290 | self->battery_devices = g_ptr_array_new_with_free_func(g_object_unref); |
| 291 | } |
| 292 | |
| 293 | MyApplication* my_application_new() { |
| 294 | return MY_APPLICATION(g_object_new(my_application_get_type(), |
| 295 | "application-id", APPLICATION_ID, "flags", |
| 296 | G_APPLICATION_NON_UNIQUE, nullptr)); |
| 297 | } |