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