Mercurial > sdl-ios-xcode
annotate src/events/SDL_mouse.c @ 844:cd0c77df70a4
*** empty log message ***
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 18 Feb 2004 03:57:13 +0000 |
parents | b8d311d90021 |
children | 715c32d8f26c |
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 */ | |
41 static Sint16 SDL_MouseX = 0; | |
42 static Sint16 SDL_MouseY = 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) */ | |
52 SDL_MouseX = 0; | |
53 SDL_MouseY = 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 { | |
75 if ( x ) | |
76 *x = SDL_MouseX; | |
77 if ( y ) | |
78 *y = SDL_MouseY; | |
79 return(SDL_ButtonState); | |
80 } | |
81 | |
82 Uint8 SDL_GetRelativeMouseState (int *x, int *y) | |
83 { | |
84 if ( x ) | |
85 *x = SDL_DeltaX; | |
86 if ( y ) | |
87 *y = SDL_DeltaY; | |
88 SDL_DeltaX = 0; | |
89 SDL_DeltaY = 0; | |
90 return(SDL_ButtonState); | |
91 } | |
92 | |
93 static void ClipOffset(Sint16 *x, Sint16 *y) | |
94 { | |
95 /* This clips absolute mouse coordinates when the apparent | |
96 display surface is smaller than the real display surface. | |
97 */ | |
98 if ( SDL_VideoSurface->offset ) { | |
99 *y -= SDL_VideoSurface->offset/SDL_VideoSurface->pitch; | |
100 *x -= (SDL_VideoSurface->offset%SDL_VideoSurface->pitch)/ | |
101 SDL_VideoSurface->format->BytesPerPixel; | |
102 } | |
103 } | |
104 | |
105 /* These are global for SDL_eventloop.c */ | |
106 int SDL_PrivateMouseMotion(Uint8 buttonstate, int relative, Sint16 x, Sint16 y) | |
107 { | |
108 int posted; | |
109 Uint16 X, Y; | |
110 Sint16 Xrel; | |
111 Sint16 Yrel; | |
112 | |
113 /* Don't handle mouse motion if there's no cursor surface */ | |
114 if ( SDL_VideoSurface == NULL ) { | |
115 return(0); | |
116 } | |
117 | |
118 /* Default buttonstate is the current one */ | |
119 if ( ! buttonstate ) { | |
120 buttonstate = SDL_ButtonState; | |
121 } | |
122 | |
123 Xrel = x; | |
124 Yrel = y; | |
125 if ( relative ) { | |
126 /* Push the cursor around */ | |
127 x = (SDL_MouseX+x); | |
128 y = (SDL_MouseY+y); | |
129 } else { | |
130 /* Do we need to clip {x,y} ? */ | |
131 ClipOffset(&x, &y); | |
132 } | |
133 | |
134 /* Mouse coordinates range from 0 - width-1 and 0 - height-1 */ | |
135 if ( x < 0 ) | |
136 X = 0; | |
137 else | |
138 if ( x >= SDL_VideoSurface->w ) | |
139 X = SDL_VideoSurface->w-1; | |
140 else | |
141 X = (Uint16)x; | |
142 | |
143 if ( y < 0 ) | |
144 Y = 0; | |
145 else | |
146 if ( y >= SDL_VideoSurface->h ) | |
147 Y = SDL_VideoSurface->h-1; | |
148 else | |
149 Y = (Uint16)y; | |
150 | |
151 /* If not relative mode, generate relative motion from clamped X/Y. | |
152 This prevents lots of extraneous large delta relative motion when | |
153 the screen is windowed mode and the mouse is outside the window. | |
154 */ | |
155 if ( ! relative ) { | |
156 Xrel = X-SDL_MouseX; | |
157 Yrel = Y-SDL_MouseY; | |
158 } | |
159 | |
160 /* Update internal mouse state */ | |
161 SDL_ButtonState = buttonstate; | |
162 SDL_MouseX = X; | |
163 SDL_MouseY = Y; | |
164 SDL_DeltaX += Xrel; | |
165 SDL_DeltaY += Yrel; | |
166 SDL_MoveCursor(SDL_MouseX, SDL_MouseY); | |
167 | |
168 /* Post the event, if desired */ | |
169 posted = 0; | |
170 if ( SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE ) { | |
171 SDL_Event event; | |
172 memset(&event, 0, sizeof(event)); | |
173 event.type = SDL_MOUSEMOTION; | |
174 event.motion.state = buttonstate; | |
175 event.motion.x = X; | |
176 event.motion.y = Y; | |
177 event.motion.xrel = Xrel; | |
178 event.motion.yrel = Yrel; | |
179 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { | |
180 posted = 1; | |
181 SDL_PushEvent(&event); | |
182 } | |
183 } | |
184 return(posted); | |
185 } | |
186 | |
187 int SDL_PrivateMouseButton(Uint8 state, Uint8 button, Sint16 x, Sint16 y) | |
188 { | |
189 SDL_Event event; | |
190 int posted; | |
191 int move_mouse; | |
192 Uint8 buttonstate; | |
193 | |
194 memset(&event, 0, sizeof(event)); | |
195 | |
196 /* Check parameters */ | |
197 if ( x || y ) { | |
198 ClipOffset(&x, &y); | |
199 move_mouse = 1; | |
200 /* Mouse coordinates range from 0 - width-1 and 0 - height-1 */ | |
201 if ( x < 0 ) | |
202 x = 0; | |
203 else | |
204 if ( x >= SDL_VideoSurface->w ) | |
205 x = SDL_VideoSurface->w-1; | |
206 | |
207 if ( y < 0 ) | |
208 y = 0; | |
209 else | |
210 if ( y >= SDL_VideoSurface->h ) | |
211 y = SDL_VideoSurface->h-1; | |
212 } else { | |
213 move_mouse = 0; | |
214 } | |
215 if ( ! x ) | |
216 x = SDL_MouseX; | |
217 if ( ! y ) | |
218 y = SDL_MouseY; | |
219 | |
220 /* Figure out which event to perform */ | |
221 buttonstate = SDL_ButtonState; | |
222 switch ( state ) { | |
223 case SDL_PRESSED: | |
224 event.type = SDL_MOUSEBUTTONDOWN; | |
225 buttonstate |= SDL_BUTTON(button); | |
226 break; | |
227 case SDL_RELEASED: | |
228 event.type = SDL_MOUSEBUTTONUP; | |
229 buttonstate &= ~SDL_BUTTON(button); | |
230 break; | |
231 default: | |
232 /* Invalid state -- bail */ | |
233 return(0); | |
234 } | |
235 | |
236 /* Update internal mouse state */ | |
237 SDL_ButtonState = buttonstate; | |
238 if ( move_mouse ) { | |
239 SDL_MouseX = x; | |
240 SDL_MouseY = y; | |
241 SDL_MoveCursor(SDL_MouseX, SDL_MouseY); | |
242 } | |
243 | |
244 /* Post the event, if desired */ | |
245 posted = 0; | |
246 if ( SDL_ProcessEvents[event.type] == SDL_ENABLE ) { | |
247 event.button.state = state; | |
248 event.button.button = button; | |
249 event.button.x = x; | |
250 event.button.y = y; | |
251 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { | |
252 posted = 1; | |
253 SDL_PushEvent(&event); | |
254 } | |
255 } | |
256 return(posted); | |
257 } | |
258 |