-
Notifications
You must be signed in to change notification settings - Fork 17
feat: switch camera hotkey to daemon hardware privacy switch #163
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
Merged
Merged
Changes from all commits
Commits
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
This file was deleted.
Oops, something went wrong.
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
192 changes: 192 additions & 0 deletions
192
src/plugin-qt/shortcut/tools/dde-shortcut-tool/cameracontroller.cpp
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,192 @@ | ||
| // SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. | ||
| // | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
|
||
| #include "cameracontroller.h" | ||
|
|
||
| #include <QDBusConnection> | ||
| #include <QDBusInterface> | ||
| #include <QDBusReply> | ||
| #include <QDebug> | ||
|
|
||
|
|
||
| namespace { | ||
|
|
||
| // dde-system-daemon owns the camera privacy switch: it applies the V4L2 | ||
| // privacy control when the hardware exposes one and otherwise unbinds the | ||
| // uvcvideo driver on the video interface (which keeps a built-in mic on the | ||
| // same device working). | ||
| constexpr const char *kDaemonService = "org.deepin.dde.Daemon1"; | ||
| constexpr const char *kDaemonPath = "/org/deepin/dde/Daemon1"; | ||
| constexpr const char *kDaemonInterface = "org.deepin.dde.Daemon1"; | ||
|
|
||
|
|
||
| } // namespace | ||
|
|
||
| CameraController::CameraController(QObject *parent) | ||
| : BaseController(parent) | ||
| , m_daemonInterface(nullptr) | ||
| { | ||
| m_daemonInterface = new QDBusInterface( | ||
| kDaemonService, | ||
| kDaemonPath, | ||
| kDaemonInterface, | ||
| QDBusConnection::systemBus(), | ||
| this | ||
| ); | ||
|
|
||
| if (!m_daemonInterface->isValid()) { | ||
| qWarning() << "Failed to connect to Daemon1 service:" | ||
| << m_daemonInterface->lastError().message(); | ||
| } | ||
| } | ||
|
|
||
| CameraController::~CameraController() | ||
| { | ||
| } | ||
|
|
||
| QStringList CameraController::commandActions() | ||
| { | ||
| return QStringList{ | ||
| "toggle", | ||
| "on", | ||
| "off" | ||
| }; | ||
| } | ||
|
|
||
| QMap<QString, QString> CameraController::commandActionHelp() | ||
| { | ||
| return { | ||
| {"toggle", "Toggle camera privacy (camera off/on)"}, | ||
| {"on", "Enable the camera (privacy off)"}, | ||
| {"off", "Disable the camera (privacy on)"} | ||
| }; | ||
| } | ||
|
|
||
| QStringList CameraController::supportedActions() const | ||
| { | ||
| return commandActions(); | ||
| } | ||
|
|
||
| bool CameraController::execute(const QString &action, const QStringList &args) | ||
| { | ||
| Q_UNUSED(args); | ||
|
|
||
| if (action == "toggle") { | ||
| return toggle(); | ||
| } else if (action == "on") { | ||
| return setPrivacy(false); | ||
| } else if (action == "off") { | ||
| return setPrivacy(true); | ||
| } | ||
|
|
||
| qWarning() << "Unknown camera action:" << action; | ||
| return false; | ||
| } | ||
|
|
||
| QString CameraController::actionHelp(const QString &action) const | ||
| { | ||
| return commandActionHelp().value(action); | ||
| } | ||
|
|
||
| bool CameraController::toggle() | ||
| { | ||
| bool known = false; | ||
| const bool privacy = currentPrivacy(&known); | ||
| if (!known) { | ||
| // No camera device to switch: the hotkey only drives hardware, so do | ||
| // nothing rather than launching a camera application. | ||
| return false; | ||
| } | ||
| return setPrivacy(!privacy); | ||
| } | ||
|
|
||
| bool CameraController::setPrivacy(bool privacy) | ||
| { | ||
| bool applied = false; | ||
| if (!requestPrivacy(privacy, &applied)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!applied) { | ||
| // No camera the daemon can switch at the hardware level: stay silent | ||
| // and do not fall back to the camera application. | ||
| return false; | ||
| } | ||
|
|
||
| showOSD(privacy ? "CameraOff" : "CameraOn"); | ||
| return true; | ||
| } | ||
|
|
||
|
|
||
| bool CameraController::currentPrivacy(bool *known) const | ||
| { | ||
| if (known) { | ||
| *known = false; | ||
| } | ||
|
|
||
| if (!m_daemonInterface || !m_daemonInterface->isValid()) { | ||
| qWarning() << "Daemon1 interface not available"; | ||
| return false; | ||
| } | ||
|
|
||
| QDBusMessage reply = m_daemonInterface->call("GetCameraPrivacy"); | ||
| if (reply.type() != QDBusMessage::ReplyMessage) { | ||
| qWarning() << "GetCameraPrivacy failed:" << reply.errorMessage(); | ||
| return false; | ||
| } | ||
|
|
||
| const QList<QVariant> values = reply.arguments(); | ||
| if (values.size() < 2) { | ||
| qWarning() << "GetCameraPrivacy returned unexpected arguments:" << values.size(); | ||
| return false; | ||
| } | ||
|
|
||
| const bool privacy = values.at(0).toBool(); | ||
| const bool deviceKnown = values.at(1).toBool(); | ||
| if (known) { | ||
| *known = deviceKnown; | ||
| } | ||
| return privacy; | ||
| } | ||
|
|
||
| bool CameraController::requestPrivacy(bool privacy, bool *applied) | ||
| { | ||
| if (applied) { | ||
| *applied = false; | ||
| } | ||
|
|
||
| if (!m_daemonInterface || !m_daemonInterface->isValid()) { | ||
| qWarning() << "Daemon1 interface not available"; | ||
| return false; | ||
| } | ||
|
|
||
| QDBusReply<bool> reply = m_daemonInterface->call("SetCameraPrivacy", privacy); | ||
| if (!reply.isValid()) { | ||
| qWarning() << "SetCameraPrivacy failed:" << reply.error().message(); | ||
| return false; | ||
| } | ||
|
|
||
| if (applied) { | ||
| *applied = reply.value(); | ||
| } | ||
| qDebug() << "SetCameraPrivacy privacy:" << privacy << "applied:" << reply.value(); | ||
| return true; | ||
| } | ||
|
|
||
|
|
||
| void CameraController::showOSD(const QString &signal) | ||
| { | ||
| QDBusInterface osdInterface( | ||
| "org.deepin.dde.Osd1", | ||
| "/org/deepin/dde/shell/osd", | ||
| "org.deepin.dde.shell.osd", | ||
| QDBusConnection::sessionBus() | ||
| ); | ||
|
|
||
| if (osdInterface.isValid()) { | ||
| osdInterface.call("ShowOSD", signal); | ||
| } else { | ||
| qWarning() << "Failed to connect to OSD interface"; | ||
| } | ||
| } | ||
54 changes: 54 additions & 0 deletions
54
src/plugin-qt/shortcut/tools/dde-shortcut-tool/cameracontroller.h
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,54 @@ | ||
| // SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. | ||
| // | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
|
||
| #ifndef CAMERACONTROLLER_H | ||
| #define CAMERACONTROLLER_H | ||
|
|
||
| #include "basecontroller.h" | ||
|
|
||
| #include <QMap> | ||
|
|
||
| class QDBusInterface; | ||
|
|
||
| /** | ||
| * @brief Camera hotkey controller | ||
| * | ||
| * Toggles camera privacy through the system daemon | ||
| * (org.deepin.dde.Daemon1.SetCameraPrivacy). The hotkey only drives the | ||
| * hardware switch; it shows the matching OSD and never launches or closes a | ||
| * camera application. | ||
| */ | ||
| class CameraController : public BaseController | ||
| { | ||
| Q_OBJECT | ||
|
|
||
| public: | ||
| explicit CameraController(QObject *parent = nullptr); | ||
| ~CameraController() override; | ||
|
|
||
| static QString commandName() { return "camera"; } | ||
| static QStringList commandActions(); | ||
| static QMap<QString, QString> commandActionHelp(); | ||
|
|
||
| // BaseController interface | ||
| QString name() const override { return commandName(); } | ||
| QStringList supportedActions() const override; | ||
| bool execute(const QString &action, const QStringList &args = QStringList()) override; | ||
| QString actionHelp(const QString &action) const override; | ||
|
|
||
| private: | ||
| bool toggle(); | ||
| bool setPrivacy(bool privacy); | ||
| // Returns the daemon's privacy state; known is false when no camera is | ||
| // available to report on. | ||
| bool currentPrivacy(bool *known) const; | ||
| // Asks the daemon to switch the camera; applied reports whether hardware | ||
| // switching happened. | ||
| bool requestPrivacy(bool privacy, bool *applied); | ||
| void showOSD(const QString &signal); | ||
|
|
||
| QDBusInterface *m_daemonInterface; | ||
| }; | ||
|
|
||
| #endif // CAMERACONTROLLER_H | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.