]> git.sesse.net Git - vlc/blob - modules/access/screen/win32.c
3812393598f0f0e53ea4c3177a530e62f8c4f489
[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         return VLC_EGENERIC;
68     }
69
70     p_data->hdc_dst = CreateCompatibleDC( p_data->hdc_src );
71     if( !p_data->hdc_dst )
72     {
73         msg_Err( p_demux, "cannot get compat device context" );
74         ReleaseDC( 0, p_data->hdc_src );
75         return VLC_EGENERIC;
76     }
77
78     i_bits_per_pixel = GetDeviceCaps( p_data->hdc_src, BITSPIXEL );
79     switch( i_bits_per_pixel )
80     {
81     case 8: /* FIXME: set the palette */
82         i_chroma = VLC_FOURCC('R','G','B','2'); break;
83     case 15:
84     case 16:    /* Yes it is really 15 bits (when using BI_RGB) */
85         i_chroma = VLC_FOURCC('R','V','1','5'); 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     es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
99     p_sys->fmt.video.i_visible_width =
100     p_sys->fmt.video.i_width  = GetDeviceCaps( p_data->hdc_src, HORZRES );
101     p_sys->fmt.video.i_visible_height =
102     p_sys->fmt.video.i_height = GetDeviceCaps( p_data->hdc_src, VERTRES );
103     p_sys->fmt.video.i_bits_per_pixel = i_bits_per_pixel;
104     p_sys->fmt.video.i_chroma = i_chroma;
105
106     switch( i_chroma )
107     {
108     case VLC_FOURCC('R','V','1','5'):
109         p_sys->fmt.video.i_rmask = 0x7c00;
110         p_sys->fmt.video.i_gmask = 0x03e0;
111         p_sys->fmt.video.i_bmask = 0x001f;
112         break;
113     case VLC_FOURCC('R','V','2','4'):
114         p_sys->fmt.video.i_rmask = 0x00ff0000;
115         p_sys->fmt.video.i_gmask = 0x0000ff00;
116         p_sys->fmt.video.i_bmask = 0x000000ff;
117         break;
118     case VLC_FOURCC('R','V','3','2'):
119         p_sys->fmt.video.i_rmask = 0x00ff0000;
120         p_sys->fmt.video.i_gmask = 0x0000ff00;
121         p_sys->fmt.video.i_bmask = 0x000000ff;
122         break;
123     default:
124         msg_Warn( p_demux, "Unknown RGB masks" );
125         break;
126     }
127
128
129     return VLC_SUCCESS;
130 }
131
132 int screen_CloseCapture( demux_t *p_demux )
133 {
134     demux_sys_t *p_sys = p_demux->p_sys;
135     screen_data_t *p_data = p_sys->p_data;
136
137     if( p_data->p_block ) block_Release( p_data->p_block );
138
139     if( p_data->hgdi_backup)
140         SelectObject( p_data->hdc_dst, p_data->hgdi_backup );
141
142     DeleteDC( p_data->hdc_dst );
143     ReleaseDC( 0, p_data->hdc_src );
144     free( p_data );
145
146     return VLC_SUCCESS;
147 }
148
149 struct block_sys_t
150 {
151     block_t self;
152     HBITMAP hbmp;
153 };
154
155 static void CaptureBlockRelease( block_t *p_block )
156 {
157     DeleteObject( ((block_sys_t *)p_block)->hbmp );
158     free( p_block );
159 }
160
161 static block_t *CaptureBlockNew( demux_t *p_demux )
162 {
163     demux_sys_t *p_sys = p_demux->p_sys;
164     screen_data_t *p_data = p_sys->p_data;
165     block_sys_t *p_block;
166     void *p_buffer;
167     int i_buffer;
168     HBITMAP hbmp;
169
170     if( p_data->bmi.bmiHeader.biSize == 0 )
171     {
172         vlc_value_t val;
173         /* Create the bitmap info header */
174         p_data->bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
175         p_data->bmi.bmiHeader.biWidth = p_sys->fmt.video.i_width;
176         p_data->bmi.bmiHeader.biHeight = - p_sys->fmt.video.i_height;
177         p_data->bmi.bmiHeader.biPlanes = 1;
178         p_data->bmi.bmiHeader.biBitCount = p_sys->fmt.video.i_bits_per_pixel;
179         p_data->bmi.bmiHeader.biCompression = BI_RGB;
180         p_data->bmi.bmiHeader.biSizeImage = 0;
181         p_data->bmi.bmiHeader.biXPelsPerMeter =
182             p_data->bmi.bmiHeader.biYPelsPerMeter = 0;
183         p_data->bmi.bmiHeader.biClrUsed = 0;
184         p_data->bmi.bmiHeader.biClrImportant = 0;
185
186         var_Create( p_demux, "screen-fragment-size",
187                     VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
188         var_Get( p_demux, "screen-fragment-size", &val );
189         p_data->i_fragment_size =
190             val.i_int > 0 ? val.i_int : p_sys->fmt.video.i_height;
191         p_data->i_fragment_size =
192             val.i_int > p_sys->fmt.video.i_height ? 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;
197         p_data->p_block = 0;
198     }
199
200
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 )
205     {
206         msg_Err( p_demux, "cannot create bitmap" );
207         if( hbmp ) DeleteObject( hbmp );
208         return NULL;
209     }
210
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 );
214     else
215         SelectObject( p_data->hdc_dst, hbmp );
216
217     if( !p_data->hgdi_backup )
218     {
219         msg_Err( p_demux, "cannot select bitmap" );
220         DeleteObject( hbmp );
221         return NULL;
222     }
223
224     /* Build block */
225     if( !(p_block = malloc( sizeof( block_t ) + sizeof( block_sys_t ) )) )
226     {
227         DeleteObject( hbmp );
228         return NULL;
229     }
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;
236
237     return &p_block->self;
238 }
239
240 block_t *screen_Capture( demux_t *p_demux )
241 {
242     demux_sys_t *p_sys = p_demux->p_sys;
243     screen_data_t *p_data = p_sys->p_data;
244
245     if( !p_data->i_fragment )
246     {
247         if( !( p_data->p_block = CaptureBlockNew( p_demux ) ) )
248         {
249             msg_Warn( p_demux, "cannot get block" );
250             return 0;
251         }
252     }
253
254     if( p_sys->b_follow_mouse )
255     {
256         POINT pos;
257         GetCursorPos( &pos );
258         FollowMouse( p_sys, pos.x, pos.y );
259     }
260
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 ) )
267     {
268         msg_Err( p_demux, "error during BitBlt()" );
269         return NULL;
270     }
271
272     p_data->i_fragment++;
273
274     if( !( p_data->i_fragment %
275            (p_sys->fmt.video.i_height/p_data->i_fragment_size) ) )
276     {
277         block_t *p_block = p_data->p_block;
278         p_data->i_fragment = 0;
279         p_data->p_block = 0;
280
281         if( p_sys->p_mouse )
282         {
283             POINT pos;
284             GetCursorPos( &pos );
285             RenderCursor( p_demux, pos.x, pos.y,
286                           p_block->p_buffer );
287         }
288
289         return p_block;
290     }
291
292     return NULL;
293 }