diff --git a/src/input_common/controller_state.h b/src/input_common/controller_state.h index 65eec9d6c..b641a45cc 100644 --- a/src/input_common/controller_state.h +++ b/src/input_common/controller_state.h @@ -10,7 +10,7 @@ namespace InputCommon { struct ControllerState { - Service::HID::PadState jpad_state; + Service::HID::PadState pad_state; s16 circle_pad_x; s16 circle_pad_y; u16 touch_screen_x; diff --git a/src/input_common/input_common.cpp b/src/input_common/input_common.cpp index 00edfc949..33a4d2c5d 100644 --- a/src/input_common/input_common.cpp +++ b/src/input_common/input_common.cpp @@ -35,6 +35,11 @@ std::string ControllerBase::GetDeviceName() const { return device_name; } + +const Service::HID::PadState ControllerBase::GetPadState() const { + return controller.pad_state; +} + void Init(ControllerBackends backend) { switch(backend) { diff --git a/src/input_common/input_common.h b/src/input_common/input_common.h index fe4351439..f8ce1cd5f 100644 --- a/src/input_common/input_common.h +++ b/src/input_common/input_common.h @@ -35,9 +35,15 @@ public: ///Returns internal name of currently selected device. Expose this to the UI std::string GetDeviceName() const; + ///Returns internal pad state + const Service::HID::PadState GetPadState() const; + protected: ///Internal name of currently selected device std::string device_name; + + ///Internal view this specific object will have of 3DS input states + ControllerState controller; }; ///Initialize the user input system diff --git a/src/input_common/sdl_input/sdl_input.cpp b/src/input_common/sdl_input/sdl_input.cpp index 53d941d36..ad7d9a38e 100644 --- a/src/input_common/sdl_input/sdl_input.cpp +++ b/src/input_common/sdl_input/sdl_input.cpp @@ -8,13 +8,15 @@ namespace InputCommon { SDLController::SDLController() { device_name = "SDL2::NONE"; - j_pad = nullptr; + jpad = nullptr; index = 0; - controller_state.circle_pad_x = 0; - controller_state.circle_pad_y = 0; - controller_state.touch_screen_x = 0; - controller_state.touch_screen_y = 0; + controller.circle_pad_x = 0; + controller.circle_pad_y = 0; + controller.touch_screen_x = 0; + controller.touch_screen_y = 0; + + Init(); } SDLController::~SDLController() { @@ -30,9 +32,10 @@ bool SDLController::Init() { //Attempt to open joystick with SDL //TODO - Use DiscoverDevice to generate a list of available joysticks, then open the correct index - j_pad = SDL_JoystickOpen(index); + jpad = SDL_JoystickOpen(index); - if(j_pad == nullptr) { + if(jpad == nullptr) { + std::cout<<"What\n"; return false; } } @@ -41,14 +44,57 @@ void SDLController::DiscoverDevices() {} void SDLController::ShutDown() { //Attempt to close joystick with SDL - if(j_pad != nullptr) { - SDL_JoystickClose(j_pad); + if(jpad != nullptr) { + SDL_JoystickClose(jpad); } //Delete the current user input plugin delete g_user_input; } -void SDLController::Poll() {} +void SDLController::Poll() { + if(SDL_PollEvent(&input_event)) { + if((input_event.type == SDL_JOYBUTTONDOWN) || (input_event.type == SDL_JOYBUTTONUP) + || (input_event.type == SDL_JOYAXISMOTION) || (input_event.type == SDL_JOYHATMOTION)) { + ProcessInput(); + } + } +} + +void SDLController::ProcessInput() { + //Use a unique id for joystick input + int pad_id = 0; + int val = 0; + + //Pressed state, for joystick buttons only + bool pressed = false; + + //Generate the id based on input from an event + switch(input_event.type) { + //Joystick button press events + case SDL_JOYBUTTONDOWN: + pad_id = 100; + pad_id += input_event.jbutton.button; + pressed = true; + break; + + //Joystick button release events + case SDL_JOYBUTTONUP: + pad_id = 100; + pad_id += input_event.jbutton.button; + pressed = false; + break; + + //Joystick axis motion events + case SDL_JOYAXISMOTION: + pad_id = 200; + pad_id += (input_event.jaxis.axis * 2); + val = input_event.jaxis.value; + + if(val > 0) { + pad_id += 1; + } + } +} }//namespace diff --git a/src/input_common/sdl_input/sdl_input.h b/src/input_common/sdl_input/sdl_input.h index 4ffb67c3b..065308383 100644 --- a/src/input_common/sdl_input/sdl_input.h +++ b/src/input_common/sdl_input/sdl_input.h @@ -27,18 +27,17 @@ public: ///Shuts down all joysticks opened by SDL2 void ShutDown(); + ///Processes input when polling + void ProcessInput(); + ///SDL event used for polling SDL_Event input_event; private: - SDL_Joystick* j_pad; + SDL_Joystick* jpad; ///Index of joystick, used for opening/closing a joystick int index; - - ///Internal view this specific object will have of 3DS input states - ControllerState controller_state; - }; } //namespace