summaryrefslogtreecommitdiffstats
path: root/dom/gamepad/ipc/GamepadTestChannelParent.cpp
blob: ca6ab7cf001272e84770695b4495003034f0682f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#include "GamepadTestChannelParent.h"

#include "mozilla/dom/GamepadPlatformService.h"
#include "mozilla/ipc/BackgroundParent.h"
#include "mozilla/Unused.h"

namespace mozilla::dom {

already_AddRefed<GamepadTestChannelParent> GamepadTestChannelParent::Create() {
  return RefPtr<GamepadTestChannelParent>(new GamepadTestChannelParent())
      .forget();
}

GamepadTestChannelParent::GamepadTestChannelParent() {
  ::mozilla::ipc::AssertIsOnBackgroundThread();
  GamepadMonitoringState::GetSingleton().AddObserver(this);
}

GamepadTestChannelParent::~GamepadTestChannelParent() {
  ::mozilla::ipc::AssertIsOnBackgroundThread();
  GamepadMonitoringState::GetSingleton().RemoveObserver(this);
}

void GamepadTestChannelParent::AddGamepadToPlatformService(
    uint32_t aPromiseId, const GamepadAdded& aGamepadAdded) {
  mozilla::ipc::AssertIsOnBackgroundThread();

  RefPtr<GamepadPlatformService> service =
      GamepadPlatformService::GetParentService();
  MOZ_ASSERT(service);

  const GamepadAdded& a = aGamepadAdded;
  nsCString gamepadID;
  LossyCopyUTF16toASCII(a.id(), gamepadID);
  GamepadHandle handle = service->AddGamepad(
      gamepadID.get(), static_cast<GamepadMappingType>(a.mapping()), a.hand(),
      a.num_buttons(), a.num_axes(), a.num_haptics(), a.num_lights(),
      a.num_touches());

  Unused << SendReplyGamepadHandle(aPromiseId, handle);
}

void GamepadTestChannelParent::OnMonitoringStateChanged(bool aNewState) {
  mozilla::ipc::AssertIsOnBackgroundThread();

  if (aNewState) {
    for (auto& deferredGamepadAdd : mDeferredGamepadAdded) {
      AddGamepadToPlatformService(deferredGamepadAdd.promiseId,
                                  deferredGamepadAdd.gamepadAdded);
    }
    mDeferredGamepadAdded.Clear();
  }
}

mozilla::ipc::IPCResult GamepadTestChannelParent::RecvGamepadTestEvent(
    const uint32_t& aID, const GamepadChangeEvent& aEvent) {
  mozilla::ipc::AssertIsOnBackgroundThread();

  RefPtr<GamepadPlatformService> service =
      GamepadPlatformService::GetParentService();
  MOZ_ASSERT(service);

  const GamepadChangeEventBody& body = aEvent.body();

  // GamepadAdded is special because it will be deferred if monitoring hasn't
  // started. Everything else won't be deferred and will fail if monitoring
  // isn't running
  if (body.type() == GamepadChangeEventBody::TGamepadAdded) {
    if (GamepadMonitoringState::GetSingleton().IsMonitoring()) {
      AddGamepadToPlatformService(aID, body.get_GamepadAdded());
    } else {
      mDeferredGamepadAdded.AppendElement(
          DeferredGamepadAdded{aID, body.get_GamepadAdded()});
    }
    return IPC_OK();
  }

  if (!GamepadMonitoringState::GetSingleton().IsMonitoring()) {
    return IPC_FAIL(this, "Simulated message received while not monitoring");
  }

  GamepadHandle handle = aEvent.handle();

  if (body.type() == GamepadChangeEventBody::TGamepadRemoved) {
    service->RemoveGamepad(handle);
    return IPC_OK();
  }
  if (body.type() == GamepadChangeEventBody::TGamepadButtonInformation) {
    const GamepadButtonInformation& a = body.get_GamepadButtonInformation();
    service->NewButtonEvent(handle, a.button(), a.pressed(), a.touched(),
                            a.value());
    return IPC_OK();
  }
  if (body.type() == GamepadChangeEventBody::TGamepadAxisInformation) {
    const GamepadAxisInformation& a = body.get_GamepadAxisInformation();
    service->NewAxisMoveEvent(handle, a.axis(), a.value());
    return IPC_OK();
  }
  if (body.type() == GamepadChangeEventBody::TGamepadPoseInformation) {
    const GamepadPoseInformation& a = body.get_GamepadPoseInformation();
    service->NewPoseEvent(handle, a.pose_state());
    return IPC_OK();
  }
  if (body.type() == GamepadChangeEventBody::TGamepadTouchInformation) {
    const GamepadTouchInformation& a = body.get_GamepadTouchInformation();
    service->NewMultiTouchEvent(handle, a.index(), a.touch_state());
    return IPC_OK();
  }

  NS_WARNING("Unknown event type.");
  return IPC_FAIL_NO_REASON(this);
}

}  // namespace mozilla::dom