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 XCB_error_Check (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 msg_Dbg (obj, " vendor : %.*s", (int)setup->vendor_len,
76 xcb_setup_vendor (setup));
77 msg_Dbg (obj, " version: %"PRIu32, setup->release_number);
82 * (Try to) register to mouse events on a window if needed.
84 static void RegisterEvents (vlc_object_t *obj, xcb_connection_t *conn,
87 /* Subscribe to parent window resize events */
88 uint32_t value = XCB_EVENT_MASK_POINTER_MOTION
89 | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
90 xcb_change_window_attributes (conn, wnd, XCB_CW_EVENT_MASK, &value);
91 /* Try to subscribe to click events */
92 /* (only one X11 client can get them, so might not work) */
93 if (var_InheritBool (obj, "mouse-events"))
95 value |= XCB_EVENT_MASK_BUTTON_PRESS
96 | XCB_EVENT_MASK_BUTTON_RELEASE;
97 xcb_change_window_attributes (conn, wnd,
98 XCB_CW_EVENT_MASK, &value);
103 * Find screen matching a given root window.
105 static const xcb_screen_t *FindScreen (vlc_object_t *obj,
106 xcb_connection_t *conn,
109 /* Find the selected screen */
110 const xcb_setup_t *setup = xcb_get_setup (conn);
111 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
112 i.rem > 0; xcb_screen_next (&i))
114 if (i.data->root == root)
116 msg_Dbg (obj, "using screen 0x%"PRIx32, root);
120 msg_Err (obj, "window screen not found");
125 * Create a VLC video X window object, connect to the corresponding X server,
126 * find the corresponding X server screen.
128 vout_window_t *XCB_parent_Create (vout_display_t *vd,
129 xcb_connection_t **restrict pconn,
130 const xcb_screen_t **restrict pscreen,
131 uint16_t *restrict pwidth,
132 uint16_t *restrict pheight)
134 vout_window_cfg_t cfg = {
135 .type = VOUT_WINDOW_TYPE_XID,
136 .x = var_InheritInteger (vd, "video-x"),
137 .y = var_InheritInteger (vd, "video-y"),
138 .width = vd->cfg->display.width,
139 .height = vd->cfg->display.height,
142 vout_window_t *wnd = vout_display_NewWindow (vd, &cfg);
145 msg_Err (vd, "window not available");
149 xcb_connection_t *conn = Connect (VLC_OBJECT(vd), wnd->display.x11);
154 /* Events must be registered before the window geometry is queried, so as
155 * to avoid missing impeding resize events. */
156 RegisterEvents (VLC_OBJECT(vd), conn, wnd->handle.xid);
158 xcb_get_geometry_reply_t *geo =
159 xcb_get_geometry_reply (conn, xcb_get_geometry (conn, wnd->handle.xid),
163 msg_Err (vd, "window not valid");
166 *pwidth = geo->width;
167 *pheight = geo->height;
169 const xcb_screen_t *screen = FindScreen (VLC_OBJECT(vd), conn, geo->root);
178 xcb_disconnect (conn);
179 vout_display_DeleteWindow (vd, wnd);
184 * Create a blank cursor.
185 * Note that the pixmaps are leaked (until the X disconnection). Hence, this
186 * function should be called no more than once per X connection.
187 * @param conn XCB connection
188 * @param scr target XCB screen
190 xcb_cursor_t XCB_cursor_Create (xcb_connection_t *conn,
191 const xcb_screen_t *scr)
193 xcb_cursor_t cur = xcb_generate_id (conn);
194 xcb_pixmap_t pix = xcb_generate_id (conn);
196 xcb_create_pixmap (conn, 1, pix, scr->root, 1, 1);
197 xcb_create_cursor (conn, cur, pix, pix, 0, 0, 0, 0, 0, 0, 1, 1);
201 /* NOTE: we assume no other thread will be _setting_ our video output events
202 * variables. Afterall, only this plugin is supposed to know when these occur.
203 * Otherwise, we'd var_OrInteger() and var_NandInteger() functions...
206 /* FIXME we assume direct mapping between XCB and VLC */
207 static void HandleButtonPress (vout_display_t *vd,
208 const xcb_button_press_event_t *ev)
210 vout_display_SendEventMousePressed (vd, ev->detail - 1);
213 static void HandleButtonRelease (vout_display_t *vd,
214 const xcb_button_release_event_t *ev)
216 vout_display_SendEventMouseReleased (vd, ev->detail - 1);
219 static void HandleMotionNotify (vout_display_t *vd, xcb_connection_t *conn,
220 const xcb_motion_notify_event_t *ev)
222 vout_display_place_t place;
224 /* show the default cursor */
225 xcb_change_window_attributes (conn, ev->event, XCB_CW_CURSOR,
226 &(uint32_t) { XCB_CURSOR_NONE });
229 /* TODO it could be saved */
230 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
232 if (place.width <= 0 || place.height <= 0)
235 const int x = vd->source.i_x_offset +
236 (int64_t)(ev->event_x - place.x) * vd->source.i_visible_width / place.width;
237 const int y = vd->source.i_y_offset +
238 (int64_t)(ev->event_y - place.y) * vd->source.i_visible_height/ place.height;
240 vout_display_SendEventMouseMoved (vd, x, y);
243 static void HandleVisibilityNotify (vout_display_t *vd, bool *visible,
244 const xcb_visibility_notify_event_t *ev)
246 *visible = ev->state != XCB_VISIBILITY_FULLY_OBSCURED;
247 msg_Dbg (vd, "display is %svisible", *visible ? "" : "not ");
251 HandleParentStructure (vout_display_t *vd,
252 const xcb_configure_notify_event_t *ev)
254 vout_display_SendEventDisplaySize (vd, ev->width, ev->height, vd->cfg->is_fullscreen);
258 * Process an X11 event.
260 static int ProcessEvent (vout_display_t *vd, xcb_connection_t *conn,
261 bool *visible, xcb_generic_event_t *ev)
263 switch (ev->response_type & 0x7f)
265 case XCB_BUTTON_PRESS:
266 HandleButtonPress (vd, (xcb_button_press_event_t *)ev);
269 case XCB_BUTTON_RELEASE:
270 HandleButtonRelease (vd, (xcb_button_release_event_t *)ev);
273 case XCB_MOTION_NOTIFY:
274 HandleMotionNotify (vd, conn, (xcb_motion_notify_event_t *)ev);
277 case XCB_VISIBILITY_NOTIFY:
278 HandleVisibilityNotify (vd, visible,
279 (xcb_visibility_notify_event_t *)ev);
282 case XCB_CONFIGURE_NOTIFY:
283 HandleParentStructure (vd, (xcb_configure_notify_event_t *)ev);
286 /* FIXME I am not sure it is the right one */
287 case XCB_DESTROY_NOTIFY:
288 vout_display_SendEventClose (vd);
291 case XCB_MAPPING_NOTIFY:
295 msg_Dbg (vd, "unhandled event %"PRIu8, ev->response_type);
303 * Process incoming X events.
305 int XCB_Manage (vout_display_t *vd, xcb_connection_t *conn, bool *visible)
307 xcb_generic_event_t *ev;
309 while ((ev = xcb_poll_for_event (conn)) != NULL)
310 ProcessEvent (vd, conn, visible, ev);
312 if (xcb_connection_has_error (conn))
314 msg_Err (vd, "X server failure");