Skip to content

Commit

Permalink
multi-threading fully working
Browse files Browse the repository at this point in the history
  • Loading branch information
sam0x17 committed Aug 23, 2018
1 parent 536f439 commit 1616471
Showing 1 changed file with 57 additions and 9 deletions.
66 changes: 57 additions & 9 deletions XJoy/XJoy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ XUSB_REPORT report;
unsigned char data_left[DATA_BUFFER_SIZE];
unsigned char data_right[DATA_BUFFER_SIZE];
int res;
HANDLE left_thread;
DWORD left_thread_id;
HANDLE right_thread;
DWORD right_thread_id;
HANDLE report_mutex;
bool kill_threads = false;

enum JOYCON_REGION {
LEFT_DPAD,
Expand Down Expand Up @@ -416,24 +422,66 @@ void process_right_joycon() {
region_part(data_right[2], RIGHT_AUX, R_STICK);
}

int main() {
std::cout << "XJoy v0.1.0" << std::endl << std::endl;

initialize_joycons();
initialize_xbox();

DWORD WINAPI left_joycon_thread(__in LPVOID lpParameter) {
WaitForSingleObject(report_mutex, INFINITE);
std::cout << " => left joycon thread started" << std::endl;
ReleaseMutex(report_mutex);
for(;;) {
if(kill_threads) return 0;
hid_read(left_joycon, data_left, DATA_BUFFER_SIZE);
WaitForSingleObject(report_mutex, INFINITE);
process_left_joycon();
report = blank_report;
XUSB_REPORT_INIT(&report);
//hid_read(left_joycon, data_left, DATA_BUFFER_SIZE);
vigem_target_x360_update(client, target, report);
std::cout << std::endl;
ReleaseMutex(report_mutex);
}
return 0;
}

DWORD WINAPI right_joycon_thread(__in LPVOID lpParameter) {
WaitForSingleObject(report_mutex, INFINITE);
std::cout << " => right joycon thread started" << std::endl;
ReleaseMutex(report_mutex);
for(;;) {
if(kill_threads) return 0;
hid_read(right_joycon, data_right, DATA_BUFFER_SIZE);
//process_left_joycon();
WaitForSingleObject(report_mutex, INFINITE);
process_right_joycon();
report = blank_report;
XUSB_REPORT_INIT(&report);
vigem_target_x360_update(client, target, report);
std::cout << std::endl;
ReleaseMutex(report_mutex);
}
return 0;
}

int main() {
std::cout << "XJoy v0.1.0" << std::endl << std::endl;

initialize_joycons();
initialize_xbox();

std::cout << std::endl;
std::cout << "initializing threads..." << std::endl;
report_mutex = CreateMutex(NULL, FALSE, NULL);
if(report_mutex == NULL) {
printf("CreateMutex error: %d\n", GetLastError());
return 1;
}
std::cout << " => created report mutex" << std::endl;
left_thread = CreateThread(0, 0, left_joycon_thread, 0, 0, &left_thread_id);
right_thread = CreateThread(0, 0, right_joycon_thread, 0, 0, &right_thread_id);
Sleep(500);
std::cout << std::endl;

Sleep(10000);
getchar();
kill_threads = true;
Sleep(10);
TerminateThread(left_thread, 0);
TerminateThread(right_thread, 0);
std::cout << "disconnecting and exiting..." << std::endl;
disconnect_exit();
}

0 comments on commit 1616471

Please sign in to comment.