summaryrefslogtreecommitdiffstats
path: root/src/engine.cpp
blob: c29a61c8fd48139e2d4ce6ce494870998e75fdb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt for Device Creation.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "engine.h"
#include "fpscounter.h"

#include <QFile>
#include <QTimerEvent>
#include <QDebug>

#include <QGuiApplication>
#include <QScreen>

#include <QQmlEngine>

#include <QQuickItem>
#include <QQuickWindow>


#define ENGINE_STATE_RUNNING QStringLiteral("running")
#define ENGINE_STATE_SETTINGS QStringLiteral("settings")

#define ENGINE_STATE_APPLAUNCHING QStringLiteral("app-launching")
#define ENGINE_STATE_APPRUNNING QStringLiteral("app-running")
#define ENGINE_STATE_APPCLOSING QStringLiteral("app-closing")

Engine::Engine(QQuickItem *parent)
    : QQuickItem(parent)
    , m_fpsCounter(0)
    , m_fps(0)
    , m_intro_done(false)
    , m_apps_ready(false)
    , m_fps_enabled(false)
    , m_glAvailable(checkForGlAvailability())
{
    m_state = ENGINE_STATE_RUNNING;

    QScreen *screen = QGuiApplication::primaryScreen();
    m_screenSize = screen->size();
    m_dpcm = screen->physicalDotsPerInchY() / 2.54f;

    // Make the buttons smaller for smaller screens to compensate for that
    // one typically holds it nearer to the eyes.
    float low = 5;
    float high = 20;
    float screenSizeCM = qMax<float>(qMin(m_screenSize.width(), m_screenSize.height()) / m_dpcm, low);
    m_dpcm *= (screenSizeCM - low) / (high - low) * 0.5 + 0.5;
    m_screenWidth = m_screenSize.width();
    m_screenHeight = m_screenSize.height();

    connect(this, SIGNAL(windowChanged(QQuickWindow*)), this, SLOT(windowChanged(QQuickWindow*)));
}

bool Engine::checkForGlAvailability()
{
    QQuickWindow window;
    return ((window.sceneGraphBackend() != "software") &&
            (window.sceneGraphBackend() != "softwarecontext"));
}

void Engine::updateReadyness()
{
    if (!m_intro_done)
        return;
    if (!m_apps_ready)
        return;

    m_state = ENGINE_STATE_RUNNING;
    emit stateChanged(m_state);
}

void Engine::updateFPSCounter()
{
    if (m_fpsCounter) {
        delete m_fpsCounter;
        m_fpsCounter = 0;
    }

    if (m_fps_enabled && window()) {
        m_fpsCounter = new FpsCounter(window());
        connect(m_fpsCounter, SIGNAL(fps(qreal)), this, SLOT(setFps(qreal)));
    }
}

void Engine::setState(const QString &state)
{
    if (state == m_state)
        return;
    m_state = state;
    emit stateChanged(m_state);
}

int Engine::titleBarSize() const
{
    return int(QGuiApplication::primaryScreen()->physicalDotsPerInch() / 2.54f);
}

void Engine::setFps(qreal fps)
{
    fps = qRound(fps);
    if (qFuzzyCompare(m_fps, fps))
        return;
    m_fps = fps;
    emit fpsChanged(m_fps);
}

void Engine::windowChanged(QQuickWindow *window)
{
    Q_UNUSED(window)
    //When window changes we need to recreate the fps counters
    updateFPSCounter();
}

void Engine::setFpsEnabled(bool enabled)
{
    if (m_fps_enabled == enabled)
        return;
    m_fps_enabled = enabled;

    updateFPSCounter();

    emit fpsEnabledChanged(m_fps_enabled);
}


int Engine::sensibleButtonSize() const
{
    // 3cm buttons, nice and big...
    int buttonSize = int(m_dpcm * 3);

    int baseSize = qMin(m_screenSize.width(), m_screenSize.height());

    // Clamp buttonSize to screen..
    if (buttonSize > baseSize)
        buttonSize = baseSize;

    return buttonSize;
}

bool Engine::fileExists(const QUrl &fileName)
{
    QFile file(fileName.toLocalFile());
    return file.exists();
}

void Engine::launchApplication(const QUrl &path, const QString &mainFile, const QString &name, const QString &desc)
{
    // only launch apps when in the homescreen...
    if (m_state != QStringLiteral("running"))
        return;

    m_applicationMain = m_applicationUrl = path;
    m_applicationMain.setPath(path.path() + "/" + mainFile);
    m_applicationName = name;
    m_applicationDescription = desc;
    emit applicationUrlChanged(m_applicationUrl);
    emit applicationMainChanged(m_applicationMain);
    emit applicationNameChanged(m_applicationName);
    emit applicationDescriptionChanged(m_applicationName);
    setState(ENGINE_STATE_APPLAUNCHING);
}

void Engine::closeApplication()
{
    emit activeIconChanged(0);

    m_applicationMain = m_applicationUrl = QUrl();
    m_applicationName = QString();
    m_applicationDescription = QString();
    emit applicationUrlChanged(m_applicationUrl);
    emit applicationMainChanged(m_applicationMain);
    emit applicationNameChanged(m_applicationName);
    emit applicationDescriptionChanged(m_applicationName);

    setState(ENGINE_STATE_RUNNING);
}