]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/events.c
vdr: clarify
[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 "events.h"
36
37 /**
38  * Check for an error
39  */
40 int XCB_error_Check (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     msg_Dbg (obj, " vendor : %.*s", (int)setup->vendor_len,
76              xcb_setup_vendor (setup));
77     msg_Dbg (obj, " version: %"PRIu32, setup->release_number);
78     return conn;
79 }
80
81 /**
82  * (Try to) register to mouse events on a window if needed.
83  */
84 static void RegisterEvents (vlc_object_t *obj, xcb_connection_t *conn,
85                             xcb_window_t wnd)
86 {
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"))
94     {
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);
99     }
100 }
101
102 /**
103  * Find screen matching a given root window.
104  */
105 static const xcb_screen_t *FindScreen (vlc_object_t *obj,
106                                        xcb_connection_t *conn,
107                                        xcb_window_t root)
108 {
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))
113     {
114         if (i.data->root == root)
115         {
116             msg_Dbg (obj, "using screen 0x%"PRIx32, root);
117             return i.data;
118         }
119     }
120     msg_Err (obj, "window screen not found");
121     return NULL;
122 }
123
124 /**
125  * Create a VLC video X window object, connect to the corresponding X server,
126  * find the corresponding X server screen.
127  */
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)
133 {
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,
140     };
141
142     vout_window_t *wnd = vout_display_NewWindow (vd, &cfg);
143     if (wnd == NULL)
144     {
145         msg_Err (vd, "window not available");
146         return NULL;
147     }
148
149     xcb_connection_t *conn = Connect (VLC_OBJECT(vd), wnd->display.x11);
150     if (conn == NULL)
151         goto error;
152     *pconn = conn;
153
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);
157
158     xcb_get_geometry_reply_t *geo =
159         xcb_get_geometry_reply (conn, xcb_get_geometry (conn, wnd->handle.xid),
160                                 NULL);
161     if (geo == NULL)
162     {
163         msg_Err (vd, "window not valid");
164         goto error;
165     }
166     *pwidth = geo->width;
167     *pheight = geo->height;
168
169     const xcb_screen_t *screen = FindScreen (VLC_OBJECT(vd), conn, geo->root);
170     free (geo);
171     if (screen == NULL)
172         goto error;
173     *pscreen = screen;
174     return wnd;
175
176 error:
177     if (conn != NULL)
178         xcb_disconnect (conn);
179     vout_display_DeleteWindow (vd, wnd);
180     return NULL;
181 }
182
183 /**
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
189  */
190 xcb_cursor_t XCB_cursor_Create (xcb_connection_t *conn,
191                                 const xcb_screen_t *scr)
192 {
193     xcb_cursor_t cur = xcb_generate_id (conn);
194     xcb_pixmap_t pix = xcb_generate_id (conn);
195
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);
198     return cur;
199 }
200
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...
204  */
205
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)
209 {
210     vout_display_SendEventMousePressed (vd, ev->detail - 1);
211 }
212
213 static void HandleButtonRelease (vout_display_t *vd,
214                                  const xcb_button_release_event_t *ev)
215 {
216     vout_display_SendEventMouseReleased (vd, ev->detail - 1);
217 }
218
219 static void HandleMotionNotify (vout_display_t *vd, xcb_connection_t *conn,
220                                 const xcb_motion_notify_event_t *ev)
221 {
222     vout_display_place_t place;
223
224     /* show the default cursor */
225     xcb_change_window_attributes (conn, ev->event, XCB_CW_CURSOR,
226                                   &(uint32_t) { XCB_CURSOR_NONE });
227     xcb_flush (conn);
228
229     /* TODO it could be saved */
230     vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
231
232     if (place.width <= 0 || place.height <= 0)
233         return;
234
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;
239
240     vout_display_SendEventMouseMoved (vd, x, y);
241 }
242
243 static void HandleVisibilityNotify (vout_display_t *vd, bool *visible,
244                                     const xcb_visibility_notify_event_t *ev)
245 {
246     *visible = ev->state != XCB_VISIBILITY_FULLY_OBSCURED;
247     msg_Dbg (vd, "display is %svisible", *visible ? "" : "not ");
248 }
249
250 static void
251 HandleParentStructure (vout_display_t *vd,
252                        const xcb_configure_notify_event_t *ev)
253 {
254     vout_display_SendEventDisplaySize (vd, ev->width, ev->height, vd->cfg->is_fullscreen);
255 }
256
257 /**
258  * Process an X11 event.
259  */
260 static int ProcessEvent (vout_display_t *vd, xcb_connection_t *conn,
261                          bool *visible, xcb_generic_event_t *ev)
262 {
263     switch (ev->response_type & 0x7f)
264     {
265         case XCB_BUTTON_PRESS:
266             HandleButtonPress (vd, (xcb_button_press_event_t *)ev);
267             break;
268
269         case XCB_BUTTON_RELEASE:
270             HandleButtonRelease (vd, (xcb_button_release_event_t *)ev);
271             break;
272
273         case XCB_MOTION_NOTIFY:
274             HandleMotionNotify (vd, conn, (xcb_motion_notify_event_t *)ev);
275             break;
276
277         case XCB_VISIBILITY_NOTIFY:
278             HandleVisibilityNotify (vd, visible,
279                                     (xcb_visibility_notify_event_t *)ev);
280             break;
281
282         case XCB_CONFIGURE_NOTIFY:
283             HandleParentStructure (vd, (xcb_configure_notify_event_t *)ev);
284             break;
285
286         /* FIXME I am not sure it is the right one */
287         case XCB_DESTROY_NOTIFY:
288             vout_display_SendEventClose (vd);
289             break;
290
291         case XCB_MAPPING_NOTIFY:
292             break;
293
294         default:
295             msg_Dbg (vd, "unhandled event %"PRIu8, ev->response_type);
296     }
297
298     free (ev);
299     return VLC_SUCCESS;
300 }
301
302 /**
303  * Process incoming X events.
304  */
305 int XCB_Manage (vout_display_t *vd, xcb_connection_t *conn, bool *visible)
306 {
307     xcb_generic_event_t *ev;
308
309     while ((ev = xcb_poll_for_event (conn)) != NULL)
310         ProcessEvent (vd, conn, visible, ev);
311
312     if (xcb_connection_has_error (conn))
313     {
314         msg_Err (vd, "X server failure");
315         return VLC_EGENERIC;
316     }
317
318     return VLC_SUCCESS;
319 }