@@ -56,9 +56,56 @@ void qhookerMain::run()
5656
5757 if (!gameName.isEmpty ()) {
5858 gameName.clear ();
59- if (settings) {
59+
60+ if (settings && settings->contains (" MameStop" ) && settings->value (" MameStop" ).typeId () == QMetaType::QStringList) {
61+ QStringList tempBuffer = settings->value (" MameStop" ).toStringList ();
62+ // qInfo() << tempBuffer;
63+ while (!tempBuffer.isEmpty ()) {
64+ if (tempBuffer.at (0 ).contains (" cmw" )) {
65+ int portNum = tempBuffer.at (0 ).at (4 ).digitValue ()-1 ;
66+ if (portNum >= 0 && portNum < validDevices.count ()) {
67+ if (serialPort[portNum].isOpen ()) {
68+ serialPort[portNum].write (tempBuffer.at (0 ).mid (6 ).toLocal8Bit ());
69+ if (!serialPort[portNum].waitForBytesWritten (500 ))
70+ printf (" Wrote to port no. %d (%04X:%04X @ %s), but wasn't sent in time apparently!?\n " ,
71+ portNum+1 , validDevices.at (portNum).vendorIdentifier (), validDevices.at (portNum).productIdentifier (), serialPort[portNum].portName ().toLocal8Bit ().constData ());
72+ } else printf (" Requested to write to port no. %d (%04X:%04X @ %s), but it's not even open yet!\n " ,
73+ portNum+1 , validDevices.at (portNum).vendorIdentifier (), validDevices.at (portNum).productIdentifier (), serialPort[portNum].portName ().toLocal8Bit ().constData ());
74+ }
75+ } else if (tempBuffer.at (0 ).contains (" cmc" )) {
76+ // close serial port at number (index(4))
77+ int portNum = tempBuffer.at (0 ).at (4 ).digitValue ()-1 ;
78+ if (portNum >= 0 && portNum < validDevices.count ()) {
79+ if (serialPort[portNum].isOpen ()) {
80+ serialPort[portNum].close ();
81+ printf (" Closed port no. %d (%04X:%04X @ %s)\n " ,
82+ portNum+1 , validDevices.at (portNum).vendorIdentifier (), validDevices.at (portNum).productIdentifier (), serialPort[portNum].portName ().toLocal8Bit ().constData ());
83+ } else printf (" Waaaaait a second... Port %d (%04X:%04X @ %s) is already closed!\n " ,
84+ portNum+1 , validDevices.at (portNum).vendorIdentifier (), validDevices.at (portNum).productIdentifier (), serialPort[portNum].portName ().toLocal8Bit ().constData ());
85+ }
86+ }
87+ tempBuffer.removeFirst ();
88+ }
89+
90+ for (int portNum = 0 ; portNum < validDevices.count (); ++portNum)
91+ if (serialPort[portNum].isOpen ()) {
92+ serialPort[portNum].write (" E" );
93+ serialPort[portNum].waitForBytesWritten (500 );
94+ serialPort[portNum].close ();
95+ }
96+
6097 delete settings;
6198 settingsMap.clear ();
99+ } else for (int portNum = 0 ; portNum < validDevices.count (); ++portNum) {
100+ if (serialPort[portNum].isOpen ()) {
101+ serialPort[portNum].write (" E" );
102+ if (serialPort[portNum].waitForBytesWritten (500 )) {
103+ serialPort[portNum].close ();
104+ printf (" Force-closed port no. %d (%04X:%04X @ %s) since this game has no MameStop entry.\n " ,
105+ portNum+1 , validDevices.at (portNum).vendorIdentifier (), validDevices.at (portNum).productIdentifier (), serialPort[portNum].portName ().toLocal8Bit ().constData ());
106+ } else printf (" Sent close signal to port %d (%04X:%04X @ %s), but wasn't sent in time apparently!?\n " ,
107+ portNum+1 , validDevices.at (portNum).vendorIdentifier (), validDevices.at (portNum).productIdentifier (), serialPort[portNum].portName ().toLocal8Bit ().constData ());
108+ }
62109 }
63110 }
64111
@@ -67,19 +114,6 @@ void qhookerMain::run()
67114 quit ();
68115 return ;
69116 }
70-
71- // in case we exit without connecting to a game (*coughFLYCASTcough*)
72- for (uint8_t i = 0 ; i < validDevices.count (); i++) {
73- if (serialPort[i].isOpen ()) {
74- serialPort[i].write (" E" );
75- if (serialPort[i].waitForBytesWritten (2000 )) {
76- printf (" Closed port %d (%s)\n " , i+1 , serialPort[i].portName ().toLocal8Bit ().constData ());
77- serialPort[i].close ();
78- } else {
79- printf (" Sent close signal to port %d, but wasn't sent in time apparently!?\n " , i+1 );
80- }
81- }
82- }
83117 }
84118 }
85119 break ;
@@ -239,7 +273,7 @@ bool qhookerMain::GameSearching(const QString &input)
239273 if (portNum >= 0 && portNum < validDevices.count ()) {
240274 if (serialPort[portNum].isOpen ()) {
241275 serialPort[portNum].write (tempBuffer.at (0 ).mid (6 ).toLocal8Bit ());
242- if (!serialPort[portNum].waitForBytesWritten (2000 ))
276+ if (!serialPort[portNum].waitForBytesWritten (500 ))
243277 printf (" Wrote to port no. %d (%04X:%04X @ %s), but wasn't sent in time apparently!?\n " ,
244278 portNum+1 , validDevices.at (portNum).vendorIdentifier (), validDevices.at (portNum).productIdentifier (), serialPort[portNum].portName ().toLocal8Bit ().constData ());
245279 } else printf (" Requested to write to port no. %d (%04X:%04X @ %s), but it's not even open yet!\n " ,
@@ -280,7 +314,7 @@ bool qhookerMain::GameStarted(const QString &input)
280314 if (!gameName.isEmpty ()) {
281315 gameName.clear ();
282316
283- if (settings && settings->contains (" MameStop" )) {
317+ if (settings && settings->contains (" MameStop" ) && settings-> value ( " MameStop " ). typeId () == QMetaType::QStringList ) {
284318 QStringList tempBuffer = settings->value (" MameStop" ).toStringList ();
285319 // qInfo() << tempBuffer;
286320 while (!tempBuffer.isEmpty ()) {
@@ -289,7 +323,7 @@ bool qhookerMain::GameStarted(const QString &input)
289323 if (portNum >= 0 && portNum < validDevices.count ()) {
290324 if (serialPort[portNum].isOpen ()) {
291325 serialPort[portNum].write (tempBuffer.at (0 ).mid (6 ).toLocal8Bit ());
292- if (!serialPort[portNum].waitForBytesWritten (2000 ))
326+ if (!serialPort[portNum].waitForBytesWritten (500 ))
293327 printf (" Wrote to port no. %d (%04X:%04X @ %s), but wasn't sent in time apparently!?\n " ,
294328 portNum+1 , validDevices.at (portNum).vendorIdentifier (), validDevices.at (portNum).productIdentifier (), serialPort[portNum].portName ().toLocal8Bit ().constData ());
295329 } else printf (" Requested to write to port no. %d (%04X:%04X @ %s), but it's not even open yet!\n " ,
@@ -310,12 +344,16 @@ bool qhookerMain::GameStarted(const QString &input)
310344 tempBuffer.removeFirst ();
311345 }
312346
347+ for (int portNum = 0 ; portNum < validDevices.count (); ++portNum)
348+ if (serialPort[portNum].isOpen ())
349+ serialPort[portNum].close ();
350+
313351 delete settings;
314352 settingsMap.clear ();
315353 } else for (int portNum = 0 ; portNum < validDevices.count (); ++portNum) {
316354 if (serialPort[portNum].isOpen ()) {
317355 serialPort[portNum].write (" E" );
318- if (serialPort[portNum].waitForBytesWritten (2000 )) {
356+ if (serialPort[portNum].waitForBytesWritten (500 )) {
319357 serialPort[portNum].close ();
320358 printf (" Force-closed port no. %d (%04X:%04X @ %s) since this game has no MameStop entry.\n " ,
321359 portNum+1 , validDevices.at (portNum).vendorIdentifier (), validDevices.at (portNum).productIdentifier (), serialPort[portNum].portName ().toLocal8Bit ().constData ());
@@ -344,7 +382,7 @@ bool qhookerMain::GameStarted(const QString &input)
344382 action[i] = action[i].replace (" %s%" , " %1" ).arg (1 );
345383
346384 serialPort[portNum].write (action.at (i).mid (action.at (i).indexOf (" cmw" )+6 ).toLocal8Bit ());
347- if (!serialPort[portNum].waitForBytesWritten (2000 ))
385+ if (!serialPort[portNum].waitForBytesWritten (500 ))
348386 printf (" Wrote to port no. %d (%04X:%04X @ %s), but wasn't sent in time apparently!?\n " ,
349387 portNum+1 , validDevices.at (portNum).vendorIdentifier (), validDevices.at (portNum).productIdentifier (), serialPort[portNum].portName ().toLocal8Bit ().constData ());
350388 }
@@ -365,7 +403,7 @@ bool qhookerMain::GameStarted(const QString &input)
365403 action[i] = action[i].replace (" %s%" , " %1" ).arg (0 );
366404
367405 serialPort[portNum].write (action.at (i).mid (action.at (i).indexOf (" cmw" )+6 ).toLocal8Bit ());
368- if (!serialPort[portNum].waitForBytesWritten (2000 ))
406+ if (!serialPort[portNum].waitForBytesWritten (500 ))
369407 printf (" Wrote to port no. %d (%04X:%04X @ %s), but wasn't sent in time apparently!?\n " ,
370408 portNum+1 , validDevices.at (portNum).vendorIdentifier (), validDevices.at (portNum).productIdentifier (), serialPort[portNum].portName ().toLocal8Bit ().constData ());
371409 }
@@ -387,7 +425,7 @@ bool qhookerMain::GameStarted(const QString &input)
387425 action[i] = action[i].replace (" %s%" , " %1" ).arg (buffer[0 ].mid (buffer[0 ].indexOf (' =' )+2 ).toInt ());
388426
389427 serialPort[portNum].write (action.at (i).mid (action.at (i).indexOf (" cmw" )+6 ).toLocal8Bit ());
390- if (!serialPort[portNum].waitForBytesWritten (2000 ))
428+ if (!serialPort[portNum].waitForBytesWritten (500 ))
391429 printf (" Wrote to port no. %d (%04X:%04X @ %s), but wasn't sent in time apparently!?\n " ,
392430 portNum+1 , validDevices.at (portNum).vendorIdentifier (), validDevices.at (portNum).productIdentifier (), serialPort[portNum].portName ().toLocal8Bit ().constData ());
393431 }
0 commit comments