]> git.sesse.net Git - vlc/blob - modules/access/screen/win32.c
3c2b0de5b3fad8782b48a042860bff72985d4b89
[vlc] / modules / access / screen / win32.c
1 /*****************************************************************************
2  * win32.c: Screen capture module.
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include "screen.h"
33
34 struct screen_data_t
35 {
36     HDC hdc_src;
37     HDC hdc_dst;
38     HBITMAP hbmp;
39     HGDIOBJ hgdi_backup;
40     uint8_t *p_buffer;
41 };
42
43 int screen_InitCapture( demux_t *p_demux )
44 {
45     demux_sys_t *p_sys = p_demux->p_sys;
46     screen_data_t *p_data;
47     int i_chroma, i_bits_per_pixel;
48
49     BITMAPINFO bmi;
50
51     p_sys->p_data = p_data = malloc( sizeof( screen_data_t ) );
52
53     /* Get the device context for the whole screen */
54     p_data->hdc_src = CreateDC( "DISPLAY", NULL, NULL, NULL );
55     if( !p_data->hdc_src )
56     {
57         msg_Err( p_demux, "cannot get device context" );
58         return VLC_EGENERIC;
59     }
60
61     p_data->hdc_dst = CreateCompatibleDC( p_data->hdc_src );
62     if( !p_data->hdc_dst )
63     {
64         msg_Err( p_demux, "cannot get compat device context" );
65         ReleaseDC( 0, p_data->hdc_src );
66         return VLC_EGENERIC;
67     }
68
69     i_bits_per_pixel = GetDeviceCaps( p_data->hdc_src, BITSPIXEL );
70     switch( i_bits_per_pixel )
71     {
72     case 8: /* FIXME: set the palette */
73         i_chroma = VLC_FOURCC('R','G','B','2'); break;
74     case 15:
75         i_chroma = VLC_FOURCC('R','V','1','5'); break;
76     case 16:
77         i_chroma = VLC_FOURCC('R','V','1','6'); break;
78     case 24:
79         i_chroma = VLC_FOURCC('R','V','2','4'); break;
80     case 32:
81         i_chroma = VLC_FOURCC('R','V','3','2'); break;
82     default:
83         msg_Err( p_demux, "unknown screen depth %i",
84                  p_sys->fmt.video.i_bits_per_pixel );
85         ReleaseDC( 0, p_data->hdc_src );
86         ReleaseDC( 0, p_data->hdc_dst );
87         return VLC_EGENERIC;
88     }
89
90 #if 1 /* For now we force RV24 because of chroma inversion in the other cases*/
91     i_chroma = VLC_FOURCC('R','V','2','4');
92     i_bits_per_pixel = 24;
93 #endif
94
95     es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
96     p_sys->fmt.video.i_width  = GetDeviceCaps( p_data->hdc_src, HORZRES );
97     p_sys->fmt.video.i_height = GetDeviceCaps( p_data->hdc_src, VERTRES );
98     p_sys->fmt.video.i_bits_per_pixel = i_bits_per_pixel;
99
100     /* Create the bitmap info header */
101     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
102     bmi.bmiHeader.biWidth = p_sys->fmt.video.i_width;
103     bmi.bmiHeader.biHeight = - p_sys->fmt.video.i_height;
104     bmi.bmiHeader.biPlanes = 1;
105     bmi.bmiHeader.biBitCount = p_sys->fmt.video.i_bits_per_pixel;
106     bmi.bmiHeader.biCompression = BI_RGB;
107     bmi.bmiHeader.biSizeImage = 0;
108     bmi.bmiHeader.biXPelsPerMeter =
109         bmi.bmiHeader.biYPelsPerMeter = 0;
110     bmi.bmiHeader.biClrUsed = 0;
111     bmi.bmiHeader.biClrImportant = 0;
112
113     /* Create the bitmap storage space */
114     p_data->hbmp = CreateDIBSection( p_data->hdc_dst, (BITMAPINFO *)&bmi,
115         DIB_RGB_COLORS, (void **)&p_data->p_buffer, NULL, 0 );
116     if( !p_data->hbmp || !p_data->p_buffer )
117     {
118         msg_Err( p_demux, "cannot create bitmap" );
119         if( p_data->hbmp ) DeleteObject( p_data->hbmp );
120         ReleaseDC( 0, p_data->hdc_src );
121         DeleteDC( p_data->hdc_dst );
122         return VLC_EGENERIC;
123     }
124
125     /* Select the bitmap into the compatible DC */
126     p_data->hgdi_backup = SelectObject( p_data->hdc_dst, p_data->hbmp );
127     if( !p_data->hgdi_backup )
128     {
129         msg_Err( p_demux, "cannot select bitmap" );
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     SelectObject( p_data->hdc_dst, p_data->hgdi_backup );
141     DeleteObject( p_data->hbmp );
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 block_t *screen_Capture( demux_t *p_demux )
150 {
151     demux_sys_t *p_sys = p_demux->p_sys;
152     screen_data_t *p_data = p_sys->p_data;
153     block_t *p_block;
154     int i_size;
155
156     if( !BitBlt( p_data->hdc_dst, 0, 0,
157                  p_sys->fmt.video.i_width, p_sys->fmt.video.i_height,
158                  p_data->hdc_src, 0, 0, SRCCOPY ) )
159     {
160         msg_Err( p_demux, "error during BitBlt()" );
161         return NULL;
162     }
163
164     i_size = (p_sys->fmt.video.i_bits_per_pixel + 7) / 8 *
165         p_sys->fmt.video.i_width * p_sys->fmt.video.i_height;
166
167     if( !( p_block = block_New( p_demux, i_size ) ) )
168     {
169         msg_Warn( p_demux, "cannot get block" );
170         return 0;
171     }
172
173     memcpy( p_block->p_buffer, p_data->p_buffer, i_size );
174
175     return p_block;
176 }
177