src/video/quartz/SDL_QuartzVideo.h
author Sam Lantinga
Sun, 04 Jan 2004 17:04:54 +0000
changeset 770 ac44ddb84f6f
parent 761 c5b2b6d2d1fe
child 777 6f4fe7f2063b
permissions -rw-r--r--
*** empty log message ***
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2003  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, Max Horn, et al.
    26     
    27     @abstract SDL video driver for Mac OS 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) (update: broken again on 10.2)
    37         - Find out what events should be sent/ignored if window is minimized
    38         - Find a way to deal with external resolution/depth switch while app is running
    39         - Resizeable windows (done)
    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) (update: not just Radeon)
    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 <OpenGL/gl.h>
    54 #include <OpenGL/glext.h>
    55 #include <Carbon/Carbon.h>
    56 #include <QuickTime/QuickTime.h>
    57 #include <IOKit/IOKitLib.h> /* For powersave handling */
    58 #include <pthread.h>
    59 
    60 #include "SDL_thread.h"
    61 #include "SDL_video.h"
    62 #include "SDL_error.h"
    63 #include "SDL_timer.h"
    64 #include "SDL_syswm.h"
    65 #include "SDL_sysvideo.h"
    66 #include "SDL_pixels_c.h"
    67 #include "SDL_events_c.h"
    68 
    69 /* 
    70     This is a workaround to directly access NSOpenGLContext's CGL context
    71     We need this to check for errors NSOpenGLContext doesn't support
    72 */
    73 @interface NSOpenGLContext (CGLContextAccess)
    74 - (CGLContextObj) cglContext;
    75 @end
    76 
    77 
    78 /* Main driver structure to store required state information */
    79 typedef struct SDL_PrivateVideoData {
    80 
    81     CGDirectDisplayID  display;            /* 0 == main display (only support single display) */
    82     CFDictionaryRef    mode;               /* current mode of the display */
    83     CFDictionaryRef    save_mode;          /* original mode of the display */
    84     CFArrayRef         mode_list;          /* list of available fullscreen modes */
    85     CGDirectPaletteRef palette;            /* palette of an 8-bit display */
    86     NSOpenGLContext    *gl_context;        /* OpenGL rendering context */
    87     Uint32             width, height, bpp; /* frequently used data about the display */
    88     Uint32             flags;              /* flags for current mode, for teardown purposes */
    89     Uint32             video_set;          /* boolean; indicates if video was set correctly */
    90     Uint32             warp_flag;          /* boolean; notify to event loop that a warp just occured */
    91     Uint32             warp_ticks;         /* timestamp when the warp occured */
    92     NSWindow           *window;            /* Cocoa window to implement the SDL window */
    93     NSQuickDrawView    *view;              /* the window's view; draw 2D and OpenGL into this view */
    94     SDL_Surface        *resize_icon;       /* icon for the resize badge, we have to draw it by hand */
    95     SDL_GrabMode       current_grab_mode;  /* default value is SDL_GRAB_OFF */
    96     SDL_Rect           **client_mode_list; /* resolution list to pass back to client */
    97     SDLKey             keymap[256];        /* Mac OS X to SDL key mapping */
    98     Uint32             current_mods;       /* current keyboard modifiers, to track modifier state */
    99     Uint32             last_virtual_button;/* last virtual mouse button pressed */
   100     io_connect_t       power_connection;   /* used with IOKit to detect wake from sleep */
   101     Uint8              expect_mouse_up;    /* used to determine when to send mouse up events */
   102     Uint8              grab_state;         /* used to manage grab behavior */
   103     NSPoint            cursor_loc;         /* saved cursor coords, for activate/deactivate when grabbed */
   104     BOOL               cursor_should_be_visible;     /* tells if cursor is supposed to be visible (SDL_ShowCursor) */
   105     BOOL               cursor_visible;     /* tells if cursor is *actually* visible or not */
   106     Uint8*             sw_buffers[2];      /* pointers to the two software buffers for double-buffer emulation */
   107     SDL_Thread         *thread;            /* thread for async updates to the screen */
   108     SDL_sem            *sem1, *sem2;       /* synchronization for async screen updates */
   109     Uint8              *current_buffer;    /* the buffer being copied to the screen */
   110     BOOL               quit_thread;        /* used to quit the async blitting thread */
   111     
   112     ImageDescriptionHandle yuv_idh;
   113     MatrixRecordPtr        yuv_matrix;
   114     DecompressorComponent  yuv_codec;
   115     ImageSequence          yuv_seq;
   116     PlanarPixmapInfoYUV420 *yuv_pixmap;
   117     Sint16                  yuv_width, yuv_height;
   118     CGrafPtr                yuv_port;
   119 
   120 } SDL_PrivateVideoData;
   121 
   122 #define _THIS    SDL_VideoDevice *this
   123 #define display_id (this->hidden->display)
   124 #define mode (this->hidden->mode)
   125 #define save_mode (this->hidden->save_mode)
   126 #define mode_list (this->hidden->mode_list)
   127 #define palette (this->hidden->palette)
   128 #define gl_context (this->hidden->gl_context)
   129 #define device_width (this->hidden->width)
   130 #define device_height (this->hidden->height)
   131 #define device_bpp (this->hidden->bpp)
   132 #define mode_flags (this->hidden->flags)
   133 #define qz_window (this->hidden->window)
   134 #define window_view (this->hidden->view)
   135 #define video_set (this->hidden->video_set)
   136 #define warp_ticks (this->hidden->warp_ticks)
   137 #define warp_flag (this->hidden->warp_flag)
   138 #define resize_icon (this->hidden->resize_icon)
   139 #define current_grab_mode (this->hidden->current_grab_mode)
   140 #define client_mode_list (this->hidden->client_mode_list)
   141 #define keymap (this->hidden->keymap)
   142 #define current_mods (this->hidden->current_mods)
   143 #define last_virtual_button (this->hidden->last_virtual_button)
   144 #define power_connection (this->hidden->power_connection)
   145 #define expect_mouse_up (this->hidden->expect_mouse_up)
   146 #define grab_state (this->hidden->grab_state)
   147 #define cursor_loc (this->hidden->cursor_loc)
   148 #define cursor_should_be_visible (this->hidden->cursor_should_be_visible)
   149 #define cursor_visible (this->hidden->cursor_visible)
   150 #define sw_buffers (this->hidden->sw_buffers)
   151 #define thread (this->hidden->thread)
   152 #define sem1 (this->hidden->sem1)
   153 #define sem2 (this->hidden->sem2)
   154 #define current_buffer (this->hidden->current_buffer)
   155 #define quit_thread (this->hidden->quit_thread)
   156 
   157 /* grab states - the input is in one of these states */
   158 enum {
   159     QZ_UNGRABBED = 0,
   160     QZ_VISIBLE_GRAB,
   161     QZ_INVISIBLE_GRAB
   162 };
   163 
   164 /* grab actions - these can change the grabbed state */
   165 enum {
   166     QZ_ENABLE_GRAB = 0,
   167     QZ_DISABLE_GRAB,
   168     QZ_HIDECURSOR,
   169     QZ_SHOWCURSOR
   170 };
   171 
   172 /* Gamma Functions */
   173 int    QZ_SetGamma          (_THIS, float red, float green, float blue);
   174 int    QZ_GetGamma          (_THIS, float *red, float *green, float *blue);
   175 int    QZ_SetGammaRamp      (_THIS, Uint16 *ramp);
   176 int    QZ_GetGammaRamp      (_THIS, Uint16 *ramp);
   177 
   178 /* OpenGL functions */
   179 int    QZ_SetupOpenGL       (_THIS, int bpp, Uint32 flags);
   180 void   QZ_TearDownOpenGL    (_THIS);
   181 void*  QZ_GL_GetProcAddress (_THIS, const char *proc);
   182 int    QZ_GL_GetAttribute   (_THIS, SDL_GLattr attrib, int* value);
   183 int    QZ_GL_MakeCurrent    (_THIS);
   184 void   QZ_GL_SwapBuffers    (_THIS);
   185 int    QZ_GL_LoadLibrary    (_THIS, const char *location);
   186 
   187 /* Cursor and Mouse functions */
   188 void         QZ_FreeWMCursor     (_THIS, WMcursor *cursor);
   189 WMcursor*    QZ_CreateWMCursor   (_THIS, Uint8 *data, Uint8 *mask,
   190                                   int w, int h, int hot_x, int hot_y);
   191 int          QZ_ShowWMCursor     (_THIS, WMcursor *cursor);
   192 void         QZ_WarpWMCursor     (_THIS, Uint16 x, Uint16 y);
   193 void         QZ_MoveWMCursor     (_THIS, int x, int y);
   194 void         QZ_CheckMouseMode   (_THIS);
   195 
   196 /* Event functions */
   197 void         QZ_InitOSKeymap     (_THIS);
   198 void         QZ_PumpEvents       (_THIS);
   199 
   200 /* Window Manager functions */
   201 void         QZ_SetCaption       (_THIS, const char *title, const char *icon);
   202 void         QZ_SetIcon          (_THIS, SDL_Surface *icon, Uint8 *mask);
   203 int          QZ_IconifyWindow    (_THIS);
   204 SDL_GrabMode QZ_GrabInput        (_THIS, SDL_GrabMode grab_mode);
   205 /*int          QZ_GetWMInfo        (_THIS, SDL_SysWMinfo *info);*/
   206 
   207 /* YUV functions */
   208 SDL_Overlay* QZ_CreateYUVOverlay (_THIS, int width, int height,
   209                                          Uint32 format, SDL_Surface *display);
   210 
   211 
   212 /* Private functions (used internally) */
   213 void         QZ_PrivateWarpCursor (_THIS, int x, int y);
   214 void         QZ_ChangeGrabState (_THIS, int action);
   215 void         QZ_RegisterForSleepNotifications (_THIS);
   216 void         QZ_ShowMouse (_THIS);
   217 void         QZ_HideMouse (_THIS);
   218 void         QZ_PrivateGlobalToLocal (_THIS, NSPoint *p);
   219 void         QZ_PrivateCocoaToSDL (_THIS, NSPoint *p);
   220