src/video/cocoa/SDL_cocoaevents.m
author Alfred Reynolds <alfred@valvesoftware.com>
Fri, 31 Jul 2015 09:16:21 -0700
changeset 9822 371d82c6bf94
parent 9820 c0bcc39a3491
child 9826 0b5b4bf90f02
permissions -rw-r--r--
SDL - fix hint check, we want or not and
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5377
diff changeset
     2
  Simple DirectMedia Layer
9619
b94b6d0bff0f Updated the copyright year to 2015
Sam Lantinga <slouken@libsdl.org>
parents: 9419
diff changeset
     3
  Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5377
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5377
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5377
diff changeset
     7
  arising from the use of this software.
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5377
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5377
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5377
diff changeset
    11
  freely, subject to the following restrictions:
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5377
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5377
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5377
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5377
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5377
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5377
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5377
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7952
diff changeset
    21
#include "../../SDL_internal.h"
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    22
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    23
#if SDL_VIDEO_DRIVER_COCOA
3120
1282a042d530 Temporary workaround for building on 64-bit Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 3025
diff changeset
    24
#include "SDL_timer.h"
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
#include "SDL_cocoavideo.h"
2738
79c1bd651f04 Fixed a bunch of compile warnings on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 1962
diff changeset
    27
#include "../../events/SDL_events_c.h"
8648
2b3eec53c66b Static analysis fix: Fixed leaking Objective-C object.
Ryan C. Gordon <icculus@icculus.org>
parents: 8290
diff changeset
    28
#include "SDL_assert.h"
9820
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9619
diff changeset
    29
#include "SDL_hints.h"
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
9364
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
    31
/* This define was added in the 10.9 SDK. */
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
    32
#ifndef kIOPMAssertPreventUserIdleDisplaySleep
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
    33
#define kIOPMAssertPreventUserIdleDisplaySleep kIOPMAssertionTypePreventUserIdleDisplaySleep
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
    34
#endif
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
    35
8239
05cbb1cb4f27 Fixed bug 2347 - On OSX, an SDL app prevents system shutdown.
Sam Lantinga <slouken@libsdl.org>
parents: 8237
diff changeset
    36
@interface SDLApplication : NSApplication
05cbb1cb4f27 Fixed bug 2347 - On OSX, an SDL app prevents system shutdown.
Sam Lantinga <slouken@libsdl.org>
parents: 8237
diff changeset
    37
05cbb1cb4f27 Fixed bug 2347 - On OSX, an SDL app prevents system shutdown.
Sam Lantinga <slouken@libsdl.org>
parents: 8237
diff changeset
    38
- (void)terminate:(id)sender;
05cbb1cb4f27 Fixed bug 2347 - On OSX, an SDL app prevents system shutdown.
Sam Lantinga <slouken@libsdl.org>
parents: 8237
diff changeset
    39
05cbb1cb4f27 Fixed bug 2347 - On OSX, an SDL app prevents system shutdown.
Sam Lantinga <slouken@libsdl.org>
parents: 8237
diff changeset
    40
@end
05cbb1cb4f27 Fixed bug 2347 - On OSX, an SDL app prevents system shutdown.
Sam Lantinga <slouken@libsdl.org>
parents: 8237
diff changeset
    41
05cbb1cb4f27 Fixed bug 2347 - On OSX, an SDL app prevents system shutdown.
Sam Lantinga <slouken@libsdl.org>
parents: 8237
diff changeset
    42
@implementation SDLApplication
05cbb1cb4f27 Fixed bug 2347 - On OSX, an SDL app prevents system shutdown.
Sam Lantinga <slouken@libsdl.org>
parents: 8237
diff changeset
    43
05cbb1cb4f27 Fixed bug 2347 - On OSX, an SDL app prevents system shutdown.
Sam Lantinga <slouken@libsdl.org>
parents: 8237
diff changeset
    44
// Override terminate to handle Quit and System Shutdown smoothly.
05cbb1cb4f27 Fixed bug 2347 - On OSX, an SDL app prevents system shutdown.
Sam Lantinga <slouken@libsdl.org>
parents: 8237
diff changeset
    45
