]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/events.c
Use var_Inherit* instead of var_CreateGet*.
[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, 0, 0, 0, 1, 1);
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_InheritBool (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 }
111
112 /* NOTE: we assume no other thread will be _setting_ our video output events
113  * variables. Afterall, only this plugin is supposed to know when these occur.
114   * Otherwise, we'd var_OrInteger() and var_NandInteger() functions...
115  */
116
117 /* FIXME we assume direct mapping between XCB and VLC */
118 static void HandleButtonPress (vout_display_t *vd,
119                                const xcb_button_press_event_t *ev)
120 {
121     vout_display_SendEventMousePressed (vd, ev->detail - 1);
122 }
123
124 static void HandleButtonRelease (vout_display_t *vd,
125                                  const xcb_button_release_event_t *ev)
126 {
127     vout_display_SendEventMouseReleased (vd, ev->detail - 1);
128 }
129
130 static void HandleMotionNotify (vout_display_t *vd, xcb_connection_t *conn,
131                                 const xcb_motion_notify_event_t *ev)
132 {
133     vout_display_place_t place;
134
135     /* show the default cursor */
136     xcb_change_window_attributes (conn, ev->event, XCB_CW_CURSOR,
137                                   &(uint32_t) { XCB_CURSOR_NONE });
138
139     /* TODO it could be saved */
140     vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
141
142     if (place.width <= 0 || place.height <= 0)
143         return;
144
145     const int x = vd->source.i_x_offset +
146         (int64_t)(ev->event_x - place.x) * vd->source.i_visible_width / place.width;
147     const int y = vd->source.i_y_offset +
148         (int64_t)(ev->event_y - place.y) * vd->source.i_visible_height/ place.height;
149
150     vout_display_SendEventMouseMoved (vd, x, y);
151 }
152
153 static void HandleVisibilityNotify (vout_display_t *vd, bool *visible,
154                                     const xcb_visibility_notify_event_t *ev)
155 {
156     *visible = ev->state != XCB_VISIBILITY_FULLY_OBSCURED;
157     msg_Dbg (vd, "display is %svisible", *visible ? "" : "not ");
158 }
159
160 static void
161 HandleParentStructure (vout_display_t *vd,
162                        const xcb_configure_notify_event_t *ev)
163 {
164     vout_display_SendEventDisplaySize (vd, ev->width, ev->height, vd->cfg->is_fullscreen);
165 }
166
167 /**
168  * Process an X11 event.
169  */
170 static int ProcessEvent (vout_display_t *vd, xcb_connection_t *conn,
171                          bool *visible, xcb_generic_event_t *ev)
172 {
173     switch (ev->response_type & 0x7f)
174     {
175         case XCB_BUTTON_PRESS:
176             HandleButtonPress (vd, (xcb_button_press_event_t *)ev);
177             break;
178
179         case XCB_BUTTON_RELEASE:
180             HandleButtonRelease (vd, (xcb_button_release_event_t *)ev);
181             break;
182
183         case XCB_MOTION_NOTIFY:
184             HandleMotionNotify (vd, conn, (xcb_motion_notify_event_t *)ev);
185             break;
186
187         case XCB_VISIBILITY_NOTIFY:
188             HandleVisibilityNotify (vd, visible,
189                                     (xcb_visibility_notify_event_t *)ev);
190             break;
191
192         case XCB_CONFIGURE_NOTIFY:
193             HandleParentStructure (vd, (xcb_configure_notify_event_t *)ev);
194             break;
195
196         /* FIXME I am not sure it is the right one */
197         case XCB_DESTROY_NOTIFY:
198             vout_display_SendEventClose (vd);
199             break;
200
201         case XCB_MAPPING_NOTIFY:
202             break;
203
204         default:
205             msg_Dbg (vd, "unhandled event %"PRIu8, ev->response_type);
206     }
207
208     free (ev);
209     return VLC_SUCCESS;
210 }
211
212 /**
213  * Process incoming X events.
214  */
215 int ManageEvent (vout_display_t *vd, xcb_connection_t *conn, bool *visible)
216 {
217     xcb_generic_event_t *ev;
218
219     while ((ev = xcb_poll_for_event (conn)) != NULL)
220         ProcessEvent (vd, conn, visible, ev);
221
222     if (xcb_connection_has_error (conn))
223     {
224         msg_Err (vd, "X server failure");
225         return VLC_EGENERIC;
226     }
227
228     return VLC_SUCCESS;
229 }