AndroidNativeShimControllerProvider.cs
12.7 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//-----------------------------------------------------------------------
// <copyright file="AndroidNativeShimControllerProvider.cs" company="Google Inc.">
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-----------------------------------------------------------------------
// This provider is only available on an Android device.
#if UNITY_ANDROID && !UNITY_EDITOR
/// @cond
namespace Gvr.Internal
{
using UnityEngine;
using System;
using System.Runtime.InteropServices;
#if UNITY_2017_2_OR_NEWER
using UnityEngine.XR;
#else
using XRDevice = UnityEngine.VR.VRDevice;
#endif // UNITY_2017_2_OR_NEWER
/// Controller Provider that uses the native GVR C API to communicate with controllers
/// via Google VR Services on Android.
class AndroidNativeShimControllerProvider : IControllerProvider
{
private const int GVR_SHIM_UNITY_API_LEVEL = 1;
// Shim API return OK value. Result is error if != OK.
private const int GVR_SHIM_OK = 1;
private enum GvrShimUnitySupport
{
/// GVRShim is supported.
Supported = 0,
/// The GVRShim plugin is not present. This happens when GVRShim is not included in
/// the Unity project and the default shim compiled with Unity is used.
PluginNotPresent = 1,
/// The expected API level is not supported by GVRShim.
ApiLevelUnavailable = 2,
/// The device is not able to run GVRShim.
DeviceNotSupported = 3,
}
private enum GvrShimConnectionStatus
{
/// Controller is disconnected.
Disconnected = 0,
/// Controller is scanning.
Scanning = 1,
/// Controller is connecting.
Connecting = 2,
/// Controller is connected.
Connected = 3,
/// There was an error connecting to the controller.
/// This enum subsumes all of the API status errors which are largely obsolete now.
Error = 100,
}
private enum GvrShimTrackedDataAvailableFlags
{
PositionAvailable = 1 << 1,
RotationAvailable = 1 << 2,
GyroAvailable = 1 << 3,
AccelerationAvailable = 1 << 4,
}
private int lastUpdateFrame = 0;
private MutablePose3D pose3d = new MutablePose3D();
private GvrControllerButton[] lastButtonStates = new GvrControllerButton[2];
public static bool ShimAvailable()
{
int supportStatus = (int)GvrShimUnitySupport.ApiLevelUnavailable;
int retval = 0;
try
{
retval = GvrShimUnity_getGVRShimSupportStatus(GVR_SHIM_UNITY_API_LEVEL, ref supportStatus);
}
catch (DllNotFoundException)
{
Debug.LogError("GVR Unity shim not found.");
return false;
}
if (retval != GVR_SHIM_OK)
{
Debug.LogError("GvrShimUnity_getGVRShimSupportStatus returned error.");
return false;
}
if (supportStatus != (int)GvrShimUnitySupport.Supported)
{
Debug.LogError("GVR Unity Shim doesn't support shim API level " + GVR_SHIM_UNITY_API_LEVEL);
return false;
}
return true;
}
internal AndroidNativeShimControllerProvider()
{
int retval = GvrShimUnity_initShimWithContext(XRDevice.GetNativePtr());
if (retval != GVR_SHIM_OK)
{
Debug.LogError("GvrShimUnity_initShimWithContext returned error.");
}
// Start the shim session.
retval = GvrShimUnity_resumeShim();
if (retval != GVR_SHIM_OK)
{
Debug.LogError("GvrShimUnity_resumeShim returned error.");
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Debug.Log("Destroying GVR API structures.");
int retval = GvrShimUnity_destroyShim();
if (retval != GVR_SHIM_OK)
{
Debug.LogError("GvrShimUnity_destroyShim returned error.");
}
}
}
public bool SupportsBatteryStatus
{
get { return true; }
}
/// Reads the number of controllers the system is configured to use. This does not
/// indicate the number of currently connected controllers.
public int MaxControllerCount
{
get
{
int count = 0;
int retval = GvrShimUnity_getControllerCount(ref count);
if (retval != GVR_SHIM_OK)
{
Debug.LogError("GvrShimUnity_getControllerCount returned error.");
return 0;
}
return count;
}
}
/// Notifies the controller provider that the application has paused.
public void OnPause()
{
int retval = GvrShimUnity_pauseShim();
if (retval != GVR_SHIM_OK)
{
Debug.LogError("GvrShimUnity_pauseShim returned error.");
}
}
/// Notifies the controller provider that the application has resumed.
public void OnResume()
{
int retval = GvrShimUnity_resumeShim();
if (retval != GVR_SHIM_OK)
{
Debug.LogError("GvrShimUnity_resumeShim returned error.");
}
}
private static GvrConnectionState ConvertConnectionState(int connectionState)
{
switch (connectionState)
{
case (int)GvrShimConnectionStatus.Connected:
return GvrConnectionState.Connected;
case (int)GvrShimConnectionStatus.Connecting:
return GvrConnectionState.Connecting;
case (int)GvrShimConnectionStatus.Scanning:
return GvrConnectionState.Scanning;
case (int)GvrShimConnectionStatus.Disconnected:
return GvrConnectionState.Disconnected;
default:
return GvrConnectionState.Error;
}
}
/// Reads the controller's current state and stores it in outState.
public void ReadState(ControllerState outState, int controller_id)
{
int retval = 0;
if (lastUpdateFrame != Time.frameCount)
{
lastUpdateFrame = Time.frameCount;
retval = GvrShimUnity_updateState();
if (retval != GVR_SHIM_OK)
{
Debug.LogError("GvrShimUnity_updateState returned error.");
return;
}
}
int connStatus = 0;
retval = GvrShimUnity_getControllerConnectionStatus(controller_id, ref connStatus);
if (retval != GVR_SHIM_OK)
{
Debug.LogError("GvrShimUnity_getControllerConnectionStatus returned error.");
return;
}
outState.connectionState = ConvertConnectionState(connStatus);
// Early out if not connected. No other state is relevant.
if (outState.connectionState != GvrConnectionState.Connected)
{
return;
}
GvrShimUnityControllerState aState = new GvrShimUnityControllerState();
retval = GvrShimUnity_getControllerState(controller_id, ref aState);
if (retval != GVR_SHIM_OK)
{
Debug.LogError("GvrShimUnity_getControllerState returned error.");
return;
}
// Convert GVR API orientation (right-handed) into Unity axis system (left-handed).
pose3d.Set(aState.position, aState.orientation);
pose3d.SetRightHanded(pose3d.Matrix);
outState.orientation = pose3d.Orientation;
outState.position = pose3d.Position;
// For accelerometer, we have to flip Z because the GVR API has Z pointing backwards
// and Unity has Z pointing forward.
outState.accel = aState.acceleration;
outState.accel.z *= -1;
// Gyro in GVR represents a right-handed angular velocity about each axis (positive means
// clockwise when sighting along axis). Since Unity uses a left-handed system, we flip the
// signs to adjust the sign of the rotational velocity (so that positive means
// counter-clockwise). In addition, since in Unity the Z axis points forward while GVR
// has Z pointing backwards, we flip the Z axis sign again. So the result is that
// we should use -X, -Y, +Z:
outState.gyro = aState.gyro;
outState.gyro.Scale(new Vector3(-1, -1, 1));
outState.touchPos = aState.touchPos;
// Shim outputs centered touchpos coordinates, but the ControllerState struct is
// top-left coordinates. Convert back to top-left.
outState.touchPos.x = (outState.touchPos.x / 2.0f) + 0.5f;
outState.touchPos.y = (-outState.touchPos.y / 2.0f) + 0.5f;
outState.is6DoF = (
aState.trackedDataAvailable &
(int)GvrShimTrackedDataAvailableFlags.PositionAvailable) != 0;
outState.buttonsState = (GvrControllerButton)aState.buttonState;
// Derive button up/down state from Unity perspective, as it can miss
// up/downs from platform, like on pause/resume.
if (controller_id >= 0 && controller_id < lastButtonStates.Length)
{
outState.SetButtonsUpDownFromPrevious(lastButtonStates[controller_id]);
lastButtonStates[controller_id] = outState.buttonsState;
}
outState.recentered = (aState.recentered != 0);
outState.isCharging = (aState.batteryCharging != 0);
outState.batteryLevel = (GvrControllerBatteryLevel)aState.batteryLevel;
}
[StructLayout(LayoutKind.Sequential, Size = 348)]
private struct GvrShimUnityControllerState
{
public Vector3 position;
public Quaternion orientation;
public Vector3 gyro;
public Vector3 acceleration;
public Vector2 touchPos;
public int buttonState;
public int buttonUp;
public int buttonDown;
public int recentered;
public int batteryCharging;
public int batteryLevel;
public int connectionState;
public int trackedDataAvailable;
}
private const string shimDllName = GvrActivityHelper.GVR_SHIM_DLL_NAME;
[DllImport(shimDllName)]
private static extern int GvrShimUnity_getGVRVersion(ref float version);
[DllImport(shimDllName)]
private static extern int GvrShimUnity_getGVRShimSupportStatus(uint expected_api_level, ref int gvrshim_support_status);
[DllImport(shimDllName)]
private static extern int GvrShimUnity_initShimWithContext(IntPtr gvr_context);
[DllImport(shimDllName)]
private static extern int GvrShimUnity_destroyShim();
[DllImport(shimDllName)]
private static extern int GvrShimUnity_pauseShim();
[DllImport(shimDllName)]
private static extern int GvrShimUnity_resumeShim();
[DllImport(shimDllName)]
private static extern int GvrShimUnity_updateState();
[DllImport(shimDllName)]
private static extern int GvrShimUnity_getControllerConnectionStatus(int device, ref int status);
[DllImport(shimDllName)]
private static extern int GvrShimUnity_getControllerState(int device, ref GvrShimUnityControllerState state);
[DllImport(shimDllName)]
private static extern int GvrShimUnity_getControllerCount(ref int count);
}
}
/// @endcond
#endif // UNITY_ANDROID && !UNITY_EDITOR