]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/window.c
xcb_window: implement fullscreen mode
[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 static int  Open (vlc_object_t *);
49 static void Close (vlc_object_t *);
50
51 /*
52  * Module descriptor
53  */
54 vlc_module_begin ()
55     set_shortname (N_("XCB window"))
56     set_description (N_("(Experimental) XCB video window"))
57     set_category (CAT_VIDEO)
58     set_subcategory (SUBCAT_VIDEO_VOUT)
59     set_capability ("vout window xid", 10)
60     set_callbacks (Open, Close)
61
62     add_string ("x11-display", NULL, NULL,
63                 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
64 vlc_module_end ()
65
66 static int Control (vout_window_t *, int, va_list ap);
67 static void *Thread (void *);
68
69 struct vout_window_sys_t
70 {
71     xcb_connection_t *conn;
72     key_handler_t *keys;
73     vlc_thread_t thread;
74
75     xcb_window_t root;
76     xcb_atom_t wm_state;
77     xcb_atom_t wm_state_above;
78     xcb_atom_t wm_state_fullscreen;
79 };
80
81 /** Set an X window property from a nul-terminated string */
82 static inline
83 void set_string (xcb_connection_t *conn, xcb_window_t window,
84                  xcb_atom_t type, xcb_atom_t atom, const char *str)
85 {
86     xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, atom, type,
87                          /* format */ 8, strlen (str), str);
88 }
89
90 /** Set an X window string property */
91 static inline
92 void set_ascii_prop (xcb_connection_t *conn, xcb_window_t window,
93                      xcb_atom_t atom, const char *value)
94 {
95     set_string (conn, window, atom, XA_STRING, value);
96 }
97
98 /** Set the Window ICCCM client machine property */
99 static inline
100 void set_hostname_prop (xcb_connection_t *conn, xcb_window_t window)
101 {
102     char* hostname;
103     long host_name_max = sysconf (_SC_HOST_NAME_MAX);
104     if (host_name_max <= 0) host_name_max = _POSIX_HOST_NAME_MAX;
105     hostname = malloc (host_name_max);
106     if(!hostname) return;
107
108     if (gethostname (hostname, host_name_max) == 0)
109     {
110         hostname[host_name_max - 1] = '\0';
111         set_ascii_prop (conn, window, XA_WM_CLIENT_MACHINE, hostname);
112     }
113     free(hostname);
114 }
115
116 /** Request the X11 server to internalize a string into an atom */
117 static inline
118 xcb_intern_atom_cookie_t intern_string (xcb_connection_t *c, const char *s)
119 {
120     return xcb_intern_atom (c, 0, strlen (s), s);
121 }
122
123 /** Extract the X11 atom from an intern request cookie */
124 static
125 xcb_atom_t get_atom (xcb_connection_t *conn, xcb_intern_atom_cookie_t ck)
126 {
127     xcb_intern_atom_reply_t *reply;
128     xcb_atom_t atom;
129
130     reply = xcb_intern_atom_reply (conn, ck, NULL);
131     if (reply == NULL)
132         return 0;
133
134     atom = reply->atom;
135     free (reply);
136     return atom;
137 }
138
139 #define NET_WM_STATE_REMOVE 0
140 #define NET_WM_STATE_ADD    1
141 #define NET_WM_STATE_TOGGLE 2
142
143 /**
144  * Create an X11 window.
145  */
146 static int Open (vlc_object_t *obj)
147 {
148     vout_window_t *wnd = (vout_window_t *)obj;
149     xcb_generic_error_t *err;
150     xcb_void_cookie_t ck;
151
152     vout_window_sys_t *p_sys = malloc (sizeof (*p_sys));
153     if (p_sys == NULL)
154         return VLC_ENOMEM;
155
156     /* Connect to X */
157     char *display = var_CreateGetNonEmptyString (wnd, "x11-display");
158     int snum;
159
160     xcb_connection_t *conn = xcb_connect (display, &snum);
161     free (display);
162     if (xcb_connection_has_error (conn) /*== NULL*/)
163         goto error;
164
165     /* Find configured screen */
166     const xcb_setup_t *setup = xcb_get_setup (conn);
167     const xcb_screen_t *scr = NULL;
168     for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
169          i.rem > 0; xcb_screen_next (&i))
170     {
171         if (snum == 0)
172         {
173             scr = i.data;
174             break;
175         }
176         snum--;
177     }
178     if (scr == NULL)
179     {
180         msg_Err (wnd, "bad X11 screen number");
181         goto error;
182     }
183
184     /* Create window */
185     const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
186     uint32_t values[2] = {
187         /* XCB_CW_BACK_PIXEL */
188         scr->black_pixel,
189         /* XCB_CW_EVENT_MASK */
190         XCB_EVENT_MASK_KEY_PRESS,
191     };
192
193     xcb_window_t window = xcb_generate_id (conn);
194     ck = xcb_create_window_checked (conn, scr->root_depth, window, scr->root,
195                                     0, 0, wnd->cfg->width, wnd->cfg->height, 0,
196                                     XCB_WINDOW_CLASS_INPUT_OUTPUT,
197                                     scr->root_visual, mask, values);
198     err = xcb_request_check (conn, ck);
199     if (err)
200     {
201         msg_Err (wnd, "creating window: X11 error %d", err->error_code);
202         goto error;
203     }
204
205     wnd->handle.xid = window;
206     wnd->control = Control;
207     wnd->sys = p_sys;
208
209     p_sys->conn = conn;
210     p_sys->keys = CreateKeyHandler (obj, conn);
211     p_sys->root = scr->root;
212
213     /* ICCCM
214      * No cut&paste nor drag&drop, only Window Manager communication. */
215     /* Plain ASCII localization of VLC for ICCCM window name */
216     set_ascii_prop (conn, window, XA_WM_NAME,
217                   vlc_pgettext ("ASCII", "VLC media player"));
218     set_ascii_prop (conn, window, XA_WM_ICON_NAME,
219                     vlc_pgettext ("ASCII", "VLC"));
220     xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, XA_WM_CLASS,
221                          XA_STRING, 8, 8, "vlc\0Vlc");
222     set_hostname_prop (conn, window);
223
224     /* EWMH */
225     xcb_intern_atom_cookie_t utf8_string_ck
226         = intern_string (conn, "UTF8_STRING");;
227     xcb_intern_atom_cookie_t net_wm_name_ck
228         = intern_string (conn, "_NET_WM_NAME");
229     xcb_intern_atom_cookie_t net_wm_icon_name_ck
230         = intern_string (conn, "_NET_WM_ICON_NAME");
231
232     xcb_atom_t utf8 = get_atom (conn, utf8_string_ck);
233
234     xcb_atom_t net_wm_name = get_atom (conn, net_wm_name_ck);
235     char *title = var_CreateGetNonEmptyString (wnd, "video-title");
236     if (title)
237     {
238         set_string (conn, window, utf8, net_wm_name, title);
239         free (title);
240     }
241     else
242         set_string (conn, window, utf8, net_wm_name, _("VLC media player"));
243
244     xcb_atom_t net_wm_icon_name = get_atom (conn, net_wm_icon_name_ck);
245     set_string (conn, window, utf8, net_wm_icon_name, _("VLC"));
246
247     /* Cache any EWMH atom we may need later */
248     xcb_intern_atom_cookie_t wm_state_ck, wm_state_above_ck, wm_state_fs_ck;
249
250     wm_state_ck = intern_string (conn, "_NET_WM_STATE");
251     wm_state_above_ck = intern_string (conn, "_NET_WM_STATE_ABOVE");
252     wm_state_fs_ck = intern_string (conn, "_NET_WM_STATE_FULLSCREEN");
253
254     p_sys->wm_state = get_atom (conn, wm_state_ck);
255     p_sys->wm_state_above = get_atom (conn, wm_state_above_ck);
256     p_sys->wm_state_fullscreen = get_atom (conn, wm_state_fs_ck);
257
258     /* Create the event thread. It will dequeue all events, so any checked
259      * request from this thread must be completed at this point. */
260     if ((p_sys->keys != NULL)
261      && vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
262         DestroyKeyHandler (p_sys->keys);
263
264     /* Make sure the window is ready */
265     xcb_map_window (conn, window);
266     xcb_flush (conn);
267
268     return VLC_SUCCESS;
269
270 error:
271     xcb_disconnect (conn);
272     free (p_sys);
273     return VLC_EGENERIC;
274 }
275
276
277 /**
278  * Destroys the X11 window.
279  */
280 static void Close (vlc_object_t *obj)
281 {
282     vout_window_t *wnd = (vout_window_t *)obj;
283     vout_window_sys_t *p_sys = wnd->sys;
284     xcb_connection_t *conn = p_sys->conn;
285     xcb_window_t window = wnd->handle.xid;
286
287     if (p_sys->keys)
288     {
289         vlc_cancel (p_sys->thread);
290         vlc_join (p_sys->thread, NULL);
291         DestroyKeyHandler (p_sys->keys);
292     }
293     xcb_unmap_window (conn, window);
294     xcb_destroy_window (conn, window);
295     xcb_disconnect (conn);
296     free (p_sys);
297 }
298
299
300 /** Background thread for X11 events handling */
301 static void *Thread (void *data)
302 {
303     vout_window_t *wnd = data;
304     vout_window_sys_t *p_sys = wnd->sys;
305     xcb_connection_t *conn = p_sys->conn;
306
307     int fd = xcb_get_file_descriptor (conn);
308     if (fd == -1)
309         return NULL;
310
311     for (;;)
312     {
313         xcb_generic_event_t *ev;
314         struct pollfd ufd = { .fd = fd, .events = POLLIN, };
315
316         poll (&ufd, 1, -1);
317
318         int canc = vlc_savecancel ();
319         while ((ev = xcb_poll_for_event (conn)) != NULL)
320         {
321             if (ProcessKeyEvent (p_sys->keys, ev) == 0)
322                 continue;
323             msg_Dbg (wnd, "unhandled event: %"PRIu8, ev->response_type);
324             free (ev);
325         }
326         vlc_restorecancel (canc);
327
328         if (xcb_connection_has_error (conn))
329         {
330             msg_Err (wnd, "X server failure");
331             break;
332         }
333     }
334     return NULL;
335 }
336
337 /** Changes the EWMH state of the window */
338 static void set_wm_state (vout_window_t *wnd, bool on, xcb_atom_t state)
339 {
340     vout_window_sys_t *sys = wnd->sys;
341     /* From EWMH "_WM_STATE" */
342     xcb_client_message_event_t ev = {
343          .response_type = XCB_CLIENT_MESSAGE,
344          .format = 32,
345          .window = wnd->handle.xid,
346          .type = sys->wm_state,
347     };
348
349     ev.data.data32[0] = on ? NET_WM_STATE_ADD : NET_WM_STATE_REMOVE;
350     ev.data.data32[1] = state;
351     ev.data.data32[2] = 0;
352     ev.data.data32[3] = 1;
353
354     /* From ICCCM "Changing Window State" */
355     xcb_send_event (sys->conn, 0, sys->root,
356                     XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
357                     XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
358                     (const char *)&ev);
359 }
360
361
362 static int Control (vout_window_t *wnd, int cmd, va_list ap)
363 {
364     vout_window_sys_t *p_sys = wnd->sys;
365     xcb_connection_t *conn = p_sys->conn;
366
367     switch (cmd)
368     {
369         case VOUT_WINDOW_SET_SIZE:
370         {
371             unsigned width = va_arg (ap, unsigned);
372             unsigned height = va_arg (ap, unsigned);
373             const uint32_t values[] = { width, height, };
374
375             xcb_configure_window (conn, wnd->handle.xid,
376                                   XCB_CONFIG_WINDOW_WIDTH |
377                                   XCB_CONFIG_WINDOW_HEIGHT, values);
378             break;
379         }
380
381         case VOUT_WINDOW_SET_ON_TOP:
382             set_wm_state (wnd, va_arg (ap, int), p_sys->wm_state_above);
383             break;
384
385         case VOUT_WINDOW_SET_FULLSCREEN:
386             set_wm_state (wnd, va_arg (ap, int), p_sys->wm_state_fullscreen);
387             break;
388
389         default:
390             msg_Err (wnd, "request %d not implemented", cmd);
391             return VLC_EGENERIC;
392     }
393     xcb_flush (p_sys->conn);
394     return VLC_SUCCESS;
395 }
396