]> git.sesse.net Git - vlc/blob - modules/access/screen/win32.c
Disable crashy FS controller by default
[vlc] / modules / access / screen / win32.c
1 /*****************************************************************************
2  * win32.c: Screen capture module.
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.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
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33
34 #include "screen.h"
35
36 #ifndef CAPTUREBLT
37 #   define CAPTUREBLT (DWORD)0x40000000 /* Include layered windows */
38 #endif
39
40 struct screen_data_t
41 {
42     HDC hdc_src;
43     HDC hdc_dst;
44     BITMAPINFO bmi;
45     HGDIOBJ hgdi_backup;
46
47     int i_fragment_size;
48     int i_fragment;
49     block_t *p_block;
50 };
51
52 int screen_InitCapture( demux_t *p_demux )
53 {
54     demux_sys_t *p_sys = p_demux->p_sys;
55     screen_data_t *p_data;
56     int i_chroma, i_bits_per_pixel;
57     vlc_value_t val;
58
59     p_sys->p_data = p_data = malloc( sizeof( screen_data_t ) );
60
61     /* Get the device context for the whole screen */
62     p_data->hdc_src = CreateDC( "DISPLAY", NULL, NULL, NULL );
63     if( !p_data->hdc_src )
64     {
65         msg_Err( p_demux, "cannot get device context" );
66         return VLC_EGENERIC;
67     }
68
69     p_data->hdc_dst = CreateCompatibleDC( p_data->hdc_src );
70     if( !p_data->hdc_dst )
71     {
72         msg_Err( p_demux, "cannot get compat device context" );
73         ReleaseDC( 0, p_data->hdc_src );
74         return VLC_EGENERIC;
75     }
76
77     i_bits_per_pixel = GetDeviceCaps( p_data->hdc_src, BITSPIXEL );
78     switch( i_bits_per_pixel )
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 %i",
92                  p_sys->fmt.video.i_bits_per_pixel );
93         ReleaseDC( 0, p_data->hdc_src );
94         ReleaseDC( 0, p_data->hdc_dst );
95         return VLC_EGENERIC;
96     }
97
98 #if 1 /* For now we force RV24 because of chroma inversion in the other cases*/
99     i_chroma = VLC_FOURCC('R','V','2','4');
100     i_bits_per_pixel = 24;
101 #endif
102
103     es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
104     p_sys->fmt.video.i_width  = GetDeviceCaps( p_data->hdc_src, HORZRES );
105     p_sys->fmt.video.i_height = GetDeviceCaps( p_data->hdc_src, VERTRES );
106     p_sys->fmt.video.i_bits_per_pixel = i_bits_per_pixel;
107
108     /* Create the bitmap info header */
109     p_data->bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
110     p_data->bmi.bmiHeader.biWidth = p_sys->fmt.video.i_width;
111     p_data->bmi.bmiHeader.biHeight = - p_sys->fmt.video.i_height;
112     p_data->bmi.bmiHeader.biPlanes = 1;
113     p_data->bmi.bmiHeader.biBitCount = p_sys->fmt.video.i_bits_per_pixel;
114     p_data->bmi.bmiHeader.biCompression = BI_RGB;
115     p_data->bmi.bmiHeader.biSizeImage = 0;
116     p_data->bmi.bmiHeader.biXPelsPerMeter =
117         p_data->bmi.bmiHeader.biYPelsPerMeter = 0;
118     p_data->bmi.bmiHeader.biClrUsed = 0;
119     p_data->bmi.bmiHeader.biClrImportant = 0;
120
121     if( i_chroma == VLC_FOURCC('R','V','2','4') )
122     {
123         /* This is in BGR format */
124         p_sys->fmt.video.i_bmask = 0x00ff0000;
125         p_sys->fmt.video.i_gmask = 0x0000ff00;
126         p_sys->fmt.video.i_rmask = 0x000000ff;
127     }
128
129     var_Create( p_demux, "screen-fragment-size",
130                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
131     var_Get( p_demux, "screen-fragment-size", &val );
132     p_data->i_fragment_size =
133         val.i_int > 0 ? val.i_int : p_sys->fmt.video.i_height;
134     p_data->i_fragment_size =
135         val.i_int > p_sys->fmt.video.i_height ? p_sys->fmt.video.i_height :
136         p_data->i_fragment_size;
137     p_sys->f_fps *= (p_sys->fmt.video.i_height/p_data->i_fragment_size);
138     p_sys->i_incr = 1000000 / p_sys->f_fps;
139     p_data->i_fragment = 0;
140     p_data->p_block = 0;
141
142     return VLC_SUCCESS;
143 }
144
145 int screen_CloseCapture( demux_t *p_demux )
146 {
147     demux_sys_t *p_sys = p_demux->p_sys;
148     screen_data_t *p_data = p_sys->p_data;
149
150     if( p_data->p_block ) block_Release( p_data->p_block );
151
152     if( p_data->hgdi_backup)
153         SelectObject( p_data->hdc_dst, p_data->hgdi_backup );
154
155     DeleteDC( p_data->hdc_dst );
156     ReleaseDC( 0, p_data->hdc_src );
157     free( p_data );
158
159     return VLC_SUCCESS;
160 }
161
162 struct block_sys_t
163 {
164     block_t self;
165     HBITMAP hbmp;
166 };
167
168 static void CaptureBlockRelease( block_t *p_block )
169 {
170     DeleteObject( ((block_sys_t *)p_block)->hbmp );
171     free( p_block );
172 }
173
174 static block_t *CaptureBlockNew( demux_t *p_demux )
175 {
176     demux_sys_t *p_sys = p_demux->p_sys;
177     screen_data_t *p_data = p_sys->p_data;
178     block_sys_t *p_block;
179     void *p_buffer;
180     int i_buffer;
181     HBITMAP hbmp;
182
183     /* Create the bitmap storage space */
184     hbmp = CreateDIBSection( p_data->hdc_dst, &p_data->bmi, DIB_RGB_COLORS,
185                              &p_buffer, NULL, 0 );
186     if( !hbmp || !p_buffer )
187     {
188         msg_Err( p_demux, "cannot create bitmap" );
189         if( hbmp ) DeleteObject( hbmp );
190         return NULL;
191     }
192
193     /* Select the bitmap into the compatible DC */
194     if( !p_data->hgdi_backup )
195         p_data->hgdi_backup = SelectObject( p_data->hdc_dst, hbmp );
196     else
197         SelectObject( p_data->hdc_dst, hbmp );
198
199     if( !p_data->hgdi_backup )
200     {
201         msg_Err( p_demux, "cannot select bitmap" );
202         DeleteObject( hbmp );
203         return NULL;
204     }
205
206     /* Build block */
207     if( !(p_block = malloc( sizeof( block_t ) + sizeof( block_sys_t ) )) )
208     {
209         DeleteObject( hbmp );
210         return NULL;
211     }
212     /* Fill all fields */
213     i_buffer = (p_sys->fmt.video.i_bits_per_pixel + 7) / 8 *
214         p_sys->fmt.video.i_width * p_sys->fmt.video.i_height;
215     block_Init( &p_block->self, p_buffer, i_buffer );
216     p_block->self.pf_release = CaptureBlockRelease;
217     p_block->hbmp            = hbmp;
218
219     return &p_block->self;
220 }
221
222 block_t *screen_Capture( demux_t *p_demux )
223 {
224     demux_sys_t *p_sys = p_demux->p_sys;
225     screen_data_t *p_data = p_sys->p_data;
226
227     if( !p_data->i_fragment )
228     {
229         if( !( p_data->p_block = CaptureBlockNew( p_demux ) ) )
230         {
231             msg_Warn( p_demux, "cannot get block" );
232             return 0;
233         }
234     }
235
236     if( !BitBlt( p_data->hdc_dst, 0, p_data->i_fragment *
237                  p_data->i_fragment_size,
238                  p_sys->fmt.video.i_width, p_data->i_fragment_size,
239                  p_data->hdc_src, 0, p_data->i_fragment *
240                  p_data->i_fragment_size,
241                  IS_WINNT ? SRCCOPY | CAPTUREBLT : SRCCOPY ) )
242     {
243         msg_Err( p_demux, "error during BitBlt()" );
244         return NULL;
245     }
246
247     p_data->i_fragment++;
248
249     if( !( p_data->i_fragment %
250            (p_sys->fmt.video.i_height/p_data->i_fragment_size) ) )
251     {
252         block_t *p_block = p_data->p_block;
253         p_data->i_fragment = 0;
254         p_data->p_block = 0;
255         return p_block;
256     }
257
258     return NULL;
259 }