on 08cbe5e help sign in
234 lines 5.5 kB history raw
1/*
2 * Copyright (C) 2016 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 */
17#include "Window.h"
19// unity-api
20#include <unity/shell/application/MirSurfaceInterface.h>
22#include <QQmlEngine>
24namespace unityapi = unity::shell::application;
26Q_LOGGING_CATEGORY(UNITY_WINDOW, "unity.window", QtWarningMsg)
28#define DEBUG_MSG qCDebug(UNITY_WINDOW).nospace() << qPrintable(toString()) << "::" << __func__
30Window::Window(int id, QObject *parent)
31 : QObject(parent)
32 , m_id(id)
34 DEBUG_MSG << "()";
35 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
38Window::~Window()
40 DEBUG_MSG << "()";
43QPoint Window::position() const
45 return m_position;
48QPoint Window::requestedPosition() const
50 return m_requestedPosition;
53void Window::setRequestedPosition(const QPoint &value)
55 m_positionRequested = true;
56 if (value != m_requestedPosition) {
57 m_requestedPosition = value;
58 Q_EMIT requestedPositionChanged(m_requestedPosition);
59 if (m_surface) {
60 m_surface->setRequestedPosition(value);
61 } else {
62 // fake-miral: always comply
63 m_position = m_requestedPosition;
64 Q_EMIT positionChanged(m_position);
65 }
66 }
69Mir::State Window::state() const
71 return m_state;
74bool Window::focused() const
76 return m_focused;
79bool Window::confinesMousePointer() const
81 if (m_surface) {
82 return m_surface->confinesMousePointer();
83 } else {
84 return false;
85 }
88int Window::id() const
90 return m_id;
93unityapi::MirSurfaceInterface* Window::surface() const
95 return m_surface;
98void Window::requestState(Mir::State state)
100 m_stateRequested = true;
101 if (m_surface) {
102 m_surface->requestState(state);
103 } else if (m_state != state) {
104 m_state = state;
105 Q_EMIT stateChanged(m_state);
106 }
109void Window::close()
111 if (m_surface) {
112 m_surface->close();
113 } else {
114 Q_EMIT closeRequested();
115 }
118void Window::activate()
120 if (m_surface) {
121 m_surface->activate();
122 } else {
123 Q_EMIT emptyWindowActivated();
124 }
127void Window::setSurface(unityapi::MirSurfaceInterface *surface)
129 DEBUG_MSG << "(" << surface << ")";
130 if (m_surface) {
131 disconnect(m_surface, 0, this, 0);
132 }
134 m_surface = surface;
136 if (m_surface) {
137 connect(surface, &unityapi::MirSurfaceInterface::focusRequested, this, [this]() {
138 Q_EMIT focusRequested();
139 });
141 connect(surface, &unityapi::MirSurfaceInterface::closeRequested, this, &Window::closeRequested);
143 connect(surface, &unityapi::MirSurfaceInterface::positionChanged, this, [this]() {
144 updatePosition();
145 });
147 connect(surface, &unityapi::MirSurfaceInterface::stateChanged, this, [this]() {
148 updateState();
149 });
151 connect(surface, &unityapi::MirSurfaceInterface::focusedChanged, this, [this]() {
152 updateFocused();
153 });
155 // bring it up to speed
156 if (m_positionRequested) {
157 m_surface->setRequestedPosition(m_requestedPosition);
158 }
159 if (m_stateRequested) {
160 m_surface->requestState(m_state);
161 }
163 // and sync with surface
164 updatePosition();
165 updateState();
166 updateFocused();
167 }
169 Q_EMIT surfaceChanged(surface);
172void Window::updatePosition()
174 if (m_surface->position() != m_position) {
175 m_position = m_surface->position();
176 Q_EMIT positionChanged(m_position);
177 }
180void Window::updateState()
182 if (m_surface->state() != m_state) {
183 m_state = m_surface->state();
184 Q_EMIT stateChanged(m_state);
185 }
188void Window::updateFocused()
190 if (m_surface->focused() != m_focused) {
191 m_focused = m_surface->focused();
192 Q_EMIT focusedChanged(m_focused);
193 }
196void Window::setFocused(bool value)
198 if (value != m_focused) {
199 DEBUG_MSG << "(" << value << ")";
200 m_focused = value;
201 Q_EMIT focusedChanged(m_focused);
202 // when we have a surface we get focus changes from updateFocused() instead
203 Q_ASSERT(!m_surface);
204 }
207QString Window::toString() const
209 if (surface()) {
210 return QString("Window[0x%1, id=%2, MirSurface[0x%3,\"%4\"]]").arg(
211 QString::number((quintptr)this, 16),
212 QString::number(id()),
213 QString::number((quintptr)surface(), 16),
214 surface()->name());
215 } else {
216 return QString("Window[0x%1, id=%2, null]").arg(
217 QString::number((quintptr)this, 0, 16),
218 QString::number(id()));
219 }
222QDebug operator<<(QDebug dbg, const Window *window)
224 QDebugStateSaver saver(dbg);
225 dbg.nospace();
227 if (window) {
228 dbg << qPrintable(window->toString());
229 } else {
230 dbg << (void*)(window);
231 }
233 return dbg;