event.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #ifndef _EVENT_H_
  28. #define _EVENT_H_
  29. #ifndef _TORQUE_TYPES_H_
  30. #include "platform/types.h"
  31. #endif
  32. #ifndef _PLATFORM_MEMORY_H_
  33. #include "platform/platformMemory.h"
  34. #endif
  35. #include "platform/platformNet.h"
  36. typedef int NetConnectionId;
  37. enum EventConstants
  38. {
  39. MaxPacketDataSize = 1500, ///< Maximum allowed size of a packet.
  40. MaxConsoleLineSize = 512 ///< Maximum allowed size of a console line.
  41. };
  42. /// Available event types.
  43. enum EventTypes
  44. {
  45. InputEventType,
  46. MouseMoveEventType,
  47. ScreenTouchEventType,
  48. PacketReceiveEventType,
  49. TimeEventType,
  50. QuitEventType,
  51. ConsoleEventType,
  52. ConnectedReceiveEventType,
  53. ConnectedAcceptEventType,
  54. ConnectedNotifyEventType
  55. };
  56. /// Base event structure (also used for FrameEvent and QuitEvent)
  57. struct Event
  58. {
  59. U16 type, size;
  60. Event() { size = sizeof(Event); }
  61. };
  62. /// The time event causes the simulation to advance.
  63. struct TimeEvent : public Event
  64. {
  65. U32 elapsedTime; ///< Indicates elapsed time in simulation.
  66. TimeEvent() { type = TimeEventType; size = sizeof(TimeEvent); }
  67. };
  68. /// Notify simulation of state of a connection.
  69. struct ConnectedNotifyEvent : public Event
  70. {
  71. /// Valid connection states
  72. enum State {
  73. DNSResolved,
  74. DNSFailed,
  75. Connected,
  76. ConnectFailed,
  77. Disconnected
  78. };
  79. U32 state; ///< Indicates current connection state.
  80. NetSocket tag; ///< Identifies connection.
  81. ConnectedNotifyEvent() { type = ConnectedNotifyEventType; size = sizeof(ConnectedNotifyEvent); }
  82. };
  83. /// Triggered when we receive data from a connected entity.
  84. struct ConnectedReceiveEvent : public Event
  85. {
  86. NetSocket tag; ///< Identifies connection
  87. U8 data[MaxPacketDataSize]; /// Payload
  88. ConnectedReceiveEvent() { type = ConnectedReceiveEventType; }
  89. };
  90. /// Triggered when we accept a new connection.
  91. struct ConnectedAcceptEvent : public Event
  92. {
  93. NetSocket portTag; ///< Identifies port we received this connection on.
  94. NetSocket connectionTag; ///< Identifies the connection.
  95. NetAddress address; ///< Originating address of connection.
  96. ConnectedAcceptEvent() { type = ConnectedAcceptEventType; size = sizeof(ConnectedAcceptEvent); }
  97. };
  98. /// Triggered on incoming (UDP) packet
  99. ///
  100. /// packetType is what type of packet it is.
  101. struct PacketReceiveEvent : public Event
  102. {
  103. NetAddress sourceAddress; ///< Originating address.
  104. U8 data[MaxPacketDataSize]; ///< Payload
  105. PacketReceiveEvent() { type = PacketReceiveEventType; }
  106. };
  107. /// Represents a line of console input.
  108. struct ConsoleEvent : public Event
  109. {
  110. char data[MaxConsoleLineSize]; ///< Payload
  111. ConsoleEvent() { type = ConsoleEventType; }
  112. };
  113. /// Header sizes for events defined later on.
  114. /// Byte offset to payload of a PacketReceiveEvent
  115. const U32 PacketReceiveEventHeaderSize = Offset(data,PacketReceiveEvent);
  116. /// Byte offset to payload of a ConnectedReceiveEvent
  117. const U32 ConnectedReceiveEventHeaderSize = Offset(data,ConnectedReceiveEvent);
  118. /// Byte offset to payload of a ConsoleEvent
  119. const U32 ConsoleEventHeaderSize = Offset(data,ConsoleEvent);
  120. /// Mouse input event.
  121. struct MouseMoveEvent : public Event
  122. {
  123. S32 xPos, yPos;
  124. U8 modifier;
  125. MouseMoveEvent() { type = MouseMoveEventType; size = sizeof(MouseMoveEvent); }
  126. };
  127. struct ScreenTouchEvent : public Event
  128. {
  129. S32 xPos, yPos;
  130. S32 touchID;
  131. U8 action;
  132. U32 numTouches;
  133. ScreenTouchEvent() { type = ScreenTouchEventType; size = sizeof(ScreenTouchEvent); }
  134. };
  135. /// Generic input event.
  136. struct InputEvent : public Event
  137. {
  138. U32 deviceInst; ///< Device instance: joystick0, joystick1, etc
  139. S32 iValue; ///< Handy for tracking IDs of things like fingers, hands, etc
  140. float fValues[7]; ///< Stores the evemt data. Sometimes only one with a range of -1.0 - 1.0 is needed, other times it might be multiple vectors
  141. U16 deviceType; ///< One of mouse, keyboard, joystick, unknown
  142. U16 objType; ///< One of SI_XAXIS, SI_BUTTON, SI_KEY ...
  143. U16 ascii; ///< ASCII character code if this is a keyboard event.
  144. U16 objInst; ///< Which type instance or a KeyCode
  145. U8 action; ///< What was the action? (MAKE/BREAK/MOVE)
  146. U8 modifier; ///< Modifier to action: SI_LSHIFT, SI_LCTRL, etc.
  147. // iOS specific
  148. char fingersX[256]; ///< Collection of x-coordinates for fingers
  149. char fingersY[256]; ///< Collection of y-coordinates for fingers
  150. char fingersZ[256]; ///< Collection of Z-coordinates for fingers
  151. char fingerIDs[256]; ///< Collection of touch IDs
  152. InputEvent()
  153. {
  154. type = InputEventType;
  155. size = sizeof(InputEvent);
  156. deviceInst = 0;
  157. iValue = -1;
  158. objType = 0;
  159. ascii = 0;
  160. objInst = 0;
  161. action = 0;
  162. modifier = 0;
  163. dMemset(fValues, 0, sizeof(fValues));
  164. dMemset(fingersX, 0, sizeof(fingersX));
  165. dMemset(fingersY, 0, sizeof(fingersY));
  166. dMemset(fingersZ, 0, sizeof(fingersZ));
  167. dMemset(fingerIDs, 0, sizeof(fingerIDs));
  168. }
  169. };
  170. /// @defgroup input_constants Input system constants
  171. /// @{
  172. /// Input event constants:
  173. enum KeyCodes {
  174. KEY_NULL = 0x000, ///< Invalid KeyCode
  175. KEY_BACKSPACE = 0x001,
  176. KEY_TAB = 0x002,
  177. KEY_RETURN = 0x003,
  178. KEY_CONTROL = 0x004,
  179. KEY_ALT = 0x005,
  180. KEY_SHIFT = 0x006,
  181. KEY_PAUSE = 0x007,
  182. KEY_CAPSLOCK = 0x008,
  183. KEY_ESCAPE = 0x009,
  184. KEY_SPACE = 0x00a,
  185. KEY_PAGE_DOWN = 0x00b,
  186. KEY_PAGE_UP = 0x00c,
  187. KEY_END = 0x00d,
  188. KEY_HOME = 0x00e,
  189. KEY_LEFT = 0x00f,
  190. KEY_UP = 0x010,
  191. KEY_RIGHT = 0x011,
  192. KEY_DOWN = 0x012,
  193. KEY_PRINT = 0x013,
  194. KEY_INSERT = 0x014,
  195. KEY_DELETE = 0x015,
  196. KEY_HELP = 0x016,
  197. KEY_0 = 0x017,
  198. KEY_1 = 0x018,
  199. KEY_2 = 0x019,
  200. KEY_3 = 0x01a,
  201. KEY_4 = 0x01b,
  202. KEY_5 = 0x01c,
  203. KEY_6 = 0x01d,
  204. KEY_7 = 0x01e,
  205. KEY_8 = 0x01f,
  206. KEY_9 = 0x020,
  207. KEY_A = 0x021,
  208. KEY_B = 0x022,
  209. KEY_C = 0x023,
  210. KEY_D = 0x024,
  211. KEY_E = 0x025,
  212. KEY_F = 0x026,
  213. KEY_G = 0x027,
  214. KEY_H = 0x028,
  215. KEY_I = 0x029,
  216. KEY_J = 0x02a,
  217. KEY_K = 0x02b,
  218. KEY_L = 0x02c,
  219. KEY_M = 0x02d,
  220. KEY_N = 0x02e,
  221. KEY_O = 0x02f,
  222. KEY_P = 0x030,
  223. KEY_Q = 0x031,
  224. KEY_R = 0x032,
  225. KEY_S = 0x033,
  226. KEY_T = 0x034,
  227. KEY_U = 0x035,
  228. KEY_V = 0x036,
  229. KEY_W = 0x037,
  230. KEY_X = 0x038,
  231. KEY_Y = 0x039,
  232. KEY_Z = 0x03a,
  233. KEY_TILDE = 0x03b,
  234. KEY_MINUS = 0x03c,
  235. KEY_EQUALS = 0x03d,
  236. KEY_LBRACKET = 0x03e,
  237. KEY_RBRACKET = 0x03f,
  238. KEY_BACKSLASH = 0x040,
  239. KEY_SEMICOLON = 0x041,
  240. KEY_APOSTROPHE = 0x042,
  241. KEY_COMMA = 0x043,
  242. KEY_PERIOD = 0x044,
  243. KEY_SLASH = 0x045,
  244. KEY_NUMPAD0 = 0x046,
  245. KEY_NUMPAD1 = 0x047,
  246. KEY_NUMPAD2 = 0x048,
  247. KEY_NUMPAD3 = 0x049,
  248. KEY_NUMPAD4 = 0x04a,
  249. KEY_NUMPAD5 = 0x04b,
  250. KEY_NUMPAD6 = 0x04c,
  251. KEY_NUMPAD7 = 0x04d,
  252. KEY_NUMPAD8 = 0x04e,
  253. KEY_NUMPAD9 = 0x04f,
  254. KEY_MULTIPLY = 0x050,
  255. KEY_ADD = 0x051,
  256. KEY_SEPARATOR = 0x052,
  257. KEY_SUBTRACT = 0x053,
  258. KEY_DECIMAL = 0x054,
  259. KEY_DIVIDE = 0x055,
  260. KEY_NUMPADENTER = 0x056,
  261. KEY_F1 = 0x057,
  262. KEY_F2 = 0x058,
  263. KEY_F3 = 0x059,
  264. KEY_F4 = 0x05a,
  265. KEY_F5 = 0x05b,
  266. KEY_F6 = 0x05c,
  267. KEY_F7 = 0x05d,
  268. KEY_F8 = 0x05e,
  269. KEY_F9 = 0x05f,
  270. KEY_F10 = 0x060,
  271. KEY_F11 = 0x061,
  272. KEY_F12 = 0x062,
  273. KEY_F13 = 0x063,
  274. KEY_F14 = 0x064,
  275. KEY_F15 = 0x065,
  276. KEY_F16 = 0x066,
  277. KEY_F17 = 0x067,
  278. KEY_F18 = 0x068,
  279. KEY_F19 = 0x069,
  280. KEY_F20 = 0x06a,
  281. KEY_F21 = 0x06b,
  282. KEY_F22 = 0x06c,
  283. KEY_F23 = 0x06d,
  284. KEY_F24 = 0x06e,
  285. KEY_NUMLOCK = 0x06f,
  286. KEY_SCROLLLOCK = 0x070,
  287. KEY_LCONTROL = 0x071,
  288. KEY_RCONTROL = 0x072,
  289. KEY_LALT = 0x073,
  290. KEY_RALT = 0x074,
  291. KEY_LSHIFT = 0x075,
  292. KEY_RSHIFT = 0x076,
  293. KEY_WIN_LWINDOW = 0x077,
  294. KEY_WIN_RWINDOW = 0x078,
  295. KEY_WIN_APPS = 0x079,
  296. KEY_OEM_102 = 0x080,
  297. KEY_MAC_OPT = 0x090,
  298. KEY_MAC_LOPT = 0x091,
  299. KEY_MAC_ROPT = 0x092,
  300. KEY_BUTTON0 = 0x0100,
  301. KEY_BUTTON1 = 0x0101,
  302. KEY_BUTTON2 = 0x0102,
  303. KEY_BUTTON3 = 0x0103,
  304. KEY_BUTTON4 = 0x0104,
  305. KEY_BUTTON5 = 0x0105,
  306. KEY_BUTTON6 = 0x0106,
  307. KEY_BUTTON7 = 0x0107,
  308. KEY_BUTTON8 = 0x0108,
  309. KEY_BUTTON9 = 0x0109,
  310. KEY_BUTTON10 = 0x010A,
  311. KEY_BUTTON11 = 0x010B,
  312. KEY_BUTTON12 = 0x010C,
  313. KEY_BUTTON13 = 0x010D,
  314. KEY_BUTTON14 = 0x010E,
  315. KEY_BUTTON15 = 0x010F,
  316. KEY_BUTTON16 = 0x0110,
  317. KEY_BUTTON17 = 0x0111,
  318. KEY_BUTTON18 = 0x0112,
  319. KEY_BUTTON19 = 0x0113,
  320. KEY_BUTTON20 = 0x0114,
  321. KEY_BUTTON21 = 0x0115,
  322. KEY_BUTTON22 = 0x0116,
  323. KEY_BUTTON23 = 0x0117,
  324. KEY_BUTTON24 = 0x0118,
  325. KEY_BUTTON25 = 0x0119,
  326. KEY_BUTTON26 = 0x011A,
  327. KEY_BUTTON27 = 0x011B,
  328. KEY_BUTTON28 = 0x011C,
  329. KEY_BUTTON29 = 0x011D,
  330. KEY_BUTTON30 = 0x011E,
  331. KEY_BUTTON31 = 0x011F,
  332. KEY_ANYKEY = 0xfffe
  333. };
  334. /// Joystick event codes.
  335. enum JoystickCodes {
  336. SI_XPOV = 0x204,
  337. SI_YPOV = 0x205,
  338. SI_UPOV = 0x206,
  339. SI_DPOV = 0x207,
  340. SI_LPOV = 0x208,
  341. SI_RPOV = 0x209,
  342. SI_XAXIS = 0x20B,
  343. SI_YAXIS = 0x20C,
  344. SI_ZAXIS = 0x20D,
  345. SI_RXAXIS = 0x20E,
  346. SI_RYAXIS = 0x20F,
  347. SI_RZAXIS = 0x210,
  348. SI_SLIDER = 0x211,
  349. SI_XPOV2 = 0x212,
  350. SI_YPOV2 = 0x213,
  351. SI_UPOV2 = 0x214,
  352. SI_DPOV2 = 0x215,
  353. SI_LPOV2 = 0x216,
  354. SI_RPOV2 = 0x217,
  355. //Xinput specific
  356. XI_CONNECT = 0x300,
  357. XI_THUMBLX = 0x301,
  358. XI_THUMBLY = 0x302,
  359. XI_THUMBRX = 0x303,
  360. XI_THUMBRY = 0x304,
  361. XI_LEFT_TRIGGER = 0x305,
  362. XI_RIGHT_TRIGGER = 0x306,
  363. XI_DPAD_UP = 0x206,
  364. XI_DPAD_DOWN = 0x207,
  365. XI_DPAD_LEFT = 0x208,
  366. XI_DPAD_RIGHT = 0x209,
  367. XI_START = 0x311,
  368. XI_BACK = 0x312,
  369. XI_LEFT_THUMB = 0x313,
  370. XI_RIGHT_THUMB = 0x314,
  371. XI_LEFT_SHOULDER = 0x315,
  372. XI_RIGHT_SHOULDER = 0x316,
  373. XI_A = 0x317,
  374. XI_B = 0x318,
  375. XI_X = 0x319,
  376. XI_Y = 0x320
  377. };
  378. enum AccelerometerCodes
  379. {
  380. SI_ACCELX = 0x300,
  381. SI_ACCELY = 0x301,
  382. SI_ACCELZ = 0x302,
  383. SI_GRAVX = 0x303,
  384. SI_GRAVY = 0x304,
  385. SI_GRAVZ = 0x305
  386. };
  387. enum GyroCodes
  388. {
  389. SI_GYROX = 0x306,
  390. SI_GYROY = 0x307,
  391. SI_GYROZ = 0x308,
  392. SI_YAW = 0x309,
  393. SI_PITCH = 0x30A,
  394. SI_ROLL = 0x30B
  395. };
  396. enum TouchCodes
  397. {
  398. SI_TOUCHDOWN = 0x30C,
  399. SI_TOUCHUP = 0x30D,
  400. SI_TOUCHMOVE = 0x30E,
  401. };
  402. enum GestureCodes
  403. {
  404. SI_CIRCLE_GESTURE = 0x403,
  405. SI_SWIPE_GESTURE = 0x404,
  406. SI_KEYTAP_GESTURE = 0x405,
  407. SI_SCREENTAP_GESTURE = 0x406,
  408. SI_PINCH_GESTURE = 0x407,
  409. SI_SCALE_GESTURE = 0x408
  410. };
  411. /// Input device types
  412. enum InputDeviceTypes
  413. {
  414. UnknownDeviceType,
  415. MouseDeviceType,
  416. KeyboardDeviceType,
  417. JoystickDeviceType,
  418. GamepadDeviceType,
  419. XInputDeviceType,
  420. ScreenTouchDeviceType,
  421. AccelerometerDeviceType,
  422. GyroscopeDeviceType
  423. };
  424. /// Device Event Action Types
  425. #define SI_MAKE 0x01
  426. #define SI_BREAK 0x02
  427. #define SI_MOVE 0x03
  428. #define SI_REPEAT 0x04
  429. #define SI_VALUE 0x05
  430. ///Device Event Types
  431. #define SI_UNKNOWN 0x01
  432. #define SI_BUTTON 0x02
  433. #define SI_POV 0x03
  434. #define SI_KEY 0x0A
  435. #define SI_TEXT 0x0B
  436. #define SI_TOUCH 0x0C
  437. #define SI_GESTURE 0x0D
  438. #define SI_MOTION 0x0F
  439. /// Event SubTypes
  440. #define SI_ANY 0xff
  441. // Modifier Keys
  442. /// shift and ctrl are the same between platforms.
  443. #define SI_LSHIFT (1<<0)
  444. #define SI_RSHIFT (1<<1)
  445. #define SI_SHIFT (SI_LSHIFT|SI_RSHIFT)
  446. #define SI_LCTRL (1<<2)
  447. #define SI_RCTRL (1<<3)
  448. #define SI_CTRL (SI_LCTRL|SI_RCTRL)
  449. /// win altkey, mapped to mac cmdkey.
  450. #define SI_LALT (1<<4)
  451. #define SI_RALT (1<<5)
  452. #define SI_ALT (SI_LALT|SI_RALT)
  453. /// mac optionkey
  454. #define SI_MAC_LOPT (1<<6)
  455. #define SI_MAC_ROPT (1<<7)
  456. #define SI_MAC_OPT (SI_MAC_LOPT|SI_MAC_ROPT)
  457. /// @}
  458. //Xinput structs
  459. typedef U32 InputObjectInstances;
  460. enum XInputEventType
  461. {
  462. XI_UNKNOWN = 0x01,
  463. XI_BUTTON = 0x02, // Button press/release
  464. XI_POV = 0x03, // Point of View hat
  465. XI_AXIS = 0x04, // Axis in range -1.0..1.0
  466. XI_POS = 0x05, // Absolute position value (Point3F)
  467. XI_ROT = 0x06, // Absolute rotation value (QuatF)
  468. XI_INT = 0x07, // Integer value (S32)
  469. XI_FLOAT = 0x08, // Float value (F32)
  470. XI_KEY = 0x0A, // Keyboard key
  471. };
  472. enum InputActionType
  473. {
  474. /// Button was depressed.
  475. XI_MAKE = 0x01,
  476. /// Button was released.
  477. XI_BREAK = 0x02,
  478. /// An axis moved.
  479. XI_MOVE = 0x03,
  480. /// A key repeat occurred. Happens in between a SI_MAKE and SI_BREAK.
  481. XI_REPEAT = 0x04,
  482. /// A value of some type. Matched with SI_FLOAT or SI_INT.
  483. XI_VALUE = 0x05,
  484. };
  485. #endif