]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/events.c
Revert "vout_window_t: simplify via anynomous union"
[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 #ifndef XCB_CURSOR_NONE
32 # define XCB_CURSOR_NONE ((xcb_cursor_t) 0U)
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_vout_display.h>
37
38 #include "xcb_vlc.h"
39
40 /**
41  * Check for an error
42  */
43 int CheckError (vout_display_t *vd, xcb_connection_t *conn,
44                 const char *str, xcb_void_cookie_t ck)
45 {
46     xcb_generic_error_t *err;
47
48     err = xcb_request_check (conn, ck);
49     if (err)
50     {
51         msg_Err (vd, "%s: X11 error %d", str, err->error_code);
52         free (err);
53         return VLC_EGENERIC;
54     }
55     return VLC_SUCCESS;
56 }
57
58 /**
59  * Gets the size of an X window.
60  */
61 int GetWindowSize (struct vout_window_t *wnd, xcb_connection_t *conn,
62                    unsigned *restrict width, unsigned *restrict height)
63 {
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);
66
67     if (!geo)
68         return -1;
69
70     *width = geo->width;
71     *height = geo->height;
72     free (geo);
73     return 0;
74 }
75
76 /**
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
82  */
83 xcb_cursor_t CreateBlankCursor (xcb_connection_t *conn,
84                                 const xcb_screen_t *scr)
85 {
86     xcb_cursor_t cur = xcb_generate_id (conn);
87     xcb_pixmap_t pix = xcb_generate_id (conn);
88
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);
91     return cur;
92 }
93
94 /**
95  * (Try to) register to mouse events on a window if needed.
96  */
97 void RegisterMouseEvents (vlc_object_t *obj, xcb_connection_t *conn,
98                           xcb_window_t wnd)
99 {
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"))
107     {
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);
112     }
113     var_Destroy (obj, "mouse-events");
114 }
115
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...
119  */
120
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)
124 {
125     vout_display_SendEventMousePressed (vd, ev->detail - 1);
126 }
127
128 static void HandleButtonRelease (vout_display_t *vd,
129                                  const xcb_button_release_event_t *ev)
130 {
131     vout_display_SendEventMouseReleased (vd, ev->detail - 1);
132 }
133
134 static void HandleMotionNotify (vout_display_t *vd, xcb_connection_t *conn,
135                                 const xcb_motion_notify_event_t *ev)
136 {
137     vout_display_place_t place;
138
139     /* show the default cursor */
140     xcb_change_window_attributes (conn, ev->event, XCB_CW_CURSOR,
141                                   &(uint32_t) { XCB_CURSOR_NONE });
142
143     /* TODO it could be saved */
144     vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
145
146     if (place.width <= 0 || place.height <= 0)
147         return;
148
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;
153
154     vout_display_SendEventMouseMoved (vd, x, y);
155 }
156
157 static void HandleVisibilityNotify (vout_display_t *vd, bool *visible,
158                                     const xcb_visibility_notify_event_t *ev)
159 {
160     *visible = ev->state != XCB_VISIBILITY_FULLY_OBSCURED;
161     msg_Dbg (vd, "display is %svisible", *visible ? "" : "not ");
162 }
163
164 static void
165 HandleParentStructure (vout_display_t *vd,
166                        const xcb_configure_notify_event_t *ev)
167 {
168     vout_display_SendEventDisplaySize (vd, ev->width, ev->height, vd->cfg->is_fullscreen);
169 }
170
171 /**
172  * Process an X11 event.
173  */
174 static int ProcessEvent (vout_display_t *vd, xcb_connection_t *conn,
175                          bool *visible, xcb_generic_event_t *ev)
176 {
177     switch (ev->response_type & 0x7f)
178     {
179         case XCB_BUTTON_PRESS:
180             HandleButtonPress (vd, (xcb_button_press_event_t *)ev);
181             break;
182
183         case XCB_BUTTON_RELEASE:
184             HandleButtonRelease (vd, (xcb_button_release_event_t *)ev);
185             break;
186
187         case XCB_MOTION_NOTIFY:
188             HandleMotionNotify (vd, conn, (xcb_motion_notify_event_t *)ev);
189             break;
190
191         case XCB_VISIBILITY_NOTIFY:
192             HandleVisibilityNotify (vd, visible,
193                                     (xcb_visibility_notify_event_t *)ev);
194             break;
195
196         case XCB_CONFIGURE_NOTIFY:
197             HandleParentStructure (vd, (xcb_configure_notify_event_t *)ev);
198             break;
199
200         /* FIXME I am not sure it is the right one */
201         case XCB_DESTROY_NOTIFY:
202             vout_display_SendEventClose (vd);
203             break;
204
205         case XCB_MAPPING_NOTIFY:
206             break;
207
208         default:
209             msg_Dbg (vd, "unhandled event %"PRIu8, ev->response_type);
210     }
211
212     free (ev);
213     return VLC_SUCCESS;
214 }
215
216 /**
217  * Process incoming X events.
218  */
219 int ManageEvent (vout_display_t *vd, xcb_connection_t *conn, bool *visible)
220 {
221     xcb_generic_event_t *ev;
222
223     while ((ev = xcb_poll_for_event (conn)) != NULL)
224         ProcessEvent (vd, conn, visible, ev);
225
226     if (xcb_connection_has_error (conn))
227     {
228         msg_Err (vd, "X server failure");
229         return VLC_EGENERIC;
230     }
231
232     return VLC_SUCCESS;
233 }