From c73fed905a85bbb06fa0e27b7f0cbda81a9618d3 Mon Sep 17 00:00:00 2001 From: NamedKitten Date: Tue, 18 Dec 2018 16:22:17 +0000 Subject: [PATCH] [Launcher] Refactored launcher. --- src/DirectMpvPlayerBackend.cpp | 3 +- src/MpvPlayerBackend.cpp | 28 ++++++------ src/enums.hpp | 2 + src/logger.cpp | 9 ++-- src/main.cpp | 84 ++++++++++++++++++++-------------- 5 files changed, 71 insertions(+), 55 deletions(-) diff --git a/src/DirectMpvPlayerBackend.cpp b/src/DirectMpvPlayerBackend.cpp index ef4f073..d1e2944 100644 --- a/src/DirectMpvPlayerBackend.cpp +++ b/src/DirectMpvPlayerBackend.cpp @@ -140,8 +140,7 @@ DirectMpvPlayerBackend::DirectMpvPlayerBackend(QQuickItem* parent) DirectMpvPlayerBackend::~DirectMpvPlayerBackend() { printf("Shutting down...\n"); - - exit(0); + qApp->quit(); printf("MPV terminated.\n"); } diff --git a/src/MpvPlayerBackend.cpp b/src/MpvPlayerBackend.cpp index e15fc9a..d44b86e 100644 --- a/src/MpvPlayerBackend.cpp +++ b/src/MpvPlayerBackend.cpp @@ -129,7 +129,7 @@ MpvPlayerBackend::MpvPlayerBackend(QQuickItem* parent) , mpv{ mpv_create() } , mpv_gl(nullptr) { - mpvLogger->set_pattern("%^[%d-%m-%Y %T.%e][%l][%n]%v%$"); + mpvLogger->set_pattern("[%n]%v%$"); if (!mpv) throw std::runtime_error("could not create mpv context"); @@ -395,10 +395,12 @@ MpvPlayerBackend::playerCommand(const Enums::Commands& cmd, break; } - + case Enums::Commands::ForcePause: { - command(QVariantList() << "set" << "pause" << "yes"); + command(QVariantList() << "set" + << "pause" + << "yes"); break; } @@ -563,17 +565,17 @@ MpvPlayerBackend::handle_mpv_event(mpv_event* event) } case MPV_EVENT_LOG_MESSAGE: { - if (m_logging) { - struct mpv_event_log_message* msg = - (struct mpv_event_log_message*)event->data; - QString logMsg = "[" + QString(msg->prefix) + "] " + QString(msg->text); - QString msgLevel = QString(msg->level); - if (msgLevel.startsWith("d")) { - mpvLogger->debug("{}", logMsg.toUtf8().constData()); - } else if (msgLevel.startsWith("v") || msgLevel.startsWith("i")) { - mpvLogger->info("{}", logMsg.toUtf8().constData()); - } + if (m_logging) { + struct mpv_event_log_message* msg = + (struct mpv_event_log_message*)event->data; + QString logMsg = "[" + QString(msg->prefix) + "] " + QString(msg->text); + QString msgLevel = QString(msg->level); + if (msgLevel.startsWith("d")) { + mpvLogger->debug("{}", logMsg.toUtf8().constData()); + } else if (msgLevel.startsWith("v") || msgLevel.startsWith("i")) { + mpvLogger->info("{}", logMsg.toUtf8().constData()); } + } break; } diff --git a/src/enums.hpp b/src/enums.hpp index 83cdbd8..aa5b505 100644 --- a/src/enums.hpp +++ b/src/enums.hpp @@ -47,12 +47,14 @@ enum class Commands : int ForcePause = 23, }; Q_ENUM_NS(Commands) + enum class Backends : int { MpvBackend = 0, DirectMpvBackend = 1 }; Q_ENUM_NS(Backends) + } #endif diff --git a/src/logger.cpp b/src/logger.cpp index 1a89d0b..8b8f411 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -1,8 +1,8 @@ #include "logger.h" #include -#include #include +#include #include std::shared_ptr @@ -10,7 +10,8 @@ initLogger(std::string name) { QSettings settings("KittehPlayer", "KittehPlayer"); - QString logFile = settings.value("Logging/logFile", "/tmp/KittehPlayer.log").toString(); + QString logFile = + settings.value("Logging/logFile", "/tmp/KittehPlayer.log").toString(); std::vector sinks; sinks.push_back(std::make_shared()); @@ -18,9 +19,7 @@ initLogger(std::string name) logFile.toUtf8().constData())); auto console = std::make_shared(name, begin(sinks), end(sinks)); - console->set_pattern("%^[%d-%m-%Y %T.%e][%l][%n] %v%$"); - console->error(logFile.toUtf8().constData()); - + console->set_pattern("[%l][%n] %v%$"); spdlog::register_logger(console); return spdlog::get(name); diff --git a/src/main.cpp b/src/main.cpp index 76aaaca..90d55a9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -88,12 +88,13 @@ main(int argc, char* argv[]) auto launcherLogger = initLogger("launcher"); launcherLogger->info("Starting up!"); - - QString backendString; + + Enums::Backends backend; + QString defaultBackend; #ifdef DISABLE_MpvPlayerBackend - Enums::Backends backend = Enums::Backends::DirectMpvBackend; + defaultBackend = "direct-mpv"; #else - Enums::Backends backend = Enums::Backends::MpvBackend; + defaultBackend = "mpv"; #endif setenv("QT_QUICK_CONTROLS_STYLE", "Desktop", 1); QApplication app(argc, argv); @@ -101,51 +102,48 @@ main(int argc, char* argv[]) app.setOrganizationName("KittehPlayer"); app.setOrganizationDomain("namedkitten.pw"); app.setApplicationName("KittehPlayer"); - + #ifdef __linux__ catchUnixSignals({ SIGQUIT, SIGINT, SIGTERM, SIGHUP }); #endif - QSettings settings; + QString backendString = + settings.value("Backend/backend", defaultBackend).toString(); + bool checkForUpdates = settings.value("Backend/checkForUpdatesOnLaunch", false).toBool(); for (int i = 1; i < argc; ++i) { - if (!qstrcmp(argv[i], "--no-update-check")) { - checkForUpdates = false; - } - } - - if (checkForUpdates) { - QtConcurrent::run(Utils::checkForUpdates); - } - - QString backendSetting = settings.value("Backend/backend", "").toString(); - if (backendSetting.length() == 0) { + QString arg = QString(argv[i]); + if (arg.startsWith("--")) { + QStringList arguments = + arg.right(arg.length() - QString("--").length()).split(QRegExp("=")); + if (arguments.length() > 0) { + if (arguments[0] == "backend") { + if (arguments.length() > 1 && arguments[1].length() > 0) { + backendString = arguments[1]; + } else { + launcherLogger->warn("Available Backends:"); #ifndef DISABLE_MpvPlayerBackend - settings.setValue("Backend/backend", "mpv"); -#else - settings.setValue("Backend/backend", "direct-mpv"); + launcherLogger->warn("mpv: MPV new Render API backend."); #endif - } - backendString = backendSetting; - - for (int i = 1; i < argc; ++i) { - if (!qstrcmp(argv[i], "--update")) { - Utils::updateAppImage(); - } else if (!qstrcmp(argv[i], "--backend=mpv") || backendSetting == "mpv") { - backend = Enums::Backends::MpvBackend; - backendString = QString("mpv"); - } else if (!qstrcmp(argv[i], "--backend=direct-mpv") || - backendSetting == "direct-mpv") { - backendString = QString("direct-mpv"); - backend = Enums::Backends::DirectMpvBackend; + launcherLogger->warn("direct-mpv: Old deprecated opengl-cb API for " + "backwards compatibility."); + launcherLogger->warn(QString("The default is: " + defaultBackend) + .toUtf8() + .constData()); + exit(0); + } + } else if (arguments[0] == "no-update-check") { + checkForUpdates = false; + } else if (arguments[0] == "update") { + Utils::updateAppImage(); + } + } } } - launcherLogger->info("Using backend={}", backendString.toUtf8().constData()); - Utils::SetDPMS(false); QString newpath = @@ -165,6 +163,18 @@ main(int argc, char* argv[]) qmlRegisterType("player", 1, 0, "Utils"); + if (backendString == "mpv") { + backend = Enums::Backends::MpvBackend; + } else if (backendString == "direct-mpv") { + backend = Enums::Backends::DirectMpvBackend; + } else { + launcherLogger->error("Invalid backend {}.", + backendString.toUtf8().constData()); + exit(0); + } + + launcherLogger->info("Using backend={}", backendString.toUtf8().constData()); + switch (backend) { case Enums::Backends::MpvBackend: { #ifndef DISABLE_MpvPlayerBackend @@ -189,5 +199,9 @@ main(int argc, char* argv[]) QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); + if (checkForUpdates) { + QtConcurrent::run(Utils::checkForUpdates); + } + return app.exec(); }