2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2006 Sam Lantinga
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.
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.
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
22 #include "SDL_config.h"
24 /* General mouse handling code for SDL */
26 #include "SDL_events.h"
27 #include "SDL_events_c.h"
28 #include "../video/SDL_cursor_c.h"
29 #include "../video/SDL_sysvideo.h"
32 /* These are static for our mouse handling code */
33 static Sint16 SDL_MouseX = 0;
34 static Sint16 SDL_MouseY = 0;
35 static Sint16 SDL_DeltaX = 0;
36 static Sint16 SDL_DeltaY = 0;
37 static Uint8 SDL_ButtonState = 0;
40 /* Public functions */
41 int SDL_MouseInit(void)
43 /* The mouse is at (0,0) */
53 void SDL_MouseQuit(void)
57 /* We lost the mouse, so post button up messages for all pressed buttons */
58 void SDL_ResetMouse(void)
61 for ( i = 0; i < sizeof(SDL_ButtonState)*8; ++i ) {
62 if ( SDL_ButtonState & SDL_BUTTON(i) ) {
63 SDL_PrivateMouseButton(SDL_RELEASED, i, 0, 0);
68 Uint8 SDL_GetMouseState (int *x, int *y)
76 return(SDL_ButtonState);
79 Uint8 SDL_GetRelativeMouseState (int *x, int *y)
87 return(SDL_ButtonState);
90 static void ClipOffset(Sint16 *x, Sint16 *y)
92 /* This clips absolute mouse coordinates when the apparent
93 display surface is smaller than the real display surface.
95 if ( SDL_VideoSurface->offset ) {
96 *y -= SDL_VideoSurface->offset/SDL_VideoSurface->pitch;
97 *x -= (SDL_VideoSurface->offset%SDL_VideoSurface->pitch)/
98 SDL_VideoSurface->format->BytesPerPixel;
102 /* These are global for SDL_eventloop.c */
103 int SDL_PrivateMouseMotion(Uint8 buttonstate, int relative, Sint16 x, Sint16 y)
110 /* Don't handle mouse motion if there's no cursor surface */
111 if ( SDL_VideoSurface == NULL ) {
115 /* Default buttonstate is the current one */
116 if ( ! buttonstate ) {
117 buttonstate = SDL_ButtonState;
123 /* Push the cursor around */
127 /* Do we need to clip {x,y} ? */
131 /* Mouse coordinates range from 0 - width-1 and 0 - height-1 */
135 if ( x >= SDL_VideoSurface->w )
136 X = SDL_VideoSurface->w-1;
143 if ( y >= SDL_VideoSurface->h )
144 Y = SDL_VideoSurface->h-1;
148 /* If not relative mode, generate relative motion from clamped X/Y.
149 This prevents lots of extraneous large delta relative motion when
150 the screen is windowed mode and the mouse is outside the window.
157 /* Drop events that don't change state */
158 if ( ! Xrel && ! Yrel ) {
160 printf("Mouse event didn't change state - dropped!\n");
165 /* Update internal mouse state */
166 SDL_ButtonState = buttonstate;
171 SDL_MoveCursor(SDL_MouseX, SDL_MouseY);
173 /* Post the event, if desired */
175 if ( SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE ) {
177 SDL_memset(&event, 0, sizeof(event));
178 event.type = SDL_MOUSEMOTION;
179 event.motion.state = buttonstate;
182 event.motion.xrel = Xrel;
183 event.motion.yrel = Yrel;
184 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
186 SDL_PushEvent(&event);
192 int SDL_PrivateMouseButton(Uint8 state, Uint8 button, Sint16 x, Sint16 y)
199 SDL_memset(&event, 0, sizeof(event));
201 /* Check parameters */
205 /* Mouse coordinates range from 0 - width-1 and 0 - height-1 */
209 if ( x >= SDL_VideoSurface->w )
210 x = SDL_VideoSurface->w-1;
215 if ( y >= SDL_VideoSurface->h )
216 y = SDL_VideoSurface->h-1;
225 /* Figure out which event to perform */
226 buttonstate = SDL_ButtonState;
229 event.type = SDL_MOUSEBUTTONDOWN;
230 buttonstate |= SDL_BUTTON(button);
233 event.type = SDL_MOUSEBUTTONUP;
234 buttonstate &= ~SDL_BUTTON(button);
237 /* Invalid state -- bail */
241 /* Update internal mouse state */
242 SDL_ButtonState = buttonstate;
246 SDL_MoveCursor(SDL_MouseX, SDL_MouseY);
249 /* Post the event, if desired */
251 if ( SDL_ProcessEvents[event.type] == SDL_ENABLE ) {
252 event.button.state = state;
253 event.button.button = button;
256 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
258 SDL_PushEvent(&event);