src/video/cocoa/SDL_cocoaevents.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Sep 2018 11:49:25 -0700
changeset 12201 8bdc4d340419
parent 11811 5d94cb6b24d3
child 12468 209066a82aef
permissions -rw-r--r--
Fixed whitespace
slouken@1931
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 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"
alfred@9820
    29
#include "SDL_hints.h"
slouken@1931
    30
icculus@9364
    31
/* This define was added in the 10.9 SDK. */
icculus@9364
    32
#ifndef kIOPMAssertPreventUserIdleDisplaySleep
icculus@9364
    33
#define kIOPMAssertPreventUserIdleDisplaySleep kIOPMAssertionTypePreventUserIdleDisplaySleep
icculus@9364
    34
#endif
icculus@9364
    35
slouken@8239
    36
@interface SDLApplication : NSApplication
slouken@8239
    37
slouken@8239
    38
- (void)terminate:(id)sender;
slouken@10441
    39
- (void)sendEvent:(NSEvent *)theEvent;
slouken@8239
    40
slime73@11697
    41
+ (void)registerUserDefaults;
slime73@11697
    42
slouken@8239
    43
@end
slouken@8239
    44
slouken@8239
    45
@implementation SDLApplication
slouken@8239
    46
slouken@8239
    47
// Override terminate to handle Quit and System Shutdown smoothly.
slouken@8239
    48
- (void)terminate:(id)sender
slouken@8239
    49
{
slouken@8239
    50
    SDL_SendQuit();
slouken@8239
    51
}
slouken@8239
    52
slouken@10535
    53
static SDL_bool s_bShouldHandleEventsInSDLApplication = SDL_FALSE;
slouken@10535
    54
slouken@10535
    55
static void Cocoa_DispatchEvent(NSEvent *theEvent)
slouken@10441
    56
{
slouken@10441
    57
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@10535
    58
slouken@10441
    59
    switch ([theEvent type]) {
slime73@11144
    60
        case NSEventTypeLeftMouseDown:
slime73@11144
    61
        case NSEventTypeOtherMouseDown:
slime73@11144
    62
        case NSEventTypeRightMouseDown:
slime73@11144
    63
        case NSEventTypeLeftMouseUp:
slime73@11144
    64
        case NSEventTypeOtherMouseUp:
slime73@11144
    65
        case NSEventTypeRightMouseUp:
slime73@11144
    66
        case NSEventTypeLeftMouseDragged:
slime73@11144
    67
        case NSEventTypeRightMouseDragged:
slime73@11144
    68
        case NSEventTypeOtherMouseDragged: /* usually middle mouse dragged */
slime73@11144
    69
        case NSEventTypeMouseMoved:
slime73@11144
    70
        case NSEventTypeScrollWheel:
slouken@10441
    71
            Cocoa_HandleMouseEvent(_this, theEvent);
slouken@10441
    72
            break;
slime73@11144
    73
        case NSEventTypeKeyDown:
slime73@11144
    74
        case NSEventTypeKeyUp:
slime73@11144
    75
        case NSEventTypeFlagsChanged:
slouken@10441
    76
            Cocoa_HandleKeyEvent(_this, theEvent);
slouken@10441
    77
            break;
slouken@10441
    78
        default:
slouken@10441
    79
            break;
slouken@10441
    80
    }
slouken@10535
    81
}
slouken@10535
    82
slouken@10535
    83
// Dispatch events here so that we can handle events caught by
slouken@10535
    84
// nextEventMatchingMask in SDL, as well as events caught by other
slouken@10535
    85
// processes (such as CEF) that are passed down to NSApp.
slouken@10535
    86
- (void)sendEvent:(NSEvent *)theEvent
slouken@10535
    87
{
slouken@10535
    88
    if (s_bShouldHandleEventsInSDLApplication) {
slouken@10535
    89
        Cocoa_DispatchEvent(theEvent);
slouken@10535
    90
    }
slouken@10535
    91
slouken@10441
    92
    [super sendEvent:theEvent];
slouken@10441
    93
}
slouken@10441
    94
slime73@11697
    95
