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, XA_STRING, atom, value);
123 void set_wm_hints (xcb_connection_t *conn, xcb_window_t window)
125 static const uint32_t wm_hints[8] = {
126 3, /* flags: Input, Initial state */
128 1, /* initial state: Normal */
129 0, 0, 0, 0, 0, /* Icon */
131 xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, XA_WM_HINTS,
132 XA_WM_HINTS, 32, 8, wm_hints);
135 /** Set the Window ICCCM client machine property */
137 void set_hostname_prop (xcb_connection_t *conn, xcb_window_t window)
140 long host_name_max = sysconf (_SC_HOST_NAME_MAX);
141 if (host_name_max <= 0) host_name_max = _POSIX_HOST_NAME_MAX;
142 hostname = malloc (host_name_max);
143 if(!hostname) return;
145 if (gethostname (hostname, host_name_max) == 0)
147 hostname[host_name_max - 1] = '\0';
148 set_ascii_prop (conn, window, XA_WM_CLIENT_MACHINE, hostname);
153 /** Request the X11 server to internalize a string into an atom */
155 xcb_intern_atom_cookie_t intern_string (xcb_connection_t *c, const char *s)
157 return xcb_intern_atom (c, 0, strlen (s), s);
160 /** Extract the X11 atom from an intern request cookie */
162 xcb_atom_t get_atom (xcb_connection_t *conn, xcb_intern_atom_cookie_t ck)
164 xcb_intern_atom_reply_t *reply;
167 reply = xcb_intern_atom_reply (conn, ck, NULL);
176 #define NET_WM_STATE_REMOVE 0
177 #define NET_WM_STATE_ADD 1
178 #define NET_WM_STATE_TOGGLE 2
180 static void CacheAtoms (vout_window_sys_t *p_sys)
182 xcb_connection_t *conn = p_sys->conn;
183 xcb_intern_atom_cookie_t wm_state_ck, wm_state_above_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_fs_ck = intern_string (conn, "_NET_WM_STATE_FULLSCREEN");
189 p_sys->wm_state = get_atom (conn, wm_state_ck);
190 p_sys->wm_state_above = get_atom (conn, wm_state_above_ck);
191 p_sys->wm_state_fullscreen = get_atom (conn, wm_state_fs_ck);
195 * Create an X11 window.
197 static int Open (vlc_object_t *obj)
199 vout_window_t *wnd = (vout_window_t *)obj;
200 xcb_generic_error_t *err;
201 xcb_void_cookie_t ck;
203 vout_window_sys_t *p_sys = malloc (sizeof (*p_sys));
208 char *display = var_CreateGetNonEmptyString (wnd, "x11-display");
211 xcb_connection_t *conn = xcb_connect (display, &snum);
213 if (xcb_connection_has_error (conn) /*== NULL*/)
216 /* Find configured screen */
217 const xcb_setup_t *setup = xcb_get_setup (conn);
218 const xcb_screen_t *scr = NULL;
219 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
220 i.rem > 0; xcb_screen_next (&i))
231 msg_Err (wnd, "bad X11 screen number");
236 const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
237 uint32_t values[2] = {
238 /* XCB_CW_BACK_PIXEL */
240 /* XCB_CW_EVENT_MASK */
241 XCB_EVENT_MASK_KEY_PRESS,
244 xcb_window_t window = xcb_generate_id (conn);
245 ck = xcb_create_window_checked (conn, scr->root_depth, window, scr->root,
246 0, 0, wnd->cfg->width, wnd->cfg->height, 0,
247 XCB_WINDOW_CLASS_INPUT_OUTPUT,
248 scr->root_visual, mask, values);
249 err = xcb_request_check (conn, ck);
252 msg_Err (wnd, "creating window: X11 error %d", err->error_code);
257 wnd->handle.xid = window;
258 wnd->control = Control;
262 p_sys->keys = CreateKeyHandler (obj, conn);
263 p_sys->root = scr->root;
266 * No cut&paste nor drag&drop, only Window Manager communication. */
267 /* Plain ASCII localization of VLC for ICCCM window name */
268 set_ascii_prop (conn, window, XA_WM_NAME,
269 vlc_pgettext ("ASCII", "VLC media player"));
270 set_ascii_prop (conn, window, XA_WM_ICON_NAME,
271 vlc_pgettext ("ASCII", "VLC"));
272 set_wm_hints (conn, window);
273 xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, XA_WM_CLASS,
274 XA_STRING, 8, 8, "vlc\0Vlc");
275 set_hostname_prop (conn, window);
278 xcb_intern_atom_cookie_t utf8_string_ck
279 = intern_string (conn, "UTF8_STRING");;
280 xcb_intern_atom_cookie_t net_wm_name_ck
281 = intern_string (conn, "_NET_WM_NAME");
282 xcb_intern_atom_cookie_t net_wm_icon_name_ck
283 = intern_string (conn, "_NET_WM_ICON_NAME");
284 xcb_intern_atom_cookie_t wm_window_role_ck
285 = intern_string (conn, "WM_WINDOW_ROLE");
287 xcb_atom_t utf8 = get_atom (conn, utf8_string_ck);
289 xcb_atom_t net_wm_name = get_atom (conn, net_wm_name_ck);
290 char *title = var_CreateGetNonEmptyString (wnd, "video-title");
293 set_string (conn, window, utf8, net_wm_name, title);
297 set_string (conn, window, utf8, net_wm_name, _("VLC media player"));
299 xcb_atom_t net_wm_icon_name = get_atom (conn, net_wm_icon_name_ck);
300 set_string (conn, window, utf8, net_wm_icon_name, _("VLC"));
302 xcb_atom_t wm_window_role = get_atom (conn, wm_window_role_ck);
303 set_ascii_prop (conn, window, wm_window_role, "vlc-video");
305 /* Make the window visible */
306 xcb_map_window (conn, window);
308 /* Cache any EWMH atom we may need later */
311 /* Create the event thread. It will dequeue all events, so any checked
312 * request from this thread must be completed at this point. */
313 if ((p_sys->keys != NULL)
314 && vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
315 DestroyKeyHandler (p_sys->keys);
317 xcb_flush (conn); /* Make sure map_window is sent (should be useless) */
322 xcb_disconnect (conn);
329 * Destroys the X11 window.
331 static void Close (vlc_object_t *obj)
333 vout_window_t *wnd = (vout_window_t *)obj;
334 vout_window_sys_t *p_sys = wnd->sys;
335 xcb_connection_t *conn = p_sys->conn;
339 vlc_cancel (p_sys->thread);
340 vlc_join (p_sys->thread, NULL);
341 DestroyKeyHandler (p_sys->keys);
343 xcb_disconnect (conn);
348 /** Background thread for X11 events handling */
349 static void *Thread (void *data)
351 vout_window_t *wnd = data;
352 vout_window_sys_t *p_sys = wnd->sys;
353 xcb_connection_t *conn = p_sys->conn;
355 int fd = xcb_get_file_descriptor (conn);
361 xcb_generic_event_t *ev;
362 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
366 int canc = vlc_savecancel ();
367 while ((ev = xcb_poll_for_event (conn)) != NULL)
369 if (ProcessKeyEvent (p_sys->keys, ev) == 0)
371 msg_Dbg (wnd, "unhandled event: %"PRIu8, ev->response_type);
374 vlc_restorecancel (canc);
376 if (xcb_connection_has_error (conn))
378 msg_Err (wnd, "X server failure");
385 /** Changes the EWMH state of the window */
386 static void set_wm_state (vout_window_t *wnd, bool on, xcb_atom_t state)
388 vout_window_sys_t *sys = wnd->sys;
389 /* From EWMH "_WM_STATE" */
390 xcb_client_message_event_t ev = {
391 .response_type = XCB_CLIENT_MESSAGE,
393 .window = wnd->handle.xid,
394 .type = sys->wm_state,
397 ev.data.data32[0] = on ? NET_WM_STATE_ADD : NET_WM_STATE_REMOVE;
398 ev.data.data32[1] = state;
399 ev.data.data32[2] = 0;
400 ev.data.data32[3] = 1;
402 /* From ICCCM "Changing Window State" */
403 xcb_send_event (sys->conn, 0, sys->root,
404 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
405 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
410 static int Control (vout_window_t *wnd, int cmd, va_list ap)
412 vout_window_sys_t *p_sys = wnd->sys;
413 xcb_connection_t *conn = p_sys->conn;
417 case VOUT_WINDOW_SET_SIZE:
419 unsigned width = va_arg (ap, unsigned);
420 unsigned height = va_arg (ap, unsigned);
421 const uint32_t values[] = { width, height, };
423 xcb_configure_window (conn, wnd->handle.xid,
424 XCB_CONFIG_WINDOW_WIDTH |
425 XCB_CONFIG_WINDOW_HEIGHT, values);
429 case VOUT_WINDOW_SET_ON_TOP:
430 set_wm_state (wnd, va_arg (ap, int), p_sys->wm_state_above);
433 case VOUT_WINDOW_SET_FULLSCREEN:
434 set_wm_state (wnd, va_arg (ap, int), p_sys->wm_state_fullscreen);
438 msg_Err (wnd, "request %d not implemented", cmd);
441 xcb_flush (p_sys->conn);
445 /*** Embedded drawable support ***/
447 static vlc_mutex_t serializer = VLC_STATIC_MUTEX;
449 /** Acquire a drawable */
450 static int AcquireDrawable (vlc_object_t *obj, xcb_window_t window)
456 if (var_Create (obj->p_libvlc, "xid-in-use", VLC_VAR_ADDRESS))
459 /* Keep a list of busy drawables, so we don't overlap videos if there are
460 * more than one video track in the stream. */
461 vlc_mutex_lock (&serializer);
462 var_Get (VLC_OBJECT (obj->p_libvlc), "xid-in-use", &val);
463 used = val.p_address;
468 if (used[n] == window)
474 used = realloc (used, sizeof (*used) * (n + 2));
479 val.p_address = used;
480 var_Set (obj->p_libvlc, "xid-in-use", val);
485 msg_Warn (obj, "X11 drawable 0x%08"PRIx8" is busy", window);
488 vlc_mutex_unlock (&serializer);
490 return (window == 0) ? VLC_EGENERIC : VLC_SUCCESS;
493 /** Remove this drawable from the list of busy ones */
494 static void ReleaseDrawable (vlc_object_t *obj, xcb_window_t window)
500 vlc_mutex_lock (&serializer);
501 var_Get (VLC_OBJECT (obj->p_libvlc), "xid-in-use", &val);
502 used = val.p_address;
504 while (used[n] != window)
510 used[n] = used[n + 1];
514 var_SetAddress (obj->p_libvlc, "xid-in-use", NULL);
515 vlc_mutex_unlock (&serializer);
519 /* Variables are reference-counted... */
520 var_Destroy (obj->p_libvlc, "xid-in-use");
524 * Wrap an existing X11 window to embed the video.
526 static int EmOpen (vlc_object_t *obj)
528 vout_window_t *wnd = (vout_window_t *)obj;
530 xcb_window_t window = var_CreateGetInteger (obj, "drawable-xid");
533 var_Destroy (obj, "drawable-xid");
535 if (AcquireDrawable (obj, window))
538 vout_window_sys_t *p_sys = malloc (sizeof (*p_sys));
539 xcb_connection_t *conn = xcb_connect (NULL, NULL);
540 if (p_sys == NULL || xcb_connection_has_error (conn))
543 wnd->handle.xid = window;
544 wnd->control = Control;
549 xcb_get_geometry_reply_t *geo =
550 xcb_get_geometry_reply (conn, xcb_get_geometry (conn, window), NULL);
553 msg_Err (obj, "bad X11 window 0x%08"PRIx8, window);
556 p_sys->root = geo->root;
559 if (var_CreateGetInteger (obj, "vout-event") != 3) /* FIXME: <- cleanup */
561 p_sys->keys = CreateKeyHandler (obj, conn);
562 if (p_sys->keys != NULL)
564 const uint32_t mask = XCB_CW_EVENT_MASK;
565 const uint32_t values[1] = {
566 XCB_EVENT_MASK_KEY_PRESS,
568 xcb_change_window_attributes (conn, window, mask, values);
573 if ((p_sys->keys != NULL)
574 && vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
575 DestroyKeyHandler (p_sys->keys);
582 xcb_disconnect (conn);
584 ReleaseDrawable (obj, window);
588 static void EmClose (vlc_object_t *obj)
590 vout_window_t *wnd = (vout_window_t *)obj;
591 xcb_window_t window = wnd->handle.xid;
594 ReleaseDrawable (obj, window);