src/video/cocoa/SDL_cocoaevents.m
author Jørgen P. Tjernø
Tue, 16 Jul 2013 01:02:51 -0700
changeset 7469 4310d5aee6fe
parent 7466 3416e8cfbaf0
child 7508 1f64c6874619
permissions -rw-r--r--
Mac: Handle SDL_CreateWindow with SDL_WINDOW_MINIMZED.

This fixes bug #1446. You can now create a window with SDL_CreateWindow(...,
SDL_WINDOW_MINIMIZED), and not have it immediately restore itself.

It also changes SDL_RaiseWindow() to be a no-op on minimized or hidden windows,
which is how it behaves on Windows.
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
jorgen@7469
    44
@interface SDLAppDelegate : NSObject {
jorgen@7469
    45
    BOOL seenFirstActivate;
jorgen@7469
    46
}
jorgen@7469
    47
jorgen@7469
    48
- (id)init;
slouken@1937
    49
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
jorgen@7466
    50
- (void)applicationDidBecomeActive:(NSNotification *)aNotification;
slouken@1937
    51
@end
slouken@1937
    52
slouken@1937
    53
@implementation SDLAppDelegate : NSObject
jorgen@7469
    54
- (id)init
jorgen@7469
    55
{
jorgen@7469
    56
    self = [super init];
jorgen@7469
    57
jorgen@7469
    58
    if (self) {
jorgen@7469
    59
        seenFirstActivate = NO;
jorgen@7469
    60
    }
jorgen@7469
    61
jorgen@7469
    62
    return self;
jorgen@7469
    63
}
jorgen@7469
    64
slouken@1937
    65
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
slouken@1937
    66
{
slouken@1937
    67
    SDL_SendQuit();
slouken@1937
    68
    return NSTerminateCancel;
slouken@1937
    69
}
slouken@6091
    70
jorgen@7466
    71
- (void)applicationDidBecomeActive:(NSNotification *)aNotification
jorgen@7466
    72
{
jorgen@7469
    73
    /* HACK: Ignore the first call. The application gets a
jorgen@7469
    74
     * applicationDidBecomeActive: a little bit after the first window is
jorgen@7469
    75
     * created, and if we don't ignore it, a window that has been created with
jorgen@7469
    76
     * SDL_WINDOW_MINIZED will ~immediately be restored.
jorgen@7469
    77
     */
jorgen@7469
    78
    if (!seenFirstActivate) {
jorgen@7469
    79
        seenFirstActivate = YES;
jorgen@7469
    80
        return;
jorgen@7469
    81
    }
jorgen@7469
    82
jorgen@7466
    83
    SDL_VideoDevice *device = SDL_GetVideoDevice();
jorgen@7466
    84
    if (device && device->windows)
jorgen@7466
    85
    {
jorgen@7466
    86
        SDL_Window *window = device->windows;
jorgen@7466
    87
        int i;
jorgen@7466
    88
        for (i = 0; i < device->num_displays; ++i)
jorgen@7466
    89
        {
jorgen@7466
    90
            SDL_Window *fullscreen_window = device->displays[i].fullscreen_window;
jorgen@7466
    91
            if (fullscreen_window)
jorgen@7466
    92
            {
jorgen@7466
    93
                if (fullscreen_window->flags & SDL_WINDOW_MINIMIZED) {
jorgen@7466
    94
                    SDL_RestoreWindow(fullscreen_window);
jorgen@7466
    95
                }
jorgen@7466
    96
                return;
jorgen@7466
    97
            }
jorgen@7466
    98
        }
jorgen@7466
    99
jorgen@7466
   100
        if (window->flags & SDL_WINDOW_MINIMIZED) {
jorgen@7466
   101
            SDL_RestoreWindow(window);
jorgen@7466
   102
        } else {
jorgen@7466
   103
            SDL_RaiseWindow(window);
jorgen@7466
   104
        }
jorgen@7466
   105
    }
jorgen@7466
   106
}
jorgen@7466
   107
slouken@6091
   108
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
slouken@6091
   109
{
slouken@6091
   110
    return (BOOL)SDL_SendDropFile([filename UTF8String]);
slouken@6091
   111
}
slouken@1937
   112
@end
slouken@1937
   113
slouken@1931
   114
static NSString *
slouken@1931
   115
