Merge pull request #4314 from MartijnKaijser/beta1
[vuplus_xbmc] / xbmc / input / touch / generic / GenericTouchActionHandler.cpp
1 /*
2  *      Copyright (C) 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
21 #include "GenericTouchActionHandler.h"
22 #include "ApplicationMessenger.h"
23 #include "guilib/GUIWindowManager.h"
24 #include "guilib/Key.h"
25 #include "windowing/WinEvents.h"
26
27 CGenericTouchActionHandler &CGenericTouchActionHandler::Get()
28 {
29   static CGenericTouchActionHandler sTouchAction;
30   return sTouchAction;
31 }
32
33 void CGenericTouchActionHandler::OnTouchAbort()
34 { }
35
36 bool CGenericTouchActionHandler::OnSingleTouchStart(float x, float y)
37 {
38   focusControl(x, y);
39
40   return true;
41 }
42
43 bool CGenericTouchActionHandler::OnSingleTouchHold(float x, float y)
44 {
45   return true;
46 }
47
48 bool CGenericTouchActionHandler::OnSingleTouchMove(float x, float y, float offsetX, float offsetY, float velocityX, float velocityY)
49 {
50   return true;
51 }
52
53 bool CGenericTouchActionHandler::OnSingleTouchEnd(float x, float y)
54 {
55   return true;
56 }
57
58 bool CGenericTouchActionHandler::OnMultiTouchDown(float x, float y, int32_t pointer)
59 {
60   return true;
61 }
62
63 bool CGenericTouchActionHandler::OnMultiTouchHold(float x, float y, int32_t pointers /* = 2 */)
64 {
65   return true;
66 }
67
68 bool CGenericTouchActionHandler::OnMultiTouchMove(float x, float y, float offsetX, float offsetY, float velocityX, float velocityY, int32_t pointer)
69 {
70   return true;
71 }
72
73 bool CGenericTouchActionHandler::OnMultiTouchUp(float x, float y, int32_t pointer)
74 {
75   return true;
76 }
77
78 bool CGenericTouchActionHandler::OnTouchGestureStart(float x, float y)
79 {
80   sendEvent(ACTION_GESTURE_BEGIN, x, y, 0.0f, 0.0f);
81
82   return true;
83 }
84
85 bool CGenericTouchActionHandler::OnTouchGesturePan(float x, float y, float offsetX, float offsetY, float velocityX, float velocityY)
86 {
87   sendEvent(ACTION_GESTURE_PAN, x, y, offsetX, offsetY);
88
89   return true;
90 }
91
92 bool CGenericTouchActionHandler::OnTouchGestureEnd(float x, float y, float offsetX, float offsetY, float velocityX, float velocityY)
93 {
94   sendEvent(ACTION_GESTURE_END, velocityX, velocityY, x, y);
95
96   return true;
97 }
98
99 void CGenericTouchActionHandler::OnTap(float x, float y, int32_t pointers /* = 1 */)
100 {
101   if (pointers <= 0 || pointers > 10)
102     return;
103
104   sendEvent(ACTION_TOUCH_TAP, (uint16_t)x, (uint16_t)y, 0.0f, 0.0f, pointers);
105 }
106
107 void CGenericTouchActionHandler::OnLongPress(float x, float y, int32_t pointers /* = 1 */)
108 {
109   if (pointers <= 0 || pointers > 10)
110     return;
111
112   sendEvent(ACTION_TOUCH_LONGPRESS, (uint16_t)x, (uint16_t)y, 0.0f, 0.0f, pointers);
113 }
114
115 void CGenericTouchActionHandler::OnSwipe(TouchMoveDirection direction, float xDown, float yDown, float xUp, float yUp, float velocityX, float velocityY, int32_t pointers /* = 1 */)
116 {
117   if (pointers <= 0 || pointers > 10)
118     return;
119
120   int actionId = 0;
121   if (direction == TouchMoveDirectionLeft)
122     actionId = ACTION_GESTURE_SWIPE_LEFT;
123   else if (direction == TouchMoveDirectionRight)
124     actionId = ACTION_GESTURE_SWIPE_RIGHT;
125   else if (direction == TouchMoveDirectionUp)
126     actionId = ACTION_GESTURE_SWIPE_UP;
127   else if (direction == TouchMoveDirectionDown)
128     actionId = ACTION_GESTURE_SWIPE_DOWN;
129   else
130     return;
131
132   sendEvent(actionId, xUp, yUp, velocityX, velocityY, pointers);
133 }
134
135 void CGenericTouchActionHandler::OnZoomPinch(float centerX, float centerY, float zoomFactor)
136 {
137   sendEvent(ACTION_GESTURE_ZOOM, centerX, centerY, zoomFactor, 0.0f);
138 }
139
140 void CGenericTouchActionHandler::OnRotate(float centerX, float centerY, float angle)
141 {
142   sendEvent(ACTION_GESTURE_ROTATE, centerX, centerY, angle, 0.0f);
143 }
144
145 int CGenericTouchActionHandler::QuerySupportedGestures(float x, float y)
146 {
147   CGUIMessage msg(GUI_MSG_GESTURE_NOTIFY, 0, 0, (int)x, (int)y);
148   if (!g_windowManager.SendMessage(msg))
149     return 0;
150
151   return msg.GetParam1();
152 }
153
154 void CGenericTouchActionHandler::touch(uint8_t type, uint8_t button, uint16_t x, uint16_t y)
155 {
156   XBMC_Event newEvent;
157   memset(&newEvent, 0, sizeof(newEvent));
158   
159   newEvent.type = type;
160   newEvent.button.type = type;
161   newEvent.button.button = button;
162   newEvent.button.x = x;
163   newEvent.button.y = y;
164   
165   CWinEvents::MessagePush(&newEvent);
166 }
167
168 void CGenericTouchActionHandler::sendEvent(int actionId, float x, float y, float x2 /* = 0.0f */, float y2 /* = 0.0f */, int pointers /* = 1 */)
169 {
170   XBMC_Event newEvent;
171   memset(&newEvent, 0, sizeof(newEvent));
172   
173   newEvent.type = XBMC_TOUCH;
174   newEvent.touch.type = XBMC_TOUCH;
175   newEvent.touch.action = actionId;
176   newEvent.touch.x = x;
177   newEvent.touch.y = y;
178   newEvent.touch.x2 = x2;
179   newEvent.touch.y2 = y2;
180   newEvent.touch.pointers = pointers;
181
182   CWinEvents::MessagePush(&newEvent);
183 }
184
185 void CGenericTouchActionHandler::focusControl(float x, float y)
186 {
187   XBMC_Event newEvent;
188   memset(&newEvent, 0, sizeof(newEvent));
189
190   newEvent.type = XBMC_SETFOCUS;
191   newEvent.focus.type = XBMC_SETFOCUS;
192   newEvent.focus.x = (uint16_t)x;
193   newEvent.focus.y = (uint16_t)y;
194
195   CWinEvents::MessagePush(&newEvent);
196 }