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 PictureAlloc (vout_thread_t *vout, picture_t *pic, size_t size,
168 xcb_connection_t *conn)
170 assert (pic->i_status == FREE_PICTURE);
172 /* Allocate shared memory segment */
173 int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0700);
176 msg_Err (vout, "shared memory allocation error: %m");
180 /* Attach the segment to VLC */
181 void *shm = shmat (id, NULL, 0 /* read/write */);
182 if (-1 == (intptr_t)shm)
184 msg_Err (vout, "shared memory attachment error: %m");
185 shmctl (id, IPC_RMID, 0);
189 xcb_shm_seg_t segment;
192 /* Attach the segment to X */
193 xcb_void_cookie_t ck;
195 segment = xcb_generate_id (conn);
196 ck = xcb_shm_attach_checked (conn, segment, id, 1);
198 if (CheckError (vout, "shared memory server-side error", ck))
200 msg_Info (vout, "using buggy X11 server - SSH proxying?");
207 shmctl (id, IPC_RMID, 0);
208 pic->p_sys = (void *)(uintptr_t)segment;
209 pic->p->p_pixels = shm;
210 pic->i_status = DESTROYED_PICTURE;
211 pic->i_type = DIRECT_PICTURE;
216 * Release picture private data: detach the shared memory segment.
218 void PictureFree (picture_t *pic, xcb_connection_t *conn)
220 xcb_shm_seg_t segment = (uintptr_t)pic->p_sys;
224 assert (conn != NULL);
225 xcb_shm_detach (conn, segment);
227 shmdt (pic->p->p_pixels);
231 * Video output thread management stuff.
232 * FIXME: Much of this should move to core
234 void CommonManage (vout_thread_t *vout)
236 if (vout->i_changes & VOUT_SCALE_CHANGE)
238 vout->b_autoscale = var_GetBool (vout, "autoscale");
239 vout->i_zoom = ZOOM_FP_FACTOR;
240 vout->i_changes &= ~VOUT_SCALE_CHANGE;
241 vout->i_changes |= VOUT_SIZE_CHANGE;
244 if (vout->i_changes & VOUT_ZOOM_CHANGE)
246 vout->b_autoscale = false;
247 vout->i_zoom = var_GetFloat (vout, "scale") * ZOOM_FP_FACTOR;
248 vout->i_changes &= ~VOUT_ZOOM_CHANGE;
249 vout->i_changes |= VOUT_SIZE_CHANGE;
252 if (vout->i_changes & VOUT_CROP_CHANGE)
254 vout->fmt_out.i_x_offset = vout->fmt_in.i_x_offset;
255 vout->fmt_out.i_y_offset = vout->fmt_in.i_y_offset;
256 vout->fmt_out.i_visible_width = vout->fmt_in.i_visible_width;
257 vout->fmt_out.i_visible_height = vout->fmt_in.i_visible_height;
258 vout->i_changes &= ~VOUT_CROP_CHANGE;
259 vout->i_changes |= VOUT_SIZE_CHANGE;
262 if (vout->i_changes & VOUT_ASPECT_CHANGE)
264 vout->fmt_out.i_aspect = vout->fmt_in.i_aspect;
265 vout->fmt_out.i_sar_num = vout->fmt_in.i_sar_num;
266 vout->fmt_out.i_sar_den = vout->fmt_in.i_sar_den;
267 vout->output.i_aspect = vout->fmt_in.i_aspect;
268 vout->i_changes &= ~VOUT_ASPECT_CHANGE;
269 vout->i_changes |= VOUT_SIZE_CHANGE;