- (void)terminate:(id)sender
05cbb1cb4f27 Fixed bug 2347 - On OSX, an SDL app prevents system shutdown.
Sam Lantinga <slouken@libsdl.org>
parents: 8237
diff changeset
    46
{
05cbb1cb4f27 Fixed bug 2347 - On OSX, an SDL app prevents system shutdown.
Sam Lantinga <slouken@libsdl.org>
parents: 8237
diff changeset
    47
    SDL_SendQuit();
05cbb1cb4f27 Fixed bug 2347 - On OSX, an SDL app prevents system shutdown.
Sam Lantinga <slouken@libsdl.org>
parents: 8237
diff changeset
    48
}
05cbb1cb4f27 Fixed bug 2347 - On OSX, an SDL app prevents system shutdown.
Sam Lantinga <slouken@libsdl.org>
parents: 8237
diff changeset
    49
05cbb1cb4f27 Fixed bug 2347 - On OSX, an SDL app prevents system shutdown.
Sam Lantinga <slouken@libsdl.org>
parents: 8237
diff changeset
    50
@end // SDLApplication
05cbb1cb4f27 Fixed bug 2347 - On OSX, an SDL app prevents system shutdown.
Sam Lantinga <slouken@libsdl.org>
parents: 8237
diff changeset
    51
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
/* setAppleMenu disappeared from the headers in 10.4 */
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
@interface NSApplication(NSAppleMenu)
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
- (void)setAppleMenu:(NSMenu *)menu;
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
@end
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
8986
1c81316ac642 Fixed bug 2631 - Mac: minor code cleanup
Sam Lantinga <slouken@libsdl.org>
parents: 8718
diff changeset
    57
@interface SDLAppDelegate : NSObject <NSApplicationDelegate> {
7801
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
    58
@public
7469
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
    59
    BOOL seenFirstActivate;
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
    60
}
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
    61
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
    62
- (id)init;
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
    63
@end
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
    64
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
    65
@implementation SDLAppDelegate : NSObject
7469
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
    66
- (id)init
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
    67
{
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
    68
    self = [super init];
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
    69
    if (self) {
9419
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    70
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    71
7469
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
    72
        seenFirstActivate = NO;
9419
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    73
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    74
        [center addObserver:self
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    75
                   selector:@selector(windowWillClose:)
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    76
                       name:NSWindowWillCloseNotification
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    77
                     object:nil];
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    78
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    79
        [center addObserver:self
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    80
                   selector:@selector(focusSomeWindow:)
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    81
                       name:NSApplicationDidBecomeActiveNotification
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    82
                     object:nil];
7469
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
    83
    }
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
    84
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
    85
    return self;
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
    86
}
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
    87
7801
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
    88
- (void)dealloc
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
    89
{
9419
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    90
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    91
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    92
    [center removeObserver:self name:NSWindowWillCloseNotification object:nil];
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    93
    [center removeObserver:self name:NSApplicationDidBecomeActiveNotification object:nil];
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    94
7801
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
    95
    [super dealloc];
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
    96
}
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
    97
