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

hardware-input.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 "backends/keymapper/hardware-input.h"
00024 
00025 #ifdef ENABLE_KEYMAPPER
00026 
00027 #include "backends/keymapper/keymapper.h"
00028 
00029 namespace Common {
00030 
00031 static const KeyTableEntry defaultKeys[] = {
00032     {"BACKSPACE", KEYCODE_BACKSPACE, ASCII_BACKSPACE, "Backspace", false},
00033     {"TAB", KEYCODE_TAB, ASCII_TAB, "Tab", false},
00034     {"CLEAR", KEYCODE_CLEAR, 0, "Clear", false},
00035     {"RETURN", KEYCODE_RETURN, ASCII_RETURN, "Return", false},
00036     {"PAUSE", KEYCODE_PAUSE, 0, "Pause", false},
00037     {"ESCAPE", KEYCODE_ESCAPE, ASCII_ESCAPE, "Esc", false},
00038     {"SPACE", KEYCODE_SPACE, ASCII_SPACE, "Space", false},
00039     {"EXCLAIM", KEYCODE_EXCLAIM, '!', "!", false},
00040     {"QUOTEDBL", KEYCODE_QUOTEDBL, '"', "\"", false},
00041     {"HASH", KEYCODE_HASH, '#', "#", false},
00042     {"DOLLAR", KEYCODE_DOLLAR, '$', "$", false},
00043     {"AMPERSAND", KEYCODE_AMPERSAND, '&', "&", false},
00044     {"QUOTE", KEYCODE_QUOTE, '\'', "'", false},
00045     {"LEFTPAREN", KEYCODE_LEFTPAREN, '(', "(", false},
00046     {"RIGHTPAREN", KEYCODE_RIGHTPAREN, ')', ")", false},
00047     {"ASTERISK", KEYCODE_ASTERISK, '*', "*", false},
00048     {"PLUS", KEYCODE_PLUS, '+', "+", false},
00049     {"COMMA", KEYCODE_COMMA, ',', ",", false},
00050     {"MINUS", KEYCODE_MINUS, '-', "-", false},
00051     {"PERIOD", KEYCODE_PERIOD, '.', ".", false},
00052     {"SLASH", KEYCODE_SLASH, '/', "/", false},
00053     {"0", KEYCODE_0, '0', "0", false},
00054     {"1", KEYCODE_1, '1', "1", false},
00055     {"2", KEYCODE_2, '2', "2", false},
00056     {"3", KEYCODE_3, '3', "3", false},
00057     {"4", KEYCODE_4, '4', "4", false},
00058     {"5", KEYCODE_5, '5', "5", false},
00059     {"6", KEYCODE_6, '6', "6", false},
00060     {"7", KEYCODE_7, '7', "7", false},
00061     {"8", KEYCODE_8, '8', "8", false},
00062     {"9", KEYCODE_9, '9', "9", false},
00063     {"COLON", KEYCODE_COLON, ':', ":", false},
00064     {"SEMICOLON", KEYCODE_SEMICOLON, ';', ";", false},
00065     {"LESS", KEYCODE_LESS, '<', "<", false},
00066     {"EQUALS", KEYCODE_EQUALS, '=', "=", false},
00067     {"GREATER", KEYCODE_GREATER, '>', ">", false},
00068     {"QUESTION", KEYCODE_QUESTION, '?', "?", false},
00069     {"AT", KEYCODE_AT, '@', "@", false},
00070 
00071     {"LEFTBRACKET", KEYCODE_LEFTBRACKET, '[', "[", false},
00072     {"BACKSLASH", KEYCODE_BACKSLASH, '\\', "\\", false},
00073     {"RIGHTBRACKET", KEYCODE_RIGHTBRACKET, ']', "]", false},
00074     {"CARET", KEYCODE_CARET, '^', "^", false},
00075     {"UNDERSCORE", KEYCODE_UNDERSCORE, '_', "_", false},
00076     {"BACKQUOTE", KEYCODE_BACKQUOTE, '`', "`", false},
00077     {"a", KEYCODE_a, 'a', "a", true},
00078     {"b", KEYCODE_b, 'b', "b", true},
00079     {"c", KEYCODE_c, 'c', "c", true},
00080     {"d", KEYCODE_d, 'd', "d", true},
00081     {"e", KEYCODE_e, 'e', "e", true},
00082     {"f", KEYCODE_f, 'f', "f", true},
00083     {"g", KEYCODE_g, 'g', "g", true},
00084     {"h", KEYCODE_h, 'h', "h", true},
00085     {"i", KEYCODE_i, 'i', "i", true},
00086     {"j", KEYCODE_j, 'j', "j", true},
00087     {"k", KEYCODE_k, 'k', "k", true},
00088     {"l", KEYCODE_l, 'l', "l", true},
00089     {"m", KEYCODE_m, 'm', "m", true},
00090     {"n", KEYCODE_n, 'n', "n", true},
00091     {"o", KEYCODE_o, 'o', "o", true},
00092     {"p", KEYCODE_p, 'p', "p", true},
00093     {"q", KEYCODE_q, 'q', "q", true},
00094     {"r", KEYCODE_r, 'r', "r", true},
00095     {"s", KEYCODE_s, 's', "s", true},
00096     {"t", KEYCODE_t, 't', "t", true},
00097     {"u", KEYCODE_u, 'u', "u", true},
00098     {"v", KEYCODE_v, 'v', "v", true},
00099     {"w", KEYCODE_w, 'w', "w", true},
00100     {"x", KEYCODE_x, 'x', "x", true},
00101     {"y", KEYCODE_y, 'y', "y", true},
00102     {"z", KEYCODE_z, 'z', "z", true},
00103     {"DELETE", KEYCODE_DELETE, 0, "Del", false},
00104 
00105     // Numeric keypad
00106     {"KP0", KEYCODE_KP0, 0, "KP0", false},
00107     {"KP1", KEYCODE_KP1, 0, "KP1", false},
00108     {"KP2", KEYCODE_KP2, 0, "KP2", false},
00109     {"KP3", KEYCODE_KP3, 0, "KP3", false},
00110     {"KP4", KEYCODE_KP4, 0, "KP4", false},
00111     {"KP5", KEYCODE_KP5, 0, "KP5", false},
00112     {"KP6", KEYCODE_KP6, 0, "KP6", false},
00113     {"KP7", KEYCODE_KP7, 0, "KP7", false},
00114     {"KP8", KEYCODE_KP8, 0, "KP8", false},
00115     {"KP9", KEYCODE_KP9, 0, "KP9", false},
00116     {"KP_PERIOD", KEYCODE_KP_PERIOD, 0, "KP.", false},
00117     {"KP_DIVIDE", KEYCODE_KP_DIVIDE, 0, "KP/", false},
00118     {"KP_MULTIPLY", KEYCODE_KP_MULTIPLY, 0, "KP*", false},
00119     {"KP_MINUS", KEYCODE_KP_MINUS, 0, "KP-", false},
00120     {"KP_PLUS", KEYCODE_KP_PLUS, 0, "KP+", false},
00121     {"KP_ENTER", KEYCODE_KP_ENTER, 0, "KP Enter", false},
00122     {"KP_EQUALS", KEYCODE_KP_EQUALS, 0, "KP=", false},
00123 
00124     // Arrows + Home/End pad
00125     {"UP", KEYCODE_UP, 0, "Up", false},
00126     {"DOWN", KEYCODE_DOWN, 0, "Down", false},
00127     {"RIGHT", KEYCODE_RIGHT, 0, "Right", false},
00128     {"LEFT", KEYCODE_LEFT, 0, "Left", false},
00129     {"INSERT", KEYCODE_INSERT, 0, "Insert", false},
00130     {"HOME", KEYCODE_HOME, 0, "Home", false},
00131     {"END", KEYCODE_END, 0, "End", false},
00132     {"PAGEUP", KEYCODE_PAGEUP, 0, "PgUp", false},
00133     {"PAGEDOWN", KEYCODE_PAGEDOWN, 0, "PgDn", false},
00134 
00135     // Function keys
00136     {"F1", KEYCODE_F1, ASCII_F1, "F1", false},
00137     {"F2", KEYCODE_F2, ASCII_F2, "F2", false},
00138     {"F3", KEYCODE_F3, ASCII_F3, "F3", false},
00139     {"F4", KEYCODE_F4, ASCII_F4, "F4", false},
00140     {"F5", KEYCODE_F5, ASCII_F5, "F5", false},
00141     {"F6", KEYCODE_F6, ASCII_F6, "F6", false},
00142     {"F7", KEYCODE_F7, ASCII_F7, "F7", false},
00143     {"F8", KEYCODE_F8, ASCII_F8, "F8", false},
00144     {"F9", KEYCODE_F9, ASCII_F9, "F9", false},
00145     {"F10", KEYCODE_F10, ASCII_F10, "F10", false},
00146     {"F11", KEYCODE_F11, ASCII_F11, "F11", false},
00147     {"F12", KEYCODE_F12, ASCII_F12, "F12", false},
00148     {"F13", KEYCODE_F13, 0, "F13", false},
00149     {"F14", KEYCODE_F14, 0, "F14", false},
00150     {"F15", KEYCODE_F15, 0, "F15", false},
00151 
00152     // Miscellaneous function keys
00153     {"HELP", KEYCODE_HELP, 0, "Help", false},
00154     {"PRINT", KEYCODE_PRINT, 0, "Print", false},
00155     {"SYSREQ", KEYCODE_SYSREQ, 0, "SysRq", false},
00156     {"BREAK", KEYCODE_BREAK, 0, "Break", false},
00157     {"MENU", KEYCODE_MENU, 0, "Menu", false},
00158         // Power Macintosh power key
00159     {"POWER", KEYCODE_POWER, 0, "Power", false},
00160         // Some european keyboards
00161     {"EURO", KEYCODE_EURO, 0, "Euro", false},
00162         // Atari keyboard has Undo
00163     {"UNDO", KEYCODE_UNDO, 0, "Undo", false},
00164     {0, KEYCODE_INVALID, 0, 0, false}
00165 };
00166 
00167 static const ModifierTableEntry defaultModifiers[] = {
00168     { 0, "", "", false },
00169     { KBD_CTRL, "C+", "Ctrl+", false },
00170     { KBD_ALT, "A+", "Alt+", false },
00171     { KBD_SHIFT, "", "", true },
00172     { KBD_CTRL | KBD_ALT, "C+A+", "Ctrl+Alt+", false },
00173     { KBD_SHIFT | KBD_CTRL, "S+C+", "Shift+Ctrl+", true },
00174     { KBD_SHIFT | KBD_CTRL | KBD_ALT, "C+A+", "Ctrl+Alt+", true },
00175     { 0, 0, 0, false }
00176 };
00177 
00178 HardwareInputSet::HardwareInputSet(bool useDefault, const KeyTableEntry *keys, const ModifierTableEntry *modifiers) {
00179     if (useDefault)
00180         addHardwareInputs(defaultKeys, defaultModifiers);
00181     if (keys)
00182         addHardwareInputs(keys, modifiers ? modifiers : defaultModifiers);
00183 }
00184 
00185 HardwareInputSet::~HardwareInputSet() {
00186     List<const HardwareInput *>::const_iterator it;
00187 
00188     for (it = _inputs.begin(); it != _inputs.end(); ++it)
00189         delete *it;
00190 }
00191 
00192 void HardwareInputSet::addHardwareInput(const HardwareInput *input) {
00193     assert(input);
00194 
00195     debug(8, "Adding hardware input [%s][%s]", input->id.c_str(), input->description.c_str());
00196 
00197     removeHardwareInput(input);
00198 
00199     _inputs.push_back(input);
00200 }
00201 
00202 const HardwareInput *HardwareInputSet::findHardwareInput(String id) const {
00203     List<const HardwareInput *>::const_iterator it;
00204 
00205     for (it = _inputs.begin(); it != _inputs.end(); ++it) {
00206         if ((*it)->id == id)
00207             return (*it);
00208     }
00209     return 0;
00210 }
00211 
00212 const HardwareInput *HardwareInputSet::findHardwareInput(const HardwareInputCode code) const {
00213     List<const HardwareInput *>::const_iterator it;
00214 
00215     for (it = _inputs.begin(); it != _inputs.end(); ++it) {
00216         const HardwareInput *entry = *it;
00217         if (entry->type == kHardwareInputTypeGeneric && entry->inputCode == code)
00218             return entry;
00219     }
00220     return 0;
00221 }
00222 
00223 const HardwareInput *HardwareInputSet::findHardwareInput(const KeyState& keystate) const {
00224     List<const HardwareInput *>::const_iterator it;
00225 
00226     for (it = _inputs.begin(); it != _inputs.end(); ++it) {
00227         const HardwareInput *entry = *it;
00228         if (entry->type == kHardwareInputTypeKeyboard && entry->key == keystate)
00229             return entry;
00230     }
00231     return 0;
00232 }
00233 
00234 void HardwareInputSet::addHardwareInputs(const HardwareInputTableEntry inputs[]) {
00235     for (const HardwareInputTableEntry *entry = inputs; entry->hwId; ++entry)
00236         addHardwareInput(new HardwareInput(entry->hwId, entry->code, entry->desc));
00237 }
00238 
00239 void HardwareInputSet::addHardwareInputs(const KeyTableEntry keys[], const ModifierTableEntry modifiers[]) {
00240     const KeyTableEntry *key;
00241     const ModifierTableEntry *mod;
00242     char fullKeyId[50];
00243     char fullKeyDesc[100];
00244     uint16 ascii;
00245 
00246     for (mod = modifiers; mod->id; mod++) {
00247         for (key = keys; key->hwId; key++) {
00248             ascii = key->ascii;
00249 
00250             if (mod->shiftable && key->shiftable) {
00251                 snprintf(fullKeyId, 50, "%s%c", mod->id, toupper(key->hwId[0]));
00252                 snprintf(fullKeyDesc, 100, "%s%c", mod->desc, toupper(key->desc[0]));
00253                 ascii = toupper(key->ascii);
00254             } else if (mod->shiftable) {
00255                 snprintf(fullKeyId, 50, "S+%s%s", mod->id, key->hwId);
00256                 snprintf(fullKeyDesc, 100, "Shift+%s%s", mod->desc, key->desc);
00257             } else {
00258                 snprintf(fullKeyId, 50, "%s%s", mod->id, key->hwId);
00259                 snprintf(fullKeyDesc, 100, "%s%s", mod->desc, key->desc);
00260             }
00261 
00262             addHardwareInput(new HardwareInput(fullKeyId, KeyState(key->keycode, ascii, mod->flag), fullKeyDesc));
00263         }
00264     }
00265 }
00266 
00267 void HardwareInputSet::removeHardwareInput(const HardwareInput *input) {
00268     if (!input)
00269         return;
00270 
00271     List<const HardwareInput *>::iterator it;
00272 
00273     for (it = _inputs.begin(); it != _inputs.end(); ++it) {
00274         const HardwareInput *entry = (*it);
00275         bool match = false;
00276         if (entry->id == input->id)
00277             match = true;
00278         else if (input->type == entry->type) {
00279             if (input->type == kHardwareInputTypeGeneric && input->inputCode == entry->inputCode)
00280                 match = true;
00281             else if (input->type == kHardwareInputTypeKeyboard && input->key == entry->key)
00282                 match = true;
00283         }
00284         if (match) {
00285             debug(7, "Removing hardware input [%s] (%s) because it matches [%s] (%s)", entry->id.c_str(), entry->description.c_str(), input->id.c_str(), input->description.c_str());
00286             delete entry;
00287             _inputs.erase(it);
00288         }
00289     }
00290 }
00291 
00292 } //namespace Common
00293 
00294 #endif // #ifdef ENABLE_KEYMAPPER
00295 


Generated on Sat May 18 2019 05:01:04 for ResidualVM by doxygen 1.7.1
curved edge   curved edge