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