]> git.sesse.net Git - vlc/blob - modules/access/screen/win32.c
Fix printf formating.
[vlc] / modules / access / screen / win32.c
1 /*****************************************************************************
2  * win32.c: Screen capture module.
3  *****************************************************************************
4  * Copyright (C) 2004-2011 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 Foundation,
21  * 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 struct screen_data_t
37 {
38     HDC hdc_src;
39     HDC hdc_dst;
40     BITMAPINFO bmi;
41     HGDIOBJ hgdi_backup;
42
43     int i_fragment_size;
44     int i_fragment;
45     block_t *p_block;
46 };
47
48 int screen_InitCapture( demux_t *p_demux )
49 {
50     demux_sys_t *p_sys = p_demux->p_sys;
51     screen_data_t *p_data;
52     int i_chroma, i_bits_per_pixel;
53
54     p_sys->p_data = p_data = calloc( 1, sizeof( screen_data_t ) );
55     if( !p_data )
56         return VLC_ENOMEM;
57
58     /* Get the device context for the whole screen */
59     p_data->hdc_src = CreateDC( "DISPLAY", NULL, NULL, NULL );
60     if( !p_data->hdc_src )
61     {
62         msg_Err( p_demux, "cannot get device context" );
63         free( p_data );
64         return VLC_EGENERIC;
65     }
66
67     p_data->hdc_dst = CreateCompatibleDC( p_data->hdc_src );
68     if( !p_data->hdc_dst )
69     {
70         msg_Err( p_demux, "cannot get compat device context" );
71         free( p_data );
72         ReleaseDC( 0, p_data->hdc_src );
73         return VLC_EGENERIC;
74     }
75
76     i_bits_per_pixel = GetDeviceCaps( p_data->hdc_src, BITSPIXEL );
77     switch( i_bits_per_pixel )
78     {
79     case 8: /* FIXME: set the palette */
80         i_chroma = VLC_CODEC_RGB8; break;
81     case 15:
82     case 16:    /* Yes it is really 15 bits (when using BI_RGB) */
83         i_chroma = VLC_CODEC_RGB15; break;
84     case 24:
85         i_chroma = VLC_CODEC_RGB24; break;
86     case 32:
87         i_chroma = VLC_CODEC_RGB32; break;
88     default:
89         msg_Err( p_demux, "unknown screen depth %i", i_bits_per_pixel );
90         DeleteDC( p_data->hdc_dst );
91         ReleaseDC( 0, p_data->hdc_src );
92         free( p_data );
93         return VLC_EGENERIC;
94     }
95
96     es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
97     p_sys->fmt.video.i_visible_width  =
98     p_sys->fmt.video.i_width          = GetDeviceCaps( p_data->hdc_src, HORZRES );
99     p_sys->fmt.video.i_visible_height =
100     p_sys->fmt.video.i_height         = GetDeviceCaps( p_data->hdc_src, VERTRES );
101     p_sys->fmt.video.i_bits_per_pixel = i_bits_per_pixel;
102     p_sys->fmt.video.i_chroma         = i_chroma;
103
104     switch( i_chroma )
105     {
106     case VLC_CODEC_RGB15:
107         p_sys->fmt.video.i_rmask = 0x7c00;
108         p_sys->fmt.video.i_gmask = 0x03e0;
109         p_sys->fmt.video.i_bmask = 0x001f;
110         break;
111     case VLC_CODEC_RGB24:
112         p_sys->fmt.video.i_rmask = 0x00ff0000;
113         p_sys->fmt.video.i_gmask = 0x0000ff00;
114         p_sys->fmt.video.i_bmask = 0x000000ff;
115         break;
116     case VLC_CODEC_RGB32:
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     default:
122         msg_Warn( p_demux, "Unknown RGB masks" );
123         break;
124     }
125
126     return VLC_SUCCESS;
127 }
128
129 int screen_CloseCapture( demux_t *p_demux )
130 {
131     demux_sys_t *p_sys = p_demux->p_sys;
132     screen_data_t *p_data = p_sys->p_data;
133
134     if( p_data->p_block ) block_Release( p_data->p_block );
135
136     if( p_data->hgdi_backup)
137         SelectObject( p_data->hdc_dst, p_data->hgdi_backup );
138
139     DeleteDC( p_data->hdc_dst );
140     ReleaseDC( 0, p_data->hdc_src );
141     free( p_data );
142
143     return VLC_SUCCESS;
144 }
145
146 struct block_sys_t
147 {
148     block_t self;
149     HBITMAP hbmp;
150 };
151
152 static void CaptureBlockRelease( block_t *p_block )
153 {
154     DeleteObject( ((block_sys_t *)p_block)->hbmp );
155     free( p_block );
156 }
157
158 static block_t *CaptureBlockNew( demux_t *p_demux )
159 {
160     demux_sys_t *p_sys = p_demux->p_sys;
161     screen_data_t *p_data = p_sys->p_data;
162     block_sys_t *p_block;
163     void *p_buffer;
164     int i_buffer;
165     HBITMAP hbmp;
166
167     if( p_data->bmi.bmiHeader.biSize == 0 )
168     {
169         int i_val;
170         /* Create the bitmap info header */
171         p_data->bmi.bmiHeader.biSize          = sizeof(BITMAPINFOHEADER);
172         p_data->bmi.bmiHeader.biWidth         = p_sys->fmt.video.i_width;
173         p_data->bmi.bmiHeader.biHeight        = - p_sys->fmt.video.i_height;
174         p_data->bmi.bmiHeader.biPlanes        = 1;
175         p_data->bmi.bmiHeader.biBitCount      = p_sys->fmt.video.i_bits_per_pixel;
176         p_data->bmi.bmiHeader.biCompression   = BI_RGB;
177         p_data->bmi.bmiHeader.biSizeImage     = 0;
178         p_data->bmi.bmiHeader.biXPelsPerMeter = 0;
179         p_data->bmi.bmiHeader.biYPelsPerMeter = 0;
180         p_data->bmi.bmiHeader.biClrUsed       = 0;
181         p_data->bmi.bmiHeader.biClrImportant  = 0;
182
183         i_val = var_CreateGetInteger( p_demux, "screen-fragment-size" );
184         p_data->i_fragment_size = i_val > 0 ? i_val : (int)p_sys->fmt.video.i_height;
185         p_data->i_fragment_size = i_val > (int)p_sys->fmt.video.i_height ?
186                                             (int)p_sys->fmt.video.i_height :
187                                             p_data->i_fragment_size;
188         p_sys->f_fps *= (p_sys->fmt.video.i_height/p_data->i_fragment_size);
189         p_sys->i_incr = 1000000 / p_sys->f_fps;
190         p_data->i_fragment = 0;
191         p_data->p_block = 0;
192     }
193
194
195     /* Create the bitmap storage space */
196     hbmp = CreateDIBSection( p_data->hdc_dst, &p_data->bmi, DIB_RGB_COLORS,
197                              &p_buffer, NULL, 0 );
198     if( !hbmp || !p_buffer )
199     {
200         msg_Err( p_demux, "cannot create bitmap" );
201         goto error;
202     }
203
204     /* Select the bitmap into the compatible DC */
205     if( !p_data->hgdi_backup )
206         p_data->hgdi_backup = SelectObject( p_data->hdc_dst, hbmp );
207     else
208         SelectObject( p_data->hdc_dst, hbmp );
209
210     if( !p_data->hgdi_backup )
211     {
212         msg_Err( p_demux, "cannot select bitmap" );
213         goto error;
214     }
215
216     /* Build block */
217     if( !(p_block = malloc( sizeof( block_t ) + sizeof( block_sys_t ) )) )
218         goto error;
219
220     /* Fill all fields */
221     i_buffer = (p_sys->fmt.video.i_bits_per_pixel + 7) / 8 *
222         p_sys->fmt.video.i_width * p_sys->fmt.video.i_height;
223     block_Init( &p_block->self, p_buffer, i_buffer );
224     p_block->self.pf_release = CaptureBlockRelease;
225     p_block->hbmp            = hbmp;
226
227     return &p_block->self;
228
229 error:
230     if( hbmp ) DeleteObject( hbmp );
231     return NULL;
232 }
233
234 block_t *screen_Capture( demux_t *p_demux )
235 {
236     demux_sys_t *p_sys = p_demux->p_sys;
237     screen_data_t *p_data = p_sys->p_data;
238
239     if( !p_data->i_fragment )
240     {
241         if( !( p_data->p_block = CaptureBlockNew( p_demux ) ) )
242         {
243             msg_Warn( p_demux, "cannot get block" );
244             return NULL;
245         }
246     }
247
248     if( p_sys->b_follow_mouse )
249     {
250         POINT pos;
251         GetCursorPos( &pos );
252         FollowMouse( p_sys, pos.x, pos.y );
253     }
254
255     if( !BitBlt( p_data->hdc_dst, 0,
256                  p_data->i_fragment * p_data->i_fragment_size,
257                  p_sys->fmt.video.i_width, p_data->i_fragment_size,
258                  p_data->hdc_src, p_sys->i_left, p_sys->i_top +
259                  p_data->i_fragment * p_data->i_fragment_size,
260                  SRCCOPY | CAPTUREBLT ) )
261     {
262         msg_Err( p_demux, "error during BitBlt()" );
263         return NULL;
264     }
265
266     p_data->i_fragment++;
267
268     if( !( p_data->i_fragment %
269            (p_sys->fmt.video.i_height/p_data->i_fragment_size) ) )
270     {
271         block_t *p_block = p_data->p_block;
272         p_data->i_fragment = 0;
273         p_data->p_block = 0;
274
275         if( p_sys->p_mouse )
276         {
277             POINT pos;
278             GetCursorPos( &pos );
279             RenderCursor( p_demux, pos.x, pos.y,
280                           p_block->p_buffer );
281         }
282
283         return p_block;
284     }
285
286     return NULL;
287 }