~alan-griffiths/miral/fix-1621917

« back to all changes in this revision

Viewing changes to miral-qt/src/modules/Unity/Application/mirsurfacemanager.cpp

  • Committer: Alan Griffiths
  • Date: 2016-09-09 13:28:01 UTC
  • mfrom: (320.2.3 miral)
  • Revision ID: alam@octopull.co.uk-20160909132801-1rmrc2tl74qrhhkl
mergeĀ :parent

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2013-2016 Canonical, Ltd.
3
 
 *
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.
7
 
 *
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.
12
 
 *
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/>.
15
 
 */
16
 
 
17
 
#include "mirsurfacemanager.h"
18
 
 
19
 
// Qt
20
 
#include <QGuiApplication>
21
 
#include <QMutexLocker>
22
 
 
23
 
// local
24
 
#include "mirsurface.h"
25
 
#include "sessionmanager.h"
26
 
#include "application_manager.h"
27
 
#include "tracepoints.h" // generated from tracepoints.tp
28
 
 
29
 
// common
30
 
#include <debughelpers.h>
31
 
 
32
 
// QPA mirserver
33
 
#include "nativeinterface.h"
34
 
#include "sessionlistener.h"
35
 
#include "logging.h"
36
 
#include "creationhints.h"
37
 
 
38
 
// mir
39
 
#include <mir/scene/surface.h>
40
 
 
41
 
 
42
 
namespace ms = mir::scene;
43
 
 
44
 
namespace qtmir {
45
 
 
46
 
MirSurfaceManager *MirSurfaceManager::instance = nullptr;
47
 
 
48
 
 
49
 
void connectToSessionListener(MirSurfaceManager *manager, SessionListener *listener)
50
 
{
51
 
    QObject::connect(listener, &SessionListener::sessionCreatedSurface,
52
 
                     manager, &MirSurfaceManager::onSessionCreatedSurface);
53
 
    QObject::connect(listener, &SessionListener::sessionDestroyingSurface,
54
 
                     manager, &MirSurfaceManager::onSessionDestroyingSurface);
55
 
}
56
 
 
57
 
MirSurfaceManager* MirSurfaceManager::singleton()
58
 
{
59
 
    if (!instance) {
60
 
 
61
 
        NativeInterface *nativeInterface = dynamic_cast<NativeInterface*>(QGuiApplication::platformNativeInterface());
62
 
 
63
 
        if (!nativeInterface) {
64
 
            qCritical("ERROR: Unity.Application QML plugin requires use of the 'mirserver' QPA plugin");
65
 
            QGuiApplication::quit();
66
 
            return nullptr;
67
 
        }
68
 
 
69
 
        SessionListener *sessionListener = static_cast<SessionListener*>(nativeInterface->nativeResourceForIntegration("SessionListener"));
70
 
        mir::shell::Shell *shell = static_cast<mir::shell::Shell*>(nativeInterface->nativeResourceForIntegration("Shell"));
71
 
 
72
 
        instance = new MirSurfaceManager(shell, SessionManager::singleton());
73
 
 
74
 
        connectToSessionListener(instance, sessionListener);
75
 
    }
76
 
    return instance;
77
 
}
78
 
 
79
 
MirSurfaceManager::MirSurfaceManager(
80
 
        mir::shell::Shell* shell,
81
 
        SessionManager* sessionManager,
82
 
        QObject* parent)
83
 
    : QObject(parent)
84
 
    , m_shell(shell)
85
 
