-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqhookermain.cpp
451 lines (393 loc) · 19.4 KB
/
qhookermain.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include "qhookermain.h"
#include <QDir>
#include <QFile>
#include <QSettings>
#include <QStandardPaths>
#include <QThread>
// TODO: core app stuff isn't really needed, looks very messy.
qhookerMain::qhookerMain(QObject *parent)
: QObject{parent}
{
// get the instance of the main application
mainApp = QCoreApplication::instance();
}
void qhookerMain::run()
{
//qDebug() << "Main app is running!";
//connect(&tcpSocket, &QAbstractSocket::readyRead, this, &qhookerMain::ReadyRead);
//connect(&tcpSocket, &QAbstractSocket::errorOccurred, this, &qhookerMain::errorOccurred);
SerialInit();
printf("Waiting for MAME-compatible Network Output @ localhost:8000 ...\n");
for(;;) {
switch(tcpSocket.state()) { // oh, bite me QT designer--these two are the only ones I need. :/
case QAbstractSocket::UnconnectedState:
tcpSocket.connectToHost("localhost", 8000);
if(tcpSocket.waitForConnected(5000))
qInfo() << "Connected to output server instance!\n";
else QThread::sleep(1);
break;
case QAbstractSocket::ConnectedState:
while(tcpSocket.state() == QAbstractSocket::ConnectedState) {
// in case of emergency for wendies, set to (+)1 instead
// possible performance implications here?
#ifdef Q_OS_WIN
if(tcpSocket.waitForReadyRead(1)) {
#else
if(tcpSocket.waitForReadyRead(-1)) {
#endif // Q_OS_WIN
while(!tcpSocket.atEnd())
ReadyRead();
// Apparently wendies maybe possibly might make false positives here,
// so check if the error is actually the host being closed, to at least stop it from ending early.
} else if(tcpSocket.error() == QAbstractSocket::RemoteHostClosedError) {
printf("Server closing, disconnecting...\n");
tcpSocket.abort();
if(!gameName.isEmpty()) {
gameName.clear();
if(settings) {
delete settings;
settingsMap.clear();
}
}
if (closeOnDisconnect) {
printf("Application closing due to -c argument.\n");
quit();
return;
}
// in case we exit without connecting to a game (*coughFLYCASTcough*)
for(uint8_t i = 0; i < validDevices.count(); i++) {
if(serialPort[i].isOpen()) {
serialPort[i].write("E");
if(serialPort[i].waitForBytesWritten(2000)) {
printf("Closed port %d (%s)\n", i+1, serialPort[i].portName().toLocal8Bit().constData());
serialPort[i].close();
} else {
printf("Sent close signal to port %d, but wasn't sent in time apparently!?\n", i+1);
}
}
}
}
}
break;
}
}
}
void qhookerMain::quit()
{
emit finished();
}
// shortly after quit is called the CoreApplication will signal this routine
// this is a good place to delete any objects that were created in the
// constructor and/or to stop any threads
void qhookerMain::aboutToQuitApp()
{
// stop threads
// sleep(1); // wait for threads to stop.
// delete any objects
}
void qhookerMain::SerialInit()
{
QList<QSerialPortInfo> serialFoundList = QSerialPortInfo::availablePorts();
if (serialFoundList.isEmpty()) {
printf("No devices found! COM devices need to be found at start time.\n");
quit();
} else {
// Filter devices based on Vendor IDs (JB = 9025, Props3D = 13939, OpenFIRE = 0xF143)
for (const QSerialPortInfo &info : serialFoundList) {
if(info.vendorIdentifier() == 9025 || // JB
info.vendorIdentifier() == 13939 || // Props3D
info.vendorIdentifier() == 0xF143) // OpenFIRE
validDevices.append(info);
else if(!info.portName().startsWith("ttyS"))
printf("Unknown device found: %s\n", info.portName().toLocal8Bit().constData());
}
// Print all device information
PrintDeviceInfo(validDevices);
if (validDevices.isEmpty()) {
printf("No VALID devices found! COM devices need to be found at start time.\n");
quit();
} else {
// Sort valid devices by Product ID ascending
std::sort(validDevices.begin(), validDevices.end(),
[](const QSerialPortInfo &a, const QSerialPortInfo &b) {
return a.productIdentifier() < b.productIdentifier();
});
// Create our array of QSerialPorts, sized to the number of valid devices
serialPort = new QSerialPort[validDevices.size()];
// Keep track of assigned PIDs and check for duplicates
QSet<quint16> assignedPids;
bool duplicateProductIds = false;
// Assign indices (ports) in sorted order (lowest PID → highest PID)
for (int i = 0; i < validDevices.size(); ++i) {
const QSerialPortInfo &info = validDevices[i];
// Check for duplicates
if(assignedPids.contains(info.productIdentifier())) {
duplicateProductIds = true;
printf("Duplicate Product ID %04X found on device %s\n",
info.productIdentifier(), info.portName().toLocal8Bit().constData());
} else assignedPids.insert(info.productIdentifier());
// Now simply assign i as the index for this device
// (port #1 for i=0, port #2 for i=1, etc.)
serialPort[i].setPort(info);
serialPort[i].setBaudRate(QSerialPort::Baud9600);
serialPort[i].setDataBits(QSerialPort::Data8);
serialPort[i].setParity(QSerialPort::NoParity);
serialPort[i].setStopBits(QSerialPort::OneStop);
serialPort[i].setFlowControl(QSerialPort::NoFlowControl);
printf("Assigning %s (%04X:%04X) to port no. %d\n",
info.portName().toLocal8Bit().constData(), info.vendorIdentifier(), info.productIdentifier(), i+1);
}
if (duplicateProductIds) {
printf("Matching identifiers detected.\n"
"To get consistent port allocations, each gun should have differentiating Product IDs.\n");
}
}
}
}
bool qhookerMain::GameSearching(QString input)
{
if(buffer.isEmpty()) {
// Split the output in case of connecting mid-way.
buffer = input.split('\r', Qt::SkipEmptyParts);
}
//qDebug() << buffer;
while(!buffer.isEmpty()) {
buffer[0] = buffer[0].trimmed();
// flycast outputs its start signal with code "game" using a game's full title instead of a mame zip name
if(buffer[0].startsWith("mame_start =") || buffer[0].startsWith("game =")) {
printf("Detected game name: ");
// flycast (standalone) ALSO doesn't disconnect at any point,
// so we terminate and unload any existing settings if a new gameStart is found while a game is already loaded.
if(!gameName.isEmpty()) {
gameName.clear();
if(settings) {
delete settings;
settingsMap.clear();
}
}
gameName = buffer[0].mid(input.indexOf('=')+2).trimmed().toLocal8Bit();
printf("%s\n", gameName.constData());
if(gameName != "___empty") {
if(customPathSet) {
LoadConfig(customPath + gameName + ".ini");
} else {
// TODO: there might be a better path for this? Trying to prevent "../QMamehook/QMamehook/ini" on Windows here.
#ifdef Q_OS_WIN
LoadConfig(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/ini/" + gameName + ".ini");
#else
LoadConfig(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/QMamehook/ini/" + gameName + ".ini");
#endif
}
if(settings->contains("MameStart")) {
//qInfo() << "Detected start statement:";
QStringList tempBuffer = settings->value("MameStart").toStringList();
//qInfo() << tempBuffer;
while(!tempBuffer.isEmpty()) {
if(tempBuffer[0].contains("cmo")) {
// open serial port at number (index(4))
uint8_t portNum = tempBuffer[0].at(4).digitValue()-1;
if(portNum >= 0 && portNum < validDevices.count()) {
if(!serialPort[portNum].isOpen()) {
serialPort[portNum].open(QIODevice::WriteOnly);
// Just in case Wendies complains:
serialPort[portNum].setDataTerminalReady(true);
printf("Opened port no. %d (%s)\n", portNum+1, serialPort[portNum].portName().toLocal8Bit().constData());
} else {
printf("Waaaaait a second... Port %d is already open!\n", portNum+1);
}
}
} else if(tempBuffer[0].contains("cmw")) {
uint8_t portNum = tempBuffer[0].at(4).digitValue()-1;
if(portNum >= 0 && portNum < validDevices.count()) {
if(serialPort[portNum].isOpen()) {
serialPort[portNum].write(tempBuffer[0].mid(6).toLocal8Bit());
if(!serialPort[portNum].waitForBytesWritten(2000))
printf("Wrote to port no. %d, but wasn't sent in time apparently!?\n", portNum+1);
} else printf("Requested to write to port no. %d, but it's not even open yet!\n", portNum+1);
}
}
tempBuffer.removeFirst();
}
}
buffer.removeFirst();
return true;
} else {
gameName.clear();
}
}
buffer.removeFirst();
}
return false;
}
bool qhookerMain::GameStarted(QString input)
{
if(buffer.isEmpty())
buffer = input.split('\r', Qt::SkipEmptyParts);
while(!buffer.isEmpty()) {
buffer[0] = buffer[0].trimmed();
if(verbosity)
printf("%s\n", buffer[0].toLocal8Bit().constData());
QString func = buffer[0].left(buffer[0].indexOf(' '));
// purge the current game name if stop signal is sent
if(func == "mame_stop") {
printf("mame_stop signal received, disconnecting.\n");
if(!gameName.isEmpty()) {
gameName.clear();
if(settings) {
delete settings;
settingsMap.clear();
for(uint8_t i = 0; i < validDevices.count(); i++) {
if(serialPort[i].isOpen()) {
serialPort[i].write("E");
if(serialPort[i].waitForBytesWritten(2000)) {
printf("Closed port %d\n", i+1);
serialPort[i].close();
} else printf("Sent close signal to port %d, but wasn't sent in time apparently!?\n", i+1);
}
}
}
}
buffer.clear();
return true;
// checking if a command for this input channel exists
} else if(!settingsMap[func].isEmpty()) {
//qDebug() << "Hey, this one isn't empty!"; // testing
//qDebug() << settingsMap[func]; // testing
if(settingsMap[func].contains('|')) {
if(buffer[0].right(1).toInt()) {
// right is for 1. Does not need replacement, but ignore "nul"
QStringList action = settingsMap[func].mid(settingsMap[func].indexOf('|')+1).split(',', Qt::SkipEmptyParts);
for(uint8_t i = 0; i < action.length(); i++) {
if(action[i].contains("cmw")) {
uint8_t portNum = action[i].at(action[i].indexOf("cmw")+4).digitValue()-1;
if(portNum >= 0 && portNum < validDevices.count()) {
// if contains %s%, s needs to be replaced by state.
// yes, even here, in case of stupid.
if(action[i].contains("%s%"))
action[i] = action[i].replace("%s%", "%1").arg(1);
serialPort[portNum].write(action[i].mid(action[i].indexOf("cmw")+6).toLocal8Bit());
if(!serialPort[portNum].waitForBytesWritten(2000))
printf("Wrote to port no. %d, but wasn't sent in time apparently!?\n", portNum+1);
}
}
}
} else {
// left is for 0. Does not need replacement, but ignore "nul"
QStringList action = settingsMap[func].left(settingsMap[func].indexOf('|')).split(',', Qt::SkipEmptyParts);
for(uint8_t i = 0; i < action.length(); i++) {
if(action[i].contains("cmw")) {
// we can safely assume that "cmw" on the left side will always be at a set place.
uint8_t portNum = action[i].at(action[i].indexOf("cmw")+4).digitValue()-1;
if(portNum >= 0 && portNum < validDevices.count()) {
// if contains %s%, s needs to be replaced by state.
// yes, even here, in case of stupid.
if(action[i].contains("%s%"))
action[i] = action[i].replace("%s%", "%1").arg(0);
serialPort[portNum].write(action[i].mid(action[i].indexOf("cmw")+6).toLocal8Bit());
if(!serialPort[portNum].waitForBytesWritten(2000))
printf("Wrote to port no. %d, but wasn't sent in time apparently!?\n", portNum+1);
}
}
}
}
// %s% wildcards: just replace with the number received
} else {
QStringList action = settingsMap[func].split(',', Qt::SkipEmptyParts);
for(uint8_t i = 0; i < action.length(); i++) {
if(action[i].contains("cmw")) {
// we can safely assume that "cmw" will always be at a set place.
uint8_t portNum = action[i].at(action[i].indexOf("cmw")+4).digitValue()-1;
if(portNum >= 0 && portNum < validDevices.count()) {
// if contains %s%, s needs to be replaced by state.
if(action[i].contains("%s%"))
action[i] = action[i].replace("%s%", "%1").arg(buffer[0].mid(buffer[0].indexOf('=')+2).toInt());
serialPort[portNum].write(action[i].mid(action[i].indexOf("cmw")+6).toLocal8Bit());
if(!serialPort[portNum].waitForBytesWritten(2000))
printf("Wrote to port no. %d, but wasn't sent in time apparently!?\n", portNum+1);
}
}
}
}
// if setting does not exist, register it
} else if(!settings->contains(func)) {
settings->beginGroup("Output");
settings->setValue(func, "");
settingsMap[func] = "";
settings->endGroup();
}
// then finally:
buffer.removeFirst();
}
return false;
}
void qhookerMain::ReadyRead()
{
buffer.clear();
if(gameName.isEmpty()) {
// if this returns early as true, then zip straight into the GameStarted function with the remaining buffer.
if(GameSearching(tcpSocket.readLine())) {
GameStarted("");
}
} else {
if(GameStarted(tcpSocket.readLine())) {
GameSearching("");
}
}
}
void qhookerMain::LoadConfig(QString path)
{
settings = new QSettings(path, QSettings::IniFormat);
if(!settings->contains("MameStart")) {
printf("Error loading file at: %s\n", path.toLocal8Bit().constData());
if(!QFile::exists(path) && !path.contains("__empty")) {
settings->setValue("MameStart", "");
settings->setValue("MameStop", "");
settings->setValue("StateChange", "");
settings->setValue("OnRotate", "");
settings->setValue("OnPause", "");
settings->setValue("KeyStates/RefreshTime", "");
}
} else printf("Loading: %s\n", path.toLocal8Bit().constData());
settings->beginGroup("Output");
QStringList settingsTemp = settings->childKeys();
for(uint8_t i = 0; i < settingsTemp.length(); i++) {
// QSettings splits anything with a comma, so we have to stitch the Q-splitted value back together.
if(settings->value(settingsTemp[i]).type() == QVariant::StringList)
settingsMap[settingsTemp[i]] = settings->value(settingsTemp[i]).toStringList().join(",");
else settingsMap[settingsTemp[i]] = settings->value(settingsTemp[i]).toString();
}
settings->endGroup();
}
void qhookerMain::PrintDeviceInfo(const QList<QSerialPortInfo> &devices)
{
for (const QSerialPortInfo &info : devices) {
printf("========================================\n");
printf("Port Name: %s\n", info.portName().toLocal8Bit().constData());
printf("Vendor Identifier: ");
if(info.hasVendorIdentifier()) {
printf("%04X", info.vendorIdentifier());
switch(info.vendorIdentifier()) {
case 9025:
printf(" (GUN4IR Lightgun)\n");
break;
case 13939:
printf(" (Blamcon Lightgun)\n");
break;
case 0xF143:
printf(" (OpenFIRE Lightgun)\n");
break;
default:
// unlikely to happen due to whitelisting, but just in case.
printf("\n");
break;
}
} else printf("N/A\n");
printf("Product Identifier: ");
if(info.hasProductIdentifier())
printf("%04X\n", info.productIdentifier());
else printf("N/A\n");
if(!info.manufacturer().isEmpty() && !info.description().isEmpty())
printf("Device: %s %s\n", info.manufacturer().toLocal8Bit().constData(), info.description().toLocal8Bit().constData());
printf("========================================\n");
}
}