src/video/cocoa/SDL_cocoaevents.m
author Jørgen P. Tjernø
Mon, 15 Jul 2013 11:58:49 -0700
changeset 7466 3416e8cfbaf0
parent 7191 75360622e65f
child 7469 4310d5aee6fe
permissions -rw-r--r--
Mac: Bring back FS windows when appropriate

This automatically restores FS windows when the application is made
active (Cmd-Tab, you click the Dock icon, or you launch the .app again).
slouken@1931
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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
*/
slouken@1931
    21
#include "SDL_config.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"
slouken@1931
    28
icculus@3624
    29
#if !defined(UsrActivity) && defined(__LP64__) && !defined(__POWER__)
icculus@3624
    30
/*
icculus@3624
    31
 * Workaround for a bug in the 10.5 SDK: By accident, OSService.h does
icculus@3624
    32
 * not include Power.h at all when compiling in 64bit mode. This has
icculus@3624
    33
 * been fixed in 10.6, but for 10.5, we manually define UsrActivity
icculus@3624
    34
 * to ensure compilation works.
icculus@3624
    35
 */
icculus@3624
    36
#define UsrActivity 1
icculus@3624
    37
#endif
slouken@3120
    38
slouken@1931
    39
/* setAppleMenu disappeared from the headers in 10.4 */
slouken@1931
    40
@interface NSApplication(NSAppleMenu)
slouken@1931
    41
- (void)setAppleMenu:(NSMenu *)menu;
slouken@1931
    42
@end
slouken@1931
    43
slouken@1937
    44
@interface SDLAppDelegate : NSObject
slouken@1937
    45
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
jorgen@7466
    46
- (void)applicationDidBecomeActive:(NSNotification *)aNotification;
slouken@1937
    47
@end
slouken@1937
    48
slouken@1937
    49
@implementation SDLAppDelegate : NSObject
slouken@1937
    50
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
slouken@1937
    51
{
slouken@1937
    52
    SDL_SendQuit();
slouken@1937
    53
    return NSTerminateCancel;
slouken@1937
    54
}
slouken@6091
    55
jorgen@7466
    56
- (void)applicationDidBecomeActive:(NSNotification *)aNotification
jorgen@7466
    57
{
jorgen@7466
    58
    SDL_VideoDevice *device = SDL_GetVideoDevice();
jorgen@7466
    59
    if (device && device->windows)
jorgen@7466
    60
    {
jorgen@7466
    61
        SDL_Window *window = device->windows;
jorgen@7466
    62
        int i;
jorgen@7466
    63
        for (i = 0; i < device->num_displays; ++i)
jorgen@7466
    64
        {
jorgen@7466
    65
            SDL_Window *fullscreen_window = device->displays[i].fullscreen_window;
jorgen@7466
    66
            if (fullscreen_window)
jorgen@7466
    67
            {
jorgen@7466
    68
                if (fullscreen_window->flags & SDL_WINDOW_MINIMIZED) {
jorgen@7466
    69
                    SDL_RestoreWindow(fullscreen_window);
jorgen@7466
    70
                }
jorgen@7466
    71
                return;
jorgen@7466
    72
            }
jorgen@7466
    73
        }
jorgen@7466
    74
jorgen@7466
    75
        if (window->flags & SDL_WINDOW_MINIMIZED) {
jorgen@7466
    76
            SDL_RestoreWindow(window);
jorgen@7466
    77
        } else {
jorgen@7466
    78
            SDL_RaiseWindow(window);
jorgen@7466
    79
        }
jorgen@7466
    80
    }
jorgen@7466
    81
}
jorgen@7466
    82
slouken@6091
    83
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
slouken@6091
    84
{
slouken@6091
    85
    return (BOOL)SDL_SendDropFile([filename UTF8String]);
slouken@6091
    86
}
slouken@1937
    87
@end
slouken@1937
    88
slouken@1931
    89
static NSString *
slouken@1931
    90
