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", 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;*/
82 void set_string (xcb_connection_t *conn, xcb_window_t window,
83 xcb_atom_t type, xcb_atom_t atom, const char *str)
85 xcb_change_property (conn, XCB_PROP_MODE_REPLACE, window, atom, type,
86 /* format */ 8, strlen (str), str);
90 void set_ascii_prop (xcb_connection_t *conn, xcb_window_t window,
91 xcb_atom_t atom, const char *value)
93 set_string (conn, window, atom, XA_STRING, value);
97 void set_hostname_prop (xcb_connection_t *conn, xcb_window_t window)
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;
105 if (gethostname (hostname, host_name_max) == 0)
107 hostname[host_name_max - 1] = '\0';
108 set_ascii_prop (conn, window, XA_WM_CLIENT_MACHINE, hostname);
114 xcb_intern_atom_cookie_t intern_string (xcb_connection_t *c, const char *s)
116 return xcb_intern_atom (c, 0, strlen (s), s);
120 xcb_atom_t get_atom (xcb_connection_t *conn, xcb_intern_atom_cookie_t ck)
122 xcb_intern_atom_reply_t *reply;
125 reply = xcb_intern_atom_reply (conn, ck, NULL);
134 #define NET_WM_STATE_REMOVE 0
135 #define NET_WM_STATE_ADD 1
136 #define NET_WM_STATE_TOGGLE 2
139 * Create an X11 window.
141 static int Open (vlc_object_t *obj)
143 vout_window_t *wnd = (vout_window_t *)obj;
144 xcb_generic_error_t *err;
145 xcb_void_cookie_t ck;
147 if (wnd->cfg->type != VOUT_WINDOW_TYPE_XWINDOW)
150 vout_window_sys_t *p_sys = malloc (sizeof (*p_sys));
155 char *display = var_CreateGetNonEmptyString (wnd, "x11-display");
158 xcb_connection_t *conn = xcb_connect (display, &snum);
160 if (xcb_connection_has_error (conn) /*== NULL*/)
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))
178 msg_Err (wnd, "bad X11 screen number");
183 const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
184 uint32_t values[2] = {
185 /* XCB_CW_BACK_PIXEL */
187 /* XCB_CW_EVENT_MASK */
188 XCB_EVENT_MASK_KEY_PRESS,
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);
199 msg_Err (wnd, "creating window: X11 error %d", err->error_code);
203 wnd->handle.xid = window;
204 wnd->control = Control;
208 p_sys->keys = CreateKeyHandler (obj, conn);
209 p_sys->root = scr->root;
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);
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");
230 xcb_atom_t utf8 = get_atom (conn, utf8_string_ck);
232 xcb_atom_t net_wm_name = get_atom (conn, net_wm_name_ck);
233 char *title = var_CreateGetNonEmptyString (wnd, "video-title");
236 set_string (conn, window, utf8, net_wm_name, title);
240 set_string (conn, window, utf8, net_wm_name, _("VLC media player"));
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"));
245 /* Cache any EWMH atom we may need later */
246 xcb_intern_atom_cookie_t wm_state_ck, wm_state_above_ck;
248 wm_state_ck = intern_string (conn, "_NET_WM_STATE");
249 wm_state_above_ck = intern_string (conn, "_NET_WM_STATE_ABOVE");
251 p_sys->wm_state = get_atom (conn, wm_state_ck);
252 p_sys->wm_state_above = get_atom (conn, wm_state_above_ck);
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);
260 /* Make sure the window is ready */
261 xcb_map_window (conn, window);
267 xcb_disconnect (conn);
274 * Destroys the X11 window.
276 static void Close (vlc_object_t *obj)
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;
285 vlc_cancel (p_sys->thread);
286 vlc_join (p_sys->thread, NULL);
287 DestroyKeyHandler (p_sys->keys);
289 xcb_unmap_window (conn, window);
290 xcb_destroy_window (conn, window);
291 xcb_disconnect (conn);
296 static void *Thread (void *data)
298 vout_window_t *wnd = data;
299 vout_window_sys_t *p_sys = wnd->sys;
300 xcb_connection_t *conn = p_sys->conn;
302 int fd = xcb_get_file_descriptor (conn);
308 xcb_generic_event_t *ev;
309 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
313 int canc = vlc_savecancel ();
314 while ((ev = xcb_poll_for_event (conn)) != NULL)
316 if (ProcessKeyEvent (p_sys->keys, ev) == 0)
318 msg_Dbg (wnd, "unhandled event: %"PRIu8, ev->response_type);
321 vlc_restorecancel (canc);
323 if (xcb_connection_has_error (conn))
325 msg_Err (wnd, "X server failure");
332 #include <vlc_vout.h>
334 static int Control (vout_window_t *wnd, int cmd, va_list ap)
336 vout_window_sys_t *p_sys = wnd->sys;
337 xcb_connection_t *conn = p_sys->conn;
341 case VOUT_WINDOW_SET_SIZE:
343 unsigned width = va_arg (ap, unsigned);
344 unsigned height = va_arg (ap, unsigned);
345 const uint32_t values[] = { width, height, };
347 xcb_configure_window (conn, wnd->handle.xid,
348 XCB_CONFIG_WINDOW_WIDTH |
349 XCB_CONFIG_WINDOW_HEIGHT, values);
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,
359 .window = wnd->handle.xid,
360 .type = p_sys->wm_state,
362 bool on = va_arg (ap, int);
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;
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,
374 xcb_flush (p_sys->conn);
379 msg_Err (wnd, "request %d not implemented", cmd);