2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 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 Sint16 SDL_MouseMaxX = 0;
38 static Sint16 SDL_MouseMaxY = 0;
39 static Uint8 SDL_ButtonState = 0;
42 /* Public functions */
43 int SDL_MouseInit(void)
45 /* The mouse is at (0,0) */
57 void SDL_MouseQuit(void)
61 /* We lost the mouse, so post button up messages for all pressed buttons */
62 void SDL_ResetMouse(void)
65 for ( i = 0; i < sizeof(SDL_ButtonState)*8; ++i ) {
66 if ( SDL_ButtonState & SDL_BUTTON(i) ) {
67 SDL_PrivateMouseButton(SDL_RELEASED, i, 0, 0);
72 Uint8 SDL_GetMouseState (int *x, int *y)
80 return(SDL_ButtonState);
83 Uint8 SDL_GetRelativeMouseState (int *x, int *y)
91 return(SDL_ButtonState);
94 static void ClipOffset(Sint16 *x, Sint16 *y)
96 /* This clips absolute mouse coordinates when the apparent
97 display surface is smaller than the real display surface.
99 if ( SDL_VideoSurface && SDL_VideoSurface->offset ) {
100 *y -= SDL_VideoSurface->offset/SDL_VideoSurface->pitch;
101 *x -= (SDL_VideoSurface->offset%SDL_VideoSurface->pitch)/
102 SDL_VideoSurface->format->BytesPerPixel;
106 void SDL_SetMouseRange(int maxX, int maxY)
108 SDL_MouseMaxX = (Sint16)maxX;
109 SDL_MouseMaxY = (Sint16)maxY;
112 /* These are global for SDL_eventloop.c */
113 int SDL_PrivateMouseMotion(Uint8 buttonstate, int relative, Sint16 x, Sint16 y)
120 /* Default buttonstate is the current one */
121 if ( ! buttonstate ) {
122 buttonstate = SDL_ButtonState;
128 /* Push the cursor around */
132 /* Do we need to clip {x,y} ? */
136 /* Mouse coordinates range from 0 - width-1 and 0 - height-1 */
140 if ( x >= SDL_MouseMaxX )
148 if ( y >= SDL_MouseMaxY )
153 /* If not relative mode, generate relative motion from clamped X/Y.
154 This prevents lots of extraneous large delta relative motion when
155 the screen is windowed mode and the mouse is outside the window.
162 /* Drop events that don't change state */
163 if ( ! Xrel && ! Yrel ) {
165 printf("Mouse event didn't change state - dropped!\n");
170 /* Update internal mouse state */
171 SDL_ButtonState = buttonstate;
176 SDL_MoveCursor(SDL_MouseX, SDL_MouseY);
178 /* Post the event, if desired */
180 if ( SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE ) {
182 SDL_memset(&event, 0, sizeof(event));
183 event.type = SDL_MOUSEMOTION;
184 event.motion.state = buttonstate;
187 event.motion.xrel = Xrel;
188 event.motion.yrel = Yrel;
189 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
191 SDL_PushEvent(&event);
197 int SDL_PrivateMouseButton(Uint8 state, Uint8 button, Sint16 x, Sint16 y)
204 SDL_memset(&event, 0, sizeof(event));
206 /* Check parameters */
210 /* Mouse coordinates range from 0 - width-1 and 0 - height-1 */
214 if ( x >= SDL_MouseMaxX )
220 if ( y >= SDL_MouseMaxY )
230 /* Figure out which event to perform */
231 buttonstate = SDL_ButtonState;
234 event.type = SDL_MOUSEBUTTONDOWN;
235 buttonstate |= SDL_BUTTON(button);
238 event.type = SDL_MOUSEBUTTONUP;
239 buttonstate &= ~SDL_BUTTON(button);
242 /* Invalid state -- bail */
246 /* Update internal mouse state */
247 SDL_ButtonState = buttonstate;
251 SDL_MoveCursor(SDL_MouseX, SDL_MouseY);
254 /* Post the event, if desired */
256 if ( SDL_ProcessEvents[event.type] == SDL_ENABLE ) {
257 event.button.state = state;
258 event.button.button = button;
261 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
263 SDL_PushEvent(&event);