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 ***
slouken@47
     1
/*
slouken@47
     2
    SDL - Simple DirectMedia Layer
slouken@761
     3
    Copyright (C) 1997-2003  Sam Lantinga
slouken@47
     4
slouken@47
     5
    This library is free software; you can redistribute it and/or
slouken@47
     6
    modify it under the terms of the GNU Library General Public
slouken@47
     7
    License as published by the Free Software Foundation; either
slouken@47
     8
    version 2 of the License, or (at your option) any later version.
slouken@47
     9
slouken@47
    10
    This library is distributed in the hope that it will be useful,
slouken@47
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@47
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@47
    13
    Library General Public License for more details.
slouken@47
    14
slouken@47
    15
    You should have received a copy of the GNU Library General Public
slouken@47
    16
    License along with this library; if not, write to the Free
slouken@47
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@47
    18
slouken@47
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@47
    21
*/
slouken@47
    22
slouken@390
    23
/*    
slouken@47
    24
    @file   SDL_QuartzVideo.h
slouken@501
    25
    @author Darrell Walisser, Max Horn, et al.
slouken@47
    26
    
slouken@501
    27
    @abstract SDL video driver for Mac OS X.
slouken@47
    28
    
slouken@47
    29
    @discussion
slouken@47
    30
    
slouken@47
    31
    TODO
slouken@47
    32
        - Hardware Cursor support with NSCursor instead of Carbon
slouken@47
    33
        - Keyboard repeat/mouse speed adjust (if needed)
slouken@47
    34
        - Multiple monitor support (currently only main display)
slouken@47
    35
        - Accelerated blitting support
slouken@501
    36
        - Fix white OpenGL window on minimize (fixed) (update: broken again on 10.2)
slouken@501
    37
        - Find out what events should be sent/ignored if window is minimized
slouken@390
    38
        - Find a way to deal with external resolution/depth switch while app is running
slouken@501
    39
        - Resizeable windows (done)
slouken@272
    40
        - Check accuracy of QZ_SetGamma()
slouken@47
    41
    Problems:
slouken@47
    42
        - OGL not working in full screen with software renderer
slouken@47
    43
        - SetColors sets palette correctly but clears framebuffer
slouken@390
    44
        - Crash in CG after several mode switches (I think this has been fixed)
slouken@272
    45
        - Retained windows don't draw their title bar quite right (OS Bug) (not using retained windows)
slouken@501
    46
        - Cursor in 8 bit modes is screwy (might just be Radeon PCI bug) (update: not just Radeon)
slouken@272
    47
        - Warping cursor delays mouse events for a fraction of a second,
slouken@272
    48
          there is a hack around this that helps a bit
slouken@47
    49
*/
slouken@47
    50
slouken@272
    51
#include <Cocoa/Cocoa.h>
slouken@47
    52
#include <OpenGL/OpenGL.h>
slouken@656
    53
#include <OpenGL/gl.h>
slouken@656
    54
#include <OpenGL/glext.h>
slouken@47
    55
#include <Carbon/Carbon.h>
slouken@390
    56
#include <QuickTime/QuickTime.h>
slouken@761
    57
#include <IOKit/IOKitLib.h> /* For powersave handling */
slouken@588
    58
#include <pthread.h>
slouken@47
    59
slouken@588
    60
#include "SDL_thread.h"
slouken@47
    61
#include "SDL_video.h"
slouken@47
    62
#include "SDL_error.h"
slouken@272
    63
#include "SDL_timer.h"
slouken@47
    64
#include "SDL_syswm.h"
slouken@47
    65
#include "SDL_sysvideo.h"
slouken@47
    66
#include "SDL_pixels_c.h"
slouken@47
    67
#include "SDL_events_c.h"
slouken@47
    68
slouken@770
    69
/* 
slouken@770
    70
    This is a workaround to directly access NSOpenGLContext's CGL context
slouken@770
    71
    We need this to check for errors NSOpenGLContext doesn't support
slouken@770
    72
*/
slouken@770
    73
@interface NSOpenGLContext (CGLContextAccess)
slouken@770
    74
- (CGLContextObj) cglContext;
slouken@770
    75
@end
slouken@770
    76
slouken@770
    77
slouken@272
    78
/* Main driver structure to store required state information */
slouken@47
    79
