| 1 | // SPDX-FileCopyrightText: 2022 Uniontech Software Technology Co.,Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 | |
| 5 | #include "dxsettings.h" |
| 6 | #include <QCoreApplication> |
| 7 | |
| 8 | DPP_BEGIN_NAMESPACE |
| 9 | |
| 10 | class DXcbEventFilter : public QThread |
| 11 | { |
| 12 | public: |
| 13 | DXcbEventFilter(xcb_connection_t *connection) |
| 14 | : m_connection(connection) |
| 15 | , m_threadProxy(new RunInThreadProxy(qApp)) |
| 16 | { |
| 17 | QThread::start(); |
| 18 | } |
| 19 | |
| 20 | void run() override { |
| 21 | xcb_generic_event_t *event; |
| 22 | while (m_connection && (event = xcb_wait_for_event(m_connection))) { |
| 23 | uint response_type = event->response_type & ~0x80; |
| 24 | switch (response_type) { |
| 25 | case XCB_PROPERTY_NOTIFY: { |
| 26 | m_threadProxy->proxyCall([event]() { |
| 27 | xcb_property_notify_event_t *pn = (xcb_property_notify_event_t *)event; |
| 28 | DXcbXSettings::handlePropertyNotifyEvent(pn); |
| 29 | }); |
| 30 | break; |
| 31 | } |
| 32 | |
| 33 | case XCB_CLIENT_MESSAGE: { |
| 34 | m_threadProxy->proxyCall([event]() { |
| 35 | xcb_client_message_event_t *ev = reinterpret_cast<xcb_client_message_event_t*>(event); |
| 36 | DXcbXSettings::handleClientMessageEvent(ev); |
| 37 | }); |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | xcb_connection_t *m_connection; |
| 46 | RunInThreadProxy *m_threadProxy = nullptr; |
| 47 | }; |
| 48 | |
| 49 | xcb_connection_t *DXSettings::xcb_connection = nullptr; |
| 50 | DXcbXSettings *DXSettings::m_xsettings = nullptr; |
| 51 | |
| 52 | void DXSettings::initXcbConnection() |
| 53 | { |
| 54 | static bool isInit = false; |
| 55 | |
| 56 | if (isInit && xcb_connection) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | isInit = true; |
| 61 | int primary_screen_number = 0; |
| 62 | xcb_connection = xcb_connect(qgetenv("DISPLAY"), &primary_screen_number); |
| 63 | |
| 64 | new DXcbEventFilter(xcb_connection); |
| 65 | } |
| 66 | |
| 67 | bool DXSettings::buildNativeSettings(QObject *object, quint32 settingWindow) |
| 68 | { |
| 69 | QByteArray settings_property = DNativeSettings::getSettingsProperty(object); |
| 70 | DXcbXSettings *settings = nullptr; |
| 71 | bool global_settings = false; |
| 72 | if (settingWindow || !settings_property.isEmpty()) { |
| 73 | settings = new DXcbXSettings(xcb_connection, settingWindow, settings_property); |
| 74 | } else { |
| 75 | global_settings = true; |
| 76 | settings = globalSettings(); |
| 77 | } |
| 78 | |
| 79 | // 跟随object销毁 |
| 80 | auto native_settings = new DNativeSettings(object, settings, global_settings); |
| 81 | |
| 82 | if (!native_settings->isValid()) { |
| 83 | delete native_settings; |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | void DXSettings::clearNativeSettings(quint32 settingWindow) |
| 91 | { |
| 92 | #ifdef Q_OS_LINUX |
| 93 | DXcbXSettings::clearSettings(settingWindow); |
| 94 | #endif |
| 95 | } |
| 96 | |
| 97 | DXcbXSettings *DXSettings::globalSettings() |
| 98 | { |
| 99 | if (Q_LIKELY(m_xsettings)) { |
| 100 | return m_xsettings; |
| 101 | } |
| 102 | |
| 103 | if (!xcb_connection) { |
| 104 | initXcbConnection(); |
| 105 | } |
| 106 | m_xsettings = new DXcbXSettings(xcb_connection); |
| 107 | |
| 108 | return m_xsettings; |
| 109 | } |
| 110 | |
| 111 | xcb_window_t DXSettings::getOwner(xcb_connection_t *conn, int screenNumber) { |
| 112 | return DXcbXSettings::getOwner(conn, screenNumber); |
| 113 | } |
| 114 | |
| 115 | DPP_END_NAMESPACE |