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
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 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,
96 uint8_t *restrict pdepth,
101 vout_window_cfg_t wnd_cfg;
103 memset( &wnd_cfg, 0, sizeof(wnd_cfg) );
104 wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
105 wnd_cfg.width = vd->cfg->display.width;
106 wnd_cfg.height = vd->cfg->display.height;
108 vout_window_t *wnd = vout_display_NewWindow (vd, &wnd_cfg);
111 msg_Err (vd, "parent window not available");
116 xcb_get_geometry_reply_t *geo;
117 xcb_get_geometry_cookie_t ck;
119 ck = xcb_get_geometry (conn, wnd->xid);
120 geo = xcb_get_geometry_reply (conn, ck, NULL);
123 msg_Err (vd, "parent window not valid");
127 *pdepth = geo->depth;
130 /* Subscribe to parent window resize events */
131 uint32_t value = XCB_EVENT_MASK_POINTER_MOTION
132 | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
133 xcb_change_window_attributes (conn, wnd->xid,
134 XCB_CW_EVENT_MASK, &value);
135 /* Try to subscribe to click events */
136 /* (only one X11 client can get them, so might not work) */
137 if (var_CreateGetBool (vd, "mouse-events"))
139 value |= XCB_EVENT_MASK_BUTTON_PRESS
140 | XCB_EVENT_MASK_BUTTON_RELEASE;
141 xcb_change_window_attributes (conn, wnd->xid,
142 XCB_CW_EVENT_MASK, &value);
146 /* Find the selected screen */
147 const xcb_setup_t *setup = xcb_get_setup (conn);
148 const xcb_screen_t *screen = NULL;
149 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
150 i.rem > 0 && screen == NULL; xcb_screen_next (&i))
152 if (i.data->root == root)
158 msg_Err (vd, "parent window screen not found");
161 msg_Dbg (vd, "using screen 0x%"PRIx32, root);
163 /* Check MIT-SHM shared memory support */
164 bool shm = var_CreateGetBool (vd, "x11-shm") > 0;
167 xcb_shm_query_version_cookie_t ck;
168 xcb_shm_query_version_reply_t *r;
170 ck = xcb_shm_query_version (conn);
171 r = xcb_shm_query_version_reply (conn, ck, NULL);
174 msg_Err (vd, "shared memory (MIT-SHM) not available");
175 msg_Warn (vd, "display will be slow");
186 vout_display_DeleteWindow (vd, wnd);
191 * Gets the size of an X window.
193 int GetWindowSize (struct vout_window_t *wnd, xcb_connection_t *conn,
194 unsigned *restrict width, unsigned *restrict height)
196 xcb_get_geometry_cookie_t ck = xcb_get_geometry (conn, wnd->xid);
197 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, ck, NULL);
203 *height = geo->height;
209 * Create a blank cursor.
210 * Note that the pixmaps are leaked (until the X disconnection). Hence, this
211 * function should be called no more than once per X connection.
212 * @param conn XCB connection
213 * @param scr target XCB screen
215 xcb_cursor_t CreateBlankCursor (xcb_connection_t *conn,
216 const xcb_screen_t *scr)
218 xcb_cursor_t cur = xcb_generate_id (conn);
219 xcb_pixmap_t pix = xcb_generate_id (conn);
221 xcb_create_pixmap (conn, 1, pix, scr->root, 1, 1);
222 xcb_create_cursor (conn, cur, pix, pix, 0, 0, 0, 1, 1, 1, 0, 0);
227 * Initialize a picture buffer as shared memory, according to the video output
228 * format. If a attach is true, the segment is attached to
229 * the X server (MIT-SHM extension).
231 int PictureResourceAlloc (vout_display_t *vd, picture_resource_t *res, size_t size,
232 xcb_connection_t *conn, bool attach)
234 res->p_sys = malloc (sizeof(*res->p_sys));
238 /* Allocate shared memory segment */
239 int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
242 msg_Err (vd, "shared memory allocation error: %m");
247 /* Attach the segment to VLC */
248 void *shm = shmat (id, NULL, 0 /* read/write */);
249 if (-1 == (intptr_t)shm)
251 msg_Err (vd, "shared memory attachment error: %m");
252 shmctl (id, IPC_RMID, 0);
257 xcb_shm_seg_t segment;
260 /* Attach the segment to X */
261 xcb_void_cookie_t ck;
263 segment = xcb_generate_id (conn);
264 ck = xcb_shm_attach_checked (conn, segment, id, 1);
266 if (CheckError (vd, conn, "shared memory server-side error", ck))
268 msg_Info (vd, "using buggy X11 server - SSH proxying?");
275 shmctl (id, IPC_RMID, 0);
276 res->p_sys->segment = segment;
277 res->p->p_pixels = shm;
282 * Release picture private data: detach the shared memory segment.
284 void PictureResourceFree (picture_resource_t *res, xcb_connection_t *conn)
286 xcb_shm_seg_t segment = res->p_sys->segment;
288 if (conn != NULL && segment != 0)
289 xcb_shm_detach (conn, segment);
290 shmdt (res->p->p_pixels);