keycode.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "keycode.h"
  17. #include "settings.h"
  18. #include "log.h"
  19. #include "debug.h"
  20. #include "util/hex.h"
  21. #include "util/string.h"
  22. #include "util/basic_macros.h"
  23. struct table_key {
  24. const char *Name;
  25. irr::EKEY_CODE Key;
  26. wchar_t Char; // L'\0' means no character assigned
  27. const char *LangName; // NULL means it doesn't have a human description
  28. };
  29. #define DEFINEKEY1(x, lang) /* Irrlicht key without character */ \
  30. { #x, irr::x, L'\0', lang },
  31. #define DEFINEKEY2(x, ch, lang) /* Irrlicht key with character */ \
  32. { #x, irr::x, ch, lang },
  33. #define DEFINEKEY3(ch) /* single Irrlicht key (e.g. KEY_KEY_X) */ \
  34. { "KEY_KEY_" TOSTRING(ch), irr::KEY_KEY_ ## ch, (wchar_t) *TOSTRING(ch), TOSTRING(ch) },
  35. #define DEFINEKEY4(ch) /* single Irrlicht function key (e.g. KEY_F3) */ \
  36. { "KEY_F" TOSTRING(ch), irr::KEY_F ## ch, L'\0', "F" TOSTRING(ch) },
  37. #define DEFINEKEY5(ch) /* key without Irrlicht keycode */ \
  38. { ch, irr::KEY_KEY_CODES_COUNT, (wchar_t) *ch, ch },
  39. #define N_(text) text
  40. static const struct table_key table[] = {
  41. // Keys that can be reliably mapped between Char and Key
  42. DEFINEKEY3(0)
  43. DEFINEKEY3(1)
  44. DEFINEKEY3(2)
  45. DEFINEKEY3(3)
  46. DEFINEKEY3(4)
  47. DEFINEKEY3(5)
  48. DEFINEKEY3(6)
  49. DEFINEKEY3(7)
  50. DEFINEKEY3(8)
  51. DEFINEKEY3(9)
  52. DEFINEKEY3(A)
  53. DEFINEKEY3(B)
  54. DEFINEKEY3(C)
  55. DEFINEKEY3(D)
  56. DEFINEKEY3(E)
  57. DEFINEKEY3(F)
  58. DEFINEKEY3(G)
  59. DEFINEKEY3(H)
  60. DEFINEKEY3(I)
  61. DEFINEKEY3(J)
  62. DEFINEKEY3(K)
  63. DEFINEKEY3(L)
  64. DEFINEKEY3(M)
  65. DEFINEKEY3(N)
  66. DEFINEKEY3(O)
  67. DEFINEKEY3(P)
  68. DEFINEKEY3(Q)
  69. DEFINEKEY3(R)
  70. DEFINEKEY3(S)
  71. DEFINEKEY3(T)
  72. DEFINEKEY3(U)
  73. DEFINEKEY3(V)
  74. DEFINEKEY3(W)
  75. DEFINEKEY3(X)
  76. DEFINEKEY3(Y)
  77. DEFINEKEY3(Z)
  78. DEFINEKEY2(KEY_PLUS, L'+', "+")
  79. DEFINEKEY2(KEY_COMMA, L',', ",")
  80. DEFINEKEY2(KEY_MINUS, L'-', "-")
  81. DEFINEKEY2(KEY_PERIOD, L'.', ".")
  82. // Keys without a Char
  83. DEFINEKEY1(KEY_LBUTTON, N_("Left Button"))
  84. DEFINEKEY1(KEY_RBUTTON, N_("Right Button"))
  85. //~ Usually paired with the Pause key
  86. DEFINEKEY1(KEY_CANCEL, N_("Break Key"))
  87. DEFINEKEY1(KEY_MBUTTON, N_("Middle Button"))
  88. DEFINEKEY1(KEY_XBUTTON1, N_("X Button 1"))
  89. DEFINEKEY1(KEY_XBUTTON2, N_("X Button 2"))
  90. DEFINEKEY1(KEY_BACK, N_("Backspace"))
  91. DEFINEKEY1(KEY_TAB, N_("Tab"))
  92. DEFINEKEY1(KEY_CLEAR, N_("Clear Key"))
  93. DEFINEKEY1(KEY_RETURN, N_("Return Key"))
  94. DEFINEKEY1(KEY_SHIFT, N_("Shift Key"))
  95. DEFINEKEY1(KEY_CONTROL, N_("Control Key"))
  96. //~ Key name, common on Windows keyboards
  97. DEFINEKEY1(KEY_MENU, N_("Menu Key"))
  98. //~ Usually paired with the Break key
  99. DEFINEKEY1(KEY_PAUSE, N_("Pause Key"))
  100. DEFINEKEY1(KEY_CAPITAL, N_("Caps Lock"))
  101. DEFINEKEY1(KEY_SPACE, N_("Space"))
  102. DEFINEKEY1(KEY_PRIOR, N_("Page Up"))
  103. DEFINEKEY1(KEY_NEXT, N_("Page Down"))
  104. DEFINEKEY1(KEY_END, N_("End"))
  105. DEFINEKEY1(KEY_HOME, N_("Home"))
  106. DEFINEKEY1(KEY_LEFT, N_("Left Arrow"))
  107. DEFINEKEY1(KEY_UP, N_("Up Arrow"))
  108. DEFINEKEY1(KEY_RIGHT, N_("Right Arrow"))
  109. DEFINEKEY1(KEY_DOWN, N_("Down Arrow"))
  110. //~ Key name
  111. DEFINEKEY1(KEY_SELECT, N_("Select"))
  112. //~ "Print screen" key
  113. DEFINEKEY1(KEY_PRINT, N_("Print"))
  114. DEFINEKEY1(KEY_EXECUT, N_("Execute"))
  115. DEFINEKEY1(KEY_SNAPSHOT, N_("Snapshot"))
  116. DEFINEKEY1(KEY_INSERT, N_("Insert"))
  117. DEFINEKEY1(KEY_DELETE, N_("Delete Key"))
  118. DEFINEKEY1(KEY_HELP, N_("Help"))
  119. DEFINEKEY1(KEY_LWIN, N_("Left Windows"))
  120. DEFINEKEY1(KEY_RWIN, N_("Right Windows"))
  121. DEFINEKEY1(KEY_NUMPAD0, N_("Numpad 0")) // These are not assigned to a char
  122. DEFINEKEY1(KEY_NUMPAD1, N_("Numpad 1")) // to prevent interference with KEY_KEY_[0-9].
  123. DEFINEKEY1(KEY_NUMPAD2, N_("Numpad 2"))
  124. DEFINEKEY1(KEY_NUMPAD3, N_("Numpad 3"))
  125. DEFINEKEY1(KEY_NUMPAD4, N_("Numpad 4"))
  126. DEFINEKEY1(KEY_NUMPAD5, N_("Numpad 5"))
  127. DEFINEKEY1(KEY_NUMPAD6, N_("Numpad 6"))
  128. DEFINEKEY1(KEY_NUMPAD7, N_("Numpad 7"))
  129. DEFINEKEY1(KEY_NUMPAD8, N_("Numpad 8"))
  130. DEFINEKEY1(KEY_NUMPAD9, N_("Numpad 9"))
  131. DEFINEKEY1(KEY_MULTIPLY, N_("Numpad *"))
  132. DEFINEKEY1(KEY_ADD, N_("Numpad +"))
  133. DEFINEKEY1(KEY_SEPARATOR, N_("Numpad ."))
  134. DEFINEKEY1(KEY_SUBTRACT, N_("Numpad -"))
  135. DEFINEKEY1(KEY_DECIMAL, NULL)
  136. DEFINEKEY1(KEY_DIVIDE, N_("Numpad /"))
  137. DEFINEKEY4(1)
  138. DEFINEKEY4(2)
  139. DEFINEKEY4(3)
  140. DEFINEKEY4(4)
  141. DEFINEKEY4(5)
  142. DEFINEKEY4(6)
  143. DEFINEKEY4(7)
  144. DEFINEKEY4(8)
  145. DEFINEKEY4(9)
  146. DEFINEKEY4(10)
  147. DEFINEKEY4(11)
  148. DEFINEKEY4(12)
  149. DEFINEKEY4(13)
  150. DEFINEKEY4(14)
  151. DEFINEKEY4(15)
  152. DEFINEKEY4(16)
  153. DEFINEKEY4(17)
  154. DEFINEKEY4(18)
  155. DEFINEKEY4(19)
  156. DEFINEKEY4(20)
  157. DEFINEKEY4(21)
  158. DEFINEKEY4(22)
  159. DEFINEKEY4(23)
  160. DEFINEKEY4(24)
  161. DEFINEKEY1(KEY_NUMLOCK, N_("Num Lock"))
  162. DEFINEKEY1(KEY_SCROLL, N_("Scroll Lock"))
  163. DEFINEKEY1(KEY_LSHIFT, N_("Left Shift"))
  164. DEFINEKEY1(KEY_RSHIFT, N_("Right Shift"))
  165. DEFINEKEY1(KEY_LCONTROL, N_("Left Control"))
  166. DEFINEKEY1(KEY_RCONTROL, N_("Right Control"))
  167. DEFINEKEY1(KEY_LMENU, N_("Left Menu"))
  168. DEFINEKEY1(KEY_RMENU, N_("Right Menu"))
  169. // Rare/weird keys
  170. DEFINEKEY1(KEY_KANA, "Kana")
  171. DEFINEKEY1(KEY_HANGUEL, "Hangul")
  172. DEFINEKEY1(KEY_HANGUL, "Hangul")
  173. DEFINEKEY1(KEY_JUNJA, "Junja")
  174. DEFINEKEY1(KEY_FINAL, "Final")
  175. DEFINEKEY1(KEY_KANJI, "Kanji")
  176. DEFINEKEY1(KEY_HANJA, "Hanja")
  177. DEFINEKEY1(KEY_ESCAPE, N_("IME Escape"))
  178. DEFINEKEY1(KEY_CONVERT, N_("IME Convert"))
  179. DEFINEKEY1(KEY_NONCONVERT, N_("IME Nonconvert"))
  180. DEFINEKEY1(KEY_ACCEPT, N_("IME Accept"))
  181. DEFINEKEY1(KEY_MODECHANGE, N_("IME Mode Change"))
  182. DEFINEKEY1(KEY_APPS, N_("Apps"))
  183. DEFINEKEY1(KEY_SLEEP, N_("Sleep"))
  184. DEFINEKEY1(KEY_OEM_1, "OEM 1") // KEY_OEM_[0-9] and KEY_OEM_102 are assigned to multiple
  185. DEFINEKEY1(KEY_OEM_2, "OEM 2") // different chars (on different platforms too) and thus w/o char
  186. DEFINEKEY1(KEY_OEM_3, "OEM 3")
  187. DEFINEKEY1(KEY_OEM_4, "OEM 4")
  188. DEFINEKEY1(KEY_OEM_5, "OEM 5")
  189. DEFINEKEY1(KEY_OEM_6, "OEM 6")
  190. DEFINEKEY1(KEY_OEM_7, "OEM 7")
  191. DEFINEKEY1(KEY_OEM_8, "OEM 8")
  192. DEFINEKEY1(KEY_OEM_AX, "OEM AX")
  193. DEFINEKEY1(KEY_OEM_102, "OEM 102")
  194. DEFINEKEY1(KEY_ATTN, "Attn")
  195. DEFINEKEY1(KEY_CRSEL, "CrSel")
  196. DEFINEKEY1(KEY_EXSEL, "ExSel")
  197. DEFINEKEY1(KEY_EREOF, N_("Erase EOF"))
  198. DEFINEKEY1(KEY_PLAY, N_("Play"))
  199. DEFINEKEY1(KEY_ZOOM, N_("Zoom Key"))
  200. DEFINEKEY1(KEY_PA1, "PA1")
  201. DEFINEKEY1(KEY_OEM_CLEAR, N_("OEM Clear"))
  202. // Keys without Irrlicht keycode
  203. DEFINEKEY5("!")
  204. DEFINEKEY5("\"")
  205. DEFINEKEY5("#")
  206. DEFINEKEY5("$")
  207. DEFINEKEY5("%")
  208. DEFINEKEY5("&")
  209. DEFINEKEY5("'")
  210. DEFINEKEY5("(")
  211. DEFINEKEY5(")")
  212. DEFINEKEY5("*")
  213. DEFINEKEY5("/")
  214. DEFINEKEY5(":")
  215. DEFINEKEY5(";")
  216. DEFINEKEY5("<")
  217. DEFINEKEY5("=")
  218. DEFINEKEY5(">")
  219. DEFINEKEY5("?")
  220. DEFINEKEY5("@")
  221. DEFINEKEY5("[")
  222. DEFINEKEY5("\\")
  223. DEFINEKEY5("]")
  224. DEFINEKEY5("^")
  225. DEFINEKEY5("_")
  226. };
  227. #undef N_
  228. struct table_key lookup_keyname(const char *name)
  229. {
  230. for (const auto &table_key : table) {
  231. if (strcmp(table_key.Name, name) == 0)
  232. return table_key;
  233. }
  234. throw UnknownKeycode(name);
  235. }
  236. struct table_key lookup_keykey(irr::EKEY_CODE key)
  237. {
  238. for (const auto &table_key : table) {
  239. if (table_key.Key == key)
  240. return table_key;
  241. }
  242. std::ostringstream os;
  243. os << "<Keycode " << (int) key << ">";
  244. throw UnknownKeycode(os.str().c_str());
  245. }
  246. struct table_key lookup_keychar(wchar_t Char)
  247. {
  248. for (const auto &table_key : table) {
  249. if (table_key.Char == Char)
  250. return table_key;
  251. }
  252. std::ostringstream os;
  253. os << "<Char " << hex_encode((char*) &Char, sizeof(wchar_t)) << ">";
  254. throw UnknownKeycode(os.str().c_str());
  255. }
  256. KeyPress::KeyPress(const char *name)
  257. {
  258. if (strlen(name) == 0) {
  259. Key = irr::KEY_KEY_CODES_COUNT;
  260. Char = L'\0';
  261. m_name = "";
  262. return;
  263. }
  264. if (strlen(name) <= 4) {
  265. // Lookup by resulting character
  266. int chars_read = mbtowc(&Char, name, 1);
  267. FATAL_ERROR_IF(chars_read != 1, "Unexpected multibyte character");
  268. try {
  269. struct table_key k = lookup_keychar(Char);
  270. m_name = k.Name;
  271. Key = k.Key;
  272. return;
  273. } catch (UnknownKeycode &e) {};
  274. } else {
  275. // Lookup by name
  276. m_name = name;
  277. try {
  278. struct table_key k = lookup_keyname(name);
  279. Key = k.Key;
  280. Char = k.Char;
  281. return;
  282. } catch (UnknownKeycode &e) {};
  283. }
  284. // It's not a known key, complain and try to do something
  285. Key = irr::KEY_KEY_CODES_COUNT;
  286. int chars_read = mbtowc(&Char, name, 1);
  287. FATAL_ERROR_IF(chars_read != 1, "Unexpected multibyte character");
  288. m_name = "";
  289. warningstream << "KeyPress: Unknown key '" << name
  290. << "', falling back to first char." << std::endl;
  291. }
  292. KeyPress::KeyPress(const irr::SEvent::SKeyInput &in, bool prefer_character)
  293. {
  294. if (prefer_character)
  295. Key = irr::KEY_KEY_CODES_COUNT;
  296. else
  297. Key = in.Key;
  298. Char = in.Char;
  299. try {
  300. if (valid_kcode(Key))
  301. m_name = lookup_keykey(Key).Name;
  302. else
  303. m_name = lookup_keychar(Char).Name;
  304. } catch (UnknownKeycode &e) {
  305. m_name.clear();
  306. };
  307. }
  308. const char *KeyPress::sym() const
  309. {
  310. return m_name.c_str();
  311. }
  312. const char *KeyPress::name() const
  313. {
  314. if (m_name.empty())
  315. return "";
  316. const char *ret;
  317. if (valid_kcode(Key))
  318. ret = lookup_keykey(Key).LangName;
  319. else
  320. ret = lookup_keychar(Char).LangName;
  321. return ret ? ret : "<Unnamed key>";
  322. }
  323. const KeyPress EscapeKey("KEY_ESCAPE");
  324. const KeyPress CancelKey("KEY_CANCEL");
  325. /*
  326. Key config
  327. */
  328. // A simple cache for quicker lookup
  329. std::unordered_map<std::string, KeyPress> g_key_setting_cache;
  330. KeyPress getKeySetting(const char *settingname)
  331. {
  332. std::unordered_map<std::string, KeyPress>::iterator n;
  333. n = g_key_setting_cache.find(settingname);
  334. if (n != g_key_setting_cache.end())
  335. return n->second;
  336. KeyPress k(g_settings->get(settingname).c_str());
  337. g_key_setting_cache[settingname] = k;
  338. return k;
  339. }
  340. void clearKeyCache()
  341. {
  342. g_key_setting_cache.clear();
  343. }
  344. irr::EKEY_CODE keyname_to_keycode(const char *name)
  345. {
  346. return lookup_keyname(name).Key;
  347. }