src/video/cocoa/SDL_cocoaevents.m
author Sam Lantinga
Sun, 17 Aug 2014 15:07:00 -0700
changeset 9087 eef2e43f60d1
parent 8986 1c81316ac642
child 9364 ad9d35983de5
permissions -rw-r--r--
Fixed 2680 - OSX: Replace NSAutoreleasePool with @autoreleasepool

Tim McDaniel

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