diff include/SDL_events.h @ 4429:faa9fc8e7f67

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
author Sam Lantinga <slouken@libsdl.org>
date Thu, 25 Mar 2010 01:08:26 -0700
parents f7b03b6838cb
children 5c64052fb476
line wrap: on
line diff
--- a/include/SDL_events.h	Wed Mar 10 15:07:20 2010 +0000
+++ b/include/SDL_events.h	Thu Mar 25 01:08:26 2010 -0700
@@ -54,83 +54,67 @@
  */
 typedef enum
 {
-    SDL_NOEVENT = 0,            /**< Unused (do not remove) */
-    SDL_WINDOWEVENT,            /**< Window state change */
-    SDL_KEYDOWN,                /**< Keys pressed */
+    SDL_FIRSTEVENT     = 0,     /**< Unused (do not remove) */
+
+    /* Application events */
+    SDL_QUIT           = 0x100, /**< User-requested quit */
+
+    /* Window events */
+    SDL_WINDOWEVENT    = 0x200, /**< Window state change */
+    SDL_SYSWMEVENT,             /**< System specific event */
+
+    /* Keyboard events */
+    SDL_KEYDOWN        = 0x300, /**< Keys pressed */
     SDL_KEYUP,                  /**< Keys released */
     SDL_TEXTEDITING,            /**< Keyboard text editing (composition) */
     SDL_TEXTINPUT,              /**< Keyboard text input */
-    SDL_MOUSEMOTION,            /**< Mouse moved */
+
+    /* Mouse events */
+    SDL_MOUSEMOTION    = 0x400, /**< Mouse moved */
     SDL_MOUSEBUTTONDOWN,        /**< Mouse button pressed */
     SDL_MOUSEBUTTONUP,          /**< Mouse button released */
     SDL_MOUSEWHEEL,             /**< Mouse wheel motion */
-    SDL_JOYAXISMOTION,          /**< Joystick axis motion */
+
+    /* Tablet events */
+    SDL_PROXIMITYIN    = 0x500, /**< Proximity In event */
+    SDL_PROXIMITYOUT,           /**< Proximity Out event */
+
+    /* Joystick events */
+    SDL_JOYAXISMOTION  = 0x600, /**< Joystick axis motion */
     SDL_JOYBALLMOTION,          /**< Joystick trackball motion */
     SDL_JOYHATMOTION,           /**< Joystick hat position change */
     SDL_JOYBUTTONDOWN,          /**< Joystick button pressed */
     SDL_JOYBUTTONUP,            /**< Joystick button released */
-    SDL_QUIT,                   /**< User-requested quit */
-    SDL_SYSWMEVENT,             /**< System specific event */
-    SDL_PROXIMITYIN,            /**< Proximity In event */
-    SDL_PROXIMITYOUT,           /**< Proximity Out event */
-    SDL_EVENT_RESERVED1,        /**< Reserved for future use... */
-    SDL_EVENT_RESERVED2,        /**< Reserved for future use... */
-    SDL_EVENT_RESERVED3,        /**< Reserved for future use... */
-    /** Events ::SDL_USEREVENT through ::SDL_MAXEVENTS-1 are for your use */
-    SDL_USEREVENT = 24,
+
+    /* Obsolete events */
+    SDL_EVENT_COMPAT1 = 0x7000, /**< SDL 1.2 events for compatibility */
+    SDL_EVENT_COMPAT2,
+    SDL_EVENT_COMPAT3,
+
+    /** Events ::SDL_USEREVENT through ::SDL_LASTEVENT are for your use,
+     *  and should be allocated with SDL_RegisterEvents()
+     */
+    SDL_USEREVENT    = 0x8000,
+
     /**
      *  This last event is only for bounding internal arrays
-     *  It is the number of bits in the event mask datatype -- Uint32
      */
-    SDL_NUMEVENTS = 32
+    SDL_LASTEVENT    = 0xFFFF
 } SDL_EventType;
 
