Mercurial > sdl-ios-xcode
annotate src/events/SDL_mouse.c @ 4928:930614179450 SDL-1.2
Patch from Haiku's Google Code-In student Daniel Marth, fixes an off-by-one issue in bwindow.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Fri, 03 Dec 2010 16:55:25 -0800 |
parents | 429c8dd3175d |
children |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
4159 | 3 Copyright (C) 1997-2009 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1283
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1283
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1283
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1283
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1283
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1283
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
22 #include "SDL_config.h" |
0 | 23 |
24 /* General mouse handling code for SDL */ | |
25 | |
26 #include "SDL_events.h" | |
27 #include "SDL_events_c.h" | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
28 #include "../video/SDL_cursor_c.h" |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
29 #include "../video/SDL_sysvideo.h" |
0 | 30 |
31 | |
32 /* These are static for our mouse handling code */ | |
1525 | 33 static Sint16 SDL_MouseX = 0; |
34 static Sint16 SDL_MouseY = 0; | |
0 | 35 static Sint16 SDL_DeltaX = 0; |
36 static Sint16 SDL_DeltaY = 0; | |
4249 | 37 static Sint16 SDL_MouseMaxX = 0; |
38 static Sint16 SDL_MouseMaxY = 0; | |
0 | 39 static Uint8 SDL_ButtonState = 0; |
40 | |
41 | |
42 /* Public functions */ | |
43 int SDL_MouseInit(void) | |
44 { | |
45 /* The mouse is at (0,0) */ | |
1525 | 46 SDL_MouseX = 0; |
47 SDL_MouseY = 0; | |
0 | 48 SDL_DeltaX = 0; |
49 SDL_DeltaY = 0; | |
4249 | 50 SDL_MouseMaxX = 0; |
51 SDL_MouseMaxY = 0; | |
0 | 52 SDL_ButtonState = 0; |
53 | |
54 /* That's it! */ | |
55 return(0); | |
56 } | |
1123
28ac87a38c17
Date: Fri, 08 Jul 2005 22:43:48 +0200 (CEST)
Sam Lantinga <slouken@libsdl.org>
parents:
943
diff
changeset
|
57 void SDL_MouseQuit(void) |
28ac87a38c17
Date: Fri, 08 Jul 2005 22:43:48 +0200 (CEST)
Sam Lantinga <slouken@libsdl.org>
parents:
943
diff
changeset
|
58 { |
28ac87a38c17
Date: Fri, 08 Jul 2005 22:43:48 +0200 (CEST)
Sam Lantinga <slouken@libsdl.org>
parents:
943
diff
changeset
|
59 } |
0 | 60 |
460
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
61 /* We lost the mouse, so post button up messages for all pressed buttons */ |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
62 void SDL_ResetMouse(void) |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
63 { |
518 | 64 Uint8 i; |
460
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
65 for ( i = 0; i < sizeof(SDL_ButtonState)*8; ++i ) { |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
66 if ( SDL_ButtonState & SDL_BUTTON(i) ) { |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
67 SDL_PrivateMouseButton(SDL_RELEASED, i, 0, 0); |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
68 } |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
69 } |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
70 } |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
71 |
0 | 72 Uint8 SDL_GetMouseState (int *x, int *y) |
73 { | |
943
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
74 if ( x ) { |
1525 | 75 *x = SDL_MouseX; |
943
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
76 } |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
77 if ( y ) { |
1525 | 78 *y = SDL_MouseY; |
943
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
79 } |
0 | 80 return(SDL_ButtonState); |
81 } | |
82 | |
83 Uint8 SDL_GetRelativeMouseState (int *x, int *y) | |
84 { | |
85 if ( x ) | |
86 *x = SDL_DeltaX; | |
87 if ( y ) | |
88 *y = SDL_DeltaY; | |
89 SDL_DeltaX = 0; | |
90 SDL_DeltaY = 0; | |
91 return(SDL_ButtonState); | |
92 } | |
93 | |
94 static void ClipOffset(Sint16 *x, Sint16 *y) | |
95 { | |
96 /* This clips absolute mouse coordinates when the apparent | |
97 display surface is smaller than the real display surface. | |
98 */ | |
4249 | 99 if ( SDL_VideoSurface && SDL_VideoSurface->offset ) { |
0 | 100 *y -= SDL_VideoSurface->offset/SDL_VideoSurface->pitch; |
101 *x -= (SDL_VideoSurface->offset%SDL_VideoSurface->pitch)/ | |
102 SDL_VideoSurface->format->BytesPerPixel; | |
103 } | |
104 } | |
105 | |
4249 | 106 void SDL_SetMouseRange(int maxX, int maxY) |
107 { | |
108 SDL_MouseMaxX = (Sint16)maxX; | |
109 SDL_MouseMaxY = (Sint16)maxY; | |
110 } | |
111 | |
0 | 112 /* These are global for SDL_eventloop.c */ |
113 int SDL_PrivateMouseMotion(Uint8 buttonstate, int relative, Sint16 x, Sint16 y) | |
114 { | |
115 int posted; | |
116 Uint16 X, Y; | |
117 Sint16 Xrel; | |
118 Sint16 Yrel; | |
119 | |
120 /* Default buttonstate is the current one */ | |
121 if ( ! buttonstate ) { | |
122 buttonstate = SDL_ButtonState; | |
123 } | |
124 | |
125 Xrel = x; | |
126 Yrel = y; | |
127 if ( relative ) { | |
128 /* Push the cursor around */ | |
129 x = (SDL_MouseX+x); | |
130 y = (SDL_MouseY+y); | |
131 } else { | |
132 /* Do we need to clip {x,y} ? */ | |
133 ClipOffset(&x, &y); | |
134 } | |
135 | |
136 /* Mouse coordinates range from 0 - width-1 and 0 - height-1 */ | |
137 if ( x < 0 ) | |
138 X = 0; | |
139 else | |
4249 | 140 if ( x >= SDL_MouseMaxX ) |
141 X = SDL_MouseMaxX-1; | |
0 | 142 else |
143 X = (Uint16)x; | |
144 | |
145 if ( y < 0 ) | |
146 Y = 0; | |
147 else | |
4249 | 148 if ( y >= SDL_MouseMaxY ) |
149 Y = SDL_MouseMaxY-1; | |
0 | 150 else |
151 Y = (Uint16)y; | |
152 | |
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. | |
156 */ | |
1525 | 157 if ( ! relative ) { |
0 | 158 Xrel = X-SDL_MouseX; |
159 Yrel = Y-SDL_MouseY; | |
160 } | |
161 | |
1283
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
162 /* Drop events that don't change state */ |
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
163 if ( ! Xrel && ! Yrel ) { |
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
164 #if 0 |
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
165 printf("Mouse event didn't change state - dropped!\n"); |
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
166 #endif |
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
167 return(0); |
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
168 } |
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
169 |
0 | 170 /* Update internal mouse state */ |
171 SDL_ButtonState = buttonstate; | |
172 SDL_MouseX = X; | |
173 SDL_MouseY = Y; | |
174 SDL_DeltaX += Xrel; | |
175 SDL_DeltaY += Yrel; | |
1442
e3242177fe4a
Updated OS/2 build, yay!
Sam Lantinga <slouken@libsdl.org>
parents:
1402
diff
changeset
|
176 SDL_MoveCursor(SDL_MouseX, SDL_MouseY); |
0 | 177 |
178 /* Post the event, if desired */ | |
179 posted = 0; | |
180 if ( SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE ) { | |
181 SDL_Event event; | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
182 SDL_memset(&event, 0, sizeof(event)); |
0 | 183 event.type = SDL_MOUSEMOTION; |
184 event.motion.state = buttonstate; | |
185 event.motion.x = X; | |
186 event.motion.y = Y; | |
187 event.motion.xrel = Xrel; | |
188 event.motion.yrel = Yrel; | |
189 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { | |
190 posted = 1; | |
191 SDL_PushEvent(&event); | |
192 } | |
193 } | |
194 return(posted); | |
195 } | |
196 | |
197 int SDL_PrivateMouseButton(Uint8 state, Uint8 button, Sint16 x, Sint16 y) | |
198 { | |
199 SDL_Event event; | |
200 int posted; | |
201 int move_mouse; | |
202 Uint8 buttonstate; | |
203 | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
204 SDL_memset(&event, 0, sizeof(event)); |
0 | 205 |
206 /* Check parameters */ | |
207 if ( x || y ) { | |
208 ClipOffset(&x, &y); | |
209 move_mouse = 1; | |
210 /* Mouse coordinates range from 0 - width-1 and 0 - height-1 */ | |
211 if ( x < 0 ) | |
212 x = 0; | |
213 else | |
4249 | 214 if ( x >= SDL_MouseMaxX ) |
215 x = SDL_MouseMaxX-1; | |
0 | 216 |
217 if ( y < 0 ) | |
218 y = 0; | |
219 else | |
4249 | 220 if ( y >= SDL_MouseMaxY ) |
221 y = SDL_MouseMaxY-1; | |
0 | 222 } else { |
223 move_mouse = 0; | |
224 } | |
225 if ( ! x ) | |
226 x = SDL_MouseX; | |
227 if ( ! y ) | |
228 y = SDL_MouseY; | |
229 | |
230 /* Figure out which event to perform */ | |
231 buttonstate = SDL_ButtonState; | |
232 switch ( state ) { | |
233 case SDL_PRESSED: | |
234 event.type = SDL_MOUSEBUTTONDOWN; | |
235 buttonstate |= SDL_BUTTON(button); | |
236 break; | |
237 case SDL_RELEASED: | |
238 event.type = SDL_MOUSEBUTTONUP; | |
239 buttonstate &= ~SDL_BUTTON(button); | |
240 break; | |
241 default: | |
242 /* Invalid state -- bail */ | |
243 return(0); | |
244 } | |
245 | |
246 /* Update internal mouse state */ | |
247 SDL_ButtonState = buttonstate; | |
248 if ( move_mouse ) { | |
249 SDL_MouseX = x; | |
250 SDL_MouseY = y; | |
251 SDL_MoveCursor(SDL_MouseX, SDL_MouseY); | |
252 } | |
253 | |
254 /* Post the event, if desired */ | |
255 posted = 0; | |
256 if ( SDL_ProcessEvents[event.type] == SDL_ENABLE ) { | |
257 event.button.state = state; | |
258 event.button.button = button; | |
259 event.button.x = x; | |
260 event.button.y = y; | |
261 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { | |
262 posted = 1; | |
263 SDL_PushEvent(&event); | |
264 } | |
265 } | |
266 return(posted); | |
267 } | |
268 |