/* SDL - Simple DirectMedia Layer Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Sam Lantinga slouken@libsdl.org */ #include // For getenv() #include // For wake from sleep detection #include // For wake from sleep detection #include "SDL_QuartzKeys.h" static void QZ_InitOSKeymap (_THIS) { const void *KCHRPtr; UInt32 state; UInt32 value; int i; int world = SDLK_WORLD_0; for ( i=0; ikeysym map. However, it will not work very well on international keyboard. Hence we now query MacOS for its own keymap to adjust our own mapping table. However, this is basically only useful for ascii char keys. This is also the reason why we keep the static table, too. */ /* Get a pointer to the systems cached KCHR */ KCHRPtr = (void *)GetScriptManagerVariable(smKCHRCache); if (KCHRPtr) { /* Loop over all 127 possible scan codes */ for (i = 0; i < 0x7F; i++) { /* We pretend a clean start to begin with (i.e. no dead keys active */ state = 0; /* Now translate the key code to a key value */ value = KeyTranslate(KCHRPtr, i, &state) & 0xff; /* If the state become 0, it was a dead key. We need to translate again, passing in the new state, to get the actual key value */ if (state != 0) value = KeyTranslate(KCHRPtr, i, &state) & 0xff; /* Now we should have an ascii value, or 0. Try to figure out to which SDL symbol it maps */ if (value >= 128) /* Some non-ASCII char, map it to SDLK_WORLD_* */ keymap[i] = world++; else if (value >= 32) /* non-control ASCII char */ keymap[i] = value; } } /* The keypad codes are re-setup here, because the loop above cannot distinguish between a key on the keypad and a regular key. We maybe could get around this problem in another fashion: NSEvent's flags include a "NSNumericPadKeyMask" bit; we could check that and modify the symbol we return on the fly. However, this flag seems to exhibit some weird behaviour related to the num lock key */ keymap[QZ_KP0] = SDLK_KP0; keymap[QZ_KP1] = SDLK_KP1; keymap[QZ_KP2] = SDLK_KP2; keymap[QZ_KP3] = SDLK_KP3; keymap[QZ_KP4] = SDLK_KP4; keymap[QZ_KP5] = SDLK_KP5; keymap[QZ_KP6] = SDLK_KP6; keymap[QZ_KP7] = SDLK_KP7; keymap[QZ_KP8] = SDLK_KP8; keymap[QZ_KP9] = SDLK_KP9; keymap[QZ_KP_MINUS] = SDLK_KP_MINUS; keymap[QZ_KP_PLUS] = SDLK_KP_PLUS; keymap[QZ_KP_PERIOD] = SDLK_KP_PERIOD; keymap[QZ_KP_EQUALS] = SDLK_KP_EQUALS; keymap[QZ_KP_DIVIDE] = SDLK_KP_DIVIDE; keymap[QZ_KP_MULTIPLY] = SDLK_KP_MULTIPLY; keymap[QZ_KP_ENTER] = SDLK_KP_ENTER; } static void QZ_DoKey (_THIS, int state, NSEvent *event) { NSString *chars; unsigned int numChars; SDL_keysym key; /* A key event can contain multiple characters, or no characters at all. In most cases, it will contain a single character. If it contains 0 characters, we'll use 0 as the unicode. If it contains multiple characters, we'll use 0 as the scancode/keysym. */ chars = [ event characters ]; numChars = [ chars length ]; if (numChars == 1) { key.scancode = [ event keyCode ]; key.sym = keymap [ key.scancode ]; key.unicode = [ chars characterAtIndex:0 ]; key.mod = KMOD_NONE; SDL_PrivateKeyboard (state, &key); } else if (numChars == 0) { key.scancode = [ event keyCode ]; key.sym = keymap [ key.scancode ]; key.unicode = 0; key.mod = KMOD_NONE; SDL_PrivateKeyboard (state, &key); } else /* (numChars > 1) */ { int i; for (i = 0; i < numChars; i++) { key.scancode = 0; key.sym = 0; key.unicode = [ chars characterAtIndex:i]; key.mod = KMOD_NONE; SDL_PrivateKeyboard (state, &key); } } } static void QZ_DoModifiers (_THIS, unsigned int newMods) { const int mapping[] = { SDLK_CAPSLOCK, SDLK_LSHIFT, SDLK_LCTRL, SDLK_LALT, SDLK_LMETA } ; int i; int bit; SDL_keysym key; key.scancode = 0; key.sym = SDLK_UNKNOWN; key.unicode = 0; key.mod = KMOD_NONE; /* Iterate through the bits, testing each against the current modifiers */ for (i = 0, bit = NSAlphaShiftKeyMask; bit <= NSCommandKeyMask; bit <<= 1, ++i) { unsigned int currentMask, newMask; currentMask = current_mods & bit; newMask = newMods & bit; if ( currentMask && currentMask != newMask ) { /* modifier up event */ key.sym = mapping[i]; /* If this was Caps Lock, we need some additional voodoo to make SDL happy */ if (bit == NSAlphaShiftKeyMask) SDL_PrivateKeyboard (SDL_PRESSED, &key); SDL_PrivateKeyboard (SDL_RELEASED, &key); } else if ( newMask && currentMask != newMask ) { /* modifier down event */ key.sym = mapping[i]; SDL_PrivateKeyboard (SDL_PRESSED, &key); /* If this was Caps Lock, we need some additional voodoo to make SDL happy */ if (bit == NSAlphaShiftKeyMask) SDL_PrivateKeyboard (SDL_RELEASED, &key); } } current_mods = newMods; } static void QZ_DoActivate (_THIS) { in_foreground = YES; /* Hide the cursor if it was hidden by SDL_ShowCursor() */ if (!cursor_visible && !cursor_hidden) { HideCursor (); cursor_hidden = YES; } /* Regrab input, only if it was previously grabbed */ if ( current_grab_mode == SDL_GRAB_ON ) { /* Restore cursor location if input was grabbed */ QZ_PrivateWarpCursor (this, cursor_loc.x, cursor_loc.y); QZ_ChangeGrabState (this, QZ_ENABLE_GRAB); } SDL_PrivateAppActive (1, SDL_APPINPUTFOCUS); } static void QZ_DoDeactivate (_THIS) { in_foreground = NO; /* Get the current cursor location, for restore on activate */ cursor_loc = [ NSEvent mouseLocation ]; /* global coordinates */ if (qz_window) QZ_PrivateGlobalToLocal (this, &cursor_loc); QZ_PrivateCocoaToSDL (this, &cursor_loc); /* Reassociate mouse and cursor */ CGAssociateMouseAndMouseCursorPosition (1); /* Show the cursor if it was hidden by SDL_ShowCursor() */ if (!cursor_visible && cursor_hidden) { ShowCursor (); cursor_hidden = NO; } SDL_PrivateAppActive (0, SDL_APPINPUTFOCUS); } void QZ_SleepNotificationHandler (void * refcon, io_service_t service, natural_t messageType, void * messageArgument ) { SDL_VideoDevice *this = (SDL_VideoDevice*)refcon; switch(messageType) { case kIOMessageSystemWillSleep: IOAllowPowerChange(power_connection, (long) messageArgument); break; case kIOMessageCanSystemSleep: IOAllowPowerChange(power_connection, (long) messageArgument); break; case kIOMessageSystemHasPoweredOn: /* awake */ SDL_PrivateExpose(); break; } } static void QZ_RegisterForSleepNotifications (_THIS) { CFRunLoopSourceRef rls; IONotificationPortRef thePortRef; io_object_t notifier; power_connection = IORegisterForSystemPower (this, &thePortRef, QZ_SleepNotificationHandler, ¬ifier); if (power_connection == 0) NSLog(@"SDL: QZ_SleepNotificationHandler() IORegisterForSystemPower failed."); rls = IONotificationPortGetRunLoopSource (thePortRef); CFRunLoopAddSource (CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode); CFRelease (rls); } // Try to map Quartz mouse buttons to SDL's lingo... static int QZ_OtherMouseButtonToSDL(int button) { switch (button) { case 0: return(SDL_BUTTON_LEFT); // 1 case 1: return(SDL_BUTTON_RIGHT); // 3 case 2: return(SDL_BUTTON_MIDDLE); // 2 } // >= 3: skip 4 & 5, since those are the SDL mousewheel buttons. return(button + 3); } static void QZ_PumpEvents (_THIS) { int firstMouseEvent; CGMouseDelta dx, dy; NSDate *distantPast; NSEvent *event; NSRect winRect; NSRect titleBarRect; NSAutoreleasePool *pool; /* Update activity every five seconds to prevent screensaver. --ryan. */ static Uint32 screensaverTicks = 0; Uint32 nowTicks = SDL_GetTicks(); if ((nowTicks - screensaverTicks) > 5000) { UpdateSystemActivity(UsrActivity); screensaverTicks = nowTicks; } pool = [ [ NSAutoreleasePool alloc ] init ]; distantPast = [ NSDate distantPast ]; winRect = NSMakeRect (0, 0, SDL_VideoSurface->w, SDL_VideoSurface->h); titleBarRect = NSMakeRect (0, SDL_VideoSurface->h, SDL_VideoSurface->w, SDL_VideoSurface->h + 22); /* send the first mouse event in absolute coordinates */ firstMouseEvent = 1; /* accumulate any additional mouse moved events into one SDL mouse event */ dx = 0; dy = 0; do { /* Poll for an event. This will not block */ event = [ NSApp nextEventMatchingMask:NSAnyEventMask untilDate:distantPast inMode: NSDefaultRunLoopMode dequeue:YES ]; if (event != nil) { int button; unsigned int type; BOOL isForGameWin; BOOL isInGameWin; #define DO_MOUSE_DOWN(button) do { \ if ( in_foreground ) { \ if ( isInGameWin ) { \ SDL_PrivateMouseButton (SDL_PRESSED, button, 0, 0); \ expect_mouse_up |= 1<= winRect.size.width ) p.x = winRect.size.width-1; if ( p.y >= winRect.size.height ) p.y = winRect.size.height-1; QZ_PrivateWarpCursor (this, p.x, p.y); } break; case NSScrollWheel: if ( isInGameWin ) { float dy; Uint8 button; dy = [ event deltaY ]; if ( dy > 0.0 ) /* Scroll up */ button = SDL_BUTTON_WHEELUP; else /* Scroll down */ button = SDL_BUTTON_WHEELDOWN; /* For now, wheel is sent as a quick down+up */ SDL_PrivateMouseButton (SDL_PRESSED, button, 0, 0); SDL_PrivateMouseButton (SDL_RELEASED, button, 0, 0); } break; case NSKeyUp: QZ_DoKey (this, SDL_RELEASED, event); break; case NSKeyDown: QZ_DoKey (this, SDL_PRESSED, event); break; case NSFlagsChanged: QZ_DoModifiers(this, [ event modifierFlags ] ); break; case NSAppKitDefined: switch ( [ event subtype ] ) { case NSApplicationActivatedEventType: QZ_DoActivate (this); break; case NSApplicationDeactivatedEventType: QZ_DoDeactivate (this); break; } [ NSApp sendEvent:event ]; break; /* case NSApplicationDefined: break; */ /* case NSPeriodic: break; */ /* case NSCursorUpdate: break; */ default: [ NSApp sendEvent:event ]; } } } while (event != nil); /* handle accumulated mouse moved events */ if (dx != 0 || dy != 0) SDL_PrivateMouseMotion (0, 1, dx, dy); [ pool release ]; }