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