Review Changes 8
This commit is contained in:
parent
9ae6bf08a4
commit
61ead16c48
6 changed files with 49 additions and 51 deletions
|
@ -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() {
|
||||
|
|
|
@ -9,12 +9,12 @@
|
|||
|
||||
namespace InputCommon {
|
||||
|
||||
struct ControllerState {
|
||||
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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<ControllerBase> g_user_input;
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -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)) {
|
||||
// See if the absolute range is beyond dead zone
|
||||
if (std::abs(range) > std::abs(dead_zone)) return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}//namespace
|
||||
}// namespace
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue