src/video/cocoa/SDL_cocoaevents.m
author Sam Lantinga
Tue, 08 Feb 2011 16:50:51 -0800
changeset 5229 c015d3e63631
parent 5060 2170edcdfbd0
child 5262 b530ef003506
permissions -rw-r--r--
Fixed setting the texture unit, still doesn't work.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 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 @interface NSApplication(NSAppleMenu)
    40 - (void)setAppleMenu:(NSMenu *)menu;
    41 @end
    42 
    43 @implementation NSApplication(SDL)
    44 - (void)setRunning
    45 {
    46     _running = 1;
    47 }
    48 @end
    49 
    50 @interface SDLAppDelegate : NSObject
    51 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
    52 @end
    53 
    54 @implementation SDLAppDelegate : NSObject
    55 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
    56 {
    57     SDL_SendQuit();
    58     return NSTerminateCancel;
    59 }
    60 @end
    61 
    62 static NSString *
    63 GetApplicationName(void)
    64 {
    65     NSDictionary *dict;
    66     NSString *appName = 0;
    67 
    68     /* Determine the application name */
    69     dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
    70     if (dict)
    71         appName = [dict objectForKey: @"CFBundleName"];
    72     
    73     if (![appName length])
    74         appName = [[NSProcessInfo processInfo] processName];
    75 
    76     return appName;
    77 }
    78 
    79 static void
    80 CreateApplicationMenus(void)
    81 {
    82     NSString *appName;
    83     NSString *title;
    84     NSMenu *appleMenu;
    85     NSMenu *windowMenu;
    86     NSMenuItem *menuItem;
    87     
    88     /* Create the main menu bar */
    89     [NSApp setMainMenu:[[NSMenu alloc] init]];
    90 
    91     /* Create the application menu */
    92     appName = GetApplicationName();
    93     appleMenu = [[NSMenu alloc] initWithTitle:@""];
    94     
    95     /* Add menu items */
    96     title = [@"About " stringByAppendingString:appName];
    97     [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
    98 
    99     [appleMenu addItem:[NSMenuItem separatorItem]];
   100 
   101     title = [@"Hide " stringByAppendingString:appName];
   102     [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@/*"h"*/""];
   103 
   104     menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@/*"h"*/""];
   105     [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
   106 
   107     [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
   108 
   109     [appleMenu addItem:[NSMenuItem separatorItem]];
   110 
   111     title = [@"Quit " stringByAppendingString:appName];
   112     [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@/*"q"*/""];
   113     
   114     /* Put menu into the menubar */
   115     menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
   116     [menuItem setSubmenu:appleMenu];
   117     [[NSApp mainMenu] addItem:menuItem];
   118     [menuItem release];
   119 
   120     /* Tell the application object that this is now the application menu */
   121     [NSApp setAppleMenu:appleMenu];
   122     [appleMenu release];
   123 
   124 
   125     /* Create the window menu */
   126     windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
   127     
   128     /* "Minimize" item */
   129     menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@/*"m"*/""];
   130     [windowMenu addItem:menuItem];
   131     [menuItem release];
   132     
   133     /* Put menu into the menubar */
   134     menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
   135     [menuItem setSubmenu:windowMenu];
   136     [[NSApp mainMenu] addItem:menuItem];
   137     [menuItem release];
   138     
   139     /* Tell the application object that this is now the window menu */
   140     [NSApp setWindowsMenu:windowMenu];
   141     [windowMenu release];
   142 }
   143 
   144 void
   145 Cocoa_RegisterApp(void)
   146 {
   147     ProcessSerialNumber psn;
   148     NSAutoreleasePool *pool;
   149 
   150     if (!GetCurrentProcess(&psn)) {
   151         TransformProcessType(&psn, kProcessTransformToForegroundApplication);
   152         SetFrontProcess(&psn);
   153     }
   154 
   155     pool = [[NSAutoreleasePool alloc] init];
   156     if (NSApp == nil) {
   157         [NSApplication sharedApplication];
   158 
   159         if ([NSApp mainMenu] == nil) {
   160             CreateApplicationMenus();
   161         }
   162         [NSApp finishLaunching];
   163     }
   164     if ([NSApp delegate] == nil) {
   165         [NSApp setDelegate:[[SDLAppDelegate alloc] init]];
   166     }
   167     [NSApp setRunning];
   168     [pool release];
   169 }
   170 
   171 void
   172 Cocoa_PumpEvents(_THIS)
   173 {
   174     NSAutoreleasePool *pool;
   175 
   176     /* Update activity every 30 seconds to prevent screensaver */
   177     if (_this->suspend_screensaver) {
   178         SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
   179         Uint32 now = SDL_GetTicks();
   180         if (!data->screensaver_activity ||
   181             (int)(now-data->screensaver_activity) >= 30000) {
   182             UpdateSystemActivity(UsrActivity);
   183             data->screensaver_activity = now;
   184         }
   185     }
   186 
   187     pool = [[NSAutoreleasePool alloc] init];
   188     while ([NSApp isRunning]) {
   189         NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
   190         if ( event == nil ) {
   191             break;
   192         }
   193 		
   194         switch ([event type]) {
   195         case NSLeftMouseDown:
   196         case NSOtherMouseDown:
   197         case NSRightMouseDown:
   198         case NSLeftMouseUp:
   199         case NSOtherMouseUp:
   200         case NSRightMouseUp:
   201         case NSLeftMouseDragged:
   202         case NSRightMouseDragged:
   203         case NSOtherMouseDragged: /* usually middle mouse dragged */
   204         case NSMouseMoved:
   205         case NSScrollWheel:
   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: */