3 * @brief Common code for XCB video output plugins
5 /*****************************************************************************
6 * Copyright © 2009 Rémi Denis-Courmont
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.
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.
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 ****************************************************************************/
30 #include <sys/types.h>
36 #include <vlc_common.h>
37 #include <vlc_vout_display.h>
44 int CheckError (vout_display_t *vd, xcb_connection_t *conn,
45 const char *str, xcb_void_cookie_t ck)
47 xcb_generic_error_t *err;
49 err = xcb_request_check (conn, ck);
52 msg_Err (vd, "%s: X11 error %d", str, err->error_code);
59 * Connect to the X server.
61 xcb_connection_t *Connect (vlc_object_t *obj)
63 char *display = var_CreateGetNonEmptyString (obj, "x11-display");
64 xcb_connection_t *conn = xcb_connect (display, NULL);
67 if (xcb_connection_has_error (conn) /*== NULL*/)
69 msg_Err (obj, "cannot connect to X server");
70 xcb_disconnect (conn);
78 * Create a VLC video X window object, find the corresponding X server screen,
79 * and probe the MIT-SHM extension.
81 vout_window_t *GetWindow (vout_display_t *vd,
82 xcb_connection_t *conn,
83 const xcb_screen_t **restrict pscreen,
88 vout_window_cfg_t wnd_cfg;
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;
95 vout_window_t *wnd = vout_display_NewWindow (vd, &wnd_cfg);
98 msg_Err (vd, "parent window not available");
103 xcb_get_geometry_reply_t *geo;
104 xcb_get_geometry_cookie_t ck;
106 ck = xcb_get_geometry (conn, wnd->handle.xid);
107 geo = xcb_get_geometry_reply (conn, ck, NULL);
110 msg_Err (vd, "parent window not valid");
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);
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))
128 if (i.data->root == root)
134 msg_Err (vd, "parent window screen not found");
137 msg_Dbg (vd, "using screen 0x%"PRIx32, root);
139 /* Check MIT-SHM shared memory support */
140 bool shm = var_CreateGetBool (vd, "x11-shm") > 0;
143 xcb_shm_query_version_cookie_t ck;
144 xcb_shm_query_version_reply_t *r;
146 ck = xcb_shm_query_version (conn);
147 r = xcb_shm_query_version_reply (conn, ck, NULL);
150 msg_Err (vd, "shared memory (MIT-SHM) not available");
151 msg_Warn (vd, "display will be slow");
162 vout_display_DeleteWindow (vd, wnd);
167 * Gets the size of an X window.
169 int GetWindowSize (struct vout_window_t *wnd, xcb_connection_t *conn,
170 unsigned *restrict width, unsigned *restrict height)
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);
179 *height = geo->height;
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).
189 int PictureResourceAlloc (vout_display_t *vd, picture_resource_t *res, size_t size,
190 xcb_connection_t *conn, bool attach)
192 res->p_sys = malloc (sizeof(*res->p_sys));
196 /* Allocate shared memory segment */
197 int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
200 msg_Err (vd, "shared memory allocation error: %m");
205 /* Attach the segment to VLC */
206 void *shm = shmat (id, NULL, 0 /* read/write */);
207 if (-1 == (intptr_t)shm)
209 msg_Err (vd, "shared memory attachment error: %m");
210 shmctl (id, IPC_RMID, 0);
215 xcb_shm_seg_t segment;
218 /* Attach the segment to X */
219 xcb_void_cookie_t ck;
221 segment = xcb_generate_id (conn);
222 ck = xcb_shm_attach_checked (conn, segment, id, 1);
224 if (CheckError (vd, conn, "shared memory server-side error", ck))
226 msg_Info (vd, "using buggy X11 server - SSH proxying?");
233 shmctl (id, IPC_RMID, 0);
234 res->p_sys->segment = segment;
235 res->p->p_pixels = shm;
240 * Release picture private data: detach the shared memory segment.
242 void PictureResourceFree (picture_resource_t *res, xcb_connection_t *conn)
244 xcb_shm_seg_t segment = res->p_sys->segment;
248 assert (conn != NULL);
249 xcb_shm_detach (conn, segment);
251 shmdt (res->p->p_pixels);