src/video/quartz/SDL_QuartzVideo.h
author Sam Lantinga
Mon, 16 Sep 2002 18:38:09 +0000
changeset 498 4b8ff8ac2c07
parent 435 140798e1e7a6
child 501 74262d2647ca
permissions -rw-r--r--
Fixed window update problems on MacOS X 10.2 (thanks Darrell!)
     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         - Fix white OpenGL window on minimize (fixed)
    37         - Find out what events should be sent/ignored if window is mimimized
    38         - Find a way to deal with external resolution/depth switch while app is running
    39         - Resizeable windows
    40         - Check accuracy of QZ_SetGamma()
    41     Problems:
    42         - OGL not working in full screen with software renderer
    43         - SetColors sets palette correctly but clears framebuffer
    44         - Crash in CG after several mode switches (I think this has been fixed)
    45         - Retained windows don't draw their title bar quite right (OS Bug) (not using retained windows)
    46         - Cursor in 8 bit modes is screwy (might just be Radeon PCI bug)
    47         - Warping cursor delays mouse events for a fraction of a second,
    48           there is a hack around this that helps a bit
    49 */
    50 
    51 #include <Cocoa/Cocoa.h>
    52 #include <OpenGL/OpenGL.h>
    53 #include <Carbon/Carbon.h>
    54 #include <QuickTime/QuickTime.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 /* 
    65    Add methods to get at private members of NSScreen. 
    66    Since there is a bug in Apple's screen switching code
    67    that does not update this variable when switching
    68    to fullscreen, we'll set it manually (but only for the
    69    main screen).
    70 */
    71 @interface NSScreen (NSScreenAccess)
    72 - (void) setFrame:(NSRect)frame;
    73 @end
    74 
    75 @implementation NSScreen (NSScreenAccess)
    76 - (void) setFrame:(NSRect)frame;
    77 {
    78     _frame = frame;
    79 }
    80 @end
    81 
    82 /* This is a workaround to directly access NSOpenGLContext's CGL context */
    83 /* We need to do this in order to check for errors */
    84 @interface NSOpenGLContext (CGLContextAccess)
    85 - (CGLContextObj) cglContext;
    86 @end
    87 
    88 @implementation NSOpenGLContext (CGLContextAccess)
    89 - (CGLContextObj) cglContext;
    90 {
    91     return _contextAuxiliary;
    92 }
    93 @end
    94 
    95 /* Structure for rez switch gamma fades */
    96 /* We can hide the monitor flicker by setting the gamma tables to 0 */
    97 #define QZ_GAMMA_TABLE_SIZE 256
    98 
    99 typedef struct {
   100 
   101     CGGammaValue red[QZ_GAMMA_TABLE_SIZE];
   102     CGGammaValue green[QZ_GAMMA_TABLE_SIZE];
   103     CGGammaValue blue[QZ_GAMMA_TABLE_SIZE];
   104 
   105 } SDL_QuartzGammaTable;
   106 
   107 /* Main driver structure to store required state information */
   108 typedef struct SDL_PrivateVideoData {
   109 
   110     CGDirectDisplayID  display;            /* 0 == main display (only support single display) */
   111     CFDictionaryRef    mode;               /* current mode of the display */
   112     CFDictionaryRef    save_mode;          /* original mode of the display */
   113     CFArrayRef         mode_list;          /* list of available fullscreen modes */
   114     CGDirectPaletteRef palette;            /* palette of an 8-bit display */
   115     NSOpenGLContext    *gl_context;        /* object that represents an OpenGL rendering context */
   116     Uint32             width, height, bpp; /* frequently used data about the display */
   117     Uint32             flags;              /* flags for mode, for teardown purposes */
   118     Uint32             video_set;          /* boolean; indicates if video was set correctly */
   119     Uint32             warp_flag;          /* boolean; notify to event loop that a warp just occured */
   120     Uint32             warp_ticks;         /* timestamp when the warp occured */
   121     NSWindow           *window;            /* Cocoa window to implement the SDL window */
   122     NSQuickDrawView    *view;              /* the window's view; draw 2D into this view */
   123     ImageDescriptionHandle yuv_idh;
   124     MatrixRecordPtr        yuv_matrix;
   125     DecompressorComponent  yuv_codec;
   126     ImageSequence          yuv_seq;
   127     PlanarPixmapInfoYUV420 *yuv_pixmap;
   128     Sint16                  yuv_width, yuv_height;
   129     CGrafPtr                yuv_port;
   130 
   131 } SDL_PrivateVideoData ;
   132 
   133 #define _THIS    SDL_VideoDevice *this
   134 #define display_id (this->hidden->display)
   135 #define mode (this->hidden->mode)
   136 #define save_mode (this->hidden->save_mode)
   137 #define mode_list (this->hidden->mode_list)
   138 #define palette (this->hidden->palette)
   139 #define gl_context (this->hidden->gl_context)
   140 #define device_width (this->hidden->width)
   141 #define device_height (this->hidden->height)
   142 #define device_bpp (this->hidden->bpp)
   143 #define mode_flags (this->hidden->flags)
   144 #define qz_window (this->hidden->window)
   145 #define window_view (this->hidden->view)
   146 #define video_set (this->hidden->video_set)
   147 #define warp_ticks (this->hidden->warp_ticks)
   148 #define warp_flag (this->hidden->warp_flag)
   149 #define yuv_idh (this->hidden->yuv_idh)
   150 #define yuv_matrix (this->hidden->yuv_matrix)
   151 #define yuv_codec (this->hidden->yuv_codec)
   152 #define yuv_seq (this->hidden->yuv_seq)
   153 #define yuv_pixmap (this->hidden->yuv_pixmap)
   154 #define yuv_data (this->hidden->yuv_data)
   155 #define yuv_width (this->hidden->yuv_width)
   156 #define yuv_height (this->hidden->yuv_height)
   157 #define yuv_port (this->hidden->yuv_port)
   158 
   159 /* Obscuring code: maximum number of windows above ours (inclusive) */
   160 #define kMaxWindows 256
   161 
   162 /* Some of the Core Graphics Server API for obscuring code */
   163 #define kCGSWindowLevelTop          2147483632
   164 #define kCGSWindowLevelDockIconDrag 500
   165 #define kCGSWindowLevelDockMenu     101
   166 #define kCGSWindowLevelMenuIgnore    21
   167 #define kCGSWindowLevelMenu          20
   168 #define kCGSWindowLevelDockLabel     12
   169 #define kCGSWindowLevelDockIcon      11
   170 #define kCGSWindowLevelDock          10
   171 #define kCGSWindowLevelUtility        3
   172 #define kCGSWindowLevelNormal         0
   173 
   174 /* For completeness; We never use these window levels, they are always below us
   175 #define kCGSWindowLevelMBarShadow -20
   176 #define kCGSWindowLevelDesktopPicture -2147483647
   177 #define kCGSWindowLevelDesktop        -2147483648
   178 */
   179 
   180 typedef CGError       CGSError;
   181 typedef long          CGSWindowCount;
   182 typedef void *        CGSConnectionID;
   183 typedef int           CGSWindowID;
   184 typedef CGSWindowID*  CGSWindowIDList;
   185 typedef CGWindowLevel CGSWindowLevel;
   186 typedef NSRect        CGSRect;
   187 
   188 extern CGSConnectionID _CGSDefaultConnection ();
   189 
   190 extern CGSError CGSGetOnScreenWindowList (CGSConnectionID cid,
   191                                           CGSConnectionID owner,
   192                                           CGSWindowCount listCapacity,
   193                                           CGSWindowIDList list,
   194                                           CGSWindowCount *listCount);
   195 
   196 extern CGSError CGSGetScreenRectForWindow (CGSConnectionID cid,
   197                                            CGSWindowID wid,
   198                                            CGSRect *rect);
   199 
   200 extern CGWindowLevel CGSGetWindowLevel (CGSConnectionID cid,
   201                                         CGSWindowID wid,
   202                                         CGSWindowLevel *level);
   203 
   204 extern CGSError CGSDisplayHWFill (CGDirectDisplayID id, unsigned int x, unsigned int y,
   205                                   unsigned int w, unsigned int h, unsigned int color);
   206 
   207 extern CGSError CGSDisplayCanHWFill (CGDirectDisplayID id);
   208 
   209 extern CGSError CGSGetMouseEnabledFlags (CGSConnectionID cid, CGSWindowID wid, int *flags);
   210 
   211 /* Bootstrap functions */
   212 static int              QZ_Available ();
   213 static SDL_VideoDevice* QZ_CreateDevice (int device_index);
   214 static void             QZ_DeleteDevice (SDL_VideoDevice *device);
   215 
   216 /* Initialization, Query, Setup, and Redrawing functions */
   217 static int          QZ_VideoInit        (_THIS, SDL_PixelFormat *video_format);
   218 
   219 static SDL_Rect**   QZ_ListModes        (_THIS, SDL_PixelFormat *format,
   220                                          Uint32 flags);
   221 static void         QZ_UnsetVideoMode   (_THIS);
   222 
   223 static SDL_Surface* QZ_SetVideoMode     (_THIS, SDL_Surface *current,
   224                                          int width, int height, int bpp,
   225                                          Uint32 flags);
   226 static int          QZ_ToggleFullScreen (_THIS, int on);
   227 static int          QZ_SetColors        (_THIS, int first_color,
   228                                          int num_colors, SDL_Color *colors);
   229 static void         QZ_DirectUpdate     (_THIS, int num_rects, SDL_Rect *rects);
   230 static int 	     QZ_LockWindow       (_THIS, SDL_Surface *surface);
   231 static void         QZ_UnlockWindow     (_THIS, SDL_Surface *surface);
   232 static void         QZ_UpdateRects      (_THIS, int num_rects, SDL_Rect *rects);
   233 static void         QZ_VideoQuit        (_THIS);
   234 
   235 /* Hardware surface functions (for fullscreen mode only) */
   236 static int  QZ_FillHWRect (_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color);
   237 static int  QZ_LockHWSurface(_THIS, SDL_Surface *surface);
   238 static void QZ_UnlockHWSurface(_THIS, SDL_Surface *surface);
   239 static void QZ_FreeHWSurface (_THIS, SDL_Surface *surface);
   240 /* static int  QZ_FlipHWSurface (_THIS, SDL_Surface *surface); */
   241 
   242 /* Gamma Functions */
   243 static int QZ_SetGamma     (_THIS, float red, float green, float blue);
   244 static int QZ_GetGamma     (_THIS, float *red, float *green, float *blue);
   245 static int QZ_SetGammaRamp (_THIS, Uint16 *ramp);
   246 static int QZ_GetGammaRamp (_THIS, Uint16 *ramp);
   247 
   248 /* OpenGL functions */
   249 static int    QZ_SetupOpenGL (_THIS, int bpp, Uint32 flags);
   250 static void   QZ_TearDownOpenGL (_THIS);
   251 static void*  QZ_GL_GetProcAddress (_THIS, const char *proc);
   252 static int    QZ_GL_GetAttribute   (_THIS, SDL_GLattr attrib, int* value);
   253 static int    QZ_GL_MakeCurrent    (_THIS);
   254 static void   QZ_GL_SwapBuffers    (_THIS);
   255 static int    QZ_GL_LoadLibrary    (_THIS, const char *location);
   256 
   257 /* Private function to warp the cursor (used internally) */
   258 static void  QZ_PrivateWarpCursor (_THIS, int x, int y);
   259 
   260 /* Cursor and Mouse functions */
   261 static void         QZ_FreeWMCursor     (_THIS, WMcursor *cursor);
   262 static WMcursor*    QZ_CreateWMCursor   (_THIS, Uint8 *data, Uint8 *mask,
   263                                          int w, int h, int hot_x, int hot_y);
   264 static int          QZ_ShowWMCursor     (_THIS, WMcursor *cursor);
   265 static void         QZ_WarpWMCursor     (_THIS, Uint16 x, Uint16 y);
   266 static void         QZ_MoveWMCursor     (_THIS, int x, int y);
   267 static void         QZ_CheckMouseMode   (_THIS);
   268 
   269 /* Event functions */
   270 static void         QZ_InitOSKeymap     (_THIS);
   271 static void         QZ_PumpEvents       (_THIS);
   272 
   273 /* Window Manager functions */
   274 static void QZ_SetCaption    (_THIS, const char *title, const char *icon);
   275 static void QZ_SetIcon       (_THIS, SDL_Surface *icon, Uint8 *mask);
   276 static int  QZ_IconifyWindow (_THIS);
   277 static SDL_GrabMode QZ_GrabInput (_THIS, SDL_GrabMode grab_mode);
   278 /*static int  QZ_GetWMInfo     (_THIS, SDL_SysWMinfo *info);*/
   279 
   280 /* YUV functions */
   281 static SDL_Overlay* QZ_CreateYUVOverlay (_THIS, int width, int height,
   282                                          Uint32 format, SDL_Surface *display);
   283