InputMacOSX.cpp 11 KB

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