Skip to content

Commit

Permalink
#1: Relocated some code to Thread. Tweaked rendering and message hand…
Browse files Browse the repository at this point in the history
…ling for PDCursesUIManager
  • Loading branch information
OOPMan committed Aug 2, 2019
1 parent 8102812 commit 7940280
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 55 deletions.
64 changes: 28 additions & 36 deletions RazorAtroxWinUSBVigEmFeeder/PDCursesUIManager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "PDCursesUIManager.h"

#include <forward_list>

PDCursesUIManager::PDCursesUIManager(DWORD parentThreadId)
: Thread("PDCursesUIManager", "PDCursesUIManager", parentThreadId, parentThreadId)
{
Expand All @@ -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) {
Expand All @@ -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<std::pair<DWORD, std::string>> 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();
}

Expand All @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions RazorAtroxWinUSBVigEmFeeder/PDCursesUIManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class PDCursesUIManager : public Thread
WINDOW *window;
WinUsbDeviceManager *winUsbDeviceManager;
THREAD_MESSAGES winUsbDeviceManagerStatus;

std::list<DWORD> winUsbDeviceThreadIdList;
std::unordered_map<DWORD, THREAD_MESSAGES> winUsbDeviceStatusMap;

void render(bool exiting);
bool checkMailbox();
std::string threadMessageToString(THREAD_MESSAGES threadMessage);
int checkMailbox();
};

20 changes: 20 additions & 0 deletions RazorAtroxWinUSBVigEmFeeder/Thread.cpp
Original file line number Diff line number Diff line change
@@ -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)
{
Expand Down
20 changes: 20 additions & 0 deletions RazorAtroxWinUSBVigEmFeeder/Thread.h
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
17 changes: 0 additions & 17 deletions RazorAtroxWinUSBVigEmFeeder/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,3 @@

typedef std::basic_string<TCHAR> 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
};

0 comments on commit 7940280

Please sign in to comment.