src/video/quartz/SDL_QuartzVideo.h
author Sam Lantinga
Wed, 06 Mar 2002 11:23:08 +0000
changeset 297 f6ffac90895c
parent 272 d1447a846d80
child 390 19e73568a75c
permissions -rw-r--r--
Updated copyright information for 2002
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 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     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /*	
    24     @file   SDL_QuartzVideo.h
    25     @author Darrell Walisser
    26     
    27     @abstract SDL video driver for MacOS X.
    28     
    29     @discussion
    30     
    31     TODO
    32         - Hardware Cursor support with NSCursor instead of Carbon
    33         - Keyboard repeat/mouse speed adjust (if needed)
    34         - Multiple monitor support (currently only main display)
    35         - Accelerated blitting support
    36         - Set the window icon (dock icon when API is available)
    37         - Fix white OpenGL window on minimize
    38         - Find out what events should be sent/ignored if window is mimimized
    39         - Find a better way to deal with resolution/depth switch while app is running
    40         - Resizeable windows
    41         - Check accuracy of QZ_SetGamma()
    42     Problems:
    43         - OGL not working in full screen with software renderer
    44         - SetColors sets palette correctly but clears framebuffer
    45         - Crash in CG after several mode switches
    46         - Retained windows don't draw their title bar quite right (OS Bug) (not using retained windows)
    47         - Cursor in 8 bit modes is screwy (might just be Radeon PCI bug)
    48         - Warping cursor delays mouse events for a fraction of a second,
    49           there is a hack around this that helps a bit
    50 */
    51 
    52 #include <Cocoa/Cocoa.h>
    53 #include <OpenGL/OpenGL.h>
    54 #include <Carbon/Carbon.h>
    55 
    56 #include "SDL_video.h"
    57 #include "SDL_error.h"
    58 #include "SDL_timer.h"
    59 #include "SDL_syswm.h"
    60 #include "SDL_sysvideo.h"
    61 #include "SDL_pixels_c.h"
    62 #include "SDL_events_c.h"
    63 
    64 /* This is a workaround to directly access NSOpenGLContext's CGL context */
    65 /* We need to do this in order to check for errors */
    66 @interface NSOpenGLContext (CGLContextAccess)
    67 - (CGLContextObj) cglContext;
    68 @end
    69 
    70 @implementation NSOpenGLContext (CGLContextAccess)
    71 - (CGLContextObj) cglContext;
    72 {
    73     return _contextAuxiliary;
    74 }
    75 @end
    76 
    77 /* Structure for rez switch gamma fades */
    78 /* We can hide the monitor flicker by setting the gamma tables to 0 */
    79 #define QZ_GAMMA_TABLE_SIZE 256
    80 
    81 typedef struct {
    82 
    83     CGGammaValue red[QZ_GAMMA_TABLE_SIZE];
    84     CGGammaValue green[QZ_GAMMA_TABLE_SIZE];
    85     CGGammaValue blue[QZ_GAMMA_TABLE_SIZE];
    86 
    87 } SDL_QuartzGammaTable;
    88 
    89 /* Main driver structure to store required state information */
    90 typedef struct SDL_PrivateVideoData {
    91 
    92     CGDirectDisplayID  display;            /* 0 == main display (only support single display) */
    93     CFDictionaryRef    mode;               /* current mode of the display */
    94     CFDictionaryRef    save_mode;          /* original mode of the display */
    95     CFArrayRef         mode_list;          /* list of available fullscreen modes */
    96     CGDirectPaletteRef palette;            /* palette of an 8-bit display */
    97     NSOpenGLContext    *gl_context;        /* object that represents an OpenGL rendering context */
    98     Uint32             width, height, bpp; /* frequently used data about the display */
    99     Uint32             flags;              /* flags for mode, for teardown purposes */
   100     Uint32             video_set;          /* boolean; indicates if video was set correctly */
   101     Uint32             warp_flag;          /* boolean; notify to event loop that a warp just occured */
   102     Uint32             warp_ticks;         /* timestamp when the warp occured */
   103     NSWindow           *window;            /* Cocoa window to implement the SDL window */
   104     NSQuickDrawView    *view;              /* the window's view; draw 2D into this view */
   105     
   106 } SDL_PrivateVideoData ;
   107 
   108 #define _THIS	SDL_VideoDevice *this
   109 #define display_id (this->hidden->display)
   110 #define mode (this->hidden->mode)
   111 #define save_mode (this->hidden->save_mode)
   112 #define mode_list (this->hidden->mode_list)
   113 #define palette (this->hidden->palette)
   114 #define gl_context (this->hidden->gl_context)
   115 #define device_width (this->hidden->width)
   116 #define device_height (this->hidden->height)
   117 #define device_bpp (this->hidden->bpp)
   118 #define mode_flags (this->hidden->flags)
   119 #define qz_window (this->hidden->window)
   120 #define window_view (this->hidden->view)
   121 #define video_set (this->hidden->video_set)
   122 #define warp_ticks (this->hidden->warp_ticks)
   123 #define warp_flag (this->hidden->warp_flag)
   124 
   125 /* Obscuring code: maximum number of windows above ours (inclusive) */
   126 #define kMaxWindows 256
   127 
   128 /* Some of the Core Graphics Server API for obscuring code */
   129 #define kCGSWindowLevelTop          2147483632
   130 #define kCGSWindowLevelDockIconDrag 500
   131 #define kCGSWindowLevelDockMenu     101
   132 #define kCGSWindowLevelMenuIgnore    21
   133 #define kCGSWindowLevelMenu          20
   134 #define kCGSWindowLevelDockLabel     12
   135 #define kCGSWindowLevelDockIcon      11
   136 #define kCGSWindowLevelDock          10
   137 #define kCGSWindowLevelUtility        3
   138 #define kCGSWindowLevelNormal         0
   139 
   140 /* For completeness; We never use these window levels, they are always below us
   141 #define kCGSWindowLevelMBarShadow -20
   142 #define kCGSWindowLevelDesktopPicture -2147483647
   143 #define kCGSWindowLevelDesktop        -2147483648
   144 */
   145 
   146 typedef CGError       CGSError;
   147 typedef long	      CGSWindowCount;
   148 typedef void *	      CGSConnectionID;
   149 typedef int	      CGSWindowID;
   150 typedef CGSWindowID*  CGSWindowIDList;
   151 typedef CGWindowLevel CGSWindowLevel;
   152 typedef NSRect        CGSRect;
   153 
   154 extern CGSConnectionID _CGSDefaultConnection ();
   155 
   156 extern CGSError CGSGetOnScreenWindowList (CGSConnectionID cid, 
   157                                           CGSConnectionID owner,
   158                                           CGSWindowCount listCapacity,
   159                                           CGSWindowIDList list,
   160                                           CGSWindowCount *listCount);
   161 
   162 extern CGSError CGSGetScreenRectForWindow (CGSConnectionID cid,
   163                                            CGSWindowID wid,
   164                                            CGSRect *rect);
   165 
   166 extern CGWindowLevel CGSGetWindowLevel (CGSConnectionID cid,
   167                                         CGSWindowID wid,
   168                                         CGSWindowLevel *level);
   169                                         
   170 extern CGSError CGSDisplayHWFill (CGDirectDisplayID id, unsigned int x, unsigned int y, 
   171                       unsigned int w, unsigned int h, unsigned int color);
   172 
   173 extern CGSError CGSDisplayCanHWFill (CGDirectDisplayID id);
   174 
   175 extern CGSError CGSGetMouseEnabledFlags (CGSConnectionID cid, CGSWindowID wid, int *flags);
   176 
   177 /* Bootstrap functions */
   178 static int              QZ_Available ();
   179 static SDL_VideoDevice* QZ_CreateDevice (int device_index);
   180 static void             QZ_DeleteDevice (SDL_VideoDevice *device);
   181 
   182 /* Initialization, Query, Setup, and Redrawing functions */
   183 static int          QZ_VideoInit        (_THIS, SDL_PixelFormat *video_format);
   184 
   185 static SDL_Rect**   QZ_ListModes        (_THIS, SDL_PixelFormat *format, 
   186 					 Uint32 flags);
   187 static void         QZ_UnsetVideoMode   (_THIS);
   188 
   189 static SDL_Surface* QZ_SetVideoMode     (_THIS, SDL_Surface *current, 
   190 					 int width, int height, int bpp, 
   191 					 Uint32 flags);
   192 static int          QZ_ToggleFullScreen (_THIS, int on);
   193 static int          QZ_SetColors        (_THIS, int first_color, 
   194 					 int num_colors, SDL_Color *colors);
   195 static void         QZ_DirectUpdate     (_THIS, int num_rects, SDL_Rect *rects);
   196 static void         QZ_UpdateRects      (_THIS, int num_rects, SDL_Rect *rects);
   197 static void         QZ_VideoQuit        (_THIS);
   198 
   199 /* Hardware surface functions (for fullscreen mode only) */
   200 static int  QZ_FillHWRect (_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color);
   201 static int  QZ_LockHWSurface(_THIS, SDL_Surface *surface);
   202 static void QZ_UnlockHWSurface(_THIS, SDL_Surface *surface);
   203 static void QZ_FreeHWSurface (_THIS, SDL_Surface *surface);
   204 /* static int  QZ_FlipHWSurface (_THIS, SDL_Surface *surface); */
   205 
   206 /* Gamma Functions */
   207 static int QZ_SetGamma     (_THIS, float red, float green, float blue);
   208 static int QZ_GetGamma     (_THIS, float *red, float *green, float *blue);
   209 static int QZ_SetGammaRamp (_THIS, Uint16 *ramp);
   210 static int QZ_GetGammaRamp (_THIS, Uint16 *ramp);
   211 
   212 /* OpenGL functions */
   213 static int    QZ_SetupOpenGL (_THIS, int bpp, Uint32 flags);
   214 static void   QZ_TearDownOpenGL (_THIS);
   215 static void*  QZ_GL_GetProcAddress (_THIS, const char *proc);
   216 static int    QZ_GL_GetAttribute   (_THIS, SDL_GLattr attrib, int* value);
   217 static int    QZ_GL_MakeCurrent    (_THIS);
   218 static void   QZ_GL_SwapBuffers    (_THIS);
   219 static int    QZ_GL_LoadLibrary    (_THIS, const char *location);
   220 
   221 /* Private function to warp the cursor (used internally) */
   222 static void  QZ_PrivateWarpCursor (_THIS, int x, int y);
   223 
   224 /* Cursor and Mouse functions */
   225 static void         QZ_FreeWMCursor     (_THIS, WMcursor *cursor);
   226 static WMcursor*    QZ_CreateWMCursor   (_THIS, Uint8 *data, Uint8 *mask, 
   227                                           int w, int h, int hot_x, int hot_y);
   228 static int          QZ_ShowWMCursor     (_THIS, WMcursor *cursor);
   229 static void         QZ_WarpWMCursor     (_THIS, Uint16 x, Uint16 y);
   230 static void         QZ_MoveWMCursor     (_THIS, int x, int y);
   231 static void         QZ_CheckMouseMode   (_THIS);
   232 
   233 /* Event functions */
   234 static void         QZ_InitOSKeymap     (_THIS);
   235 static void         QZ_PumpEvents       (_THIS);
   236 
   237 /* Window Manager functions */
   238 static void QZ_SetCaption    (_THIS, const char *title, const char *icon);
   239 static void QZ_SetIcon       (_THIS, SDL_Surface *icon, Uint8 *mask);
   240 static int  QZ_IconifyWindow (_THIS);
   241 static SDL_GrabMode QZ_GrabInput (_THIS, SDL_GrabMode grab_mode);
   242 /*static int  QZ_GetWMInfo     (_THIS, SDL_SysWMinfo *info);*/
   243