File: KeyName.cpp

package info (click to toggle)
0ad 0.0.26-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,460 kB
  • sloc: cpp: 261,824; ansic: 198,392; javascript: 19,067; python: 14,557; sh: 7,629; perl: 4,072; xml: 849; makefile: 741; java: 533; ruby: 229; php: 190; pascal: 30; sql: 21; tcl: 4
file content (223 lines) | stat: -rw-r--r-- 7,273 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* Copyright (C) 2021 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

// Ooh, a file of keynames. Fun.

#include "precompiled.h"

#include "KeyName.h"

#include "lib/external_libraries/libsdl.h"
#include "ps/CStr.h"

#include <algorithm>
#include <unordered_map>
#include <vector>

// Some scancodes <-> names that SDL doesn't recognise.
// Those are tested first so they override SDL defaults (useful for UNIFIED keys).
static const std::unordered_map<int, std::vector<CStr>> scancodemap {{
	{ SDL_SCANCODE_DOWN, { "DownArrow" } },
	{ SDL_SCANCODE_UP, { "UpArrow" } },
	{ SDL_SCANCODE_LEFT, { "LeftArrow" } },
	{ SDL_SCANCODE_RIGHT, { "RightArrow" } },

	{ SDL_SCANCODE_EQUALS, { "Plus" } },
	{ SDL_SCANCODE_MINUS, { "Minus" } },

	{ SDL_SCANCODE_KP_ENTER, { "NumEnter" } },
	{ SDL_SCANCODE_KP_DIVIDE, { "NumDivide" } },
	{ SDL_SCANCODE_KP_MULTIPLY, { "NumMultiply" } },
	{ SDL_SCANCODE_KP_EQUALS, { "NumEquals" } },
	{ SDL_SCANCODE_KP_PERIOD, { "NumDecimal" } },
	{ SDL_SCANCODE_KP_PLUS, { "NumPlus" } },
	{ SDL_SCANCODE_KP_MINUS, { "NumMinus" } },
	{ SDL_SCANCODE_KP_0, { "Num0" } },
	{ SDL_SCANCODE_KP_1, { "Num1" } },
	{ SDL_SCANCODE_KP_2, { "Num2" } },
	{ SDL_SCANCODE_KP_3, { "Num3" } },
	{ SDL_SCANCODE_KP_4, { "Num4" } },
	{ SDL_SCANCODE_KP_5, { "Num5" } },
	{ SDL_SCANCODE_KP_6, { "Num6" } },
	{ SDL_SCANCODE_KP_7, { "Num7" } },
	{ SDL_SCANCODE_KP_8, { "Num8" } },
	{ SDL_SCANCODE_KP_9, { "Num9" } },

	{ SDL_SCANCODE_COMMA, { "Comma" } },
	{ SDL_SCANCODE_PERIOD, { "Period" } },
	{ SDL_SCANCODE_APOSTROPHE, { "Quote" } },
	{ SDL_SCANCODE_SEMICOLON, { "Semicolon" } },
	{ SDL_SCANCODE_GRAVE, { "Backquote" } },
	{ SDL_SCANCODE_LEFTBRACKET, { "LeftBracket" } },
	{ SDL_SCANCODE_RIGHTBRACKET, { "RightBracket" } },
	{ SDL_SCANCODE_BACKSLASH, { "Backslash" } },
	{ SDL_SCANCODE_SLASH, { "Slash" } },

	{ SDL_SCANCODE_RETURN, { "Enter" } },
	{ SDL_SCANCODE_ESCAPE, { "Esc" } },
	{ SDL_SCANCODE_PAUSE, { "Break" } },
	{ SDL_SCANCODE_DELETE, { "Del" } },

	{ MOUSE_LEFT, { "MouseLeft" } },
	{ MOUSE_RIGHT, { "MouseRight" } },
	{ MOUSE_MIDDLE, { "MouseMiddle" } },
	{ MOUSE_WHEELUP, { "WheelUp" } },
	{ MOUSE_WHEELDOWN, { "WheelDown" } },
	{ MOUSE_X1, { "WheelLeft", "MouseX1" } },
	{ MOUSE_X2, { "WheelRight", "MouseX2" } },

	{ UNIFIED_SHIFT, { "Shift", "Left Shift", "Right Shift" } },
	{ UNIFIED_CTRL, { "Ctrl", "Left Ctrl", "Right Ctrl" } },
	{ UNIFIED_ALT, { "Alt", "Left Alt", "Right Alt" } },
	{ UNIFIED_SUPER, { "Super", "Left Gui", "Right Gui" } },
}};

SDL_Scancode FindScancode(const CStr8& keyname)
{
	// Find (ignoring case) a corresponding scancode, if one exists.
	std::unordered_map<int, std::vector<CStr>>::const_iterator it =
		std::find_if(scancodemap.begin(), scancodemap.end(), [&keyname](const std::pair<int, std::vector<CStr>>& names) {
			return std::find_if(names.second.begin(), names.second.end(), [&keyname](const CStr& t) {
				return t.LowerCase() == keyname.LowerCase();
			})!= names.second.end();
		});

	if (it != scancodemap.end())
		return static_cast<SDL_Scancode>(it->first);

	SDL_Scancode code = SDL_GetScancodeFromName(keyname.c_str());
	if (code != SDL_SCANCODE_UNKNOWN)
		return code;

	// Parse SYM_XX codes, see below.
	if (keyname.size() > 4 && keyname.Left(4) == "SYM_")
		return static_cast<SDL_Scancode>(CStr(keyname.substr(4)).ToInt());

	return SDL_SCANCODE_UNKNOWN;
}

