diff options
author | Ojas Kavathe <[email protected]> | 2024-11-27 04:18:52 +0530 |
---|---|---|
committer | Ojas Kavathe <[email protected]> | 2024-11-27 04:22:41 +0530 |
commit | 358044bdfd357916bad9ae9298fd89b54291a7a6 (patch) | |
tree | 64fc44167dce2c711037de048d567ca5b237fd2a /lib | |
parent | 12fa0fbf1c05319a6b7a9a772a7b1f03e7ca4cae (diff) |
feat: add battery_percent 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 | 7 | ||||
-rw-r--r-- | lib/bluetooth/interfaces.vala | 5 | ||||
-rw-r--r-- | lib/bluetooth/meson.build | 1 |
5 files changed, 60 insertions, 0 deletions
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<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..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; } @@ -104,6 +105,12 @@ public class AstalBluetooth.Device : Object { } /** + * 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. * * In case no alias is set, it will return the remote device [[email protected]:name]. 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', ) |