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