on 16fa310 help sign in
507 lines 11 kB history raw
1/*
2 * Copyright (C) 2015-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 "MirSurface.h"
19#include <QDebug>
20#include <QQmlEngine>
22// local
23#include "SurfaceManager.h"
25#define MIRSURFACE_DEBUG 0
27#if MIRSURFACE_DEBUG
28#define DEBUG_MSG(params) qDebug().nospace() << "MirSurface[" << (void*)this << "," << m_name << "]::" << __func__ << " " << params
29#define XDEBUG_MSG(params) qDebug().nospace() << "MirSurface[" << (void*)this << "," << m_name << "]::" << params
31const char *stateToStr(Mir::State state)
33 switch (state) {
34 case Mir::UnknownState:
35 return "unknown";
36 case Mir::RestoredState:
37 return "restored";
38 case Mir::MinimizedState:
39 return "minimized";
40 case Mir::MaximizedState:
41 return "maximized";
42 case Mir::VertMaximizedState:
43 return "vertMaximized";
44 case Mir::FullscreenState:
45 return "fullscreen";
46 case Mir::HorizMaximizedState:
47 return "horizMaximized";
48 case Mir::MaximizedLeftState:
49 return "maximizedLeft";
50 case Mir::MaximizedRightState:
51 return "maximizedRight";
52 case Mir::MaximizedTopLeftState:
53 return "maximizedTopLeft";
54 case Mir::MaximizedTopRightState:
55 return "maximizedTopRight";
56 case Mir::MaximizedBottomLeftState:
57 return "maximizedBottomLeft";
58 case Mir::MaximizedBottomRightState:
59 return "maximizedBottomRight";
60 case Mir::HiddenState:
61 return "hidden";
62 default:
63 return "???";
64 }
67#else
68#define DEBUG_MSG(params) ((void)0)
69#define XDEBUG_MSG(params) ((void)0)
70#endif
72using namespace unity::shell::application;
74MirSurface::MirSurface(const QString& name,
75 Mir::Type type,
76 Mir::State state,
77 const QUrl& screenshot,
78 const QUrl &qmlFilePath)
79 : unity::shell::application::MirSurfaceInterface(nullptr)
80 , m_name(name)
81 , m_type(type)
82 , m_state(state)
83 , m_orientationAngle(Mir::Angle0)
84 , m_screenshotUrl(screenshot)
85 , m_qmlFilePath(qmlFilePath)
86 , m_live(true)
87 , m_focused(false)
88 , m_activeFocus(false)
89 , m_width(-1)
90 , m_height(-1)
91 , m_slowToResize(false)
92 , m_shellChrome(Mir::NormalChrome)
94 DEBUG_MSG("state=" << stateToStr(state));
96 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
98 m_delayedResizeTimer.setInterval(600);
99 m_delayedResizeTimer.setSingleShot(true);
100 connect(&m_delayedResizeTimer, &QTimer::timeout, this, &MirSurface::applyDelayedResize);
102 m_zombieTimer.setInterval(100);
103 m_zombieTimer.setSingleShot(true);
104 connect(&m_zombieTimer, &QTimer::timeout, this, [this](){ this->setLive(false); });
106 updateInputBoundsAfterResize();
109MirSurface::~MirSurface()
111 DEBUG_MSG("");
113 // Early warning, while MirSurface methods can still be accessed.
114 Q_EMIT destroyed(this);
117QString MirSurface::name() const
119 return m_name;
122QString MirSurface::persistentId() const
124 return m_name+"Id";
127Mir::Type MirSurface::type() const
129 return m_type;
132Mir::State MirSurface::state() const
134 return m_state;
137void MirSurface::requestState(Mir::State state)
139 if (state == m_state) {
140 return;
141 }
142 DEBUG_MSG(stateToStr(state));
143 Q_EMIT stateRequested(state);
146void MirSurface::setState(Mir::State state)
148 if (state == m_state) {
149 return;
150 }
151 DEBUG_MSG(stateToStr(state));
153 bool oldVisible = visible();
155 m_state = state;
156 Q_EMIT stateChanged(state);
158 if (visible() != oldVisible) {
159 XDEBUG_MSG("visibleChanged("<<visible()<<")");
160 Q_EMIT visibleChanged(visible());
161 }
164bool MirSurface::live() const
166 return m_live;
169bool MirSurface::visible() const
171 return m_state != Mir::MinimizedState && m_state != Mir::HiddenState;
174void MirSurface::setLive(bool live)
176 if (live == m_live)
177 return;
179 DEBUG_MSG(live);
180 m_live = live;
181 Q_EMIT liveChanged(live);
183 if (!m_live && m_views.count() == 0) {
184 deleteLater();
185 }
188QUrl MirSurface::qmlFilePath() const
190 return m_qmlFilePath;
193QUrl MirSurface::screenshotUrl() const
195 return m_screenshotUrl;
198void MirSurface::setScreenshotUrl(QUrl screenshotUrl)
200 if (screenshotUrl == m_screenshotUrl)
201 return;
203 m_screenshotUrl = screenshotUrl;
204 Q_EMIT screenshotUrlChanged(screenshotUrl);
207Mir::OrientationAngle MirSurface::orientationAngle() const
209 return m_orientationAngle;
212void MirSurface::setOrientationAngle(Mir::OrientationAngle angle)
214 if (angle == m_orientationAngle)
215 return;
217 m_orientationAngle = angle;
218 Q_EMIT orientationAngleChanged(angle);
221void MirSurface::setKeymap(const QString &value)
223 if (value != m_keymap) {
224 DEBUG_MSG(value);
225 m_keymap = value;
226 Q_EMIT keymapChanged(m_keymap);
227 }
230QString MirSurface::keymap() const
232 return m_keymap;
235Mir::ShellChrome MirSurface::shellChrome() const
237 return m_shellChrome;
240void MirSurface::setShellChrome(Mir::ShellChrome shellChrome)
242 if (shellChrome == m_shellChrome)
243 return;
245 DEBUG_MSG(shellChrome);
246 m_shellChrome = shellChrome;
247 Q_EMIT shellChromeChanged(shellChrome);
250void MirSurface::registerView(qintptr viewId)
252 m_views.insert(viewId, MirSurface::View{false});
253 DEBUG_MSG(viewId << " after=" << m_views.count());
256void MirSurface::unregisterView(qintptr viewId)
258 m_views.remove(viewId);
259 DEBUG_MSG(viewId << " after=" << m_views.count() << " live=" << m_live);
260 if (!m_live && m_views.count() == 0) {
261 deleteLater();
262 }
263 updateExposure();
266void MirSurface::setViewExposure(qintptr viewId, bool visible)
268 if (!m_views.contains(viewId)) return;
270 m_views[viewId].visible = visible;
271 updateExposure();
274void MirSurface::updateExposure()
276 bool newExposure = false;
277 QHashIterator<qintptr, View> i(m_views);
278 while (i.hasNext()) {
279 i.next();
280 newExposure |= i.value().visible;
281 }
283 if (newExposure != m_exposed) {
284 m_exposed = newExposure;
285 DEBUG_MSG(m_exposed);
286 Q_EMIT exposedChanged(m_exposed);
287 updateInputBoundsAfterResize();
288 }
291bool MirSurface::activeFocus() const
293 return m_activeFocus;
296void MirSurface::setActiveFocus(bool value)
298 if (m_activeFocus == value)
299 return;
301 m_activeFocus = value;
303 Q_EMIT activeFocusChanged(value);
305 if (m_activeFocus && !m_focused) {
306 requestFocus();
307 }
310int MirSurface::width() const
312 return m_width;
315int MirSurface::height() const
317 return m_height;
320void MirSurface::resize(int width, int height)
322 if (m_slowToResize) {
323 if (!m_delayedResizeTimer.isActive()) {
324 m_delayedResize.setWidth(width);
325 m_delayedResize.setHeight(height);
326 m_delayedResizeTimer.start();
327 } else {
328 m_pendingResize.setWidth(width);
329 m_pendingResize.setHeight(height);
330 }
331 } else {
332 doResize(width, height);
333 }
336void MirSurface::applyDelayedResize()
338 doResize(m_delayedResize.width(), m_delayedResize.height());
339 m_delayedResize.setWidth(-1);
340 m_delayedResize.setHeight(-1);
342 if (m_pendingResize.isValid()) {
343 QSize size = m_pendingResize;
344 m_pendingResize.setWidth(-1);
345 m_pendingResize.setHeight(-1);
346 resize(size.width(), size.height());
347 }
350void MirSurface::doResize(int width, int height)
352 bool changed = false;
354 if (width != m_width) {
355 m_width = width;
356 Q_EMIT widthChanged();
357 changed = true;
358 }
360 if (m_height != height) {
361 m_height = height;
362 Q_EMIT heightChanged();
363 changed = true;
364 }
366 if (changed) {
367 XDEBUG_MSG("sizeChanged(width="<<width<<", height="<<height<<")");
368 Q_EMIT sizeChanged(QSize(width, height));
369 }
371 updateInputBoundsAfterResize();
374void MirSurface::updateInputBoundsAfterResize()
376 setInputBounds(QRect(0, 0, m_width, m_height));
379bool MirSurface::isSlowToResize() const
381 return m_slowToResize;
384void MirSurface::setSlowToResize(bool value)
386 if (m_slowToResize != value) {
387 DEBUG_MSG(value);
388 m_slowToResize = value;
389 Q_EMIT slowToResizeChanged();
390 if (!m_slowToResize && m_delayedResizeTimer.isActive()) {
391 m_delayedResizeTimer.stop();
392 applyDelayedResize();
393 }
394 }
397void MirSurface::setMinimumWidth(int value)
399 if (value != m_minimumWidth) {
400 m_minimumWidth = value;
401 Q_EMIT minimumWidthChanged(m_minimumWidth);
402 }
405void MirSurface::setMaximumWidth(int value)
407 if (value != m_maximumWidth) {
408 m_maximumWidth = value;
409 Q_EMIT maximumWidthChanged(m_maximumWidth);
410 }
413void MirSurface::setMinimumHeight(int value)
415 if (value != m_minimumHeight) {
416 m_minimumHeight = value;
417 Q_EMIT minimumHeightChanged(m_minimumHeight);
418 }
421void MirSurface::setMaximumHeight(int value)
423 if (value != m_maximumHeight) {
424 m_maximumHeight = value;
425 Q_EMIT maximumHeightChanged(m_maximumHeight);
426 }
429void MirSurface::setWidthIncrement(int value)
431 if (value != m_widthIncrement) {
432 m_widthIncrement = value;
433 Q_EMIT widthIncrementChanged(m_widthIncrement);
434 }
437void MirSurface::setHeightIncrement(int value)
439 if (value != m_heightIncrement) {
440 m_heightIncrement = value;
441 Q_EMIT heightIncrementChanged(m_heightIncrement);
442 }
445void MirSurface::close()
447 DEBUG_MSG("");
448 if (!m_zombieTimer.isActive()) {
449 m_zombieTimer.start();
450 Q_EMIT closeRequested();
451 }
454void MirSurface::activate()
456 DEBUG_MSG("");
457 SurfaceManager::instance()->activate(this);
460void MirSurface::requestFocus()
462 DEBUG_MSG("");
463 Q_EMIT focusRequested();
466void MirSurface::setFocused(bool value)
468 if (m_focused == value)
469 return;
471 DEBUG_MSG("(" << value << ")");
473 m_focused = value;
474 Q_EMIT focusedChanged(value);
477bool MirSurface::focused() const
479 return m_focused;
482QRect MirSurface::inputBounds() const
484 return m_inputBounds;
487void MirSurface::setInputBounds(const QRect &boundsRect)
489 if (boundsRect != m_inputBounds) {
490 m_inputBounds = boundsRect;
491 DEBUG_MSG("(" << m_inputBounds << ")");
492 Q_EMIT inputBoundsChanged(m_inputBounds);
493 }
496void MirSurface::setRequestedPosition(const QPoint &value)
498 if (value != m_requestedPosition) {
499 m_requestedPosition = value;
500 Q_EMIT requestedPositionChanged(value);
502 // fake-miral: always comply
503 m_position = m_requestedPosition;
504 XDEBUG_MSG("positionChanged("<<m_position<<")");
505 Q_EMIT positionChanged(m_position);
506 }