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);
60 * Connect to the X server.
62 xcb_connection_t *Connect (vlc_object_t *obj)
64 char *display = var_CreateGetNonEmptyString (obj, "x11-display");
65 xcb_connection_t *conn = xcb_connect (display, NULL);
68 if (xcb_connection_has_error (conn) /*== NULL*/)
70 msg_Err (obj, "cannot connect to X server");
71 xcb_disconnect (conn);
75 const xcb_setup_t *setup = xcb_get_setup (conn);
76 msg_Dbg (obj, "connected to X%"PRIu16".%"PRIu16" server",
77 setup->protocol_major_version, setup->protocol_minor_version);
78 char *vendor = strndup (xcb_setup_vendor (setup), setup->vendor_len);
81 msg_Dbg (obj, " vendor : %s", vendor);
84 msg_Dbg (obj, " version: %"PRIu32, setup->release_number);
90 * Create a VLC video X window object, find the corresponding X server screen,
91 * and probe the MIT-SHM extension.
93 vout_window_t *GetWindow (vout_display_t *vd,
94 xcb_connection_t *conn,
95 const xcb_screen_t **restrict pscreen,
100 vout_window_cfg_t wnd_cfg;
102 memset( &wnd_cfg, 0, sizeof(wnd_cfg) );
103 wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
104 wnd_cfg.width = vd->cfg->display.width;
105 wnd_cfg.height = vd->cfg->display.height;
107 vout_window_t *wnd = vout_display_NewWindow (vd, &wnd_cfg);
110 msg_Err (vd, "parent window not available");
115 xcb_get_geometry_reply_t *geo;
116 xcb_get_geometry_cookie_t ck;
118 ck = xcb_get_geometry (conn, wnd->handle.xid);
119 geo = xcb_get_geometry_reply (conn, ck, NULL);
122 msg_Err (vd, "parent window not valid");
128 /* Subscribe to parent window resize events */
129 uint32_t value = XCB_EVENT_MASK_STRUCTURE_NOTIFY;
130 xcb_change_window_attributes (conn, wnd->handle.xid,
131 XCB_CW_EVENT_MASK, &value);
132 /* Try to subscribe to click events */
133 /* (only one X11 client can get them, so might not work) */
134 value |= XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE;
135 xcb_change_window_attributes (conn, wnd->handle.xid,
136 XCB_CW_EVENT_MASK, &value);
139 /* Find the selected screen */
140 const xcb_setup_t *setup = xcb_get_setup (conn);
141 const xcb_screen_t *screen = NULL;
142 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
143 i.rem > 0 && screen == NULL; xcb_screen_next (&i))
145 if (i.data->root == root)
151 msg_Err (vd, "parent window screen not found");
154 msg_Dbg (vd, "using screen 0x%"PRIx32, root);
156 /* Check MIT-SHM shared memory support */
157 bool shm = var_CreateGetBool (vd, "x11-shm") > 0;
160 xcb_shm_query_version_cookie_t ck;
161 xcb_shm_query_version_reply_t *r;
163 ck = xcb_shm_query_version (conn);
164 r = xcb_shm_query_version_reply (conn, ck, NULL);
167 msg_Err (vd, "shared memory (MIT-SHM) not available");
168 msg_Warn (vd, "display will be slow");
179 vout_display_DeleteWindow (vd, wnd);
184 * Gets the size of an X window.
186 int GetWindowSize (struct vout_window_t *wnd, xcb_connection_t *conn,
187 unsigned *restrict width, unsigned *restrict height)
189 xcb_get_geometry_cookie_t ck = xcb_get_geometry (conn, wnd->handle.xid);
190 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, ck, NULL);
196 *height = geo->height;
202 * Initialize a picture buffer as shared memory, according to the video output
203 * format. If a attach is true, the segment is attached to
204 * the X server (MIT-SHM extension).
206 int PictureResourceAlloc (vout_display_t *vd, picture_resource_t *res, size_t size,
207 xcb_connection_t *conn, bool attach)
209 res->p_sys = malloc (sizeof(*res->p_sys));
213 /* Allocate shared memory segment */
214 int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
217 msg_Err (vd, "shared memory allocation error: %m");
222 /* Attach the segment to VLC */
223 void *shm = shmat (id, NULL, 0 /* read/write */);
224 if (-1 == (intptr_t)shm)
226 msg_Err (vd, "shared memory attachment error: %m");
227 shmctl (id, IPC_RMID, 0);
232 xcb_shm_seg_t segment;
235 /* Attach the segment to X */
236 xcb_void_cookie_t ck;
238 segment = xcb_generate_id (conn);
239 ck = xcb_shm_attach_checked (conn, segment, id, 1);
241 if (CheckError (vd, conn, "shared memory server-side error", ck))
243 msg_Info (vd, "using buggy X11 server - SSH proxying?");
250 shmctl (id, IPC_RMID, 0);
251 res->p_sys->segment = segment;
252 res->p->p_pixels = shm;
257 * Release picture private data: detach the shared memory segment.
259 void PictureResourceFree (picture_resource_t *res, xcb_connection_t *conn)
261 xcb_shm_seg_t segment = res->p_sys->segment;
265 assert (conn != NULL);
266 xcb_shm_detach (conn, segment);
268 shmdt (res->p->p_pixels);