]> git.sesse.net Git - vlc/blob - modules/access/screen/x11.c
A bit of headers cleanup
[vlc] / modules / access / screen / x11.c
1 /*****************************************************************************
2  * x11.c: Screen capture module.
3  *****************************************************************************
4  * Copyright (C) 2004 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 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30
31 #include <X11/Xlib.h>
32 #include <X11/Xutil.h>
33
34 #include "screen.h"
35
36 int screen_InitCapture( demux_t *p_demux )
37 {
38     demux_sys_t *p_sys = p_demux->p_sys;
39     Display *p_display;
40     XWindowAttributes win_info;
41     int i_chroma;
42
43     /* Open the display */
44     p_display = XOpenDisplay( NULL );
45     if( !p_display )
46     {
47         msg_Err( p_demux, "cannot open display" );
48         return VLC_EGENERIC;
49     }
50     p_sys->p_data = (void *)p_display;
51
52     /* Get the parameters of the root window */
53     if( !XGetWindowAttributes( p_display,
54                                DefaultRootWindow( p_display ),
55                                &win_info ) )
56     {
57         msg_Err( p_demux, "can't get root window attributes" );
58         XCloseDisplay( p_display );
59         return VLC_EGENERIC;
60     }
61
62     switch( win_info.depth )
63     {
64     case 8: /* FIXME: set the palette */
65         i_chroma = VLC_FOURCC('R','G','B','2'); break;
66     case 15:
67         i_chroma = VLC_FOURCC('R','V','1','5'); break;
68     case 16:
69         i_chroma = VLC_FOURCC('R','V','1','6'); break;
70     case 24:
71     case 32:
72         i_chroma = VLC_FOURCC('R','V','3','2');
73         win_info.depth = 32;
74         break;
75     default:
76         msg_Err( p_demux, "unknown screen depth %i", win_info.depth );
77         XCloseDisplay( p_display );
78         return VLC_EGENERIC;
79     }
80
81     es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
82     p_sys->fmt.video.i_width  = win_info.width;
83     p_sys->fmt.video.i_height = win_info.height;
84     p_sys->fmt.video.i_bits_per_pixel = win_info.depth;
85
86 #if 0
87     win_info.visual->red_mask;
88     win_info.visual->green_mask;
89     win_info.visual->blue_mask;
90     win_info.visual->bits_per_rgb;
91 #endif
92
93     return VLC_SUCCESS;
94 }
95
96 int screen_CloseCapture( demux_t *p_demux )
97 {
98     demux_sys_t *p_sys = p_demux->p_sys;
99     Display *p_display = (Display *)p_sys->p_data;
100
101     XCloseDisplay( p_display );
102     return VLC_SUCCESS;
103 }
104
105 block_t *screen_Capture( demux_t *p_demux )
106 {
107     demux_sys_t *p_sys = p_demux->p_sys;
108     Display *p_display = (Display *)p_sys->p_data;
109     block_t *p_block;
110     XImage *image;
111     int i_size;
112
113     image = XGetImage( p_display, DefaultRootWindow( p_display ),
114                        0, 0, p_sys->fmt.video.i_width,
115                        p_sys->fmt.video.i_height, AllPlanes, ZPixmap );
116
117     if( !image )
118     {
119         msg_Warn( p_demux, "cannot get image" );
120         return 0;
121     }
122
123     i_size = image->bytes_per_line * image->height;
124
125     if( !( p_block = block_New( p_demux, i_size ) ) )
126     {
127         msg_Warn( p_demux, "cannot get block" );
128         XDestroyImage( image );
129         return 0;
130     }
131
132     memcpy( p_block->p_buffer, image->data, i_size );
133
134     XDestroyImage( image );
135
136     return p_block;
137 }
138