3 * @brief X C Bindings window provider module for VLC media player
5 /*****************************************************************************
6 * Copyright © 2009 Rémi Denis-Courmont
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.
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.
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 ****************************************************************************/
30 #include <unistd.h> /* gethostname() and sysconf() */
31 #include <limits.h> /* _POSIX_HOST_NAME_MAX */
34 typedef xcb_atom_t Atom;
35 #include <X11/Xatom.h> /* XA_WM_NAME */
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_vout_window.h>
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.")
48 static int Open (vlc_object_t *);
49 static void Close (vlc_object_t *);
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)
62 add_string ("x11-display", NULL, NULL,
63 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
66 static int Control (vout_window_t *, int, va_list ap);
67 static void *Thread (void *);
69 struct vout_window_sys_t
71 xcb_connection_t *conn;
77 xcb_atom_t wm_state_above;
78 /*xcb_atom_t wmstate_fullscreen;*/
81 /** Set an X window property from a nul-terminated string */
83 void set_string (xcb_connection_t *conn, xcb_window_t window,
84 xcb_atom_t type, xcb_atom_t atom, const char *str)
86 xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, atom, type,
87 /* format */ 8, strlen (str), str);
90 /** Set an X window string property */
92 void set_ascii_prop (xcb_connection_t *conn, xcb_window_t window,
93 xcb_atom_t atom, const char *value)
95 set_string (conn, window, atom, XA_STRING, value);
98 /** Set the Window ICCCM client machine property */
100 void set_hostname_prop (xcb_connection_t *conn, xcb_window_t window)
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;
108 if (gethostname (hostname, host_name_max) == 0)
110 hostname[host_name_max - 1] = '\0';
111 set_ascii_prop (conn, window, XA_WM_CLIENT_MACHINE, hostname);
116 /** Request the X11 server to internalize a string into an atom */
118 xcb_intern_atom_cookie_t intern_string (xcb_connection_t *c, const char *s)
120 return xcb_intern_atom (c, 0, strlen (s), s);
123 /** Extract the X11 atom from an intern request cookie */
125 xcb_atom_t get_atom (xcb_connection_t *conn, xcb_intern_atom_cookie_t ck)
127 xcb_intern_atom_reply_t *reply;
130 reply = xcb_intern_atom_reply (conn, ck, NULL);
139 #define NET_WM_STATE_REMOVE 0
140 #define NET_WM_STATE_ADD 1
141 #define NET_WM_STATE_TOGGLE 2
144 * Create an X11 window.
146 static int Open (vlc_object_t *obj)
148 vout_window_t *wnd = (vout_window_t *)obj;
149 xcb_generic_error_t *err;
150 xcb_void_cookie_t ck;
152 vout_window_sys_t *p_sys = malloc (sizeof (*p_sys));
157 char *display = var_CreateGetNonEmptyString (wnd, "x11-display");
160 xcb_connection_t *conn = xcb_connect (display, &snum);
162 if (xcb_connection_has_error (conn) /*== NULL*/)
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))
180 msg_Err (wnd, "bad X11 screen number");
185 const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
186 uint32_t values[2] = {
187 /* XCB_CW_BACK_PIXEL */
189 /* XCB_CW_EVENT_MASK */
190 XCB_EVENT_MASK_KEY_PRESS,
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);
201 msg_Err (wnd, "creating window: X11 error %d", err->error_code);
205 wnd->handle.xid = window;
206 wnd->control = Control;
210 p_sys->keys = CreateKeyHandler (obj, conn);
211 p_sys->root = scr->root;
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);
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");
232 xcb_atom_t utf8 = get_atom (conn, utf8_string_ck);
234 xcb_atom_t net_wm_name = get_atom (conn, net_wm_name_ck);
235 char *title = var_CreateGetNonEmptyString (wnd, "video-title");
238 set_string (conn, window, utf8, net_wm_name, title);
242 set_string (conn, window, utf8, net_wm_name, _("VLC media player"));
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"));
247 /* Cache any EWMH atom we may need later */
248 xcb_intern_atom_cookie_t wm_state_ck, wm_state_above_ck;
250 wm_state_ck = intern_string (conn, "_NET_WM_STATE");
251 wm_state_above_ck = intern_string (conn, "_NET_WM_STATE_ABOVE");
253 p_sys->wm_state = get_atom (conn, wm_state_ck);
254 p_sys->wm_state_above = get_atom (conn, wm_state_above_ck);
256 /* Create the event thread. It will dequeue all events, so any checked
257 * request from this thread must be completed at this point. */
258 if ((p_sys->keys != NULL)
259 && vlc_clone (&p_sys->thread, Thread, wnd, VLC_THREAD_PRIORITY_LOW))
260 DestroyKeyHandler (p_sys->keys);
262 /* Make sure the window is ready */
263 xcb_map_window (conn, window);
269 xcb_disconnect (conn);
276 * Destroys the X11 window.
278 static void Close (vlc_object_t *obj)
280 vout_window_t *wnd = (vout_window_t *)obj;
281 vout_window_sys_t *p_sys = wnd->sys;
282 xcb_connection_t *conn = p_sys->conn;
283 xcb_window_t window = wnd->handle.xid;
287 vlc_cancel (p_sys->thread);
288 vlc_join (p_sys->thread, NULL);
289 DestroyKeyHandler (p_sys->keys);
291 xcb_unmap_window (conn, window);
292 xcb_destroy_window (conn, window);
293 xcb_disconnect (conn);
298 /** Background thread for X11 events handling */
299 static void *Thread (void *data)
301 vout_window_t *wnd = data;
302 vout_window_sys_t *p_sys = wnd->sys;
303 xcb_connection_t *conn = p_sys->conn;
305 int fd = xcb_get_file_descriptor (conn);
311 xcb_generic_event_t *ev;
312 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
316 int canc = vlc_savecancel ();
317 while ((ev = xcb_poll_for_event (conn)) != NULL)
319 if (ProcessKeyEvent (p_sys->keys, ev) == 0)
321 msg_Dbg (wnd, "unhandled event: %"PRIu8, ev->response_type);
324 vlc_restorecancel (canc);
326 if (xcb_connection_has_error (conn))
328 msg_Err (wnd, "X server failure");
335 /** Changes the EWMH state of the window */
336 static void set_wm_state (vout_window_t *wnd, bool on, xcb_atom_t state)
338 vout_window_sys_t *sys = wnd->sys;
339 /* From EWMH "_WM_STATE" */
340 xcb_client_message_event_t ev = {
341 .response_type = XCB_CLIENT_MESSAGE,
343 .window = wnd->handle.xid,
344 .type = sys->wm_state,
347 ev.data.data32[0] = on ? NET_WM_STATE_ADD : NET_WM_STATE_REMOVE;
348 ev.data.data32[1] = state;
349 ev.data.data32[2] = 0;
350 ev.data.data32[3] = 1;
352 /* From ICCCM "Changing Window State" */
353 xcb_send_event (sys->conn, 0, sys->root,
354 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
355 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
360 static int Control (vout_window_t *wnd, int cmd, va_list ap)
362 vout_window_sys_t *p_sys = wnd->sys;
363 xcb_connection_t *conn = p_sys->conn;
367 case VOUT_WINDOW_SET_SIZE:
369 unsigned width = va_arg (ap, unsigned);
370 unsigned height = va_arg (ap, unsigned);
371 const uint32_t values[] = { width, height, };
373 xcb_configure_window (conn, wnd->handle.xid,
374 XCB_CONFIG_WINDOW_WIDTH |
375 XCB_CONFIG_WINDOW_HEIGHT, values);
380 case VOUT_WINDOW_SET_ON_TOP:
382 bool on = va_arg (ap, int);
384 set_wm_state (wnd, on, p_sys->wm_state_above);
385 xcb_flush (p_sys->conn);
390 msg_Err (wnd, "request %d not implemented", cmd);