src/video/cocoa/SDL_cocoaevents.m
author Ryan C. Gordon
Thu, 20 Mar 2014 16:05:57 -0400
changeset 8653 b50c61457c91
parent 8648 2b3eec53c66b
child 8718 61ae7b328789
permissions -rw-r--r--
Static analysis fix: 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;
icculus@8653
   162
    NSMenu *mainMenu;
slouken@7191
   163
slouken@7511
   164
    if (NSApp == nil) {
jorgen@7508
   165
        return;
jorgen@7508
   166
    }
icculus@8653
   167
icculus@8653
   168
    mainMenu = [[NSMenu alloc] init];
icculus@8653
   169
slouken@1931
   170
    /* Create the main menu bar */
icculus@8653
   171
    [NSApp setMainMenu:mainMenu];
icculus@8653
   172
icculus@8653
   173
    [mainMenu release];  /* we're done with it, let NSApp own it. */
icculus@8653
   174
    mainMenu = nil;
slouken@1931
   175
slouken@1931
   176
    /* Create the application menu */
slouken@1931
   177
    appName = GetApplicationName();
slouken@1931
   178
    appleMenu = [[NSMenu alloc] initWithTitle:@""];
slouken@7191
   179
slouken@1931
   180
    /* Add menu items */
slouken@1931
   181
    title = [@"About " stringByAppendingString:appName];
