Mercurial > sdl-ios-xcode
annotate src/events/SDL_mouse.c @ 1077:f122afdfa025
The Darwin/MacOSX joystick code is largely copied from the HID Utilities
package...make the symbols defined by HID Utilities static inside SDL so that
an app can link against their own copy of this package without symbol clash.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Wed, 15 Jun 2005 23:41:57 +0000 |
parents | 715c32d8f26c |
children | 28ac87a38c17 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
769
b8d311d90021
Updated copyright information for 2004 (Happy New Year!)
Sam Lantinga <slouken@libsdl.org>
parents:
518
diff
changeset
|
3 Copyright (C) 1997-2004 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 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 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
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 */ |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
27 | |
28 /* General mouse handling code for SDL */ | |
29 | |
30 #include <stdio.h> | |
31 #include <stdlib.h> | |
32 #include <string.h> | |
33 | |
34 #include "SDL_events.h" | |
35 #include "SDL_events_c.h" | |
36 #include "SDL_cursor_c.h" | |
37 #include "SDL_sysvideo.h" | |
38 | |
39 | |
40 /* These are static for our mouse handling code */ | |
943
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
41 static Sint16 SDL_MouseX = -1; |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
42 static Sint16 SDL_MouseY = -1; |
0 | 43 static Sint16 SDL_DeltaX = 0; |
44 static Sint16 SDL_DeltaY = 0; | |
45 static Uint8 SDL_ButtonState = 0; | |
46 | |
47 | |
48 /* Public functions */ | |
49 int SDL_MouseInit(void) | |
50 { | |
51 /* The mouse is at (0,0) */ | |
943
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
52 SDL_MouseX = -1; |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
53 SDL_MouseY = -1; |
0 | 54 SDL_DeltaX = 0; |
55 SDL_DeltaY = 0; | |
56 SDL_ButtonState = 0; | |
57 | |
58 /* That's it! */ | |
59 return(0); | |
60 } | |
61 | |
460
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
62 /* 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
|
63 void SDL_ResetMouse(void) |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
64 { |
518 | 65 Uint8 i; |
460
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
66 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
|
67 if ( SDL_ButtonState & SDL_BUTTON(i) ) { |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
68 SDL_PrivateMouseButton(SDL_RELEASED, i, 0, 0); |
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 } |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
72 |
0 | 73 Uint8 SDL_GetMouseState (int *x, int *y) |
74 { | |
943
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
75 if ( x ) { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
76 if ( SDL_MouseX < 0 ) { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
77 *x = 0; |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
78 } else { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
79 *x = SDL_MouseX; |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
80 } |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
81 } |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
82 if ( y ) { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
83 if ( SDL_MouseY < 0 ) { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
84 *y = 0; |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
85 } else { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
86 *y = SDL_MouseY; |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
87 } |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
88 } |
0 | 89 return(SDL_ButtonState); |
90 } | |
91 | |
92 Uint8 SDL_GetRelativeMouseState (int *x, int *y) | |
93 { | |
94 if ( x ) | |
95 *x = SDL_DeltaX; | |
96 if ( y ) | |
97 *y = SDL_DeltaY; | |
98 SDL_DeltaX = 0; | |
99 SDL_DeltaY = 0; | |
100 return(SDL_ButtonState); | |
101 } | |
102 | |
103 static void ClipOffset(Sint16 *x, Sint16 *y) | |
104 { | |
105 /* This clips absolute mouse coordinates when the apparent | |
106 display surface is smaller than the real display surface. | |
107 */ | |
108 if ( SDL_VideoSurface->offset ) { | |
109 *y -= SDL_VideoSurface->offset/SDL_VideoSurface->pitch; | |
110 *x -= (SDL_VideoSurface->offset%SDL_VideoSurface->pitch)/ | |
111 SDL_VideoSurface->format->BytesPerPixel; | |
112 } | |
113 } | |
114 | |
115 /* These are global for SDL_eventloop.c */ | |
116 int SDL_PrivateMouseMotion(Uint8 buttonstate, int relative, Sint16 x, Sint16 y) | |
117 { | |
118 int posted; | |
119 Uint16 X, Y; | |
120 Sint16 Xrel; | |
121 Sint16 Yrel; | |
122 | |
123 /* Don't handle mouse motion if there's no cursor surface */ | |
124 if ( SDL_VideoSurface == NULL ) { | |
125 return(0); | |
126 } | |
127 | |
128 /* Default buttonstate is the current one */ | |
129 if ( ! buttonstate ) { | |
130 buttonstate = SDL_ButtonState; | |
131 } | |
132 | |
133 Xrel = x; | |
134 Yrel = y; | |
135 if ( relative ) { | |
136 /* Push the cursor around */ | |
137 x = (SDL_MouseX+x); | |
138 y = (SDL_MouseY+y); | |
139 } else { | |
140 /* Do we need to clip {x,y} ? */ | |
141 ClipOffset(&x, &y); | |
142 } | |
143 | |
144 /* Mouse coordinates range from 0 - width-1 and 0 - height-1 */ | |
145 if ( x < 0 ) | |
146 X = 0; | |
147 else | |
148 if ( x >= SDL_VideoSurface->w ) | |
149 X = SDL_VideoSurface->w-1; | |
150 else | |
151 X = (Uint16)x; | |
152 | |
153 if ( y < 0 ) | |
154 Y = 0; | |
155 else | |
156 if ( y >= SDL_VideoSurface->h ) | |
157 Y = SDL_VideoSurface->h-1; | |
158 else | |
159 Y = (Uint16)y; | |
160 | |
161 /* If not relative mode, generate relative motion from clamped X/Y. | |
162 This prevents lots of extraneous large delta relative motion when | |
163 the screen is windowed mode and the mouse is outside the window. | |
164 */ | |
943
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
165 if ( ! relative && SDL_MouseX >= 0 && SDL_MouseY >= 0 ) { |
0 | 166 Xrel = X-SDL_MouseX; |
167 Yrel = Y-SDL_MouseY; | |
168 } | |
169 | |
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; | |
176 SDL_MoveCursor(SDL_MouseX, SDL_MouseY); | |
177 | |
178 /* Post the event, if desired */ | |
179 posted = 0; | |
180 if ( SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE ) { | |
181 SDL_Event event; | |
182 memset(&event, 0, sizeof(event)); | |
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 | |
204 memset(&event, 0, sizeof(event)); | |
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 | |
214 if ( x >= SDL_VideoSurface->w ) | |
215 x = SDL_VideoSurface->w-1; | |
216 | |
217 if ( y < 0 ) | |
218 y = 0; | |
219 else | |
220 if ( y >= SDL_VideoSurface->h ) | |
221 y = SDL_VideoSurface->h-1; | |
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 |