2011-04-23 23:54:25 -07:00
|
|
|
/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-04-26 05:30:17 -07:00
|
|
|
|
|
|
|
#include "nsDOMTouchEvent.h"
|
|
|
|
#include "nsGUIEvent.h"
|
|
|
|
#include "nsContentUtils.h"
|
2011-05-24 23:31:59 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-12-16 16:24:11 -08:00
|
|
|
#include "nsPresContext.h"
|
2013-04-05 01:49:00 -07:00
|
|
|
#include "mozilla/dom/Touch.h"
|
2011-05-24 23:31:59 -07:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2013-04-19 15:18:33 -07:00
|
|
|
using namespace mozilla::dom;
|
2011-04-26 05:30:17 -07:00
|
|
|
|
|
|
|
// TouchList
|
2012-08-02 10:41:42 -07:00
|
|
|
nsDOMTouchList::nsDOMTouchList(nsTArray<nsCOMPtr<nsIDOMTouch> > &aTouches)
|
|
|
|
{
|
|
|
|
mPoints.AppendElements(aTouches);
|
2013-03-07 10:53:19 -08:00
|
|
|
nsJSContext::LikelyShortLivingObjectCreated();
|
2012-08-02 10:41:42 -07:00
|
|
|
}
|
2011-04-26 05:30:17 -07:00
|
|
|
|
|
|
|
DOMCI_DATA(TouchList, nsDOMTouchList)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMTouchList)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMTouchList)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(TouchList)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-01-12 04:40:33 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_1(nsDOMTouchList, mPoints)
|
2011-04-26 05:30:17 -07:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMTouchList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMTouchList)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDOMTouchList::GetLength(uint32_t* aLength)
|
2011-04-26 05:30:17 -07:00
|
|
|
{
|
2011-12-16 16:24:11 -08:00
|
|
|
*aLength = mPoints.Length();
|
2011-04-26 05:30:17 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDOMTouchList::Item(uint32_t aIndex, nsIDOMTouch** aRetVal)
|
2011-04-26 05:30:17 -07:00
|
|
|
{
|
2012-08-12 03:43:47 -07:00
|
|
|
NS_IF_ADDREF(*aRetVal = mPoints.SafeElementAt(aIndex, nullptr));
|
2011-04-26 05:30:17 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDOMTouchList::IdentifiedTouch(int32_t aIdentifier, nsIDOMTouch** aRetVal)
|
2011-04-26 05:30:17 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aRetVal = nullptr;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mPoints.Length(); ++i) {
|
2011-04-27 04:58:25 -07:00
|
|
|
nsCOMPtr<nsIDOMTouch> point = mPoints[i];
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t identifier;
|
2011-04-26 05:30:17 -07:00
|
|
|
if (point && NS_SUCCEEDED(point->GetIdentifier(&identifier)) &&
|
|
|
|
aIdentifier == identifier) {
|
|
|
|
point.swap(*aRetVal);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TouchEvent
|
|
|
|
|
2013-03-09 03:34:29 -08:00
|
|
|
nsDOMTouchEvent::nsDOMTouchEvent(mozilla::dom::EventTarget* aOwner,
|
|
|
|
nsPresContext* aPresContext,
|
2011-12-16 16:24:11 -08:00
|
|
|
nsTouchEvent* aEvent)
|
2013-03-09 03:34:29 -08:00
|
|
|
: nsDOMUIEvent(aOwner, aPresContext,
|
|
|
|
aEvent ? aEvent : new nsTouchEvent(false, 0, nullptr))
|
2011-04-26 05:30:17 -07:00
|
|
|
{
|
|
|
|
if (aEvent) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mEventIsInternal = false;
|
2011-12-16 16:24:11 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aEvent->touches.Length(); ++i) {
|
2011-12-16 16:24:11 -08:00
|
|
|
nsIDOMTouch *touch = aEvent->touches[i];
|
2013-04-05 01:49:00 -07:00
|
|
|
dom::Touch *domtouch = static_cast<dom::Touch*>(touch);
|
2011-12-16 16:24:11 -08:00
|
|
|
domtouch->InitializePoints(mPresContext, aEvent);
|
|
|
|
}
|
2011-04-26 05:30:17 -07:00
|
|
|
} else {
|
2011-10-17 07:59:28 -07:00
|
|
|
mEventIsInternal = true;
|
2011-04-26 05:30:17 -07:00
|
|
|
mEvent->time = PR_Now();
|
|
|
|
}
|
2013-04-15 13:33:46 -07:00
|
|
|
SetIsDOMBinding();
|
2011-04-26 05:30:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMTouchEvent::~nsDOMTouchEvent()
|
|
|
|
{
|
|
|
|
if (mEventIsInternal && mEvent) {
|
2011-12-16 16:24:11 -08:00
|
|
|
delete static_cast<nsTouchEvent*>(mEvent);
|
2012-07-30 07:20:58 -07:00
|
|
|
mEvent = nullptr;
|
2011-04-26 05:30:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsDOMTouchEvent, nsDOMUIEvent)
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mTouches)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mTargetTouches)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mChangedTouches)
|
2011-04-26 05:30:17 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsDOMTouchEvent, nsDOMUIEvent)
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTouches)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTargetTouches)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mChangedTouches)
|
2011-04-26 05:30:17 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDOMTouchEvent)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMTouchEvent)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMUIEvent)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(nsDOMTouchEvent, nsDOMUIEvent)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsDOMTouchEvent, nsDOMUIEvent)
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMTouchEvent::InitTouchEvent(const nsAString& aType,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aCanBubble,
|
|
|
|
bool aCancelable,
|
2011-04-23 23:54:25 -07:00
|
|
|
nsIDOMWindow* aView,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aDetail,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aCtrlKey,
|
|
|
|
bool aAltKey,
|
|
|
|
bool aShiftKey,
|
|
|
|
bool aMetaKey,
|
2011-04-26 05:30:17 -07:00
|
|
|
nsIDOMTouchList* aTouches,
|
|
|
|
nsIDOMTouchList* aTargetTouches,
|
|
|
|
nsIDOMTouchList* aChangedTouches)
|
|
|
|
{
|
2011-12-16 16:24:11 -08:00
|
|
|
nsresult rv = nsDOMUIEvent::InitUIEvent(aType,
|
|
|
|
aCanBubble,
|
|
|
|
aCancelable,
|
|
|
|
aView,
|
|
|
|
aDetail);
|
2011-04-26 05:30:17 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-04-24 20:00:02 -07:00
|
|
|
static_cast<nsInputEvent*>(mEvent)->InitBasicModifiers(aCtrlKey, aAltKey,
|
|
|
|
aShiftKey, aMetaKey);
|
2013-04-25 23:48:00 -07:00
|
|
|
mTouches = static_cast<nsDOMTouchList*>(aTouches);
|
|
|
|
mTargetTouches = static_cast<nsDOMTouchList*>(aTargetTouches);
|
|
|
|
mChangedTouches = static_cast<nsDOMTouchList*>(aChangedTouches);
|
2011-04-26 05:30:17 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMTouchEvent::GetTouches(nsIDOMTouchList** aTouches)
|
|
|
|
{
|
2011-12-16 16:24:11 -08:00
|
|
|
NS_ENSURE_ARG_POINTER(aTouches);
|
2013-04-25 23:48:00 -07:00
|
|
|
NS_ADDREF(*aTouches = Touches());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-12-16 16:24:11 -08:00
|
|
|
|
2013-04-25 23:48:00 -07:00
|
|
|
nsDOMTouchList*
|
|
|
|
nsDOMTouchEvent::Touches()
|
|
|
|
{
|
|
|
|
if (!mTouches) {
|
|
|
|
nsTouchEvent* touchEvent = static_cast<nsTouchEvent*>(mEvent);
|
|
|
|
if (mEvent->message == NS_TOUCH_END || mEvent->message == NS_TOUCH_CANCEL) {
|
|
|
|
// for touchend events, remove any changed touches from the touches array
|
|
|
|
nsTArray<nsCOMPtr<nsIDOMTouch> > unchangedTouches;
|
|
|
|
const nsTArray<nsCOMPtr<nsIDOMTouch> >& touches = touchEvent->touches;
|
|
|
|
for (uint32_t i = 0; i < touches.Length(); ++i) {
|
|
|
|
if (!touches[i]->mChanged) {
|
|
|
|
unchangedTouches.AppendElement(touches[i]);
|
|
|
|
}
|
2011-12-16 16:24:11 -08:00
|
|
|
}
|
2013-04-25 23:48:00 -07:00
|
|
|
mTouches = new nsDOMTouchList(unchangedTouches);
|
|
|
|
} else {
|
|
|
|
mTouches = new nsDOMTouchList(touchEvent->touches);
|
2011-12-16 16:24:11 -08:00
|
|
|
}
|
|
|
|
}
|
2013-04-25 23:48:00 -07:00
|
|
|
return mTouches;
|
2011-04-26 05:30:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMTouchEvent::GetTargetTouches(nsIDOMTouchList** aTargetTouches)
|
|
|
|
{
|
2011-12-16 16:24:11 -08:00
|
|
|
NS_ENSURE_ARG_POINTER(aTargetTouches);
|
2013-04-25 23:48:00 -07:00
|
|
|
NS_ADDREF(*aTargetTouches = TargetTouches());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-12-16 16:24:11 -08:00
|
|
|
|
2013-04-25 23:48:00 -07:00
|
|
|
nsDOMTouchList*
|
|
|
|
nsDOMTouchEvent::TargetTouches()
|
|
|
|
{
|
|
|
|
if (!mTargetTouches) {
|
|
|
|
nsTArray<nsCOMPtr<nsIDOMTouch> > targetTouches;
|
|
|
|
nsTouchEvent* touchEvent = static_cast<nsTouchEvent*>(mEvent);
|
|
|
|
const nsTArray<nsCOMPtr<nsIDOMTouch> >& touches = touchEvent->touches;
|
|
|
|
for (uint32_t i = 0; i < touches.Length(); ++i) {
|
|
|
|
// for touchend/cancel events, don't append to the target list if this is a
|
|
|
|
// touch that is ending
|
|
|
|
if ((mEvent->message != NS_TOUCH_END &&
|
|
|
|
mEvent->message != NS_TOUCH_CANCEL) || !touches[i]->mChanged) {
|
|
|
|
EventTarget* targetPtr = touches[i]->GetTarget();
|
|
|
|
if (targetPtr == mEvent->originalTarget) {
|
|
|
|
targetTouches.AppendElement(touches[i]);
|
|
|
|
}
|
2011-12-16 16:24:11 -08:00
|
|
|
}
|
|
|
|
}
|
2013-04-25 23:48:00 -07:00
|
|
|
mTargetTouches = new nsDOMTouchList(targetTouches);
|
2011-12-16 16:24:11 -08:00
|
|
|
}
|
2013-04-25 23:48:00 -07:00
|
|
|
return mTargetTouches;
|
2011-04-26 05:30:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMTouchEvent::GetChangedTouches(nsIDOMTouchList** aChangedTouches)
|
|
|
|
{
|
2011-12-16 16:24:11 -08:00
|
|
|
NS_ENSURE_ARG_POINTER(aChangedTouches);
|
2013-04-25 23:48:00 -07:00
|
|
|
NS_ADDREF(*aChangedTouches = ChangedTouches());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-12-16 16:24:11 -08:00
|
|
|
|
2013-04-25 23:48:00 -07:00
|
|
|
nsDOMTouchList*
|
|
|
|
nsDOMTouchEvent::ChangedTouches()
|
|
|
|
{
|
|
|
|
if (!mChangedTouches) {
|
|
|
|
nsTArray<nsCOMPtr<nsIDOMTouch> > changedTouches;
|
|
|
|
nsTouchEvent* touchEvent = static_cast<nsTouchEvent*>(mEvent);
|
|
|
|
const nsTArray<nsCOMPtr<nsIDOMTouch> >& touches = touchEvent->touches;
|
|
|
|
for (uint32_t i = 0; i < touches.Length(); ++i) {
|
|
|
|
if (touches[i]->mChanged) {
|
|
|
|
changedTouches.AppendElement(touches[i]);
|
|
|
|
}
|
2011-12-16 16:24:11 -08:00
|
|
|
}
|
2013-04-25 23:48:00 -07:00
|
|
|
mChangedTouches = new nsDOMTouchList(changedTouches);
|
2011-12-16 16:24:11 -08:00
|
|
|
}
|
2013-04-25 23:48:00 -07:00
|
|
|
return mChangedTouches;
|
2011-04-26 05:30:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDOMTouchEvent::GetAltKey(bool* aAltKey)
|
2011-04-26 05:30:17 -07:00
|
|
|
{
|
2013-04-15 13:33:46 -07:00
|
|
|
*aAltKey = AltKey();
|
2011-04-26 05:30:17 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDOMTouchEvent::GetMetaKey(bool* aMetaKey)
|
2011-04-26 05:30:17 -07:00
|
|
|
{
|
2013-04-15 13:33:46 -07:00
|
|
|
*aMetaKey = MetaKey();
|
2011-04-26 05:30:17 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDOMTouchEvent::GetCtrlKey(bool* aCtrlKey)
|
2011-04-26 05:30:17 -07:00
|
|
|
{
|
2013-04-15 13:33:46 -07:00
|
|
|
*aCtrlKey = CtrlKey();
|
2011-04-26 05:30:17 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDOMTouchEvent::GetShiftKey(bool* aShiftKey)
|
2011-04-26 05:30:17 -07:00
|
|
|
{
|
2013-04-15 13:33:46 -07:00
|
|
|
*aShiftKey = ShiftKey();
|
2011-04-26 05:30:17 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-25 07:57:51 -07:00
|
|
|
#ifdef XP_WIN
|
|
|
|
namespace mozilla {
|
|
|
|
namespace widget {
|
|
|
|
extern int32_t IsTouchDeviceSupportPresent();
|
|
|
|
} }
|
|
|
|
#endif
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-04-26 05:30:17 -07:00
|
|
|
nsDOMTouchEvent::PrefEnabled()
|
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool sDidCheckPref = false;
|
|
|
|
static bool sPrefValue = false;
|
2011-04-26 05:30:17 -07:00
|
|
|
if (!sDidCheckPref) {
|
2011-10-17 07:59:28 -07:00
|
|
|
sDidCheckPref = true;
|
2012-10-25 07:57:51 -07:00
|
|
|
int32_t flag = 0;
|
|
|
|
if (NS_SUCCEEDED(Preferences::GetInt("dom.w3c_touch_events.enabled",
|
|
|
|
&flag))) {
|
|
|
|
if (flag == 2) {
|
|
|
|
#ifdef XP_WIN
|
|
|
|
// On Windows we auto-detect based on device support.
|
|
|
|
sPrefValue = mozilla::widget::IsTouchDeviceSupportPresent();
|
|
|
|
#else
|
|
|
|
NS_WARNING("dom.w3c_touch_events.enabled=2 not implemented!");
|
|
|
|
sPrefValue = false;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
sPrefValue = !!flag;
|
|
|
|
}
|
|
|
|
}
|
2011-04-26 05:30:17 -07:00
|
|
|
if (sPrefValue) {
|
|
|
|
nsContentUtils::InitializeTouchEventTable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sPrefValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewDOMTouchEvent(nsIDOMEvent** aInstancePtrResult,
|
2013-03-09 03:34:29 -08:00
|
|
|
mozilla::dom::EventTarget* aOwner,
|
2011-04-26 05:30:17 -07:00
|
|
|
nsPresContext* aPresContext,
|
2011-12-16 16:24:11 -08:00
|
|
|
nsTouchEvent *aEvent)
|
2011-04-26 05:30:17 -07:00
|
|
|
{
|
2013-03-09 03:34:29 -08:00
|
|
|
nsDOMTouchEvent* it = new nsDOMTouchEvent(aOwner, aPresContext, aEvent);
|
2011-04-26 05:30:17 -07:00
|
|
|
return CallQueryInterface(it, aInstancePtrResult);
|
|
|
|
}
|