WIP study of XInput for windows support.

This commit is contained in:
2025-05-22 23:08:10 -05:00
parent 71094053a2
commit 2be8d8e7a1
5 changed files with 124 additions and 8 deletions

View File

@@ -35,7 +35,9 @@ if (UNIX AND NOT APPLE)
endif()
if (WIN32)
add_library(jstick STATIC ${jstick_SRC})
add_library(jstick STATIC ${jstick_SRC}
src/platform/linux/jstick_linux.cpp
src/platform/windows/jstick_xinput.cpp)
endif()
target_include_directories(jstick PUBLIC
@@ -46,7 +48,7 @@ target_include_directories(jstick PUBLIC
target_include_directories(jstick PUBLIC ${jstick_SOURCE_DIR}/include)
target_link_libraries(jstick PUBLIC Event jlog J3ML)
#target_link_libraries(jstick PUBLIC Event jlog J3ML)
install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME})
@@ -54,4 +56,4 @@ install(FILES ${jstick_HEADERS} DESTINATION include/${PROJECT_NAME})
add_executable(jstick-test main.cpp)
target_link_libraries(jstick-test ${PROJECT_NAME})
#target_link_libraries(jstick-test ${PROJECT_NAME})

View File

@@ -14,10 +14,12 @@
* https://www.kernel.org/doc/Documentation/input/joystick-api.txt
*/
#include <fcntl.h>
#ifdef __linux__
#include <cstdio>
#include <unistd.h>
#include <linux/joystick.h>
#include <cstdint>
#include <cstring>
#include <string>
@@ -25,6 +27,10 @@
#include <filesystem>
#include <format>
#include <jstick.hpp>
#include <fcntl.h>
#include <unistd.h>
#include <linux/joystick.h>
enum class XBoxButton : uint8_t
{
@@ -356,3 +362,71 @@ int main(int argc, char *argv[])
return 0;
*/
}
#endif
#ifdef WIN32
#include <iostream>
#include <windows.h>
#include <XInput.h>
#pragma comment(lib, "XInput.lib")
class TController
{
private:
int n;
XINPUT_STATE state;
public:
TController(int num)
{
n = num;
}
XINPUT_STATE GetState()
{
ZeroMemory(&state, sizeof(XINPUT_STATE));
XInputGetState(n, &state);
return state;
}
bool IsConnected()
{
ZeroMemory(&state, sizeof(XINPUT_STATE));
DWORD statenow = XInputGetState(n, &state);
if(statenow == ERROR_SUCCESS) return true;
return false;
}
void vibrate(int LV=0, int RV=0)
{
XINPUT_VIBRATION vibration;
ZeroMemory(&vibration, sizeof(XINPUT_VIBRATION));
vibration.wLeftMotorSpeed = LV;
vibration.wRightMotorSpeed = RV;
XInputSetState(n, &vibration);
}
};
int main()
{
TController* controller0;
controller0 = new TController(0);
if (controller0->IsConnected())
{
while (controller0->IsConnected())
{
if(controller0->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_X)
{
std::cout << "A.";
controller0->vibrate(65535, 0);
}
}
}
}
#endif

View File

@@ -1,14 +1,22 @@
#include <jstick.hpp>
#ifdef UNIX
#include <linux/joystick.h>
#include <unistd.h>
#include <bits/fs_fwd.h>
#endif
#ifdef WIN32
//#include <joystickapi.h>
#endif
#include <cstddef>
#include <cstdio>
#include <fcntl.h>
#include <format>
#include <filesystem>
#include <iostream>
#include <unistd.h>
#include <bits/fs_fwd.h>
#include <J3ML/LinearAlgebra/Vector2i.hpp>
struct axis_state {

View File

@@ -0,0 +1,3 @@
//
// Created by josh on 5/22/2025.
//

View File

@@ -0,0 +1,29 @@
#include <iostream>
#include <jstick.hpp>
#include <windows.h>
#include <XInput.h>
bool jstick::Initialize()
{
}
bool jstick::JoystickDetected(int jsHandle)
{
DWORD dwUserIndex = (DWORD)jsHandle;
XINPUT_STATE state;
ZeroMemory(&state, sizeof(XINPUT_STATE));
DWORD result = XInputGetState(dwUserIndex, &state);
if (result == ERROR_SUCCESS)
{
return true;
} else {
std::cout << "Error Code: " << result << std::endl;
return false;
}
}