src/video/cocoa/SDL_cocoaevents.m
author Ryan C. Gordon
Thu, 19 Feb 2015 19:55:30 -0500
changeset 9364 ad9d35983de5
parent 9087 eef2e43f60d1
child 9419 9763f689bced
permissions -rw-r--r--
Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).

Fixes Bugzilla #2858.
slouken@1931
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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
*/
icculus@8093
    21
#include "../../SDL_internal.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"
icculus@8648
    28
#include "SDL_assert.h"
slouken@1931
    29
icculus@9364
    30
/* This define was added in the 10.9 SDK. */
icculus@9364
    31
#ifndef kIOPMAssertPreventUserIdleDisplaySleep
icculus@9364
    32
#define kIOPMAssertPreventUserIdleDisplaySleep kIOPMAssertionTypePreventUserIdleDisplaySleep
icculus@9364
    33
#endif
icculus@9364
    34
slouken@8239
    35
@interface SDLApplication : NSApplication
slouken@8239
    36
slouken@8239
    37
- (void)terminate:(id)sender;
slouken@8239
    38
slouken@8239
    39
@end
slouken@8239
    40
slouken@8239
    41
@implementation SDLApplication
slouken@8239
    42
slouken@8239
    43
// Override terminate to handle Quit and System Shutdown smoothly.
slouken@8239
    44
- (void)terminate:(id)sender
slouken@8239
    45
{
slouken@8239
    46
    SDL_SendQuit();
slouken@8239
    47
}
slouken@8239
    48
slouken@8239
    49
@end // SDLApplication
slouken@8239
    50
slouken@1931
    51
/* setAppleMenu disappeared from the headers in 10.4 */
slouken@1931
    52
@interface NSApplication(NSAppleMenu)
slouken@1931
    53
- (void)setAppleMenu:(NSMenu *)menu;
slouken@1931
    54
@end
slouken@1931
    55
slouken@8986
    56
@interface SDLAppDelegate : NSObject <NSApplicationDelegate> {
jorgen@7801
    57
@public
jorgen@7469
    58
    BOOL seenFirstActivate;
jorgen@7469
    59
}
jorgen@7469
    60
jorgen@7469
    61
- (id)init;
slouken@1937
    62
@end
slouken@1937
    63
slouken@1937
    64
@implementation SDLAppDelegate : NSObject
jorgen@7469
    65
- (id)init
jorgen@7469
    66
{
jorgen@7469
    67
    self = [super init];
jorgen@7469
    68
    if (self) {
jorgen@7469
    69
        seenFirstActivate = NO;
jorgen@7801
    70
        [[NSNotificationCenter defaultCenter] addObserver:self
jorgen@7801
    71
                                                 selector:@selector(focusSomeWindow:)
jorgen@7801
    72
                                                     name:NSApplicationDidBecomeActiveNotification
jorgen@7801
    73
                                                   object:nil];
jorgen@7469
    74
    }
jorgen@7469
    75
jorgen@7469
    76
    return self;
jorgen@7469
    77
}
jorgen@7469
    78
jorgen@7801
    79
- (void)dealloc
jorgen@7801
    80
{
jorgen@7801
    81
    [[NSNotificationCenter defaultCenter] removeObserver:self];
jorgen@7801
    82
    [super dealloc];
jorgen@7801
    83
}
jorgen@7801
    84
jorgen@7801
    85
- (void)focusSomeWindow:(NSNotification *)aNotification
jorgen@7466
    86
{
jorgen@7469
    87
    /* HACK: Ignore the first call. The application gets a
jorgen@7469
    88
     * applicationDidBecomeActive: a little bit after the first window is
jorgen@7469
    89
     * created, and if we don't ignore it, a window that has been created with
jorgen@7469
    90
     * SDL_WINDOW_MINIZED will ~immediately be restored.
jorgen@7469
    91
     */
jorgen@7469
    92
    if (!seenFirstActivate) {
jorgen@7469
    93
        seenFirstActivate = YES;
jorgen@7469
    94
        return;
jorgen@7469
    95
    }
jorgen@7469
    96
jorgen@7466
    97
    SDL_VideoDevice *device = SDL_GetVideoDevice();
slouken@8986
    98
    if (device && device->windows) {
jorgen@7466
    99
        SDL_Window *window = device->windows;
jorgen@7466
   100
        int i;
slouken@8986
   101
        for (i = 0; i < device->num_displays; ++i) {
jorgen@7466
   102
            SDL_Window *fullscreen_window = device->displays[i].fullscreen_window;
slouken@8986
   103
            if (fullscreen_window) {
jorgen@7466
   104
                if (fullscreen_window->flags & SDL_WINDOW_MINIMIZED) {
jorgen@7466
   105
                    SDL_RestoreWindow(fullscreen_window);
jorgen@7466
   106
                }
jorgen@7466
   107
                return;
jorgen@7466
   108
            }
jorgen@7466
   109
        }
jorgen@7466
   110
jorgen@7466
   111
        if (window->flags & SDL_WINDOW_MINIMIZED) {
jorgen@7466
   112
            SDL_RestoreWindow(window);
jorgen@7466
   113
        } else {
jorgen@7466
   114
            SDL_RaiseWindow(window);
jorgen@7466
   115
        }
jorgen@7466
   116
    }
jorgen@7466
   117
}
jorgen@7466
   118