-/*@{*/
-#define SDL_EVENTMASK(X)	(1<<(X))
-/**
- * \brief Predefined event masks
- */
-typedef enum
-{
-    SDL_WINDOWEVENTMASK = SDL_EVENTMASK(SDL_WINDOWEVENT),
-    SDL_KEYDOWNMASK = SDL_EVENTMASK(SDL_KEYDOWN),
-    SDL_KEYUPMASK = SDL_EVENTMASK(SDL_KEYUP),
-    SDL_KEYEVENTMASK = SDL_EVENTMASK(SDL_KEYDOWN) | SDL_EVENTMASK(SDL_KEYUP),
-    SDL_TEXTEDITINGMASK = SDL_EVENTMASK(SDL_TEXTEDITING),
-    SDL_TEXTINPUTMASK = SDL_EVENTMASK(SDL_TEXTINPUT),
-    SDL_MOUSEMOTIONMASK = SDL_EVENTMASK(SDL_MOUSEMOTION),
-    SDL_MOUSEBUTTONDOWNMASK = SDL_EVENTMASK(SDL_MOUSEBUTTONDOWN),
-    SDL_MOUSEBUTTONUPMASK = SDL_EVENTMASK(SDL_MOUSEBUTTONUP),
-    SDL_MOUSEWHEELMASK = SDL_EVENTMASK(SDL_MOUSEWHEEL),
-    SDL_MOUSEEVENTMASK = SDL_EVENTMASK(SDL_MOUSEMOTION) |
-        SDL_EVENTMASK(SDL_MOUSEBUTTONDOWN) | SDL_EVENTMASK(SDL_MOUSEBUTTONUP),
-    SDL_JOYAXISMOTIONMASK = SDL_EVENTMASK(SDL_JOYAXISMOTION),
-    SDL_JOYBALLMOTIONMASK = SDL_EVENTMASK(SDL_JOYBALLMOTION),
-    SDL_JOYHATMOTIONMASK = SDL_EVENTMASK(SDL_JOYHATMOTION),
-    SDL_JOYBUTTONDOWNMASK = SDL_EVENTMASK(SDL_JOYBUTTONDOWN),
-    SDL_JOYBUTTONUPMASK = SDL_EVENTMASK(SDL_JOYBUTTONUP),
-    SDL_JOYEVENTMASK = SDL_EVENTMASK(SDL_JOYAXISMOTION) |
-        SDL_EVENTMASK(SDL_JOYBALLMOTION) |
-        SDL_EVENTMASK(SDL_JOYHATMOTION) |
-        SDL_EVENTMASK(SDL_JOYBUTTONDOWN) | SDL_EVENTMASK(SDL_JOYBUTTONUP),
-    SDL_QUITMASK = SDL_EVENTMASK(SDL_QUIT),
-    SDL_SYSWMEVENTMASK = SDL_EVENTMASK(SDL_SYSWMEVENT),
-    SDL_PROXIMITYINMASK = SDL_EVENTMASK(SDL_PROXIMITYIN),
-    SDL_PROXIMITYOUTMASK = SDL_EVENTMASK(SDL_PROXIMITYOUT)
-} SDL_EventMask;
-#define SDL_ALLEVENTS		0xFFFFFFFF
-/*@}*/
-
 /**
  *  \brief Window state change event data (event.window.*)
  */
 typedef struct SDL_WindowEvent
 {
-    Uint8 type;             /**< ::SDL_WINDOWEVENT */
-    Uint32 windowID;        /**< The associated window */
-    Uint8 event;            /**< ::SDL_WindowEventID */
-    int data1;              /**< event dependent data */
-    int data2;              /**< event dependent data */
+    Uint32 type;        /**< ::SDL_WINDOWEVENT */
+    Uint32 windowID;    /**< The associated window */
+    Uint8 event;        /**< ::SDL_WindowEventID */
+    Uint8 padding1;
+    Uint8 padding2;
+    Uint8 padding3;
+    int data1;          /**< event dependent data */
+    int data2;          /**< event dependent data */
 } SDL_WindowEvent;
 
 /**
@@ -138,11 +122,13 @@
  */
 typedef struct SDL_KeyboardEvent
 {
-    Uint8 type;             /**< ::SDL_KEYDOWN or ::SDL_KEYUP */
-    Uint32 windowID;        /**< The window with keyboard focus, if any */
-    Uint8 which;            /**< The keyboard device index */
-    Uint8 state;            /**< ::SDL_PRESSED or ::SDL_RELEASED */
-    SDL_keysym keysym;      /**< The key that was pressed or released */
+    Uint32 type;        /**< ::SDL_KEYDOWN or ::SDL_KEYUP */
+    Uint32 windowID;    /**< The window with keyboard focus, if any */
+    Uint8 which;        /**< The keyboard device index */
+    Uint8 state;        /**< ::SDL_PRESSED or ::SDL_RELEASED */
+    Uint8 padding1;
+    Uint8 padding2;
+    SDL_keysym keysym;  /**< The key that was pressed or released */
 } SDL_KeyboardEvent;
 
 #define SDL_TEXTEDITINGEVENT_TEXT_SIZE (32)
