Unity 8
Window.cpp
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  */
16 
17 #include "Window.h"
18 
19 // unity-api
20 #include <unity/shell/application/MirSurfaceInterface.h>
21 
22 #include <QQmlEngine>
23 #include <QTextStream>
24 
26 
27 Q_LOGGING_CATEGORY(UNITY_WINDOW, "unity.window", QtWarningMsg)
28 
29 #define DEBUG_MSG qCDebug(UNITY_WINDOW).nospace() << qPrintable(toString()) << "::" << __func__
30 
31 Window::Window(int id, QObject *parent)
32  : QObject(parent)
33  , m_id(id)
34 {
35  DEBUG_MSG << "()";
36  QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
37 }
38 
39 Window::~Window()
40 {
41  DEBUG_MSG << "()";
42 }
43 
44 QPoint Window::position() const
45 {
46  return m_position;
47 }
48 
49 QPoint Window::requestedPosition() const
50 {
51  return m_requestedPosition;
52 }
53 
54 void Window::setRequestedPosition(const QPoint &value)
55 {
56  m_positionRequested = true;
57  if (value != m_requestedPosition) {
58  m_requestedPosition = value;
59  Q_EMIT requestedPositionChanged(m_requestedPosition);
60  if (m_surface) {
61  m_surface->setRequestedPosition(value);
62  } else {
63  // fake-miral: always comply
64  m_position = m_requestedPosition;
65  Q_EMIT positionChanged(m_position);
66  }
67  }
68 }
69 
70 Mir::State Window::state() const
71 {
72  return m_state;
73 }
74 
75 bool Window::focused() const
76 {
77  return m_focused;
78 }
79 
81 {
82  if (m_surface) {
83  return m_surface->confinesMousePointer();
84  } else {
85  return false;
86  }
87 }
88 
89 int Window::id() const
90 {
91  return m_id;
92 }
93 
94 unityapi::MirSurfaceInterface* Window::surface() const
95 {
96  return m_surface;
97 }
98 
99 void Window::requestState(Mir::State state)
100 {
101  m_stateRequested = true;
102  if (m_surface) {
103  m_surface->requestState(state);
104  } else if (m_state != state) {
105  m_state = state;
106  Q_EMIT stateChanged(m_state);
107  }
108 }
109 
111 {
112  if (m_surface) {
113  m_surface->close();
114  } else {
115  Q_EMIT closeRequested();
116  }
117 }
118 
120 {
121  DEBUG_MSG << "()";
122  if (m_surface) {
123  m_surface->activate();
124  } else {
125  Q_EMIT emptyWindowActivated();
126  }
127 }
128 
129 void Window::setSurface(unityapi::MirSurfaceInterface *surface)
130 {
131  DEBUG_MSG << "(" << surface << ")";
132  if (m_surface) {
133  disconnect(m_surface, 0, this, 0);
134  }
135 
136  m_surface = surface;
137 
138  if (m_surface) {
139  connect(surface, &unityapi::MirSurfaceInterface::focusRequested, this, [this]() {
140  Q_EMIT focusRequested();
141  });
142 
143  connect(surface, &unityapi::MirSurfaceInterface::closeRequested, this, &Window::closeRequested);
144 
145  connect(surface, &unityapi::MirSurfaceInterface::positionChanged, this, [this]() {
146  updatePosition();
147  });
148 
149  connect(surface, &unityapi::MirSurfaceInterface::stateChanged, this, [this]() {
150  updateState();
151  });
152 
153  connect(surface, &unityapi::MirSurfaceInterface::focusedChanged, this, [this]() {
154  updateFocused();
155  });
156 
157  // bring it up to speed
158  if (m_positionRequested) {
159  m_surface->setRequestedPosition(m_requestedPosition);
160  }
161  if (m_stateRequested && m_surface->state() == Mir::RestoredState) {
162  m_surface->requestState(m_state);
163  }
164 
165  // and sync with surface
166  updatePosition();
167  updateState();
168  updateFocused();
169  }
170 
171  Q_EMIT surfaceChanged(surface);
172 }
173 
174 void Window::updatePosition()
175 {
176  if (m_surface->position() != m_position) {
177  m_position = m_surface->position();
178  Q_EMIT positionChanged(m_position);
179  }
180 }
181 
182 void Window::updateState()
183 {
184  if (m_surface->state() != m_state) {
185  m_state = m_surface->state();
186  Q_EMIT stateChanged(m_state);
187  }
188 }
189 
190 void Window::updateFocused()
191 {
192  if (m_surface->focused() != m_focused) {
193  m_focused = m_surface->focused();
194  Q_EMIT focusedChanged(m_focused);
195  }
196 }
197 
198 void Window::setFocused(bool value)
199 {
200  if (value != m_focused) {
201  DEBUG_MSG << "(" << value << ")";
202  m_focused = value;
203  Q_EMIT focusedChanged(m_focused);
204  // when we have a surface we get focus changes from updateFocused() instead
205  Q_ASSERT(!m_surface);
206  }
207 }
208 
209 QString Window::toString() const
210 {
211  QString result;
212  {
213  QTextStream stream(&result);
214  stream << "Window["<<(void*)this<<", id="<<id()<<", ";
215  if (surface()) {
216  stream << "MirSurface["<<(void*)surface()<<",\""<<surface()->name()<<"\"]";
217  } else {
218  stream << "null";
219  }
220  stream << "]";
221  }
222  return result;
223 }
224 
225 QDebug operator<<(QDebug dbg, const Window *window)
226 {
227  QDebugStateSaver saver(dbg);
228  dbg.nospace();
229 
230  if (window) {
231  dbg << qPrintable(window->toString());
232  } else {
233  dbg << (void*)(window);
234  }
235 
236  return dbg;
237 }
A slightly higher concept than MirSurface.
Definition: Window.h:46
Mir::State state
State of the surface.
Definition: Window.h:63
void activate()
Focuses and raises the window.
Definition: Window.cpp:119
unity::shell::application::MirSurfaceInterface surface
Surface backing up this window It might be null if a surface hasn&#39;t been created yet (application is ...
Definition: Window.h:91
bool focused
Whether the surface is focused.
Definition: Window.h:70
void requestState(Mir::State state)
Requests a change to the specified state.
Definition: Window.cpp:99
QPoint position
Position of the current surface buffer, in pixels.
Definition: Window.h:53
int id
A unique identifier for this window. Useful for telling windows apart in a list model as they get mov...
Definition: Window.h:83
QPoint requestedPosition
Requested position of the current surface buffer, in pixels.
Definition: Window.h:58
void close()
Sends a close request.
Definition: Window.cpp:110
bool confinesMousePointer
Whether the surface wants to confine the mouse pointer within its boundaries.
Definition: Window.h:77