|
| 1 | +#include "include/battery_plus_linux/battery_plus_linux_plugin.h" |
| 2 | +#include "upower_device.h" |
| 3 | + |
| 4 | +#include <flutter_linux/flutter_linux.h> |
| 5 | +#include <gtk/gtk.h> |
| 6 | +#include <cmath> |
| 7 | + |
| 8 | +#define BATTERY_PLUS_LINUX_PLUGIN(obj) \ |
| 9 | + (G_TYPE_CHECK_INSTANCE_CAST((obj), battery_plus_linux_plugin_get_type(), \ |
| 10 | + BatteryPlusLinuxPlugin)) |
| 11 | + |
| 12 | +static const char* kMethodChannel = "plugins.flutter.io/battery"; |
| 13 | +static const char* kEventChannel = "plugins.flutter.io/charging"; |
| 14 | +static const char* kBatteryLevelMethod = "getBatteryLevel"; |
| 15 | +static const char* kDbusError = "D-BUS Error"; |
| 16 | +static const char* kDbusInterface = "org.freedesktop.UPower"; |
| 17 | +static const char* kDbusObject = "/org/freedesktop/UPower/devices/DisplayDevice"; |
| 18 | + |
| 19 | +struct _BatteryPlusLinuxPlugin { |
| 20 | + GObject parent_instance; |
| 21 | + UPowerDevice* state_device = nullptr; |
| 22 | +}; |
| 23 | + |
| 24 | +G_DEFINE_TYPE(BatteryPlusLinuxPlugin, battery_plus_linux_plugin, g_object_get_type()) |
| 25 | + |
| 26 | +enum UPowerState { |
| 27 | + Unknown = 0, |
| 28 | + Charging = 1, |
| 29 | + Discharging = 2, |
| 30 | + Empty = 3, |
| 31 | + FullyCharged = 4, |
| 32 | + PendingCharge = 5, |
| 33 | + PendingDischarge = 6 |
| 34 | +}; |
| 35 | + |
| 36 | +// ### TODO: introduce a new 'unknown' battery state for workstations? |
| 37 | +static const gchar *upower_state_str(guint state) { |
| 38 | + switch (state) { |
| 39 | + case Charging: |
| 40 | + return "charging"; |
| 41 | + case Discharging: |
| 42 | + return "discharging"; |
| 43 | + default: |
| 44 | + return "full"; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +static UPowerDevice* upower_device_new(FlMethodResponse **response) { |
| 49 | + g_autoptr(GError) error = nullptr; |
| 50 | + UPowerDevice* device = upower_device_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, |
| 51 | + G_DBUS_PROXY_FLAGS_NONE, |
| 52 | + kDbusInterface, |
| 53 | + kDbusObject, |
| 54 | + nullptr, |
| 55 | + &error); |
| 56 | + if (error) { |
| 57 | + *response = FL_METHOD_RESPONSE(fl_method_error_response_new(kDbusError, |
| 58 | + error->message, |
| 59 | + nullptr)); |
| 60 | + } |
| 61 | + return device; |
| 62 | +} |
| 63 | + |
| 64 | +static void battery_plus_linux_plugin_handle_method_call(BatteryPlusLinuxPlugin* self, |
| 65 | + FlMethodCall* method_call) { |
| 66 | + g_autoptr(FlMethodResponse) response = nullptr; |
| 67 | + const gchar* method_name = fl_method_call_get_name(method_call); |
| 68 | + if (strcmp(method_name, kBatteryLevelMethod) == 0) { |
| 69 | + UPowerDevice* device = upower_device_new(&response); |
| 70 | + if (!response) { |
| 71 | + gdouble level = upower_device_get_percentage(device); |
| 72 | + g_autoptr(FlValue) result = fl_value_new_int(std::round(level)); |
| 73 | + g_object_unref(device); |
| 74 | + response = FL_METHOD_RESPONSE(fl_method_success_response_new(result)); |
| 75 | + } |
| 76 | + } else { |
| 77 | + response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new()); |
| 78 | + } |
| 79 | + fl_method_call_respond(method_call, response, nullptr); |
| 80 | +} |
| 81 | + |
| 82 | +static void send_battery_state_event(FlEventChannel *event_channel, guint state) { |
| 83 | + g_autoptr(FlValue) message = fl_value_new_string(upower_state_str(state)); |
| 84 | + g_autoptr(GError) error = nullptr; |
| 85 | + if (!fl_event_channel_send(event_channel, message, NULL, &error)) { |
| 86 | + g_warning("Failed to send event: %s", error->message); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +static void properties_changed_cb(GDBusProxy* proxy, |
| 91 | + GVariant* changed_properties, |
| 92 | + const gchar* invalidated_properties, |
| 93 | + gpointer user_data) |
| 94 | +{ |
| 95 | + GVariant* value = g_variant_lookup_value(changed_properties, |
| 96 | + "State", |
| 97 | + G_VARIANT_TYPE_UINT32); |
| 98 | + if (value) { |
| 99 | + guint32 state = g_variant_get_uint32(value); |
| 100 | + send_battery_state_event(FL_EVENT_CHANNEL(user_data), state); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +static FlMethodErrorResponse* battery_plus_linux_plugin_listen_events(BatteryPlusLinuxPlugin* self, |
| 105 | + FlEventChannel* event_channel, |
| 106 | + FlValue* args) { |
| 107 | + FlMethodResponse *response = nullptr; |
| 108 | + self->state_device = upower_device_new(&response); |
| 109 | + if (self->state_device) { |
| 110 | + g_signal_connect(self->state_device, |
| 111 | + "g-properties-changed", |
| 112 | + G_CALLBACK(properties_changed_cb), |
| 113 | + event_channel); |
| 114 | + } |
| 115 | + send_battery_state_event(event_channel, upower_device_get_state(self->state_device)); |
| 116 | + return FL_METHOD_ERROR_RESPONSE(response); |
| 117 | +} |
| 118 | + |
| 119 | +static FlMethodErrorResponse* battery_plus_linux_plugin_cancel_events(BatteryPlusLinuxPlugin* self, |
| 120 | + FlEventChannel* event_channel, |
| 121 | + FlValue* args) { |
| 122 | + g_clear_object(&self->state_device); |
| 123 | + return nullptr; |
| 124 | +} |
| 125 | + |
| 126 | +static void battery_plus_linux_plugin_dispose(GObject* object) { |
| 127 | + G_OBJECT_CLASS(battery_plus_linux_plugin_parent_class)->dispose(object); |
| 128 | +} |
| 129 | + |
| 130 | +static void battery_plus_linux_plugin_class_init(BatteryPlusLinuxPluginClass* klass) { |
| 131 | + G_OBJECT_CLASS(klass)->dispose = battery_plus_linux_plugin_dispose; |
| 132 | +} |
| 133 | + |
| 134 | +static void battery_plus_linux_plugin_init(BatteryPlusLinuxPlugin* self) { } |
| 135 | + |
| 136 | +static void method_call_cb(FlMethodChannel* method_channel, |
| 137 | + FlMethodCall* method_call, |
| 138 | + gpointer user_data) { |
| 139 | + BatteryPlusLinuxPlugin* plugin = BATTERY_PLUS_LINUX_PLUGIN(user_data); |
| 140 | + battery_plus_linux_plugin_handle_method_call(plugin, method_call); |
| 141 | +} |
| 142 | + |
| 143 | +static FlMethodErrorResponse* listen_cb(FlEventChannel* event_channel, |
| 144 | + FlValue* args, |
| 145 | + gpointer user_data) { |
| 146 | + BatteryPlusLinuxPlugin* plugin = BATTERY_PLUS_LINUX_PLUGIN(user_data); |
| 147 | + return battery_plus_linux_plugin_listen_events(plugin, event_channel, args); |
| 148 | +} |
| 149 | + |
| 150 | +static FlMethodErrorResponse* cancel_cb(FlEventChannel* event_channel, |
| 151 | + FlValue* args, |
| 152 | + gpointer user_data) { |
| 153 | + BatteryPlusLinuxPlugin* plugin = BATTERY_PLUS_LINUX_PLUGIN(user_data); |
| 154 | + return battery_plus_linux_plugin_cancel_events(plugin, event_channel, args); |
| 155 | +} |
| 156 | + |
| 157 | +void battery_plus_linux_plugin_register_with_registrar(FlPluginRegistrar* registrar) { |
| 158 | + BatteryPlusLinuxPlugin* plugin = |
| 159 | + BATTERY_PLUS_LINUX_PLUGIN(g_object_new(battery_plus_linux_plugin_get_type(), nullptr)); |
| 160 | + |
| 161 | + FlBinaryMessenger* messenger = fl_plugin_registrar_get_messenger(registrar); |
| 162 | + g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new(); |
| 163 | + |
| 164 | + g_autoptr(FlMethodChannel) method_channel = fl_method_channel_new(messenger, |
| 165 | + kMethodChannel, |
| 166 | + FL_METHOD_CODEC(codec)); |
| 167 | + fl_method_channel_set_method_call_handler(method_channel, |
| 168 | + method_call_cb, |
| 169 | + g_object_ref(plugin), |
| 170 | + g_object_unref); |
| 171 | + |
| 172 | + g_autoptr(FlEventChannel) event_channel = fl_event_channel_new(messenger, |
| 173 | + kEventChannel, |
| 174 | + FL_METHOD_CODEC(codec)); |
| 175 | + fl_event_channel_set_stream_handlers(event_channel, |
| 176 | + listen_cb, |
| 177 | + cancel_cb, |
| 178 | + g_object_ref(plugin), |
| 179 | + g_object_unref); |
| 180 | + |
| 181 | + g_object_unref(plugin); |
| 182 | +} |
0 commit comments