keycode.cpp 9.9 KB

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