]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/window.c
Define XCB_CURSOR_NONE if missing
[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     p_sys->keys = CreateKeyHandler (obj, conn);
277     p_sys->root = scr->root;
278
279     /* ICCCM
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);
290
291     /* EWMH */
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");
300
301     xcb_atom_t utf8 = get_atom (conn, utf8_string_ck);
302
303     xcb_atom_t net_wm_name = get_atom (conn, net_wm_name_ck);
304     char *title = var_CreateGetNonEmptyString (wnd, "video-title");
305     if (title)
306     {
307         set_string (conn, window, utf8, net_wm_name, title);
308         free (title);
309     }
310     else
311         set_string (conn, window, utf8, net_wm_name, _("VLC media player"));
312
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"));
315
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");
318
319     /* Cache any EWMH atom we may need later */
320     CacheAtoms (p_sys);
321 #ifdef MATCHBOX_HACK
322     if (p_sys->mb_current_app_window)
323     {
324         uint32_t value = XCB_EVENT_MASK_PROPERTY_CHANGE;
325         xcb_change_window_attributes (conn, scr->root,
326                                       XCB_CW_EVENT_MASK, &value);
327     }
328 #endif
329
330     /* Make the window visible */
331     xcb_map_window (conn, window);
332
333     /* Create the event thread. It will dequeue all events, so any checked
334      * request from this thread must be completed at this point. */
335     if ((p_sys->keys != NULL)
336      && vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
337         DestroyKeyHandler (p_sys->keys);
338
339 #ifdef MATCHBOX_HACK
340     if (p_sys->mb_current_app_window)
341         xcb_set_input_focus (p_sys->conn, XCB_INPUT_FOCUS_POINTER_ROOT,
342                              wnd->handle.xid, XCB_CURRENT_TIME);
343 #endif
344     xcb_flush (conn); /* Make sure map_window is sent (should be useless) */
345     return VLC_SUCCESS;
346
347 error:
348     xcb_disconnect (conn);
349     free (p_sys);
350     return VLC_EGENERIC;
351 }
352
353
354 /**
355  * Destroys the X11 window.
356  */
357 static void Close (vlc_object_t *obj)
358 {
359     vout_window_t *wnd = (vout_window_t *)obj;
360     vout_window_sys_t *p_sys = wnd->sys;
361     xcb_connection_t *conn = p_sys->conn;
362
363     if (p_sys->keys)
364     {
365         vlc_cancel (p_sys->thread);
366         vlc_join (p_sys->thread, NULL);
367         DestroyKeyHandler (p_sys->keys);
368     }
369     xcb_disconnect (conn);
370     free (p_sys);
371 }
372
373
374 /** Background thread for X11 events handling */
375 static void *Thread (void *data)
376 {
377     vout_window_t *wnd = data;
378     vout_window_sys_t *p_sys = wnd->sys;
379     xcb_connection_t *conn = p_sys->conn;
380
381     int fd = xcb_get_file_descriptor (conn);
382     if (fd == -1)
383         return NULL;
384
385     for (;;)
386     {
387         xcb_generic_event_t *ev;
388         struct pollfd ufd = { .fd = fd, .events = POLLIN, };
389
390         poll (&ufd, 1, -1);
391
392         int canc = vlc_savecancel ();
393         while ((ev = xcb_poll_for_event (conn)) != NULL)
394         {
395             if (ProcessKeyEvent (p_sys->keys, ev) == 0)
396                 continue;
397 #ifdef MATCHBOX_HACK
398             if (p_sys->mb_current_app_window
399              && (ev->response_type & 0x7f) == XCB_PROPERTY_NOTIFY)
400             {
401                 const xcb_property_notify_event_t *pne =
402                     (xcb_property_notify_event_t *)ev;
403                 if (pne->atom == p_sys->mb_current_app_window
404                  && pne->state == XCB_PROPERTY_NEW_VALUE)
405                 {
406                     xcb_get_property_reply_t *r =
407                         xcb_get_property_reply (conn,
408                             xcb_get_property (conn, 0, pne->window, pne->atom,
409                                               XA_WINDOW, 0, 4), NULL);
410                     if (r != NULL
411                      && !memcmp (xcb_get_property_value (r), &wnd->handle.xid,
412                                  4))
413                     {
414                         msg_Dbg (wnd, "asking Matchbox for input focus");
415                         xcb_set_input_focus (conn,
416                                              XCB_INPUT_FOCUS_POINTER_ROOT,
417                                              wnd->handle.xid, pne->time);
418                         xcb_flush (conn);
419                     }
420                     free (r);
421                 }
422             }
423             else
424 #endif
425                 msg_Dbg (wnd, "unhandled event: %"PRIu8, ev->response_type);
426             free (ev);
427         }
428         vlc_restorecancel (canc);
429
430         if (xcb_connection_has_error (conn))
431         {
432             msg_Err (wnd, "X server failure");
433             break;
434         }
435     }
436     return NULL;
437 }
438
439 /** Changes the EWMH state of the window */
440 static void set_wm_state (vout_window_t *wnd, bool on, xcb_atom_t state)
441 {
442     vout_window_sys_t *sys = wnd->sys;
443     /* From EWMH "_WM_STATE" */
444     xcb_client_message_event_t ev = {
445          .response_type = XCB_CLIENT_MESSAGE,
446          .format = 32,
447          .window = wnd->handle.xid,
448          .type = sys->wm_state,
449     };
450
451     ev.data.data32[0] = on ? NET_WM_STATE_ADD : NET_WM_STATE_REMOVE;
452     ev.data.data32[1] = state;
453     ev.data.data32[2] = 0;
454     ev.data.data32[3] = 1;
455
456     /* From ICCCM "Changing Window State" */
457     xcb_send_event (sys->conn, 0, sys->root,
458                     XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
459                     XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
460                     (const char *)&ev);
461 }
462
463
464 static int Control (vout_window_t *wnd, int cmd, va_list ap)
465 {
466     vout_window_sys_t *p_sys = wnd->sys;
467     xcb_connection_t *conn = p_sys->conn;
468
469     switch (cmd)
470     {
471         case VOUT_WINDOW_SET_SIZE:
472         {
473             unsigned width = va_arg (ap, unsigned);
474             unsigned height = va_arg (ap, unsigned);
475             const uint32_t values[] = { width, height, };
476
477             xcb_configure_window (conn, wnd->handle.xid,
478                                   XCB_CONFIG_WINDOW_WIDTH |
479                                   XCB_CONFIG_WINDOW_HEIGHT, values);
480             break;
481         }
482
483         case VOUT_WINDOW_SET_ON_TOP:
484             set_wm_state (wnd, va_arg (ap, int), p_sys->wm_state_above);
485             break;
486
487         case VOUT_WINDOW_SET_FULLSCREEN:
488             set_wm_state (wnd, va_arg (ap, int), p_sys->wm_state_fullscreen);
489             break;
490
491         default:
492             msg_Err (wnd, "request %d not implemented", cmd);
493             return VLC_EGENERIC;
494     }
495     xcb_flush (p_sys->conn);
496     return VLC_SUCCESS;
497 }
498
499 /*** Embedded drawable support ***/
500
501 static vlc_mutex_t serializer = VLC_STATIC_MUTEX;
502
503 /** Acquire a drawable */
504 static int AcquireDrawable (vlc_object_t *obj, xcb_window_t window)
505 {
506     xcb_window_t *used;
507     size_t n = 0;
508
509     if (var_Create (obj->p_libvlc, "xid-in-use", VLC_VAR_ADDRESS))
510         return VLC_ENOMEM;
511
512     /* Keep a list of busy drawables, so we don't overlap videos if there are
513      * more than one video track in the stream. */
514     vlc_mutex_lock (&serializer);
515     used = var_GetAddress (obj->p_libvlc, "xid-in-use");
516     if (used != NULL)
517     {
518         while (used[n])
519         {
520             if (used[n] == window)
521                 goto skip;
522             n++;
523         }
524     }
525
526     used = realloc (used, sizeof (*used) * (n + 2));
527     if (used != NULL)
528     {
529         used[n] = window;
530         used[n + 1] = 0;
531         var_SetAddress (obj->p_libvlc, "xid-in-use", used);
532     }
533     else
534     {
535 skip:
536         msg_Warn (obj, "X11 drawable 0x%08"PRIx8" is busy", window);
537         window = 0;
538     }
539     vlc_mutex_unlock (&serializer);
540
541     return (window == 0) ? VLC_EGENERIC : VLC_SUCCESS;
542 }
543
544 /** Remove this drawable from the list of busy ones */
545 static void ReleaseDrawable (vlc_object_t *obj, xcb_window_t window)
546 {
547     xcb_window_t *used;
548     size_t n = 0;
549
550     vlc_mutex_lock (&serializer);
551     used = var_GetAddress (obj->p_libvlc, "xid-in-use");
552     assert (used);
553     while (used[n] != window)
554     {
555         assert (used[n]);
556         n++;
557     }
558     do
559         used[n] = used[n + 1];
560     while (used[++n]);
561
562     if (n == 0)
563          var_SetAddress (obj->p_libvlc, "xid-in-use", NULL);
564     vlc_mutex_unlock (&serializer);
565
566     if (n == 0)
567         free (used);
568     /* Variables are reference-counted... */
569     var_Destroy (obj->p_libvlc, "xid-in-use");
570 }
571
572 /**
573  * Wrap an existing X11 window to embed the video.
574  */
575 static int EmOpen (vlc_object_t *obj)
576 {
577     vout_window_t *wnd = (vout_window_t *)obj;
578
579     xcb_window_t window = var_CreateGetInteger (obj, "drawable-xid");
580     if (window == 0)
581         return VLC_EGENERIC;
582     var_Destroy (obj, "drawable-xid");
583
584     if (AcquireDrawable (obj, window))
585         return VLC_EGENERIC;
586
587     vout_window_sys_t *p_sys = malloc (sizeof (*p_sys));
588     xcb_connection_t *conn = xcb_connect (NULL, NULL);
589     if (p_sys == NULL || xcb_connection_has_error (conn))
590         goto error;
591
592     wnd->handle.xid = window;
593     wnd->control = Control;
594     wnd->sys = p_sys;
595
596     p_sys->conn = conn;
597
598     xcb_get_geometry_reply_t *geo =
599         xcb_get_geometry_reply (conn, xcb_get_geometry (conn, window), NULL);
600     if (geo == NULL)
601     {
602         msg_Err (obj, "bad X11 window 0x%08"PRIx8, window);
603         goto error;
604     }
605     p_sys->root = geo->root;
606     free (geo);
607
608     if (var_CreateGetInteger (obj, "vout-event") != 3) /* FIXME: <- cleanup */
609     {
610         p_sys->keys = CreateKeyHandler (obj, conn);
611         if (p_sys->keys != NULL)
612         {
613             const uint32_t mask = XCB_CW_EVENT_MASK;
614             const uint32_t values[1] = {
615                 XCB_EVENT_MASK_KEY_PRESS,
616             };
617             xcb_change_window_attributes (conn, window, mask, values);
618         }
619     }
620
621     CacheAtoms (p_sys);
622     if ((p_sys->keys != NULL)
623      && vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
624         DestroyKeyHandler (p_sys->keys);
625
626     xcb_flush (conn);
627
628     return VLC_SUCCESS;
629
630 error:
631     xcb_disconnect (conn);
632     free (p_sys);
633     ReleaseDrawable (obj, window);
634     return VLC_EGENERIC;
635 }
636
637 static void EmClose (vlc_object_t *obj)
638 {
639     vout_window_t *wnd = (vout_window_t *)obj;
640     xcb_window_t window = wnd->handle.xid;
641
642     Close (obj);
643     ReleaseDrawable (obj, window);
644 }