src/video/cocoa/SDL_cocoaevents.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 11 Feb 2013 17:39:52 -0800
changeset 6848 478ecc8a58b3
parent 6836 b0ca1571caf3
child 6885 700f1b25f77f
permissions -rw-r--r--
Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
slouken@1931
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 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@6044
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_COCOA
slouken@3120
    24
#include "SDL_timer.h"
slouken@1931
    25
slouken@1931
    26
#include "SDL_cocoavideo.h"
slouken@2738
    27
#include "../../events/SDL_events_c.h"
slouken@1931
    28
icculus@3624
    29
#if !defined(UsrActivity) && defined(__LP64__) && !defined(__POWER__)
icculus@3624
    30
/*
icculus@3624
    31
 * Workaround for a bug in the 10.5 SDK: By accident, OSService.h does
icculus@3624
    32
 * not include Power.h at all when compiling in 64bit mode. This has
icculus@3624
    33
 * been fixed in 10.6, but for 10.5, we manually define UsrActivity
icculus@3624
    34
 * to ensure compilation works.
icculus@3624
    35
 */
icculus@3624
    36
#define UsrActivity 1
icculus@3624
    37
#endif
slouken@3120
    38
slouken@1931
    39
/* setAppleMenu disappeared from the headers in 10.4 */
slouken@1931
    40
@interface NSApplication(NSAppleMenu)
slouken@1931
    41
- (void)setAppleMenu:(NSMenu *)menu;
slouken@1931
    42
@end
slouken@1931
    43
slouken@1937
    44
@interface SDLAppDelegate : NSObject
slouken@1937
    45
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
slouken@1937
    46
@end
slouken@1937
    47
slouken@1937
    48
@implementation SDLAppDelegate : NSObject
slouken@1937
    49
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
slouken@1937
    50
{
slouken@1937
    51
    SDL_SendQuit();
slouken@1937
    52
    return NSTerminateCancel;
slouken@1937
    53
}
slouken@6091
    54
slouken@6091
    55
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
slouken@6091
    56
{
slouken@6091
    57
    return (BOOL)SDL_SendDropFile([filename UTF8String]);
slouken@6091
    58
}
slouken@1937
    59
@end
slouken@1937
    60
slouken@1931
    61
static NSString *
slouken@1931
    62
GetApplicationName(void)
slouken@1931
    63
{
slouken@1931
    64
    NSDictionary *dict;
slouken@1931
    65
    NSString *appName = 0;
slouken@1931
    66
slouken@1931
    67
    /* Determine the application name */
slouken@1931
    68
    dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
slouken@1931
    69
    if (dict)
slouken@1931
    70
        appName = [dict objectForKey: @"CFBundleName"];
slouken@1931
    71
    
slouken@1931
    72
    if (![appName length])
slouken@1931
    73
        appName = [[NSProcessInfo processInfo] processName];
slouken@1931
    74
slouken@1931
    75
    return appName;
slouken@1931
    76
}
slouken@1931
    77
slouken@1931
    78
static void
slouken@1931
    79
