1 /*****************************************************************************
2 * win32.c: Screen capture module.
3 *****************************************************************************
4 * Copyright (C) 2004-2008 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 = calloc( 1, sizeof( screen_data_t ) );
62 /* Get the device context for the whole screen */
63 p_data->hdc_src = CreateDC( "DISPLAY", NULL, NULL, NULL );
64 if( !p_data->hdc_src )
66 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" );
76 ReleaseDC( 0, p_data->hdc_src );
80 i_bits_per_pixel = GetDeviceCaps( p_data->hdc_src, BITSPIXEL );
81 switch( i_bits_per_pixel )
83 case 8: /* FIXME: set the palette */
84 i_chroma = VLC_CODEC_RGB8; break;
86 case 16: /* Yes it is really 15 bits (when using BI_RGB) */
87 i_chroma = VLC_CODEC_RGB15; break;
89 i_chroma = VLC_CODEC_RGB24; break;
91 i_chroma = VLC_CODEC_RGB32; break;
93 msg_Err( p_demux, "unknown screen depth %i",
94 p_sys->fmt.video.i_bits_per_pixel );
95 DeleteDC( p_data->hdc_dst );
96 ReleaseDC( 0, p_data->hdc_src );
101 es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
102 p_sys->fmt.video.i_visible_width =
103 p_sys->fmt.video.i_width = GetDeviceCaps( p_data->hdc_src, HORZRES );
104 p_sys->fmt.video.i_visible_height =
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 p_sys->fmt.video.i_chroma = i_chroma;
111 case VLC_CODEC_RGB15:
112 p_sys->fmt.video.i_rmask = 0x7c00;
113 p_sys->fmt.video.i_gmask = 0x03e0;
114 p_sys->fmt.video.i_bmask = 0x001f;
116 case VLC_CODEC_RGB24:
117 p_sys->fmt.video.i_rmask = 0x00ff0000;
118 p_sys->fmt.video.i_gmask = 0x0000ff00;
119 p_sys->fmt.video.i_bmask = 0x000000ff;
121 case VLC_CODEC_RGB32:
122 p_sys->fmt.video.i_rmask = 0x00ff0000;
123 p_sys->fmt.video.i_gmask = 0x0000ff00;
124 p_sys->fmt.video.i_bmask = 0x000000ff;
127 msg_Warn( p_demux, "Unknown RGB masks" );
135 int screen_CloseCapture( demux_t *p_demux )
137 demux_sys_t *p_sys = p_demux->p_sys;
138 screen_data_t *p_data = p_sys->p_data;
140 if( p_data->p_block ) block_Release( p_data->p_block );
142 if( p_data->hgdi_backup)
143 SelectObject( p_data->hdc_dst, p_data->hgdi_backup );
145 DeleteDC( p_data->hdc_dst );
146 ReleaseDC( 0, p_data->hdc_src );
158 static void CaptureBlockRelease( block_t *p_block )
160 DeleteObject( ((block_sys_t *)p_block)->hbmp );
164 static block_t *CaptureBlockNew( demux_t *p_demux )
166 demux_sys_t *p_sys = p_demux->p_sys;
167 screen_data_t *p_data = p_sys->p_data;
168 block_sys_t *p_block;
173 if( p_data->bmi.bmiHeader.biSize == 0 )
176 /* Create the bitmap info header */
177 p_data->bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
178 p_data->bmi.bmiHeader.biWidth = p_sys->fmt.video.i_width;
179 p_data->bmi.bmiHeader.biHeight = - p_sys->fmt.video.i_height;
180 p_data->bmi.bmiHeader.biPlanes = 1;
181 p_data->bmi.bmiHeader.biBitCount = p_sys->fmt.video.i_bits_per_pixel;
182 p_data->bmi.bmiHeader.biCompression = BI_RGB;
183 p_data->bmi.bmiHeader.biSizeImage = 0;
184 p_data->bmi.bmiHeader.biXPelsPerMeter =
185 p_data->bmi.bmiHeader.biYPelsPerMeter = 0;
186 p_data->bmi.bmiHeader.biClrUsed = 0;
187 p_data->bmi.bmiHeader.biClrImportant = 0;
189 i_val = var_CreateGetInteger( p_demux, "screen-fragment-size" );
190 p_data->i_fragment_size = i_val > 0 ? i_val : p_sys->fmt.video.i_height;
191 p_data->i_fragment_size = i_val > p_sys->fmt.video.i_height ?
192 p_sys->fmt.video.i_height :
193 p_data->i_fragment_size;
194 p_sys->f_fps *= (p_sys->fmt.video.i_height/p_data->i_fragment_size);
195 p_sys->i_incr = 1000000 / p_sys->f_fps;
196 p_data->i_fragment = 0;
201 /* Create the bitmap storage space */
202 hbmp = CreateDIBSection( p_data->hdc_dst, &p_data->bmi, DIB_RGB_COLORS,
203 &p_buffer, NULL, 0 );
204 if( !hbmp || !p_buffer )
206 msg_Err( p_demux, "cannot create bitmap" );
207 if( hbmp ) DeleteObject( hbmp );
211 /* Select the bitmap into the compatible DC */
212 if( !p_data->hgdi_backup )
213 p_data->hgdi_backup = SelectObject( p_data->hdc_dst, hbmp );
215 SelectObject( p_data->hdc_dst, hbmp );
217 if( !p_data->hgdi_backup )
219 msg_Err( p_demux, "cannot select bitmap" );
220 DeleteObject( hbmp );
225 if( !(p_block = malloc( sizeof( block_t ) + sizeof( block_sys_t ) )) )
227 DeleteObject( hbmp );
230 /* Fill all fields */
231 i_buffer = (p_sys->fmt.video.i_bits_per_pixel + 7) / 8 *
232 p_sys->fmt.video.i_width * p_sys->fmt.video.i_height;
233 block_Init( &p_block->self, p_buffer, i_buffer );
234 p_block->self.pf_release = CaptureBlockRelease;
235 p_block->hbmp = hbmp;
237 return &p_block->self;
240 block_t *screen_Capture( demux_t *p_demux )
242 demux_sys_t *p_sys = p_demux->p_sys;
243 screen_data_t *p_data = p_sys->p_data;
245 if( !p_data->i_fragment )
247 if( !( p_data->p_block = CaptureBlockNew( p_demux ) ) )
249 msg_Warn( p_demux, "cannot get block" );
254 if( p_sys->b_follow_mouse )
257 GetCursorPos( &pos );
258 FollowMouse( p_sys, pos.x, pos.y );
261 if( !BitBlt( p_data->hdc_dst, 0,
262 p_data->i_fragment * p_data->i_fragment_size,
263 p_sys->fmt.video.i_width, p_data->i_fragment_size,
264 p_data->hdc_src, p_sys->i_left, p_sys->i_top +
265 p_data->i_fragment * p_data->i_fragment_size,
266 SRCCOPY | CAPTUREBLT ) )
268 msg_Err( p_demux, "error during BitBlt()" );
272 p_data->i_fragment++;
274 if( !( p_data->i_fragment %
275 (p_sys->fmt.video.i_height/p_data->i_fragment_size) ) )
277 block_t *p_block = p_data->p_block;
278 p_data->i_fragment = 0;
284 GetCursorPos( &pos );
285 RenderCursor( p_demux, pos.x, pos.y,