]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/events.c
6da22fbdb21d67ed60b66e5f9cd17f461bfcc5db
[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 /* NOTE: we assume no other thread will be _setting_ our video output events
41  * variables. Afterall, only this plugin is supposed to know when these occur.
42   * Otherwise, we'd var_OrInteger() and var_NandInteger() functions...
43  */
44
45 /* FIXME we assume direct mapping between XCB and VLC */
46 static void HandleButtonPress (vout_display_t *vd,
47                                const xcb_button_press_event_t *ev)
48 {
49     vout_display_SendEventMousePressed (vd, ev->detail - 1);
50 }
51
52 static void HandleButtonRelease (vout_display_t *vd,
53                                  const xcb_button_release_event_t *ev)
54 {
55     vout_display_SendEventMouseReleased (vd, ev->detail - 1);
56 }
57
58 static void HandleMotionNotify (vout_display_t *vd, xcb_connection_t *conn,
59                                 const xcb_motion_notify_event_t *ev)
60 {
61     vout_display_place_t place;
62
63     /* show the default cursor */
64     xcb_change_window_attributes (conn, ev->event, XCB_CW_CURSOR,
65                                   &(uint32_t) { XCB_CURSOR_NONE });
66
67     /* TODO it could be saved */
68     vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
69
70     if (place.width <= 0 || place.height <= 0)
71         return;
72
73     const int x = vd->source.i_x_offset +
74         (int64_t)(ev->event_x - place.x) * vd->source.i_visible_width / place.width;
75     const int y = vd->source.i_y_offset +
76         (int64_t)(ev->event_y - place.y) * vd->source.i_visible_height/ place.height;
77
78     vout_display_SendEventMouseMoved (vd, x, y);
79 }
80
81 static void HandleVisibilityNotify (vout_display_t *vd, bool *visible,
82                                     const xcb_visibility_notify_event_t *ev)
83 {
84     *visible = ev->state != XCB_VISIBILITY_FULLY_OBSCURED;
85     msg_Dbg (vd, "display is %svisible", *visible ? "" : "not ");
86 }
87
88 static void
89 HandleParentStructure (vout_display_t *vd,
90                        const xcb_configure_notify_event_t *ev)
91 {
92     if (ev->width  != vd->cfg->display.width ||
93         ev->height != vd->cfg->display.height)
94         vout_display_SendEventDisplaySize (vd, ev->width, ev->height, vd->cfg->is_fullscreen);
95 }
96
97 /**
98  * Process an X11 event.
99  */
100 static int ProcessEvent (vout_display_t *vd, xcb_connection_t *conn,
101                          bool *visible, xcb_generic_event_t *ev)
102 {
103     switch (ev->response_type & 0x7f)
104     {
105         case XCB_BUTTON_PRESS:
106             HandleButtonPress (vd, (xcb_button_press_event_t *)ev);
107             break;
108
109         case XCB_BUTTON_RELEASE:
110             HandleButtonRelease (vd, (xcb_button_release_event_t *)ev);
111             break;
112
113         case XCB_MOTION_NOTIFY:
114             HandleMotionNotify (vd, conn, (xcb_motion_notify_event_t *)ev);
115             break;
116
117         case XCB_VISIBILITY_NOTIFY:
118             HandleVisibilityNotify (vd, visible,
119                                     (xcb_visibility_notify_event_t *)ev);
120             break;
121
122         case XCB_CONFIGURE_NOTIFY:
123             HandleParentStructure (vd, (xcb_configure_notify_event_t *)ev);
124             break;
125
126         /* FIXME I am not sure it is the right one */
127         case XCB_DESTROY_NOTIFY:
128             vout_display_SendEventClose (vd);
129             break;
130
131         case XCB_MAPPING_NOTIFY:
132             break;
133
134         default:
135             msg_Dbg (vd, "unhandled event %"PRIu8, ev->response_type);
136     }
137
138     free (ev);
139     return VLC_SUCCESS;
140 }
141
142 /**
143  * Process incoming X events.
144  */
145 int ManageEvent (vout_display_t *vd, xcb_connection_t *conn, bool *visible)
146 {
147     xcb_generic_event_t *ev;
148
149     while ((ev = xcb_poll_for_event (conn)) != NULL)
150         ProcessEvent (vd, conn, visible, ev);
151
152     if (xcb_connection_has_error (conn))
153     {
154         msg_Err (vd, "X server failure");
155         return VLC_EGENERIC;
156     }
157
158     return VLC_SUCCESS;
159 }