src/video/cocoa/SDL_cocoavideo.m
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Feb 2011 00:25:44 -0800
changeset 5249 7a963be087ef
parent 5088 455bc74f7034
child 5262 b530ef003506
permissions -rw-r--r--
Mostly fixed fullscreen mode on Mac OS X, and you can toggle it on and off.

There are still some problems with the ConvertNSRect() calculations when switching video modes, which causes wierd window positioning issues, and the fullscreen window is still minimized on exit.
slouken@1931
     1
/*
slouken@1931
     2
    SDL - Simple DirectMedia Layer
slouken@4498
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@1931
     4
slouken@1931
     5
    This library is free software; you can redistribute it and/or
slouken@1931
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1931
     7
    License as published by the Free Software Foundation; either
slouken@1931
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1931
     9
slouken@1931
    10
    This library is distributed in the hope that it will be useful,
slouken@1931
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1931
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1931
    13
    Lesser General Public License for more details.
slouken@1931
    14
slouken@1931
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1931
    16
    License along with this library; if not, write to the Free Software
slouken@1931
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1931
    18
slouken@1931
    19
    Sam Lantinga
slouken@1931
    20
    slouken@libsdl.org
slouken@1931
    21
*/
slouken@1931
    22
#include "SDL_config.h"
slouken@1931
    23
slouken@1931
    24
#include "SDL_cocoavideo.h"
egottlieb@4827
    25
#include "SDL_cocoashape.h"
icculus@3660
    26
#include "SDL_assert.h"
slouken@1931
    27
slouken@1931
    28
/* Initialization/Query functions */
slouken@1931
    29
static int Cocoa_VideoInit(_THIS);
slouken@1931
    30
static void Cocoa_VideoQuit(_THIS);
slouken@1931
    31
slouken@1931
    32
/* Cocoa driver bootstrap functions */
slouken@1931
    33
slouken@1931
    34
static int
slouken@1931
    35
Cocoa_Available(void)
slouken@1931
    36
{
slouken@1931
    37
    return (1);
slouken@1931
    38
}
slouken@1931
    39
slouken@1931
    40
static void
slouken@1931
    41
Cocoa_DeleteDevice(SDL_VideoDevice * device)
slouken@1931
    42
{
slouken@1931
    43
    SDL_free(device->driverdata);
slouken@1931
    44
    SDL_free(device);
slouken@1931
    45
}
slouken@1931
    46
slouken@1931
    47
static SDL_VideoDevice *
slouken@1931
    48
Cocoa_CreateDevice(int devindex)
slouken@1931
    49
{
slouken@1931
    50
    SDL_VideoDevice *device;
slouken@1931
    51
    SDL_VideoData *data;
slouken@1931
    52
slouken@1931
    53
    Cocoa_RegisterApp();
slouken@1931
    54
slouken@1931
    55
    /* Initialize all variables that we clean on shutdown */
slouken@1931
    56
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
slouken@1931
    57
    if (device) {
slouken@1931
    58
        data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
slouken@4457
    59
    } else {
slouken@4457
    60
        data = NULL;
slouken@1931
    61
    }
slouken@4457
    62
    if (!data) {
slouken@1931
    63
        SDL_OutOfMemory();
slouken@1931
    64
        if (device) {
slouken@1931
    65
            SDL_free(device);
slouken@1931
    66
        }
slouken@1931
    67
        return NULL;
slouken@1931
    68
    }
slouken@1931
    69
    device->driverdata = data;
slouken@1931
    70
slouken@1959
    71
    /* Find out what version of Mac OS X we're running */
slouken@1959
    72
    Gestalt(gestaltSystemVersion, &data->osversion);
slouken@1959
    73
slouken@1931
    74
    /* Set the function pointers */
slouken@1931
    75
    device->VideoInit = Cocoa_VideoInit;
slouken@1931
    76
    device->VideoQuit = Cocoa_VideoQuit;
slouken@3528
    77
    device->GetDisplayBounds = Cocoa_GetDisplayBounds;
slouken@1931
    78
    device->GetDisplayModes = Cocoa_GetDisplayModes;
slouken@1931
    79
    device->SetDisplayMode = Cocoa_SetDisplayMode;
slouken@1931
    80
    device->PumpEvents = Cocoa_PumpEvents;
slouken@1931
    81
slouken@1931
    82
    device->CreateWindow = Cocoa_CreateWindow;
slouken@1931
    83
    device->CreateWindowFrom = Cocoa_CreateWindowFrom;
slouken@1931
    84
    device->SetWindowTitle = Cocoa_SetWindowTitle;
slouken@1931
    85
    device->SetWindowPosition = Cocoa_SetWindowPosition;
slouken@1931
    86
    device->SetWindowSize = Cocoa_SetWindowSize;
slouken@1931
    87
    device->ShowWindow = Cocoa_ShowWindow;
slouken@1931
    88
    device->HideWindow = Cocoa_HideWindow;
slouken@1931
    89
    device->RaiseWindow = Cocoa_RaiseWindow;
slouken@1931
    90
    device->MaximizeWindow = Cocoa_MaximizeWindow;
slouken@1931
    91
    device->MinimizeWindow = Cocoa_MinimizeWindow;
slouken@1931
    92
    device->RestoreWindow = Cocoa_RestoreWindow;
slouken@5249
    93
    device->SetWindowFullscreen = Cocoa_SetWindowFullscreen;
slouken@1931
    94
    device->SetWindowGrab = Cocoa_SetWindowGrab;
slouken@1931
    95
    device->DestroyWindow = Cocoa_DestroyWindow;
slouken@1931
    96
    device->GetWindowWMInfo = Cocoa_GetWindowWMInfo;
eligottlieb@4810
    97
    
eligottlieb@4810
    98
    device->shape_driver.CreateShaper = Cocoa_CreateShaper;
eligottlieb@4810
    99
    device->shape_driver.SetWindowShape = Cocoa_SetWindowShape;
eligottlieb@4810
   100
    device->shape_driver.ResizeWindowShape = Cocoa_ResizeWindowShape;
eligottlieb@4810
   101
    
slouken@5088
   102
#if SDL_VIDEO_OPENGL_CGL
slouken@1931
   103
    device->GL_LoadLibrary = Cocoa_GL_LoadLibrary;
slouken@1931
   104
    device->GL_GetProcAddress = Cocoa_GL_GetProcAddress;
slouken@3057
   105
    device->GL_UnloadLibrary = Cocoa_GL_UnloadLibrary;
slouken@1931
   106
    device->GL_CreateContext = Cocoa_GL_CreateContext;
slouken@1931
   107
    device->GL_MakeCurrent = Cocoa_GL_MakeCurrent;
slouken@1931
   108
    device->GL_SetSwapInterval = Cocoa_GL_SetSwapInterval;
slouken@1931
   109
    device->GL_GetSwapInterval = Cocoa_GL_GetSwapInterval;
slouken@1931
   110
    device->GL_SwapWindow = Cocoa_GL_SwapWindow;
slouken@1931
   111
    device->GL_DeleteContext = Cocoa_GL_DeleteContext;
slouken@1931
   112
#endif
slouken@1931
   113
slouken@3280
   114
    device->StartTextInput = Cocoa_StartTextInput;
slouken@3280
   115
    device->StopTextInput = Cocoa_StopTextInput;
slouken@3280
   116
    device->SetTextInputRect = Cocoa_SetTextInputRect;
slouken@3280
   117
slouken@4499
   118
    device->SetClipboardText = Cocoa_SetClipboardText;
slouken@4499
   119
    device->GetClipboardText = Cocoa_GetClipboardText;
slouken@4499
   120
    device->HasClipboardText = Cocoa_HasClipboardText;
slouken@4499
   121
slouken@1931
   122
    device->free = Cocoa_DeleteDevice;
slouken@1931
   123
slouken@1931
   124
    return device;
slouken@1931
   125
}
slouken@1931
   126
