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