]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/events.c
XCB: mouse events handling and background color
[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
29 #include <xcb/xcb.h>
30
31 #include <vlc_common.h>
32 #include <vlc_vout.h>
33
34 #include "xcb_vlc.h"
35
36 /* NOTE: we assume no other thread will be _setting_ our video output events
37  * variables. Afterall, only this plugin is supposed to know when these occur.
38   * Otherwise, we'd var_OrInteger() and var_NandInteger() functions...
39  */
40
41 static void HandleButtonPress (vout_thread_t *vout,
42                                xcb_button_press_event_t *ev)
43 {
44     unsigned buttons = var_GetInteger (vout, "mouse-button-down");
45     buttons |= (1 << (ev->detail - 1));
46     var_SetInteger (vout, "mouse-button-down", buttons);
47 }
48
49 static void HandleButtonRelease (vout_thread_t *vout,
50                                  xcb_button_release_event_t *ev)
51 {
52     unsigned buttons = var_GetInteger (vout, "mouse-button-down");
53     buttons &= ~(1 << (ev->detail - 1));
54     var_SetInteger (vout, "mouse-button-down", buttons);
55
56     if (ev->detail == 1) /* left mouse button */
57         var_SetBool (vout, "mouse-clicked", true);
58 }
59
60 static void HandleMotionNotify (vout_thread_t *vout,
61                                 xcb_motion_notify_event_t *ev)
62 {
63     unsigned x, y, width, height;
64     int v;
65
66     vout_PlacePicture (vout, vout->output.i_width, vout->output.i_height,
67                        &x, &y, &width, &height);
68     v = vout->fmt_in.i_x_offset
69         + ((ev->event_x - x) * vout->fmt_in.i_visible_width / width);
70     if (v < 0)
71         v = 0; /* to the left of the picture */
72     else if ((unsigned)v > vout->fmt_in.i_width)
73         v = vout->fmt_in.i_width; /* to the right of the picture */
74     var_SetInteger (vout, "mouse-x", v);
75
76     v = vout->fmt_in.i_y_offset
77         + ((ev->event_y - y) * vout->fmt_in.i_visible_height / height);
78     if (v < 0)
79         v = 0; /* above the picture */
80     else if ((unsigned)v > vout->fmt_in.i_height)
81         v = vout->fmt_in.i_height; /* below the picture */
82     var_SetInteger (vout, "mouse-y", v);
83 }
84
85 /**
86  * Process an X11 event.
87  */
88 int ProcessEvent (vout_thread_t *vout, xcb_generic_event_t *ev)
89 {
90     switch (ev->response_type & 0x7f)
91     {
92         case XCB_BUTTON_PRESS:
93             HandleButtonPress (vout, (xcb_button_press_event_t *)ev);
94             break;
95
96         case XCB_BUTTON_RELEASE:
97             HandleButtonRelease (vout, (xcb_button_release_event_t *)ev);
98             break;
99
100         case XCB_MOTION_NOTIFY:
101             HandleMotionNotify (vout, (xcb_motion_notify_event_t *)ev);
102             break;
103
104         default:
105             msg_Dbg (vout, "unhandled event %02x", (unsigned)ev->response_type);
106     }
107
108     free (ev);
109     return VLC_SUCCESS;
110 }