@@ -151,7 +137,7 @@
  */
 typedef struct SDL_TextEditingEvent
 {
-    Uint8 type;                                 /**< ::SDL_TEXTEDITING */
+    Uint32 type;                                /**< ::SDL_TEXTEDITING */
     char text[SDL_TEXTEDITINGEVENT_TEXT_SIZE];  /**< The editing text */
     int start;                                  /**< The start cursor of selected editing text */
     int length;                                 /**< The length of selected editing text */
@@ -164,9 +150,12 @@
  */
 typedef struct SDL_TextInputEvent
 {
-    Uint8 type;                               /**< ::SDL_TEXTINPUT */
+    Uint32 type;                              /**< ::SDL_TEXTINPUT */
     Uint32 windowID;                          /**< The window with keyboard focus, if any */
     Uint8 which;                              /**< The keyboard device index */
+    Uint8 padding1;
+    Uint8 padding2;
+    Uint8 padding3;
     char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];  /**< The input text */
 } SDL_TextInputEvent;
 
@@ -175,22 +164,24 @@
  */
 typedef struct SDL_MouseMotionEvent
 {
-    Uint8 type;             /**< ::SDL_MOUSEMOTION */
-    Uint32 windowID;        /**< The window with mouse focus, if any */
-    Uint8 which;            /**< The mouse device index */
-    Uint8 state;            /**< The current button state */
-    int x;                  /**< X coordinate, relative to window */
-    int y;                  /**< Y coordinate, relative to window */
-    int z;                  /**< Z coordinate, for future use */
-    int pressure;           /**< Pressure reported by tablets */
-    int pressure_max;       /**< Maximum value of the pressure reported by the device */
-    int pressure_min;       /**< Minimum value of the pressure reported by the device */
-    int rotation;           /**< For future use */
-    int tilt_x;             /**< For future use */
-    int tilt_y;             /**< For future use */
-    int cursor;             /**< The cursor being used in the event */
-    int xrel;               /**< The relative motion in the X direction */
-    int yrel;               /**< The relative motion in the Y direction */
+    Uint32 type;        /**< ::SDL_MOUSEMOTION */
+    Uint32 windowID;    /**< The window with mouse focus, if any */
+    Uint8 which;        /**< The mouse device index */
+    Uint8 state;        /**< The current button state */
+    Uint8 padding1;
+    Uint8 padding2;
+    int x;              /**< X coordinate, relative to window */
+    int y;              /**< Y coordinate, relative to window */
+    int z;              /**< Z coordinate, for future use */
+    int pressure;       /**< Pressure reported by tablets */
+    int pressure_max;   /**< Maximum value of the pressure reported by the device */
+    int pressure_min;   /**< Minimum value of the pressure reported by the device */
+    int rotation;       /**< For future use */
+    int tilt_x;         /**< For future use */
+    int tilt_y;         /**< For future use */
+    int cursor;         /**< The cursor being used in the event */
+    int xrel;           /**< The relative motion in the X direction */
+    int yrel;           /**< The relative motion in the Y direction */
 } SDL_MouseMotionEvent;
 
 /**
@@ -198,13 +189,14 @@
  */
 typedef struct SDL_MouseButtonEvent
 {
-    Uint8 type;             /**< ::SDL_MOUSEBUTTONDOWN or ::SDL_MOUSEBUTTONUP */
-    Uint32 windowID;        /**< The window with mouse focus, if any */
-    Uint8 which;            /**< The mouse device index */
-    Uint8 button;           /**< The mouse button index */
-    Uint8 state;            /**< ::SDL_PRESSED or ::SDL_RELEASED */
-    int x;                  /**< X coordinate, relative to window */
-    int y;                  /**< Y coordinate, relative to window */
+    Uint32 type;        /**< ::SDL_MOUSEBUTTONDOWN or ::SDL_MOUSEBUTTONUP */
+    Uint32 windowID;    /**< The window with mouse focus, if any */
+    Uint8 which;        /**< The mouse device index */
+    Uint8 button;       /**< The mouse button index */
+    Uint8 state;        /**< ::SDL_PRESSED or ::SDL_RELEASED */
+    Uint8 padding1;
+    int x;              /**< X coordinate, relative to window */
+    int y;              /**< Y coordinate, relative to window */
 } SDL_MouseButtonEvent;
 
 /**
@@ -212,21 +204,42 @@
  */
 typedef struct SDL_MouseWheelEvent
 {
-    Uint8 type;             /**< ::SDL_MOUSEWHEEL */
-    Uint32 windowID;        /**< The window with mouse focus, if any */
-    Uint8 which;            /**< The mouse device index */
-    int x;                  /**< The amount scrolled horizontally */
-    int y;                  /**< The amount scrolled vertically */
+    Uint32 type;        /**< ::SDL_MOUSEWHEEL */
+    Uint32 windowID;    /**< The window with mouse focus, if any */
+    Uint8 which;        /**< The mouse device index */
+    Uint8 padding1;
+    Uint8 padding2;
+    Uint8 padding3;
+    int x;              /**< The amount scrolled horizontally */
+    int y;              /**< The amount scrolled vertically */
 } SDL_MouseWheelEvent;
 
 /**
+ * \brief Tablet pen proximity event
+ */
+typedef struct SDL_ProximityEvent
+{
+    Uint32 type;        /**< ::SDL_PROXIMITYIN or ::SDL_PROXIMITYOUT */
+    Uint32 windowID;    /**< The associated window */
+    Uint8 which;
+    Uint8 padding1;
+    Uint8 padding2;
+    Uint8 padding3;
+    int cursor;
+    int x;
+    int y;
+} SDL_ProximityEvent;
+
+/**
  *  \brief Joystick axis motion event structure (event.jaxis.*)
  */
 typedef struct SDL_JoyAxisEvent
 {
-    Uint8 type;         /**< ::SDL_JOYAXISMOTION */
+    Uint32 type;        /**< ::SDL_JOYAXISMOTION */
     Uint8 which;        /**< The joystick device index */
     Uint8 axis;         /**< The joystick axis index */
+    Uint8 padding1;
+    Uint8 padding2;
     int value;          /**< The axis value (range: -32768 to 32767) */
 } SDL_JoyAxisEvent;
 
