]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/common.c
Merge branch '1.0-bugfix'
[vlc] / modules / video_output / xcb / common.c
1 /**
2  * @file common.c
3  * @brief Common code for XCB video output plugins
4  */
5 /*****************************************************************************
6  * Copyright © 2009 Rémi Denis-Courmont
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2.0
11  * of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <assert.h>
29
30 #include <sys/types.h>
31 #include <sys/shm.h>
32
33 #include <xcb/xcb.h>
34 #include <xcb/shm.h>
35
36 #include <vlc_common.h>
37 #include <vlc_vout.h>
38 #include <vlc_window.h>
39
40 #include "xcb_vlc.h"
41
42 /**
43  * Connect to the X server.
44  */
45 xcb_connection_t *Connect (vlc_object_t *obj)
46 {
47     char *display = var_CreateGetNonEmptyString (obj, "x11-display");
48     xcb_connection_t *conn = xcb_connect (display, NULL);
49
50     free (display);
51     if (xcb_connection_has_error (conn) /*== NULL*/)
52     {
53         msg_Err (obj, "cannot connect to X server");
54         xcb_disconnect (conn);
55         return NULL;
56     }
57     return conn;
58 }
59
60
61 /**
62  * Create a VLC video X window object, find the corresponding X server screen,
63  * and probe the MIT-SHM extension.
64  */
65 vout_window_t *GetWindow (vout_thread_t *obj,
66                           xcb_connection_t *conn,
67                           const xcb_screen_t **restrict pscreen,
68                           bool *restrict pshm)
69 {
70     /* Get window */
71     xcb_window_t root;
72     vout_window_t *wnd = vout_RequestXWindow (obj, &(int){ 0 }, &(int){ 0 },
73                                         &(unsigned){ 0 }, &(unsigned){ 0 });
74     if (wnd == NULL)
75     {
76         msg_Err (obj, "parent window not available");
77         return NULL;
78     }
79     else
80     {
81         xcb_get_geometry_reply_t *geo;
82         xcb_get_geometry_cookie_t ck;
83
84         ck = xcb_get_geometry (conn, wnd->handle.xid);
85         geo = xcb_get_geometry_reply (conn, ck, NULL);
86         if (geo == NULL)
87         {
88             msg_Err (obj, "parent window not valid");
89             goto error;
90         }
91         root = geo->root;
92         free (geo);
93
94         /* Subscribe to parent window resize events */
95         const uint32_t value = XCB_EVENT_MASK_STRUCTURE_NOTIFY;
96         xcb_change_window_attributes (conn, wnd->handle.xid,
97                                       XCB_CW_EVENT_MASK, &value);
98     }
99
100     /* Find the selected screen */
101     const xcb_setup_t *setup = xcb_get_setup (conn);
102     xcb_screen_t *screen = NULL;
103     for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
104          i.rem > 0 && screen == NULL; xcb_screen_next (&i))
105     {
106         if (i.data->root == root)
107             screen = i.data;
108     }
109
110     if (screen == NULL)
111     {
112         msg_Err (obj, "parent window screen not found");
113         goto error;
114     }
115     msg_Dbg (obj, "using screen 0x%"PRIx32, root);
116
117     /* Check MIT-SHM shared memory support */
118     bool shm = var_CreateGetBool (obj, "x11-shm") > 0;
119     if (shm)
120     {
121         xcb_shm_query_version_cookie_t ck;
122         xcb_shm_query_version_reply_t *r;
123
124         ck = xcb_shm_query_version (conn);
125         r = xcb_shm_query_version_reply (conn, ck, NULL);
126         if (!r)
127         {
128             msg_Err (obj, "shared memory (MIT-SHM) not available");
129             msg_Warn (obj, "display will be slow");
130             shm = false;
131         }
132         free (r);
133     }
134
135     *pscreen = screen;
136     *pshm = shm;
137     return wnd;
138
139 error:
140     vout_ReleaseWindow (wnd);
141     return NULL;
142 }
143
144 /**
145  * Gets the size of an X window.
146  */
147 int GetWindowSize (struct vout_window_t *wnd, xcb_connection_t *conn,
148                    unsigned *restrict width, unsigned *restrict height)
149 {
150     xcb_get_geometry_cookie_t ck = xcb_get_geometry (conn, wnd->handle.xid);
151     xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, ck, NULL);
152
153     if (!geo)
154         return -1;
155
156     *width = geo->width;
157     *height = geo->height;
158     free (geo);
159     return 0;
160 }
161
162 /**
163  * Initialize a picture buffer as shared memory, according to the video output
164  * format. If a XCB connection pointer is supplied, the segment is attached to
165  * the X server (MIT-SHM extension).
166  */
167 int PictureAlloc (vout_thread_t *vout, picture_t *pic, size_t size,
168                   xcb_connection_t *conn)
169 {
170     assert (pic->i_status == FREE_PICTURE);
171
172     /* Allocate shared memory segment */
173     int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
174     if (id == -1)
175     {
176         msg_Err (vout, "shared memory allocation error: %m");
177         return VLC_EGENERIC;
178     }
179
180     /* Attach the segment to VLC */
181     void *shm = shmat (id, NULL, 0 /* read/write */);
182     if (-1 == (intptr_t)shm)
183     {
184         msg_Err (vout, "shared memory attachment error: %m");
185         shmctl (id, IPC_RMID, 0);
186         return VLC_EGENERIC;
187     }
188
189     xcb_shm_seg_t segment;
190     if (conn != NULL)
191     {
192         /* Attach the segment to X */
193         xcb_void_cookie_t ck;
194
195         segment = xcb_generate_id (conn);
196         ck = xcb_shm_attach_checked (conn, segment, id, 1);
197
198         if (CheckError (vout, "shared memory server-side error", ck))
199         {
200             msg_Info (vout, "using buggy X11 server - SSH proxying?");
201             segment = 0;
202         }
203     }
204     else
205         segment = 0;
206
207     shmctl (id, IPC_RMID, 0);
208     pic->p_sys = (void *)(uintptr_t)segment;
209     pic->p->p_pixels = shm;
210     pic->i_status = DESTROYED_PICTURE;
211     pic->i_type = DIRECT_PICTURE;
212     return VLC_SUCCESS;
213 }
214
215 /**
216  * Release picture private data: detach the shared memory segment.
217  */
218 void PictureFree (picture_t *pic, xcb_connection_t *conn)
219 {
220     xcb_shm_seg_t segment = (uintptr_t)pic->p_sys;
221
222     if (segment != 0)
223     {
224         assert (conn != NULL);
225         xcb_shm_detach (conn, segment);
226     }
227     shmdt (pic->p->p_pixels);
228 }
229
230 /**
231  * Video output thread management stuff.
232  * FIXME: Much of this should move to core
233  */
234 void CommonManage (vout_thread_t *vout)
235 {
236     if (vout->i_changes & VOUT_SCALE_CHANGE)
237     {
238         vout->b_autoscale = var_GetBool (vout, "autoscale");
239         vout->i_zoom = ZOOM_FP_FACTOR;
240         vout->i_changes &= ~VOUT_SCALE_CHANGE;
241         vout->i_changes |= VOUT_SIZE_CHANGE;
242     }
243
244     if (vout->i_changes & VOUT_ZOOM_CHANGE)
245     {
246         vout->b_autoscale = false;
247         vout->i_zoom = var_GetFloat (vout, "scale") * ZOOM_FP_FACTOR;
248         vout->i_changes &= ~VOUT_ZOOM_CHANGE;
249         vout->i_changes |= VOUT_SIZE_CHANGE;
250     }
251
252     if (vout->i_changes & VOUT_CROP_CHANGE)
253     {
254         vout->fmt_out.i_x_offset = vout->fmt_in.i_x_offset;
255         vout->fmt_out.i_y_offset = vout->fmt_in.i_y_offset;
256         vout->fmt_out.i_visible_width = vout->fmt_in.i_visible_width;
257         vout->fmt_out.i_visible_height = vout->fmt_in.i_visible_height;
258         vout->i_changes &= ~VOUT_CROP_CHANGE;
259         vout->i_changes |= VOUT_SIZE_CHANGE;
260     }
261
262     if (vout->i_changes & VOUT_ASPECT_CHANGE)
263     {
264         vout->fmt_out.i_aspect = vout->fmt_in.i_aspect;
265         vout->fmt_out.i_sar_num = vout->fmt_in.i_sar_num;
266         vout->fmt_out.i_sar_den = vout->fmt_in.i_sar_den;
267         vout->output.i_aspect = vout->fmt_in.i_aspect;
268         vout->i_changes &= ~VOUT_ASPECT_CHANGE;
269         vout->i_changes |= VOUT_SIZE_CHANGE;
270     }
271 }