TouchCounter.cpp (2232B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "TouchCounter.h" 8 9 #include "InputData.h" 10 #include "mozilla/TouchEvents.h" 11 12 namespace mozilla { 13 namespace layers { 14 15 TouchCounter::TouchCounter() : mActiveTouchCount(0) {} 16 17 void TouchCounter::Update(const MultiTouchInput& aInput) { 18 switch (aInput.mType) { 19 case MultiTouchInput::MULTITOUCH_START: 20 // touch-start event contains all active touches of the current session 21 mActiveTouchCount = aInput.mTouches.Length(); 22 break; 23 case MultiTouchInput::MULTITOUCH_END: 24 if (mActiveTouchCount >= aInput.mTouches.Length()) { 25 // touch-end event contains only released touches 26 mActiveTouchCount -= aInput.mTouches.Length(); 27 } else { 28 NS_WARNING("Got an unexpected touchend"); 29 mActiveTouchCount = 0; 30 } 31 break; 32 case MultiTouchInput::MULTITOUCH_CANCEL: 33 mActiveTouchCount = 0; 34 break; 35 case MultiTouchInput::MULTITOUCH_MOVE: 36 break; 37 } 38 } 39 40 void TouchCounter::Update(const WidgetTouchEvent& aEvent) { 41 switch (aEvent.mMessage) { 42 case eTouchStart: 43 // touch-start event contains all active touches of the current session 44 mActiveTouchCount = aEvent.mTouches.Length(); 45 break; 46 case eTouchEnd: { 47 // touch-end contains all touches, but ones being lifted are marked as 48 // changed 49 uint32_t liftedTouches = 0; 50 for (const auto& touch : aEvent.mTouches) { 51 if (touch->mChanged) { 52 liftedTouches++; 53 } 54 } 55 if (mActiveTouchCount >= liftedTouches) { 56 mActiveTouchCount -= liftedTouches; 57 } else { 58 NS_WARNING("Got an unexpected touchend"); 59 mActiveTouchCount = 0; 60 } 61 break; 62 } 63 case eTouchCancel: 64 mActiveTouchCount = 0; 65 break; 66 default: 67 break; 68 } 69 } 70 71 uint32_t TouchCounter::GetActiveTouchCount() const { return mActiveTouchCount; } 72 73 } // namespace layers 74 } // namespace mozilla