Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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
15
16enum {
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
27{
28 // Selection, tray atoms for GNOME, NET WM Specification
29 const xcb_atom_t trayAtom = connection->atom(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(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)
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.
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(m_connection->xcb_connection(), 0, m_trayWindow, XCB_EVENT_MASK_NO_EVENT, (const char *)&trayRequest);
63}
64
65// API for QPlatformNativeInterface/QPlatformSystemTrayIcon: Return tray window.
67{
68 if (!m_trayWindow) {
69 m_trayWindow = m_connection->selectionOwner(m_selection);
70 if (m_trayWindow) { // Listen for DestroyNotify on tray.
71 m_connection->addWindowEventListener(m_trayWindow, this);
72 const quint32 mask = XCB_CW_EVENT_MASK;
73 const quint32 value = XCB_EVENT_MASK_STRUCTURE_NOTIFY;
74 xcb_change_window_attributes(m_connection->xcb_connection(), m_trayWindow, mask, &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(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(m_trayWindow);
98 m_trayWindow = XCB_WINDOW_NONE;
99 emitSystemTrayWindowChanged();
100 }
101}
102
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(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(systray_atom_reply.get()) > 0) {
127 xcb_visualid_t * vids = (uint32_t *)xcb_get_property_value(systray_atom_reply.get());
128 systrayVisualId = vids[0];
129 }
130
131 return systrayVisualId;
132}
133
135
136#include "moc_qxcbsystemtraytracker.cpp"
\inmodule QtCore
Definition qbytearray.h:57
static QByteArray number(int, int base=10)
Returns a byte-array representing the whole number n as text.
\inmodule QtCore
Definition qobject.h:103
The QPlatformScreen class provides an abstraction for visual displays.
@ Atom_NET_SYSTEM_TRAY_OPCODE
Definition qxcbatom.h:21
@ Atom_NET_SYSTEM_TRAY_VISUAL
Definition qxcbatom.h:127
xcb_connection_t * xcb_connection() const
xcb_atom_t atom(QXcbAtom::Atom qatom) const
xcb_window_t selectionOwner(xcb_atom_t atom) const
QXcbScreen * primaryScreen() const
void addWindowEventListener(xcb_window_t id, QXcbWindowEventListener *eventListener)
void removeWindowEventListener(xcb_window_t id)
xcb_screen_t * screen() const
Definition qxcbscreen.h:152
void systemTrayWindowChanged(QScreen *screen)
static QXcbSystemTrayTracker * create(QXcbConnection *connection)
void requestSystemTrayWindowDock(xcb_window_t window) const
void notifyManagerClientMessageEvent(const xcb_client_message_event_t *)
void handleDestroyNotifyEvent(const xcb_destroy_notify_event_t *) override
Combined button and popup list for selecting options.
#define QByteArrayLiteral(str)
Definition qbytearray.h:52
DBusConnection * connection
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLint GLint GLint GLint GLint GLint GLint GLbitfield mask
struct _cl_event * event
GLdouble GLdouble t
Definition qopenglext.h:243
#define emit
unsigned int quint32
Definition qtypes.h:50
#define Q_XCB_REPLY_UNCHECKED(call,...)
@ SystemTrayRequestDock
@ SystemTrayCancelMessage
@ SystemTrayBeginMessage
QItemSelection * selection
[0]
aWidget window() -> setWindowTitle("New Window Title")
[2]