Lomiri
Loading...
Searching...
No Matches
Window.cpp
1/*
2 * Copyright (C) 2016-2017 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "Window.h"
18
19// lomiri-api
20#include <lomiri/shell/application/MirSurfaceInterface.h>
21
22#include <QQmlEngine>
23#include <QTextStream>
24
25namespace lomiriapi = lomiri::shell::application;
26
27Q_LOGGING_CATEGORY(LOMIRI_WINDOW, "lomiri.window", QtWarningMsg)
28
29#define DEBUG_MSG qCDebug(LOMIRI_WINDOW).nospace() << qPrintable(toString()) << "::" << __func__
30#define WARNING_MSG qCWarning(LOMIRI_WINDOW).nospace() << qPrintable(toString()) << "::" << __func__
31
32Window::Window(int id, QObject *parent)
33 : QObject(parent)
34 , m_id(id)
35{
36 DEBUG_MSG << "()";
37 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
38}
39
40Window::~Window()
41{
42 DEBUG_MSG << "()";
43}
44
45QPoint Window::position() const
46{
47 return m_position;
48}
49
50QPoint Window::requestedPosition() const
51{
52 return m_requestedPosition;
53}
54
55void Window::setRequestedPosition(const QPoint &value)
56{
57 m_positionRequested = true;
58 if (value != m_requestedPosition) {
59 m_requestedPosition = value;
60 Q_EMIT requestedPositionChanged(m_requestedPosition);
61 if (m_surface) {
62 m_surface->setRequestedPosition(value);
63 } else {
64 // fake-miral: always comply
65 m_position = m_requestedPosition;
66 Q_EMIT positionChanged(m_position);
67 }
68 }
69}
70
72{
73 return m_allowClientResize;
74}
75
76void Window::setAllowClientResize(bool value)
77{
78 if (value != m_allowClientResize) {
79 DEBUG_MSG << "("<<value<<")";
80 m_allowClientResize = value;
81 if (m_surface) {
82 m_surface->setAllowClientResize(value);
83 }
84 Q_EMIT allowClientResizeChanged(m_allowClientResize);
85 }
86}
87
88Mir::State Window::state() const
89{
90 return m_state;
91}
92
93bool Window::focused() const
94{
95 return m_focused;
96}
97
99{
100 if (m_surface) {
101 return m_surface->confinesMousePointer();
102 } else {
103 return false;
104 }
105}
106
107bool Window::constrained() const
108{
109 return m_constrained;
110}
111
112int Window::id() const
113{
114 return m_id;
115}
116
117lomiriapi::MirSurfaceInterface* Window::surface() const
118{
119 return m_surface;
120}
121
123{
124 m_stateRequested = true;
125 if (m_surface) {
126 m_surface->requestState(state);
127 } else if (m_state != state) {
128 m_state = state;
129 Q_EMIT stateChanged(m_state);
130 }
131}
132
134{
135 if (m_surface) {
136 m_surface->close();
137 } else {
138 Q_EMIT closeRequested();
139 }
140}
141
143{
144 DEBUG_MSG << "()";
145 if (m_surface) {
146 m_surface->activate();
147 } else {
148 Q_EMIT emptyWindowActivated();
149 }
150}
151
152void Window::setSurface(lomiriapi::MirSurfaceInterface *surface)
153{
154 DEBUG_MSG << "(" << surface << ")";
155 if (m_surface) {
156 disconnect(m_surface, 0, this, 0);
157 }
158
159 m_surface = surface;
160
161 if (m_surface) {
162 connect(surface, &lomiriapi::MirSurfaceInterface::focusRequested, this, [this]() {
163 Q_EMIT focusRequested();
164 });
165
166 connect(surface, &lomiriapi::MirSurfaceInterface::closeRequested, this, &Window::closeRequested);
167
168 connect(surface, &lomiriapi::MirSurfaceInterface::positionChanged, this, [this]() {
169 updatePosition();
170 });
171
172 connect(surface, &lomiriapi::MirSurfaceInterface::stateChanged, this, [this]() {
173 updateState();
174 });
175
176 connect(surface, &lomiriapi::MirSurfaceInterface::focusedChanged, this, [this]() {
177 updateFocused();
178 });
179
180 connect(surface, &lomiriapi::MirSurfaceInterface::allowClientResizeChanged, this, [this]() {
181 if (m_surface->allowClientResize() != m_allowClientResize) {
182 m_allowClientResize = m_surface->allowClientResize();
183 Q_EMIT allowClientResizeChanged(m_allowClientResize);
184 }
185 });
186
187 connect(surface, &lomiriapi::MirSurfaceInterface::liveChanged, this, &Window::liveChanged);
188
189 connect(surface, &QObject::destroyed, this, [this]() {
190 setSurface(nullptr);
191 });
192
193 // Surface should never be focused at this point!
194 if (m_surface->focused()) {
195 WARNING_MSG << "Initial surface is focused!";
196 }
197
198 // bring it up to speed
199 if (m_focused) {
200 m_surface->activate();
201 }
202 if (m_positionRequested) {
203 m_surface->setRequestedPosition(m_requestedPosition);
204 }
205 if (m_stateRequested && m_surface->state() == Mir::RestoredState) {
206 m_surface->requestState(m_state);
207 }
208 m_surface->setAllowClientResize(m_allowClientResize);
209
210 // and sync with surface
211 updatePosition();
212 updateState();
213 updateFocused();
214 }
215
216 Q_EMIT surfaceChanged(surface);
217}
218
219void Window::updatePosition()
220{
221 if (m_surface->position() != m_position) {
222 m_position = m_surface->position();
223 Q_EMIT positionChanged(m_position);
224 }
225}
226
227void Window::updateState()
228{
229 if (m_surface->state() != m_state) {
230 m_state = m_surface->state();
231 Q_EMIT stateChanged(m_state);
232 }
233}
234
235void Window::updateFocused()
236{
237 if (m_surface->focused() != m_focused) {
238 m_focused = m_surface->focused();
239 Q_EMIT focusedChanged(m_focused);
240 }
241}
242
243void Window::setFocused(bool value)
244{
245 if (value != m_focused) {
246 DEBUG_MSG << "(" << value << ")";
247 m_focused = value;
248 Q_EMIT focusedChanged(m_focused);
249 // when we have a surface we get focus changes from updateFocused() instead
250 Q_ASSERT(!m_surface);
251 }
252}
253
254void Window::setConstrained(bool value)
255{
256 if (value != m_constrained) {
257 DEBUG_MSG << "(" << value << ")";
258 m_constrained = value;
259 Q_EMIT constrainedChanged(m_constrained);
260 }
261}
262
263QString Window::toString() const
264{
265 QString result;
266 {
267 QTextStream stream(&result);
268 stream << "Window["<<(void*)this<<", id="<<id()<<", ";
269 if (surface()) {
270 stream << "MirSurface["<<(void*)surface()<<",\""<<surface()->name()<<"\"]";
271 } else {
272 stream << "null";
273 }
274 stream << "]";
275 }
276 return result;
277}
278
279QDebug operator<<(QDebug dbg, const Window *window)
280{
281 QDebugStateSaver saver(dbg);
282 dbg.nospace();
283
284 if (window) {
285 dbg << qPrintable(window->toString());
286 } else {
287 dbg << (void*)(window);
288 }
289
290 return dbg;
291}
A slightly higher concept than MirSurface.
Definition Window.h:48
bool allowClientResize
Whether to comply to resize requests coming from the client side.
Definition Window.h:106
void requestState(Mir::State state)
Requests a change to the specified state.
Definition Window.cpp:122
int id
A unique identifier for this window. Useful for telling windows apart in a list model as they get mov...
Definition Window.h:91
void focusRequested()
Emitted when focus for this window is requested by an external party.
void close()
Sends a close request.
Definition Window.cpp:133
lomiri::shell::application::MirSurfaceInterface * surface
Surface backing up this window It might be null if a surface hasn't been created yet (application is ...
Definition Window.h:99
QPoint requestedPosition
Requested position of the current surface buffer, in pixels.
Definition Window.h:59
bool confinesMousePointer
Whether the surface wants to confine the mouse pointer within its boundaries.
Definition Window.h:78
QPoint position
Position of the current surface buffer, in pixels.
Definition Window.h:54
bool focused
Whether the surface is focused.
Definition Window.h:71
bool constrained
Whether the surface has been constrained in screen bounds already.
Definition Window.h:85
void activate()
Focuses and raises the window.
Definition Window.cpp:142
Mir::State state
State of the surface.
Definition Window.h:64