Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qxcbsystemtraytracker.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
7#include "qxcbscreen.h"
8
9#include <QtCore/QDebug>
10#include <QtCore/QRect>
11#include <QtGui/QScreen>
12
13#include <qpa/qplatformnativeinterface.h>
14
16
17enum {
21};
22
23// QXcbSystemTrayTracker provides API for accessing the tray window and tracks
24// its lifecycle by listening for its destruction and recreation.
25// See http://standards.freedesktop.org/systemtray-spec/systemtray-spec-latest.html
26
27QXcbSystemTrayTracker *QXcbSystemTrayTracker::create(QXcbConnection *connection)
28{
29 // Selection, tray atoms for GNOME, NET WM Specification
30 const xcb_atom_t trayAtom = connection->atom(QXcbAtom::Atom_NET_SYSTEM_TRAY_OPCODE);
31 if (!trayAtom)
32 return nullptr;
33 const QByteArray netSysTray = QByteArrayLiteral("_NET_SYSTEM_TRAY_S") + QByteArray::number(connection->primaryScreenNumber());
34 const xcb_atom_t selection = connection->internAtom(netSysTray.constData());
35 if (!selection)
36 return nullptr;
37
38 return new QXcbSystemTrayTracker(connection, trayAtom, selection);
39}
40
41QXcbSystemTrayTracker::QXcbSystemTrayTracker(QXcbConnection *connection,
42 xcb_atom_t trayAtom,
43 xcb_atom_t selection)
44 : QObject(connection)
45 , m_selection(selection)
46 , m_trayAtom(trayAtom)
47 , m_connection(connection)
48{
49}
50
51// Request a window to be docked on the tray.
52void QXcbSystemTrayTracker::requestSystemTrayWindowDock(xcb_window_t window) const
53{
54 xcb_client_message_event_t trayRequest;
55 trayRequest.response_type = XCB_CLIENT_MESSAGE;
56 trayRequest.format = 32;
57 trayRequest.sequence = 0;
58 trayRequest.window = m_trayWindow;
59 trayRequest.type = m_trayAtom;
60 trayRequest.data.data32[0] = XCB_CURRENT_TIME;
61 trayRequest.data.data32[1] = SystemTrayRequestDock;
62 trayRequest.data.data32[2] = window;
63 xcb_send_event(m_connection->xcb_connection(), 0, m_trayWindow, XCB_EVENT_MASK_NO_EVENT, (const char *)&trayRequest);
64}
65
66// API for QPlatformNativeInterface/QPlatformSystemTrayIcon: Return tray window.
68{
69 if (!m_trayWindow) {
70 m_trayWindow = m_connection->selectionOwner(m_selection);
71 if (m_trayWindow) { // Listen for DestroyNotify on tray.
72 m_connection->addWindowEventListener(m_trayWindow, this);
73 const quint32 mask = XCB_CW_EVENT_MASK;
74 const quint32 value = XCB_EVENT_MASK_STRUCTURE_NOTIFY;
75 xcb_change_window_attributes(m_connection->xcb_connection(), m_trayWindow, mask, &value);
76 }
77 }
78 return m_trayWindow;
79}
80
81inline void QXcbSystemTrayTracker::emitSystemTrayWindowChanged()
82{
83 if (const QPlatformScreen *ps = m_connection->primaryScreen())
84 emit systemTrayWindowChanged(ps->screen());
85}
86
87// Client messages with the "MANAGER" atom on the root window indicate creation of a new tray.
88void QXcbSystemTrayTracker::notifyManagerClientMessageEvent(const xcb_client_message_event_t *t)
89{
90 if (t->data.data32[1] == m_selection)
91 emitSystemTrayWindowChanged();
92}
93
94// Listen for destruction of the tray.
95void QXcbSystemTrayTracker::handleDestroyNotifyEvent(const xcb_destroy_notify_event_t *event)
96{
97 if (event->window == m_trayWindow) {
98 m_connection->removeWindowEventListener(m_trayWindow);
99 m_trayWindow = XCB_WINDOW_NONE;
100 emitSystemTrayWindowChanged();
101 }
102}
103
105{
106 xcb_visualid_t visual = netSystemTrayVisual();
107 if (visual == XCB_NONE)
108 visual = m_connection->primaryScreen()->screen()->root_visual;
109 return visual;
110}
111
112xcb_visualid_t QXcbSystemTrayTracker::netSystemTrayVisual()
113{
114 if (m_trayWindow == XCB_WINDOW_NONE)
115 return XCB_NONE;
116
117 xcb_atom_t tray_atom = m_connection->atom(QXcbAtom::Atom_NET_SYSTEM_TRAY_VISUAL);
118
119 // Get the xcb property for the _NET_SYSTEM_TRAY_VISUAL atom
120 auto systray_atom_reply = Q_XCB_REPLY_UNCHECKED(xcb_get_property, m_connection->xcb_connection(),
121 false, m_trayWindow,
122 tray_atom, XCB_ATOM_VISUALID, 0, 1);
123 if (!systray_atom_reply)
124 return XCB_NONE;
125
126 xcb_visualid_t systrayVisualId = XCB_NONE;
127 if (systray_atom_reply->value_len > 0 && xcb_get_property_value_length(systray_atom_reply.get()) > 0) {
128 xcb_visualid_t * vids = (uint32_t *)xcb_get_property_value(systray_atom_reply.get());
129 systrayVisualId = vids[0];
130 }
131
132 return systrayVisualId;
133}
134
135QT_END_NAMESPACE
136
137#include "moc_qxcbsystemtraytracker.cpp"
@ Atom_NET_SYSTEM_TRAY_OPCODE
Definition qxcbatom.h:22
@ Atom_NET_SYSTEM_TRAY_VISUAL
Definition qxcbatom.h:128
void requestSystemTrayWindowDock(xcb_window_t window) const
void notifyManagerClientMessageEvent(const xcb_client_message_event_t *)
void handleDestroyNotifyEvent(const xcb_destroy_notify_event_t *) override
#define Q_XCB_REPLY_UNCHECKED(call,...)
@ SystemTrayBeginMessage
@ SystemTrayCancelMessage
@ SystemTrayRequestDock