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