comparison src/video/quartz/SDL_QuartzEvents.m @ 47:45b1c4303f87

Added initial support for Quartz video (thanks Darrell!)
author Sam Lantinga <slouken@lokigames.com>
date Thu, 07 Jun 2001 14:28:11 +0000
parents
children aac75d5f7869
comparison
equal deleted inserted replaced
46:3dcf26fa9d15 47:45b1c4303f87
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
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
20 slouken@devolution.com
21 */
22
23 #include "SDL_QuartzKeys.h"
24
25 static void QZ_InitOSKeymap (_THIS) {
26 int i;
27
28 for ( i=0; i<SDL_TABLESIZE(keymap); ++i )
29 keymap[i] = SDLK_UNKNOWN;
30
31 // This keymap is almost exactly the same as the OS 9 one
32 keymap[QZ_ESCAPE] = SDLK_ESCAPE;
33 keymap[QZ_F1] = SDLK_F1;
34 keymap[QZ_F2] = SDLK_F2;
35 keymap[QZ_F3] = SDLK_F3;
36 keymap[QZ_F4] = SDLK_F4;
37 keymap[QZ_F5] = SDLK_F5;
38 keymap[QZ_F6] = SDLK_F6;
39 keymap[QZ_F7] = SDLK_F7;
40 keymap[QZ_F8] = SDLK_F8;
41 keymap[QZ_F9] = SDLK_F9;
42 keymap[QZ_F10] = SDLK_F10;
43 keymap[QZ_F11] = SDLK_F11;
44 keymap[QZ_F12] = SDLK_F12;
45 keymap[QZ_PRINT] = SDLK_PRINT;
46 keymap[QZ_SCROLLOCK] = SDLK_SCROLLOCK;
47 keymap[QZ_PAUSE] = SDLK_PAUSE;
48 keymap[QZ_POWER] = SDLK_POWER;
49 keymap[QZ_BACKQUOTE] = SDLK_BACKQUOTE;
50 keymap[QZ_1] = SDLK_1;
51 keymap[QZ_2] = SDLK_2;
52 keymap[QZ_3] = SDLK_3;
53 keymap[QZ_4] = SDLK_4;
54 keymap[QZ_5] = SDLK_5;
55 keymap[QZ_6] = SDLK_6;
56 keymap[QZ_7] = SDLK_7;
57 keymap[QZ_8] = SDLK_8;
58 keymap[QZ_9] = SDLK_9;
59 keymap[QZ_0] = SDLK_0;
60 keymap[QZ_MINUS] = SDLK_MINUS;
61 keymap[QZ_EQUALS] = SDLK_EQUALS;
62 keymap[QZ_BACKSPACE] = SDLK_BACKSPACE;
63 keymap[QZ_INSERT] = SDLK_INSERT;
64 keymap[QZ_HOME] = SDLK_HOME;
65 keymap[QZ_PAGEUP] = SDLK_PAGEUP;
66 keymap[QZ_NUMLOCK] = SDLK_NUMLOCK;
67 keymap[QZ_KP_EQUALS] = SDLK_KP_EQUALS;
68 keymap[QZ_KP_DIVIDE] = SDLK_KP_DIVIDE;
69 keymap[QZ_KP_MULTIPLY] = SDLK_KP_MULTIPLY;
70 keymap[QZ_TAB] = SDLK_TAB;
71 keymap[QZ_q] = SDLK_q;
72 keymap[QZ_w] = SDLK_w;
73 keymap[QZ_e] = SDLK_e;
74 keymap[QZ_r] = SDLK_r;
75 keymap[QZ_t] = SDLK_t;
76 keymap[QZ_y] = SDLK_y;
77 keymap[QZ_u] = SDLK_u;
78 keymap[QZ_i] = SDLK_i;
79 keymap[QZ_o] = SDLK_o;
80 keymap[QZ_p] = SDLK_p;
81 keymap[QZ_LEFTBRACKET] = SDLK_LEFTBRACKET;
82 keymap[QZ_RIGHTBRACKET] = SDLK_RIGHTBRACKET;
83 keymap[QZ_BACKSLASH] = SDLK_BACKSLASH;
84 keymap[QZ_DELETE] = SDLK_DELETE;
85 keymap[QZ_END] = SDLK_END;
86 keymap[QZ_PAGEDOWN] = SDLK_PAGEDOWN;
87 keymap[QZ_KP7] = SDLK_KP7;
88 keymap[QZ_KP8] = SDLK_KP8;
89 keymap[QZ_KP9] = SDLK_KP9;
90 keymap[QZ_KP_MINUS] = SDLK_KP_MINUS;
91 keymap[QZ_CAPSLOCK] = SDLK_CAPSLOCK;
92 keymap[QZ_a] = SDLK_a;
93 keymap[QZ_s] = SDLK_s;
94 keymap[QZ_d] = SDLK_d;
95 keymap[QZ_f] = SDLK_f;
96 keymap[QZ_g] = SDLK_g;
97 keymap[QZ_h] = SDLK_h;
98 keymap[QZ_j] = SDLK_j;
99 keymap[QZ_k] = SDLK_k;
100 keymap[QZ_l] = SDLK_l;
101 keymap[QZ_SEMICOLON] = SDLK_SEMICOLON;
102 keymap[QZ_QUOTE] = SDLK_QUOTE;
103 keymap[QZ_RETURN] = SDLK_RETURN;
104 keymap[QZ_KP4] = SDLK_KP4;
105 keymap[QZ_KP5] = SDLK_KP5;
106 keymap[QZ_KP6] = SDLK_KP6;
107 keymap[QZ_KP_PLUS] = SDLK_KP_PLUS;
108 keymap[QZ_LSHIFT] = SDLK_LSHIFT;
109 keymap[QZ_z] = SDLK_z;
110 keymap[QZ_x] = SDLK_x;
111 keymap[QZ_c] = SDLK_c;
112 keymap[QZ_v] = SDLK_v;
113 keymap[QZ_b] = SDLK_b;
114 keymap[QZ_n] = SDLK_n;
115 keymap[QZ_m] = SDLK_m;
116 keymap[QZ_COMMA] = SDLK_COMMA;
117 keymap[QZ_PERIOD] = SDLK_PERIOD;
118 keymap[QZ_SLASH] = SDLK_SLASH;
119 keymap[QZ_UP] = SDLK_UP;
120 keymap[QZ_KP1] = SDLK_KP1;
121 keymap[QZ_KP2] = SDLK_KP2;
122 keymap[QZ_KP3] = SDLK_KP3;
123 keymap[QZ_KP_ENTER] = SDLK_KP_ENTER;
124 keymap[QZ_LCTRL] = SDLK_LCTRL;
125 keymap[QZ_LALT] = SDLK_LALT;
126 keymap[QZ_LMETA] = SDLK_LMETA;
127 keymap[QZ_SPACE] = SDLK_SPACE;
128 keymap[QZ_LEFT] = SDLK_LEFT;
129 keymap[QZ_DOWN] = SDLK_DOWN;
130 keymap[QZ_RIGHT] = SDLK_RIGHT;
131 keymap[QZ_KP0] = SDLK_KP0;
132 keymap[QZ_KP_PERIOD] = SDLK_KP_PERIOD;
133 keymap[QZ_IBOOK_ENTER] = SDLK_KP_ENTER;
134 keymap[QZ_IBOOK_RIGHT] = SDLK_RIGHT;
135 keymap[QZ_IBOOK_DOWN] = SDLK_DOWN;
136 keymap[QZ_IBOOK_UP] = SDLK_UP;
137 keymap[QZ_IBOOK_LEFT] = SDLK_LEFT;
138 }
139
140 static void QZ_DoKey (int state, NSEvent *event) {
141
142 NSString *chars;
143 int i;
144 SDL_keysym key;
145
146 /* An event can contain multiple characters */
147 /* I'll ignore this fact for now, since there is only one virtual key code per event */
148 chars = [ event characters ];
149 for (i =0; i < 1 /*[ chars length ] */; i++) {
150
151 key.scancode = [ event keyCode ];
152 key.sym = keymap [ key.scancode ];
153 key.unicode = [ chars characterAtIndex:i];
154 key.mod = KMOD_NONE;
155
156 SDL_PrivateKeyboard (state, &key);
157 }
158 }
159
160 static void QZ_DoModifiers (unsigned int newMods) {
161
162 const int offset = 18;
163 const int mapping[] = { SDLK_LSHIFT, SDLK_LCTRL, SDLK_LALT, 0, SDLK_LMETA } ;
164
165 int bit;
166 SDL_keysym key;
167 key.scancode = 0;
168 key.sym = SDLK_UNKNOWN;
169 key.unicode = 0;
170 key.mod = KMOD_NONE;
171
172 /* Iterate through the bits, testing each against the current modifiers */
173 for (bit = NSShiftKeyMask; bit <= NSCommandKeyMask; bit <<= 1) {
174
175 unsigned int currentMask, newMask;
176
177 currentMask = currentMods & bit;
178 newMask = newMods & bit;
179
180 if ( currentMask &&
181 currentMask != newMask ) { /* modifier up event */
182
183 key.sym = mapping[ currentMask >> offset ];
184 SDL_PrivateKeyboard (SDL_RELEASED, &key);
185 }
186 else
187 if ( newMask &&
188 currentMask != newMask ) { /* modifier down event */
189
190 key.sym = mapping [ newMask >> offset ];
191 SDL_PrivateKeyboard (SDL_PRESSED, &key);
192 }
193 }
194
195 currentMods = newMods;
196 }
197
198 static void QZ_DoActivate (_THIS, NSPoint p) {
199
200 inForeground = YES;
201
202 /* Regrab the mouse */
203 if (currentGrabMode == SDL_GRAB_ON) {
204 QZ_WarpWMCursor (this, SDL_VideoSurface->w / 2, SDL_VideoSurface->h / 2);
205 CGAssociateMouseAndMouseCursorPosition (0);
206 }
207
208 SDL_PrivateAppActive (1, SDL_APPINPUTFOCUS);
209 }
210
211 static void QZ_DoDeactivate (_THIS) {
212
213 inForeground = NO;
214
215 /* Ungrab mouse if it is grabbed */
216 if (currentGrabMode == SDL_GRAB_ON) {
217 CGAssociateMouseAndMouseCursorPosition (1);
218 }
219
220 SDL_PrivateAppActive (0, SDL_APPINPUTFOCUS);
221 }
222
223 static void QZ_PumpEvents (_THIS) {
224
225 NSDate *distantPast = [ NSDate distantPast ];
226
227 //NSAutoreleasePool *pool;
228 //pool = [ [ NSAutoreleasePool alloc ] init ];
229
230 NSEvent *event;
231 NSRect winRect;
232 NSRect titleBarRect;
233
234 winRect = NSMakeRect (0, 0, SDL_VideoSurface->w + 1, SDL_VideoSurface->h + 1);
235 titleBarRect = NSMakeRect ( 0, SDL_VideoSurface->h, SDL_VideoSurface->w,
236 SDL_VideoSurface->h + 22 );
237
238 do {
239
240 /* Poll for an event. This will not block */
241 event = [ NSApp nextEventMatchingMask:NSAnyEventMask
242 untilDate:distantPast
243 inMode: NSDefaultRunLoopMode dequeue:YES ];
244
245 if (event != nil) {
246 unsigned int type;
247 NSPoint p;
248
249 type = [ event type ];
250 p = [ event locationInWindow ];
251
252 #define DO_MOUSE_DOWN(button, sendToWindow) \
253 if ( inForeground ) { \
254 if ( NSPointInRect(p,winRect)) \
255 SDL_PrivateMouseButton (SDL_PRESSED, button, p.x, SDL_VideoSurface->h - p.y); \
256 else if (sendToWindow) \
257 [ window sendEvent:event ]; \
258 } \
259 else { \
260 QZ_DoActivate (this, p); \
261 }
262
263 #define DO_MOUSE_UP(button, sendToWindow) \
264 if ( ! NSPointInRect (p, titleBarRect) ) \
265 SDL_PrivateMouseButton (SDL_RELEASED, button, p.x, SDL_VideoSurface->h - p.y); \
266 if (sendToWindow) \
267 [ window sendEvent:event ]
268
269 switch ( type) {
270
271 case NSLeftMouseDown:
272 if ( NSCommandKeyMask & currentMods ) {
273 DO_MOUSE_DOWN (3, 0);
274 }
275 else if ( NSAlternateKeyMask & currentMods ) {
276 DO_MOUSE_DOWN (2, 0);
277 }
278 else {
279 DO_MOUSE_DOWN (1, 1);
280 }
281 break;
282 case 25: DO_MOUSE_DOWN (2, 0); break;
283 case NSRightMouseDown: DO_MOUSE_DOWN (3, 0); break;
284 case NSLeftMouseUp:
285 if ( NSCommandKeyMask & currentMods ) {
286 DO_MOUSE_UP (3, 0);
287 }
288 else if ( NSAlternateKeyMask & currentMods ) {
289 DO_MOUSE_UP (2, 0);
290 }
291 else
292 DO_MOUSE_UP (1, 1);
293 break;
294 case 26: DO_MOUSE_UP (2, 0); break;
295 case NSRightMouseUp: DO_MOUSE_UP (3, 0); break;
296 case NSSystemDefined:
297 //if ([event subtype] == 7) {
298 // unsigned int buttons; // up to 32 mouse button states!
299 // buttons = [ event data2 ];
300 //}
301 break;
302 case NSLeftMouseDragged:
303 case NSRightMouseDragged:
304 case 27:
305 SDL_PrivateMouseMotion (SDL_PRESSED, 0, p.x, SDL_VideoSurface->h - p.y);
306 break;
307 case NSMouseMoved:
308 {
309 static int moves = 0;
310
311 if ( (moves % 10) == 0 ) {
312 SDL_PrivateMouseMotion (SDL_RELEASED, 0, p.x, SDL_VideoSurface->h - p.y);
313 moves = 0;
314 }
315 else {
316 CGMouseDelta dx, dy;
317 CGGetLastMouseDelta (&dx, &dy);
318 SDL_PrivateMouseMotion (SDL_RELEASED, 1, dx, dy);
319 moves++;
320 }
321 }
322 break;
323 case NSScrollWheel:
324 {
325 if (NSPointInRect(p, winRect)) {
326 float dy;
327 dy = [ event deltaY ];
328 if ( dy > 0.0 ) /* Scroll up */
329 SDL_PrivateMouseButton (SDL_PRESSED, 4, p.x, SDL_VideoSurface->h - p.y);
330 else /* Scroll down */
331 SDL_PrivateMouseButton (SDL_PRESSED, 5, p.x, SDL_VideoSurface->h - p.y);
332 }
333 }
334 break;
335 case NSKeyUp:
336 QZ_DoKey (SDL_RELEASED, event);
337 break;
338 case NSKeyDown:
339 QZ_DoKey (SDL_PRESSED, event);
340 break;
341 case NSFlagsChanged:
342 QZ_DoModifiers( [ event modifierFlags ] );
343 break;
344 case NSMouseEntered: break;
345 case NSMouseExited: break;
346 case NSAppKitDefined:
347 switch ( [ event subtype ] ) {
348 case NSApplicationActivatedEventType:
349 QZ_DoActivate (this, p);
350 break;
351 case NSApplicationDeactivatedEventType:
352 QZ_DoDeactivate (this);
353 break;
354 case NSWindowMovedEventType:
355 [ window sendEvent:event ];
356 break;
357 }
358 break;
359 case NSApplicationDefined: break;
360 case NSPeriodic: break;
361 case NSCursorUpdate: break;
362 }
363 }
364 // [ pool release ];
365
366 } while (event != nil);
367 }
368