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