Skip to content

Commit

Permalink
Fixed driver hang-ups, cleaned the code up
Browse files Browse the repository at this point in the history
  • Loading branch information
localcc committed Oct 30, 2021
1 parent 46b0c15 commit 24407dc
Show file tree
Hide file tree
Showing 18 changed files with 563 additions and 107 deletions.
439 changes: 439 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion WindowsDriver/Libs/ViGEm
Submodule ViGEm updated from 52682b to e99ba7
9 changes: 5 additions & 4 deletions WindowsDriver/WindowsDriver/WindowsDriver.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,26 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
Expand Down Expand Up @@ -89,6 +89,7 @@
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir)Libs\ViGEm\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down
22 changes: 11 additions & 11 deletions WindowsDriver/WindowsDriver/WindowsDriver.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Pliki źródłowe">
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Pliki nagłówkowe">
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Pliki zasobów">
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="client.cpp">
<Filter>Pliki źródłowe</Filter>
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="controller_conversion.cpp">
<Filter>Pliki źródłowe</Filter>
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="feeder.cpp">
<Filter>Pliki źródłowe</Filter>
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="main.cpp">
<Filter>Pliki źródłowe</Filter>
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="client.h">
<Filter>Pliki źródłowe</Filter>
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="controller_conversion.h">
<Filter>Pliki źródłowe</Filter>
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="controller_offsets.h">
<Filter>Pliki źródłowe</Filter>
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="feeder.h">
<Filter>Pliki źródłowe</Filter>
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
5 changes: 4 additions & 1 deletion WindowsDriver/WindowsDriver/WindowsDriver.vcxproj.user
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerCommandArguments>192.168.1.158</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>
22 changes: 14 additions & 8 deletions WindowsDriver/WindowsDriver/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@ client::client(const char* hostname, int port) {
int res = 0;
WSAData data;
res = WSAStartup(MAKEWORD(2, 2), &data);
if (res != 0)
this->~client();
if (res != 0)
throw std::runtime_error("Failed to start WSA!");

sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock_fd == INVALID_SOCKET)
this->~client();
throw std::runtime_error("Failed to create socket!");

u_long iMode = 1;
if (ioctlsocket(sock_fd, FIONBIO, &iMode) != NO_ERROR) {
throw std::runtime_error("Failed to make non-blocking socket!");
}

address.sin_family = AF_INET;

res = InetPton(AF_INET, hostname, &address.sin_addr.s_addr);
if (res < 0)
this->~client();
throw std::runtime_error("Failed to get ip!");
address.sin_port = htons(port);
}

Expand All @@ -28,14 +34,14 @@ client::~client() {
}


