src/video/cocoa/SDL_cocoavideo.m
author Ryan C. Gordon <icculus@icculus.org>
Mon, 04 Jan 2016 23:52:40 -0500
changeset 10019 36f7e8084508
parent 9998 f67cf37e9cd4
child 10025 bf4f8cde1c54
permissions -rw-r--r--
Added SDL_GetDisplayUsableBounds().
slouken@1931
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 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
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@1931
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_COCOA
slouken@6044
    24
icculus@5544
    25
#include "SDL.h"
slouken@5375
    26
#include "SDL_endian.h"
slouken@1931
    27
#include "SDL_cocoavideo.h"
egottlieb@4827
    28
#include "SDL_cocoashape.h"
icculus@3660
    29
#include "SDL_assert.h"
slouken@1931
    30
slouken@1931
    31
/* Initialization/Query functions */
slouken@1931
    32
static int Cocoa_VideoInit(_THIS);
slouken@1931
    33
static void Cocoa_VideoQuit(_THIS);
slouken@1931
    34
slouken@1931
    35
/* Cocoa driver bootstrap functions */
slouken@1931
    36
slouken@1931
    37
static int
slouken@1931
    38
Cocoa_Available(void)
slouken@1931
    39
{
slouken@1931
    40
    return (1);
slouken@1931
    41
}
slouken@1931
    42
slouken@1931
    43
static void
slouken@1931
    44
Cocoa_DeleteDevice(SDL_VideoDevice * device)
slouken@1931
    45
{
slouken@1931
    46
    SDL_free(device->driverdata);
slouken@1931
    47
    SDL_free(device);
slouken@1931
    48
}
slouken@1931
    49
slouken@1931
    50
static SDL_VideoDevice *
slouken@1931
    51
