1 /*****************************************************************************
2 * win32.c: Screen capture module.
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
37 # define CAPTUREBLT (DWORD)0x40000000 /* Include layered windows */
52 int screen_InitCapture( demux_t *p_demux )
54 demux_sys_t *p_sys = p_demux->p_sys;
55 screen_data_t *p_data;
56 int i_chroma, i_bits_per_pixel;
58 p_sys->p_data = p_data = malloc( sizeof( screen_data_t ) );
61 memset( p_data, 0, sizeof( screen_data_t ) );
63 /* Get the device context for the whole screen */
64 p_data->hdc_src = CreateDC( "DISPLAY", NULL, NULL, NULL );
65 if( !p_data->hdc_src )
67 msg_Err( p_demux, "cannot get device context" );
71 p_data->hdc_dst = CreateCompatibleDC( p_data->hdc_src );
72 if( !p_data->hdc_dst )
74 msg_Err( p_demux, "cannot get compat device context" );
75 ReleaseDC( 0, p_data->hdc_src );
79 i_bits_per_pixel = GetDeviceCaps( p_data->hdc_src, BITSPIXEL );
80 switch( i_bits_per_pixel )
82 case 8: /* FIXME: set the palette */
83 i_chroma = VLC_FOURCC('R','G','B','2'); break;
85 case 16: /* Yes it is really 15 bits (when using BI_RGB) */
86 i_chroma = VLC_FOURCC('R','V','1','5'); break;
88 i_chroma = VLC_FOURCC('R','V','2','4'); break;
90 i_chroma = VLC_FOURCC('R','V','3','2'); break;
92 msg_Err( p_demux, "unknown screen depth %i",
93 p_sys->fmt.video.i_bits_per_pixel );
94 ReleaseDC( 0, p_data->hdc_src );
95 ReleaseDC( 0, p_data->hdc_dst );
99 es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
100 p_sys->fmt.video.i_width = GetDeviceCaps( p_data->hdc_src, HORZRES );
101 p_sys->fmt.video.i_height = GetDeviceCaps( p_data->hdc_src, VERTRES );
102 p_sys->fmt.video.i_bits_per_pixel = i_bits_per_pixel;
106 case VLC_FOURCC('R','V','1','5'):
107 p_sys->fmt.video.i_rmask = 0x7c00;
108 p_sys->fmt.video.i_gmask = 0x03e0;
109 p_sys->fmt.video.i_bmask = 0x001f;
111 case VLC_FOURCC('R','V','2','4'):
112 p_sys->fmt.video.i_rmask = 0x00ff0000;
113 p_sys->fmt.video.i_gmask = 0x0000ff00;
114 p_sys->fmt.video.i_bmask = 0x000000ff;
116 case VLC_FOURCC('R','V','3','2'):
117 p_sys->fmt.video.i_rmask = 0x00ff0000;
118 p_sys->fmt.video.i_gmask = 0x0000ff00;
119 p_sys->fmt.video.i_bmask = 0x000000ff;
122 msg_Warn( p_demux, "Unknown RGB masks" );
130 int screen_CloseCapture( demux_t *p_demux )
132 demux_sys_t *p_sys = p_demux->p_sys;
133 screen_data_t *p_data = p_sys->p_data;
135 if( p_data->p_block ) block_Release( p_data->p_block );
137 if( p_data->hgdi_backup)
138 SelectObject( p_data->hdc_dst, p_data->hgdi_backup );
140 DeleteDC( p_data->hdc_dst );
141 ReleaseDC( 0, p_data->hdc_src );
153 static void CaptureBlockRelease( block_t *p_block )
155 DeleteObject( ((block_sys_t *)p_block)->hbmp );
159 static block_t *CaptureBlockNew( demux_t *p_demux )
161 demux_sys_t *p_sys = p_demux->p_sys;
162 screen_data_t *p_data = p_sys->p_data;
163 block_sys_t *p_block;
168 if( p_data->bmi.bmiHeader.biSize == 0 )
171 /* Create the bitmap info header */
172 p_data->bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
173 p_data->bmi.bmiHeader.biWidth = p_sys->fmt.video.i_width;
174 p_data->bmi.bmiHeader.biHeight = - p_sys->fmt.video.i_height;
175 p_data->bmi.bmiHeader.biPlanes = 1;
176 p_data->bmi.bmiHeader.biBitCount = p_sys->fmt.video.i_bits_per_pixel;
177 p_data->bmi.bmiHeader.biCompression = BI_RGB;
178 p_data->bmi.bmiHeader.biSizeImage = 0;
179 p_data->bmi.bmiHeader.biXPelsPerMeter =
180 p_data->bmi.bmiHeader.biYPelsPerMeter = 0;
181 p_data->bmi.bmiHeader.biClrUsed = 0;
182 p_data->bmi.bmiHeader.biClrImportant = 0;
184 var_Create( p_demux, "screen-fragment-size",
185 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
186 var_Get( p_demux, "screen-fragment-size", &val );
187 p_data->i_fragment_size =
188 val.i_int > 0 ? val.i_int : p_sys->fmt.video.i_height;
189 p_data->i_fragment_size =
190 val.i_int > p_sys->fmt.video.i_height ? p_sys->fmt.video.i_height :
191 p_data->i_fragment_size;
192 p_sys->f_fps *= (p_sys->fmt.video.i_height/p_data->i_fragment_size);
193 p_sys->i_incr = 1000000 / p_sys->f_fps;
194 p_data->i_fragment = 0;
199 /* Create the bitmap storage space */
200 hbmp = CreateDIBSection( p_data->hdc_dst, &p_data->bmi, DIB_RGB_COLORS,
201 &p_buffer, NULL, 0 );
202 if( !hbmp || !p_buffer )
204 msg_Err( p_demux, "cannot create bitmap" );
205 if( hbmp ) DeleteObject( hbmp );
209 /* Select the bitmap into the compatible DC */
210 if( !p_data->hgdi_backup )
211 p_data->hgdi_backup = SelectObject( p_data->hdc_dst, hbmp );
213 SelectObject( p_data->hdc_dst, hbmp );
215 if( !p_data->hgdi_backup )
217 msg_Err( p_demux, "cannot select bitmap" );
218 DeleteObject( hbmp );
223 if( !(p_block = malloc( sizeof( block_t ) + sizeof( block_sys_t ) )) )
225 DeleteObject( hbmp );
228 /* Fill all fields */
229 i_buffer = (p_sys->fmt.video.i_bits_per_pixel + 7) / 8 *
230 p_sys->fmt.video.i_width * p_sys->fmt.video.i_height;
231 block_Init( &p_block->self, p_buffer, i_buffer );
232 p_block->self.pf_release = CaptureBlockRelease;
233 p_block->hbmp = hbmp;
235 return &p_block->self;
238 block_t *screen_Capture( demux_t *p_demux )
240 demux_sys_t *p_sys = p_demux->p_sys;
241 screen_data_t *p_data = p_sys->p_data;
243 if( !p_data->i_fragment )
245 if( !( p_data->p_block = CaptureBlockNew( p_demux ) ) )
247 msg_Warn( p_demux, "cannot get block" );
252 if( p_sys->b_follow_mouse )
255 GetCursorPos( &pos );
256 FollowMouse( p_sys, pos.x, pos.y );
259 if( !BitBlt( p_data->hdc_dst, 0,
260 p_data->i_fragment * p_data->i_fragment_size,
261 p_sys->fmt.video.i_width, p_data->i_fragment_size,
262 p_data->hdc_src, p_sys->i_left, p_sys->i_top +
263 p_data->i_fragment * p_data->i_fragment_size,
264 IS_WINNT ? SRCCOPY | CAPTUREBLT : SRCCOPY ) )
266 msg_Err( p_demux, "error during BitBlt()" );
270 p_data->i_fragment++;
272 if( !( p_data->i_fragment %
273 (p_sys->fmt.video.i_height/p_data->i_fragment_size) ) )
275 block_t *p_block = p_data->p_block;
276 p_data->i_fragment = 0;