src/video/cocoa/SDL_cocoavideo.m
author Sam Lantinga <slouken@libsdl.org>
Wed, 13 Jan 2010 06:47:17 +0000
changeset 3647 c5925cd41955
parent 3528 59ff7a2beb57
child 3660 2d17445190f6
permissions -rw-r--r--
First pass at Ryan's assertion code, minor tweaks to come.
slouken@1931
     1
/*
slouken@1931
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 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"
slouken@1931
    25
slouken@1931
    26
/* Initialization/Query functions */
slouken@1931
    27
static int Cocoa_VideoInit(_THIS);
slouken@1931
    28
static void Cocoa_VideoQuit(_THIS);
slouken@1931
    29
slouken@1931
    30
/* Cocoa driver bootstrap functions */
slouken@1931
    31
slouken@1931
    32
static int
slouken@1931
    33
Cocoa_Available(void)
slouken@1931
    34
{
slouken@1931
    35
    return (1);
slouken@1931
    36
}
slouken@1931
    37
slouken@1931
    38
static void
slouken@1931
    39
Cocoa_DeleteDevice(SDL_VideoDevice * device)
slouken@1931
    40
{
slouken@1931
    41
    SDL_VideoData *data = (SDL_VideoData *) device->driverdata;
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@1931
    59
    }
slouken@1931
    60
    if (!device || !data) {
slouken@1931
    61
        SDL_OutOfMemory();
slouken@1931
    62
        if (device) {
slouken@1931
    63
            SDL_free(device);
slouken@1931
    64
        }
slouken@1931
    65
        return NULL;
slouken@1931
    66
    }
slouken@1931
    67
    device->driverdata = data;
slouken@1931
    68
slouken@1959
    69
    /* Find out what version of Mac OS X we're running */
slouken@1959
    70
    Gestalt(gestaltSystemVersion, &data->osversion);
slouken@1959
    71
slouken@1931
    72
    /* Set the function pointers */
slouken@1931
    73
    device->VideoInit = Cocoa_VideoInit;
slouken@1931
    74
    device->VideoQuit = Cocoa_VideoQuit;
slouken@3528
    75
    device->GetDisplayBounds = Cocoa_GetDisplayBounds;
slouken@1931
    76
    device->GetDisplayModes = Cocoa_GetDisplayModes;
slouken@1931
    77
    device->SetDisplayMode = Cocoa_SetDisplayMode;
slouken@1931
    78
    device->PumpEvents = Cocoa_PumpEvents;
slouken@1931
    79
slouken@1931
    80
    device->CreateWindow = Cocoa_CreateWindow;
slouken@1931
    81
    device->CreateWindowFrom = Cocoa_CreateWindowFrom;
slouken@1931
    82
    device->SetWindowTitle = Cocoa_SetWindowTitle;
slouken@1931
    83
    device->SetWindowPosition = Cocoa_SetWindowPosition;
slouken@1931
    84
    device->SetWindowSize = Cocoa_SetWindowSize;
slouken@1931
    85
    device->ShowWindow = Cocoa_ShowWindow;
slouken@1931
    86
    device->HideWindow = Cocoa_HideWindow;
slouken@1931
    87
    device->RaiseWindow = Cocoa_RaiseWindow;
slouken@1931
    88
    device->MaximizeWindow = Cocoa_MaximizeWindow;
slouken@1931
    89
    device->MinimizeWindow = Cocoa_MinimizeWindow;
slouken@1931
    90
    device->RestoreWindow = Cocoa_RestoreWindow;
slouken@1931
    91
    device->SetWindowGrab = Cocoa_SetWindowGrab;
slouken@1931
    92
    device->DestroyWindow = Cocoa_DestroyWindow;
slouken@1931
    93
    device->GetWindowWMInfo = Cocoa_GetWindowWMInfo;
slouken@1952
    94
#ifdef SDL_VIDEO_OPENGL_CGL
slouken@1931
    95
    device->GL_LoadLibrary = Cocoa_GL_LoadLibrary;
slouken@1931
    96
    device->GL_GetProcAddress = Cocoa_GL_GetProcAddress;
slouken@3057
    97
    device->GL_UnloadLibrary = Cocoa_GL_UnloadLibrary;
slouken@1931
    98
    device->GL_CreateContext = Cocoa_GL_CreateContext;
slouken@1931
    99
    device->GL_MakeCurrent = Cocoa_GL_MakeCurrent;
slouken@1931
   100
    device->GL_SetSwapInterval = Cocoa_GL_SetSwapInterval;