9419
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    98
- (void)windowWillClose:(NSNotification *)notification;
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
    99
{
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   100
    NSWindow *win = (NSWindow*)[notification object];
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   101
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   102
    if (![win isKeyWindow]) {
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   103
        return;
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   104
    }
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   105
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   106
    /* HACK: Make the next window in the z-order key when the key window is
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   107
     * closed. The custom event loop and/or windowing code we have seems to
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   108
     * prevent the normal behavior: https://bugzilla.libsdl.org/show_bug.cgi?id=1825
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   109
     */
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   110
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   111
    /* +[NSApp orderedWindows] never includes the 'About' window, but we still
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   112
     * want to try its list first since the behavior in other apps is to only
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   113
     * make the 'About' window key if no other windows are on-screen.
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   114
     */
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   115
    for (NSWindow *window in [NSApp orderedWindows]) {
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   116
        if (window != win && [window canBecomeKeyWindow]) {
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   117
            if ([window respondsToSelector:@selector(isOnActiveSpace)]) {
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   118
                if (![window isOnActiveSpace]) {
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   119
                    continue;
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   120
                }
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   121
            }
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   122
            [window makeKeyAndOrderFront:self];
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   123
            return;
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   124
        }
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   125
    }
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   126
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   127
    /* If a window wasn't found above, iterate through all visible windows
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   128
     * (including the 'About' window, if it's shown) and make the first one key.
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   129
     * Note that +[NSWindow windowNumbersWithOptions:] was added in 10.6.
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   130
     */
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   131
    if ([NSWindow respondsToSelector:@selector(windowNumbersWithOptions:)]) {
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   132
        /* Get all visible windows in the active Space, in z-order. */
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   133
        for (NSNumber *num in [NSWindow windowNumbersWithOptions:0]) {
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   134
            NSWindow *window = [NSApp windowWithWindowNumber:[num integerValue]];
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   135
            if (window && window != win && [window canBecomeKeyWindow]) {
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   136
                [window makeKeyAndOrderFront:self];
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   137
                return;
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   138
            }
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   139
        }
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   140
    }
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   141
}
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   142
7801
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
   143
- (void)focusSomeWindow:(NSNotification *)aNotification
7466
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   144
{
7469
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
   145
    /* HACK: Ignore the first call. The application gets a
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
   146
     * applicationDidBecomeActive: a little bit after the first window is
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
   147
     * created, and if we don't ignore it, a window that has been created with
9419
9763f689bced Cocoa: Handle more cases of lost focus when Key window closes (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9364
diff changeset
   148
     * SDL_WINDOW_MINIMIZED will ~immediately be restored.
7469
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
   149
     */
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
   150
    if (!seenFirstActivate) {
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
   151
        seenFirstActivate = YES;
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
   152
        return;
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
   153
    }
4310d5aee6fe Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7466
diff changeset
   154
7466
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   155
    SDL_VideoDevice *device = SDL_GetVideoDevice();
8986
1c81316ac642 Fixed bug 2631 - Mac: minor code cleanup
Sam Lantinga <slouken@libsdl.org>
parents: 8718
diff changeset
   156
    if (device && device->windows) {
7466
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   157
        SDL_Window *window = device->windows;
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   158
        int i;
8986
1c81316ac642 Fixed bug 2631 - Mac: minor code cleanup
Sam Lantinga <slouken@libsdl.org>
parents: 8718
diff changeset
   159
        for (i = 0; i < device->num_displays; ++i) {
7466
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   160
            SDL_Window *fullscreen_window = device->displays[i].fullscreen_window;
8986
1c81316ac642 Fixed bug 2631 - Mac: minor code cleanup
Sam Lantinga <slouken@libsdl.org>
parents: 8718
diff changeset
   161
            if (fullscreen_window) {
7466
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   162
                if (fullscreen_window->flags & SDL_WINDOW_MINIMIZED) {
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   163
                    SDL_RestoreWindow(fullscreen_window);
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   164
                }
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   165
                return;
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   166
            }
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   167
        }
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   168
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   169
        if (window->flags & SDL_WINDOW_MINIMIZED) {
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   170
            SDL_RestoreWindow(window);
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   171
        } else {
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   172
            SDL_RaiseWindow(window);
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   173
        }
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   174
    }
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   175
}
3416e8cfbaf0 Mac: Bring back FS windows when appropriate
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   176
6091
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   177
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   178
{
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   179
    return (BOOL)SDL_SendDropFile([filename UTF8String]);
61fa4f3238c5 First pass at a simple drag and drop API, allowing you to accept files dropped into your application.
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   180
}
1937
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
   181
@end
05e88d266921 Fixed Objective-C memory leaks
Sam Lantinga <slouken@libsdl.org>
parents: 1936
diff changeset
   182
7801
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
   183
