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