]> git.sesse.net Git - vlc/blob - modules/access/screen/mac.c
Remove useless test i_chunk is uint32_t and cannot be lower then zero. This fixes...
[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$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30
31 #include <Carbon/Carbon.h>
32 #include <ApplicationServices/ApplicationServices.h>
33
34 typedef int CGSConnectionRef;
35 extern CGError CGSNewConnection(void* unknown, CGSConnectionRef* newConnection);
36 extern CGError CGSReleaseConnection(CGSConnectionRef connection);
37
38 #include "screen.h"
39
40 struct screen_data_t
41 {
42     RGBColor          oldForeColor, oldBackColor;
43     PenState          oldState;
44     CGDirectDisplayID displayID;
45     CGSConnectionRef  gConnection;
46     GDHandle          gMainDevice;
47     char              gDeviceState;
48     PixMapHandle      gDevicePix;
49     GWorldPtr         LocalBufferGW;
50     PixMapHandle      LocalBufferPix;
51 };
52
53 int screen_InitCapture( demux_t *p_demux )
54 {
55     demux_sys_t   *p_sys = p_demux->p_sys;
56     screen_data_t *p_data;
57     int            i_chroma, i_bbp, i_offset;
58
59     i_chroma = i_bbp = i_offset = 0;
60
61     p_sys->p_data = p_data =
62         (screen_data_t *)malloc( sizeof( screen_data_t ) );
63
64     p_data->gConnection = nil;
65     p_data->gMainDevice = NULL;
66     p_data->gDevicePix = NULL;
67     p_data->gDeviceState = nil;
68     p_data->LocalBufferGW = NULL;
69     p_data->LocalBufferPix = NULL;
70
71     p_data->displayID = CGMainDisplayID();
72     (void) GetMainDevice();
73
74     if( CGDisplaySamplesPerPixel(p_data->displayID) != 3 )
75     {
76         msg_Err( p_demux, "screenformat not supported" );
77     } 
78     
79     switch( CGDisplaySamplesPerPixel(p_data->displayID) * CGDisplayBitsPerSample(p_data->displayID) )
80     {
81     /* TODO figure out 256 colors (who uses it anyways) */
82     case 15: /* TODO this is not RV16, but BGR16 */
83         i_chroma = VLC_FOURCC('R','V','1','6');
84         i_bbp = 16;
85         i_offset = 8;
86         break;
87     case 24:
88     case 32:
89         i_chroma = VLC_FOURCC('R','V','3','2');
90         i_bbp = 32;
91         i_offset = 4;
92         break;
93     default:
94         msg_Err( p_demux, "unknown screen depth: %d", (int)(CGDisplaySamplesPerPixel(p_data->displayID) * CGDisplayBitsPerSample(p_data->displayID)) );
95         return VLC_EGENERIC;
96     }
97
98     GetBackColor(&p_data->oldBackColor);
99     GetPenState(&p_data->oldState);
100     ForeColor(blackColor);
101     BackColor(whiteColor);
102     
103     p_data->gMainDevice = GetMainDevice();
104     p_data->gDeviceState = HGetState((Handle)p_data->gMainDevice);
105     HLock((Handle)p_data->gMainDevice);
106     p_data->gDevicePix = (**p_data->gMainDevice).gdPMap;
107
108     NewGWorld(&p_data->LocalBufferGW, (**p_data->gDevicePix).pixelSize, &(**p_data->gDevicePix).bounds, (**p_data->gDevicePix).pmTable, NULL, 0);
109     p_data->LocalBufferPix = GetGWorldPixMap(p_data->LocalBufferGW);
110     LockPixels(p_data->LocalBufferPix);
111     
112     es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
113     p_sys->fmt.video.i_width  = CGDisplayPixelsWide(p_data->displayID) + i_offset;
114     p_sys->fmt.video.i_visible_width  = CGDisplayPixelsWide(p_data->displayID);
115     p_sys->fmt.video.i_height = CGDisplayPixelsHigh(p_data->displayID);
116     p_sys->fmt.video.i_bits_per_pixel = i_bbp;
117
118     GetForeColor(&p_data->oldForeColor);
119
120     HSetState( (Handle)p_data->gMainDevice, p_data->gDeviceState );
121     SetPenState( &p_data->oldState);
122     RGBForeColor( &p_data->oldForeColor );
123     RGBBackColor( &p_data->oldBackColor );
124
125     return VLC_SUCCESS;
126 }
127
128 int screen_CloseCapture( demux_t *p_demux )
129 {
130     screen_data_t *p_data = (screen_data_t *)p_demux->p_sys->p_data;
131
132     if(p_data->LocalBufferPix) UnlockPixels(p_data->LocalBufferPix); p_data->LocalBufferPix = NULL;
133     if(p_data->LocalBufferGW) DisposeGWorld(p_data->LocalBufferGW); p_data->LocalBufferGW = NULL;
134
135     return VLC_SUCCESS;
136 }
137
138 block_t *screen_Capture( demux_t *p_demux )
139 {
140     demux_sys_t *p_sys = p_demux->p_sys;
141     screen_data_t *p_data = (screen_data_t *)p_sys->p_data;
142     block_t *p_block;
143     int i_size;
144  
145     i_size = p_sys->fmt.video.i_height * p_sys->fmt.video.i_width * 32 / 8; 
146
147     if( !( p_block = block_New( p_demux, i_size ) ) )
148     {
149         msg_Warn( p_demux, "cannot get block" );
150         return 0;
151     }
152
153     GetForeColor(&p_data->oldForeColor);
154     GetBackColor(&p_data->oldBackColor);
155     GetPenState(&p_data->oldState);
156     ForeColor(blackColor);
157     BackColor(whiteColor);
158
159     assert(CGSNewConnection(NULL, &p_data->gConnection) == kCGErrorSuccess);
160     p_data->gMainDevice = GetMainDevice();
161     p_data->gDeviceState = HGetState((Handle)p_data->gMainDevice);
162     HLock((Handle)p_data->gMainDevice);
163     p_data->gDevicePix = (**p_data->gMainDevice).gdPMap;
164
165     CopyBits(( BitMap*)*p_data->gDevicePix, (BitMap*)*p_data->LocalBufferPix,
166              &(**p_data->gDevicePix).bounds, &(**p_data->gDevicePix).bounds,
167              srcCopy, NULL );
168
169     HSetState( (Handle)p_data->gMainDevice, p_data->gDeviceState );
170     SetPenState( &p_data->oldState );
171     RGBForeColor( &p_data->oldForeColor );
172     RGBBackColor( &p_data->oldBackColor );
173
174     assert(CGSReleaseConnection(p_data->gConnection) == kCGErrorSuccess);
175     memcpy( p_block->p_buffer, (**p_data->LocalBufferPix).baseAddr, i_size );
176
177     return p_block;
178 }
179