Lomiri
Loading...
Searching...
No Matches
TopLevelWindowModel.cpp
1/*
2 * Copyright (C) 2016-2017 Canonical Ltd.
3 * Copyright 2019 UBports Foundation
4 *
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License version 3, as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
11 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "TopLevelWindowModel.h"
19#include "WindowManagerObjects.h"
20
21// lomiri-api
22#include <lomiri/shell/application/ApplicationInfoInterface.h>
23#include <lomiri/shell/application/ApplicationManagerInterface.h>
24#include <lomiri/shell/application/MirSurfaceInterface.h>
25#include <lomiri/shell/application/MirSurfaceListInterface.h>
26#include <lomiri/shell/application/SurfaceManagerInterface.h>
27
28// Qt
29#include <QDebug>
30
31// local
32#include "Window.h"
33#include "Workspace.h"
34#include "InputMethodManager.h"
35
36Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL, "toplevelwindowmodel", QtInfoMsg)
37
38#define DEBUG_MSG qCDebug(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
39#define INFO_MSG qCInfo(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
40
41namespace lomiriapi = lomiri::shell::application;
42
43TopLevelWindowModel::TopLevelWindowModel(Workspace* workspace)
44 : m_nullWindow(createWindow(nullptr)),
45 m_workspace(workspace),
46 m_surfaceManagerBusy(false)
47{
48 connect(WindowManagerObjects::instance(), &WindowManagerObjects::applicationManagerChanged,
49 this, &TopLevelWindowModel::setApplicationManager);
50 connect(WindowManagerObjects::instance(), &WindowManagerObjects::surfaceManagerChanged,
51 this, &TopLevelWindowModel::setSurfaceManager);
52
53 setApplicationManager(WindowManagerObjects::instance()->applicationManager());
54 setSurfaceManager(WindowManagerObjects::instance()->surfaceManager());
55
56 connect(m_nullWindow, &Window::focusedChanged, this, [this] {
57 Q_EMIT rootFocusChanged();
58 });
59}
60
61TopLevelWindowModel::~TopLevelWindowModel()
62{
63}
64
65void TopLevelWindowModel::setApplicationManager(lomiriapi::ApplicationManagerInterface* value)
66{
67 if (m_applicationManager == value) {
68 return;
69 }
70
71 DEBUG_MSG << "(" << value << ")";
72
73 Q_ASSERT(m_modelState == IdleState);
74 m_modelState = ResettingState;
75
76 beginResetModel();
77
78 if (m_applicationManager) {
79 disconnect(m_applicationManager, 0, this, 0);
80 }
81
82 m_applicationManager = value;
83
84 if (m_applicationManager) {
85 connect(m_applicationManager, &QAbstractItemModel::rowsInserted,
86 this, [this](const QModelIndex &/*parent*/, int first, int last) {
87 if (!m_workspace || !m_workspace->isActive())
88 return;
89
90 for (int i = first; i <= last; ++i) {
91 auto application = m_applicationManager->get(i);
92 addApplication(application);
93 }
94 });
95
96 connect(m_applicationManager, &QAbstractItemModel::rowsAboutToBeRemoved,
97 this, [this](const QModelIndex &/*parent*/, int first, int last) {
98 for (int i = first; i <= last; ++i) {
99 auto application = m_applicationManager->get(i);
100 removeApplication(application);
101 }
102 });
103 }
104
105 refreshWindows();
106
107 endResetModel();
108 m_modelState = IdleState;
109}
110
111void TopLevelWindowModel::setSurfaceManager(lomiriapi::SurfaceManagerInterface *surfaceManager)
112{
113 if (surfaceManager == m_surfaceManager) {
114 return;
115 }
116
117 DEBUG_MSG << "(" << surfaceManager << ")";
118
119 Q_ASSERT(m_modelState == IdleState);
120 m_modelState = ResettingState;
121
122 beginResetModel();
123
124 if (m_surfaceManager) {
125 disconnect(m_surfaceManager, 0, this, 0);
126 }
127
128 m_surfaceManager = surfaceManager;
129
130 if (m_surfaceManager) {
131 connect(m_surfaceManager, &lomiriapi::SurfaceManagerInterface::surfacesAddedToWorkspace, this, &TopLevelWindowModel::onSurfacesAddedToWorkspace);
132 connect(m_surfaceManager, &lomiriapi::SurfaceManagerInterface::surfacesRaised, this, &TopLevelWindowModel::onSurfacesRaised);
133 connect(m_surfaceManager, &lomiriapi::SurfaceManagerInterface::surfaceRemoved, this, &TopLevelWindowModel::onSurfaceDestroyed);
134 connect(m_surfaceManager, &lomiriapi::SurfaceManagerInterface::modificationsStarted, this, &TopLevelWindowModel::onModificationsStarted);
135 connect(m_surfaceManager, &lomiriapi::SurfaceManagerInterface::modificationsEnded, this, &TopLevelWindowModel::onModificationsEnded);
136 }
137
138 refreshWindows();
139
140 endResetModel();
141 m_modelState = IdleState;
142}
143
144void TopLevelWindowModel::addApplication(lomiriapi::ApplicationInfoInterface *application)
145{
146 DEBUG_MSG << "(" << application->appId() << ")";
147
148 if (application->state() != lomiriapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
149 prependPlaceholder(application);
150 }
151}
152
153void TopLevelWindowModel::removeApplication(lomiriapi::ApplicationInfoInterface *application)
154{
155 DEBUG_MSG << "(" << application->appId() << ")";
156
157 Q_ASSERT(m_modelState == IdleState);
158
159 int i = 0;
160 while (i < m_windowModel.count()) {
161 if (m_windowModel.at(i).application == application) {
162 deleteAt(i);
163 } else {
164 ++i;
165 }
166 }
167}
168
169void TopLevelWindowModel::prependPlaceholder(lomiriapi::ApplicationInfoInterface *application)
170{
171 DEBUG_MSG << "(" << application->appId() << ")";
172
173 if (!application->showSplash())
174 return;
175
176 prependSurfaceHelper(nullptr, application);
177}
178
179// We do a fuzzy comparison to avoid matching Xwayland windows to the wrong
180// application window, ie: surface appId "chromium-browser" should match
181// "chromiumut.shapa_chromelauncher" but no other window appId.
182inline bool fuzzyNameCompare(const QString& appId, const QString& surfaceAppId)
183{
184 QString::const_iterator it = nullptr;
185 const QString appName = appId.mid(appId.indexOf("_") + 1).toLower();
186 const QString surfaceName = surfaceAppId.toLower();
187 const int minSuccess = std::min<int>(surfaceName.length(), 5);
188 int sameLetterFound = 0;
189
190 DEBUG_MSG << "appName: " << appName << " surfaceName: " << surfaceName;
191
192 it = appName.cbegin();
193 for (const auto& c : surfaceName) {
194 QString::const_iterator tmpIt = std::find(it, appName.cend(), c);
195 if (tmpIt != appName.end() && tmpIt >= it) {
196 DEBUG_MSG << "fuzzy match letter: " << c;
197 ++sameLetterFound;
198 it = tmpIt;
199 }
200 }
201
202 return sameLetterFound >= minSuccess;
203}
204
205void TopLevelWindowModel::prependSurface(lomiriapi::MirSurfaceInterface *surface, lomiriapi::ApplicationInfoInterface *application)
206{
207 Q_ASSERT(surface != nullptr);
208
209 connectSurface(surface);
210 m_allSurfaces.insert(surface);
211
212 bool filledPlaceholder = false;
213 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
214 ModelEntry &entry = m_windowModel[i];
215 if (entry.application == application && (entry.window->surface() == nullptr || !entry.window->surface()->live())) {
216 entry.window->setSurface(surface);
217 DEBUG_MSG << " appId=" << application->appId() << " surface=" << surface
218 << ", filling out placeholder. after: " << toString();
219 filledPlaceholder = true;
220 }
221 }
222
223 // Mir doesn't give us the pid of Xwayland surfaces but rather of Lomiri since it spawns the X socket
224 // and activates Xwayland in the back. Do extra work to match the surface to the appropriate window.
225 // TODO: Use XCB to get the pid of an Xwayland window? How to distinguish between X windows with the same name?
226 if (!filledPlaceholder && application->appId() == QStringLiteral("xwayland.qtmir")) {
227 // First, let's see if there's a window's appId that contains the surface's appId,
228 // ie: window app Id "steam_steam" & surface app Id "steam"
229 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
230 ModelEntry &entry = m_windowModel[i];
231 if ((!entry.window->surface() || !entry.window->surface()->live()) &&
232 entry.application->appId().contains(surface->appId()))
233 {
234 entry.window->setSurface(surface);
235 DEBUG_MSG << " Xwayland app " << entry.application->appId() << " contains surface=" << surface->appId()
236 << ", filling out placeholder. after: " << toString();
237 filledPlaceholder = true;
238 }
239 }
240
241 // Reduce false positives: Check if there's more than 1 fuzzy match candidate
242 unsigned int candidates = 0;
243 int selectedCandidate = -1;
244 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
245 ModelEntry &entry = m_windowModel[i];
246 if ((!entry.window->surface() || !entry.window->surface()->live()) &&
247 fuzzyNameCompare(entry.application->appId(), surface->appId()))
248 {
249 ++candidates;
250 selectedCandidate = i;
251 }
252 }
253
254 // Fuzzy match if there's only one
255 if (candidates == 1 && !filledPlaceholder) {
256 ModelEntry &entry = m_windowModel[selectedCandidate];
257 entry.window->setSurface(surface);
258 DEBUG_MSG << " Xwayland app " << entry.application->appId() << " fuzzy match surface=" << surface->appId()
259 << ", filling out placeholder. after: " << toString();
260 filledPlaceholder = true;
261 }
262 }
263
264 if (!filledPlaceholder) {
265 DEBUG_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
266 prependSurfaceHelper(surface, application);
267 }
268}
269
270void TopLevelWindowModel::prependSurfaceHelper(lomiriapi::MirSurfaceInterface *surface, lomiriapi::ApplicationInfoInterface *application)
271{
272
273 Window *window = createWindow(surface);
274
275 connect(window, &Window::stateChanged, this, [=](Mir::State newState) {
276 if (newState == Mir::HiddenState) {
277 // Comply, removing it from our model. Just as if it didn't exist anymore.
278 removeAt(indexForId(window->id()));
279 } else {
280 if (indexForId(window->id()) == -1) {
281 // was probably hidden before. put it back on the list
282 auto *application = m_applicationManager->findApplicationWithSurface(window->surface());
283 Q_ASSERT(application);
284 prependWindow(window, application);
285 }
286 }
287 });
288
289 prependWindow(window, application);
290
291 // Activate the newly-prepended window.
292 window->activate();
293
294 DEBUG_MSG << " after " << toString();
295}
296
297void TopLevelWindowModel::prependWindow(Window *window, lomiriapi::ApplicationInfoInterface *application)
298{
299 if (m_modelState == IdleState) {
300 m_modelState = InsertingState;
301 beginInsertRows(QModelIndex(), 0 /*first*/, 0 /*last*/);
302 } else {
303 Q_ASSERT(m_modelState == ResettingState);
304 // No point in signaling anything if we're resetting the whole model
305 }
306
307 m_windowModel.prepend(ModelEntry(window, application));
308
309 if (m_modelState == InsertingState) {
310 endInsertRows();
311 Q_EMIT countChanged();
312 Q_EMIT listChanged();
313 m_modelState = IdleState;
314 }
315}
316
317void TopLevelWindowModel::connectWindow(Window *window)
318{
319 connect(window, &Window::focusRequested, this, [this, window]() {
320 if (!window->surface()) {
321 activateEmptyWindow(window);
322 }
323 });
324
325 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
326 if (window->surface()) {
327 if (focused) {
328 setFocusedWindow(window);
329 m_focusedWindowCleared = false;
330 } else if (m_focusedWindow == window) {
331 // Condense changes to the focused window
332 // eg: Do focusedWindow=A to focusedWindow=B instead of
333 // focusedWindow=A to focusedWindow=null to focusedWindow=B
334 m_focusedWindowCleared = true;
335 } else {
336 // don't clear the focused window if you were not there in the first place
337 // happens when a filled window gets replaced with an empty one (no surface) as the focused window.
338 }
339 }
340 });
341
342 connect(window, &Window::closeRequested, this, [this, window]() {
343 if (!window->surface()) {
344 // do things ourselves as miral doesn't know about this window
345 int id = window->id();
346 int index = indexForId(id);
347 bool focusOther = false;
348 Q_ASSERT(index >= 0);
349 if (window->focused()) {
350 focusOther = true;
351 }
352 m_windowModel[index].application->close();
353 if (focusOther) {
354 activateTopMostWindowWithoutId(id);
355 }
356 }
357 });
358
359 connect(window, &Window::emptyWindowActivated, this, [this, window]() {
360 activateEmptyWindow(window);
361 });
362
363 connect(window, &Window::liveChanged, this, [this, window](bool isAlive) {
364 if (!isAlive && window->state() == Mir::HiddenState) {
365 // Hidden windows are not in the model. So just delete it right away.
366 delete window;
367 }
368 });
369}
370
371void TopLevelWindowModel::activateEmptyWindow(Window *window)
372{
373 Q_ASSERT(!window->surface());
374 DEBUG_MSG << "(" << window << ")";
375
376 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
377 // So we have to activate them ourselves (instead of asking SurfaceManager to do it for us).
378
379 window->setFocused(true);
380 raiseId(window->id());
381 Window *previousWindow = m_focusedWindow;
382 setFocusedWindow(window);
383 if (previousWindow && previousWindow->surface() && previousWindow->surface()->focused()) {
384 m_surfaceManager->activate(nullptr);
385 }
386}
387
388void TopLevelWindowModel::connectSurface(lomiriapi::MirSurfaceInterface *surface)
389{
390 connect(surface, &lomiriapi::MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
391 if (!live) {
392 onSurfaceDied(surface);
393 }
394 });
395 connect(surface, &QObject::destroyed, this, [this, surface](QObject*){
396 this->onSurfaceDestroyed(surface);
397 });
398}
399
400int TopLevelWindowModel::countWindowsWithApplication(lomiri::shell::application::ApplicationInfoInterface *application)
401{
402 int count = 0;
403
404 for (const auto & item : qAsConst(m_windowModel)) {
405 if (item.application == application)
406 count++;
407 }
408
409 return count;
410}
411
412void TopLevelWindowModel::onSurfaceDied(lomiriapi::MirSurfaceInterface *surface)
413{
414 if (surface->type() == Mir::InputMethodType) {
415 removeInputMethodWindow();
416 return;
417 }
418
419 int i = indexOf(surface);
420 if (i == -1) {
421 return;
422 }
423
424 auto application = m_windowModel[i].application;
425
426 DEBUG_MSG << " application->name()=" << application->name()
427 << " application->state()=" << application->state();
428
429 // assume a touch app got killed by the out-of-memory daemon.
430 //
431 // Leave at most 1 entry in the model and only remove its surface, so shell can display a screenshot
432 // in its place.
433 if (application->isTouchApp() && countWindowsWithApplication(application) == 1)
434 m_windowModel[i].removeOnceSurfaceDestroyed = false;
435 else
436 m_windowModel[i].removeOnceSurfaceDestroyed = true;
437}
438
439void TopLevelWindowModel::onSurfaceDestroyed(lomiriapi::MirSurfaceInterface *surface)
440{
441 int i = indexOf(surface);
442 if (i == -1) {
443 return;
444 }
445
446 auto application = m_windowModel[i].application;
447
448 // Clean up non-touch windows immediately, we cannot reliably restart them from an OOM situation
449 // and we cannot allow stuck windows to stick around after actually having closed them.
450 if (application->appId() == QStringLiteral("xwayland.qtmir") || !application->isTouchApp()) {
451 m_windowModel[i].removeOnceSurfaceDestroyed = true;
452 }
453
454 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
455 deleteAt(i);
456 } else {
457 auto window = m_windowModel[i].window;
458 window->setFocused(false);
459 m_allSurfaces.remove(surface);
460 DEBUG_MSG << " Removed surface from entry. After: " << toString();
461 }
462}
463
464Window *TopLevelWindowModel::createWindow(lomiriapi::MirSurfaceInterface *surface)
465{
466 int id = m_nextId.fetchAndAddAcquire(1);
467 return createWindowWithId(surface, id);
468}
469
470Window *TopLevelWindowModel::createNullWindow()
471{
472 return createWindowWithId(nullptr, 0);
473}
474
475Window *TopLevelWindowModel::createWindowWithId(lomiriapi::MirSurfaceInterface *surface, int id)
476{
477 Window *qmlWindow = new Window(id, this);
478 connectWindow(qmlWindow);
479 if (surface) {
480 qmlWindow->setSurface(surface);
481 }
482 return qmlWindow;
483}
484
485void TopLevelWindowModel::onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
486 const QVector<lomiri::shell::application::MirSurfaceInterface*> surfaces)
487{
488 if (!m_workspace || !m_applicationManager) return;
489 if (workspace != m_workspace->workspace()) {
490 removeSurfaces(surfaces);
491 return;
492 }
493
494 Q_FOREACH(auto surface, surfaces) {
495 if (m_allSurfaces.contains(surface)) continue;
496
497 if (surface->parentSurface()) {
498 // Wrap it in a Window so that we keep focusedWindow() up to date.
499 Window *window = createWindow(surface);
500 connect(surface, &QObject::destroyed, window, [=](){
501 window->setSurface(nullptr);
502 window->deleteLater();
503 });
504 } else {
505 if (surface->type() == Mir::InputMethodType) {
506 connectSurface(surface);
507 setInputMethodWindow(createWindow(surface));
508 } else {
509 auto *application = m_applicationManager->findApplicationWithSurface(surface);
510 if (application) {
511 if (surface->state() == Mir::HiddenState) {
512 // Ignore it until it's finally shown
513 connect(surface, &lomiriapi::MirSurfaceInterface::stateChanged, this, [=](Mir::State newState) {
514 Q_ASSERT(newState != Mir::HiddenState);
515 disconnect(surface, &lomiriapi::MirSurfaceInterface::stateChanged, this, 0);
516 prependSurface(surface, application);
517 });
518 } else {
519 prependSurface(surface, application);
520 }
521 } else {
522 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
523 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
524 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
525 Window *promptWindow = createWindow(surface);
526 connect(surface, &QObject::destroyed, promptWindow, [=](){
527 promptWindow->setSurface(nullptr);
528 promptWindow->deleteLater();
529 });
530 }
531 }
532 }
533 }
534}
535
536void TopLevelWindowModel::removeSurfaces(const QVector<lomiri::shell::application::MirSurfaceInterface *> surfaces)
537{
538 int start = -1;
539 int end = -1;
540 for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
541 auto surface = *iter;
542 iter++;
543
544 // Do removals in adjacent blocks.
545 start = end = indexOf(surface);
546 if (start == -1) {
547 // could be a child surface
548 m_allSurfaces.remove(surface);
549 continue;
550 }
551 while(iter != surfaces.constEnd()) {
552 int index = indexOf(*iter);
553 if (index != end+1) {
554 break;
555 }
556 end++;
557 iter++;
558 }
559
560 if (m_modelState == IdleState) {
561 beginRemoveRows(QModelIndex(), start, end);
562 m_modelState = RemovingState;
563 } else {
564 Q_ASSERT(m_modelState == ResettingState);
565 // No point in signaling anything if we're resetting the whole model
566 }
567
568 for (int index = start; index <= end; index++) {
569 auto window = m_windowModel[start].window;
570 window->setSurface(nullptr);
571 window->setFocused(false);
572
573 if (window == m_previousWindow) {
574 m_previousWindow = nullptr;
575 }
576
577 m_windowModel.removeAt(start);
578 m_allSurfaces.remove(surface);
579 }
580
581 if (m_modelState == RemovingState) {
582 endRemoveRows();
583 Q_EMIT countChanged();
584 Q_EMIT listChanged();
585 m_modelState = IdleState;
586 }
587 }
588}
589
590void TopLevelWindowModel::deleteAt(int index)
591{
592 auto window = m_windowModel[index].window;
593
594 removeAt(index);
595
596 window->setSurface(nullptr);
597
598 delete window;
599}
600
601void TopLevelWindowModel::removeAt(int index)
602{
603 if (m_modelState == IdleState) {
604 beginRemoveRows(QModelIndex(), index, index);
605 m_modelState = RemovingState;
606 } else {
607 Q_ASSERT(m_modelState == ResettingState);
608 // No point in signaling anything if we're resetting the whole model
609 }
610
611 auto window = m_windowModel[index].window;
612 auto surface = window->surface();
613
614 if (!window->surface()) {
615 window->setFocused(false);
616 }
617
618 if (window == m_previousWindow) {
619 m_previousWindow = nullptr;
620 }
621
622 m_windowModel.removeAt(index);
623 m_allSurfaces.remove(surface);
624
625 if (m_modelState == RemovingState) {
626 endRemoveRows();
627 Q_EMIT countChanged();
628 Q_EMIT listChanged();
629 m_modelState = IdleState;
630 }
631
632 if (m_focusedWindow == window) {
633 setFocusedWindow(nullptr);
634 m_focusedWindowCleared = false;
635 }
636
637 if (m_previousWindow == window) {
638 m_previousWindow = nullptr;
639 }
640
641 if (m_closingAllApps) {
642 if (m_windowModel.isEmpty()) {
643 Q_EMIT closedAllWindows();
644 }
645 }
646
647 DEBUG_MSG << " after " << toString() << " apps left " << m_windowModel.count();
648}
649
650void TopLevelWindowModel::setInputMethodWindow(Window *window)
651{
652 if (m_inputMethodWindow) {
653 qWarning("Multiple Input Method Surfaces created, removing the old one!");
654 delete m_inputMethodWindow;
655 }
656 m_inputMethodWindow = window;
657 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
658 InputMethodManager::instance()->setWindow(window);
659}
660
661void TopLevelWindowModel::removeInputMethodWindow()
662{
663 if (m_inputMethodWindow) {
664 auto surface = m_inputMethodWindow->surface();
665 if (surface) {
666 m_allSurfaces.remove(surface);
667 }
668 if (m_focusedWindow == m_inputMethodWindow) {
669 setFocusedWindow(nullptr);
670 m_focusedWindowCleared = false;
671 }
672
673 delete m_inputMethodWindow;
674 m_inputMethodWindow = nullptr;
675 Q_EMIT inputMethodSurfaceChanged(nullptr);
676 InputMethodManager::instance()->setWindow(nullptr);
677 }
678}
679
680void TopLevelWindowModel::onSurfacesRaised(const QVector<lomiriapi::MirSurfaceInterface*> &surfaces)
681{
682 DEBUG_MSG << "(" << surfaces << ")";
683 const int raiseCount = surfaces.size();
684 for (int i = 0; i < raiseCount; i++) {
685 int fromIndex = indexOf(surfaces[i]);
686 if (fromIndex != -1) {
687 move(fromIndex, 0);
688 }
689 }
690}
691
692int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
693{
694 return m_windowModel.count();
695}
696
697QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
698{
699 if (index.row() < 0 || index.row() >= m_windowModel.size())
700 return QVariant();
701
702 if (role == WindowRole) {
703 Window *window = m_windowModel.at(index.row()).window;
704 return QVariant::fromValue(window);
705 } else if (role == ApplicationRole) {
706 return QVariant::fromValue(m_windowModel.at(index.row()).application);
707 } else {
708 return QVariant();
709 }
710}
711
712QString TopLevelWindowModel::toString()
713{
714 QString str;
715 for (int i = 0; i < m_windowModel.count(); ++i) {
716 auto item = m_windowModel.at(i);
717
718 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
719 .arg(QString::number(i),
720 item.application->appId(),
721 QString::number((qintptr)item.window->surface(), 16),
722 QString::number(item.window->id()));
723
724 if (i > 0) {
725 str.append(",");
726 }
727 str.append(itemStr);
728 }
729 return str;
730}
731
732int TopLevelWindowModel::indexOf(lomiriapi::MirSurfaceInterface *surface)
733{
734 for (int i = 0; i < m_windowModel.count(); ++i) {
735 if (m_windowModel.at(i).window->surface() == surface) {
736 return i;
737 }
738 }
739 return -1;
740}
741
743{
744 for (int i = 0; i < m_windowModel.count(); ++i) {
745 if (m_windowModel[i].window->id() == id) {
746 return i;
747 }
748 }
749 return -1;
750}
751
753{
754 if (index >=0 && index < m_windowModel.count()) {
755 return m_windowModel[index].window;
756 } else {
757 return nullptr;
758 }
759}
760
761lomiriapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
762{
763 if (index >=0 && index < m_windowModel.count()) {
764 return m_windowModel[index].window->surface();
765 } else {
766 return nullptr;
767 }
768}
769
770lomiriapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
771{
772 if (index >=0 && index < m_windowModel.count()) {
773 return m_windowModel[index].application;
774 } else {
775 return nullptr;
776 }
777}
778
779int TopLevelWindowModel::idAt(int index) const
780{
781 if (index >=0 && index < m_windowModel.count()) {
782 return m_windowModel[index].window->id();
783 } else {
784 return 0;
785 }
786}
787
789{
790 if (m_modelState == IdleState) {
791 DEBUG_MSG << "(id=" << id << ") - do it now.";
792 doRaiseId(id);
793 } else {
794 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
795 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
796 // if we perform yet another model change straight away.
797 //
798 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
799 // the index is definitely within bounds.
800 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
801 }
802}
803
804void TopLevelWindowModel::doRaiseId(int id)
805{
806 int fromIndex = indexForId(id);
807 // can't raise something that doesn't exist or that it's already on top
808 if (fromIndex != -1 && fromIndex != 0) {
809 auto surface = m_windowModel[fromIndex].window->surface();
810 if (surface && surface->live()) {
811 m_surfaceManager->raise(surface);
812 } else {
813 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
814 // miral can do about it.
815 move(fromIndex, 0);
816 }
817 }
818}
819
820void TopLevelWindowModel::setFocusedWindow(Window *window)
821{
822 if (window != m_focusedWindow) {
823 DEBUG_MSG << "(" << window << ")";
824
825 m_previousWindow = m_focusedWindow;
826
827 m_focusedWindow = window;
828 Q_EMIT focusedWindowChanged(m_focusedWindow);
829
830 if (m_previousWindow && m_previousWindow->focused() && !m_previousWindow->surface()) {
831 // do it ourselves. miral doesn't know about this window
832 m_previousWindow->setFocused(false);
833 }
834 }
835
836 // Reset
837 m_pendingActivation = false;
838}
839
840lomiriapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
841{
842 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
843}
844
846{
847 return m_focusedWindow;
848}
849
850void TopLevelWindowModel::move(int from, int to)
851{
852 if (from == to) return;
853 DEBUG_MSG << " from=" << from << " to=" << to;
854
855 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
856 QModelIndex parent;
857 /* When moving an item down, the destination index needs to be incremented
858 by one, as explained in the documentation:
859 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
860
861 Q_ASSERT(m_modelState == IdleState);
862 m_modelState = MovingState;
863
864 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
865#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
866 const auto &window = m_windowModel.takeAt(from);
867 m_windowModel.insert(to, window);
868#else
869 m_windowModel.move(from, to);
870#endif
871 endMoveRows();
872
873 Q_EMIT listChanged();
874 m_modelState = IdleState;
875
876 DEBUG_MSG << " after " << toString();
877 }
878}
879void TopLevelWindowModel::onModificationsStarted()
880{
881 m_surfaceManagerBusy = true;
882}
883
884void TopLevelWindowModel::onModificationsEnded()
885{
886 if (m_focusedWindowCleared) {
887 setFocusedWindow(nullptr);
888 }
889 // reset
890 m_focusedWindowCleared = false;
891 m_surfaceManagerBusy = false;
892}
893
894void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
895{
896 DEBUG_MSG << "(" << forbiddenId << ")";
897
898 for (int i = 0; i < m_windowModel.count(); ++i) {
899 Window *window = m_windowModel[i].window;
900 if (window->id() != forbiddenId) {
901 window->activate();
902 break;
903 }
904 }
905}
906
907void TopLevelWindowModel::refreshWindows()
908{
909 DEBUG_MSG << "()";
910 clear();
911
912 if (!m_workspace || !m_applicationManager || !m_surfaceManager) return;
913
914 m_surfaceManager->forEachSurfaceInWorkspace(m_workspace->workspace(), [this](lomiri::shell::application::MirSurfaceInterface* surface) {
915 if (surface->parentSurface()) {
916 // Wrap it in a Window so that we keep focusedWindow() up to date.
917 Window *window = createWindow(surface);
918 connect(surface, &QObject::destroyed, window, [=](){
919 window->setSurface(nullptr);
920 window->deleteLater();
921 });
922 } else {
923 if (surface->type() == Mir::InputMethodType) {
924 setInputMethodWindow(createWindow(surface));
925 } else {
926 auto *application = m_applicationManager->findApplicationWithSurface(surface);
927 if (application) {
928 prependSurface(surface, application);
929 } else {
930 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
931 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
932 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
933 Window *promptWindow = createWindow(surface);
934 connect(surface, &QObject::destroyed, promptWindow, [=](){
935 promptWindow->setSurface(nullptr);
936 promptWindow->deleteLater();
937 });
938 }
939 }
940 }
941 });
942}
943
944void TopLevelWindowModel::clear()
945{
946 DEBUG_MSG << "()";
947
948 while(m_windowModel.count() > 0) {
949 ModelEntry entry = m_windowModel.takeAt(0);
950 disconnect(entry.window, 0, this, 0);
951 delete entry.window;
952 }
953 m_allSurfaces.clear();
954 setFocusedWindow(nullptr);
955 m_focusedWindowCleared = false;
956 m_previousWindow = nullptr;
957}
958
960{
961 m_closingAllApps = true;
962 for (auto win : m_windowModel) {
963 win.window->close();
964 }
965
966 // This is done after the for loop in the unlikely event that
967 // an app starts in between this
968 if (m_windowModel.isEmpty()) {
969 Q_EMIT closedAllWindows();
970 }
971}
972
974{
975 return !m_nullWindow->focused();
976}
977
978void TopLevelWindowModel::setRootFocus(bool focus)
979{
980 DEBUG_MSG << "(" << focus << "), surfaceManagerBusy is " << m_surfaceManagerBusy;
981
982 if (m_surfaceManagerBusy) {
983 // Something else is probably being focused already, let's not add to
984 // the noise.
985 return;
986 }
987
988 if (focus) {
989 // Give focus back to previous focused window, only if null window is focused.
990 // If null window is not focused, a different app had taken the focus and we
991 // should repect that, or if a pendingActivation is going on.
992 if (m_previousWindow && !m_previousWindow->focused() && !m_pendingActivation &&
993 m_nullWindow == m_focusedWindow && m_previousWindow != m_nullWindow) {
994 m_previousWindow->activate();
995 } else if (!m_pendingActivation) {
996 // The previous window does not exist any more, focus top window.
997 activateTopMostWindowWithoutId(-1);
998 }
999 } else {
1000 if (!m_nullWindow->focused()) {
1001 m_nullWindow->activate();
1002 }
1003 }
1004}
1005
1006// Pending Activation will block refocus of previous focused window
1007// this is needed since surface activation with miral is async,
1008// and activation of placeholder is sync. This causes a race condition
1009// between placeholder and prev window. This results in prev window
1010// gets focused, as it will always be later than the placeholder.
1012{
1013 m_pendingActivation = true;
1014}
Q_INVOKABLE void raiseId(int id)
Raises the row with the given id to the top of the window stack (index == count-1).
bool rootFocus
Sets whether a user Window or "nothing" should be focused.
Q_INVOKABLE Window * windowAt(int index) const
Returns the window at the given index.
Q_INVOKABLE lomiri::shell::application::MirSurfaceInterface * surfaceAt(int index) const
Returns the surface at the given index.
Q_INVOKABLE void pendingActivation()
Sets pending activation flag.
Q_INVOKABLE int indexForId(int id) const
Returns the index where the row with the given id is located.
lomiri::shell::application::MirSurfaceInterface * inputMethodSurface
The input method surface, if any.
Q_INVOKABLE int idAt(int index) const
Returns the unique id of the element at the given index.
Q_INVOKABLE void closeAllWindows()
Closes all windows, emits closedAllWindows when done.
int count
Number of top-level surfaces in this model.
void listChanged()
Emitted when the list changes.
Q_INVOKABLE lomiri::shell::application::ApplicationInfoInterface * applicationAt(int index) const
Returns the application at the given index.
Window * focusedWindow
The currently focused window, if any.
A slightly higher concept than MirSurface.
Definition Window.h:48
int id
A unique identifier for this window. Useful for telling windows apart in a list model as they get mov...
Definition Window.h:84
void focusRequested()
Emitted when focus for this window is requested by an external party.
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:92
bool focused
Whether the surface is focused.
Definition Window.h:71
void activate()
Focuses and raises the window.
Definition Window.cpp:137
Mir::State state
State of the surface.
Definition Window.h:64