3 * @brief X C Bindings VLC video output events handling
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
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 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 ****************************************************************************/
31 #ifndef XCB_CURSOR_NONE
32 # define XCB_CURSOR_NONE ((xcb_cursor_t) 0U)
35 #include <vlc_common.h>
36 #include <vlc_vout_display.h>
43 int CheckError (vout_display_t *vd, xcb_connection_t *conn,
44 const char *str, xcb_void_cookie_t ck)
46 xcb_generic_error_t *err;
48 err = xcb_request_check (conn, ck);
51 msg_Err (vd, "%s: X11 error %d", str, err->error_code);
59 * Gets the size of an X window.
61 int GetWindowSize (struct vout_window_t *wnd, xcb_connection_t *conn,
62 unsigned *restrict width, unsigned *restrict height)
64 xcb_get_geometry_cookie_t ck = xcb_get_geometry (conn, wnd->handle.xid);
65 xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, ck, NULL);
71 *height = geo->height;
77 * Create a blank cursor.
78 * Note that the pixmaps are leaked (until the X disconnection). Hence, this
79 * function should be called no more than once per X connection.
80 * @param conn XCB connection
81 * @param scr target XCB screen
83 xcb_cursor_t CreateBlankCursor (xcb_connection_t *conn,
84 const xcb_screen_t *scr)
86 xcb_cursor_t cur = xcb_generate_id (conn);
87 xcb_pixmap_t pix = xcb_generate_id (conn);
89 xcb_create_pixmap (conn, 1, pix, scr->root, 1, 1);
90 xcb_create_cursor (conn, cur, pix, pix, 0, 0, 0, 1, 1, 1, 0, 0);
95 * (Try to) register to mouse events on a window if needed.
97 void RegisterMouseEvents (vlc_object_t *obj, xcb_connection_t *conn,
100 /* Subscribe to parent window resize events */
101 uint32_t value = XCB_EVENT_MASK_POINTER_MOTION
102 | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
103 xcb_change_window_attributes (conn, wnd, XCB_CW_EVENT_MASK, &value);
104 /* Try to subscribe to click events */
105 /* (only one X11 client can get them, so might not work) */
106 if (var_CreateGetBool (obj, "mouse-events"))
108 value |= XCB_EVENT_MASK_BUTTON_PRESS
109 | XCB_EVENT_MASK_BUTTON_RELEASE;
110 xcb_change_window_attributes (conn, wnd,
111 XCB_CW_EVENT_MASK, &value);
113 var_Destroy (obj, "mouse-events");
116 /* NOTE: we assume no other thread will be _setting_ our video output events
117 * variables. Afterall, only this plugin is supposed to know when these occur.
118 * Otherwise, we'd var_OrInteger() and var_NandInteger() functions...
121 /* FIXME we assume direct mapping between XCB and VLC */
122 static void HandleButtonPress (vout_display_t *vd,
123 const xcb_button_press_event_t *ev)
125 vout_display_SendEventMousePressed (vd, ev->detail - 1);
128 static void HandleButtonRelease (vout_display_t *vd,
129 const xcb_button_release_event_t *ev)
131 vout_display_SendEventMouseReleased (vd, ev->detail - 1);
134 static void HandleMotionNotify (vout_display_t *vd, xcb_connection_t *conn,
135 const xcb_motion_notify_event_t *ev)
137 vout_display_place_t place;
139 /* show the default cursor */
140 xcb_change_window_attributes (conn, ev->event, XCB_CW_CURSOR,
141 &(uint32_t) { XCB_CURSOR_NONE });
143 /* TODO it could be saved */
144 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
146 if (place.width <= 0 || place.height <= 0)
149 const int x = vd->source.i_x_offset +
150 (int64_t)(ev->event_x - place.x) * vd->source.i_visible_width / place.width;
151 const int y = vd->source.i_y_offset +
152 (int64_t)(ev->event_y - place.y) * vd->source.i_visible_height/ place.height;
154 vout_display_SendEventMouseMoved (vd, x, y);
157 static void HandleVisibilityNotify (vout_display_t *vd, bool *visible,
158 const xcb_visibility_notify_event_t *ev)
160 *visible = ev->state != XCB_VISIBILITY_FULLY_OBSCURED;
161 msg_Dbg (vd, "display is %svisible", *visible ? "" : "not ");
165 HandleParentStructure (vout_display_t *vd,
166 const xcb_configure_notify_event_t *ev)
168 vout_display_SendEventDisplaySize (vd, ev->width, ev->height, vd->cfg->is_fullscreen);
172 * Process an X11 event.
174 static int ProcessEvent (vout_display_t *vd, xcb_connection_t *conn,
175 bool *visible, xcb_generic_event_t *ev)
177 switch (ev->response_type & 0x7f)
179 case XCB_BUTTON_PRESS:
180 HandleButtonPress (vd, (xcb_button_press_event_t *)ev);
183 case XCB_BUTTON_RELEASE:
184 HandleButtonRelease (vd, (xcb_button_release_event_t *)ev);
187 case XCB_MOTION_NOTIFY:
188 HandleMotionNotify (vd, conn, (xcb_motion_notify_event_t *)ev);
191 case XCB_VISIBILITY_NOTIFY:
192 HandleVisibilityNotify (vd, visible,
193 (xcb_visibility_notify_event_t *)ev);
196 case XCB_CONFIGURE_NOTIFY:
197 HandleParentStructure (vd, (xcb_configure_notify_event_t *)ev);
200 /* FIXME I am not sure it is the right one */
201 case XCB_DESTROY_NOTIFY:
202 vout_display_SendEventClose (vd);
205 case XCB_MAPPING_NOTIFY:
209 msg_Dbg (vd, "unhandled event %"PRIu8, ev->response_type);
217 * Process incoming X events.
219 int ManageEvent (vout_display_t *vd, xcb_connection_t *conn, bool *visible)
221 xcb_generic_event_t *ev;
223 while ((ev = xcb_poll_for_event (conn)) != NULL)
224 ProcessEvent (vd, conn, visible, ev);
226 if (xcb_connection_has_error (conn))
228 msg_Err (vd, "X server failure");