#pragma once // SDL2 input processor of the SDL2 port (BreaknesSDL). // // The architecture mirrors the managed IOProcessor (Breaknes/IOProcessing.cs): // real input devices (keyboard, game controllers) are polled via the SDL2 Input // API and turned into events; each attached device then translates the events // it is bound to into SetState calls in BreaksCore (issue #516). #include #include #include #include "IOConfig.h" #ifdef _WIN32 #include "SDL.h" #else #include #endif namespace BreaknesSDL { /// /// The maximum number of simultaneously opened game controllers. /// Event names refer to the controller slot: "Controller0_ButtonA", ... /// const int kMaxControllers = 4; /// /// One input event, in the spirit of the managed IOEvent. /// The name is the string that the bindings refer to: SDL key names /// ("Up", "Down", "Left", "Right", "A", "S", "Z", "X", "Return", ...) or /// game controller events ("Controller0_ButtonA", "Controller0_DPadUp", /// "Controller0_AxisLeftX-", "Controller0_TriggerLeft", ...). /// struct IOEvent { std::string name; uint32_t value = 0; // 0/1 }; /// /// A device instance created in BreaksCore and attached to a motherboard port. /// struct AttachedDevice { size_t handle = (size_t)-1; int port = -1; IOConfigDevice device; }; /// /// One open SDL game controller (a slot). /// struct SDLController { SDL_GameController* gamepad = nullptr; SDL_JoystickID instance_id = 0; // Last digital state of the axes, to emit events only on transitions. // axis_digital[axis][0] = negative direction ("-"), [1] = positive ("+"). // Set to -1 ("unknown") when the controller is opened so that the first // axis motion always emits. int axis_digital[SDL_CONTROLLER_AXIS_MAX][2] = {}; }; /// /// SDL2 input processor: turns SDL input events into IOEvents and forwards them /// to the devices attached to the BreaksCore motherboard via the bindings. /// class IOProcessor { std::vector controllers; // fixed-size slots (kMaxControllers) std::map controller_slot; // joystick instance id -> slot std::vector devices; std::vector event_queue; int FindFreeControllerSlot(); int GetControllerSlot(SDL_JoystickID instance_id); void OnControllerAdded(int device_index); void OnControllerRemoved(SDL_JoystickID instance_id); void PushEvent(const char* name, uint32_t value); void PushAxisDigital(int slot, SDL_GameControllerAxis axis, const char* event_name_neg, const char* event_name_pos, int16_t value); public: IOProcessor(); ~IOProcessor(); /// /// Feed an SDL event to the processor (called from the main event loop). /// void ProcessEvent(const SDL_Event& event); /// /// Apply the accumulated events to the attached devices (SetState in BreaksCore). /// void Dispatch(); /// /// Create device instances from the config file and attach them to the board ports. /// void AttachDevicesToBoard(const char* board_name, const char* config_path); /// /// Detach and dispose all device instances. /// void DetachDevicesFromBoard(); /// /// Take the first pending event with value != 0 (a press). The rest of the /// queue is discarded. Used by the configurator to grab a key/button for a binding. /// bool TakeFirstPress(IOEvent& out_event); /// /// Name of a game controller button, e.g. "ButtonA", "DPadUp". /// static const char* ButtonToString(SDL_GameControllerButton button); }; }