2 * Copyright (C) 2012-2013 Team XBMC
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)
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.
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/>.
21 #include "AndroidTouch.h"
23 #include "guilib/GUIWindowManager.h"
24 #include "windowing/WinEvents.h"
25 #include "ApplicationMessenger.h"
27 CAndroidTouch::CAndroidTouch() : m_dpi(160)
29 CTouchInput::Get().RegisterHandler(this);
32 CAndroidTouch::~CAndroidTouch()
34 CTouchInput::Get().UnregisterHandler();
37 bool CAndroidTouch::onTouchEvent(AInputEvent* event)
39 CXBMCApp::android_printf("%s", __PRETTY_FUNCTION__);
43 size_t numPointers = AMotionEvent_getPointerCount(event);
46 CXBMCApp::android_printf(" => aborting touch event because there are no active pointers");
50 if (numPointers > TOUCH_MAX_POINTERS)
51 numPointers = TOUCH_MAX_POINTERS;
53 int32_t eventAction = AMotionEvent_getAction(event);
54 int8_t touchAction = eventAction & AMOTION_EVENT_ACTION_MASK;
55 size_t touchPointer = eventAction >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
57 CTouchInput::TouchEvent touchEvent = CTouchInput::TouchEventAbort;
60 case AMOTION_EVENT_ACTION_DOWN:
61 case AMOTION_EVENT_ACTION_POINTER_DOWN:
62 touchEvent = CTouchInput::TouchEventDown;
65 case AMOTION_EVENT_ACTION_UP:
66 case AMOTION_EVENT_ACTION_POINTER_UP:
67 touchEvent = CTouchInput::TouchEventUp;
70 case AMOTION_EVENT_ACTION_MOVE:
71 touchEvent = CTouchInput::TouchEventMove;
74 case AMOTION_EVENT_ACTION_OUTSIDE:
75 case AMOTION_EVENT_ACTION_CANCEL:
80 float x = AMotionEvent_getX(event, touchPointer);
81 float y = AMotionEvent_getY(event, touchPointer);
82 float size = m_dpi / 16.0f;
83 int64_t time = AMotionEvent_getEventTime(event);
85 // first update all touch pointers
86 for (unsigned int pointer = 0; pointer < numPointers; pointer++)
87 CTouchInput::Get().Update(pointer, AMotionEvent_getX(event, pointer), AMotionEvent_getY(event, pointer),
88 AMotionEvent_getEventTime(event), m_dpi / 16.0f);
91 return CTouchInput::Get().Handle(touchEvent, x, y, time, touchPointer, size);
94 bool CAndroidTouch::OnSingleTouchStart(float x, float y)
96 // Send a mouse motion event for getting the current guiitem selected
97 XBMC_Touch(XBMC_MOUSEMOTION, 0, (uint16_t)x, (uint16_t)y);
102 bool CAndroidTouch::OnMultiTouchStart(float x, float y, int32_t pointers /* = 2 */)
104 XBMC_TouchGesture(ACTION_GESTURE_BEGIN, x, y, 0.0f, 0.0f);
109 bool CAndroidTouch::OnMultiTouchEnd(float x, float y, int32_t pointers /* = 2 */)
111 XBMC_TouchGesture(ACTION_GESTURE_END, 0.0f, 0.0f, 0.0f, 0.0f);
116 bool CAndroidTouch::OnTouchGesturePanStart(float x, float y)
118 XBMC_TouchGesture(ACTION_GESTURE_BEGIN, x, y, 0.0f, 0.0f);
123 bool CAndroidTouch::OnTouchGesturePan(float x, float y, float offsetX, float offsetY, float velocityX, float velocityY)
125 XBMC_TouchGesture(ACTION_GESTURE_PAN, x, y, offsetX, offsetY);
130 bool CAndroidTouch::OnTouchGesturePanEnd(float x, float y, float offsetX, float offsetY, float velocityX, float velocityY)
132 XBMC_TouchGesture(ACTION_GESTURE_END, velocityX, velocityY, x, y);
134 // unfocus the focused GUI item
135 g_windowManager.SendMessage(GUI_MSG_UNFOCUS_ALL, 0, 0, 0, 0);
140 void CAndroidTouch::OnSingleTap(float x, float y)
142 XBMC_Touch(XBMC_MOUSEBUTTONDOWN, XBMC_BUTTON_LEFT, (uint16_t)x, (uint16_t)y);
143 XBMC_Touch(XBMC_MOUSEBUTTONUP, XBMC_BUTTON_LEFT, (uint16_t)x, (uint16_t)y);
146 void CAndroidTouch::OnSingleLongPress(float x, float y)
148 // we send a right-click for this
149 XBMC_Touch(XBMC_MOUSEBUTTONDOWN, XBMC_BUTTON_RIGHT, (uint16_t)x, (uint16_t)y);
150 XBMC_Touch(XBMC_MOUSEBUTTONUP, XBMC_BUTTON_RIGHT, (uint16_t)x, (uint16_t)y);
153 void CAndroidTouch::OnZoomPinch(float centerX, float centerY, float zoomFactor)
155 XBMC_TouchGesture(ACTION_GESTURE_ZOOM, centerX, centerY, zoomFactor, 0);
158 void CAndroidTouch::OnRotate(float centerX, float centerY, float angle)
160 XBMC_TouchGesture(ACTION_GESTURE_ROTATE, centerX, centerY, angle, 0);
163 void CAndroidTouch::setDPI(uint32_t dpi)
169 void CAndroidTouch::XBMC_Touch(uint8_t type, uint8_t button, uint16_t x, uint16_t y)
172 memset(&newEvent, 0, sizeof(newEvent));
174 newEvent.type = type;
175 newEvent.button.type = type;
176 newEvent.button.button = button;
177 newEvent.button.x = x;
178 newEvent.button.y = y;
180 CXBMCApp::android_printf("XBMC_Touch(%u, %u, %u, %u)", type, button, x, y);
181 CWinEvents::MessagePush(&newEvent);
184 void CAndroidTouch::XBMC_TouchGesture(int32_t action, float posX, float posY, float offsetX, float offsetY)
186 CXBMCApp::android_printf("XBMC_TouchGesture(%d, %f, %f, %f, %f)", action, posX, posY, offsetX, offsetY);
187 if (action == ACTION_GESTURE_BEGIN)
188 CApplicationMessenger::Get().SendAction(CAction(action, 0, posX, posY, 0, 0), WINDOW_INVALID, false);
189 else if (action == ACTION_GESTURE_PAN)
190 CApplicationMessenger::Get().SendAction(CAction(action, 0, posX, posY, offsetX, offsetY), WINDOW_INVALID, false);
191 else if (action == ACTION_GESTURE_END)
192 CApplicationMessenger::Get().SendAction(CAction(action, 0, posX, posY, offsetX, offsetY), WINDOW_INVALID, false);
193 else if (action == ACTION_GESTURE_ZOOM || action == ACTION_GESTURE_ROTATE)
194 CApplicationMessenger::Get().SendAction(CAction(action, 0, posX, posY, offsetX, 0), WINDOW_INVALID, false);