slouken@1931
   101
    device->GL_GetSwapInterval = Cocoa_GL_GetSwapInterval;
slouken@1931
   102
    device->GL_SwapWindow = Cocoa_GL_SwapWindow;
slouken@1931
   103
    device->GL_DeleteContext = Cocoa_GL_DeleteContext;
slouken@1931
   104
#endif
slouken@1931
   105
slouken@3280
   106
    device->StartTextInput = Cocoa_StartTextInput;
slouken@3280
   107
    device->StopTextInput = Cocoa_StopTextInput;
slouken@3280
   108
    device->SetTextInputRect = Cocoa_SetTextInputRect;
slouken@3280
   109
slouken@1931
   110
    device->free = Cocoa_DeleteDevice;
slouken@1931
   111
slouken@1931
   112
    return device;
slouken@1931
   113
}
slouken@1931
   114
slouken@1931
   115
VideoBootStrap COCOA_bootstrap = {
slouken@1931
   116
    "cocoa", "SDL Cocoa video driver",
slouken@1931
   117
    Cocoa_Available, Cocoa_CreateDevice
slouken@1931
   118
};
slouken@1931
   119
slouken@1931
   120
slouken@1931
   121
int
slouken@1931
   122
Cocoa_VideoInit(_THIS)
slouken@1931
   123
{
slouken@1931
   124
    Cocoa_InitModes(_this);
slouken@1931
   125
    Cocoa_InitKeyboard(_this);
slouken@1931
   126
    Cocoa_InitMouse(_this);
slouken@1931
   127
    return 0;
slouken@1931
   128
}
slouken@1931
   129
slouken@1931
   130
void
slouken@1931
   131
Cocoa_VideoQuit(_THIS)
slouken@1931
   132
{
slouken@1931
   133
    Cocoa_QuitModes(_this);
slouken@1931
   134
    Cocoa_QuitKeyboard(_this);
slouken@1931
   135
    Cocoa_QuitMouse(_this);
slouken@1931
   136
}
slouken@1931
   137
slouken@3647
   138
slouken@3647
   139
/*
slouken@3647
   140
 * Mac OS X assertion support.
slouken@3647
   141
 *
slouken@3647
   142
 * This doesn't really have aything to do with the interfaces of the SDL video
slouken@3647
   143
 *  subsystem, but we need to stuff this into an Objective-C source code file.
slouken@3647
   144
 */
slouken@3647
   145
slouken@3647
   146
SDL_assert_state
slouken@3647
   147
SDL_PromptAssertion_cocoa(const SDL_assert_data *data)
slouken@3647
   148
{
slouken@3647
   149
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3647
   150
slouken@3647
   151
    NSString *msg = [NSString stringWithFormat:
slouken@3647
   152
            @"Assertion failure at %s (%s:%d), triggered %u time%s:\n  '%s'",
slouken@3647
   153
                data->function, data->filename, data->linenum,
slouken@3647
   154
                data->trigger_count, (data->trigger_count == 1) ? "" : "s",
slouken@3647
   155
                data->condition];
slouken@3647
   156
slouken@3647
   157
    NSLog(msg);
slouken@3647
   158
slouken@3647
   159
    /*
slouken@3647
   160
     * !!! FIXME: this code needs to deal with fullscreen modes:
slouken@3647
   161
     * !!! FIXME:  reset to default desktop, runModal, reset to current?
slouken@3647
   162
     */
slouken@3647
   163
slouken@3647
   164
    NSAlert* alert = [[NSAlert alloc] init];
slouken@3647
   165
    [alert setAlertStyle:NSCriticalAlertStyle];
slouken@3647
   166
    [alert setMessageText:msg];
slouken@3647
   167
    [alert addButtonWithTitle:@"Retry"];
slouken@3647
   168
    [alert addButtonWithTitle:@"Break"];
slouken@3647
   169
    [alert addButtonWithTitle:@"Abort"];
slouken@3647
   170
    [alert addButtonWithTitle:@"Ignore"];
slouken@3647
   171
    [alert addButtonWithTitle:@"Always Ignore"];
slouken@3647
   172
    const NSInteger clicked = [alert runModal];
slouken@3647
   173
    [pool release];
slouken@3647
   174
    return (SDL_assert_state) (clicked - NSAlertFirstButtonReturn);
slouken@3647
   175
}
slouken@3647
   176
slouken@1931
   177
/* vim: set ts=4 sw=4 expandtab: */
slouken@3647
   178