typedef struct SDL_PrivateVideoData {
slouken@47
    80
slouken@272
    81
    CGDirectDisplayID  display;            /* 0 == main display (only support single display) */
slouken@272
    82
    CFDictionaryRef    mode;               /* current mode of the display */
slouken@272
    83
    CFDictionaryRef    save_mode;          /* original mode of the display */
slouken@272
    84
    CFArrayRef         mode_list;          /* list of available fullscreen modes */
slouken@272
    85
    CGDirectPaletteRef palette;            /* palette of an 8-bit display */
slouken@501
    86
    NSOpenGLContext    *gl_context;        /* OpenGL rendering context */
slouken@272
    87
    Uint32             width, height, bpp; /* frequently used data about the display */
slouken@501
    88
    Uint32             flags;              /* flags for current mode, for teardown purposes */
slouken@272
    89
    Uint32             video_set;          /* boolean; indicates if video was set correctly */
slouken@272
    90
    Uint32             warp_flag;          /* boolean; notify to event loop that a warp just occured */
slouken@272
    91
    Uint32             warp_ticks;         /* timestamp when the warp occured */
slouken@272
    92
    NSWindow           *window;            /* Cocoa window to implement the SDL window */
slouken@501
    93
    NSQuickDrawView    *view;              /* the window's view; draw 2D and OpenGL into this view */
slouken@501
    94
    SDL_Surface        *resize_icon;       /* icon for the resize badge, we have to draw it by hand */
slouken@501
    95
    SDL_GrabMode       current_grab_mode;  /* default value is SDL_GRAB_OFF */
slouken@501
    96
    SDL_Rect           **client_mode_list; /* resolution list to pass back to client */
slouken@501
    97
    SDLKey             keymap[256];        /* Mac OS X to SDL key mapping */
slouken@501
    98
    Uint32             current_mods;       /* current keyboard modifiers, to track modifier state */
slouken@501
    99
    Uint32             last_virtual_button;/* last virtual mouse button pressed */
icculus@563
   100
    io_connect_t       power_connection;   /* used with IOKit to detect wake from sleep */
icculus@563
   101
    Uint8              expect_mouse_up;    /* used to determine when to send mouse up events */
icculus@563
   102
    Uint8              grab_state;         /* used to manage grab behavior */
icculus@563
   103
    NSPoint            cursor_loc;         /* saved cursor coords, for activate/deactivate when grabbed */
slouken@761
   104
    BOOL               cursor_should_be_visible;     /* tells if cursor is supposed to be visible (SDL_ShowCursor) */
slouken@761
   105
    BOOL               cursor_visible;     /* tells if cursor is *actually* visible or not */
slouken@588
   106
    Uint8*             sw_buffers[2];      /* pointers to the two software buffers for double-buffer emulation */
slouken@588
   107
    SDL_Thread         *thread;            /* thread for async updates to the screen */
slouken@588
   108
    SDL_sem            *sem1, *sem2;       /* synchronization for async screen updates */
slouken@588
   109
    Uint8              *current_buffer;    /* the buffer being copied to the screen */
slouken@588
   110
    BOOL               quit_thread;        /* used to quit the async blitting thread */
icculus@563
   111
    
slouken@390
   112
    ImageDescriptionHandle yuv_idh;
slouken@390
   113
    MatrixRecordPtr        yuv_matrix;
slouken@390
   114
    DecompressorComponent  yuv_codec;
slouken@390
   115
    ImageSequence          yuv_seq;
slouken@390
   116
    PlanarPixmapInfoYUV420 *yuv_pixmap;
slouken@390
   117
    Sint16                  yuv_width, yuv_height;
slouken@390
   118
    CGrafPtr                yuv_port;
slouken@390
   119
slouken@761
   120
} SDL_PrivateVideoData;
slouken@47
   121
slouken@390
   122
#define _THIS    SDL_VideoDevice *this
slouken@47
   123
#define display_id (this->hidden->display)
slouken@47
   124
#define mode (this->hidden->mode)
slouken@47
   125
#define save_mode (this->hidden->save_mode)
slouken@47
   126
#define mode_list (this->hidden->mode_list)
slouken@47
   127
#define palette (this->hidden->palette)
slouken@47
   128
#define gl_context (this->hidden->gl_context)
slouken@47
   129
#define device_width (this->hidden->width)
slouken@47
   130
#define device_height (this->hidden->height)
slouken@47
   131
#define device_bpp (this->hidden->bpp)
slouken@47
   132
#define mode_flags (this->hidden->flags)
slouken@158
   133
#define qz_window (this->hidden->window)
slouken@272
   134
#define window_view (this->hidden->view)
slouken@272
   135
#define video_set (this->hidden->video_set)
slouken@272
   136
#define warp_ticks (this->hidden->warp_ticks)
slouken@272
   137
#define warp_flag (this->hidden->warp_flag)
slouken@501
   138
#define resize_icon (this->hidden->resize_icon)
slouken@501
   139
#define current_grab_mode (this->hidden->current_grab_mode)
slouken@501
   140
#define client_mode_list (this->hidden->client_mode_list)
slouken@501
   141
#define keymap (this->hidden->keymap)
slouken@501
   142
#define current_mods (this->hidden->current_mods)
slouken@501
   143
#define last_virtual_button (this->hidden->last_virtual_button)
icculus@563
   144
#define power_connection (this->hidden->power_connection)
icculus@563
   145
#define expect_mouse_up (this->hidden->expect_mouse_up)
icculus@563
   146
#define grab_state (this->hidden->grab_state)
icculus@563
   147
#define cursor_loc (this->hidden->cursor_loc)
slouken@761
   148
#define cursor_should_be_visible (this->hidden->cursor_should_be_visible)
icculus@563
   149
