forked from Skarsnik/QUsb2snes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
157 lines (144 loc) · 5.02 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "appui.h"
//#include "sd2snesdevice.h"
#include "wsserver.h"
#ifdef Q_OS_MACOS
#include "osx/appnap.h"
#endif
#include <QSerialPortInfo>
#include <QDebug>
#include <QApplication>
#include <QTimer>
#include <QThread>
#include <QSystemTrayIcon>
#include <QFile>
#include <QMessageBox>
#include <QMenu>
#include <QObject>
#include <QHostInfo>
#include <QVersionNumber>
WSServer wsServer;
QSettings* globalSettings;
static QTextStream logfile;
static QTextStream debugLogFile;
//static QTextStream lowlogfile;
static QTextStream cout(stdout);
//bool dontLogNext = false;
bool dontLogNext = false;
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
QTextStream* log = &logfile;
//cout << msg;
if (dontLogNext)
return ;
#ifdef QT_NO_DEBUG
QString logString = QString("%6 %5 - %7: %1").arg(localMsg.constData()).arg(context.category, 20).arg(QDateTime::currentDateTime().toString(Qt::ISODate));
#else
QString logString = QString("%6 %5 - %7: %1 \t(%2:%3, %4)").arg(localMsg.constData()).arg(context.file).arg(context.line).arg(context.function).arg(context.category, 20).arg(QDateTime::currentDateTime().toString(Qt::ISODate));
#endif
switch (type)
{
case QtDebugMsg:
break;
case QtCriticalMsg:
*log << logString.arg("Critical");
break;
case QtWarningMsg:
*log << logString.arg("Warning");
break;
case QtFatalMsg:
*log << logString.arg("Fatal");
*log<< "\n"; log->flush();
QMessageBox::critical(nullptr, QObject::tr("Critical error"), msg);
qApp->exit(1);
break;
case QtInfoMsg:
*log << logString.arg("Info");
break;
}
if (debugLogFile.device() != nullptr)
{
debugLogFile << logString.arg("Debug");
debugLogFile << "\n";
debugLogFile.flush();
}
if (type != QtDebugMsg)
{
*log << "\n";
log->flush();
}
cout << QString("%1 : %2").arg(context.category, 20).arg(msg) << "\n";
cout.flush();
}
void startServer()
{
//myTrayIcon->showMessage(QString("Webserver started"), QString("Webserver started"));
quint16 port = 8080;
QHostAddress addr = QHostInfo::fromName("localhost").addresses().first();
if (globalSettings->contains("listen"))
addr = QHostInfo::fromName(globalSettings->value("listen").toString()).addresses().first();
if (globalSettings->contains("port"))
port = globalSettings->value("port").toUInt();
wsServer.start(addr, port);
}
int main(int ac, char *ag[])
{
QApplication app(ac, ag);
QFile mlog(qApp->applicationDirPath() + "/log.txt");
QFile mDebugLog(qApp->applicationDirPath() + "/log-debug.txt");
logfile.setDevice(&mlog);
globalSettings = new QSettings("config.ini", QSettings::IniFormat);
if (globalSettings->contains("debugLog") && globalSettings->value("debugLog").toBool())
{
mDebugLog.open(QIODevice::WriteOnly | QIODevice::Text);
debugLogFile.setDevice(&mDebugLog);
}
if (mlog.open(QIODevice::WriteOnly | QIODevice::Text))
qInstallMessageHandler(myMessageOutput);
QApplication::setApplicationName("QUsb2Snes");
// This is only defined in the PRO file
#ifdef GIT_TAG_VERSION
QString plop(GIT_TAG_VERSION);
plop.remove(0, 1); // Remove the v
QApplication::setApplicationVersion(QVersionNumber::fromString(plop).toString());
#else
QApplication::setApplicationVersion("0.8");
#endif
#ifdef Q_OS_MACOS
auto appNap = new AppNapSuspender();
appNap->suspend();
#endif
// let set some know trusted domain
wsServer.addTrusted("http://www.multitroid.com");
wsServer.addTrusted("https://multiworld.samus.link/");
if (app.arguments().size() == 2 && app.arguments().at(1) == "-nogui")
{
SD2SnesFactory* sd2snesFactory = new SD2SnesFactory();
wsServer.addDeviceFactory(sd2snesFactory);
if (globalSettings->value("retroarchdevice").toBool())
{
RetroArchFactory* retroarchFactory = new RetroArchFactory();
wsServer.addDeviceFactory(retroarchFactory);
}
if (globalSettings->value("luabridge").toBool())
{
LuaBridge* luaBridge = new LuaBridge();
wsServer.addDeviceFactory(luaBridge);
}
if (globalSettings->value("snesclassic").toBool())
{
SNESClassicFactory* snesClassic = new SNESClassicFactory();
wsServer.addDeviceFactory(snesClassic);
}
QObject::connect(&wsServer, &WSServer::listenFailed, [=](const QString& err) {
fprintf(stderr, "Can't listen on localhost:8080 or the host set in the config file: %s\n", err.toLatin1().data());
qApp->exit(1);
});
QTimer::singleShot(100, &startServer);
return app.exec();
}
AppUi* appUi = new AppUi();
appUi->sysTray->show();
QTimer::singleShot(200, &startServer);
return app.exec();
}