event.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. XI_CONNECT = 0x300,
  240. XI_THUMBLX = 0x301,
  241. XI_THUMBLY = 0x302,
  242. XI_THUMBRX = 0x303,
  243. XI_THUMBRY = 0x304,
  244. XI_LEFT_TRIGGER = 0x305,
  245. XI_RIGHT_TRIGGER = 0x306,
  246. /*XI_DPAD_UP = 0x307,
  247. XI_DPAD_DOWN = 0x308,
  248. XI_DPAD_LEFT = 0x309,
  249. XI_DPAD_RIGHT = 0x310,*/
  250. XI_START = 0x311,
  251. XI_BACK = 0x312,
  252. XI_LEFT_THUMB = 0x313,
  253. XI_RIGHT_THUMB = 0x314,
  254. XI_LEFT_SHOULDER = 0x315,
  255. XI_RIGHT_SHOULDER = 0x316,
  256. XI_A = 0x317,
  257. XI_B = 0x318,
  258. XI_X = 0x319,
  259. XI_Y = 0x320,
  260. INPUT_DEVICE_PLUGIN_CODES_START = 0x400,
  261. };
  262. /// Input device types
  263. typedef U32 InputDeviceTypes;
  264. enum InputDeviceTypesEnum
  265. {
  266. UnknownDeviceType,
  267. MouseDeviceType,
  268. KeyboardDeviceType,
  269. JoystickDeviceType,
  270. GamepadDeviceType,
  271. XInputDeviceType,
  272. NUM_INPUT_DEVICE_TYPES,
  273. INPUT_DEVICE_PLUGIN_DEVICES_START = NUM_INPUT_DEVICE_TYPES,
  274. };
  275. /// Device Event Action Types
  276. enum InputActionType
  277. {
  278. /// Button was depressed.
  279. SI_MAKE = 0x01,
  280. /// Button was released.
  281. SI_BREAK = 0x02,
  282. /// An axis moved.
  283. SI_MOVE = 0x03,
  284. /// A key repeat occurred. Happens in between a SI_MAKE and SI_BREAK.
  285. SI_REPEAT = 0x04,
  286. /// A value of some type. Matched with SI_FLOAT or SI_INT.
  287. SI_VALUE = 0x05,
  288. };
  289. ///Device Event Types
  290. enum InputEventType
  291. {
  292. SI_UNKNOWN = 0x01,
  293. SI_BUTTON = 0x02, // Button press/release
  294. SI_POV = 0x03, // Point of View hat
  295. SI_AXIS = 0x04, // Axis in range -1.0..1.0
  296. SI_POS = 0x05, // Absolute position value (Point3F)
  297. SI_ROT = 0x06, // Absolute rotation value (QuatF)
  298. SI_INT = 0x07, // Integer value (S32)
  299. SI_FLOAT = 0x08, // Float value (F32)
  300. SI_KEY = 0x0A, // Keyboard key
  301. };
  302. /// Wildcard match used by the input system.
  303. #define SI_ANY 0xff
  304. // Modifier Keys
  305. enum InputModifiers
  306. {
  307. /// shift and ctrl are the same between platforms.
  308. SI_LSHIFT = BIT(0),
  309. SI_RSHIFT = BIT(1),
  310. SI_SHIFT = (SI_LSHIFT|SI_RSHIFT),
  311. SI_LCTRL = BIT(2),
  312. SI_RCTRL = BIT(3),
  313. SI_CTRL = (SI_LCTRL|SI_RCTRL),
  314. /// win altkey, mapped to mac cmdkey.
  315. SI_LALT = BIT(4),
  316. SI_RALT = BIT(5),
  317. SI_ALT = (SI_LALT|SI_RALT),
  318. /// mac optionkey
  319. SI_MAC_LOPT = BIT(6),
  320. SI_MAC_ROPT = BIT(7),
  321. SI_MAC_OPT = (SI_MAC_LOPT|SI_MAC_ROPT),
  322. /// modifier keys used for common operations
  323. #if defined(TORQUE_OS_MAC)
  324. SI_COPYPASTE = SI_ALT,
  325. SI_MULTISELECT = SI_ALT,
  326. SI_RANGESELECT = SI_SHIFT,
  327. SI_PRIMARY_ALT = SI_MAC_OPT, ///< Primary key used for toggling into alternates of commands.
  328. SI_PRIMARY_CTRL = SI_ALT, ///< Primary key used for triggering commands.
  329. #else
  330. SI_COPYPASTE = SI_CTRL,
  331. SI_MULTISELECT = SI_CTRL,
  332. SI_RANGESELECT = SI_SHIFT,
  333. SI_PRIMARY_ALT = SI_ALT,
  334. SI_PRIMARY_CTRL = SI_CTRL,
  335. #endif
  336. /// modfier key used in conjunction w/ arrow keys to move cursor to next word
  337. #if defined(TORQUE_OS_MAC)
  338. SI_WORDJUMP = SI_MAC_OPT,
  339. #else
  340. SI_WORDJUMP = SI_CTRL,
  341. #endif
  342. /// modifier key used in conjunction w/ arrow keys to move cursor to beginning / end of line
  343. SI_LINEJUMP = SI_ALT,
  344. /// modifier key used in conjunction w/ home & end to jump to the top or bottom of a document
  345. #if defined(TORQUE_OS_MAC)
  346. SI_DOCJUMP = SI_ANY,
  347. #else
  348. SI_DOCJUMP = SI_CTRL,
  349. #endif
  350. };
  351. /// @}
  352. /// Generic input event.
  353. struct InputEventInfo
  354. {
  355. InputEventInfo()
  356. {
  357. deviceInst = 0;
  358. fValue = 0.f;
  359. fValue2 = 0.f;
  360. fValue3 = 0.f;
  361. fValue4 = 0.f;
  362. iValue = 0;
  363. deviceType = (InputDeviceTypes)0;
  364. objType = (InputEventType)0;
  365. ascii = 0;
  366. objInst = (InputObjectInstances)0;
  367. action = (InputActionType)0;
  368. modifier = (InputModifiers)0;
  369. }
  370. /// Device instance: joystick0, joystick1, etc
  371. U32 deviceInst;
  372. /// Value typically ranges from -1.0 to 1.0, but doesn't have to.
  373. /// It depends on the context.
  374. F32 fValue;
  375. /// Extended float values (often used for absolute rotation Quat)
  376. F32 fValue2;
  377. F32 fValue3;
  378. F32 fValue4;
  379. /// Signed integer value
  380. S32 iValue;
  381. /// What was the action? (MAKE/BREAK/MOVE)
  382. InputActionType action;
  383. InputDeviceTypes deviceType;
  384. InputEventType objType;
  385. InputObjectInstances objInst;
  386. /// ASCII character code if this is a keyboard event.
  387. U16 ascii;
  388. /// Modifiers to action: SI_LSHIFT, SI_LCTRL, etc.
  389. InputModifiers modifier;
  390. inline void postToSignal(InputEvent &ie)
  391. {
  392. ie.trigger(deviceInst, fValue, fValue2, fValue3, fValue4, iValue, deviceType, objType, ascii, objInst, action, modifier);
  393. }
  394. };
  395. class Point3F;
  396. class QuatF;
  397. /// Handles input device plug-ins
  398. class InputEventManager
  399. {
  400. public:
  401. struct VirtualMapData
  402. {
  403. StringTableEntry desc;
  404. InputEventType type;
  405. InputObjectInstances code;
  406. };
  407. public:
  408. InputEventManager();
  409. virtual ~InputEventManager();
  410. /// Get the next device type code
  411. U32 getNextDeviceType();
  412. /// Get the next device action code
  413. U32 getNextDeviceCode();
  414. void registerDevice(IInputDevice* device);
  415. void unregisterDevice(IInputDevice* device);
  416. /// Check if the given device name is a registered device.
  417. /// The given name can optionally include an instance number on the end.
  418. bool isRegisteredDevice(const char* name);
  419. /// Check if the given device type is a registered device.
  420. bool isRegisteredDevice(U32 type);
  421. /// Same as above but also provides the found device type and actual
  422. // device name length. Used by ActionMap::getDeviceTypeAndInstance()
  423. bool isRegisteredDeviceWithAttributes(const char* name, U32& deviceType, U32&nameLen);
  424. /// Returns the name of a registered device given its type
  425. const char* getRegisteredDeviceName(U32 type);
  426. void start();
  427. void stop();
  428. void process();
  429. // Add to the virtual map table
  430. void addVirtualMap(const char* description, InputEventType type, InputObjectInstances code);
  431. // Find a virtual map entry based on the text description
  432. VirtualMapData* findVirtualMap(const char* description);
  433. // Find a virtual map entry's description based on the action code
  434. const char* findVirtualMapDescFromCode(U32 code);
  435. /// Build an input event based on a single iValue
  436. void buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, S32 iValue);
  437. /// Build an input event based on a single fValue
  438. void buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, F32 fValue);
  439. /// Build an input event based on a Point3F
  440. void buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, Point3F& pValue);
  441. /// Build an input event based on a QuatF
  442. void buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, QuatF& qValue);
  443. protected:
  444. U32 mNextDeviceTypeCode;
  445. U32 mNextDeviceCode;
  446. Vector<IInputDevice*> mDeviceList;
  447. // Holds description to VirtualMapData struct
  448. SimpleHashTable<VirtualMapData> mVirtualMap;
  449. // Used to look up a description based on a VirtualMapData.code
  450. HashTable<U32, VirtualMapData> mActionCodeMap;
  451. protected:
  452. void buildVirtualMap();
  453. public:
  454. // For ManagedSingleton.
  455. static const char* getSingletonName() { return "InputEventManager"; }
  456. };
  457. /// Returns the InputEventManager singleton.
  458. #define INPUTMGR ManagedSingleton<InputEventManager>::instance()
  459. #endif