Fix doc path
[opentx.git] / companion / src / companion.cpp
blobc983c088d6ab4f59d32b7da2a12d99d2455399b0
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 "simulatorinterface.h"
37 #include "storage.h"
38 #include "translations.h"
40 #ifdef __APPLE__
41 #include <QProxyStyle>
43 class MyProxyStyle : public QProxyStyle
45 public:
46 void polish ( QWidget * w ) {
47 QMenu* mn = dynamic_cast<QMenu*>(w);
48 QPushButton* pb = dynamic_cast<QPushButton*>(w);
49 if(!(mn || pb) && !w->testAttribute(Qt::WA_MacNormalSize))
50 w->setAttribute(Qt::WA_MacSmallSize);
53 #endif
55 int main(int argc, char *argv[])
57 #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
58 /* From doc: This attribute must be set before Q(Gui)Application is constructed. */
59 QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
60 #endif
62 QApplication app(argc, argv);
63 app.setApplicationName(APP_COMPANION);
64 app.setOrganizationName(COMPANY);
65 app.setOrganizationDomain(COMPANY_DOMAIN);
66 app.setAttribute(Qt::AA_DontShowIconsInMenus, false);
68 Q_INIT_RESOURCE(companion);
70 if (AppDebugMessageHandler::instance())
71 AppDebugMessageHandler::instance()->installAppMessageHandler();
73 CustomDebug::setFilterRules();
75 if (!g.hasCurrentSettings()) {
76 QString previousVersion;
77 if (g.findPreviousVersionSettings(&previousVersion)) {
78 QMessageBox msgBox;
79 msgBox.setText(QCoreApplication::translate("Companion", "We have found existing settings for Companion version: %1.\nDo you want to import them?").arg(previousVersion));
80 msgBox.setIcon(QMessageBox::Information);
81 msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
82 msgBox.setDefaultButton(QMessageBox::Yes);
83 int ret = msgBox.exec();
85 if (ret == QMessageBox::Yes)
86 g.importSettings(previousVersion);
89 g.init();
91 QStringList strl = QApplication::arguments();
92 if (strl.contains("--version")) {
93 printf("%s\n", VERSION);
94 fflush(stdout);
95 exit(0);
98 QFile dbgLog;
99 if (AppDebugMessageHandler::instance() && g.appDebugLog() && !g.appLogsDir().isEmpty() && QDir().mkpath(g.appLogsDir())) {
100 QString fn = g.appLogsDir() % "/CompanionDebug_" % QDateTime::currentDateTime().toString("yy-MM-dd_HH-mm-ss") % ".log";
101 dbgLog.setFileName(fn);
102 if (dbgLog.open(QIODevice::WriteOnly | QIODevice::Text)) {
103 AppDebugMessageHandler::instance()->addOutputDevice(&dbgLog);
107 #ifdef __APPLE__
108 app.setStyle(new MyProxyStyle);
109 #endif
111 Translations::installTranslators();
113 #if defined(JOYSTICKS) || defined(SIMU_AUDIO)
114 uint32_t sdlFlags = 0;
115 #ifdef JOYSTICKS
116 sdlFlags |= SDL_INIT_JOYSTICK;
117 #endif
118 #ifdef SIMU_AUDIO
119 sdlFlags |= SDL_INIT_AUDIO;
120 #endif
121 if (SDL_Init(sdlFlags) < 0) {
122 fprintf(stderr, "ERROR: couldn't initialize SDL: %s\n", SDL_GetError());
124 #endif
126 registerStorageFactories();
127 registerOpenTxFirmwares();
128 SimulatorLoader::registerSimulators();
130 if (g.profile[g.id()].fwType().isEmpty()){
131 g.profile[g.id()].fwType(Firmware::getDefaultVariant()->getId());
132 g.profile[g.id()].fwName("");
135 QString splashScreen;
136 splashScreen = ":/images/splash.png";
138 QPixmap pixmap = QPixmap(splashScreen);
139 QSplashScreen *splash = new QSplashScreen(pixmap);
141 Firmware::setCurrentVariant(Firmware::getFirmwareForId(g.profile[g.id()].fwType()));
143 MainWindow *mainWin = new MainWindow();
144 if (g.showSplash()) {
145 splash->show();
146 QTimer::singleShot(1000*SPLASH_TIME, splash, SLOT(close()));
147 QTimer::singleShot(1000*SPLASH_TIME, mainWin, SLOT(show()));
149 else {
150 mainWin->show();
153 int result = app.exec();
155 delete splash;
156 delete mainWin;
158 SimulatorLoader::unregisterSimulators();
159 unregisterOpenTxFirmwares();
160 unregisterStorageFactories();
162 #if defined(JOYSTICKS) || defined(SIMU_AUDIO)
163 SDL_Quit();
164 #endif
166 qDebug() << "COMPANION EXIT" << result;
168 if (dbgLog.isOpen()) {
169 AppDebugMessageHandler::instance()->removeOutputDevice(&dbgLog);
170 dbgLog.close();
173 return result;