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