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