]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/events.c
XCB/window: pass requested position to the X server
[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 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.
12  *
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.
17  *
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  ****************************************************************************/
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         msg_Err (vd, "%s: X11 error %d", str, err->error_code);
49         free (err);
50         return VLC_EGENERIC;
51     }
52     return VLC_SUCCESS;
53 }
54
55 /**
56  * Gets the size of an X window.
57  */
58 int GetWindowSize (struct vout_window_t *wnd, xcb_connection_t *conn,
59                    unsigned *restrict width, unsigned *restrict height)
60 {
61     xcb_get_geometry_cookie_t ck = xcb_get_geometry (conn, wnd->handle.xid);
62     xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, ck, NULL);
63
64     if (!geo)
65         return -1;
66
67     *width = geo->width;
68     *height = geo->height;
69     free (geo);
70     return 0;
71 }
72
73 /**
74  * Create a blank cursor.
75  * Note that the pixmaps are leaked (until the X disconnection). Hence, this
76  * function should be called no more than once per X connection.
77  * @param conn XCB connection
78  * @param scr target XCB screen
79  */
80 xcb_cursor_t CreateBlankCursor (xcb_connection_t *conn,
81                                 const xcb_screen_t *scr)
82 {
83     xcb_cursor_t cur = xcb_generate_id (conn);
84     xcb_pixmap_t pix = xcb_generate_id (conn);
85
86     xcb_create_pixmap (conn, 1, pix, scr->root, 1, 1);
87     xcb_create_cursor (conn, cur, pix, pix, 0, 0, 0, 1, 1, 1, 0, 0);
88     return cur;
89 }
90
91 /**
92  * (Try to) register to mouse events on a window if needed.
93  */
94 void RegisterMouseEvents (vlc_object_t *obj, xcb_connection_t *conn,
95                           xcb_window_t wnd)
96 {
97     /* Subscribe to parent window resize events */
98     uint32_t value = XCB_EVENT_MASK_POINTER_MOTION
99                    | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
100     xcb_change_window_attributes (conn, wnd, XCB_CW_EVENT_MASK, &value);
101     /* Try to subscribe to click events */
102     /* (only one X11 client can get them, so might not work) */
103     if (var_CreateGetBool (obj, "mouse-events"))
104     {
105         value |= XCB_EVENT_MASK_BUTTON_PRESS
106                | XCB_EVENT_MASK_BUTTON_RELEASE;
107         xcb_change_window_attributes (conn, wnd,
108                                       XCB_CW_EVENT_MASK, &value);
109     }
110     var_Destroy (obj, "mouse-events");
111 }
112
113 /* NOTE: we assume no other thread will be _setting_ our video output events
114  * variables. Afterall, only this plugin is supposed to know when these occur.
115   * Otherwise, we'd var_OrInteger() and var_NandInteger() functions...
116  */
117
118 /* FIXME we assume direct mapping between XCB and VLC */
119 static void HandleButtonPress (vout_display_t *vd,
120                                const xcb_button_press_event_t *ev)
121 {
122     vout_display_SendEventMousePressed (vd, ev->detail - 1);
123 }
124
125 static void HandleButtonRelease (vout_display_t *vd,
126                                  const xcb_button_release_event_t *ev)
127 {
128     vout_display_SendEventMouseReleased (vd, ev->detail - 1);
129 }
130
131 static void HandleMotionNotify (vout_display_t *vd, xcb_connection_t *conn,
132                                 const xcb_motion_notify_event_t *ev)
133 {
134     vout_display_place_t place;
135
136     /* show the default cursor */
137     xcb_change_window_attributes (conn, ev->event, XCB_CW_CURSOR,
138                                   &(uint32_t) { XCB_CURSOR_NONE });
139
140     /* TODO it could be saved */
141     vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
142
143     if (place.width <= 0 || place.height <= 0)
144         return;
145
146     const int x = vd->source.i_x_offset +
147         (int64_t)(ev->event_x - place.x) * vd->source.i_visible_width / place.width;
148     const int y = vd->source.i_y_offset +
149         (int64_t)(ev->event_y - place.y) * vd->source.i_visible_height/ place.height;
150
151     vout_display_SendEventMouseMoved (vd, x, y);
152 }
153
154 static void HandleVisibilityNotify (vout_display_t *vd, bool *visible,
155                                     const xcb_visibility_notify_event_t *ev)
156 {
157     *visible = ev->state != XCB_VISIBILITY_FULLY_OBSCURED;
158     msg_Dbg (vd, "display is %svisible", *visible ? "" : "not ");
159 }
160
161 static void
162 HandleParentStructure (vout_display_t *vd,
163                        const xcb_configure_notify_event_t *ev)
164 {
165     vout_display_SendEventDisplaySize (vd, ev->width, ev->height, vd->cfg->is_fullscreen);
166 }
167
168 /**
169  * Process an X11 event.
170  */
171 static int ProcessEvent (vout_display_t *vd, xcb_connection_t *conn,
172                          bool *visible, xcb_generic_event_t *ev)
173 {
174     switch (ev->response_type & 0x7f)
175     {
176         case XCB_BUTTON_PRESS:
177             HandleButtonPress (vd, (xcb_button_press_event_t *)ev);
178             break;
179
180         case XCB_BUTTON_RELEASE:
181             HandleButtonRelease (vd, (xcb_button_release_event_t *)ev);
182             break;
183
184         case XCB_MOTION_NOTIFY:
185             HandleMotionNotify (vd, conn, (xcb_motion_notify_event_t *)ev);
186             break;
187
188         case XCB_VISIBILITY_NOTIFY:
189             HandleVisibilityNotify (vd, visible,
190                                     (xcb_visibility_notify_event_t *)ev);
191             break;
192
193         case XCB_CONFIGURE_NOTIFY:
194             HandleParentStructure (vd, (xcb_configure_notify_event_t *)ev);
195             break;
196
197         /* FIXME I am not sure it is the right one */
198         case XCB_DESTROY_NOTIFY:
199             vout_display_SendEventClose (vd);
200             break;
201
202         case XCB_MAPPING_NOTIFY:
203             break;
204
205         default:
206             msg_Dbg (vd, "unhandled event %"PRIu8, ev->response_type);
207     }
208
209     free (ev);
210     return VLC_SUCCESS;
211 }
212
213 /**
214  * Process incoming X events.
215  */
216 int ManageEvent (vout_display_t *vd, xcb_connection_t *conn, bool *visible)
217 {
218     xcb_generic_event_t *ev;
219
220     while ((ev = xcb_poll_for_event (conn)) != NULL)
221         ProcessEvent (vd, conn, visible, ev);
222
223     if (xcb_connection_has_error (conn))
224     {
225         msg_Err (vd, "X server failure");
226         return VLC_EGENERIC;
227     }
228
229     return VLC_SUCCESS;
230 }