Skip to content

Commit

Permalink
Add getBatteryPercent & isCharging APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
z3ntu committed Apr 28, 2024
1 parent 6f1e921 commit 169904e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
14 changes: 14 additions & 0 deletions include/libopenrazer/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ class Device : public QObject
*/
virtual QVector<ushort> getAllowedDPI() = 0;

/*!
* Returns the percentage the battery of the device is charged.
*/
virtual double getBatteryPercent() = 0;

/*!
* Returns whether the device is currently charging.
*/
virtual bool isCharging() = 0;

/*!
* Sets the lighting to custom mode (applies effects set from defineCustomFrame()).
*
Expand Down Expand Up @@ -204,6 +214,8 @@ class Device : public ::libopenrazer::Device
QPair<uchar, QVector<::openrazer::RazerDPI>> getDPIStages() override;
ushort maxDPI() override;
QVector<ushort> getAllowedDPI() override;
double getBatteryPercent() override;
bool isCharging() override;
void displayCustomFrame() override;
void defineCustomFrame(uchar row, uchar startColumn, uchar endColumn, QVector<::openrazer::RGB> colorData) override;
::openrazer::MatrixDimensions getMatrixDimensions() override;
Expand Down Expand Up @@ -248,6 +260,8 @@ class Device : public ::libopenrazer::Device
QPair<uchar, QVector<::openrazer::RazerDPI>> getDPIStages() override;
ushort maxDPI() override;
QVector<ushort> getAllowedDPI() override;
double getBatteryPercent() override;
bool isCharging() override;
void displayCustomFrame() override;
void defineCustomFrame(uchar row, uchar startColumn, uchar endColumn, QVector<::openrazer::RGB> colorData) override;
::openrazer::MatrixDimensions getMatrixDimensions() override;
Expand Down
5 changes: 5 additions & 0 deletions src/demo/libopenrazerdemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ int main(int argc, char *argv[])
qDebug() << "Matrix dimensions:" << device->getMatrixDimensions();
}

if (device->hasFeature("battery")) {
qDebug() << "Battery:" << device->getBatteryPercent() << "%";
qDebug() << "Charging:" << device->isCharging();
}

for (libopenrazer::Led *led : device->getLeds()) {
qDebug() << "LED:" << led->getLedId();
if (led->hasBrightness()) {
Expand Down
27 changes: 27 additions & 0 deletions src/openrazer/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ void DevicePrivate::setupCapabilities()
supportedFeatures.append("poll_rate");
if (hasCapabilityInternal("razer.device.lighting.chroma", "setCustom"))
supportedFeatures.append("custom_frame");
if (hasCapabilityInternal("razer.device.power", "getBattery"))
supportedFeatures.append("battery");

// razer.device.lighting.chroma more than only the normal fx, so check for methods directly
if (hasCapabilityInternal("razer.device.lighting.chroma", "setNone")
Expand Down Expand Up @@ -269,6 +271,18 @@ ushort Device::maxDPI()
return handleDBusReply(reply, Q_FUNC_INFO);
}

double Device::getBatteryPercent()
{
QDBusReply<double> reply = d->devicePowerIface()->call("getBattery");
return handleDBusReply(reply, Q_FUNC_INFO);
}

bool Device::isCharging()
{
QDBusReply<bool> reply = d->devicePowerIface()->call("isCharging");
return handleDBusReply(reply, Q_FUNC_INFO);
}

QVector<ushort> Device::getAllowedDPI()
{
QDBusReply<QVector<int>> reply = d->deviceDpiIface()->call("availableDPI");
Expand Down Expand Up @@ -339,6 +353,19 @@ QDBusInterface *DevicePrivate::deviceDpiIface()
return ifaceDpi;
}

QDBusInterface *DevicePrivate::devicePowerIface()
{
if (ifacePower == nullptr) {
ifacePower = new QDBusInterface(OPENRAZER_SERVICE_NAME, mObjectPath.path(), "razer.device.power",
OPENRAZER_DBUS_BUS, mParent);
}
if (!ifacePower->isValid()) {
fprintf(stderr, "%s\n",
qPrintable(OPENRAZER_DBUS_BUS.lastError().message()));
}
return ifacePower;
}

QDBusInterface *DevicePrivate::deviceLightingChromaIface()
{
if (ifaceLightingChroma == nullptr) {
Expand Down
2 changes: 2 additions & 0 deletions src/openrazer/device_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ class DevicePrivate

QDBusInterface *ifaceMisc = nullptr;
QDBusInterface *ifaceDpi = nullptr;
QDBusInterface *ifacePower = nullptr;
QDBusInterface *ifaceLightingChroma = nullptr;
QDBusInterface *deviceMiscIface();
QDBusInterface *deviceDpiIface();
QDBusInterface *devicePowerIface();
QDBusInterface *deviceLightingChromaIface();

QDBusObjectPath mObjectPath;
Expand Down
12 changes: 12 additions & 0 deletions src/razer_test/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ QVector<ushort> Device::getAllowedDPI()
return {};
}

double Device::getBatteryPercent()
{
// TODO Needs implementation
return 100;
}

bool Device::isCharging()
{
// TODO Needs implementation
return true;
}

void Device::displayCustomFrame()
{
QDBusReply<bool> reply = d->deviceIface()->call("displayCustomFrame");
Expand Down

0 comments on commit 169904e

Please sign in to comment.