on f136e64 help sign in
unity8/plugins/WindowManager/TopLevelSurfaceList.cpp
481 lines 15 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 "TopLevelSurfaceList.h"
19// unity-api
20#include <unity/shell/application/ApplicationInfoInterface.h>
21#include <unity/shell/application/MirSurfaceInterface.h>
22#include <unity/shell/application/MirSurfaceListInterface.h>
24#include <QMetaObject>
26Q_LOGGING_CATEGORY(UNITY_TOPSURFACELIST, "unity.topsurfacelist", QtDebugMsg)
28#define DEBUG_MSG qCDebug(UNITY_TOPSURFACELIST).nospace().noquote() << __func__
30using namespace unity::shell::application;
32TopLevelSurfaceList::TopLevelSurfaceList(QObject *parent) :
33 QAbstractListModel(parent)
35 DEBUG_MSG << "()";
38TopLevelSurfaceList::~TopLevelSurfaceList()
40 DEBUG_MSG << "()";
43int TopLevelSurfaceList::rowCount(const QModelIndex &parent) const
45 return !parent.isValid() ? m_surfaceList.size() : 0;
48QVariant TopLevelSurfaceList::data(const QModelIndex& index, int role) const
50 if (index.row() < 0 || index.row() >= m_surfaceList.size())
51 return QVariant();
53 if (role == SurfaceRole) {
54 MirSurfaceInterface *surface = m_surfaceList.at(index.row()).surface;
55 return QVariant::fromValue(surface);
56 } else if (role == ApplicationRole) {
57 return QVariant::fromValue(m_surfaceList.at(index.row()).application);
58 } else if (role == IdRole) {
59 return QVariant::fromValue(m_surfaceList.at(index.row()).id);
60 } else {
61 return QVariant();
62 }
65void TopLevelSurfaceList::raise(MirSurfaceInterface *surface)
67 if (!surface)
68 return;
70 DEBUG_MSG << "(MirSurface[" << (void*)surface << "])";
72 int i = indexOf(surface);
73 if (i != -1) {
74 raiseId(m_surfaceList.at(i).id);
75 }
78void TopLevelSurfaceList::appendPlaceholder(ApplicationInfoInterface *application)
80 DEBUG_MSG << "(" << application->appId() << ")";
82 appendSurfaceHelper(nullptr, application);
85void TopLevelSurfaceList::appendSurface(MirSurfaceInterface *surface, ApplicationInfoInterface *application)
87 Q_ASSERT(surface != nullptr);
89 bool filledPlaceholder = false;
90 for (int i = 0; i < m_surfaceList.count() && !filledPlaceholder; ++i) {
91 ModelEntry &entry = m_surfaceList[i];
92 if (entry.application == application && entry.surface == nullptr) {
93 entry.surface = surface;
94 connectSurface(surface);
95 DEBUG_MSG << " appId=" << application->appId() << " surface=" << surface
96 << ", filling out placeholder. after: " << toString();
97 Q_EMIT dataChanged(index(i) /* topLeft */, index(i) /* bottomRight */, QVector<int>() << SurfaceRole);
98 filledPlaceholder = true;
99 }
100 }
102 if (!filledPlaceholder) {
103 DEBUG_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
104 appendSurfaceHelper(surface, application);
105 }
108void TopLevelSurfaceList::appendSurfaceHelper(MirSurfaceInterface *surface, ApplicationInfoInterface *application)
110 if (m_modelState == IdleState) {
111 m_modelState = InsertingState;
112 beginInsertRows(QModelIndex(), m_surfaceList.size() /*first*/, m_surfaceList.size() /*last*/);
113 } else {
114 Q_ASSERT(m_modelState == ResettingState);
115 // No point in signaling anything if we're resetting the whole model
116 }
118 int id = generateId();
119 m_surfaceList.append(ModelEntry(surface, application, id));
120 if (surface) {
121 connectSurface(surface);
122 }
124 if (m_modelState == InsertingState) {
125 endInsertRows();
126 Q_EMIT countChanged();
127 Q_EMIT listChanged();
128 m_modelState = IdleState;
129 }
131 DEBUG_MSG << " after " << toString();
134void TopLevelSurfaceList::connectSurface(MirSurfaceInterface *surface)
136 connect(surface, &MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
137 if (!live) {
138 onSurfaceDied(surface);
139 }
140 });
141 connect(surface, &QObject::destroyed, this, [this, surface](){ this->onSurfaceDestroyed(surface); });
144void TopLevelSurfaceList::onSurfaceDied(MirSurfaceInterface *surface)
146 int i = indexOf(surface);
147 if (i == -1) {
148 return;
149 }
151 auto application = m_surfaceList[i].application;
153 // can't be starting if it already has a surface
154 Q_ASSERT(application->state() != ApplicationInfoInterface::Starting);
156 if (application->state() == ApplicationInfoInterface::Running) {
157 m_surfaceList[i].removeOnceSurfaceDestroyed = true;
158 } else {
159 // assume it got killed by the out-of-memory daemon.
160 //
161 // So leave entry in the model and only remove its surface, so shell can display a screenshot
162 // in its place.
163 m_surfaceList[i].removeOnceSurfaceDestroyed = false;
164 }
167void TopLevelSurfaceList::onSurfaceDestroyed(MirSurfaceInterface *surface)
169 int i = indexOf(surface);
170 if (i == -1) {
171 return;
172 }
174 if (m_surfaceList[i].removeOnceSurfaceDestroyed) {
175 removeAt(i);
176 } else {
177 m_surfaceList[i].surface = nullptr;
178 Q_EMIT dataChanged(index(i) /* topLeft */, index(i) /* bottomRight */, QVector<int>() << SurfaceRole);
179 DEBUG_MSG << " Removed surface from entry. After: " << toString();
180 }
183void TopLevelSurfaceList::removeAt(int index)
185 if (m_modelState == IdleState) {
186 beginRemoveRows(QModelIndex(), index, index);
187 m_modelState = RemovingState;
188 } else {
189 Q_ASSERT(m_modelState == ResettingState);
190 // No point in signaling anything if we're resetting the whole model
191 }
193 m_surfaceList.removeAt(index);
195 if (m_modelState == RemovingState) {
196 endRemoveRows();
197 Q_EMIT countChanged();
198 Q_EMIT listChanged();
199 m_modelState = IdleState;
200 }
202 DEBUG_MSG << " after " << toString();
205int TopLevelSurfaceList::indexOf(MirSurfaceInterface *surface)
207 for (int i = 0; i < m_surfaceList.count(); ++i) {
208 if (m_surfaceList.at(i).surface == surface) {
209 return i;
210 }
211 }
212 return -1;
215void TopLevelSurfaceList::move(int from, int to)
217 if (from == to) return;
218 DEBUG_MSG << " from=" << from << " to=" << to;
220 if (from >= 0 && from < m_surfaceList.size() && to >= 0 && to < m_surfaceList.size()) {
221 QModelIndex parent;
222 /* When moving an item down, the destination index needs to be incremented
223 by one, as explained in the documentation:
224 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
226 Q_ASSERT(m_modelState == IdleState);
227 m_modelState = MovingState;
229 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
230 m_surfaceList.move(from, to);
231 endMoveRows();
232 Q_EMIT listChanged();
234 m_modelState = IdleState;
236 DEBUG_MSG << " after " << toString();
237 }
240MirSurfaceInterface *TopLevelSurfaceList::surfaceAt(int index) const
242 if (index >=0 && index < m_surfaceList.count()) {
243 return m_surfaceList[index].surface;
244 } else {
245 return nullptr;
246 }
249ApplicationInfoInterface *TopLevelSurfaceList::applicationAt(int index) const
251 if (index >=0 && index < m_surfaceList.count()) {
252 return m_surfaceList[index].application;
253 } else {
254 return nullptr;
255 }
258int TopLevelSurfaceList::idAt(int index) const
260 if (index >=0 && index < m_surfaceList.count()) {
261 return m_surfaceList[index].id;
262 } else {
263 return 0;
264 }
267int TopLevelSurfaceList::indexForId(int id) const
269 for (int i = 0; i < m_surfaceList.count(); ++i) {
270 if (m_surfaceList[i].id == id) {
271 return i;
272 }
273 }
274 return -1;
277void TopLevelSurfaceList::doRaiseId(int id)
279 int fromIndex = indexForId(id);
280 if (fromIndex != -1) {
281 move(fromIndex, 0 /* toIndex */);
282 }
285void TopLevelSurfaceList::raiseId(int id)
287 if (m_modelState == IdleState) {
288 DEBUG_MSG << "(id=" << id << ") - do it now.";
289 doRaiseId(id);
290 } else {
291 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
292 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
293 // if we perform yet another model change straight away.
294 //
295 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
296 // the index is definitely within bounds.
297 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
298 }
301int TopLevelSurfaceList::generateId()
303 int id = m_nextId;
304 m_nextId = nextFreeId(m_nextId + 1);
305 Q_EMIT nextIdChanged();
306 return id;
309int TopLevelSurfaceList::nextFreeId(int candidateId)
311 if (candidateId > m_maxId) {
312 return nextFreeId(1);
313 } else {
314 if (indexForId(candidateId) == -1) {
315 // it's indeed free
316 return candidateId;
317 } else {
318 return nextFreeId(candidateId + 1);
319 }
320 }
323QString TopLevelSurfaceList::toString()
325 QString str;
326 for (int i = 0; i < m_surfaceList.count(); ++i) {
327 auto item = m_surfaceList.at(i);
329 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
330 .arg(i)
331 .arg(item.application->appId())
332 .arg((qintptr)item.surface, 0, 16)
333 .arg(item.id);
335 if (i > 0) {
336 str.append(",");
337 }
338 str.append(itemStr);
339 }
340 return str;
343void TopLevelSurfaceList::addApplication(ApplicationInfoInterface *application)
345 DEBUG_MSG << "(" << application->appId() << ")";
346 Q_ASSERT(!m_applications.contains(application));
347 m_applications.append(application);
349 MirSurfaceListInterface *surfaceList = application->surfaceList();
351 if (application->state() != ApplicationInfoInterface::Stopped) {
352 if (surfaceList->count() == 0) {
353 appendPlaceholder(application);
354 } else {
355 for (int i = 0; i < surfaceList->count(); ++i) {
356 appendSurface(surfaceList->get(i), application);
357 }
358 }
359 }
361 connect(surfaceList, &QAbstractItemModel::rowsInserted, this,
362 [this, application, surfaceList](const QModelIndex & /*parent*/, int first, int last)
363 {
364 for (int i = last; i >= first; --i) {
365 this->appendSurface(surfaceList->get(i), application);
366 }
367 });
370void TopLevelSurfaceList::removeApplication(ApplicationInfoInterface *application)
372 DEBUG_MSG << "(" << application->appId() << ")";
373 Q_ASSERT(m_applications.contains(application));
375 MirSurfaceListInterface *surfaceList = application->surfaceList();
377 disconnect(surfaceList, 0, this, 0);
379 Q_ASSERT(m_modelState == IdleState);
380 m_modelState = RemovingState;
382 int i = 0;
383 while (i < m_surfaceList.count()) {
384 if (m_surfaceList.at(i).application == application) {
385 beginRemoveRows(QModelIndex(), i, i);
386 m_surfaceList.removeAt(i);
387 endRemoveRows();
388 Q_EMIT countChanged();
389 Q_EMIT listChanged();
390 } else {
391 ++i;
392 }
393 }
395 m_modelState = IdleState;
397 DEBUG_MSG << " after " << toString();
399 m_applications.removeAll(application);
402QAbstractListModel *TopLevelSurfaceList::applicationsModel() const
404 return m_applicationsModel;
407void TopLevelSurfaceList::setApplicationsModel(QAbstractListModel* value)
409 if (m_applicationsModel == value) {
410 return;
411 }
413 DEBUG_MSG << "(" << value << ")";
415 Q_ASSERT(m_modelState == IdleState);
416 m_modelState = ResettingState;
418 beginResetModel();
420 if (m_applicationsModel) {
421 m_surfaceList.clear();
422 m_applications.clear();
423 disconnect(m_applicationsModel, 0, this, 0);
424 }
426 m_applicationsModel = value;
428 if (m_applicationsModel) {
429 findApplicationRole();
431 connect(m_applicationsModel, &QAbstractItemModel::rowsInserted,
432 this, [this](const QModelIndex &/*parent*/, int first, int last) {
433 for (int i = first; i <= last; ++i) {
434 auto application = getApplicationFromModelAt(i);
435 addApplication(application);
436 }
437 });
439 connect(m_applicationsModel, &QAbstractItemModel::rowsAboutToBeRemoved,
440 this, [this](const QModelIndex &/*parent*/, int first, int last) {
441 for (int i = first; i <= last; ++i) {
442 auto application = getApplicationFromModelAt(i);
443 removeApplication(application);
444 }
445 });
447 for (int i = 0; i < m_applicationsModel->rowCount(); ++i) {
448 auto application = getApplicationFromModelAt(i);
449 addApplication(application);
450 }
451 }
453 endResetModel();
454 m_modelState = IdleState;
457ApplicationInfoInterface *TopLevelSurfaceList::getApplicationFromModelAt(int index)
459 QModelIndex modelIndex = m_applicationsModel->index(index);
461 QVariant variant = m_applicationsModel->data(modelIndex, m_applicationRole);
463 // variant.value<ApplicationInfoInterface*>() returns null for some reason.
464 return static_cast<ApplicationInfoInterface*>(variant.value<QObject*>());
467void TopLevelSurfaceList::findApplicationRole()
469 QHash<int, QByteArray> namesHash = m_applicationsModel->roleNames();
471 m_applicationRole = -1;
472 for (auto i = namesHash.begin(); i != namesHash.end() && m_applicationRole == -1; ++i) {
473 if (i.value() == "application") {
474 m_applicationRole = i.key();
475 }
476 }
478 if (m_applicationRole == -1) {
479 qFatal("TopLevelSurfaceList: applicationsModel must have a \"application\" role.");
480 }