128 lines
3.9 KiB
C++
128 lines
3.9 KiB
C++
/// jstick - Dead Simple Game Controller Input Wrapper
|
|
/// Developed and maintained by Josh O'Leary @ Redacted Software
|
|
/// (c) 2025 redacted.cc
|
|
/// This work is dedicated to the public domain.
|
|
|
|
#pragma once
|
|
#include <cstdint>
|
|
#include <Event.h>
|
|
#include <J3ML/LinearAlgebra.hpp>
|
|
|
|
// 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 {
|
|
|
|
/// A data structure that represents a common symbol for a device button,
|
|
/// which has different enum IDs depending on the brand / style of the device.
|
|
struct ButtonMapping {};
|
|
|
|
namespace Buttons {}
|
|
|
|
class Joystick {
|
|
Joystick(int hwid = 0);
|
|
|
|
public:
|
|
protected:
|
|
unsigned int hwid;
|
|
private:
|
|
};
|
|
|
|
class XBoxJoystick : public Joystick {
|
|
public:
|
|
protected:
|
|
private:
|
|
};
|
|
|
|
enum class ControllerType {
|
|
XBox,
|
|
PS3, PS4, PS5,
|
|
Switch,
|
|
Generic,
|
|
Unknown,
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
};
|
|
|
|
/// This event is fired when a new joystick device is detected.
|
|
// TODO: Call JoystickConnected for each device found on library runtime start.
|
|
inline Event<int> JoystickConnected;
|
|
|
|
/// This event is fired when a joystick device is unplugged or otherwise dropped.
|
|
inline Event<int> JoystickDisconnected;
|
|
|
|
/// This event is fired when a joystick button is pressed.
|
|
inline Event<XBoxButton> ButtonPressed;
|
|
|
|
/// This event is fired when a xbox button is pressed.
|
|
inline Event<XBoxButton> ButtonReleased;
|
|
inline Event<Vector2> LeftThumbstickMoved;
|
|
inline Event<Vector2> RightThumbstickMoved;
|
|
inline Event<Vector2> DPadMoved;
|
|
inline Event<short> LeftTriggerMoved;
|
|
inline Event<short> RightTriggerMoved;
|
|
//Event<int, JoystickAxis> AxisMoved;
|
|
|
|
|
|
bool Initialize();
|
|
bool Cleanup();
|
|
|
|
/// 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);
|
|
|
|
/// @return True if there is a connected joystick device with the matching Hardware ID.
|
|
/// @note jstick supports only one device at a time, currently.
|
|
bool Connected(int hwid = 0);
|
|
/// Attempts to connect and initialize a joystick device with the given Hardware ID.
|
|
int Connect(int hwid = 0);
|
|
bool Disconnect(int hwid = 0);
|
|
|
|
ControllerType GetDeviceTypeFromName(const std::string& name);
|
|
std::string GetDeviceName(int hwid = 0);
|
|
int NumJoysticksDetected();
|
|
void JoystickServiceUpdate();
|
|
void ReadEventLoop();
|
|
|
|
bool IsButtonDown(const XBoxButton& btn);
|
|
short GetLeftTrigger();
|
|
short GetRightTrigger();
|
|
|
|
|
|
Vector2 GetLeftThumbstickAxis(float deadzone = 1e-2f);
|
|
Vector2 GetRightThumbstickAxis(float deadzone = 1e-2f);
|
|
Vector2 GetDPadAxis(float deadzone = 1e-2f);
|
|
|
|
|
|
// 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);
|
|
|
|
float GetLeftTriggerNormalized();
|
|
float GetRightTriggerNormalized();
|
|
}
|