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