slouken@6091
   119
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
slouken@6091
   120
{
slouken@6091
   121
    return (BOOL)SDL_SendDropFile([filename UTF8String]);
slouken@6091
   122
}
slouken@1937
   123
@end
slouken@1937
   124
jorgen@7801
   125
static SDLAppDelegate *appDelegate = nil;
jorgen@7801
   126
slouken@1931
   127
static NSString *
slouken@1931
   128
GetApplicationName(void)
slouken@1931
   129
{
slouken@8237
   130
    NSString *appName;
slouken@1931
   131
slouken@1931
   132
    /* Determine the application name */
slouken@8237
   133
    appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
slouken@8986
   134
    if (!appName) {
slouken@8237
   135
        appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
slouken@8986
   136
    }
slouken@7191
   137
slouken@8986
   138
    if (![appName length]) {
slouken@1931
   139
        appName = [[NSProcessInfo processInfo] processName];
slouken@8986
   140
    }
slouken@1931
   141
slouken@1931
   142
    return appName;
slouken@1931
   143
}
slouken@1931
   144
slouken@1931
   145
static void
slouken@1931
   146
CreateApplicationMenus(void)
slouken@1931
   147
{
slouken@1931
   148
    NSString *appName;
slouken@1931
   149
    NSString *title;
slouken@1931
   150
    NSMenu *appleMenu;
slouken@6515
   151
    NSMenu *serviceMenu;
slouken@1931
   152
    NSMenu *windowMenu;
slouken@7952
   153
    NSMenu *viewMenu;
slouken@1931
   154
    NSMenuItem *menuItem;
icculus@8653
   155
    NSMenu *mainMenu;
slouken@7191
   156
slouken@7511
   157
    if (NSApp == nil) {
jorgen@7508
   158
        return;
jorgen@7508
   159
    }
icculus@8653
   160
icculus@8653
   161
    mainMenu = [[NSMenu alloc] init];
icculus@8653
   162
slouken@1931
   163
    /* Create the main menu bar */
icculus@8653
   164
    [NSApp setMainMenu:mainMenu];
icculus@8653
   165
icculus@8653
   166
    [mainMenu release];  /* we're done with it, let NSApp own it. */
icculus@8653
   167
    mainMenu = nil;
slouken@1931
   168
slouken@1931
   169
    /* Create the application menu */
slouken@1931
   170
    appName = GetApplicationName();
slouken@1931
   171
    appleMenu = [[NSMenu alloc] initWithTitle:@""];
slouken@7191
   172
slouken@1931
   173
    /* Add menu items */
slouken@1931
   174
    title = [@"About " stringByAppendingString:appName];
slouken@1931
   175
    [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
slouken@1931
   176
slouken@1931
   177
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@1931
   178
slouken@6515
   179
    [appleMenu addItemWithTitle:@"Preferences…" action:nil keyEquivalent:@","];
slouken@6515
   180
slouken@6515
   181
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@6515
   182
slouken@6515
   183
    serviceMenu = [[NSMenu alloc] initWithTitle:@""];
slouken@6515
   184
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
slouken@6515
   185
    [menuItem setSubmenu:serviceMenu];
slouken@6515
   186
slouken@6515
   187
    [NSApp setServicesMenu:serviceMenu];
slouken@6836
   188
    [serviceMenu release];
slouken@5377
   189
slouken@5377
   190
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@5377
   191
slouken@1931
   192
    title = [@"Hide " stringByAppendingString:appName];
slouken@6515
   193
    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
slouken@1931
   194
slouken@6515
   195
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
slouken@1931
   196
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
slouken@1931
   197
slouken@1931
   198
    [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
slouken@1931
   199
slouken@1931
   200
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@1931
   201
slouken@1931
   202
    title = [@"Quit " stringByAppendingString:appName];
slouken@6515
   203
    [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
slouken@7191
   204
slouken@1931
   205
    /* Put menu into the menubar */
slouken@1931
   206
    menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
slouken@1931
   207
    [menuItem setSubmenu:appleMenu];
slouken@1931
   208
    [[NSApp mainMenu] addItem:menuItem];
slouken@1931
   209
    [menuItem release];
slouken@1931
   210
slouken@1931
   211
    /* Tell the application object that this is now the application menu */
slouken@1931
   212
    [NSApp setAppleMenu:appleMenu];
slouken@1931
   213
    [appleMenu release];
slouken@1931
   214
slouken@1931
   215
slouken@1931
   216
    /* Create the window menu */
slouken@1931
   217
    windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
slouken@7191
   218
slouken@6515
   219
    /* Add menu items */
slouken@6515
   220
    [windowMenu addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
slouken@7191
   221
slouken@6515
   222
    [windowMenu addItemWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""];
slouken@6515
   223
slouken@1931
   224
    /* Put menu into the menubar */
slouken@1931
   225
    menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
slouken@1931
   226
    [menuItem setSubmenu:windowMenu];
slouken@1931
   227
    [[NSApp mainMenu] addItem:menuItem];
slouken@1931
   228
    [menuItem release];
slouken@7191
   229
slouken@1931
   230
    /* Tell the application object that this is now the window menu */
slouken@1931
   231
    [NSApp setWindowsMenu:windowMenu];
slouken@1931
   232
    [windowMenu release];
slouken@7952
   233
slouken@7952
   234
slouken@7952
   235
    /* Add the fullscreen view toggle menu option, if supported */
slouken@7952
   236
    if ([NSApp respondsToSelector:@selector(setPresentationOptions:)]) {
slouken@7952
   237
        /* Create the view menu */
slouken@7952
   238
        viewMenu = [[NSMenu alloc] initWithTitle:@"View"];
slouken@7952
   239
slouken@7952
   240
        /* Add menu items */
slouken@7952
   241
        menuItem = [viewMenu addItemWithTitle:@"Toggle Full Screen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"];
slouken@7952
   242
        [menuItem setKeyEquivalentModifierMask:NSControlKeyMask | NSCommandKeyMask];
slouken@7952
   243
slouken@7952
   244
        /* Put menu into the menubar */
slouken@7952
   245
        menuItem = [[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""];
slouken@7952
   246
        [menuItem setSubmenu:viewMenu];
slouken@7952
   247
        [[NSApp mainMenu] addItem:menuItem];
slouken@7952
   248
        [menuItem release];
slouken@7952
   249
slouken@7952
   250
        [viewMenu release];
slouken@7952
   251
    }
slouken@1931
   252
}
slouken@1931
   253
slouken@1931
   254
void
slouken@1931
   255
Cocoa_RegisterApp(void)
slouken@9087
   256
{ @autoreleasepool
slouken@1931
   257
{
icculus@6639
   258
    /* This can get called more than once! Be careful what you initialize! */
slouken@1931
   259
slouken@6848
   260
    if (NSApp == nil) {
slouken@8239
   261
        [SDLApplication sharedApplication];
icculus@8648
   262
        SDL_assert(NSApp != nil);
slouken@1931
   263
icculus@9364
   264
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
icculus@9364
   265
        if ([NSApp respondsToSelector:@selector(setActivationPolicy:)]) {
icculus@9364
   266
#endif
icculus@9364
   267
            [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
icculus@9364
   268
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
icculus@9364
   269
        } else {
icculus@9364
   270
            ProcessSerialNumber psn = {0, kCurrentProcess};
icculus@9364
   271
            TransformProcessType(&psn, kProcessTransformToForegroundApplication);
icculus@9364
   272
        }
icculus@9364
   273
#endif
icculus@9364
   274
icculus@9364
   275
        [NSApp activateIgnoringOtherApps:YES];
icculus@9364
   276
slouken@6848
   277
        if ([NSApp mainMenu] == nil) {
slouken@6848
   278
            CreateApplicationMenus();
slouken@1931
   279
        }
slouken@6848
   280
        [NSApp finishLaunching];
slouken@8289
   281
        NSDictionary *appDefaults = [[NSDictionary alloc] initWithObjectsAndKeys:
slouken@8290
   282
            [NSNumber numberWithBool:NO], @"AppleMomentumScrollSupported",
slouken@8290
   283
            [NSNumber numberWithBool:NO], @"ApplePressAndHoldEnabled",
jorgen@8718
   284
            [NSNumber numberWithBool:YES], @"ApplePersistenceIgnoreState",
slouken@8290
   285
            nil];
slouken@7739
   286
        [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
icculus@8648
   287
        [appDefaults release];
slouken@1931
   288
    }
jorgen@7801
   289
    if (NSApp && !appDelegate) {
jorgen@7801
   290
        appDelegate = [[SDLAppDelegate alloc] init];
jorgen@7801
   291
jorgen@7801
   292
        /* If someone else has an app delegate, it means we can't turn a
jorgen@7801
   293
         * termination into SDL_Quit, and we can't handle application:openFile:
jorgen@7801
   294
         */
jorgen@7801
   295
        if (![NSApp delegate]) {
slouken@8986
   296
            [(NSApplication *)NSApp setDelegate:appDelegate];
jorgen@7801
   297
        } else {
jorgen@7801
   298
            appDelegate->seenFirstActivate = YES;
jorgen@7801
   299
        }
slouken@6848
   300
    }
slouken@9087
   301
}}
slouken@1931
   302
slouken@1931
   303
void
slouken@1931
   304
Cocoa_PumpEvents(_THIS)
slouken@9087
   305
{ @autoreleasepool
slouken@1931
   306
{
slouken@3025
   307
    /* Update activity every 30 seconds to prevent screensaver */
icculus@9364
   308
    SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
icculus@9364
   309
    if (_this->suspend_screensaver && !data->screensaver_use_iopm) {
slouken@3025
   310
        Uint32 now = SDL_GetTicks();
slouken@3025
   311
        if (!data->screensaver_activity ||
slouken@7857
   312
            SDL_TICKS_PASSED(now, data->screensaver_activity + 30000)) {
slouken@3025
   313
            UpdateSystemActivity(UsrActivity);
slouken@3025
   314
            data->screensaver_activity = now;
slouken@3025
   315
        }
slouken@3025
   316
    }
slouken@3025
   317
slouken@6848
   318
    for ( ; ; ) {
slouken@6848
   319
        NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
slouken@6848
   320
        if ( event == nil ) {
slouken@6848
   321
            break;
slouken@1931
   322
        }
slouken@7191
   323
slouken@6848
   324
        switch ([event type]) {
slouken@6848
   325
        case NSLeftMouseDown:
slouken@6848
   326
        case NSOtherMouseDown:
slouken@6848
   327
        case NSRightMouseDown:
slouken@6848
   328
        case NSLeftMouseUp:
slouken@6848
   329
        case NSOtherMouseUp:
slouken@6848
   330
        case NSRightMouseUp:
slouken@6848
   331
        case NSLeftMouseDragged:
slouken@6848
   332
        case NSRightMouseDragged:
slouken@6848
   333
        case NSOtherMouseDragged: /* usually middle mouse dragged */
slouken@6848
   334
        case NSMouseMoved:
slouken@6848
   335
        case NSScrollWheel:
slouken@6848
   336
            Cocoa_HandleMouseEvent(_this, event);
slouken@6848
   337
            break;
slouken@6848
   338
        case NSKeyDown:
slouken@6848
   339
        case NSKeyUp:
slouken@6848
   340
        case NSFlagsChanged:
slouken@6848
   341
            Cocoa_HandleKeyEvent(_this, event);
slouken@6848
   342
            break;
slouken@6848
   343
        default:
slouken@6848
   344
            break;
slouken@6848
   345
        }
slouken@6848
   346
        /* Pass through to NSApp to make sure everything stays in sync */
slouken@6848
   347
        [NSApp sendEvent:event];
slouken@1931
   348
    }
slouken@9087
   349
}}
slouken@1931
   350
icculus@9364
   351
void
icculus@9364
   352
Cocoa_SuspendScreenSaver(_THIS)
icculus@9364
   353
{ @autoreleasepool
icculus@9364
   354
{
icculus@9364
   355
    SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
icculus@9364
   356
icculus@9364
   357
    if (!data->screensaver_use_iopm) {
icculus@9364
   358
        return;
icculus@9364
   359
    }
icculus@9364
   360
icculus@9364
   361
    if (data->screensaver_assertion) {
icculus@9364
   362
        IOPMAssertionRelease(data->screensaver_assertion);
icculus@9364
   363
        data->screensaver_assertion = 0;
icculus@9364
   364
    }
icculus@9364
   365
icculus@9364
   366
    if (_this->suspend_screensaver) {
icculus@9364
   367
        /* FIXME: this should ideally describe the real reason why the game
icculus@9364
   368
         * called SDL_DisableScreenSaver. Note that the name is only meant to be
icculus@9364
   369
         * seen by OS X power users. there's an additional optional human-readable
icculus@9364
   370
         * (localized) reason parameter which we don't set.
icculus@9364
   371
         */
icculus@9364
   372
        NSString *name = [GetApplicationName() stringByAppendingString:@" using SDL_DisableScreenSaver"];
icculus@9364
   373
        IOPMAssertionCreateWithDescription(kIOPMAssertPreventUserIdleDisplaySleep,
icculus@9364
   374
                                           (CFStringRef) name,
icculus@9364
   375
                                           NULL, NULL, NULL, 0, NULL,
icculus@9364
   376
                                           &data->screensaver_assertion);
icculus@9364
   377
    }
icculus@9364
   378
}}
icculus@9364
   379
slouken@6044
   380
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
   381
slouken@1931
   382
/* vi: set ts=4 sw=4 expandtab: */