Merge pull request #2886 from levnikmyskin/feat/upower_bat_bluetooth
Bluetooth module: fetch battery percentage from upower if not found from bluez
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
#include "modules/bluetooth.hpp"
|
#include "modules/bluetooth.hpp"
|
||||||
|
|
||||||
#include <fmt/format.h>
|
#include <fmt/format.h>
|
||||||
|
#include <libupower-glib/upower.h>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@ -392,6 +393,30 @@ auto waybar::modules::Bluetooth::getDeviceBatteryPercentage(GDBusObject* object)
|
|||||||
|
|
||||||
return battery_percentage;
|
return battery_percentage;
|
||||||
}
|
}
|
||||||
|
GDBusProxy* proxy_device = G_DBUS_PROXY(g_dbus_object_get_interface(object, "org.bluez.Device1"));
|
||||||
|
if (proxy_device != nullptr) {
|
||||||
|
auto serial = getStringProperty(proxy_device, "Address");
|
||||||
|
std::transform(serial.begin(), serial.end(), serial.begin(),
|
||||||
|
[](unsigned char c) { return std::tolower(c); });
|
||||||
|
|
||||||
|
auto* client = up_client_new();
|
||||||
|
if (client == nullptr) return std::nullopt;
|
||||||
|
|
||||||
|
auto* devices = up_client_get_devices2(client);
|
||||||
|
UpDevice* dev;
|
||||||
|
char* udev_serial;
|
||||||
|
double percentage;
|
||||||
|
for (int i = 0; i < devices->len; i++) {
|
||||||
|
dev = (UpDevice*)g_ptr_array_index(devices, i);
|
||||||
|
g_object_get(dev, "serial", &udev_serial, nullptr);
|
||||||
|
if (serial == udev_serial) {
|
||||||
|
g_object_get(dev, "percentage", &percentage, nullptr);
|
||||||
|
g_ptr_array_unref(devices);
|
||||||
|
g_object_unref(client);
|
||||||
|
return percentage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user