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

grim_controls.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM 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/events.h"
00024 #include "engines/grim/lua.h"
00025 #include "engines/grim/grim.h"
00026 #include "engines/grim/debugger.h"
00027 
00028 namespace Grim {
00029 
00030 // Entries in the system.controls table
00031 
00032 const ControlDescriptor controls[] = {
00033     { "KEY_ESCAPE", Common::KEYCODE_ESCAPE },
00034     { "KEY_1", Common::KEYCODE_1 },
00035     { "KEY_2", Common::KEYCODE_2 },
00036     { "KEY_3", Common::KEYCODE_3 },
00037     { "KEY_4", Common::KEYCODE_4 },
00038     { "KEY_5", Common::KEYCODE_5 },
00039     { "KEY_6", Common::KEYCODE_6 },
00040     { "KEY_7", Common::KEYCODE_7 },
00041     { "KEY_8", Common::KEYCODE_8 },
00042     { "KEY_9", Common::KEYCODE_9 },
00043     { "KEY_0", Common::KEYCODE_0 },
00044     { "KEY_MINUS", Common::KEYCODE_MINUS },
00045     { "KEY_EQUALS", Common::KEYCODE_EQUALS },
00046     { "KEY_BACK", Common::KEYCODE_BACKSPACE },
00047     { "KEY_TAB", Common::KEYCODE_TAB },
00048     { "KEY_Q", Common::KEYCODE_q },
00049     { "KEY_W", Common::KEYCODE_w },
00050     { "KEY_E", Common::KEYCODE_e },
00051     { "KEY_R", Common::KEYCODE_r },
00052     { "KEY_T", Common::KEYCODE_t },
00053     { "KEY_Y", Common::KEYCODE_y },
00054     { "KEY_U", Common::KEYCODE_u },
00055     { "KEY_I", Common::KEYCODE_i },
00056     { "KEY_O", Common::KEYCODE_o },
00057     { "KEY_P", Common::KEYCODE_p },
00058     { "KEY_LBRACKET", Common::KEYCODE_LEFTBRACKET },
00059     { "KEY_RBRACKET", Common::KEYCODE_RIGHTBRACKET },
00060     { "KEY_RETURN", Common::KEYCODE_RETURN },
00061     { "KEY_LCONTROL", Common::KEYCODE_LCTRL },
00062     { "KEY_A", Common::KEYCODE_a },
00063     { "KEY_S", Common::KEYCODE_s },
00064     { "KEY_D", Common::KEYCODE_d },
00065     { "KEY_F", Common::KEYCODE_f },
00066     { "KEY_G", Common::KEYCODE_g },
00067     { "KEY_H", Common::KEYCODE_h },
00068     { "KEY_J", Common::KEYCODE_j },
00069     { "KEY_K", Common::KEYCODE_k },
00070     { "KEY_L", Common::KEYCODE_l },
00071     { "KEY_SEMICOLON", Common::KEYCODE_SEMICOLON },
00072     { "KEY_APOSTROPHE", Common::KEYCODE_QUOTEDBL },
00073     { "KEY_GRAVE", Common::KEYCODE_BACKQUOTE },
00074     { "KEY_LSHIFT", Common::KEYCODE_LSHIFT },
00075     { "KEY_BACKSLASH", Common::KEYCODE_BACKSLASH },
00076     { "KEY_Z", Common::KEYCODE_z },
00077     { "KEY_X", Common::KEYCODE_x },
00078     { "KEY_C", Common::KEYCODE_c },
00079     { "KEY_V", Common::KEYCODE_v },
00080     { "KEY_B", Common::KEYCODE_b },
00081     { "KEY_N", Common::KEYCODE_n },
00082     { "KEY_M", Common::KEYCODE_m },
00083     { "KEY_COMMA", Common::KEYCODE_COMMA },
00084     { "KEY_PERIOD", Common::KEYCODE_PERIOD },
00085     { "KEY_SLASH", Common::KEYCODE_SLASH },
00086     { "KEY_RSHIFT", Common::KEYCODE_RSHIFT },
00087     { "KEY_MULTIPLY", Common::KEYCODE_ASTERISK },
00088     { "KEY_LMENU", Common::KEYCODE_LALT },
00089     { "KEY_SPACE", Common::KEYCODE_SPACE },
00090     { "KEY_CAPITAL", Common::KEYCODE_CAPSLOCK },
00091     { "KEY_F1", Common::KEYCODE_F1 },
00092     { "KEY_F2", Common::KEYCODE_F2 },
00093     { "KEY_F3", Common::KEYCODE_F3 },
00094     { "KEY_F4", Common::KEYCODE_F4 },
00095     { "KEY_F5", Common::KEYCODE_F5 },
00096     { "KEY_F6", Common::KEYCODE_F6 },
00097     { "KEY_F7", Common::KEYCODE_F7 },
00098     { "KEY_F8", Common::KEYCODE_F8 },
00099     { "KEY_F9", Common::KEYCODE_F9 },
00100     { "KEY_F10", Common::KEYCODE_F10 },
00101     { "KEY_NUMLOCK", Common::KEYCODE_NUMLOCK },
00102     { "KEY_SCROLL", Common::KEYCODE_SCROLLOCK },
00103     { "KEY_NUMPAD7", Common::KEYCODE_KP7 },
00104     { "KEY_NUMPAD8", Common::KEYCODE_KP8 },
00105     { "KEY_NUMPAD9", Common::KEYCODE_KP9 },
00106     { "KEY_SUBTRACT", Common::KEYCODE_KP_MINUS },
00107     { "KEY_NUMPAD4", Common::KEYCODE_KP4 },
00108     { "KEY_NUMPAD5", Common::KEYCODE_KP5 },
00109     { "KEY_NUMPAD6", Common::KEYCODE_KP6 },
00110     { "KEY_ADD", Common::KEYCODE_KP_PLUS },
00111     { "KEY_NUMPAD1", Common::KEYCODE_KP1 },
00112     { "KEY_NUMPAD2", Common::KEYCODE_KP2 },
00113     { "KEY_NUMPAD3", Common::KEYCODE_KP3 },
00114     { "KEY_NUMPAD0", Common::KEYCODE_KP0 },
00115     { "KEY_DECIMAL", Common::KEYCODE_KP_PERIOD },
00116     { "KEY_F11", Common::KEYCODE_F11 },
00117     { "KEY_F12", Common::KEYCODE_F12 },
00118     { "KEY_F13", Common::KEYCODE_F13 },
00119     { "KEY_F14", Common::KEYCODE_F14 },
00120     { "KEY_F15", Common::KEYCODE_F15 },
00121     { "KEY_NUMPADEQUALS", Common::KEYCODE_KP_EQUALS },
00122     { "KEY_AT", Common::KEYCODE_AT },
00123     { "KEY_COLON", Common::KEYCODE_COLON },
00124     { "KEY_UNDERLINE", Common::KEYCODE_UNDERSCORE },
00125     { "KEY_STOP", Common::KEYCODE_BREAK },
00126     { "KEY_NUMPADENTER", Common::KEYCODE_KP_ENTER },
00127     { "KEY_RCONTROL", Common::KEYCODE_RCTRL },
00128     { "KEY_NUMPADCOMMA", Common::KEYCODE_KP_PERIOD },
00129     { "KEY_DIVIDE", Common::KEYCODE_KP_DIVIDE },
00130     { "KEY_SYSRQ", Common::KEYCODE_SYSREQ },
00131     { "KEY_RMENU", Common::KEYCODE_RALT },
00132     { "KEY_HOME", Common::KEYCODE_HOME },
00133     { "KEY_UP", Common::KEYCODE_UP },
00134     { "KEY_PRIOR", Common::KEYCODE_PAGEUP },
00135     { "KEY_LEFT", Common::KEYCODE_LEFT },
00136     { "KEY_RIGHT", Common::KEYCODE_RIGHT },
00137     { "KEY_END", Common::KEYCODE_END },
00138     { "KEY_DOWN", Common::KEYCODE_DOWN },
00139     { "KEY_NEXT", Common::KEYCODE_PAGEDOWN },
00140     { "KEY_INSERT", Common::KEYCODE_INSERT },
00141     { "KEY_DELETE", Common::KEYCODE_DELETE },
00142     { "KEY_LWIN", Common::KEYCODE_LSUPER },
00143     { "KEY_RWIN", Common::KEYCODE_RSUPER },
00144     { "KEY_APPS", Common::KEYCODE_MENU },
00145     { "KEY_EQUAL", Common::KEYCODE_EQUALS },
00146     { "KEY_CONTROL", Common::KEYCODE_LCTRL },
00147     { "KEY_SHIFT", Common::KEYCODE_LSHIFT },
00148     { "KEY_ALT", Common::KEYCODE_LALT },
00149     { "KEY_BACKSPACE", Common::KEYCODE_BACKSPACE },
00150     { "KEY_NP9", Common::KEYCODE_KP9 },
00151     { "KEY_NP8", Common::KEYCODE_KP8 },
00152     { "KEY_NP7", Common::KEYCODE_KP7 },
00153     { "KEY_NP6", Common::KEYCODE_KP6 },
00154     { "KEY_NP5", Common::KEYCODE_KP5 },
00155     { "KEY_NP4", Common::KEYCODE_KP4 },
00156     { "KEY_NP3", Common::KEYCODE_KP3 },
00157     { "KEY_NP2", Common::KEYCODE_KP2 },
00158     { "KEY_NP1", Common::KEYCODE_KP1 },
00159     { "KEY_NP0", Common::KEYCODE_KP0 },
00160     { "KEY_NPENTER", Common::KEYCODE_KP_ENTER },
00161     { "KEY_PAGEUP", Common::KEYCODE_PAGEUP },
00162     { "KEY_PAGEDOWN", Common::KEYCODE_PAGEDOWN },
00163     { "KEY_SCROLL", Common::KEYCODE_SCROLLOCK },
00164     { "KEY_CAPSLOCK", Common::KEYCODE_CAPSLOCK },
00165     { "KEY_LEFTBRACKET", Common::KEYCODE_LEFTBRACKET },
00166     { "KEY_RIGHTBRACKET", Common::KEYCODE_RIGHTBRACKET },
00167     { "KEY_TILDE", Common::KEYCODE_TILDE },
00168     { "KEY_QUOTE", Common::KEYCODE_QUOTE },
00169     { "KEY_PAUSE", Common::KEYCODE_PAUSE },
00170     { "KEY_CLEAR", Common::KEYCODE_CLEAR },
00171 
00172     { "KEY_JOY1_B1", KEYCODE_JOY1_B1 },
00173     { "KEY_JOY1_B2", KEYCODE_JOY1_B2 },
00174     { "KEY_JOY1_B3", KEYCODE_JOY1_B3 },
00175     { "KEY_JOY1_B4", KEYCODE_JOY1_B4 },
00176     { "KEY_JOY1_B5", KEYCODE_JOY1_B5 },
00177     { "KEY_JOY1_B6", KEYCODE_JOY1_B6 },
00178     { "KEY_JOY1_B7", KEYCODE_JOY1_B7 },
00179     { "KEY_JOY1_B8", KEYCODE_JOY1_B8 },
00180     { "KEY_JOY1_B9", KEYCODE_JOY1_B9 },
00181     { "KEY_JOY1_B10", KEYCODE_JOY1_B10 },
00182     { "KEY_JOY1_B11", KEYCODE_JOY1_B11 },
00183     { "KEY_JOY1_B12", KEYCODE_JOY1_B12 },
00184     { "KEY_JOY1_B13", KEYCODE_JOY1_B13 },
00185     { "KEY_JOY1_B14", KEYCODE_JOY1_B14 },
00186     { "KEY_JOY1_B15", KEYCODE_JOY1_B15 },
00187     { "KEY_JOY1_B16", KEYCODE_JOY1_B16 },
00188     { "KEY_JOY1_B17", KEYCODE_JOY1_B17 },
00189     { "KEY_JOY1_B18", KEYCODE_JOY1_B18 },
00190     { "KEY_JOY1_B19", KEYCODE_JOY1_B19 },
00191     { "KEY_JOY1_B20", KEYCODE_JOY1_B20 },
00192     { "KEY_JOY1_HLEFT", KEYCODE_JOY1_HLEFT },
00193     { "KEY_JOY1_HUP", KEYCODE_JOY1_HUP },
00194     { "KEY_JOY1_HRIGHT", KEYCODE_JOY1_HRIGHT },
00195     { "KEY_JOY1_HDOWN", KEYCODE_JOY1_HDOWN },
00196     { "KEY_JOY2_B1", KEYCODE_JOY2_B1 },
00197     { "KEY_JOY2_B2", KEYCODE_JOY2_B2 },
00198     { "KEY_JOY2_B3", KEYCODE_JOY2_B3 },
00199     { "KEY_JOY2_B4", KEYCODE_JOY2_B4 },
00200     { "KEY_JOY2_B5", KEYCODE_JOY2_B5 },
00201     { "KEY_JOY2_B6", KEYCODE_JOY2_B6 },
00202     { "KEY_JOY2_B7", KEYCODE_JOY2_B7 },
00203     { "KEY_JOY2_B8", KEYCODE_JOY2_B8 },
00204     { "KEY_JOY2_B9", KEYCODE_JOY2_B9 },
00205     { "KEY_JOY2_B10", KEYCODE_JOY2_B10 },
00206     { "KEY_JOY2_HLEFT", KEYCODE_JOY2_HLEFT },
00207     { "KEY_JOY2_HUP", KEYCODE_JOY2_HUP },
00208     { "KEY_JOY2_HRIGHT", KEYCODE_JOY2_HRIGHT },
00209     { "KEY_JOY2_HDOWN", KEYCODE_JOY2_HDOWN },
00210     { "KEY_MOUSE_B1", KEYCODE_MOUSE_B1 },
00211     { "KEY_MOUSE_B2", KEYCODE_MOUSE_B2 },
00212     { "KEY_MOUSE_B3", KEYCODE_MOUSE_B3 },
00213     { "KEY_MOUSE_B4", KEYCODE_MOUSE_B4 },
00214     { "AXIS_JOY1_X", KEYCODE_AXIS_JOY1_X },
00215     { "AXIS_JOY1_Y", KEYCODE_AXIS_JOY1_Y },
00216     { "AXIS_JOY1_Z", KEYCODE_AXIS_JOY1_Z },
00217     { "AXIS_JOY1_R", KEYCODE_AXIS_JOY1_R },
00218     { "AXIS_JOY1_U", KEYCODE_AXIS_JOY1_U },
00219     { "AXIS_JOY1_V", KEYCODE_AXIS_JOY1_V },
00220     { "AXIS_JOY2_X", KEYCODE_AXIS_JOY2_X },
00221     { "AXIS_JOY2_Y", KEYCODE_AXIS_JOY2_Y },
00222     { "AXIS_JOY2_Z", KEYCODE_AXIS_JOY2_Z },
00223     { "AXIS_JOY2_R", KEYCODE_AXIS_JOY2_R },
00224     { "AXIS_JOY2_U", KEYCODE_AXIS_JOY2_U },
00225     { "AXIS_JOY2_V", KEYCODE_AXIS_JOY2_V },
00226     { "AXIS_MOUSE_X", KEYCODE_AXIS_MOUSE_X },
00227     { "AXIS_MOUSE_Y", KEYCODE_AXIS_MOUSE_Y },
00228     { "AXIS_MOUSE_Z", KEYCODE_AXIS_MOUSE_Z },
00229 
00230 //PS2
00231     { "KEY_JOY1_SQUARE", KEYCODE_JOY1_B1 },
00232     { "KEY_JOY1_TRIANGLE", KEYCODE_JOY1_B2 },
00233     { "KEY_JOY1_CIRCLE", KEYCODE_JOY1_B3 },
00234     { "KEY_JOY1_X", KEYCODE_JOY1_B4 },
00235     { "KEY_JOY1_R1", KEYCODE_JOY1_B5 },
00236     { "KEY_JOY1_L1", KEYCODE_JOY1_B6 },
00237     { "KEY_JOY1_R2", KEYCODE_JOY1_B7 },
00238     { "KEY_JOY1_L2", KEYCODE_JOY1_B8 },
00239     { "KEY_JOY1_START", KEYCODE_JOY1_B9 },
00240     { "KEY_JOY1_SELECT", KEYCODE_JOY1_B10 },
00241     { "KEY_JOY1_DPAD_U", KEYCODE_JOY1_B11 },
00242     { "KEY_JOY1_DPAD_D", KEYCODE_JOY1_B12 },
00243     { "KEY_JOY1_DPAD_L", KEYCODE_JOY1_B13 },
00244     { "KEY_JOY1_DPAD_R", KEYCODE_JOY1_B14 },
00245     { "KEY_JOY1_LMUSHROOM", KEYCODE_JOY1_B15 },
00246     { "KEY_JOY1_RMUSHROOM", KEYCODE_JOY1_B16 },
00247 // Joy2
00248     { "KEY_JOY2_SQUARE", KEYCODE_JOY2_B1 },
00249     { "KEY_JOY2_TRIANGLE", KEYCODE_JOY2_B2 },
00250     { "KEY_JOY2_CIRCLE", KEYCODE_JOY2_B3 },
00251     { "KEY_JOY2_X", KEYCODE_JOY2_B4 },
00252     { "KEY_JOY2_R1", KEYCODE_JOY2_B5 },
00253     { "KEY_JOY2_L1", KEYCODE_JOY2_B6 },
00254     { "KEY_JOY2_R2", KEYCODE_JOY2_B7 },
00255     { "KEY_JOY2_L2", KEYCODE_JOY2_B8 },
00256     { "KEY_JOY2_START", KEYCODE_JOY2_B9 },
00257     { "KEY_JOY2_SELECT", KEYCODE_JOY2_B10 },
00258     { "KEY_JOY2_DPAD_U", KEYCODE_JOY2_B11 },
00259     { "KEY_JOY2_DPAD_D", KEYCODE_JOY2_B12 },
00260     { "KEY_JOY2_DPAD_L", KEYCODE_JOY2_B13 },
00261     { "KEY_JOY2_DPAD_R", KEYCODE_JOY2_B14 },
00262     { "KEY_JOY2_LMUSHROOM", KEYCODE_JOY2_B15 },
00263     { "KEY_JOY2_RMUSHROOM", KEYCODE_JOY2_B16 },
00264 
00265 // tell EMI that the joystick can be used
00266     { "joy_selected", 1 },
00267 
00268     { nullptr, 0 }
00269 };
00270 
00271 // CHAR_KEY tests to see whether a keycode is for
00272 // a "character" handler or a "button" handler
00273 #define CHAR_KEY(k) ((k >= 'a' && k <= 'z') || (k >= 'A' && k <= 'Z') || (k >= '0' && k <= '9') || k == ' ')
00274 
00275 void GrimEngine::handleChars(Common::EventType operation, const Common::KeyState &key) {
00276     if (!CHAR_KEY(key.ascii))
00277         return;
00278 
00279     char keychar[2];
00280     keychar[0] = key.ascii;
00281     keychar[1] = 0;
00282 
00283     LuaObjects objects;
00284     objects.add(keychar);
00285 
00286     if (!LuaBase::instance()->callback("characterHandler", objects)) {
00287         error("handleChars: invalid handler");
00288     }
00289 }
00290 
00291 void GrimEngine::handleControls(Common::EventType operation, const Common::KeyState &key) {
00292     // Might also want to support keypad-enter?
00293     if (key.hasFlags(Common::KBD_ALT) && key.keycode == Common::KEYCODE_RETURN && operation == Common::EVENT_KEYDOWN) {
00294         _changeFullscreenState = true;
00295         return;
00296     }
00297 
00298     if (key.hasFlags(Common::KBD_CTRL) && key.keycode == Common::KEYCODE_d && operation == Common::EVENT_KEYDOWN) {
00299         _debugger->attach();
00300         clearEventQueue();
00301         return;
00302     }
00303     // If we're not supposed to handle the key then don't
00304     if (!_controlsEnabled[key.keycode])
00305         return;
00306 
00307     LuaObjects objects;
00308     objects.add(key.keycode);
00309     if (operation == Common::EVENT_KEYDOWN) {
00310         objects.add(1);
00311         objects.add(1);
00312     } else {
00313         objects.addNil();
00314         objects.add(0);
00315     }
00316     objects.add(0);
00317     if (!LuaBase::instance()->callback("buttonHandler", objects)) {
00318         error("handleControls: invalid keys handler");
00319     }
00320 
00321     if (operation == Common::EVENT_KEYDOWN)
00322         _controlsState[key.keycode] = true;
00323     else if (operation == Common::EVENT_KEYUP)
00324         _controlsState[key.keycode] = false;
00325 }
00326 
00327 void GrimEngine::handleJoyAxis(byte axis, int16 position) {
00328     if (axis > NUM_JOY_AXES)
00329         return;
00330 
00331     int keycode = KEYCODE_AXIS_JOY1_X + axis;
00332     if (!_controlsEnabled[keycode])
00333         return;
00334 
00335     float fpos;
00336     if (position < Common::JOYAXIS_MIN / 2) {
00337         fpos = -1;
00338     } else if (position > Common::JOYAXIS_MAX / 2) {
00339         fpos = 1;
00340     } else {
00341         fpos = 0;
00342     }
00343 
00344     if (fabsf(fpos - _joyAxisPosition[axis]) > 0.1) {
00345         LuaObjects objects;
00346         objects.add(keycode);
00347         objects.add(fpos);
00348         if (!LuaBase::instance()->callback("axisHandler", objects)) {
00349             error("handleJoyAxis: invalid joystick handler");
00350         }
00351         _joyAxisPosition[axis] = fpos;
00352     }
00353 }
00354 
00355 void GrimEngine::handleJoyButton(Common::EventType operation, byte button) {
00356     if (button > NUM_JOY_BUTTONS)
00357         return;
00358 
00359     int keycode = KEYCODE_JOY1_B1 + button;
00360     if (!_controlsEnabled[keycode])
00361         return;
00362 
00363     LuaObjects objects;
00364     objects.add(keycode);
00365     if (operation == Common::EVENT_JOYBUTTON_DOWN) {
00366         objects.add(1);
00367         objects.add(1);
00368     } else if (operation == Common::EVENT_JOYBUTTON_UP) {
00369         objects.addNil();
00370         objects.add(0);
00371     }
00372     objects.add(0);
00373     if (!LuaBase::instance()->callback("buttonHandler", objects)) {
00374         error("handleControls: invalid keys handler");
00375     }
00376 
00377     if (operation == Common::EVENT_JOYBUTTON_DOWN)
00378         _controlsState[keycode] = true;
00379     else if (operation == Common::EVENT_JOYBUTTON_UP)
00380         _controlsState[keycode] = false;
00381 }
00382 
00383 } // end of namespace Grim
00384 


Generated on Sat Feb 16 2019 05:00:52 for ResidualVM by doxygen 1.7.1
curved edge   curved edge