CreateApplicationMenus(void)
slouken@1931
    80
{
slouken@1931
    81
    NSString *appName;
slouken@1931
    82
    NSString *title;
slouken@1931
    83
    NSMenu *appleMenu;
slouken@6515
    84
    NSMenu *serviceMenu;
slouken@1931
    85
    NSMenu *windowMenu;
slouken@1931
    86
    NSMenuItem *menuItem;
slouken@1931
    87
    
slouken@1931
    88
    /* Create the main menu bar */
slouken@1931
    89
    [NSApp setMainMenu:[[NSMenu alloc] init]];
slouken@1931
    90
slouken@1931
    91
    /* Create the application menu */
slouken@1931
    92
    appName = GetApplicationName();
slouken@1931
    93
    appleMenu = [[NSMenu alloc] initWithTitle:@""];
slouken@1931
    94
    
slouken@1931
    95
    /* Add menu items */
slouken@1931
    96
    title = [@"About " stringByAppendingString:appName];
slouken@1931
    97
    [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
slouken@1931
    98
slouken@1931
    99
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@1931
   100
slouken@6515
   101
    [appleMenu addItemWithTitle:@"Preferences…" action:nil keyEquivalent:@","];
slouken@6515
   102
slouken@6515
   103
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@6515
   104
slouken@6515
   105
    serviceMenu = [[NSMenu alloc] initWithTitle:@""];
slouken@6515
   106
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
slouken@6515
   107
    [menuItem setSubmenu:serviceMenu];
slouken@6515
   108
slouken@6515
   109
    [NSApp setServicesMenu:serviceMenu];
slouken@6836
   110
    [serviceMenu release];
slouken@5377
   111
slouken@5377
   112
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@5377
   113
slouken@1931
   114
    title = [@"Hide " stringByAppendingString:appName];
slouken@6515
   115
    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
slouken@1931
   116
slouken@6515
   117
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
slouken@1931
   118
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
slouken@1931
   119
slouken@1931
   120
    [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
slouken@1931
   121
slouken@1931
   122
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@1931
   123
slouken@1931
   124
    title = [@"Quit " stringByAppendingString:appName];
slouken@6515
   125
    [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
slouken@1931
   126
    
slouken@1931
   127
    /* Put menu into the menubar */
slouken@1931
   128
    menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
slouken@1931
   129
    [menuItem setSubmenu:appleMenu];
slouken@1931
   130
    [[NSApp mainMenu] addItem:menuItem];
slouken@1931
   131
    [menuItem release];
slouken@1931
   132
slouken@1931
   133
    /* Tell the application object that this is now the application menu */
slouken@1931
   134
    [NSApp setAppleMenu:appleMenu];
slouken@1931
   135
    [appleMenu release];
slouken@1931
   136
slouken@1931
   137
slouken@1931
   138
    /* Create the window menu */
slouken@1931
   139
    windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
slouken@1931
   140
    
slouken@6515
   141
    /* Add menu items */
slouken@6515
   142
    [windowMenu addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
slouken@1931
   143
    
slouken@6515
   144
    [windowMenu addItemWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""];
slouken@6515
   145
slouken@1931
   146
    /* Put menu into the menubar */
slouken@1931
   147
    menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
slouken@1931
   148
    [menuItem setSubmenu:windowMenu];
slouken@1931
   149
    [[NSApp mainMenu] addItem:menuItem];
slouken@1931
   150
    [menuItem release];
slouken@1931
   151
    
slouken@1931
   152
    /* Tell the application object that this is now the window menu */
slouken@1931
   153
    [NSApp setWindowsMenu:windowMenu];
slouken@1931
   154
    [windowMenu release];
slouken@1931
   155
}
slouken@1931
   156
slouken@1931
   157
void
slouken@1931
   158
Cocoa_RegisterApp(void)
slouken@1931
   159
{
icculus@6639
   160
    /* This can get called more than once! Be careful what you initialize! */
slouken@1931
   161
    ProcessSerialNumber psn;
slouken@6848
   162
    NSAutoreleasePool *pool;
slouken@1931
   163
slouken@1931
   164
    if (!GetCurrentProcess(&psn)) {
slouken@1931
   165
        TransformProcessType(&psn, kProcessTransformToForegroundApplication);
slouken@1931
   166
        SetFrontProcess(&psn);
slouken@1931
   167
    }
slouken@1931
   168
slouken@6848
   169
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   170
    if (NSApp == nil) {
slouken@6848
   171
        [NSApplication sharedApplication];
slouken@1931
   172
slouken@6848
   173
        if ([NSApp mainMenu] == nil) {
slouken@6848
   174
            CreateApplicationMenus();
slouken@1931
   175
        }
slouken@6848
   176
        [NSApp finishLaunching];
slouken@1931
   177
    }
slouken@6848
   178
    if ([NSApp delegate] == nil) {
slouken@6848
   179
        [NSApp setDelegate:[[SDLAppDelegate alloc] init]];
slouken@6848
   180
    }
slouken@6848
   181
    [pool release];
slouken@1931
   182
}
slouken@1931
   183
slouken@1931
   184
void
slouken@1931
   185
Cocoa_PumpEvents(_THIS)
slouken@1931
   186
{
slouken@6848
   187
    NSAutoreleasePool *pool;
slouken@6848
   188
slouken@3025
   189
    /* Update activity every 30 seconds to prevent screensaver */
slouken@3025
   190
    if (_this->suspend_screensaver) {
slouken@3025
   191
        SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
slouken@3025
   192
        Uint32 now = SDL_GetTicks();
slouken@3025
   193
        if (!data->screensaver_activity ||
slouken@3025
   194
            (int)(now-data->screensaver_activity) >= 30000) {
slouken@3025
   195
            UpdateSystemActivity(UsrActivity);
slouken@3025
   196
            data->screensaver_activity = now;
slouken@3025
   197
        }
slouken@3025
   198
    }
slouken@3025
   199
slouken@6848
   200
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   201
    for ( ; ; ) {
slouken@6848
   202
        NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
slouken@6848
   203
        if ( event == nil ) {
slouken@6848
   204
            break;
slouken@1931
   205
        }
slouken@6848
   206
		
slouken@6848
   207
        switch ([event type]) {
slouken@6848
   208
        case NSLeftMouseDown:
slouken@6848
   209
        case NSOtherMouseDown:
slouken@6848
   210
        case NSRightMouseDown:
slouken@6848
   211
        case NSLeftMouseUp:
slouken@6848
   212
        case NSOtherMouseUp:
slouken@6848
   213
        case NSRightMouseUp:
slouken@6848
   214
        case NSLeftMouseDragged:
slouken@6848
   215
        case NSRightMouseDragged:
slouken@6848
   216
        case NSOtherMouseDragged: /* usually middle mouse dragged */
slouken@6848
   217
        case NSMouseMoved:
slouken@6848
   218
        case NSScrollWheel:
slouken@6848
   219
            Cocoa_HandleMouseEvent(_this, event);
slouken@6848
   220
            break;
slouken@6848
   221
        case NSKeyDown:
slouken@6848
   222
        case NSKeyUp:
slouken@6848
   223
        case NSFlagsChanged:
slouken@6848
   224
            Cocoa_HandleKeyEvent(_this, event);
slouken@6848
   225
            break;
slouken@6848
   226
        default:
slouken@6848
   227
            break;
slouken@6848
   228
        }
slouken@6848
   229
        /* Pass through to NSApp to make sure everything stays in sync */
slouken@6848
   230
        [NSApp sendEvent:event];
slouken@1931
   231
    }
slouken@6848
   232
    [pool release];
slouken@1931
   233
}
slouken@1931
   234
slouken@6044
   235
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
   236
slouken@1931
   237
/* vi: set ts=4 sw=4 expandtab: */