3 * @brief X C Bindings VLC video output events handling
5 /*****************************************************************************
6 * Copyright © 2009 Rémi Denis-Courmont
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program 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 Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_vout_display.h>
40 int CheckError (vout_display_t *vd, xcb_connection_t *conn,
41 const char *str, xcb_void_cookie_t ck)
43 xcb_generic_error_t *err;
45 err = xcb_request_check (conn, ck);
48 int code = err->error_code;
51 msg_Err (vd, "%s: X11 error %d", str, code);
59 * Connect to the X server.
61 static xcb_connection_t *Connect (vlc_object_t *obj, const char *display)
63 xcb_connection_t *conn = xcb_connect (display, NULL);
64 if (xcb_connection_has_error (conn) /*== NULL*/)
66 msg_Err (obj, "cannot connect to X server (%s)",
67 (display != NULL) ? display : "default");
68 xcb_disconnect (conn);
72 const xcb_setup_t *setup = xcb_get_setup (conn);
73 msg_Dbg (obj, "connected to X%"PRIu16".%"PRIu16" server",
74 setup->protocol_major_version, setup->protocol_minor_version);
75 char *vendor = strndup (xcb_setup_vendor (setup), setup->vendor_len);
78 msg_Dbg (obj, " vendor : %s", vendor);
81 msg_Dbg (obj, " version: %"PRIu32, setup->release_number);
86 * (Try to) register to mouse events on a window if needed.
88 static void RegisterEvents (vlc_object_t *obj, xcb_connection_t *conn,
91 /* Subscribe to parent window resize events */
92 uint32_t value = XCB_EVENT_MASK_POINTER_MOTION
93 | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
94 xcb_change_window_attributes (conn, wnd, XCB_CW_EVENT_MASK, &value);
95 /* Try to subscribe to click events */
96 /* (only one X11 client can get them, so might not work) */
97 if (var_InheritBool (obj, "mouse-events"))
99 value |= XCB_EVENT_MASK_BUTTON_PRESS
100 | XCB_EVENT_MASK_BUTTON_RELEASE;
101 xcb_change_window_attributes (conn, wnd,
102 XCB_CW_EVENT_MASK, &value);
107 * Find screen matching a given root window.
109 static const xcb_screen_t *FindScreen (vlc_object_t *obj,
110 xcb_connection_t *conn,
113 /* Find the selected screen */
114 const xcb_setup_t *setup = xcb_get_setup (conn);
115 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
116 i.rem > 0; xcb_screen_next (&i))
118 if (i.data->root == root)
120 msg_Dbg (obj, "using screen 0x%"PRIx32, root);
124 msg_Err (obj, "window screen not found");
129 * Create a VLC video X window object, connect to the corresponding X server,
130 * find the corresponding X server screen.
132 vout_window_t *GetWindow (vout_display_t *vd,
133 xcb_connection_t **restrict pconn,
134 const xcb_screen_t **restrict pscreen,
135 uint8_t *restrict pdepth,
136 uint16_t *restrict pwidth,
137 uint16_t *restrict pheight)
139 vout_window_cfg_t cfg = {
140 .type = VOUT_WINDOW_TYPE_XID,
141 .x = var_InheritInteger (vd, "video-x"),
142 .y = var_InheritInteger (vd, "video-y"),
143 .width = vd->cfg->display.width,
144 .height = vd->cfg->display.height,
147 vout_window_t *wnd = vout_display_NewWindow (vd, &cfg);
150 msg_Err (vd, "window not available");
154 xcb_connection_t *conn = Connect (VLC_OBJECT(vd), wnd->display.x11);
159 /* Events must be registered before the window geometry is queried, so as
160 * to avoid missing impeding resize events. */
161 RegisterEvents (VLC_OBJECT(vd), conn, wnd->handle.xid);
163 xcb_get_geometry_reply_t *geo =
164 xcb_get_geometry_reply (conn, xcb_get_geometry (conn, wnd->handle.xid),
168 msg_Err (vd, "window not valid");
171 *pdepth = geo->depth;
172 *pwidth = geo->width;
173 *pheight = geo->height;
175 const xcb_screen_t *screen = FindScreen (VLC_OBJECT(vd), conn, geo->root);
184 xcb_disconnect (conn);
185 vout_display_DeleteWindow (vd, wnd);
190 * Create a blank cursor.
191 * Note that the pixmaps are leaked (until the X disconnection). Hence, this
192 * function should be called no more than once per X connection.
193 * @param conn XCB connection
194 * @param scr target XCB screen
196 xcb_cursor_t CreateBlankCursor (xcb_connection_t *conn,
197 const xcb_screen_t *scr)
199 xcb_cursor_t cur = xcb_generate_id (conn);
200 xcb_pixmap_t pix = xcb_generate_id (conn);
202 xcb_create_pixmap (conn, 1, pix, scr->root, 1, 1);
203 xcb_create_cursor (conn, cur, pix, pix, 0, 0, 0, 0, 0, 0, 1, 1);
207 /* NOTE: we assume no other thread will be _setting_ our video output events
208 * variables. Afterall, only this plugin is supposed to know when these occur.
209 * Otherwise, we'd var_OrInteger() and var_NandInteger() functions...
212 /* FIXME we assume direct mapping between XCB and VLC */
213 static void HandleButtonPress (vout_display_t *vd,
214 const xcb_button_press_event_t *ev)
216 vout_display_SendEventMousePressed (vd, ev->detail - 1);
219 static void HandleButtonRelease (vout_display_t *vd,
220 const xcb_button_release_event_t *ev)
222 vout_display_SendEventMouseReleased (vd, ev->detail - 1);
225 static void HandleMotionNotify (vout_display_t *vd, xcb_connection_t *conn,
226 const xcb_motion_notify_event_t *ev)
228 vout_display_place_t place;
230 /* show the default cursor */
231 xcb_change_window_attributes (conn, ev->event, XCB_CW_CURSOR,
232 &(uint32_t) { XCB_CURSOR_NONE });
235 /* TODO it could be saved */
236 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
238 if (place.width <= 0 || place.height <= 0)
241 const int x = vd->source.i_x_offset +
242 (int64_t)(ev->event_x - place.x) * vd->source.i_visible_width / place.width;
243 const int y = vd->source.i_y_offset +
244 (int64_t)(ev->event_y - place.y) * vd->source.i_visible_height/ place.height;
246 vout_display_SendEventMouseMoved (vd, x, y);
249 static void HandleVisibilityNotify (vout_display_t *vd, bool *visible,
250 const xcb_visibility_notify_event_t *ev)
252 *visible = ev->state != XCB_VISIBILITY_FULLY_OBSCURED;
253 msg_Dbg (vd, "display is %svisible", *visible ? "" : "not ");
257 HandleParentStructure (vout_display_t *vd,
258 const xcb_configure_notify_event_t *ev)
260 vout_display_SendEventDisplaySize (vd, ev->width, ev->height, vd->cfg->is_fullscreen);
264 * Process an X11 event.
266 static int ProcessEvent (vout_display_t *vd, xcb_connection_t *conn,
267 bool *visible, xcb_generic_event_t *ev)
269 switch (ev->response_type & 0x7f)
271 case XCB_BUTTON_PRESS:
272 HandleButtonPress (vd, (xcb_button_press_event_t *)ev);
275 case XCB_BUTTON_RELEASE:
276 HandleButtonRelease (vd, (xcb_button_release_event_t *)ev);
279 case XCB_MOTION_NOTIFY:
280 HandleMotionNotify (vd, conn, (xcb_motion_notify_event_t *)ev);
283 case XCB_VISIBILITY_NOTIFY:
284 HandleVisibilityNotify (vd, visible,
285 (xcb_visibility_notify_event_t *)ev);
288 case XCB_CONFIGURE_NOTIFY:
289 HandleParentStructure (vd, (xcb_configure_notify_event_t *)ev);
292 /* FIXME I am not sure it is the right one */
293 case XCB_DESTROY_NOTIFY:
294 vout_display_SendEventClose (vd);
297 case XCB_MAPPING_NOTIFY:
301 msg_Dbg (vd, "unhandled event %"PRIu8, ev->response_type);
309 * Process incoming X events.
311 int ManageEvent (vout_display_t *vd, xcb_connection_t *conn, bool *visible)
313 xcb_generic_event_t *ev;
315 while ((ev = xcb_poll_for_event (conn)) != NULL)
316 ProcessEvent (vd, conn, visible, ev);
318 if (xcb_connection_has_error (conn))
320 msg_Err (vd, "X server failure");