[cosmetic] cleanup copyright headers
[vuplus_xbmc] / xbmc / windowing / wayland / PointerProcessor.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20 #include <wayland-client.h>
21 #include <xkbcommon/xkbcommon.h>
22
23 #include "windowing/DllWaylandClient.h"
24 #include "windowing/DllXKBCommon.h"
25
26 #include "CursorManager.h"
27 #include "EventListener.h"
28 #include "Pointer.h"
29 #include "PointerProcessor.h"
30
31 xbmc::PointerProcessor::PointerProcessor(IEventListener &listener,
32                                          ICursorManager &manager) :
33   m_listener(listener),
34   m_cursorManager(manager)
35 {
36 }
37
38 void xbmc::PointerProcessor::Motion(uint32_t time,
39                                     const float &x,
40                                     const float &y)
41 {
42   XBMC_Event event;
43
44   event.type = XBMC_MOUSEMOTION;
45   event.motion.xrel = ::round(x);
46   event.motion.yrel = ::round(y);
47   event.motion.state = 0;
48   event.motion.type = XBMC_MOUSEMOTION;
49   event.motion.which = 0;
50   event.motion.x = event.motion.xrel;
51   event.motion.y = event.motion.yrel;
52
53   m_lastPointerX = x;
54   m_lastPointerY = y;
55
56   m_listener.OnEvent(event);
57 }
58
59 void xbmc::PointerProcessor::Button(uint32_t serial,
60                                     uint32_t time,
61                                     uint32_t button,
62                                     enum wl_pointer_button_state state)
63 {
64   static const struct ButtonTable
65   {
66     unsigned int WaylandButton;
67     unsigned int XBMCButton;
68   } buttonTable[] =
69   {
70     { WaylandLeftButton, 1 },
71     { WaylandMiddleButton, 2 },
72     { WaylandRightButton, 3 }
73   };
74
75   size_t buttonTableSize = sizeof(buttonTable) / sizeof(buttonTable[0]);
76
77   /* Find the xbmc button number that corresponds to the evdev
78    * button that we just received. There may be some buttons we don't
79    * recognize so just ignore them */
80   unsigned int xbmcButton = 0;
81
82   for (size_t i = 0; i < buttonTableSize; ++i)
83     if (buttonTable[i].WaylandButton == button)
84       xbmcButton = buttonTable[i].XBMCButton;
85
86   if (!xbmcButton)
87     return;
88
89   /* Keep track of currently pressed buttons, we need that for
90    * motion events */
91   if (state & WL_POINTER_BUTTON_STATE_PRESSED)
92     m_currentlyPressedButton |= 1 << button;
93   else if (state & WL_POINTER_BUTTON_STATE_RELEASED)
94     m_currentlyPressedButton &= ~(1 << button);
95
96   XBMC_Event event;
97
98   event.type = state & WL_POINTER_BUTTON_STATE_PRESSED ?
99                XBMC_MOUSEBUTTONDOWN : XBMC_MOUSEBUTTONUP;
100   event.button.button = xbmcButton;
101   event.button.state = 0;
102   event.button.type = event.type;
103   event.button.which = 0;
104   event.button.x = ::round(m_lastPointerX);
105   event.button.y = ::round(m_lastPointerY);
106
107   m_listener.OnEvent(event);
108 }
109
110 void xbmc::PointerProcessor::Axis(uint32_t time,
111                                   uint32_t axis,
112                                   float value)
113 {
114   if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
115   {
116     /* Negative is up */
117     bool direction = value < 0.0f;
118     int  button = direction ? WheelUpButton :
119                               WheelDownButton;
120
121     /* For axis events we only care about the vector direction
122      * and not the scalar magnitude. Every axis event callback
123      * generates one scroll button event for XBMC */
124     XBMC_Event event;
125
126     event.type = XBMC_MOUSEBUTTONDOWN;
127     event.button.button = button;
128     event.button.state = 0;
129     event.button.type = XBMC_MOUSEBUTTONDOWN;
130     event.button.which = 0;
131     event.button.x = ::round(m_lastPointerX);
132     event.button.y = ::round(m_lastPointerY);
133
134     m_listener.OnEvent(event);
135     
136     /* We must also send a button up on the same
137      * wheel "button" */
138     event.type = XBMC_MOUSEBUTTONUP;
139     event.button.type = XBMC_MOUSEBUTTONUP;
140     
141     m_listener.OnEvent(event);
142   }
143 }
144
145 void
146 xbmc::PointerProcessor::Enter(struct wl_surface *surface,
147                               double surfaceX,
148                               double surfaceY)
149 {
150   m_cursorManager.SetCursor(0, NULL, 0, 0);
151 }