#define cursor_visible (this->hidden->cursor_visible)
slouken@588
   150
#define sw_buffers (this->hidden->sw_buffers)
slouken@588
   151
#define thread (this->hidden->thread)
slouken@588
   152
#define sem1 (this->hidden->sem1)
slouken@588
   153
#define sem2 (this->hidden->sem2)
slouken@588
   154
#define current_buffer (this->hidden->current_buffer)
slouken@588
   155
#define quit_thread (this->hidden->quit_thread)
slouken@501
   156
icculus@563
   157
/* grab states - the input is in one of these states */
icculus@563
   158
enum {
icculus@563
   159
    QZ_UNGRABBED = 0,
icculus@563
   160
    QZ_VISIBLE_GRAB,
icculus@563
   161
    QZ_INVISIBLE_GRAB
icculus@563
   162
};
icculus@563
   163
icculus@563
   164
/* grab actions - these can change the grabbed state */
icculus@563
   165
enum {
icculus@563
   166
    QZ_ENABLE_GRAB = 0,
icculus@563
   167
    QZ_DISABLE_GRAB,
icculus@563
   168
    QZ_HIDECURSOR,
icculus@563
   169
    QZ_SHOWCURSOR
icculus@563
   170
};
icculus@563
   171
slouken@47
   172
/* Gamma Functions */
slouken@761
   173
int    QZ_SetGamma          (_THIS, float red, float green, float blue);
slouken@761
   174
int    QZ_GetGamma          (_THIS, float *red, float *green, float *blue);
slouken@761
   175
int    QZ_SetGammaRamp      (_THIS, Uint16 *ramp);
slouken@761
   176
int    QZ_GetGammaRamp      (_THIS, Uint16 *ramp);
slouken@47
   177
slouken@47
   178
/* OpenGL functions */
slouken@761
   179
int    QZ_SetupOpenGL       (_THIS, int bpp, Uint32 flags);
slouken@761
   180
void   QZ_TearDownOpenGL    (_THIS);
slouken@761
   181
void*  QZ_GL_GetProcAddress (_THIS, const char *proc);
slouken@761
   182
int    QZ_GL_GetAttribute   (_THIS, SDL_GLattr attrib, int* value);
slouken@761
   183
int    QZ_GL_MakeCurrent    (_THIS);
slouken@761
   184
void   QZ_GL_SwapBuffers    (_THIS);
slouken@761
   185
int    QZ_GL_LoadLibrary    (_THIS, const char *location);
slouken@47
   186
slouken@47
   187
/* Cursor and Mouse functions */
slouken@761
   188
void         QZ_FreeWMCursor     (_THIS, WMcursor *cursor);
slouken@761
   189
WMcursor*    QZ_CreateWMCursor   (_THIS, Uint8 *data, Uint8 *mask,
slouken@761
   190
                                  int w, int h, int hot_x, int hot_y);
slouken@761
   191
int          QZ_ShowWMCursor     (_THIS, WMcursor *cursor);
slouken@761
   192
void         QZ_WarpWMCursor     (_THIS, Uint16 x, Uint16 y);
slouken@761
   193
void         QZ_MoveWMCursor     (_THIS, int x, int y);
slouken@761
   194
void         QZ_CheckMouseMode   (_THIS);
slouken@47
   195
slouken@47
   196
/* Event functions */
slouken@761
   197
void         QZ_InitOSKeymap     (_THIS);
slouken@761
   198
void         QZ_PumpEvents       (_THIS);
slouken@47
   199
slouken@47
   200
/* Window Manager functions */
slouken@761
   201
void         QZ_SetCaption       (_THIS, const char *title, const char *icon);
slouken@761
   202
void         QZ_SetIcon          (_THIS, SDL_Surface *icon, Uint8 *mask);
slouken@761
   203
int          QZ_IconifyWindow    (_THIS);
slouken@761
   204
SDL_GrabMode QZ_GrabInput        (_THIS, SDL_GrabMode grab_mode);
slouken@761
   205
/*int          QZ_GetWMInfo        (_THIS, SDL_SysWMinfo *info);*/
slouken@272
   206
slouken@390
   207
/* YUV functions */
slouken@761
   208
SDL_Overlay* QZ_CreateYUVOverlay (_THIS, int width, int height,
slouken@561
   209
                                         Uint32 format, SDL_Surface *display);
slouken@761
   210
slouken@761
   211
slouken@761
   212
/* Private functions (used internally) */
slouken@761
   213
void         QZ_PrivateWarpCursor (_THIS, int x, int y);
slouken@761
   214
void         QZ_ChangeGrabState (_THIS, int action);
slouken@761
   215
void         QZ_RegisterForSleepNotifications (_THIS);
slouken@761
   216
void         QZ_ShowMouse (_THIS);
slouken@761
   217
void         QZ_HideMouse (_THIS);
slouken@761
   218
void         QZ_PrivateGlobalToLocal (_THIS, NSPoint *p);
slouken@761
   219
void         QZ_PrivateCocoaToSDL (_THIS, NSPoint *p);
slouken@761
   220