include/SDL_events.h
author Sam Lantinga <slouken@libsdl.org>
Thu, 25 Mar 2010 01:08:26 -0700
changeset 4429 faa9fc8e7f67
parent 3697 f7b03b6838cb
child 4434 5c64052fb476
permissions -rw-r--r--
General improvements for user custom event registration

* Switched event type to enum (int32)
* Switched polling by mask to polling by type range
* Added SDL_RegisterEvents() to allow dynamic user event registration
* Spread events out to allow inserting new related events without breaking binary compatibility
* Added padding to event structures so they're the same size regardless of 32-bit compiler structure packing settings
* Split SDL_HasEvent() to SDL_HasEvent() for a single event and SDL_HasEvents() for a range of events
* Added SDL_GetEventState() as a shortcut for SDL_EventState(X, SDL_QUERY)
* Added SDL_FlushEvent() and SDL_FlushEvents() to clear events from the event queue
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 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     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /**
    24  *  \file SDL_events.h
    25  *  
    26  *  Include file for SDL event handling.
    27  */
    28 
    29 #ifndef _SDL_events_h
    30 #define _SDL_events_h
    31 
    32 #include "SDL_stdinc.h"
    33 #include "SDL_error.h"
    34 #include "SDL_video.h"
    35 #include "SDL_keyboard.h"
    36 #include "SDL_mouse.h"
    37 #include "SDL_joystick.h"
    38 #include "SDL_quit.h"
    39 
    40 #include "begin_code.h"
    41 /* Set up for C function definitions, even when using C++ */
    42 #ifdef __cplusplus
    43 /* *INDENT-OFF* */
    44 extern "C" {
    45 /* *INDENT-ON* */
    46 #endif
    47 
    48 /* General keyboard/mouse state definitions */
    49 #define SDL_RELEASED	0
    50 #define SDL_PRESSED	1
    51 
    52 /**
    53  * \brief The types of events that can be delivered.
    54  */
    55 typedef enum
    56 {
    57     SDL_FIRSTEVENT     = 0,     /**< Unused (do not remove) */
    58 
    59     /* Application events */
    60     SDL_QUIT           = 0x100, /**< User-requested quit */
    61 
    62     /* Window events */
    63     SDL_WINDOWEVENT    = 0x200, /**< Window state change */
    64     SDL_SYSWMEVENT,             /**< System specific event */
    65 
    66     /* Keyboard events */
    67     SDL_KEYDOWN        = 0x300, /**< Keys pressed */
    68     SDL_KEYUP,                  /**< Keys released */
    69     SDL_TEXTEDITING,            /**< Keyboard text editing (composition) */
    70     SDL_TEXTINPUT,              /**< Keyboard text input */
    71 
    72     /* Mouse events */
    73     SDL_MOUSEMOTION    = 0x400, /**< Mouse moved */
    74     SDL_MOUSEBUTTONDOWN,        /**< Mouse button pressed */
    75     SDL_MOUSEBUTTONUP,          /**< Mouse button released */
    76     SDL_MOUSEWHEEL,             /**< Mouse wheel motion */
    77 
    78     /* Tablet events */
    79     SDL_PROXIMITYIN    = 0x500, /**< Proximity In event */
    80     SDL_PROXIMITYOUT,           /**< Proximity Out event */
    81 
    82     /* Joystick events */
    83     SDL_JOYAXISMOTION  = 0x600, /**< Joystick axis motion */
    84     SDL_JOYBALLMOTION,          /**< Joystick trackball motion */
    85     SDL_JOYHATMOTION,           /**< Joystick hat position change */
    86     SDL_JOYBUTTONDOWN,          /**< Joystick button pressed */
    87     SDL_JOYBUTTONUP,            /**< Joystick button released */
    88 
    89     /* Obsolete events */
    90     SDL_EVENT_COMPAT1 = 0x7000, /**< SDL 1.2 events for compatibility */
    91     SDL_EVENT_COMPAT2,
    92     SDL_EVENT_COMPAT3,
    93 
    94     /** Events ::SDL_USEREVENT through ::SDL_LASTEVENT are for your use,
    95      *  and should be allocated with SDL_RegisterEvents()
    96      */
    97     SDL_USEREVENT    = 0x8000,
    98 
    99     /**
   100      *  This last event is only for bounding internal arrays
   101      */
   102     SDL_LASTEVENT    = 0xFFFF
   103 } SDL_EventType;
   104 
   105 /**
   106  *  \brief Window state change event data (event.window.*)
   107  */
   108 typedef struct SDL_WindowEvent
   109 {
   110     Uint32 type;        /**< ::SDL_WINDOWEVENT */
   111     Uint32 windowID;    /**< The associated window */
   112     Uint8 event;        /**< ::SDL_WindowEventID */
   113     Uint8 padding1;
   114     Uint8 padding2;
   115     Uint8 padding3;
   116     int data1;          /**< event dependent data */
   117     int data2;          /**< event dependent data */
   118 } SDL_WindowEvent;
   119 
   120 /**
   121  *  \brief Keyboard button event structure (event.key.*)
   122  */
   123 typedef struct SDL_KeyboardEvent
   124 {
   125     Uint32 type;        /**< ::SDL_KEYDOWN or ::SDL_KEYUP */
   126     Uint32 windowID;    /**< The window with keyboard focus, if any */
   127     Uint8 which;        /**< The keyboard device index */
   128     Uint8 state;        /**< ::SDL_PRESSED or ::SDL_RELEASED */
   129     Uint8 padding1;
   130     Uint8 padding2;
   131     SDL_keysym keysym;  /**< The key that was pressed or released */
   132 } SDL_KeyboardEvent;
   133 
   134 #define SDL_TEXTEDITINGEVENT_TEXT_SIZE (32)
   135 /**
   136  *  \brief Keyboard text editing event structure (event.edit.*)
   137  */
   138 typedef struct SDL_TextEditingEvent
   139 {
   140     Uint32 type;                                /**< ::SDL_TEXTEDITING */
   141     char text[SDL_TEXTEDITINGEVENT_TEXT_SIZE];  /**< The editing text */
   142     int start;                                  /**< The start cursor of selected editing text */
   143     int length;                                 /**< The length of selected editing text */
   144 } SDL_TextEditingEvent;
   145 
   146 
   147 #define SDL_TEXTINPUTEVENT_TEXT_SIZE (32)
   148 /**
   149  *  \brief Keyboard text input event structure (event.text.*)
   150  */
   151 typedef struct SDL_TextInputEvent
   152 {
   153     Uint32 type;                              /**< ::SDL_TEXTINPUT */
   154     Uint32 windowID;                          /**< The window with keyboard focus, if any */
   155     Uint8 which;                              /**< The keyboard device index */
   156     Uint8 padding1;
   157     Uint8 padding2;
   158     Uint8 padding3;
   159     char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];  /**< The input text */
   160 } SDL_TextInputEvent;
   161 
   162 /**
   163  *  \brief Mouse motion event structure (event.motion.*)
   164  */
   165 typedef struct SDL_MouseMotionEvent
   166 {
   167     Uint32 type;        /**< ::SDL_MOUSEMOTION */
   168     Uint32 windowID;    /**< The window with mouse focus, if any */
   169     Uint8 which;        /**< The mouse device index */
   170     Uint8 state;        /**< The current button state */
   171     Uint8 padding1;
   172     Uint8 padding2;
   173     int x;              /**< X coordinate, relative to window */
   174     int y;              /**< Y coordinate, relative to window */
   175     int z;              /**< Z coordinate, for future use */
   176     int pressure;       /**< Pressure reported by tablets */
   177     int pressure_max;   /**< Maximum value of the pressure reported by the device */
   178     int pressure_min;   /**< Minimum value of the pressure reported by the device */
   179     int rotation;       /**< For future use */
   180     int tilt_x;         /**< For future use */
   181     int tilt_y;         /**< For future use */
   182     int cursor;         /**< The cursor being used in the event */
   183     int xrel;           /**< The relative motion in the X direction */
   184     int yrel;           /**< The relative motion in the Y direction */
   185 } SDL_MouseMotionEvent;
   186 
   187 /**
   188  *  \brief Mouse button event structure (event.button.*)
   189  */
   190 typedef struct SDL_MouseButtonEvent
   191 {
   192     Uint32 type;        /**< ::SDL_MOUSEBUTTONDOWN or ::SDL_MOUSEBUTTONUP */
   193     Uint32 windowID;    /**< The window with mouse focus, if any */
   194     Uint8 which;        /**< The mouse device index */
   195     Uint8 button;       /**< The mouse button index */
   196     Uint8 state;        /**< ::SDL_PRESSED or ::SDL_RELEASED */
   197     Uint8 padding1;
   198     int x;              /**< X coordinate, relative to window */
   199     int y;              /**< Y coordinate, relative to window */
   200 } SDL_MouseButtonEvent;
   201 
   202 /**
   203  *  \brief Mouse wheel event structure (event.wheel.*)
   204  */
   205 typedef struct SDL_MouseWheelEvent
   206 {
   207     Uint32 type;        /**< ::SDL_MOUSEWHEEL */
   208     Uint32 windowID;    /**< The window with mouse focus, if any */
   209     Uint8 which;        /**< The mouse device index */
   210     Uint8 padding1;
   211     Uint8 padding2;
   212     Uint8 padding3;
   213     int x;              /**< The amount scrolled horizontally */
   214     int y;              /**< The amount scrolled vertically */
   215 } SDL_MouseWheelEvent;
   216 
   217 /**
   218  * \brief Tablet pen proximity event
   219  */
   220 typedef struct SDL_ProximityEvent
   221 {
   222     Uint32 type;        /**< ::SDL_PROXIMITYIN or ::SDL_PROXIMITYOUT */
   223     Uint32 windowID;    /**< The associated window */
   224     Uint8 which;
   225     Uint8 padding1;
   226     Uint8 padding2;
   227     Uint8 padding3;
   228     int cursor;
   229     int x;
   230     int y;
   231 } SDL_ProximityEvent;
   232 
   233 /**
   234  *  \brief Joystick axis motion event structure (event.jaxis.*)
   235  */
   236 typedef struct SDL_JoyAxisEvent
   237 {
   238     Uint32 type;        /**< ::SDL_JOYAXISMOTION */
   239     Uint8 which;        /**< The joystick device index */
   240     Uint8 axis;         /**< The joystick axis index */
   241     Uint8 padding1;
   242     Uint8 padding2;
   243     int value;          /**< The axis value (range: -32768 to 32767) */
   244 } SDL_JoyAxisEvent;
   245 
   246 /**
   247  *  \brief Joystick trackball motion event structure (event.jball.*)
   248  */
   249 typedef struct SDL_JoyBallEvent
   250 {
   251     Uint32 type;        /**< ::SDL_JOYBALLMOTION */
   252     Uint8 which;        /**< The joystick device index */
   253     Uint8 ball;         /**< The joystick trackball index */
   254     Uint8 padding1;
   255     Uint8 padding2;
   256     int xrel;           /**< The relative motion in the X direction */
   257     int yrel;           /**< The relative motion in the Y direction */
   258 } SDL_JoyBallEvent;
   259 
   260 /**
   261  *  \brief Joystick hat position change event structure (event.jhat.*)
   262  */
   263 typedef struct SDL_JoyHatEvent
   264 {
   265     Uint32 type;        /**< ::SDL_JOYHATMOTION */
   266     Uint8 which;        /**< The joystick device index */
   267     Uint8 hat;          /**< The joystick hat index */
   268     Uint8 value;        /**< The hat position value.
   269                          *   \sa ::SDL_HAT_LEFTUP ::SDL_HAT_UP ::SDL_HAT_RIGHTUP
   270                          *   \sa ::SDL_HAT_LEFT ::SDL_HAT_CENTERED ::SDL_HAT_RIGHT
   271                          *   \sa ::SDL_HAT_LEFTDOWN ::SDL_HAT_DOWN ::SDL_HAT_RIGHTDOWN
   272                          *   
   273                          *   Note that zero means the POV is centered.
   274                          */
   275     Uint8 padding1;
   276 } SDL_JoyHatEvent;
   277 
   278 /**
   279  *  \brief Joystick button event structure (event.jbutton.*)
   280  */
   281 typedef struct SDL_JoyButtonEvent
   282 {
   283     Uint32 type;        /**< ::SDL_JOYBUTTONDOWN or ::SDL_JOYBUTTONUP */
   284     Uint8 which;        /**< The joystick device index */
   285     Uint8 button;       /**< The joystick button index */
   286     Uint8 state;        /**< ::SDL_PRESSED or ::SDL_RELEASED */
   287     Uint8 padding1;
   288 } SDL_JoyButtonEvent;
   289 
   290 /**
   291  *  \brief The "quit requested" event
   292  */
   293 typedef struct SDL_QuitEvent
   294 {
   295     Uint32 type;        /**< ::SDL_QUIT */
   296 } SDL_QuitEvent;
   297 
   298 /**
   299  *  \brief A user-defined event type (event.user.*)
   300  */
   301 typedef struct SDL_UserEvent
   302 {
   303     Uint32 type;        /**< ::SDL_USEREVENT through ::SDL_NUMEVENTS-1 */
   304     Uint32 windowID;    /**< The associated window if any */
   305     int code;           /**< User defined event code */
   306     void *data1;        /**< User defined data pointer */
   307     void *data2;        /**< User defined data pointer */
   308 } SDL_UserEvent;
   309 
   310 struct SDL_SysWMmsg;
   311 typedef struct SDL_SysWMmsg SDL_SysWMmsg;
   312 
   313 /**
   314  *  \brief A video driver dependent system event (event.syswm.*)
   315  *  
   316  *  \note If you want to use this event, you should include SDL_syswm.h.
   317  */
   318 typedef struct SDL_SysWMEvent
   319 {
   320     Uint32 type;        /**< ::SDL_SYSWMEVENT */
   321     SDL_SysWMmsg *msg;  /**< driver dependent data, defined in SDL_syswm.h */
   322 } SDL_SysWMEvent;
   323 
   324 #ifndef SDL_NO_COMPAT
   325 /**
   326  *  \addtogroup Compatibility 
   327  */
   328 /*@{*/
   329 
   330 /**
   331  *  \name Typedefs for backwards compatibility
   332  */
   333 /*@{*/
   334 typedef struct SDL_ActiveEvent
   335 {
   336     Uint32 type;
   337     Uint8 gain;
   338     Uint8 state;
   339 } SDL_ActiveEvent;
   340 
   341 typedef struct SDL_ResizeEvent
   342 {
   343     Uint32 type;
   344     int w;
   345     int h;
   346 } SDL_ResizeEvent;
   347 /*@}*/
   348 
   349 /*@}*//*Compatibility*/
   350 #endif
   351 
   352 /**
   353  *  \brief General event structure
   354  */
   355 typedef union SDL_Event
   356 {
   357     Uint32 type;                    /**< Event type, shared with all events */
   358     SDL_WindowEvent window;         /**< Window event data */
   359     SDL_KeyboardEvent key;          /**< Keyboard event data */
   360     SDL_TextEditingEvent edit;      /**< Text editing event data */
   361     SDL_TextInputEvent text;        /**< Text input event data */
   362     SDL_MouseMotionEvent motion;    /**< Mouse motion event data */
   363     SDL_MouseButtonEvent button;    /**< Mouse button event data */
   364     SDL_MouseWheelEvent wheel;      /**< Mouse wheel event data */
   365     SDL_JoyAxisEvent jaxis;         /**< Joystick axis event data */
   366     SDL_JoyBallEvent jball;         /**< Joystick ball event data */
   367     SDL_JoyHatEvent jhat;           /**< Joystick hat event data */
   368     SDL_JoyButtonEvent jbutton;     /**< Joystick button event data */
   369     SDL_QuitEvent quit;             /**< Quit request event data */
   370     SDL_UserEvent user;             /**< Custom event data */
   371     SDL_SysWMEvent syswm;           /**< System dependent window event data */
   372     SDL_ProximityEvent proximity;   /**< Proximity In or Out event */
   373 
   374     /** Temporarily here for backwards compatibility */
   375     /*@{*/
   376 #ifndef SDL_NO_COMPAT
   377     SDL_ActiveEvent active;
   378     SDL_ResizeEvent resize;
   379 #endif
   380     /*@}*/
   381 } SDL_Event;
   382 
   383 
   384 /* Function prototypes */
   385 
   386 /**
   387  *  Pumps the event loop, gathering events from the input devices.
   388  *  
   389  *  This function updates the event queue and internal input device state.
   390  *  
   391  *  This should only be run in the thread that sets the video mode.
   392  */
   393 extern DECLSPEC void SDLCALL SDL_PumpEvents(void);
   394 
   395 /*@{*/
   396 typedef enum
   397 {
   398     SDL_ADDEVENT,
   399     SDL_PEEKEVENT,
   400     SDL_GETEVENT
   401 } SDL_eventaction;
   402 
   403 /**
   404  *  Checks the event queue for messages and optionally returns them.
   405  *  
   406  *  If \c action is ::SDL_ADDEVENT, up to \c numevents events will be added to
   407  *  the back of the event queue.
   408  *  
   409  *  If \c action is ::SDL_PEEKEVENT, up to \c numevents events at the front
   410  *  of the event queue, matching \c mask, will be returned and will not
   411  *  be removed from the queue.
   412  *  
   413  *  If \c action is ::SDL_GETEVENT, up to \c numevents events at the front 
   414  *  of the event queue, matching \c mask, will be returned and will be
   415  *  removed from the queue.
   416  *  
   417  *  \return The number of events actually stored, or -1 if there was an error.
   418  *  
   419  *  This function is thread-safe.
   420  */
   421 extern DECLSPEC int SDLCALL SDL_PeepEvents(SDL_Event * events, int numevents,
   422                                            SDL_eventaction action,
   423                                            Uint32 minType, Uint32 maxType);
   424 /*@}*/
   425 
   426 /**
   427  *  Checks to see if certain event types are in the event queue.
   428  */
   429 extern DECLSPEC SDL_bool SDLCALL SDL_HasEvent(Uint32 type);
   430 extern DECLSPEC SDL_bool SDLCALL SDL_HasEvents(Uint32 minType, Uint32 maxType);
   431 
   432 /**
   433  *  This function clears events from the event queue
   434  */
   435 extern DECLSPEC void SDLCALL SDL_FlushEvent(Uint32 type);
   436 extern DECLSPEC void SDLCALL SDL_FlushEvents(Uint32 minType, Uint32 maxType);
   437 
   438 /**
   439  *  \brief Polls for currently pending events.
   440  *  
   441  *  \return 1 if there are any pending events, or 0 if there are none available.
   442  *  
   443  *  \param event If not NULL, the next event is removed from the queue and 
   444  *               stored in that area.
   445  */
   446 extern DECLSPEC int SDLCALL SDL_PollEvent(SDL_Event * event);
   447 
   448 /**
   449  *  \brief Waits indefinitely for the next available event.
   450  *  
   451  *  \return 1, or 0 if there was an error while waiting for events.
   452  *   
   453  *  \param event If not NULL, the next event is removed from the queue and 
   454  *               stored in that area.
   455  */
   456 extern DECLSPEC int SDLCALL SDL_WaitEvent(SDL_Event * event);
   457 
   458 /**
   459  *  \brief Waits until the specified timeout (in milliseconds) for the next 
   460  *         available event.
   461  *  
   462  *  \return 1, or 0 if there was an error while waiting for events.
   463  *  
   464  *  \param event If not NULL, the next event is removed from the queue and 
   465  *               stored in that area.
   466  */
   467 extern DECLSPEC int SDLCALL SDL_WaitEventTimeout(SDL_Event * event,
   468                                                  int timeout);
   469 
   470 /**
   471  *  \brief Add an event to the event queue.
   472  *  
   473  *  \return 1 on success, 0 if the event was filtered, or -1 if the event queue 
   474  *          was full or there was some other error.
   475  */
   476 extern DECLSPEC int SDLCALL SDL_PushEvent(SDL_Event * event);
   477 
   478 typedef int (SDLCALL * SDL_EventFilter) (void *userdata, SDL_Event * event);
   479 
   480 /**
   481  *  Sets up a filter to process all events before they change internal state and
   482  *  are posted to the internal event queue.
   483  *  
   484  *  The filter is protypted as:
   485  *  \code
   486  *      int SDL_EventFilter(void *userdata, SDL_Event * event);
   487  *  \endcode
   488  *
   489  *  If the filter returns 1, then the event will be added to the internal queue.
   490  *  If it returns 0, then the event will be dropped from the queue, but the 
   491  *  internal state will still be updated.  This allows selective filtering of
   492  *  dynamically arriving events.
   493  *  
   494  *  \warning  Be very careful of what you do in the event filter function, as 
   495  *            it may run in a different thread!
   496  *  
   497  *  There is one caveat when dealing with the ::SDL_QUITEVENT event type.  The
   498  *  event filter is only called when the window manager desires to close the
   499  *  application window.  If the event filter returns 1, then the window will
   500  *  be closed, otherwise the window will remain open if possible.
   501  *
   502  *  If the quit event is generated by an interrupt signal, it will bypass the
   503  *  internal queue and be delivered to the application at the next event poll.
   504  */
   505 extern DECLSPEC void SDLCALL SDL_SetEventFilter(SDL_EventFilter filter,
   506                                                 void *userdata);
   507 
   508 /**
   509  *  Return the current event filter - can be used to "chain" filters.
   510  *  If there is no event filter set, this function returns SDL_FALSE.
   511  */
   512 extern DECLSPEC SDL_bool SDLCALL SDL_GetEventFilter(SDL_EventFilter * filter,
   513                                                     void **userdata);
   514 
   515 /**
   516  *  Run the filter function on the current event queue, removing any
   517  *  events for which the filter returns 0.
   518  */
   519 extern DECLSPEC void SDLCALL SDL_FilterEvents(SDL_EventFilter filter,
   520                                               void *userdata);
   521 
   522 /*@{*/
   523 #define SDL_QUERY	-1
   524 #define SDL_IGNORE	 0
   525 #define SDL_DISABLE	 0
   526 #define SDL_ENABLE	 1
   527 
   528 /**
   529  *  This function allows you to set the state of processing certain events.
   530  *   - If \c state is set to ::SDL_IGNORE, that event will be automatically 
   531  *     dropped from the event queue and will not event be filtered.
   532  *   - If \c state is set to ::SDL_ENABLE, that event will be processed 
   533  *     normally.
   534  *   - If \c state is set to ::SDL_QUERY, SDL_EventState() will return the 
   535  *     current processing state of the specified event.
   536  */
   537 extern DECLSPEC Uint8 SDLCALL SDL_EventState(Uint32 type, int state);
   538 /*@}*/
   539 #define SDL_GetEventState(type) SDL_EventState(type, SDL_QUERY)
   540 
   541 /**
   542  *  This function allocates a set of user-defined events, and returns
   543  *  the beginning event number for that set of events.
   544  *
   545  *  If there aren't enough user-defined events left, this function
   546  *  returns (Uint32)-1
   547  */
   548 extern DECLSPEC Uint32 SDLCALL SDL_RegisterEvents(int numevents);
   549 
   550 /* Ends C function definitions when using C++ */
   551 #ifdef __cplusplus
   552 /* *INDENT-OFF* */
   553 }
   554 /* *INDENT-ON* */
   555 #endif
   556 #include "close_code.h"
   557 
   558 #endif /* _SDL_events_h */
   559 
   560 /* vi: set ts=4 sw=4 expandtab: */