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