Mercurial > sdl-ios-xcode
annotate src/video/aalib/SDL_aaevents.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 | f6ffac90895c |
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 /* Handle the event stream, converting AA events into SDL events */ | |
29 | |
30 #include <stdio.h> | |
31 | |
32 #include <aalib.h> | |
33 | |
34 #include "SDL.h" | |
35 #include "SDL_sysevents.h" | |
36 #include "SDL_events_c.h" | |
37 #include "SDL_aavideo.h" | |
38 #include "SDL_aaevents_c.h" | |
39 | |
40 /* The translation tables from a console scancode to a SDL keysym */ | |
41 static SDLKey keymap[401]; | |
42 | |
43 static SDL_keysym *TranslateKey(int scancode, SDL_keysym *keysym); | |
44 | |
45 | |
46 void AA_PumpEvents(_THIS) | |
47 { | |
48 int posted = 0; | |
49 int mouse_button, mouse_x, mouse_y; | |
50 int evt; | |
51 SDL_keysym keysym; | |
52 | |
53 static int prev_button = -1, prev_x = -1, prev_y = -1; | |
54 | |
55 if( ! this->screen ) /* Wait till we got the screen initialized */ | |
56 return; | |
57 | |
58 do { | |
59 posted = 0; | |
60 /* Gather events */ | |
61 | |
62 /* Get mouse status */ | |
63 SDL_mutexP(AA_mutex); | |
64 aa_getmouse (AA_context, &mouse_x, &mouse_y, &mouse_button); | |
65 SDL_mutexV(AA_mutex); | |
66 mouse_x = mouse_x * this->screen->w / aa_scrwidth (AA_context); | |
67 mouse_y = mouse_y * this->screen->h / aa_scrheight (AA_context); | |
68 | |
69 /* Compare against previous state and generate events */ | |
70 if( prev_button != mouse_button ) { | |
71 if( mouse_button & AA_BUTTON1 ) { | |
72 if ( ! (prev_button & AA_BUTTON1) ) { | |
73 posted += SDL_PrivateMouseButton(SDL_PRESSED, 1, 0, 0); | |
74 } | |
75 } else { | |
76 if ( prev_button & AA_BUTTON1 ) { | |
77 posted += SDL_PrivateMouseButton(SDL_RELEASED, 1, 0, 0); | |
78 } | |
79 } | |
80 if( mouse_button & AA_BUTTON2 ) { | |
81 if ( ! (prev_button & AA_BUTTON2) ) { | |
82 posted += SDL_PrivateMouseButton(SDL_PRESSED, 2, 0, 0); | |
83 } | |
84 } else { | |
85 if ( prev_button & AA_BUTTON2 ) { | |
86 posted += SDL_PrivateMouseButton(SDL_RELEASED, 2, 0, 0); | |
87 } | |
88 } | |
89 if( mouse_button & AA_BUTTON3 ) { | |
90 if ( ! (prev_button & AA_BUTTON3) ) { | |
91 posted += SDL_PrivateMouseButton(SDL_PRESSED, 3, 0, 0); | |
92 } | |
93 } else { | |
94 if ( prev_button & AA_BUTTON3 ) { | |
95 posted += SDL_PrivateMouseButton(SDL_RELEASED, 3, 0, 0); | |
96 } | |
97 } | |
98 } | |
99 if ( prev_x != mouse_x || prev_y != mouse_y ) { | |
100 posted += SDL_PrivateMouseMotion(0, 0, mouse_x, mouse_y); | |
101 } | |
102 | |
103 prev_button = mouse_button; | |
104 prev_x = mouse_x; prev_y = mouse_y; | |
105 | |
106 /* Get keyboard event */ | |
107 SDL_mutexP(AA_mutex); | |
108 evt = aa_getevent(AA_context, 0); | |
109 SDL_mutexV(AA_mutex); | |
110 if ( (evt > AA_NONE) && (evt < AA_RELEASE) && (evt != AA_MOUSE) && (evt != AA_RESIZE) ) { | |
111 /* Key pressed */ | |
112 /* printf("Key pressed: %d (%c)\n", evt, evt); */ | |
113 posted += SDL_PrivateKeyboard(SDL_PRESSED, TranslateKey(evt, &keysym)); | |
114 } else if ( evt >= AA_RELEASE ) { | |
115 /* Key released */ | |
116 evt &= ~AA_RELEASE; | |
117 /* printf("Key released: %d (%c)\n", evt, evt); */ | |
118 posted += SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(evt, &keysym)); | |
119 } | |
120 } while ( posted ); | |
121 } | |
122 | |
123 void AA_InitOSKeymap(_THIS) | |
124 { | |
125 int i; | |
126 static const char *std_keys = " 01234567890&#'()_-|$*+-=/\\:;.,!?<>{}[]@~%^\x9"; | |
127 const char *std; | |
128 | |
129 /* Initialize the AAlib key translation table */ | |
130 for ( i=0; i<SDL_TABLESIZE(keymap); ++i ) | |
131 keymap[i] = SDLK_UNKNOWN; | |
132 | |
133 keymap[AA_ESC] = SDLK_ESCAPE; | |
134 keymap[AA_UP] = SDLK_UP; | |
135 keymap[AA_DOWN] = SDLK_DOWN; | |
136 keymap[AA_LEFT] = SDLK_LEFT; | |
137 keymap[AA_RIGHT] = SDLK_RIGHT; | |
138 | |
139 /* Alphabet keys */ | |
140 for ( i = 0; i<26; ++i ){ | |
141 keymap['a' + i] = SDLK_a+i; | |
142 keymap['A' + i] = SDLK_a+i; | |
143 } | |
144 /* Function keys */ | |
145 for ( i = 0; i<12; ++i ){ | |
146 keymap[334 + i] = SDLK_F1+i; | |
147 } | |
148 /* Keys that have the same symbols and don't have to be translated */ | |
149 for( std = std_keys; *std; std ++ ) { | |
150 keymap[*std] = *std; | |
151 } | |
152 | |
153 keymap[13] = SDLK_RETURN; | |
154 keymap[AA_BACKSPACE] = SDLK_BACKSPACE; | |
155 | |
156 keymap[369] = SDLK_LSHIFT; | |
157 keymap[370] = SDLK_RSHIFT; | |
158 keymap[371] = SDLK_LCTRL; | |
159 keymap[372] = SDLK_RCTRL; | |
160 keymap[377] = SDLK_LALT; | |
161 keymap[270] = SDLK_RALT; | |
162 keymap[271] = SDLK_NUMLOCK; | |
163 keymap[373] = SDLK_CAPSLOCK; | |
164 keymap[164] = SDLK_SCROLLOCK; | |
165 | |
166 keymap[243] = SDLK_INSERT; | |
167 keymap[304] = SDLK_DELETE; | |
168 keymap[224] = SDLK_HOME; | |
169 keymap[231] = SDLK_END; | |
170 keymap[229] = SDLK_PAGEUP; | |
171 keymap[230] = SDLK_PAGEDOWN; | |
172 | |
173 keymap[241] = SDLK_PRINT; | |
174 keymap[163] = SDLK_BREAK; | |
175 | |
176 keymap[302] = SDLK_KP0; | |
177 keymap[300] = SDLK_KP1; | |
178 keymap[297] = SDLK_KP2; | |
179 keymap[299] = SDLK_KP3; | |
180 keymap[294] = SDLK_KP4; | |
181 keymap[301] = SDLK_KP5; | |
182 keymap[296] = SDLK_KP6; | |
183 keymap[293] = SDLK_KP7; | |
184 keymap[295] = SDLK_KP8; | |
185 keymap[298] = SDLK_KP9; | |
186 } | |
187 | |
188 static SDL_keysym *TranslateKey(int scancode, SDL_keysym *keysym) | |
189 { | |
190 /* Set the keysym information */ | |
191 keysym->scancode = scancode; | |
192 keysym->sym = keymap[scancode]; | |
193 keysym->mod = KMOD_NONE; | |
194 | |
195 /* If UNICODE is on, get the UNICODE value for the key */ | |
196 keysym->unicode = 0; | |
197 if ( SDL_TranslateUNICODE ) { | |
198 /* Populate the unicode field with the ASCII value */ | |
199 keysym->unicode = scancode; | |
200 } | |
201 return(keysym); | |
202 } |