]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/events.c
xcb-xv: adaptor selection with --xvideo-adaptor
[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.0
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 Lesser 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                                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                                  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,
56                                 xcb_motion_notify_event_t *ev)
57 {
58     vout_display_place_t place;
59
60     /* TODO it could be saved */
61     vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
62
63     if (place.width <= 0 || place.height <= 0)
64         return;
65
66     const int x = vd->source.i_x_offset +
67         (int64_t)(ev->event_x -0*place.x) * vd->source.i_visible_width / place.width;
68     const int y = vd->source.i_y_offset +
69         (int64_t)(ev->event_y -0*place.y) * vd->source.i_visible_height/ place.height;
70
71     /* TODO show the cursor ? */
72     if (x >= vd->source.i_x_offset && x < vd->source.i_x_offset + vd->source.i_visible_width &&
73         y >= vd->source.i_y_offset && y < vd->source.i_y_offset + vd->source.i_visible_height)
74         vout_display_SendEventMouseMoved (vd, x, y);
75 }
76
77 static void
78 HandleParentStructure (vout_display_t *vd, xcb_configure_notify_event_t *ev)
79 {
80     if (ev->width  != vd->cfg->display.width ||
81         ev->height != vd->cfg->display.height)
82         vout_display_SendEventDisplaySize (vd, ev->width, ev->height);
83 }
84
85 /**
86  * Process an X11 event.
87  */
88 static int ProcessEvent (vout_display_t *vd,
89                          xcb_window_t window, xcb_generic_event_t *ev)
90 {
91     switch (ev->response_type & 0x7f)
92     {
93         case XCB_BUTTON_PRESS:
94             HandleButtonPress (vd, (xcb_button_press_event_t *)ev);
95             break;
96
97         case XCB_BUTTON_RELEASE:
98             HandleButtonRelease (vd, (xcb_button_release_event_t *)ev);
99             break;
100
101         case XCB_MOTION_NOTIFY:
102             HandleMotionNotify (vd, (xcb_motion_notify_event_t *)ev);
103             break;
104
105         case XCB_CONFIGURE_NOTIFY:
106         {
107             xcb_configure_notify_event_t *cn =
108                 (xcb_configure_notify_event_t *)ev;
109
110             assert (cn->window != window);
111             HandleParentStructure (vd, cn);
112             break;
113         }
114
115         /* FIXME I am not sure it is the right one */
116         case XCB_DESTROY_NOTIFY:
117             vout_display_SendEventClose (vd);
118             break;
119
120         case XCB_MAPPING_NOTIFY:
121             break;
122
123         default:
124             msg_Dbg (vd, "unhandled event %"PRIu8, ev->response_type);
125     }
126
127     free (ev);
128     return VLC_SUCCESS;
129 }
130
131 /**
132  * Process incoming X events.
133  */
134 int ManageEvent (vout_display_t *vd, xcb_connection_t *conn, xcb_window_t window)
135 {
136     xcb_generic_event_t *ev;
137
138     while ((ev = xcb_poll_for_event (conn)) != NULL)
139         ProcessEvent (vd, window, ev);
140
141     if (xcb_connection_has_error (conn))
142     {
143         msg_Err (vd, "X server failure");
144         return VLC_EGENERIC;
145     }
146
147     return VLC_SUCCESS;
148 }
149
150
151