]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/window.c
d28353f9b3539bbc9c02b82db19ca58b4d013b8e
[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
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 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 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).")
47
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 *);
52
53 /*
54  * Module descriptor
55  */
56 vlc_module_begin ()
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)
63
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")
69
70     add_submodule ()
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)
77
78     add_integer ("drawable-xid", 0, NULL, XID_TEXT, XID_LONGTEXT, true)
79         change_unsaveable ()
80
81 vlc_module_end ()
82
83 static int Control (vout_window_t *, int, va_list ap);
84 static void *Thread (void *);
85
86 #define MATCHBOX_HACK 1 /* Matchbox focus hack */
87
88 struct vout_window_sys_t
89 {
90     xcb_connection_t *conn;
91     key_handler_t *keys;
92     vlc_thread_t thread;
93
94     xcb_window_t root;
95     xcb_atom_t wm_state;
96     xcb_atom_t wm_state_above;
97     xcb_atom_t wm_state_fullscreen;
98 #ifdef MATCHBOX_HACK
99     xcb_atom_t mb_current_app_window;
100 #endif
101 };
102
103 /** Set an X window property from a nul-terminated string */
104 static inline
105 void set_string (xcb_connection_t *conn, xcb_window_t window,
106                  xcb_atom_t type, xcb_atom_t atom, const char *str)
107 {
108     xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, atom, type,
109                          /* format */ 8, strlen (str), str);
110 }
111
112 /** Set an X window string property */
113 static inline
114 void set_ascii_prop (xcb_connection_t *conn, xcb_window_t window,
115                      xcb_atom_t atom, const char *value)
116 {
117     set_string (conn, window, XA_STRING, atom, value);
118 }
119
120 static inline
121 void set_wm_hints (xcb_connection_t *conn, xcb_window_t window)
122 {
123     static const uint32_t wm_hints[8] = {
124         3, /* flags: Input, Initial state */
125         1, /* input: True */
126         1, /* initial state: Normal */
127         0, 0, 0, 0, 0, /* Icon */
128     };
129     xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, XA_WM_HINTS,
130                          XA_WM_HINTS, 32, 8, wm_hints);
131 }
132
133 /** Set the Window ICCCM client machine property */
134 static inline
135 void set_hostname_prop (xcb_connection_t *conn, xcb_window_t window)
136 {
137     char* hostname;
138     long host_name_max = sysconf (_SC_HOST_NAME_MAX);
139     if (host_name_max <= 0) host_name_max = _POSIX_HOST_NAME_MAX;
140     hostname = malloc (host_name_max);
141     if(!hostname) return;
142
143     if (gethostname (hostname, host_name_max) == 0)
144     {
145         hostname[host_name_max - 1] = '\0';
146         set_ascii_prop (conn, window, XA_WM_CLIENT_MACHINE, hostname);
147     }
148     free(hostname);
149 }
150
151 /** Request the X11 server to internalize a string into an atom */
152 static inline
153 xcb_intern_atom_cookie_t intern_string (xcb_connection_t *c, const char *s)
154 {
155     return xcb_intern_atom (c, 0, strlen (s), s);
156 }
157
158 /** Extract the X11 atom from an intern request cookie */
159 static
160 xcb_atom_t get_atom (xcb_connection_t *conn, xcb_intern_atom_cookie_t ck)
161 {
162     xcb_intern_atom_reply_t *reply;
163     xcb_atom_t atom;
164
165     reply = xcb_intern_atom_reply (conn, ck, NULL);
166     if (reply == NULL)
167         return 0;
168
169     atom = reply->atom;
170     free (reply);
171     return atom;
172 }
173
174 #define NET_WM_STATE_REMOVE 0
175 #define NET_WM_STATE_ADD    1
176 #define NET_WM_STATE_TOGGLE 2
177
178 static void CacheAtoms (vout_window_sys_t *p_sys)
179 {
180     xcb_connection_t *conn = p_sys->conn;
181     xcb_intern_atom_cookie_t wm_state_ck, wm_state_above_ck, wm_state_fs_ck;
182
183     wm_state_ck = intern_string (conn, "_NET_WM_STATE");
184     wm_state_above_ck = intern_string (conn, "_NET_WM_STATE_ABOVE");
185     wm_state_fs_ck = intern_string (conn, "_NET_WM_STATE_FULLSCREEN");
186 #ifdef MATCHBOX_HACK
187     xcb_intern_atom_cookie_t mb_current_app_window;
188     mb_current_app_window = xcb_intern_atom (conn, true,
189                                              strlen ("_MB_CURRENT_APP_WINDOW"),
190                                              "_MB_CURRENT_APP_WINDOW");
191 #endif
192
193     p_sys->wm_state = get_atom (conn, wm_state_ck);
194     p_sys->wm_state_above = get_atom (conn, wm_state_above_ck);
195     p_sys->wm_state_fullscreen = get_atom (conn, wm_state_fs_ck);
196 #ifdef MATCHBOX_HACK
197     p_sys->mb_current_app_window = get_atom (conn, mb_current_app_window);
198 #endif
199 }
200
201 /**
202  * Create an X11 window.
203  */
204 static int Open (vlc_object_t *obj)
205 {
206     vout_window_t *wnd = (vout_window_t *)obj;
207     xcb_generic_error_t *err;
208     xcb_void_cookie_t ck;
209
210     vout_window_sys_t *p_sys = malloc (sizeof (*p_sys));
211     if (p_sys == NULL)
212         return VLC_ENOMEM;
213
214     /* Connect to X */
215     char *display = var_CreateGetNonEmptyString (wnd, "x11-display");
216     int snum;
217
218     xcb_connection_t *conn = xcb_connect (display, &snum);
219     free (display);
220     if (xcb_connection_has_error (conn) /*== NULL*/)
221         goto error;
222
223     /* Find configured screen */
224     const xcb_setup_t *setup = xcb_get_setup (conn);
225     const xcb_screen_t *scr = NULL;
226     for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
227          i.rem > 0; xcb_screen_next (&i))
228     {
229         if (snum == 0)
230         {
231             scr = i.data;
232             break;
233         }
234         snum--;
235     }
236     if (scr == NULL)
237     {
238         msg_Err (wnd, "bad X11 screen number");
239         goto error;
240     }
241
242     /* Create window */
243     const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
244     uint32_t values[2] = {
245         /* XCB_CW_BACK_PIXEL */
246         scr->black_pixel,
247         /* XCB_CW_EVENT_MASK */
248         XCB_EVENT_MASK_KEY_PRESS,
249     };
250
251     xcb_window_t window = xcb_generate_id (conn);
252     ck = xcb_create_window_checked (conn, scr->root_depth, window, scr->root,
253                                     0, 0, wnd->cfg->width, wnd->cfg->height, 0,
254                                     XCB_WINDOW_CLASS_INPUT_OUTPUT,
255                                     scr->root_visual, mask, values);
256     err = xcb_request_check (conn, ck);
257     if (err)
258     {
259         msg_Err (wnd, "creating window: X11 error %d", err->error_code);
260         free (err);
261         goto error;
262     }
263
264     wnd->handle.xid = window;
265     wnd->control = Control;
266     wnd->sys = p_sys;
267
268     p_sys->conn = conn;
269     if (var_CreateGetBool (obj, "keyboard-events"))
270         p_sys->keys = CreateKeyHandler (obj, conn);
271     else
272         p_sys->keys = NULL;
273     p_sys->root = scr->root;
274
275     /* ICCCM
276      * No cut&paste nor drag&drop, only Window Manager communication. */
277     /* Plain ASCII localization of VLC for ICCCM window name */
278     set_ascii_prop (conn, window, XA_WM_NAME,
279                   vlc_pgettext ("ASCII", "VLC media player"));
280     set_ascii_prop (conn, window, XA_WM_ICON_NAME,
281                     vlc_pgettext ("ASCII", "VLC"));
282     set_wm_hints (conn, window);
283     xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, XA_WM_CLASS,
284                          XA_STRING, 8, 8, "vlc\0Vlc");
285     set_hostname_prop (conn, window);
286
287     /* EWMH */
288     xcb_intern_atom_cookie_t utf8_string_ck
289         = intern_string (conn, "UTF8_STRING");;
290     xcb_intern_atom_cookie_t net_wm_name_ck
291         = intern_string (conn, "_NET_WM_NAME");
292     xcb_intern_atom_cookie_t net_wm_icon_name_ck
293         = intern_string (conn, "_NET_WM_ICON_NAME");
294     xcb_intern_atom_cookie_t wm_window_role_ck
295         = intern_string (conn, "WM_WINDOW_ROLE");
296
297     xcb_atom_t utf8 = get_atom (conn, utf8_string_ck);
298
299     xcb_atom_t net_wm_name = get_atom (conn, net_wm_name_ck);
300     char *title = var_CreateGetNonEmptyString (wnd, "video-title");
301     if (title)
302     {
303         set_string (conn, window, utf8, net_wm_name, title);
304         free (title);
305     }
306     else
307         set_string (conn, window, utf8, net_wm_name, _("VLC media player"));
308
309     xcb_atom_t net_wm_icon_name = get_atom (conn, net_wm_icon_name_ck);
310     set_string (conn, window, utf8, net_wm_icon_name, _("VLC"));
311
312     xcb_atom_t wm_window_role = get_atom (conn, wm_window_role_ck);
313     set_ascii_prop (conn, window, wm_window_role, "vlc-video");
314
315     /* Cache any EWMH atom we may need later */
316     CacheAtoms (p_sys);
317 #ifdef MATCHBOX_HACK
318     if (p_sys->mb_current_app_window)
319     {
320         uint32_t value = XCB_EVENT_MASK_PROPERTY_CHANGE;
321         xcb_change_window_attributes (conn, scr->root,
322                                       XCB_CW_EVENT_MASK, &value);
323     }
324 #endif
325
326     /* Make the window visible */
327     xcb_map_window (conn, window);
328
329     /* Create the event thread. It will dequeue all events, so any checked
330      * request from this thread must be completed at this point. */
331     if ((p_sys->keys != NULL)
332      && vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
333         DestroyKeyHandler (p_sys->keys);
334
335 #ifdef MATCHBOX_HACK
336     if (p_sys->mb_current_app_window)
337         xcb_set_input_focus (p_sys->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
338                              wnd->handle.xid, XCB_CURRENT_TIME);
339 #endif
340     xcb_flush (conn); /* Make sure map_window is sent (should be useless) */
341     return VLC_SUCCESS;
342
343 error:
344     xcb_disconnect (conn);
345     free (p_sys);
346     return VLC_EGENERIC;
347 }
348
349
350 /**
351  * Destroys the X11 window.
352  */
353 static void Close (vlc_object_t *obj)
354 {
355     vout_window_t *wnd = (vout_window_t *)obj;
356     vout_window_sys_t *p_sys = wnd->sys;
357     xcb_connection_t *conn = p_sys->conn;
358
359     if (p_sys->keys)
360     {
361         vlc_cancel (p_sys->thread);
362         vlc_join (p_sys->thread, NULL);
363         DestroyKeyHandler (p_sys->keys);
364     }
365     xcb_disconnect (conn);
366     free (p_sys);
367 }
368
369
370 /** Background thread for X11 events handling */
371 static void *Thread (void *data)
372 {
373     vout_window_t *wnd = data;
374     vout_window_sys_t *p_sys = wnd->sys;
375     xcb_connection_t *conn = p_sys->conn;
376
377     int fd = xcb_get_file_descriptor (conn);
378     if (fd == -1)
379         return NULL;
380
381     for (;;)
382     {
383         xcb_generic_event_t *ev;
384         struct pollfd ufd = { .fd = fd, .events = POLLIN, };
385
386         poll (&ufd, 1, -1);
387
388         int canc = vlc_savecancel ();
389         while ((ev = xcb_poll_for_event (conn)) != NULL)
390         {
391             if (ProcessKeyEvent (p_sys->keys, ev) == 0)
392                 continue;
393 #ifdef MATCHBOX_HACK
394             if (p_sys->mb_current_app_window
395              && (ev->response_type & 0x7f) == XCB_PROPERTY_NOTIFY)
396             {
397                 const xcb_property_notify_event_t *pne =
398                     (xcb_property_notify_event_t *)ev;
399                 if (pne->atom == p_sys->mb_current_app_window
400                  && pne->state == XCB_PROPERTY_NEW_VALUE)
401                 {
402                     xcb_get_property_reply_t *r =
403                         xcb_get_property_reply (conn,
404                             xcb_get_property (conn, 0, pne->window, pne->atom,
405                                               XA_WINDOW, 0, 4), NULL);
406                     if (r != NULL
407                      && !memcmp (xcb_get_property_value (r), &wnd->handle.xid,
408                                  4))
409                     {
410                         msg_Dbg (wnd, "asking Matchbox for input focus");
411                         xcb_set_input_focus (conn,
412                                              XCB_INPUT_FOCUS_POINTER_ROOT,
413                                              wnd->handle.xid, pne->time);
414                         xcb_flush (conn);
415                     }
416                     free (r);
417                 }
418             }
419             else
420 #endif
421                 msg_Dbg (wnd, "unhandled event: %"PRIu8, ev->response_type);
422             free (ev);
423         }
424         vlc_restorecancel (canc);
425
426         if (xcb_connection_has_error (conn))
427         {
428             msg_Err (wnd, "X server failure");
429             break;
430         }
431     }
432     return NULL;
433 }
434
435 /** Changes the EWMH state of the window */
436 static void set_wm_state (vout_window_t *wnd, bool on, xcb_atom_t state)
437 {
438     vout_window_sys_t *sys = wnd->sys;
439     /* From EWMH "_WM_STATE" */
440     xcb_client_message_event_t ev = {
441          .response_type = XCB_CLIENT_MESSAGE,
442          .format = 32,
443          .window = wnd->handle.xid,
444          .type = sys->wm_state,
445     };
446
447     ev.data.data32[0] = on ? NET_WM_STATE_ADD : NET_WM_STATE_REMOVE;
448     ev.data.data32[1] = state;
449     ev.data.data32[2] = 0;
450     ev.data.data32[3] = 1;
451
452     /* From ICCCM "Changing Window State" */
453     xcb_send_event (sys->conn, 0, sys->root,
454                     XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
455                     XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
456                     (const char *)&ev);
457 }
458
459
460 static int Control (vout_window_t *wnd, int cmd, va_list ap)
461 {
462     vout_window_sys_t *p_sys = wnd->sys;
463     xcb_connection_t *conn = p_sys->conn;
464
465     switch (cmd)
466     {
467         case VOUT_WINDOW_SET_SIZE:
468         {
469             unsigned width = va_arg (ap, unsigned);
470             unsigned height = va_arg (ap, unsigned);
471             const uint32_t values[] = { width, height, };
472
473             xcb_configure_window (conn, wnd->handle.xid,
474                                   XCB_CONFIG_WINDOW_WIDTH |
475                                   XCB_CONFIG_WINDOW_HEIGHT, values);
476             break;
477         }
478
479         case VOUT_WINDOW_SET_ON_TOP:
480             set_wm_state (wnd, va_arg (ap, int), p_sys->wm_state_above);
481             break;
482
483         case VOUT_WINDOW_SET_FULLSCREEN:
484             set_wm_state (wnd, va_arg (ap, int), p_sys->wm_state_fullscreen);
485             break;
486
487         default:
488             msg_Err (wnd, "request %d not implemented", cmd);
489             return VLC_EGENERIC;
490     }
491     xcb_flush (p_sys->conn);
492     return VLC_SUCCESS;
493 }
494
495 /*** Embedded drawable support ***/
496
497 static vlc_mutex_t serializer = VLC_STATIC_MUTEX;
498
499 /** Acquire a drawable */
500 static int AcquireDrawable (vlc_object_t *obj, xcb_window_t window)
501 {
502     xcb_window_t *used;
503     size_t n = 0;
504
505     if (var_Create (obj->p_libvlc, "xid-in-use", VLC_VAR_ADDRESS))
506         return VLC_ENOMEM;
507
508     /* Keep a list of busy drawables, so we don't overlap videos if there are
509      * more than one video track in the stream. */
510     vlc_mutex_lock (&serializer);
511     used = var_GetAddress (obj->p_libvlc, "xid-in-use");
512     if (used != NULL)
513     {
514         while (used[n])
515         {
516             if (used[n] == window)
517                 goto skip;
518             n++;
519         }
520     }
521
522     used = realloc (used, sizeof (*used) * (n + 2));
523     if (used != NULL)
524     {
525         used[n] = window;
526         used[n + 1] = 0;
527         var_SetAddress (obj->p_libvlc, "xid-in-use", used);
528     }
529     else
530     {
531 skip:
532         msg_Warn (obj, "X11 drawable 0x%08"PRIx8" is busy", window);
533         window = 0;
534     }
535     vlc_mutex_unlock (&serializer);
536
537     return (window == 0) ? VLC_EGENERIC : VLC_SUCCESS;
538 }
539
540 /** Remove this drawable from the list of busy ones */
541 static void ReleaseDrawable (vlc_object_t *obj, xcb_window_t window)
542 {
543     xcb_window_t *used;
544     size_t n = 0;
545
546     vlc_mutex_lock (&serializer);
547     used = var_GetAddress (obj->p_libvlc, "xid-in-use");
548     assert (used);
549     while (used[n] != window)
550     {
551         assert (used[n]);
552         n++;
553     }
554     do
555         used[n] = used[n + 1];
556     while (used[++n]);
557
558     if (n == 0)
559          var_SetAddress (obj->p_libvlc, "xid-in-use", NULL);
560     vlc_mutex_unlock (&serializer);
561
562     if (n == 0)
563         free (used);
564     /* Variables are reference-counted... */
565     var_Destroy (obj->p_libvlc, "xid-in-use");
566 }
567
568 /**
569  * Wrap an existing X11 window to embed the video.
570  */
571 static int EmOpen (vlc_object_t *obj)
572 {
573     vout_window_t *wnd = (vout_window_t *)obj;
574
575     xcb_window_t window = var_CreateGetInteger (obj, "drawable-xid");
576     if (window == 0)
577         return VLC_EGENERIC;
578     var_Destroy (obj, "drawable-xid");
579
580     if (AcquireDrawable (obj, window))
581         return VLC_EGENERIC;
582
583     vout_window_sys_t *p_sys = malloc (sizeof (*p_sys));
584     xcb_connection_t *conn = xcb_connect (NULL, NULL);
585     if (p_sys == NULL || xcb_connection_has_error (conn))
586         goto error;
587
588     wnd->handle.xid = window;
589     wnd->control = Control;
590     wnd->sys = p_sys;
591
592     p_sys->conn = conn;
593
594     xcb_get_geometry_reply_t *geo =
595         xcb_get_geometry_reply (conn, xcb_get_geometry (conn, window), NULL);
596     if (geo == NULL)
597     {
598         msg_Err (obj, "bad X11 window 0x%08"PRIx8, window);
599         goto error;
600     }
601     p_sys->root = geo->root;
602     free (geo);
603
604     if (var_CreateGetBool (obj, "keyboard-events"))
605     {
606         p_sys->keys = CreateKeyHandler (obj, conn);
607         if (p_sys->keys != NULL)
608         {
609             const uint32_t mask = XCB_CW_EVENT_MASK;
610             const uint32_t values[1] = {
611                 XCB_EVENT_MASK_KEY_PRESS,
612             };
613             xcb_change_window_attributes (conn, window, mask, values);
614         }
615     }
616
617     CacheAtoms (p_sys);
618     if ((p_sys->keys != NULL)
619      && vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
620         DestroyKeyHandler (p_sys->keys);
621
622     xcb_flush (conn);
623
624     return VLC_SUCCESS;
625
626 error:
627     xcb_disconnect (conn);
628     free (p_sys);
629     ReleaseDrawable (obj, window);
630     return VLC_EGENERIC;
631 }
632
633 static void EmClose (vlc_object_t *obj)
634 {
635     vout_window_t *wnd = (vout_window_t *)obj;
636     xcb_window_t window = wnd->handle.xid;
637
638     Close (obj);
639     ReleaseDrawable (obj, window);
640 }