mac.c 10.3 KB
Newer Older
1 2 3
/*****************************************************************************
 * mac.c: Screen capture module for the Mac.
 *****************************************************************************
Felix Paul Kühne's avatar
Felix Paul Kühne committed
4
 * Copyright (C) 2004, 2008 the VideoLAN team
Antoine Cellerier's avatar
Antoine Cellerier committed
5
 * $Id$
6 7
 *
 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
Felix Paul Kühne's avatar
Felix Paul Kühne committed
8
 *          arai <arai_a@mac.com>
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
Felix Paul Kühne's avatar
Felix Paul Kühne committed
28
#import <stdlib.h>
29

Felix Paul Kühne's avatar
Felix Paul Kühne committed
30 31 32
#ifdef HAVE_CONFIG_H
# import "config.h"
#endif
33

34
#import <vlc_common.h>
Felix Paul Kühne's avatar
Felix Paul Kühne committed
35 36

#import <ApplicationServices/ApplicationServices.h>
37
#import <OpenGL/OpenGL.h>
38
#import <OpenGL/gl.h>
39 40

typedef int CGSConnectionRef;
41 42 43 44 45 46 47
extern CGError CGSNewConnection( void *, CGSConnectionRef * );
extern CGError CGSReleaseConnection( CGSConnectionRef );
extern CGError CGSGetGlobalCursorDataSize( CGSConnectionRef, int * );
extern CGError CGSGetGlobalCursorData( CGSConnectionRef, unsigned char *,
                                       int *, int *, CGRect *, CGPoint *,
                                       int *, int *, int * );
extern CGError CGSGetCurrentCursorLocation( CGSConnectionRef, CGPoint * );
48

Felix Paul Kühne's avatar
Felix Paul Kühne committed
49
#import "screen.h"
50 51 52

struct screen_data_t
{
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  CGLContextObj screen;
  
  CGLContextObj scaled;
  char *scaled_image;
  
  GLuint texture;
  char *texture_image;
  
  GLuint cursor_texture;
  
  int left;
  int top;
  int src_width;
  int src_height;
  
  int dest_width;
  int dest_height;
  
  int screen_width;
  int screen_height;
  
  CGSConnectionRef connection;
75 76 77 78 79 80
};

int screen_InitCapture( demux_t *p_demux )
{
    demux_sys_t   *p_sys = p_demux->p_sys;
    screen_data_t *p_data;
81 82 83 84 85
    CGLPixelFormatAttribute attribs[4];
    CGLPixelFormatObj pix;
    GLint npix;
    GLint viewport[4];
    
86
    p_sys->p_data = p_data =
87 88 89 90 91 92 93 94 95 96
        ( screen_data_t * )malloc( sizeof( screen_data_t ) );
    
    attribs[0] = kCGLPFAFullScreen;
    attribs[1] = kCGLPFADisplayMask;
    attribs[2] = CGDisplayIDToOpenGLDisplayMask( CGMainDisplayID() );
    attribs[3] = 0;
    
    CGLChoosePixelFormat( attribs, &pix, &npix );
    CGLCreateContext( pix, NULL, &( p_data->screen ) );
    CGLDestroyPixelFormat( pix );
97

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    CGLSetCurrentContext( p_data->screen );
    CGLSetFullScreen( p_data->screen );
    
    glGetIntegerv( GL_VIEWPORT, viewport );
    
    p_data->screen_width = viewport[2];
    p_data->screen_height = viewport[3];
    
    p_data->left = 0;
    p_data->top = 0;
    p_data->src_width = p_data->screen_width;
    p_data->src_height = p_data->screen_height;
    p_data->dest_width = p_data->src_width;
    p_data->dest_height = p_data->src_height;
    
    attribs [0] = kCGLPFAOffScreen;
    attribs [1] = kCGLPFAColorSize;
    attribs [2] = 32;
    attribs [3] = 0;
    
    CGLChoosePixelFormat( attribs, &pix, &npix );
    CGLCreateContext( pix, NULL, &( p_data->scaled ) );
    CGLDestroyPixelFormat( pix );
121

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    CGLSetCurrentContext( p_data->scaled );
    p_data->scaled_image = ( char * )malloc( p_data->dest_width
                                          * p_data->dest_height * 4 );
    CGLSetOffScreen( p_data->scaled, p_data->dest_width, p_data->dest_height,
                     p_data->dest_width * 4, p_data->scaled_image );
    
    es_format_Init( &p_sys->fmt, VIDEO_ES, VLC_FOURCC( 'R','V','3','2' ) );
    
    p_sys->fmt.video.i_width = p_data->dest_width;
    p_sys->fmt.video.i_visible_width = p_data->dest_width;
    p_sys->fmt.video.i_height = p_data->dest_height;
    p_sys->fmt.video.i_bits_per_pixel = 32;
    
    glGenTextures( 1, &( p_data->texture ) );
    glBindTexture( GL_TEXTURE_2D, p_data->texture );
    
    p_data->texture_image
      = ( char * )malloc( p_data->src_width * p_data->src_height * 4 );
    
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
145

146 147 148 149 150 151 152 153 154 155
    glGenTextures( 1, &( p_data->cursor_texture ) );
    glBindTexture( GL_TEXTURE_2D, p_data->cursor_texture );
    
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
    
    CGSNewConnection( NULL, &( p_data->connection ) );
    
156 157 158 159 160
    return VLC_SUCCESS;
}

int screen_CloseCapture( demux_t *p_demux )
{
161 162 163 164 165 166 167 168
    screen_data_t *p_data = ( screen_data_t * )p_demux->p_sys->p_data;
    
    CGSReleaseConnection( p_data->connection );
    
    CGLSetCurrentContext( NULL );
    CGLClearDrawable( p_data->screen );
    CGLDestroyContext( p_data->screen );
    
169 170 171 172 173 174
    return VLC_SUCCESS;
}

block_t *screen_Capture( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
175
    screen_data_t *p_data = ( screen_data_t * )p_sys->p_data;
176 177
    block_t *p_block;
    int i_size;
178 179 180
    
    i_size = p_sys->fmt.video.i_height * p_sys->fmt.video.i_width * 4; 
    
181 182 183 184 185
    if( !( p_block = block_New( p_demux, i_size ) ) )
    {
        msg_Warn( p_demux, "cannot get block" );
        return 0;
    }
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    
    CGLSetCurrentContext( p_data->screen );
    glReadPixels( p_data->left,
                  p_data->screen_height - p_data->top - p_data->src_height,
                  p_data->src_width,
                  p_data->src_height,
                  GL_RGBA, GL_UNSIGNED_BYTE,
                  p_data->texture_image );
    
    CGLSetCurrentContext( p_data->scaled );
    glEnable( GL_TEXTURE_2D );
    glBindTexture( GL_TEXTURE_2D, p_data->texture );
    glTexImage2D( GL_TEXTURE_2D, 0,
                  GL_RGBA8, p_data->src_width, p_data->src_height, 0,
                  GL_RGBA, GL_UNSIGNED_BYTE, p_data->texture_image );
    
    glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
    glClear( GL_COLOR_BUFFER_BIT );
    glColor3f( 1.0f, 1.0f, 1.0f );
    glEnable( GL_TEXTURE_2D );
    glBindTexture( GL_TEXTURE_2D, p_data->texture );
    glBegin( GL_POLYGON );
    glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, -1.0 );
    glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, -1.0 );
    glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, 1.0 );
    glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, 1.0 );
    glEnd();
    glDisable( GL_TEXTURE_2D );
    
    CGPoint cursor_pos;
    int size;
    int tmp1, tmp2, tmp3, tmp4;
    unsigned char *cursor_image;
    CGRect cursor_rect;
    CGPoint cursor_hot;
    
    cursor_pos.x = 0;
    cursor_pos.y = 0;
    
    if( CGSGetCurrentCursorLocation( p_data->connection, &cursor_pos )
        == kCGErrorSuccess
        && CGSGetGlobalCursorDataSize( p_data->connection, &size )
        == kCGErrorSuccess )
    {
        cursor_image = ( unsigned char* )malloc( size );
        if( CGSGetGlobalCursorData( p_data->connection,
                                    cursor_image, &size,
                                    &tmp1,
                                    &cursor_rect, &cursor_hot,
                                    &tmp2, &tmp3, &tmp4 )
            == kCGErrorSuccess )
        {
            glEnable( GL_TEXTURE_2D );
            glBindTexture( GL_TEXTURE_2D, p_data->cursor_texture );
            glTexImage2D( GL_TEXTURE_2D, 0,
                          GL_RGBA8,
                          ( int )( cursor_rect.size.width ),
                          ( int )( cursor_rect.size.height ), 0,
                          GL_RGBA, GL_UNSIGNED_BYTE,
                          ( char * )cursor_image );
            
            cursor_rect.origin.x = cursor_pos.x - p_data->left - cursor_hot.x;
            cursor_rect.origin.y = cursor_pos.y - p_data->top - cursor_hot.y;
            
            cursor_rect.origin.x
              = 2.0 * cursor_rect.origin.x / p_data->src_width - 1.0;
            cursor_rect.origin.y
              = 2.0 * cursor_rect.origin.y / p_data->src_height - 1.0;
            cursor_rect.size.width
              = 2.0 * cursor_rect.size.width / p_data->src_width;
            cursor_rect.size.height
              = 2.0 * cursor_rect.size.height / p_data->src_height;
            
            glColor3f( 1.0f, 1.0f, 1.0f );
            glEnable( GL_TEXTURE_2D );
            glEnable( GL_BLEND );
            glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
            glBindTexture( GL_TEXTURE_2D, p_data->cursor_texture );
            glBegin( GL_POLYGON );
            glTexCoord2f( 0.0, 0.0 ); glVertex2f( cursor_rect.origin.x,
                                                  cursor_rect.origin.y );
            glTexCoord2f( 1.0, 0.0 ); glVertex2f( cursor_rect.origin.x
                                                  + cursor_rect.size.width,
                                                  cursor_rect.origin.y );
            glTexCoord2f( 1.0, 1.0 ); glVertex2f( cursor_rect.origin.x
                                                  + cursor_rect.size.width,
                                                  cursor_rect.origin.y
                                                  + cursor_rect.size.height );
            glTexCoord2f( 0.0, 1.0 ); glVertex2f( cursor_rect.origin.x,
                                                  cursor_rect.origin.y
                                                  + cursor_rect.size.height );
            glEnd();
            glDisable( GL_BLEND );
            glDisable( GL_TEXTURE_2D );
        }
        free( cursor_image );
    }
    
    glReadPixels( 0, 0, 
                  p_data->dest_width,
                  p_data->dest_height,
                  GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
                  p_block->p_buffer );
    
290 291
    return p_block;
}