]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/common.c
Converted vout xcb to "vout display" API.
[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_display.h>
38
39 #include "xcb_vlc.h"
40
41 /**
42  * Check for an error
43  */
44 int CheckError (vout_display_t *vd, xcb_connection_t *conn,
45                 const char *str, xcb_void_cookie_t ck)
46 {
47     xcb_generic_error_t *err;
48
49     err = xcb_request_check (conn, ck);
50     if (err)
51     {
52         msg_Err (vd, "%s: X11 error %d", str, err->error_code);
53         return VLC_EGENERIC;
54     }
55     return VLC_SUCCESS;
56 }
57
58 /**
59  * Connect to the X server.
60  */
61 xcb_connection_t *Connect (vlc_object_t *obj)
62 {
63     char *display = var_CreateGetNonEmptyString (obj, "x11-display");
64     xcb_connection_t *conn = xcb_connect (display, NULL);
65
66     free (display);
67     if (xcb_connection_has_error (conn) /*== NULL*/)
68     {
69         msg_Err (obj, "cannot connect to X server");
70         xcb_disconnect (conn);
71         return NULL;
72     }
73     return conn;
74 }
75
76
77 /**
78  * Create a VLC video X window object, find the corresponding X server screen,
79  * and probe the MIT-SHM extension.
80  */
81 vout_window_t *GetWindow (vout_display_t *vd,
82                           xcb_connection_t *conn,
83                           const xcb_screen_t **restrict pscreen,
84                           bool *restrict pshm)
85 {
86     /* Get window */
87     xcb_window_t root;
88     vout_window_cfg_t wnd_cfg;
89
90     memset( &wnd_cfg, 0, sizeof(wnd_cfg) );
91     wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
92     wnd_cfg.width  = vd->cfg->display.width;
93     wnd_cfg.height = vd->cfg->display.height;
94
95     vout_window_t *wnd = vout_display_NewWindow (vd, &wnd_cfg);
96     if (wnd == NULL)
97     {
98         msg_Err (vd, "parent window not available");
99         return NULL;
100     }
101     else
102     {
103         xcb_get_geometry_reply_t *geo;
104         xcb_get_geometry_cookie_t ck;
105
106         ck = xcb_get_geometry (conn, wnd->handle.xid);
107         geo = xcb_get_geometry_reply (conn, ck, NULL);
108         if (geo == NULL)
109         {
110             msg_Err (vd, "parent window not valid");
111             goto error;
112         }
113         root = geo->root;
114         free (geo);
115
116         /* Subscribe to parent window resize events */
117         const uint32_t value = XCB_EVENT_MASK_STRUCTURE_NOTIFY;
118         xcb_change_window_attributes (conn, wnd->handle.xid,
119                                       XCB_CW_EVENT_MASK, &value);
120     }
121
122     /* Find the selected screen */
123     const xcb_setup_t *setup = xcb_get_setup (conn);
124     const xcb_screen_t *screen = NULL;
125     for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
126          i.rem > 0 && screen == NULL; xcb_screen_next (&i))
127     {
128         if (i.data->root == root)
129             screen = i.data;
130     }
131
132     if (screen == NULL)
133     {
134         msg_Err (vd, "parent window screen not found");
135         goto error;
136     }
137     msg_Dbg (vd, "using screen 0x%"PRIx32, root);
138
139     /* Check MIT-SHM shared memory support */
140     bool shm = var_CreateGetBool (vd, "x11-shm") > 0;
141     if (shm)
142     {
143         xcb_shm_query_version_cookie_t ck;
144         xcb_shm_query_version_reply_t *r;
145
146         ck = xcb_shm_query_version (conn);
147         r = xcb_shm_query_version_reply (conn, ck, NULL);
148         if (!r)
149         {
150             msg_Err (vd, "shared memory (MIT-SHM) not available");
151             msg_Warn (vd, "display will be slow");
152             shm = false;
153         }
154         free (r);
155     }
156
157     *pscreen = screen;
158     *pshm = shm;
159     return wnd;
160
161 error:
162     vout_display_DeleteWindow (vd, wnd);
163     return NULL;
164 }
165
166 /**
167  * Gets the size of an X window.
168  */
169 int GetWindowSize (struct vout_window_t *wnd, xcb_connection_t *conn,
170                    unsigned *restrict width, unsigned *restrict height)
171 {
172     xcb_get_geometry_cookie_t ck = xcb_get_geometry (conn, wnd->handle.xid);
173     xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, ck, NULL);
174
175     if (!geo)
176         return -1;
177
178     *width = geo->width;
179     *height = geo->height;
180     free (geo);
181     return 0;
182 }
183
184 /**
185  * Initialize a picture buffer as shared memory, according to the video output
186  * format. If a attach is true, the segment is attached to
187  * the X server (MIT-SHM extension).
188  */
189 int PictureResourceAlloc (vout_display_t *vd, picture_resource_t *res, size_t size,
190                           xcb_connection_t *conn, bool attach)
191 {
192     res->p_sys = malloc (sizeof(*res->p_sys));
193     if (!res->p_sys)
194         return VLC_EGENERIC;
195
196     /* Allocate shared memory segment */
197     int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
198     if (id == -1)
199     {
200         msg_Err (vd, "shared memory allocation error: %m");
201         free (res->p_sys);
202         return VLC_EGENERIC;
203     }
204
205     /* Attach the segment to VLC */
206     void *shm = shmat (id, NULL, 0 /* read/write */);
207     if (-1 == (intptr_t)shm)
208     {
209         msg_Err (vd, "shared memory attachment error: %m");
210         shmctl (id, IPC_RMID, 0);
211         free (res->p_sys);
212         return VLC_EGENERIC;
213     }
214
215     xcb_shm_seg_t segment;
216     if (attach)
217     {
218         /* Attach the segment to X */
219         xcb_void_cookie_t ck;
220
221         segment = xcb_generate_id (conn);
222         ck = xcb_shm_attach_checked (conn, segment, id, 1);
223
224         if (CheckError (vd, conn, "shared memory server-side error", ck))
225         {
226             msg_Info (vd, "using buggy X11 server - SSH proxying?");
227             segment = 0;
228         }
229     }
230     else
231         segment = 0;
232
233     shmctl (id, IPC_RMID, 0);
234     res->p_sys->segment = segment;
235     res->p->p_pixels = shm;
236     return VLC_SUCCESS;
237 }
238
239 /**
240  * Release picture private data: detach the shared memory segment.
241  */
242 void PictureResourceFree (picture_resource_t *res, xcb_connection_t *conn)
243 {
244     xcb_shm_seg_t segment = res->p_sys->segment;
245
246     if (segment != 0)
247     {
248         assert (conn != NULL);
249         xcb_shm_detach (conn, segment);
250     }
251     shmdt (res->p->p_pixels);
252 }
253