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() */
31 #include <limits.h> /* 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_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 ("xwindow", 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)
99 char hostname[HOST_NAME_MAX];
101 if (gethostname (hostname, sizeof (hostname)) == 0)
103 hostname[sizeof (hostname) - 1] = '\0';
104 set_ascii_prop (conn, window, XA_WM_CLIENT_MACHINE, hostname);
109 xcb_intern_atom_cookie_t intern_string (xcb_connection_t *c, const char *s)
111 return xcb_intern_atom (c, 0, strlen (s), s);
115 xcb_atom_t get_atom (xcb_connection_t *conn, xcb_intern_atom_cookie_t ck)
117 xcb_intern_atom_reply_t *reply;
120 reply = xcb_intern_atom_reply (conn, ck, NULL);
129 #define NET_WM_STATE_REMOVE 0
130 #define NET_WM_STATE_ADD 1
131 #define NET_WM_STATE_TOGGLE 2
134 * Create an X11 window.
136 static int Open (vlc_object_t *obj)
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;
147 char *display = var_CreateGetNonEmptyString (wnd, "x11-display");
150 xcb_connection_t *conn = xcb_connect (display, &snum);
152 if (xcb_connection_has_error (conn) /*== NULL*/)
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))
170 msg_Err (wnd, "bad X11 screen number");
175 const uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
176 uint32_t values[2] = {
177 /* XCB_CW_BACK_PIXEL */
179 /* XCB_CW_EVENT_MASK */
180 XCB_EVENT_MASK_KEY_PRESS,
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);
191 msg_Err (wnd, "creating window: X11 error %d", err->error_code);
195 wnd->handle.xid = window;
197 wnd->control = Control;
200 p_sys->keys = CreateKeyHandler (obj, conn);
201 p_sys->root = scr->root;
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);
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");
222 xcb_atom_t utf8 = get_atom (conn, utf8_string_ck);
224 xcb_atom_t net_wm_name = get_atom (conn, net_wm_name_ck);
225 char *title = var_CreateGetNonEmptyString (wnd, "video-title");
228 set_string (conn, window, utf8, net_wm_name, title);
232 set_string (conn, window, utf8, net_wm_name, _("VLC media player"));
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"));
237 /* Cache any EWMH atom we may need later */
238 xcb_intern_atom_cookie_t wm_state_ck, wm_state_above_ck;
240 wm_state_ck = intern_string (conn, "_NET_WM_STATE");
241 wm_state_above_ck = intern_string (conn, "_NET_WM_STATE_ABOVE");
243 p_sys->wm_state = get_atom (conn, wm_state_ck);
244 p_sys->wm_state_above = get_atom (conn, wm_state_above_ck);
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);
252 /* Make sure the window is ready */
253 xcb_map_window (conn, window);
259 xcb_disconnect (conn);
266 * Destroys the X11 window.
268 static void Close (vlc_object_t *obj)
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;
277 vlc_cancel (p_sys->thread);
278 vlc_join (p_sys->thread, NULL);
279 DestroyKeyHandler (p_sys->keys);
281 xcb_unmap_window (conn, window);
282 xcb_destroy_window (conn, window);
283 xcb_disconnect (conn);
288 static void *Thread (void *data)
290 vout_window_t *wnd = data;
291 vout_window_sys_t *p_sys = wnd->p_sys;
292 xcb_connection_t *conn = p_sys->conn;
294 int fd = xcb_get_file_descriptor (conn);
300 xcb_generic_event_t *ev;
301 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
305 int canc = vlc_savecancel ();
306 while ((ev = xcb_poll_for_event (conn)) != NULL)
308 if (ProcessKeyEvent (p_sys->keys, ev) == 0)
310 msg_Dbg (wnd, "unhandled event: %"PRIu8, ev->response_type);
313 vlc_restorecancel (canc);
315 if (xcb_connection_has_error (conn))
317 msg_Err (wnd, "X server failure");
324 #include <vlc_vout.h>
326 static int Control (vout_window_t *wnd, int cmd, va_list ap)
328 vout_window_sys_t *p_sys = wnd->p_sys;
329 xcb_connection_t *conn = p_sys->conn;
335 unsigned width = va_arg (ap, unsigned);
336 unsigned height = va_arg (ap, unsigned);
337 const uint32_t values[] = { width, height, };
339 xcb_configure_window (conn, wnd->handle.xid,
340 XCB_CONFIG_WINDOW_WIDTH |
341 XCB_CONFIG_WINDOW_HEIGHT, values);
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,
351 .window = wnd->handle.xid,
352 .type = p_sys->wm_state,
354 bool on = va_arg (ap, int);
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;
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,
366 xcb_flush (p_sys->conn);
371 msg_Err (wnd, "request %d not implemented", cmd);