diff options
author | Aylur <[email protected]> | 2025-01-28 11:41:33 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2025-01-28 11:41:33 +0100 |
commit | a23ef6a6c0bd9da6c68f8bc5e6d8e1efd9210e09 (patch) | |
tree | 1acc45b813a844440d494e05de8dcf34085ab9d8 /lib | |
parent | d0de56e715cc1982d2eb0097b687c61133a0ff75 (diff) | |
parent | 5418f29bf1c8e3c2ce53add5d0a54b1cd003c13f (diff) |
Merge pull request #141 from ojaskavathe/main
feat: add battery_percentage to bluetooth devices
Diffstat (limited to 'lib')
-rw-r--r-- | lib/bluetooth/battery.vala | 33 | ||||
-rw-r--r-- | lib/bluetooth/bluetooth.vala | 14 | ||||
-rw-r--r-- | lib/bluetooth/device.vala | 21 | ||||
-rw-r--r-- | lib/bluetooth/interfaces.vala | 5 | ||||
-rw-r--r-- | lib/bluetooth/meson.build | 1 |
5 files changed, 74 insertions, 0 deletions
diff --git a/lib/bluetooth/battery.vala b/lib/bluetooth/battery.vala new file mode 100644 index 0000000..9fdd70d --- /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]]. + */ +internal 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<string, Variant>)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..02005c3 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.set_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..b38a5ce 100644 --- a/lib/bluetooth/device.vala +++ b/lib/bluetooth/device.vala @@ -3,6 +3,7 @@ */ public class AstalBluetooth.Device : Object { private IDevice proxy; + private Battery battery; internal ObjectPath object_path { owned get; private set; } @@ -20,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. */ @@ -104,6 +114,16 @@ public class AstalBluetooth.Device : Object { } /** + * The percentage of battery left on the device if it has one, else -1. + */ + public double battery_percentage { + get { + if (battery != null) return battery.percentage * 0.01; + else return -1; + } + } + + /** * The name alias for the remote device. * * In case no alias is set, it will return the remote device [[email protected]:name]. @@ -137,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. 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 01d2cb1..529e85f 100644 --- a/lib/bluetooth/meson.build +++ b/lib/bluetooth/meson.build @@ -38,6 +38,7 @@ sources = [config] + files( 'adapter.vala', 'bluetooth.vala', 'device.vala', + 'battery.vala', 'interfaces.vala', 'utils.vala', ) |