[gnome-bluetooth/wip/hadess/battery-reporting: 7/9] lib: Add battery properties to BluetoothDevice
- From: Bastien Nocera <hadess src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-bluetooth/wip/hadess/battery-reporting: 7/9] lib: Add battery properties to BluetoothDevice
- Date: Mon, 7 Feb 2022 09:23:35 +0000 (UTC)
commit bab9c8f463814ef818dea4380b0df7e0762f3fbc
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 | 1 +
3 files changed, 42 insertions(+)
---
diff --git a/lib/bluetooth-device.c b/lib/bluetooth-device.c
index 3b02b0de..a3514675 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[] = {
@@ -126,6 +133,15 @@ bluetooth_device_get_property (GObject *object,
case PROP_CONNECTABLE:
g_value_set_boolean (value, device->connectable);
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;
@@ -181,6 +197,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;
@@ -245,6 +270,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
@@ -277,6 +312,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 63a04caa..04a1491e 100644
--- a/meson.build
+++ b/meson.build
@@ -107,6 +107,7 @@ libadwaita_dep = dependency(
)
libnotify_dep = dependency('libnotify', version: '>= 0.7.0')
libudev_dep = dependency('libudev')
+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]