@@ -156,6 +156,7 @@ void qhookerMain::SerialInit()
156156 for (auto &port : serialPort)
157157 delete port;
158158 serialPort.clear ();
159+ printf (" No devices found!\n " );
159160 }
160161 } else {
161162 QList<QSerialPortInfo> newDevices;
@@ -176,23 +177,16 @@ void qhookerMain::SerialInit()
176177 for (auto &port : serialPort)
177178 delete port;
178179 serialPort.clear ();
180+ printf (" No devices found!\n " );
179181 }
180182 } else {
181- // Sort valid devices by Product ID ascending
182- std::sort (newDevices.begin (), newDevices.end (),
183- [](const QSerialPortInfo &a, const QSerialPortInfo &b) {
184- return a.productIdentifier () < b.productIdentifier ();
185- });
186-
187183 if (newDevices.size () != validDevices.size ()) {
188184 printf (" Current ports list does not match new list, overriding...\n\n " );
189- PrintDeviceInfo (newDevices);
190185 AddNewDevices (newDevices);
191186 } else for (const auto &newPort : std::as_const (newDevices)) {
192187 if (!validIDs.contains (newPort.vendorIdentifier () | newPort.productIdentifier () << 16 )) {
193188 printf (" %04X:%04X not found in current ports, overriding old serial devices list...\n\n " ,
194189 newPort.vendorIdentifier (), newPort.productIdentifier ());
195- PrintDeviceInfo (newDevices);
196190 AddNewDevices (newDevices);
197191 break ;
198192 }
@@ -202,7 +196,7 @@ void qhookerMain::SerialInit()
202196}
203197
204198
205- void qhookerMain::AddNewDevices (const QList<QSerialPortInfo> &newDevices)
199+ void qhookerMain::AddNewDevices (QList<QSerialPortInfo> &newDevices)
206200{
207201 if (serialPort.count ()) {
208202 for (auto &port : serialPort)
@@ -214,6 +208,92 @@ void qhookerMain::AddNewDevices(const QList<QSerialPortInfo> &newDevices)
214208 for (const auto &device : newDevices)
215209 serialPort << new QSerialPort;
216210
211+ switch (sortType) {
212+ case sortPIDascend:
213+ std::sort (newDevices.begin (), newDevices.end (),
214+ [](const QSerialPortInfo &a, const QSerialPortInfo &b) {
215+ return a.productIdentifier () < b.productIdentifier ();
216+ });
217+ break ;
218+ case sortPIDdescend:
219+ std::sort (newDevices.begin (), newDevices.end (),
220+ [](const QSerialPortInfo &a, const QSerialPortInfo &b) {
221+ return a.productIdentifier () > b.productIdentifier ();
222+ });
223+ break ;
224+ case sortPortAscend:
225+ std::sort (newDevices.begin (), newDevices.end (),
226+ [](const QSerialPortInfo &a, const QSerialPortInfo &b) {
227+ return a.portName ().mid (
228+ #ifdef Q_OS_WIN
229+ 3 // COM
230+ #else
231+ 6 // ttyACM
232+ #endif
233+ ).toInt () < b.portName ().mid (
234+ #ifdef Q_OS_WIN
235+ 3
236+ #else
237+ 6
238+ #endif
239+ ).toInt ();
240+ });
241+ break ;
242+ case sortPortDescend:
243+ std::sort (newDevices.begin (), newDevices.end (),
244+ [](const QSerialPortInfo &a, const QSerialPortInfo &b) {
245+ return a.portName ().mid (
246+ #ifdef Q_OS_WIN
247+ 3 // COM
248+ #else
249+ 6 // ttyACM
250+ #endif
251+ ).toInt () > b.portName ().mid (
252+ #ifdef Q_OS_WIN
253+ 3
254+ #else
255+ 6
256+ #endif
257+ ).toInt ();
258+ });
259+ break ;
260+ default :
261+ break ;
262+ }
263+
264+ for (const QSerialPortInfo &info : newDevices) {
265+ printf (" ========================================\n " );
266+ printf (" Port Name: %s\n " , info.portName ().toLocal8Bit ().constData ());
267+ printf (" Vendor Identifier: " );
268+ if (info.hasVendorIdentifier ()) {
269+ printf (" %04X" , info.vendorIdentifier ());
270+ switch (info.vendorIdentifier ()) {
271+ case 9025 :
272+ printf (" (GUN4IR Lightgun)\n " );
273+ break ;
274+ case 13939 :
275+ printf (" (Blamcon Lightgun)\n " );
276+ break ;
277+ case 0xF143 :
278+ printf (" (OpenFIRE Lightgun)\n " );
279+ break ;
280+ default :
281+ // unlikely to happen due to whitelisting, but just in case.
282+ printf (" \n " );
283+ break ;
284+ }
285+ } else printf (" N/A\n " );
286+
287+ printf (" Product Identifier: " );
288+ if (info.hasProductIdentifier ())
289+ printf (" %04X\n " , info.productIdentifier ());
290+ else printf (" N/A\n " );
291+
292+ if (!info.manufacturer ().isEmpty () && !info.description ().isEmpty ())
293+ printf (" Device: %s %s\n " , info.manufacturer ().toLocal8Bit ().constData (), info.description ().toLocal8Bit ().constData ());
294+ printf (" ========================================\n " );
295+ }
296+
217297 printf (" \n " );
218298
219299 // Keep track of assigned PIDs and check for duplicates
@@ -534,39 +614,3 @@ void qhookerMain::LoadConfig(const QString &path)
534614 }
535615 settings->endGroup ();
536616}
537-
538- void qhookerMain::PrintDeviceInfo (const QList<QSerialPortInfo> &devices)
539- {
540- for (const QSerialPortInfo &info : devices) {
541- printf (" ========================================\n " );
542- printf (" Port Name: %s\n " , info.portName ().toLocal8Bit ().constData ());
543- printf (" Vendor Identifier: " );
544- if (info.hasVendorIdentifier ()) {
545- printf (" %04X" , info.vendorIdentifier ());
546- switch (info.vendorIdentifier ()) {
547- case 9025 :
548- printf (" (GUN4IR Lightgun)\n " );
549- break ;
550- case 13939 :
551- printf (" (Blamcon Lightgun)\n " );
552- break ;
553- case 0xF143 :
554- printf (" (OpenFIRE Lightgun)\n " );
555- break ;
556- default :
557- // unlikely to happen due to whitelisting, but just in case.
558- printf (" \n " );
559- break ;
560- }
561- } else printf (" N/A\n " );
562-
563- printf (" Product Identifier: " );
564- if (info.hasProductIdentifier ())
565- printf (" %04X\n " , info.productIdentifier ());
566- else printf (" N/A\n " );
567-
568- if (!info.manufacturer ().isEmpty () && !info.description ().isEmpty ())
569- printf (" Device: %s %s\n " , info.manufacturer ().toLocal8Bit ().constData (), info.description ().toLocal8Bit ().constData ());
570- printf (" ========================================\n " );
571- }
572- }
0 commit comments