3 * @brief X C Bindings window provider module for VLC media player
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 <unistd.h> /* gethostname() and sysconf() */
31 #include <limits.h> /* _POSIX_HOST_NAME_MAX */
34 typedef xcb_atom_t Atom;
35 #include <X11/Xatom.h> /* XA_WM_NAME */
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_vout_window.h>
43 #define DISPLAY_TEXT N_("X11 display")
44 #define DISPLAY_LONGTEXT N_( \
45 "X11 hardware display to use. By default VLC will " \
46 "use the value of the DISPLAY environment variable.")
48 #define XID_TEXT N_("ID of the video output X window")
49 #define XID_LONGTEXT N_( \
50 "VLC can embed its video output in an existing X11 window. " \
51 "This is the X identifier of that window (0 means none).")
53 static int Open (vlc_object_t *);
54 static void Close (vlc_object_t *);
55 static int EmOpen (vlc_object_t *);
56 static void EmClose (vlc_object_t *);
62 set_shortname (N_("X window"))
63 set_description (N_("X11 video window (XCB)"))
64 set_category (CAT_VIDEO)
65 set_subcategory (SUBCAT_VIDEO_VOUT)
66 set_capability ("vout window xid", 10)
67 set_callbacks (Open, Close)
69 add_string ("x11-display", NULL, NULL,
70 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
71 /* Obsolete since 1.1.0: */
72 add_obsolete_bool ("x11-altfullscreen")
73 add_obsolete_bool ("xvideo-altfullscreen")
74 add_obsolete_bool ("xvmc-altfullscreen")
75 add_obsolete_bool ("glx-altfullscreen")
78 set_shortname (N_("Drawable"))
79 set_description (N_("Embedded window video"))
80 set_category (CAT_VIDEO)
81 set_subcategory (SUBCAT_VIDEO_VOUT)
82 set_capability ("vout window xid", 70)
83 set_callbacks (EmOpen, EmClose)
85 add_integer ("drawable-xid", 0, NULL, XID_TEXT, XID_LONGTEXT, true)
90 static int Control (vout_window_t *, int, va_list ap);
91 static void *Thread (void *);
93 struct vout_window_sys_t
95 xcb_connection_t *conn;
101 xcb_atom_t wm_state_above;
102 xcb_atom_t wm_state_fullscreen;
105 /** Set an X window property from a nul-terminated string */
107 void set_string (xcb_connection_t *conn, xcb_window_t window,
108 xcb_atom_t type, xcb_atom_t atom, const char *str)
110 xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, atom, type,
111 /* format */ 8, strlen (str), str);
114 /** Set an X window string property */
116 void set_ascii_prop (xcb_connection_t *conn, xcb_window_t window,
117 xcb_atom_t atom, const char *value)
119 set_string (conn, window, atom, XA_STRING, value);
122 /** Set the Window ICCCM client machine property */
124 void set_hostname_prop (xcb_connection_t *conn, xcb_window_t window)
127 long host_name_max = sysconf (_SC_HOST_NAME_MAX);
128 if (host_name_max <= 0) host_name_max = _POSIX_HOST_NAME_MAX;
129 hostname = malloc (host_name_max);
130 if(!hostname) return;
132 if (gethostname (hostname, host_name_max) == 0)
134 hostname[host_name_max - 1] = '\0';
135 set_ascii_prop (conn, window, XA_WM_CLIENT_MACHINE, hostname);
140 /** Request the X11 server to internalize a string into an atom */
142 xcb_intern_atom_cookie_t intern_string (xcb_connection_t *c, const char *s)
144 return xcb_intern_atom (c, 0, strlen (s), s);
147 /** Extract the X11 atom from an intern request cookie */
149 xcb_atom_t get_atom (xcb_connection_t *conn, xcb_intern_atom_cookie_t ck)
151 xcb_intern_atom_reply_t *reply;
154 reply = xcb_intern_atom_reply (conn, ck, NULL);
163 #define NET_WM_STATE_REMOVE 0
164 #define NET_WM_STATE_ADD 1
165 #define NET_WM_STATE_TOGGLE 2
167 static void CacheAtoms (vout_window_sys_t *p_sys)
169 xcb_connection_t *conn = p_sys->conn;
170 xcb_intern_atom_cookie_t wm_state_ck, wm_state_above_ck, wm_state_fs_ck;
172 wm_state_ck = intern_string (conn, "_NET_WM_STATE");
173 wm_state_above_ck = intern_string (conn, "_NET_WM_STATE_ABOVE");
174 wm_state_fs_ck = intern_string (conn, "_NET_WM_STATE_FULLSCREEN");
176 p_sys->wm_state = get_atom (conn, wm_state_ck);
177 p_sys->wm_state_above = get_atom (conn, wm_state_above_ck);
178 p_sys->wm_state_fullscreen = get_atom (conn, wm_state_fs_ck);
182 * Create an X11 window.
184 static int Open (vlc_object_t *obj)
186 vout_window_t *wnd = (vout_window_t *)obj;
187 xcb_generic_error_t *err;
188 xcb_void_cookie_t ck;
190 vout_window_sys_t *p_sys = malloc (sizeof (*p_sys));
195 char *display = var_CreateGetNonEmptyString (wnd, "x11-display");
198 xcb_connection_t *conn = xcb_connect (display, &snum);
200 if (xcb_connection_has_error (conn) /*== NULL*/)
203 /* Find configured screen */
204 const xcb_setup_t *setup = xcb_get_setup (conn);
205 const xcb_screen_t *scr = NULL;
206 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
207 i.rem > 0; xcb_screen_next (&i))
218 msg_Err (wnd, "bad X11 screen number");
223 const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
224 uint32_t values[2] = {
225 /* XCB_CW_BACK_PIXEL */
227 /* XCB_CW_EVENT_MASK */
228 XCB_EVENT_MASK_KEY_PRESS,
231 xcb_window_t window = xcb_generate_id (conn);
232 ck = xcb_create_window_checked (conn, scr->root_depth, window, scr->root,
233 0, 0, wnd->cfg->width, wnd->cfg->height, 0,
234 XCB_WINDOW_CLASS_INPUT_OUTPUT,
235 scr->root_visual, mask, values);
236 err = xcb_request_check (conn, ck);
239 msg_Err (wnd, "creating window: X11 error %d", err->error_code);
244 wnd->handle.xid = window;
245 wnd->control = Control;
249 p_sys->keys = CreateKeyHandler (obj, conn);
250 p_sys->root = scr->root;
253 * No cut&paste nor drag&drop, only Window Manager communication. */
254 /* Plain ASCII localization of VLC for ICCCM window name */
255 set_ascii_prop (conn, window, XA_WM_NAME,
256 vlc_pgettext ("ASCII", "VLC media player"));
257 set_ascii_prop (conn, window, XA_WM_ICON_NAME,
258 vlc_pgettext ("ASCII", "VLC"));
259 xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, XA_WM_CLASS,
260 XA_STRING, 8, 8, "vlc\0Vlc");
261 set_hostname_prop (conn, window);
264 xcb_intern_atom_cookie_t utf8_string_ck
265 = intern_string (conn, "UTF8_STRING");;
266 xcb_intern_atom_cookie_t net_wm_name_ck
267 = intern_string (conn, "_NET_WM_NAME");
268 xcb_intern_atom_cookie_t net_wm_icon_name_ck
269 = intern_string (conn, "_NET_WM_ICON_NAME");
270 xcb_intern_atom_cookie_t wm_window_role_ck
271 = intern_string (conn, "WM_WINDOW_ROLE");
273 xcb_atom_t utf8 = get_atom (conn, utf8_string_ck);
275 xcb_atom_t net_wm_name = get_atom (conn, net_wm_name_ck);
276 char *title = var_CreateGetNonEmptyString (wnd, "video-title");
279 set_string (conn, window, utf8, net_wm_name, title);
283 set_string (conn, window, utf8, net_wm_name, _("VLC media player"));
285 xcb_atom_t net_wm_icon_name = get_atom (conn, net_wm_icon_name_ck);
286 set_string (conn, window, utf8, net_wm_icon_name, _("VLC"));
288 xcb_atom_t wm_window_role = get_atom (conn, wm_window_role_ck);
289 set_ascii_prop (conn, window, wm_window_role, "vlc-video");
291 /* Make the window visible */
292 xcb_map_window (conn, window);
294 /* Cache any EWMH atom we may need later */
297 /* Create the event thread. It will dequeue all events, so any checked
298 * request from this thread must be completed at this point. */
299 if ((p_sys->keys != NULL)
300 && vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
301 DestroyKeyHandler (p_sys->keys);
303 xcb_flush (conn); /* Make sure map_window is sent (should be useless) */
308 xcb_disconnect (conn);
315 * Destroys the X11 window.
317 static void Close (vlc_object_t *obj)
319 vout_window_t *wnd = (vout_window_t *)obj;
320 vout_window_sys_t *p_sys = wnd->sys;
321 xcb_connection_t *conn = p_sys->conn;
325 vlc_cancel (p_sys->thread);
326 vlc_join (p_sys->thread, NULL);
327 DestroyKeyHandler (p_sys->keys);
329 xcb_disconnect (conn);
334 /** Background thread for X11 events handling */
335 static void *Thread (void *data)
337 vout_window_t *wnd = data;
338 vout_window_sys_t *p_sys = wnd->sys;
339 xcb_connection_t *conn = p_sys->conn;
341 int fd = xcb_get_file_descriptor (conn);
347 xcb_generic_event_t *ev;
348 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
352 int canc = vlc_savecancel ();
353 while ((ev = xcb_poll_for_event (conn)) != NULL)
355 if (ProcessKeyEvent (p_sys->keys, ev) == 0)
357 msg_Dbg (wnd, "unhandled event: %"PRIu8, ev->response_type);
360 vlc_restorecancel (canc);
362 if (xcb_connection_has_error (conn))
364 msg_Err (wnd, "X server failure");
371 /** Changes the EWMH state of the window */
372 static void set_wm_state (vout_window_t *wnd, bool on, xcb_atom_t state)
374 vout_window_sys_t *sys = wnd->sys;
375 /* From EWMH "_WM_STATE" */
376 xcb_client_message_event_t ev = {
377 .response_type = XCB_CLIENT_MESSAGE,
379 .window = wnd->handle.xid,
380 .type = sys->wm_state,
383 ev.data.data32[0] = on ? NET_WM_STATE_ADD : NET_WM_STATE_REMOVE;
384 ev.data.data32[1] = state;
385 ev.data.data32[2] = 0;
386 ev.data.data32[3] = 1;
388 /* From ICCCM "Changing Window State" */
389 xcb_send_event (sys->conn, 0, sys->root,
390 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
391 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
396 static int Control (vout_window_t *wnd, int cmd, va_list ap)
398 vout_window_sys_t *p_sys = wnd->sys;
399 xcb_connection_t *conn = p_sys->conn;
403 case VOUT_WINDOW_SET_SIZE:
405 unsigned width = va_arg (ap, unsigned);
406 unsigned height = va_arg (ap, unsigned);
407 const uint32_t values[] = { width, height, };
409 xcb_configure_window (conn, wnd->handle.xid,
410 XCB_CONFIG_WINDOW_WIDTH |
411 XCB_CONFIG_WINDOW_HEIGHT, values);
415 case VOUT_WINDOW_SET_ON_TOP:
416 set_wm_state (wnd, va_arg (ap, int), p_sys->wm_state_above);
419 case VOUT_WINDOW_SET_FULLSCREEN:
420 set_wm_state (wnd, va_arg (ap, int), p_sys->wm_state_fullscreen);
424 msg_Err (wnd, "request %d not implemented", cmd);
427 xcb_flush (p_sys->conn);
431 /*** Embedded drawable support ***/
433 static vlc_mutex_t serializer = VLC_STATIC_MUTEX;
435 /** Acquire a drawable */
436 static int AcquireDrawable (vlc_object_t *obj, xcb_window_t window)
442 if (var_Create (obj->p_libvlc, "xid-in-use", VLC_VAR_ADDRESS))
445 /* Keep a list of busy drawables, so we don't overlap videos if there are
446 * more than one video track in the stream. */
447 vlc_mutex_lock (&serializer);
448 var_Get (VLC_OBJECT (obj->p_libvlc), "xid-in-use", &val);
449 used = val.p_address;
454 if (used[n] == window)
460 used = realloc (used, sizeof (*used) * (n + 2));
465 val.p_address = used;
466 var_Set (obj->p_libvlc, "xid-in-use", val);
471 msg_Warn (obj, "X11 drawable 0x%08"PRIx8" is busy", window);
474 vlc_mutex_unlock (&serializer);
476 return (window == 0) ? VLC_EGENERIC : VLC_SUCCESS;
479 /** Remove this drawable from the list of busy ones */
480 static void ReleaseDrawable (vlc_object_t *obj, xcb_window_t window)
486 vlc_mutex_lock (&serializer);
487 var_Get (VLC_OBJECT (obj->p_libvlc), "xid-in-use", &val);
488 used = val.p_address;
490 while (used[n] != window)
496 used[n] = used[n + 1];
500 var_SetAddress (obj->p_libvlc, "xid-in-use", NULL);
501 vlc_mutex_unlock (&serializer);
505 /* Variables are reference-counted... */
506 var_Destroy (obj->p_libvlc, "xid-in-use");
510 * Wrap an existing X11 window to embed the video.
512 static int EmOpen (vlc_object_t *obj)
514 vout_window_t *wnd = (vout_window_t *)obj;
516 xcb_window_t window = var_CreateGetInteger (obj, "drawable-xid");
519 var_Destroy (obj, "drawable-xid");
521 if (AcquireDrawable (obj, window))
524 vout_window_sys_t *p_sys = malloc (sizeof (*p_sys));
525 xcb_connection_t *conn = xcb_connect (NULL, NULL);
526 if (p_sys == NULL || xcb_connection_has_error (conn))
529 wnd->handle.xid = window;
530 wnd->control = Control;
535 xcb_get_geometry_reply_t *geo =
536 xcb_get_geometry_reply (conn, xcb_get_geometry (conn, window), NULL);
539 msg_Err (obj, "bad X11 window 0x%08"PRIx8, window);
542 p_sys->root = geo->root;
545 if (var_CreateGetInteger (obj, "vout-event") != 3) /* FIXME: <- cleanup */
547 p_sys->keys = CreateKeyHandler (obj, conn);
548 if (p_sys->keys != NULL)
550 const uint32_t mask = XCB_CW_EVENT_MASK;
551 const uint32_t values[1] = {
552 XCB_EVENT_MASK_KEY_PRESS,
554 xcb_change_window_attributes (conn, window, mask, values);
559 if ((p_sys->keys != NULL)
560 && vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
561 DestroyKeyHandler (p_sys->keys);
568 xcb_disconnect (conn);
570 ReleaseDrawable (obj, window);
574 static void EmClose (vlc_object_t *obj)
576 vout_window_t *wnd = (vout_window_t *)obj;
577 xcb_window_t window = wnd->handle.xid;
580 ReleaseDrawable (obj, window);