event.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. typedef int NetConnectionId;
  36. /// Generic network address
  37. ///
  38. /// This is used to represent IP addresses.
  39. struct NetAddress {
  40. int type; ///< Type of address (IPAddress currently)
  41. /// Acceptable NetAddress types.
  42. enum {
  43. IPAddress,
  44. IPXAddress
  45. };
  46. U8 netNum[4]; ///< For IP: sin_addr<br>
  47. /// For IPX: sa_netnum
  48. U8 nodeNum[6]; ///< For IP: Not used.<br>
  49. /// For IPX: sa_nodenum (not used by IP)
  50. U16 port; ///< For IP: sin_port<br>
  51. /// For IPX: sa_socket
  52. };
  53. enum EventConstants
  54. {
  55. MaxPacketDataSize = 1500, ///< Maximum allowed size of a packet.
  56. MaxConsoleLineSize = 512 ///< Maximum allowed size of a console line.
  57. };
  58. /// Available event types.
  59. enum EventTypes
  60. {
  61. InputEventType,
  62. MouseMoveEventType,
  63. ScreenTouchEventType,
  64. PacketReceiveEventType,
  65. TimeEventType,
  66. QuitEventType,
  67. ConsoleEventType,
  68. ConnectedReceiveEventType,
  69. ConnectedAcceptEventType,
  70. ConnectedNotifyEventType
  71. };
  72. /// Base event structure (also used for FrameEvent and QuitEvent)
  73. struct Event
  74. {
  75. U16 type, size;
  76. Event() { size = sizeof(Event); }
  77. };
  78. /// The time event causes the simulation to advance.
  79. struct TimeEvent : public Event
  80. {
  81. U32 elapsedTime; ///< Indicates elapsed time in simulation.
  82. TimeEvent() { type = TimeEventType; size = sizeof(TimeEvent); }
  83. };
  84. /// Notify simulation of state of a connection.
  85. struct ConnectedNotifyEvent : public Event
  86. {
  87. /// Valid connection states
  88. enum State {
  89. DNSResolved,
  90. DNSFailed,
  91. Connected,
  92. ConnectFailed,
  93. Disconnected
  94. };
  95. U32 state; ///< Indicates current connection state.
  96. U32 tag; ///< Identifies connection.
  97. ConnectedNotifyEvent() { type = ConnectedNotifyEventType; size = sizeof(ConnectedNotifyEvent); }
  98. };
  99. /// Triggered when we receive data from a connected entity.
  100. struct ConnectedReceiveEvent : public Event
  101. {
  102. U32 tag; ///< Identifies connection
  103. U8 data[MaxPacketDataSize]; /// Payload
  104. ConnectedReceiveEvent() { type = ConnectedReceiveEventType; }
  105. };
  106. /// Triggered when we accept a new connection.
  107. struct ConnectedAcceptEvent : public Event
  108. {
  109. U32 portTag; ///< Identifies port we received this connection on.
  110. U32 connectionTag; ///< Identifies the connection.
  111. NetAddress address; ///< Originating address of connection.
  112. ConnectedAcceptEvent() { type = ConnectedAcceptEventType; size = sizeof(ConnectedAcceptEvent); }
  113. };
  114. /// Triggered on incoming (UDP) packet
  115. ///
  116. /// packetType is what type of packet it is.
  117. struct PacketReceiveEvent : public Event
  118. {
  119. NetAddress sourceAddress; ///< Originating address.
  120. U8 data[MaxPacketDataSize]; ///< Payload
  121. PacketReceiveEvent() { type = PacketReceiveEventType; }
  122. };
  123. /// Represents a line of console input.
  124. struct ConsoleEvent : public Event
  125. {
  126. char data[MaxConsoleLineSize]; ///< Payload
  127. ConsoleEvent() { type = ConsoleEventType; }
  128. };
  129. /// Header sizes for events defined later on.
  130. /// Byte offset to payload of a PacketReceiveEvent
  131. const U32 PacketReceiveEventHeaderSize = Offset(data,PacketReceiveEvent);
  132. /// Byte offset to payload of a ConnectedReceiveEvent
  133. const U32 ConnectedReceiveEventHeaderSize = Offset(data,ConnectedReceiveEvent);
  134. /// Byte offset to payload of a ConsoleEvent
  135. const U32 ConsoleEventHeaderSize = Offset(data,ConsoleEvent);
  136. /// Mouse input event.
  137. struct MouseMoveEvent : public Event
  138. {
  139. S32 xPos, yPos;
  140. U8 modifier;
  141. MouseMoveEvent() { type = MouseMoveEventType; size = sizeof(MouseMoveEvent); }
  142. };
  143. struct ScreenTouchEvent : public Event
  144. {
  145. S32 xPos, yPos;
  146. S32 touchID;
  147. U8 action;
  148. U32 numTouches;
  149. ScreenTouchEvent() { type = ScreenTouchEventType; size = sizeof(ScreenTouchEvent); }
  150. };
  151. /// Generic input event.
  152. struct InputEvent : public Event
  153. {
  154. U32 deviceInst; ///< Device instance: joystick0, joystick1, etc
  155. float fValue; ///< Value ranges from -1.0 to 1.0
  156. U16 deviceType; ///< One of mouse, keyboard, joystick, unknown
  157. U16 objType; ///< One of SI_XAXIS, SI_BUTTON, SI_KEY ...
  158. U16 ascii; ///< ASCII character code if this is a keyboard event.
  159. U16 objInst; ///< Which type instance or a KeyCode
  160. U8 action; ///< What was the action? (MAKE/BREAK/MOVE)
  161. U8 modifier; ///< Modifier to action: SI_LSHIFT, SI_LCTRL, etc.
  162. // iOS specific
  163. char touchesX[256]; ///< Collection of x-coordinates for touches
  164. char touchesY[256]; ///< Collection of y-coordinates for touches
  165. char touchIDs[256]; ///< Collection of touch IDs
  166. InputEvent() { type = InputEventType; size = sizeof(InputEvent); dMemset(touchesX, 0, sizeof(touchesX));
  167. dMemset(touchesY, 0, sizeof(touchesY));
  168. dMemset(touchIDs, 0, sizeof(touchIDs));}
  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. };
  356. enum AccelerometerCodes
  357. {
  358. SI_ACCELX = 0x300,
  359. SI_ACCELY = 0x301,
  360. SI_ACCELZ = 0x302,
  361. SI_GRAVX = 0x303,
  362. SI_GRAVY = 0x304,
  363. SI_GRAVZ = 0x305
  364. };
  365. enum GyroCodes
  366. {
  367. SI_GYROX = 0x306,
  368. SI_GYROY = 0x307,
  369. SI_GYROZ = 0x308,
  370. SI_YAW = 0x309,
  371. SI_PITCH = 0x30A,
  372. SI_ROLL = 0x30B
  373. };
  374. enum TouchCodes
  375. {
  376. SI_TOUCHDOWN = 0x30C,
  377. SI_TOUCHUP = 0x30D,
  378. SI_TOUCHMOVE = 0x30E,
  379. SI_PINCH = 0x30F,
  380. SI_SCALE = 0x401,
  381. SI_TAP = 0x402
  382. };
  383. /// Input device types
  384. enum InputDeviceTypes
  385. {
  386. UnknownDeviceType,
  387. MouseDeviceType,
  388. KeyboardDeviceType,
  389. JoystickDeviceType,
  390. ScreenTouchDeviceType,
  391. AccelerometerDeviceType,
  392. GyroscopeDeviceType
  393. };
  394. /// Device Event Action Types
  395. #define SI_MAKE 0x01
  396. #define SI_BREAK 0x02
  397. #define SI_MOVE 0x03
  398. #define SI_REPEAT 0x04
  399. ///Device Event Types
  400. #define SI_UNKNOWN 0x01
  401. #define SI_BUTTON 0x02
  402. #define SI_POV 0x03
  403. #define SI_KEY 0x0A
  404. #define SI_TEXT 0x0B
  405. #define SI_TOUCH 0x0C
  406. #define SI_GESTURE 0x0D
  407. #define SI_MOTION 0x0F
  408. /// Event SubTypes
  409. #define SI_ANY 0xff
  410. // Modifier Keys
  411. /// shift and ctrl are the same between platforms.
  412. #define SI_LSHIFT (1<<0)
  413. #define SI_RSHIFT (1<<1)
  414. #define SI_SHIFT (SI_LSHIFT|SI_RSHIFT)
  415. #define SI_LCTRL (1<<2)
  416. #define SI_RCTRL (1<<3)
  417. #define SI_CTRL (SI_LCTRL|SI_RCTRL)
  418. /// win altkey, mapped to mac cmdkey.
  419. #define SI_LALT (1<<4)
  420. #define SI_RALT (1<<5)
  421. #define SI_ALT (SI_LALT|SI_RALT)
  422. /// mac optionkey
  423. #define SI_MAC_LOPT (1<<6)
  424. #define SI_MAC_ROPT (1<<7)
  425. #define SI_MAC_OPT (SI_MAC_LOPT|SI_MAC_ROPT)
  426. /// @}
  427. #endif