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
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 <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 XID_TEXT N_("ID of the video output X window")
44 #define XID_LONGTEXT N_( \
45 "VLC can embed its video output in an existing X11 window. " \
46 "This is the X identifier of that window (0 means none).")
48 static int Open (vlc_object_t *);
49 static void Close (vlc_object_t *);
50 static int EmOpen (vlc_object_t *);
51 static void EmClose (vlc_object_t *);
57 set_shortname (N_("X window"))
58 set_description (N_("X11 video window (XCB)"))
59 set_category (CAT_VIDEO)
60 set_subcategory (SUBCAT_VIDEO_VOUT)
61 set_capability ("vout window xid", 10)
62 set_callbacks (Open, Close)
64 /* Obsolete since 1.1.0: */
65 add_obsolete_bool ("x11-altfullscreen")
66 add_obsolete_bool ("xvideo-altfullscreen")
67 add_obsolete_bool ("xvmc-altfullscreen")
68 add_obsolete_bool ("glx-altfullscreen")
71 set_shortname (N_("Drawable"))
72 set_description (N_("Embedded window video"))
73 set_category (CAT_VIDEO)
74 set_subcategory (SUBCAT_VIDEO_VOUT)
75 set_capability ("vout window xid", 70)
76 set_callbacks (EmOpen, EmClose)
78 add_integer ("drawable-xid", 0, NULL, XID_TEXT, XID_LONGTEXT, true)
83 static int Control (vout_window_t *, int, va_list ap);
84 static void *Thread (void *);
86 #define MATCHBOX_HACK 1 /* Matchbox focus hack */
88 struct vout_window_sys_t
90 xcb_connection_t *conn;
96 xcb_atom_t wm_state_above;
97 xcb_atom_t wm_state_below;
98 xcb_atom_t wm_state_fullscreen;
100 xcb_atom_t mb_current_app_window;
104 /** Set an X window property from a nul-terminated string */
106 void set_string (xcb_connection_t *conn, xcb_window_t window,
107 xcb_atom_t type, xcb_atom_t atom, const char *str)
109 xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, atom, type,
110 /* format */ 8, strlen (str), str);
113 /** Set an X window string property */
115 void set_ascii_prop (xcb_connection_t *conn, xcb_window_t window,
116 xcb_atom_t atom, const char *value)
118 set_string (conn, window, XA_STRING, atom, value);
122 void set_wm_hints (xcb_connection_t *conn, xcb_window_t window)
124 static const uint32_t wm_hints[8] = {
125 3, /* flags: Input, Initial state */
127 1, /* initial state: Normal */
128 0, 0, 0, 0, 0, /* Icon */
130 xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, XA_WM_HINTS,
131 XA_WM_HINTS, 32, 8, wm_hints);
134 /** Set the Window ICCCM client machine property */
136 void set_hostname_prop (xcb_connection_t *conn, xcb_window_t window)
139 long host_name_max = sysconf (_SC_HOST_NAME_MAX);
140 if (host_name_max <= 0) host_name_max = _POSIX_HOST_NAME_MAX;
141 hostname = malloc (host_name_max);
142 if(!hostname) return;
144 if (gethostname (hostname, host_name_max) == 0)
146 hostname[host_name_max - 1] = '\0';
147 set_ascii_prop (conn, window, XA_WM_CLIENT_MACHINE, hostname);
152 /** Request the X11 server to internalize a string into an atom */
154 xcb_intern_atom_cookie_t intern_string (xcb_connection_t *c, const char *s)
156 return xcb_intern_atom (c, 0, strlen (s), s);
159 /** Extract the X11 atom from an intern request cookie */
161 xcb_atom_t get_atom (xcb_connection_t *conn, xcb_intern_atom_cookie_t ck)
163 xcb_intern_atom_reply_t *reply;
166 reply = xcb_intern_atom_reply (conn, ck, NULL);
175 #define NET_WM_STATE_REMOVE 0
176 #define NET_WM_STATE_ADD 1
177 #define NET_WM_STATE_TOGGLE 2
179 static void CacheAtoms (vout_window_sys_t *p_sys)
181 xcb_connection_t *conn = p_sys->conn;
182 xcb_intern_atom_cookie_t wm_state_ck, wm_state_above_ck,
183 wm_state_below_ck, wm_state_fs_ck;
185 wm_state_ck = intern_string (conn, "_NET_WM_STATE");
186 wm_state_above_ck = intern_string (conn, "_NET_WM_STATE_ABOVE");
187 wm_state_below_ck = intern_string (conn, "_NET_WM_STATE_BELOW");
188 wm_state_fs_ck = intern_string (conn, "_NET_WM_STATE_FULLSCREEN");
190 xcb_intern_atom_cookie_t mb_current_app_window;
191 mb_current_app_window = xcb_intern_atom (conn, true,
192 strlen ("_MB_CURRENT_APP_WINDOW"),
193 "_MB_CURRENT_APP_WINDOW");
196 p_sys->wm_state = get_atom (conn, wm_state_ck);
197 p_sys->wm_state_above = get_atom (conn, wm_state_above_ck);
198 p_sys->wm_state_below = get_atom (conn, wm_state_below_ck);
199 p_sys->wm_state_fullscreen = get_atom (conn, wm_state_fs_ck);
201 p_sys->mb_current_app_window = get_atom (conn, mb_current_app_window);
206 * Create an X11 window.
208 static int Open (vlc_object_t *obj)
210 vout_window_t *wnd = (vout_window_t *)obj;
211 xcb_generic_error_t *err;
212 xcb_void_cookie_t ck;
214 vout_window_sys_t *p_sys = malloc (sizeof (*p_sys));
219 char *display = var_CreateGetNonEmptyString (wnd, "x11-display");
222 xcb_connection_t *conn = xcb_connect (display, &snum);
223 if (xcb_connection_has_error (conn) /*== NULL*/)
226 /* Find configured screen */
227 const xcb_setup_t *setup = xcb_get_setup (conn);
228 const xcb_screen_t *scr = NULL;
229 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
230 i.rem > 0; xcb_screen_next (&i))
241 msg_Err (wnd, "bad X11 screen number");
246 const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
247 uint32_t values[2] = {
248 /* XCB_CW_BACK_PIXEL */
250 /* XCB_CW_EVENT_MASK */
251 XCB_EVENT_MASK_KEY_PRESS,
254 xcb_window_t window = xcb_generate_id (conn);
255 ck = xcb_create_window_checked (conn, scr->root_depth, window, scr->root,
256 0, 0, wnd->cfg->width, wnd->cfg->height, 0,
257 XCB_WINDOW_CLASS_INPUT_OUTPUT,
258 scr->root_visual, mask, values);
259 err = xcb_request_check (conn, ck);
262 msg_Err (wnd, "creating window: X11 error %d", err->error_code);
267 wnd->handle.xid = window;
268 wnd->x11_display = display;
269 wnd->control = Control;
273 if (var_CreateGetBool (obj, "keyboard-events"))
274 p_sys->keys = CreateKeyHandler (obj, conn);
277 p_sys->root = scr->root;
280 * No cut&paste nor drag&drop, only Window Manager communication. */
281 /* Plain ASCII localization of VLC for ICCCM window name */
282 set_ascii_prop (conn, window, XA_WM_NAME,
283 vlc_pgettext ("ASCII", "VLC media player"));
284 set_ascii_prop (conn, window, XA_WM_ICON_NAME,
285 vlc_pgettext ("ASCII", "VLC"));
286 set_wm_hints (conn, window);
287 xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, XA_WM_CLASS,
288 XA_STRING, 8, 8, "vlc\0Vlc");
289 set_hostname_prop (conn, window);
292 xcb_intern_atom_cookie_t utf8_string_ck
293 = intern_string (conn, "UTF8_STRING");;
294 xcb_intern_atom_cookie_t net_wm_name_ck
295 = intern_string (conn, "_NET_WM_NAME");
296 xcb_intern_atom_cookie_t net_wm_icon_name_ck
297 = intern_string (conn, "_NET_WM_ICON_NAME");
298 xcb_intern_atom_cookie_t wm_window_role_ck
299 = intern_string (conn, "WM_WINDOW_ROLE");
301 xcb_atom_t utf8 = get_atom (conn, utf8_string_ck);
303 xcb_atom_t net_wm_name = get_atom (conn, net_wm_name_ck);
304 char *title = var_CreateGetNonEmptyString (wnd, "video-title");
307 set_string (conn, window, utf8, net_wm_name, title);
311 set_string (conn, window, utf8, net_wm_name, _("VLC media player"));
313 xcb_atom_t net_wm_icon_name = get_atom (conn, net_wm_icon_name_ck);
314 set_string (conn, window, utf8, net_wm_icon_name, _("VLC"));
316 xcb_atom_t wm_window_role = get_atom (conn, wm_window_role_ck);
317 set_ascii_prop (conn, window, wm_window_role, "vlc-video");
319 /* Cache any EWMH atom we may need later */
322 if (p_sys->mb_current_app_window)
324 uint32_t value = XCB_EVENT_MASK_PROPERTY_CHANGE;
325 xcb_change_window_attributes (conn, scr->root,
326 XCB_CW_EVENT_MASK, &value);
330 /* Make the window visible */
331 xcb_map_window (conn, window);
333 if (var_CreateGetBool (obj, "video-wallpaper"))
335 vout_window_SetState (wnd, VOUT_WINDOW_STATE_BELOW);
336 vout_window_SetFullScreen (wnd, true);
339 /* Create the event thread. It will dequeue all events, so any checked
340 * request from this thread must be completed at this point. */
341 if ((p_sys->keys != NULL)
342 && vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
343 DestroyKeyHandler (p_sys->keys);
346 if (p_sys->mb_current_app_window)
347 xcb_set_input_focus (p_sys->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
348 wnd->handle.xid, XCB_CURRENT_TIME);
350 xcb_flush (conn); /* Make sure map_window is sent (should be useless) */
354 xcb_disconnect (conn);
362 * Destroys the X11 window.
364 static void Close (vlc_object_t *obj)
366 vout_window_t *wnd = (vout_window_t *)obj;
367 vout_window_sys_t *p_sys = wnd->sys;
368 xcb_connection_t *conn = p_sys->conn;
372 vlc_cancel (p_sys->thread);
373 vlc_join (p_sys->thread, NULL);
374 DestroyKeyHandler (p_sys->keys);
376 xcb_disconnect (conn);
377 free (wnd->x11_display);
382 /** Background thread for X11 events handling */
383 static void *Thread (void *data)
385 vout_window_t *wnd = data;
386 vout_window_sys_t *p_sys = wnd->sys;
387 xcb_connection_t *conn = p_sys->conn;
389 int fd = xcb_get_file_descriptor (conn);
395 xcb_generic_event_t *ev;
396 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
400 int canc = vlc_savecancel ();
401 while ((ev = xcb_poll_for_event (conn)) != NULL)
403 if (ProcessKeyEvent (p_sys->keys, ev) == 0)
406 if (p_sys->mb_current_app_window
407 && (ev->response_type & 0x7f) == XCB_PROPERTY_NOTIFY)
409 const xcb_property_notify_event_t *pne =
410 (xcb_property_notify_event_t *)ev;
411 if (pne->atom == p_sys->mb_current_app_window
412 && pne->state == XCB_PROPERTY_NEW_VALUE)
414 xcb_get_property_reply_t *r =
415 xcb_get_property_reply (conn,
416 xcb_get_property (conn, 0, pne->window, pne->atom,
417 XA_WINDOW, 0, 4), NULL);
419 && !memcmp (xcb_get_property_value (r), &wnd->handle.xid,
422 msg_Dbg (wnd, "asking Matchbox for input focus");
423 xcb_set_input_focus (conn,
424 XCB_INPUT_FOCUS_POINTER_ROOT,
425 wnd->handle.xid, pne->time);
433 msg_Dbg (wnd, "unhandled event: %"PRIu8, ev->response_type);
436 vlc_restorecancel (canc);
438 if (xcb_connection_has_error (conn))
440 msg_Err (wnd, "X server failure");
447 /** Changes the EWMH state of the window */
448 static void set_wm_state (vout_window_t *wnd, bool on, xcb_atom_t state)
450 vout_window_sys_t *sys = wnd->sys;
451 /* From EWMH "_WM_STATE" */
452 xcb_client_message_event_t ev = {
453 .response_type = XCB_CLIENT_MESSAGE,
455 .window = wnd->handle.xid,
456 .type = sys->wm_state,
459 ev.data.data32[0] = on ? NET_WM_STATE_ADD : NET_WM_STATE_REMOVE;
460 ev.data.data32[1] = state;
461 ev.data.data32[2] = 0;
462 ev.data.data32[3] = 1;
464 /* From ICCCM "Changing Window State" */
465 xcb_send_event (sys->conn, 0, sys->root,
466 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
467 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
472 static int Control (vout_window_t *wnd, int cmd, va_list ap)
474 vout_window_sys_t *p_sys = wnd->sys;
475 xcb_connection_t *conn = p_sys->conn;
479 case VOUT_WINDOW_SET_SIZE:
481 unsigned width = va_arg (ap, unsigned);
482 unsigned height = va_arg (ap, unsigned);
483 const uint32_t values[] = { width, height, };
485 xcb_configure_window (conn, wnd->handle.xid,
486 XCB_CONFIG_WINDOW_WIDTH |
487 XCB_CONFIG_WINDOW_HEIGHT, values);
491 case VOUT_WINDOW_SET_STATE:
493 unsigned state = va_arg (ap, unsigned);
494 bool above = (state & VOUT_WINDOW_STATE_ABOVE) != 0;
495 bool below = (state & VOUT_WINDOW_STATE_BELOW) != 0;
497 set_wm_state (wnd, above, p_sys->wm_state_above);
498 set_wm_state (wnd, below, p_sys->wm_state_below);
502 case VOUT_WINDOW_SET_FULLSCREEN:
504 bool fs = va_arg (ap, int);
505 if (!fs && var_GetBool (wnd, "video-wallpaper"))
507 set_wm_state (wnd, fs, p_sys->wm_state_fullscreen);
512 msg_Err (wnd, "request %d not implemented", cmd);
515 xcb_flush (p_sys->conn);
519 /*** Embedded drawable support ***/
521 static vlc_mutex_t serializer = VLC_STATIC_MUTEX;
523 /** Acquire a drawable */
524 static int AcquireDrawable (vlc_object_t *obj, xcb_window_t window)
529 if (var_Create (obj->p_libvlc, "xid-in-use", VLC_VAR_ADDRESS))
532 /* Keep a list of busy drawables, so we don't overlap videos if there are
533 * more than one video track in the stream. */
534 vlc_mutex_lock (&serializer);
535 used = var_GetAddress (obj->p_libvlc, "xid-in-use");
540 if (used[n] == window)
546 used = realloc (used, sizeof (*used) * (n + 2));
551 var_SetAddress (obj->p_libvlc, "xid-in-use", used);
556 msg_Warn (obj, "X11 drawable 0x%08"PRIx8" is busy", window);
559 vlc_mutex_unlock (&serializer);
561 return (window == 0) ? VLC_EGENERIC : VLC_SUCCESS;
564 /** Remove this drawable from the list of busy ones */
565 static void ReleaseDrawable (vlc_object_t *obj, xcb_window_t window)
570 vlc_mutex_lock (&serializer);
571 used = var_GetAddress (obj->p_libvlc, "xid-in-use");
573 while (used[n] != window)
579 used[n] = used[n + 1];
583 var_SetAddress (obj->p_libvlc, "xid-in-use", NULL);
584 vlc_mutex_unlock (&serializer);
588 /* Variables are reference-counted... */
589 var_Destroy (obj->p_libvlc, "xid-in-use");
593 * Wrap an existing X11 window to embed the video.
595 static int EmOpen (vlc_object_t *obj)
597 vout_window_t *wnd = (vout_window_t *)obj;
599 xcb_window_t window = var_CreateGetInteger (obj, "drawable-xid");
602 var_Destroy (obj, "drawable-xid");
604 if (AcquireDrawable (obj, window))
607 vout_window_sys_t *p_sys = malloc (sizeof (*p_sys));
608 xcb_connection_t *conn = xcb_connect (NULL, NULL);
609 if (p_sys == NULL || xcb_connection_has_error (conn))
612 wnd->handle.xid = window;
613 wnd->control = Control;
618 xcb_get_geometry_reply_t *geo =
619 xcb_get_geometry_reply (conn, xcb_get_geometry (conn, window), NULL);
622 msg_Err (obj, "bad X11 window 0x%08"PRIx8, window);
625 p_sys->root = geo->root;
628 if (var_CreateGetBool (obj, "keyboard-events"))
630 p_sys->keys = CreateKeyHandler (obj, conn);
631 if (p_sys->keys != NULL)
633 const uint32_t mask = XCB_CW_EVENT_MASK;
634 const uint32_t values[1] = {
635 XCB_EVENT_MASK_KEY_PRESS,
637 xcb_change_window_attributes (conn, window, mask, values);
642 if ((p_sys->keys != NULL)
643 && vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
644 DestroyKeyHandler (p_sys->keys);
651 xcb_disconnect (conn);
653 ReleaseDrawable (obj, window);
657 static void EmClose (vlc_object_t *obj)
659 vout_window_t *wnd = (vout_window_t *)obj;
660 xcb_window_t window = wnd->handle.xid;
663 ReleaseDrawable (obj, window);