| 1 | // SPDX-FileCopyrightText: 2017 - 2022 Uniontech Software Technology Co.,Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 | |
| 5 | #include "dwaylandintegration.h" |
| 6 | #include "dwaylandinterfacehook.h" |
| 7 | #include "vtablehook.h" |
| 8 | #include "dxsettings.h" |
| 9 | |
| 10 | #include <wayland-cursor.h> |
| 11 | |
| 12 | #define private public |
| 13 | #include <QtWaylandClient/private/qwaylanddisplay_p.h> |
| 14 | #include <QtWaylandClient/private/qwaylandscreen_p.h> |
| 15 | #include <QtWaylandClient/private/qwaylandcursor_p.h> |
| 16 | #include <QtWaylandClient/private/qwaylandinputdevice_p.h> |
| 17 | #undef private |
| 18 | #include <QtWaylandClientVersion> |
| 19 | |
| 20 | #include <QDebug> |
| 21 | #include <QTimer> |
| 22 | |
| 23 | #include <qpa/qplatformnativeinterface.h> |
| 24 | #include <private/qguiapplication_p.h> |
| 25 | |
| 26 | DPP_BEGIN_NAMESPACE |
| 27 | |
| 28 | #define XSETTINGS_DOUBLE_CLICK_TIME QByteArrayLiteral("Net/DoubleClickTime") |
| 29 | #define XSETTINGS_CURSOR_THEME_NAME QByteArrayLiteral("Gtk/CursorThemeName") |
| 30 | #define XSETTINGS_PRIMARY_MONITOR_RECT QByteArrayLiteral("DDE/PrimaryMonitorRect") |
| 31 | |
| 32 | enum XSettingType:quint64 { |
| 33 | Gtk_CursorThemeName, |
| 34 | Dde_PrimaryMonitorRect |
| 35 | }; |
| 36 | |
| 37 | static void overrideChangeCursor(QPlatformCursor *cursorHandle, QCursor * cursor, QWindow * widget) |
| 38 | { |
| 39 | if (!(widget && widget->handle())) |
| 40 | return; |
| 41 | |
| 42 | if (widget->property(disableOverrideCursor).toBool()) |
| 43 | return; |
| 44 | |
| 45 | // qtwayland里面判断了,如果没有设置环境变量,就用默认大小32, 这里通过设在环境变量将大小设置我们需要的24 |
| 46 | static bool xcursorSizeIsSet = qEnvironmentVariableIsSet("XCURSOR_SIZE"); |
| 47 | if (!xcursorSizeIsSet) { |
| 48 | qputenv("XCURSOR_SIZE", QByteArray::number(24 * qApp->devicePixelRatio())); |
| 49 | } |
| 50 | |
| 51 | HookCall(cursorHandle, &QPlatformCursor::changeCursor, cursor, widget); |
| 52 | |
| 53 | // 调用changeCursor后,控制中心窗口的光标没有及时更新,这里强制刷新一下 |
| 54 | for (auto *inputDevice : DWaylandIntegration::instance()->display()->inputDevices()) { |
| 55 | if (inputDevice->pointer()) { |
| 56 | inputDevice->pointer()->updateCursor(); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static void onXSettingsChanged(xcb_connection_t *connection, const QByteArray &name, const QVariant &property, void *handle) |
| 62 | { |
| 63 | Q_UNUSED(connection) |
| 64 | Q_UNUSED(property) |
| 65 | Q_UNUSED(name) |
| 66 | |
| 67 | quint64 type = reinterpret_cast<quint64>(handle); |
| 68 | |
| 69 | switch (type) { |
| 70 | case Gtk_CursorThemeName: |
| 71 | const QByteArray &cursor_name = dXSettings->globalSettings()->setting(name).toByteArray(); |
| 72 | |
| 73 | #if QTWAYLANDCLIENT_VERSION < QT_VERSION_CHECK(5, 13, 0) |
| 74 | const auto &cursor_map = DWaylandIntegration::instance()->display()->mCursorThemesBySize; |
| 75 | #elif QTWAYLANDCLIENT_VERSION < QT_VERSION_CHECK(5, 16, 0) |
| 76 | const auto &cursor_map = DWaylandIntegration::instance()->display()->mCursorThemes; |
| 77 | #endif |
| 78 | // 处理光标主题变化 |
| 79 | for (auto cursor = cursor_map.constBegin(); cursor != cursor_map.constEnd(); ++cursor) { |
| 80 | QtWaylandClient::QWaylandCursorTheme *ct = cursor.value(); |
| 81 | // 根据大小记载新的主题 |
| 82 | |
| 83 | #if QTWAYLANDCLIENT_VERSION < QT_VERSION_CHECK(5, 13, 0) |
| 84 | auto theme = wl_cursor_theme_load(cursor_name.constData(), cursor.key(), DWaylandIntegration::instance()->display()->shm()->object()); |
| 85 | #elif QTWAYLANDCLIENT_VERSION < QT_VERSION_CHECK(5, 16, 0) |
| 86 | auto theme = wl_cursor_theme_load(cursor_name.constData(), cursor.key().second, DWaylandIntegration::instance()->display()->shm()->object()); |
| 87 | #endif |
| 88 | // 如果新主题创建失败则不更新数据 |
| 89 | if (!theme) |
| 90 | continue; |
| 91 | |
| 92 | // 先尝试销毁旧主题 |
| 93 | if (ct->m_theme) { |
| 94 | wl_cursor_theme_destroy(ct->m_theme); |
| 95 | } |
| 96 | |
| 97 | // 清理缓存数据 |
| 98 | ct->m_cursors.clear(); |
| 99 | ct->m_theme = theme; |
| 100 | } |
| 101 | |
| 102 | // 更新窗口光标 |
| 103 | for (auto s : DWaylandIntegration::instance()->display()->screens()) { |
| 104 | for (QWindow *w : s->windows()) { |
| 105 | QCursor cursor = w->cursor(); |
| 106 | // 为窗口重新设置光标 |
| 107 | s->cursor()->changeCursor(&cursor, w); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | break; |
| 112 | |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | static void onPrimaryRectChanged(xcb_connection_t *connection, const QByteArray &name, const QVariant &property, void *handle) |
| 117 | { |
| 118 | Q_UNUSED(connection) |
| 119 | Q_UNUSED(property) |
| 120 | |
| 121 | quint64 type = reinterpret_cast<quint64>(handle); |
| 122 | switch (type) { |
| 123 | case Dde_PrimaryMonitorRect: |
| 124 | { |
| 125 | auto screens = DWaylandIntegration::instance()->display()->screens(); |
| 126 | const QString &primaryScreenRect = dXSettings->globalSettings()->setting(name).toString(); |
| 127 | auto list = primaryScreenRect.split('-'); |
| 128 | if (list.size() != 4) |
| 129 | return; |
| 130 | |
| 131 | QRect xsettingsRect(list.at(0).toInt(), list.at(1).toInt(), list.at(2).toInt(), list.at(3).toInt()); |
| 132 | |
| 133 | qDebug() << "primary rect from xsettings:" << primaryScreenRect << ", key:" << name; |
| 134 | for (auto s : screens) { |
| 135 | qDebug() << "screen info:" |
| 136 | << s->screen()->name() << s->screen()->model() << s->screen()->geometry() << s->geometry() |
| 137 | << s->name() << s->model() << s->screen()->geometry() << s->geometry(); |
| 138 | } |
| 139 | |
| 140 | // 设置新的主屏 |
| 141 | QtWaylandClient::QWaylandScreen *primaryScreen = nullptr; |
| 142 | |
| 143 | // 坐标一致,认定为主屏 |
| 144 | for (auto screen : screens) { |
| 145 | if (screen->geometry().topLeft() == xsettingsRect.topLeft() && screen->screen() != qApp->primaryScreen()) { |
| 146 | primaryScreen = screen; |
| 147 | qDebug() << "got primary:" << primaryScreen->name() << primaryScreen->geometry(); |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (primaryScreen) { |
| 153 | #if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0) |
| 154 | QWindowSystemInterface::handlePrimaryScreenChanged(primaryScreen); |
| 155 | #else |
| 156 | DWaylandIntegration::instance()->setPrimaryScreen(primaryScreen); |
| 157 | #endif |
| 158 | } |
| 159 | |
| 160 | break; |
| 161 | } |
| 162 | default: |
| 163 | break; |
| 164 | } |
| 165 | qDebug() << "primary screen info:" << QGuiApplication::primaryScreen()->name() << QGuiApplication::primaryScreen()->model() << QGuiApplication::primaryScreen()->geometry(); |
| 166 | } |
| 167 | |
| 168 | DWaylandIntegration::DWaylandIntegration() |
| 169 | { |
| 170 | dXSettings->initXcbConnection(); |
| 171 | } |
| 172 | |
| 173 | void DWaylandIntegration::initialize() |
| 174 | { |
| 175 | // 由于Qt代码中可能写死了判断是不是wayland平台,所以只能伪装成是wayland |
| 176 | if (qgetenv("DXCB_FAKE_PLATFORM_NAME_XCB") != "0") { |
| 177 | *QGuiApplicationPrivate::platform_name = "wayland"; |
| 178 | } |
| 179 | qApp->setProperty("_d_isDwayland", true); |
| 180 | |
| 181 | QWaylandIntegration::initialize(); |
| 182 | // 覆盖wayland的平台函数接口,用于向上层返回一些必要的与平台相关的函数供其使用 |
| 183 | HookOverride(nativeInterface(), &QPlatformNativeInterface::platformFunction, &DWaylandInterfaceHook::platformFunction); |
| 184 | // hook qtwayland的改变光标的函数,用于设置环境变量并及时更新 |
| 185 | for (QScreen *screen : qApp->screens()) { |
| 186 | //在没有屏幕的时候,qtwayland会创建一个虚拟屏幕,此时不能调用screen->handle()->cursor() |
| 187 | if (screen && screen->handle() && screen->handle()->cursor()) { |
| 188 | HookOverride(screen->handle()->cursor(), &QPlatformCursor::changeCursor, &overrideChangeCursor); |
| 189 | } |
| 190 | } |
| 191 | // 监听xsettings的信号,用于更新程序状态(如更新光标主题) |
| 192 | dXSettings->globalSettings()->registerCallbackForProperty(XSETTINGS_CURSOR_THEME_NAME, onXSettingsChanged, reinterpret_cast<void*>(XSettingType::Gtk_CursorThemeName)); |
| 193 | |
| 194 | // 增加rect的属性,保存主屏的具体坐标,不依靠其name判断(根据name查找对应的屏幕时概率性出错,根据主屏的rect确定哪一个QScreen才是主屏) |
| 195 | dXSettings->globalSettings()->registerCallbackForProperty(XSETTINGS_PRIMARY_MONITOR_RECT, onPrimaryRectChanged, reinterpret_cast<void*>(XSettingType::Dde_PrimaryMonitorRect)); |
| 196 | |
| 197 | //初始化时应该设一次主屏,防止应用启动时主屏闪变 |
| 198 | onPrimaryRectChanged(nullptr, XSETTINGS_PRIMARY_MONITOR_RECT, QVariant(), reinterpret_cast<void*>(XSettingType::Dde_PrimaryMonitorRect)); |
| 199 | |
| 200 | QTimer *m_delayTimer = new QTimer; |
| 201 | m_delayTimer->setInterval(10); |
| 202 | m_delayTimer->setSingleShot(true); |
| 203 | QObject::connect(qApp, &QGuiApplication::aboutToQuit, m_delayTimer, &QObject::deleteLater); |
| 204 | |
| 205 | QObject::connect(m_delayTimer, &QTimer::timeout, []{ |
| 206 | onPrimaryRectChanged(nullptr, XSETTINGS_PRIMARY_MONITOR_RECT, QVariant(), reinterpret_cast<void*>(XSettingType::Dde_PrimaryMonitorRect)); |
| 207 | }); |
| 208 | |
| 209 | // 显示器信息发生变化时,刷新主屏信息 |
| 210 | auto handleScreenInfoChanged = [ = ](QScreen *s) { |
| 211 | #define HANDLE_SCREEN_SIGNAL(signal) \ |
| 212 | QObject::connect(s, signal, m_delayTimer, static_cast<void (QTimer::*)()>(&QTimer::start)); |
| 213 | |
| 214 | HANDLE_SCREEN_SIGNAL(&QScreen::geometryChanged); |
| 215 | HANDLE_SCREEN_SIGNAL(&QScreen::availableGeometryChanged); |
| 216 | HANDLE_SCREEN_SIGNAL(&QScreen::physicalSizeChanged); |
| 217 | HANDLE_SCREEN_SIGNAL(&QScreen::physicalDotsPerInchChanged); |
| 218 | HANDLE_SCREEN_SIGNAL(&QScreen::logicalDotsPerInchChanged); |
| 219 | HANDLE_SCREEN_SIGNAL(&QScreen::virtualGeometryChanged); |
| 220 | HANDLE_SCREEN_SIGNAL(&QScreen::primaryOrientationChanged); |
| 221 | HANDLE_SCREEN_SIGNAL(&QScreen::orientationChanged); |
| 222 | HANDLE_SCREEN_SIGNAL(&QScreen::refreshRateChanged); |
| 223 | |
| 224 | m_delayTimer->start(); |
| 225 | }; |
| 226 | |
| 227 | for (auto s : qApp->screens()) { |
| 228 | handleScreenInfoChanged(s); |
| 229 | } |
| 230 | |
| 231 | QObject::connect(qApp, &QGuiApplication::screenAdded, handleScreenInfoChanged); |
| 232 | QObject::connect(qApp, &QGuiApplication::screenAdded, m_delayTimer, static_cast<void (QTimer::*)()>(&QTimer::start)); |
| 233 | } |
| 234 | |
| 235 | QStringList DWaylandIntegration::themeNames() const |
| 236 | { |
| 237 | auto list = QWaylandIntegration::themeNames(); |
| 238 | const QByteArray desktop_session = qgetenv("DESKTOP_SESSION"); |
| 239 | |
| 240 | // 在lightdm环境中,无此环境变量。默认使用deepin平台主题 |
| 241 | if (desktop_session.isEmpty() || desktop_session == "deepin") |
| 242 | list.prepend("deepin"); |
| 243 | |
| 244 | return list; |
| 245 | } |
| 246 | |
| 247 | #define GET_VALID_XSETTINGS(key) { \ |
| 248 | auto value = dXSettings->globalSettings()->setting(key); \ |
| 249 | if (value.isValid()) return value; \ |
| 250 | } |
| 251 | |
| 252 | QVariant DWaylandIntegration::styleHint(QPlatformIntegration::StyleHint hint) const |
| 253 | { |
| 254 | #ifdef Q_OS_LINUX |
| 255 | switch ((int)hint) { |
| 256 | case MouseDoubleClickInterval: |
| 257 | GET_VALID_XSETTINGS(XSETTINGS_DOUBLE_CLICK_TIME); |
| 258 | break; |
| 259 | case ShowShortcutsInContextMenus: |
| 260 | return false; |
| 261 | default: |
| 262 | break; |
| 263 | } |
| 264 | #endif |
| 265 | |
| 266 | return QtWaylandClient::QWaylandIntegration::styleHint(hint); |
| 267 | } |
| 268 | |
| 269 | #if QT_VERSION < QT_VERSION_CHECK(5, 12, 0) |
| 270 | void DWaylandIntegration::setPrimaryScreen(QPlatformScreen *newPrimary) |
| 271 | { |
| 272 | return QPlatformIntegration::setPrimaryScreen(newPrimary); |
| 273 | } |
| 274 | #endif |
| 275 | |
| 276 | DPP_END_NAMESPACE |