src/video/cocoa/SDL_cocoavideo.m
author Sam Lantinga
Tue, 08 Feb 2011 16:50:51 -0800
changeset 5229 c015d3e63631
parent 5088 455bc74f7034
child 5249 7a963be087ef
permissions -rw-r--r--
Fixed setting the texture unit, still doesn't work.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include "SDL_cocoavideo.h"
    25 #include "SDL_cocoashape.h"
    26 #include "SDL_assert.h"
    27 
    28 /* Initialization/Query functions */
    29 static int Cocoa_VideoInit(_THIS);
    30 static void Cocoa_VideoQuit(_THIS);
    31 
    32 /* Cocoa driver bootstrap functions */
    33 
    34 static int
    35 Cocoa_Available(void)
    36 {
    37     return (1);
    38 }
    39 
    40 static void
    41 Cocoa_DeleteDevice(SDL_VideoDevice * device)
    42 {
    43     SDL_free(device->driverdata);
    44     SDL_free(device);
    45 }
    46 
    47 static SDL_VideoDevice *
    48 Cocoa_CreateDevice(int devindex)
    49 {
    50     SDL_VideoDevice *device;
    51     SDL_VideoData *data;
    52 
    53     Cocoa_RegisterApp();
    54 
    55     /* Initialize all variables that we clean on shutdown */
    56     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    57     if (device) {
    58         data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
    59     } else {
    60         data = NULL;
    61     }
    62     if (!data) {
    63         SDL_OutOfMemory();
    64         if (device) {
    65             SDL_free(device);
    66         }
    67         return NULL;
    68     }
    69     device->driverdata = data;
    70 
    71     /* Find out what version of Mac OS X we're running */
    72     Gestalt(gestaltSystemVersion, &data->osversion);
    73 
    74     /* Set the function pointers */
    75     device->VideoInit = Cocoa_VideoInit;
    76     device->VideoQuit = Cocoa_VideoQuit;
    77     device->GetDisplayBounds = Cocoa_GetDisplayBounds;
    78     device->GetDisplayModes = Cocoa_GetDisplayModes;
    79     device->SetDisplayMode = Cocoa_SetDisplayMode;
    80     device->PumpEvents = Cocoa_PumpEvents;
    81 
    82     device->CreateWindow = Cocoa_CreateWindow;
    83     device->CreateWindowFrom = Cocoa_CreateWindowFrom;
    84     device->SetWindowTitle = Cocoa_SetWindowTitle;
    85     device->SetWindowPosition = Cocoa_SetWindowPosition;
    86     device->SetWindowSize = Cocoa_SetWindowSize;
    87     device->ShowWindow = Cocoa_ShowWindow;
    88     device->HideWindow = Cocoa_HideWindow;
    89     device->RaiseWindow = Cocoa_RaiseWindow;
    90     device->MaximizeWindow = Cocoa_MaximizeWindow;
    91     device->MinimizeWindow = Cocoa_MinimizeWindow;
    92     device->RestoreWindow = Cocoa_RestoreWindow;
    93     device->SetWindowGrab = Cocoa_SetWindowGrab;
    94     device->DestroyWindow = Cocoa_DestroyWindow;
    95     device->GetWindowWMInfo = Cocoa_GetWindowWMInfo;
    96     
    97     device->shape_driver.CreateShaper = Cocoa_CreateShaper;
    98     device->shape_driver.SetWindowShape = Cocoa_SetWindowShape;
    99     device->shape_driver.ResizeWindowShape = Cocoa_ResizeWindowShape;
   100     
   101 #if SDL_VIDEO_OPENGL_CGL
   102     device->GL_LoadLibrary = Cocoa_GL_LoadLibrary;
   103     device->GL_GetProcAddress = Cocoa_GL_GetProcAddress;
   104     device->GL_UnloadLibrary = Cocoa_GL_UnloadLibrary;
   105     device->GL_CreateContext = Cocoa_GL_CreateContext;
   106     device->GL_MakeCurrent = Cocoa_GL_MakeCurrent;
   107     device->GL_SetSwapInterval = Cocoa_GL_SetSwapInterval;
   108     device->GL_GetSwapInterval = Cocoa_GL_GetSwapInterval;
   109     device->GL_SwapWindow = Cocoa_GL_SwapWindow;
   110     device->GL_DeleteContext = Cocoa_GL_DeleteContext;
   111 #endif
   112 
   113     device->StartTextInput = Cocoa_StartTextInput;
   114     device->StopTextInput = Cocoa_StopTextInput;
   115     device->SetTextInputRect = Cocoa_SetTextInputRect;
   116 
   117     device->SetClipboardText = Cocoa_SetClipboardText;
   118     device->GetClipboardText = Cocoa_GetClipboardText;
   119     device->HasClipboardText = Cocoa_HasClipboardText;
   120 
   121     device->free = Cocoa_DeleteDevice;
   122 
   123     return device;
   124 }
   125 
   126 VideoBootStrap COCOA_bootstrap = {
   127     "cocoa", "SDL Cocoa video driver",
   128     Cocoa_Available, Cocoa_CreateDevice
   129 };
   130 
   131 
   132 int
   133 Cocoa_VideoInit(_THIS)
   134 {
   135     Cocoa_InitModes(_this);
   136     Cocoa_InitKeyboard(_this);
   137     Cocoa_InitMouse(_this);
   138     return 0;
   139 }
   140 
   141 void
   142 Cocoa_VideoQuit(_THIS)
   143 {
   144     Cocoa_QuitModes(_this);
   145     Cocoa_QuitKeyboard(_this);
   146     Cocoa_QuitMouse(_this);
   147 }
   148 
   149 
   150 /*
   151  * Mac OS X assertion support.
   152  *
   153  * This doesn't really have aything to do with the interfaces of the SDL video
   154  *  subsystem, but we need to stuff this into an Objective-C source code file.
   155  */
   156 
   157 SDL_assert_state
   158 SDL_PromptAssertion_cocoa(const SDL_assert_data *data)
   159 {
   160     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   161 
   162     NSString *msg = [NSString stringWithFormat:
   163             @"Assertion failure at %s (%s:%d), triggered %u time%s:\n  '%s'",
   164                 data->function, data->filename, data->linenum,
   165                 data->trigger_count, (data->trigger_count == 1) ? "" : "s",
   166                 data->condition];
   167 
   168     NSLog(@"%s", msg);
   169 
   170     /*
   171      * !!! FIXME: this code needs to deal with fullscreen modes:
   172      * !!! FIXME:  reset to default desktop, runModal, reset to current?
   173      */
   174 
   175     NSAlert* alert = [[NSAlert alloc] init];
   176     [alert setAlertStyle:NSCriticalAlertStyle];
   177     [alert setMessageText:msg];
   178     [alert addButtonWithTitle:@"Retry"];
   179     [alert addButtonWithTitle:@"Break"];
   180     [alert addButtonWithTitle:@"Abort"];
   181     [alert addButtonWithTitle:@"Ignore"];
   182     [alert addButtonWithTitle:@"Always Ignore"];
   183     const NSInteger clicked = [alert runModal];
   184     [pool release];
   185     return (SDL_assert_state) (clicked - NSAlertFirstButtonReturn);
   186 }
   187 
   188 /* vim: set ts=4 sw=4 expandtab: */