include/SDL_events.h
author Sam Lantinga <slouken@libsdl.org>
Sun, 09 May 2010 20:47:22 -0700
changeset 4465 3e69e077cb95
parent 4435 e953700da4ca
child 4503 524dfefd554c
permissions -rw-r--r--
Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.

Plus, this lets me start implementing cursor support.
     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 or multiple mice input device events */
    79     SDL_INPUTMOTION    = 0x500, /**< Input moved */
    80     SDL_INPUTBUTTONDOWN,        /**< Input button pressed */
    81     SDL_INPUTBUTTONUP,          /**< Input button released */
    82     SDL_INPUTWHEEL,             /**< Input wheel motion */
    83     SDL_INPUTPROXIMITYIN,       /**< Input pen entered proximity */
    84     SDL_INPUTPROXIMITYOUT,      /**< Input pen left proximity */
    85 
    86     /* Joystick events */
    87     SDL_JOYAXISMOTION  = 0x600, /**< Joystick axis motion */
    88     SDL_JOYBALLMOTION,          /**< Joystick trackball motion */
    89     SDL_JOYHATMOTION,           /**< Joystick hat position change */
    90     SDL_JOYBUTTONDOWN,          /**< Joystick button pressed */
    91     SDL_JOYBUTTONUP,            /**< Joystick button released */
    92 
    93     /* Obsolete events */
    94     SDL_EVENT_COMPAT1 = 0x7000, /**< SDL 1.2 events for compatibility */
    95     SDL_EVENT_COMPAT2,
    96     SDL_EVENT_COMPAT3,
    97 
    98     /** Events ::SDL_USEREVENT through ::SDL_LASTEVENT are for your use,
    99      *  and should be allocated with SDL_RegisterEvents()
   100      */
   101     SDL_USEREVENT    = 0x8000,
   102 
   103     /**
   104      *  This last event is only for bounding internal arrays
   105      */
   106     SDL_LASTEVENT    = 0xFFFF
   107 } SDL_EventType;
   108 
   109 /**
   110  *  \brief Window state change event data (event.window.*)
   111  */
   112 typedef struct SDL_WindowEvent
   113 {
   114     Uint32 type;        /**< ::SDL_WINDOWEVENT */
   115     Uint32 windowID;    /**< The associated window */
   116     Uint8 event;        /**< ::SDL_WindowEventID */
   117     Uint8 padding1;
   118     Uint8 padding2;
   119     Uint8 padding3;
   120     int data1;          /**< event dependent data */
   121     int data2;          /**< event dependent data */
   122 } SDL_WindowEvent;
   123 
   124 /**
   125  *  \brief Keyboard button event structure (event.key.*)
   126  */
   127 typedef struct SDL_KeyboardEvent
   128 {
   129     Uint32 type;        /**< ::SDL_KEYDOWN or ::SDL_KEYUP */
   130     Uint32 windowID;    /**< The window with keyboard focus, if any */
   131     Uint8 state;        /**< ::SDL_PRESSED or ::SDL_RELEASED */
   132     Uint8 padding1;
   133     Uint8 padding2;
   134     Uint8 padding3;
   135     SDL_keysym keysym;  /**< The key that was pressed or released */
   136 } SDL_KeyboardEvent;
   137 
   138 #define SDL_TEXTEDITINGEVENT_TEXT_SIZE (32)
   139 /**
   140  *  \brief Keyboard text editing event structure (event.edit.*)
   141  */
   142 typedef struct SDL_TextEditingEvent
   143 {
   144     Uint32 type;                                /**< ::SDL_TEXTEDITING */
   145     Uint32 windowID;                            /**< The window with keyboard focus, if any */
   146     char text[SDL_TEXTEDITINGEVENT_TEXT_SIZE];  /**< The editing text */
   147     int start;                                  /**< The start cursor of selected editing text */
   148     int length;                                 /**< The length of selected editing text */
   149 } SDL_TextEditingEvent;
   150 
   151 
   152 #define SDL_TEXTINPUTEVENT_TEXT_SIZE (32)
   153 /**
   154  *  \brief Keyboard text input event structure (event.text.*)
   155  */
   156 typedef struct SDL_TextInputEvent
   157 {
   158     Uint32 type;                              /**< ::SDL_TEXTINPUT */
   159     Uint32 windowID;                          /**< The window with keyboard focus, if any */
   160     char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];  /**< The input text */
   161 } SDL_TextInputEvent;
   162 
   163 /**
   164  *  \brief Mouse motion event structure (event.motion.*)
   165  */
   166 typedef struct SDL_MouseMotionEvent
   167 {
   168     Uint32 type;        /**< ::SDL_MOUSEMOTION */
   169     Uint32 windowID;    /**< The window with mouse focus, if any */
   170     Uint8 state;        /**< The current button state */
   171     Uint8 padding1;
   172     Uint8 padding2;
   173     Uint8 padding3;
   174     int x;              /**< X coordinate, relative to window */
   175     int y;              /**< Y coordinate, relative to window */
   176     int xrel;           /**< The relative motion in the X direction */
   177     int yrel;           /**< The relative motion in the Y direction */
   178 } SDL_MouseMotionEvent;
   179 
   180 /**
   181  *  \brief Mouse button event structure (event.button.*)
   182  */
   183 typedef struct SDL_MouseButtonEvent
   184 {
   185     Uint32 type;        /**< ::SDL_MOUSEBUTTONDOWN or ::SDL_MOUSEBUTTONUP */
   186     Uint32 windowID;    /**< The window with mouse focus, if any */
   187     Uint8 button;       /**< The mouse button index */
   188     Uint8 state;        /**< ::SDL_PRESSED or ::SDL_RELEASED */
   189     Uint8 padding1;
   190     Uint8 padding2;
   191     int x;              /**< X coordinate, relative to window */
   192     int y;              /**< Y coordinate, relative to window */
   193 } SDL_MouseButtonEvent;
   194 
   195 /**
   196  *  \brief Mouse wheel event structure (event.wheel.*)
   197  */
   198 typedef struct SDL_MouseWheelEvent
   199 {
   200     Uint32 type;        /**< ::SDL_MOUSEWHEEL */
   201     Uint32 windowID;    /**< The window with mouse focus, if any */
   202     int x;              /**< The amount scrolled horizontally */
   203     int y;              /**< The amount scrolled vertically */
   204 } SDL_MouseWheelEvent;
   205 
   206 /**
   207  *  \brief Joystick axis motion event structure (event.jaxis.*)
   208  */
   209 typedef struct SDL_JoyAxisEvent
   210 {
   211     Uint32 type;        /**< ::SDL_JOYAXISMOTION */
   212     Uint8 which;        /**< The joystick device index */
   213     Uint8 axis;         /**< The joystick axis index */
   214     Uint8 padding1;
   215     Uint8 padding2;
   216     int value;          /**< The axis value (range: -32768 to 32767) */
   217 } SDL_JoyAxisEvent;
   218 
   219 /**
   220  *  \brief Joystick trackball motion event structure (event.jball.*)
   221  */
   222 typedef struct SDL_JoyBallEvent
   223 {
   224     Uint32 type;        /**< ::SDL_JOYBALLMOTION */
   225     Uint8 which;        /**< The joystick device index */
   226     Uint8 ball;         /**< The joystick trackball index */
   227     Uint8 padding1;
   228     Uint8 padding2;
   229     int xrel;           /**< The relative motion in the X direction */
   230     int yrel;           /**< The relative motion in the Y direction */
   231 } SDL_JoyBallEvent;
   232 
   233 /**
   234  *  \brief Joystick hat position change event structure (event.jhat.*)
   235  */
   236 typedef struct SDL_JoyHatEvent
   237 {
   238     Uint32 type;        /**< ::SDL_JOYHATMOTION */
   239     Uint8 which;        /**< The joystick device index */
   240     Uint8 hat;          /**< The joystick hat index */
   241     Uint8 value;        /**< The hat position value.
   242                          *   \sa ::SDL_HAT_LEFTUP ::SDL_HAT_UP ::SDL_HAT_RIGHTUP
   243                          *   \sa ::SDL_HAT_LEFT ::SDL_HAT_CENTERED ::SDL_HAT_RIGHT
   244                          *   \sa ::SDL_HAT_LEFTDOWN ::SDL_HAT_DOWN ::SDL_HAT_RIGHTDOWN
   245                          *   
   246                          *   Note that zero means the POV is centered.
   247                          */
   248     Uint8 padding1;
   249 } SDL_JoyHatEvent;
   250 
   251 /**
   252  *  \brief Joystick button event structure (event.jbutton.*)
   253  */
   254 typedef struct SDL_JoyButtonEvent
   255 {
   256     Uint32 type;        /**< ::SDL_JOYBUTTONDOWN or ::SDL_JOYBUTTONUP */
   257     Uint8 which;        /**< The joystick device index */
   258     Uint8 button;       /**< The joystick button index */
   259     Uint8 state;        /**< ::SDL_PRESSED or ::SDL_RELEASED */
   260     Uint8 padding1;
   261 } SDL_JoyButtonEvent;
   262 
   263 /**
   264  *  \brief The "quit requested" event
   265  */
   266 typedef struct SDL_QuitEvent
   267 {
   268     Uint32 type;        /**< ::SDL_QUIT */
   269 } SDL_QuitEvent;
   270 
   271 /**
   272  *  \brief A user-defined event type (event.user.*)
   273  */
   274 typedef struct SDL_UserEvent
   275 {
   276     Uint32 type;        /**< ::SDL_USEREVENT through ::SDL_NUMEVENTS-1 */
   277     Uint32 windowID;    /**< The associated window if any */
   278     int code;           /**< User defined event code */
   279     void *data1;        /**< User defined data pointer */
   280     void *data2;        /**< User defined data pointer */
   281 } SDL_UserEvent;
   282 
   283 struct SDL_SysWMmsg;
   284 typedef struct SDL_SysWMmsg SDL_SysWMmsg;
   285 
   286 /**
   287  *  \brief A video driver dependent system event (event.syswm.*)
   288  *  
   289  *  \note If you want to use this event, you should include SDL_syswm.h.
   290  */
   291 typedef struct SDL_SysWMEvent
   292 {
   293     Uint32 type;        /**< ::SDL_SYSWMEVENT */
   294     SDL_SysWMmsg *msg;  /**< driver dependent data, defined in SDL_syswm.h */
   295 } SDL_SysWMEvent;
   296 
   297 #ifndef SDL_NO_COMPAT
   298 /**
   299  *  \addtogroup Compatibility 
   300  */
   301 /*@{*/
   302 
   303 /**
   304  *  \name Typedefs for backwards compatibility
   305  */
   306 /*@{*/
   307 typedef struct SDL_ActiveEvent
   308 {
   309     Uint32 type;
   310     Uint8 gain;
   311     Uint8 state;
   312 } SDL_ActiveEvent;
   313 
   314 typedef struct SDL_ResizeEvent
   315 {
   316     Uint32 type;
   317     int w;
   318     int h;
   319 } SDL_ResizeEvent;
   320 /*@}*/
   321 
   322 /*@}*//*Compatibility*/
   323 #endif
   324 
   325 /**
   326  *  \brief General event structure
   327  */
   328 typedef union SDL_Event
   329 {
   330     Uint32 type;                    /**< Event type, shared with all events */
   331     SDL_WindowEvent window;         /**< Window event data */
   332     SDL_KeyboardEvent key;          /**< Keyboard event data */
   333     SDL_TextEditingEvent edit;      /**< Text editing event data */
   334     SDL_TextInputEvent text;        /**< Text input event data */
   335     SDL_MouseMotionEvent motion;    /**< Mouse motion event data */
   336     SDL_MouseButtonEvent button;    /**< Mouse button event data */
   337     SDL_MouseWheelEvent wheel;      /**< Mouse wheel event data */
   338     SDL_JoyAxisEvent jaxis;         /**< Joystick axis event data */
   339     SDL_JoyBallEvent jball;         /**< Joystick ball event data */
   340     SDL_JoyHatEvent jhat;           /**< Joystick hat event data */
   341     SDL_JoyButtonEvent jbutton;     /**< Joystick button event data */
   342     SDL_QuitEvent quit;             /**< Quit request event data */
   343     SDL_UserEvent user;             /**< Custom event data */
   344     SDL_SysWMEvent syswm;           /**< System dependent window event data */
   345 
   346     /** Temporarily here for backwards compatibility */
   347     /*@{*/
   348 #ifndef SDL_NO_COMPAT
   349     SDL_ActiveEvent active;
   350     SDL_ResizeEvent resize;
   351 #endif
   352     /*@}*/
   353 } SDL_Event;
   354 
   355 
   356 /* Function prototypes */
   357 
   358 /**
   359  *  Pumps the event loop, gathering events from the input devices.
   360  *  
   361  *  This function updates the event queue and internal input device state.
   362  *  
   363  *  This should only be run in the thread that sets the video mode.
   364  */
   365 extern DECLSPEC void SDLCALL SDL_PumpEvents(void);
   366 
   367 /*@{*/
   368 typedef enum
   369 {
   370     SDL_ADDEVENT,
   371     SDL_PEEKEVENT,
   372     SDL_GETEVENT
   373 } SDL_eventaction;
   374 
   375 /**
   376  *  Checks the event queue for messages and optionally returns them.
   377  *  
   378  *  If \c action is ::SDL_ADDEVENT, up to \c numevents events will be added to
   379  *  the back of the event queue.
   380  *  
   381  *  If \c action is ::SDL_PEEKEVENT, up to \c numevents events at the front
   382  *  of the event queue, matching \c mask, will be returned and will not
   383  *  be removed from the queue.
   384  *  
   385  *  If \c action is ::SDL_GETEVENT, up to \c numevents events at the front 
   386  *  of the event queue, matching \c mask, will be returned and will be
   387  *  removed from the queue.
   388  *  
   389  *  \return The number of events actually stored, or -1 if there was an error.
   390  *  
   391  *  This function is thread-safe.
   392  */
   393 extern DECLSPEC int SDLCALL SDL_PeepEvents(SDL_Event * events, int numevents,
   394                                            SDL_eventaction action,
   395                                            Uint32 minType, Uint32 maxType);
   396 /*@}*/
   397 
   398 /**
   399  *  Checks to see if certain event types are in the event queue.
   400  */
   401 extern DECLSPEC SDL_bool SDLCALL SDL_HasEvent(Uint32 type);
   402 extern DECLSPEC SDL_bool SDLCALL SDL_HasEvents(Uint32 minType, Uint32 maxType);
   403 
   404 /**
   405  *  This function clears events from the event queue
   406  */
   407 extern DECLSPEC void SDLCALL SDL_FlushEvent(Uint32 type);
   408 extern DECLSPEC void SDLCALL SDL_FlushEvents(Uint32 minType, Uint32 maxType);
   409 
   410 /**
   411  *  \brief Polls for currently pending events.
   412  *  
   413  *  \return 1 if there are any pending events, or 0 if there are none available.
   414  *  
   415  *  \param event If not NULL, the next event is removed from the queue and 
   416  *               stored in that area.
   417  */
   418 extern DECLSPEC int SDLCALL SDL_PollEvent(SDL_Event * event);
   419 
   420 /**
   421  *  \brief Waits indefinitely for the next available event.
   422  *  
   423  *  \return 1, or 0 if there was an error while waiting for events.
   424  *   
   425  *  \param event If not NULL, the next event is removed from the queue and 
   426  *               stored in that area.
   427  */
   428 extern DECLSPEC int SDLCALL SDL_WaitEvent(SDL_Event * event);
   429 
   430 /**
   431  *  \brief Waits until the specified timeout (in milliseconds) for the next 
   432  *         available event.
   433  *  
   434  *  \return 1, or 0 if there was an error while waiting for events.
   435  *  
   436  *  \param event If not NULL, the next event is removed from the queue and 
   437  *               stored in that area.
   438  */
   439 extern DECLSPEC int SDLCALL SDL_WaitEventTimeout(SDL_Event * event,
   440                                                  int timeout);
   441 
   442 /**
   443  *  \brief Add an event to the event queue.
   444  *  
   445  *  \return 1 on success, 0 if the event was filtered, or -1 if the event queue 
   446  *          was full or there was some other error.
   447  */
   448 extern DECLSPEC int SDLCALL SDL_PushEvent(SDL_Event * event);
   449 
   450 typedef int (SDLCALL * SDL_EventFilter) (void *userdata, SDL_Event * event);
   451 
   452 /**
   453  *  Sets up a filter to process all events before they change internal state and
   454  *  are posted to the internal event queue.
   455  *  
   456  *  The filter is protypted as:
   457  *  \code
   458  *      int SDL_EventFilter(void *userdata, SDL_Event * event);
   459  *  \endcode
   460  *
   461  *  If the filter returns 1, then the event will be added to the internal queue.
   462  *  If it returns 0, then the event will be dropped from the queue, but the 
   463  *  internal state will still be updated.  This allows selective filtering of
   464  *  dynamically arriving events.
   465  *  
   466  *  \warning  Be very careful of what you do in the event filter function, as 
   467  *            it may run in a different thread!
   468  *  
   469  *  There is one caveat when dealing with the ::SDL_QUITEVENT event type.  The
   470  *  event filter is only called when the window manager desires to close the
   471  *  application window.  If the event filter returns 1, then the window will
   472  *  be closed, otherwise the window will remain open if possible.
   473  *
   474  *  If the quit event is generated by an interrupt signal, it will bypass the
   475  *  internal queue and be delivered to the application at the next event poll.
   476  */
   477 extern DECLSPEC void SDLCALL SDL_SetEventFilter(SDL_EventFilter filter,
   478                                                 void *userdata);
   479 
   480 /**
   481  *  Return the current event filter - can be used to "chain" filters.
   482  *  If there is no event filter set, this function returns SDL_FALSE.
   483  */
   484 extern DECLSPEC SDL_bool SDLCALL SDL_GetEventFilter(SDL_EventFilter * filter,
   485                                                     void **userdata);
   486 
   487 /**
   488  *  Run the filter function on the current event queue, removing any
   489  *  events for which the filter returns 0.
   490  */
   491 extern DECLSPEC void SDLCALL SDL_FilterEvents(SDL_EventFilter filter,
   492                                               void *userdata);
   493 
   494 /*@{*/
   495 #define SDL_QUERY	-1
   496 #define SDL_IGNORE	 0
   497 #define SDL_DISABLE	 0
   498 #define SDL_ENABLE	 1
   499 
   500 /**
   501  *  This function allows you to set the state of processing certain events.
   502  *   - If \c state is set to ::SDL_IGNORE, that event will be automatically 
   503  *     dropped from the event queue and will not event be filtered.
   504  *   - If \c state is set to ::SDL_ENABLE, that event will be processed 
   505  *     normally.
   506  *   - If \c state is set to ::SDL_QUERY, SDL_EventState() will return the 
   507  *     current processing state of the specified event.
   508  */
   509 extern DECLSPEC Uint8 SDLCALL SDL_EventState(Uint32 type, int state);
   510 /*@}*/
   511 #define SDL_GetEventState(type) SDL_EventState(type, SDL_QUERY)
   512 
   513 /**
   514  *  This function allocates a set of user-defined events, and returns
   515  *  the beginning event number for that set of events.
   516  *
   517  *  If there aren't enough user-defined events left, this function
   518  *  returns (Uint32)-1
   519  */
   520 extern DECLSPEC Uint32 SDLCALL SDL_RegisterEvents(int numevents);
   521 
   522 /* Ends C function definitions when using C++ */
   523 #ifdef __cplusplus
   524 /* *INDENT-OFF* */
   525 }
   526 /* *INDENT-ON* */
   527 #endif
   528 #include "close_code.h"
   529 
   530 #endif /* _SDL_events_h */
   531 
   532 /* vi: set ts=4 sw=4 expandtab: */