- X7S option removed (#5388)
[opentx.git] / companion / src / companion.cpp
blobee3964a1dcb961020faab8e3e3fb847f0581a7ff
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include <QApplication>
22 #include <QDateTime>
23 #include <QDir>
24 #include <QFile>
25 #include <QSplashScreen>
26 #if defined(JOYSTICKS) || defined(SIMU_AUDIO)
27 #include <SDL.h>
28 #undef main
29 #endif
31 #include "appdebugmessagehandler.h"
32 #include "customdebug.h"
33 #include "mainwindow.h"
34 #include "version.h"
35 #include "appdata.h"
36 #include "storage.h"
37 #include "translations.h"
39 #ifdef __APPLE__
40 #include <QProxyStyle>
42 class MyProxyStyle : public QProxyStyle
44 public:
45 void polish ( QWidget * w ) {
46 QMenu* mn = dynamic_cast<QMenu*>(w);
47 QPushButton* pb = dynamic_cast<QPushButton*>(w);
48 if(!(mn || pb) && !w->testAttribute(Qt::WA_MacNormalSize))
49 w->setAttribute(Qt::WA_MacSmallSize);
52 #endif
54 int main(int argc, char *argv[])
56 #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
57 /* From doc: This attribute must be set before Q(Gui)Application is constructed. */
58 QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
59 #endif
61 QApplication app(argc, argv);
62 app.setApplicationName(APP_COMPANION);
63 app.setOrganizationName(COMPANY);
64 app.setOrganizationDomain(COMPANY_DOMAIN);
65 app.setAttribute(Qt::AA_DontShowIconsInMenus, false);
67 Q_INIT_RESOURCE(companion);
69 if (AppDebugMessageHandler::instance())
70 AppDebugMessageHandler::instance()->installAppMessageHandler();
72 CustomDebug::setFilterRules();
74 if (!g.hasCurrentSettings()) {
75 QString previousVersion;
76 if (g.findPreviousVersionSettings(&previousVersion)) {
77 QMessageBox msgBox;
78 msgBox.setText(QObject::tr("We have found existing settings for Companion version: %1.\nDo you want to import them?").arg(previousVersion));
79 msgBox.setIcon(QMessageBox::Information);
80 msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
81 msgBox.setDefaultButton(QMessageBox::Yes);
82 int ret = msgBox.exec();
84 if (ret == QMessageBox::Yes)
85 g.importSettings(previousVersion);
88 g.init();
90 QStringList strl = QApplication::arguments();
91 if (strl.contains("--version")) {
92 printf("%s\n", VERSION);
93 fflush(stdout);
94 exit(0);
97 QFile dbgLog;
98 if (AppDebugMessageHandler::instance() && g.appDebugLog() && !g.appLogsDir().isEmpty() && QDir().mkpath(g.appLogsDir())) {
99 QString fn = g.appLogsDir() % "/CompanionDebug_" % QDateTime::currentDateTime().toString("yy-MM-dd_HH-mm-ss") % ".log";
100 dbgLog.setFileName(fn);
101 if (dbgLog.open(QIODevice::WriteOnly | QIODevice::Text)) {
102 AppDebugMessageHandler::instance()->addOutputDevice(&dbgLog);
106 #ifdef __APPLE__
107 app.setStyle(new MyProxyStyle);
108 #endif
110 Translations::installTranslators();
112 #if defined(JOYSTICKS) || defined(SIMU_AUDIO)
113 uint32_t sdlFlags = 0;
114 #ifdef JOYSTICKS
115 sdlFlags |= SDL_INIT_JOYSTICK;
116 #endif
117 #ifdef SIMU_AUDIO
118 sdlFlags |= SDL_INIT_AUDIO;
119 #endif
120 if (SDL_Init(sdlFlags) < 0) {
121 fprintf(stderr, "ERROR: couldn't initialize SDL: %s\n", SDL_GetError());
123 #endif
125 registerStorageFactories();
126 registerOpenTxFirmwares();
127 SimulatorLoader::registerSimulators();
129 if (g.profile[g.id()].fwType().isEmpty()){
130 g.profile[g.id()].fwType(Firmware::getDefaultVariant()->getId());
131 g.profile[g.id()].fwName("");
134 QString splashScreen;
135 splashScreen = ":/images/splash.png";
137 QPixmap pixmap = QPixmap(splashScreen);
138 QSplashScreen *splash = new QSplashScreen(pixmap);
140 Firmware::setCurrentVariant(Firmware::getFirmwareForId(g.profile[g.id()].fwType()));
142 MainWindow *mainWin = new MainWindow();
143 if (g.showSplash()) {
144 splash->show();
145 QTimer::singleShot(1000*SPLASH_TIME, splash, SLOT(close()));
146 QTimer::singleShot(1000*SPLASH_TIME, mainWin, SLOT(show()));
148 else {
149 mainWin->show();
152 int result = app.exec();
154 delete splash;
155 delete mainWin;
157 SimulatorLoader::unregisterSimulators();
158 unregisterOpenTxFirmwares();
159 unregisterStorageFactories();
161 #if defined(JOYSTICKS) || defined(SIMU_AUDIO)
162 SDL_Quit();
163 #endif
165 qDebug() << "COMPANION EXIT" << result;
167 if (dbgLog.isOpen()) {
168 AppDebugMessageHandler::instance()->removeOutputDevice(&dbgLog);
169 dbgLog.close();
172 return result;