| 1 | // SPDX-FileCopyrightText: 2017 - 2022 Uniontech Software Technology Co.,Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 | |
| 5 | #define private public |
| 6 | #include "QtWaylandClient/private/qwaylandnativeinterface_p.h" |
| 7 | #undef private |
| 8 | |
| 9 | #include "dwaylandinterfacehook.h" |
| 10 | #include "dhighdpi.h" |
| 11 | #include "dxsettings.h" |
| 12 | #include "dnotitlebarwindowhelper_wl.h" |
| 13 | |
| 14 | #include <qpa/qplatformnativeinterface.h> |
| 15 | #include <private/qguiapplication_p.h> |
| 16 | #include <QtWaylandClientVersion> |
| 17 | |
| 18 | #include <QDebug> |
| 19 | #include <QLoggingCategory> |
| 20 | |
| 21 | #ifndef QT_DEBUG |
| 22 | Q_LOGGING_CATEGORY(dwli, "dtk.wayland.interface" , QtInfoMsg); |
| 23 | #else |
| 24 | Q_LOGGING_CATEGORY(dwli, "dtk.wayland.interface"); |
| 25 | #endif |
| 26 | |
| 27 | DPP_BEGIN_NAMESPACE |
| 28 | |
| 29 | static QFunctionPointer getFunction(const QByteArray &function) |
| 30 | { |
| 31 | static QHash<QByteArray, QFunctionPointer> functionCache = { |
| 32 | {buildNativeSettings, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::buildNativeSettings)}, |
| 33 | {clearNativeSettings, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::clearNativeSettings)}, |
| 34 | {setEnableNoTitlebar, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::setEnableNoTitlebar)}, |
| 35 | {isEnableNoTitlebar, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::isEnableNoTitlebar)}, |
| 36 | {setWindowRadius, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::setWindowRadius)}, |
| 37 | {setBorderColor, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::setBorderColor)}, |
| 38 | {setBorderWidth, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::setBorderWidth)}, |
| 39 | {setShadowColor, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::setShadowColor)}, |
| 40 | {setShadowOffset, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::setShadowOffset)}, |
| 41 | {setShadowRadius, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::setShadowRadius)}, |
| 42 | {setWindowEffect, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::setWindowEffect)}, |
| 43 | {setWindowStartUpEffect, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::setWindowStartUpEffect)}, |
| 44 | {setWindowProperty, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::setWindowProperty)}, |
| 45 | {popupSystemWindowMenu, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::popupSystemWindowMenu)}, |
| 46 | {enableDwayland, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::enableDwayland)}, |
| 47 | {isEnableDwayland, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::isEnableDwayland)}, |
| 48 | {splitWindowOnScreen, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::splitWindowOnScreen)}, |
| 49 | {supportForSplittingWindow, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::supportForSplittingWindow)}, |
| 50 | {splitWindowOnScreenByType, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::splitWindowOnScreenByType)}, |
| 51 | {supportForSplittingWindowByType, reinterpret_cast<QFunctionPointer>(&DWaylandInterfaceHook::supportForSplittingWindowByType)} |
| 52 | }; |
| 53 | return functionCache.value(function); |
| 54 | } |
| 55 | |
| 56 | QFunctionPointer DWaylandInterfaceHook::platformFunction(QPlatformNativeInterface *interface, const QByteArray &function) |
| 57 | { |
| 58 | QFunctionPointer f = getFunction(function); |
| 59 | |
| 60 | #if QTWAYLANDCLIENT_VERSION >= QT_VERSION_CHECK(5, 4, 0) |
| 61 | if (Q_UNLIKELY(!f)) { |
| 62 | f = static_cast<QtWaylandClient::QWaylandNativeInterface*>(interface)->QtWaylandClient::QWaylandNativeInterface::platformFunction(function); |
| 63 | } |
| 64 | #endif |
| 65 | return f; |
| 66 | } |
| 67 | |
| 68 | bool DWaylandInterfaceHook::buildNativeSettings(QObject *object, quint32 settingWindow) { |
| 69 | return dXSettings->buildNativeSettings(object, settingWindow); |
| 70 | } |
| 71 | |
| 72 | void DWaylandInterfaceHook::clearNativeSettings(quint32 settingWindow) { |
| 73 | dXSettings->clearNativeSettings(settingWindow); |
| 74 | } |
| 75 | |
| 76 | bool DWaylandInterfaceHook::setEnableNoTitlebar(QWindow *window, bool enable) |
| 77 | { |
| 78 | if (enable) { |
| 79 | if (DNoTitlebarWlWindowHelper::mapped.value(window)) |
| 80 | return true; |
| 81 | if (window->type() == Qt::Desktop) |
| 82 | return false; |
| 83 | window->setProperty(noTitlebar, true); |
| 84 | Q_UNUSED(new DNoTitlebarWlWindowHelper(window)) |
| 85 | return true; |
| 86 | } else { |
| 87 | if (auto helper = DNoTitlebarWlWindowHelper::mapped.value(window)) { |
| 88 | helper->deleteLater(); |
| 89 | } |
| 90 | window->setProperty(noTitlebar, false); |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | bool DWaylandInterfaceHook::isEnableNoTitlebar(QWindow *window) |
| 97 | { |
| 98 | return window->property(noTitlebar).toBool(); |
| 99 | } |
| 100 | |
| 101 | void DWaylandInterfaceHook::setWindowRadius(QWindow *window, int value) |
| 102 | { |
| 103 | if (!window) |
| 104 | return; |
| 105 | DNoTitlebarWlWindowHelper::setWindowProperty(window, ::windowRadius, value); |
| 106 | } |
| 107 | |
| 108 | void DWaylandInterfaceHook::setBorderColor(QWindow *window, const QColor &value) |
| 109 | { |
| 110 | if (!window) |
| 111 | return; |
| 112 | DNoTitlebarWlWindowHelper::setWindowProperty(window, ::borderColor, value); |
| 113 | } |
| 114 | |
| 115 | void DWaylandInterfaceHook::setShadowColor(QWindow *window, const QColor &value) |
| 116 | { |
| 117 | if (!window) |
| 118 | return; |
| 119 | DNoTitlebarWlWindowHelper::setWindowProperty(window, ::shadowColor, value); |
| 120 | } |
| 121 | |
| 122 | void DWaylandInterfaceHook::setShadowRadius(QWindow *window, int value) |
| 123 | { |
| 124 | if (!window) |
| 125 | return; |
| 126 | DNoTitlebarWlWindowHelper::setWindowProperty(window, ::shadowRadius, value); |
| 127 | } |
| 128 | |
| 129 | void DWaylandInterfaceHook::setShadowOffset(QWindow *window, const QPoint &value) |
| 130 | { |
| 131 | if (!window) |
| 132 | return; |
| 133 | |
| 134 | QPoint offect = value; |
| 135 | if (window->screen()) |
| 136 | offect *= window->screen()->devicePixelRatio(); |
| 137 | |
| 138 | DNoTitlebarWlWindowHelper::setWindowProperty(window, ::shadowOffset, offect); |
| 139 | } |
| 140 | |
| 141 | void DWaylandInterfaceHook::setBorderWidth(QWindow *window, int value) |
| 142 | { |
| 143 | if (!window) |
| 144 | return; |
| 145 | DNoTitlebarWlWindowHelper::setWindowProperty(window, ::borderWidth, value); |
| 146 | } |
| 147 | |
| 148 | void DWaylandInterfaceHook::setWindowEffect(QWindow *window, const QVariant &value) |
| 149 | { |
| 150 | if (!window) |
| 151 | return; |
| 152 | DNoTitlebarWlWindowHelper::setWindowProperty(window, ::windowEffect, value); |
| 153 | } |
| 154 | |
| 155 | void DWaylandInterfaceHook::setWindowStartUpEffect(QWindow *window, const QVariant &value) |
| 156 | { |
| 157 | if (!window) |
| 158 | return; |
| 159 | DNoTitlebarWlWindowHelper::setWindowProperty(window, ::windowStartUpEffect, value); |
| 160 | } |
| 161 | |
| 162 | void DWaylandInterfaceHook::setWindowProperty(QWindow *window, const char *name, const QVariant &value) |
| 163 | { |
| 164 | DNoTitlebarWlWindowHelper::setWindowProperty(window, name, value); |
| 165 | } |
| 166 | |
| 167 | void DWaylandInterfaceHook::popupSystemWindowMenu(WId wid) |
| 168 | { |
| 169 | DNoTitlebarWlWindowHelper::popupSystemWindowMenu(wid); |
| 170 | } |
| 171 | |
| 172 | bool DWaylandInterfaceHook::enableDwayland(QWindow *window) |
| 173 | { |
| 174 | static bool xwayland = QByteArrayLiteral("wayland") == qgetenv("XDG_SESSION_TYPE") |
| 175 | && !qEnvironmentVariableIsEmpty("WAYLAND_DISPLAY"); |
| 176 | |
| 177 | if (xwayland) { |
| 178 | // for xwayland |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | if (window->type() == Qt::Desktop) |
| 183 | return false; |
| 184 | |
| 185 | QPlatformWindow *xw = static_cast<QPlatformWindow*>(window->handle()); |
| 186 | |
| 187 | if (!xw) { |
| 188 | window->setProperty(useDwayland, true); |
| 189 | |
| 190 | return true; |
| 191 | } |
| 192 | if (DNoTitlebarWlWindowHelper::mapped.value(window)) |
| 193 | return true; |
| 194 | |
| 195 | if (xw->isExposed()) |
| 196 | return false; |
| 197 | |
| 198 | #ifndef USE_NEW_IMPLEMENTING |
| 199 | return false; |
| 200 | #endif |
| 201 | |
| 202 | window->setProperty(useDwayland, true); |
| 203 | // window->setProperty("_d_dwayland_TransparentBackground", window->format().hasAlpha()); |
| 204 | |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | bool DWaylandInterfaceHook::isEnableDwayland(const QWindow *window) |
| 209 | { |
| 210 | return window->property(useDwayland).toBool(); |
| 211 | } |
| 212 | |
| 213 | void DWaylandInterfaceHook::splitWindowOnScreen(WId wid, quint32 type) |
| 214 | { |
| 215 | return splitWindowOnScreenByType(wid, 1, type); |
| 216 | } |
| 217 | |
| 218 | void DWaylandInterfaceHook::splitWindowOnScreenByType(WId wid, quint32 position, quint32 type) |
| 219 | { |
| 220 | QWindow *window = fromQtWinId(wid); |
| 221 | if(!window || !window->handle()) |
| 222 | return; |
| 223 | // position: 15 not preview |
| 224 | if (position == 15) { |
| 225 | if (window->windowStates().testFlag(Qt::WindowMaximized)) { |
| 226 | window->showNormal(); |
| 227 | } else { |
| 228 | window->showMaximized(); |
| 229 | } |
| 230 | } else { |
| 231 | // type 1:two splitting 2:three splitting 4:four splitting |
| 232 | // position enum class SplitType in kwin-dev, Left=0x1, Right=0x10, Top=0x100, Bottom=0x1000 |
| 233 | QVariantList value{position, type}; |
| 234 | DNoTitlebarWlWindowHelper::setWindowProperty(window, ::splitWindowOnScreen, value); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | bool DWaylandInterfaceHook::supportForSplittingWindow(WId wid) |
| 239 | { |
| 240 | return supportForSplittingWindowByType(wid, 1); |
| 241 | } |
| 242 | |
| 243 | // screenSplittingType: 0: can't splitting, 1:two splitting, 2: four splitting(includ three splitting) |
| 244 | bool DWaylandInterfaceHook::supportForSplittingWindowByType(quint32 wid, quint32 screenSplittingType) |
| 245 | { |
| 246 | QWindow *window = fromQtWinId(wid); |
| 247 | if(!window || !window->handle()) |
| 248 | return false; |
| 249 | DNoTitlebarWlWindowHelper::setWindowProperty(window, ::supportForSplittingWindow, false); |
| 250 | return window->property(::supportForSplittingWindow).toInt() >= screenSplittingType; |
| 251 | } |
| 252 | |
| 253 | DPP_END_NAMESPACE |