GetApplicationName(void)
slouken@1931
    91
{
slouken@1931
    92
    NSDictionary *dict;
slouken@1931
    93
    NSString *appName = 0;
slouken@1931
    94
slouken@1931
    95
    /* Determine the application name */
slouken@1931
    96
    dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
slouken@1931
    97
    if (dict)
slouken@1931
    98
        appName = [dict objectForKey: @"CFBundleName"];
slouken@7191
    99
slouken@1931
   100
    if (![appName length])
slouken@1931
   101
        appName = [[NSProcessInfo processInfo] processName];
slouken@1931
   102
slouken@1931
   103
    return appName;
slouken@1931
   104
}
slouken@1931
   105
slouken@1931
   106
static void
slouken@1931
   107
CreateApplicationMenus(void)
slouken@1931
   108
{
slouken@1931
   109
    NSString *appName;
slouken@1931
   110
    NSString *title;
slouken@1931
   111
    NSMenu *appleMenu;
slouken@6515
   112
    NSMenu *serviceMenu;
slouken@1931
   113
    NSMenu *windowMenu;
slouken@1931
   114
    NSMenuItem *menuItem;
slouken@7191
   115
slouken@1931
   116
    /* Create the main menu bar */
slouken@1931
   117
    [NSApp setMainMenu:[[NSMenu alloc] init]];
slouken@1931
   118
slouken@1931
   119
    /* Create the application menu */
slouken@1931
   120
    appName = GetApplicationName();
slouken@1931
   121
    appleMenu = [[NSMenu alloc] initWithTitle:@""];
slouken@7191
   122
slouken@1931
   123
    /* Add menu items */
slouken@1931
   124
    title = [@"About " stringByAppendingString:appName];
slouken@1931
   125
    [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
slouken@1931
   126
slouken@1931
   127
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@1931
   128
slouken@6515
   129
    [appleMenu addItemWithTitle:@"Preferences…" action:nil keyEquivalent:@","];
slouken@6515
   130
slouken@6515
   131
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@6515
   132
slouken@6515
   133
    serviceMenu = [[NSMenu alloc] initWithTitle:@""];
slouken@6515
   134
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
slouken@6515
   135
    [menuItem setSubmenu:serviceMenu];
slouken@6515
   136
slouken@6515
   137
    [NSApp setServicesMenu:serviceMenu];
slouken@6836
   138
    [serviceMenu release];
slouken@5377
   139
slouken@5377
   140
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@5377
   141
slouken@1931
   142
    title = [@"Hide " stringByAppendingString:appName];
slouken@6515
   143
    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
slouken@1931
   144
slouken@6515
   145
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
slouken@1931
   146
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
slouken@1931
   147
slouken@1931
   148
    [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
slouken@1931
   149
slouken@1931
   150
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@1931
   151
slouken@1931
   152
    title = [@"Quit " stringByAppendingString:appName];
slouken@6515
   153
    [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
slouken@7191
   154
slouken@1931
   155
    /* Put menu into the menubar */
slouken@1931
   156
    menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
slouken@1931
   157
    [menuItem setSubmenu:appleMenu];
slouken@1931
   158
    [[NSApp mainMenu] addItem:menuItem];
slouken@1931
   159
    [menuItem release];
slouken@1931
   160
slouken@1931
   161
    /* Tell the application object that this is now the application menu */
slouken@1931
   162
    [NSApp setAppleMenu:appleMenu];
slouken@1931
   163
    [appleMenu release];
slouken@1931
   164
slouken@1931
   165
slouken@1931
   166
    /* Create the window menu */
slouken@1931
   167
    windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
slouken@7191
   168
slouken@6515
   169
    /* Add menu items */
slouken@6515
   170
    [windowMenu addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
slouken@7191
   171
slouken@6515
   172
    [windowMenu addItemWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""];
slouken@6515
   173
slouken@1931
   174
    /* Put menu into the menubar */
slouken@1931
   175
    menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
slouken@1931
   176
    [menuItem setSubmenu:windowMenu];
slouken@1931
   177
    [[NSApp mainMenu] addItem:menuItem];
slouken@1931
   178
    [menuItem release];
slouken@7191
   179
slouken@1931
   180
    /* Tell the application object that this is now the window menu */
slouken@1931
   181
    [NSApp setWindowsMenu:windowMenu];
slouken@1931
   182
    [windowMenu release];
slouken@1931
   183
}
slouken@1931
   184
slouken@1931
   185
void
slouken@1931
   186
Cocoa_RegisterApp(void)
slouken@1931
   187
{
icculus@6639
   188
    /* This can get called more than once! Be careful what you initialize! */
slouken@1931
   189
    ProcessSerialNumber psn;
slouken@6848
   190
    NSAutoreleasePool *pool;
slouken@1931
   191
slouken@1931
   192
    if (!GetCurrentProcess(&psn)) {
slouken@1931
   193
        TransformProcessType(&psn, kProcessTransformToForegroundApplication);
slouken@1931
   194
        SetFrontProcess(&psn);
slouken@1931
   195
    }
slouken@1931
   196
slouken@6848
   197
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   198
    if (NSApp == nil) {
slouken@6848
   199
        [NSApplication sharedApplication];
slouken@1931
   200
slouken@6848
   201
        if ([NSApp mainMenu] == nil) {
slouken@6848
   202
            CreateApplicationMenus();
slouken@1931
   203
        }
slouken@6848
   204
        [NSApp finishLaunching];
slouken@1931
   205
    }
slouken@6848
   206
    if ([NSApp delegate] == nil) {
slouken@6848
   207
        [NSApp setDelegate:[[SDLAppDelegate alloc] init]];
slouken@6848
   208
    }
slouken@6848
   209
    [pool release];
slouken@1931
   210
}
slouken@1931
   211
slouken@1931
   212
void
slouken@1931
   213
Cocoa_PumpEvents(_THIS)
slouken@1931
   214
{
slouken@6848
   215
    NSAutoreleasePool *pool;
slouken@6848
   216
slouken@3025
   217
    /* Update activity every 30 seconds to prevent screensaver */
slouken@3025
   218
    if (_this->suspend_screensaver) {
slouken@3025
   219
        SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
slouken@3025
   220
        Uint32 now = SDL_GetTicks();
slouken@3025
   221
        if (!data->screensaver_activity ||
slouken@3025
   222
            (int)(now-data->screensaver_activity) >= 30000) {
slouken@3025
   223
            UpdateSystemActivity(UsrActivity);
slouken@3025
   224
            data->screensaver_activity = now;
slouken@3025
   225
        }
slouken@3025
   226
    }
slouken@3025
   227
slouken@6848
   228
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   229
    for ( ; ; ) {
slouken@6848
   230
        NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
slouken@6848
   231
        if ( event == nil ) {
slouken@6848
   232
            break;
slouken@1931
   233
        }
slouken@7191
   234
slouken@6848
   235
        switch ([event type]) {
slouken@6848
   236
        case NSLeftMouseDown:
slouken@6848
   237
        case NSOtherMouseDown:
slouken@6848
   238
        case NSRightMouseDown:
slouken@6848
   239
        case NSLeftMouseUp:
slouken@6848
   240
        case NSOtherMouseUp:
slouken@6848
   241
        case NSRightMouseUp:
slouken@6848
   242
        case NSLeftMouseDragged:
slouken@6848
   243
        case NSRightMouseDragged:
slouken@6848
   244
        case NSOtherMouseDragged: /* usually middle mouse dragged */
slouken@6848
   245
        case NSMouseMoved:
slouken@6848
   246
        case NSScrollWheel:
slouken@6848
   247
            Cocoa_HandleMouseEvent(_this, event);
slouken@6848
   248
            break;
slouken@6848
   249
        case NSKeyDown:
slouken@6848
   250
        case NSKeyUp:
slouken@6848
   251
        case NSFlagsChanged:
slouken@6848
   252
            Cocoa_HandleKeyEvent(_this, event);
slouken@6848
   253
            break;
slouken@6848
   254
        default:
slouken@6848
   255
            break;
slouken@6848
   256
        }
slouken@6848
   257
        /* Pass through to NSApp to make sure everything stays in sync */
slouken@6848
   258
        [NSApp sendEvent:event];
slouken@1931
   259
    }
slouken@6848
   260
    [pool release];
slouken@1931
   261
}
slouken@1931
   262
slouken@6044
   263
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
   264
slouken@1931
   265
/* vi: set ts=4 sw=4 expandtab: */