Cocoa_CreateDevice(int devindex)
slouken@1931
    52
{
slouken@1931
    53
    SDL_VideoDevice *device;
slouken@1931
    54
    SDL_VideoData *data;
slouken@1931
    55
slouken@1931
    56
    Cocoa_RegisterApp();
slouken@1931
    57
slouken@1931
    58
    /* Initialize all variables that we clean on shutdown */
slouken@1931
    59
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
slouken@1931
    60
    if (device) {
slouken@1931
    61
        data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
slouken@4457
    62
    } else {
slouken@4457
    63
        data = NULL;
slouken@1931
    64
    }
slouken@4457
    65
    if (!data) {
slouken@1931
    66
        SDL_OutOfMemory();
slouken@7719
    67
        SDL_free(device);
slouken@1931
    68
        return NULL;
slouken@1931
    69
    }
slouken@1931
    70
    device->driverdata = data;
slouken@1931
    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;
icculus@10019
    76
    device->GetDisplayUsableBounds = Cocoa_GetDisplayUsableBounds;
slouken@1931
    77
    device->GetDisplayModes = Cocoa_GetDisplayModes;
slouken@1931
    78
    device->SetDisplayMode = Cocoa_SetDisplayMode;
slouken@1931
    79
    device->PumpEvents = Cocoa_PumpEvents;
icculus@9364
    80
    device->SuspendScreenSaver = Cocoa_SuspendScreenSaver;
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@5375
    85
    device->SetWindowIcon = Cocoa_SetWindowIcon;
slouken@1931
    86
    device->SetWindowPosition = Cocoa_SetWindowPosition;
slouken@1931
    87
    device->SetWindowSize = Cocoa_SetWindowSize;
stopiccot@6681
    88
    device->SetWindowMinimumSize = Cocoa_SetWindowMinimumSize;
slouken@6788
    89
    device->SetWindowMaximumSize = Cocoa_SetWindowMaximumSize;
slouken@1931
    90
    device->ShowWindow = Cocoa_ShowWindow;
slouken@1931
    91
    device->HideWindow = Cocoa_HideWindow;
slouken@1931
    92
    device->RaiseWindow = Cocoa_RaiseWindow;
slouken@1931
    93
    device->MaximizeWindow = Cocoa_MaximizeWindow;
slouken@1931
    94
    device->MinimizeWindow = Cocoa_MinimizeWindow;
slouken@1931
    95
    device->RestoreWindow = Cocoa_RestoreWindow;
icculus@6425
    96
    device->SetWindowBordered = Cocoa_SetWindowBordered;
slouken@5249
    97
    device->SetWindowFullscreen = Cocoa_SetWindowFullscreen;
slouken@5466
    98
    device->SetWindowGammaRamp = Cocoa_SetWindowGammaRamp;
slouken@5466
    99
    device->GetWindowGammaRamp = Cocoa_GetWindowGammaRamp;
slouken@1931
   100
    device->SetWindowGrab = Cocoa_SetWindowGrab;
slouken@1931
   101
    device->DestroyWindow = Cocoa_DestroyWindow;
slouken@1931
   102
    device->GetWindowWMInfo = Cocoa_GetWindowWMInfo;
icculus@8935
   103
    device->SetWindowHitTest = Cocoa_SetWindowHitTest;
slouken@7191
   104
eligottlieb@4810
   105
    device->shape_driver.CreateShaper = Cocoa_CreateShaper;
eligottlieb@4810
   106
    device->shape_driver.SetWindowShape = Cocoa_SetWindowShape;
eligottlieb@4810
   107
    device->shape_driver.ResizeWindowShape = Cocoa_ResizeWindowShape;
slouken@7191
   108
slouken@5088
   109
#if SDL_VIDEO_OPENGL_CGL
slouken@1931
   110
    device->GL_LoadLibrary = Cocoa_GL_LoadLibrary;
slouken@1931
   111
    device->GL_GetProcAddress = Cocoa_GL_GetProcAddress;
slouken@3057
   112
    device->GL_UnloadLibrary = Cocoa_GL_UnloadLibrary;
slouken@1931
   113
    device->GL_CreateContext = Cocoa_GL_CreateContext;
slouken@1931
   114
    device->GL_MakeCurrent = Cocoa_GL_MakeCurrent;
urkle@7746
   115
    device->GL_GetDrawableSize = Cocoa_GL_GetDrawableSize;
slouken@1931
   116
    device->GL_SetSwapInterval = Cocoa_GL_SetSwapInterval;
slouken@1931
   117
    device->GL_GetSwapInterval = Cocoa_GL_GetSwapInterval;
slouken@1931
   118
    device->GL_SwapWindow = Cocoa_GL_SwapWindow;
slouken@1931
   119
    device->GL_DeleteContext = Cocoa_GL_DeleteContext;
slouken@1931
   120
#endif
slouken@1931
   121
slouken@3280
   122
    device->StartTextInput = Cocoa_StartTextInput;
slouken@3280
   123
    device->StopTextInput = Cocoa_StopTextInput;
slouken@3280
   124
    device->SetTextInputRect = Cocoa_SetTextInputRect;
slouken@3280
   125
slouken@4499
   126
    device->SetClipboardText = Cocoa_SetClipboardText;
slouken@4499
   127
    device->GetClipboardText = Cocoa_GetClipboardText;
slouken@4499
   128
    device->HasClipboardText = Cocoa_HasClipboardText;
slouken@4499
   129
slouken@1931
   130
    device->free = Cocoa_DeleteDevice;
slouken@1931
   131
slouken@1931
   132
    return device;
slouken@1931
   133
}
slouken@1931
   134
slouken@1931
   135
VideoBootStrap COCOA_bootstrap = {
slouken@1931
   136
    "cocoa", "SDL Cocoa video driver",
slouken@1931
   137
    Cocoa_Available, Cocoa_CreateDevice
slouken@1931
   138
};
slouken@1931
   139
slouken@1931
   140
slouken@1931
   141
int
slouken@1931
   142
Cocoa_VideoInit(_THIS)
slouken@1931
   143
{
icculus@8295
   144
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
icculus@8295
   145
slouken@1931
   146
    Cocoa_InitModes(_this);
slouken@1931
   147
    Cocoa_InitKeyboard(_this);
slouken@1931
   148
    Cocoa_InitMouse(_this);
icculus@8295
   149
icculus@8295
   150
    const char *hint = SDL_GetHint(SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES);
slouken@8986
   151
    data->allow_spaces = ( (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6) && (!hint || (*hint != '0')) );
icculus@8295
   152
icculus@9364
   153
    /* The IOPM assertion API can disable the screensaver as of 10.7. */
icculus@9364
   154
    data->screensaver_use_iopm = floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6;
icculus@9364
   155
slouken@1931
   156
    return 0;
slouken@1931
   157
}
slouken@1931
   158
slouken@1931
   159
void
slouken@1931
   160
