2 * Copyright (C) 2016 Canonical, Ltd.
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License version 3, as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17#include "TopLevelWindowModel.h"
20#include <unity/shell/application/ApplicationInfoInterface.h>
21#include <unity/shell/application/ApplicationManagerInterface.h>
22#include <unity/shell/application/MirSurfaceInterface.h>
23#include <unity/shell/application/MirSurfaceListInterface.h>
24#include <unity/shell/application/SurfaceManagerInterface.h>
32#include "wmpolicyinterface.h"
35#include <qtmir/mirserverapplication.h>
36#include <qtmir/windowmodelnotifier.h>
38Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL, "toplevelwindowmodel", QtInfoMsg)
40#define DEBUG_MSG qCDebug(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
41#define INFO_MSG qCInfo(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
43namespace unityapi = unity::shell::application;
45TopLevelWindowModel::TopLevelWindowModel()
49void TopLevelWindowModel::setApplicationManager(unityapi::ApplicationManagerInterface* value)
51 if (m_applicationManager == value) {
55 DEBUG_MSG << "(" << value << ")";
57 Q_ASSERT(m_modelState == IdleState);
58 m_modelState = ResettingState;
62 if (m_applicationManager) {
63 disconnect(m_applicationManager, 0, this, 0);
66 m_applicationManager = value;
68 if (m_applicationManager) {
69 connect(m_applicationManager, &QAbstractItemModel::rowsInserted,
70 this, [this](const QModelIndex &/*parent*/, int first, int last) {
71 if (!m_workspace || !m_workspace->isActive())
74 for (int i = first; i <= last; ++i) {
75 auto application = m_applicationManager->get(i);
76 addApplication(application);
80 connect(m_applicationManager, &QAbstractItemModel::rowsAboutToBeRemoved,
81 this, [this](const QModelIndex &/*parent*/, int first, int last) {
82 for (int i = first; i <= last; ++i) {
83 auto application = m_applicationManager->get(i);
84 removeApplication(application);
89 Q_EMIT applicationManagerChanged(m_applicationManager);
93 m_modelState = IdleState;
96void TopLevelWindowModel::setSurfaceManager(unityapi::SurfaceManagerInterface *surfaceManager)
98 if (surfaceManager == m_surfaceManager) {
102 DEBUG_MSG << "(" << surfaceManager << ")";
104 Q_ASSERT(m_modelState == IdleState);
105 m_modelState = ResettingState;
109 if (m_surfaceManager) {
110 disconnect(m_surfaceManager, 0, this, 0);
113 m_surfaceManager = surfaceManager;
115 if (m_surfaceManager) {
116 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfaceCreated, this, &TopLevelWindowModel::onSurfaceCreated);
117 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesRaised, this, &TopLevelWindowModel::onSurfacesRaised);
118 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsStarted, this, &TopLevelWindowModel::onModificationsStarted);
119 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::modificationsEnded, this, &TopLevelWindowModel::onModificationsEnded);
121 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAddedToWorkspace,
122 this, &TopLevelWindowModel::onSurfacesAddedToWorkspace);
123 connect(m_surfaceManager, &unityapi::SurfaceManagerInterface::surfacesAboutToBeRemovedFromWorkspace,
124 this, &TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace);
128 Q_EMIT surfaceManagerChanged(m_surfaceManager);
131 m_modelState = IdleState;
134void TopLevelWindowModel::setWorkspace(Workspace *workspace)
136 if (workspace == m_workspace) {
140 DEBUG_MSG << "(" << workspace << ")";
142 Q_ASSERT(m_modelState == IdleState);
143 m_modelState = ResettingState;
148 disconnect(m_workspace, 0, this, 0);
151 m_workspace = workspace;
154 Q_EMIT workspaceChanged(workspace);
157 m_modelState = IdleState;
160void TopLevelWindowModel::addApplication(unityapi::ApplicationInfoInterface *application)
162 DEBUG_MSG << "(" << application->appId() << ")";
164 if (application->state() != unityapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
165 prependPlaceholder(application);
169void TopLevelWindowModel::removeApplication(unityapi::ApplicationInfoInterface *application)
171 DEBUG_MSG << "(" << application->appId() << ")";
173 Q_ASSERT(m_modelState == IdleState);
176 while (i < m_windowModel.count()) {
177 if (m_windowModel.at(i).application == application) {
185void TopLevelWindowModel::prependPlaceholder(unityapi::ApplicationInfoInterface *application)
187 INFO_MSG << "(" << application->appId() << ")";
189 prependSurfaceHelper(nullptr, application);
192void TopLevelWindowModel::prependSurface(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
194 Q_ASSERT(surface != nullptr);
196 bool filledPlaceholder = false;
197 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
198 ModelEntry &entry = m_windowModel[i];
199 if (entry.application == application && entry.window->surface() == nullptr) {
200 entry.window->setSurface(surface);
201 connectSurface(surface);
202 INFO_MSG << " appId=" << application->appId() << " surface=" << surface
203 << ", filling out placeholder. after: " << toString();
204 filledPlaceholder = true;
208 if (!filledPlaceholder) {
209 INFO_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
210 prependSurfaceHelper(surface, application);
214void TopLevelWindowModel::prependSurfaceHelper(unityapi::MirSurfaceInterface *surface, unityapi::ApplicationInfoInterface *application)
216 if (m_modelState == IdleState) {
217 m_modelState = InsertingState;
218 beginInsertRows(QModelIndex(), 0 /*first*/, 0 /*last*/);
220 Q_ASSERT(m_modelState == ResettingState);
221 // No point in signaling anything if we're resetting the whole model
224 Window *window = createWindow(surface);
226 m_windowModel.prepend(ModelEntry(window, application));
227 m_allSurfaces.insert(surface);
229 connectSurface(surface);
232 if (m_modelState == InsertingState) {
234 Q_EMIT countChanged();
235 Q_EMIT listChanged();
236 m_modelState = IdleState;
240 activateEmptyWindow(window);
243 INFO_MSG << " after " << toString();
246void TopLevelWindowModel::connectWindow(Window *window)
248 connect(window, &Window::focusRequested, this, [this, window]() {
249 if (!window->surface()) {
250 activateEmptyWindow(window);
254 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
255 if (window->surface()) {
256 // Condense changes to the focused window
257 // eg: Do focusedWindow=A to focusedWindow=B instead of
258 // focusedWindow=A to focusedWindow=null to focusedWindow=B
260 Q_ASSERT(m_newlyFocusedWindow == nullptr);
261 m_focusedWindowChanged = true;
262 m_newlyFocusedWindow = window;
263 } else if (m_focusedWindow == window) {
264 m_focusedWindowChanged = true;
266 // don't clear the focused window if you were not there in the first place
267 // happens when a filled window gets replaced with an empty one (no surface) as the focused window.
272 connect(window, &Window::closeRequested, this, [this, window]() {
273 if (!window->surface()) {
274 // do things ourselves as miral doesn't know about this window
275 int id = window->id();
276 int index = indexForId(id);
277 bool focusOther = false;
278 Q_ASSERT(index >= 0);
279 if (window->focused()) {
282 m_windowModel[index].application->close();
284 activateTopMostWindowWithoutId(id);
289 connect(window, &Window::emptyWindowActivated, this, [this, window]() {
290 activateEmptyWindow(window);
294void TopLevelWindowModel::activateEmptyWindow(Window *window)
296 Q_ASSERT(!window->surface());
297 DEBUG_MSG << "(" << window << ")";
299 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
300 // So we have to activate them ourselves (instead of asking SurfaceManager to do it for us).
302 window->setFocused(true);
303 raiseId(window->id());
304 Window *previousWindow = m_focusedWindow;
305 setFocusedWindow(window);
306 if (previousWindow && previousWindow->surface() && previousWindow->surface()->focused()) {
307 m_surfaceManager->activate(nullptr);
311void TopLevelWindowModel::connectSurface(unityapi::MirSurfaceInterface *surface)
313 connect(surface, &unityapi::MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
315 onSurfaceDied(surface);
318 connect(surface, &QObject::destroyed, this, [this, surface](){ this->onSurfaceDestroyed(surface); });
321void TopLevelWindowModel::onSurfaceDied(unityapi::MirSurfaceInterface *surface)
323 if (surface->type() == Mir::InputMethodType) {
324 removeInputMethodWindow();
328 int i = indexOf(surface);
333 auto application = m_windowModel[i].application;
335 // can't be starting if it already has a surface
336 Q_ASSERT(application->state() != unityapi::ApplicationInfoInterface::Starting);
338 if (application->state() == unityapi::ApplicationInfoInterface::Running) {
339 m_windowModel[i].removeOnceSurfaceDestroyed = true;
341 // assume it got killed by the out-of-memory daemon.
343 // So leave entry in the model and only remove its surface, so shell can display a screenshot
345 m_windowModel[i].removeOnceSurfaceDestroyed = false;
349void TopLevelWindowModel::onSurfaceDestroyed(unityapi::MirSurfaceInterface *surface)
351 int i = indexOf(surface);
356 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
359 auto window = m_windowModel[i].window;
360 window->setSurface(nullptr);
361 window->setFocused(false);
362 m_allSurfaces.remove(surface);
363 INFO_MSG << " Removed surface from entry. After: " << toString();
367Window *TopLevelWindowModel::createWindow(unityapi::MirSurfaceInterface *surface)
369 int id = generateId();
370 Window *qmlWindow = new Window(id, this);
371 connectWindow(qmlWindow);
373 qmlWindow->setSurface(surface);
379void TopLevelWindowModel::onSurfaceCreated(unityapi::MirSurfaceInterface */*surface*/)
383void TopLevelWindowModel::onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
384 const QVector<unity::shell::application::MirSurfaceInterface*> surfaces)
386 if (!m_workspace || !m_applicationManager) return;
387 if (workspace != m_workspace->workspace()) return;
389 Q_FOREACH(auto surface, surfaces) {
390 if (m_allSurfaces.contains(surface)) continue;
392 if (surface->parentSurface()) {
393 // Wrap it in a Window so that we keep focusedWindow() up to date.
394 Window *window = createWindow(surface);
395 connect(surface, &QObject::destroyed, window, [=](){
396 window->setSurface(nullptr);
397 window->deleteLater();
400 if (surface->type() == Mir::InputMethodType) {
401 setInputMethodWindow(createWindow(surface));
403 auto *application = m_applicationManager->findApplicationWithSurface(surface);
405 prependSurface(surface, application);
407 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
408 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
409 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
410 Window *promptWindow = createWindow(surface);
411 connect(surface, &QObject::destroyed, promptWindow, [=](){
412 promptWindow->setSurface(nullptr);
413 promptWindow->deleteLater();
421void TopLevelWindowModel::onSurfacesAboutToBeRemovedFromWorkspace(const std::shared_ptr<miral::Workspace> &workspace,
422 const QVector<unity::shell::application::MirSurfaceInterface *> surfaces)
424 if (workspace != m_workspace->workspace()) return;
428 for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
429 auto surface = *iter;
432 // Do removals in adjacent blocks.
433 start = end = indexOf(surface);
435 // probably a child surface
436 m_allSurfaces.remove(surface);
439 while(iter != surfaces.constEnd()) {
440 int index = indexOf(*iter);
441 if (index != end+1) {
448 if (m_modelState == IdleState) {
449 beginRemoveRows(QModelIndex(), start, end);
450 m_modelState = RemovingState;
452 Q_ASSERT(m_modelState == ResettingState);
453 // No point in signaling anything if we're resetting the whole model
456 for (int index = start; index <= end; index++) {
457 auto window = m_windowModel[start].window;
458 window->setSurface(nullptr);
459 window->setFocused(false);
461 m_windowModel.removeAt(start);
462 m_allSurfaces.remove(window->surface());
465 if (m_modelState == RemovingState) {
467 Q_EMIT countChanged();
468 Q_EMIT listChanged();
469 m_modelState = IdleState;
474void TopLevelWindowModel::removeAt(int index)
476 if (m_modelState == IdleState) {
477 beginRemoveRows(QModelIndex(), index, index);
478 m_modelState = RemovingState;
480 Q_ASSERT(m_modelState == ResettingState);
481 // No point in signaling anything if we're resetting the whole model
484 auto window = m_windowModel[index].window;
486 window->setSurface(nullptr);
487 window->setFocused(false);
489 m_windowModel.removeAt(index);
490 m_allSurfaces.remove(window->surface());
492 if (m_modelState == RemovingState) {
494 Q_EMIT countChanged();
495 Q_EMIT listChanged();
496 m_modelState = IdleState;
499 disconnect(window, 0, this, 0);
500 if (m_focusedWindow == window) {
501 setFocusedWindow(nullptr);
505 INFO_MSG << " after " << toString();
508void TopLevelWindowModel::setInputMethodWindow(Window *window)
510 if (m_inputMethodWindow) {
511 qWarning("Multiple Input Method Surfaces created, removing the old one!");
512 delete m_inputMethodWindow;
514 m_inputMethodWindow = window;
515 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
518void TopLevelWindowModel::removeInputMethodWindow()
520 if (m_inputMethodWindow) {
521 delete m_inputMethodWindow;
522 m_inputMethodWindow = nullptr;
523 Q_EMIT inputMethodSurfaceChanged(nullptr);
527void TopLevelWindowModel::onSurfacesRaised(const QVector<unityapi::MirSurfaceInterface*> &surfaces)
529 DEBUG_MSG << "(" << surfaces << ")";
530 const int raiseCount = surfaces.size();
531 for (int i = 0; i < raiseCount; i++) {
532 int fromIndex = findIndexOf(surfaces[i]);
533 if (fromIndex != -1) {
539int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
541 return m_windowModel.count();
544QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
546 if (index.row() < 0 || index.row() >= m_windowModel.size())
549 if (role == WindowRole) {
550 Window *window = m_windowModel.at(index.row()).window;
551 return QVariant::fromValue(window);
552 } else if (role == ApplicationRole) {
553 return QVariant::fromValue(m_windowModel.at(index.row()).application);
559int TopLevelWindowModel::findIndexOf(const unityapi::MirSurfaceInterface *surface) const
561 for (int i=0; i<m_windowModel.count(); i++) {
562 if (m_windowModel[i].window->surface() == surface) {
569int TopLevelWindowModel::generateId()
572 m_nextId = nextFreeId(nextId(id), id);
576int TopLevelWindowModel::nextId(int id) const
585int TopLevelWindowModel::nextFreeId(int candidateId, const int latestId)
587 int firstCandidateId = candidateId;
589 while (indexForId(candidateId) != -1 || candidateId == latestId) {
590 candidateId = nextId(candidateId);
592 if (candidateId == firstCandidateId) {
593 qFatal("TopLevelWindowModel: run out of window ids.");
600QString TopLevelWindowModel::toString()
603 for (int i = 0; i < m_windowModel.count(); ++i) {
604 auto item = m_windowModel.at(i);
606 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
607 .arg(QString::number(i),
608 item.application->appId(),
609 QString::number((qintptr)item.window->surface(), 16),
610 QString::number(item.window->id()));
620int TopLevelWindowModel::indexOf(unityapi::MirSurfaceInterface *surface)
622 for (int i = 0; i < m_windowModel.count(); ++i) {
623 if (m_windowModel.at(i).window->surface() == surface) {
630int TopLevelWindowModel::indexForId(int id) const
632 for (int i = 0; i < m_windowModel.count(); ++i) {
633 if (m_windowModel[i].window->id() == id) {
640Window *TopLevelWindowModel::windowAt(int index) const
642 if (index >=0 && index < m_windowModel.count()) {
643 return m_windowModel[index].window;
649unityapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
651 if (index >=0 && index < m_windowModel.count()) {
652 return m_windowModel[index].window->surface();
658unityapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
660 if (index >=0 && index < m_windowModel.count()) {
661 return m_windowModel[index].application;
667int TopLevelWindowModel::idAt(int index) const
669 if (index >=0 && index < m_windowModel.count()) {
670 return m_windowModel[index].window->id();
676void TopLevelWindowModel::raiseId(int id)
678 if (m_modelState == IdleState) {
679 DEBUG_MSG << "(id=" << id << ") - do it now.";
682 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
683 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
684 // if we perform yet another model change straight away.
686 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
687 // the index is definitely within bounds.
688 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
692void TopLevelWindowModel::doRaiseId(int id)
694 int fromIndex = indexForId(id);
695 // can't raise something that doesn't exist or that it's already on top
696 if (fromIndex != -1 && fromIndex != 0) {
697 auto surface = m_windowModel[fromIndex].window->surface();
699 m_surfaceManager->raise(surface);
701 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
702 // miral can do about it.
708void TopLevelWindowModel::setFocusedWindow(Window *window)
710 if (window != m_focusedWindow) {
711 INFO_MSG << "(" << window << ")";
713 Window* previousWindow = m_focusedWindow;
715 m_focusedWindow = window;
716 Q_EMIT focusedWindowChanged(m_focusedWindow);
718 if (previousWindow && previousWindow->focused() && !previousWindow->surface()) {
719 // do it ourselves. miral doesn't know about this window
720 previousWindow->setFocused(false);
725unityapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
727 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
730Window* TopLevelWindowModel::focusedWindow() const
732 return m_focusedWindow;
735void TopLevelWindowModel::move(int from, int to)
737 if (from == to) return;
738 DEBUG_MSG << " from=" << from << " to=" << to;
740 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
742 /* When moving an item down, the destination index needs to be incremented
743 by one, as explained in the documentation:
744 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
746 Q_ASSERT(m_modelState == IdleState);
747 m_modelState = MovingState;
749 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
750#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
751 const auto &window = m_windowModel.takeAt(from);
752 m_windowModel.insert(to, window);
754 m_windowModel.move(from, to);
758 Q_EMIT listChanged();
759 m_modelState = IdleState;
761 INFO_MSG << " after " << toString();
764void TopLevelWindowModel::onModificationsStarted()
768void TopLevelWindowModel::onModificationsEnded()
770 if (m_focusedWindowChanged) {
771 setFocusedWindow(m_newlyFocusedWindow);
774 m_focusedWindowChanged = false;
775 m_newlyFocusedWindow = nullptr;
778void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
780 DEBUG_MSG << "(" << forbiddenId << ")";
782 for (int i = 0; i < m_windowModel.count(); ++i) {
783 Window *window = m_windowModel[i].window;
784 if (window->id() != forbiddenId) {
790void TopLevelWindowModel::refreshWindows()
794 m_windowModel.clear();
795 m_allSurfaces.clear();
797 if (!m_workspace || !m_applicationManager) return;
798 WMPolicyInterface::instance()->forEachWindowInWorkspace(m_workspace->workspace(), [this](const miral::Window &window) {
799 auto surface = m_surfaceManager->surfaceFor(window);
801 if (surface->parentSurface()) {
802 // Wrap it in a Window so that we keep focusedWindow() up to date.
803 Window *window = createWindow(surface);
804 connect(surface, &QObject::destroyed, window, [=](){
805 window->setSurface(nullptr);
806 window->deleteLater();
809 if (surface->type() == Mir::InputMethodType) {
810 setInputMethodWindow(createWindow(surface));
812 auto *application = m_applicationManager->findApplicationWithSurface(surface);
814 prependSurface(surface, application);
816 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
817 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
818 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
819 Window *promptWindow = createWindow(surface);
820 connect(surface, &QObject::destroyed, promptWindow, [=](){
821 promptWindow->setSurface(nullptr);
822 promptWindow->deleteLater();
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list