Mercurial > sdl-ios-xcode
annotate src/events/SDL_mouse.c @ 1336:3692456e7b0f
Use SDL_ prefixed versions of C library functions.
FIXME:
Change #include <stdlib.h> to #include "SDL_stdlib.h"
Change #include <string.h> to #include "SDL_string.h"
Make sure nothing else broke because of this...
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 07 Feb 2006 06:59:48 +0000 |
parents | 450721ad5436 |
children | c71e05b4dc2e |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1283
diff
changeset
|
3 Copyright (C) 1997-2006 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 */ |
22 | |
23 /* General mouse handling code for SDL */ | |
24 | |
25 #include "SDL_events.h" | |
1330
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
26 #include "SDL_string.h" |
0 | 27 #include "SDL_events_c.h" |
28 #include "SDL_cursor_c.h" | |
29 #include "SDL_sysvideo.h" | |
30 | |
31 | |
32 /* 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
|
33 static Sint16 SDL_MouseX = -1; |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
34 static Sint16 SDL_MouseY = -1; |
0 | 35 static Sint16 SDL_DeltaX = 0; |
36 static Sint16 SDL_DeltaY = 0; | |
37 static Uint8 SDL_ButtonState = 0; | |
38 | |
39 | |
40 /* Public functions */ | |
41 int SDL_MouseInit(void) | |
42 { | |
43 /* 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
|
44 SDL_MouseX = -1; |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
45 SDL_MouseY = -1; |
0 | 46 SDL_DeltaX = 0; |
47 SDL_DeltaY = 0; | |
48 SDL_ButtonState = 0; | |
49 | |
50 /* That's it! */ | |
51 return(0); | |
52 } | |
1123
28ac87a38c17
Date: Fri, 08 Jul 2005 22:43:48 +0200 (CEST)
Sam Lantinga <slouken@libsdl.org>
parents:
943
diff
changeset
|
53 void SDL_MouseQuit(void) |
28ac87a38c17
Date: Fri, 08 Jul 2005 22:43:48 +0200 (CEST)
Sam Lantinga <slouken@libsdl.org>
parents:
943
diff
changeset
|
54 { |
28ac87a38c17
Date: Fri, 08 Jul 2005 22:43:48 +0200 (CEST)
Sam Lantinga <slouken@libsdl.org>
parents:
943
diff
changeset
|
55 } |
0 | 56 |
460
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
57 /* 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
|
58 void SDL_ResetMouse(void) |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
59 { |
518 | 60 Uint8 i; |
460
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
61 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
|
62 if ( SDL_ButtonState & SDL_BUTTON(i) ) { |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
63 SDL_PrivateMouseButton(SDL_RELEASED, i, 0, 0); |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
64 } |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
65 } |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
66 } |
a888b3ae31ff
Reset mouse state when changing video modes
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
67 |
0 | 68 Uint8 SDL_GetMouseState (int *x, int *y) |
69 { | |
943
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
70 if ( x ) { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
71 if ( SDL_MouseX < 0 ) { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
72 *x = 0; |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
73 } else { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
74 *x = SDL_MouseX; |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
75 } |
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 ) { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
78 if ( SDL_MouseY < 0 ) { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
79 *y = 0; |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
80 } else { |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
81 *y = SDL_MouseY; |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
82 } |
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
83 } |
0 | 84 return(SDL_ButtonState); |
85 } | |
86 | |
87 Uint8 SDL_GetRelativeMouseState (int *x, int *y) | |
88 { | |
89 if ( x ) | |
90 *x = SDL_DeltaX; | |
91 if ( y ) | |
92 *y = SDL_DeltaY; | |
93 SDL_DeltaX = 0; | |
94 SDL_DeltaY = 0; | |
95 return(SDL_ButtonState); | |
96 } | |
97 | |
98 static void ClipOffset(Sint16 *x, Sint16 *y) | |
99 { | |
100 /* This clips absolute mouse coordinates when the apparent | |
101 display surface is smaller than the real display surface. | |
102 */ | |
103 if ( SDL_VideoSurface->offset ) { | |
104 *y -= SDL_VideoSurface->offset/SDL_VideoSurface->pitch; | |
105 *x -= (SDL_VideoSurface->offset%SDL_VideoSurface->pitch)/ | |
106 SDL_VideoSurface->format->BytesPerPixel; | |
107 } | |
108 } | |
109 | |
110 /* These are global for SDL_eventloop.c */ | |
111 int SDL_PrivateMouseMotion(Uint8 buttonstate, int relative, Sint16 x, Sint16 y) | |
112 { | |
113 int posted; | |
114 Uint16 X, Y; | |
115 Sint16 Xrel; | |
116 Sint16 Yrel; | |
117 | |
118 /* Don't handle mouse motion if there's no cursor surface */ | |
119 if ( SDL_VideoSurface == NULL ) { | |
120 return(0); | |
121 } | |
122 | |
123 /* Default buttonstate is the current one */ | |
124 if ( ! buttonstate ) { | |
125 buttonstate = SDL_ButtonState; | |
126 } | |
127 | |
128 Xrel = x; | |
129 Yrel = y; | |
130 if ( relative ) { | |
131 /* Push the cursor around */ | |
132 x = (SDL_MouseX+x); | |
133 y = (SDL_MouseY+y); | |
134 } else { | |
135 /* Do we need to clip {x,y} ? */ | |
136 ClipOffset(&x, &y); | |
137 } | |
138 | |
139 /* Mouse coordinates range from 0 - width-1 and 0 - height-1 */ | |
140 if ( x < 0 ) | |
141 X = 0; | |
142 else | |
143 if ( x >= SDL_VideoSurface->w ) | |
144 X = SDL_VideoSurface->w-1; | |
145 else | |
146 X = (Uint16)x; | |
147 | |
148 if ( y < 0 ) | |
149 Y = 0; | |
150 else | |
151 if ( y >= SDL_VideoSurface->h ) | |
152 Y = SDL_VideoSurface->h-1; | |
153 else | |
154 Y = (Uint16)y; | |
155 | |
156 /* If not relative mode, generate relative motion from clamped X/Y. | |
157 This prevents lots of extraneous large delta relative motion when | |
158 the screen is windowed mode and the mouse is outside the window. | |
159 */ | |
943
715c32d8f26c
Date: Sun, 18 Jul 2004 00:22:07 -0400
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
160 if ( ! relative && SDL_MouseX >= 0 && SDL_MouseY >= 0 ) { |
0 | 161 Xrel = X-SDL_MouseX; |
162 Yrel = Y-SDL_MouseY; | |
163 } | |
164 | |
1283
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
165 /* 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
|
166 if ( ! Xrel && ! Yrel ) { |
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
167 #if 0 |
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
168 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
|
169 #endif |
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
170 return(0); |
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
171 } |
f214b6fae45a
Date: Fri, 14 Jan 2005 21:52:46 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1123
diff
changeset
|
172 |
0 | 173 /* Update internal mouse state */ |
174 SDL_ButtonState = buttonstate; | |
175 SDL_MouseX = X; | |
176 SDL_MouseY = Y; | |
177 SDL_DeltaX += Xrel; | |
178 SDL_DeltaY += Yrel; | |
179 SDL_MoveCursor(SDL_MouseX, SDL_MouseY); | |
180 | |
181 /* Post the event, if desired */ | |
182 posted = 0; | |
183 if ( SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE ) { | |
184 SDL_Event event; | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
185 SDL_memset(&event, 0, sizeof(event)); |
0 | 186 event.type = SDL_MOUSEMOTION; |
187 event.motion.state = buttonstate; | |
188 event.motion.x = X; | |
189 event.motion.y = Y; | |
190 event.motion.xrel = Xrel; | |
191 event.motion.yrel = Yrel; | |
192 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { | |
193 posted = 1; | |
194 SDL_PushEvent(&event); | |
195 } | |
196 } | |
197 return(posted); | |
198 } | |
199 | |
200 int SDL_PrivateMouseButton(Uint8 state, Uint8 button, Sint16 x, Sint16 y) | |
201 { | |
202 SDL_Event event; | |
203 int posted; | |
204 int move_mouse; | |
205 Uint8 buttonstate; | |
206 | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
207 SDL_memset(&event, 0, sizeof(event)); |
0 | 208 |
209 /* Check parameters */ | |
210 if ( x || y ) { | |
211 ClipOffset(&x, &y); | |
212 move_mouse = 1; | |
213 /* Mouse coordinates range from 0 - width-1 and 0 - height-1 */ | |
214 if ( x < 0 ) | |
215 x = 0; | |
216 else | |
217 if ( x >= SDL_VideoSurface->w ) | |
218 x = SDL_VideoSurface->w-1; | |
219 | |
220 if ( y < 0 ) | |
221 y = 0; | |
222 else | |
223 if ( y >= SDL_VideoSurface->h ) | |
224 y = SDL_VideoSurface->h-1; | |
225 } else { | |
226 move_mouse = 0; | |
227 } | |
228 if ( ! x ) | |
229 x = SDL_MouseX; | |
230 if ( ! y ) | |
231 y = SDL_MouseY; | |
232 | |
233 /* Figure out which event to perform */ | |
234 buttonstate = SDL_ButtonState; | |
235 switch ( state ) { | |
236 case SDL_PRESSED: | |
237 event.type = SDL_MOUSEBUTTONDOWN; | |
238 buttonstate |= SDL_BUTTON(button); | |
239 break; | |
240 case SDL_RELEASED: | |
241 event.type = SDL_MOUSEBUTTONUP; | |
242 buttonstate &= ~SDL_BUTTON(button); | |
243 break; | |
244 default: | |
245 /* Invalid state -- bail */ | |
246 return(0); | |
247 } | |
248 | |
249 /* Update internal mouse state */ | |
250 SDL_ButtonState = buttonstate; | |
251 if ( move_mouse ) { | |
252 SDL_MouseX = x; | |
253 SDL_MouseY = y; | |
254 SDL_MoveCursor(SDL_MouseX, SDL_MouseY); | |
255 } | |
256 | |
257 /* Post the event, if desired */ | |
258 posted = 0; | |
259 if ( SDL_ProcessEvents[event.type] == SDL_ENABLE ) { | |
260 event.button.state = state; | |
261 event.button.button = button; | |
262 event.button.x = x; | |
263 event.button.y = y; | |
264 if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) { | |
265 posted = 1; | |
266 SDL_PushEvent(&event); | |
267 } | |
268 } | |
269 return(posted); | |
270 } | |
271 |