]> git.sesse.net Git - vlc/blob - modules/access/screen/win32.c
screen_win32: fix potential memleak and use DeleteDC instead of ReleaseDC.
[vlc] / modules / access / screen / win32.c
1 /*****************************************************************************
2  * win32.c: Screen capture module.
3  *****************************************************************************
4  * Copyright (C) 2004-2008 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
58     p_sys->p_data = p_data = calloc( 1, sizeof( screen_data_t ) );
59     if( !p_data )
60         return VLC_ENOMEM;
61
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 )
65     {
66         msg_Err( p_demux, "cannot get device context" );
67         free( p_data );
68         return VLC_EGENERIC;
69     }
70
71     p_data->hdc_dst = CreateCompatibleDC( p_data->hdc_src );
72     if( !p_data->hdc_dst )
73     {
74         msg_Err( p_demux, "cannot get compat device context" );
75         free( p_data );
76         ReleaseDC( 0, p_data->hdc_src );
77         return VLC_EGENERIC;
78     }
79
80     i_bits_per_pixel = GetDeviceCaps( p_data->hdc_src, BITSPIXEL );
81     switch( i_bits_per_pixel )
82     {
83     case 8: /* FIXME: set the palette */
84         i_chroma = VLC_FOURCC('R','G','B','2'); break;
85     case 15:
86     case 16:    /* Yes it is really 15 bits (when using BI_RGB) */
87         i_chroma = VLC_FOURCC('R','V','1','5'); break;
88     case 24:
89         i_chroma = VLC_FOURCC('R','V','2','4'); break;
90     case 32:
91         i_chroma = VLC_FOURCC('R','V','3','2'); break;
92     default:
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 );
97         free( p_data );
98         return VLC_EGENERIC;
99     }
100
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;
108
109     switch( i_chroma )
110     {
111     case VLC_FOURCC('R','V','1','5'):
112         p_sys->fmt.video.i_rmask = 0x7c00;
113         p_sys->fmt.video.i_gmask = 0x03e0;
114         p_sys->fmt.video.i_bmask = 0x001f;
115         break;
116     case VLC_FOURCC('R','V','2','4'):
117         p_sys->fmt.video.i_rmask = 0x00ff0000;
118         p_sys->fmt.video.i_gmask = 0x0000ff00;
119         p_sys->fmt.video.i_bmask = 0x000000ff;
120         break;
121     case VLC_FOURCC('R','V','3','2'):
122         p_sys->fmt.video.i_rmask = 0x00ff0000;
123         p_sys->fmt.video.i_gmask = 0x0000ff00;
124         p_sys->fmt.video.i_bmask = 0x000000ff;
125         break;
126     default:
127         msg_Warn( p_demux, "Unknown RGB masks" );
128         break;
129     }
130
131
132     return VLC_SUCCESS;
133 }
134
135 int screen_CloseCapture( demux_t *p_demux )
136 {
137     demux_sys_t *p_sys = p_demux->p_sys;
138     screen_data_t *p_data = p_sys->p_data;
139
140     if( p_data->p_block ) block_Release( p_data->p_block );
141
142     if( p_data->hgdi_backup)
143         SelectObject( p_data->hdc_dst, p_data->hgdi_backup );
144
145     DeleteDC( p_data->hdc_dst );
146     ReleaseDC( 0, p_data->hdc_src );
147     free( p_data );
148
149     return VLC_SUCCESS;
150 }
151
152 struct block_sys_t
153 {
154     block_t self;
155     HBITMAP hbmp;
156 };
157
158 static void CaptureBlockRelease( block_t *p_block )
159 {
160     DeleteObject( ((block_sys_t *)p_block)->hbmp );
161     free( p_block );
162 }
163
164 static block_t *CaptureBlockNew( demux_t *p_demux )
165 {
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;
169     void *p_buffer;
170     int i_buffer;
171     HBITMAP hbmp;
172
173     if( p_data->bmi.bmiHeader.biSize == 0 )
174     {
175         vlc_value_t val;
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;
188
189         var_Create( p_demux, "screen-fragment-size",
190                     VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
191         var_Get( p_demux, "screen-fragment-size", &val );
192         p_data->i_fragment_size =
193             val.i_int > 0 ? val.i_int : p_sys->fmt.video.i_height;
194         p_data->i_fragment_size =
195             val.i_int > p_sys->fmt.video.i_height ? p_sys->fmt.video.i_height :
196             p_data->i_fragment_size;
197         p_sys->f_fps *= (p_sys->fmt.video.i_height/p_data->i_fragment_size);
198         p_sys->i_incr = 1000000 / p_sys->f_fps;
199         p_data->i_fragment = 0;
200         p_data->p_block = 0;
201     }
202
203
204     /* Create the bitmap storage space */
205     hbmp = CreateDIBSection( p_data->hdc_dst, &p_data->bmi, DIB_RGB_COLORS,
206                              &p_buffer, NULL, 0 );
207     if( !hbmp || !p_buffer )
208     {
209         msg_Err( p_demux, "cannot create bitmap" );
210         if( hbmp ) DeleteObject( hbmp );
211         return NULL;
212     }
213
214     /* Select the bitmap into the compatible DC */
215     if( !p_data->hgdi_backup )
216         p_data->hgdi_backup = SelectObject( p_data->hdc_dst, hbmp );
217     else
218         SelectObject( p_data->hdc_dst, hbmp );
219
220     if( !p_data->hgdi_backup )
221     {
222         msg_Err( p_demux, "cannot select bitmap" );
223         DeleteObject( hbmp );
224         return NULL;
225     }
226
227     /* Build block */
228     if( !(p_block = malloc( sizeof( block_t ) + sizeof( block_sys_t ) )) )
229     {
230         DeleteObject( hbmp );
231         return NULL;
232     }
233     /* Fill all fields */
234     i_buffer = (p_sys->fmt.video.i_bits_per_pixel + 7) / 8 *
235         p_sys->fmt.video.i_width * p_sys->fmt.video.i_height;
236     block_Init( &p_block->self, p_buffer, i_buffer );
237     p_block->self.pf_release = CaptureBlockRelease;
238     p_block->hbmp            = hbmp;
239
240     return &p_block->self;
241 }
242
243 block_t *screen_Capture( demux_t *p_demux )
244 {
245     demux_sys_t *p_sys = p_demux->p_sys;
246     screen_data_t *p_data = p_sys->p_data;
247
248     if( !p_data->i_fragment )
249     {
250         if( !( p_data->p_block = CaptureBlockNew( p_demux ) ) )
251         {
252             msg_Warn( p_demux, "cannot get block" );
253             return 0;
254         }
255     }
256
257     if( p_sys->b_follow_mouse )
258     {
259         POINT pos;
260         GetCursorPos( &pos );
261         FollowMouse( p_sys, pos.x, pos.y );
262     }
263
264     if( !BitBlt( p_data->hdc_dst, 0,
265                  p_data->i_fragment * p_data->i_fragment_size,
266                  p_sys->fmt.video.i_width, p_data->i_fragment_size,
267                  p_data->hdc_src, p_sys->i_left, p_sys->i_top +
268                  p_data->i_fragment * p_data->i_fragment_size,
269                  SRCCOPY | CAPTUREBLT ) )
270     {
271         msg_Err( p_demux, "error during BitBlt()" );
272         return NULL;
273     }
274
275     p_data->i_fragment++;
276
277     if( !( p_data->i_fragment %
278            (p_sys->fmt.video.i_height/p_data->i_fragment_size) ) )
279     {
280         block_t *p_block = p_data->p_block;
281         p_data->i_fragment = 0;
282         p_data->p_block = 0;
283
284         if( p_sys->p_mouse )
285         {
286             POINT pos;
287             GetCursorPos( &pos );
288             RenderCursor( p_demux, pos.x, pos.y,
289                           p_block->p_buffer );
290         }
291
292         return p_block;
293     }
294
295     return NULL;
296 }