@@ -235,9 +248,11 @@
  */
 typedef struct SDL_JoyBallEvent
 {
-    Uint8 type;         /**< ::SDL_JOYBALLMOTION */
+    Uint32 type;        /**< ::SDL_JOYBALLMOTION */
     Uint8 which;        /**< The joystick device index */
     Uint8 ball;         /**< The joystick trackball index */
+    Uint8 padding1;
+    Uint8 padding2;
     int xrel;           /**< The relative motion in the X direction */
     int yrel;           /**< The relative motion in the Y direction */
 } SDL_JoyBallEvent;
@@ -247,7 +262,7 @@
  */
 typedef struct SDL_JoyHatEvent
 {
-    Uint8 type;         /**< ::SDL_JOYHATMOTION */
+    Uint32 type;        /**< ::SDL_JOYHATMOTION */
     Uint8 which;        /**< The joystick device index */
     Uint8 hat;          /**< The joystick hat index */
     Uint8 value;        /**< The hat position value.
@@ -257,6 +272,7 @@
                          *   
                          *   Note that zero means the POV is centered.
                          */
+    Uint8 padding1;
 } SDL_JoyHatEvent;
 
 /**
@@ -264,10 +280,11 @@
  */
 typedef struct SDL_JoyButtonEvent
 {
-    Uint8 type;         /**< ::SDL_JOYBUTTONDOWN or ::SDL_JOYBUTTONUP */
+    Uint32 type;        /**< ::SDL_JOYBUTTONDOWN or ::SDL_JOYBUTTONUP */
     Uint8 which;        /**< The joystick device index */
     Uint8 button;       /**< The joystick button index */
     Uint8 state;        /**< ::SDL_PRESSED or ::SDL_RELEASED */
+    Uint8 padding1;
 } SDL_JoyButtonEvent;
 
 /**
@@ -275,7 +292,7 @@
  */
 typedef struct SDL_QuitEvent
 {
-    Uint8 type;         /**< ::SDL_QUIT */
+    Uint32 type;        /**< ::SDL_QUIT */
 } SDL_QuitEvent;
 
 /**
@@ -283,11 +300,11 @@
  */
 typedef struct SDL_UserEvent
 {
-    Uint8 type;             /**< ::SDL_USEREVENT through ::SDL_NUMEVENTS-1 */
-    Uint32 windowID;        /**< The associated window if any*/
-    int code;               /**< User defined event code */
-    void *data1;            /**< User defined data pointer */
-    void *data2;            /**< User defined data pointer */
+    Uint32 type;        /**< ::SDL_USEREVENT through ::SDL_NUMEVENTS-1 */
+    Uint32 windowID;    /**< The associated window if any */
+    int code;           /**< User defined event code */
+    void *data1;        /**< User defined data pointer */
+    void *data2;        /**< User defined data pointer */
 } SDL_UserEvent;
 
 struct SDL_SysWMmsg;
