Skip to content

Commit a23ef6a

Browse files
authored
Merge pull request #141 from ojaskavathe/main
feat: add battery_percentage to bluetooth devices
2 parents d0de56e + 5418f29 commit a23ef6a

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

lib/bluetooth/battery.vala

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Object representing a [[https://github.com/bluez/bluez/blob/master/doc/org.bluez.Battery.rst|battery]].
3+
*/
4+
internal class AstalBluetooth.Battery : Object {
5+
private IBattery proxy;
6+
7+
internal ObjectPath object_path { owned get; private set; }
8+
9+
internal Battery(IBattery proxy) {
10+
this.proxy = proxy;
11+
this.object_path = (ObjectPath)proxy.g_object_path;
12+
proxy.g_properties_changed.connect((props) => {
13+
var map = (HashTable<string, Variant>)props;
14+
foreach (var key in map.get_keys()) {
15+
var prop = kebab_case(key);
16+
if (get_class().find_property(prop) != null) {
17+
notify_property(prop);
18+
}
19+
}
20+
});
21+
}
22+
23+
/**
24+
* The percentage of battery left as an unsigned 8-bit integer.
25+
*/
26+
public uint percentage { get { return proxy.percentage; } }
27+
28+
/**
29+
* Describes where the battery information comes from.
30+
*/
31+
public string source { owned get { return proxy.source; } }
32+
33+
}

lib/bluetooth/bluetooth.vala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ public class AstalBluetooth.Bluetooth : Object {
138138
[CCode (cname="astal_bluetooth_iadapter_proxy_get_type")]
139139
extern static GLib.Type get_iadapter_proxy_type();
140140

141+
[CCode (cname="astal_bluetooth_ibattery_proxy_get_type")]
142+
extern static GLib.Type get_ibattery_proxy_type();
143+
141144
private Type manager_proxy_get_type(DBusObjectManagerClient _, string object_path, string? interface_name) {
142145
if (interface_name == null)
143146
return typeof(DBusObjectProxy);
@@ -147,6 +150,8 @@ public class AstalBluetooth.Bluetooth : Object {
147150
return get_idevice_proxy_type();
148151
case "org.bluez.Adapter1":
149152
return get_iadapter_proxy_type();
153+
case "org.bluez.Battery1":
154+
return get_ibattery_proxy_type();
150155
default:
151156
return typeof(DBusProxy);
152157
}
@@ -161,6 +166,15 @@ public class AstalBluetooth.Bluetooth : Object {
161166
sync();
162167
}
163168

169+
if (iface is IBattery) {
170+
var battery = new Battery((IBattery)iface);
171+
var device = _devices.lookup(iface.g_object_path);
172+
if (device != null) {
173+
device.set_battery(battery);
174+
}
175+
sync();
176+
}
177+
164178
if (iface is IAdapter) {
165179
var adapter = new Adapter((IAdapter)iface);
166180
_adapters.set(adapter.object_path, adapter);

lib/bluetooth/device.vala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
public class AstalBluetooth.Device : Object {
55
private IDevice proxy;
6+
private Battery battery;
67

78
internal ObjectPath object_path { owned get; private set; }
89

@@ -20,6 +21,15 @@ public class AstalBluetooth.Device : Object {
2021
});
2122
}
2223

24+
internal void set_battery(Battery battery) {
25+
this.battery = battery;
26+
27+
notify_property("battery_percentage");
28+
battery.notify["percentage"].connect((obj, pspec) => {
29+
notify_property("battery_percentage");
30+
});
31+
}
32+
2333
/**
2434
* List of 128-bit UUIDs that represents the available remote services.
2535
*/
@@ -103,6 +113,16 @@ public class AstalBluetooth.Device : Object {
103113
set { proxy.trusted = value; }
104114
}
105115

116+
/**
117+
* The percentage of battery left on the device if it has one, else -1.
118+
*/
119+
public double battery_percentage {
120+
get {
121+
if (battery != null) return battery.percentage * 0.01;
122+
else return -1;
123+
}
124+
}
125+
106126
/**
107127
* The name alias for the remote device.
108128
*
@@ -137,6 +157,7 @@ public class AstalBluetooth.Device : Object {
137157
yield proxy.disconnect();
138158
}
139159

160+
140161
/**
141162
* This method connects a specific profile of this device.
142163
* The UUID provided is the remote service UUID for the profile.

lib/bluetooth/interfaces.vala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,8 @@ private interface AstalBluetooth.IDevice : DBusProxy {
4444
public abstract uint32 class { get; }
4545
}
4646

47+
[DBus (name = "org.bluez.Battery1")]
48+
private interface AstalBluetooth.IBattery : DBusProxy {
49+
public abstract uint8 percentage { get; }
50+
public abstract string source { owned get; }
51+
}

lib/bluetooth/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ sources = [config] + files(
3838
'adapter.vala',
3939
'bluetooth.vala',
4040
'device.vala',
41+
'battery.vala',
4142
'interfaces.vala',
4243
'utils.vala',
4344
)

0 commit comments

Comments
 (0)