1 /*
2  * Hedgewars, a free turn based strategy game
3  * Copyright (c) 2004-2019 Andrey Korotaev <unC0Rr@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include <QStringList>
19 #include "KeyMap.h"
20 #include "SDL.h"
21 
instance()22 KeyMap & KeyMap::instance()
23 {
24     static KeyMap instance;
25     instance.getKeyMap();
26     return instance;
27 }
28 
getKeyMap()29 bool KeyMap::getKeyMap()
30 {
31     if (keyMapGenerated)
32         return true;
33     QFile keyFile(":keys.csv");
34     if (!keyFile.open(QIODevice::ReadOnly))
35     {
36         qWarning("ERROR: keys.csv could not be opened!");
37         return false;
38     }
39     QString keyString = keyFile.readAll();
40     QStringList cells = QStringList() << QString("") << QString("");
41     QChar currChar;
42     bool isInQuote = false;
43     int cell = 0;
44     int charInCell = 0;
45     QString scancode = "";
46     QString keyname = "";
47     for(long long int i = 0; i < keyString.length(); i++)
48     {
49         currChar = keyString.at(i);
50         if (currChar == '\"') {
51             isInQuote = !isInQuote;
52         }
53         if (currChar == ',' && !isInQuote) {
54             cell++;
55             continue;
56         }
57         if (currChar == '\n') {
58             mapOfKeynames[(SDL_Scancode) scancode.toInt()] = keyname;
59             mapOfScancodes[keyname] = (SDL_Scancode) scancode.toInt();
60             if ((SDL_Scancode) scancode.toInt() == SDL_SCANCODE_UNKNOWN)
61                 continue;
62             cell = 0;
63             scancode = "";
64             keyname = "";
65             continue;
66         }
67         if (cell == 0 && currChar != '\"') {
68             scancode += currChar;
69         } else if (cell == 1 && currChar != '\"') {
70             keyname += currChar;
71         }
72         charInCell++;
73     }
74     keyMapGenerated = true;
75     keyFile.close();
76     return true;
77 }
78 
getScancodeFromKeyname(QString keyname)79 SDL_Scancode KeyMap::getScancodeFromKeyname(QString keyname)
80 {
81     if (mapOfScancodes.contains(keyname))
82         return mapOfScancodes[keyname];
83     else
84         return SDL_SCANCODE_UNKNOWN;
85 }
86 
getKeynameFromScancode(int scancode)87 QString KeyMap::getKeynameFromScancode(int scancode)
88 {
89     if (mapOfKeynames.contains((SDL_Scancode) scancode))
90         if ((SDL_Scancode) scancode == SDL_SCANCODE_UNKNOWN)
91             return QString("none");
92         else
93             return mapOfKeynames[(SDL_Scancode) scancode];
94     else
95         return QString("");
96 }
97 
getKeynameFromScancodeConverted(int scancode)98 QString KeyMap::getKeynameFromScancodeConverted(int scancode)
99 {
100     SDL_Keycode keycode = SDL_GetKeyFromScancode((SDL_Scancode) scancode);
101     return SDL_GetKeyName(keycode);
102 }
103 
104