@@ -300,20 +317,10 @@
  */
 typedef struct SDL_SysWMEvent
 {
-    Uint8 type;         /**< ::SDL_SYSWMEVENT */
+    Uint32 type;        /**< ::SDL_SYSWMEVENT */
     SDL_SysWMmsg *msg;  /**< driver dependent data, defined in SDL_syswm.h */
 } SDL_SysWMEvent;
 
-typedef struct SDL_ProximityEvent
-{
-    Uint8 type;
-    Uint32 windowID;    /**< The associated window */
-    Uint8 which;
-    int cursor;
-    int x;
-    int y;
-} SDL_ProximityEvent;
-
 #ifndef SDL_NO_COMPAT
 /**
  *  \addtogroup Compatibility 
@@ -326,14 +333,14 @@
 /*@{*/
 typedef struct SDL_ActiveEvent
 {
-    Uint8 type;
+    Uint32 type;
     Uint8 gain;
     Uint8 state;
 } SDL_ActiveEvent;
 
 typedef struct SDL_ResizeEvent
 {
-    Uint8 type;
+    Uint32 type;
     int w;
     int h;
 } SDL_ResizeEvent;
@@ -347,7 +354,7 @@
  */
 typedef union SDL_Event
 {
-    Uint8 type;                     /**< Event type, shared with all events */
+    Uint32 type;                    /**< Event type, shared with all events */
     SDL_WindowEvent window;         /**< Window event data */
     SDL_KeyboardEvent key;          /**< Keyboard event data */
     SDL_TextEditingEvent edit;      /**< Text editing event data */
@@ -413,13 +420,20 @@
  */
 extern DECLSPEC int SDLCALL SDL_PeepEvents(SDL_Event * events, int numevents,
                                            SDL_eventaction action,
-                                           Uint32 mask);
+                                           Uint32 minType, Uint32 maxType);
 /*@}*/
 
 /**
  *  Checks to see if certain event types are in the event queue.
  */
-extern DECLSPEC SDL_bool SDLCALL SDL_HasEvent(Uint32 mask);
+extern DECLSPEC SDL_bool SDLCALL SDL_HasEvent(Uint32 type);
+extern DECLSPEC SDL_bool SDLCALL SDL_HasEvents(Uint32 minType, Uint32 maxType);
+
+/**
+ *  This function clears events from the event queue
+ */
+extern DECLSPEC void SDLCALL SDL_FlushEvent(Uint32 type);
+extern DECLSPEC void SDLCALL SDL_FlushEvents(Uint32 minType, Uint32 maxType);
 
 /**
  *  \brief Polls for currently pending events.
@@ -520,8 +534,18 @@
  *   - If \c state is set to ::SDL_QUERY, SDL_EventState() will return the 
  *     current processing state of the specified event.
  */
-extern DECLSPEC Uint8 SDLCALL SDL_EventState(Uint8 type, int state);
+extern DECLSPEC Uint8 SDLCALL SDL_EventState(Uint32 type, int state);
 /*@}*/
+#define SDL_GetEventState(type) SDL_EventState(type, SDL_QUERY)
+
+/**
+ *  This function allocates a set of user-defined events, and returns
+ *  the beginning event number for that set of events.
+ *
+ *  If there aren't enough user-defined events left, this function
+ *  returns (Uint32)-1
+ */
+extern DECLSPEC Uint32 SDLCALL SDL_RegisterEvents(int numevents);
 
 /* Ends C function definitions when using C++ */
 #ifdef __cplusplus