]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/window.c
405cefc5599e8bd9ba72110f6e34c5de1bb94b3b
[vlc] / modules / video_output / xcb / window.c
1 /**
2  * @file window.c
3  * @brief X C Bindings window provider module for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright © 2009 Rémi Denis-Courmont
7  *
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.
12  *
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.
17  *
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  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdarg.h>
28 #include <assert.h>
29 #include <poll.h>
30 #include <unistd.h> /* gethostname() and sysconf() */
31 #include <limits.h> /* _POSIX_HOST_NAME_MAX */
32
33 #include <xcb/xcb.h>
34 typedef xcb_atom_t Atom;
35 #include <X11/Xatom.h> /* XA_WM_NAME */
36
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_vout_window.h>
40
41 #include "xcb_vlc.h"
42
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.")
47
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).")
52
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 *);
57
58 /*
59  * Module descriptor
60  */
61 vlc_module_begin ()
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)
68
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")
76
77     add_submodule ()
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)
84
85     add_integer ("drawable-xid", 0, NULL, XID_TEXT, XID_LONGTEXT, true)
86         change_unsaveable ()
87
88 vlc_module_end ()
89
90 static int Control (vout_window_t *, int, va_list ap);
91 static void *Thread (void *);
92
93 struct vout_window_sys_t
94 {
95     xcb_connection_t *conn;
96     key_handler_t *keys;
97     vlc_thread_t thread;
98
99     xcb_window_t root;
100     xcb_atom_t wm_state;
101     xcb_atom_t wm_state_above;
102     xcb_atom_t wm_state_fullscreen;
103 };
104
105 /** Set an X window property from a nul-terminated string */
106 static inline
107 void set_string (xcb_connection_t *conn, xcb_window_t window,
108                  xcb_atom_t type, xcb_atom_t atom, const char *str)
109 {
110     xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, atom, type,
111                          /* format */ 8, strlen (str), str);
112 }
113
114 /** Set an X window string property */
115 static inline
116 void set_ascii_prop (xcb_connection_t *conn, xcb_window_t window,
117                      xcb_atom_t atom, const char *value)
118 {
119     set_string (conn, window, atom, XA_STRING, value);
120 }
121
122 /** Set the Window ICCCM client machine property */
123 static inline
124 void set_hostname_prop (xcb_connection_t *conn, xcb_window_t window)
125 {
126     char* hostname;
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;
131
132     if (gethostname (hostname, host_name_max) == 0)
133     {
134         hostname[host_name_max - 1] = '\0';
135         set_ascii_prop (conn, window, XA_WM_CLIENT_MACHINE, hostname);
136     }
137     free(hostname);
138 }
139
140 /** Request the X11 server to internalize a string into an atom */
141 static inline
142 xcb_intern_atom_cookie_t intern_string (xcb_connection_t *c, const char *s)
143 {
144     return xcb_intern_atom (c, 0, strlen (s), s);
145 }
146
147 /** Extract the X11 atom from an intern request cookie */
148 static
149 xcb_atom_t get_atom (xcb_connection_t *conn, xcb_intern_atom_cookie_t ck)
150 {
151     xcb_intern_atom_reply_t *reply;
152     xcb_atom_t atom;
153
154     reply = xcb_intern_atom_reply (conn, ck, NULL);
155     if (reply == NULL)
156         return 0;
157
158     atom = reply->atom;
159     free (reply);
160     return atom;
161 }
162
163 #define NET_WM_STATE_REMOVE 0
164 #define NET_WM_STATE_ADD    1
165 #define NET_WM_STATE_TOGGLE 2
166
167 static void CacheAtoms (vout_window_sys_t *p_sys)
168 {
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;
171
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");
175
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);
179 }
180
181 /**
182  * Create an X11 window.
183  */
184 static int Open (vlc_object_t *obj)
185 {
186     vout_window_t *wnd = (vout_window_t *)obj;
187     xcb_generic_error_t *err;
188     xcb_void_cookie_t ck;
189
190     vout_window_sys_t *p_sys = malloc (sizeof (*p_sys));
191     if (p_sys == NULL)
192         return VLC_ENOMEM;
193
194     /* Connect to X */
195     char *display = var_CreateGetNonEmptyString (wnd, "x11-display");
196     int snum;
197
198     xcb_connection_t *conn = xcb_connect (display, &snum);
199     free (display);
200     if (xcb_connection_has_error (conn) /*== NULL*/)
201         goto error;
202
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))
208     {
209         if (snum == 0)
210         {
211             scr = i.data;
212             break;
213         }
214         snum--;
215     }
216     if (scr == NULL)
217     {
218         msg_Err (wnd, "bad X11 screen number");
219         goto error;
220     }
221
222     /* Create window */
223     const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
224     uint32_t values[2] = {
225         /* XCB_CW_BACK_PIXEL */
226         scr->black_pixel,
227         /* XCB_CW_EVENT_MASK */
228         XCB_EVENT_MASK_KEY_PRESS,
229     };
230
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);
237     if (err)
238     {
239         msg_Err (wnd, "creating window: X11 error %d", err->error_code);
240         free (err);
241         goto error;
242     }
243
244     wnd->handle.xid = window;
245     wnd->control = Control;
246     wnd->sys = p_sys;
247
248     p_sys->conn = conn;
249     p_sys->keys = CreateKeyHandler (obj, conn);
250     p_sys->root = scr->root;
251
252     /* ICCCM
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);
262
263     /* EWMH */
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
271     xcb_atom_t utf8 = get_atom (conn, utf8_string_ck);
272
273     xcb_atom_t net_wm_name = get_atom (conn, net_wm_name_ck);
274     char *title = var_CreateGetNonEmptyString (wnd, "video-title");
275     if (title)
276     {
277         set_string (conn, window, utf8, net_wm_name, title);
278         free (title);
279     }
280     else
281         set_string (conn, window, utf8, net_wm_name, _("VLC media player"));
282
283     xcb_atom_t net_wm_icon_name = get_atom (conn, net_wm_icon_name_ck);
284     set_string (conn, window, utf8, net_wm_icon_name, _("VLC"));
285
286     /* Make the window visible */
287     xcb_map_window (conn, window);
288
289     /* Cache any EWMH atom we may need later */
290     CacheAtoms (p_sys);
291
292     /* Create the event thread. It will dequeue all events, so any checked
293      * request from this thread must be completed at this point. */
294     if ((p_sys->keys != NULL)
295      && vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
296         DestroyKeyHandler (p_sys->keys);
297
298     xcb_flush (conn); /* Make sure map_window is sent (should be useless) */
299
300     return VLC_SUCCESS;
301
302 error:
303     xcb_disconnect (conn);
304     free (p_sys);
305     return VLC_EGENERIC;
306 }
307
308
309 /**
310  * Destroys the X11 window.
311  */
312 static void Close (vlc_object_t *obj)
313 {
314     vout_window_t *wnd = (vout_window_t *)obj;
315     vout_window_sys_t *p_sys = wnd->sys;
316     xcb_connection_t *conn = p_sys->conn;
317
318     if (p_sys->keys)
319     {
320         vlc_cancel (p_sys->thread);
321         vlc_join (p_sys->thread, NULL);
322         DestroyKeyHandler (p_sys->keys);
323     }
324     xcb_disconnect (conn);
325     free (p_sys);
326 }
327
328
329 /** Background thread for X11 events handling */
330 static void *Thread (void *data)
331 {
332     vout_window_t *wnd = data;
333     vout_window_sys_t *p_sys = wnd->sys;
334     xcb_connection_t *conn = p_sys->conn;
335
336     int fd = xcb_get_file_descriptor (conn);
337     if (fd == -1)
338         return NULL;
339
340     for (;;)
341     {
342         xcb_generic_event_t *ev;
343         struct pollfd ufd = { .fd = fd, .events = POLLIN, };
344
345         poll (&ufd, 1, -1);
346
347         int canc = vlc_savecancel ();
348         while ((ev = xcb_poll_for_event (conn)) != NULL)
349         {
350             if (ProcessKeyEvent (p_sys->keys, ev) == 0)
351                 continue;
352             msg_Dbg (wnd, "unhandled event: %"PRIu8, ev->response_type);
353             free (ev);
354         }
355         vlc_restorecancel (canc);
356
357         if (xcb_connection_has_error (conn))
358         {
359             msg_Err (wnd, "X server failure");
360             break;
361         }
362     }
363     return NULL;
364 }
365
366 /** Changes the EWMH state of the window */
367 static void set_wm_state (vout_window_t *wnd, bool on, xcb_atom_t state)
368 {
369     vout_window_sys_t *sys = wnd->sys;
370     /* From EWMH "_WM_STATE" */
371     xcb_client_message_event_t ev = {
372          .response_type = XCB_CLIENT_MESSAGE,
373          .format = 32,
374          .window = wnd->handle.xid,
375          .type = sys->wm_state,
376     };
377
378     ev.data.data32[0] = on ? NET_WM_STATE_ADD : NET_WM_STATE_REMOVE;
379     ev.data.data32[1] = state;
380     ev.data.data32[2] = 0;
381     ev.data.data32[3] = 1;
382
383     /* From ICCCM "Changing Window State" */
384     xcb_send_event (sys->conn, 0, sys->root,
385                     XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
386                     XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
387                     (const char *)&ev);
388 }
389
390
391 static int Control (vout_window_t *wnd, int cmd, va_list ap)
392 {
393     vout_window_sys_t *p_sys = wnd->sys;
394     xcb_connection_t *conn = p_sys->conn;
395
396     switch (cmd)
397     {
398         case VOUT_WINDOW_SET_SIZE:
399         {
400             unsigned width = va_arg (ap, unsigned);
401             unsigned height = va_arg (ap, unsigned);
402             const uint32_t values[] = { width, height, };
403
404             xcb_configure_window (conn, wnd->handle.xid,
405                                   XCB_CONFIG_WINDOW_WIDTH |
406                                   XCB_CONFIG_WINDOW_HEIGHT, values);
407             break;
408         }
409
410         case VOUT_WINDOW_SET_ON_TOP:
411             set_wm_state (wnd, va_arg (ap, int), p_sys->wm_state_above);
412             break;
413
414         case VOUT_WINDOW_SET_FULLSCREEN:
415             set_wm_state (wnd, va_arg (ap, int), p_sys->wm_state_fullscreen);
416             break;
417
418         default:
419             msg_Err (wnd, "request %d not implemented", cmd);
420             return VLC_EGENERIC;
421     }
422     xcb_flush (p_sys->conn);
423     return VLC_SUCCESS;
424 }
425
426 /*** Embedded drawable support ***/
427
428 static vlc_mutex_t serializer = VLC_STATIC_MUTEX;
429
430 /** Acquire a drawable */
431 static int AcquireDrawable (vlc_object_t *obj, xcb_window_t window)
432 {
433     vlc_value_t val;
434     xcb_window_t *used;
435     size_t n = 0;
436
437     if (var_Create (obj->p_libvlc, "xid-in-use", VLC_VAR_ADDRESS))
438         return VLC_ENOMEM;
439
440     /* Keep a list of busy drawables, so we don't overlap videos if there are
441      * more than one video track in the stream. */
442     vlc_mutex_lock (&serializer);
443     var_Get (VLC_OBJECT (obj->p_libvlc), "xid-in-use", &val);
444     used = val.p_address;
445     if (used != NULL)
446     {
447         while (used[n])
448         {
449             if (used[n] == window)
450                 goto skip;
451             n++;
452         }
453     }
454
455     used = realloc (used, sizeof (*used) * (n + 2));
456     if (used != NULL)
457     {
458         used[n] = window;
459         used[n + 1] = 0;
460         val.p_address = used;
461         var_Set (obj->p_libvlc, "xid-in-use", val);
462     }
463     else
464     {
465 skip:
466         msg_Warn (obj, "X11 drawable 0x%08"PRIx8" is busy", window);
467         window = 0;
468     }
469     vlc_mutex_unlock (&serializer);
470
471     return (window == 0) ? VLC_EGENERIC : VLC_SUCCESS;
472 }
473
474 /** Remove this drawable from the list of busy ones */
475 static void ReleaseDrawable (vlc_object_t *obj, xcb_window_t window)
476 {
477     vlc_value_t val;
478     xcb_window_t *used;
479     size_t n = 0;
480
481     vlc_mutex_lock (&serializer);
482     var_Get (VLC_OBJECT (obj->p_libvlc), "xid-in-use", &val);
483     used = val.p_address;
484     assert (used);
485     while (used[n] != window)
486     {
487         assert (used[n]);
488         n++;
489     }
490     do
491         used[n] = used[n + 1];
492     while (used[++n]);
493
494     if (n == 0)
495          var_SetAddress (obj->p_libvlc, "xid-in-use", NULL);
496     vlc_mutex_unlock (&serializer);
497
498     if (n == 0)
499         free (used);
500     /* Variables are reference-counted... */
501     var_Destroy (obj->p_libvlc, "xid-in-use");
502 }
503
504 /**
505  * Wrap an existing X11 window to embed the video.
506  */
507 static int EmOpen (vlc_object_t *obj)
508 {
509     vout_window_t *wnd = (vout_window_t *)obj;
510
511     xcb_window_t window = var_CreateGetInteger (obj, "drawable-xid");
512     if (window == 0)
513         return VLC_EGENERIC;
514     var_Destroy (obj, "drawable-xid");
515
516     if (AcquireDrawable (obj, window))
517         return VLC_EGENERIC;
518
519     vout_window_sys_t *p_sys = malloc (sizeof (*p_sys));
520     xcb_connection_t *conn = xcb_connect (NULL, NULL);
521     if (p_sys == NULL || xcb_connection_has_error (conn))
522         goto error;
523
524     wnd->handle.xid = window;
525     wnd->control = Control;
526     wnd->sys = p_sys;
527
528     p_sys->conn = conn;
529
530     xcb_get_geometry_reply_t *geo =
531         xcb_get_geometry_reply (conn, xcb_get_geometry (conn, window), NULL);
532     if (geo == NULL)
533     {
534         msg_Err (obj, "bad X11 window 0x%08"PRIx8, window);
535         goto error;
536     }
537     p_sys->root = geo->root;
538     free (geo);
539
540     if (var_CreateGetInteger (obj, "vout-event") != 3) /* FIXME: <- cleanup */
541     {
542         p_sys->keys = CreateKeyHandler (obj, conn);
543         if (p_sys->keys != NULL)
544         {
545             const uint32_t mask = XCB_CW_EVENT_MASK;
546             const uint32_t values[1] = {
547                 XCB_EVENT_MASK_KEY_PRESS,
548             };
549             xcb_change_window_attributes (conn, window, mask, values);
550         }
551     }
552
553     CacheAtoms (p_sys);
554     if ((p_sys->keys != NULL)
555      && vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
556         DestroyKeyHandler (p_sys->keys);
557
558     xcb_flush (conn);
559
560     return VLC_SUCCESS;
561
562 error:
563     xcb_disconnect (conn);
564     free (p_sys);
565     ReleaseDrawable (obj, window);
566     return VLC_EGENERIC;
567 }
568
569 static void EmClose (vlc_object_t *obj)
570 {
571     vout_window_t *wnd = (vout_window_t *)obj;
572     xcb_window_t window = wnd->handle.xid;
573
574     Close (obj);
575     ReleaseDrawable (obj, window);
576 }