]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/events.c
XCB/GLX: reuse windowing code from other XCB plugins
[vlc] / modules / video_output / xcb / events.c
1 /**
2  * @file events.c
3  * @brief X C Bindings VLC video output events handling
4  */
5 /*****************************************************************************
6  * Copyright © 2009 Rémi Denis-Courmont
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <inttypes.h>
28 #include <assert.h>
29
30 #include <xcb/xcb.h>
31
32 #include <vlc_common.h>
33 #include <vlc_vout_display.h>
34
35 #include "xcb_vlc.h"
36
37 /**
38  * Check for an error
39  */
40 int CheckError (vout_display_t *vd, xcb_connection_t *conn,
41                 const char *str, xcb_void_cookie_t ck)
42 {
43     xcb_generic_error_t *err;
44
45     err = xcb_request_check (conn, ck);
46     if (err)
47     {
48         int code = err->error_code;
49
50         free (err);
51         msg_Err (vd, "%s: X11 error %d", str, code);
52         assert (code != 0);
53         return code;
54     }
55     return 0;
56 }
57
58 /**
59  * Connect to the X server.
60  */
61 static xcb_connection_t *Connect (vlc_object_t *obj, const char *display)
62 {
63     xcb_connection_t *conn = xcb_connect (display, NULL);
64     if (xcb_connection_has_error (conn) /*== NULL*/)
65     {
66         msg_Err (obj, "cannot connect to X server (%s)",
67                  (display != NULL) ? display : "default");
68         xcb_disconnect (conn);
69         return NULL;
70     }
71
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);
76     if (vendor)
77     {
78         msg_Dbg (obj, " vendor : %s", vendor);
79         free (vendor);
80     }
81     msg_Dbg (obj, " version: %"PRIu32, setup->release_number);
82     return conn;
83 }
84
85 /**
86  * (Try to) register to mouse events on a window if needed.
87  */
88 static void RegisterEvents (vlc_object_t *obj, xcb_connection_t *conn,
89                             xcb_window_t wnd)
90 {
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"))
98     {
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);
103     }
104 }
105
106 /**
107  * Find screen matching a given root window.
108  */
109 static const xcb_screen_t *FindScreen (vlc_object_t *obj,
110                                        xcb_connection_t *conn,
111                                        xcb_window_t root)
112 {
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))
117     {
118         if (i.data->root == root)
119         {
120             msg_Dbg (obj, "using screen 0x%"PRIx32, root);
121             return i.data;
122         }
123     }
124     msg_Err (obj, "window screen not found");
125     return NULL;
126 }
127
128 /**
129  * Create a VLC video X window object, connect to the corresponding X server,
130  * find the corresponding X server screen.
131  */
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)
138 {
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,
145     };
146
147     vout_window_t *wnd = vout_display_NewWindow (vd, &cfg);
148     if (wnd == NULL)
149     {
150         msg_Err (vd, "window not available");
151         return NULL;
152     }
153
154     xcb_connection_t *conn = Connect (VLC_OBJECT(vd), wnd->display.x11);
155     if (conn == NULL)
156         goto error;
157     *pconn = conn;
158
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);
162
163     xcb_get_geometry_reply_t *geo =
164         xcb_get_geometry_reply (conn, xcb_get_geometry (conn, wnd->handle.xid),
165                                 NULL);
166     if (geo == NULL)
167     {
168         msg_Err (vd, "window not valid");
169         goto error;
170     }
171     *pdepth = geo->depth;
172     *pwidth = geo->width;
173     *pheight = geo->height;
174
175     const xcb_screen_t *screen = FindScreen (VLC_OBJECT(vd), conn, geo->root);
176     free (geo);
177     if (screen == NULL)
178         goto error;
179     *pscreen = screen;
180     return wnd;
181
182 error:
183     if (conn != NULL)
184         xcb_disconnect (conn);
185     vout_display_DeleteWindow (vd, wnd);
186     return NULL;
187 }
188
189 /**
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
195  */
196 xcb_cursor_t CreateBlankCursor (xcb_connection_t *conn,
197                                 const xcb_screen_t *scr)
198 {
199     xcb_cursor_t cur = xcb_generate_id (conn);
200     xcb_pixmap_t pix = xcb_generate_id (conn);
201
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);
204     return cur;
205 }
206
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...
210  */
211
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)
215 {
216     vout_display_SendEventMousePressed (vd, ev->detail - 1);
217 }
218
219 static void HandleButtonRelease (vout_display_t *vd,
220                                  const xcb_button_release_event_t *ev)
221 {
222     vout_display_SendEventMouseReleased (vd, ev->detail - 1);
223 }
224
225 static void HandleMotionNotify (vout_display_t *vd, xcb_connection_t *conn,
226                                 const xcb_motion_notify_event_t *ev)
227 {
228     vout_display_place_t place;
229
230     /* show the default cursor */
231     xcb_change_window_attributes (conn, ev->event, XCB_CW_CURSOR,
232                                   &(uint32_t) { XCB_CURSOR_NONE });
233     xcb_flush (conn);
234
235     /* TODO it could be saved */
236     vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
237
238     if (place.width <= 0 || place.height <= 0)
239         return;
240
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;
245
246     vout_display_SendEventMouseMoved (vd, x, y);
247 }
248
249 static void HandleVisibilityNotify (vout_display_t *vd, bool *visible,
250                                     const xcb_visibility_notify_event_t *ev)
251 {
252     *visible = ev->state != XCB_VISIBILITY_FULLY_OBSCURED;
253     msg_Dbg (vd, "display is %svisible", *visible ? "" : "not ");
254 }
255
256 static void
257 HandleParentStructure (vout_display_t *vd,
258                        const xcb_configure_notify_event_t *ev)
259 {
260     vout_display_SendEventDisplaySize (vd, ev->width, ev->height, vd->cfg->is_fullscreen);
261 }
262
263 /**
264  * Process an X11 event.
265  */
266 static int ProcessEvent (vout_display_t *vd, xcb_connection_t *conn,
267                          bool *visible, xcb_generic_event_t *ev)
268 {
269     switch (ev->response_type & 0x7f)
270     {
271         case XCB_BUTTON_PRESS:
272             HandleButtonPress (vd, (xcb_button_press_event_t *)ev);
273             break;
274
275         case XCB_BUTTON_RELEASE:
276             HandleButtonRelease (vd, (xcb_button_release_event_t *)ev);
277             break;
278
279         case XCB_MOTION_NOTIFY:
280             HandleMotionNotify (vd, conn, (xcb_motion_notify_event_t *)ev);
281             break;
282
283         case XCB_VISIBILITY_NOTIFY:
284             HandleVisibilityNotify (vd, visible,
285                                     (xcb_visibility_notify_event_t *)ev);
286             break;
287
288         case XCB_CONFIGURE_NOTIFY:
289             HandleParentStructure (vd, (xcb_configure_notify_event_t *)ev);
290             break;
291
292         /* FIXME I am not sure it is the right one */
293         case XCB_DESTROY_NOTIFY:
294             vout_display_SendEventClose (vd);
295             break;
296
297         case XCB_MAPPING_NOTIFY:
298             break;
299
300         default:
301             msg_Dbg (vd, "unhandled event %"PRIu8, ev->response_type);
302     }
303
304     free (ev);
305     return VLC_SUCCESS;
306 }
307
308 /**
309  * Process incoming X events.
310  */
311 int ManageEvent (vout_display_t *vd, xcb_connection_t *conn, bool *visible)
312 {
313     xcb_generic_event_t *ev;
314
315     while ((ev = xcb_poll_for_event (conn)) != NULL)
316         ProcessEvent (vd, conn, visible, ev);
317
318     if (xcb_connection_has_error (conn))
319     {
320         msg_Err (vd, "X server failure");
321         return VLC_EGENERIC;
322     }
323
324     return VLC_SUCCESS;
325 }