From 61ead16c48b408958d21784b7f8d9153ffd39d45 Mon Sep 17 00:00:00 2001 From: Daniel Stuart Baxter Date: Sun, 31 May 2015 10:39:12 -0500 Subject: [PATCH] Review Changes 8 --- src/core/system.cpp | 2 +- src/input_common/controller_state.h | 16 ++++---- src/input_common/input_common.cpp | 24 ++++++------ src/input_common/input_common.h | 6 +-- src/input_common/sdl_input/sdl_input.cpp | 48 ++++++++++++------------ src/input_common/sdl_input/sdl_input.h | 4 +- 6 files changed, 49 insertions(+), 51 deletions(-) diff --git a/src/core/system.cpp b/src/core/system.cpp index d59e12894..ce6e1ca01 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -24,7 +24,7 @@ void Init(EmuWindow* emu_window) { Kernel::Init(); HLE::Init(); VideoCore::Init(emu_window); - InputCommon::Init(InputCommon::SDL2_JOY); + InputCommon::Init(InputCommon::SDL2); } void Shutdown() { diff --git a/src/input_common/controller_state.h b/src/input_common/controller_state.h index b641a45cc..85124e896 100644 --- a/src/input_common/controller_state.h +++ b/src/input_common/controller_state.h @@ -9,12 +9,12 @@ namespace InputCommon { - struct ControllerState { - Service::HID::PadState pad_state; - s16 circle_pad_x; - s16 circle_pad_y; - u16 touch_screen_x; - u16 touch_screen_y; - }; +struct ControllerState { + Service::HID::PadState pad_state; + s16 circle_pad_x; + s16 circle_pad_y; + u16 touch_screen_x; + u16 touch_screen_y; +}; -} //namespace +} // namespace diff --git a/src/input_common/input_common.cpp b/src/input_common/input_common.cpp index ac0b51c5b..2fb898c31 100644 --- a/src/input_common/input_common.cpp +++ b/src/input_common/input_common.cpp @@ -13,24 +13,24 @@ namespace InputCommon { -//Set to nullptr until initialized by a backend +// Set to nullptr until initialized by a backend ControllerBase* g_user_input = nullptr; -//Event id for CoreTiming +// Event id for CoreTiming static int input_event; -//Ticks for CoreTiming event, same as VBlank in GPU +// Ticks for CoreTiming event, same as VBlank in GPU static u64 frame_ticks; /// Callback for CoreTiming static void InputCallback(u64 userdata, int cycles_late) { - //Call user input plugin Poll() + // Call user input plugin Poll() if (g_user_input != nullptr) { g_user_input->Poll(); } - //Reschedule recurrent event + // Reschedule recurrent event CoreTiming::ScheduleEvent(frame_ticks - cycles_late, input_event); } @@ -45,28 +45,28 @@ Service::HID::PadState ControllerBase::GetPadState() const { void Init(ControllerBackends backend) { switch (backend) { #ifdef HAS_SDL - //SDL2 backend selected - case SDL2_JOY: + // SDL2 backend selected + case SDL2: g_user_input = new SDLController(); break; #endif - //No backend selected - case NO_JOY: + // No backend selected + case NONE: break; - //What? + // What? default: break; } - //Shutdown immediately if backend failed to initialize + // Shutdown immediately if backend failed to initialize if (g_user_input != nullptr && !g_user_input->activated) { Shutdown(); return; } - //Setup CoreTiming + // Setup CoreTiming frame_ticks = 268123480 / Settings::values.gpu_refresh_rate / 16; input_event = CoreTiming::RegisterEvent("InputCommon::InputCallback", InputCallback); diff --git a/src/input_common/input_common.h b/src/input_common/input_common.h index 15e9b99e6..14c588b00 100644 --- a/src/input_common/input_common.h +++ b/src/input_common/input_common.h @@ -11,8 +11,8 @@ namespace InputCommon { /// Enum defining available backends enum ControllerBackends { - NO_JOY, - SDL2_JOY + NONE, + SDL2 }; class ControllerBase { @@ -55,6 +55,6 @@ void Init(ControllerBackends backend); void Shutdown(); /// InputCommon 'plugin' -extern ControllerBase* g_user_input; +extern std::unique_ptr g_user_input; } // namespace diff --git a/src/input_common/sdl_input/sdl_input.cpp b/src/input_common/sdl_input/sdl_input.cpp index 3668f06cc..c0e9c2147 100644 --- a/src/input_common/sdl_input/sdl_input.cpp +++ b/src/input_common/sdl_input/sdl_input.cpp @@ -27,13 +27,13 @@ SDLController::~SDLController() { bool SDLController::Init() { - //Attempt to initialize SDL with joystick only + // Attempt to initialize SDL with joystick only if (SDL_Init(SDL_INIT_JOYSTICK) != 0) { return false; } - //Attempt to open joystick with SDL - //TODO - Use DiscoverDevice to generate a list of available joysticks, then open the correct index + // Attempt to open joystick with SDL + // TODO - Use DiscoverDevice to generate a list of available joysticks, then open the correct index jpad = SDL_JoystickOpen(index); if (jpad == nullptr) { @@ -42,12 +42,12 @@ bool SDLController::Init() { device_name = SDL_JoystickNameForIndex(index); - //If joystick successfully opened, load up keymap from input configuration - //TODO - Make it NOT hardcoded + // If joystick successfully opened, load up keymap from input configuration + // TODO - Make it NOT hardcoded joypad_id = KeyMap::NewDeviceId(); SetupKeyMaps(); - //TODO - Make it NOT hardcoded + // TODO - Make it NOT hardcoded SetDeadZone(8000); return true; @@ -56,14 +56,14 @@ bool SDLController::Init() { void SDLController::DiscoverDevices() {} void SDLController::Shutdown() { - //Attempt to close joystick with SDL + // Attempt to close joystick with SDL if (jpad != nullptr) { SDL_JoystickClose(jpad); } } void SDLController::Poll() { - if (SDL_PollEvent(&input_event)) { + while (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(); @@ -72,13 +72,13 @@ void SDLController::Poll() { } void SDLController::ProcessInput() { - //Use a unique id for joystick input + // Use a unique id for joystick input int pad_id = 0; int val = 0; - //Generate the id based on input from an event + // Generate the id based on input from an event switch (input_event.type) { - //Joystick button press events + // Joystick button press events case SDL_JOYBUTTONDOWN: pad_id = 100; pad_id += input_event.jbutton.button; @@ -86,7 +86,7 @@ void SDLController::ProcessInput() { controller.pad_state.hex |= KeyMap::GetPadKey({pad_id, joypad_id}).hex; break; - //Joystick button release events + // Joystick button release events case SDL_JOYBUTTONUP: pad_id = 100; pad_id += input_event.jbutton.button; @@ -94,24 +94,24 @@ void SDLController::ProcessInput() { controller.pad_state.hex &= ~KeyMap::GetPadKey({pad_id, joypad_id}).hex; break; - //Joystick axis motion events + // 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; - } + // Make pad_id even or odd depending on positive/negative value + // Differentiates the direction of analog input + if (val > 0) pad_id += 1; - //If analog input is beyond dead zone, set it + // If analog input is beyond dead zone, set it if (CheckDeadZone(val)) { controller.pad_state.hex |= KeyMap::GetPadKey({pad_id, joypad_id}).hex; } else { controller.pad_state.hex &= ~KeyMap::GetPadKey({pad_id, joypad_id}).hex; } - //Release opposite axis + // Release opposite axis if (pad_id % 2 == 0) { controller.pad_state.hex &= ~KeyMap::GetPadKey({pad_id+1, joypad_id}).hex; } else { @@ -120,7 +120,7 @@ void SDLController::ProcessInput() { break; - //Joystick hat motion events + // Joystick hat motion events case SDL_JOYHATMOTION: pad_id = 300; pad_id += input_event.jhat.hat * 4; @@ -228,11 +228,9 @@ void SDLController::SetDeadZone(int range) { } bool SDLController::CheckDeadZone(int range) const { - if ((range < -dead_zone) || (range > dead_zone)) { - return true; - } else if ((range > -dead_zone) && (range < dead_zone)) { - return false; - } + // See if the absolute range is beyond dead zone + if (std::abs(range) > std::abs(dead_zone)) return true; + return false; } -}//namespace +}// namespace diff --git a/src/input_common/sdl_input/sdl_input.h b/src/input_common/sdl_input/sdl_input.h index 3ef8bae43..4e78404f0 100644 --- a/src/input_common/sdl_input/sdl_input.h +++ b/src/input_common/sdl_input/sdl_input.h @@ -46,7 +46,7 @@ private: SDL_Event input_event; /// Index of joystick, used for opening/closing a joystick - int index; + u32 index; /// Keymap id for this controller int joypad_id; @@ -54,4 +54,4 @@ private: int dead_zone; }; -} //namespace +} // namespace