Compare commits
10 Commits
Prerelease
...
master
Author | SHA1 | Date | |
---|---|---|---|
9117f778b3 | |||
c9e64c5fe6 | |||
ce5dbc98de | |||
bbaf07e76e | |||
f05307d318 | |||
6f993eab34 | |||
e07ac37699 | |||
2be8d8e7a1 | |||
71094053a2 | |||
cda59803a5 |
@@ -26,16 +26,18 @@ CPMAddPackage(
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
file(GLOB_RECURSE jstick_HEADERS "include/*.hpp")
|
||||
file(GLOB_RECURSE jstick_SRC "src/*.cpp")
|
||||
file(GLOB_RECURSE SHARED_SOURCES "src/shared/*.cpp")
|
||||
file(GLOB_RECURSE WINDOWS_SOURCES "src/platform/windows/*.cpp")
|
||||
file(GLOB_RECURSE LINUX_SOURCES "src/platform/linux/*.cpp")
|
||||
|
||||
include_directories("include")
|
||||
|
||||
if (UNIX)
|
||||
add_library(jstick SHARED ${jstick_SRC})
|
||||
if (UNIX AND NOT APPLE)
|
||||
add_library(jstick SHARED ${SHARED_SOURCES} ${LINUX_SOURCES})
|
||||
endif()
|
||||
|
||||
if (WIN32)
|
||||
add_library(jstick STATIC ${jstick_SRC})
|
||||
add_library(jstick STATIC ${SHARED_SOURCES} ${WINDOWS_SOURCES})
|
||||
endif()
|
||||
|
||||
target_include_directories(jstick PUBLIC
|
||||
|
19
README.md
Normal file
19
README.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# Josh Stick API
|
||||
|
||||
A single-header, single-namespace, single API for reading controller and joystick device input on Linux and Windows.
|
||||
|
||||
## Features
|
||||
* Windows Support via XInput API
|
||||
* Linux Support via jsdev API
|
||||
|
||||
## TODO List
|
||||
|
||||
* Support multiple devices at once.
|
||||
* Note that XInput only supports a maximum of 4 devices.
|
||||
|
||||
|
||||
## Contributing
|
||||
|
||||
## Licensing
|
||||
|
||||
## Other Notes
|
@@ -11,6 +11,10 @@
|
||||
// TODO: Implement simulate DPad Axis button.
|
||||
// TODO: Make sure Ash applies @ redlobster website.
|
||||
// TODO: Periodically poll the system to discover device connection and disconnection.
|
||||
// TODO: Rumble Motor Support
|
||||
// TODO: Gyroscope Support
|
||||
// TODO: Calibration API
|
||||
// TODO: The static events are not propagating.
|
||||
|
||||
namespace jstick {
|
||||
|
||||
@@ -35,8 +39,6 @@ namespace jstick {
|
||||
private:
|
||||
};
|
||||
|
||||
|
||||
|
||||
enum class ControllerType {
|
||||
XBox,
|
||||
PS3, PS4, PS5,
|
||||
@@ -47,8 +49,7 @@ namespace jstick {
|
||||
|
||||
|
||||
|
||||
enum class XBoxButton : uint8_t
|
||||
{
|
||||
enum class XBoxButton : uint8_t {
|
||||
ButtonA = 0,
|
||||
ButtonB = 1,
|
||||
ButtonX = 2,
|
||||
@@ -60,35 +61,42 @@ namespace jstick {
|
||||
TheBigOne = 8,
|
||||
ThumbL = 9,
|
||||
ThumbR = 10,
|
||||
DPadDown = 11,
|
||||
DPadRight = 12,
|
||||
DPadUp = 13,
|
||||
DPadLeft = 14,
|
||||
};
|
||||
|
||||
/// This event is fired when a new joystick device is detected.
|
||||
// TODO: Call JoystickConnected for each device found on library runtime start.
|
||||
static Event<int> JoystickConnected;
|
||||
|
||||
inline Event<int> JoystickConnected;
|
||||
/// This event is fired when a joystick device is unplugged or otherwise dropped.
|
||||
static Event<int> JoystickDisconnected;
|
||||
|
||||
inline Event<int> JoystickDisconnected;
|
||||
/// This event is fired when a joystick button is pressed.
|
||||
static Event<XBoxButton> ButtonPressed;
|
||||
|
||||
inline Event<XBoxButton> ButtonPressed;
|
||||
/// This event is fired when a xbox button is pressed.
|
||||
static Event<XBoxButton> ButtonReleased;
|
||||
static Event<Vector2> LeftThumbstickMoved;
|
||||
static Event<Vector2> RightThumbstickMoved;
|
||||
static Event<Vector2> DPadMoved;
|
||||
static Event<short> LeftTriggerMoved;
|
||||
static Event<short> RightTriggerMoved;
|
||||
//Event<int, JoystickAxis> AxisMoved;
|
||||
|
||||
inline Event<XBoxButton> ButtonReleased;
|
||||
///
|
||||
inline Event<Vector2> LeftThumbstickMoved;
|
||||
///
|
||||
inline Event<Vector2> RightThumbstickMoved;
|
||||
///
|
||||
inline Event<Vector2> DPadMoved;
|
||||
///
|
||||
inline Event<short> LeftTriggerMoved;
|
||||
///
|
||||
inline Event<short> RightTriggerMoved;
|
||||
|
||||
bool Initialize();
|
||||
bool Cleanup();
|
||||
|
||||
/// @return true if the input system treats the DPad as a Vector2 axis, or false if treated as four buttons.
|
||||
bool GetDPadIsAxisOrButtons();
|
||||
|
||||
/// Checks the device files for a joystick with the given handle.
|
||||
/// @return True if the joystick device file exists.
|
||||
/// @note Joystick devices are indexed by an incrementing integer ID, on *nix systems.
|
||||
bool JoystickDetected(int jsHandle = 0);
|
||||
bool JoystickDetected(int hwid = 0);
|
||||
|
||||
/// @return True if there is a connected joystick device with the matching Hardware ID.
|
||||
/// @note jstick supports only one device at a time, currently.
|
||||
@@ -103,23 +111,25 @@ namespace jstick {
|
||||
void JoystickServiceUpdate();
|
||||
void ReadEventLoop();
|
||||
|
||||
bool IsButtonDown(const XBoxButton& btn);
|
||||
short GetLeftTrigger();
|
||||
short GetRightTrigger();
|
||||
bool IsButtonDown(const XBoxButton& btn, int hwid = 0);
|
||||
short GetLeftTrigger(int hwid = 0);
|
||||
short GetRightTrigger(int hwid = 0);
|
||||
|
||||
|
||||
Vector2 GetLeftThumbstickAxis(float deadzone = 1e-2f);
|
||||
Vector2 GetRightThumbstickAxis(float deadzone = 1e-2f);
|
||||
Vector2 GetDPadAxis(float deadzone = 1e-2f);
|
||||
Vector2 GetLeftThumbstickAxis(float deadzone = 1e-2f, int hwid = 0);
|
||||
Vector2 GetRightThumbstickAxis(float deadzone = 1e-2f, int hwid = 0);
|
||||
Vector2 GetDPadAxis(float deadzone = 1e-2f, int hwid = 0);
|
||||
|
||||
|
||||
// TODO: Find a better name for the function set that converts -32768/+32768 range to -1/+1
|
||||
// TODO: Because Normalization specifically refers to clamping the Axis Vector to Unit Vector range,
|
||||
// TODO: And I wan that set of functions as part of the API as well.
|
||||
Vector2 GetLeftThumbstickAxisNormalized(float deadzone = 1e-3f);
|
||||
Vector2 GetRightThumbstickAxisNormalized(float deadzone = 1e-3f);
|
||||
Vector2 GetDPadAxisNormalized(float deadzone = 1e-3f);
|
||||
Vector2 GetLeftThumbstickAxisNormalized(float deadzone = 1e-3f, int hwid = 0);
|
||||
Vector2 GetRightThumbstickAxisNormalized(float deadzone = 1e-3f, int hwid = 0);
|
||||
Vector2 GetDPadAxisNormalized(float deadzone = 1e-3f, int hwid = 0);
|
||||
|
||||
float GetLeftTriggerNormalized();
|
||||
float GetRightTriggerNormalized();
|
||||
float GetLeftTriggerNormalized(int hwid = 0);
|
||||
float GetRightTriggerNormalized(int hwid = 0);
|
||||
|
||||
void Vibrate(int LV = 0, int RV = 0, int hwid = 0);
|
||||
}
|
||||
|
394
main.cpp
394
main.cpp
@@ -1,358 +1,84 @@
|
||||
/**
|
||||
* Author: Jason White
|
||||
*
|
||||
* Description:
|
||||
* Reads joystick/gamepad events and displays them.
|
||||
*
|
||||
* Compile:
|
||||
* gcc joystick.c -o joystick
|
||||
*
|
||||
* Run:
|
||||
* ./joystick [/dev/input/jsX]
|
||||
*
|
||||
* See also:
|
||||
* https://www.kernel.org/doc/Documentation/input/joystick-api.txt
|
||||
*/
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <cstdio>
|
||||
#include <unistd.h>
|
||||
#include <linux/joystick.h>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <jstick.hpp>
|
||||
#include <thread>
|
||||
|
||||
enum class XBoxButton : uint8_t
|
||||
{
|
||||
ButtonA = 0,
|
||||
ButtonB = 1,
|
||||
ButtonX = 2,
|
||||
ButtonY = 3,
|
||||
BumperL = 4,
|
||||
BumperR = 5,
|
||||
Back = 6,
|
||||
Start = 7,
|
||||
TheBigOne = 8,
|
||||
ThumbL = 9,
|
||||
ThumbR = 10,
|
||||
};
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
/// Reads a joystick event from the joystick device.
|
||||
/// @returns 0 on success. Otherwise -1 is returned.
|
||||
int read_event(int fd, struct js_event *event)
|
||||
{
|
||||
ssize_t bytes;
|
||||
int main() {
|
||||
bool success = jstick::Initialize();
|
||||
|
||||
bytes = read(fd, event, sizeof(*event));
|
||||
|
||||
if (bytes == sizeof(*event))
|
||||
return 0;
|
||||
|
||||
/// Error, could not read full event.
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// @returns the number of axes on the controller or 0 if an error occurs.
|
||||
size_t get_axis_count(int fd)
|
||||
{
|
||||
__u8 axes;
|
||||
|
||||
if (ioctl(fd, JSIOCGAXES, &axes) == -1)
|
||||
return 0;
|
||||
|
||||
return axes;
|
||||
}
|
||||
|
||||
/// @returns the number of buttons on the controller or 0 if an error occurs.
|
||||
size_t get_button_count(int fd)
|
||||
{
|
||||
__u8 buttons;
|
||||
if (ioctl(fd, JSIOCGBUTTONS, &buttons) == -1)
|
||||
return 0;
|
||||
|
||||
return buttons;
|
||||
}
|
||||
|
||||
/// Current state of an axis.
|
||||
|
||||
|
||||
struct Axes {
|
||||
short LeftThumbX = 0;
|
||||
short LeftThumbY = 0;
|
||||
short RightThumbX = 0;
|
||||
short RightThumbY = 0;
|
||||
short DPadX = 0;
|
||||
short DPadY = 0;
|
||||
// TODO Left & Right triggers should start at -32767 because 0 would be pressed half way.
|
||||
short LeftTrigger = 0;
|
||||
short RightTrigger = 0;
|
||||
};
|
||||
|
||||
|
||||
/// Keeps track of the current axis state.
|
||||
/// @note This function assumes that axes are numbered starting from 0, and that
|
||||
/// the X axis is an even number, and the Y axis is an odd number. However, this
|
||||
/// is usually a safe assumption.
|
||||
/// @returns the axis that the event indicated.
|
||||
size_t get_axis_state(struct js_event *event, struct Axes& axes)
|
||||
{
|
||||
/// Mappings on my device, meaning these assumptions are incorrect.
|
||||
// LStick X = 0
|
||||
// LStick Y = 1
|
||||
// LTrigger = 2
|
||||
// RStick X = 3
|
||||
// RStick Y = 4
|
||||
// RTrigger = 5
|
||||
// DirPad X = 6
|
||||
// DirPad Y = 7
|
||||
|
||||
size_t axis = event->number;
|
||||
short value = event->value;
|
||||
|
||||
// X-Box 360 & X-Box One are the same.
|
||||
if (axis == 0) axes.LeftThumbX = value;
|
||||
if (axis == 1) axes.LeftThumbY = value;
|
||||
if (axis == 2) axes.LeftTrigger = value;
|
||||
if (axis == 3) axes.RightThumbX = value;
|
||||
if (axis == 4) axes.RightThumbY = value;
|
||||
if (axis == 5) axes.RightTrigger = value;
|
||||
if (axis == 6) axes.DPadX = value;
|
||||
if (axis == 7) axes.DPadY = value;
|
||||
|
||||
// "Sony PLAYSTATION(R)3 Controller", Has 6 axes.
|
||||
/*
|
||||
// D-PAD UP -> Button 12
|
||||
// D-Pad DOWN -> Button 13
|
||||
// D-Pad LEFT -> Button 14
|
||||
// D-Pad RIGHT -> Button 15
|
||||
if (axis == 0) axes.LeftThumbX = value;
|
||||
if (axis == 1) axes.LeftThumbY = value;
|
||||
if (axis == 3) axes.RightThumbX = value;
|
||||
if (axis == 4) axes.RightThumbY = value;
|
||||
if (axis == 2) axes.LeftTrigger = value;
|
||||
if (axis == 5) axes.RightTrigger = value;
|
||||
*/
|
||||
|
||||
|
||||
// Playstation 4
|
||||
/*
|
||||
if (axis == 0) axes.LeftThumbX = value;
|
||||
if (axis == 1) axes.LeftThumbY = value;
|
||||
if (axis == 3) axes.RightThumbX = value;
|
||||
if (axis == 4) axes.RightThumbY = value;
|
||||
if (axis == 2) axes.LeftTrigger = value;
|
||||
if (axis == 5) axes.RightTrigger = value;
|
||||
if (axis == 6) axes.DPadX = value;
|
||||
if (axis == 7) axes.DPadY = value;
|
||||
*/
|
||||
|
||||
// "8BitDo Pro 2" in Direct Input mode. This controller also supports the Xbox mode.
|
||||
/*
|
||||
if (axis == 0) axes.LeftThumbX = value;
|
||||
if (axis == 1) axes.LeftThumbY = value;
|
||||
if (axis == 2) axes.RightThumbX = value;
|
||||
if (axis == 3) axes.RightThumbY = value;
|
||||
if (axis == 4) axes.RightTrigger = value;
|
||||
if (axis == 5) axes.LeftTrigger = value;
|
||||
if (axis == 6) axes.DPadX = value;
|
||||
if (axis == 7) axes.DPadY = value;
|
||||
*/
|
||||
|
||||
|
||||
std::cout << std::format("axis_index: {}, val: {}", axis, value) << std::endl;
|
||||
std::cout << std::format("lthumb: {},{} ltrigger: {}, rthumb: {},{}, rtrigger: {}, dp: {},{}", axes.LeftThumbX, axes.LeftThumbY, axes.LeftTrigger, axes.RightThumbX, axes.RightThumbY, axes.RightTrigger, axes.DPadX, axes.DPadY) << std::endl;
|
||||
|
||||
/*size_t axis = event->number / 2;
|
||||
|
||||
if (axis < 3)
|
||||
{
|
||||
if (event->number % 2 == 0)
|
||||
axes[axis].x = event->value;
|
||||
else
|
||||
axes[axis].y = event->value;
|
||||
if (!success) {
|
||||
std::cerr << "Could not initialize jstick!" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return axis;*/
|
||||
return axis;
|
||||
}
|
||||
|
||||
uint8_t get_num_axes(int joystick_handle) {
|
||||
char num_axes;
|
||||
ioctl(joystick_handle, JSIOCGAXES, &num_axes);
|
||||
return num_axes;
|
||||
}
|
||||
|
||||
std::string get_joystick_name(int joystick_handle)
|
||||
{
|
||||
char name_buf[128];
|
||||
if (ioctl(joystick_handle, JSIOCGNAME(sizeof(name_buf)), name_buf) < 0)
|
||||
strncpy(name_buf, "Unknown", sizeof(name_buf));
|
||||
return std::string(name_buf);
|
||||
}
|
||||
|
||||
int get_joystick_driver_ver(int joystick_handle)
|
||||
{
|
||||
int version;
|
||||
ioctl(joystick_handle, JSIOCGVERSION, &version);
|
||||
return version;
|
||||
}
|
||||
|
||||
bool check_for_joystick_device()
|
||||
{
|
||||
return std::filesystem::exists("/dev/input/js0");
|
||||
}
|
||||
|
||||
int js;
|
||||
bool device_open;
|
||||
|
||||
bool connect_device() {
|
||||
js = open("/dev/input/js0", O_NONBLOCK);
|
||||
|
||||
if (js == -1)
|
||||
perror("Could not open joystick");
|
||||
|
||||
|
||||
std::cout << get_joystick_name(js) << std::endl;
|
||||
std::cout << get_joystick_driver_ver(js) << std::endl;
|
||||
std::cout << get_axis_count(js) << std::endl;
|
||||
}
|
||||
|
||||
void disconnect_device() {
|
||||
close(js);
|
||||
}
|
||||
|
||||
#define ENUM_TO_STRING(var) (#var)
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
std::cout << jstick::NumJoysticksDetected() << std::endl;
|
||||
if (!jstick::JoystickDetected()) {
|
||||
std::cerr << "No devices detected!" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int hwid = jstick::Connect(0);
|
||||
|
||||
if (hwid != -1) {
|
||||
|
||||
std::cout << jstick::GetDeviceName(hwid) << std::endl;
|
||||
|
||||
|
||||
jstick::ButtonPressed += [](jstick::XBoxButton btn){
|
||||
std::cout << std::format("Button Pressed: {}", (int)btn) << std::endl;
|
||||
};
|
||||
|
||||
jstick::ButtonReleased += [](jstick::XBoxButton btn) {
|
||||
std::cout << std::format("Button Released: {}", (int)btn) << std::endl;
|
||||
};
|
||||
|
||||
jstick::DPadMoved += [](Vector2 v) {
|
||||
std::cout << std::format("DPad Moved {},{}", v.x, v.y) << std::endl;
|
||||
};
|
||||
|
||||
jstick::LeftThumbstickMoved += [](Vector2 v){
|
||||
std::cout << std::format("LThumb Moved {},{}", v.x, v.y) << std::endl;
|
||||
};
|
||||
|
||||
jstick::RightThumbstickMoved += [](Vector2 v){
|
||||
std::cout << std::format("RThumb Moved {},{}", v.x, v.y) << std::endl;
|
||||
};
|
||||
|
||||
jstick::LeftTriggerMoved += [] (short val) {
|
||||
std::cout << std::format("LTrigger Moved {}", val) << std::endl;
|
||||
};
|
||||
|
||||
jstick::RightTriggerMoved += [](short val) {
|
||||
std::cout << std::format("RTrigger Moved {}", val) << std::endl;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
while(true) {
|
||||
while (jstick::JoystickDetected()) {
|
||||
jstick::ReadEventLoop();
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
/*
|
||||
const char *device;
|
||||
|
||||
struct js_event event;
|
||||
struct Axes axes = {};
|
||||
size_t axis;
|
||||
|
||||
if (argc > 1)
|
||||
device = argv[1];
|
||||
else
|
||||
device = "/dev/input/js0";
|
||||
|
||||
js = open(device, O_NONBLOCK);
|
||||
|
||||
if (js == -1)
|
||||
perror("Could not open joystick");
|
||||
else
|
||||
{
|
||||
std::cout << get_joystick_name(js) << std::endl;
|
||||
std::cout << get_joystick_driver_ver(js) << std::endl;
|
||||
std::cout << get_axis_count(js) << std::endl;
|
||||
}
|
||||
|
||||
|
||||
// Vibrate the joystick device by how much the thumbsticks are moved.
|
||||
Vector2 axisL = jstick::GetLeftThumbstickAxis();
|
||||
Vector2 axisR = jstick::GetRightThumbstickAxis();
|
||||
|
||||
/// This loop will exit if the controller is unplugged.
|
||||
|
||||
bool had = false;
|
||||
|
||||
while (1)
|
||||
{
|
||||
bool has_file = check_for_joystick_device();
|
||||
|
||||
if (has_file && !had) {
|
||||
if (js == -1)
|
||||
{
|
||||
std::cout << "Controller plugged in!" << std::endl;
|
||||
js = open(device, O_NONBLOCK);
|
||||
|
||||
if (js == -1)
|
||||
perror("Could not open joystick");
|
||||
}
|
||||
|
||||
} else if (!has_file && had) {
|
||||
std::cout << "Controller unplugged!" << std::endl;
|
||||
if (js != -1) {
|
||||
close(js);
|
||||
js = -1;
|
||||
}
|
||||
}
|
||||
|
||||
had = has_file;
|
||||
jstick::Vibrate(axisL.Magnitude(), axisR.Magnitude());
|
||||
|
||||
|
||||
while (read_event(js, &event) == 0)
|
||||
if (jstick::IsButtonDown(jstick::XBoxButton::Back))
|
||||
std::cout << "Back Button Pressed" << std::endl;
|
||||
|
||||
if (jstick::IsButtonDown(jstick::XBoxButton::Start))
|
||||
std::cout << "Start Button Pressed" << std::endl;
|
||||
|
||||
if (jstick::IsButtonDown(jstick::XBoxButton::ButtonA))
|
||||
std::cout << "A Button Pressed" << std::endl;
|
||||
|
||||
if (jstick::IsButtonDown(jstick::XBoxButton::ButtonB))
|
||||
std::cout << "B Button Pressed" << std::endl;
|
||||
|
||||
if (jstick::IsButtonDown(jstick::XBoxButton::ButtonX))
|
||||
std::cout << "X Button Pressed" << std::endl;
|
||||
|
||||
if (jstick::IsButtonDown(jstick::XBoxButton::ButtonY))
|
||||
std::cout << "Y Button Pressed" << std::endl;
|
||||
|
||||
if (jstick::IsButtonDown(jstick::XBoxButton::BumperL))
|
||||
std::cout << "Left Bumper Pressed" << std::endl;
|
||||
|
||||
if (jstick::IsButtonDown(jstick::XBoxButton::BumperR))
|
||||
std::cout << "Right Bumper Pressed" << std::endl;
|
||||
|
||||
if (jstick::IsButtonDown(jstick::XBoxButton::ThumbL))
|
||||
std::cout << "Left Thumb Pressed" << std::endl;
|
||||
|
||||
if (jstick::IsButtonDown(jstick::XBoxButton::ThumbR))
|
||||
std::cout << "Right Thumb Pressed" << std::endl;
|
||||
|
||||
if (!jstick::GetDPadIsAxisOrButtons())
|
||||
{
|
||||
switch (event.type)
|
||||
{
|
||||
case JS_EVENT_BUTTON:
|
||||
printf("Button %u %s\n", event.number, event.value ? "pressed" : "released");
|
||||
break;
|
||||
case JS_EVENT_AXIS:
|
||||
axis = get_axis_state(&event, axes);
|
||||
//if (axis < 3)
|
||||
//printf("Axis %zu at (%6d, %6d)\n", axis, axes[axis].x, axes[axis].y);
|
||||
break;
|
||||
case JS_EVENT_INIT:
|
||||
default:
|
||||
/// Ignore init events.
|
||||
break;
|
||||
}
|
||||
if (jstick::IsButtonDown(jstick::XBoxButton::DPadUp))
|
||||
std::cout << "DPad Up Pressed" << std::endl;
|
||||
|
||||
fflush(stdout);
|
||||
if (jstick::IsButtonDown(jstick::XBoxButton::DPadDown))
|
||||
std::cout << "DPad Down Pressed" << std::endl;
|
||||
|
||||
if (jstick::IsButtonDown(jstick::XBoxButton::DPadLeft))
|
||||
std::cout << "DPad Left Pressed" << std::endl;
|
||||
|
||||
if (jstick::IsButtonDown(jstick::XBoxButton::DPadRight))
|
||||
std::cout << "DPad Right Pressed" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
std::this_thread::sleep_for(10ms);
|
||||
}
|
||||
|
||||
|
||||
close(js);
|
||||
return 0;
|
||||
*/
|
||||
}
|
||||
|
@@ -1,14 +1,15 @@
|
||||
#include <jstick.hpp>
|
||||
|
||||
#include <linux/joystick.h>
|
||||
#include <unistd.h>
|
||||
#include <bits/fs_fwd.h>
|
||||
#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 {
|
||||
@@ -47,6 +48,9 @@ std::string js_device_file(int hwid) {
|
||||
return std::format("/dev/input/js{}", hwid);
|
||||
}
|
||||
|
||||
|
||||
bool jstick::Initialize() { return true;}
|
||||
|
||||
bool jstick::JoystickDetected(int jsHandle) {
|
||||
return std::filesystem::exists(js_device_file(jsHandle));
|
||||
}
|
||||
@@ -71,6 +75,8 @@ std::string jstick::GetDeviceName(int hwid) {
|
||||
return std::string(name_buf);
|
||||
}
|
||||
|
||||
bool jstick::GetDPadIsAxisOrButtons() { return true; }
|
||||
|
||||
int jstick::Connect(int hwid) {
|
||||
if (js_connected) {
|
||||
std::cerr << "Joystick already connected! Support for multiple joysticks is in-progress!" << std::endl;
|
||||
@@ -125,6 +131,8 @@ void ProcessButtonEvent(uint8_t button_index, bool value) {
|
||||
} else {
|
||||
jstick::ButtonReleased.Invoke(btn);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void ProcessAxisEvent(uint8_t axis, short value) {
|
||||
@@ -138,8 +146,6 @@ void ProcessAxisEvent(uint8_t axis, short value) {
|
||||
|
||||
if (device_type == jstick::ControllerType::XBox)
|
||||
{
|
||||
|
||||
|
||||
if (axis == 0) l_thumb.x = value;
|
||||
if (axis == 1) l_thumb.y = value;
|
||||
if (axis == 2) l_trigger = value;
|
||||
@@ -205,37 +211,37 @@ void jstick::ReadEventLoop() {
|
||||
}
|
||||
}
|
||||
|
||||
bool jstick::IsButtonDown(const XBoxButton &btn) {
|
||||
bool jstick::IsButtonDown(const XBoxButton &btn, int hwid) {
|
||||
return btn_state[(uint8_t)btn];
|
||||
}
|
||||
|
||||
Vector2 jstick::GetLeftThumbstickAxis(float deadzone) { return l_thumb;}
|
||||
Vector2 jstick::GetRightThumbstickAxis(float deadzone) { return r_thumb; }
|
||||
Vector2 jstick::GetDPadAxis(float deadzone) { return dpad; }
|
||||
Vector2 jstick::GetLeftThumbstickAxis(float deadzone, int hwid) { return l_thumb;}
|
||||
Vector2 jstick::GetRightThumbstickAxis(float deadzone, int hwid) { return r_thumb; }
|
||||
Vector2 jstick::GetDPadAxis(float deadzone, int hwid) { return dpad; }
|
||||
|
||||
|
||||
short jstick::GetRightTrigger() { return r_trigger;}
|
||||
short jstick::GetLeftTrigger() { return l_trigger; }
|
||||
short jstick::GetRightTrigger(int hwid) { return r_trigger;}
|
||||
short jstick::GetLeftTrigger(int hwid) { return l_trigger; }
|
||||
|
||||
constexpr float short_range = 32768.f;
|
||||
|
||||
Vector2 jstick::GetLeftThumbstickAxisNormalized(float deadzone) {
|
||||
return l_thumb / short_range;
|
||||
Vector2 jstick::GetLeftThumbstickAxisNormalized(float deadzone, int hwid) {
|
||||
return GetLeftThumbstickAxis(deadzone, hwid) / short_range;
|
||||
}
|
||||
|
||||
Vector2 jstick::GetRightThumbstickAxisNormalized(float deadzone) {
|
||||
return r_thumb / short_range;
|
||||
Vector2 jstick::GetRightThumbstickAxisNormalized(float deadzone, int hwid) {
|
||||
return GetRightThumbstickAxis(deadzone, hwid) / short_range;
|
||||
}
|
||||
|
||||
Vector2 jstick::GetDPadAxisNormalized(float deadzone) {
|
||||
Vector2 jstick::GetDPadAxisNormalized(float deadzone, int hwid) {
|
||||
return dpad / short_range;
|
||||
}
|
||||
|
||||
float jstick::GetLeftTriggerNormalized() {
|
||||
float jstick::GetLeftTriggerNormalized(int hwid) {
|
||||
return l_trigger / short_range;
|
||||
}
|
||||
|
||||
float jstick::GetRightTriggerNormalized() {
|
||||
float jstick::GetRightTriggerNormalized(int hwid) {
|
||||
return r_trigger / short_range;
|
||||
}
|
||||
|
||||
@@ -246,3 +252,7 @@ jstick::ControllerType jstick::GetDeviceTypeFromName(const std::string &name) {
|
||||
|
||||
return ControllerType::Unknown;
|
||||
}
|
||||
|
||||
void jstick::Vibrate(int LV, int RV, int hwid) {
|
||||
|
||||
}
|
243
src/platform/windows/jstick_xinput.cpp
Normal file
243
src/platform/windows/jstick_xinput.cpp
Normal file
@@ -0,0 +1,243 @@
|
||||
#include <iostream>
|
||||
#include <jstick.hpp>
|
||||
#include <windows.h>
|
||||
#include <XInput.h>
|
||||
|
||||
#pragma comment(lib, "XInput.lib")
|
||||
|
||||
|
||||
// TODO: Track device state of N devices
|
||||
// TODO: Keep prev_state, so we can fire Events when buttons are pressed/releaed, etc.
|
||||
|
||||
#define MAX_DEVICES 4
|
||||
#define SHORT_RANGE 32768.f;
|
||||
|
||||
bool device_exists[MAX_DEVICES];
|
||||
XINPUT_STATE cur_state[MAX_DEVICES];
|
||||
XINPUT_STATE prev_state[MAX_DEVICES];
|
||||
|
||||
bool jstick::Initialize() { return true; }
|
||||
|
||||
bool jstick::Cleanup() { return true; }
|
||||
|
||||
void jstick::JoystickServiceUpdate() { }
|
||||
|
||||
// XInput cannot tell us what type of device it is, so assume it's a legitimate Microsoft XB360 controller.
|
||||
std::string jstick::GetDeviceName(int hwid) { return "Microsoft X-Box 360 pad"; }
|
||||
|
||||
jstick::ControllerType jstick::GetDeviceTypeFromName(const std::string &name) {
|
||||
// On Windows, we assume the standard controller, we have no way of knowing specifics currently.
|
||||
// ~josh.
|
||||
return ControllerType::XBox;
|
||||
}
|
||||
|
||||
int jstick::NumJoysticksDetected() {
|
||||
// TODO: Check it properly!!
|
||||
if (JoystickDetected())
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Vector2 jstick::GetDPadAxis(float deadzone, int hwid)
|
||||
{
|
||||
// "Synthesize" a DPad vector based upon whether the buttons are pressed.
|
||||
// This vector will not be analog as is the case with the linux/jsdev implementation.
|
||||
Vector2 dpad;
|
||||
|
||||
if (IsButtonDown(XBoxButton::DPadDown))
|
||||
dpad.y += SHORT_RANGE;
|
||||
if (IsButtonDown(XBoxButton::DPadRight))
|
||||
dpad.x += SHORT_RANGE;
|
||||
if (IsButtonDown(XBoxButton::DPadLeft))
|
||||
dpad.x -= SHORT_RANGE;
|
||||
if (IsButtonDown(XBoxButton::DPadUp))
|
||||
dpad.y -= SHORT_RANGE;
|
||||
|
||||
return dpad;
|
||||
|
||||
}
|
||||
|
||||
/// XInput treats the DPad as buttons.
|
||||
bool jstick::GetDPadIsAxisOrButtons() { return false; }
|
||||
|
||||
/// XInput does not work via an event-poll loop, so we do nothing here.
|
||||
void jstick::ReadEventLoop()
|
||||
{
|
||||
prev_state[0] = cur_state[0];
|
||||
|
||||
|
||||
DWORD dwUserIndex = (DWORD)0;
|
||||
|
||||
XINPUT_STATE state;
|
||||
ZeroMemory(&state, sizeof(XINPUT_STATE));
|
||||
|
||||
DWORD result = XInputGetState(dwUserIndex, &state);
|
||||
|
||||
if (result == ERROR_SUCCESS)
|
||||
{
|
||||
cur_state[0] = state;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void jstick::Vibrate(int LV, int RV, int hwid)
|
||||
{
|
||||
|
||||
XINPUT_VIBRATION vibration;
|
||||
ZeroMemory(&vibration, sizeof(XINPUT_VIBRATION));
|
||||
|
||||
vibration.wLeftMotorSpeed = LV;
|
||||
vibration.wRightMotorSpeed = RV;
|
||||
|
||||
XInputSetState(hwid, &vibration);
|
||||
}
|
||||
|
||||
bool jstick::JoystickDetected(int hwid)
|
||||
{
|
||||
DWORD dwUserIndex = (DWORD)hwid;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Vector2 jstick::GetLeftThumbstickAxis(float deadzone, int hwid)
|
||||
{
|
||||
XINPUT_STATE state;
|
||||
ZeroMemory(&state, sizeof(XINPUT_STATE));
|
||||
|
||||
DWORD result = XInputGetState(hwid, &state);
|
||||
|
||||
if (result != ERROR_SUCCESS) {
|
||||
return {0, 0};
|
||||
}
|
||||
|
||||
return Vector2(state.Gamepad.sThumbLX, state.Gamepad.sThumbLY);
|
||||
}
|
||||
|
||||
short jstick::GetLeftTrigger(int hwid)
|
||||
{
|
||||
XINPUT_STATE state;
|
||||
ZeroMemory(&state, sizeof(XINPUT_STATE));
|
||||
|
||||
DWORD result = XInputGetState(hwid, &state);
|
||||
|
||||
if (result != ERROR_SUCCESS) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return state.Gamepad.bLeftTrigger;
|
||||
}
|
||||
|
||||
short jstick::GetRightTrigger(int hwid)
|
||||
{
|
||||
XINPUT_STATE state;
|
||||
ZeroMemory(&state, sizeof(XINPUT_STATE));
|
||||
|
||||
DWORD result = XInputGetState(hwid, &state);
|
||||
|
||||
if (result != ERROR_SUCCESS) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return state.Gamepad.bRightTrigger;
|
||||
}
|
||||
|
||||
Vector2 jstick::GetRightThumbstickAxis(float deadzone, int hwid)
|
||||
{
|
||||
XINPUT_STATE state;
|
||||
ZeroMemory(&state, sizeof(XINPUT_STATE));
|
||||
|
||||
DWORD result = XInputGetState(hwid, &state);
|
||||
|
||||
if (result != ERROR_SUCCESS) {
|
||||
return {0, 0};
|
||||
}
|
||||
|
||||
return Vector2(state.Gamepad.sThumbRX, state.Gamepad.sThumbRY);
|
||||
}
|
||||
|
||||
int jstick::Connect(int hwid)
|
||||
{
|
||||
return hwid;
|
||||
}
|
||||
|
||||
bool jstick::Disconnect(int hwid) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool jstick::Connected(int hwid) { return true; }
|
||||
|
||||
Vector2 jstick::GetRightThumbstickAxisNormalized(float deadzone, int hwid)
|
||||
{
|
||||
Vector2 axisL = jstick::GetLeftThumbstickAxis(deadzone, hwid);
|
||||
if (axisL.Magnitude() > 0.f)
|
||||
return axisL.Normalized();
|
||||
return {0,0};
|
||||
}
|
||||
|
||||
Vector2 jstick::GetLeftThumbstickAxisNormalized(float deadzone, int hwid)
|
||||
{
|
||||
Vector2 axisR = jstick::GetRightThumbstickAxis(deadzone, hwid);
|
||||
|
||||
|
||||
return axisR.Normalized();
|
||||
}
|
||||
|
||||
|
||||
|
||||
float jstick::GetLeftTriggerNormalized(int hwid) {
|
||||
return GetLeftTrigger(hwid) / SHORT_RANGE;
|
||||
}
|
||||
|
||||
float jstick::GetRightTriggerNormalized(int hwid) {
|
||||
return GetRightTrigger(hwid) / SHORT_RANGE;
|
||||
}
|
||||
|
||||
|
||||
bool jstick::IsButtonDown(const XBoxButton& btn, int hwid)
|
||||
{
|
||||
XINPUT_STATE state;
|
||||
ZeroMemory(&state, sizeof(XINPUT_STATE));
|
||||
|
||||
DWORD result = XInputGetState(hwid, &state);
|
||||
|
||||
if (result != ERROR_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (btn) {
|
||||
case XBoxButton::Back: if (state.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) { return true; } break;
|
||||
case XBoxButton::Start: if (state.Gamepad.wButtons & XINPUT_GAMEPAD_START) { return true; } break;
|
||||
case XBoxButton::ThumbL: if (state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB) { return true; } break;
|
||||
case XBoxButton::ThumbR: if (state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) { return true; } break;
|
||||
case XBoxButton::ButtonA: if (state.Gamepad.wButtons & XINPUT_GAMEPAD_A) { return true; } break;
|
||||
case XBoxButton::ButtonB: if (state.Gamepad.wButtons & XINPUT_GAMEPAD_B) { return true; } break;
|
||||
case XBoxButton::ButtonX: if (state.Gamepad.wButtons & XINPUT_GAMEPAD_X) { return true; } break;
|
||||
case XBoxButton::ButtonY: if (state.Gamepad.wButtons & XINPUT_GAMEPAD_Y) { return true; } break;
|
||||
|
||||
case XBoxButton::BumperL: if (state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) { return true; } break;
|
||||
case XBoxButton::BumperR: if (state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) { return true; } break;
|
||||
|
||||
case XBoxButton::DPadUp: if (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP) { return true; } break;
|
||||
case XBoxButton::DPadLeft: if (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) { return true; } break;
|
||||
case XBoxButton::DPadDown: if (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN) { return true; } break;
|
||||
case XBoxButton::DPadRight: if (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) { return true; } break;
|
||||
default: return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
1
src/shared/jstick_shared.cpp
Normal file
1
src/shared/jstick_shared.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#pragma bruh
|
Reference in New Issue
Block a user