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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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#if UNITY_EDITOR
using System;
using System.Runtime.InteropServices;
using UnityEditor;
using UnityEngine.InputSystem.LowLevel;

namespace UnityEngine.InputSystem
{
    /// <summary>
    /// Adds support for processing input-related messages sent from the <c>Unite Remote</c> app.
    /// </summary>
    /// <remarks>
    /// A hook in the Unity runtime allows us to observe messages received from the remote (see Modules/GenericRemoteEditor).
    /// We get the binary blob of each message and a shot at processing the message instead of
    /// the native code doing it.
    /// </remarks>
    internal static class UnityRemoteSupport
    {
        public static bool isConnected => s_State.connected;

        public static void Initialize()
        {
            InputRuntime.s_Instance.onUnityRemoteMessage = ProcessMessageFromUnityRemote;

            InputSystem.onSettingsChange += () =>
            {
                if (InputSystem.settings.IsFeatureEnabled(InputFeatureNames.kDisableUnityRemoteSupport))
                {
                    InputRuntime.s_Instance.onUnityRemoteMessage = null;
                    if (s_State.connected)
                        Disconnect();
                }
                else
                    InputRuntime.s_Instance.onUnityRemoteMessage = ProcessMessageFromUnityRemote;
            };
        }

        private static unsafe bool ProcessMessageFromUnityRemote(IntPtr messageData)
        {
            var messageHeader = (MessageHeader*)messageData;

            switch (messageHeader->type)
            {
                case (byte)MessageType.Hello:
                    if (s_State.connected)
                        break;

                    // Install handlers.
                    s_State.deviceChangeHandler = OnDeviceChange;
                    s_State.deviceCommandHandler = OnDeviceCommand; ////REVIEW: We really should have a way of installing a handler just for a specific device.
                    InputSystem.onDeviceChange += s_State.deviceChangeHandler;
                    InputSystem.onDeviceCommand += s_State.deviceCommandHandler;

                    // Add devices.
                    s_State.touchscreen = InputSystem.AddDevice<Touchscreen>();
                    s_State.touchscreen.m_DeviceFlags |= InputDevice.DeviceFlags.Remote;
                    s_State.accelerometer = InputSystem.AddDevice<Accelerometer>();
                    s_State.accelerometer.m_DeviceFlags |= InputDevice.DeviceFlags.Remote;
                    // Gryo etc. added only when we receive GyroSettingsMessage.

                    s_State.connected = true;
                    Debug.Log("Unity Remote connected to input!");
                    break;

                case (byte)MessageType.Goodbye:
                    if (!s_State.connected)
                        break;
                    Disconnect();
                    Debug.Log("Unity Remote disconnected from input!");
                    break;

                case (byte)MessageType.Options:
                    var optionsMessage = (OptionsMessage*)messageData;
                    s_State.screenSize = DetermineScreenSize(optionsMessage->dimension1, optionsMessage->dimension2);
                    break;

                case (byte)MessageType.TouchInput:
                    if (s_State.touchscreen == null)
                        break;
                    // Android Remote seems to not be sending the last two fields (azimuthAngle and attitudeAngle).
                    if (messageHeader->length < 56)
                        break;
                    var touchMessage = (TouchInputMessage*)messageData;
                    var phase = TouchPhase.None;
                    switch (touchMessage->phase)
                    {
                        case (int)UnityEngine.TouchPhase.Began: phase = TouchPhase.Began; break;
                        case (int)UnityEngine.TouchPhase.Canceled: phase = TouchPhase.Canceled; break;
                        case (int)UnityEngine.TouchPhase.Ended: phase = TouchPhase.Ended; break;
                        case (int)UnityEngine.TouchPhase.Moved: phase = TouchPhase.Moved; break;
                            // Ignore stationary.
                    }
                    if (phase == default)
                        break;
                    InputSystem.QueueStateEvent(s_State.touchscreen, new TouchState
                    {
                        touchId = touchMessage->id + 1,
                        phase = phase,
                        position = MapRemoteTouchCoordinatesToLocal(new Vector2(touchMessage->positionX, touchMessage->positionY)),
                        radius = new Vector2(touchMessage->radius, touchMessage->radius),
                        pressure = touchMessage->pressure
                    });
                    break;

                case (byte)MessageType.GyroSettings:
                    var gyroSettingsMessage = (GyroSettingsMessage*)messageData;
                    if (!s_State.gyroInitialized)
                    {
                        // Message itself indicates presence of a gyro. Add the devices.
                        s_State.gyroscope = InputSystem.AddDevice<Gyroscope>();
                        s_State.attitude = InputSystem.AddDevice<AttitudeSensor>();
                        s_State.gravity = InputSystem.AddDevice<GravitySensor>();
                        s_State.linearAcceleration = InputSystem.AddDevice<LinearAccelerationSensor>();
                        s_State.gyroscope.m_DeviceFlags |= InputDevice.DeviceFlags.Remote;
                        s_State.attitude.m_DeviceFlags |= InputDevice.DeviceFlags.Remote;
                        s_State.gravity.m_DeviceFlags |= InputDevice.DeviceFlags.Remote;
                        s_State.linearAcceleration.m_DeviceFlags |= InputDevice.DeviceFlags.Remote;

                        s_State.gyroInitialized = true;
                    }
                    // Disable them if they are not currently enabled.
                    if (gyroSettingsMessage->enabled == 0)
                    {
                        InputSystem.DisableDevice(s_State.gyroscope);
                        InputSystem.DisableDevice(s_State.attitude);
                        InputSystem.DisableDevice(s_State.gravity);
                        InputSystem.DisableDevice(s_State.linearAcceleration);
                    }
                    else
                    {
                        s_State.gyroEnabled = true;
                    }
                    s_State.gyroUpdateInterval = gyroSettingsMessage->receivedGyroUpdateInternal;
                    break;

                case (byte)MessageType.GyroInput:
                    var gyroInputMessage = (GyroInputMessage*)messageData;
                    if (s_State.attitude != null && s_State.attitude.enabled)
                    {
                        InputSystem.QueueStateEvent(s_State.attitude, new AttitudeState
                        {
                            attitude = new Quaternion(gyroInputMessage->attitudeX, gyroInputMessage->attitudeY, gyroInputMessage->attitudeZ,
                                gyroInputMessage->attitudeW)
                        });
                    }
                    if (s_State.gyroscope != null && s_State.gyroscope.enabled)
                    {
                        InputSystem.QueueStateEvent(s_State.gyroscope, new GyroscopeState
                        {
                            angularVelocity = new Vector3(gyroInputMessage->rotationRateX, gyroInputMessage->rotationRateY,
                                gyroInputMessage->rotationRateZ)
                        });
                    }
                    if (s_State.gravity != null && s_State.gravity.enabled)
                    {
                        InputSystem.QueueStateEvent(s_State.gravity, new GravityState
                        {
                            gravity = new Vector3(gyroInputMessage->gravityX, gyroInputMessage->gravityY,
                                gyroInputMessage->gravityZ)
                        });
                    }
                    if (s_State.linearAcceleration != null && s_State.linearAcceleration.enabled)
                    {
                        InputSystem.QueueStateEvent(s_State.linearAcceleration, new LinearAccelerationState
                        {
                            acceleration = new Vector3(gyroInputMessage->userAccelerationX, gyroInputMessage->userAccelerationY,
                                gyroInputMessage->userAccelerationZ)
                        });
                    }
                    break;

                case (byte)MessageType.AccelerometerInput:
                    if (s_State.accelerometer == null)
                        break;
                    var accelerometerMessage = (AccelerometerInputMessage*)messageData;
                    InputSystem.QueueStateEvent(s_State.accelerometer, new AccelerometerState
                    {
                        acceleration = new Vector3(accelerometerMessage->accelerationX, accelerometerMessage->accelerationY,
                            accelerometerMessage->accelerationZ)
                    });
                    break;
            }

            return false;
        }

