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