src/video/cocoa/SDL_cocoaevents.m
author Sam Lantinga <slouken@libsdl.org>
Tue, 19 Jan 2010 07:28:51 +0000
changeset 3683 4c5ab6841fdc
parent 3624 9b1af1049f66
child 4498 3d91e31fcf71
permissions -rw-r--r--
From: Jjgod Jiang
Date: Mon, 18 Jan 2010 17:46:35 +0800
Subject: [PATCH] Polish text input handling for Mac OS X

- Prevent crash caused by uninitialized video data
- Prevent beeping caused by unhandled Cocoa selectors (like moveUp:
moveDown:, etc.)
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 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     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 #include "SDL_timer.h"
    24 
    25 #include "SDL_cocoavideo.h"
    26 #include "../../events/SDL_events_c.h"
    27 
    28 #if !defined(UsrActivity) && defined(__LP64__) && !defined(__POWER__)
    29 /*
    30  * Workaround for a bug in the 10.5 SDK: By accident, OSService.h does
    31  * not include Power.h at all when compiling in 64bit mode. This has
    32  * been fixed in 10.6, but for 10.5, we manually define UsrActivity
    33  * to ensure compilation works.
    34  */
    35 #define UsrActivity 1
    36 #endif
    37 
    38 /* setAppleMenu disappeared from the headers in 10.4 */
    39 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
    40 @interface NSApplication(NSAppleMenu)
    41 - (void)setAppleMenu:(NSMenu *)menu;
    42 @end
    43 #endif
    44 
    45 @implementation NSApplication(SDL)
    46 - (void)setRunning
    47 {
    48     _running = 1;
    49 }
    50 @end
    51 
    52 @interface SDLAppDelegate : NSObject
    53 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
    54 @end
    55 
    56 @implementation SDLAppDelegate : NSObject
    57 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
    58 {
    59     SDL_SendQuit();
    60     return NSTerminateCancel;
    61 }
    62 @end
    63 
    64 static NSString *
    65 GetApplicationName(void)
    66 {
    67     NSDictionary *dict;
    68     NSString *appName = 0;
    69 
    70     /* Determine the application name */
    71     dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
    72     if (dict)
    73         appName = [dict objectForKey: @"CFBundleName"];
    74     
    75     if (![appName length])
    76         appName = [[NSProcessInfo processInfo] processName];
    77 
    78     return appName;
    79 }
    80 
    81 static void
    82 CreateApplicationMenus(void)
    83 {
    84     NSString *appName;
    85     NSString *title;
    86     NSMenu *appleMenu;
    87     NSMenu *windowMenu;
    88     NSMenuItem *menuItem;
    89     
    90     /* Create the main menu bar */
    91     [NSApp setMainMenu:[[NSMenu alloc] init]];
    92 
    93     /* Create the application menu */
    94     appName = GetApplicationName();
    95     appleMenu = [[NSMenu alloc] initWithTitle:@""];
    96     
    97     /* Add menu items */
    98     title = [@"About " stringByAppendingString:appName];
    99     [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
   100 
   101     [appleMenu addItem:[NSMenuItem separatorItem]];
   102 
   103     title = [@"Hide " stringByAppendingString:appName];
   104     [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@/*"h"*/""];
   105 
   106     menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@/*"h"*/""];
   107     [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
   108 
   109     [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
   110 
   111     [appleMenu addItem:[NSMenuItem separatorItem]];
   112 
   113     title = [@"Quit " stringByAppendingString:appName];
   114     [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@/*"q"*/""];
   115     
   116     /* Put menu into the menubar */
   117     menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
   118     [menuItem setSubmenu:appleMenu];
   119     [[NSApp mainMenu] addItem:menuItem];
   120     [menuItem release];
   121 
   122     /* Tell the application object that this is now the application menu */
   123     [NSApp setAppleMenu:appleMenu];
   124     [appleMenu release];
   125 
   126 
   127     /* Create the window menu */
   128     windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
   129     
   130     /* "Minimize" item */
   131     menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@/*"m"*/""];
   132     [windowMenu addItem:menuItem];
   133     [menuItem release];
   134     
   135     /* Put menu into the menubar */
   136     menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
   137     [menuItem setSubmenu:windowMenu];
   138     [[NSApp mainMenu] addItem:menuItem];
   139     [menuItem release];
   140     
   141     /* Tell the application object that this is now the window menu */
   142     [NSApp setWindowsMenu:windowMenu];
   143     [windowMenu release];
   144 }
   145 
   146 void
   147 Cocoa_RegisterApp(void)
   148 {
   149     ProcessSerialNumber psn;
   150     NSAutoreleasePool *pool;
   151 
   152     if (!GetCurrentProcess(&psn)) {
   153         TransformProcessType(&psn, kProcessTransformToForegroundApplication);
   154         SetFrontProcess(&psn);
   155     }
   156 
   157     pool = [[NSAutoreleasePool alloc] init];
   158     if (NSApp == nil) {
   159         [NSApplication sharedApplication];
   160 
   161         if ([NSApp mainMenu] == nil) {
   162             CreateApplicationMenus();
   163         }
   164         [NSApp finishLaunching];
   165     }
   166     if ([NSApp delegate] == nil) {
   167         [NSApp setDelegate:[[SDLAppDelegate alloc] init]];
   168     }
   169     [NSApp setRunning];
   170     [pool release];
   171 }
   172 
   173 void
   174 Cocoa_PumpEvents(_THIS)
   175 {
   176     NSAutoreleasePool *pool;
   177 
   178     /* Update activity every 30 seconds to prevent screensaver */
   179     if (_this->suspend_screensaver) {
   180         SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
   181         Uint32 now = SDL_GetTicks();
   182         if (!data->screensaver_activity ||
   183             (int)(now-data->screensaver_activity) >= 30000) {
   184             UpdateSystemActivity(UsrActivity);
   185             data->screensaver_activity = now;
   186         }
   187     }
   188 
   189     pool = [[NSAutoreleasePool alloc] init];
   190     while ([NSApp isRunning]) {
   191         NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
   192         if ( event == nil ) {
   193             break;
   194         }
   195         switch ([event type]) {
   196         case NSLeftMouseDown:
   197         case NSOtherMouseDown:
   198         case NSRightMouseDown:
   199         case NSLeftMouseUp:
   200         case NSOtherMouseUp:
   201         case NSRightMouseUp:
   202         case NSLeftMouseDragged:
   203         case NSRightMouseDragged:
   204         case NSOtherMouseDragged: /* usually middle mouse dragged */
   205         case NSMouseMoved:
   206             Cocoa_HandleMouseEvent(_this, event);
   207             /* Pass through to NSApp to make sure everything stays in sync */
   208             [NSApp sendEvent:event];
   209             break;
   210         case NSKeyDown:
   211         case NSKeyUp:
   212         case NSFlagsChanged:
   213             Cocoa_HandleKeyEvent(_this, event);
   214             /* Fall through to pass event to NSApp; er, nevermind... */
   215 
   216             /* Add to support system-wide keyboard shortcuts like CMD+Space */
   217             if (([event modifierFlags] & NSCommandKeyMask) || [event type] == NSFlagsChanged)
   218                [NSApp sendEvent: event];
   219             break;
   220         default:
   221             [NSApp sendEvent:event];
   222             break;
   223         }
   224     }
   225     [pool release];
   226 }
   227 
   228 /* vi: set ts=4 sw=4 expandtab: */