From 358044bdfd357916bad9ae9298fd89b54291a7a6 Mon Sep 17 00:00:00 2001 From: Ojas Kavathe Date: Wed, 27 Nov 2024 04:18:52 +0530 Subject: feat: add battery_percent to bluetooth devices --- lib/bluetooth/battery.vala | 33 +++++++++++++++++++++++++++++++++ lib/bluetooth/bluetooth.vala | 14 ++++++++++++++ lib/bluetooth/device.vala | 7 +++++++ lib/bluetooth/interfaces.vala | 5 +++++ lib/bluetooth/meson.build | 1 + 5 files changed, 60 insertions(+) create mode 100644 lib/bluetooth/battery.vala (limited to 'lib') diff --git a/lib/bluetooth/battery.vala b/lib/bluetooth/battery.vala new file mode 100644 index 0000000..a828994 --- /dev/null +++ b/lib/bluetooth/battery.vala @@ -0,0 +1,33 @@ +/** + * Object representing a [[https://github.com/bluez/bluez/blob/master/doc/org.bluez.Battery.rst|battery]]. + */ +public class AstalBluetooth.Battery : Object { + private IBattery proxy; + + internal ObjectPath object_path { owned get; private set; } + + internal Battery(IBattery proxy) { + this.proxy = proxy; + this.object_path = (ObjectPath)proxy.g_object_path; + proxy.g_properties_changed.connect((props) => { + var map = (HashTable)props; + foreach (var key in map.get_keys()) { + var prop = kebab_case(key); + if (get_class().find_property(prop) != null) { + notify_property(prop); + } + } + }); + } + + /** + * The percentage of battery left as an unsigned 8-bit integer. + */ + public uint percentage { get { return proxy.percentage; } } + + /** + * Describes where the battery information comes from. + */ + public string source { owned get { return proxy.source; } } + +} diff --git a/lib/bluetooth/bluetooth.vala b/lib/bluetooth/bluetooth.vala index 6eb6b76..68431e5 100644 --- a/lib/bluetooth/bluetooth.vala +++ b/lib/bluetooth/bluetooth.vala @@ -138,6 +138,9 @@ public class AstalBluetooth.Bluetooth : Object { [CCode (cname="astal_bluetooth_iadapter_proxy_get_type")] extern static GLib.Type get_iadapter_proxy_type(); + [CCode (cname="astal_bluetooth_ibattery_proxy_get_type")] + extern static GLib.Type get_ibattery_proxy_type(); + private Type manager_proxy_get_type(DBusObjectManagerClient _, string object_path, string? interface_name) { if (interface_name == null) return typeof(DBusObjectProxy); @@ -147,6 +150,8 @@ public class AstalBluetooth.Bluetooth : Object { return get_idevice_proxy_type(); case "org.bluez.Adapter1": return get_iadapter_proxy_type(); + case "org.bluez.Battery1": + return get_ibattery_proxy_type(); default: return typeof(DBusProxy); } @@ -161,6 +166,15 @@ public class AstalBluetooth.Bluetooth : Object { sync(); } + if (iface is IBattery) { + var battery = new Battery((IBattery)iface); + var device = _devices.lookup(iface.g_object_path); + if (device != null) { + device.battery = battery; + } + sync(); + } + if (iface is IAdapter) { var adapter = new Adapter((IAdapter)iface); _adapters.set(adapter.object_path, adapter); diff --git a/lib/bluetooth/device.vala b/lib/bluetooth/device.vala index 3f00cd9..e4df528 100644 --- a/lib/bluetooth/device.vala +++ b/lib/bluetooth/device.vala @@ -3,6 +3,7 @@ */ public class AstalBluetooth.Device : Object { private IDevice proxy; + public Battery battery; internal ObjectPath object_path { owned get; private set; } @@ -103,6 +104,12 @@ public class AstalBluetooth.Device : Object { set { proxy.trusted = value; } } + /** + * The percentage of battery left as an unsigned 8-bit integer. + */ + public uint battery_percentage { get { return battery.percentage; } } + + /** * The name alias for the remote device. * diff --git a/lib/bluetooth/interfaces.vala b/lib/bluetooth/interfaces.vala index dcb1c4b..043470e 100644 --- a/lib/bluetooth/interfaces.vala +++ b/lib/bluetooth/interfaces.vala @@ -44,3 +44,8 @@ private interface AstalBluetooth.IDevice : DBusProxy { public abstract uint32 class { get; } } +[DBus (name = "org.bluez.Battery1")] +private interface AstalBluetooth.IBattery : DBusProxy { + public abstract uint8 percentage { get; } + public abstract string source { owned get; } +} diff --git a/lib/bluetooth/meson.build b/lib/bluetooth/meson.build index 347b463..5288a9e 100644 --- a/lib/bluetooth/meson.build +++ b/lib/bluetooth/meson.build @@ -37,6 +37,7 @@ sources = [config] + files( 'adapter.vala', 'bluetooth.vala', 'device.vala', + 'battery.vala', 'interfaces.vala', 'utils.vala', ) -- cgit v1.2.3 From 95b2daac6cd4867ead5fb84defa6673410421039 Mon Sep 17 00:00:00 2001 From: Ojas Kavathe Date: Wed, 27 Nov 2024 15:24:57 +0530 Subject: bluetooth: percentage notify on change --- lib/bluetooth/bluetooth.vala | 2 +- lib/bluetooth/device.vala | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/bluetooth/bluetooth.vala b/lib/bluetooth/bluetooth.vala index 68431e5..02005c3 100644 --- a/lib/bluetooth/bluetooth.vala +++ b/lib/bluetooth/bluetooth.vala @@ -170,7 +170,7 @@ public class AstalBluetooth.Bluetooth : Object { var battery = new Battery((IBattery)iface); var device = _devices.lookup(iface.g_object_path); if (device != null) { - device.battery = battery; + device.set_battery(battery); } sync(); } diff --git a/lib/bluetooth/device.vala b/lib/bluetooth/device.vala index e4df528..74f8d83 100644 --- a/lib/bluetooth/device.vala +++ b/lib/bluetooth/device.vala @@ -3,7 +3,7 @@ */ public class AstalBluetooth.Device : Object { private IDevice proxy; - public Battery battery; + private Battery battery; internal ObjectPath object_path { owned get; private set; } @@ -21,6 +21,15 @@ public class AstalBluetooth.Device : Object { }); } + internal void set_battery(Battery battery) { + this.battery = battery; + + notify_property("battery_percentage"); + battery.notify["percentage"].connect((obj, pspec) => { + notify_property("battery_percentage"); + }); + } + /** * List of 128-bit UUIDs that represents the available remote services. */ @@ -105,10 +114,14 @@ public class AstalBluetooth.Device : Object { } /** - * The percentage of battery left as an unsigned 8-bit integer. + * The percentage of battery left on the device if it has one, else 0. */ - public uint battery_percentage { get { return battery.percentage; } } - + public uint battery_percentage { + get { + if (battery != null) return battery.percentage; + else return 0; + } + } /** * The name alias for the remote device. @@ -144,6 +157,7 @@ public class AstalBluetooth.Device : Object { yield proxy.disconnect(); } + /** * This method connects a specific profile of this device. * The UUID provided is the remote service UUID for the profile. -- cgit v1.2.3 From 5418f29bf1c8e3c2ce53add5d0a54b1cd003c13f Mon Sep 17 00:00:00 2001 From: Ojas Kavathe Date: Fri, 24 Jan 2025 09:01:07 +0530 Subject: fix(bluetooth): battery between 0-1 --- lib/bluetooth/battery.vala | 2 +- lib/bluetooth/device.vala | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/bluetooth/battery.vala b/lib/bluetooth/battery.vala index a828994..9fdd70d 100644 --- a/lib/bluetooth/battery.vala +++ b/lib/bluetooth/battery.vala @@ -1,7 +1,7 @@ /** * Object representing a [[https://github.com/bluez/bluez/blob/master/doc/org.bluez.Battery.rst|battery]]. */ -public class AstalBluetooth.Battery : Object { +internal class AstalBluetooth.Battery : Object { private IBattery proxy; internal ObjectPath object_path { owned get; private set; } diff --git a/lib/bluetooth/device.vala b/lib/bluetooth/device.vala index 74f8d83..b38a5ce 100644 --- a/lib/bluetooth/device.vala +++ b/lib/bluetooth/device.vala @@ -114,12 +114,12 @@ public class AstalBluetooth.Device : Object { } /** - * The percentage of battery left on the device if it has one, else 0. + * The percentage of battery left on the device if it has one, else -1. */ - public uint battery_percentage { + public double battery_percentage { get { - if (battery != null) return battery.percentage; - else return 0; + if (battery != null) return battery.percentage * 0.01; + else return -1; } } -- cgit v1.2.3