Cocoa_VideoQuit(_THIS)
slouken@1931
   161
{
slouken@1931
   162
    Cocoa_QuitModes(_this);
slouken@1931
   163
    Cocoa_QuitKeyboard(_this);
slouken@1931
   164
    Cocoa_QuitMouse(_this);
slouken@1931
   165
}
slouken@1931
   166
slouken@5375
   167
/* This function assumes that it's called from within an autorelease pool */
slouken@5375
   168
NSImage *
slouken@5375
   169
Cocoa_CreateImage(SDL_Surface * surface)
slouken@5375
   170
{
slouken@5375
   171
    SDL_Surface *converted;
slouken@5375
   172
    NSBitmapImageRep *imgrep;
slouken@5375
   173
    Uint8 *pixels;
slouken@5375
   174
    int i;
slouken@5375
   175
    NSImage *img;
slouken@5375
   176
slouken@7191
   177
    converted = SDL_ConvertSurfaceFormat(surface,
slouken@5375
   178
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@5375
   179
                                         SDL_PIXELFORMAT_RGBA8888,
slouken@5375
   180
#else
slouken@5375
   181
                                         SDL_PIXELFORMAT_ABGR8888,
slouken@5375
   182
#endif
slouken@5375
   183
                                         0);
slouken@5375
   184
    if (!converted) {
slouken@5375
   185
        return nil;
slouken@5375
   186
    }
slouken@5375
   187
slouken@5375
   188
    imgrep = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
slouken@5375
   189
                    pixelsWide: converted->w
slouken@5375
   190
                    pixelsHigh: converted->h
slouken@5375
   191
                    bitsPerSample: 8
slouken@5375
   192
                    samplesPerPixel: 4
slouken@5375
   193
                    hasAlpha: YES
slouken@5375
   194
                    isPlanar: NO
slouken@5375
   195
                    colorSpaceName: NSDeviceRGBColorSpace
slouken@5375
   196
                    bytesPerRow: converted->pitch
slouken@5375
   197
                    bitsPerPixel: converted->format->BitsPerPixel] autorelease];
slouken@5375
   198
    if (imgrep == nil) {
slouken@5375
   199
        SDL_FreeSurface(converted);
slouken@5375
   200
        return nil;
slouken@5375
   201
    }
slouken@5375
   202
slouken@5375
   203
    /* Copy the pixels */
slouken@5375
   204
    pixels = [imgrep bitmapData];
slouken@5375
   205
    SDL_memcpy(pixels, converted->pixels, converted->h * converted->pitch);
slouken@5375
   206
    SDL_FreeSurface(converted);
slouken@5375
   207
slouken@5375
   208
    /* Premultiply the alpha channel */
slouken@6028
   209
    for (i = (surface->h * surface->w); i--; ) {
slouken@5375
   210
        Uint8 alpha = pixels[3];
slouken@5375
   211
        pixels[0] = (Uint8)(((Uint16)pixels[0] * alpha) / 255);
slouken@5375
   212
        pixels[1] = (Uint8)(((Uint16)pixels[1] * alpha) / 255);
slouken@5375
   213
        pixels[2] = (Uint8)(((Uint16)pixels[2] * alpha) / 255);
slouken@5375
   214
        pixels += 4;
slouken@5375
   215
    }
slouken@5375
   216
slouken@5375
   217
    img = [[[NSImage alloc] initWithSize: NSMakeSize(surface->w, surface->h)] autorelease];
slouken@5375
   218
    if (img != nil) {
slouken@5375
   219
        [img addRepresentation: imgrep];
slouken@5375
   220
    }
slouken@5375
   221
    return img;
slouken@5375
   222
}
slouken@3647
   223
slouken@3647
   224
/*
slouken@6616
   225
 * Mac OS X log support.
slouken@6616
   226
 *
slouken@6616
   227
 * This doesn't really have aything to do with the interfaces of the SDL video
slouken@6616
   228
 *  subsystem, but we need to stuff this into an Objective-C source code file.
slouken@6616
   229
 */
slouken@6616
   230
slouken@6616
   231
void SDL_NSLog(const char *text)
slouken@6616
   232
{
slouken@6714
   233
    NSLog(@"%s", text);
slouken@6616
   234
}
slouken@6616
   235
slouken@6044
   236
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
   237
slouken@1931
   238
/* vim: set ts=4 sw=4 expandtab: */