|
| 1 | +// Copyright 2007-2022 Aleix Pol Gonzalez <[email protected]>. |
| 2 | +// All rights reserved. |
| 3 | +// Use of this source code is governed by a BSD-style license |
| 4 | +// that can be found in the LICENSE file at the root of the |
| 5 | +// Mumble source tree or at <https://www.mumble.info/LICENSE>. |
| 6 | + |
| 7 | +#include "GlobalShortcut_xdp.h" |
| 8 | + |
| 9 | +#include "Settings.h" |
| 10 | +#include "Global.h" |
| 11 | +#include "globalshortcuts_portal_interface.h" |
| 12 | +#include "portalsrequest_interface.h" |
| 13 | +#include "GlobalShortcutTypes.h" |
| 14 | + |
| 15 | +#include <QtGui/QWindow> |
| 16 | + |
| 17 | +Q_GLOBAL_STATIC_WITH_ARGS(OrgFreedesktopPortalGlobalShortcutsInterface, s_shortcutsInterface, (QLatin1String("org.freedesktop.portal.Desktop"), |
| 18 | + QLatin1String("/org/freedesktop/portal/desktop"), |
| 19 | + QDBusConnection::sessionBus())); |
| 20 | + |
| 21 | +bool GlobalShortcutXdp::isAvailable() |
| 22 | +{ |
| 23 | + return s_shortcutsInterface->isValid(); |
| 24 | +} |
| 25 | + |
| 26 | +GlobalShortcutXdp::GlobalShortcutXdp() { |
| 27 | + qDBusRegisterMetaType<XdpShortcuts>(); |
| 28 | + qDBusRegisterMetaType<QPair<QString, QVariantMap>>(); |
| 29 | + |
| 30 | + connect(s_shortcutsInterface, &OrgFreedesktopPortalGlobalShortcutsInterface::Activated, this, [this] (const QDBusObjectPath &session_handle, const QString &shortcut_id, qulonglong timestamp, const QVariantMap &options) { |
| 31 | + Q_UNUSED(session_handle); |
| 32 | + Q_UNUSED(timestamp); |
| 33 | + Q_UNUSED(options); |
| 34 | + |
| 35 | + foreach (GlobalShortcut *shortcut, qmShortcuts) { |
| 36 | + if (shortcut_id == shortcut->objectName()) { |
| 37 | + shortcut->triggered(true, shortcut_id); |
| 38 | + shortcut->down(shortcut_id); |
| 39 | + } |
| 40 | + } |
| 41 | + }); |
| 42 | + connect(s_shortcutsInterface, &OrgFreedesktopPortalGlobalShortcutsInterface::Deactivated, this, [this] (const QDBusObjectPath &session_handle, const QString &shortcut_id, qulonglong timestamp, const QVariantMap &options) { |
| 43 | + Q_UNUSED(session_handle); |
| 44 | + Q_UNUSED(timestamp); |
| 45 | + Q_UNUSED(options); |
| 46 | + |
| 47 | + foreach (GlobalShortcut *shortcut, qmShortcuts) { |
| 48 | + if (shortcut_id == shortcut->objectName()) { |
| 49 | + shortcut->triggered(false, shortcut_id); |
| 50 | + } |
| 51 | + } |
| 52 | + }); |
| 53 | + connect(s_shortcutsInterface, &OrgFreedesktopPortalGlobalShortcutsInterface::ShortcutsChanged, this, [this] (const QDBusObjectPath &session_handle, const QList<QPair<QString,QVariantMap>> &shortcuts) { |
| 54 | + Q_UNUSED(session_handle); |
| 55 | + qDebug() << "ShortcutsChanged" << shortcuts; |
| 56 | + if (m_shortcuts != shortcuts) { |
| 57 | + m_shortcuts = shortcuts; |
| 58 | + Q_EMIT shortcutsChanged(); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + QTimer::singleShot(0, this, &GlobalShortcutXdp::createSession); |
| 63 | +} |
| 64 | + |
| 65 | +GlobalShortcutXdp::~GlobalShortcutXdp() { |
| 66 | + |
| 67 | +} |
| 68 | + |
| 69 | +void GlobalShortcutXdp::createSession() |
| 70 | +{ |
| 71 | + XdpShortcuts initialShortcuts; |
| 72 | + initialShortcuts.reserve(qmShortcuts.count()); |
| 73 | + Global::get().s.qlShortcuts.clear(); |
| 74 | + |
| 75 | + int i = 0; |
| 76 | + m_ids.resize(qmShortcuts.count()); |
| 77 | + foreach (GlobalShortcut *shortcut, qmShortcuts) { |
| 78 | + initialShortcuts.append({shortcut->objectName(), { |
| 79 | + { QStringLiteral("description"), shortcut->name } |
| 80 | + }}); |
| 81 | + |
| 82 | + Shortcut ssss = { i, {uint(i)}, shortcut->qvDefault, false}; |
| 83 | + m_ids[i] = shortcut->objectName(); |
| 84 | + Global::get().s.qlShortcuts << ssss; |
| 85 | + ++i; |
| 86 | + } |
| 87 | + |
| 88 | + QDBusArgument arg; |
| 89 | + arg << initialShortcuts; |
| 90 | + auto reply = s_shortcutsInterface->CreateSession({ |
| 91 | + { QLatin1String("session_handle_token"), "Mumble" }, |
| 92 | + { QLatin1String("handle_token"), QLatin1String("mumble") }, |
| 93 | + { QLatin1String("shortcuts"), QVariant::fromValue(arg) }, |
| 94 | + }); |
| 95 | + reply.waitForFinished(); |
| 96 | + if (reply.isError()) { |
| 97 | + qWarning() << "Couldn't get reply"; |
| 98 | + qWarning() << "Error:" << reply.error().message(); |
| 99 | + } else { |
| 100 | + QDBusConnection::sessionBus().connect(QString(), |
| 101 | + reply.value().path(), |
| 102 | + QLatin1String("org.freedesktop.portal.Request"), |
| 103 | + QLatin1String("Response"), |
| 104 | + this, |
| 105 | + SLOT(gotGlobalShortcutsCreateSessionResponse(uint,QVariantMap))); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +void GlobalShortcutXdp::run() { |
| 110 | + // 🤘🤪 |
| 111 | +} |
| 112 | + |
| 113 | +bool GlobalShortcutXdp::canDisable() { |
| 114 | + return false; |
| 115 | +} |
| 116 | + |
| 117 | +GlobalShortcutXdp::ButtonInfo GlobalShortcutXdp::buttonInfo(const QVariant &v) { |
| 118 | + ButtonInfo info; |
| 119 | + bool ok; |
| 120 | + unsigned int key = v.toUInt(&ok); |
| 121 | + if (!ok) { |
| 122 | + return info; |
| 123 | + } |
| 124 | + |
| 125 | + info.device = tr("Desktop"); |
| 126 | + info.devicePrefix = QString(); |
| 127 | + const auto id = m_ids[key]; |
| 128 | + for (const auto &x : m_shortcuts) { |
| 129 | + if (x.first == id) { |
| 130 | + info.name = x.second["trigger_description"].toString(); |
| 131 | + } |
| 132 | + } |
| 133 | + return info; |
| 134 | +} |
| 135 | + |
| 136 | +static QString parentWindowId() |
| 137 | +{ |
| 138 | + if (qEnvironmentVariableIsSet("WAYLAND_DISPLAY")) { |
| 139 | + // TODO |
| 140 | + return {}; |
| 141 | + } |
| 142 | + return QLatin1String("x11:") + QString::number(qApp->focusWindow()->winId()); |
| 143 | +} |
| 144 | + |
| 145 | +void GlobalShortcutXdp::gotGlobalShortcutsCreateSessionResponse(uint code, const QVariantMap &results) |
| 146 | +{ |
| 147 | + if (code != 0) { |
| 148 | + qWarning() << "failed to create a global shortcuts session" << code << results; |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + m_globalShortcutsSession = QDBusObjectPath(results["session_handle"].toString()); |
| 153 | + |
| 154 | + auto reply = s_shortcutsInterface->ListShortcuts(m_globalShortcutsSession, {}); |
| 155 | + reply.waitForFinished(); |
| 156 | + if (reply.isError()) { |
| 157 | + qWarning() << "failed to call ListShortcuts" << reply.error(); |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + auto req = new OrgFreedesktopPortalRequestInterface(QLatin1String("org.freedesktop.portal.Desktop"), |
| 162 | + reply.value().path(), QDBusConnection::sessionBus(), this); |
| 163 | + |
| 164 | + // BindShortcuts and ListShortcuts answer the same |
| 165 | + connect(req, &OrgFreedesktopPortalRequestInterface::Response, this, &GlobalShortcutXdp::gotListShortcutsResponse); |
| 166 | + connect(req, &OrgFreedesktopPortalRequestInterface::Response, req, &QObject::deleteLater); |
| 167 | +} |
| 168 | + |
| 169 | +void GlobalShortcutXdp::gotListShortcutsResponse(uint, const QVariantMap &results) |
| 170 | +{ |
| 171 | + const auto arg = results["shortcuts"].value<QDBusArgument>(); |
| 172 | + arg >> m_shortcuts; |
| 173 | +} |
| 174 | + |
| 175 | +void GlobalShortcutXdp::configure() |
| 176 | +{ |
| 177 | + auto reply = s_shortcutsInterface->BindShortcuts(m_globalShortcutsSession, m_shortcuts, parentWindowId(), {}); |
| 178 | + reply.waitForFinished(); |
| 179 | + if (reply.isError()) { |
| 180 | + qWarning() << "failed to call ListShortcuts" << reply.error(); |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + auto req = new OrgFreedesktopPortalRequestInterface(QLatin1String("org.freedesktop.portal.Desktop"), |
| 185 | + reply.value().path(), QDBusConnection::sessionBus(), this); |
| 186 | + |
| 187 | + // BindShortcuts and ListShortcuts answer the same |
| 188 | + connect(req, &OrgFreedesktopPortalRequestInterface::Response, this, &GlobalShortcutXdp::gotListShortcutsResponse); |
| 189 | + connect(req, &OrgFreedesktopPortalRequestInterface::Response, req, &QObject::deleteLater); |
| 190 | +} |
0 commit comments