        private static void Disconnect()
        {
            InputSystem.RemoveDevice(s_State.touchscreen);
            InputSystem.RemoveDevice(s_State.accelerometer);
            if (s_State.gyroscope != null)
                InputSystem.RemoveDevice(s_State.gyroscope);
            if (s_State.attitude != null)
                InputSystem.RemoveDevice(s_State.attitude);
            if (s_State.gravity != null)
                InputSystem.RemoveDevice(s_State.gravity);
            if (s_State.linearAcceleration != null)
                InputSystem.RemoveDevice(s_State.linearAcceleration);

            ResetGlobalState();
        }

        private static void OnDeviceChange(InputDevice device, InputDeviceChange change)
        {
            switch (change)
            {
                case InputDeviceChange.Removed:
                    // Deal with someone manually removing one of our devices.
                    if (device == s_State.accelerometer)
                        s_State.accelerometer = null;
                    else if (device == s_State.attitude)
                        s_State.accelerometer = null;
                    else if (device == s_State.gravity)
                        s_State.gravity = null;
                    else if (device == s_State.gyroscope)
                        s_State.gyroscope = null;
                    else if (device == s_State.touchscreen)
                        s_State.touchscreen = null;
                    else if (device == s_State.linearAcceleration)
                        s_State.linearAcceleration = null;
                    break;

                case InputDeviceChange.Enabled:
                case InputDeviceChange.Disabled:
                    // If it's any of our devices that make up the remote gyro,
                    // send a message to the remote.
                    if (device == s_State.attitude || device == s_State.gravity || device == s_State.gyroscope ||
                        device == s_State.linearAcceleration)
                    {
                        SyncGyroEnabledInRemote();
                    }
                    break;
            }
        }

