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