CStr FindScancodeName(SDL_Scancode scancode)
{
	if (scancodemap.find(scancode) != scancodemap.end())
		return scancodemap.at(scancode).front();

	const char* name = SDL_GetScancodeName(scancode);
	// Some scancodes have no name, but we must have something to save/load/recognize it, so parse it as SYM_XX
	if (strlen(name) == 0)
		return CStr("SYM_") + CStr::FromInt(scancode);
	return name;
}

// Rename some SDL key names (!scancodes) for easier readability.
// NB: this does not intend to be exhaustive, merely cover the usual suspects.
static const std::unordered_map<SDL_Keycode, CStr> keyNames {{
	{ SDLK_COMMA, "Comma" },
	{ SDLK_SEMICOLON, "Semicolon" },
	{ SDLK_COLON, "Colon" },
	{ SDLK_PERIOD, "Period" },
	{ SDLK_EQUALS, "Equals" },
	{ SDLK_PLUS, "Plus" },
	{ SDLK_MINUS, "Minus" },

	{ SDLK_QUOTE, "SingleQuote" },
	{ SDLK_QUOTEDBL, "DoubleQuote" },
	{ SDLK_BACKQUOTE, "BackQuote" },

	{ SDLK_LEFTPAREN, { "LeftParen" } },

	{ SDLK_LEFTBRACKET, { "LeftBracket" } },
	{ SDLK_RIGHTBRACKET, { "RightBracket" } },
	{ SDLK_BACKSLASH, { "Backslash" } },
	{ SDLK_SLASH, { "Slash" } },

	{ SDLK_KP_ENTER, "NumEnter" },
	{ SDLK_KP_DIVIDE, "NumDivide" },
	{ SDLK_KP_MULTIPLY, "NumMultiply" },
	{ SDLK_KP_EQUALS, "NumEquals" },
	{ SDLK_KP_PERIOD, "NumDecimal" },
	{ SDLK_KP_PLUS, "NumPlus" },
	{ SDLK_KP_MINUS, "NumMinus" },
	{ SDLK_KP_0, "Num0" },
	{ SDLK_KP_1, "Num1" },
	{ SDLK_KP_2, "Num2" },
	{ SDLK_KP_3, "Num3" },
	{ SDLK_KP_4, "Num4" },
	{ SDLK_KP_5, "Num5" },
	{ SDLK_KP_6, "Num6" },
	{ SDLK_KP_7, "Num7" },
	{ SDLK_KP_8, "Num8" },
	{ SDLK_KP_9, "Num9" },

	{ SDLK_UP, "\xe2\x86\x91" },
	{ SDLK_DOWN, "\xe2\x86\x93" },
	{ SDLK_LEFT, "\xe2\x86\x90" },
	{ SDLK_RIGHT, "\xe2\x86\x92" },
}};

CStr FindKeyName(SDL_Scancode scancode)
{
	// Mouse and unified modifiers are harcoded.
	if (static_cast<int>(scancode) == UNIFIED_SHIFT)
		return "Shift";
	else if (static_cast<int>(scancode) == UNIFIED_ALT)
		return "Alt";
	else if (static_cast<int>(scancode) == UNIFIED_CTRL)
		return "Ctrl";
	else if (static_cast<int>(scancode) == UNIFIED_SUPER)
		return "Super";
	else if (static_cast<int>(scancode) == MOUSE_LEFT)
		return "MouseLeft";
	else if (static_cast<int>(scancode) == MOUSE_RIGHT)
		return "MouseRight";
	else if (static_cast<int>(scancode) == MOUSE_MIDDLE)
		return "MouseMiddle";
	else if (static_cast<int>(scancode) == MOUSE_WHEELUP)
		return "WheelUp";
	else if (static_cast<int>(scancode) == MOUSE_WHEELDOWN)
		return "WheelDown";
	else if (static_cast<int>(scancode) == MOUSE_X1)
		return "WheelLeft";
	else if (static_cast<int>(scancode) == MOUSE_X2)
		return "WheelRight";

	SDL_Keycode code = SDL_GetKeyFromScancode(scancode);

	if (keyNames.find(code) != keyNames.end())
		return keyNames.at(code);

	if (code != SDLK_UNKNOWN)
	{
		const char* keyName = SDL_GetKeyName(code);
		if (strlen(keyName) != 0)
			return keyName;
	}

	// Try the scancode name.
	const char* name = SDL_GetScancodeName(scancode);

	// xxtreme hack: some SDLKeycodes map to chars, and we need to escape [ and \ .
	if (keyNames.find(static_cast<SDL_Keycode>(*name)) != keyNames.end())
		return keyNames.at(static_cast<SDL_Keycode>(*name));

	if (strlen(name) != 0)
		return name;

	// Else, show something regardless, so the player knows it's at least recognized.
	return CStr("SYM_") + CStr::FromInt(scancode);
}