-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqhookermain.cpp
467 lines (426 loc) · 20.1 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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include "qhookermain.h"
#include <QDir>
#include <QFile>
#include <QSettings>
#include <QStandardPaths>
#include <QThread>
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();
qInfo() << "Waiting for MAME-compatible Network Output @ localhost:8000 ...";
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!";
} 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) {
qInfo() << "Server closing, disconnecting...";
tcpSocket.abort();
if(!gameName.isEmpty()) {
gameName.clear();
if(settings) {
delete settings;
settingsMap.clear();
}
}
if (closeOnDisconnect) {
qInfo() << "Application closing due to -c argument.";
quit();
return;
}
// in case we exit without connecting to a game (*coughFLYCASTcough*)
for(uint8_t i = 0; i < serialFoundList.count(); i++) {
if(serialPort[i].isOpen()) {
serialPort[i].write("E");
if(serialPort[i].waitForBytesWritten(2000)) {
qInfo() << "Closed port" << i+1 << QString("(%1)").arg(serialPort[i].portName());
serialPort[i].close();
} else {
qInfo() << "Sent close signal to port" << i+1 << ", but wasn't sent in time apparently!?";
}
}
}
}
}
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()
{
serialFoundList = QSerialPortInfo::availablePorts();
if (serialFoundList.isEmpty()) {
qWarning() << "No devices found! COM devices need to be found at start time.";
quit();
} else {
// Create a list to hold valid devices
QList<QSerialPortInfo> validDevices;
// Filter devices based on Vendor IDs and collect valid devices
for (const QSerialPortInfo& info : serialFoundList) {
if (info.vendorIdentifier() == 9025 || // JB
info.vendorIdentifier() == 13939 || // Props3D
info.vendorIdentifier() == 0xF143) // OpenFIRE
{
// qInfo() << "Found device @" << info.systemLocation();
validDevices.append(info);
} else {
if(!info.portName().startsWith("tty"))
qWarning() << "Unknown device found:" << info.portName();
}
}
// Print all device information
PrintDeviceInfo(validDevices);
if (validDevices.isEmpty()) {
qWarning() << "No VALID devices found! COM devices need to be found at start time.";
quit();
} else {
int maxIndex = -1;
foreach (const QSerialPortInfo &info, validDevices) {
int index = -1;
if (info.vendorIdentifier() == 0xF143) {
// For OpenFIRE devices, derive index from productId
int productId = info.productIdentifier();
if (productId == 0x1998)
// If default OpenFIRE product ID set to 0
index = 0;
else index = productId - 1;
if (index > maxIndex)
maxIndex = index;
} else {
// For non-OpenFIRE (JB or Props3D),
++maxIndex;
}
}
serialPort = new QSerialPort[maxIndex + 1];
QSet<int> assignedIndices;
bool duplicateProductIds = false;
foreach (const QSerialPortInfo &info, validDevices) {
int index = -1;
if (info.vendorIdentifier() == 0xF143) {
// OpenFIRE device
int productId = info.productIdentifier();
if (productId == 0x1998)
index = 0;
else index = productId - 1;
if (assignedIndices.contains(index)) {
duplicateProductIds = true;
qWarning() << "Duplicate Product ID"
<< productId << "found on device" << info.portName();
} else assignedIndices.insert(index);
} else {
// Non-OpenFIRE devices
// Start scanning from 0 until we find a free spot
index = 0;
while (assignedIndices.contains(index)) {
index++;
}
assignedIndices.insert(index);
}
// Safety check for array bounds
if (index >= 0 && index < (maxIndex + 1)) {
serialPort[index].setPort(info);
serialPort[index].setBaudRate(QSerialPort::Baud9600);
serialPort[index].setDataBits(QSerialPort::Data8);
serialPort[index].setParity(QSerialPort::NoParity);
serialPort[index].setStopBits(QSerialPort::OneStop);
serialPort[index].setFlowControl(QSerialPort::NoFlowControl);
qInfo() << "Assigning" << info.portName()
<< "to port no." << index + 1;
}
else {
qWarning() << "Index" << index << "out of bounds";
}
}
if (duplicateProductIds) {
qWarning() << "Matching identifiers detected. "
"Make sure to assign different USB/PID identifiers for each gun ";
}
}
}
}
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 =")) {
qInfo() << "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();
qInfo() << gameName;
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(QString(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/ini/" + gameName + ".ini"));
#else
LoadConfig(QString(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 < serialFoundList.count()) {
if(!serialPort[portNum].isOpen()) {
serialPort[portNum].open(QIODevice::WriteOnly);
// Just in case Wendies complains:
serialPort[portNum].setDataTerminalReady(true);
qInfo() << "Opened port no" << portNum+1 << QString("(%1)").arg(serialPort[portNum].portName());
} else {
qWarning() << "Waaaaait a second... Port" << portNum+1 << "is already open!";
}
}
} else if(tempBuffer[0].contains("cmw")) {
uint8_t portNum = tempBuffer[0].at(4).digitValue()-1;
if(portNum >= 0 && portNum < serialFoundList.count()) {
if(serialPort[portNum].isOpen()) {
serialPort[portNum].write(tempBuffer[0].mid(6).toLocal8Bit());
if(!serialPort[portNum].waitForBytesWritten(2000)) {
qWarning() << "Wrote to port no" << portNum+1 << ", but wasn't sent in time apparently!?";
}
} else {
qWarning() << "Requested to write to port no" << portNum+1 << ", but it's not even open yet!";
}
}
}
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) {
qInfo() << buffer[0];
}
QString func = buffer[0].left(buffer[0].indexOf(' '));
// purge the current game name if stop signal is sent
if(func == "mame_stop") {
qInfo() << "mame_stop signal received, disconnecting.";
if(!gameName.isEmpty()) {
gameName.clear();
if(settings) {
delete settings;
settingsMap.clear();
for(uint8_t i = 0; i < serialFoundList.count(); i++) {
if(serialPort[i].isOpen()) {
serialPort[i].write("E");
if(serialPort[i].waitForBytesWritten(2000)) {
qInfo() << "Closed port" << i+1;
serialPort[i].close();
} else {
qInfo() << "Sent close signal to port" << i+1 << ", but wasn't sent in time apparently!?";
}
}
}
}
}
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 < serialFoundList.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)) {
qWarning() << "Wrote to port no" << portNum+1 << ", but wasn't sent in time apparently!?";
}
}
}
}
} 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 < serialFoundList.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)) {
qWarning() << "Wrote to port no" << portNum+1 << ", but wasn't sent in time apparently!?";
}
}
}
}
}
// %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 < serialFoundList.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)) {
qWarning() << "Wrote to port no" << portNum+1 << ", but wasn't sent in time apparently!?";
}
}
}
}
}
// 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")) {
qWarning() << "Error loading file at:" << path;
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 {
qInfo() << "Loading:" << path;
}
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) {
qInfo() << "========================================";
qInfo() << "Port Name:" << info.portName();
qInfo() << "Vendor Identifier:"
<< (info.hasVendorIdentifier() ? QString::number(info.vendorIdentifier(), 16)
: "N/A");
qInfo() << "Product Identifier:"
<< (info.hasProductIdentifier() ? QString::number(info.productIdentifier(), 16)
: "N/A");
qInfo() << "========================================";
}
}