]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/window.c
7f43abc3d0ff703b924c86f35ac19559ce0e20bb
[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 #include <xcb/xcb_aux.h>
35 typedef xcb_atom_t Atom;
36 #include <X11/Xatom.h> /* XA_WM_NAME */
37
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
40 #include <vlc_window.h>
41
42 #include "xcb_vlc.h"
43
44 #define DISPLAY_TEXT N_("X11 display")
45 #define DISPLAY_LONGTEXT N_( \
46     "X11 hardware display to use. By default VLC will " \
47     "use the value of the DISPLAY environment variable.")
48
49 static int  Open (vlc_object_t *);
50 static void Close (vlc_object_t *);
51
52 /*
53  * Module descriptor
54  */
55 vlc_module_begin ()
56     set_shortname (N_("XCB window"))
57     set_description (N_("(Experimental) XCB video window"))
58     set_category (CAT_VIDEO)
59     set_subcategory (SUBCAT_VIDEO_VOUT)
60     set_capability ("xwindow", 10)
61     set_callbacks (Open, Close)
62
63     add_string ("x11-display", NULL, NULL,
64                 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
65 vlc_module_end ()
66
67 static int Control (vout_window_t *, int, va_list ap);
68 static void *Thread (void *);
69
70 struct vout_window_sys_t
71 {
72     xcb_connection_t *conn;
73     key_handler_t *keys;
74     vlc_thread_t thread;
75
76     xcb_window_t root;
77     xcb_atom_t wm_state;
78     xcb_atom_t wm_state_above;
79     /*xcb_atom_t wmstate_fullscreen;*/
80 };
81
82 static inline
83 void set_ascii_prop (xcb_connection_t *conn, xcb_window_t window,
84                      xcb_atom_t atom, const char *value)
85 {
86     xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, atom,
87                          XA_STRING, 8, strlen (value), value);
88 }
89
90 static inline
91 void set_hostname_prop (xcb_connection_t *conn, xcb_window_t window)
92 {
93     char hostname[HOST_NAME_MAX];
94
95     if (gethostname (hostname, sizeof (hostname)) == 0)
96     {
97         hostname[sizeof (hostname) - 1] = '\0';
98         set_ascii_prop (conn, window, XA_WM_CLIENT_MACHINE, hostname);
99     }
100 }
101
102 static
103 xcb_atom_t get_atom (xcb_connection_t *conn, xcb_intern_atom_cookie_t ck)
104 {
105     xcb_intern_atom_reply_t *reply;
106     xcb_atom_t atom;
107
108     reply = xcb_intern_atom_reply (conn, ck, NULL);
109     if (reply == NULL)
110         return 0;
111
112     atom = reply->atom;
113     free (reply);
114     return atom;
115 }
116
117 /**
118  * Create an X11 window.
119  */
120 static int Open (vlc_object_t *obj)
121 {
122     vout_window_t *wnd = (vout_window_t *)obj;
123     vout_window_sys_t *p_sys = malloc (sizeof (*p_sys));
124     xcb_generic_error_t *err;
125     xcb_void_cookie_t ck;
126
127     if (p_sys == NULL)
128         return VLC_ENOMEM;
129
130     /* Connect to X */
131     char *display = var_CreateGetNonEmptyString (wnd, "x11-display");
132     int snum;
133
134     xcb_connection_t *conn = xcb_connect (display, &snum);
135     free (display);
136     if (xcb_connection_has_error (conn) /*== NULL*/)
137         goto error;
138
139     /* Create window */
140     xcb_screen_t *scr = xcb_aux_get_screen (conn, snum);
141     const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
142     uint32_t values[2] = {
143         /* XCB_CW_BACK_PIXEL */
144         scr->black_pixel,
145         /* XCB_CW_EVENT_MASK */
146         XCB_EVENT_MASK_KEY_PRESS,
147     };
148
149     xcb_window_t window = xcb_generate_id (conn);
150     ck = xcb_create_window_checked (conn, scr->root_depth, window, scr->root,
151                                     0, 0, wnd->width, wnd->height, 0,
152                                     XCB_WINDOW_CLASS_INPUT_OUTPUT,
153                                     scr->root_visual, mask, values);
154     err = xcb_request_check (conn, ck);
155     if (err)
156     {
157         msg_Err (wnd, "creating window: X11 error %d", err->error_code);
158         goto error;
159     }
160
161     /* ICCCM
162      * No cut&paste nor drag&drop, only Window Manager communication. */
163     /* Plain ASCII localization of VLC for ICCCM window name */
164     set_ascii_prop (conn, window, XA_WM_NAME,
165                     pgettext ("ASCII VLC media player", "VLC media player"));
166     set_ascii_prop (conn, window, XA_WM_ICON_NAME,
167                     pgettext ("ASCII VLC", "VLC"));
168     xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, XA_WM_CLASS,
169                          XA_STRING, 8, 8, "vlc\0Vlc");
170     set_hostname_prop (conn, window);
171
172     wnd->handle.xid = window;
173     wnd->p_sys = p_sys;
174     wnd->control = Control;
175
176     p_sys->conn = conn;
177     p_sys->keys = CreateKeyHandler (obj, conn);
178     p_sys->root = scr->root;
179
180     /* Cache any EWMH atom we may need later */
181     xcb_intern_atom_cookie_t wm_state_ck, wm_state_above_ck;
182
183     wm_state_ck = xcb_intern_atom (conn, 0, 13, "_NET_WM_STATE");
184     wm_state_above_ck = xcb_intern_atom (conn, 0, 18, "_NET_WM_STATE_ABOVE");
185
186     p_sys->wm_state = get_atom (conn, wm_state_ck);
187     p_sys->wm_state_above = get_atom (conn, wm_state_above_ck);
188
189     /* Create the event thread. It will dequeue all events, so any checked
190      * request from this thread must be completed at this point. */
191     if ((p_sys->keys != NULL)
192      && vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
193         DestroyKeyHandler (p_sys->keys);
194
195     /* Make sure the window is ready */
196     xcb_map_window (conn, window);
197     xcb_flush (conn);
198
199     return VLC_SUCCESS;
200
201 error:
202     xcb_disconnect (conn);
203     free (p_sys);
204     return VLC_EGENERIC;
205 }
206
207
208 /**
209  * Destroys the X11 window.
210  */
211 static void Close (vlc_object_t *obj)
212 {
213     vout_window_t *wnd = (vout_window_t *)obj;
214     vout_window_sys_t *p_sys = wnd->p_sys;
215     xcb_connection_t *conn = p_sys->conn;
216     xcb_window_t window = wnd->handle.xid;
217
218     if (p_sys->keys)
219     {
220         vlc_cancel (p_sys->thread);
221         vlc_join (p_sys->thread, NULL);
222         DestroyKeyHandler (p_sys->keys);
223     }
224     xcb_unmap_window (conn, window);
225     xcb_destroy_window (conn, window);
226     xcb_disconnect (conn);
227     free (p_sys);
228 }
229
230
231 static void *Thread (void *data)
232 {
233     vout_window_t *wnd = data;
234     vout_window_sys_t *p_sys = wnd->p_sys;
235     xcb_connection_t *conn = p_sys->conn;
236
237     int fd = xcb_get_file_descriptor (conn);
238     if (fd == -1)
239         return NULL;
240
241     for (;;)
242     {
243         xcb_generic_event_t *ev;
244         struct pollfd ufd = { .fd = fd, .events = POLLIN, };
245
246         poll (&ufd, 1, -1);
247
248         int canc = vlc_savecancel ();
249         while ((ev = xcb_poll_for_event (conn)) != NULL)
250         {
251             if (ProcessKeyEvent (p_sys->keys, ev) == 0)
252                 continue;
253             msg_Dbg (wnd, "unhandled event: %"PRIu8, ev->response_type);
254             free (ev);
255         }
256         vlc_restorecancel (canc);
257
258         if (xcb_connection_has_error (conn))
259         {
260             msg_Err (wnd, "X server failure");
261             break;
262         }
263     }
264     return NULL;
265 }
266
267 #include <vlc_vout.h>
268
269 static int Control (vout_window_t *wnd, int cmd, va_list ap)
270 {
271     vout_window_sys_t *p_sys = wnd->p_sys;
272     xcb_connection_t *conn = p_sys->conn;
273
274     switch (cmd)
275     {
276         case VOUT_SET_SIZE:
277         {
278             unsigned width = va_arg (ap, unsigned);
279             unsigned height = va_arg (ap, unsigned);
280             const uint32_t values[] = { width, height, };
281
282             xcb_configure_window (conn, wnd->handle.xid,
283                                   XCB_CONFIG_WINDOW_WIDTH |
284                                   XCB_CONFIG_WINDOW_HEIGHT, values);
285             xcb_flush (conn);
286             break;
287         }
288
289         case VOUT_SET_STAY_ON_TOP:
290         {   /* From EWMH "_WM_STATE" */
291             xcb_client_message_event_t ev = {
292                 .response_type = 0x80 | XCB_CLIENT_MESSAGE,
293                 .format = 32,
294                 .window = wnd->handle.xid,
295                 .type = p_sys->wm_state,
296             };
297             bool on = va_arg (ap, int);
298
299             ev.data.data32[0] = on;
300             ev.data.data32[1] = p_sys->wm_state_above;
301             ev.data.data32[1] = 289;
302             ev.data.data32[3] = 1;
303
304             /* From ICCCM "Changing Window State" */
305             xcb_send_event (p_sys->conn, 0, p_sys->root,
306                             XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
307                             XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
308                             (const char *)&ev);
309             xcb_flush (p_sys->conn);
310             return VLC_SUCCESS;
311         }
312
313         default:
314             msg_Err (wnd, "request %d not implemented", cmd);
315             return VLC_EGENERIC;
316     }
317     return VLC_SUCCESS;
318 }
319