mirror of
https://github.com/citra-emu/citra.git
synced 2024-11-24 17:01:06 +00:00
Review Changes 8
This commit is contained in:
parent
9ae6bf08a4
commit
61ead16c48
@ -24,7 +24,7 @@ void Init(EmuWindow* emu_window) {
|
|||||||
Kernel::Init();
|
Kernel::Init();
|
||||||
HLE::Init();
|
HLE::Init();
|
||||||
VideoCore::Init(emu_window);
|
VideoCore::Init(emu_window);
|
||||||
InputCommon::Init(InputCommon::SDL2_JOY);
|
InputCommon::Init(InputCommon::SDL2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shutdown() {
|
void Shutdown() {
|
||||||
|
@ -9,12 +9,12 @@
|
|||||||
|
|
||||||
namespace InputCommon {
|
namespace InputCommon {
|
||||||
|
|
||||||
struct ControllerState {
|
struct ControllerState {
|
||||||
Service::HID::PadState pad_state;
|
Service::HID::PadState pad_state;
|
||||||
s16 circle_pad_x;
|
s16 circle_pad_x;
|
||||||
s16 circle_pad_y;
|
s16 circle_pad_y;
|
||||||
u16 touch_screen_x;
|
u16 touch_screen_x;
|
||||||
u16 touch_screen_y;
|
u16 touch_screen_y;
|
||||||
};
|
};
|
||||||
|
|
||||||
} //namespace
|
} // namespace
|
||||||
|
@ -13,24 +13,24 @@
|
|||||||
|
|
||||||
namespace InputCommon {
|
namespace InputCommon {
|
||||||
|
|
||||||
//Set to nullptr until initialized by a backend
|
// Set to nullptr until initialized by a backend
|
||||||
ControllerBase* g_user_input = nullptr;
|
ControllerBase* g_user_input = nullptr;
|
||||||
|
|
||||||
//Event id for CoreTiming
|
// Event id for CoreTiming
|
||||||
static int input_event;
|
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;
|
static u64 frame_ticks;
|
||||||
|
|
||||||
/// Callback for CoreTiming
|
/// Callback for CoreTiming
|
||||||
static void InputCallback(u64 userdata, int cycles_late) {
|
static void InputCallback(u64 userdata, int cycles_late) {
|
||||||
|
|
||||||
//Call user input plugin Poll()
|
// Call user input plugin Poll()
|
||||||
if (g_user_input != nullptr) {
|
if (g_user_input != nullptr) {
|
||||||
g_user_input->Poll();
|
g_user_input->Poll();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Reschedule recurrent event
|
// Reschedule recurrent event
|
||||||
CoreTiming::ScheduleEvent(frame_ticks - cycles_late, input_event);
|
CoreTiming::ScheduleEvent(frame_ticks - cycles_late, input_event);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,28 +45,28 @@ Service::HID::PadState ControllerBase::GetPadState() const {
|
|||||||
void Init(ControllerBackends backend) {
|
void Init(ControllerBackends backend) {
|
||||||
switch (backend) {
|
switch (backend) {
|
||||||
#ifdef HAS_SDL
|
#ifdef HAS_SDL
|
||||||
//SDL2 backend selected
|
// SDL2 backend selected
|
||||||
case SDL2_JOY:
|
case SDL2:
|
||||||
g_user_input = new SDLController();
|
g_user_input = new SDLController();
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//No backend selected
|
// No backend selected
|
||||||
case NO_JOY:
|
case NONE:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//What?
|
// What?
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Shutdown immediately if backend failed to initialize
|
// Shutdown immediately if backend failed to initialize
|
||||||
if (g_user_input != nullptr && !g_user_input->activated) {
|
if (g_user_input != nullptr && !g_user_input->activated) {
|
||||||
Shutdown();
|
Shutdown();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Setup CoreTiming
|
// Setup CoreTiming
|
||||||
frame_ticks = 268123480 / Settings::values.gpu_refresh_rate / 16;
|
frame_ticks = 268123480 / Settings::values.gpu_refresh_rate / 16;
|
||||||
|
|
||||||
input_event = CoreTiming::RegisterEvent("InputCommon::InputCallback", InputCallback);
|
input_event = CoreTiming::RegisterEvent("InputCommon::InputCallback", InputCallback);
|
||||||
|
@ -11,8 +11,8 @@ namespace InputCommon {
|
|||||||
|
|
||||||
/// Enum defining available backends
|
/// Enum defining available backends
|
||||||
enum ControllerBackends {
|
enum ControllerBackends {
|
||||||
NO_JOY,
|
NONE,
|
||||||
SDL2_JOY
|
SDL2
|
||||||
};
|
};
|
||||||
|
|
||||||
class ControllerBase {
|
class ControllerBase {
|
||||||
@ -55,6 +55,6 @@ void Init(ControllerBackends backend);
|
|||||||
void Shutdown();
|
void Shutdown();
|
||||||
|
|
||||||
/// InputCommon 'plugin'
|
/// InputCommon 'plugin'
|
||||||
extern ControllerBase* g_user_input;
|
extern std::unique_ptr<ControllerBase> g_user_input;
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
@ -27,13 +27,13 @@ SDLController::~SDLController() {
|
|||||||
|
|
||||||
bool SDLController::Init() {
|
bool SDLController::Init() {
|
||||||
|
|
||||||
//Attempt to initialize SDL with joystick only
|
// Attempt to initialize SDL with joystick only
|
||||||
if (SDL_Init(SDL_INIT_JOYSTICK) != 0) {
|
if (SDL_Init(SDL_INIT_JOYSTICK) != 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Attempt to open joystick with SDL
|
// Attempt to open joystick with SDL
|
||||||
//TODO - Use DiscoverDevice to generate a list of available joysticks, then open the correct index
|
// TODO - Use DiscoverDevice to generate a list of available joysticks, then open the correct index
|
||||||
jpad = SDL_JoystickOpen(index);
|
jpad = SDL_JoystickOpen(index);
|
||||||
|
|
||||||
if (jpad == nullptr) {
|
if (jpad == nullptr) {
|
||||||
@ -42,12 +42,12 @@ bool SDLController::Init() {
|
|||||||
|
|
||||||
device_name = SDL_JoystickNameForIndex(index);
|
device_name = SDL_JoystickNameForIndex(index);
|
||||||
|
|
||||||
//If joystick successfully opened, load up keymap from input configuration
|
// If joystick successfully opened, load up keymap from input configuration
|
||||||
//TODO - Make it NOT hardcoded
|
// TODO - Make it NOT hardcoded
|
||||||
joypad_id = KeyMap::NewDeviceId();
|
joypad_id = KeyMap::NewDeviceId();
|
||||||
SetupKeyMaps();
|
SetupKeyMaps();
|
||||||
|
|
||||||
//TODO - Make it NOT hardcoded
|
// TODO - Make it NOT hardcoded
|
||||||
SetDeadZone(8000);
|
SetDeadZone(8000);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -56,14 +56,14 @@ bool SDLController::Init() {
|
|||||||
void SDLController::DiscoverDevices() {}
|
void SDLController::DiscoverDevices() {}
|
||||||
|
|
||||||
void SDLController::Shutdown() {
|
void SDLController::Shutdown() {
|
||||||
//Attempt to close joystick with SDL
|
// Attempt to close joystick with SDL
|
||||||
if (jpad != nullptr) {
|
if (jpad != nullptr) {
|
||||||
SDL_JoystickClose(jpad);
|
SDL_JoystickClose(jpad);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SDLController::Poll() {
|
void SDLController::Poll() {
|
||||||
if (SDL_PollEvent(&input_event)) {
|
while (SDL_PollEvent(&input_event)) {
|
||||||
if (input_event.type == SDL_JOYBUTTONDOWN || input_event.type == SDL_JOYBUTTONUP
|
if (input_event.type == SDL_JOYBUTTONDOWN || input_event.type == SDL_JOYBUTTONUP
|
||||||
|| input_event.type == SDL_JOYAXISMOTION || input_event.type == SDL_JOYHATMOTION) {
|
|| input_event.type == SDL_JOYAXISMOTION || input_event.type == SDL_JOYHATMOTION) {
|
||||||
ProcessInput();
|
ProcessInput();
|
||||||
@ -72,13 +72,13 @@ void SDLController::Poll() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SDLController::ProcessInput() {
|
void SDLController::ProcessInput() {
|
||||||
//Use a unique id for joystick input
|
// Use a unique id for joystick input
|
||||||
int pad_id = 0;
|
int pad_id = 0;
|
||||||
int val = 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) {
|
switch (input_event.type) {
|
||||||
//Joystick button press events
|
// Joystick button press events
|
||||||
case SDL_JOYBUTTONDOWN:
|
case SDL_JOYBUTTONDOWN:
|
||||||
pad_id = 100;
|
pad_id = 100;
|
||||||
pad_id += input_event.jbutton.button;
|
pad_id += input_event.jbutton.button;
|
||||||
@ -86,7 +86,7 @@ void SDLController::ProcessInput() {
|
|||||||
controller.pad_state.hex |= KeyMap::GetPadKey({pad_id, joypad_id}).hex;
|
controller.pad_state.hex |= KeyMap::GetPadKey({pad_id, joypad_id}).hex;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//Joystick button release events
|
// Joystick button release events
|
||||||
case SDL_JOYBUTTONUP:
|
case SDL_JOYBUTTONUP:
|
||||||
pad_id = 100;
|
pad_id = 100;
|
||||||
pad_id += input_event.jbutton.button;
|
pad_id += input_event.jbutton.button;
|
||||||
@ -94,24 +94,24 @@ void SDLController::ProcessInput() {
|
|||||||
controller.pad_state.hex &= ~KeyMap::GetPadKey({pad_id, joypad_id}).hex;
|
controller.pad_state.hex &= ~KeyMap::GetPadKey({pad_id, joypad_id}).hex;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//Joystick axis motion events
|
// Joystick axis motion events
|
||||||
case SDL_JOYAXISMOTION:
|
case SDL_JOYAXISMOTION:
|
||||||
pad_id = 200;
|
pad_id = 200;
|
||||||
pad_id += (input_event.jaxis.axis * 2);
|
pad_id += (input_event.jaxis.axis * 2);
|
||||||
val = input_event.jaxis.value;
|
val = input_event.jaxis.value;
|
||||||
|
|
||||||
if (val > 0) {
|
// Make pad_id even or odd depending on positive/negative value
|
||||||
pad_id += 1;
|
// 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)) {
|
if (CheckDeadZone(val)) {
|
||||||
controller.pad_state.hex |= KeyMap::GetPadKey({pad_id, joypad_id}).hex;
|
controller.pad_state.hex |= KeyMap::GetPadKey({pad_id, joypad_id}).hex;
|
||||||
} else {
|
} else {
|
||||||
controller.pad_state.hex &= ~KeyMap::GetPadKey({pad_id, joypad_id}).hex;
|
controller.pad_state.hex &= ~KeyMap::GetPadKey({pad_id, joypad_id}).hex;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Release opposite axis
|
// Release opposite axis
|
||||||
if (pad_id % 2 == 0) {
|
if (pad_id % 2 == 0) {
|
||||||
controller.pad_state.hex &= ~KeyMap::GetPadKey({pad_id+1, joypad_id}).hex;
|
controller.pad_state.hex &= ~KeyMap::GetPadKey({pad_id+1, joypad_id}).hex;
|
||||||
} else {
|
} else {
|
||||||
@ -120,7 +120,7 @@ void SDLController::ProcessInput() {
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//Joystick hat motion events
|
// Joystick hat motion events
|
||||||
case SDL_JOYHATMOTION:
|
case SDL_JOYHATMOTION:
|
||||||
pad_id = 300;
|
pad_id = 300;
|
||||||
pad_id += input_event.jhat.hat * 4;
|
pad_id += input_event.jhat.hat * 4;
|
||||||
@ -228,11 +228,9 @@ void SDLController::SetDeadZone(int range) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool SDLController::CheckDeadZone(int range) const {
|
bool SDLController::CheckDeadZone(int range) const {
|
||||||
if ((range < -dead_zone) || (range > dead_zone)) {
|
// See if the absolute range is beyond dead zone
|
||||||
return true;
|
if (std::abs(range) > std::abs(dead_zone)) return true;
|
||||||
} else if ((range > -dead_zone) && (range < dead_zone)) {
|
return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}//namespace
|
}// namespace
|
||||||
|
@ -46,7 +46,7 @@ private:
|
|||||||
SDL_Event input_event;
|
SDL_Event input_event;
|
||||||
|
|
||||||
/// Index of joystick, used for opening/closing a joystick
|
/// Index of joystick, used for opening/closing a joystick
|
||||||
int index;
|
u32 index;
|
||||||
|
|
||||||
/// Keymap id for this controller
|
/// Keymap id for this controller
|
||||||
int joypad_id;
|
int joypad_id;
|
||||||
@ -54,4 +54,4 @@ private:
|
|||||||
int dead_zone;
|
int dead_zone;
|
||||||
};
|
};
|
||||||
|
|
||||||
} //namespace
|
} // namespace
|
||||||
|
Loading…
Reference in New Issue
Block a user