        private static unsafe long? OnDeviceCommand(InputDevice device, InputDeviceCommand* command)
        {
            if (device != s_State.attitude && device != s_State.gyroscope && device != s_State.gravity &&
                device != s_State.linearAcceleration)
                return null;

            if (command->type == SetSamplingFrequencyCommand.Type)
            {
                s_State.gyroUpdateInterval = ((SetSamplingFrequencyCommand*)command)->frequency;
                InputRuntime.s_Instance.SetUnityRemoteGyroUpdateInterval(s_State.gyroUpdateInterval);
                return InputDeviceCommand.GenericSuccess;
            }

            if (command->type == QuerySamplingFrequencyCommand.Type)
            {
                ((QuerySamplingFrequencyCommand*)command)->frequency = s_State.gyroUpdateInterval;
                return InputDeviceCommand.GenericSuccess;
            }

            return InputDeviceCommand.GenericFailure;
        }

        private static void SyncGyroEnabledInRemote()
        {
            var enabled = (s_State.attitude?.enabled ?? false) || (s_State.gravity?.enabled ?? false) ||
                (s_State.gyroscope?.enabled ?? false) || (s_State.linearAcceleration?.enabled ?? false);
            if (enabled != s_State.gyroEnabled)
            {
                s_State.gyroEnabled = enabled;
                InputRuntime.s_Instance.SetUnityRemoteGyroEnabled(enabled);
            }
        }

