]> git.sesse.net Git - vlc/blob - modules/access/screen/x11.c
8723f150149cfdd333398818eae0d209ee2295b8
[vlc] / modules / access / screen / x11.c
1 /*****************************************************************************
2  * x11.c: Screen capture module.
3  *****************************************************************************
4  * Copyright (C) 2004-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Antoine Cellerier <dionoea at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34
35 #include <X11/Xlib.h>
36 #include <X11/Xutil.h>
37
38 #include "screen.h"
39
40 int screen_InitCapture( demux_t *p_demux )
41 {
42     demux_sys_t *p_sys = p_demux->p_sys;
43     Display *p_display;
44     char *psz_display = var_CreateGetNonEmptyString( p_demux, "x11-display" );
45     XWindowAttributes win_info;
46     int i_chroma;
47
48     /* Open the display */
49     p_display = XOpenDisplay( psz_display );
50     free( psz_display );
51     if( !p_display )
52     {
53         msg_Err( p_demux, "cannot open display" );
54         return VLC_EGENERIC;
55     }
56     p_sys->p_data = (void *)p_display;
57
58     /* Get the parameters of the root window */
59     if( !XGetWindowAttributes( p_display,
60                                DefaultRootWindow( p_display ),
61                                &win_info ) )
62     {
63         msg_Err( p_demux, "can't get root window attributes" );
64         XCloseDisplay( p_display );
65         return VLC_EGENERIC;
66     }
67
68     switch( win_info.depth )
69     {
70     case 8: /* FIXME: set the palette */
71         i_chroma = VLC_FOURCC('R','G','B','2'); break;
72     case 15:
73         i_chroma = VLC_FOURCC('R','V','1','5'); break;
74     case 16:
75         i_chroma = VLC_FOURCC('R','V','1','6'); break;
76     case 24:
77     case 32:
78         i_chroma = VLC_FOURCC('R','V','3','2');
79         win_info.depth = 32;
80         break;
81     default:
82         msg_Err( p_demux, "unknown screen depth %i", win_info.depth );
83         XCloseDisplay( p_display );
84         return VLC_EGENERIC;
85     }
86
87     es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
88     p_sys->fmt.video.i_visible_width =
89     p_sys->fmt.video.i_width  = win_info.width;
90     p_sys->fmt.video.i_visible_height =
91     p_sys->fmt.video.i_height = win_info.height;
92     p_sys->fmt.video.i_bits_per_pixel = win_info.depth;
93     p_sys->fmt.video.i_chroma = i_chroma;
94
95 #if 0
96     win_info.visual->red_mask;
97     win_info.visual->green_mask;
98     win_info.visual->blue_mask;
99     win_info.visual->bits_per_rgb;
100 #endif
101
102     return VLC_SUCCESS;
103 }
104
105 int screen_CloseCapture( 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
110     XCloseDisplay( p_display );
111     if( p_sys->p_blend )
112     {
113         module_unneed( p_sys->p_blend, p_sys->p_blend->p_module );
114         vlc_object_detach( p_sys->p_blend );
115         vlc_object_release( p_sys->p_blend );
116     }
117     return VLC_SUCCESS;
118 }
119
120 block_t *screen_Capture( demux_t *p_demux )
121 {
122     demux_sys_t *p_sys = p_demux->p_sys;
123     Display *p_display = (Display *)p_sys->p_data;
124     block_t *p_block;
125     XImage *image;
126     int i_size;
127     int root_x = 0, root_y = 0;
128
129     if( p_sys->b_follow_mouse || p_sys->p_mouse )
130     {
131         Window root = DefaultRootWindow( p_display ), child;
132         int win_x, win_y;
133         unsigned int mask;
134         if( XQueryPointer( p_display, root,
135             &root, &child, &root_x, &root_y, &win_x, &win_y,
136             &mask ) )
137         {
138             if( p_sys->b_follow_mouse )
139                 FollowMouse( p_sys, root_x, root_y );
140         }
141         else
142             msg_Dbg( p_demux, "XQueryPointer() failed" );
143
144     }
145
146     image = XGetImage( p_display, DefaultRootWindow( p_display ),
147                        p_sys->i_left, p_sys->i_top, p_sys->fmt.video.i_width,
148                        p_sys->fmt.video.i_height, AllPlanes, ZPixmap );
149
150     if( !image )
151     {
152         msg_Warn( p_demux, "cannot get image" );
153         return NULL;
154     }
155
156     i_size = image->bytes_per_line * image->height;
157
158     if( !( p_block = block_New( p_demux, i_size ) ) )
159     {
160         msg_Warn( p_demux, "cannot get block" );
161         XDestroyImage( image );
162         return NULL;
163     }
164
165     vlc_memcpy( p_block->p_buffer, image->data, i_size );
166
167     if( p_sys->p_mouse )
168         RenderCursor( p_demux, root_x, root_y,
169                       p_block->p_buffer );
170
171     XDestroyImage( image );
172
173     return p_block;
174 }
175