src/video/cocoa/SDL_cocoaevents.m
author Sam Lantinga
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5377 6125f6da3dae
child 6044 35448a5ea044
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
slouken@1931
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@1931
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1931
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1931
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1931
    20
*/
slouken@1931
    21
#include "SDL_config.h"
slouken@3120
    22
#include "SDL_timer.h"
slouken@1931
    23
slouken@1931
    24
#include "SDL_cocoavideo.h"
slouken@2738
    25
#include "../../events/SDL_events_c.h"
slouken@1931
    26
icculus@3624
    27
#if !defined(UsrActivity) && defined(__LP64__) && !defined(__POWER__)
icculus@3624
    28
/*
icculus@3624
    29
 * Workaround for a bug in the 10.5 SDK: By accident, OSService.h does
icculus@3624
    30
 * not include Power.h at all when compiling in 64bit mode. This has
icculus@3624
    31
 * been fixed in 10.6, but for 10.5, we manually define UsrActivity
icculus@3624
    32
 * to ensure compilation works.
icculus@3624
    33
 */
icculus@3624
    34
#define UsrActivity 1
icculus@3624
    35
#endif
slouken@3120
    36
slouken@1931
    37
/* setAppleMenu disappeared from the headers in 10.4 */
slouken@1931
    38
@interface NSApplication(NSAppleMenu)
slouken@1931
    39
- (void)setAppleMenu:(NSMenu *)menu;
slouken@1931
    40
@end
slouken@1931
    41
slouken@1937
    42
@interface SDLAppDelegate : NSObject
slouken@1937
    43
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
slouken@1937
    44
@end
slouken@1937
    45
slouken@1937
    46
@implementation SDLAppDelegate : NSObject
slouken@1937
    47
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
slouken@1937
    48
{
slouken@1937
    49
    SDL_SendQuit();
slouken@1937
    50
    return NSTerminateCancel;
slouken@1937
    51
}
slouken@1937
    52
@end
slouken@1937
    53
slouken@1931
    54
static NSString *
slouken@1931
    55
GetApplicationName(void)
slouken@1931
    56
{
slouken@1931
    57
    NSDictionary *dict;
slouken@1931
    58
    NSString *appName = 0;
slouken@1931
    59
slouken@1931
    60
    /* Determine the application name */
slouken@1931
    61
    dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
slouken@1931
    62
    if (dict)
slouken@1931
    63
        appName = [dict objectForKey: @"CFBundleName"];
slouken@1931
    64
    
slouken@1931
    65
    if (![appName length])
slouken@1931
    66
        appName = [[NSProcessInfo processInfo] processName];
slouken@1931
    67
slouken@1931
    68
    return appName;
slouken@1931
    69
}
slouken@1931
    70
slouken@1931
    71
static void
slouken@1931
    72
CreateApplicationMenus(void)
slouken@1931
    73
{
slouken@1931
    74
    NSString *appName;
slouken@1931
    75
    NSString *title;
slouken@1931
    76
    NSMenu *appleMenu;
slouken@1931
    77
    NSMenu *windowMenu;
slouken@1931
    78
    NSMenuItem *menuItem;
slouken@1931
    79
    
slouken@1931
    80
    /* Create the main menu bar */
slouken@1931
    81
    [NSApp setMainMenu:[[NSMenu alloc] init]];
slouken@1931
    82
slouken@1931
    83
    /* Create the application menu */
slouken@1931
    84
    appName = GetApplicationName();
slouken@1931
    85
    appleMenu = [[NSMenu alloc] initWithTitle:@""];
slouken@1931
    86
    
slouken@1931
    87
    /* Add menu items */
slouken@1931
    88
    title = [@"About " stringByAppendingString:appName];
slouken@1931
    89
    [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
slouken@1931
    90
slouken@1931
    91
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@1931
    92
slouken@5377
    93
    [appleMenu addItemWithTitle:@"Preferences" action:nil keyEquivalent:@""];
slouken@5377
    94
slouken@5377
    95
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@5377
    96
slouken@1931
    97
    title = [@"Hide " stringByAppendingString:appName];
slouken@1959
    98
    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@/*"h"*/""];
slouken@1931
    99
slouken@1959
   100
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@/*"h"*/""];
slouken@1931
   101
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
slouken@1931
   102
slouken@1931
   103
    [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
slouken@1931
   104
slouken@1931
   105
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@1931
   106
slouken@1931
   107
    title = [@"Quit " stringByAppendingString:appName];
slouken@1959
   108
    [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@/*"q"*/""];
slouken@1931
   109
    
slouken@1931
   110
    /* Put menu into the menubar */
slouken@1931
   111
    menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
slouken@1931
   112
    [menuItem setSubmenu:appleMenu];
slouken@1931
   113
    [[NSApp mainMenu] addItem:menuItem];
slouken@1931
   114
    [menuItem release];
slouken@1931
   115
slouken@1931
   116
    /* Tell the application object that this is now the application menu */
slouken@1931
   117
    [NSApp setAppleMenu:appleMenu];
slouken@1931
   118
    [appleMenu release];
slouken@1931
   119
slouken@1931
   120
slouken@1931
   121
    /* Create the window menu */
slouken@1931
   122
    windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
slouken@1931
   123
    
slouken@1931
   124
    /* "Minimize" item */
slouken@1959
   125
    menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@/*"m"*/""];
