InputMacOSX.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include <macosx/InputMacOSX.h>
  29. #include <RmlUi/Core/Context.h>
  30. #include <RmlUi/Core/Input.h>
  31. #include <RmlUi/Debugger.h>
  32. #include <Shell.h>
  33. // Defines for Carbon key modifiers.
  34. #define KEY_ALT 256
  35. #define KEY_SHIFT 512
  36. #define KEY_CAPS 1024
  37. #define KEY_OPTION 2048
  38. #define KEY_CTRL 4096
  39. static void InitialiseKeymap();
  40. static int GetKeyModifierState(EventRef event);
  41. static const int KEYMAP_SIZE = 256;
  42. static Rml::Core::Input::KeyIdentifier key_identifier_map[KEYMAP_SIZE];
  43. bool InputMacOSX::Initialise()
  44. {
  45. InitialiseKeymap();
  46. return true;
  47. }
  48. void InputMacOSX::Shutdown()
  49. {
  50. }
  51. OSStatus InputMacOSX::EventHandler(EventHandlerCallRef next_handler, EventRef event, void* p)
  52. {
  53. // Process all mouse and keyboard events
  54. switch (GetEventClass(event))
  55. {
  56. case kEventClassMouse:
  57. {
  58. switch (GetEventKind(event))
  59. {
  60. case kEventMouseDown:
  61. {
  62. EventMouseButton mouse_button;
  63. if (GetEventParameter(event, kEventParamMouseButton, typeMouseButton, nullptr, sizeof(EventMouseButton), nullptr, &mouse_button) == noErr)
  64. context->ProcessMouseButtonDown(mouse_button - 1, GetKeyModifierState(event));
  65. }
  66. break;
  67. case kEventMouseUp:
  68. {
  69. EventMouseButton mouse_button;
  70. if (GetEventParameter(event, kEventParamMouseButton, typeMouseButton, nullptr, sizeof(EventMouseButton), nullptr, &mouse_button) == noErr)
  71. context->ProcessMouseButtonUp(mouse_button - 1, GetKeyModifierState(event));
  72. }
  73. break;
  74. case kEventMouseWheelMoved:
  75. {
  76. EventMouseWheelAxis axis;
  77. SInt32 delta;
  78. if (GetEventParameter(event, kEventParamMouseWheelAxis, typeMouseWheelAxis, nullptr, sizeof(EventMouseWheelAxis), nullptr, &axis) == noErr &&
  79. GetEventParameter(event, kEventParamMouseWheelDelta, typeLongInteger, nullptr, sizeof(SInt32), nullptr, &delta) == noErr)
  80. {
  81. if (axis == kEventMouseWheelAxisY)
  82. context->ProcessMouseWheel(-delta, GetKeyModifierState(event));
  83. }
  84. }
  85. break;
  86. case kEventMouseMoved:
  87. case kEventMouseDragged:
  88. {
  89. HIPoint position;
  90. if (GetEventParameter(event, kEventParamWindowMouseLocation, typeHIPoint, nullptr, sizeof(HIPoint), nullptr, &position) == noErr)
  91. context->ProcessMouseMove(position.x, position.y - 22, GetKeyModifierState(event));
  92. }
  93. break;
  94. }
  95. }
  96. break;
  97. case kEventClassKeyboard:
  98. {
  99. switch (GetEventKind(event))
  100. {
  101. case kEventRawKeyDown:
  102. {
  103. UInt32 key_code;
  104. if (GetEventParameter(event, kEventParamKeyCode, typeUInt32, nullptr, sizeof(UInt32), nullptr, &key_code) == noErr)
  105. {
  106. Rml::Core::Input::KeyIdentifier key_identifier = key_identifier_map[key_code & 0xFF];
  107. int key_modifier_state = GetKeyModifierState(event);
  108. // Check for a shift-~ to toggle the debugger.
  109. if (key_identifier == Rml::Core::Input::KI_OEM_3 &&
  110. key_modifier_state & Rml::Core::Input::KM_SHIFT)
  111. {
  112. Rml::Debugger::SetVisible(!Rml::Debugger::IsVisible());
  113. break;
  114. }
  115. if (key_identifier != Rml::Core::Input::KI_UNKNOWN)
  116. context->ProcessKeyDown(key_identifier, key_modifier_state);
  117. Rml::Core::word character = GetCharacterCode(key_identifier, key_modifier_state);
  118. if (character > 0)
  119. context->ProcessTextInput(character);
  120. }
  121. }
  122. break;
  123. case kEventRawKeyUp:
  124. {
  125. UInt32 key_code;
  126. if (GetEventParameter(event, kEventParamKeyCode, typeUInt32, nullptr, sizeof(UInt32), nullptr, &key_code) == noErr)
  127. {
  128. Rml::Core::Input::KeyIdentifier key_identifier = key_identifier_map[key_code & 0xFF];
  129. int key_modifier_state = GetKeyModifierState(event);
  130. if (key_identifier != Rml::Core::Input::KI_UNKNOWN)
  131. context->ProcessKeyUp(key_identifier, key_modifier_state);
  132. }
  133. }
  134. break;
  135. }
  136. }
  137. break;
  138. }
  139. return CallNextEventHandler(next_handler, event);
  140. }
  141. static int GetKeyModifierState(EventRef event)
  142. {
  143. int key_modifier_state = 0;
  144. UInt32 carbon_key_modifier_state;
  145. if (GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, nullptr, sizeof(UInt32), nullptr, &carbon_key_modifier_state) == noErr)
  146. {
  147. if (carbon_key_modifier_state & KEY_ALT)
  148. key_modifier_state |= Rml::Core::Input::KM_ALT;
  149. if (carbon_key_modifier_state & KEY_SHIFT)
  150. key_modifier_state |= Rml::Core::Input::KM_SHIFT;
  151. if (carbon_key_modifier_state & KEY_CAPS)
  152. key_modifier_state |= Rml::Core::Input::KM_CAPSLOCK;
  153. if (carbon_key_modifier_state & KEY_OPTION)
  154. key_modifier_state |= Rml::Core::Input::KM_META;
  155. if (carbon_key_modifier_state & KEY_CTRL)
  156. key_modifier_state |= Rml::Core::Input::KM_CTRL;
  157. }
  158. return key_modifier_state;
  159. }
  160. static void InitialiseKeymap()
  161. {
  162. // Initialise the key map with default values.
  163. memset(key_identifier_map, sizeof(key_identifier_map), 0);
  164. key_identifier_map[0x00] = Rml::Core::Input::KI_A;
  165. key_identifier_map[0x01] = Rml::Core::Input::KI_S;
  166. key_identifier_map[0x02] = Rml::Core::Input::KI_D;
  167. key_identifier_map[0x03] = Rml::Core::Input::KI_F;
  168. key_identifier_map[0x04] = Rml::Core::Input::KI_H;
  169. key_identifier_map[0x05] = Rml::Core::Input::KI_G;
  170. key_identifier_map[0x06] = Rml::Core::Input::KI_Z;
  171. key_identifier_map[0x07] = Rml::Core::Input::KI_X;
  172. key_identifier_map[0x08] = Rml::Core::Input::KI_C;
  173. key_identifier_map[0x09] = Rml::Core::Input::KI_V;
  174. key_identifier_map[0x0B] = Rml::Core::Input::KI_B;
  175. key_identifier_map[0x0C] = Rml::Core::Input::KI_Q;
  176. key_identifier_map[0x0D] = Rml::Core::Input::KI_W;
  177. key_identifier_map[0x0E] = Rml::Core::Input::KI_E;
  178. key_identifier_map[0x0F] = Rml::Core::Input::KI_R;
  179. key_identifier_map[0x10] = Rml::Core::Input::KI_Y;
  180. key_identifier_map[0x11] = Rml::Core::Input::KI_T;
  181. key_identifier_map[0x12] = Rml::Core::Input::KI_1;
  182. key_identifier_map[0x13] = Rml::Core::Input::KI_2;
  183. key_identifier_map[0x14] = Rml::Core::Input::KI_3;
  184. key_identifier_map[0x15] = Rml::Core::Input::KI_4;
  185. key_identifier_map[0x16] = Rml::Core::Input::KI_6;
  186. key_identifier_map[0x17] = Rml::Core::Input::KI_5;
  187. key_identifier_map[0x18] = Rml::Core::Input::KI_OEM_PLUS;
  188. key_identifier_map[0x19] = Rml::Core::Input::KI_9;
  189. key_identifier_map[0x1A] = Rml::Core::Input::KI_7;
  190. key_identifier_map[0x1B] = Rml::Core::Input::KI_OEM_MINUS;
  191. key_identifier_map[0x1C] = Rml::Core::Input::KI_8;
  192. key_identifier_map[0x1D] = Rml::Core::Input::KI_0;
  193. key_identifier_map[0x1E] = Rml::Core::Input::KI_OEM_6;
  194. key_identifier_map[0x1F] = Rml::Core::Input::KI_O;
  195. key_identifier_map[0x20] = Rml::Core::Input::KI_U;
  196. key_identifier_map[0x21] = Rml::Core::Input::KI_OEM_4;
  197. key_identifier_map[0x22] = Rml::Core::Input::KI_I;
  198. key_identifier_map[0x23] = Rml::Core::Input::KI_P;
  199. key_identifier_map[0x24] = Rml::Core::Input::KI_RETURN;
  200. key_identifier_map[0x25] = Rml::Core::Input::KI_L;
  201. key_identifier_map[0x26] = Rml::Core::Input::KI_J;
  202. key_identifier_map[0x27] = Rml::Core::Input::KI_OEM_7;
  203. key_identifier_map[0x28] = Rml::Core::Input::KI_K;
  204. key_identifier_map[0x29] = Rml::Core::Input::KI_OEM_1;
  205. key_identifier_map[0x2A] = Rml::Core::Input::KI_OEM_5;
  206. key_identifier_map[0x2B] = Rml::Core::Input::KI_OEM_COMMA;
  207. key_identifier_map[0x2C] = Rml::Core::Input::KI_OEM_2;
  208. key_identifier_map[0x2D] = Rml::Core::Input::KI_N;
  209. key_identifier_map[0x2E] = Rml::Core::Input::KI_M;
  210. key_identifier_map[0x2F] = Rml::Core::Input::KI_OEM_PERIOD;
  211. key_identifier_map[0x30] = Rml::Core::Input::KI_TAB;
  212. key_identifier_map[0x31] = Rml::Core::Input::KI_SPACE;
  213. key_identifier_map[0x32] = Rml::Core::Input::KI_OEM_3;
  214. key_identifier_map[0x33] = Rml::Core::Input::KI_BACK;
  215. key_identifier_map[0x35] = Rml::Core::Input::KI_ESCAPE;
  216. key_identifier_map[0x37] = Rml::Core::Input::KI_LMETA;
  217. key_identifier_map[0x38] = Rml::Core::Input::KI_LSHIFT;
  218. key_identifier_map[0x39] = Rml::Core::Input::KI_CAPITAL;
  219. key_identifier_map[0x3A] = Rml::Core::Input::KI_LMENU;
  220. key_identifier_map[0x3B] = Rml::Core::Input::KI_LCONTROL;
  221. key_identifier_map[0x41] = Rml::Core::Input::KI_DECIMAL;
  222. key_identifier_map[0x43] = Rml::Core::Input::KI_MULTIPLY;
  223. key_identifier_map[0x45] = Rml::Core::Input::KI_ADD;
  224. key_identifier_map[0x4B] = Rml::Core::Input::KI_DIVIDE;
  225. key_identifier_map[0x4C] = Rml::Core::Input::KI_NUMPADENTER;
  226. key_identifier_map[0x4E] = Rml::Core::Input::KI_SUBTRACT;
  227. key_identifier_map[0x51] = Rml::Core::Input::KI_OEM_PLUS;
  228. key_identifier_map[0x52] = Rml::Core::Input::KI_NUMPAD0;
  229. key_identifier_map[0x53] = Rml::Core::Input::KI_NUMPAD1;
  230. key_identifier_map[0x54] = Rml::Core::Input::KI_NUMPAD2;
  231. key_identifier_map[0x55] = Rml::Core::Input::KI_NUMPAD3;
  232. key_identifier_map[0x56] = Rml::Core::Input::KI_NUMPAD4;
  233. key_identifier_map[0x57] = Rml::Core::Input::KI_NUMPAD5;
  234. key_identifier_map[0x58] = Rml::Core::Input::KI_NUMPAD6;
  235. key_identifier_map[0x59] = Rml::Core::Input::KI_NUMPAD7;
  236. key_identifier_map[0x5B] = Rml::Core::Input::KI_NUMPAD8;
  237. key_identifier_map[0x5C] = Rml::Core::Input::KI_NUMPAD9;
  238. key_identifier_map[0x60] = Rml::Core::Input::KI_F5;
  239. key_identifier_map[0x61] = Rml::Core::Input::KI_F6;
  240. key_identifier_map[0x62] = Rml::Core::Input::KI_F7;
  241. key_identifier_map[0x63] = Rml::Core::Input::KI_F3;
  242. key_identifier_map[0x64] = Rml::Core::Input::KI_F8;
  243. key_identifier_map[0x65] = Rml::Core::Input::KI_F9;
  244. key_identifier_map[0x67] = Rml::Core::Input::KI_F11;
  245. key_identifier_map[0x69] = Rml::Core::Input::KI_F13;
  246. key_identifier_map[0x6B] = Rml::Core::Input::KI_F14;
  247. key_identifier_map[0x6D] = Rml::Core::Input::KI_F10;
  248. key_identifier_map[0x6F] = Rml::Core::Input::KI_F12;
  249. key_identifier_map[0x71] = Rml::Core::Input::KI_F15;
  250. key_identifier_map[0x73] = Rml::Core::Input::KI_HOME;
  251. key_identifier_map[0x74] = Rml::Core::Input::KI_PRIOR;
  252. key_identifier_map[0x75] = Rml::Core::Input::KI_DELETE;
  253. key_identifier_map[0x76] = Rml::Core::Input::KI_F4;
  254. key_identifier_map[0x77] = Rml::Core::Input::KI_END;
  255. key_identifier_map[0x78] = Rml::Core::Input::KI_F2;
  256. key_identifier_map[0x79] = Rml::Core::Input::KI_NEXT;
  257. key_identifier_map[0x7A] = Rml::Core::Input::KI_F1;
  258. key_identifier_map[0x7B] = Rml::Core::Input::KI_LEFT;
  259. key_identifier_map[0x7C] = Rml::Core::Input::KI_RIGHT;
  260. key_identifier_map[0x7D] = Rml::Core::Input::KI_DOWN;
  261. key_identifier_map[0x7E] = Rml::Core::Input::KI_UP;
  262. }