SDL_events.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. SDL - Simple DirectMedia Layer
  3. Copyright (C) 1997-2012 Sam Lantinga
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. Sam Lantinga
  16. slouken@libsdl.org
  17. */
  18. /**
  19. * @file SDL_events.h
  20. * Include file for SDL event handling
  21. */
  22. #ifndef _SDL_events_h
  23. #define _SDL_events_h
  24. #include "SDL_stdinc.h"
  25. #include "SDL_error.h"
  26. #include "SDL_active.h"
  27. #include "SDL_keyboard.h"
  28. #include "SDL_mouse.h"
  29. #include "SDL_joystick.h"
  30. #include "SDL_quit.h"
  31. #include "begin_code.h"
  32. /* Set up for C function definitions, even when using C++ */
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /** @name General keyboard/mouse state definitions */
  37. /*@{*/
  38. #define SDL_RELEASED 0
  39. #define SDL_PRESSED 1
  40. /*@}*/
  41. /** Event enumerations */
  42. typedef enum {
  43. SDL_NOEVENT = 0, /**< Unused (do not remove) */
  44. SDL_ACTIVEEVENT, /**< Application loses/gains visibility */
  45. SDL_KEYDOWN, /**< Keys pressed */
  46. SDL_KEYUP, /**< Keys released */
  47. SDL_MOUSEMOTION, /**< Mouse moved */
  48. SDL_MOUSEBUTTONDOWN, /**< Mouse button pressed */
  49. SDL_MOUSEBUTTONUP, /**< Mouse button released */
  50. SDL_JOYAXISMOTION, /**< Joystick axis motion */
  51. SDL_JOYBALLMOTION, /**< Joystick trackball motion */
  52. SDL_JOYHATMOTION, /**< Joystick hat position change */
  53. SDL_JOYBUTTONDOWN, /**< Joystick button pressed */
  54. SDL_JOYBUTTONUP, /**< Joystick button released */
  55. SDL_QUIT, /**< User-requested quit */
  56. SDL_SYSWMEVENT, /**< System specific event */
  57. SDL_EVENT_RESERVEDA, /**< Reserved for future use.. */
  58. SDL_EVENT_RESERVEDB, /**< Reserved for future use.. */
  59. SDL_VIDEORESIZE, /**< User resized video mode */
  60. SDL_VIDEOEXPOSE, /**< Screen needs to be redrawn */
  61. SDL_EVENT_RESERVED2, /**< Reserved for future use.. */
  62. SDL_EVENT_RESERVED3, /**< Reserved for future use.. */
  63. SDL_EVENT_RESERVED4, /**< Reserved for future use.. */
  64. SDL_EVENT_RESERVED5, /**< Reserved for future use.. */
  65. SDL_EVENT_RESERVED6, /**< Reserved for future use.. */
  66. SDL_EVENT_RESERVED7, /**< Reserved for future use.. */
  67. /** Events SDL_USEREVENT through SDL_MAXEVENTS-1 are for your use */
  68. SDL_USEREVENT = 24,
  69. /** This last event is only for bounding internal arrays
  70. * It is the number of bits in the event mask datatype -- Uint32
  71. */
  72. SDL_NUMEVENTS = 32
  73. } SDL_EventType;
  74. /** @name Predefined event masks */
  75. /*@{*/
  76. #define SDL_EVENTMASK(X) (1<<(X))
  77. typedef enum {
  78. SDL_ACTIVEEVENTMASK = SDL_EVENTMASK(SDL_ACTIVEEVENT),
  79. SDL_KEYDOWNMASK = SDL_EVENTMASK(SDL_KEYDOWN),
  80. SDL_KEYUPMASK = SDL_EVENTMASK(SDL_KEYUP),
  81. SDL_KEYEVENTMASK = SDL_EVENTMASK(SDL_KEYDOWN)|
  82. SDL_EVENTMASK(SDL_KEYUP),
  83. SDL_MOUSEMOTIONMASK = SDL_EVENTMASK(SDL_MOUSEMOTION),
  84. SDL_MOUSEBUTTONDOWNMASK = SDL_EVENTMASK(SDL_MOUSEBUTTONDOWN),
  85. SDL_MOUSEBUTTONUPMASK = SDL_EVENTMASK(SDL_MOUSEBUTTONUP),
  86. SDL_MOUSEEVENTMASK = SDL_EVENTMASK(SDL_MOUSEMOTION)|
  87. SDL_EVENTMASK(SDL_MOUSEBUTTONDOWN)|
  88. SDL_EVENTMASK(SDL_MOUSEBUTTONUP),
  89. SDL_JOYAXISMOTIONMASK = SDL_EVENTMASK(SDL_JOYAXISMOTION),
  90. SDL_JOYBALLMOTIONMASK = SDL_EVENTMASK(SDL_JOYBALLMOTION),
  91. SDL_JOYHATMOTIONMASK = SDL_EVENTMASK(SDL_JOYHATMOTION),
  92. SDL_JOYBUTTONDOWNMASK = SDL_EVENTMASK(SDL_JOYBUTTONDOWN),
  93. SDL_JOYBUTTONUPMASK = SDL_EVENTMASK(SDL_JOYBUTTONUP),
  94. SDL_JOYEVENTMASK = SDL_EVENTMASK(SDL_JOYAXISMOTION)|
  95. SDL_EVENTMASK(SDL_JOYBALLMOTION)|
  96. SDL_EVENTMASK(SDL_JOYHATMOTION)|
  97. SDL_EVENTMASK(SDL_JOYBUTTONDOWN)|
  98. SDL_EVENTMASK(SDL_JOYBUTTONUP),
  99. SDL_VIDEORESIZEMASK = SDL_EVENTMASK(SDL_VIDEORESIZE),
  100. SDL_VIDEOEXPOSEMASK = SDL_EVENTMASK(SDL_VIDEOEXPOSE),
  101. SDL_QUITMASK = SDL_EVENTMASK(SDL_QUIT),
  102. SDL_SYSWMEVENTMASK = SDL_EVENTMASK(SDL_SYSWMEVENT)
  103. } SDL_EventMask ;
  104. #define SDL_ALLEVENTS 0xFFFFFFFF
  105. /*@}*/
  106. /** Application visibility event structure */
  107. typedef struct SDL_ActiveEvent {
  108. Uint8 type; /**< SDL_ACTIVEEVENT */
  109. Uint8 gain; /**< Whether given states were gained or lost (1/0) */
  110. Uint8 state; /**< A mask of the focus states */
  111. } SDL_ActiveEvent;
  112. /** Keyboard event structure */
  113. typedef struct SDL_KeyboardEvent {
  114. Uint8 type; /**< SDL_KEYDOWN or SDL_KEYUP */
  115. Uint8 which; /**< The keyboard device index */
  116. Uint8 state; /**< SDL_PRESSED or SDL_RELEASED */
  117. SDL_keysym keysym;
  118. } SDL_KeyboardEvent;
  119. /** Mouse motion event structure */
  120. typedef struct SDL_MouseMotionEvent {
  121. Uint8 type; /**< SDL_MOUSEMOTION */
  122. Uint8 which; /**< The mouse device index */
  123. Uint8 state; /**< The current button state */
  124. Uint16 x, y; /**< The X/Y coordinates of the mouse */
  125. Sint16 xrel; /**< The relative motion in the X direction */
  126. Sint16 yrel; /**< The relative motion in the Y direction */
  127. } SDL_MouseMotionEvent;
  128. /** Mouse button event structure */
  129. typedef struct SDL_MouseButtonEvent {
  130. Uint8 type; /**< SDL_MOUSEBUTTONDOWN or SDL_MOUSEBUTTONUP */
  131. Uint8 which; /**< The mouse device index */
  132. Uint8 button; /**< The mouse button index */
  133. Uint8 state; /**< SDL_PRESSED or SDL_RELEASED */
  134. Uint16 x, y; /**< The X/Y coordinates of the mouse at press time */
  135. } SDL_MouseButtonEvent;
  136. /** Joystick axis motion event structure */
  137. typedef struct SDL_JoyAxisEvent {
  138. Uint8 type; /**< SDL_JOYAXISMOTION */
  139. Uint8 which; /**< The joystick device index */
  140. Uint8 axis; /**< The joystick axis index */
  141. Sint16 value; /**< The axis value (range: -32768 to 32767) */
  142. } SDL_JoyAxisEvent;
  143. /** Joystick trackball motion event structure */
  144. typedef struct SDL_JoyBallEvent {
  145. Uint8 type; /**< SDL_JOYBALLMOTION */
  146. Uint8 which; /**< The joystick device index */
  147. Uint8 ball; /**< The joystick trackball index */
  148. Sint16 xrel; /**< The relative motion in the X direction */
  149. Sint16 yrel; /**< The relative motion in the Y direction */
  150. } SDL_JoyBallEvent;
  151. /** Joystick hat position change event structure */
  152. typedef struct SDL_JoyHatEvent {
  153. Uint8 type; /**< SDL_JOYHATMOTION */
  154. Uint8 which; /**< The joystick device index */
  155. Uint8 hat; /**< The joystick hat index */
  156. Uint8 value; /**< The hat position value:
  157. * SDL_HAT_LEFTUP SDL_HAT_UP SDL_HAT_RIGHTUP
  158. * SDL_HAT_LEFT SDL_HAT_CENTERED SDL_HAT_RIGHT
  159. * SDL_HAT_LEFTDOWN SDL_HAT_DOWN SDL_HAT_RIGHTDOWN
  160. * Note that zero means the POV is centered.
  161. */
  162. } SDL_JoyHatEvent;
  163. /** Joystick button event structure */
  164. typedef struct SDL_JoyButtonEvent {
  165. Uint8 type; /**< SDL_JOYBUTTONDOWN or SDL_JOYBUTTONUP */
  166. Uint8 which; /**< The joystick device index */
  167. Uint8 button; /**< The joystick button index */
  168. Uint8 state; /**< SDL_PRESSED or SDL_RELEASED */
  169. } SDL_JoyButtonEvent;
  170. /** The "window resized" event
  171. * When you get this event, you are responsible for setting a new video
  172. * mode with the new width and height.
  173. */
  174. typedef struct SDL_ResizeEvent {
  175. Uint8 type; /**< SDL_VIDEORESIZE */
  176. int w; /**< New width */
  177. int h; /**< New height */
  178. } SDL_ResizeEvent;
  179. /** The "screen redraw" event */
  180. typedef struct SDL_ExposeEvent {
  181. Uint8 type; /**< SDL_VIDEOEXPOSE */
  182. } SDL_ExposeEvent;
  183. /** The "quit requested" event */
  184. typedef struct SDL_QuitEvent {
  185. Uint8 type; /**< SDL_QUIT */
  186. } SDL_QuitEvent;
  187. /** A user-defined event type */
  188. typedef struct SDL_UserEvent {
  189. Uint8 type; /**< SDL_USEREVENT through SDL_NUMEVENTS-1 */
  190. int code; /**< User defined event code */
  191. void *data1; /**< User defined data pointer */
  192. void *data2; /**< User defined data pointer */
  193. } SDL_UserEvent;
  194. /** If you want to use this event, you should include SDL_syswm.h */
  195. struct SDL_SysWMmsg;
  196. typedef struct SDL_SysWMmsg SDL_SysWMmsg;
  197. typedef struct SDL_SysWMEvent {
  198. Uint8 type;
  199. SDL_SysWMmsg *msg;
  200. } SDL_SysWMEvent;
  201. /** General event structure */
  202. typedef union SDL_Event {
  203. Uint8 type;
  204. SDL_ActiveEvent active;
  205. SDL_KeyboardEvent key;
  206. SDL_MouseMotionEvent motion;
  207. SDL_MouseButtonEvent button;
  208. SDL_JoyAxisEvent jaxis;
  209. SDL_JoyBallEvent jball;
  210. SDL_JoyHatEvent jhat;
  211. SDL_JoyButtonEvent jbutton;
  212. SDL_ResizeEvent resize;
  213. SDL_ExposeEvent expose;
  214. SDL_QuitEvent quit;
  215. SDL_UserEvent user;
  216. SDL_SysWMEvent syswm;
  217. } SDL_Event;
  218. /* Function prototypes */
  219. /** Pumps the event loop, gathering events from the input devices.
  220. * This function updates the event queue and internal input device state.
  221. * This should only be run in the thread that sets the video mode.
  222. */
  223. extern DECLSPEC void SDLCALL SDL_PumpEvents(void);
  224. typedef enum {
  225. SDL_ADDEVENT,
  226. SDL_PEEKEVENT,
  227. SDL_GETEVENT
  228. } SDL_eventaction;
  229. /**
  230. * Checks the event queue for messages and optionally returns them.
  231. *
  232. * If 'action' is SDL_ADDEVENT, up to 'numevents' events will be added to
  233. * the back of the event queue.
  234. * If 'action' is SDL_PEEKEVENT, up to 'numevents' events at the front
  235. * of the event queue, matching 'mask', will be returned and will not
  236. * be removed from the queue.
  237. * If 'action' is SDL_GETEVENT, up to 'numevents' events at the front
  238. * of the event queue, matching 'mask', will be returned and will be
  239. * removed from the queue.
  240. *
  241. * @return
  242. * This function returns the number of events actually stored, or -1
  243. * if there was an error.
  244. *
  245. * This function is thread-safe.
  246. */
  247. extern DECLSPEC int SDLCALL SDL_PeepEvents(SDL_Event *events, int numevents,
  248. SDL_eventaction action, Uint32 mask);
  249. /** Polls for currently pending events, and returns 1 if there are any pending
  250. * events, or 0 if there are none available. If 'event' is not NULL, the next
  251. * event is removed from the queue and stored in that area.
  252. */
  253. extern DECLSPEC int SDLCALL SDL_PollEvent(SDL_Event *event);
  254. /** Waits indefinitely for the next available event, returning 1, or 0 if there
  255. * was an error while waiting for events. If 'event' is not NULL, the next
  256. * event is removed from the queue and stored in that area.
  257. */
  258. extern DECLSPEC int SDLCALL SDL_WaitEvent(SDL_Event *event);
  259. /** Add an event to the event queue.
  260. * This function returns 0 on success, or -1 if the event queue was full
  261. * or there was some other error.
  262. */
  263. extern DECLSPEC int SDLCALL SDL_PushEvent(SDL_Event *event);
  264. /** @name Event Filtering */
  265. /*@{*/
  266. typedef int (SDLCALL *SDL_EventFilter)(const SDL_Event *event);
  267. /**
  268. * This function sets up a filter to process all events before they
  269. * change internal state and are posted to the internal event queue.
  270. *
  271. * The filter is protypted as:
  272. * @code typedef int (SDLCALL *SDL_EventFilter)(const SDL_Event *event); @endcode
  273. *
  274. * If the filter returns 1, then the event will be added to the internal queue.
  275. * If it returns 0, then the event will be dropped from the queue, but the
  276. * internal state will still be updated. This allows selective filtering of
  277. * dynamically arriving events.
  278. *
  279. * @warning Be very careful of what you do in the event filter function, as
  280. * it may run in a different thread!
  281. *
  282. * There is one caveat when dealing with the SDL_QUITEVENT event type. The
  283. * event filter is only called when the window manager desires to close the
  284. * application window. If the event filter returns 1, then the window will
  285. * be closed, otherwise the window will remain open if possible.
  286. * If the quit event is generated by an interrupt signal, it will bypass the
  287. * internal queue and be delivered to the application at the next event poll.
  288. */
  289. extern DECLSPEC void SDLCALL SDL_SetEventFilter(SDL_EventFilter filter);
  290. /**
  291. * Return the current event filter - can be used to "chain" filters.
  292. * If there is no event filter set, this function returns NULL.
  293. */
  294. extern DECLSPEC SDL_EventFilter SDLCALL SDL_GetEventFilter(void);
  295. /*@}*/
  296. /** @name Event State */
  297. /*@{*/
  298. #define SDL_QUERY -1
  299. #define SDL_IGNORE 0
  300. #define SDL_DISABLE 0
  301. #define SDL_ENABLE 1
  302. /*@}*/
  303. /**
  304. * This function allows you to set the state of processing certain events.
  305. * If 'state' is set to SDL_IGNORE, that event will be automatically dropped
  306. * from the event queue and will not event be filtered.
  307. * If 'state' is set to SDL_ENABLE, that event will be processed normally.
  308. * If 'state' is set to SDL_QUERY, SDL_EventState() will return the
  309. * current processing state of the specified event.
  310. */
  311. extern DECLSPEC Uint8 SDLCALL SDL_EventState(Uint8 type, int state);
  312. /* Ends C function definitions when using C++ */
  313. #ifdef __cplusplus
  314. }
  315. #endif
  316. #include "close_code.h"
  317. #endif /* _SDL_events_h */