diff --git a/RazorAtroxWinUSBVigEmFeeder/PDCursesUIManager.cpp b/RazorAtroxWinUSBVigEmFeeder/PDCursesUIManager.cpp index 25f443d..1e93ae7 100644 --- a/RazorAtroxWinUSBVigEmFeeder/PDCursesUIManager.cpp +++ b/RazorAtroxWinUSBVigEmFeeder/PDCursesUIManager.cpp @@ -1,5 +1,7 @@ #include "PDCursesUIManager.h" +#include + PDCursesUIManager::PDCursesUIManager(DWORD parentThreadId) : Thread("PDCursesUIManager", "PDCursesUIManager", parentThreadId, parentThreadId) { @@ -10,30 +12,12 @@ void PDCursesUIManager::wait() WaitForSingleObject(this->threadHandle, INFINITE); } -std::string PDCursesUIManager::threadMessageToString(THREAD_MESSAGES threadMessage) -{ - switch (threadMessage) { - case RAWUVEF_WIN_USB_DEVICE_MANAGER_STARTED: return "Started"; - case RAWUVEF_WIN_USB_DEVICE_MANAGER_SCANNING: - case RAWUVEF_WIN_USB_DEVICE_MANAGER_SLEEPING: return "Active"; - case RAWUVEF_WIN_USB_DEVICE_MANAGER_TERMINATING: return "Terminating..."; - case RAWUVEF_WIN_USB_DEVICE_MANAGER_ERROR: return "Error!"; - case RAWUVEF_WIN_USB_DEVICE_STARTED: return "Started"; - case RAWUVEF_WIN_USB_DEVICE_VIGEM_CONNECT: return "VigEmClient connect..."; - case RAWUVEF_WIN_USB_DEVICE_VIGEM_TARGET_ADD: return "VigEmClient target add..."; - case RAWUVEF_WIN_USB_DEVICE_OPEN: return "Device Open..."; - case RAWUVEF_WIN_USB_DEVICE_INIT: return "Device Init..."; - case RAWUVEF_WIN_USB_DEVICE_READ_INPUT: return "Reading input..."; - case RAWUVEF_WIN_USB_DEVICE_TERMINATING: return "Terminating..."; - case RAWUVEF_WIN_USB_DEVICE_ERROR: return "Error!"; - } - return "Unknown Thread Message"; -} - -bool PDCursesUIManager::checkMailbox() +int PDCursesUIManager::checkMailbox() { MSG message; + int messageCount = 0; while (PeekMessage(&message, NULL, WM_USER, WM_APP, PM_REMOVE) != FALSE) { + messageCount++; DWORD threadId = message.wParam; THREAD_MESSAGES threadMessage = (THREAD_MESSAGES)message.message; switch (threadMessage) { @@ -51,34 +35,43 @@ bool PDCursesUIManager::checkMailbox() case RAWUVEF_WIN_USB_DEVICE_INIT: case RAWUVEF_WIN_USB_DEVICE_READ_INPUT: case RAWUVEF_WIN_USB_DEVICE_TERMINATING: - case RAWUVEF_WIN_USB_DEVICE_ERROR: - this->winUsbDeviceStatusMap.insert_or_assign(threadId, threadMessage); + case RAWUVEF_WIN_USB_DEVICE_ERROR: + if (this->winUsbDeviceStatusMap.find(threadId) == this->winUsbDeviceStatusMap.end()) this->winUsbDeviceThreadIdList.push_back(threadId); + this->winUsbDeviceStatusMap.insert_or_assign(threadId, threadMessage); break; - case RAWUVEF_STOPPED: + case RAWUVEF_STOPPED: + { if (this->winUsbDeviceStatusMap.find(threadId) == this->winUsbDeviceStatusMap.end()) { this->logger->warn("Thread %v not in WinUsbDeviceStatusMap", threadId); break; - } + } + auto predicate = [threadId](DWORD threadIdB) { return threadId == threadIdB; }; + this->winUsbDeviceThreadIdList.remove_if(predicate); this->winUsbDeviceStatusMap.erase(threadId); break; - case RAWUVEF_STOP: return false; + } + default: + this->logger->warn("Unsupported message: %d", threadMessage); } } - return true; + return messageCount; } void PDCursesUIManager::render(bool exiting) { erase(); mvwprintw(this->window, 0, 0, "Razer Atrox WinUSB VigEm Feeder %s", exiting ? "" : "(Press Q to exit)"); - mvwprintw(this->window, 2, 0, "WinUSB Device Manager (Thread ID %d) status: %s", this->winUsbDeviceManager->getThreadId(), this->threadMessageToString(this->winUsbDeviceManagerStatus)); + mvwprintw(this->window, 2, 0, "WinUSB Device Manager (Thread ID %d) status: %s", this->winUsbDeviceManager->getThreadId(), threadMessageToString(this->winUsbDeviceManagerStatus).data()); auto counter = 0; - for (auto tuple : this->winUsbDeviceStatusMap) { - // For some reason we have to use the string data here or pdcurses prints out gibberish - auto status = this->threadMessageToString(tuple.second).data(); - mvwprintw(this->window, 3 + counter, 0, "WinUSB Device %d (Thread ID %d) status: %s", counter, tuple.first, status); - } - if (exiting) mvwprintw(this->window, 5 + counter, 0, "Exiting. Waiting for all threads to exit..."); + std::list> statusList; + for (auto threadId : this->winUsbDeviceThreadIdList) statusList.push_back(std::make_pair( + threadId, threadMessageToString(this->winUsbDeviceStatusMap[threadId]) + )); + for (auto tuple : statusList) { + mvwprintw(this->window, 3 + counter, 0, "WinUSB Device %d (Thread ID %d) status: %s", counter, tuple.first, tuple.second.data()); + counter++; + } + if (exiting) mvwprintw(this->window, 4 + counter, 0, "Exiting. Waiting for all threads to exit..."); refresh(); } @@ -94,8 +87,7 @@ DWORD PDCursesUIManager::run() this->winUsbDeviceManager = new WinUsbDeviceManager(this->threadId, this->threadId); this->logger->info("Entering GUI processing loop"); while (true) { - if (!this->checkMailbox()) break; - this->render(false); + if (this->checkMailbox() > 0) this->render(false); auto key = getch(); if (key == 'Q' || key == 'q') break; } diff --git a/RazorAtroxWinUSBVigEmFeeder/PDCursesUIManager.h b/RazorAtroxWinUSBVigEmFeeder/PDCursesUIManager.h index aba07ac..3243513 100644 --- a/RazorAtroxWinUSBVigEmFeeder/PDCursesUIManager.h +++ b/RazorAtroxWinUSBVigEmFeeder/PDCursesUIManager.h @@ -17,10 +17,11 @@ class PDCursesUIManager : public Thread WINDOW *window; WinUsbDeviceManager *winUsbDeviceManager; THREAD_MESSAGES winUsbDeviceManagerStatus; + + std::list winUsbDeviceThreadIdList; std::unordered_map winUsbDeviceStatusMap; void render(bool exiting); - bool checkMailbox(); - std::string threadMessageToString(THREAD_MESSAGES threadMessage); + int checkMailbox(); }; diff --git a/RazorAtroxWinUSBVigEmFeeder/Thread.cpp b/RazorAtroxWinUSBVigEmFeeder/Thread.cpp index f748554..8ae9a6a 100644 --- a/RazorAtroxWinUSBVigEmFeeder/Thread.cpp +++ b/RazorAtroxWinUSBVigEmFeeder/Thread.cpp @@ -1,5 +1,25 @@ #include "Thread.h" +std::string threadMessageToString(THREAD_MESSAGES threadMessage) +{ + switch (threadMessage) { + case RAWUVEF_WIN_USB_DEVICE_MANAGER_STARTED: return "Started"; + case RAWUVEF_WIN_USB_DEVICE_MANAGER_SCANNING: + case RAWUVEF_WIN_USB_DEVICE_MANAGER_SLEEPING: return "Active"; + case RAWUVEF_WIN_USB_DEVICE_MANAGER_TERMINATING: return "Terminating..."; + case RAWUVEF_WIN_USB_DEVICE_MANAGER_ERROR: return "Error!"; + case RAWUVEF_WIN_USB_DEVICE_STARTED: return "Started"; + case RAWUVEF_WIN_USB_DEVICE_VIGEM_CONNECT: return "VigEmClient connect..."; + case RAWUVEF_WIN_USB_DEVICE_VIGEM_TARGET_ADD: return "VigEmClient target add..."; + case RAWUVEF_WIN_USB_DEVICE_OPEN: return "Device Open..."; + case RAWUVEF_WIN_USB_DEVICE_INIT: return "Device Init..."; + case RAWUVEF_WIN_USB_DEVICE_READ_INPUT: return "Reading input..."; + case RAWUVEF_WIN_USB_DEVICE_TERMINATING: return "Terminating..."; + case RAWUVEF_WIN_USB_DEVICE_ERROR: return "Error!"; + } + return "Unknown Thread Message"; +} + Thread::Thread(std::string identifier, std::string loggerName, DWORD parentThreadId, DWORD uiManagerThreadId) : identifier(identifier), logger(el::Loggers::getLogger(loggerName)), parentThreadId(parentThreadId), uiManagerThreadId(uiManagerThreadId) { diff --git a/RazorAtroxWinUSBVigEmFeeder/Thread.h b/RazorAtroxWinUSBVigEmFeeder/Thread.h index 923752d..ebc6f64 100644 --- a/RazorAtroxWinUSBVigEmFeeder/Thread.h +++ b/RazorAtroxWinUSBVigEmFeeder/Thread.h @@ -1,6 +1,26 @@ #pragma once #include "pch.h" +enum THREAD_MESSAGES { + RAWUVEF_STOP = WM_USER + 0, + RAWUVEF_STOPPED = WM_USER + 1, + RAWUVEF_WIN_USB_DEVICE_MANAGER_STARTED = WM_USER + 2, + RAWUVEF_WIN_USB_DEVICE_MANAGER_SCANNING = WM_USER + 3, + RAWUVEF_WIN_USB_DEVICE_MANAGER_SLEEPING = WM_USER + 4, + RAWUVEF_WIN_USB_DEVICE_MANAGER_TERMINATING = WM_USER + 5, + RAWUVEF_WIN_USB_DEVICE_MANAGER_ERROR = WM_USER + 6, + RAWUVEF_WIN_USB_DEVICE_STARTED = WM_USER + 7, + RAWUVEF_WIN_USB_DEVICE_VIGEM_CONNECT = WM_USER + 8, + RAWUVEF_WIN_USB_DEVICE_VIGEM_TARGET_ADD = WM_USER + 9, + RAWUVEF_WIN_USB_DEVICE_OPEN = WM_USER + 10, + RAWUVEF_WIN_USB_DEVICE_INIT = WM_USER + 11, + RAWUVEF_WIN_USB_DEVICE_READ_INPUT = WM_USER + 12, + RAWUVEF_WIN_USB_DEVICE_TERMINATING = WM_USER + 13, + RAWUVEF_WIN_USB_DEVICE_ERROR = WM_USER + 14 +}; + +std::string threadMessageToString(THREAD_MESSAGES threadMessage); + class Thread { public: diff --git a/RazorAtroxWinUSBVigEmFeeder/pch.h b/RazorAtroxWinUSBVigEmFeeder/pch.h index dc912e3..fb1b3da 100644 --- a/RazorAtroxWinUSBVigEmFeeder/pch.h +++ b/RazorAtroxWinUSBVigEmFeeder/pch.h @@ -14,20 +14,3 @@ typedef std::basic_string tstring; -enum THREAD_MESSAGES { - RAWUVEF_STOP = WM_USER + 0, - RAWUVEF_STOPPED = WM_USER + 1, - RAWUVEF_WIN_USB_DEVICE_MANAGER_STARTED = WM_USER + 2, - RAWUVEF_WIN_USB_DEVICE_MANAGER_SCANNING = WM_USER + 3, - RAWUVEF_WIN_USB_DEVICE_MANAGER_SLEEPING = WM_USER + 4, - RAWUVEF_WIN_USB_DEVICE_MANAGER_TERMINATING = WM_USER + 5, - RAWUVEF_WIN_USB_DEVICE_MANAGER_ERROR = WM_USER + 6, - RAWUVEF_WIN_USB_DEVICE_STARTED = WM_USER + 7, - RAWUVEF_WIN_USB_DEVICE_VIGEM_CONNECT = WM_USER + 8, - RAWUVEF_WIN_USB_DEVICE_VIGEM_TARGET_ADD = WM_USER + 9, - RAWUVEF_WIN_USB_DEVICE_OPEN = WM_USER + 10, - RAWUVEF_WIN_USB_DEVICE_INIT = WM_USER + 11, - RAWUVEF_WIN_USB_DEVICE_READ_INPUT = WM_USER + 12, - RAWUVEF_WIN_USB_DEVICE_TERMINATING = WM_USER + 13, - RAWUVEF_WIN_USB_DEVICE_ERROR = WM_USER + 14 -};