        // This is taken from HandleOptionsMessage() in GenericRemote.cpp.
        private static Vector2 DetermineScreenSize(int dimension1, int dimension2)
        {
            const float kMaxPixels = 640 * 480;  // limit the resolution to VGA
            float screenPixels = dimension1 * dimension2;
            var divider = (int)Mathf.Ceil(Mathf.Sqrt(screenPixels / kMaxPixels));

            if (divider == 0)
                return default;

            var hdim1 = dimension1 / divider;
            var hdim2 = dimension2 / divider;

            // GetConfigValue is private. Reflect around it.
            var getConfigValueMethod = typeof(EditorSettings).GetMethod("GetConfigValue");
            if (getConfigValueMethod != null && "Normal".Equals(getConfigValueMethod.Invoke(null, new[] { "UnityRemoteResolution" })))
            {
                hdim1 = dimension1;
                hdim2 = dimension2;
            }

            if (hdim1 >= 1 && hdim2 >= 1)
                return new Vector2(dimension1, dimension2);

            return default;
        }

        private static Vector2 MapRemoteTouchCoordinatesToLocal(Vector2 position)
        {
            var screenSizeRemote = s_State.screenSize;
            var screenSizeLocal = InputRuntime.s_Instance.screenSize;

            return new Vector2(
                position.x / screenSizeRemote.x * screenSizeLocal.x,
                position.y = position.y / screenSizeRemote.y * screenSizeLocal.y);
        }

        // See Editor/Src/RemoteInput/GenericRemote.cpp

        internal enum MessageType : byte
        {
            Invalid = 0,

            Hello = 1,
            Options = 2,
            GyroSettings = 3,
            DeviceOrientation = 4,
            DeviceFeatures = 5,

            TouchInput = 10,
            AccelerometerInput = 11,
            TrackBallInput = 12,
            Key = 13,
            GyroInput = 14,
            MousePresence = 15,
            JoystickInput = 16,
            JoystickNames = 17,

            WebCamDeviceList = 20,
            WebCamStream = 21,

            LocationServiceData = 30,
            CompassData = 31,

            Goodbye = 32,

            Reserved = 255,
        }

        internal interface IUnityRemoteMessage
        {
            byte staticType { get; }
        }

        [StructLayout(LayoutKind.Explicit, Size = 5)]
        internal struct MessageHeader
        {
            // Unfortunately, the header has an odd 5 byte length and everything
            // coming after it is misaligned. Reason is that native reads is as a stream
            // and wants to pack tightly.
            [FieldOffset(0)] public byte type;
            [FieldOffset(1)] public int length;
        }

        [StructLayout(LayoutKind.Explicit)]
        internal unsafe struct HelloMessage : IUnityRemoteMessage
        {
            [FieldOffset(0)] public MessageHeader header;
            [FieldOffset(5)] public uint protocolIdLength;
            [FieldOffset(9)] public fixed char protocolId[11];
            [FieldOffset(20)] public int protocolVersion;

            public byte staticType => (byte)MessageType.Hello;

            public static HelloMessage Create()
            {
                var msg = default(HelloMessage);
                msg.protocolIdLength = 11;
                msg.protocolId[0] = 'U';
                msg.protocolId[1] = 'n';
                msg.protocolId[2] = 'i';
                msg.protocolId[3] = 't';
                msg.protocolId[4] = 'y';
                msg.protocolId[5] = 'R';
                msg.protocolId[6] = 'e';
                msg.protocolId[7] = 'm';
                msg.protocolId[8] = 'o';
                msg.protocolId[9] = 't';
                msg.protocolId[10] = 'e';
                msg.protocolVersion = 0;
                return msg;
            }
        }

        [StructLayout(LayoutKind.Explicit)]
        internal struct OptionsMessage : IUnityRemoteMessage
        {
            [FieldOffset(0)] public MessageHeader header;
            [FieldOffset(5)] public int dimension1;
            [FieldOffset(9)] public int dimension2;

            public byte staticType => (byte)MessageType.Options;
        }

        [StructLayout(LayoutKind.Explicit)]
        internal struct GoodbyeMessage : IUnityRemoteMessage
        {
            [FieldOffset(0)] public MessageHeader header;

            public byte staticType => (byte)MessageType.Goodbye;
        }

