-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add D-Bus interface for battery monitoring #407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
1jehuang
wants to merge
4
commits into
kavishdevar:main
Choose a base branch
from
1jehuang:feature/dbus-battery-interface
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+165
−11
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| #pragma once | ||
|
|
||
| #include <QObject> | ||
| #include <QDBusAbstractAdaptor> | ||
| #include <QDBusConnection> | ||
| #include <QDBusMessage> | ||
| #include "battery.hpp" | ||
| #include "deviceinfo.hpp" | ||
|
|
||
| class BatteryDBusAdaptor : public QDBusAbstractAdaptor | ||
| { | ||
| Q_OBJECT | ||
| Q_CLASSINFO("D-Bus Interface", "me.kavishdevar.librepods.Battery") | ||
|
|
||
| // Battery levels (0-100) | ||
| Q_PROPERTY(int LeftLevel READ leftLevel NOTIFY BatteryChanged) | ||
| Q_PROPERTY(int RightLevel READ rightLevel NOTIFY BatteryChanged) | ||
| Q_PROPERTY(int CaseLevel READ caseLevel NOTIFY BatteryChanged) | ||
| Q_PROPERTY(int HeadsetLevel READ headsetLevel NOTIFY BatteryChanged) | ||
|
|
||
| // Charging status | ||
| Q_PROPERTY(bool LeftCharging READ leftCharging NOTIFY BatteryChanged) | ||
| Q_PROPERTY(bool RightCharging READ rightCharging NOTIFY BatteryChanged) | ||
| Q_PROPERTY(bool CaseCharging READ caseCharging NOTIFY BatteryChanged) | ||
| Q_PROPERTY(bool HeadsetCharging READ headsetCharging NOTIFY BatteryChanged) | ||
|
|
||
| // Availability (connected/detected) | ||
| Q_PROPERTY(bool LeftAvailable READ leftAvailable NOTIFY BatteryChanged) | ||
| Q_PROPERTY(bool RightAvailable READ rightAvailable NOTIFY BatteryChanged) | ||
| Q_PROPERTY(bool CaseAvailable READ caseAvailable NOTIFY BatteryChanged) | ||
| Q_PROPERTY(bool HeadsetAvailable READ headsetAvailable NOTIFY BatteryChanged) | ||
|
|
||
| // Device info | ||
| Q_PROPERTY(QString DeviceName READ deviceName NOTIFY DeviceChanged) | ||
| Q_PROPERTY(bool Connected READ connected NOTIFY DeviceChanged) | ||
|
|
||
| public: | ||
| BatteryDBusAdaptor(Battery *battery, DeviceInfo *deviceInfo, QObject *parent) | ||
| : QDBusAbstractAdaptor(parent), m_battery(battery), m_deviceInfo(deviceInfo) | ||
| { | ||
| setAutoRelaySignals(true); | ||
|
|
||
| // Connect battery signals to our relay | ||
| connect(m_battery, &Battery::batteryStatusChanged, this, [this]() { | ||
| emit BatteryChanged(); | ||
| }); | ||
|
|
||
| connect(m_deviceInfo, &DeviceInfo::batteryStatusChanged, this, [this]() { | ||
| emit BatteryChanged(); | ||
| }); | ||
|
|
||
| connect(m_deviceInfo, &DeviceInfo::deviceNameChanged, this, [this]() { | ||
| emit DeviceChanged(); | ||
| }); | ||
| } | ||
|
|
||
| // Battery levels | ||
| int leftLevel() const { return m_battery->getLeftPodLevel(); } | ||
| int rightLevel() const { return m_battery->getRightPodLevel(); } | ||
| int caseLevel() const { return m_battery->getCaseLevel(); } | ||
| int headsetLevel() const { return m_battery->getHeadsetLevel(); } | ||
|
|
||
| // Charging status | ||
| bool leftCharging() const { return m_battery->isLeftPodCharging(); } | ||
| bool rightCharging() const { return m_battery->isRightPodCharging(); } | ||
| bool caseCharging() const { return m_battery->isCaseCharging(); } | ||
| bool headsetCharging() const { return m_battery->isHeadsetCharging(); } | ||
|
|
||
| // Availability | ||
| bool leftAvailable() const { return m_battery->isLeftPodAvailable(); } | ||
| bool rightAvailable() const { return m_battery->isRightPodAvailable(); } | ||
| bool caseAvailable() const { return m_battery->isCaseAvailable(); } | ||
| bool headsetAvailable() const { return m_battery->isHeadsetAvailable(); } | ||
|
|
||
| // Device info - connected if device name is set and any battery is available | ||
| QString deviceName() const { return m_deviceInfo->deviceName(); } | ||
| bool connected() const { | ||
| return !m_deviceInfo->deviceName().isEmpty() && | ||
| (leftAvailable() || rightAvailable() || headsetAvailable()); | ||
| } | ||
|
|
||
| public slots: | ||
| // Method to get all battery info at once (useful for waybar) | ||
| QVariantMap GetBatteryInfo() | ||
| { | ||
| QVariantMap info; | ||
| info["left_level"] = leftLevel(); | ||
| info["left_charging"] = leftCharging(); | ||
| info["left_available"] = leftAvailable(); | ||
| info["right_level"] = rightLevel(); | ||
| info["right_charging"] = rightCharging(); | ||
| info["right_available"] = rightAvailable(); | ||
| info["case_level"] = caseLevel(); | ||
| info["case_charging"] = caseCharging(); | ||
| info["case_available"] = caseAvailable(); | ||
| info["headset_level"] = headsetLevel(); | ||
| info["headset_charging"] = headsetCharging(); | ||
| info["headset_available"] = headsetAvailable(); | ||
| info["device_name"] = deviceName(); | ||
| info["connected"] = connected(); | ||
| return info; | ||
| } | ||
|
|
||
| signals: | ||
| void BatteryChanged(); | ||
| void DeviceChanged(); | ||
|
|
||
| private: | ||
| Battery *m_battery; | ||
| DeviceInfo *m_deviceInfo; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: kavishdevar/librepods
Length of output: 4236
🏁 Script executed:
Repository: kavishdevar/librepods
Length of output: 1716
🏁 Script executed:
Repository: kavishdevar/librepods
Length of output: 5212
Add D-Bus cleanup on registration failures and application shutdown.
The D-Bus initialization code is mostly correct. The
BatteryDBusAdaptormemory management is proper—it correctly uses Qt's parent-child pattern by passingthistoQDBusAbstractAdaptoras a parent (line 38-39).However, there are two issues to address:
Partial initialization cleanup missing: If service registration succeeds (line 166) but object registration fails (line 172), the service remains registered without an object. Consider calling
sessionBus.unregisterService()before returning on object registration failure.No D-Bus cleanup on shutdown: The
aboutToQuithandler (line 1142-1147) only closes the local server but doesn't unregister the D-Bus service and object. Add calls tosessionBus.unregisterObject()andsessionBus.unregisterService()to properly clean up on shutdown.Service name conflicts are mitigated by the existing single-instance mechanism (via
QLocalServer), which prevents concurrent instances from running.🤖 Prompt for AI Agents