Mercurial > sdl-ios-xcode
annotate src/events/SDL_mouse.c @ 563:04dcaf3da918
Massive Quartz input enhancements from Darrell Walisser. His email:
Enclosed is a patch that addresses the following:
--Various minor cleanups.
Removed dead/obsolete code, made some style cleanups
--Mouse Events
Now keep track of what button(s) were pressed so we know when to send
the mouse up event. This fixes the case where the mouse is dragged
outside of the game window and released (in which case we want to send
the mouse up event even though the mouse is outside the game window).
--Input Grabbing
Here is my take on the grabbing situation, which is the basis for the
new implementation.
There are 3 grab states, ungrabbed (UG), visible (VG), and invisible
(IG). Both VG and IG keep the mouse constrained to the window and
produce relative motion events. In VG the cursor is visible (duh), in
IG it is not. In VG, absolute motion events also work.
There are 6 actions that can affect grabbing:
1. Set Fullscreen/Window (F/W). In fullscreen, a visible grab should do
nothing. However, a fullscreen visible grab can be treated just like a
windowed visible grab, which is what I have done to help simplify
things.
2. Cursor hide/show (H/S). If the cursor is hidden when grabbing, the
grab is an invisible grab. If the cursor is visible, the grab should
just constrain the mouse to the window.
3. Input grab/ungrab(G/U). If grabbed, the cursor should be confined to
the window as should the keyboard input. On Mac OS X, the keyboard
input is implicitly grabbed by confining the cursor, except for
command-tab which can switch away from the application. Should the
window come to the foreground if the application is deactivated and
grab input is called? This isn't necessary in this implementation
because the grab state will be asserted upon activation.
Using my notation, these are all the cases that need to be handled
(state + action = new state).
UG+U = UG
UG+G = VG or IG, if cursor is visible or not
UG+H = UG
UG+S = UG
VG+U = UG
VG+G = VG
VG+H = IG
VG+S = VG
IG+U = UG
IG+G = IG
IG+H = IG
IG+S = VG
The cases that result in the same state can be ignored in the code,
which cuts it down to just 5 cases.
Another issue is what happens when the app loses/gains input focus from
deactivate/activate or iconify/deiconify. I think that if input focus
is ever lost (outside of SDL's control), the grab state should be
suspended and the cursor should become visible and active again. When
regained, the cursor should reappear in its original location and/or
grab state. This way, when reactivating the cursor is still in the same
position as before so apps shouldn't get confused when the next motion
event comes in. This is what I've done in this patch.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Fri, 27 Dec 2002 20:52:41 +0000 |
parents | 300b46757b7a |
children | b8d311d90021 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
297
f6ffac90895c
Updated copyright information for 2002
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 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 |