]> git.sesse.net Git - vlc/blob - modules/access/screen/mac.c
Make Zorglub less unhappy
[vlc] / modules / access / screen / mac.c
1 /*****************************************************************************
2  * mac.c: Screen capture module for the Mac.
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id: x11.c 8290 2004-07-26 20:29:24Z gbazin $
6  *
7  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include <Carbon/Carbon.h>
33 #include <ApplicationServices/ApplicationServices.h>
34
35 typedef int CGSConnectionRef;
36 extern CGError CGSNewConnection(void* unknown, CGSConnectionRef* newConnection);
37 extern CGError CGSReleaseConnection(CGSConnectionRef connection);
38
39 #include "screen.h"
40
41 struct screen_data_t
42 {
43     RGBColor          oldForeColor, oldBackColor;
44     PenState          oldState;
45     CGDirectDisplayID displayID;
46     CGSConnectionRef  gConnection;
47     GDHandle          gMainDevice;
48     char              gDeviceState;
49     PixMapHandle      gDevicePix;
50     GWorldPtr         LocalBufferGW;
51     PixMapHandle      LocalBufferPix;
52 };
53
54 int screen_InitCapture( demux_t *p_demux )
55 {
56     demux_sys_t   *p_sys = p_demux->p_sys;
57     screen_data_t *p_data;
58     int            i_chroma, i_bbp, i_offset;
59
60     i_chroma = i_bbp = i_offset = 0;
61
62     p_sys->p_data = p_data =
63         (screen_data_t *)malloc( sizeof( screen_data_t ) );
64
65     p_data->gConnection = NULL;
66     p_data->gMainDevice = NULL;
67     p_data->gDevicePix = NULL;
68     p_data->gDeviceState = NULL;
69     p_data->LocalBufferGW = NULL;
70     p_data->LocalBufferPix = NULL;
71
72     p_data->displayID = CGMainDisplayID();
73     (void) GetMainDevice();
74
75     if( CGDisplaySamplesPerPixel(p_data->displayID) != 3 )
76     {
77         msg_Err( p_demux, "screenformat not supported" );
78     } 
79     
80     switch( CGDisplaySamplesPerPixel(p_data->displayID) * CGDisplayBitsPerSample(p_data->displayID) )
81     {
82     /* TODO figure out 256 colors (who uses it anyways) */
83     case 15: /* TODO this is not RV16, but BGR16 */
84         i_chroma = VLC_FOURCC('R','V','1','6');
85         i_bbp = 16;
86         i_offset = 8;
87         break;
88     case 24:
89     case 32:
90         i_chroma = VLC_FOURCC('R','V','3','2');
91         i_bbp = 32;
92         i_offset = 4;
93         break;
94     default:
95         msg_Err( p_demux, "unknown screen depth: %d", CGDisplaySamplesPerPixel(p_data->displayID) * CGDisplayBitsPerSample(p_data->displayID) );
96         return VLC_EGENERIC;
97     }
98
99     GetBackColor(&p_data->oldBackColor);
100     GetPenState(&p_data->oldState);
101     ForeColor(blackColor);
102     BackColor(whiteColor);
103     
104     p_data->gMainDevice = GetMainDevice();
105     p_data->gDeviceState = HGetState((Handle)p_data->gMainDevice);
106     HLock((Handle)p_data->gMainDevice);
107     p_data->gDevicePix = (**p_data->gMainDevice).gdPMap;
108
109     NewGWorld(&p_data->LocalBufferGW, (**p_data->gDevicePix).pixelSize, &(**p_data->gDevicePix).bounds, (**p_data->gDevicePix).pmTable, NULL, 0);
110     p_data->LocalBufferPix = GetGWorldPixMap(p_data->LocalBufferGW);
111     LockPixels(p_data->LocalBufferPix);
112     
113     es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
114     p_sys->fmt.video.i_width  = CGDisplayPixelsWide(p_data->displayID) + i_offset;
115     p_sys->fmt.video.i_visible_width  = CGDisplayPixelsWide(p_data->displayID);
116     p_sys->fmt.video.i_height = CGDisplayPixelsHigh(p_data->displayID);
117     p_sys->fmt.video.i_bits_per_pixel = i_bbp;
118
119     GetForeColor(&p_data->oldForeColor);
120
121     HSetState( (Handle)p_data->gMainDevice, p_data->gDeviceState );
122     SetPenState( &p_data->oldState);
123     RGBForeColor( &p_data->oldForeColor );
124     RGBBackColor( &p_data->oldBackColor );
125
126     return VLC_SUCCESS;
127 }
128
129 int screen_CloseCapture( demux_t *p_demux )
130 {
131     screen_data_t *p_data = (screen_data_t *)p_demux->p_sys->p_data;
132
133     if(p_data->LocalBufferPix) UnlockPixels(p_data->LocalBufferPix); p_data->LocalBufferPix = NULL;
134     if(p_data->LocalBufferGW) DisposeGWorld(p_data->LocalBufferGW); p_data->LocalBufferGW = NULL;
135
136     return VLC_SUCCESS;
137 }
138
139 block_t *screen_Capture( demux_t *p_demux )
140 {
141     demux_sys_t *p_sys = p_demux->p_sys;
142     screen_data_t *p_data = (screen_data_t *)p_sys->p_data;
143     block_t *p_block;
144     int i_size;
145  
146     i_size = p_sys->fmt.video.i_height * p_sys->fmt.video.i_width * 32 / 8; 
147
148     if( !( p_block = block_New( p_demux, i_size ) ) )
149     {
150         msg_Warn( p_demux, "cannot get block" );
151         return 0;
152     }
153
154     GetForeColor(&p_data->oldForeColor);
155     GetBackColor(&p_data->oldBackColor);
156     GetPenState(&p_data->oldState);
157     ForeColor(blackColor);
158     BackColor(whiteColor);
159
160     assert(CGSNewConnection(NULL, &p_data->gConnection) == kCGErrorSuccess);
161     p_data->gMainDevice = GetMainDevice();
162     p_data->gDeviceState = HGetState((Handle)p_data->gMainDevice);
163     HLock((Handle)p_data->gMainDevice);
164     p_data->gDevicePix = (**p_data->gMainDevice).gdPMap;
165
166     CopyBits(( BitMap*)*p_data->gDevicePix, (BitMap*)*p_data->LocalBufferPix,
167              &(**p_data->gDevicePix).bounds, &(**p_data->gDevicePix).bounds,
168              srcCopy, NULL );
169
170     HSetState( (Handle)p_data->gMainDevice, p_data->gDeviceState );
171     SetPenState( &p_data->oldState );
172     RGBForeColor( &p_data->oldForeColor );
173     RGBBackColor( &p_data->oldBackColor );
174
175     assert(CGSReleaseConnection(p_data->gConnection) == kCGErrorSuccess);
176     memcpy( p_block->p_buffer, (**p_data->LocalBufferPix).baseAddr, i_size );
177
178     return p_block;
179 }
180