Skip to content

Commit 3d724ce

Browse files
committed
Bug 1555484 - Clean DS4 GamepadTouchState when stopping touching. r=baku
Differential Revision: https://phabricator.services.mozilla.com/D33209 --HG-- extra : moz-landing-system : lando
1 parent c5bc139 commit 3d724ce

File tree

2 files changed

+50
-37
lines changed

2 files changed

+50
-37
lines changed

dom/gamepad/Gamepad.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ Gamepad::Gamepad(nsISupports* aParent, const nsAString& aID, uint32_t aIndex,
7070
mTouchEvents.AppendElement(new GamepadTouch(mParent));
7171
}
7272

73+
// Mapping touchId(0) to touchIdHash(0) by default.
74+
mTouchIdHash.Put(0, mTouchIdHashValue);
75+
++mTouchIdHashValue;
7376
UpdateTimestamp();
7477
}
7578

dom/gamepad/GamepadRemapping.cpp

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,11 @@ class StadiaControllerRemapper final : public GamepadRemapper {
361361

362362
class Dualshock4Remapper final : public GamepadRemapper {
363363
public:
364+
Dualshock4Remapper() {
365+
mLastTouches.SetLength(TOUCH_EVENT_COUNT);
366+
mLastTouchId.SetLength(TOUCH_EVENT_COUNT);
367+
}
368+
364369
virtual uint32_t GetAxisCount() const override { return AXIS_INDEX_COUNT; }
365370

366371
virtual uint32_t GetButtonCount() const override {
@@ -403,54 +408,59 @@ class Dualshock4Remapper final : public GamepadRemapper {
403408
touches.SetLength(TOUCH_EVENT_COUNT);
404409
uint8_t* rawData = (uint8_t*)aInput;
405410

411+
const uint32_t kTouchDimensionX = 1920;
412+
const uint32_t kTouchDimensionY = 942;
406413
bool touch0Pressed = (((rawData[35] & 0xff) >> 7) == 0);
407414
bool touch1Pressed = (((rawData[39] & 0xff) >> 7) == 0);
408415

409-
if (!touch0Pressed && !touch1Pressed) {
410-
return;
411-
}
412-
413-
if ((touch0Pressed && (rawData[35] & 0xff) < mLastTouch0Id) ||
414-
(touch1Pressed && (rawData[39] & 0xff) < mLastTouch1Id)) {
416+
if ((touch0Pressed && (rawData[35] & 0xff) < mLastTouchId[0]) ||
417+
(touch1Pressed && (rawData[39] & 0xff) < mLastTouchId[1])) {
415418
mTouchIdBase += 128;
416419
}
417420

418-
const uint32_t kTouchDimensionX = 1920;
419-
const uint32_t kTouchDimensionY = 942;
420-
421-
touches[0].touchId = mTouchIdBase + (rawData[35] & 0x7f);
422-
touches[0].surfaceId = 0;
423-
touches[0].position[0] = NormalizeTouch(
424-
((rawData[37] & 0xf) << 8) | rawData[36], 0, (kTouchDimensionX - 1));
425-
touches[0].position[1] =
426-
NormalizeTouch(rawData[38] << 4 | ((rawData[37] & 0xf0) >> 4), 0,
427-
(kTouchDimensionY - 1));
428-
touches[0].surfaceDimensions[0] = kTouchDimensionX;
429-
touches[0].surfaceDimensions[1] = kTouchDimensionY;
430-
touches[0].isSurfaceDimensionsValid = true;
431-
mLastTouch0Id = rawData[35] & 0x7f;
432-
433-
touches[1].touchId = mTouchIdBase + (rawData[39] & 0x7f);
434-
touches[1].surfaceId = 0;
435-
touches[1].position[0] =
436-
NormalizeTouch((((rawData[41] & 0xf) << 8) | rawData[40]) + 1, 0,
437-
(kTouchDimensionX - 1));
438-
touches[1].position[1] =
439-
NormalizeTouch(rawData[42] << 4 | ((rawData[41] & 0xf0) >> 4), 0,
440-
(kTouchDimensionY - 1));
441-
touches[1].surfaceDimensions[0] = kTouchDimensionX;
442-
touches[1].surfaceDimensions[1] = kTouchDimensionY;
443-
touches[1].isSurfaceDimensionsValid = true;
444-
mLastTouch1Id = rawData[39] & 0x7f;
421+
if (touch0Pressed) {
422+
touches[0].touchId = mTouchIdBase + (rawData[35] & 0x7f);
423+
touches[0].surfaceId = 0;
424+
touches[0].position[0] = NormalizeTouch(
425+
((rawData[37] & 0xf) << 8) | rawData[36], 0, (kTouchDimensionX - 1));
426+
touches[0].position[1] =
427+
NormalizeTouch(rawData[38] << 4 | ((rawData[37] & 0xf0) >> 4), 0,
428+
(kTouchDimensionY - 1));
429+
touches[0].surfaceDimensions[0] = kTouchDimensionX;
430+
touches[0].surfaceDimensions[1] = kTouchDimensionY;
431+
touches[0].isSurfaceDimensionsValid = true;
432+
mLastTouchId[0] = rawData[35] & 0x7f;
433+
}
434+
if (touch1Pressed) {
435+
touches[1].touchId = mTouchIdBase + (rawData[39] & 0x7f);
436+
touches[1].surfaceId = 0;
437+
touches[1].position[0] =
438+
NormalizeTouch((((rawData[41] & 0xf) << 8) | rawData[40]) + 1, 0,
439+
(kTouchDimensionX - 1));
440+
touches[1].position[1] =
441+
NormalizeTouch(rawData[42] << 4 | ((rawData[41] & 0xf0) >> 4), 0,
442+
(kTouchDimensionY - 1));
443+
touches[1].surfaceDimensions[0] = kTouchDimensionX;
444+
touches[1].surfaceDimensions[1] = kTouchDimensionY;
445+
touches[1].isSurfaceDimensionsValid = true;
446+
mLastTouchId[1] = rawData[39] & 0x7f;
447+
}
445448

446449
RefPtr<GamepadPlatformService> service =
447450
GamepadPlatformService::GetParentService();
448451
if (!service) {
449452
return;
450453
}
451454

452-
service->NewMultiTouchEvent(aIndex, 0, touches[0]);
453-
service->NewMultiTouchEvent(aIndex, 1, touches[1]);
455+
// Avoid to send duplicate untouched events to the gamepad service.
456+
if ((mLastTouches[0] != touch0Pressed) || touch0Pressed) {
457+
service->NewMultiTouchEvent(aIndex, 0, touches[0]);
458+
}
459+
if ((mLastTouches[1] != touch1Pressed) || touch1Pressed) {
460+
service->NewMultiTouchEvent(aIndex, 1, touches[1]);
461+
}
462+
mLastTouches[0] = touch0Pressed;
463+
mLastTouches[1] = touch1Pressed;
454464
}
455465

456466
virtual void RemapAxisMoveEvent(uint32_t aIndex, uint32_t aAxis,
@@ -537,8 +547,8 @@ class Dualshock4Remapper final : public GamepadRemapper {
537547
static const uint32_t LIGHT_INDICATOR_COUNT = 1;
538548
static const uint32_t TOUCH_EVENT_COUNT = 2;
539549

540-
unsigned long mLastTouch0Id = 0;
541-
unsigned long mLastTouch1Id = 0;
550+
nsTArray<unsigned long> mLastTouchId;
551+
nsTArray<bool> mLastTouches;
542552
unsigned long mTouchIdBase = 0;
543553
};
544554

0 commit comments

Comments
 (0)