1 | /**************************************************************************** |
---|---|
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtWaylandCompositor module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
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 General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | |
31 | #include "qwaylandcompositorextension.h" |
32 | #include "qwaylandcompositorextension_p.h" |
33 | |
34 | #include <QtCore/QCoreApplication> |
35 | #include <QtCore/QDebug> |
36 | |
37 | #include <wayland-server-core.h> |
38 | |
39 | QT_BEGIN_NAMESPACE |
40 | |
41 | QWaylandCompositorExtension::QWaylandCompositorExtension() |
42 | : QWaylandObject(*new QWaylandCompositorExtensionPrivate()) |
43 | { |
44 | } |
45 | |
46 | QWaylandCompositorExtension::QWaylandCompositorExtension(QWaylandObject *container) |
47 | : QWaylandObject(*new QWaylandCompositorExtensionPrivate()) |
48 | { |
49 | d_func()->extension_container = container; |
50 | QCoreApplication::postEvent(receiver: this, event: new QEvent(QEvent::Polish)); |
51 | } |
52 | |
53 | QWaylandCompositorExtension::QWaylandCompositorExtension(QWaylandCompositorExtensionPrivate &dd) |
54 | : QWaylandObject(dd) |
55 | { |
56 | } |
57 | |
58 | QWaylandCompositorExtension::QWaylandCompositorExtension(QWaylandObject *container, QWaylandCompositorExtensionPrivate &dd) |
59 | : QWaylandObject(dd) |
60 | { |
61 | d_func()->extension_container = container; |
62 | QCoreApplication::postEvent(receiver: this, event: new QEvent(QEvent::Polish)); |
63 | } |
64 | |
65 | QWaylandCompositorExtension::~QWaylandCompositorExtension() |
66 | { |
67 | Q_D(QWaylandCompositorExtension); |
68 | if (d->extension_container) |
69 | d->extension_container->removeExtension(extension: this); |
70 | } |
71 | |
72 | QWaylandObject *QWaylandCompositorExtension::extensionContainer() const |
73 | { |
74 | Q_D(const QWaylandCompositorExtension); |
75 | return d->extension_container; |
76 | } |
77 | |
78 | void QWaylandCompositorExtension::setExtensionContainer(QWaylandObject *container) |
79 | { |
80 | Q_D(QWaylandCompositorExtension); |
81 | d->extension_container = container; |
82 | } |
83 | |
84 | void QWaylandCompositorExtension::initialize() |
85 | { |
86 | Q_D(QWaylandCompositorExtension); |
87 | if (d->initialized) { |
88 | qWarning() << "QWaylandCompositorExtension:"<< extensionInterface()->name << "is already initialized"; |
89 | return; |
90 | } |
91 | |
92 | if (!d->extension_container && parent()) { |
93 | QWaylandObject *parentObj = qobject_cast<QWaylandObject*>(object: parent()); |
94 | if (parentObj) |
95 | setExtensionContainer(parentObj); |
96 | } |
97 | |
98 | if (!d->extension_container) { |
99 | qWarning() << "QWaylandCompositorExtension:"<< extensionInterface()->name << "requests to initialize with no extension container set"; |
100 | return; |
101 | } |
102 | |
103 | d->extension_container->addExtension(extension: this); |
104 | d->initialized = true; |
105 | } |
106 | |
107 | bool QWaylandCompositorExtension::isInitialized() const |
108 | { |
109 | Q_D(const QWaylandCompositorExtension); |
110 | return d->initialized; |
111 | } |
112 | |
113 | bool QWaylandCompositorExtension::event(QEvent *event) |
114 | { |
115 | switch(event->type()) { |
116 | case QEvent::Polish: |
117 | if (!isInitialized()) |
118 | initialize(); |
119 | break; |
120 | default: |
121 | break; |
122 | } |
123 | return QWaylandObject::event(event); |
124 | } |
125 | |
126 | QWaylandObject::QWaylandObject(QObject *parent) |
127 | :QObject(parent) |
128 | { |
129 | } |
130 | |
131 | QWaylandObject::QWaylandObject(QObjectPrivate &d, QObject *parent) |
132 | :QObject(d, parent) |
133 | { |
134 | } |
135 | |
136 | |
137 | QWaylandObject::~QWaylandObject() |
138 | { |
139 | for (QWaylandCompositorExtension *extension : qAsConst(t&: extension_vector)) |
140 | QWaylandCompositorExtensionPrivate::get(extension)->extension_container = nullptr; |
141 | } |
142 | |
143 | QWaylandCompositorExtension *QWaylandObject::extension(const QByteArray &name) |
144 | { |
145 | for (int i = 0; i < extension_vector.size(); i++) { |
146 | if (extension_vector.at(i)->extensionInterface()->name == name) |
147 | return extension_vector.at(i); |
148 | } |
149 | return nullptr; |
150 | } |
151 | |
152 | QWaylandCompositorExtension *QWaylandObject::extension(const wl_interface *interface) |
153 | { |
154 | for (int i = 0; i < extension_vector.size(); i++) { |
155 | if (extension_vector.at(i)->extensionInterface() == interface) |
156 | return extension_vector.at(i); |
157 | } |
158 | return nullptr; |
159 | } |
160 | |
161 | QList<QWaylandCompositorExtension *> QWaylandObject::extensions() const |
162 | { |
163 | return extension_vector; |
164 | } |
165 | |
166 | void QWaylandObject::addExtension(QWaylandCompositorExtension *extension) |
167 | { |
168 | Q_ASSERT(!extension_vector.contains(extension)); |
169 | extension_vector.append(t: extension); |
170 | } |
171 | |
172 | void QWaylandObject::removeExtension(QWaylandCompositorExtension *extension) |
173 | { |
174 | Q_ASSERT(extension_vector.contains(extension)); |
175 | extension_vector.removeOne(t: extension); |
176 | } |
177 | |
178 | QT_END_NAMESPACE |
179 |