+ (void)registerUserDefaults
slime73@11697
    96
{
slime73@11697
    97
    NSDictionary *appDefaults = [[NSDictionary alloc] initWithObjectsAndKeys:
slime73@11697
    98
                                 [NSNumber numberWithBool:NO], @"AppleMomentumScrollSupported",
slime73@11697
    99
                                 [NSNumber numberWithBool:NO], @"ApplePressAndHoldEnabled",
slime73@11697
   100
                                 [NSNumber numberWithBool:YES], @"ApplePersistenceIgnoreState",
slime73@11697
   101
                                 nil];
slime73@11697
   102
    [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
slime73@11697
   103
    [appDefaults release];
slime73@11697
   104
}
slime73@11697
   105
slouken@8239
   106
@end // SDLApplication
slouken@8239
   107
slouken@1931
   108
/* setAppleMenu disappeared from the headers in 10.4 */
slouken@1931
   109
@interface NSApplication(NSAppleMenu)
slouken@1931
   110
- (void)setAppleMenu:(NSMenu *)menu;
slouken@1931
   111
@end
slouken@1931
   112
slouken@8986
   113
@interface SDLAppDelegate : NSObject <NSApplicationDelegate> {
jorgen@7801
   114
@public
jorgen@7469
   115
    BOOL seenFirstActivate;
jorgen@7469
   116
}
jorgen@7469
   117
jorgen@7469
   118
- (id)init;
slouken@1937
   119
@end
slouken@1937
   120
slouken@1937
   121
@implementation SDLAppDelegate : NSObject
jorgen@7469
   122
- (id)init
jorgen@7469
   123
{
jorgen@7469
   124
    self = [super init];
jorgen@7469
   125
    if (self) {
icculus@9419
   126
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
icculus@9419
   127
jorgen@7469
   128
        seenFirstActivate = NO;
icculus@9419
   129
icculus@9419
   130
        [center addObserver:self
icculus@9419
   131
                   selector:@selector(windowWillClose:)
icculus@9419
   132
                       name:NSWindowWillCloseNotification
icculus@9419
   133
                     object:nil];
icculus@9419
   134
icculus@9419
   135
        [center addObserver:self
icculus@9419
   136
                   selector:@selector(focusSomeWindow:)
icculus@9419
   137
                       name:NSApplicationDidBecomeActiveNotification
icculus@9419
   138
                     object:nil];
jorgen@7469
   139
    }
jorgen@7469
   140
jorgen@7469
   141
    return self;
jorgen@7469
   142
}
jorgen@7469
   143
jorgen@7801
   144
- (void)dealloc
jorgen@7801
   145
{
icculus@9419
   146
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
icculus@9419
   147
icculus@9419
   148
    [center removeObserver:self name:NSWindowWillCloseNotification object:nil];
icculus@9419
   149
    [center removeObserver:self name:NSApplicationDidBecomeActiveNotification object:nil];
icculus@9419
   150
jorgen@7801
   151
    [super dealloc];
jorgen@7801
   152
}
jorgen@7801
   153
icculus@9419
   154
- (void)windowWillClose:(NSNotification *)notification;
icculus@9419
   155
{
icculus@9419
   156
    NSWindow *win = (NSWindow*)[notification object];
icculus@9419
   157
icculus@9419
   158
    if (![win isKeyWindow]) {
icculus@9419
   159
        return;
icculus@9419
   160
    }
icculus@9419
   161
icculus@9419
   162
    /* HACK: Make the next window in the z-order key when the key window is
icculus@9419
   163
     * closed. The custom event loop and/or windowing code we have seems to
icculus@9419
   164
     * prevent the normal behavior: https://bugzilla.libsdl.org/show_bug.cgi?id=1825
icculus@9419
   165
     */
icculus@9419
   166
icculus@9419
   167
    /* +[NSApp orderedWindows] never includes the 'About' window, but we still
icculus@9419
   168
     * want to try its list first since the behavior in other apps is to only
icculus@9419
   169
     * make the 'About' window key if no other windows are on-screen.
icculus@9419
   170
     */
icculus@9419
   171
    for (NSWindow *window in [NSApp orderedWindows]) {
icculus@9419
   172
        if (window != win && [window canBecomeKeyWindow]) {
slime73@10176
   173
            if (![window isOnActiveSpace]) {
slime73@10176
   174
                continue;
icculus@9419
   175
            }
icculus@9419
   176
            [window makeKeyAndOrderFront:self];
icculus@9419
   177
            return;
icculus@9419
   178
        }
icculus@9419
   179
    }
icculus@9419
   180
slime73@10176
   181
    /* If a window wasn't found above, iterate through all visible windows in
slime73@10176
   182
     * the active Space in z-order (including the 'About' window, if it's shown)
slime73@10176
   183
     * and make the first one key.
icculus@9419
   184
     */
slime73@10176
   185
    for (NSNumber *num in [NSWindow windowNumbersWithOptions:0]) {
slime73@10176
   186
        NSWindow *window = [NSApp windowWithWindowNumber:[num integerValue]];
slime73@10176
   187
        if (window && window != win && [window canBecomeKeyWindow]) {
slime73@10176
   188
            [window makeKeyAndOrderFront:self];
slime73@10176
   189
            return;
icculus@9419
   190
        }
icculus@9419
   191
    }
icculus@9419
   192
}
icculus@9419
   193
jorgen@7801
   194
- (void)focusSomeWindow:(NSNotification *)aNotification
jorgen@7466
   195
{
jorgen@7469
   196
    /* HACK: Ignore the first call. The application gets a
jorgen@7469
   197
     * applicationDidBecomeActive: a little bit after the first window is
jorgen@7469
   198
     * created, and if we don't ignore it, a window that has been created with
icculus@9419
   199
     * SDL_WINDOW_MINIMIZED will ~immediately be restored.
jorgen@7469
   200
     */
jorgen@7469
   201
    if (!seenFirstActivate) {
jorgen@7469
   202
        seenFirstActivate = YES;
jorgen@7469
   203
        return;
jorgen@7469
   204
    }
jorgen@7469
   205
jorgen@7466
   206
    SDL_VideoDevice *device = SDL_GetVideoDevice();
slouken@8986
   207
    if (device && device->windows) {
jorgen@7466
   208
        SDL_Window *window = device->windows;
jorgen@7466
   209
        int i;
slouken@8986
   210
        for (i = 0; i < device->num_displays; ++i) {
jorgen@7466
   211
            SDL_Window *fullscreen_window = device->displays[i].fullscreen_window;
slouken@8986
   212
            if (fullscreen_window) {
jorgen@7466
   213
                if (fullscreen_window->flags & SDL_WINDOW_MINIMIZED) {
jorgen@7466
   214
                    SDL_RestoreWindow(fullscreen_window);
jorgen@7466
   215
                }
jorgen@7466
   216
                return;
jorgen@7466
   217
            }
jorgen@7466
   218
        }
jorgen@7466
   219
jorgen@7466
   220
        if (window->flags & SDL_WINDOW_MINIMIZED) {
jorgen@7466
   221
            SDL_RestoreWindow(window);
jorgen@7466
   222
        } else {
jorgen@7466
   223
            SDL_RaiseWindow(window);
jorgen@7466
   224
        }
jorgen@7466
   225
    }
jorgen@7466
   226
}
jorgen@7466
   227
slouken@6091
   228
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
slouken@6091
   229
{
icculus@10022
   230
    return (BOOL)SDL_SendDropFile(NULL, [filename UTF8String]) && SDL_SendDropComplete(NULL);
slouken@6091
   231
}
slime73@11545
   232
slime73@11545
   233
- (void)applicationDidFinishLaunching:(NSNotification *)notification
slime73@11545
   234
{
slime73@11545
   235
    /* The menu bar of SDL apps which don't have the typical .app bundle
slime73@11545
   236
     * structure fails to work the first time a window is created (until it's
slime73@11545
   237
     * de-focused and re-focused), if this call is in Cocoa_RegisterApp instead
slime73@11545
   238
     * of here. https://bugzilla.libsdl.org/show_bug.cgi?id=3051
slime73@11545
   239
     */
slime73@11545
   240
    if (!SDL_GetHintBoolean(SDL_HINT_MAC_BACKGROUND_APP, SDL_FALSE)) {
slime73@11545
   241
        [NSApp activateIgnoringOtherApps:YES];
slime73@11545
   242
    }
slime73@11697
   243
slime73@11697
   244
    /* If we call this before NSApp activation, macOS might print a complaint
slime73@11697
   245
     * about ApplePersistenceIgnoreState. */
slime73@11697
   246
    [SDLApplication registerUserDefaults];
slime73@11545
   247
}
slouken@1937
   248
@end
slouken@1937
   249
jorgen@7801
   250
static SDLAppDelegate *appDelegate = nil;
jorgen@7801
   251
slouken@1931
   252
static NSString *
slouken@1931
   253
GetApplicationName(void)
slouken@1931
   254
{
slouken@8237
   255
    NSString *appName;
slouken@1931
   256
slouken@1931
   257
    /* Determine the application name */
slouken@8237
   258
    appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
slouken@8986
   259
    if (!appName) {
slouken@8237
   260
        appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
slouken@8986
   261
    }
slouken@7191
   262
slouken@8986
   263
    if (![appName length]) {
slouken@1931
   264
        appName = [[NSProcessInfo processInfo] processName];
slouken@8986
   265
    }
slouken@1931
   266
slouken@1931
   267
    return appName;
slouken@1931
   268
}
slouken@1931
   269
slouken@1931
   270
static void
slouken@1931
   271
CreateApplicationMenus(void)
slouken@1931
   272
{
slouken@1931
   273
    NSString *appName;
slouken@1931
   274
    NSString *title;
slouken@1931
   275
    NSMenu *appleMenu;
slouken@6515
   276
    NSMenu *serviceMenu;
slouken@1931
   277
    NSMenu *windowMenu;
slouken@7952
   278
    NSMenu *viewMenu;
slouken@1931
   279
    NSMenuItem *menuItem;
icculus@8653
   280
    NSMenu *mainMenu;
slouken@7191
   281
slouken@7511
   282
    if (NSApp == nil) {
jorgen@7508
   283
        return;
jorgen@7508
   284
    }
icculus@8653
   285
icculus@8653
   286
    mainMenu = [[NSMenu alloc] init];
icculus@8653
   287
slouken@1931
   288
    /* Create the main menu bar */
icculus@8653
   289
    [NSApp setMainMenu:mainMenu];
icculus@8653
   290
icculus@8653
   291
    [mainMenu release];  /* we're done with it, let NSApp own it. */
icculus@8653
   292
    mainMenu = nil;
slouken@1931
   293
slouken@1931
   294
    /* Create the application menu */
slouken@1931
   295
    appName = GetApplicationName();
slouken@1931
   296
    appleMenu = [[NSMenu alloc] initWithTitle:@""];
slouken@7191
   297
slouken@1931
   298
    /* Add menu items */
slouken@1931
   299
    title = [@"About " stringByAppendingString:appName];
slouken@1931
   300
    [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
slouken@1931
   301
slouken@1931
   302
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@1931
   303
slouken@6515
   304
    [appleMenu addItemWithTitle:@"Preferences…" action:nil keyEquivalent:@","];
slouken@6515
   305
slouken@6515
   306
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@6515
   307
slouken@6515
   308
    serviceMenu = [[NSMenu alloc] initWithTitle:@""];
slouken@6515
   309
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
slouken@6515
   310
    [menuItem setSubmenu:serviceMenu];
slouken@6515
   311
slouken@6515
   312
    [NSApp setServicesMenu:serviceMenu];
slouken@6836
   313
    [serviceMenu release];
slouken@5377
   314
slouken@5377
   315
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@5377
   316
slouken@1931
   317
    title = [@"Hide " stringByAppendingString:appName];
slouken@6515
   318
    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
slouken@1931
   319
slouken@6515
   320
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
slime73@11144
   321
    [menuItem setKeyEquivalentModifierMask:(NSEventModifierFlagOption|NSEventModifierFlagCommand)];
slouken@1931
   322
slouken@1931
   323
    [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
slouken@1931
   324
slouken@1931
   325
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@1931
   326
slouken@1931
   327
    title = [@"Quit " stringByAppendingString:appName];
slouken@6515
   328
    [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
slouken@7191
   329
slouken@1931
   330
    /* Put menu into the menubar */
slouken@1931
   331
    menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
slouken@1931
   332
    [menuItem setSubmenu:appleMenu];
slouken@1931
   333
    [[NSApp mainMenu] addItem:menuItem];
slouken@1931
   334
    [menuItem release];
slouken@1931
   335
slouken@1931
   336
    /* Tell the application object that this is now the application menu */
slouken@1931
   337
    [NSApp setAppleMenu:appleMenu];
slouken@1931
   338
    [appleMenu release];
slouken@1931
   339
slouken@1931
   340
slouken@1931
   341
    /* Create the window menu */
slouken@1931
   342
    windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
slouken@7191
   343
slouken@6515
   344
    /* Add menu items */
slouken@6515
   345
    [windowMenu addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
slouken@7191
   346
slouken@6515
   347
    [windowMenu addItemWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""];
slouken@6515
   348
slouken@1931
   349
    /* Put menu into the menubar */
slouken@1931
   350
    menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
slouken@1931
   351
    [menuItem setSubmenu:windowMenu];
slouken@1931
   352
    [[NSApp mainMenu] addItem:menuItem];
slouken@1931
   353
    [menuItem release];
slouken@7191
   354
slouken@1931
   355
    /* Tell the application object that this is now the window menu */
slouken@1931
   356
    [NSApp setWindowsMenu:windowMenu];
slouken@1931
   357
    [windowMenu release];
slouken@7952
   358
slouken@7952
   359
slouken@7952
   360
    /* Add the fullscreen view toggle menu option, if supported */
slime73@10176
   361
    if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6) {
slouken@7952
   362
        /* Create the view menu */
slouken@7952
   363
        viewMenu = [[NSMenu alloc] initWithTitle:@"View"];
slouken@7952
   364
slouken@7952
   365
        /* Add menu items */
slouken@7952
   366
        menuItem = [viewMenu addItemWithTitle:@"Toggle Full Screen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"];
slime73@11144
   367
        [menuItem setKeyEquivalentModifierMask:NSEventModifierFlagControl | NSEventModifierFlagCommand];
slouken@7952
   368
slouken@7952
   369
        /* Put menu into the menubar */
slouken@7952
   370
        menuItem = [[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""];
slouken@7952
   371
        [menuItem setSubmenu:viewMenu];
slouken@7952
   372
        [[NSApp mainMenu] addItem:menuItem];
slouken@7952
   373
        [menuItem release];
slouken@7952
   374
slouken@7952
   375
        [viewMenu release];
slouken@7952
   376
    }
slouken@1931
   377
}
slouken@1931
   378
slouken@1931
   379
void
slouken@1931
   380
Cocoa_RegisterApp(void)
slouken@9087
   381
{ @autoreleasepool
slouken@1931
   382
{
icculus@6639
   383
    /* This can get called more than once! Be careful what you initialize! */
slouken@1931
   384
slouken@6848
   385
    if (NSApp == nil) {
slouken@8239
   386
        [SDLApplication sharedApplication];
icculus@8648
   387
        SDL_assert(NSApp != nil);
slouken@1931
   388
slouken@10535
   389
        s_bShouldHandleEventsInSDLApplication = SDL_TRUE;
slouken@10535
   390
slouken@10499
   391
        if (!SDL_GetHintBoolean(SDL_HINT_MAC_BACKGROUND_APP, SDL_FALSE)) {
slime73@10176
   392
            [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
slouken@12201
   393
        }
slouken@12201
   394
slouken@6848
   395
        if ([NSApp mainMenu] == nil) {
slouken@6848
   396
            CreateApplicationMenus();
slouken@1931
   397
        }
slouken@6848
   398
        [NSApp finishLaunching];
slime73@11697
   399
        if ([NSApp delegate]) {
slime73@11697
   400
            /* The SDL app delegate calls this in didFinishLaunching if it's
slime73@11697
   401
             * attached to the NSApp, otherwise we need to call it manually.
slime73@11697
   402
             */
slime73@11697
   403
            [SDLApplication registerUserDefaults];
slime73@11697
   404
        }
slouken@1931
   405
    }
jorgen@7801
   406
    if (NSApp && !appDelegate) {
jorgen@7801
   407
        appDelegate = [[SDLAppDelegate alloc] init];
jorgen@7801
   408
jorgen@7801
   409
        /* If someone else has an app delegate, it means we can't turn a
jorgen@7801
   410
         * termination into SDL_Quit, and we can't handle application:openFile:
jorgen@7801
   411
         */
jorgen@7801
   412
        if (![NSApp delegate]) {
slouken@8986
   413
            [(NSApplication *)NSApp setDelegate:appDelegate];
jorgen@7801
   414
        } else {
jorgen@7801
   415
            appDelegate->seenFirstActivate = YES;
jorgen@7801
   416
        }
slouken@6848
   417
    }
slouken@9087
   418
}}
slouken@1931
   419
slouken@1931
   420
void
slouken@1931
   421
Cocoa_PumpEvents(_THIS)
slouken@9087
   422
{ @autoreleasepool
slouken@1931
   423
{
slime73@11145
   424
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
slouken@10669
   425
    /* Update activity every 30 seconds to prevent screensaver */
slouken@10669
   426
    SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
slouken@10669
   427
    if (_this->suspend_screensaver && !data->screensaver_use_iopm) {
slouken@10669
   428
        Uint32 now = SDL_GetTicks();
slouken@10669
   429
        if (!data->screensaver_activity ||
slouken@10669
   430
            SDL_TICKS_PASSED(now, data->screensaver_activity + 30000)) {
slouken@10669
   431
            UpdateSystemActivity(UsrActivity);
slouken@10669
   432
            data->screensaver_activity = now;
slouken@10669
   433
        }
slouken@10669
   434
    }
slime73@11145
   435
#endif
slouken@10669
   436
slouken@6848
   437
    for ( ; ; ) {
slime73@11144
   438
        NSEvent *event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
slouken@6848
   439
        if ( event == nil ) {
slouken@6848
   440
            break;
slouken@1931
   441
        }
slouken@7191
   442
slouken@10535
   443
        if (!s_bShouldHandleEventsInSDLApplication) {
slouken@10535
   444
            Cocoa_DispatchEvent(event);
slouken@10535
   445
        }
slouken@10535
   446
slouken@10441
   447
        // Pass events down to SDLApplication to be handled in sendEvent:
slouken@6848
   448
        [NSApp sendEvent:event];
slouken@1931
   449
    }
slouken@9087
   450
}}
slouken@1931
   451
icculus@9364
   452
void
icculus@9364
   453
Cocoa_SuspendScreenSaver(_THIS)
icculus@9364
   454
{ @autoreleasepool
icculus@9364
   455
{
icculus@9364
   456
    SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
icculus@9364
   457
slouken@10669
   458
    if (!data->screensaver_use_iopm) {
slouken@10669
   459
        return;
slouken@10669
   460
    }
slouken@10669
   461
icculus@9364
   462
    if (data->screensaver_assertion) {
icculus@9364
   463
        IOPMAssertionRelease(data->screensaver_assertion);
icculus@9364
   464
        data->screensaver_assertion = 0;
icculus@9364
   465
    }
icculus@9364
   466
icculus@9364
   467
    if (_this->suspend_screensaver) {
icculus@9364
   468
        /* FIXME: this should ideally describe the real reason why the game
icculus@9364
   469
         * called SDL_DisableScreenSaver. Note that the name is only meant to be
icculus@9364
   470
         * seen by OS X power users. there's an additional optional human-readable
icculus@9364
   471
         * (localized) reason parameter which we don't set.
icculus@9364
   472
         */
icculus@9364
   473
        NSString *name = [GetApplicationName() stringByAppendingString:@" using SDL_DisableScreenSaver"];
icculus@9364
   474
        IOPMAssertionCreateWithDescription(kIOPMAssertPreventUserIdleDisplaySleep,
icculus@9364
   475
                                           (CFStringRef) name,
icculus@9364
   476
                                           NULL, NULL, NULL, 0, NULL,
icculus@9364
   477
                                           &data->screensaver_assertion);
icculus@9364
   478
    }
icculus@9364
   479
}}
icculus@9364
   480
slouken@6044
   481
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
   482
slouken@1931
   483
/* vi: set ts=4 sw=4 expandtab: */