[gnome-bluetooth/wip/hadess/battery-reporting: 9/11] lib: Add battery properties to BluetoothDevice




commit 665783dfc0110b6928ca9ed7ee56ef75328156d5
Author: Bastien Nocera <hadess hadess net>
Date:   Sun Jan 23 13:51:56 2022 +0100

    lib: Add battery properties to BluetoothDevice

 lib/bluetooth-device.c | 39 +++++++++++++++++++++++++++++++++++++++
 lib/meson.build        |  2 ++
 meson.build            |  2 ++
 3 files changed, 43 insertions(+)
---
diff --git a/lib/bluetooth-device.c b/lib/bluetooth-device.c
index 9dc8d5a5..a3247249 100644
--- a/lib/bluetooth-device.c
+++ b/lib/bluetooth-device.c
@@ -7,6 +7,7 @@
 #include "config.h"
 
 #include <glib-object.h>
+#include <libupower-glib/upower.h>
 
 #include "bluetooth-device.h"
 #include "bluetooth-utils.h"
@@ -25,6 +26,9 @@ enum {
        PROP_LEGACYPAIRING,
        PROP_UUIDS,
        PROP_CONNECTABLE,
+       PROP_BATTERY_TYPE,
+       PROP_BATTERY_PERCENTAGE,
+       PROP_BATTERY_LEVEL
 };
 
 struct _BluetoothDevice {
@@ -42,6 +46,9 @@ struct _BluetoothDevice {
        gboolean legacy_pairing;
        char **uuids;
        gboolean connectable;
+       BluetoothBatteryType battery_type;
+       double battery_percentage;
+       UpDeviceLevel battery_level;
 };
 
 static const char *connectable_uuids[] = {
@@ -123,6 +130,15 @@ bluetooth_device_get_property (GObject        *object,
        case PROP_UUIDS:
                g_value_set_boxed (value, device->uuids);
                break;
+       case PROP_BATTERY_TYPE:
+               g_value_set_enum (value, device->battery_type);
+               break;
+       case PROP_BATTERY_PERCENTAGE:
+               g_value_set_double (value, device->battery_percentage);
+               break;
+       case PROP_BATTERY_LEVEL:
+               g_value_set_uint (value, device->battery_level);
+               break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
                break;
@@ -178,6 +194,15 @@ bluetooth_device_set_property (GObject        *object,
                device->uuids = g_value_dup_boxed (value);
                update_connectable (device);
                break;
+       case PROP_BATTERY_TYPE:
+               device->battery_type = g_value_get_enum (value);
+               break;
+       case PROP_BATTERY_PERCENTAGE:
+               device->battery_percentage = g_value_get_double (value);
+               break;
+       case PROP_BATTERY_LEVEL:
+               device->battery_level = g_value_get_uint (value);
+               break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
                break;
@@ -242,6 +267,16 @@ static void bluetooth_device_class_init(BluetoothDeviceClass *klass)
        g_object_class_install_property (object_class, PROP_CONNECTABLE,
                                         g_param_spec_boolean ("connectable", NULL, "Connectable",
                                                               FALSE, G_PARAM_READABLE));
+       g_object_class_install_property (object_class, PROP_BATTERY_TYPE,
+                                        g_param_spec_enum ("battery-type", NULL, "Battery Type",
+                                                           BLUETOOTH_TYPE_BATTERY_TYPE, 
BLUETOOTH_BATTERY_TYPE_NONE, G_PARAM_READWRITE));
+       g_object_class_install_property (object_class, PROP_BATTERY_PERCENTAGE,
+                                        g_param_spec_double ("battery-percentage", NULL, "Battery 
Percentage",
+                                                           0.0, 100.f, 0.0, G_PARAM_READWRITE));
+       g_object_class_install_property (object_class, PROP_BATTERY_LEVEL,
+                                        g_param_spec_uint ("battery-level", NULL, "Battery Level",
+                                                           UP_DEVICE_LEVEL_UNKNOWN, UP_DEVICE_LEVEL_LAST - 
1, UP_DEVICE_LEVEL_UNKNOWN,
+                                                           G_PARAM_READWRITE));
 }
 
 static void
@@ -274,6 +309,10 @@ bluetooth_device_to_string (BluetoothDevice *device)
        g_string_append_printf (str, "\tD-Bus Path: %s\n", device->proxy ? g_dbus_proxy_get_object_path 
(device->proxy) : "(none)");
        g_string_append_printf (str, "\tType: %s Icon: %s\n", bluetooth_type_to_string (device->type), 
device->icon);
        g_string_append_printf (str, "\tPaired: %s Trusted: %s Connected: %s\n", BOOL_STR(device->paired), 
BOOL_STR(device->trusted), BOOL_STR(device->connected));
+       if (device->battery_type == BLUETOOTH_BATTERY_TYPE_PERCENTAGE)
+               g_string_append_printf (str, "\tBattery: %.02g%%\n", device->battery_percentage);
+       else if (device->battery_type == BLUETOOTH_BATTERY_TYPE_COARSE)
+               g_string_append_printf (str, "\tBattery: %s\n", up_device_level_to_string 
(device->battery_level));
        if (device->uuids != NULL) {
                guint i;
                g_string_append_printf (str, "\tUUIDs: ");
diff --git a/lib/meson.build b/lib/meson.build
index 8940d928..b5377f16 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -66,6 +66,7 @@ private_deps = [
   gio_unix_dep,
   libudev_dep,
   m_dep,
+  upower_dep,
 ]
 
 ui_deps = [
@@ -79,6 +80,7 @@ ui_private_deps = [
   gsound_dep,
   libnotify_dep,
   libudev_dep,
+  upower_dep,
   m_dep,
 ]
 
diff --git a/meson.build b/meson.build
index 6bead6d0..caa1e959 100644
--- a/meson.build
+++ b/meson.build
@@ -102,6 +102,8 @@ gsound_dep = dependency('gsound')
 libadwaita_dep = dependency('libadwaita-1')
 libnotify_dep = dependency('libnotify', version: '>= 0.7.0')
 libudev_dep = dependency('libudev')
+# FIXME require new enough version
+upower_dep = dependency('upower-glib')
 
 m_dep = cc.find_library('m')
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]