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