From 289966819b657de3c1b1704555963ee77d9b60da Mon Sep 17 00:00:00 2001 From: aqua Date: Sat, 27 Apr 2024 19:18:12 +0300 Subject: Readded command line options --- src/main.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 18 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 60c3031..42acf5c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,21 +25,61 @@ int main(int argc, char **argv) spdlog::set_level(spdlog::level::debug); // Set global log level to debug #endif - init_conf(""); - - // argc, argv, allowSecondary - Browser app(argc, argv); - // set this, otherwise the webview becomes black when using a stylesheet - app.setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true); - QCommandLineParser parser; parser.addHelpOption(); + parser.addVersionOption(); parser.addPositionalArgument("url", "URLs to open"); + + // generic options + QCommandLineOption cmd_config({ "c", "config" }, "Set the configuration file.", "path"); + parser.addOption(cmd_config); + + QCommandLineOption cmd_session({ "s", "session" }, "Open the specified session.", "path"); + parser.addOption(cmd_session); + + QCommandLineOption cmd_pick_session("pick-session", "Show all available sessions and select which one to open."); + parser.addOption(cmd_pick_session); + + // Qt options + QCommandLineOption cmd_renderer("renderer", "Select the OpenGL renderer used by the application. Available options are: desktop, gles, software", "value"); + parser.addOption(cmd_renderer); + + // SingleApplication options + QCommandLineOption cmd_no_remote("no-remote", "Start a new instance that won't accept or send remote commands."); + parser.addOption(cmd_no_remote); + + { // handle command line options that need to be set before the QApplication object is created + QStringList args; + for(int i = 0; i < argc; ++i) + args.append(argv[i]); + parser.parse(args); + + // the OpenGL implementation needs to be manually set to software if it's not properly configured + if(parser.isSet(cmd_renderer)) { + const auto value = parser.value(cmd_renderer); + if(value == QLatin1String("desktop")) { + QApplication::setAttribute(Qt::AA_UseDesktopOpenGL, true); + } else if(value == QLatin1String("gles")) { + QApplication::setAttribute(Qt::AA_UseOpenGLES, true); + } else if(value == QLatin1String("software")) { + QApplication::setAttribute(Qt::AA_UseSoftwareOpenGL, true); + } else { + spdlog::error("Unknown cmd_renderer flag: {}", qUtf8Printable(value)); + } + } + } + + // set this, otherwise the webview becomes black when using a stylesheet + QApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true); + + init_conf(parser.value(cmd_config).toStdString()); + + Browser app(argc, argv); parser.process(app); const auto profile = []() { - Configuration c; - return c.value("profile.default").value(); + Configuration conf; + return conf.value("profile.default").value(); }(); QStringList urls = parser.positionalArguments(); @@ -48,8 +88,8 @@ int main(int argc, char **argv) } // if app is primary, create new sessions from received messages - if(app.isPrimary() /*&& !cmd_noRemote*/) { - QObject::connect(&app, &Browser::receivedMessage, &app, [&app](quint32 instanceId, QByteArray message) { + if(app.isPrimary() && !parser.isSet(cmd_no_remote)) { + QObject::connect(&app, &Browser::receivedMessage, &app, [&app](quint32 instanceId, const QByteArray &message) { Q_UNUSED(instanceId); JsonSession session(message); app.open(session.get()); @@ -58,28 +98,28 @@ int main(int argc, char **argv) { const auto session = [&]() { - /*if(cmd_session) { - QFile sessionJson(QString::fromStdString(args::get(cmd_session))); + if(parser.isSet(cmd_session)) { + QFile sessionJson(parser.value(cmd_session)); if(sessionJson.open(QIODevice::ReadOnly | QIODevice::Text)) { return JsonSession(sessionJson.readAll()); } } - if(cmd_pickSession) { + if(parser.isSet(cmd_pick_session)) { SessionDialog dlg; if(const auto pick = dlg.pickSession()) { return JsonSession(pick.value()); } - }*/ + } return JsonSession(profile, urls); }(); - if(app.isPrimary() /*|| cmd_noRemote */) { + if(app.isPrimary() || parser.isSet(cmd_no_remote)) { app.open(session.get()); } else { // app is secondary and not standalone - return app.sendMessage(session.serialize()); + return app.sendMessage(session.serialize()) ? EXIT_SUCCESS : EXIT_FAILURE; } } - return app.exec(); + return QApplication::exec(); } -- cgit v1.2.1