-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteamControllerDaemon.cs
More file actions
266 lines (230 loc) · 9.88 KB
/
SteamControllerDaemon.cs
File metadata and controls
266 lines (230 loc) · 9.88 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Steamworks;
namespace com.github.lhervier.ksp
{
// <summary>
// Daemon in charge of listening to steam controllers connection/disconnection
// It also allow to change the current action set of the controller
// </summary>
[KSPAddon(KSPAddon.Startup.PSystemSpawn, true)]
public class SteamControllerDaemon : MonoBehaviour
{
// ==========================================================================================
// Static properties
// ==========================================================================================
// <summary>
// Instance of the daemon
// </summary>
public static SteamControllerDaemon Instance { get; private set; }
// <summary>
// Logger object
// </summary>
private static SteamControllerLogger LOGGER = new SteamControllerLogger("ConnectionDaemon");
// ===============================================
// <summary>
// Called when a new controller is connected
// </summary>
public EventVoid OnControllerConnected { get; private set; }
// <summary>
// Called when a controller is disconnected
// </summary>
public EventVoid OnControllerDisconnected {get; private set; }
// <summary>
// Is a controller connected ?
// </summary>
public bool ControllerConnected { get; private set; }
// ==============================================
// <summary>
// Handle to the first connected controller. No sense if ControllerConnected = false
// </summary>
private ControllerHandle_t controllerHandle;
// <summary>
// The action sets handles defined in the steam controller configuration template
// </summary>
private IDictionary<KSPActionSets, ControllerActionSetHandle_t> actionsSetsHandles = new Dictionary<KSPActionSets, ControllerActionSetHandle_t>();
// <summary>
// Handles to the connected steam controllers.
// Don't use. This array is here to prevent from instanciating a new one every cycle.
// </summary>
private ControllerHandle_t[] _controllerHandles = new ControllerHandle_t[Constants.STEAM_CONTROLLER_MAX_COUNT];
// =======================================================================
// <summary>
// Coroutine to check for a controller
// </summary>
private IEnumerator checkForControllerCoroutine;
// =======================================================================
// Unity Lifecycle
// =======================================================================
// <summary>
// Component awaked
// </summary>
public void Awake()
{
DontDestroyOnLoad(this);
this.OnControllerConnected = new EventVoid("controller.OnConnected");
this.OnControllerDisconnected = new EventVoid("controller.OnDisconnected");
this.ControllerConnected = false;
Instance = this;
LOGGER.Log("Awaked");
}
// <summary>
// Component destroyed
// </summary>
public void OnDestroy()
{
Instance = null;
this.StopCoroutine(this.checkForControllerCoroutine);
LOGGER.Log("Destroyed");
}
// <summary>
// Startup of the component
// </summary>
public void Start()
{
if( !SteamManager.Initialized )
{
LOGGER.Log("Steam not detected. Unable to start the daemon.");
return;
}
SteamController.Init();
this.checkForControllerCoroutine = this.CheckForController();
this.StartCoroutine(this.checkForControllerCoroutine);
LOGGER.Log("Started");
}
// ==============================================================================
// Detection of connection/disconnection of controllers
// ==============================================================================
// <summary>
// Main loop to detect controller connection/disconnection
// </summary>
private IEnumerator CheckForController()
{
WaitForSeconds waitFor1Second = new WaitForSeconds(1);
while( true )
{
SteamController.RunFrame();
// Detect connection/disconnection
int nbControllers = SteamController.GetConnectedControllers(this._controllerHandles);
bool newController = false;
bool disconnectedController = false;
if( nbControllers == 0 )
{
if( this.ControllerConnected )
{
newController = false;
disconnectedController = true;
}
else
{
newController = false;
disconnectedController = false;
}
}
else
{
if( this.ControllerConnected )
{
if( this.controllerHandle == this._controllerHandles[0] )
{
newController = false;
disconnectedController = false;
}
else
{
newController = true;
disconnectedController = true;
}
}
else
{
newController = true;
disconnectedController = false;
}
}
// Disconnect the current controller
if( disconnectedController )
{
LOGGER.Log("Steam Controller disconnected");
this.ControllerConnected = false;
this.UnloadActionSets();
this.OnControllerDisconnected.Fire();
}
// Connects a new controller
if( newController )
{
LOGGER.Log("Steam Controller connected");
this.controllerHandle = this._controllerHandles[0];
this.ControllerConnected = true;
this.LoadActionSets();
this.StartCoroutine(this.SayHello());
this.OnControllerConnected.Fire();
}
// Wait for 1 second
yield return waitFor1Second;
}
}
// <summary>
// Load action sets handles. The API don' ask for a handle on a controller.
// It seems to load the action sets of the first controller.
// </summary>
private void LoadActionSets()
{
LOGGER.Log("Loading Action Set Handles");
foreach(KSPActionSets actionSet in Enum.GetValues(typeof(KSPActionSets)))
{
string actionSetName = actionSet.GetId();
LOGGER.Log("- Getting action set handle for " + actionSetName);
// Action Sets list should depend on the used controller. But that's not what the API is waiting for...
ControllerActionSetHandle_t actionSetHandle = SteamController.GetActionSetHandle(actionSetName);
if( actionSetHandle.m_ControllerActionSetHandle == 0L )
{
LOGGER.Log("ERROR : Action set handle for " + actionSetName + " not found. I will use the default action set instead");
}
this.actionsSetsHandles[actionSet] = actionSetHandle;
}
}
// <summary>
// Unloads the action sets
// </summary>
private void UnloadActionSets()
{
this.actionsSetsHandles.Clear();
}
// <summary>
// Trigger a set of pulses on the current controller to say hello
// </summary>
private IEnumerator SayHello()
{
if( this.ControllerConnected )
{
LOGGER.Log("Hello new Controller !!");
for( int i = 0; i < 4; i++ )
{
SteamController.TriggerHapticPulse(this.controllerHandle, Steamworks.ESteamControllerPad.k_ESteamControllerPad_Right, ushort.MaxValue);
yield return new WaitForSeconds(0.1f);
SteamController.TriggerHapticPulse(this.controllerHandle, Steamworks.ESteamControllerPad.k_ESteamControllerPad_Left, ushort.MaxValue);
yield return new WaitForSeconds(0.1f);
}
}
}
// =========================================================================================
// <param name="actionSet">The action set to set</param>
// <summary>
// Change the current action set
// </summary>
public void setActionSet(KSPActionSets actionSet)
{
if( !this.ControllerConnected )
{
return;
}
SteamController.ActivateActionSet(
this.controllerHandle,
this.actionsSetsHandles[actionSet]
);
}
}
}