src/video/cocoa/SDL_cocoavideo.m
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Mar 2011 08:49:20 -0800
changeset 5466 006883d5fa51
parent 5375 16877f74123c
child 5535 96594ac5fd1a
permissions -rw-r--r--
Gamma support is back!

New API functions:
SDL_SetWindowBrightness()
SDL_GetWindowBrightness()
SDL_SetWindowGammaRamp()
SDL_GetWindowGammaRamp()
SDL_CalculateGammaRamp()
slouken@1931
     1
/*
slouken@1931
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 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@5375
    24
#include "SDL_endian.h"
slouken@1931
    25
#include "SDL_cocoavideo.h"
egottlieb@4827
    26
#include "SDL_cocoashape.h"
icculus@3660
    27
#include "SDL_assert.h"
slouken@1931
    28
slouken@1931
    29
/* Initialization/Query functions */
slouken@1931
    30
static int Cocoa_VideoInit(_THIS);
slouken@1931
    31
static void Cocoa_VideoQuit(_THIS);
slouken@1931
    32
slouken@1931
    33
/* Cocoa driver bootstrap functions */
slouken@1931
    34
slouken@1931
    35
static int
slouken@1931
    36
Cocoa_Available(void)
slouken@1931
    37
{
slouken@1931
    38
    return (1);
slouken@1931
    39
}
slouken@1931
    40
slouken@1931
    41
static void
slouken@1931
    42
Cocoa_DeleteDevice(SDL_VideoDevice * device)
slouken@1931
    43
{
slouken@1931
    44
    SDL_free(device->driverdata);
slouken@1931
    45
    SDL_free(device);
slouken@1931
    46
}
slouken@1931
    47
slouken@1931
    48
static SDL_VideoDevice *
slouken@1931
    49
Cocoa_CreateDevice(int devindex)
slouken@1931
    50
{
slouken@1931
    51
    SDL_VideoDevice *device;
slouken@1931
    52
    SDL_VideoData *data;
slouken@1931
    53
slouken@1931
    54
    Cocoa_RegisterApp();
slouken@1931
    55
slouken@1931
    56
    /* Initialize all variables that we clean on shutdown */
slouken@1931
    57
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
slouken@1931
    58
    if (device) {
slouken@1931
    59
        data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
slouken@4457
    60
    } else {
slouken@4457
    61
        data = NULL;
slouken@1931
    62
    }
slouken@4457
    63
    if (!data) {
slouken@1931
    64
        SDL_OutOfMemory();
slouken@1931
    65
        if (device) {
slouken@1931
    66
            SDL_free(device);
slouken@1931
    67
        }
slouken@1931
    68
        return NULL;
slouken@1931
    69
    }
slouken@1931
    70
    device->driverdata = data;
slouken@1931
    71
slouken@1959
    72
    /* Find out what version of Mac OS X we're running */
slouken@1959
    73
    Gestalt(gestaltSystemVersion, &data->osversion);
slouken@1959
    74
slouken@1931
    75
    /* Set the function pointers */
slouken@1931
    76
    device->VideoInit = Cocoa_VideoInit;
slouken@1931
    77
    device->VideoQuit = Cocoa_VideoQuit;
slouken@3528
    78
    device->GetDisplayBounds = Cocoa_GetDisplayBounds;
slouken@1931
    79
    device->GetDisplayModes = Cocoa_GetDisplayModes;
slouken@1931
    80
    device->SetDisplayMode = Cocoa_SetDisplayMode;
slouken@1931
    81
    device->PumpEvents = Cocoa_PumpEvents;
slouken@1931
    82
slouken@1931
    83
    device->CreateWindow = Cocoa_CreateWindow;
slouken@1931
    84
    device->CreateWindowFrom = Cocoa_CreateWindowFrom;
slouken@1931
    85
    device->SetWindowTitle = Cocoa_SetWindowTitle;
slouken@5375
    86
    device->SetWindowIcon = Cocoa_SetWindowIcon;
slouken@1931
    87
    device->SetWindowPosition = Cocoa_SetWindowPosition;
slouken@1931
    88
    device->SetWindowSize = Cocoa_SetWindowSize;
slouken@1931
    89
    device->ShowWindow = Cocoa_ShowWindow;
slouken@1931
    90
    device->HideWindow = Cocoa_HideWindow;
slouken@1931
    91
    device->RaiseWindow = Cocoa_RaiseWindow;
slouken@1931
    92
    device->MaximizeWindow = Cocoa_MaximizeWindow;
slouken@1931
    93
    device->MinimizeWindow = Cocoa_MinimizeWindow;
slouken@1931
    94
    device->RestoreWindow = Cocoa_RestoreWindow;
slouken@5249
    95
    device->SetWindowFullscreen = Cocoa_SetWindowFullscreen;
slouken@5466
    96
    device->SetWindowGammaRamp = Cocoa_SetWindowGammaRamp;
slouken@5466
    97
    device->GetWindowGammaRamp = Cocoa_GetWindowGammaRamp;
slouken@1931
    98
    device->SetWindowGrab = Cocoa_SetWindowGrab;
slouken@1931
    99
    device->DestroyWindow = Cocoa_DestroyWindow;
slouken@1931
   100
    device->GetWindowWMInfo = Cocoa_GetWindowWMInfo;
eligottlieb@4810
   101
    
eligottlieb@4810
   102
    device->shape_driver.CreateShaper = Cocoa_CreateShaper;
eligottlieb@4810
   103
    device->shape_driver.SetWindowShape = Cocoa_SetWindowShape;
eligottlieb@4810
   104
    device->shape_driver.ResizeWindowShape = Cocoa_ResizeWindowShape;
eligottlieb@4810
   105
    
slouken@5088
   106
#if SDL_VIDEO_OPENGL_CGL
slouken@1931
   107
    device->GL_LoadLibrary = Cocoa_GL_LoadLibrary;
slouken@1931
   108
    device->GL_GetProcAddress = Cocoa_GL_GetProcAddress;
slouken@3057
   109
    device->GL_UnloadLibrary = Cocoa_GL_UnloadLibrary;
slouken@1931
   110
    device->GL_CreateContext = Cocoa_GL_CreateContext;
slouken@1931
   111
    device->GL_MakeCurrent = Cocoa_GL_MakeCurrent;
slouken@1931
   112
    device->GL_SetSwapInterval = Cocoa_GL_SetSwapInterval;
slouken@1931
   113
    device->GL_GetSwapInterval = Cocoa_GL_GetSwapInterval;
slouken@1931
   114
    device->GL_SwapWindow = Cocoa_GL_SwapWindow;
slouken@1931
   115
    device->GL_DeleteContext = Cocoa_GL_DeleteContext;
slouken@1931
   116
#endif
slouken@1931
   117
slouken@3280
   118
    device->StartTextInput = Cocoa_StartTextInput;
slouken@3280
   119
    device->StopTextInput = Cocoa_StopTextInput;
slouken@3280
   120
    device->SetTextInputRect = Cocoa_SetTextInputRect;
slouken@3280
   121
slouken@4499
   122
    device->SetClipboardText = Cocoa_SetClipboardText;
slouken@4499
   123
    device->GetClipboardText = Cocoa_GetClipboardText;
slouken@4499
   124
    device->HasClipboardText = Cocoa_HasClipboardText;
slouken@4499
   125
slouken@1931
   126
    device->free = Cocoa_DeleteDevice;
slouken@1931
   127
slouken@1931
   128
    return device;
slouken@1931
   129
}
slouken@1931
   130
