]> git.sesse.net Git - vlc/blob - modules/access/screen/mac.c
* screen module for OSX. Does not yet work. Seems close, but can't find the problem...
[vlc] / modules / access / screen / mac.c
1 /*****************************************************************************
2  * mac.c: Screen capture module for the Mac.
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
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;
59
60     p_sys->p_data = p_data =
61         (screen_data_t *)malloc( sizeof( screen_data_t ) );
62
63     p_data->gConnection = NULL;
64     p_data->gMainDevice = NULL;
65     p_data->gDevicePix = NULL;
66     p_data->gDeviceState = NULL;
67     p_data->LocalBufferGW = NULL;
68     p_data->LocalBufferPix = NULL;
69
70     p_data->displayID = CGMainDisplayID();
71     (void) GetMainDevice();
72
73     if( CGDisplaySamplesPerPixel(p_data->displayID) != 3 )
74     {
75         msg_Err( p_demux, "screenformat not supported" );
76     } 
77     
78     switch( CGDisplaySamplesPerPixel(p_data->displayID) * CGDisplayBitsPerSample(p_data->displayID) )
79     {
80     case 8: /* FIXME: set the palette */
81         i_chroma = VLC_FOURCC('R','G','B','2'); break;
82     case 15:
83         i_chroma = VLC_FOURCC('R','V','1','5'); break;
84     case 16:
85         i_chroma = VLC_FOURCC('R','V','1','6'); break;
86     case 24:
87         i_chroma = VLC_FOURCC('R','V','2','4'); break;
88     case 32:
89         i_chroma = VLC_FOURCC('R','V','3','2'); break;
90     default:
91         msg_Err( p_demux, "unknown screen depth" );
92         return VLC_EGENERIC;
93     }
94
95     GetBackColor(&p_data->oldBackColor);
96     GetPenState(&p_data->oldState);
97 //    ForeColor(blackColor);
98 //    BackColor(whiteColor);
99     
100     p_data->gMainDevice = GetMainDevice();
101     p_data->gDeviceState = HGetState((Handle)p_data->gMainDevice);
102     HLock((Handle)p_data->gMainDevice);
103     p_data->gDevicePix = (**p_data->gMainDevice).gdPMap;
104
105     NewGWorld(&p_data->LocalBufferGW, (**p_data->gDevicePix).pixelSize, &(**p_data->gDevicePix).bounds, (**p_data->gDevicePix).pmTable, NULL, 0);
106     p_data->LocalBufferPix = GetGWorldPixMap(p_data->LocalBufferGW);
107     LockPixels(p_data->LocalBufferPix);
108     
109     es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
110     p_sys->fmt.video.i_width  = CGDisplayPixelsWide(p_data->displayID);
111     p_sys->fmt.video.i_height = CGDisplayPixelsHigh(p_data->displayID);
112     p_sys->fmt.video.i_bits_per_pixel = CGDisplaySamplesPerPixel(p_data->displayID) * CGDisplayBitsPerSample(p_data->displayID);
113
114     GetForeColor(&p_data->oldForeColor);
115     assert(CGSNewConnection(NULL, &p_data->gConnection) == kCGErrorSuccess);
116
117     HSetState( (Handle)p_data->gMainDevice, p_data->gDeviceState );
118     SetPenState( &p_data->oldState);
119     RGBForeColor( &p_data->oldForeColor );
120     RGBBackColor( &p_data->oldBackColor );
121
122     return VLC_SUCCESS;
123 }
124
125 int screen_CloseCapture( demux_t *p_demux )
126 {
127     screen_data_t *p_data = (screen_data_t *)p_demux->p_sys->p_data;
128
129     assert(CGSReleaseConnection(p_data->gConnection) == kCGErrorSuccess);
130     if(p_data->LocalBufferPix) UnlockPixels(p_data->LocalBufferPix); p_data->LocalBufferPix = NULL;
131     if(p_data->LocalBufferGW) DisposeGWorld(p_data->LocalBufferGW); p_data->LocalBufferGW = NULL;
132
133     return VLC_SUCCESS;
134 }
135
136 block_t *screen_Capture( demux_t *p_demux )
137 {
138     demux_sys_t *p_sys = p_demux->p_sys;
139     screen_data_t *p_data = (screen_data_t *)p_sys->p_data;
140     block_t *p_block;
141     int i_size;
142  
143     i_size = p_sys->fmt.video.i_height * CGDisplayBytesPerRow(p_data->displayID);
144
145     if( !( p_block = block_New( p_demux, i_size ) ) )
146     {
147         msg_Warn( p_demux, "cannot get block" );
148         return 0;
149     }
150
151     GetForeColor(&p_data->oldForeColor);
152     GetBackColor(&p_data->oldBackColor);
153     GetPenState(&p_data->oldState);
154 //    ForeColor(blackColor);
155 //    BackColor(whiteColor);
156
157     p_data->gMainDevice = GetMainDevice();
158     p_data->gDeviceState = HGetState((Handle)p_data->gMainDevice);
159     HLock((Handle)p_data->gMainDevice);
160     p_data->gDevicePix = (**p_data->gMainDevice).gdPMap;
161
162     CopyBits(( BitMap*)*p_data->gDevicePix, (BitMap*)*p_data->LocalBufferPix,
163              &(**p_data->gDevicePix).bounds, &(**p_data->gDevicePix).bounds,
164              srcCopy, NULL );
165
166     HSetState( (Handle)p_data->gMainDevice, p_data->gDeviceState );
167     SetPenState( &p_data->oldState );
168     RGBForeColor( &p_data->oldForeColor );
169     RGBBackColor( &p_data->oldBackColor );
170
171     memcpy( p_block->p_buffer, (**p_data->LocalBufferPix).baseAddr, i_size );
172
173     return p_block;
174 }
175