Mercurial > sdl-ios-xcode
annotate src/events/SDL_mouse.c @ 1283:f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
From: "SkunkGuru"
Subject: [SDL] Repeated mousemotion event on notebook
it seems that every ~500ms something fires a mousemotion event,
but with the same x and y position.
I tryed with both directx and windib video driver.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 29 Jan 2006 08:18:06 +0000 |
parents | 28ac87a38c17 |
children | c9b51268668f |
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 } | |
1123
28ac87a38c17
Date: Fri, 08 Jul 2005 22:43:48 +0200 (CEST)
Sam Lantinga <slouken@libsdl.org>
parents:
943
diff
changeset
|
61 void SDL_MouseQuit(void) |
28ac87a38c17
Date: Fri, 08 Jul 2005 22:43:48 +0200 (CEST)
Sam Lantinga <slouken@libsdl.org>
parents:
943
diff
changeset
|
62 { |
28ac87a38c17
Date: Fri, 08 Jul 2005 22:43:48 +0200 (CEST)
Sam Lantinga <slouken@libsdl.org>
parents:
943
diff
changeset
|
63 } |
0 | 64 |
460
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
65 /* 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
|
66 void SDL_ResetMouse(void) |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
67 { |
518 | 68 Uint8 i; |
460
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
69 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
|
70 if ( SDL_ButtonState & SDL_BUTTON(i) ) { |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
71 SDL_PrivateMouseButton(SDL_RELEASED, i, 0, 0); |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
72 } |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
73 } |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
74 } |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
75 |
0 | 76 Uint8 SDL_GetMouseState (int *x, int *y) |
77 { | |
943
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
78 if ( x ) { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
79 if ( SDL_MouseX < 0 ) { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
80 *x = 0; |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
81 } else { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
82 *x = SDL_MouseX; |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
83 } |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
84 } |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
85 if ( y ) { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
86 if ( SDL_MouseY < 0 ) { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
87 *y = 0; |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
88 } else { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
89 *y = SDL_MouseY; |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
90 } |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
91 } |
0 | 92 return(SDL_ButtonState); |
93 } | |
94 | |
95 Uint8 SDL_GetRelativeMouseState (int *x, int *y) | |
96 { | |
97 if ( x ) | |
98 *x = SDL_DeltaX; | |
99 if ( y ) | |
100 *y = SDL_DeltaY; | |
101 SDL_DeltaX = 0; | |
102 SDL_DeltaY = 0; | |
103 return(SDL_ButtonState); | |
104 } | |
105 | |
106 static void ClipOffset(Sint16 *x, Sint16 *y) | |
107 { | |
108 /* This clips absolute mouse coordinates when the apparent | |
109 display surface is smaller than the real display surface. | |
110 */ | |
111 if ( SDL_VideoSurface->offset ) { | |
112 *y -= SDL_VideoSurface->offset/SDL_VideoSurface->pitch; | |
113 *x -= (SDL_VideoSurface->offset%SDL_VideoSurface->pitch)/ | |
114 SDL_VideoSurface->format->BytesPerPixel; | |
115 } | |
116 } | |
117 | |
118 /* These are global for SDL_eventloop.c */ | |
119 int SDL_PrivateMouseMotion(Uint8 buttonstate, int relative, Sint16 x, Sint16 y) | |
120 { | |
121 int posted; | |
122 Uint16 X, Y; | |
123 Sint16 Xrel; | |
124 Sint16 Yrel; | |
125 | |
126 /* Don't handle mouse motion if there's no cursor surface */ | |
127 if ( SDL_VideoSurface == NULL ) { | |
128 return(0); | |
129 } | |
130 | |
131 /* Default buttonstate is the current one */ | |
132 if ( ! buttonstate ) { | |
133 buttonstate = SDL_ButtonState; | |
134 } | |
135 | |
136 Xrel = x; | |
137 Yrel = y; | |
138 if ( relative ) { | |
139 /* Push the cursor around */ | |
140 x = (SDL_MouseX+x); | |
141 y = (SDL_MouseY+y); | |
142 } else { | |
143 /* Do we need to clip {x,y} ? */ | |
144 ClipOffset(&x, &y); | |
145 } | |
146 | |
147 /* Mouse coordinates range from 0 - width-1 and 0 - height-1 */ | |
148 if ( x < 0 ) | |
149 X = 0; | |
150 else | |
151 if ( x >= SDL_VideoSurface->w ) | |
152 X = SDL_VideoSurface->w-1; | |
153 else | |
154 X = (Uint16)x; | |
155 | |
156 if ( y < 0 ) | |
157 Y = 0; | |
158 else | |
159 if ( y >= SDL_VideoSurface->h ) | |
160 Y = SDL_VideoSurface->h-1; | |
161 else | |
162 Y = (Uint16)y; | |
163 | |
164 /* If not relative mode, generate relative motion from clamped X/Y. | |
165 This prevents lots of extraneous large delta relative motion when | |
166 the screen is windowed mode and the mouse is outside the window. | |
167 */ | |
943
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
168 if ( ! relative && SDL_MouseX >= 0 && SDL_MouseY >= 0 ) { |
0 | 169 Xrel = X-SDL_MouseX; |
170 Yrel = Y-SDL_MouseY; | |
171 } | |
172 | |
1283
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
173 /* 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
|
174 if ( ! Xrel && ! Yrel ) { |
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
175 #if 0 |
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
176 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
|
177 #endif |
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
178 return(0); |
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
179 } |
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
180 |
0 | 181 /* Update internal mouse state */ |
182 SDL_ButtonState = buttonstate; | |
183 SDL_MouseX = X; | |
184 SDL_MouseY = Y; | |
185 SDL_DeltaX += Xrel; | |
186 SDL_DeltaY += Yrel; | |
187 SDL_MoveCursor(SDL_MouseX, SDL_MouseY); | |
188 | |
189 /* Post the event, if desired */ | |
190 posted = 0; | |
191 if ( SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE ) { | |
192 SDL_Event event; | |
193 memset(&event, 0, sizeof(event)); | |
194 event.type = SDL_MOUSEMOTION; | |
195 event.motion.state = buttonstate; | |
196 event.motion.x = X; | |
197 event.motion.y = Y; | |
198 event.motion.xrel = Xrel; | |
199 event.motion.yrel = Yrel; | |
200 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { | |
201 posted = 1; | |
202 SDL_PushEvent(&event); | |
203 } | |
204 } | |
205 return(posted); | |
206 } | |
207 | |
208 int SDL_PrivateMouseButton(Uint8 state, Uint8 button, Sint16 x, Sint16 y) | |
209 { | |
210 SDL_Event event; | |
211 int posted; | |
212 int move_mouse; | |
213 Uint8 buttonstate; | |
214 | |
215 memset(&event, 0, sizeof(event)); | |
216 | |
217 /* Check parameters */ | |
218 if ( x || y ) { | |
219 ClipOffset(&x, &y); | |
220 move_mouse = 1; | |
221 /* Mouse coordinates range from 0 - width-1 and 0 - height-1 */ | |
222 if ( x < 0 ) | |
223 x = 0; | |
224 else | |
225 if ( x >= SDL_VideoSurface->w ) | |
226 x = SDL_VideoSurface->w-1; | |
227 | |
228 if ( y < 0 ) | |
229 y = 0; | |
230 else | |
231 if ( y >= SDL_VideoSurface->h ) | |
232 y = SDL_VideoSurface->h-1; | |
233 } else { | |
234 move_mouse = 0; | |
235 } | |
236 if ( ! x ) | |
237 x = SDL_MouseX; | |
238 if ( ! y ) | |
239 y = SDL_MouseY; | |
240 | |
241 /* Figure out which event to perform */ | |
242 buttonstate = SDL_ButtonState; | |
243 switch ( state ) { | |
244 case SDL_PRESSED: | |
245 event.type = SDL_MOUSEBUTTONDOWN; | |
246 buttonstate |= SDL_BUTTON(button); | |
247 break; | |
248 case SDL_RELEASED: | |
249 event.type = SDL_MOUSEBUTTONUP; | |
250 buttonstate &= ~SDL_BUTTON(button); | |
251 break; | |
252 default: | |
253 /* Invalid state -- bail */ | |
254 return(0); | |
255 } | |
256 | |
257 /* Update internal mouse state */ | |
258 SDL_ButtonState = buttonstate; | |
259 if ( move_mouse ) { | |
260 SDL_MouseX = x; | |
261 SDL_MouseY = y; | |
262 SDL_MoveCursor(SDL_MouseX, SDL_MouseY); | |
263 } | |
264 | |
265 /* Post the event, if desired */ | |
266 posted = 0; | |
267 if ( SDL_ProcessEvents[event.type] == SDL_ENABLE ) { | |
268 event.button.state = state; | |
269 event.button.button = button; | |
270 event.button.x = x; | |
271 event.button.y = y; | |
272 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { | |
273 posted = 1; | |
274 SDL_PushEvent(&event); | |
275 } | |
276 } | |
277 return(posted); | |
278 } | |
279 |