slouken@1931
   182
    [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
slouken@1931
   183
slouken@1931
   184
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@1931
   185
slouken@6515
   186
    [appleMenu addItemWithTitle:@"Preferences…" action:nil keyEquivalent:@","];
slouken@6515
   187
slouken@6515
   188
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@6515
   189
slouken@6515
   190
    serviceMenu = [[NSMenu alloc] initWithTitle:@""];
slouken@6515
   191
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
slouken@6515
   192
    [menuItem setSubmenu:serviceMenu];
slouken@6515
   193
slouken@6515
   194
    [NSApp setServicesMenu:serviceMenu];
slouken@6836
   195
    [serviceMenu release];
slouken@5377
   196
slouken@5377
   197
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@5377
   198
slouken@1931
   199
    title = [@"Hide " stringByAppendingString:appName];
slouken@6515
   200
    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
slouken@1931
   201
slouken@6515
   202
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
slouken@1931
   203
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
slouken@1931
   204
slouken@1931
   205
    [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
slouken@1931
   206
slouken@1931
   207
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@1931
   208
slouken@1931
   209
    title = [@"Quit " stringByAppendingString:appName];
slouken@6515
   210
    [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
slouken@7191
   211
slouken@1931
   212
    /* Put menu into the menubar */
slouken@1931
   213
    menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
slouken@1931
   214
    [menuItem setSubmenu:appleMenu];
slouken@1931
   215
    [[NSApp mainMenu] addItem:menuItem];
slouken@1931
   216
    [menuItem release];
slouken@1931
   217
slouken@1931
   218
    /* Tell the application object that this is now the application menu */
slouken@1931
   219
    [NSApp setAppleMenu:appleMenu];
slouken@1931
   220
    [appleMenu release];
slouken@1931
   221
slouken@1931
   222
slouken@1931
   223
    /* Create the window menu */
slouken@1931
   224
    windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
slouken@7191
   225
slouken@6515
   226
    /* Add menu items */
slouken@6515
   227
    [windowMenu addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
slouken@7191
   228
slouken@6515
   229
    [windowMenu addItemWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""];
slouken@6515
   230
slouken@1931
   231
    /* Put menu into the menubar */
slouken@1931
   232
    menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
slouken@1931
   233
    [menuItem setSubmenu:windowMenu];
slouken@1931
   234
    [[NSApp mainMenu] addItem:menuItem];
slouken@1931
   235
    [menuItem release];
slouken@7191
   236
slouken@1931
   237
    /* Tell the application object that this is now the window menu */
slouken@1931
   238
    [NSApp setWindowsMenu:windowMenu];
slouken@1931
   239
    [windowMenu release];
slouken@7952
   240
slouken@7952
   241
slouken@7952
   242
    /* Add the fullscreen view toggle menu option, if supported */
slouken@7952
   243
    if ([NSApp respondsToSelector:@selector(setPresentationOptions:)]) {
slouken@7952
   244
        /* Create the view menu */
slouken@7952
   245
        viewMenu = [[NSMenu alloc] initWithTitle:@"View"];
slouken@7952
   246
slouken@7952
   247
        /* Add menu items */
slouken@7952
   248
        menuItem = [viewMenu addItemWithTitle:@"Toggle Full Screen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"];
slouken@7952
   249
        [menuItem setKeyEquivalentModifierMask:NSControlKeyMask | NSCommandKeyMask];
slouken@7952
   250
slouken@7952
   251
        /* Put menu into the menubar */
slouken@7952
   252
        menuItem = [[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""];
slouken@7952
   253
        [menuItem setSubmenu:viewMenu];
slouken@7952
   254
        [[NSApp mainMenu] addItem:menuItem];
slouken@7952
   255
        [menuItem release];
slouken@7952
   256
slouken@7952
   257
        [viewMenu release];
slouken@7952
   258
    }
slouken@1931
   259
}
slouken@1931
   260
slouken@1931
   261
void
slouken@1931
   262
Cocoa_RegisterApp(void)
slouken@1931
   263
{
icculus@6639
   264
    /* This can get called more than once! Be careful what you initialize! */
slouken@1931
   265
    ProcessSerialNumber psn;
slouken@6848
   266
    NSAutoreleasePool *pool;
slouken@1931
   267
slouken@1931
   268
    if (!GetCurrentProcess(&psn)) {
slouken@1931
   269
        TransformProcessType(&psn, kProcessTransformToForegroundApplication);
slouken@1931
   270
        SetFrontProcess(&psn);
slouken@1931
   271
    }
slouken@1931
   272
slouken@6848
   273
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   274
    if (NSApp == nil) {
slouken@8239
   275
        [SDLApplication sharedApplication];
icculus@8648
   276
        SDL_assert(NSApp != nil);
slouken@1931
   277
slouken@6848
   278
        if ([NSApp mainMenu] == nil) {
slouken@6848
   279
            CreateApplicationMenus();
slouken@1931
   280
        }
slouken@6848
   281
        [NSApp finishLaunching];
slouken@8289
   282
        NSDictionary *appDefaults = [[NSDictionary alloc] initWithObjectsAndKeys:
slouken@8290
   283
            [NSNumber numberWithBool:NO], @"AppleMomentumScrollSupported",
slouken@8290
   284
            [NSNumber numberWithBool:NO], @"ApplePressAndHoldEnabled",
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]) {
jorgen@7801
   296
            [NSApp setDelegate:appDelegate];
jorgen@7801
   297
        } else {
jorgen@7801
   298
            appDelegate->seenFirstActivate = YES;
jorgen@7801
   299
        }
slouken@6848
   300
    }
slouken@6848
   301
    [pool release];
slouken@1931
   302
}
slouken@1931
   303
slouken@1931
   304
void
slouken@1931
   305
Cocoa_PumpEvents(_THIS)
slouken@1931
   306
{
slouken@6848
   307
    NSAutoreleasePool *pool;
slouken@6848
   308
slouken@3025
   309
    /* Update activity every 30 seconds to prevent screensaver */
slouken@3025
   310
    if (_this->suspend_screensaver) {
slouken@3025
   311
        SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
slouken@3025
   312
        Uint32 now = SDL_GetTicks();
slouken@3025
   313
        if (!data->screensaver_activity ||
slouken@7857
   314
            SDL_TICKS_PASSED(now, data->screensaver_activity + 30000)) {
slouken@3025
   315
            UpdateSystemActivity(UsrActivity);
slouken@3025
   316
            data->screensaver_activity = now;
slouken@3025
   317
        }
slouken@3025
   318
    }
slouken@3025
   319
slouken@6848
   320
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   321
    for ( ; ; ) {
slouken@6848
   322
        NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
slouken@6848
   323
        if ( event == nil ) {
slouken@6848
   324
            break;
slouken@1931
   325
        }
slouken@7191
   326
slouken@6848
   327
        switch ([event type]) {
slouken@6848
   328
        case NSLeftMouseDown:
slouken@6848
   329
        case NSOtherMouseDown:
slouken@6848
   330
        case NSRightMouseDown:
slouken@6848
   331
        case NSLeftMouseUp:
slouken@6848
   332
        case NSOtherMouseUp:
slouken@6848
   333
        case NSRightMouseUp:
slouken@6848
   334
        case NSLeftMouseDragged:
slouken@6848
   335
        case NSRightMouseDragged:
slouken@6848
   336
        case NSOtherMouseDragged: /* usually middle mouse dragged */
slouken@6848
   337
        case NSMouseMoved:
slouken@6848
   338
        case NSScrollWheel:
slouken@6848
   339
            Cocoa_HandleMouseEvent(_this, event);
slouken@6848
   340
            break;
slouken@6848
   341
        case NSKeyDown:
slouken@6848
   342
        case NSKeyUp:
slouken@6848
   343
        case NSFlagsChanged:
slouken@6848
   344
            Cocoa_HandleKeyEvent(_this, event);
slouken@6848
   345
            break;
slouken@6848
   346
        default:
slouken@6848
   347
            break;
slouken@6848
   348
        }
slouken@6848
   349
        /* Pass through to NSApp to make sure everything stays in sync */
slouken@6848
   350
        [NSApp sendEvent:event];
slouken@1931
   351
    }
slouken@6848
   352
    [pool release];
slouken@1931
   353
}
slouken@1931
   354
slouken@6044
   355
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
   356
slouken@1931
   357
/* vi: set ts=4 sw=4 expandtab: */