event.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. /// @file
  23. /// Library-wide input events
  24. ///
  25. /// All external events are converted into system events, which are defined
  26. /// in this file.
  27. ///
  28. #ifndef _EVENT_H_
  29. #define _EVENT_H_
  30. #include "platform/types.h"
  31. #include "platform/input/IInputDevice.h"
  32. #include "core/util/journal/journaledSignal.h"
  33. #include "core/util/tSingleton.h"
  34. #include "core/util/tDictionary.h"
  35. #include "core/tSimpleHashTable.h"
  36. #define AddInputVirtualMap( description, type, code ) \
  37. INPUTMGR->addVirtualMap( #description, type, code );
  38. /// @defgroup input_constants Input system constants
  39. /// @{
  40. /// Input event constants:
  41. typedef U32 InputObjectInstances;
  42. enum InputObjectInstancesEnum
  43. {
  44. KEY_NULL = 0x000, ///< Invalid KeyCode
  45. KEY_BACKSPACE = 0x001,
  46. KEY_TAB = 0x002,
  47. KEY_RETURN = 0x003,
  48. KEY_CONTROL = 0x004,
  49. KEY_ALT = 0x005,
  50. KEY_SHIFT = 0x006,
  51. KEY_PAUSE = 0x007,
  52. KEY_CAPSLOCK = 0x008,
  53. KEY_ESCAPE = 0x009,
  54. KEY_SPACE = 0x00a,
  55. KEY_PAGE_DOWN = 0x00b,
  56. KEY_PAGE_UP = 0x00c,
  57. KEY_END = 0x00d,
  58. KEY_HOME = 0x00e,
  59. KEY_LEFT = 0x00f,
  60. KEY_UP = 0x010,
  61. KEY_RIGHT = 0x011,
  62. KEY_DOWN = 0x012,
  63. KEY_PRINT = 0x013,
  64. KEY_INSERT = 0x014,
  65. KEY_DELETE = 0x015,
  66. KEY_HELP = 0x016,
  67. KEY_0 = 0x017,
  68. KEY_1 = 0x018,
  69. KEY_2 = 0x019,
  70. KEY_3 = 0x01a,
  71. KEY_4 = 0x01b,
  72. KEY_5 = 0x01c,
  73. KEY_6 = 0x01d,
  74. KEY_7 = 0x01e,
  75. KEY_8 = 0x01f,
  76. KEY_9 = 0x020,
  77. KEY_A = 0x021,
  78. KEY_B = 0x022,
  79. KEY_C = 0x023,
  80. KEY_D = 0x024,
  81. KEY_E = 0x025,
  82. KEY_F = 0x026,
  83. KEY_G = 0x027,
  84. KEY_H = 0x028,
  85. KEY_I = 0x029,
  86. KEY_J = 0x02a,
  87. KEY_K = 0x02b,
  88. KEY_L = 0x02c,
  89. KEY_M = 0x02d,
  90. KEY_N = 0x02e,
  91. KEY_O = 0x02f,
  92. KEY_P = 0x030,
  93. KEY_Q = 0x031,
  94. KEY_R = 0x032,
  95. KEY_S = 0x033,
  96. KEY_T = 0x034,
  97. KEY_U = 0x035,
  98. KEY_V = 0x036,
  99. KEY_W = 0x037,
  100. KEY_X = 0x038,
  101. KEY_Y = 0x039,
  102. KEY_Z = 0x03a,
  103. KEY_TILDE = 0x03b,
  104. KEY_MINUS = 0x03c,
  105. KEY_EQUALS = 0x03d,
  106. KEY_LBRACKET = 0x03e,
  107. KEY_RBRACKET = 0x03f,
  108. KEY_BACKSLASH = 0x040,
  109. KEY_SEMICOLON = 0x041,
  110. KEY_APOSTROPHE = 0x042,
  111. KEY_COMMA = 0x043,
  112. KEY_PERIOD = 0x044,
  113. KEY_SLASH = 0x045,
  114. KEY_NUMPAD0 = 0x046,
  115. KEY_NUMPAD1 = 0x047,
  116. KEY_NUMPAD2 = 0x048,
  117. KEY_NUMPAD3 = 0x049,
  118. KEY_NUMPAD4 = 0x04a,
  119. KEY_NUMPAD5 = 0x04b,
  120. KEY_NUMPAD6 = 0x04c,
  121. KEY_NUMPAD7 = 0x04d,
  122. KEY_NUMPAD8 = 0x04e,
  123. KEY_NUMPAD9 = 0x04f,
  124. KEY_MULTIPLY = 0x050,
  125. KEY_ADD = 0x051,
  126. KEY_SEPARATOR = 0x052,
  127. KEY_SUBTRACT = 0x053,
  128. KEY_DECIMAL = 0x054,
  129. KEY_DIVIDE = 0x055,
  130. KEY_NUMPADENTER = 0x056,
  131. KEY_F1 = 0x057,
  132. KEY_F2 = 0x058,
  133. KEY_F3 = 0x059,
  134. KEY_F4 = 0x05a,
  135. KEY_F5 = 0x05b,
  136. KEY_F6 = 0x05c,
  137. KEY_F7 = 0x05d,
  138. KEY_F8 = 0x05e,
  139. KEY_F9 = 0x05f,
  140. KEY_F10 = 0x060,
  141. KEY_F11 = 0x061,
  142. KEY_F12 = 0x062,
  143. KEY_F13 = 0x063,
  144. KEY_F14 = 0x064,
  145. KEY_F15 = 0x065,
  146. KEY_F16 = 0x066,
  147. KEY_F17 = 0x067,
  148. KEY_F18 = 0x068,
  149. KEY_F19 = 0x069,
  150. KEY_F20 = 0x06a,
  151. KEY_F21 = 0x06b,
  152. KEY_F22 = 0x06c,
  153. KEY_F23 = 0x06d,
  154. KEY_F24 = 0x06e,
  155. KEY_NUMLOCK = 0x06f,
  156. KEY_SCROLLLOCK = 0x070,
  157. KEY_LCONTROL = 0x071,
  158. KEY_RCONTROL = 0x072,
  159. KEY_LALT = 0x073,
  160. KEY_RALT = 0x074,
  161. KEY_LSHIFT = 0x075,
  162. KEY_RSHIFT = 0x076,
  163. KEY_WIN_LWINDOW = 0x077,
  164. KEY_WIN_RWINDOW = 0x078,
  165. KEY_WIN_APPS = 0x079,
  166. KEY_OEM_102 = 0x080,
  167. KEY_MAC_OPT = 0x090,
  168. KEY_MAC_LOPT = 0x091,
  169. KEY_MAC_ROPT = 0x092,
  170. KEY_BUTTON0 = 0x0100,
  171. KEY_BUTTON1 = 0x0101,
  172. KEY_BUTTON2 = 0x0102,
  173. KEY_BUTTON3 = 0x0103,
  174. KEY_BUTTON4 = 0x0104,
  175. KEY_BUTTON5 = 0x0105,
  176. KEY_BUTTON6 = 0x0106,
  177. KEY_BUTTON7 = 0x0107,
  178. KEY_BUTTON8 = 0x0108,
  179. KEY_BUTTON9 = 0x0109,
  180. KEY_BUTTON10 = 0x010A,
  181. KEY_BUTTON11 = 0x010B,
  182. KEY_BUTTON12 = 0x010C,
  183. KEY_BUTTON13 = 0x010D,
  184. KEY_BUTTON14 = 0x010E,
  185. KEY_BUTTON15 = 0x010F,
  186. KEY_BUTTON16 = 0x0110,
  187. KEY_BUTTON17 = 0x0111,
  188. KEY_BUTTON18 = 0x0112,
  189. KEY_BUTTON19 = 0x0113,
  190. KEY_BUTTON20 = 0x0114,
  191. KEY_BUTTON21 = 0x0115,
  192. KEY_BUTTON22 = 0x0116,
  193. KEY_BUTTON23 = 0x0117,
  194. KEY_BUTTON24 = 0x0118,
  195. KEY_BUTTON25 = 0x0119,
  196. KEY_BUTTON26 = 0x011A,
  197. KEY_BUTTON27 = 0x011B,
  198. KEY_BUTTON28 = 0x011C,
  199. KEY_BUTTON29 = 0x011D,
  200. KEY_BUTTON30 = 0x011E,
  201. KEY_BUTTON31 = 0x011F,
  202. KEY_BUTTON32 = 0x0120,
  203. KEY_BUTTON33 = 0x0121,
  204. KEY_BUTTON34 = 0x0122,
  205. KEY_BUTTON35 = 0x0123,
  206. KEY_BUTTON36 = 0x0124,
  207. KEY_BUTTON37 = 0x0125,
  208. KEY_BUTTON38 = 0x0126,
  209. KEY_BUTTON39 = 0x0127,
  210. KEY_BUTTON40 = 0x0128,
  211. KEY_BUTTON41 = 0x0129,
  212. KEY_BUTTON42 = 0x012A,
  213. KEY_BUTTON43 = 0x012B,
  214. KEY_BUTTON44 = 0x012C,
  215. KEY_BUTTON45 = 0x012D,
  216. KEY_BUTTON46 = 0x012E,
  217. KEY_BUTTON47 = 0x012F,
  218. KEY_ANYKEY = 0xfffe,
  219. /// Joystick event codes.
  220. SI_XPOV = 0x204,
  221. SI_YPOV = 0x205,
  222. SI_UPOV = 0x206,
  223. SI_DPOV = 0x207,
  224. SI_LPOV = 0x208,
  225. SI_RPOV = 0x209,
  226. SI_XAXIS = 0x20B,
  227. SI_YAXIS = 0x20C,
  228. SI_ZAXIS = 0x20D,
  229. SI_RXAXIS = 0x20E,
  230. SI_RYAXIS = 0x20F,
  231. SI_RZAXIS = 0x210,
  232. SI_SLIDER = 0x211,
  233. SI_XPOV2 = 0x212,
  234. SI_YPOV2 = 0x213,
  235. SI_UPOV2 = 0x214,
  236. SI_DPOV2 = 0x215,
  237. SI_LPOV2 = 0x216,
  238. SI_RPOV2 = 0x217,
  239. SI_POVMASK = 0x218,
  240. SI_POVMASK2 = 0x219,
  241. /// Trackball event codes.
  242. SI_XBALL = 0x21A,
  243. SI_YBALL = 0x21B,
  244. SI_XBALL2 = 0x21C,
  245. SI_YBALL2 = 0x21D,
  246. XI_CONNECT = 0x300,
  247. XI_THUMBLX = 0x301,
  248. XI_THUMBLY = 0x302,
  249. XI_THUMBRX = 0x303,
  250. XI_THUMBRY = 0x304,
  251. XI_LEFT_TRIGGER = 0x305,
  252. XI_RIGHT_TRIGGER = 0x306,
  253. /*XI_DPAD_UP = 0x307,
  254. XI_DPAD_DOWN = 0x308,
  255. XI_DPAD_LEFT = 0x309,
  256. XI_DPAD_RIGHT = 0x310,*/
  257. XI_START = 0x311,
  258. XI_BACK = 0x312,
  259. XI_LEFT_THUMB = 0x313,
  260. XI_RIGHT_THUMB = 0x314,
  261. XI_LEFT_SHOULDER = 0x315,
  262. XI_RIGHT_SHOULDER = 0x316,
  263. XI_A = 0x317,
  264. XI_B = 0x318,
  265. XI_X = 0x319,
  266. XI_Y = 0x31A,
  267. XI_GUIDE = 0x31B,
  268. INPUT_DEVICE_PLUGIN_CODES_START = 0x400,
  269. };
  270. /// Input device types
  271. typedef U32 InputDeviceTypes;
  272. enum InputDeviceTypesEnum
  273. {
  274. UnknownDeviceType,
  275. MouseDeviceType,
  276. KeyboardDeviceType,
  277. JoystickDeviceType,
  278. GamepadDeviceType,
  279. XInputDeviceType,
  280. NUM_INPUT_DEVICE_TYPES,
  281. INPUT_DEVICE_PLUGIN_DEVICES_START = NUM_INPUT_DEVICE_TYPES,
  282. };
  283. /// Device Event Action Types
  284. enum InputActionType
  285. {
  286. /// Button was depressed.
  287. SI_MAKE = 0x01,
  288. /// Button was released.
  289. SI_BREAK = 0x02,
  290. /// An axis moved.
  291. SI_MOVE = 0x03,
  292. /// A key repeat occurred. Happens in between a SI_MAKE and SI_BREAK.
  293. SI_REPEAT = 0x04,
  294. /// A value of some type. Matched with SI_FLOAT or SI_INT.
  295. SI_VALUE = 0x05,
  296. };
  297. ///Device Event Types
  298. enum InputEventType
  299. {
  300. SI_UNKNOWN = 0x01,
  301. SI_BUTTON = 0x02, // Button press/release
  302. SI_POV = 0x03, // Point of View hat
  303. SI_AXIS = 0x04, // Axis in range -1.0..1.0
  304. SI_POS = 0x05, // Absolute position value (Point3F)
  305. SI_ROT = 0x06, // Absolute rotation value (QuatF)
  306. SI_INT = 0x07, // Integer value (S32)
  307. SI_FLOAT = 0x08, // Float value (F32)
  308. SI_KEY = 0x0A, // Keyboard key
  309. };
  310. /// Wildcard match used by the input system.
  311. #define SI_ANY 0xff
  312. // Modifier Keys
  313. enum InputModifiers
  314. {
  315. /// shift and ctrl are the same between platforms.
  316. SI_LSHIFT = BIT(0),
  317. SI_RSHIFT = BIT(1),
  318. SI_SHIFT = (SI_LSHIFT|SI_RSHIFT),
  319. SI_LCTRL = BIT(2),
  320. SI_RCTRL = BIT(3),
  321. SI_CTRL = (SI_LCTRL|SI_RCTRL),
  322. /// win altkey, mapped to mac cmdkey.
  323. SI_LALT = BIT(4),
  324. SI_RALT = BIT(5),
  325. SI_ALT = (SI_LALT|SI_RALT),
  326. /// mac optionkey
  327. SI_MAC_LOPT = BIT(6),
  328. SI_MAC_ROPT = BIT(7),
  329. SI_MAC_OPT = (SI_MAC_LOPT|SI_MAC_ROPT),
  330. /// modifier keys used for common operations
  331. #if defined(TORQUE_OS_MAC)
  332. SI_COPYPASTE = SI_ALT,
  333. SI_MULTISELECT = SI_ALT,
  334. SI_RANGESELECT = SI_SHIFT,
  335. SI_PRIMARY_ALT = SI_MAC_OPT, ///< Primary key used for toggling into alternates of commands.
  336. SI_PRIMARY_CTRL = SI_ALT, ///< Primary key used for triggering commands.
  337. #else
  338. SI_COPYPASTE = SI_CTRL,
  339. SI_MULTISELECT = SI_CTRL,
  340. SI_RANGESELECT = SI_SHIFT,
  341. SI_PRIMARY_ALT = SI_ALT,
  342. SI_PRIMARY_CTRL = SI_CTRL,
  343. #endif
  344. /// modfier key used in conjunction w/ arrow keys to move cursor to next word
  345. #if defined(TORQUE_OS_MAC)
  346. SI_WORDJUMP = SI_MAC_OPT,
  347. #else
  348. SI_WORDJUMP = SI_CTRL,
  349. #endif
  350. /// modifier key used in conjunction w/ arrow keys to move cursor to beginning / end of line
  351. SI_LINEJUMP = SI_ALT,
  352. /// modifier key used in conjunction w/ home & end to jump to the top or bottom of a document
  353. #if defined(TORQUE_OS_MAC)
  354. SI_DOCJUMP = SI_ANY,
  355. #else
  356. SI_DOCJUMP = SI_CTRL,
  357. #endif
  358. };
  359. /// @}
  360. /// Generic input event.
  361. struct InputEventInfo
  362. {
  363. InputEventInfo()
  364. {
  365. deviceInst = 0;
  366. fValue = 0.f;
  367. fValue2 = 0.f;
  368. fValue3 = 0.f;
  369. fValue4 = 0.f;
  370. iValue = 0;
  371. deviceType = (InputDeviceTypes)0;
  372. objType = (InputEventType)0;
  373. ascii = 0;
  374. objInst = (InputObjectInstances)0;
  375. action = (InputActionType)0;
  376. modifier = (InputModifiers)0;
  377. }
  378. /// Device instance: joystick0, joystick1, etc
  379. U32 deviceInst;
  380. /// Value typically ranges from -1.0 to 1.0, but doesn't have to.
  381. /// It depends on the context.
  382. F32 fValue;
  383. /// Extended float values (often used for absolute rotation Quat)
  384. F32 fValue2;
  385. F32 fValue3;
  386. F32 fValue4;
  387. /// Signed integer value
  388. S32 iValue;
  389. /// What was the action? (MAKE/BREAK/MOVE)
  390. InputActionType action;
  391. InputDeviceTypes deviceType;
  392. InputEventType objType;
  393. InputObjectInstances objInst;
  394. /// ASCII character code if this is a keyboard event.
  395. U16 ascii;
  396. /// Modifiers to action: SI_LSHIFT, SI_LCTRL, etc.
  397. U32 modifier;
  398. inline void postToSignal(InputEvent &ie)
  399. {
  400. ie.trigger(deviceInst, fValue, fValue2, fValue3, fValue4, iValue, deviceType, objType, ascii, objInst, action, modifier);
  401. }
  402. };
  403. class Point3F;
  404. class QuatF;
  405. /// Handles input device plug-ins
  406. class InputEventManager
  407. {
  408. public:
  409. struct VirtualMapData
  410. {
  411. StringTableEntry desc;
  412. InputEventType type;
  413. InputObjectInstances code;
  414. };
  415. public:
  416. InputEventManager();
  417. virtual ~InputEventManager();
  418. /// Get the next device type code
  419. U32 getNextDeviceType();
  420. /// Get the next device action code
  421. U32 getNextDeviceCode();
  422. void registerDevice(IInputDevice* device);
  423. void unregisterDevice(IInputDevice* device);
  424. /// Check if the given device name is a registered device.
  425. /// The given name can optionally include an instance number on the end.
  426. bool isRegisteredDevice(const char* name);
  427. /// Check if the given device type is a registered device.
  428. bool isRegisteredDevice(U32 type);
  429. /// Same as above but also provides the found device type and actual
  430. // device name length. Used by ActionMap::getDeviceTypeAndInstance()
  431. bool isRegisteredDeviceWithAttributes(const char* name, U32& deviceType, U32&nameLen);
  432. /// Returns the name of a registered device given its type
  433. const char* getRegisteredDeviceName(U32 type);
  434. void start();
  435. void stop();
  436. void process();
  437. // Add to the virtual map table
  438. void addVirtualMap(const char* description, InputEventType type, InputObjectInstances code);
  439. // Find a virtual map entry based on the text description
  440. VirtualMapData* findVirtualMap(const char* description);
  441. // Find a virtual map entry's description based on the action code
  442. const char* findVirtualMapDescFromCode(U32 code);
  443. /// Build an input event based on a single iValue
  444. void buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, S32 iValue);
  445. /// Build an input event based on a single fValue
  446. void buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, F32 fValue);
  447. /// Build an input event based on a Point3F
  448. void buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, Point3F& pValue);
  449. /// Build an input event based on a QuatF
  450. void buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, QuatF& qValue);
  451. /// Build an input event based on a AngAxisF
  452. void buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, AngAxisF& qValue);
  453. protected:
  454. U32 mNextDeviceTypeCode;
  455. U32 mNextDeviceCode;
  456. Vector<IInputDevice*> mDeviceList;
  457. // Holds description to VirtualMapData struct
  458. SimpleHashTable<VirtualMapData> mVirtualMap;
  459. // Used to look up a description based on a VirtualMapData.code
  460. HashTable<U32, VirtualMapData> mActionCodeMap;
  461. protected:
  462. void buildVirtualMap();
  463. public:
  464. // For ManagedSingleton.
  465. static const char* getSingletonName() { return "InputEventManager"; }
  466. };
  467. /// Returns the InputEventManager singleton.
  468. #define INPUTMGR ManagedSingleton<InputEventManager>::instance()
  469. #endif