GetApplicationName(void)
slouken@1931
   116
{
slouken@1931
   117
    NSDictionary *dict;
slouken@1931
   118
    NSString *appName = 0;
slouken@1931
   119
slouken@1931
   120
    /* Determine the application name */
slouken@1931
   121
    dict = (NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
slouken@1931
   122
    if (dict)
slouken@1931
   123
        appName = [dict objectForKey: @"CFBundleName"];
slouken@7191
   124
slouken@1931
   125
    if (![appName length])
slouken@1931
   126
        appName = [[NSProcessInfo processInfo] processName];
slouken@1931
   127
slouken@1931
   128
    return appName;
slouken@1931
   129
}
slouken@1931
   130
slouken@1931
   131
static void
slouken@1931
   132
CreateApplicationMenus(void)
slouken@1931
   133
{
slouken@1931
   134
    NSString *appName;
slouken@1931
   135
    NSString *title;
slouken@1931
   136
    NSMenu *appleMenu;
slouken@6515
   137
    NSMenu *serviceMenu;
slouken@1931
   138
    NSMenu *windowMenu;
slouken@1931
   139
    NSMenuItem *menuItem;
slouken@7191
   140
slouken@1931
   141
    /* Create the main menu bar */
slouken@1931
   142
    [NSApp setMainMenu:[[NSMenu alloc] init]];
slouken@1931
   143
slouken@1931
   144
    /* Create the application menu */
slouken@1931
   145
    appName = GetApplicationName();
slouken@1931
   146
    appleMenu = [[NSMenu alloc] initWithTitle:@""];
slouken@7191
   147
slouken@1931
   148
    /* Add menu items */
slouken@1931
   149
    title = [@"About " stringByAppendingString:appName];
slouken@1931
   150
    [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
slouken@1931
   151
slouken@1931
   152
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@1931
   153
slouken@6515
   154
    [appleMenu addItemWithTitle:@"Preferences…" action:nil keyEquivalent:@","];
slouken@6515
   155
slouken@6515
   156
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@6515
   157
slouken@6515
   158
    serviceMenu = [[NSMenu alloc] initWithTitle:@""];
slouken@6515
   159
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
slouken@6515
   160
    [menuItem setSubmenu:serviceMenu];
slouken@6515
   161
slouken@6515
   162
    [NSApp setServicesMenu:serviceMenu];
slouken@6836
   163
    [serviceMenu release];
slouken@5377
   164
slouken@5377
   165
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@5377
   166
slouken@1931
   167
    title = [@"Hide " stringByAppendingString:appName];
slouken@6515
   168
    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
slouken@1931
   169
slouken@6515
   170
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
slouken@1931
   171
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
slouken@1931
   172
slouken@1931
   173
    [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
slouken@1931
   174
slouken@1931
   175
    [appleMenu addItem:[NSMenuItem separatorItem]];
slouken@1931
   176
slouken@1931
   177
    title = [@"Quit " stringByAppendingString:appName];
slouken@6515
   178
    [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
slouken@7191
   179
slouken@1931
   180
    /* Put menu into the menubar */
slouken@1931
   181
    menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
slouken@1931
   182
    [menuItem setSubmenu:appleMenu];
slouken@1931
   183
    [[NSApp mainMenu] addItem:menuItem];
slouken@1931
   184
    [menuItem release];
slouken@1931
   185
slouken@1931
   186
    /* Tell the application object that this is now the application menu */
slouken@1931
   187
    [NSApp setAppleMenu:appleMenu];
slouken@1931
   188
    [appleMenu release];
slouken@1931
   189
slouken@1931
   190
slouken@1931
   191
    /* Create the window menu */
slouken@1931
   192
    windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
slouken@7191
   193
slouken@6515
   194
    /* Add menu items */
slouken@6515
   195
    [windowMenu addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
slouken@7191
   196
slouken@6515
   197
    [windowMenu addItemWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""];
slouken@6515
   198
slouken@1931
   199
    /* Put menu into the menubar */
slouken@1931
   200
    menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
slouken@1931
   201
    [menuItem setSubmenu:windowMenu];
slouken@1931
   202
    [[NSApp mainMenu] addItem:menuItem];
slouken@1931
   203
    [menuItem release];
slouken@7191
   204
slouken@1931
   205
    /* Tell the application object that this is now the window menu */
slouken@1931
   206
    [NSApp setWindowsMenu:windowMenu];
slouken@1931
   207
    [windowMenu release];
slouken@1931
   208
}
slouken@1931
   209
slouken@1931
   210
void
slouken@1931
   211
Cocoa_RegisterApp(void)
slouken@1931
   212
{
icculus@6639
   213
    /* This can get called more than once! Be careful what you initialize! */
slouken@1931
   214
    ProcessSerialNumber psn;
slouken@6848
   215
    NSAutoreleasePool *pool;
slouken@1931
   216
slouken@1931
   217
    if (!GetCurrentProcess(&psn)) {
slouken@1931
   218
        TransformProcessType(&psn, kProcessTransformToForegroundApplication);
slouken@1931
   219
        SetFrontProcess(&psn);
slouken@1931
   220
    }
slouken@1931
   221
slouken@6848
   222
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   223
    if (NSApp == nil) {
slouken@6848
   224
        [NSApplication sharedApplication];
slouken@1931
   225
slouken@6848
   226
        if ([NSApp mainMenu] == nil) {
slouken@6848
   227
            CreateApplicationMenus();
slouken@1931
   228
        }
slouken@6848
   229
        [NSApp finishLaunching];
slouken@1931
   230
    }
slouken@6848
   231
    if ([NSApp delegate] == nil) {
slouken@6848
   232
        [NSApp setDelegate:[[SDLAppDelegate alloc] init]];
slouken@6848
   233
    }
slouken@6848
   234
    [pool release];
slouken@1931
   235
}
slouken@1931
   236
slouken@1931
   237
void
slouken@1931
   238
Cocoa_PumpEvents(_THIS)
slouken@1931
   239
{
slouken@6848
   240
    NSAutoreleasePool *pool;
slouken@6848
   241
slouken@3025
   242
    /* Update activity every 30 seconds to prevent screensaver */
slouken@3025
   243
    if (_this->suspend_screensaver) {
slouken@3025
   244
        SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
slouken@3025
   245
        Uint32 now = SDL_GetTicks();
slouken@3025
   246
        if (!data->screensaver_activity ||
slouken@3025
   247
            (int)(now-data->screensaver_activity) >= 30000) {
slouken@3025
   248
            UpdateSystemActivity(UsrActivity);
slouken@3025
   249
            data->screensaver_activity = now;
slouken@3025
   250
        }
slouken@3025
   251
    }
slouken@3025
   252
slouken@6848
   253
    pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   254
    for ( ; ; ) {
slouken@6848
   255
        NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
slouken@6848
   256
        if ( event == nil ) {
slouken@6848
   257
            break;
slouken@1931
   258
        }
slouken@7191
   259
slouken@6848
   260
        switch ([event type]) {
slouken@6848
   261
        case NSLeftMouseDown:
slouken@6848
   262
        case NSOtherMouseDown:
slouken@6848
   263
        case NSRightMouseDown:
slouken@6848
   264
        case NSLeftMouseUp:
slouken@6848
   265
        case NSOtherMouseUp:
slouken@6848
   266
        case NSRightMouseUp:
slouken@6848
   267
        case NSLeftMouseDragged:
slouken@6848
   268
        case NSRightMouseDragged:
slouken@6848
   269
        case NSOtherMouseDragged: /* usually middle mouse dragged */
slouken@6848
   270
        case NSMouseMoved:
slouken@6848
   271
        case NSScrollWheel:
slouken@6848
   272
            Cocoa_HandleMouseEvent(_this, event);
slouken@6848
   273
            break;
slouken@6848
   274
        case NSKeyDown:
slouken@6848
   275
        case NSKeyUp:
slouken@6848
   276
        case NSFlagsChanged:
slouken@6848
   277
            Cocoa_HandleKeyEvent(_this, event);
slouken@6848
   278
            break;
slouken@6848
   279
        default:
slouken@6848
   280
            break;
slouken@6848
   281
        }
slouken@6848
   282
        /* Pass through to NSApp to make sure everything stays in sync */
slouken@6848
   283
        [NSApp sendEvent:event];
slouken@1931
   284
    }
slouken@6848
   285
    [pool release];
slouken@1931
   286
}
slouken@1931
   287
slouken@6044
   288
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
   289
slouken@1931
   290
/* vi: set ts=4 sw=4 expandtab: */