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>
38 #include <vlc_window.h>
43 * Connect to the X server.
45 xcb_connection_t *Connect (vlc_object_t *obj)
47 char *display = var_CreateGetNonEmptyString (obj, "x11-display");
48 xcb_connection_t *conn = xcb_connect (display, NULL);
51 if (xcb_connection_has_error (conn) /*== NULL*/)
53 msg_Err (obj, "cannot connect to X server");
54 xcb_disconnect (conn);
62 * Create a VLC video X window object, find the corresponding X server screen,
63 * and probe the MIT-SHM extension.
65 vout_window_t *GetWindow (vout_thread_t *obj,
66 xcb_connection_t *conn,
67 const xcb_screen_t **restrict pscreen,
72 vout_window_t *wnd = vout_RequestXWindow (obj, &(int){ 0 }, &(int){ 0 },
73 &(unsigned){ 0 }, &(unsigned){ 0 });
76 msg_Err (obj, "parent window not available");
81 xcb_get_geometry_reply_t *geo;
82 xcb_get_geometry_cookie_t ck;
84 ck = xcb_get_geometry (conn, wnd->handle.xid);
85 geo = xcb_get_geometry_reply (conn, ck, NULL);
88 msg_Err (obj, "parent window not valid");
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);
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))
106 if (i.data->root == root)
112 msg_Err (obj, "parent window screen not found");
115 msg_Dbg (obj, "using screen 0x%"PRIx32, root);
117 /* Check MIT-SHM shared memory support */
118 bool shm = var_CreateGetBool (obj, "x11-shm") > 0;
121 xcb_shm_query_version_cookie_t ck;
122 xcb_shm_query_version_reply_t *r;
124 ck = xcb_shm_query_version (conn);
125 r = xcb_shm_query_version_reply (conn, ck, NULL);
128 msg_Err (obj, "shared memory (MIT-SHM) not available");
129 msg_Warn (obj, "display will be slow");
140 vout_ReleaseWindow (wnd);
145 * Gets the size of an X window.
147 int GetWindowSize (struct vout_window_t *wnd, xcb_connection_t *conn,
148 unsigned *restrict width, unsigned *restrict height)
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);
157 *height = geo->height;
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).
167 int PictureInit (vout_thread_t *vout, picture_t *pic, xcb_connection_t *conn)
169 assert (pic->i_status == FREE_PICTURE);
170 vout_InitPicture (vout, pic, vout->output.i_chroma,
171 vout->output.i_width, vout->output.i_height,
172 vout->output.i_aspect);
174 const size_t size = pic->p->i_pitch * pic->p->i_lines;
176 /* Allocate shared memory segment */
177 int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
180 msg_Err (vout, "shared memory allocation error: %m");
184 /* Attach the segment to VLC */
185 void *shm = shmat (id, NULL, 0 /* read/write */);
186 if (-1 == (intptr_t)shm)
188 msg_Err (vout, "shared memory attachment error: %m");
189 shmctl (id, IPC_RMID, 0);
193 xcb_shm_seg_t segment;
196 /* Attach the segment to X */
197 xcb_void_cookie_t ck;
199 segment = xcb_generate_id (conn);
200 ck = xcb_shm_attach_checked (conn, segment, id, 1);
202 if (CheckError (vout, "shared memory server-side error", ck))
204 msg_Info (vout, "using buggy X11 server - SSH proxying?");
211 shmctl (id, IPC_RMID, 0);
212 pic->p_sys = (void *)(uintptr_t)segment;
213 pic->p->p_pixels = shm;
214 pic->i_status = DESTROYED_PICTURE;
215 pic->i_type = DIRECT_PICTURE;
220 * Release picture private data: detach the shared memory segment.
222 void PictureDeinit (picture_t *pic, xcb_connection_t *conn)
224 xcb_shm_seg_t segment = (uintptr_t)pic->p_sys;
228 assert (conn != NULL);
229 xcb_shm_detach (conn, segment);
231 shmdt (pic->p->p_pixels);