aboutsummaryrefslogtreecommitdiff
path: root/backends/events/sdl
diff options
context:
space:
mode:
authorBastien Bouclet2017-12-10 10:50:08 +0100
committerBastien Bouclet2017-12-26 21:11:04 +0100
commitac78058fcdf5d0e98522edf837ce250130e45eaa (patch)
tree8d176e07669e7c28d7a1b8d5ac1ade205b91b5d4 /backends/events/sdl
parent9cb2e4357715873f192d40ddd7570116cde98d25 (diff)
downloadscummvm-rg350-ac78058fcdf5d0e98522edf837ce250130e45eaa.tar.gz
scummvm-rg350-ac78058fcdf5d0e98522edf837ce250130e45eaa.tar.bz2
scummvm-rg350-ac78058fcdf5d0e98522edf837ce250130e45eaa.zip
SDL: Initial support for game controllers
The button mapping is that of the Vita port Co-Authored-By: rsn8887 <rsn8887@users.noreply.github.com>
Diffstat (limited to 'backends/events/sdl')
-rw-r--r--backends/events/sdl/sdl-events.cpp250
-rw-r--r--backends/events/sdl/sdl-events.h15
2 files changed, 212 insertions, 53 deletions
diff --git a/backends/events/sdl/sdl-events.cpp b/backends/events/sdl/sdl-events.cpp
index d01d9c6c4a..09530675c4 100644
--- a/backends/events/sdl/sdl-events.cpp
+++ b/backends/events/sdl/sdl-events.cpp
@@ -77,7 +77,7 @@ static uint32 convUTF8ToUTF32(const char *src) {
SdlEventSource::SdlEventSource()
: EventSource(), _scrollLock(false), _joystick(0), _lastScreenID(0), _graphicsManager(0), _queuedFakeMouseMove(false)
#if SDL_VERSION_ATLEAST(2, 0, 0)
- , _queuedFakeKeyUp(false), _fakeKeyUp()
+ , _queuedFakeKeyUp(false), _fakeKeyUp(), _controller(nullptr)
#endif
{
// Reset mouse state
@@ -90,16 +90,30 @@ SdlEventSource::SdlEventSource()
error("Could not initialize SDL: %s", SDL_GetError());
}
+#if SDL_VERSION_ATLEAST(2, 0, 0)
+ if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) == -1) {
+ error("Could not initialize SDL: %s", SDL_GetError());
+ }
+#endif
+
// Enable joystick
if (SDL_NumJoysticks() > joystick_num) {
- _joystick = SDL_JoystickOpen(joystick_num);
- debug("Using joystick: %s",
#if SDL_VERSION_ATLEAST(2, 0, 0)
- SDL_JoystickName(_joystick)
+ if (SDL_IsGameController(joystick_num)) {
+ _controller = SDL_GameControllerOpen(joystick_num);
+ debug("Using game controller: %s", SDL_GameControllerName(_controller));
+ } else
+#endif
+ {
+ _joystick = SDL_JoystickOpen(joystick_num);
+ debug("Using joystick: %s",
+#if SDL_VERSION_ATLEAST(2, 0, 0)
+ SDL_JoystickName(_joystick)
#else
- SDL_JoystickName(joystick_num)
+ SDL_JoystickName(joystick_num)
#endif
- );
+ );
+ }
} else {
warning("Invalid joystick: %d", joystick_num);
}
@@ -107,6 +121,10 @@ SdlEventSource::SdlEventSource()
}
SdlEventSource::~SdlEventSource() {
+#if SDL_VERSION_ATLEAST(2, 0, 0)
+ if (_controller)
+ SDL_GameControllerClose(_controller);
+#endif
if (_joystick)
SDL_JoystickClose(_joystick);
}
@@ -541,12 +559,6 @@ bool SdlEventSource::dispatchSDLEvent(SDL_Event &ev, Common::Event &event) {
return handleMouseButtonDown(ev, event);
case SDL_MOUSEBUTTONUP:
return handleMouseButtonUp(ev, event);
- case SDL_JOYBUTTONDOWN:
- return handleJoyButtonDown(ev, event);
- case SDL_JOYBUTTONUP:
- return handleJoyButtonUp(ev, event);
- case SDL_JOYAXISMOTION:
- return handleJoyAxisMotion(ev, event);
#if SDL_VERSION_ATLEAST(2, 0, 0)
case SDL_MOUSEWHEEL: {
@@ -630,6 +642,30 @@ bool SdlEventSource::dispatchSDLEvent(SDL_Event &ev, Common::Event &event) {
}
+ if (_joystick) {
+ switch (ev.type) {
+ case SDL_JOYBUTTONDOWN:
+ return handleJoyButtonDown(ev, event);
+ case SDL_JOYBUTTONUP:
+ return handleJoyButtonUp(ev, event);
+ case SDL_JOYAXISMOTION:
+ return handleJoyAxisMotion(ev, event);
+ }
+ }
+
+#if SDL_VERSION_ATLEAST(2, 0, 0)
+ if (_controller) {
+ switch (ev.type) {
+ case SDL_CONTROLLERBUTTONDOWN:
+ return handleControllerButton(ev, event, false);
+ case SDL_CONTROLLERBUTTONUP:
+ return handleControllerButton(ev, event, true);
+ case SDL_CONTROLLERAXISMOTION:
+ return handleControllerAxisMotion(ev, event);
+ }
+ }
+#endif
+
return false;
}
@@ -867,70 +903,178 @@ bool SdlEventSource::handleJoyButtonUp(SDL_Event &ev, Common::Event &event) {
}
bool SdlEventSource::handleJoyAxisMotion(SDL_Event &ev, Common::Event &event) {
+ if (ev.jaxis.axis == JOY_XAXIS) {
+ _km.joy_x = ev.jaxis.value;
+ return handleAxisToMouseMotion(_km.joy_x, _km.joy_y);
+ } else if (ev.jaxis.axis == JOY_YAXIS) {
+ _km.joy_y = ev.jaxis.value;
+ return handleAxisToMouseMotion(_km.joy_x, _km.joy_y);
+ }
- int axis = ev.jaxis.value;
-#ifdef JOY_ANALOG
- // conversion factor between keyboard mouse and joy axis value
- int vel_to_axis = (1500 / MULTIPLIER);
+ return false;
+}
+
+#if SDL_VERSION_ATLEAST(2, 0, 0)
+bool SdlEventSource::handleControllerButton(const SDL_Event &ev, Common::Event &event, bool buttonUp) {
+ using namespace Common;
+
+ struct ControllerEventMapping {
+ EventType normalType;
+ KeyState normalKeystate;
+ EventType modifierType;
+ KeyState modifierKeystate;
+ };
+
+ static const ControllerEventMapping mapping[] = {
+ // SDL_CONTROLLER_BUTTON_A: Left mouse button
+ { EVENT_LBUTTONDOWN, KeyState(), EVENT_LBUTTONDOWN, KeyState() },
+ // SDL_CONTROLLER_BUTTON_B: Right mouse button
+ { EVENT_RBUTTONDOWN, KeyState(), EVENT_RBUTTONDOWN, KeyState() },
+ // SDL_CONTROLLER_BUTTON_X: Period (+R_trigger: Space)
+ { EVENT_KEYDOWN, KeyState(KEYCODE_PERIOD, '.'), EVENT_KEYDOWN, KeyState(KEYCODE_SPACE, ASCII_SPACE) },
+ // SDL_CONTROLLER_BUTTON_Y: Escape (+R_trigger: Return)
+ { EVENT_KEYDOWN, KeyState(KEYCODE_ESCAPE, ASCII_ESCAPE), EVENT_KEYDOWN, KeyState(KEYCODE_RETURN, ASCII_RETURN) },
+ // SDL_CONTROLLER_BUTTON_BACK: Virtual keyboard (+R_trigger: Predictive Input Dialog)
+#ifdef ENABLE_VKEYBD
+ { EVENT_VIRTUAL_KEYBOARD, KeyState(), EVENT_PREDICTIVE_DIALOG, KeyState() },
#else
- if (axis > JOY_DEADZONE) {
- axis -= JOY_DEADZONE;
- } else if (axis < -JOY_DEADZONE) {
- axis += JOY_DEADZONE;
- } else
- axis = 0;
+ { EVENT_INVALID, KeyState(), EVENT_PREDICTIVE_DIALOG, KeyState() },
#endif
+ // SDL_CONTROLLER_BUTTON_GUIDE: Unmapped
+ { EVENT_INVALID, KeyState(), EVENT_INVALID, KeyState() },
+ // SDL_CONTROLLER_BUTTON_START: ScummVM in game menu
+ { EVENT_MAINMENU, KeyState(), EVENT_MAINMENU, KeyState() },
+ // SDL_CONTROLLER_BUTTON_LEFTSTICK: Unmapped
+ { EVENT_INVALID, KeyState(), EVENT_INVALID, KeyState() },
+ // SDL_CONTROLLER_BUTTON_RIGHTSTICK: Unmapped
+ { EVENT_INVALID, KeyState(), EVENT_INVALID, KeyState() },
+ // SDL_CONTROLLER_BUTTON_LEFTSHOULDER: Game menu
+ { EVENT_KEYDOWN, KeyState(KEYCODE_F5, ASCII_F5), EVENT_KEYDOWN, KeyState(KEYCODE_F5, ASCII_F5) },
+ // SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: Modifier + Shift
+ { EVENT_KEYDOWN, KeyState(KEYCODE_INVALID, 0, KBD_SHIFT), EVENT_KEYDOWN, KeyState(KEYCODE_INVALID, 0, 0) },
+ // SDL_CONTROLLER_BUTTON_DPAD_UP: Up (+R_trigger: Up+Right)
+ { EVENT_KEYDOWN, KeyState(KEYCODE_KP8, 0), EVENT_KEYDOWN, KeyState(KEYCODE_KP9, 0) },
+ // SDL_CONTROLLER_BUTTON_DPAD_DOWN: Down (+R_trigger: Down+Left)
+ { EVENT_KEYDOWN, KeyState(KEYCODE_KP2, 0), EVENT_KEYDOWN, KeyState(KEYCODE_KP1, 0) },
+ // SDL_CONTROLLER_BUTTON_DPAD_LEFT: Left (+R_trigger: Up+Left)
+ { EVENT_KEYDOWN, KeyState(KEYCODE_KP4, 0), EVENT_KEYDOWN, KeyState(KEYCODE_KP7, 0) },
+ // SDL_CONTROLLER_BUTTON_DPAD_RIGHT: Right (+R_trigger: Down+Right)
+ { EVENT_KEYDOWN, KeyState(KEYCODE_KP6, 0), EVENT_KEYDOWN, KeyState(KEYCODE_KP3, 0) }
+ };
+
+ if (ev.cbutton.button > SDL_CONTROLLER_BUTTON_DPAD_RIGHT) {
+ warning("Unknown SDL controller button: '%d'", ev.cbutton.button);
+ return false;
+ }
- if (ev.jaxis.axis == JOY_XAXIS) {
-#ifdef JOY_ANALOG
- _km.joy_x = axis;
-#else
- if (axis != 0) {
- _km.x_vel = (axis > 0) ? 1 * MULTIPLIER:-1 * MULTIPLIER;
- _km.x_down_count = 1;
+ if (!_km.modifier) {
+ event.type = mapping[ev.cbutton.button].normalType;
+ event.kbd = mapping[ev.cbutton.button].normalKeystate;
+ } else {
+ event.type = mapping[ev.cbutton.button].modifierType;
+ event.kbd = mapping[ev.cbutton.button].modifierKeystate;
+ }
+
+ // Setting the mouse speed modifier after filling the event structure above
+ // ensures that the shift key events are correctly handled
+ if (ev.cbutton.button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER) {
+ // Right shoulder is the modifier button that makes the mouse go slower
+ // and allows access to an extended layout while pressed.
+ _km.modifier = !buttonUp;
+ }
+
+ if (event.type == EVENT_LBUTTONDOWN || event.type == EVENT_RBUTTONDOWN) {
+ processMouseEvent(event, _km.x / MULTIPLIER, _km.y / MULTIPLIER);
+ }
+
+ if (buttonUp) {
+ // The event mapping table is for button down events. If we received a button up event,
+ // transform the event type to the corresponding up type.
+ if (event.type == EVENT_KEYDOWN) {
+ event.type = EVENT_KEYUP;
+ } else if (event.type == EVENT_LBUTTONDOWN) {
+ event.type = EVENT_LBUTTONUP;
+ } else if (event.type == EVENT_RBUTTONDOWN) {
+ event.type = EVENT_RBUTTONUP;
} else {
- _km.x_vel = 0;
- _km.x_down_count = 0;
+ // Handled in key down
+ event.type = EVENT_INVALID;
}
+ }
+
+ return event.type != EVENT_INVALID;
+}
+
+bool SdlEventSource::handleControllerAxisMotion(const SDL_Event &ev, Common::Event &event) {
+ if (ev.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX) {
+ _km.joy_x = ev.caxis.value;
+ return handleAxisToMouseMotion(_km.joy_x, _km.joy_y);
+ } else if (ev.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY) {
+ _km.joy_y = ev.caxis.value;
+ return handleAxisToMouseMotion(_km.joy_x, _km.joy_y);
+ }
+
+ return false;
+}
#endif
- } else if (ev.jaxis.axis == JOY_YAXIS) {
-#ifndef JOY_INVERT_Y
- axis = -axis;
-#endif
-#ifdef JOY_ANALOG
- _km.joy_y = -axis;
-#else
- if (axis != 0) {
- _km.y_vel = (-axis > 0) ? 1 * MULTIPLIER: -1 * MULTIPLIER;
- _km.y_down_count = 1;
- } else {
- _km.y_vel = 0;
- _km.y_down_count = 0;
- }
+
+bool SdlEventSource::handleAxisToMouseMotion(int16 xAxis, int16 yAxis) {
+#ifdef JOY_INVERT_Y
+ yAxis = -yAxis;
#endif
- }
+
#ifdef JOY_ANALOG
- // radial and scaled analog joystick deadzone
- float analogX = (float)_km.joy_x;
- float analogY = (float)_km.joy_y;
+ // conversion factor between keyboard mouse and joy axis value
+ int vel_to_axis = (1500 / MULTIPLIER);
+
+ // radial and scaled deadzone
+
+ float analogX = (float)xAxis;
+ float analogY = (float)yAxis;
float deadZone = (float)JOY_DEADZONE;
if (g_system->hasFeature(OSystem::kFeatureJoystickDeadzone))
deadZone = (float)ConfMan.getInt("joystick_deadzone") * 1000.0f;
- float scalingFactor = 1.0f;
- float magnitude = 0.0f;
- magnitude = sqrt(analogX * analogX + analogY * analogY);
+ float magnitude = sqrt(analogX * analogX + analogY * analogY);
if (magnitude >= deadZone) {
_km.x_down_count = 0;
_km.y_down_count = 0;
- scalingFactor = 1.0f / magnitude * (magnitude - deadZone) / (32769.0f - deadZone);
+ float scalingFactor = 1.0f / magnitude * (magnitude - deadZone) / (32769.0f - deadZone);
_km.x_vel = (int16)(analogX * scalingFactor * 32768.0f / vel_to_axis);
_km.y_vel = (int16)(analogY * scalingFactor * 32768.0f / vel_to_axis);
} else {
_km.x_vel = 0;
_km.y_vel = 0;
}
+#else
+ if (xAxis > JOY_DEADZONE) {
+ xAxis -= JOY_DEADZONE;
+ } else if (xAxis < -JOY_DEADZONE) {
+ xAxis += JOY_DEADZONE;
+ } else
+ xAxis = 0;
+ if (yAxis > JOY_DEADZONE) {
+ yAxis -= JOY_DEADZONE;
+ } else if (yAxis < -JOY_DEADZONE) {
+ yAxis += JOY_DEADZONE;
+ } else
+ yAxis = 0;
+
+ if (xAxis != 0) {
+ _km.x_vel = (xAxis > 0) ? 1 * MULTIPLIER:-1 * MULTIPLIER;
+ _km.x_down_count = 1;
+ } else {
+ _km.x_vel = 0;
+ _km.x_down_count = 0;
+ }
+ if (yAxis != 0) {
+ _km.y_vel = (yAxis > 0) ? 1 * MULTIPLIER: -1 * MULTIPLIER;
+ _km.y_down_count = 1;
+ } else {
+ _km.y_vel = 0;
+ _km.y_down_count = 0;
+ }
#endif
return false;
diff --git a/backends/events/sdl/sdl-events.h b/backends/events/sdl/sdl-events.h
index 5fd3cb7ea5..cea944fb1a 100644
--- a/backends/events/sdl/sdl-events.h
+++ b/backends/events/sdl/sdl-events.h
@@ -80,6 +80,11 @@ protected:
/** Joystick */
SDL_Joystick *_joystick;
+#if SDL_VERSION_ATLEAST(2, 0, 0)
+ /** Game controller */
+ SDL_GameController *_controller;
+#endif
+
/** Last screen id for checking if it was modified */
int _lastScreenID;
@@ -117,9 +122,19 @@ protected:
virtual bool handleJoyAxisMotion(SDL_Event &ev, Common::Event &event);
virtual bool handleKbdMouse(Common::Event &event);
+#if SDL_VERSION_ATLEAST(2, 0, 0)
+ virtual bool handleControllerButton(const SDL_Event &ev, Common::Event &event, bool buttonUp);
+ virtual bool handleControllerAxisMotion(const SDL_Event &ev, Common::Event &event);
+#endif
+
//@}
/**
+ * Update the virtual mouse according to a joystick or game controller axis position change
+ */
+ bool handleAxisToMouseMotion(int16 xAxis, int16 yAxis);
+
+ /**
* Assigns the mouse coords to the mouse event. Furthermore notify the
* graphics manager about the position change.
*/