|
| 1 | +#include "inhibitor.hpp" |
| 2 | + |
| 3 | +#include <private/qwaylandwindow_p.h> |
| 4 | +#include <qguiapplication.h> |
| 5 | +#include <qlogging.h> |
| 6 | +#include <qloggingcategory.h> |
| 7 | +#include <qobject.h> |
| 8 | +#include <qtmetamacros.h> |
| 9 | +#include <qwindow.h> |
| 10 | + |
| 11 | +#include "../../window/proxywindow.hpp" |
| 12 | +#include "../../window/windowinterface.hpp" |
| 13 | +#include "proto.hpp" |
| 14 | + |
| 15 | +namespace qs::wayland::shortcuts_inhibit { |
| 16 | +using QtWaylandClient::QWaylandWindow; |
| 17 | + |
| 18 | +ShortcutInhibitor::ShortcutInhibitor() { |
| 19 | + this->bBoundWindow.setBinding([this] { |
| 20 | + return this->bEnabled ? this->bWindowObject.value() : nullptr; |
| 21 | + }); |
| 22 | + |
| 23 | + this->bActive.setBinding([this]() { |
| 24 | + auto* inhibitor = this->bInhibitor.value(); |
| 25 | + if (!inhibitor) return false; |
| 26 | + if (!inhibitor->bindableActive().value()) return false; |
| 27 | + return this->bWindow.value() == this->bFocusedWindow; |
| 28 | + }); |
| 29 | + |
| 30 | + QObject::connect( |
| 31 | + dynamic_cast<QGuiApplication*>(QGuiApplication::instance()), |
| 32 | + &QGuiApplication::focusWindowChanged, |
| 33 | + this, |
| 34 | + &ShortcutInhibitor::onFocusedWindowChanged |
| 35 | + ); |
| 36 | + |
| 37 | + this->onFocusedWindowChanged(QGuiApplication::focusWindow()); |
| 38 | +} |
| 39 | + |
| 40 | +ShortcutInhibitor::~ShortcutInhibitor() { |
| 41 | + if (!this->bInhibitor) return; |
| 42 | + |
| 43 | + auto* manager = impl::ShortcutsInhibitManager::instance(); |
| 44 | + if (!manager) return; |
| 45 | + |
| 46 | + manager->unrefShortcutsInhibitor(this->bInhibitor); |
| 47 | +} |
| 48 | + |
| 49 | +void ShortcutInhibitor::onBoundWindowChanged() { |
| 50 | + auto* window = this->bBoundWindow.value(); |
| 51 | + auto* proxyWindow = qobject_cast<ProxyWindowBase*>(window); |
| 52 | + |
| 53 | + if (!proxyWindow) { |
| 54 | + if (auto* iface = qobject_cast<WindowInterface*>(window)) { |
| 55 | + proxyWindow = iface->proxyWindow(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (proxyWindow == this->proxyWindow) return; |
| 60 | + |
| 61 | + if (this->proxyWindow) { |
| 62 | + QObject::disconnect(this->proxyWindow, nullptr, this, nullptr); |
| 63 | + this->proxyWindow = nullptr; |
| 64 | + } |
| 65 | + |
| 66 | + if (this->mWaylandWindow) { |
| 67 | + QObject::disconnect(this->mWaylandWindow, nullptr, this, nullptr); |
| 68 | + this->mWaylandWindow = nullptr; |
| 69 | + this->onWaylandSurfaceDestroyed(); |
| 70 | + } |
| 71 | + |
| 72 | + if (proxyWindow) { |
| 73 | + this->proxyWindow = proxyWindow; |
| 74 | + |
| 75 | + QObject::connect(proxyWindow, &QObject::destroyed, this, &ShortcutInhibitor::onWindowDestroyed); |
| 76 | + |
| 77 | + QObject::connect( |
| 78 | + proxyWindow, |
| 79 | + &ProxyWindowBase::backerVisibilityChanged, |
| 80 | + this, |
| 81 | + &ShortcutInhibitor::onWindowVisibilityChanged |
| 82 | + ); |
| 83 | + |
| 84 | + this->onWindowVisibilityChanged(); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void ShortcutInhibitor::onWindowDestroyed() { |
| 89 | + this->proxyWindow = nullptr; |
| 90 | + this->onWaylandSurfaceDestroyed(); |
| 91 | +} |
| 92 | + |
| 93 | +void ShortcutInhibitor::onWindowVisibilityChanged() { |
| 94 | + if (!this->proxyWindow->isVisibleDirect()) return; |
| 95 | + |
| 96 | + auto* window = this->proxyWindow->backingWindow(); |
| 97 | + if (!window->handle()) window->create(); |
| 98 | + |
| 99 | + auto* waylandWindow = dynamic_cast<QWaylandWindow*>(window->handle()); |
| 100 | + if (!waylandWindow) { |
| 101 | + qCCritical(impl::logShortcutsInhibit()) << "Window handle is not a QWaylandWindow"; |
| 102 | + return; |
| 103 | + } |
| 104 | + if (waylandWindow == this->mWaylandWindow) return; |
| 105 | + this->mWaylandWindow = waylandWindow; |
| 106 | + this->bWindow = window; |
| 107 | + |
| 108 | + QObject::connect( |
| 109 | + waylandWindow, |
| 110 | + &QObject::destroyed, |
| 111 | + this, |
| 112 | + &ShortcutInhibitor::onWaylandWindowDestroyed |
| 113 | + ); |
| 114 | + |
| 115 | + QObject::connect( |
| 116 | + waylandWindow, |
| 117 | + &QWaylandWindow::surfaceCreated, |
| 118 | + this, |
| 119 | + &ShortcutInhibitor::onWaylandSurfaceCreated |
| 120 | + ); |
| 121 | + |
| 122 | + QObject::connect( |
| 123 | + waylandWindow, |
| 124 | + &QWaylandWindow::surfaceDestroyed, |
| 125 | + this, |
| 126 | + &ShortcutInhibitor::onWaylandSurfaceDestroyed |
| 127 | + ); |
| 128 | + |
| 129 | + if (waylandWindow->surface()) this->onWaylandSurfaceCreated(); |
| 130 | +} |
| 131 | + |
| 132 | +void ShortcutInhibitor::onWaylandWindowDestroyed() { this->mWaylandWindow = nullptr; } |
| 133 | + |
| 134 | +void ShortcutInhibitor::onWaylandSurfaceCreated() { |
| 135 | + auto* manager = impl::ShortcutsInhibitManager::instance(); |
| 136 | + |
| 137 | + if (!manager) { |
| 138 | + qWarning() << "Cannot enable shortcuts inhibitor as keyboard-shortcuts-inhibit-unstable-v1 is " |
| 139 | + "not supported by " |
| 140 | + "the current compositor."; |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + if (this->bInhibitor) { |
| 145 | + qFatal("ShortcutsInhibitor: inhibitor already exists when creating surface"); |
| 146 | + } |
| 147 | + |
| 148 | + this->bInhibitor = manager->createShortcutsInhibitor(this->mWaylandWindow); |
| 149 | +} |
| 150 | + |
| 151 | +void ShortcutInhibitor::onWaylandSurfaceDestroyed() { |
| 152 | + if (!this->bInhibitor) return; |
| 153 | + |
| 154 | + auto* manager = impl::ShortcutsInhibitManager::instance(); |
| 155 | + if (!manager) return; |
| 156 | + |
| 157 | + manager->unrefShortcutsInhibitor(this->bInhibitor); |
| 158 | + this->bInhibitor = nullptr; |
| 159 | +} |
| 160 | + |
| 161 | +void ShortcutInhibitor::onInhibitorChanged() { |
| 162 | + auto* inhibitor = this->bInhibitor.value(); |
| 163 | + if (inhibitor) { |
| 164 | + QObject::connect( |
| 165 | + inhibitor, |
| 166 | + &impl::ShortcutsInhibitor::activeChanged, |
| 167 | + this, |
| 168 | + &ShortcutInhibitor::onInhibitorActiveChanged |
| 169 | + ); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +void ShortcutInhibitor::onInhibitorActiveChanged() { |
| 174 | + auto* inhibitor = this->bInhibitor.value(); |
| 175 | + if (inhibitor && !inhibitor->isActive()) { |
| 176 | + // Compositor has deactivated the inhibitor, making it invalid. |
| 177 | + // Set enabled to false so the user can enable it again to create a new inhibitor. |
| 178 | + this->bEnabled = false; |
| 179 | + emit this->cancelled(); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +void ShortcutInhibitor::onFocusedWindowChanged(QWindow* focusedWindow) { |
| 184 | + this->bFocusedWindow = focusedWindow; |
| 185 | +} |
| 186 | + |
| 187 | +} // namespace qs::wayland::shortcuts_inhibit |
0 commit comments