keyboard.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #if defined(Hiro_Keyboard)
  2. namespace hiro {
  3. auto pKeyboard::poll() -> vector<bool> {
  4. if(Application::state().quit) return {};
  5. vector<bool> result;
  6. char state[256];
  7. #if defined(DISPLAY_XORG)
  8. XQueryKeymap(pApplication::state().display, state);
  9. #endif
  10. for(auto& code : settings.keycodes) {
  11. result.append(_pressed(state, code));
  12. }
  13. return result;
  14. }
  15. auto pKeyboard::pressed(uint code) -> bool {
  16. char state[256];
  17. #if defined(DISPLAY_XORG)
  18. XQueryKeymap(pApplication::state().display, state);
  19. #endif
  20. return _pressed(state, code);
  21. }
  22. auto pKeyboard::_pressed(const char* state, uint16_t code) -> bool {
  23. uint8_t lo = code >> 0;
  24. uint8_t hi = code >> 8;
  25. #if defined(DISPLAY_WINDOWS)
  26. if(lo && GetAsyncKeyState(lo) & 0x8000) return true;
  27. if(hi && GetAsyncKeyState(hi) & 0x8000) return true;
  28. #endif
  29. #if defined(DISPLAY_XORG)
  30. if(lo && state[lo >> 3] & (1 << (lo & 7))) return true;
  31. if(hi && state[hi >> 3] & (1 << (hi & 7))) return true;
  32. #endif
  33. return false;
  34. }
  35. auto pKeyboard::_translate(uint code) -> int {
  36. switch(code) {
  37. case GDK_KEY_Escape: return 0;
  38. case GDK_KEY_F1: return 0;
  39. case GDK_KEY_F2: return 0;
  40. case GDK_KEY_F3: return 0;
  41. case GDK_KEY_F4: return 0;
  42. case GDK_KEY_F5: return 0;
  43. case GDK_KEY_F6: return 0;
  44. case GDK_KEY_F7: return 0;
  45. case GDK_KEY_F8: return 0;
  46. case GDK_KEY_F9: return 0;
  47. case GDK_KEY_F10: return 0;
  48. case GDK_KEY_F11: return 0;
  49. case GDK_KEY_F12: return 0;
  50. case GDK_KEY_Print: return 0;
  51. case GDK_KEY_Scroll_Lock: return 0;
  52. case GDK_KEY_Pause: return 0;
  53. case GDK_KEY_Insert: return 0;
  54. case GDK_KEY_Delete: return 0;
  55. case GDK_KEY_Home: return 0;
  56. case GDK_KEY_End: return 0;
  57. case GDK_KEY_Prior: return 0;
  58. case GDK_KEY_Next: return 0;
  59. case GDK_KEY_Up: return 0;
  60. case GDK_KEY_Down: return 0;
  61. case GDK_KEY_Left: return 0;
  62. case GDK_KEY_Right: return 0;
  63. case GDK_KEY_grave: return '`';
  64. case GDK_KEY_1: return '1';
  65. case GDK_KEY_2: return '2';
  66. case GDK_KEY_3: return '3';
  67. case GDK_KEY_4: return '4';
  68. case GDK_KEY_5: return '5';
  69. case GDK_KEY_6: return '6';
  70. case GDK_KEY_7: return '7';
  71. case GDK_KEY_8: return '8';
  72. case GDK_KEY_9: return '9';
  73. case GDK_KEY_0: return '0';
  74. case GDK_KEY_minus: return '-';
  75. case GDK_KEY_equal: return '=';
  76. case GDK_KEY_BackSpace: return '\b';
  77. case GDK_KEY_asciitilde: return '~';
  78. case GDK_KEY_exclam: return '!';
  79. case GDK_KEY_at: return '@';
  80. case GDK_KEY_numbersign: return '#';
  81. case GDK_KEY_dollar: return '$';
  82. case GDK_KEY_percent: return '%';
  83. case GDK_KEY_asciicircum: return '^';
  84. case GDK_KEY_ampersand: return '&';
  85. case GDK_KEY_asterisk: return '*';
  86. case GDK_KEY_parenleft: return '(';
  87. case GDK_KEY_parenright: return ')';
  88. case GDK_KEY_underscore: return '_';
  89. case GDK_KEY_plus: return '+';
  90. case GDK_KEY_Tab: return '\t';
  91. case GDK_KEY_Caps_Lock: return 0;
  92. case GDK_KEY_Return: return '\n';
  93. case GDK_KEY_Shift_L: return 0;
  94. case GDK_KEY_Shift_R: return 0;
  95. case GDK_KEY_Control_L: return 0;
  96. case GDK_KEY_Control_R: return 0;
  97. case GDK_KEY_Alt_L: return 0;
  98. case GDK_KEY_Alt_R: return 0;
  99. case GDK_KEY_Super_L: return 0;
  100. case GDK_KEY_Super_R: return 0;
  101. case GDK_KEY_Menu: return 0;
  102. case GDK_KEY_space: return ' ';
  103. case GDK_KEY_bracketleft: return '[';
  104. case GDK_KEY_bracketright: return ']';
  105. case GDK_KEY_backslash: return '\\';
  106. case GDK_KEY_semicolon: return ';';
  107. case GDK_KEY_apostrophe: return '\'';
  108. case GDK_KEY_comma: return ',';
  109. case GDK_KEY_period: return '.';
  110. case GDK_KEY_slash: return '/';
  111. case GDK_KEY_braceleft: return '{';
  112. case GDK_KEY_braceright: return '}';
  113. case GDK_KEY_bar: return '|';
  114. case GDK_KEY_colon: return ':';
  115. case GDK_KEY_quotedbl: return '\"';
  116. case GDK_KEY_less: return '<';
  117. case GDK_KEY_greater: return '>';
  118. case GDK_KEY_question: return '?';
  119. case GDK_KEY_A: return 'A';
  120. case GDK_KEY_B: return 'B';
  121. case GDK_KEY_C: return 'C';
  122. case GDK_KEY_D: return 'D';
  123. case GDK_KEY_E: return 'E';
  124. case GDK_KEY_F: return 'F';
  125. case GDK_KEY_G: return 'G';
  126. case GDK_KEY_H: return 'H';
  127. case GDK_KEY_I: return 'I';
  128. case GDK_KEY_J: return 'J';
  129. case GDK_KEY_K: return 'K';
  130. case GDK_KEY_L: return 'L';
  131. case GDK_KEY_M: return 'M';
  132. case GDK_KEY_N: return 'N';
  133. case GDK_KEY_O: return 'O';
  134. case GDK_KEY_P: return 'P';
  135. case GDK_KEY_Q: return 'Q';
  136. case GDK_KEY_R: return 'R';
  137. case GDK_KEY_S: return 'S';
  138. case GDK_KEY_T: return 'T';
  139. case GDK_KEY_U: return 'U';
  140. case GDK_KEY_V: return 'V';
  141. case GDK_KEY_W: return 'W';
  142. case GDK_KEY_X: return 'X';
  143. case GDK_KEY_Y: return 'Y';
  144. case GDK_KEY_Z: return 'Z';
  145. case GDK_KEY_a: return 'a';
  146. case GDK_KEY_b: return 'b';
  147. case GDK_KEY_c: return 'c';
  148. case GDK_KEY_d: return 'd';
  149. case GDK_KEY_e: return 'e';
  150. case GDK_KEY_f: return 'f';
  151. case GDK_KEY_g: return 'g';
  152. case GDK_KEY_h: return 'h';
  153. case GDK_KEY_i: return 'i';
  154. case GDK_KEY_j: return 'j';
  155. case GDK_KEY_k: return 'k';
  156. case GDK_KEY_l: return 'l';
  157. case GDK_KEY_m: return 'm';
  158. case GDK_KEY_n: return 'n';
  159. case GDK_KEY_o: return 'o';
  160. case GDK_KEY_p: return 'p';
  161. case GDK_KEY_q: return 'q';
  162. case GDK_KEY_r: return 'r';
  163. case GDK_KEY_s: return 's';
  164. case GDK_KEY_t: return 't';
  165. case GDK_KEY_u: return 'u';
  166. case GDK_KEY_v: return 'v';
  167. case GDK_KEY_w: return 'w';
  168. case GDK_KEY_x: return 'x';
  169. case GDK_KEY_y: return 'y';
  170. case GDK_KEY_z: return 'z';
  171. case GDK_KEY_Num_Lock: return 0;
  172. case GDK_KEY_KP_Divide: return '/';
  173. case GDK_KEY_KP_Multiply: return '*';
  174. case GDK_KEY_KP_Subtract: return '-';
  175. case GDK_KEY_KP_Add: return '+';
  176. case GDK_KEY_KP_Enter: return '\n';
  177. case GDK_KEY_KP_Decimal: return '.';
  178. case GDK_KEY_KP_1: return '1';
  179. case GDK_KEY_KP_2: return '2';
  180. case GDK_KEY_KP_3: return '3';
  181. case GDK_KEY_KP_4: return '4';
  182. case GDK_KEY_KP_5: return '5';
  183. case GDK_KEY_KP_6: return '6';
  184. case GDK_KEY_KP_7: return '7';
  185. case GDK_KEY_KP_8: return '8';
  186. case GDK_KEY_KP_9: return '9';
  187. case GDK_KEY_KP_0: return '0';
  188. case GDK_KEY_KP_Home: return 0;
  189. case GDK_KEY_KP_End: return 0;
  190. case GDK_KEY_KP_Page_Up: return 0;
  191. case GDK_KEY_KP_Page_Down: return 0;
  192. case GDK_KEY_KP_Up: return 0;
  193. case GDK_KEY_KP_Down: return 0;
  194. case GDK_KEY_KP_Left: return 0;
  195. case GDK_KEY_KP_Right: return 0;
  196. case GDK_KEY_KP_Begin: return 0;
  197. case GDK_KEY_KP_Insert: return 0;
  198. case GDK_KEY_KP_Delete: return 0;
  199. }
  200. return 0;
  201. }
  202. auto pKeyboard::initialize() -> void {
  203. auto append = [](uint lo, uint hi = 0) {
  204. #if defined(DISPLAY_XORG)
  205. lo = lo ? (uint8_t)XKeysymToKeycode(pApplication::state().display, lo) : 0;
  206. hi = hi ? (uint8_t)XKeysymToKeycode(pApplication::state().display, hi) : 0;
  207. #endif
  208. settings.keycodes.append(lo | (hi << 8));
  209. };
  210. #define map(name, ...) if(key == name) { append(__VA_ARGS__); continue; }
  211. for(auto& key : Keyboard::keys) {
  212. #if defined(DISPLAY_WINDOWS)
  213. #include <hiro/platform/windows/keyboard.hpp>
  214. #endif
  215. #if defined(DISPLAY_XORG)
  216. #include <hiro/platform/xorg/keyboard.hpp>
  217. #endif
  218. //print("[hiro/gtk] warning: unhandled key: ", key, "\n");
  219. append(0);
  220. }
  221. #undef map
  222. }
  223. }
  224. #endif