int client::client_write(char* arr, int amount) {
return sendto(sock_fd, arr, amount, 0, reinterpret_cast<sockaddr*>(&address), sizeof(address));
int client::client_write(uint8_t* arr, int amount) {
return sendto(sock_fd, reinterpret_cast<char*>(arr), amount, 0, reinterpret_cast<sockaddr*>(&address), sizeof(address));
}

int client::client_read(char* arr, int amount) {
int client::client_read(uint8_t* arr, int amount) {
sockaddr_in from;
int size = sizeof(from);
int ret = recvfrom(sock_fd, arr, amount, 0, reinterpret_cast<SOCKADDR*>(&from), &size);
int ret = recvfrom(sock_fd, reinterpret_cast<char*>(arr), amount, 0, reinterpret_cast<SOCKADDR*>(&from), &size);

return ret;
}
Expand Down
6 changes: 4 additions & 2 deletions WindowsDriver/WindowsDriver/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
#ifndef UDPCLIENT_CLIENT_H
#define UDPCLIENT_CLIENT_H
#include <ws2tcpip.h>
#include <cstdint>
#include <stdexcept>



class client {
public:
client(const char* hostname, int port);
~client();
int client_read(char* arr, int amount);
int client_write(char* arr, int amount);
int client_read(uint8_t* arr, int amount);
int client_write(uint8_t* arr, int amount);
void close_socket();
private:
SOCKET sock_fd;
Expand Down
32 changes: 16 additions & 16 deletions WindowsDriver/WindowsDriver/controller_conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
#include "controller_offsets.h"
#include <cstdio>

XUSB_REPORT controller_conversion::data_to_report(char* data) {
XUSB_REPORT controller_conversion::data_to_report(uint8_t* data) {
XUSB_REPORT report;

report.wButtons = 0;
if (data[0] & 0b00000001) report.wButtons |= XUSB_GAMEPAD_A;
if ((data[0] >> GB_OFF_X) & 0b00000001) report.wButtons |= XUSB_GAMEPAD_X;
if ((data[0] >> GB_OFF_B) & 0b00000001) report.wButtons |= XUSB_GAMEPAD_B;
if ((data[0] >> GB_OFF_Y) & 0b00000001) report.wButtons |= XUSB_GAMEPAD_Y;
if ((data[0] >> GB_OFF_LB) & 0b00000001) report.wButtons |= XUSB_GAMEPAD_LEFT_SHOULDER;
if ((data[0] >> GB_OFF_RB) & 0b00000001) report.wButtons |= XUSB_GAMEPAD_RIGHT_SHOULDER;
if ((data[0] >> GB_OFF_UP) & 0b00000001) report.wButtons |= XUSB_GAMEPAD_DPAD_UP;
if ((data[0] >> GB_OFF_DOWN) & 0b00000001) report.wButtons |= XUSB_GAMEPAD_DPAD_DOWN;
if (data[1] & 0b00000001) report.wButtons |= XUSB_GAMEPAD_DPAD_LEFT;
if ((data[1] >> GB_OFF_RIGHT) & 0b00000001) report.wButtons |= XUSB_GAMEPAD_DPAD_RIGHT;
if ((data[1] >> GB_OFF_S1_Z) & 0b00000001) report.wButtons |= XUSB_GAMEPAD_LEFT_THUMB;
if ((data[1] >> GB_OFF_S2_Z) & 0b00000001) report.wButtons |= XUSB_GAMEPAD_RIGHT_THUMB;
if ((data[1] >> GB_OFF_START) & 0b00000001) report.wButtons |= XUSB_GAMEPAD_START;
if ((data[1] >> GB_OFF_BACK) & 0b00000001) report.wButtons |= XUSB_GAMEPAD_BACK;
if ((data[1] >> GB_OFF_LOGO) & 0b00000001) report.wButtons |= XUSB_GAMEPAD_GUIDE;
if (data[0] & 0b1) report.wButtons |= XUSB_GAMEPAD_A;
if ((data[0] >> GB_OFF_X) & 0b1) report.wButtons |= XUSB_GAMEPAD_X;
if ((data[0] >> GB_OFF_B) & 0b1) report.wButtons |= XUSB_GAMEPAD_B;
if ((data[0] >> GB_OFF_Y) & 0b1) report.wButtons |= XUSB_GAMEPAD_Y;
if ((data[0] >> GB_OFF_LB) & 0b1) report.wButtons |= XUSB_GAMEPAD_LEFT_SHOULDER;
if ((data[0] >> GB_OFF_RB) & 0b1) report.wButtons |= XUSB_GAMEPAD_RIGHT_SHOULDER;
if ((data[0] >> GB_OFF_UP) & 0b1) report.wButtons |= XUSB_GAMEPAD_DPAD_UP;
if ((data[0] >> GB_OFF_DOWN) & 0b1) report.wButtons |= XUSB_GAMEPAD_DPAD_DOWN;
if (data[1] & 0b1) report.wButtons |= XUSB_GAMEPAD_DPAD_LEFT;
if ((data[1] >> GB_OFF_RIGHT) & 0b1) report.wButtons |= XUSB_GAMEPAD_DPAD_RIGHT;
if ((data[1] >> GB_OFF_S1_Z) & 0b1) report.wButtons |= XUSB_GAMEPAD_LEFT_THUMB;
if ((data[1] >> GB_OFF_S2_Z) & 0b1) report.wButtons |= XUSB_GAMEPAD_RIGHT_THUMB;
if ((data[1] >> GB_OFF_START) & 0b1) report.wButtons |= XUSB_GAMEPAD_START;
if ((data[1] >> GB_OFF_BACK) & 0b1) report.wButtons |= XUSB_GAMEPAD_BACK;
if ((data[1] >> GB_OFF_LOGO) & 0b1) report.wButtons |= XUSB_GAMEPAD_GUIDE;

report.bLeftTrigger = data[2] | (data[3] << 8);
report.bRightTrigger = data[4] | (data[5] << 8);
Expand Down
3 changes: 2 additions & 1 deletion WindowsDriver/WindowsDriver/controller_conversion.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once
#include "ViGEm/Common.h"
#include <cstdint>

class controller_conversion {
public:
static XUSB_REPORT data_to_report(char* data);
static XUSB_REPORT data_to_report(uint8_t* data);

};
75 changes: 40 additions & 35 deletions WindowsDriver/WindowsDriver/feeder.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
#include "feeder.h"
#include "controller_conversion.h"



static char get[1] = { 1 };
static char poweroff[1] = { 2 };
static char cb[1] = { 3 };

feeder* feeder::c_instance = nullptr;

feeder::feeder(const char* hostname, int port) {
this->udp_client = new client(hostname, port);
feeder::feeder(const char* hostname, int port) : udp_client(hostname, port) {
this->driver_client = vigem_alloc();
if (this->driver_client == nullptr) {
throw std::runtime_error("Failed to allocate ViGEm bus driver!");
}
if (!VIGEM_SUCCESS(vigem_connect(this->driver_client))) {
throw std::runtime_error("Failed to connect to ViGEm bus driver!");
}
this->driver_target = vigem_target_x360_alloc();
connected = false;
c_instance = this;
if (this->driver_target == nullptr) {
throw std::runtime_error("Failed to allocate driver handle!");
}
}

feeder::~feeder() {
this->udp_client->close_socket();
connected = false;
this->udp_client->~client();
this->udp_client.close_socket();
this->connected = false;
vigem_target_remove(this->driver_client, this->driver_target);
vigem_disconnect(this->driver_client);
vigem_target_free(this->driver_target);
Expand All @@ -29,10 +26,13 @@ feeder::~feeder() {
}

int feeder::connect() {
if (!VIGEM_SUCCESS(vigem_target_add(this->driver_client, this->driver_target))) return -81;

if (vigem_connect(this->driver_client) != VIGEM_ERROR_NONE) return -80;
if (vigem_target_add(this->driver_client, this->driver_target) != VIGEM_ERROR_NONE) return -81;
connected = true;
this->connected = true;
if (!VIGEM_SUCCESS(vigem_target_x360_register_notification(this->driver_client, this->driver_target,
reinterpret_cast<PFN_VIGEM_X360_NOTIFICATION>(&feeder::controller_callback), this))) {
return 1;
}
return 0;
}

Expand All @@ -42,17 +42,23 @@ void feeder::start_feeder_thread() {
}

void feeder::stop_feeder_thread() {
connected = false;
this->disconnect();
this->sender_thread.join();
}

void feeder::controller_callback(PVIGEM_CLIENT Client,
PVIGEM_TARGET Target,
UCHAR LeftMotor,
UCHAR RightMotor,
UCHAR LedNumber) {
char data[3] = { cb[0], LeftMotor, RightMotor };
feeder::GetInstance()->udp_client->client_write(data, 3);
UCHAR LedNumber,
LPVOID Instance) {
feeder* f = reinterpret_cast<feeder*>(Instance);
if (LeftMotor == f->left_motor && RightMotor == f->right_motor) return;
f->left_motor = LeftMotor;
f->right_motor = RightMotor;

uint8_t data[3] = { cb, LeftMotor, RightMotor };
f->udp_client.client_write(data, 3);
}


Expand All @@ -64,29 +70,28 @@ void feeder::controller_callback(PVIGEM_CLIENT Client,
2 - poweroff
3 - cb, write 2 bytes after, 1st byte - left rumble, 2nd - right rumble
*/

static uint8_t get[1] = { 1 };
static uint8_t poweroff[1] = { 2 };
static uint8_t cb = 3;

void feeder::feed() {

char* buf = (char*)malloc(14); // buffer for data
char data[2] = { 0, 0 };
uint8_t* buf = new uint8_t[14];
uint8_t data[2]{ 0 };

while (connected) {

//Do work
this->udp_client->client_write(get, 1); // asking for controller data
this->udp_client.client_write(get, 1); // asking for controller data
memset(buf, 0, 14);
this->udp_client->client_read(buf, 14); // receiving it
vigem_target_x360_register_notification(this->driver_client, this->driver_target,
reinterpret_cast<PFN_VIGEM_X360_NOTIFICATION>(&feeder::controller_callback));
this->udp_client.client_read(buf, 14);;

vigem_target_x360_update(this->driver_client, this->driver_target, controller_conversion::data_to_report(buf));

std::this_thread::sleep_for(std::chrono::milliseconds(2));
}

this->udp_client->client_write(poweroff, 1);
this->udp_client.client_write(poweroff, 1);
}

void feeder::disconnect() {
this->udp_client->close_socket();
this->connected = false;
this->udp_client.close_socket();
}

12 changes: 7 additions & 5 deletions WindowsDriver/WindowsDriver/feeder.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#pragma once
#include "client.h"
#include "ViGEm/Client.h"
#include <stdexcept>
#include <thread>
#include <chrono>

class feeder {
public:
feeder(const char* hostname, int port);
~feeder();

static feeder* GetInstance() { return c_instance; }

int connect();
void disconnect();
void start_feeder_thread();
Expand All @@ -18,15 +18,17 @@ class feeder {
PVIGEM_TARGET Target,
UCHAR LargeMotor,
UCHAR SmallMotor,
UCHAR LedNumber);
UCHAR LedNumber,
LPVOID Instance);

private:
UCHAR left_motor;
UCHAR right_motor;

void feed();
static feeder* c_instance;
PVIGEM_CLIENT driver_client;
PVIGEM_TARGET driver_target;
std::thread sender_thread;
client* udp_client;
client udp_client;
bool connected;
};
Loading

0 comments on commit 24407dc

Please sign in to comment.