1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qxcbsystemtraytracker.h"
5#include "qxcbconnection.h"
6#include "qxcbscreen.h"
7
8#include <QtCore/QDebug>
9#include <QtCore/QRect>
10#include <QtGui/QScreen>
11
12#include <qpa/qplatformnativeinterface.h>
13
14QT_BEGIN_NAMESPACE
15
16enum {
17 SystemTrayRequestDock = 0,
18 SystemTrayBeginMessage = 1,
19 SystemTrayCancelMessage = 2
20};
21
22// QXcbSystemTrayTracker provides API for accessing the tray window and tracks
23// its lifecycle by listening for its destruction and recreation.
24// See http://standards.freedesktop.org/systemtray-spec/systemtray-spec-latest.html
25
26QXcbSystemTrayTracker *QXcbSystemTrayTracker::create(QXcbConnection *connection)
27{
28 // Selection, tray atoms for GNOME, NET WM Specification
29 const xcb_atom_t trayAtom = connection->atom(qatom: QXcbAtom::Atom_NET_SYSTEM_TRAY_OPCODE);
30 if (!trayAtom)
31 return nullptr;
32 const QByteArray netSysTray = QByteArrayLiteral("_NET_SYSTEM_TRAY_S") + QByteArray::number(connection->primaryScreenNumber());
33 const xcb_atom_t selection = connection->internAtom(name: netSysTray.constData());
34 if (!selection)
35 return nullptr;
36
37 return new QXcbSystemTrayTracker(connection, trayAtom, selection);
38}
39
40QXcbSystemTrayTracker::QXcbSystemTrayTracker(QXcbConnection *connection,
41 xcb_atom_t trayAtom,
42 xcb_atom_t selection)
43 : QObject(connection)
44 , m_selection(selection)
45 , m_trayAtom(trayAtom)
46 , m_connection(connection)
47{
48}
49
50// Request a window to be docked on the tray.
51void QXcbSystemTrayTracker::requestSystemTrayWindowDock(xcb_window_t window) const
52{
53 xcb_client_message_event_t trayRequest;
54 trayRequest.response_type = XCB_CLIENT_MESSAGE;
55 trayRequest.format = 32;
56 trayRequest.sequence = 0;
57 trayRequest.window = m_trayWindow;
58 trayRequest.type = m_trayAtom;
59 trayRequest.data.data32[0] = XCB_CURRENT_TIME;
60 trayRequest.data.data32[1] = SystemTrayRequestDock;
61 trayRequest.data.data32[2] = window;
62 xcb_send_event(c: m_connection->xcb_connection(), propagate: 0, destination: m_trayWindow, event_mask: XCB_EVENT_MASK_NO_EVENT, event: (const char *)&trayRequest);
63}
64
65// API for QPlatformNativeInterface/QPlatformSystemTrayIcon: Return tray window.
66xcb_window_t QXcbSystemTrayTracker::trayWindow()
67{
68 if (!m_trayWindow) {
69 m_trayWindow = m_connection->selectionOwner(atom: m_selection);
70 if (m_trayWindow) { // Listen for DestroyNotify on tray.
71 m_connection->addWindowEventListener(id: m_trayWindow, eventListener: this);
72 const quint32 mask = XCB_CW_EVENT_MASK;
73 const quint32 value = XCB_EVENT_MASK_STRUCTURE_NOTIFY;
74 xcb_change_window_attributes(c: m_connection->xcb_connection(), window: m_trayWindow, value_mask: mask, value_list: &value);
75 }
76 }
77 return m_trayWindow;
78}
79
80inline void QXcbSystemTrayTracker::emitSystemTrayWindowChanged()
81{
82 if (const QPlatformScreen *ps = m_connection->primaryScreen())
83 emit systemTrayWindowChanged(screen: ps->screen());
84}
85
86// Client messages with the "MANAGER" atom on the root window indicate creation of a new tray.
87void QXcbSystemTrayTracker::notifyManagerClientMessageEvent(const xcb_client_message_event_t *t)
88{
89 if (t->data.data32[1] == m_selection)
90 emitSystemTrayWindowChanged();
91}
92
93// Listen for destruction of the tray.
94void QXcbSystemTrayTracker::handleDestroyNotifyEvent(const xcb_destroy_notify_event_t *event)
95{
96 if (event->window == m_trayWindow) {
97 m_connection->removeWindowEventListener(id: m_trayWindow);
98 m_trayWindow = XCB_WINDOW_NONE;
99 emitSystemTrayWindowChanged();
100 }
101}
102
103xcb_visualid_t QXcbSystemTrayTracker::visualId()
104{
105 xcb_visualid_t visual = netSystemTrayVisual();
106 if (visual == XCB_NONE)
107 visual = m_connection->primaryScreen()->screen()->root_visual;
108 return visual;
109}
110
111xcb_visualid_t QXcbSystemTrayTracker::netSystemTrayVisual()
112{
113 if (m_trayWindow == XCB_WINDOW_NONE)
114 return XCB_NONE;
115
116 xcb_atom_t tray_atom = m_connection->atom(qatom: QXcbAtom::Atom_NET_SYSTEM_TRAY_VISUAL);
117
118 // Get the xcb property for the _NET_SYSTEM_TRAY_VISUAL atom
119 auto systray_atom_reply = Q_XCB_REPLY_UNCHECKED(xcb_get_property, m_connection->xcb_connection(),
120 false, m_trayWindow,
121 tray_atom, XCB_ATOM_VISUALID, 0, 1);
122 if (!systray_atom_reply)
123 return XCB_NONE;
124
125 xcb_visualid_t systrayVisualId = XCB_NONE;
126 if (systray_atom_reply->value_len > 0 && xcb_get_property_value_length(R: systray_atom_reply.get()) > 0) {
127 xcb_visualid_t * vids = (uint32_t *)xcb_get_property_value(R: systray_atom_reply.get());
128 systrayVisualId = vids[0];
129 }
130
131 return systrayVisualId;
132}
133
134QT_END_NAMESPACE
135
136#include "moc_qxcbsystemtraytracker.cpp"
137

source code of qtbase/src/plugins/platforms/xcb/qxcbsystemtraytracker.cpp