slouken@1931
   127
VideoBootStrap COCOA_bootstrap = {
slouken@1931
   128
    "cocoa", "SDL Cocoa video driver",
slouken@1931
   129
    Cocoa_Available, Cocoa_CreateDevice
slouken@1931
   130
};
slouken@1931
   131
slouken@1931
   132
slouken@1931
   133
int
slouken@1931
   134
Cocoa_VideoInit(_THIS)
slouken@1931
   135
{
slouken@1931
   136
    Cocoa_InitModes(_this);
slouken@1931
   137
    Cocoa_InitKeyboard(_this);
slouken@1931
   138
    Cocoa_InitMouse(_this);
slouken@1931
   139
    return 0;
slouken@1931
   140
}
slouken@1931
   141
slouken@1931
   142
void
slouken@1931
   143
Cocoa_VideoQuit(_THIS)
slouken@1931
   144
{
slouken@1931
   145
    Cocoa_QuitModes(_this);
slouken@1931
   146
    Cocoa_QuitKeyboard(_this);
slouken@1931
   147
    Cocoa_QuitMouse(_this);
slouken@1931
   148
}
slouken@1931
   149
slouken@3647
   150
slouken@3647
   151
/*
slouken@3647
   152
 * Mac OS X assertion support.
slouken@3647
   153
 *
slouken@3647
   154
 * This doesn't really have aything to do with the interfaces of the SDL video
slouken@3647
   155
 *  subsystem, but we need to stuff this into an Objective-C source code file.
slouken@3647
   156
 */
slouken@3647
   157
slouken@3647
   158
SDL_assert_state
slouken@3647
   159
SDL_PromptAssertion_cocoa(const SDL_assert_data *data)
slouken@3647
   160
{
slouken@3647
   161
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3647
   162
slouken@3647
   163
    NSString *msg = [NSString stringWithFormat:
slouken@3647
   164
            @"Assertion failure at %s (%s:%d), triggered %u time%s:\n  '%s'",
slouken@3647
   165
                data->function, data->filename, data->linenum,
slouken@3647
   166
                data->trigger_count, (data->trigger_count == 1) ? "" : "s",
slouken@3647
   167
                data->condition];
slouken@3647
   168
icculus@3660
   169
    NSLog(@"%s", msg);
slouken@3647
   170
slouken@3647
   171
    /*
slouken@3647
   172
     * !!! FIXME: this code needs to deal with fullscreen modes:
slouken@3647
   173
     * !!! FIXME:  reset to default desktop, runModal, reset to current?
slouken@3647
   174
     */
slouken@3647
   175
slouken@3647
   176
    NSAlert* alert = [[NSAlert alloc] init];
slouken@3647
   177
    [alert setAlertStyle:NSCriticalAlertStyle];
slouken@3647
   178
    [alert setMessageText:msg];
slouken@3647
   179
    [alert addButtonWithTitle:@"Retry"];
slouken@3647
   180
    [alert addButtonWithTitle:@"Break"];
slouken@3647
   181
    [alert addButtonWithTitle:@"Abort"];
slouken@3647
   182
    [alert addButtonWithTitle:@"Ignore"];
slouken@3647
   183
    [alert addButtonWithTitle:@"Always Ignore"];
slouken@3647
   184
    const NSInteger clicked = [alert runModal];
slouken@3647
   185
    [pool release];
slouken@3647
   186
    return (SDL_assert_state) (clicked - NSAlertFirstButtonReturn);
slouken@3647
   187
}
slouken@3647
   188
slouken@1931
   189
/* vim: set ts=4 sw=4 expandtab: */