        [StructLayout(LayoutKind.Explicit)]
        internal struct TouchInputMessage : IUnityRemoteMessage
        {
            [FieldOffset(0)] public MessageHeader header;
            [FieldOffset(5)] public float positionX;
            [FieldOffset(9)] public float positionY;
            [FieldOffset(13)] public ulong frame;
            [FieldOffset(21)] public int id;
            [FieldOffset(25)] public int phase;
            [FieldOffset(29)] public int tapCount;
            [FieldOffset(33)] public float radius;
            [FieldOffset(37)] public float radiusVariance;
            [FieldOffset(41)] public int type;
            [FieldOffset(45)] public float pressure;
            [FieldOffset(49)] public float maximumPossiblePressure;
            [FieldOffset(53)] public float azimuthAngle;
            [FieldOffset(57)] public float altitudeAngle;

            public byte staticType => (byte)MessageType.TouchInput;
        }

        [StructLayout(LayoutKind.Explicit)]
        internal struct GyroSettingsMessage : IUnityRemoteMessage
        {
            [FieldOffset(0)] public MessageHeader header;
            [FieldOffset(5)] public int enabled;
            [FieldOffset(9)] public float receivedGyroUpdateInternal;

            public byte staticType => (byte)MessageType.GyroSettings;
        }

        [StructLayout(LayoutKind.Explicit)]
        internal struct GyroInputMessage : IUnityRemoteMessage
        {
            [FieldOffset(0)] public MessageHeader header;
            [FieldOffset(5)] public float rotationRateX;
            [FieldOffset(9)] public float rotationRateY;
            [FieldOffset(13)] public float rotationRateZ;
            [FieldOffset(17)] public float rotationRateUnbiasedX;
            [FieldOffset(21)] public float rotationRateUnbiasedY;
            [FieldOffset(25)] public float rotationRateUnbiasedZ;
            [FieldOffset(29)] public float gravityX;
            [FieldOffset(33)] public float gravityY;
            [FieldOffset(37)] public float gravityZ;
            [FieldOffset(41)] public float userAccelerationX;
            [FieldOffset(45)] public float userAccelerationY;
            [FieldOffset(49)] public float userAccelerationZ;
            [FieldOffset(53)] public float attitudeX;
            [FieldOffset(57)] public float attitudeY;
            [FieldOffset(61)] public float attitudeZ;
            [FieldOffset(65)] public float attitudeW;

            public byte staticType => (byte)MessageType.GyroInput;
        }

        [StructLayout(LayoutKind.Explicit)]
        internal struct AccelerometerInputMessage : IUnityRemoteMessage
        {
            [FieldOffset(0)] public MessageHeader header;
            [FieldOffset(5)] public float accelerationX;
            [FieldOffset(9)] public float accelerationY;
            [FieldOffset(13)] public float accelerationZ;
            [FieldOffset(17)] public float deltaTime;

            public byte staticType => (byte)MessageType.AccelerometerInput;
        }

        private struct State
        {
            public bool connected;
            public bool gyroInitialized;
            public bool gyroEnabled;
            public float gyroUpdateInterval;
            public Vector2 screenSize;

            public Action<InputDevice, InputDeviceChange> deviceChangeHandler;
            public InputDeviceCommandDelegate deviceCommandHandler;

            // Devices that we create for receiving input from the remote.
            public Touchscreen touchscreen;
            public Accelerometer accelerometer;
            public Gyroscope gyroscope;
            public AttitudeSensor attitude;
            public GravitySensor gravity;
            public LinearAccelerationSensor linearAcceleration;
        }

        private static State s_State;

        ////TODO: hook this into Hakan's new cleanup mechanism
        internal static void ResetGlobalState()
        {
            if (s_State.deviceChangeHandler != null)
                InputSystem.onDeviceChange -= s_State.deviceChangeHandler;
            if (s_State.deviceCommandHandler != null)
                InputSystem.onDeviceCommand -= s_State.deviceCommandHandler;
            s_State = default;
        }
    }
}
#endif