ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

sdl-events.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/scummsys.h"
00024 
00025 #if defined(SDL_BACKEND)
00026 
00027 #include "backends/events/sdl/sdl-events.h"
00028 #include "backends/platform/sdl/sdl.h"
00029 #include "backends/graphics/graphics.h"
00030 #include "common/config-manager.h"
00031 #include "common/textconsole.h"
00032 #include "common/fs.h"
00033 
00034 //ResidualVM specific:
00035 #if defined(__amigaos4__)
00036 // KEYCODE_LESS and KEYCODE_GREATER are already defined in AmigaOS, inside
00037 // include/include_h/intuition/intuition.h (bug #3121350)
00038 #if defined(KEYCODE_LESS) && defined(KEYCODE_GREATER)
00039 #undef KEYCODE_LESS
00040 #undef KEYCODE_GREATER
00041 #endif
00042 #endif
00043 
00044 // FIXME move joystick defines out and replace with confile file options
00045 // we should really allow users to map any key to a joystick button
00046 
00047 // #define JOY_INVERT_Y
00048 #define JOY_XAXIS 0
00049 #define JOY_YAXIS 1
00050 // buttons
00051 #define JOY_BUT_LMOUSE 0
00052 #define JOY_BUT_RMOUSE 2
00053 #define JOY_BUT_ESCAPE 3
00054 #define JOY_BUT_PERIOD 1
00055 #define JOY_BUT_SPACE 4
00056 #define JOY_BUT_F5 5
00057 #ifdef ENABLE_VKEYBD
00058 #define JOY_BUT_VKEYBOARD 7
00059 #endif
00060 
00061 
00062 #if SDL_VERSION_ATLEAST(2, 0, 0)
00063 #define GAMECONTROLLERDB_FILE "gamecontrollerdb.txt"
00064 
00065 static uint32 convUTF8ToUTF32(const char *src) {
00066     uint32 utf32 = 0;
00067 
00068     char *dst = SDL_iconv_string(
00069 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
00070                                  "UTF-32BE",
00071 #else
00072                                  "UTF-32LE",
00073 #endif
00074                                  "UTF-8", src, SDL_strlen(src) + 1);
00075 
00076     if (dst) {
00077         utf32 = *((uint32 *)dst);
00078         SDL_free(dst);
00079     }
00080 
00081     return utf32;
00082 }
00083 
00084 void SdlEventSource::loadGameControllerMappingFile() {
00085     bool loaded = false;
00086     if (ConfMan.hasKey("controller_map_db")) {
00087         Common::FSNode file = Common::FSNode(ConfMan.get("controller_map_db"));
00088         if (file.exists()) {
00089             if (SDL_GameControllerAddMappingsFromFile(file.getPath().c_str()) < 0)
00090                 error("File %s not valid: %s", file.getPath().c_str(), SDL_GetError()); 
00091             else {
00092                 loaded = true;
00093                 debug("Game controller DB file loaded: %s", file.getPath().c_str());
00094             }
00095         } else
00096             warning("Game controller DB file not found: %s", file.getPath().c_str());
00097     }
00098     if (!loaded && ConfMan.hasKey("extrapath")) {
00099         Common::FSNode dir = Common::FSNode(ConfMan.get("extrapath"));
00100         Common::FSNode file = dir.getChild(GAMECONTROLLERDB_FILE);
00101         if (file.exists()) {
00102             if (SDL_GameControllerAddMappingsFromFile(file.getPath().c_str()) < 0)
00103                 error("File %s not valid: %s", file.getPath().c_str(), SDL_GetError()); 
00104             else
00105                 debug("Game controller DB file loaded: %s", file.getPath().c_str());
00106         }
00107     }
00108 }
00109 #endif
00110 
00111 SdlEventSource::SdlEventSource()
00112     : EventSource(), _scrollLock(false), _joystick(0), _lastScreenID(0), _graphicsManager(0), _queuedFakeMouseMove(false)
00113 #if SDL_VERSION_ATLEAST(2, 0, 0)
00114       , _queuedFakeKeyUp(false), _fakeKeyUp(), _controller(nullptr)
00115 #endif
00116       {
00117     // Reset mouse state
00118     memset(&_km, 0, sizeof(_km));
00119 
00120     int joystick_num = ConfMan.getInt("joystick_num");
00121     if (joystick_num >= 0) {
00122         // Initialize SDL joystick subsystem
00123         if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1) {
00124             error("Could not initialize SDL: %s", SDL_GetError());
00125         }
00126 
00127 #if SDL_VERSION_ATLEAST(2, 0, 0)
00128         if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) == -1) {
00129             error("Could not initialize SDL: %s", SDL_GetError());
00130         }
00131         loadGameControllerMappingFile();
00132 #endif
00133 
00134         openJoystick(joystick_num);
00135     }
00136 }
00137 
00138 SdlEventSource::~SdlEventSource() {
00139     closeJoystick();
00140 }
00141 
00142 int SdlEventSource::mapKey(SDLKey sdlKey, SDLMod mod, Uint16 unicode) {
00143     Common::KeyCode key = SDLToOSystemKeycode(sdlKey);
00144 
00145     // Keep unicode in case it's regular ASCII text or in case we didn't get a valid keycode
00146     //
00147     // We need to use unicode in those cases, simply because SDL1.x passes us non-layout-adjusted keycodes.
00148     // So unicode is the only way to get layout-adjusted keys.
00149     if (unicode < 0x20) {
00150         // don't use unicode, in case it's control characters
00151         unicode = 0;
00152     } else {
00153         // Use unicode, in case keycode is invalid.
00154         // Umlauts and others will set KEYCODE_INVALID on SDL2, so in such a case always keep unicode.
00155         if (key != Common::KEYCODE_INVALID) {
00156             // keycode is valid, check further also depending on modifiers
00157             if (mod & (KMOD_CTRL | KMOD_ALT)) {
00158                 // Ctrl and/or Alt is active
00159                 //
00160                 // We need to restrict unicode to only up to 0x7E, because on macOS the option/alt key will switch to
00161                 // an alternate keyboard, which will cause us to receive Unicode characters for some keys, which are outside
00162                 // of the ASCII range (e.g. alt-x will get us U+2248). We need to return 'x' for alt-x, so using unicode
00163                 // in that case would break alt-shortcuts.
00164                 if (unicode > 0x7E)
00165                     unicode = 0; // do not allow any characters above 0x7E
00166             } else {
00167                 // We must not restrict as much as when Ctrl/Alt-modifiers are active, otherwise
00168                 // we wouldn't let umlauts through for SDL1. For SDL1 umlauts may set for example KEYCODE_QUOTE, KEYCODE_MINUS, etc.
00169                 if (unicode > 0xFF)
00170                     unicode = 0; // do not allow any characters above 0xFF
00171             }
00172         }
00173     }
00174 
00175     // Attention:
00176     // When using SDL1.x, we will get scancodes via sdlKey, that are raw scancodes, so NOT adjusted to keyboard layout/
00177     // mapping. So for example for certain locales, we will get KEYCODE_y, when 'z' is pressed and so on.
00178     // When using SDL2.x however, we will get scancodes based on the keyboard layout.
00179 
00180     if (key >= Common::KEYCODE_F1 && key <= Common::KEYCODE_F9) {
00181         return key - Common::KEYCODE_F1 + Common::ASCII_F1;
00182     } else if (key >= Common::KEYCODE_KP0 && key <= Common::KEYCODE_KP9) {
00183         if ((mod & KMOD_NUM) == 0)
00184             return 0; // In case Num-Lock is NOT enabled, return 0 for ascii, so that directional keys on numpad work
00185         return key - Common::KEYCODE_KP0 + '0';
00186     } else if (key >= Common::KEYCODE_UP && key <= Common::KEYCODE_PAGEDOWN) {
00187         return key;
00188     } else if (unicode) {
00189         // Return unicode in case it's still set and wasn't filtered.
00190         return unicode;
00191     } else if (key >= 'a' && key <= 'z' && (mod & KMOD_SHIFT)) {
00192         return key & ~0x20;
00193     } else if (key >= Common::KEYCODE_NUMLOCK && key <= Common::KEYCODE_EURO) {
00194         return 0;
00195     } else {
00196         return key;
00197     }
00198 }
00199 
00200 // ResidualVM specific relMouse x,y
00201 bool SdlEventSource::processMouseEvent(Common::Event &event, int x, int y, int relx, int rely) {
00202     event.mouse.x = x;
00203     event.mouse.y = y;
00204     event.relMouse.x = relx;
00205     event.relMouse.y = rely;
00206 
00207     if (_graphicsManager) {
00208         return _graphicsManager->notifyMousePosition(event.mouse);
00209     }
00210 
00211     return true;
00212 }
00213 
00214 void SdlEventSource::updateKbdMouse() {
00215     uint32 curTime = g_system->getMillis(true);
00216     if (curTime < _km.last_time + _km.delay_time) {
00217         return;
00218     }
00219 
00220     _km.last_time = curTime;
00221     if (_km.x_down_count == 1) {
00222         _km.x_down_time = curTime;
00223         _km.x_down_count = 2;
00224     }
00225     if (_km.y_down_count == 1) {
00226         _km.y_down_time = curTime;
00227         _km.y_down_count = 2;
00228     }
00229 
00230     if (_km.x_vel || _km.y_vel) {
00231         if (_km.x_down_count) {
00232             if (curTime > _km.x_down_time + 300) {
00233                 if (_km.x_vel > 0)
00234                     _km.x_vel += MULTIPLIER;
00235                 else
00236                     _km.x_vel -= MULTIPLIER;
00237             } else if (curTime > _km.x_down_time + 200) {
00238                 if (_km.x_vel > 0)
00239                     _km.x_vel = 5 * MULTIPLIER;
00240                 else
00241                     _km.x_vel = -5 * MULTIPLIER;
00242             }
00243         }
00244         if (_km.y_down_count) {
00245             if (curTime > _km.y_down_time + 300) {
00246                 if (_km.y_vel > 0)
00247                     _km.y_vel += MULTIPLIER;
00248                 else
00249                     _km.y_vel -= MULTIPLIER;
00250             } else if (curTime > _km.y_down_time + 200) {
00251                 if (_km.y_vel > 0)
00252                     _km.y_vel = 5 * MULTIPLIER;
00253                 else
00254                     _km.y_vel = -5 * MULTIPLIER;
00255             }
00256         }
00257 
00258         int16 speedFactor = computeJoystickMouseSpeedFactor();
00259 
00260         // - The modifier key makes the mouse movement slower
00261         // - The extra factor "delay/speedFactor" ensures velocities
00262         // are independent of the kbdMouse update rate
00263         // - all velocities were originally chosen
00264         // at a delay of 25, so that is the reference used here
00265         // - note: operator order is important to avoid overflow
00266         if (_km.modifier) {
00267             _km.x += ((_km.x_vel / 10) * ((int16)_km.delay_time)) / speedFactor;
00268             _km.y += ((_km.y_vel / 10) * ((int16)_km.delay_time)) / speedFactor;
00269         } else {
00270             _km.x += (_km.x_vel * ((int16)_km.delay_time)) / speedFactor;
00271             _km.y += (_km.y_vel * ((int16)_km.delay_time)) / speedFactor;
00272         }
00273 
00274         if (_km.x < 0) {
00275             _km.x = 0;
00276             _km.x_vel = -1 * MULTIPLIER;
00277             _km.x_down_count = 1;
00278         } else if (_km.x > _km.x_max * MULTIPLIER) {
00279             _km.x = _km.x_max * MULTIPLIER;
00280             _km.x_vel = 1 * MULTIPLIER;
00281             _km.x_down_count = 1;
00282         }
00283 
00284         if (_km.y < 0) {
00285             _km.y = 0;
00286             _km.y_vel = -1 * MULTIPLIER;
00287             _km.y_down_count = 1;
00288         } else if (_km.y > _km.y_max * MULTIPLIER) {
00289             _km.y = _km.y_max * MULTIPLIER;
00290             _km.y_vel = 1 * MULTIPLIER;
00291             _km.y_down_count = 1;
00292         }
00293     }
00294 }
00295 
00296 bool SdlEventSource::handleKbdMouse(Common::Event &event) {
00297     int16 oldKmX = _km.x;
00298     int16 oldKmY = _km.y;
00299 
00300     updateKbdMouse();
00301 
00302     if (_km.x != oldKmX || _km.y != oldKmY) {
00303         if (_graphicsManager) {
00304             _graphicsManager->getWindow()->warpMouseInWindow((Uint16)(_km.x / MULTIPLIER), (Uint16)(_km.y / MULTIPLIER));
00305         }
00306 
00307         event.type = Common::EVENT_MOUSEMOVE;
00308         return processMouseEvent(event, _km.x / MULTIPLIER, _km.y / MULTIPLIER);
00309     }
00310 
00311     return false;
00312 }
00313 
00314 int16 SdlEventSource::computeJoystickMouseSpeedFactor() const {
00315     int16 speedFactor;
00316 
00317     switch (ConfMan.getInt("kbdmouse_speed")) {
00318     // 0.25 keyboard pointer speed
00319     case 0:
00320         speedFactor = 100;
00321         break;
00322     // 0.5 speed
00323     case 1:
00324         speedFactor = 50;
00325         break;
00326     // 0.75 speed
00327     case 2:
00328         speedFactor = 33;
00329         break;
00330     // 1.0 speed
00331     case 3:
00332         speedFactor = 25;
00333         break;
00334     // 1.25 speed
00335     case 4:
00336         speedFactor = 20;
00337         break;
00338     // 1.5 speed
00339     case 5:
00340         speedFactor = 17;
00341         break;
00342     // 1.75 speed
00343     case 6:
00344         speedFactor = 14;
00345         break;
00346     // 2.0 speed
00347     case 7:
00348         speedFactor = 12;
00349         break;
00350     default:
00351         speedFactor = 25;
00352     }
00353 
00354     // Scale the mouse cursor speed with the display size so moving across
00355     // the screen takes a reasonable amount of time at higher resolutions.
00356     return speedFactor * 480 / _km.y_max;
00357 }
00358 
00359 void SdlEventSource::SDLModToOSystemKeyFlags(SDLMod mod, Common::Event &event) {
00360 
00361     event.kbd.flags = 0;
00362 
00363     if (mod & KMOD_SHIFT)
00364         event.kbd.flags |= Common::KBD_SHIFT;
00365     if (mod & KMOD_ALT)
00366         event.kbd.flags |= Common::KBD_ALT;
00367     if (mod & KMOD_CTRL)
00368         event.kbd.flags |= Common::KBD_CTRL;
00369     if (mod & KMOD_META)
00370         event.kbd.flags |= Common::KBD_META;
00371 
00372     // Sticky flags
00373     if (mod & KMOD_NUM)
00374         event.kbd.flags |= Common::KBD_NUM;
00375     if (mod & KMOD_CAPS)
00376         event.kbd.flags |= Common::KBD_CAPS;
00377 }
00378 
00379 Common::KeyCode SdlEventSource::SDLToOSystemKeycode(const SDLKey key) {
00380     switch (key) {
00381     case SDLK_BACKSPACE: return Common::KEYCODE_BACKSPACE;
00382     case SDLK_TAB: return Common::KEYCODE_TAB;
00383     case SDLK_CLEAR: return Common::KEYCODE_CLEAR;
00384     case SDLK_RETURN: return Common::KEYCODE_RETURN;
00385     case SDLK_PAUSE: return Common::KEYCODE_PAUSE;
00386     case SDLK_ESCAPE: return Common::KEYCODE_ESCAPE;
00387     case SDLK_SPACE: return Common::KEYCODE_SPACE;
00388     case SDLK_EXCLAIM: return Common::KEYCODE_EXCLAIM;
00389     case SDLK_QUOTEDBL: return Common::KEYCODE_QUOTEDBL;
00390     case SDLK_HASH: return Common::KEYCODE_HASH;
00391     case SDLK_DOLLAR: return Common::KEYCODE_DOLLAR;
00392     case SDLK_AMPERSAND: return Common::KEYCODE_AMPERSAND;
00393     case SDLK_QUOTE: return Common::KEYCODE_QUOTE;
00394     case SDLK_LEFTPAREN: return Common::KEYCODE_LEFTPAREN;
00395     case SDLK_RIGHTPAREN: return Common::KEYCODE_RIGHTPAREN;
00396     case SDLK_ASTERISK: return Common::KEYCODE_ASTERISK;
00397     case SDLK_PLUS: return Common::KEYCODE_PLUS;
00398     case SDLK_COMMA: return Common::KEYCODE_COMMA;
00399     case SDLK_MINUS: return Common::KEYCODE_MINUS;
00400     case SDLK_PERIOD: return Common::KEYCODE_PERIOD;
00401     case SDLK_SLASH: return Common::KEYCODE_SLASH;
00402     case SDLK_0: return Common::KEYCODE_0;
00403     case SDLK_1: return Common::KEYCODE_1;
00404     case SDLK_2: return Common::KEYCODE_2;
00405     case SDLK_3: return Common::KEYCODE_3;
00406     case SDLK_4: return Common::KEYCODE_4;
00407     case SDLK_5: return Common::KEYCODE_5;
00408     case SDLK_6: return Common::KEYCODE_6;
00409     case SDLK_7: return Common::KEYCODE_7;
00410     case SDLK_8: return Common::KEYCODE_8;
00411     case SDLK_9: return Common::KEYCODE_9;
00412     case SDLK_COLON: return Common::KEYCODE_COLON;
00413     case SDLK_SEMICOLON: return Common::KEYCODE_SEMICOLON;
00414     case SDLK_LESS: return Common::KEYCODE_LESS;
00415     case SDLK_EQUALS: return Common::KEYCODE_EQUALS;
00416     case SDLK_GREATER: return Common::KEYCODE_GREATER;
00417     case SDLK_QUESTION: return Common::KEYCODE_QUESTION;
00418     case SDLK_AT: return Common::KEYCODE_AT;
00419     case SDLK_LEFTBRACKET: return Common::KEYCODE_LEFTBRACKET;
00420     case SDLK_BACKSLASH: return Common::KEYCODE_BACKSLASH;
00421     case SDLK_RIGHTBRACKET: return Common::KEYCODE_RIGHTBRACKET;
00422     case SDLK_CARET: return Common::KEYCODE_CARET;
00423     case SDLK_UNDERSCORE: return Common::KEYCODE_UNDERSCORE;
00424     case SDLK_BACKQUOTE: return Common::KEYCODE_BACKQUOTE;
00425     case SDLK_a: return Common::KEYCODE_a;
00426     case SDLK_b: return Common::KEYCODE_b;
00427     case SDLK_c: return Common::KEYCODE_c;
00428     case SDLK_d: return Common::KEYCODE_d;
00429     case SDLK_e: return Common::KEYCODE_e;
00430     case SDLK_f: return Common::KEYCODE_f;
00431     case SDLK_g: return Common::KEYCODE_g;
00432     case SDLK_h: return Common::KEYCODE_h;
00433     case SDLK_i: return Common::KEYCODE_i;
00434     case SDLK_j: return Common::KEYCODE_j;
00435     case SDLK_k: return Common::KEYCODE_k;
00436     case SDLK_l: return Common::KEYCODE_l;
00437     case SDLK_m: return Common::KEYCODE_m;
00438     case SDLK_n: return Common::KEYCODE_n;
00439     case SDLK_o: return Common::KEYCODE_o;
00440     case SDLK_p: return Common::KEYCODE_p;
00441     case SDLK_q: return Common::KEYCODE_q;
00442     case SDLK_r: return Common::KEYCODE_r;
00443     case SDLK_s: return Common::KEYCODE_s;
00444     case SDLK_t: return Common::KEYCODE_t;
00445     case SDLK_u: return Common::KEYCODE_u;
00446     case SDLK_v: return Common::KEYCODE_v;
00447     case SDLK_w: return Common::KEYCODE_w;
00448     case SDLK_x: return Common::KEYCODE_x;
00449     case SDLK_y: return Common::KEYCODE_y;
00450     case SDLK_z: return Common::KEYCODE_z;
00451     case SDLK_DELETE: return Common::KEYCODE_DELETE;
00452 #if SDL_VERSION_ATLEAST(2, 0, 0)
00453     case SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_GRAVE): return Common::KEYCODE_TILDE;
00454 #else
00455     case SDLK_WORLD_16: return Common::KEYCODE_TILDE;
00456 #endif
00457     case SDLK_KP0: return Common::KEYCODE_KP0;
00458     case SDLK_KP1: return Common::KEYCODE_KP1;
00459     case SDLK_KP2: return Common::KEYCODE_KP2;
00460     case SDLK_KP3: return Common::KEYCODE_KP3;
00461     case SDLK_KP4: return Common::KEYCODE_KP4;
00462     case SDLK_KP5: return Common::KEYCODE_KP5;
00463     case SDLK_KP6: return Common::KEYCODE_KP6;
00464     case SDLK_KP7: return Common::KEYCODE_KP7;
00465     case SDLK_KP8: return Common::KEYCODE_KP8;
00466     case SDLK_KP9: return Common::KEYCODE_KP9;
00467     case SDLK_KP_PERIOD: return Common::KEYCODE_KP_PERIOD;
00468     case SDLK_KP_DIVIDE: return Common::KEYCODE_KP_DIVIDE;
00469     case SDLK_KP_MULTIPLY: return Common::KEYCODE_KP_MULTIPLY;
00470     case SDLK_KP_MINUS: return Common::KEYCODE_KP_MINUS;
00471     case SDLK_KP_PLUS: return Common::KEYCODE_KP_PLUS;
00472     case SDLK_KP_ENTER: return Common::KEYCODE_KP_ENTER;
00473     case SDLK_KP_EQUALS: return Common::KEYCODE_KP_EQUALS;
00474     case SDLK_UP: return Common::KEYCODE_UP;
00475     case SDLK_DOWN: return Common::KEYCODE_DOWN;
00476     case SDLK_RIGHT: return Common::KEYCODE_RIGHT;
00477     case SDLK_LEFT: return Common::KEYCODE_LEFT;
00478     case SDLK_INSERT: return Common::KEYCODE_INSERT;
00479     case SDLK_HOME: return Common::KEYCODE_HOME;
00480     case SDLK_END: return Common::KEYCODE_END;
00481     case SDLK_PAGEUP: return Common::KEYCODE_PAGEUP;
00482     case SDLK_PAGEDOWN: return Common::KEYCODE_PAGEDOWN;
00483     case SDLK_F1: return Common::KEYCODE_F1;
00484     case SDLK_F2: return Common::KEYCODE_F2;
00485     case SDLK_F3: return Common::KEYCODE_F3;
00486     case SDLK_F4: return Common::KEYCODE_F4;
00487     case SDLK_F5: return Common::KEYCODE_F5;
00488     case SDLK_F6: return Common::KEYCODE_F6;
00489     case SDLK_F7: return Common::KEYCODE_F7;
00490     case SDLK_F8: return Common::KEYCODE_F8;
00491     case SDLK_F9: return Common::KEYCODE_F9;
00492     case SDLK_F10: return Common::KEYCODE_F10;
00493     case SDLK_F11: return Common::KEYCODE_F11;
00494     case SDLK_F12: return Common::KEYCODE_F12;
00495     case SDLK_F13: return Common::KEYCODE_F13;
00496     case SDLK_F14: return Common::KEYCODE_F14;
00497     case SDLK_F15: return Common::KEYCODE_F15;
00498     case SDLK_NUMLOCK: return Common::KEYCODE_NUMLOCK;
00499     case SDLK_CAPSLOCK: return Common::KEYCODE_CAPSLOCK;
00500     case SDLK_SCROLLOCK: return Common::KEYCODE_SCROLLOCK;
00501     case SDLK_RSHIFT: return Common::KEYCODE_RSHIFT;
00502     case SDLK_LSHIFT: return Common::KEYCODE_LSHIFT;
00503     case SDLK_RCTRL: return Common::KEYCODE_RCTRL;
00504     case SDLK_LCTRL: return Common::KEYCODE_LCTRL;
00505     case SDLK_RALT: return Common::KEYCODE_RALT;
00506     case SDLK_LALT: return Common::KEYCODE_LALT;
00507     case SDLK_LSUPER: return Common::KEYCODE_LSUPER;
00508     case SDLK_RSUPER: return Common::KEYCODE_RSUPER;
00509     case SDLK_MODE: return Common::KEYCODE_MODE;
00510     case SDLK_COMPOSE: return Common::KEYCODE_COMPOSE;
00511     case SDLK_HELP: return Common::KEYCODE_HELP;
00512     case SDLK_PRINT: return Common::KEYCODE_PRINT;
00513     case SDLK_SYSREQ: return Common::KEYCODE_SYSREQ;
00514 #if !SDL_VERSION_ATLEAST(2, 0, 0)
00515     case SDLK_BREAK: return Common::KEYCODE_BREAK;
00516 #endif
00517     case SDLK_MENU: return Common::KEYCODE_MENU;
00518     case SDLK_POWER: return Common::KEYCODE_POWER;
00519     case SDLK_UNDO: return Common::KEYCODE_UNDO;
00520     default: return Common::KEYCODE_INVALID;
00521     }
00522 }
00523 
00524 bool SdlEventSource::pollEvent(Common::Event &event) {
00525 
00526 #if SDL_VERSION_ATLEAST(2, 0, 0)
00527     // In case we still need to send a key up event for a key down from a
00528     // TEXTINPUT event we do this immediately.
00529     if (_queuedFakeKeyUp) {
00530         event = _fakeKeyUp;
00531         _queuedFakeKeyUp = false;
00532         return true;
00533     }
00534 #endif
00535 
00536     // If the screen changed, send an Common::EVENT_SCREEN_CHANGED
00537     int screenID = ((OSystem_SDL *)g_system)->getGraphicsManager()->getScreenChangeID();
00538     if (screenID != _lastScreenID) {
00539         _lastScreenID = screenID;
00540         event.type = Common::EVENT_SCREEN_CHANGED;
00541         return true;
00542     }
00543 
00544     if (_queuedFakeMouseMove) {
00545         event = _fakeMouseMove;
00546         _queuedFakeMouseMove = false;
00547         return true;
00548     }
00549 
00550     SDL_Event ev;
00551     while (SDL_PollEvent(&ev)) {
00552         preprocessEvents(&ev);
00553         if (dispatchSDLEvent(ev, event))
00554             return true;
00555     }
00556 
00557     // Handle mouse control via analog joystick and keyboard
00558     if (handleKbdMouse(event)) {
00559         return true;
00560     }
00561 
00562     return false;
00563 }
00564 
00565 bool SdlEventSource::dispatchSDLEvent(SDL_Event &ev, Common::Event &event) {
00566     switch (ev.type) {
00567     case SDL_KEYDOWN:
00568         return handleKeyDown(ev, event);
00569     case SDL_KEYUP:
00570         return handleKeyUp(ev, event);
00571     case SDL_MOUSEMOTION:
00572         return handleMouseMotion(ev, event);
00573     case SDL_MOUSEBUTTONDOWN:
00574         return handleMouseButtonDown(ev, event);
00575     case SDL_MOUSEBUTTONUP:
00576         return handleMouseButtonUp(ev, event);
00577 
00578 #if SDL_VERSION_ATLEAST(2, 0, 0)
00579     case SDL_MOUSEWHEEL: {
00580         Sint32 yDir = ev.wheel.y;
00581         // HACK: It seems we want the mouse coordinates supplied
00582         // with a mouse wheel event. However, SDL2 does not supply
00583         // these, thus we use whatever we got last time. It seems
00584         // these are always stored in _km.x, _km.y.
00585         if (!processMouseEvent(event, _km.x / MULTIPLIER, _km.y / MULTIPLIER)) {
00586             return false;
00587         }
00588         if (yDir < 0) {
00589             event.type = Common::EVENT_WHEELDOWN;
00590             return true;
00591         } else if (yDir > 0) {
00592             event.type = Common::EVENT_WHEELUP;
00593             return true;
00594         } else {
00595             return false;
00596         }
00597         }
00598 
00599     case SDL_TEXTINPUT: {
00600         // When we get a TEXTINPUT event it means we got some user input for
00601         // which no KEYDOWN exists. SDL 1.2 introduces a "fake" key down+up
00602         // in such cases. We will do the same to mimic it's behavior.
00603         event.type = Common::EVENT_KEYDOWN;
00604 
00605         event.kbd = Common::KeyState(Common::KEYCODE_INVALID, convUTF8ToUTF32(ev.text.text), 0);
00606 
00607         SDLModToOSystemKeyFlags(SDL_GetModState(), event);
00608         // Set the scroll lock sticky flag
00609         if (_scrollLock)
00610             event.kbd.flags |= Common::KBD_SCRL;
00611 
00612         // Fake a key up when we have a proper ascii value.
00613         _queuedFakeKeyUp = (event.kbd.ascii != 0);
00614         _fakeKeyUp = event;
00615         _fakeKeyUp.type = Common::EVENT_KEYUP;
00616 
00617         return _queuedFakeKeyUp;
00618         }
00619 
00620     case SDL_WINDOWEVENT:
00621         switch (ev.window.event) {
00622         case SDL_WINDOWEVENT_EXPOSED:
00623             if (_graphicsManager)
00624                 _graphicsManager->notifyVideoExpose();
00625             return false;
00626 
00627         // SDL2 documentation indicate that SDL_WINDOWEVENT_SIZE_CHANGED is sent either as a result
00628         // of the size being changed by an external event (for example the user resizing the window
00629         // or going fullscreen) or a call to the SDL API (for example SDL_SetWindowSize). On the
00630         // other hand SDL_WINDOWEVENT_RESIZED is only sent for resize resulting from an external event,
00631         // and is always preceded by a SDL_WINDOWEVENT_SIZE_CHANGED event.
00632         // We need to handle the programmatic resize as well so that the graphics manager always know
00633         // the current size. See comments in SdlWindow::createOrUpdateWindow for details of one case
00634         // where we need to call SDL_SetWindowSize and we need the resulting event to be processed.
00635         // However if the documentation is correct we can ignore SDL_WINDOWEVENT_RESIZED since when we
00636         // get one we should always get a SDL_WINDOWEVENT_SIZE_CHANGED as well.
00637         case SDL_WINDOWEVENT_SIZE_CHANGED:
00638         //case SDL_WINDOWEVENT_RESIZED:
00639             return handleResizeEvent(event, ev.window.data1, ev.window.data2);
00640 
00641         default:
00642             return false;
00643         }
00644 
00645     case SDL_JOYDEVICEADDED:
00646         return handleJoystickAdded(ev.jdevice);
00647 
00648     case SDL_JOYDEVICEREMOVED:
00649         return handleJoystickRemoved(ev.jdevice);
00650 #else
00651     case SDL_VIDEOEXPOSE:
00652         if (_graphicsManager)
00653             _graphicsManager->notifyVideoExpose();
00654         return false;
00655 
00656     case SDL_VIDEORESIZE:
00657         return handleResizeEvent(event, ev.resize.w, ev.resize.h);
00658 #endif
00659 
00660     case SDL_QUIT:
00661         event.type = Common::EVENT_QUIT;
00662         return true;
00663 
00664     }
00665 
00666     if (_joystick) {
00667         switch (ev.type) {
00668         case SDL_JOYBUTTONDOWN:
00669             return handleJoyButtonDown(ev, event);
00670         case SDL_JOYBUTTONUP:
00671             return handleJoyButtonUp(ev, event);
00672         case SDL_JOYAXISMOTION:
00673             return handleJoyAxisMotion(ev, event);
00674         }
00675     }
00676 
00677 #if SDL_VERSION_ATLEAST(2, 0, 0)
00678     if (_controller) {
00679         switch (ev.type) {
00680         case SDL_CONTROLLERBUTTONDOWN:
00681             return handleControllerButton(ev, event, false);
00682         case SDL_CONTROLLERBUTTONUP:
00683             return handleControllerButton(ev, event, true);
00684         case SDL_CONTROLLERAXISMOTION:
00685             return handleControllerAxisMotion(ev, event);
00686         }
00687     }
00688 #endif
00689 
00690     return false;
00691 }
00692 
00693 
00694 bool SdlEventSource::handleKeyDown(SDL_Event &ev, Common::Event &event) {
00695 
00696     SDLModToOSystemKeyFlags(SDL_GetModState(), event);
00697 
00698     SDLKey sdlKeycode = obtainKeycode(ev.key.keysym);
00699 
00700     // Handle scroll lock as a key modifier
00701     if (sdlKeycode == SDLK_SCROLLOCK)
00702         _scrollLock = !_scrollLock;
00703 
00704     if (_scrollLock)
00705         event.kbd.flags |= Common::KBD_SCRL;
00706 
00707     // Ctrl-m toggles mouse capture
00708     if (event.kbd.hasFlags(Common::KBD_CTRL) && sdlKeycode == 'm') {
00709         if (_graphicsManager) {
00710             _graphicsManager->getWindow()->toggleMouseGrab();
00711         }
00712         return false;
00713     }
00714 
00715 #if defined(MACOSX)
00716     // On Macintosh, Cmd-Q quits
00717     if ((ev.key.keysym.mod & KMOD_META) && sdlKeycode == 'q') {
00718         event.type = Common::EVENT_QUIT;
00719         return true;
00720     }
00721 #elif defined(POSIX)
00722     // On other *nix systems, Control-Q quits
00723     if ((ev.key.keysym.mod & KMOD_CTRL) && sdlKeycode == 'q') {
00724         event.type = Common::EVENT_QUIT;
00725         return true;
00726     }
00727 #else
00728     // Ctrl-z quits
00729     if ((event.kbd.hasFlags(Common::KBD_CTRL) && sdlKeycode == 'z')) {
00730         event.type = Common::EVENT_QUIT;
00731         return true;
00732     }
00733 
00734     #ifdef WIN32
00735     // On Windows, also use the default Alt-F4 quit combination
00736     if ((ev.key.keysym.mod & KMOD_ALT) && sdlKeycode == SDLK_F4) {
00737         event.type = Common::EVENT_QUIT;
00738         return true;
00739     }
00740     #endif
00741 #endif
00742 
00743     // Ctrl-u toggles mute
00744     if ((ev.key.keysym.mod & KMOD_CTRL) && sdlKeycode == 'u') {
00745         event.type = Common::EVENT_MUTE;
00746         return true;
00747     }
00748 
00749     if (remapKey(ev, event))
00750         return true;
00751 
00752     event.type = Common::EVENT_KEYDOWN;
00753     event.kbd.keycode = SDLToOSystemKeycode(sdlKeycode);
00754     event.kbd.ascii = mapKey(sdlKeycode, (SDLMod)ev.key.keysym.mod, obtainUnicode(ev.key.keysym));
00755 
00756 #if SDL_VERSION_ATLEAST(2, 0, 0)
00757     event.kbdRepeat = ev.key.repeat;
00758 #endif
00759 
00760     return true;
00761 }
00762 
00763 bool SdlEventSource::handleKeyUp(SDL_Event &ev, Common::Event &event) {
00764     if (remapKey(ev, event))
00765         return true;
00766 
00767     SDLKey sdlKeycode = obtainKeycode(ev.key.keysym);
00768     SDLMod mod = SDL_GetModState();
00769 
00770     // Check if this is an event handled by handleKeyDown(), and stop if it is
00771 
00772     // Check if the Ctrl key is down, so that we can trap cases where the
00773     // user has the Ctrl key down, and has just released a special key
00774     if (mod & KMOD_CTRL) {
00775         if (sdlKeycode == 'm' ||    // Ctrl-m toggles mouse capture
00776 #if defined(MACOSX)
00777             // Meta - Q, handled below
00778 #elif defined(POSIX)
00779             sdlKeycode == 'q' ||    // On other *nix systems, Control-Q quits
00780 #else
00781             sdlKeycode == 'z' ||    // Ctrl-z quit
00782 #endif
00783             sdlKeycode == 'u')  // Ctrl-u toggles mute
00784             return false;
00785     }
00786 
00787     // Same for other keys (Meta and Alt)
00788 #if defined(MACOSX)
00789     if ((mod & KMOD_META) && sdlKeycode == 'q')
00790         return false;   // On Macintosh, Cmd-Q quits
00791 #endif
00792 
00793     // If we reached here, this isn't an event handled by handleKeyDown(), thus
00794     // continue normally
00795 
00796     event.type = Common::EVENT_KEYUP;
00797     event.kbd.keycode = SDLToOSystemKeycode(sdlKeycode);
00798     event.kbd.ascii = mapKey(sdlKeycode, (SDLMod)ev.key.keysym.mod, 0);
00799 
00800     // Ctrl-Alt-<key> will change the GFX mode
00801     SDLModToOSystemKeyFlags(mod, event);
00802 
00803     // Set the scroll lock sticky flag
00804     if (_scrollLock)
00805         event.kbd.flags |= Common::KBD_SCRL;
00806 
00807     return true;
00808 }
00809 
00810 bool SdlEventSource::handleMouseMotion(SDL_Event &ev, Common::Event &event) {
00811     event.type = Common::EVENT_MOUSEMOVE;
00812 
00813     // update KbdMouse
00814     _km.x = ev.motion.x * MULTIPLIER;
00815     _km.y = ev.motion.y * MULTIPLIER;
00816 
00817     return processMouseEvent(event, ev.motion.x, ev.motion.y, ev.motion.xrel, ev.motion.yrel); // ResidualVM xrel,yrel
00818 }
00819 
00820 bool SdlEventSource::handleMouseButtonDown(SDL_Event &ev, Common::Event &event) {
00821     if (ev.button.button == SDL_BUTTON_LEFT)
00822         event.type = Common::EVENT_LBUTTONDOWN;
00823     else if (ev.button.button == SDL_BUTTON_RIGHT)
00824         event.type = Common::EVENT_RBUTTONDOWN;
00825 #if defined(SDL_BUTTON_WHEELUP) && defined(SDL_BUTTON_WHEELDOWN)
00826     else if (ev.button.button == SDL_BUTTON_WHEELUP)
00827         event.type = Common::EVENT_WHEELUP;
00828     else if (ev.button.button == SDL_BUTTON_WHEELDOWN)
00829         event.type = Common::EVENT_WHEELDOWN;
00830 #endif
00831 #if defined(SDL_BUTTON_MIDDLE)
00832     else if (ev.button.button == SDL_BUTTON_MIDDLE)
00833         event.type = Common::EVENT_MBUTTONDOWN;
00834 #endif
00835     else
00836         return false;
00837 
00838     // update KbdMouse
00839     _km.x = ev.button.x * MULTIPLIER;
00840     _km.y = ev.button.y * MULTIPLIER;
00841 
00842     return processMouseEvent(event, ev.button.x, ev.button.y);
00843 }
00844 
00845 bool SdlEventSource::handleMouseButtonUp(SDL_Event &ev, Common::Event &event) {
00846     if (ev.button.button == SDL_BUTTON_LEFT)
00847         event.type = Common::EVENT_LBUTTONUP;
00848     else if (ev.button.button == SDL_BUTTON_RIGHT)
00849         event.type = Common::EVENT_RBUTTONUP;
00850 #if defined(SDL_BUTTON_MIDDLE)
00851     else if (ev.button.button == SDL_BUTTON_MIDDLE)
00852         event.type = Common::EVENT_MBUTTONUP;
00853 #endif
00854     else
00855         return false;
00856 
00857     // update KbdMouse
00858     _km.x = ev.button.x * MULTIPLIER;
00859     _km.y = ev.button.y * MULTIPLIER;
00860 
00861     return processMouseEvent(event, ev.button.x, ev.button.y);
00862 }
00863 
00864 void SdlEventSource::openJoystick(int joystickIndex) {
00865     if (SDL_NumJoysticks() > joystickIndex) {
00866 #if SDL_VERSION_ATLEAST(2, 0, 0)
00867         if (SDL_IsGameController(joystickIndex)) {
00868             _controller = SDL_GameControllerOpen(joystickIndex);
00869             debug("Using game controller: %s", SDL_GameControllerName(_controller));
00870         } else
00871 #endif
00872         {
00873             _joystick = SDL_JoystickOpen(joystickIndex);
00874             debug("Using joystick: %s",
00875 #if SDL_VERSION_ATLEAST(2, 0, 0)
00876                   SDL_JoystickName(_joystick)
00877 #else
00878                   SDL_JoystickName(joystickIndex)
00879 #endif
00880             );
00881         }
00882     } else {
00883         warning("Invalid joystick: %d", joystickIndex);
00884     }
00885 }
00886 
00887 void SdlEventSource::closeJoystick() {
00888 #if SDL_VERSION_ATLEAST(2, 0, 0)
00889     if (_controller) {
00890         SDL_GameControllerClose(_controller);
00891         _controller = nullptr;
00892     }
00893 #endif
00894     if (_joystick) {
00895         SDL_JoystickClose(_joystick);
00896         _joystick = nullptr;
00897     }
00898 }
00899 
00900 bool SdlEventSource::handleJoyButtonDown(SDL_Event &ev, Common::Event &event) {
00901     if (ev.jbutton.button == JOY_BUT_LMOUSE) {
00902         event.type = Common::EVENT_LBUTTONDOWN;
00903         return processMouseEvent(event, _km.x / MULTIPLIER, _km.y / MULTIPLIER);
00904     } else if (ev.jbutton.button == JOY_BUT_RMOUSE) {
00905         event.type = Common::EVENT_RBUTTONDOWN;
00906         return processMouseEvent(event, _km.x / MULTIPLIER, _km.y / MULTIPLIER);
00907     } else {
00908         event.type = Common::EVENT_KEYDOWN;
00909         switch (ev.jbutton.button) {
00910         case JOY_BUT_ESCAPE:
00911             event.kbd.keycode = Common::KEYCODE_ESCAPE;
00912             event.kbd.ascii = mapKey(SDLK_ESCAPE, (SDLMod)ev.key.keysym.mod, 0);
00913             break;
00914         case JOY_BUT_PERIOD:
00915             event.kbd.keycode = Common::KEYCODE_PERIOD;
00916             event.kbd.ascii = mapKey(SDLK_PERIOD, (SDLMod)ev.key.keysym.mod, 0);
00917             break;
00918         case JOY_BUT_SPACE:
00919             event.kbd.keycode = Common::KEYCODE_SPACE;
00920             event.kbd.ascii = mapKey(SDLK_SPACE, (SDLMod)ev.key.keysym.mod, 0);
00921             break;
00922         case JOY_BUT_F5:
00923             event.kbd.keycode = Common::KEYCODE_F5;
00924             event.kbd.ascii = mapKey(SDLK_F5, (SDLMod)ev.key.keysym.mod, 0);
00925             break;
00926 #ifdef ENABLE_VKEYBD
00927         case JOY_BUT_VKEYBOARD: // Toggles virtual keyboard
00928             event.type = Common::EVENT_VIRTUAL_KEYBOARD;
00929             break;
00930 #endif
00931         }
00932         return true;
00933     }
00934 }
00935 
00936 bool SdlEventSource::handleJoyButtonUp(SDL_Event &ev, Common::Event &event) {
00937     if (ev.jbutton.button == JOY_BUT_LMOUSE) {
00938         event.type = Common::EVENT_LBUTTONUP;
00939         return processMouseEvent(event, _km.x / MULTIPLIER, _km.y / MULTIPLIER);
00940     } else if (ev.jbutton.button == JOY_BUT_RMOUSE) {
00941         event.type = Common::EVENT_RBUTTONUP;
00942         return processMouseEvent(event, _km.x / MULTIPLIER, _km.y / MULTIPLIER);
00943     } else {
00944         event.type = Common::EVENT_KEYUP;
00945         switch (ev.jbutton.button) {
00946         case JOY_BUT_ESCAPE:
00947             event.kbd.keycode = Common::KEYCODE_ESCAPE;
00948             event.kbd.ascii = mapKey(SDLK_ESCAPE, (SDLMod)ev.key.keysym.mod, 0);
00949             break;
00950         case JOY_BUT_PERIOD:
00951             event.kbd.keycode = Common::KEYCODE_PERIOD;
00952             event.kbd.ascii = mapKey(SDLK_PERIOD, (SDLMod)ev.key.keysym.mod, 0);
00953             break;
00954         case JOY_BUT_SPACE:
00955             event.kbd.keycode = Common::KEYCODE_SPACE;
00956             event.kbd.ascii = mapKey(SDLK_SPACE, (SDLMod)ev.key.keysym.mod, 0);
00957             break;
00958         case JOY_BUT_F5:
00959             event.kbd.keycode = Common::KEYCODE_F5;
00960             event.kbd.ascii = mapKey(SDLK_F5, (SDLMod)ev.key.keysym.mod, 0);
00961             break;
00962 #ifdef ENABLE_VKEYBD
00963         case JOY_BUT_VKEYBOARD: // Toggles virtual keyboard
00964             // Handled in key down
00965             break;
00966 #endif
00967         }
00968         return true;
00969     }
00970 }
00971 
00972 bool SdlEventSource::handleJoyAxisMotion(SDL_Event &ev, Common::Event &event) {
00973     if (ev.jaxis.axis == JOY_XAXIS) {
00974         _km.joy_x = ev.jaxis.value;
00975         return handleAxisToMouseMotion(_km.joy_x, _km.joy_y);
00976     } else if (ev.jaxis.axis == JOY_YAXIS) {
00977         _km.joy_y = ev.jaxis.value;
00978         return handleAxisToMouseMotion(_km.joy_x, _km.joy_y);
00979     }
00980 
00981     return false;
00982 }
00983 
00984 #if SDL_VERSION_ATLEAST(2, 0, 0)
00985 bool SdlEventSource::handleJoystickAdded(const SDL_JoyDeviceEvent &device) {
00986     debug(5, "SdlEventSource: Received joystick added event for index '%d'", device.which);
00987 
00988     int joystick_num = ConfMan.getInt("joystick_num");
00989     if (joystick_num == device.which) {
00990         debug(5, "SdlEventSource: Newly added joystick with index '%d' matches 'joysticky_num', trying to use it", device.which);
00991 
00992         closeJoystick();
00993         openJoystick(joystick_num);
00994     }
00995 
00996     return false;
00997 }
00998 
00999 bool SdlEventSource::handleJoystickRemoved(const SDL_JoyDeviceEvent &device) {
01000     debug(5, "SdlEventSource: Received joystick removed event for instance id '%d'", device.which);
01001 
01002     SDL_Joystick *joystick;
01003     if (_controller) {
01004         joystick = SDL_GameControllerGetJoystick(_controller);
01005     } else {
01006         joystick = _joystick;
01007     }
01008 
01009     if (!joystick) {
01010         return false;
01011     }
01012 
01013     if (SDL_JoystickInstanceID(joystick) == device.which) {
01014         debug(5, "SdlEventSource: Newly removed joystick with instance id '%d' matches currently used joystick, closing current joystick", device.which);
01015 
01016         closeJoystick();
01017     }
01018 
01019     return false;
01020 }
01021 
01022 bool SdlEventSource::handleControllerButton(const SDL_Event &ev, Common::Event &event, bool buttonUp) {
01023     using namespace Common;
01024 
01025     struct ControllerEventMapping {
01026         EventType normalType;
01027         KeyState normalKeystate;
01028         EventType modifierType;
01029         KeyState modifierKeystate;
01030     };
01031 
01032     static const ControllerEventMapping mapping[] = {
01033             // SDL_CONTROLLER_BUTTON_A: Left mouse button
01034             { EVENT_LBUTTONDOWN, KeyState(), EVENT_LBUTTONDOWN, KeyState() },
01035             // SDL_CONTROLLER_BUTTON_B: Right mouse button
01036             { EVENT_RBUTTONDOWN, KeyState(), EVENT_RBUTTONDOWN, KeyState() },
01037             // SDL_CONTROLLER_BUTTON_X: Period (+R_trigger: Space)
01038             { EVENT_KEYDOWN, KeyState(KEYCODE_PERIOD, '.'), EVENT_KEYDOWN, KeyState(KEYCODE_SPACE, ASCII_SPACE) },
01039             // SDL_CONTROLLER_BUTTON_Y: Escape (+R_trigger: Return)
01040             { EVENT_KEYDOWN, KeyState(KEYCODE_ESCAPE, ASCII_ESCAPE), EVENT_KEYDOWN, KeyState(KEYCODE_RETURN, ASCII_RETURN) },
01041             // SDL_CONTROLLER_BUTTON_BACK: Virtual keyboard (+R_trigger: Predictive Input Dialog)
01042 #ifdef ENABLE_VKEYBD
01043             { EVENT_VIRTUAL_KEYBOARD, KeyState(), EVENT_PREDICTIVE_DIALOG, KeyState() },
01044 #else
01045             { EVENT_INVALID, KeyState(), EVENT_PREDICTIVE_DIALOG, KeyState() },
01046 #endif
01047             // SDL_CONTROLLER_BUTTON_GUIDE: Unmapped
01048             { EVENT_INVALID, KeyState(), EVENT_INVALID, KeyState() },
01049             // SDL_CONTROLLER_BUTTON_START: ScummVM in game menu
01050             { EVENT_MAINMENU, KeyState(), EVENT_MAINMENU, KeyState() },
01051             // SDL_CONTROLLER_BUTTON_LEFTSTICK: Unmapped
01052             { EVENT_INVALID, KeyState(), EVENT_INVALID, KeyState() },
01053             // SDL_CONTROLLER_BUTTON_RIGHTSTICK: Unmapped
01054             { EVENT_INVALID, KeyState(), EVENT_INVALID, KeyState() },
01055             // SDL_CONTROLLER_BUTTON_LEFTSHOULDER: Game menu
01056             { EVENT_KEYDOWN, KeyState(KEYCODE_F5, ASCII_F5), EVENT_KEYDOWN, KeyState(KEYCODE_F5, ASCII_F5) },
01057             // SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: Modifier + Shift
01058             { EVENT_KEYDOWN, KeyState(KEYCODE_INVALID, 0, KBD_SHIFT), EVENT_KEYDOWN, KeyState(KEYCODE_INVALID, 0, 0) },
01059             // SDL_CONTROLLER_BUTTON_DPAD_UP: Up (+R_trigger: Up+Right)
01060             { EVENT_KEYDOWN, KeyState(KEYCODE_KP8, 0), EVENT_KEYDOWN, KeyState(KEYCODE_KP9, 0) },
01061             // SDL_CONTROLLER_BUTTON_DPAD_DOWN: Down (+R_trigger: Down+Left)
01062             { EVENT_KEYDOWN, KeyState(KEYCODE_KP2, 0), EVENT_KEYDOWN, KeyState(KEYCODE_KP1, 0) },
01063             // SDL_CONTROLLER_BUTTON_DPAD_LEFT: Left (+R_trigger: Up+Left)
01064             { EVENT_KEYDOWN, KeyState(KEYCODE_KP4, 0), EVENT_KEYDOWN, KeyState(KEYCODE_KP7, 0) },
01065             // SDL_CONTROLLER_BUTTON_DPAD_RIGHT: Right (+R_trigger: Down+Right)
01066             { EVENT_KEYDOWN, KeyState(KEYCODE_KP6, 0), EVENT_KEYDOWN, KeyState(KEYCODE_KP3, 0) }
01067     };
01068 
01069     if (ev.cbutton.button > SDL_CONTROLLER_BUTTON_DPAD_RIGHT) {
01070         warning("Unknown SDL controller button: '%d'", ev.cbutton.button);
01071         return false;
01072     }
01073 
01074     if (!_km.modifier) {
01075         event.type = mapping[ev.cbutton.button].normalType;
01076         event.kbd = mapping[ev.cbutton.button].normalKeystate;
01077     } else {
01078         event.type = mapping[ev.cbutton.button].modifierType;
01079         event.kbd = mapping[ev.cbutton.button].modifierKeystate;
01080     }
01081 
01082     // Setting the mouse speed modifier after filling the event structure above
01083     // ensures that the shift key events are correctly handled
01084     if (ev.cbutton.button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER) {
01085         // Right shoulder is the modifier button that makes the mouse go slower
01086         // and allows access to an extended layout while pressed.
01087         _km.modifier = !buttonUp;
01088     }
01089 
01090     if (event.type == EVENT_LBUTTONDOWN || event.type == EVENT_RBUTTONDOWN) {
01091         processMouseEvent(event, _km.x / MULTIPLIER, _km.y / MULTIPLIER);
01092     }
01093 
01094     if (buttonUp) {
01095         // The event mapping table is for button down events. If we received a button up event,
01096         // transform the event type to the corresponding up type.
01097         if (event.type == EVENT_KEYDOWN) {
01098             event.type = EVENT_KEYUP;
01099         } else if (event.type == EVENT_LBUTTONDOWN) {
01100             event.type = EVENT_LBUTTONUP;
01101         } else if (event.type == EVENT_RBUTTONDOWN) {
01102             event.type = EVENT_RBUTTONUP;
01103         } else {
01104             // Handled in key down
01105             event.type = EVENT_INVALID;
01106         }
01107     }
01108 
01109     return event.type != EVENT_INVALID;
01110 }
01111 
01112 bool SdlEventSource::handleControllerAxisMotion(const SDL_Event &ev, Common::Event &event) {
01113     if (ev.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX) {
01114         _km.joy_x = ev.caxis.value;
01115         return handleAxisToMouseMotion(_km.joy_x, _km.joy_y);
01116     } else if (ev.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY) {
01117         _km.joy_y = ev.caxis.value;
01118         return handleAxisToMouseMotion(_km.joy_x, _km.joy_y);
01119     }
01120 
01121     return false;
01122 }
01123 #endif
01124 
01125 bool SdlEventSource::handleAxisToMouseMotion(int16 xAxis, int16 yAxis) {
01126 #ifdef JOY_INVERT_Y
01127     yAxis = -yAxis;
01128 #endif
01129 
01130     // conversion factor between keyboard mouse and joy axis value
01131     int vel_to_axis = (1500 / MULTIPLIER);
01132 
01133     // radial and scaled deadzone
01134 
01135     float analogX = (float)xAxis;
01136     float analogY = (float)yAxis;
01137     float deadZone = (float)ConfMan.getInt("joystick_deadzone") * 1000.0f;
01138 
01139     float magnitude = sqrt(analogX * analogX + analogY * analogY);
01140 
01141     if (magnitude >= deadZone) {
01142         _km.x_down_count = 0;
01143         _km.y_down_count = 0;
01144         float scalingFactor = 1.0f / magnitude * (magnitude - deadZone) / (32769.0f - deadZone);
01145         _km.x_vel = (int16)(analogX * scalingFactor * 32768.0f / vel_to_axis);
01146         _km.y_vel = (int16)(analogY * scalingFactor * 32768.0f / vel_to_axis);
01147     } else {
01148         _km.x_vel = 0;
01149         _km.y_vel = 0;
01150     }
01151 
01152     return false;
01153 }
01154 
01155 bool SdlEventSource::remapKey(SDL_Event &ev, Common::Event &event) {
01156     return false;
01157 }
01158 
01159 void SdlEventSource::resetKeyboardEmulation(int16 x_max, int16 y_max) {
01160     _km.x_max = x_max;
01161     _km.y_max = y_max;
01162     _km.delay_time = 12;
01163     _km.last_time = 0;
01164     _km.modifier = false;
01165     _km.joy_x = 0;
01166     _km.joy_y = 0;
01167 }
01168 
01169 void SdlEventSource::fakeWarpMouse(const int x, const int y) {
01170     _queuedFakeMouseMove = true;
01171     _fakeMouseMove.type = Common::EVENT_MOUSEMOVE;
01172     _fakeMouseMove.mouse = Common::Point(x, y);
01173 }
01174 
01175 bool SdlEventSource::handleResizeEvent(Common::Event &event, int w, int h) {
01176     if (_graphicsManager) {
01177         _graphicsManager->notifyResize(w, h);
01178 
01179         // If the screen changed, send an Common::EVENT_SCREEN_CHANGED
01180         int screenID = ((OSystem_SDL *)g_system)->getGraphicsManager()->getScreenChangeID();
01181         if (screenID != _lastScreenID) {
01182             _lastScreenID = screenID;
01183             event.type = Common::EVENT_SCREEN_CHANGED;
01184             return true;
01185         }
01186     }
01187 
01188     return false;
01189 }
01190 
01191 SDLKey SdlEventSource::obtainKeycode(const SDL_keysym keySym) {
01192 #if !SDL_VERSION_ATLEAST(2, 0, 0) && defined(WIN32) && !defined(_WIN32_WCE)
01193     // WORKAROUND: SDL 1.2 on Windows does not use the user configured keyboard layout,
01194     // resulting in "keySym.sym" values to always be those expected for an US keyboard.
01195     // For example, SDL returns SDLK_Q when pressing the 'A' key on an AZERTY keyboard.
01196     // This defeats the purpose of keycodes which is to be able to refer to a key without
01197     // knowing where it is physically located.
01198     // We work around this issue by querying the currently active Windows keyboard layout
01199     // using the scancode provided by SDL.
01200 
01201     if (keySym.sym >= SDLK_0 && keySym.sym <= SDLK_9) {
01202         // The keycode returned by SDL is kept for the number keys.
01203         // Querying the keyboard layout for those would return the base key values
01204         // for AZERTY keyboards, which are not numbers. For example, SDLK_1 would
01205         // map to SDLK_AMPERSAND. This is theoretically correct but practically unhelpful,
01206         // because it makes it impossible to handle key combinations such as "ctrl-1".
01207         return keySym.sym;
01208     }
01209 
01210     int vk = MapVirtualKey(keySym.scancode, MAPVK_VSC_TO_VK);
01211     if (vk) {
01212         int ch = (MapVirtualKey(vk, MAPVK_VK_TO_CHAR) & 0x7FFF);
01213         // The top bit of the result of MapVirtualKey with MAPVK_VSC_TO_VK signals
01214         // a dead key was pressed. In that case we keep the value of the accent alone.
01215         if (ch) {
01216             if (ch >= 'A' && ch <= 'Z') {
01217                 // Windows returns uppercase ASCII whereas SDL expects lowercase
01218                 return (SDLKey)(SDLK_a + (ch - 'A'));
01219             } else {
01220                 return (SDLKey)ch;
01221             }
01222         }
01223     }
01224 #endif
01225 
01226     return keySym.sym;
01227 }
01228 
01229 uint32 SdlEventSource::obtainUnicode(const SDL_keysym keySym) {
01230 #if SDL_VERSION_ATLEAST(2, 0, 0)
01231     SDL_Event events[2];
01232 
01233     // Update the event queue here to give SDL a chance to insert TEXTINPUT
01234     // events for KEYDOWN events. Otherwise we have a high chance that on
01235     // Windows the TEXTINPUT event is not in the event queue at this point.
01236     // In this case we will get two events with ascii values due to mapKey
01237     // and dispatchSDLEvent. This results in nasty double input of characters
01238     // in the GUI.
01239     //
01240     // FIXME: This is all a bit fragile because in mapKey we derive the ascii
01241     // value from the key code if no unicode value is given. This is legacy
01242     // behavior and should be removed anyway. If that is removed, we might not
01243     // even need to do this peeking here but instead can rely on the
01244     // SDL_TEXTINPUT case in dispatchSDLEvent to introduce keydown/keyup with
01245     // proper ASCII values (but with KEYCODE_INVALID as keycode).
01246     SDL_PumpEvents();
01247 
01248     // In SDL2, the unicode field has been removed from the keysym struct.
01249     // Instead a SDL_TEXTINPUT event is generated on key combinations that
01250     // generates unicode.
01251     // Here we peek into the event queue for the event to see if it exists.
01252     int n = SDL_PeepEvents(events, 2, SDL_PEEKEVENT, SDL_KEYDOWN, SDL_TEXTINPUT);
01253     // Make sure that the TEXTINPUT event belongs to this KEYDOWN
01254     // event and not another pending one.
01255     if ((n > 0 && events[0].type == SDL_TEXTINPUT)
01256         || (n > 1 && events[0].type != SDL_KEYDOWN && events[1].type == SDL_TEXTINPUT)) {
01257         // Remove the text input event we associate with the key press. This
01258         // makes sure we never get any SDL_TEXTINPUT events which do "belong"
01259         // to SDL_KEYDOWN events.
01260         n = SDL_PeepEvents(events, 1, SDL_GETEVENT, SDL_TEXTINPUT, SDL_TEXTINPUT);
01261         // This is basically a paranoia safety check because we know there
01262         // must be a text input event in the queue.
01263         if (n > 0) {
01264             return convUTF8ToUTF32(events[0].text.text);
01265         } else {
01266             return 0;
01267         }
01268     } else {
01269         return 0;
01270     }
01271 #else
01272     return keySym.unicode;
01273 #endif
01274 }
01275 
01276 #endif


Generated on Sat May 25 2019 05:00:53 for ResidualVM by doxygen 1.7.1
curved edge   curved edge