Files
jstick/include/jstick.hpp
2025-03-17 19:02:46 -05:00

105 lines
3.1 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.
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();
public:
protected:
unsigned int hwid;
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,
};
Event<int> JoystickConnected;
Event<int> JoystickDisconnected;
Event<XBoxButton> ButtonPressed;
Event<XBoxButton> ButtonReleased;
Event<Vector2> LeftThumbstickMoved;
Event<Vector2> RightThumbstickMoved;
Event<Vector2> DPadMoved;
Event<short> LeftTriggerMoved;
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.
bool Connect(int hwid = 0);
bool Disconnect(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();
}