    , m_sessionManager(sessionManager)
86
 
{
87
 
    qCDebug(QTMIR_SURFACES) << "MirSurfaceManager::MirSurfaceManager - this=" << this;
88
 
    setObjectName(QStringLiteral("qtmir::SurfaceManager"));
89
 
}
90
 
 
91
 
MirSurfaceManager::~MirSurfaceManager()
92
 
{
93
 
    qCDebug(QTMIR_SURFACES) << "MirSurfaceManager::~MirSurfaceManager - this=" << this;
94
 
 
95
 
    m_mirSurfaceToQmlSurfaceHash.clear();
96
 
}
97
 
 
98
 
void MirSurfaceManager::onSessionCreatedSurface(const mir::scene::Session *mirSession,
99
 
                                                const std::shared_ptr<mir::scene::Surface> &surface,
100
 
                                                const std::shared_ptr<SurfaceObserver> &/*observer*/,
101
 
                                                const qtmir::CreationHints &creationHints)
102
 
{
103
 
    qCDebug(QTMIR_SURFACES) << "MirSurfaceManager::onSessionCreatedSurface - mirSession=" << mirSession
104
 
                            << "surface=" << surface.get() << "surface.name=" << surface->name().c_str()
105
 
                            << "creationHints=" << creationHints.toString();
106
 
 
107
 
//    SessionInterface* session = m_sessionManager->findSession(mirSession);
108
 
//    auto qmlSurface = new MirSurface(surface, session, m_shell, observer, creationHints);
109
 
//    {
110
 
//        QMutexLocker lock(&m_mutex);
111
 
//        m_mirSurfaceToQmlSurfaceHash.insert(surface.get(), qmlSurface);
112
 
//    }
113
 
 
114
 
//    if (session)
115
 
//        session->registerSurface(qmlSurface);
116
 
 
117
 
//    if (qmlSurface->type() == Mir::InputMethodType) {
118
 
//        m_inputMethodSurface = qmlSurface;
119
 
//        Q_EMIT inputMethodSurfaceChanged();
120
 
//    }
121
 
 
122
 
//    // Only notify QML of surface creation once it has drawn its first frame.
123
 
//    connect(qmlSurface, &MirSurfaceInterface::firstFrameDrawn, this, [=]() {
124
 
//        tracepoint(qtmir, firstFrameDrawn);
125
 
//        Q_EMIT surfaceCreated(qmlSurface);
126
 
//    });
127
 
 
128
 
//    // clean up after MirSurface is destroyed
129
 
//    connect(qmlSurface, &QObject::destroyed, this, [&](QObject *obj) {
130
 
//        auto qmlSurface = static_cast<MirSurfaceInterface*>(obj);
131
 
//        {
132
 
//            QMutexLocker lock(&m_mutex);
133
 
//            m_mirSurfaceToQmlSurfaceHash.remove(m_mirSurfaceToQmlSurfaceHash.key(qmlSurface));
134
 
//        }
135
 
 
136
 
//        tracepoint(qtmir, surfaceDestroyed);
137
 
//    });
138
 
//    tracepoint(qtmir, surfaceCreated);
139
 
}
140
 
 
141
 
void MirSurfaceManager::onSessionDestroyingSurface(const mir::scene::Session *session,
142
 
                                                   const std::shared_ptr<mir::scene::Surface> &surface)
143
 
{
144
 
    qCDebug(QTMIR_SURFACES) << "MirSurfaceManager::onSessionDestroyingSurface - session=" << session
145
 
                            << "surface=" << surface.get() << "surface.name=" << surface->name().c_str();
146
 
 
147
 
//    MirSurfaceInterface* qmlSurface = nullptr;
148
 
//    {
149
 
//        QMutexLocker lock(&m_mutex);
150
 
//        auto it = m_mirSurfaceToQmlSurfaceHash.find(surface.get());
151
 
//        if (it != m_mirSurfaceToQmlSurfaceHash.end()) {
152
 
//            qmlSurface = it.value();
153
 
//            m_mirSurfaceToQmlSurfaceHash.erase(it);
154
 
//        } else {
155
 
//            qCritical() << "MirSurfaceManager::onSessionDestroyingSurface: unable to find MirSurface corresponding"
156
 
//                        << "to surface=" << surface.get() << "surface.name=" << surface->name().c_str();
157
 
//            return;
158
 
//        }
159
 
//    }
160
 
 
161
 
//    if (qmlSurface->type() == Mir::InputMethodType) {
162
 
//        m_inputMethodSurface = nullptr;
163
 
//        Q_EMIT inputMethodSurfaceChanged();
164
 
//    }
165
 
 
166
 
//    qmlSurface->setLive(false);
167
 
//    Q_EMIT surfaceDestroyed(qmlSurface);
168
 
}
169
 
 
170
 
MirSurfaceInterface* MirSurfaceManager::inputMethodSurface() const
171
 
{
172
 
    return m_inputMethodSurface;
173
 
}
174
 
 
175
 
} // namespace qtmir