slouken@1931
   131
VideoBootStrap COCOA_bootstrap = {
slouken@1931
   132
    "cocoa", "SDL Cocoa video driver",
slouken@1931
   133
    Cocoa_Available, Cocoa_CreateDevice
slouken@1931
   134
};
slouken@1931
   135
slouken@1931
   136
slouken@1931
   137
int
slouken@1931
   138
Cocoa_VideoInit(_THIS)
slouken@1931
   139
{
slouken@1931
   140
    Cocoa_InitModes(_this);
slouken@1931
   141
    Cocoa_InitKeyboard(_this);
slouken@1931
   142
    Cocoa_InitMouse(_this);
slouken@1931
   143
    return 0;
slouken@1931
   144
}
slouken@1931
   145
slouken@1931
   146
void
slouken@1931
   147
Cocoa_VideoQuit(_THIS)
slouken@1931
   148
{
slouken@1931
   149
    Cocoa_QuitModes(_this);
slouken@1931
   150
    Cocoa_QuitKeyboard(_this);
slouken@1931
   151
    Cocoa_QuitMouse(_this);
slouken@1931
   152
}
slouken@1931
   153
slouken@5375
   154
/* This function assumes that it's called from within an autorelease pool */
slouken@5375
   155
NSImage *
slouken@5375
   156
