summaryrefslogtreecommitdiffstats
path: root/xbmc/input/joysticks/RumbleGenerator.cpp
blob: db60cfa548083f3b0cf7a43f0681c8d62e18e1a1 (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
121
122
123
124
125
126
127
128
129
130
/*
 *  Copyright (C) 2016-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "RumbleGenerator.h"

#include "ServiceBroker.h"
#include "games/controllers/Controller.h"
#include "games/controllers/ControllerIDs.h"
#include "games/controllers/ControllerManager.h"
#include "input/joysticks/interfaces/IInputReceiver.h"

#include <algorithm>

using namespace std::chrono_literals;

namespace
{
constexpr auto RUMBLE_TEST_DURATION_MS = 1000ms; // Per motor
constexpr auto RUMBLE_NOTIFICATION_DURATION_MS = 300ms;

// From game.controller.default profile
#define WEAK_MOTOR_NAME "rightmotor"
} // namespace

using namespace KODI;
using namespace JOYSTICK;

CRumbleGenerator::CRumbleGenerator()
  : CThread("RumbleGenerator"), m_motors(GetMotors(ControllerID()))
{
}

std::string CRumbleGenerator::ControllerID() const
{
  return DEFAULT_CONTROLLER_ID;
}

void CRumbleGenerator::NotifyUser(IInputReceiver* receiver)
{
  if (receiver && !m_motors.empty())
  {
    if (IsRunning())
      StopThread(true);

    m_receiver = receiver;
    m_type = RUMBLE_NOTIFICATION;
    Create();
  }
}

bool CRumbleGenerator::DoTest(IInputReceiver* receiver)
{
  if (receiver && !m_motors.empty())
  {
    if (IsRunning())
      StopThread(true);

    m_receiver = receiver;
    m_type = RUMBLE_TEST;
    Create();

    return true;
  }
  return false;
}

void CRumbleGenerator::Process(void)
{
  switch (m_type)
  {
    case RUMBLE_NOTIFICATION:
    {
      std::vector<std::string> motors;

      if (std::find(m_motors.begin(), m_motors.end(), WEAK_MOTOR_NAME) != m_motors.end())
        motors.emplace_back(WEAK_MOTOR_NAME);
      else
        motors = m_motors; // Not using default profile? Just rumble all motors

      for (const std::string& motor : motors)
        m_receiver->SetRumbleState(motor, 1.0f);

      CThread::Sleep(RUMBLE_NOTIFICATION_DURATION_MS);

      if (m_bStop)
        break;

      for (const std::string& motor : motors)
        m_receiver->SetRumbleState(motor, 0.0f);

      break;
    }
    case RUMBLE_TEST:
    {
      for (const std::string& motor : m_motors)
      {
        m_receiver->SetRumbleState(motor, 1.0f);

        CThread::Sleep(RUMBLE_TEST_DURATION_MS);

        if (m_bStop)
          break;

        m_receiver->SetRumbleState(motor, 0.0f);
      }
      break;
    }
    default:
      break;
  }
}

std::vector<std::string> CRumbleGenerator::GetMotors(const std::string& controllerId)
{
  using namespace GAME;

  std::vector<std::string> motors;

  CControllerManager& controllerManager = CServiceBroker::GetGameControllerManager();
  ControllerPtr controller = controllerManager.GetController(controllerId);
  if (controller)
    controller->GetFeatures(motors, FEATURE_TYPE::MOTOR);

  return motors;
}