From d63dd8d081a6fcd0715cd13f26df59fb831769d2 Mon Sep 17 00:00:00 2001 From: "Iain R. Learmonth" Date: Sun, 18 Oct 2015 18:51:00 +0100 Subject: Imported Upstream version 1.5.0 --- main.cpp | 253 ++++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 203 insertions(+), 50 deletions(-) (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp index add1ab16..6475dd33 100644 --- a/main.cpp +++ b/main.cpp @@ -1,60 +1,213 @@ -#ifdef QT5 -#include -#else -#include -#endif +#include +#include +#include +#include + +#include + #include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if QT_VERSION >= 0x050200 +#include +#include +#endif + +#include "revision_utils.hpp" + +#include "SettingsGroup.hpp" +#include "TraceFile.hpp" #include "mainwindow.h" -// Multiple instances: -QSharedMemory mem_jt9; -QUuid my_uuid; -QString my_key; int main(int argc, char *argv[]) { + // Multiple instances: + QSharedMemory mem_jt9; + QApplication a(argc, argv); + try + { + setlocale (LC_NUMERIC, "C"); // ensure number forms are in + // consistent format, do this after + // instantiating QApplication so + // that GUI has correct l18n + + // Override programs executable basename as application name. + a.setApplicationName ("WSJT-X"); + a.setApplicationVersion (version ()); + + bool multiple {false}; + +#if QT_VERSION >= 0x050200 + QCommandLineParser parser; + parser.setApplicationDescription ("\nJT65A & JT9 Weak Signal Communications Program."); + auto help_option = parser.addHelpOption (); + auto version_option = parser.addVersionOption (); + + // support for multiple instances running from a single installation + QCommandLineOption rig_option (QStringList {} << "r" << "rig-name" + , a.translate ("main", "Where is for multi-instance support.") + , a.translate ("main", "rig-name")); + parser.addOption (rig_option); + + QCommandLineOption test_option (QStringList {} << "test-mode" + , a.translate ("main", "Writable files in test location. Use with caution, for testing only.")); + parser.addOption (test_option); + + if (!parser.parse (a.arguments ())) + { + QMessageBox::critical (nullptr, a.applicationName (), parser.errorText ()); + return -1; + } + else + { + if (parser.isSet (help_option)) + { + QMessageBox::information (nullptr, a.applicationName (), parser.helpText ()); + return 0; + } + else if (parser.isSet (version_option)) + { + QMessageBox::information (nullptr, a.applicationName (), a.applicationVersion ()); + return 0; + } + } + + QStandardPaths::setTestModeEnabled (parser.isSet (test_option)); + + // support for multiple instances running from a single installation + if (parser.isSet (rig_option) || parser.isSet (test_option)) + { + auto temp_name = parser.value (rig_option); + if (!temp_name.isEmpty ()) + { + if (temp_name.contains (QRegularExpression {R"([\\/,])"})) + { + std::cerr << QObject::tr ("Invalid rig name - \\ & / not allowed").toLocal8Bit ().data () << std::endl; + parser.showHelp (-1); + } + + a.setApplicationName (a.applicationName () + " - " + temp_name); + } + + if (parser.isSet (test_option)) + { + a.setApplicationName (a.applicationName () + " - test"); + } + + multiple = true; + } - QFile f("fonts.txt"); - if(f.open(QIODevice::ReadOnly)) { - QTextStream in(&f); //Example: - QString fontFamily; // helvetica - qint32 fontSize,fontWeight; // 8,50 - in >> fontFamily >> fontSize >> fontWeight; - f.close(); - QFont font; - font=QFont(fontFamily,fontSize,fontWeight); - a.setFont(font); - } - - // Create and initialize shared memory segment - // Multiple instances: generate shared memory keys with UUID - my_uuid = QUuid::createUuid(); - my_key = my_uuid.toString(); - mem_jt9.setKey(my_key); - - if(!mem_jt9.attach()) { - if (!mem_jt9.create(sizeof(jt9com_))) { - QMessageBox::critical( 0, "Error", "Unable to create shared memory segment."); - exit(1); + // disallow multiple instances with same instance key + QLockFile instance_lock {QDir {QStandardPaths::writableLocation (QStandardPaths::TempLocation)}.absoluteFilePath (a.applicationName () + ".lock")}; + instance_lock.setStaleLockTime (0); + auto ok = false; + while (!(ok = instance_lock.tryLock ())) + { + if (QLockFile::LockFailedError == instance_lock.error ()) + { + auto button = QMessageBox::question (nullptr + , QApplication::applicationName () + , QObject::tr ("Another instance may be running, try to remove stale lock file?") + , QMessageBox::Yes | QMessageBox::Retry | QMessageBox::No + , QMessageBox::Yes); + switch (button) + { + case QMessageBox::Yes: + instance_lock.removeStaleLockFile (); + break; + + case QMessageBox::Retry: + break; + + default: + throw std::runtime_error {"Multiple instances must have unique rig names"}; + } + } + } +#endif + + auto config_directory = QStandardPaths::writableLocation (QStandardPaths::ConfigLocation); + QDir config_path {config_directory}; // will be "." if config_directory is empty + if (!config_path.mkpath (".")) + { + throw std::runtime_error {"Cannot find a usable configuration path \"" + config_path.path ().toStdString () + '"'}; + } + + auto settings_file = config_path.absoluteFilePath (a.applicationName () + ".ini"); + QSettings settings(settings_file, QSettings::IniFormat); + if (!settings.isWritable ()) + { + throw std::runtime_error {QString {"Cannot access \"%1\" for writing"}.arg (settings_file).toStdString ()}; + } + +#if WSJT_QDEBUG_TO_FILE + // // open a trace file + TraceFile trace_file {QDir {QStandardPaths::writableLocation (QStandardPaths::TempLocation)}.absoluteFilePath (a.applicationName () + "_trace.log")}; + + // announce to trace file + qDebug () << program_title (revision ()) + " - Program startup"; +#endif + + // Create and initialize shared memory segment + // Multiple instances: use rig_name as shared memory key + mem_jt9.setKey(a.applicationName ()); + + if(!mem_jt9.attach()) { + if (!mem_jt9.create(sizeof(jt9com_))) { + QMessageBox::critical (nullptr, "Error", "Unable to create shared memory segment."); + exit(1); + } + } + char *to = (char*)mem_jt9.data(); + int size=sizeof(jt9com_); + if(jt9com_.newdat==0) { + } + memset(to,0,size); //Zero all decoding params in shared memory + + unsigned downSampleFactor; + { + SettingsGroup {&settings, "Tune"}; + + // deal with Windows Vista and earlier input audio rate + // converter problems + downSampleFactor = settings.value ("Audio/DisableInputResampling", +#if defined (Q_OS_WIN) + // default to true for + // Windows Vista and older + QSysInfo::WV_VISTA >= QSysInfo::WindowsVersion ? true : false +#else + false +#endif + ).toBool () ? 1u : 4u; + } + + MainWindow w(multiple, &settings, &mem_jt9, downSampleFactor); + w.show(); + + QObject::connect (&a, SIGNAL (lastWindowClosed()), &a, SLOT (quit())); + return a.exec(); + } + catch (std::exception const& e) + { + QMessageBox::critical (nullptr, a.applicationName (), e.what ()); + std::cerr << "Error: " << e.what () << '\n'; + } + catch (...) + { + QMessageBox::critical (nullptr, a.applicationName (), QObject::tr ("Unexpected error")); + std::cerr << "Unexpected error\n"; + throw; // hoping the runtime might tell us more about the exception } - } - char *to = (char*)mem_jt9.data(); - int size=sizeof(jt9com_); - if(jt9com_.newdat==0) { - } - memset(to,0,size); //Zero all decoding params in shared memory - - //Initialize Portaudio - PaError paerr=Pa_Initialize(); - if(paerr!=paNoError) { - QMessageBox::critical( 0, "Error", "Unable to initialize PortAudio."); - exit(1); - } - -// Multiple instances: Call MainWindow() with the UUID key - MainWindow w(&mem_jt9, &my_key); - w.show(); - return a.exec(); + return -1; } -- cgit v1.2.3