static SDLAppDelegate *appDelegate = nil;
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
   184
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
static NSString *
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   186
GetApplicationName(void)
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   187
{
8237
6cc44a5d9eef Fixed bug 2395 - OSX: App name in the menu bar is not localized.
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   188
    NSString *appName;
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   189
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   190
    /* Determine the application name */
8237
6cc44a5d9eef Fixed bug 2395 - OSX: App name in the menu bar is not localized.
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   191
    appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
8986
1c81316ac642 Fixed bug 2631 - Mac: minor code cleanup
Sam Lantinga <slouken@libsdl.org>
parents: 8718
diff changeset
   192
    if (!appName) {
8237
6cc44a5d9eef Fixed bug 2395 - OSX: App name in the menu bar is not localized.
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   193
        appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
8986
1c81316ac642 Fixed bug 2631 - Mac: minor code cleanup
Sam Lantinga <slouken@libsdl.org>
parents: 8718
diff changeset
   194
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   195
8986
1c81316ac642 Fixed bug 2631 - Mac: minor code cleanup
Sam Lantinga <slouken@libsdl.org>
parents: 8718
diff changeset
   196
    if (![appName length]) {
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   197
        appName = [[NSProcessInfo processInfo] processName];
8986
1c81316ac642 Fixed bug 2631 - Mac: minor code cleanup
Sam Lantinga <slouken@libsdl.org>
parents: 8718
diff changeset
   198
    }
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   199
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   200
    return appName;
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   201
}
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   202
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   203
static void
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   204
CreateApplicationMenus(void)
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   205
{
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   206
    NSString *appName;
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   207
    NSString *title;
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   208
    NSMenu *appleMenu;
6515
f750d9ecae1e Bring Cocoa application menus up to par with the OS X Human Interface Guidelines, including supporting key equivalents.
Sam Lantinga <slouken@libsdl.org>
parents: 6461
diff changeset
   209
    NSMenu *serviceMenu;
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   210
    NSMenu *windowMenu;
7952
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   211
    NSMenu *viewMenu;
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   212
    NSMenuItem *menuItem;
8653
b50c61457c91 Static analysis fix: leaking Objective-C object.
Ryan C. Gordon <icculus@icculus.org>
parents: 8648
diff changeset
   213
    NSMenu *mainMenu;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   214
7511
4ce3cc948a00 A little cleanup on the cleanup, just for consistency.
Sam Lantinga <slouken@libsdl.org>
parents: 7508
diff changeset
   215
    if (NSApp == nil) {
7508
1f64c6874619 Fix some clang analyzer warnings.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7469
diff changeset
   216
        return;
1f64c6874619 Fix some clang analyzer warnings.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7469
diff changeset
   217
    }
8653
b50c61457c91 Static analysis fix: leaking Objective-C object.
Ryan C. Gordon <icculus@icculus.org>
parents: 8648
diff changeset
   218
b50c61457c91 Static analysis fix: leaking Objective-C object.
Ryan C. Gordon <icculus@icculus.org>
parents: 8648
diff changeset
   219
    mainMenu = [[NSMenu alloc] init];
b50c61457c91 Static analysis fix: leaking Objective-C object.
Ryan C. Gordon <icculus@icculus.org>
parents: 8648
diff changeset
   220
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   221
    /* Create the main menu bar */
8653
b50c61457c91 Static analysis fix: leaking Objective-C object.
Ryan C. Gordon <icculus@icculus.org>
parents: 8648
diff changeset
   222
    [NSApp setMainMenu:mainMenu];
b50c61457c91 Static analysis fix: leaking Objective-C object.
Ryan C. Gordon <icculus@icculus.org>
parents: 8648
diff changeset
   223
b50c61457c91 Static analysis fix: leaking Objective-C object.
Ryan C. Gordon <icculus@icculus.org>
parents: 8648
diff changeset
   224
    [mainMenu release];  /* we're done with it, let NSApp own it. */
b50c61457c91 Static analysis fix: leaking Objective-C object.
Ryan C. Gordon <icculus@icculus.org>
parents: 8648
diff changeset
   225
    mainMenu = nil;
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   226
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   227
    /* Create the application menu */
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   228
    appName = GetApplicationName();
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   229
    appleMenu = [[NSMenu alloc] initWithTitle:@""];
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   230
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   231
    /* Add menu items */
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   232
    title = [@"About " stringByAppendingString:appName];
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   233
    [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   234
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   235
    [appleMenu addItem:[NSMenuItem separatorItem]];
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   236
6515
f750d9ecae1e Bring Cocoa application menus up to par with the OS X Human Interface Guidelines, including supporting key equivalents.
Sam Lantinga <slouken@libsdl.org>
parents: 6461
diff changeset
   237
    [appleMenu addItemWithTitle:@"Preferences…" action:nil keyEquivalent:@","];
f750d9ecae1e Bring Cocoa application menus up to par with the OS X Human Interface Guidelines, including supporting key equivalents.
Sam Lantinga <slouken@libsdl.org>
parents: 6461
diff changeset
   238
f750d9ecae1e Bring Cocoa application menus up to par with the OS X Human Interface Guidelines, including supporting key equivalents.
Sam Lantinga <slouken@libsdl.org>
parents: 6461
diff changeset
   239
    [appleMenu addItem:[NSMenuItem separatorItem]];
f750d9ecae1e Bring Cocoa application menus up to par with the OS X Human Interface Guidelines, including supporting key equivalents.
Sam Lantinga <slouken@libsdl.org>
parents: 6461
diff changeset
   240
f750d9ecae1e Bring Cocoa application menus up to par with the OS X Human Interface Guidelines, including supporting key equivalents.
Sam Lantinga <slouken@libsdl.org>
parents: 6461
diff changeset
   241
    serviceMenu = [[NSMenu alloc] initWithTitle:@""];
f750d9ecae1e Bring Cocoa application menus up to par with the OS X Human Interface Guidelines, including supporting key equivalents.
Sam Lantinga <slouken@libsdl.org>
parents: 6461
diff changeset
   242
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
f750d9ecae1e Bring Cocoa application menus up to par with the OS X Human Interface Guidelines, including supporting key equivalents.
Sam Lantinga <slouken@libsdl.org>
parents: 6461
diff changeset
   243
    [menuItem setSubmenu:serviceMenu];
f750d9ecae1e Bring Cocoa application menus up to par with the OS X Human Interface Guidelines, including supporting key equivalents.
Sam Lantinga <slouken@libsdl.org>
parents: 6461
diff changeset
   244
f750d9ecae1e Bring Cocoa application menus up to par with the OS X Human Interface Guidelines, including supporting key equivalents.
Sam Lantinga <slouken@libsdl.org>
parents: 6461
diff changeset
   245
    [NSApp setServicesMenu:serviceMenu];
6836
b0ca1571caf3 Fixed bug 1689 - Leaks galore in OS X Cocoa code.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   246
    [serviceMenu release];
5377
6125f6da3dae Added disabled preferences menu option so it looks more like other application menus.
Sam Lantinga <slouken@libsdl.org>
parents: 5266
diff changeset
   247
6125f6da3dae Added disabled preferences menu option so it looks more like other application menus.
Sam Lantinga <slouken@libsdl.org>
parents: 5266
diff changeset
   248
    [appleMenu addItem:[NSMenuItem separatorItem]];
6125f6da3dae Added disabled preferences menu option so it looks more like other application menus.
Sam Lantinga <slouken@libsdl.org>
parents: 5266
diff changeset
   249
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   250
    title = [@"Hide " stringByAppendingString:appName];
6515
f750d9ecae1e Bring Cocoa application menus up to par with the OS X Human Interface Guidelines, including supporting key equivalents.
Sam Lantinga <slouken@libsdl.org>
parents: 6461
diff changeset
   251
    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   252
6515
f750d9ecae1e Bring Cocoa application menus up to par with the OS X Human Interface Guidelines, including supporting key equivalents.
Sam Lantinga <slouken@libsdl.org>
parents: 6461
diff changeset
   253
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   254
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   255
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   256
    [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   257
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   258
    [appleMenu addItem:[NSMenuItem separatorItem]];
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   259
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   260
    title = [@"Quit " stringByAppendingString:appName];
6515
f750d9ecae1e Bring Cocoa application menus up to par with the OS X Human Interface Guidelines, including supporting key equivalents.
Sam Lantinga <slouken@libsdl.org>
parents: 6461
diff changeset
   261
    [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   262
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   263
    /* Put menu into the menubar */
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   264
    menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   265
    [menuItem setSubmenu:appleMenu];
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   266
    [[NSApp mainMenu] addItem:menuItem];
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   267
    [menuItem release];
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   268
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   269
    /* Tell the application object that this is now the application menu */
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   270
    [NSApp setAppleMenu:appleMenu];
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   271
    [appleMenu release];
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   272
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   273
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   274
    /* Create the window menu */
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   275
    windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   276
6515
f750d9ecae1e Bring Cocoa application menus up to par with the OS X Human Interface Guidelines, including supporting key equivalents.
Sam Lantinga <slouken@libsdl.org>
parents: 6461
diff changeset
   277
    /* Add menu items */
f750d9ecae1e Bring Cocoa application menus up to par with the OS X Human Interface Guidelines, including supporting key equivalents.
Sam Lantinga <slouken@libsdl.org>
parents: 6461
diff changeset
   278
    [windowMenu addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   279
6515
f750d9ecae1e Bring Cocoa application menus up to par with the OS X Human Interface Guidelines, including supporting key equivalents.
Sam Lantinga <slouken@libsdl.org>
parents: 6461
diff changeset
   280
    [windowMenu addItemWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""];
f750d9ecae1e Bring Cocoa application menus up to par with the OS X Human Interface Guidelines, including supporting key equivalents.
Sam Lantinga <slouken@libsdl.org>
parents: 6461
diff changeset
   281
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   282
    /* Put menu into the menubar */
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   283
    menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   284
    [menuItem setSubmenu:windowMenu];
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   285
    [[NSApp mainMenu] addItem:menuItem];
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   286
    [menuItem release];
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   287
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   288
    /* Tell the application object that this is now the window menu */
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   289
    [NSApp setWindowsMenu:windowMenu];
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   290
    [windowMenu release];
7952
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   291
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   292
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   293
    /* Add the fullscreen view toggle menu option, if supported */
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   294
    if ([NSApp respondsToSelector:@selector(setPresentationOptions:)]) {
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   295
        /* Create the view menu */
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   296
        viewMenu = [[NSMenu alloc] initWithTitle:@"View"];
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   297
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   298
        /* Add menu items */
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   299
        menuItem = [viewMenu addItemWithTitle:@"Toggle Full Screen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"];
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   300
        [menuItem setKeyEquivalentModifierMask:NSControlKeyMask | NSCommandKeyMask];
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   301
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   302
        /* Put menu into the menubar */
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   303
        menuItem = [[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""];
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   304
        [menuItem setSubmenu:viewMenu];
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   305
        [[NSApp mainMenu] addItem:menuItem];
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   306
        [menuItem release];
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   307
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   308
        [viewMenu release];
5ac1a895b313 Added support for new style fullscreen transitions on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 7857
diff changeset
   309
    }
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   310
}
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   311
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   312
void
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   313
Cocoa_RegisterApp(void)
9087
eef2e43f60d1 Fixed 2680 - OSX: Replace NSAutoreleasePool with @autoreleasepool
Sam Lantinga <slouken@libsdl.org>
parents: 8986
diff changeset
   314
{ @autoreleasepool
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   315
{
6639
c7e81fae23c4 Cocoa_ShowMessageBox() should call Cocoa_RegisterApp().
Ryan C. Gordon <icculus@icculus.org>
parents: 6515
diff changeset
   316
    /* This can get called more than once! Be careful what you initialize! */
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   317
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   318
    if (NSApp == nil) {
8239
05cbb1cb4f27 Fixed bug 2347 - On OSX, an SDL app prevents system shutdown.
Sam Lantinga <slouken@libsdl.org>
parents: 8237
diff changeset
   319
        [SDLApplication sharedApplication];
8648
2b3eec53c66b Static analysis fix: Fixed leaking Objective-C object.
Ryan C. Gordon <icculus@icculus.org>
parents: 8290
diff changeset
   320
        SDL_assert(NSApp != nil);
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   321
9820
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9619
diff changeset
   322
        const char *hint = SDL_GetHint(SDL_HINT_MAC_BACKGROUND_APP);
9822
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9820
diff changeset
   323
		if (!hint || *hint != '0') {
9364
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   324
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
9820
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9619
diff changeset
   325
			if ([NSApp respondsToSelector:@selector(setActivationPolicy:)]) {
9364
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   326
#endif
9820
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9619
diff changeset
   327
				[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
9364
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   328
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
9820
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9619
diff changeset
   329
			} else {
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9619
diff changeset
   330
				ProcessSerialNumber psn = {0, kCurrentProcess};
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9619
diff changeset
   331
				TransformProcessType(&psn, kProcessTransformToForegroundApplication);
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9619
diff changeset
   332
			}
9364
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   333
#endif
9820
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9619
diff changeset
   334
            [NSApp activateIgnoringOtherApps:YES];
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9619
diff changeset
   335
		}
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9619
diff changeset
   336
		
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   337
        if ([NSApp mainMenu] == nil) {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   338
            CreateApplicationMenus();
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   339
        }
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   340
        [NSApp finishLaunching];
8289
d2acd700f8f7 Updated to build on Mac OS X 10.7 with Xcode 4.6
Sam Lantinga <slouken@libsdl.org>
parents: 8250
diff changeset
   341
        NSDictionary *appDefaults = [[NSDictionary alloc] initWithObjectsAndKeys:
8290
b64c52d62264 Fixed dictionary initialization
Sam Lantinga <slouken@libsdl.org>
parents: 8289
diff changeset
   342
            [NSNumber numberWithBool:NO], @"AppleMomentumScrollSupported",
b64c52d62264 Fixed dictionary initialization
Sam Lantinga <slouken@libsdl.org>
parents: 8289
diff changeset
   343
            [NSNumber numberWithBool:NO], @"ApplePressAndHoldEnabled",
8718
61ae7b328789 Mac: Don't prompt to reopen windows after crash.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 8653
diff changeset
   344
            [NSNumber numberWithBool:YES], @"ApplePersistenceIgnoreState",
8290
b64c52d62264 Fixed dictionary initialization
Sam Lantinga <slouken@libsdl.org>
parents: 8289
diff changeset
   345
            nil];
7739
329c9da4bfeb Mac: Turn off momentum-based scrolling.
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   346
        [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
8648
2b3eec53c66b Static analysis fix: Fixed leaking Objective-C object.
Ryan C. Gordon <icculus@icculus.org>
parents: 8290
diff changeset
   347
        [appDefaults release];
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   348
    }
7801
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
   349
    if (NSApp && !appDelegate) {
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
   350
        appDelegate = [[SDLAppDelegate alloc] init];
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
   351
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
   352
        /* If someone else has an app delegate, it means we can't turn a
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
   353
         * termination into SDL_Quit, and we can't handle application:openFile:
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
   354
         */
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
   355
        if (![NSApp delegate]) {
8986
1c81316ac642 Fixed bug 2631 - Mac: minor code cleanup
Sam Lantinga <slouken@libsdl.org>
parents: 8718
diff changeset
   356
            [(NSApplication *)NSApp setDelegate:appDelegate];
7801
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
   357
        } else {
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
   358
            appDelegate->seenFirstActivate = YES;
f00cc0a8cd5d Mac: Better handling when someone else is the app delegate.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7739
diff changeset
   359
        }
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   360
    }
9087
eef2e43f60d1 Fixed 2680 - OSX: Replace NSAutoreleasePool with @autoreleasepool
Sam Lantinga <slouken@libsdl.org>
parents: 8986
diff changeset
   361
}}
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   362
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   363
void
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   364
Cocoa_PumpEvents(_THIS)
9087
eef2e43f60d1 Fixed 2680 - OSX: Replace NSAutoreleasePool with @autoreleasepool
Sam Lantinga <slouken@libsdl.org>
parents: 8986
diff changeset
   365
{ @autoreleasepool
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   366
{
3025
54fac87e1f34 Added an API to enable/disable the screen saver.
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   367
    /* Update activity every 30 seconds to prevent screensaver */
9364
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   368
    SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   369
    if (_this->suspend_screensaver && !data->screensaver_use_iopm) {
3025
54fac87e1f34 Added an API to enable/disable the screen saver.
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   370
        Uint32 now = SDL_GetTicks();
54fac87e1f34 Added an API to enable/disable the screen saver.
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   371
        if (!data->screensaver_activity ||
7857
6388f5229bb7 Added a macro SDL_TICKS_PASSED() to correctly compare two 32-bit tick values.
Sam Lantinga <slouken@libsdl.org>
parents: 7801
diff changeset
   372
            SDL_TICKS_PASSED(now, data->screensaver_activity + 30000)) {
3025
54fac87e1f34 Added an API to enable/disable the screen saver.
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   373
            UpdateSystemActivity(UsrActivity);
54fac87e1f34 Added an API to enable/disable the screen saver.
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   374
            data->screensaver_activity = now;
54fac87e1f34 Added an API to enable/disable the screen saver.
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   375
        }
54fac87e1f34 Added an API to enable/disable the screen saver.
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   376
    }
54fac87e1f34 Added an API to enable/disable the screen saver.
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   377
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   378
    for ( ; ; ) {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   379
        NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   380
        if ( event == nil ) {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   381
            break;
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   382
        }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   383
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   384
        switch ([event type]) {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   385
        case NSLeftMouseDown:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   386
        case NSOtherMouseDown:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   387
        case NSRightMouseDown:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   388
        case NSLeftMouseUp:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   389
        case NSOtherMouseUp:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   390
        case NSRightMouseUp:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   391
        case NSLeftMouseDragged:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   392
        case NSRightMouseDragged:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   393
        case NSOtherMouseDragged: /* usually middle mouse dragged */
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   394
        case NSMouseMoved:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   395
        case NSScrollWheel:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   396
            Cocoa_HandleMouseEvent(_this, event);
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   397
            break;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   398
        case NSKeyDown:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   399
        case NSKeyUp:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   400
        case NSFlagsChanged:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   401
            Cocoa_HandleKeyEvent(_this, event);
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   402
            break;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   403
        default:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   404
            break;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   405
        }
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   406
        /* Pass through to NSApp to make sure everything stays in sync */
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6836
diff changeset
   407
        [NSApp sendEvent:event];
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   408
    }
9087
eef2e43f60d1 Fixed 2680 - OSX: Replace NSAutoreleasePool with @autoreleasepool
Sam Lantinga <slouken@libsdl.org>
parents: 8986
diff changeset
   409
}}
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   410
9364
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   411
void
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   412
Cocoa_SuspendScreenSaver(_THIS)
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   413
{ @autoreleasepool
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   414
{
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   415
    SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   416
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   417
    if (!data->screensaver_use_iopm) {
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   418
        return;
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   419
    }
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   420
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   421
    if (data->screensaver_assertion) {
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   422
        IOPMAssertionRelease(data->screensaver_assertion);
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   423
        data->screensaver_assertion = 0;
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   424
    }
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   425
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   426
    if (_this->suspend_screensaver) {
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   427
        /* FIXME: this should ideally describe the real reason why the game
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   428
         * called SDL_DisableScreenSaver. Note that the name is only meant to be
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   429
         * seen by OS X power users. there's an additional optional human-readable
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   430
         * (localized) reason parameter which we don't set.
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   431
         */
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   432
        NSString *name = [GetApplicationName() stringByAppendingString:@" using SDL_DisableScreenSaver"];
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   433
        IOPMAssertionCreateWithDescription(kIOPMAssertPreventUserIdleDisplaySleep,
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   434
                                           (CFStringRef) name,
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   435
                                           NULL, NULL, NULL, 0, NULL,
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   436
                                           &data->screensaver_assertion);
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   437
    }
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   438
}}
ad9d35983de5 Mac OS X: replace some deprecated APIs with modern equivalents (thanks, Alex!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9087
diff changeset
   439
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   440
#endif /* SDL_VIDEO_DRIVER_COCOA */
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   441
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   442
/* vi: set ts=4 sw=4 expandtab: */