| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the plugins of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qxcbsystemtraytracker.h" |
| 41 | #include "qxcbconnection.h" |
| 42 | #include "qxcbscreen.h" |
| 43 | |
| 44 | #include <QtCore/QDebug> |
| 45 | #include <QtCore/QRect> |
| 46 | #include <QtGui/QScreen> |
| 47 | |
| 48 | #include <qpa/qplatformnativeinterface.h> |
| 49 | |
| 50 | QT_BEGIN_NAMESPACE |
| 51 | |
| 52 | enum { |
| 53 | SystemTrayRequestDock = 0, |
| 54 | SystemTrayBeginMessage = 1, |
| 55 | SystemTrayCancelMessage = 2 |
| 56 | }; |
| 57 | |
| 58 | // QXcbSystemTrayTracker provides API for accessing the tray window and tracks |
| 59 | // its lifecyle by listening for its destruction and recreation. |
| 60 | // See http://standards.freedesktop.org/systemtray-spec/systemtray-spec-latest.html |
| 61 | |
| 62 | QXcbSystemTrayTracker *QXcbSystemTrayTracker::create(QXcbConnection *connection) |
| 63 | { |
| 64 | // Selection, tray atoms for GNOME, NET WM Specification |
| 65 | const xcb_atom_t trayAtom = connection->atom(qatom: QXcbAtom::_NET_SYSTEM_TRAY_OPCODE); |
| 66 | if (!trayAtom) |
| 67 | return nullptr; |
| 68 | const QByteArray netSysTray = QByteArrayLiteral("_NET_SYSTEM_TRAY_S" ) + QByteArray::number(connection->primaryScreenNumber()); |
| 69 | const xcb_atom_t selection = connection->internAtom(name: netSysTray.constData()); |
| 70 | if (!selection) |
| 71 | return nullptr; |
| 72 | |
| 73 | return new QXcbSystemTrayTracker(connection, trayAtom, selection); |
| 74 | } |
| 75 | |
| 76 | QXcbSystemTrayTracker::QXcbSystemTrayTracker(QXcbConnection *connection, |
| 77 | xcb_atom_t trayAtom, |
| 78 | xcb_atom_t selection) |
| 79 | : QObject(connection) |
| 80 | , m_selection(selection) |
| 81 | , m_trayAtom(trayAtom) |
| 82 | , m_connection(connection) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | xcb_window_t QXcbSystemTrayTracker::locateTrayWindow(const QXcbConnection *connection, xcb_atom_t selection) |
| 87 | { |
| 88 | auto reply = Q_XCB_REPLY(xcb_get_selection_owner, connection->xcb_connection(), selection); |
| 89 | if (!reply) |
| 90 | return 0; |
| 91 | return reply->owner; |
| 92 | } |
| 93 | |
| 94 | // Request a window to be docked on the tray. |
| 95 | void QXcbSystemTrayTracker::requestSystemTrayWindowDock(xcb_window_t window) const |
| 96 | { |
| 97 | xcb_client_message_event_t trayRequest; |
| 98 | trayRequest.response_type = XCB_CLIENT_MESSAGE; |
| 99 | trayRequest.format = 32; |
| 100 | trayRequest.sequence = 0; |
| 101 | trayRequest.window = m_trayWindow; |
| 102 | trayRequest.type = m_trayAtom; |
| 103 | trayRequest.data.data32[0] = XCB_CURRENT_TIME; |
| 104 | trayRequest.data.data32[1] = SystemTrayRequestDock; |
| 105 | trayRequest.data.data32[2] = window; |
| 106 | xcb_send_event(c: m_connection->xcb_connection(), propagate: 0, destination: m_trayWindow, event_mask: XCB_EVENT_MASK_NO_EVENT, event: (const char *)&trayRequest); |
| 107 | } |
| 108 | |
| 109 | // API for QPlatformNativeInterface/QPlatformSystemTrayIcon: Return tray window. |
| 110 | xcb_window_t QXcbSystemTrayTracker::trayWindow() |
| 111 | { |
| 112 | if (!m_trayWindow) { |
| 113 | m_trayWindow = QXcbSystemTrayTracker::locateTrayWindow(connection: m_connection, selection: m_selection); |
| 114 | if (m_trayWindow) { // Listen for DestroyNotify on tray. |
| 115 | m_connection->addWindowEventListener(id: m_trayWindow, eventListener: this); |
| 116 | const quint32 mask = XCB_CW_EVENT_MASK; |
| 117 | const quint32 value = XCB_EVENT_MASK_STRUCTURE_NOTIFY; |
| 118 | xcb_change_window_attributes(c: m_connection->xcb_connection(), window: m_trayWindow, value_mask: mask, value_list: &value); |
| 119 | } |
| 120 | } |
| 121 | return m_trayWindow; |
| 122 | } |
| 123 | |
| 124 | inline void QXcbSystemTrayTracker::emitSystemTrayWindowChanged() |
| 125 | { |
| 126 | if (const QPlatformScreen *ps = m_connection->primaryScreen()) |
| 127 | emit systemTrayWindowChanged(screen: ps->screen()); |
| 128 | } |
| 129 | |
| 130 | // Client messages with the "MANAGER" atom on the root window indicate creation of a new tray. |
| 131 | void QXcbSystemTrayTracker::notifyManagerClientMessageEvent(const xcb_client_message_event_t *t) |
| 132 | { |
| 133 | if (t->data.data32[1] == m_selection) |
| 134 | emitSystemTrayWindowChanged(); |
| 135 | } |
| 136 | |
| 137 | // Listen for destruction of the tray. |
| 138 | void QXcbSystemTrayTracker::handleDestroyNotifyEvent(const xcb_destroy_notify_event_t *event) |
| 139 | { |
| 140 | if (event->window == m_trayWindow) { |
| 141 | m_connection->removeWindowEventListener(id: m_trayWindow); |
| 142 | m_trayWindow = XCB_WINDOW_NONE; |
| 143 | emitSystemTrayWindowChanged(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | xcb_visualid_t QXcbSystemTrayTracker::visualId() |
| 148 | { |
| 149 | xcb_visualid_t visual = netSystemTrayVisual(); |
| 150 | if (visual == XCB_NONE) |
| 151 | visual = m_connection->primaryScreen()->screen()->root_visual; |
| 152 | return visual; |
| 153 | } |
| 154 | |
| 155 | xcb_visualid_t QXcbSystemTrayTracker::netSystemTrayVisual() |
| 156 | { |
| 157 | if (m_trayWindow == XCB_WINDOW_NONE) |
| 158 | return XCB_NONE; |
| 159 | |
| 160 | xcb_atom_t tray_atom = m_connection->atom(qatom: QXcbAtom::_NET_SYSTEM_TRAY_VISUAL); |
| 161 | |
| 162 | // Get the xcb property for the _NET_SYSTEM_TRAY_VISUAL atom |
| 163 | auto systray_atom_reply = Q_XCB_REPLY_UNCHECKED(xcb_get_property, m_connection->xcb_connection(), |
| 164 | false, m_trayWindow, |
| 165 | tray_atom, XCB_ATOM_VISUALID, 0, 1); |
| 166 | if (!systray_atom_reply) |
| 167 | return XCB_NONE; |
| 168 | |
| 169 | xcb_visualid_t systrayVisualId = XCB_NONE; |
| 170 | if (systray_atom_reply->value_len > 0 && xcb_get_property_value_length(R: systray_atom_reply.get()) > 0) { |
| 171 | xcb_visualid_t * vids = (uint32_t *)xcb_get_property_value(R: systray_atom_reply.get()); |
| 172 | systrayVisualId = vids[0]; |
| 173 | } |
| 174 | |
| 175 | return systrayVisualId; |
| 176 | } |
| 177 | |
| 178 | QT_END_NAMESPACE |
| 179 | |