Debian Codegraph @

xbmc/input/joysticks/RumbleGenerator.cpp

raw
1 /*
2 * Copyright (C) 2016-2024 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "RumbleGenerator.h"
10
11 #include "ServiceBroker.h"
12 #include "games/controllers/Controller.h"
13 #include "games/controllers/ControllerIDs.h"
14 #include "games/controllers/ControllerManager.h"
15 #include "input/joysticks/interfaces/IInputReceiver.h"
16
17 #include <algorithm>
18
19 using namespace std::chrono_literals;
20
21 namespace
22 {
23 constexpr auto RUMBLE_TEST_DURATION_MS = 1000ms; // Per motor
24 constexpr auto RUMBLE_NOTIFICATION_DURATION_MS = 300ms;
25
26 // From game.controller.default profile
27 #define WEAK_MOTOR_NAME "rightmotor"
28 } // namespace
29
30 using namespace KODI;
31 using namespace JOYSTICK;
32
33 CRumbleGenerator::CRumbleGenerator()
34 : CThread("RumbleGenerator"), m_motors(GetMotors(ControllerID()))
35 {
36 }
37
38 std::string CRumbleGenerator::ControllerID() const
39 {
40 return DEFAULT_CONTROLLER_ID;
41 }
42
43 void CRumbleGenerator::NotifyUser(IInputReceiver* receiver)
44 {
45 if (receiver && !m_motors.empty())
46 {
47 if (IsRunning())
48 StopThread(true);
49
50 m_receiver = receiver;
51 m_type = RUMBLE_NOTIFICATION;
52 Create();
53 }
54 }
55
56 bool CRumbleGenerator::DoTest(IInputReceiver* receiver)
57 {
58 if (receiver && !m_motors.empty())
59 {
60 if (IsRunning())
61 StopThread(true);
62
63 m_receiver = receiver;
64 m_type = RUMBLE_TEST;
65 Create();
66
67 return true;
68 }
69 return false;
70 }
71
72 void CRumbleGenerator::Process(void)
73 {
74 switch (m_type)
75 {
76 case RUMBLE_NOTIFICATION:
77 {
78 std::vector<std::string> motors;
79
80 if (std::find(m_motors.begin(), m_motors.end(), WEAK_MOTOR_NAME) != m_motors.end())
81 motors.emplace_back(WEAK_MOTOR_NAME);
82 else
83 motors = m_motors; // Not using default profile? Just rumble all motors
84
85 for (const std::string& motor : motors)
86 m_receiver->SetRumbleState(motor, 1.0f);
87
88 CThread::Sleep(RUMBLE_NOTIFICATION_DURATION_MS);
89
90 if (m_bStop)
91 break;
92
93 for (const std::string& motor : motors)
94 m_receiver->SetRumbleState(motor, 0.0f);
95
96 break;
97 }
98 case RUMBLE_TEST:
99 {
100 for (const std::string& motor : m_motors)
101 {
102 m_receiver->SetRumbleState(motor, 1.0f);
103
104 CThread::Sleep(RUMBLE_TEST_DURATION_MS);
105
106 if (m_bStop)
107 break;
108
109 m_receiver->SetRumbleState(motor, 0.0f);
110 }
111 break;
112 }
113 default:
114 break;
115 }
116 }
117
118 std::vector<std::string> CRumbleGenerator::GetMotors(const std::string& controllerId)
119 {
120 using namespace GAME;
121
122 std::vector<std::string> motors;
123
124 CControllerManager& controllerManager = CServiceBroker::GetGameControllerManager();
125 ControllerPtr controller = controllerManager.GetController(controllerId);
126 if (controller)
127 controller->GetFeatures(motors, FEATURE_TYPE::MOTOR);
128
129 return motors;
130 }