Cocoa_CreateImage(SDL_Surface * surface)
slouken@5375
   157
{
slouken@5375
   158
    SDL_Surface *converted;
slouken@5375
   159
    NSBitmapImageRep *imgrep;
slouken@5375
   160
    Uint8 *pixels;
slouken@5375
   161
    int i;
slouken@5375
   162
    NSImage *img;
slouken@5375
   163
slouken@5375
   164
    converted = SDL_ConvertSurfaceFormat(surface, 
slouken@5375
   165
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@5375
   166
                                         SDL_PIXELFORMAT_RGBA8888,
slouken@5375
   167
#else
slouken@5375
   168
                                         SDL_PIXELFORMAT_ABGR8888,
slouken@5375
   169
#endif
slouken@5375
   170
                                         0);
slouken@5375
   171
    if (!converted) {
slouken@5375
   172
        return nil;
slouken@5375
   173
    }
slouken@5375
   174
slouken@5375
   175
    imgrep = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
slouken@5375
   176
                    pixelsWide: converted->w
slouken@5375
   177
                    pixelsHigh: converted->h
slouken@5375
   178
                    bitsPerSample: 8
slouken@5375
   179
                    samplesPerPixel: 4
slouken@5375
   180
                    hasAlpha: YES
slouken@5375
   181
                    isPlanar: NO
slouken@5375
   182
                    colorSpaceName: NSDeviceRGBColorSpace
slouken@5375
   183
                    bytesPerRow: converted->pitch
slouken@5375
   184
                    bitsPerPixel: converted->format->BitsPerPixel] autorelease];
slouken@5375
   185
    if (imgrep == nil) {
slouken@5375
   186
        SDL_FreeSurface(converted);
slouken@5375
   187
        return nil;
slouken@5375
   188
    }
slouken@5375
   189
slouken@5375
   190
    /* Copy the pixels */
slouken@5375
   191
    pixels = [imgrep bitmapData];
slouken@5375
   192
    SDL_memcpy(pixels, converted->pixels, converted->h * converted->pitch);
slouken@5375
   193
    SDL_FreeSurface(converted);
slouken@5375
   194
slouken@5375
   195
    /* Premultiply the alpha channel */
slouken@5375
   196
    for (i = (converted->h * converted->w); i--; ) {
slouken@5375
   197
        Uint8 alpha = pixels[3];
slouken@5375
   198
        pixels[0] = (Uint8)(((Uint16)pixels[0] * alpha) / 255);
slouken@5375
   199
        pixels[1] = (Uint8)(((Uint16)pixels[1] * alpha) / 255);
slouken@5375
   200
        pixels[2] = (Uint8)(((Uint16)pixels[2] * alpha) / 255);
slouken@5375
   201
        pixels += 4;
slouken@5375
   202
    }
slouken@5375
   203
slouken@5375
   204
    img = [[[NSImage alloc] initWithSize: NSMakeSize(surface->w, surface->h)] autorelease];
slouken@5375
   205
    if (img != nil) {
slouken@5375
   206
        [img addRepresentation: imgrep];
slouken@5375
   207
    }
slouken@5375
   208
    return img;
slouken@5375
   209
}
slouken@3647
   210
slouken@3647
   211
/*
slouken@3647
   212
 * Mac OS X assertion support.
slouken@3647
   213
 *
slouken@3647
   214
 * This doesn't really have aything to do with the interfaces of the SDL video
slouken@3647
   215
 *  subsystem, but we need to stuff this into an Objective-C source code file.
slouken@3647
   216
 */
slouken@3647
   217
slouken@3647
   218
SDL_assert_state
slouken@3647
   219
SDL_PromptAssertion_cocoa(const SDL_assert_data *data)
slouken@3647
   220
{
slouken@3647
   221
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@3647
   222
slouken@3647
   223
    NSString *msg = [NSString stringWithFormat:
slouken@3647
   224
            @"Assertion failure at %s (%s:%d), triggered %u time%s:\n  '%s'",
slouken@3647
   225
                data->function, data->filename, data->linenum,
slouken@3647
   226
                data->trigger_count, (data->trigger_count == 1) ? "" : "s",
slouken@3647
   227
                data->condition];
slouken@3647
   228
icculus@3660
   229
    NSLog(@"%s", msg);
slouken@3647
   230
slouken@3647
   231
    /*
slouken@3647
   232
     * !!! FIXME: this code needs to deal with fullscreen modes:
slouken@3647
   233
     * !!! FIXME:  reset to default desktop, runModal, reset to current?
slouken@3647
   234
     */
slouken@3647
   235
slouken@3647
   236
    NSAlert* alert = [[NSAlert alloc] init];
slouken@3647
   237
    [alert setAlertStyle:NSCriticalAlertStyle];
slouken@3647
   238
    [alert setMessageText:msg];
slouken@3647
   239
    [alert addButtonWithTitle:@"Retry"];
slouken@3647
   240
    [alert addButtonWithTitle:@"Break"];
slouken@3647
   241
    [alert addButtonWithTitle:@"Abort"];
slouken@3647
   242
    [alert addButtonWithTitle:@"Ignore"];
slouken@3647
   243
    [alert addButtonWithTitle:@"Always Ignore"];
slouken@3647
   244
    const NSInteger clicked = [alert runModal];
slouken@3647
   245
    [pool release];
slouken@3647
   246
    return (SDL_assert_state) (clicked - NSAlertFirstButtonReturn);
slouken@3647
   247
}
slouken@3647
   248
slouken@1931
   249
/* vim: set ts=4 sw=4 expandtab: */