src/video/quartz/SDL_QuartzVideo.h
author Sam Lantinga
Sat, 01 Jun 2002 23:05:05 +0000
changeset 390 19e73568a75c
parent 297 f6ffac90895c
child 435 140798e1e7a6
permissions -rw-r--r--
Date: Sat, 1 Jun 2002 17:56:45 -0500
From: Darrell Walisser <dwaliss1@purdue.edu>
Subject: mac patch

In this patch:

- yuv code
- links to QuickTime
- tabs -> 4 spaces
- mouse events fix
- SDLMain path parsing fix
- BUGS updates
- some miscellaneous docs/comments/code cleanup
     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 /* 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     ImageDescriptionHandle yuv_idh;
   106     MatrixRecordPtr        yuv_matrix;
   107     DecompressorComponent  yuv_codec;
   108     ImageSequence          yuv_seq;
   109     PlanarPixmapInfoYUV420 *yuv_pixmap;
   110     Sint16                  yuv_width, yuv_height;
   111     CGrafPtr                yuv_port;
   112 
   113 } SDL_PrivateVideoData ;
   114 
   115 #define _THIS    SDL_VideoDevice *this
   116 #define display_id (this->hidden->display)
   117 #define mode (this->hidden->mode)
   118 #define save_mode (this->hidden->save_mode)
   119 #define mode_list (this->hidden->mode_list)
   120 #define palette (this->hidden->palette)
   121 #define gl_context (this->hidden->gl_context)
   122 #define device_width (this->hidden->width)
   123 #define device_height (this->hidden->height)
   124 #define device_bpp (this->hidden->bpp)
   125 #define mode_flags (this->hidden->flags)
   126 #define qz_window (this->hidden->window)
   127 #define window_view (this->hidden->view)
   128 #define video_set (this->hidden->video_set)
   129 #define warp_ticks (this->hidden->warp_ticks)
   130 #define warp_flag (this->hidden->warp_flag)
   131 #define yuv_idh (this->hidden->yuv_idh)
   132 #define yuv_matrix (this->hidden->yuv_matrix)
   133 #define yuv_codec (this->hidden->yuv_codec)
   134 #define yuv_seq (this->hidden->yuv_seq)
   135 #define yuv_pixmap (this->hidden->yuv_pixmap)
   136 #define yuv_data (this->hidden->yuv_data)
   137 #define yuv_width (this->hidden->yuv_width)
   138 #define yuv_height (this->hidden->yuv_height)
   139 #define yuv_port (this->hidden->yuv_port)
   140 
   141 /* Obscuring code: maximum number of windows above ours (inclusive) */
   142 #define kMaxWindows 256
   143 
   144 /* Some of the Core Graphics Server API for obscuring code */
   145 #define kCGSWindowLevelTop          2147483632
   146 #define kCGSWindowLevelDockIconDrag 500
   147 #define kCGSWindowLevelDockMenu     101
   148 #define kCGSWindowLevelMenuIgnore    21
   149 #define kCGSWindowLevelMenu          20
   150 #define kCGSWindowLevelDockLabel     12
   151 #define kCGSWindowLevelDockIcon      11
   152 #define kCGSWindowLevelDock          10
   153 #define kCGSWindowLevelUtility        3
   154 #define kCGSWindowLevelNormal         0
   155 
   156 /* For completeness; We never use these window levels, they are always below us
   157 #define kCGSWindowLevelMBarShadow -20
   158 #define kCGSWindowLevelDesktopPicture -2147483647
   159 #define kCGSWindowLevelDesktop        -2147483648
   160 */
   161 
   162 typedef CGError       CGSError;
   163 typedef long          CGSWindowCount;
   164 typedef void *        CGSConnectionID;
   165 typedef int           CGSWindowID;
   166 typedef CGSWindowID*  CGSWindowIDList;
   167 typedef CGWindowLevel CGSWindowLevel;
   168 typedef NSRect        CGSRect;
   169 
   170 extern CGSConnectionID _CGSDefaultConnection ();
   171 
   172 extern CGSError CGSGetOnScreenWindowList (CGSConnectionID cid,
   173                                           CGSConnectionID owner,
   174                                           CGSWindowCount listCapacity,
   175                                           CGSWindowIDList list,
   176                                           CGSWindowCount *listCount);
   177 
   178 extern CGSError CGSGetScreenRectForWindow (CGSConnectionID cid,
   179                                            CGSWindowID wid,
   180                                            CGSRect *rect);
   181 
   182 extern CGWindowLevel CGSGetWindowLevel (CGSConnectionID cid,
   183                                         CGSWindowID wid,
   184                                         CGSWindowLevel *level);
   185 
   186 extern CGSError CGSDisplayHWFill (CGDirectDisplayID id, unsigned int x, unsigned int y,
   187                                   unsigned int w, unsigned int h, unsigned int color);
   188 
   189 extern CGSError CGSDisplayCanHWFill (CGDirectDisplayID id);
   190 
   191 extern CGSError CGSGetMouseEnabledFlags (CGSConnectionID cid, CGSWindowID wid, int *flags);
   192 
   193 /* Bootstrap functions */
   194 static int              QZ_Available ();
   195 static SDL_VideoDevice* QZ_CreateDevice (int device_index);
   196 static void             QZ_DeleteDevice (SDL_VideoDevice *device);
   197 
   198 /* Initialization, Query, Setup, and Redrawing functions */
   199 static int          QZ_VideoInit        (_THIS, SDL_PixelFormat *video_format);
   200 
   201 static SDL_Rect**   QZ_ListModes        (_THIS, SDL_PixelFormat *format,
   202                                          Uint32 flags);
   203 static void         QZ_UnsetVideoMode   (_THIS);
   204 
   205 static SDL_Surface* QZ_SetVideoMode     (_THIS, SDL_Surface *current,
   206                                          int width, int height, int bpp,
   207                                          Uint32 flags);
   208 static int          QZ_ToggleFullScreen (_THIS, int on);
   209 static int          QZ_SetColors        (_THIS, int first_color,
   210                                          int num_colors, SDL_Color *colors);
   211 static void         QZ_DirectUpdate     (_THIS, int num_rects, SDL_Rect *rects);
   212 static void         QZ_UpdateRects      (_THIS, int num_rects, SDL_Rect *rects);
   213 static void         QZ_VideoQuit        (_THIS);
   214 
   215 /* Hardware surface functions (for fullscreen mode only) */
   216 static int  QZ_FillHWRect (_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color);
   217 static int  QZ_LockHWSurface(_THIS, SDL_Surface *surface);
   218 static void QZ_UnlockHWSurface(_THIS, SDL_Surface *surface);
   219 static void QZ_FreeHWSurface (_THIS, SDL_Surface *surface);
   220 /* static int  QZ_FlipHWSurface (_THIS, SDL_Surface *surface); */
   221 
   222 /* Gamma Functions */
   223 static int QZ_SetGamma     (_THIS, float red, float green, float blue);
   224 static int QZ_GetGamma     (_THIS, float *red, float *green, float *blue);
   225 static int QZ_SetGammaRamp (_THIS, Uint16 *ramp);
   226 static int QZ_GetGammaRamp (_THIS, Uint16 *ramp);
   227 
   228 /* OpenGL functions */
   229 static int    QZ_SetupOpenGL (_THIS, int bpp, Uint32 flags);
   230 static void   QZ_TearDownOpenGL (_THIS);
   231 static void*  QZ_GL_GetProcAddress (_THIS, const char *proc);
   232 static int    QZ_GL_GetAttribute   (_THIS, SDL_GLattr attrib, int* value);
   233 static int    QZ_GL_MakeCurrent    (_THIS);
   234 static void   QZ_GL_SwapBuffers    (_THIS);
   235 static int    QZ_GL_LoadLibrary    (_THIS, const char *location);
   236 
   237 /* Private function to warp the cursor (used internally) */
   238 static void  QZ_PrivateWarpCursor (_THIS, int x, int y);
   239 
   240 /* Cursor and Mouse functions */
   241 static void         QZ_FreeWMCursor     (_THIS, WMcursor *cursor);
   242 static WMcursor*    QZ_CreateWMCursor   (_THIS, Uint8 *data, Uint8 *mask,
   243                                          int w, int h, int hot_x, int hot_y);
   244 static int          QZ_ShowWMCursor     (_THIS, WMcursor *cursor);
   245 static void         QZ_WarpWMCursor     (_THIS, Uint16 x, Uint16 y);
   246 static void         QZ_MoveWMCursor     (_THIS, int x, int y);
   247 static void         QZ_CheckMouseMode   (_THIS);
   248 
   249 /* Event functions */
   250 static void         QZ_InitOSKeymap     (_THIS);
   251 static void         QZ_PumpEvents       (_THIS);
   252 
   253 /* Window Manager functions */
   254 static void QZ_SetCaption    (_THIS, const char *title, const char *icon);
   255 static void QZ_SetIcon       (_THIS, SDL_Surface *icon, Uint8 *mask);
   256 static int  QZ_IconifyWindow (_THIS);
   257 static SDL_GrabMode QZ_GrabInput (_THIS, SDL_GrabMode grab_mode);
   258 /*static int  QZ_GetWMInfo     (_THIS, SDL_SysWMinfo *info);*/
   259 
   260 /* YUV functions */
   261 static SDL_Overlay* QZ_CreateYUVOverlay (_THIS, int width, int height,
   262                                          Uint32 format, SDL_Surface *display);
   263