Debian Codegraph @

examples/widgets/qnx/foreignwindows/collector.cpp

raw
1 /***************************************************************************
2 **
3 ** Copyright (C) 2018 QNX Software Systems. All rights reserved.
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 <QApplication>
41 #include <QDebug>
42 #include <QHBoxLayout>
43 #include <QWindow>
44
45 #include <algorithm>
46 #include <screen/screen.h>
47
48 #include "collector.h"
49
50 constexpr int MANAGER_EVENT_NAME_SUGGESTION = 9999;
51
52 Collector::Collector(QWidget *parent)
53 : QWidget(parent)
54 {
55 QApplication::instance()->installNativeEventFilter(this);
56
57 QLayout *layout = new QHBoxLayout(this);
58 setLayout(layout);
59 }
60
61 Collector::~Collector()
62 {
63 QApplication::instance()->removeNativeEventFilter(this);
64 }
65
66 bool Collector::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
67 {
68 Q_UNUSED(result);
69
70 if (eventType == QByteArrayLiteral("screen_event_t"))
71 return filterQnxScreenEvent(static_cast<screen_event_t>(message));
72
73 return false;
74 }
75
76 bool Collector::filterQnxScreenEvent(screen_event_t event)
77 {
78 int objectType = SCREEN_OBJECT_TYPE_CONTEXT;
79 screen_get_event_property_iv(event, SCREEN_PROPERTY_OBJECT_TYPE, &objectType);
80
81 if (objectType == SCREEN_OBJECT_TYPE_WINDOW)
82 return filterQnxScreenWindowEvent(event);
83
84 return false;
85 }
86
87 bool Collector::filterQnxScreenWindowEvent(screen_event_t event)
88 {
89 int eventType = SCREEN_EVENT_NONE;
90 screen_get_event_property_iv(event, SCREEN_PROPERTY_TYPE, &eventType);
91 screen_window_t window = nullptr;
92 screen_get_event_property_pv(event,
93 SCREEN_PROPERTY_WINDOW,
94 reinterpret_cast<void **>(&window));
95
96 if (eventType == SCREEN_EVENT_CREATE)
97 return filterQnxScreenWindowCreateEvent(window, event);
98 else if (eventType == SCREEN_EVENT_CLOSE)
99 return filterQnxScreenWindowCloseEvent(window, event);
100 else if (eventType == SCREEN_EVENT_MANAGER)
101 return filterQnxScreenWindowManagerEvent(window, event);
102
103 return false;
104 }
105
106 bool Collector::filterQnxScreenWindowCreateEvent(screen_window_t window, screen_event_t event)
107 {
108 Q_UNUSED(event);
109 WId winId = reinterpret_cast<WId>(window);
110
111 QByteArray parentGroup(256, 0);
112 screen_get_window_property_cv(window,
113 SCREEN_PROPERTY_PARENT,
114 parentGroup.length(),
115 parentGroup.data());
116 parentGroup.resize(strlen(parentGroup.constData()));
117
118 QByteArray group(256, 0);
119 screen_get_window_property_cv(reinterpret_cast<screen_window_t>(windowHandle()->winId()),
120 SCREEN_PROPERTY_GROUP,
121 group.length(),
122 group.data());
123 group.resize(strlen(group.constData()));
124
125 if (parentGroup != group)
126 return false;
127
128 Collectible collectible;
129 collectible.window = QWindow::fromWinId(winId);
130 collectible.widget = QWidget::createWindowContainer(collectible.window, this);
131 layout()->addWidget(collectible.widget);
132 m_collectibles.append(collectible);
133
134 return false;
135 }
136
137 bool Collector::filterQnxScreenWindowCloseEvent(screen_window_t window, screen_event_t event)
138 {
139 Q_UNUSED(event);
140 WId winId = reinterpret_cast<WId>(window);
141 auto it = std::find_if(m_collectibles.begin(), m_collectibles.end(),
142 [winId] (const Collectible &collectible) {
143 return collectible.window->winId() == winId;
144 });
145 if (it != m_collectibles.end()) {
146 delete it->widget;
147 // it->window is deleted by it->widget.
148 m_collectibles.erase(it);
149 }
150
151 return false;
152 }
153
154 bool Collector::filterQnxScreenWindowManagerEvent(screen_window_t window, screen_event_t event)
155 {
156 int managerEventType = 0;
157 screen_get_event_property_iv(event, SCREEN_PROPERTY_SUBTYPE, &managerEventType);
158
159 if (managerEventType == MANAGER_EVENT_NAME_SUGGESTION)
160 return filterQnxScreenWindowManagerNameEvent(window, event);
161
162 return false;
163 }
164
165 bool Collector::filterQnxScreenWindowManagerNameEvent(screen_window_t window, screen_event_t event)
166 {
167 Q_UNUSED(window);
168 int dataSize = 0;
169 screen_get_event_property_iv(event, SCREEN_PROPERTY_SIZE, &dataSize);
170 if (dataSize > 0) {
171 QByteArray data(dataSize, 0);
172 screen_get_event_property_cv(event, SCREEN_PROPERTY_USER_DATA, data.size(), data.data());
173 }
174
175 return false;
176 }