commits
tags
#include <QDir>
#include <algorithm>
#include <QKeyEvent>
#include <QKeySequence>
#include <QTimer>
#include <QFile>
#include <QGuiApplication>
#include <QIcon>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickStyle>
#include <QQuickWindow>
#include <QtWebEngineQuick/qtwebenginequickglobal.h>
#include "appstore.h"
#include "chromiumapps.h"
#include "desktopscanner.h"
#include "iconfetcher.h"
#include "iconprovider.h"
#include "inputmanager.h"
#include "prefs.h"
#include "processlauncher.h"
#include "shell.h"
// Utan skrivbordsmiljö (cage) finns inget ikontema satt, så vi väljer ett som finns.
static void pickIconTheme()
{
if (!QIcon::themeName().isEmpty() && QIcon::themeName() != QLatin1String("hicolor"))
return;
const QStringList candidates{"breeze-dark", "breeze", "Papirus-Dark", "Papirus", "Adwaita", "hicolor"};
for (const QString &name : candidates) {
for (const QString &base : QIcon::themeSearchPaths()) {
if (QFile::exists(base + "/" + name + "/index.theme")) {
QIcon::setThemeName(name);
QIcon::setFallbackThemeName("hicolor");
return;
}
}
}
}
int main(int argc, char *argv[])
{
QCoreApplication::setApplicationName("tv-launcher");
QtWebEngineQuick::initialize();
// Genomskinliga fönster (skärmtangentbordet över andra program).
QQuickWindow::setDefaultAlphaBuffer(true);
QGuiApplication app(argc, argv);
QQuickStyle::setStyle("Basic");
pickIconTheme();
const bool windowed = app.arguments().contains("--windowed");
AppStore store;
ProcessLauncher launcher;
// I testläget (--screenshot) ska en riktig handkontroll inte störa.
const QStringList args = app.arguments();
const bool shotMode = std::any_of(args.cbegin(), args.cend(),
[](const QString &a) { return a.startsWith("--screenshot="); });
InputManager input(!shotMode);
DesktopScanner scanner;
IconFetcher fetcher;
Shell shell;
Prefs prefs;
QObject::connect(&store, &AppStore::iconWanted, &fetcher, &IconFetcher::fetch);
QObject::connect(&fetcher, &IconFetcher::iconReady, &store, &AppStore::setIconIfEmpty);
store.fetchMissingIcons();
ChromiumApps chromiumApps(&store);
chromiumApps.scan();
// När ett externt program körs ska launchern inte reagera på handkontrollen.
QObject::connect(&launcher, &ProcessLauncher::runningChanged, &input,
[&] { input.setSuspended(launcher.running()); });
QQmlApplicationEngine engine;
engine.addImageProvider("icon", new IconProvider);
engine.rootContext()->setContextProperty("apps", &store);
engine.rootContext()->setContextProperty("launcher", &launcher);
engine.rootContext()->setContextProperty("input", &input);
engine.rootContext()->setContextProperty("scanner", &scanner);
engine.rootContext()->setContextProperty("shell", &shell);
engine.rootContext()->setContextProperty("prefs", &prefs);
engine.rootContext()->setContextProperty("chromiumApps", &chromiumApps);
engine.rootContext()->setContextProperty("windowedMode", windowed);
QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app,
[] { QCoreApplication::exit(1); }, Qt::QueuedConnection);
engine.loadFromModule("TvLauncher", "Main");
if (engine.rootObjects().isEmpty())
return 1;
auto *window = qobject_cast<QQuickWindow *>(engine.rootObjects().constFirst());
app.installEventFilter(&input);
input.setWindow(window);
// Utvecklarläge: --keys=Down,Right,Menu spelar upp tangenttryck och
// --screenshot=fil.png sparar en bild av fönstret och avslutar.
QString shotPath;
QStringList keys;
for (const QString &a : app.arguments()) {
if (a.startsWith("--screenshot="))
shotPath = a.mid(13);
else if (a.startsWith("--keys="))
keys = a.mid(7).split(',', Qt::SkipEmptyParts);
}
if (!shotPath.isEmpty()) {
int delay = 2500;
for (const QString &k : std::as_const(keys)) {
QTimer::singleShot(delay, window, [window, k] {
const int code = QKeySequence::fromString(k)[0].toCombined();
QKeyEvent press(QEvent::KeyPress, code & ~Qt::KeyboardModifierMask,
Qt::KeyboardModifiers(code & Qt::KeyboardModifierMask),
k.size() == 1 ? k : QString());
QKeyEvent release(QEvent::KeyRelease, code & ~Qt::KeyboardModifierMask,
Qt::KeyboardModifiers(code & Qt::KeyboardModifierMask));
QCoreApplication::sendEvent(window, &press);
QCoreApplication::sendEvent(window, &release);
});
delay += 400;
}
QTimer::singleShot(delay + 1000, window, [window, shotPath] {
window->grabWindow().save(shotPath);
QCoreApplication::quit();
});
}
return app.exec();
}