slouken@1931
   126
    [windowMenu addItem:menuItem];
slouken@1931
   127
    [menuItem release];
slouken@1931
   128
    
slouken@1931
   129
    /* Put menu into the menubar */
slouken@1931
   130
    menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
slouken@1931
   131
    [menuItem setSubmenu:windowMenu];
slouken@1931
   132
    [[NSApp mainMenu] addItem:menuItem];
slouken@1931
   133
    [menuItem release];
slouken@1931
   134
    
slouken@1931
   135
    /* Tell the application object that this is now the window menu */
slouken@1931
   136
    [NSApp setWindowsMenu:windowMenu];
slouken@1931
   137
    [windowMenu release];
slouken@1931
   138
}
slouken@1931
   139
slouken@1931
   140
void
slouken@1931
   141
Cocoa_RegisterApp(void)
slouken@1931
   142
{
slouken@1931
   143
    ProcessSerialNumber psn;
slouken@1931
   144
    NSAutoreleasePool *pool;
slouken@1931
   145
slouken@1931
   146
    if (!GetCurrentProcess(&psn)) {
slouken@1931
   147
        TransformProcessType(&psn, kProcessTransformToForegroundApplication);
slouken@1931
   148
        SetFrontProcess(&psn);
slouken@1931
   149
    }
slouken@1931
   150
slouken@1931
   151
    pool = [[NSAutoreleasePool alloc] init];
slouken@1931
   152
    if (NSApp == nil) {
slouken@1936
   153
        [NSApplication sharedApplication];
slouken@1931
   154
slouken@1931
   155
        if ([NSApp mainMenu] == nil) {
slouken@1931
   156
            CreateApplicationMenus();
slouken@1931
   157
        }
slouken@1931
   158
        [NSApp finishLaunching];
slouken@1931
   159
    }
slouken@1937
   160
    if ([NSApp delegate] == nil) {
slouken@1937
   161
        [NSApp setDelegate:[[SDLAppDelegate alloc] init]];
slouken@1937
   162
    }
slouken@1931
   163
    [pool release];
slouken@1931
   164
}
slouken@1931
   165
slouken@1931
   166
void
slouken@1931
   167
Cocoa_PumpEvents(_THIS)
slouken@1931
   168
{
slouken@1931
   169
    NSAutoreleasePool *pool;
slouken@1931
   170
slouken@3025
   171
    /* Update activity every 30 seconds to prevent screensaver */
slouken@3025
   172
    if (_this->suspend_screensaver) {
slouken@3025
   173
        SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
slouken@3025
   174
        Uint32 now = SDL_GetTicks();
slouken@3025
   175
        if (!data->screensaver_activity ||
slouken@3025
   176
            (int)(now-data->screensaver_activity) >= 30000) {
slouken@3025
   177
            UpdateSystemActivity(UsrActivity);
slouken@3025
   178
            data->screensaver_activity = now;
slouken@3025
   179
        }
slouken@3025
   180
    }
slouken@3025
   181
slouken@1931
   182
    pool = [[NSAutoreleasePool alloc] init];
slouken@5266
   183
    for ( ; ; ) {
slouken@1931
   184
        NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
slouken@1931
   185
        if ( event == nil ) {
slouken@1931
   186
            break;
slouken@1931
   187
        }
jimtla@4670
   188
		
slouken@1959
   189
        switch ([event type]) {
slouken@3517
   190
        case NSLeftMouseDown:
slouken@3517
   191
        case NSOtherMouseDown:
slouken@3517
   192
        case NSRightMouseDown:
slouken@3517
   193
        case NSLeftMouseUp:
slouken@3517
   194
        case NSOtherMouseUp:
slouken@3517
   195
        case NSRightMouseUp:
slouken@3517
   196
        case NSLeftMouseDragged:
slouken@3517
   197
        case NSRightMouseDragged:
slouken@3517
   198
        case NSOtherMouseDragged: /* usually middle mouse dragged */
slouken@5060
   199
        case NSMouseMoved:
gzjjgod@5057
   200
        case NSScrollWheel:
slouken@3517
   201
            Cocoa_HandleMouseEvent(_this, event);
slouken@3517
   202
            /* Pass through to NSApp to make sure everything stays in sync */
slouken@3517
   203
            [NSApp sendEvent:event];
slouken@3517
   204
            break;
slouken@1959
   205
        case NSKeyDown:
slouken@1959
   206
        case NSKeyUp:
slouken@1959
   207
        case NSFlagsChanged:
slouken@1959
   208
            Cocoa_HandleKeyEvent(_this, event);
slouken@1962
   209
            /* Fall through to pass event to NSApp; er, nevermind... */
slouken@3280
   210
slouken@3280
   211
            /* Add to support system-wide keyboard shortcuts like CMD+Space */
slouken@3280
   212
            if (([event modifierFlags] & NSCommandKeyMask) || [event type] == NSFlagsChanged)
slouken@3280
   213
               [NSApp sendEvent: event];
slouken@1962
   214
            break;
slouken@1959
   215
        default:
slouken@1959
   216
            [NSApp sendEvent:event];
slouken@1959
   217
            break;
slouken@1959
   218
        }
slouken@1931
   219
    }
slouken@1931
   220
    [pool release];
slouken@1931
   221
}
slouken@1931
   222
slouken@1931
   223
/* vi: set ts=4 sw=4 expandtab: */