GvrControllerInput.cs 35.4 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 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 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
//-----------------------------------------------------------------------
// <copyright file="GvrControllerInput.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>
//-----------------------------------------------------------------------

using System;
using System.Collections;
using Gvr.Internal;
using UnityEngine;

/// <summary>Represents a controller's current connection state.</summary>
/// <remarks>
/// All values and semantics below (except for Error) are from gvr_types.h in the GVR C API.
/// </remarks>
public enum GvrConnectionState
{
    /// <summary>Indicates that an error has occurred.</summary>
    Error = -1,

    /// <summary>Indicates a controller is disconnected.</summary>
    Disconnected = 0,

    /// <summary>Indicates that the device is scanning for controllers.</summary>
    Scanning = 1,

    /// <summary>Indicates that the device is connecting to a controller.</summary>
    Connecting = 2,

    /// <summary>Indicates that the device is connected to a controller.</summary>
    Connected = 3,
}

/// <summary>Represents the status of the controller API.</summary>
/// <remarks>Values and semantics are from `gvr_types.h` in the GVR C API.</remarks>
public enum GvrControllerApiStatus
{
    /// <summary>A Unity-localized error occurred.</summary>
    /// <remarks>This is the only value that isn't in `gvr_types.h`.</remarks>
    Error = -1,

    /// <summary>API is happy and healthy.</summary>
    /// <remarks>
    /// This doesn't mean any controllers are connected, it just means that the underlying service
    /// is working properly.
    /// </remarks>
    Ok = 0,

    // Any other status represents a permanent failure that requires
    // external action to fix:

    /// <summary>
    /// API failed because this device does not support controllers (API is too low, or other
    /// required feature not present).
    /// </summary>
    Unsupported = 1,

    /// <summary>
    /// This app was not authorized to use the service (e.g., missing permissions, the app is
    /// blacklisted by the underlying service, etc).
    /// </summary>
    NotAuthorized = 2,

    /// <summary>The underlying VR service is not present.</summary>
    Unavailable = 3,

    /// <summary>The underlying VR service is too old, needs upgrade.</summary>
    ApiServiceObsolete = 4,

    /// <summary>
    /// The underlying VR service is too new, is incompatible with current client.
    /// </summary>
    ApiClientObsolete = 5,

    /// <summary>The underlying VR service is malfunctioning. Try again later.</summary>
    ApiMalfunction = 6,
}

/// <summary>Represents a controller's current battery level.</summary>
/// <remarks>Values and semantics from gvr_types.h in the GVR C API.</remarks>
public enum GvrControllerBatteryLevel
{
    /// <summary>A Unity-localized error occurred.</summary>
    /// <remarks>This is the only value that isn't in `gvr_types.h`.</remarks>
    Error = -1,

    /// <summary>The battery state is currently unreported.</summary>
    Unknown = 0,

    /// <summary>Equivalent to 1 out of 5 bars on the battery indicator.</summary>
    CriticalLow = 1,

    /// <summary>Equivalent to 2 out of 5 bars on the battery indicator.</summary>
    Low = 2,

    /// <summary>Equivalent to 3 out of 5 bars on the battery indicator.</summary>
    Medium = 3,

    /// <summary>Equivalent to 4 out of 5 bars on the battery indicator.</summary>
    AlmostFull = 4,

    /// <summary>Equivalent to 5 out of 5 bars on the battery indicator.</summary>
    Full = 5,
}

/// <summary>Represents controller buttons.</summary>
/// <remarks>
/// Values 0-9 are from `gvr_types.h` in the GVR C API.  Value 31 is not represented in the C API.
/// </remarks>
public enum GvrControllerButton
{
    /// <summary>The Button under the touch pad.</summary>
    /// <remarks>Formerly known as Click.</remarks>
    TouchPadButton = 1 << 1,

    /// <summary>Touch pad touching indicator.</summary>
    TouchPadTouch = 1 << 31,

    /// <summary>General application button.</summary>
    App = 1 << 3,

    /// <summary>System button.</summary>
    /// <remarks>Formerly known as Home.</remarks>
    System = 1 << 2,

    /// <summary>Primary button on the underside of the controller.</summary>
    Trigger = 1 << 6,

    /// <summary>Secondary button on the underside of the controller.</summary>
    Grip = 1 << 7,

    /// <summary>Buttons reserved for future use. Subject to name change.</summary>
    Reserved2 = 1 << 8,
}

/// <summary>Represents controller handedness.</summary>
public enum GvrControllerHand
{
    /// <summary>Right hand.</summary>
    Right,

    /// <summary>Left hand.</summary>
    Left,

    /// <summary>Alias for dominant hand as specified by `GvrSettings.Handedness`.</summary>
    Dominant,

    /// <summary>Alias for non-dominant hand.</summary>
    NonDominant,
}

/// <summary>Main entry point for the Daydream controller API.</summary>
/// <remarks>
/// To use this API, add this script to a game object in your scene, or use the
/// **GvrControllerMain** prefab.  This is a singleton object. There can only be one object with
/// this script in your scene.
/// <para>
/// To access a controller's state, get a device from `GvrControllerInput.GetDevice` then query it
/// for state. For example, to the dominant controller's current orientation, use
/// `GvrControllerInput.GetDevice(GvrControllerHand.Dominant).Orientation`.
/// </para></remarks>
[HelpURL("https://developers.google.com/vr/reference/unity/class/GvrControllerInput")]
public class GvrControllerInput : MonoBehaviour
{
    /// <summary>Indicates how to connect to the controller emulator.</summary>
#if UNITY_EDITOR
    [GvrInfo("Hold Shift to use the Mouse as the dominant controller.\n\n" +
             "Controls:  Shift +\n" +
             "   • Move Mouse = Change Orientation\n" +
             "   • Left Mouse Button = ClickButton\n" +
             "   • Right Mouse Button = AppButton\n" +
             "   • Middle Mouse Button = HomeButton/Recenter\n" +
             "   • Ctrl = IsTouching\n" +
             "   • Ctrl + Move Mouse = Change TouchPos", 8, UnityEditor.MessageType.None)]
    [Tooltip("How to connect to the emulator: USB cable (recommended) or WIFI.")]
    [GvrInfo("Controller Emulator is now Deprecated", 2, UnityEditor.MessageType.Warning)]
#endif  // UNITY_EDITOR
    public EmulatorConnectionMode emulatorConnectionMode = EmulatorConnectionMode.USB;

    private static GvrControllerInputDevice[] instances = new GvrControllerInputDevice[0];
    private static IControllerProvider controllerProvider;
    private static GvrSettings.UserPrefsHandedness handedness;
    private static Action onDevicesChangedInternal;

    /// <summary>Event handler for when the connection state of a controller changes.</summary>
    /// <param name="state">The new state.</param>
    /// <param name="oldState">The previous state.</param>
    public delegate void OnStateChangedEvent(GvrConnectionState state, GvrConnectionState oldState);

    /// <summary>
    /// Event handler for receiving button, touchpad, and IMU updates from the controllers.
    /// </summary>
    /// <remarks>Use this handler to update app state based on controller input.</remarks>
    public static event Action OnControllerInputUpdated;

    /// <summary>
    /// Event handler for receiving a second notification callback, after all
    /// `OnControllerInputUpdated` events have fired.
    /// </summary>
    public static event Action OnPostControllerInputUpdated;

    /// <summary>Event handler for when controller devices have changed.</summary>
    /// <remarks>
    /// Any code that stores a `GvrControllerInputDevice` should get a new device instance from
    /// `GetDevice`. Existing `GvrControllerInputDevice`s will be marked invalid and will log errors
    /// when used. Event handlers are called immediately when added.
    /// </remarks>
    public static event Action OnDevicesChanged
    {
        [SuppressMemoryAllocationError(
            IsWarning = false, Reason = "Only called on input device change.")]
        add
        {
            onDevicesChangedInternal += value;
            value();
        }

        [SuppressMemoryAllocationError(
            IsWarning = false, Reason = "Only called on input device change.")]
        remove
        {
            onDevicesChangedInternal -= value;
        }
    }

    /// <summary>
    /// Event handler for when the connection state of the dominant controller changes.
    /// </summary>
    [System.Obsolete("Replaced by GvrControllerInputDevice.OnStateChangedEvent.")]
    public static event OnStateChangedEvent OnStateChanged
    {
        add
        {
            if (instances.Length > 0)
            {
                instances[0].OnStateChanged += value;
            }
            else
            {
                Debug.LogError(
                    "GvrControllerInput: Adding OnStateChanged event before instance created.");
            }
        }

        remove
        {
            if (instances.Length > 0)
            {
                instances[0].OnStateChanged -= value;
            }
            else
            {
                Debug.LogError(
                    "GvrControllerInput: Removing OnStateChanged event before instance created.");
            }
        }
    }

    /// <summary>Controller Emulatuor connection modes.</summary>
    public enum EmulatorConnectionMode
    {
        /// <summary>Emulator disconnected.</summary>
        OFF,

        /// <summary>Emulator connects over USB.</summary>
        USB,

        /// <summary>Emulator connects over WIFI.</summary>
        WIFI,
    }

    /// @deprecated Replaced by `GvrControllerInputDevice.State`.
    /// <summary>Gets the dominant controller's current connection state.</summary>
    /// <remarks>
    /// Returns `GvrConnectionState.Error` if `GvrControllerInput` is uninitialized.
    /// </remarks>
    /// <value>The state.</value>
    [System.Obsolete("Replaced by GvrControllerInputDevice.State.")]
    public static GvrConnectionState State
    {
        get
        {
            if (instances.Length == 0)
            {
                return GvrConnectionState.Error;
            }

            return instances[0].State;
        }
    }

    /// <summary>Gets the status of the controller API.</summary>
    /// <remarks>
    /// Returns `GvrControllerApiStatus.Error` if `GvrControllerInput` is uninitialized.
    /// </remarks>
    /// <value>The api status.</value>
    public static GvrControllerApiStatus ApiStatus
    {
        get
        {
            if (instances.Length == 0)
            {
                return GvrControllerApiStatus.Error;
            }

            return instances[0].ApiStatus;
        }
    }

    /// <summary>Gets a value indicating whether battery status is supported.</summary>
    /// <remarks>Returns `false` if `GvrControllerInput` is uninitialized.</remarks>
    /// <value>
    /// Value `true` if the GVR Controller Input supports BatteryStatus calls, `false` otherwise.
    /// </value>
    public static bool SupportsBatteryStatus
    {
        get
        {
            if (controllerProvider == null)
            {
                return false;
            }

            return controllerProvider.SupportsBatteryStatus;
        }
    }

    /// @deprecated Replaced by `GvrControllerInputDevice.Orientation`.
    /// <summary>
    /// Gets the dominant controller's current orientation in space, as a quaternion.
    /// </summary>
    /// <remarks>
    /// The rotation is provided in 'orientation space' which means the rotation is given relative
    /// to the last time the user recentered their controllers.  To make a game object in your scene
    /// have the same orientation as the dominant controller, simply assign this quaternion to the
    /// object's `transform.rotation`.  To match the relative rotation, use
    /// `transform.localRotation` instead.
    /// </remarks>
    /// <value>
    /// The orientation.  This is `Quaternion.identity` if `GvrControllerInput` is uninitialized.
    /// </value>
    [System.Obsolete("Replaced by GvrControllerInputDevice.Orientation.")]
    public static Quaternion Orientation
    {
        get
        {
            if (instances.Length == 0)
            {
                return Quaternion.identity;
            }

            return instances[0].Orientation;
        }
    }

    /// @deprecated Replaced by `GvrControllerInputDevice.Gyro`.
    /// <summary>
    /// Gets the dominant controller's current angular speed in radians per second.
    /// </summary>
    /// <remarks>
    /// Uses the right-hand rule (positive means a right-hand rotation about the given axis), as
    /// measured by the controller's gyroscope.  Returns `Vector3.zero` if `GvrControllerInput` is
    /// uninitialized.
    /// <para>
    /// The controller's axes are:
    /// - X points to the right.
    /// - Y points perpendicularly up from the controller's top surface.
    /// - Z lies along the controller's body, pointing towards the front.
    /// </para></remarks>
    /// <value>The gyro's angular speed.</value>
    [System.Obsolete("Replaced by GvrControllerInputDevice.Gyro.")]
    public static Vector3 Gyro
    {
        get
        {
            if (instances.Length == 0)
            {
                return Vector3.zero;
            }

            return instances[0].Gyro;
        }
    }

    /// @deprecated Replaced by `GvrControllerInputDevice.Accel`.
    /// <summary>
    /// Gets the dominant controller's current acceleration in meters per second squared.
    /// </summary>
    /// <remarks>
    /// The controller's axes are:
    /// - X points to the right.
    /// - Y points perpendicularly up from the controller's top surface.
    /// - Z lies along the controller's body, pointing towards the front.
    /// <para>
    /// Note that gravity is indistinguishable from acceleration, so when the controller is resting
    /// on a surface, expect to measure an acceleration of 9.8 m/s^2 on the Y axis.  The
    /// accelerometer reading will be zero on all three axes only if the controller is in free fall,
    /// or if the user is in a zero gravity environment like a space station.
    /// </para></remarks>
    /// <value>
    /// The acceleration.  Will be `Vector3.zero` if `GvrControllerInput` is uninitialized.
    /// </value>
    [System.Obsolete("Replaced by GvrControllerInputDevice.Accel.")]
    public static Vector3 Accel
    {
        get
        {
            if (instances.Length == 0)
            {
                return Vector3.zero;
            }

            return instances[0].Accel;
        }
    }

    /// @deprecated Replaced by
    /// `GvrControllerInputDevice.GetButton(GvrControllerButton.TouchPadTouch)`.
    /// <summary>
    /// Gets a value indicating whether the user is touching the dominant controller's touchpad.
    /// </summary>
    /// <remarks>Returns `false` if `GvrControllerInput` is uninitialized.</remarks>
    /// <value>Value `true` if is touching. Otherwise, `false`.</value>
    [System.Obsolete(
        "Replaced by GvrControllerInputDevice.GetButton(GvrControllerButton.TouchPadTouch).")]
    public static bool IsTouching
    {
        get
        {
            if (instances.Length == 0)
            {
                return false;
            }

            return instances[0].GetButton(GvrControllerButton.TouchPadTouch);
        }
    }

    /// @deprecated Replaced by
    /// `GvrControllerInputDevice.GetButtonDown(GvrControllerButton.TouchPadTouch)`.
    /// <summary>
    /// Gets a value indicating whether this frame is the frame the user starts touching the
    /// dominant controller's touchpad.
    /// </summary>
    /// <remarks>
    /// Returns `false` if `GvrControllerInput` is uninitialized.  Every `TouchDown` event is
    /// guaranteed to be followed by exactly one `TouchUp` event in a later frame. Also, `TouchDown`
    /// and `TouchUp` will never both be `true` in the same frame.
    /// </remarks>
    /// <value>
    /// Value `true` if this is the frame after the user starts touching the dominant controller's
    /// touchpad, `false` otherwise.
    /// </value>
    [System.Obsolete(
        "Replaced by GvrControllerInputDevice.GetButtonDown(GvrControllerButton.TouchPadTouch).")]
    public static bool TouchDown
    {
        get
        {
            if (instances.Length == 0)
            {
                return false;
            }

            return instances[0].GetButtonDown(GvrControllerButton.TouchPadTouch);
        }
    }

    /// @deprecated Replaced by
    /// `GvrControllerInputDevice.GetButtonUp(GvrControllerButton.TouchPadTouch)`.
    /// <summary>
    /// Gets a value indicating whether this frame is the frame after the user stops touching the
    /// dominant controller's touchpad.
    /// </summary>
    /// <remarks>
    /// Returns `false` if `GvrControllerInput` is uninitialized.  Every `TouchUp` event is
    /// guaranteed to be preceded by exactly one `TouchDown` event in an earlier frame. Also,
    /// `TouchDown` and `TouchUp` will never both be `true` in the same frame.
    /// </remarks>
    /// <value>
    /// Value `true` if this is the frame after the user stops touching the dominant controller's
    /// touchpad, `false` otherwise.
    /// </value>
    [System.Obsolete(
        "Replaced by GvrControllerInputDevice.GetButtonUp(GvrControllerButton.TouchPadTouch).")]
    public static bool TouchUp
    {
        get
        {
            if (instances.Length == 0)
            {
                return false;
            }

            return instances[0].GetButtonUp(GvrControllerButton.TouchPadTouch);
        }
    }

    /// @deprecated Please migrate to the center-relative `GvrControllerInputDevice.TouchPos`.
    /// <summary>
    /// Gets the position of the dominant controller's current touch, if touching the touchpad.
    /// </summary>
    /// <remarks>
    /// Returns `Vector2(0.5f, 0.5f)` if `GvrControllerInput` is uninitialized.  If not touching,
    /// this is the position of the last touch (when the finger left the touchpad).  The X and Y
    /// range is from 0 to 1.  (0, 0) is the top left of the touchpad and (1, 1) is the bottom right
    /// of the touchpad.
    /// </remarks>
    /// <value>The touch position.</value>
    [System.Obsolete("Obsolete. Migrate to the center-relative GvrControllerInputDevice.TouchPos.")]
    public static Vector2 TouchPos
    {
        get
        {
            if (instances.Length == 0)
            {
                return new Vector2(0.5f, 0.5f);
            }

            Vector2 touchPos = instances[0].TouchPos;
            touchPos.x = (touchPos.x / 2.0f) + 0.5f;
            touchPos.y = (-touchPos.y / 2.0f) + 0.5f;
            return touchPos;
        }
    }

    /// @deprecated Please migrate to the center-relative `GvrControllerInputDevice.TouchPos`.
    /// <summary>
    /// Gets the position of the dominant controller's current touch, if touching the touchpad.
    /// </summary>
    /// <remarks>
    /// Returns `Vector2.zero` if `GvrControllerInput` is uninitialized.  If not touching, this is
    /// the position of the last touch (when the finger left the touchpad).  The X and Y range is
    /// from -1 to 1. (-.707,-.707) is bottom left, (.707,.707) is upper right.  (0, 0) is the
    /// center of the touchpad.  The magnitude of the touch vector is guaranteed to be less than or
    /// equal to 1.
    /// </remarks>
    /// <value>The touch position centered.</value>
    [System.Obsolete("Replaced by GvrControllerInputDevice.TouchPos.")]
    public static Vector2 TouchPosCentered
    {
        get
        {
            if (instances.Length == 0)
            {
                return Vector2.zero;
            }

            return instances[0].TouchPos;
        }
    }

    /// @deprecated Use `Recentered` to detect when user has completed the recenter gesture.
    /// <summary>Gets a value indicating whether the user is currently recentering.</summary>
    /// <value>Value `true` if the user is currently recentering, `false` otherwise.</value>
    [System.Obsolete("Use Recentered to detect when user has completed the recenter gesture.")]
    public static bool Recentering
    {
        get
        {
            return false;
        }
    }

    /// <summary>
    /// Gets a value indicating whether the user just completed the recenter gesture.
    /// </summary>
    /// <remarks>
    /// Returns `false` if `GvrControllerInput` is uninitialized.  The headset and the dominant
    /// controller's orientation are now being reported in the new recentered coordinate system.
    /// This is an event flag (it is true for only one frame after the event happens, then reverts
    /// to false).
    /// </remarks>
    /// <value>Value `true` if the user has just finished recentering, `false` otherwise.</value>
    public static bool Recentered
    {
        get
        {
            if (instances.Length == 0)
            {
                return false;
            }

            return instances[0].Recentered;
        }
    }

    /// @deprecated Replaced by
    /// `GvrControllerInputDevice.GetButton(GvrControllerButton.TouchPadButton)`.
    /// <summary>
    /// Gets a value indicating whether the user currently holds down the dominant controller's
    /// touchpad button.
    /// </summary>
    /// <remarks>Returns `false` if `GvrControllerInput` is uninitialized.</remarks>
    /// <value>
    /// Value `true` if the user currently holds down the dominant controller's touchpad button,
    /// `false` otherwise.
    /// </value>
    [System.Obsolete(
        "Replaced by GvrControllerInputDevice.GetButton(GvrControllerButton.TouchPadButton).")]
    public static bool ClickButton
    {
        get
        {
            if (instances.Length == 0)
            {
                return false;
            }

            return instances[0].GetButton(GvrControllerButton.TouchPadButton);
        }
    }

    /// @deprecated Replaced by
    /// `GvrControllerInputDevice.GetButtonDown(GvrControllerButton.TouchPadButton)`.
    /// <summary>
    /// Gets a value indicating whether this is the frame the user starts pressing down the dominant
    /// controller's touchpad button.
    /// </summary>
    /// <remarks>
    /// Returns `false` if `GvrControllerInput` is uninitialized.  Every `ClickButtonDown` event is
    /// guaranteed to be followed by exactly one `ClickButtonUp` event in a later frame. Also,
    /// `ClickButtonDown` and `ClickButtonUp` will never both be `true` in the same frame.
    /// </remarks>
    /// <value>
    /// Value `true` this is the frame the user started pressing down the dominant controller's
    /// touchpad button, `false` otherwise.
    /// </value>
    [System.Obsolete(
        "Replaced by GvrControllerInputDevice.GetButtonDown(GvrControllerButton.TouchPadButton).")]
    public static bool ClickButtonDown
    {
        get
        {
            if (instances.Length == 0)
            {
                return false;
            }

            return instances[0].GetButtonDown(GvrControllerButton.TouchPadButton);
        }
    }

    /// @deprecated Replaced by
    /// `GvrControllerInputDevice.GetButtonUp(GvrControllerButton.TouchPadButton)`.
    /// <summary>
    /// Gets a value indicating whether this is the frame after the user stops pressing down the
    /// dominant controller's touchpad button.
    /// </summary>
    /// <remarks>
    /// Returns `false` if `GvrControllerInput` is uninitialized.  Every `ClickButtonUp` event is
    /// guaranteed to be preceded by exactly one `ClickButtonDown` event in an earlier frame. Also,
    /// `ClickButtonDown` and `ClickButtonUp` will never both be `true` in the same frame.
    /// </remarks>
    /// <value>
    /// Value `true` if this is the frame after the user stops pressing down the dominant
    /// controller's touchpad button, `false` otherwise.
    /// </value>
    [System.Obsolete(
        "Replaced by GvrControllerInputDevice.GetButtonUp(GvrControllerButton.TouchPadButton).")]
    public static bool ClickButtonUp
    {
        get
        {
            if (instances.Length == 0)
            {
                return false;
            }

            return instances[0].GetButtonUp(GvrControllerButton.TouchPadButton);
        }
    }

    /// @deprecated Replaced by `GvrControllerInputDevice.GetButton(GvrControllerButton.App)`.
    /// <summary>
    /// Gets a value indicating whether the user is currently holding down the dominant controller's
    /// app button.
    /// </summary>
    /// <remarks>Returns `false` if `GvrControllerInput` is uninitialized.</remarks>
    /// <value>
    /// Value `true` if the user is currently holding down the dominant controller's app button,
    /// `false` otherwise.
    /// </value>
    [System.Obsolete("Replaced by GvrControllerInputDevice.GetButton(GvrControllerButton.App).")]
    public static bool AppButton
    {
        get
        {
            if (instances.Length == 0)
            {
                return false;
            }

            return instances[0].GetButton(GvrControllerButton.App);
        }
    }

    /// @deprecated Replaced by `GvrControllerInputDevice.GetButtonDown(GvrControllerButton.App)`.
    /// <summary>
    /// Gets a value indicating whether this is the frame the user started pressing down the
    /// dominant controller's app button.
    /// </summary>
    /// <remarks>
    /// Returns `false` if `GvrControllerInput` is uninitialized. Every `AppButtonDown` event is
    /// guaranteed to be followed by exactly one `AppButtonUp` event in a later frame.
    /// `AppButtonDown` and `AppButtonUp` will never both be `true` in the same frame.
    /// </remarks>
    /// <value>
    /// Value `true` if this is the frame the user started pressing down the dominant controller's
    /// app button, `false` otherwise.
    /// </value>
    [System.Obsolete(
        "Replaced by GvrControllerInputDevice.GetButtonDown(GvrControllerButton.App).")]
    public static bool AppButtonDown
    {
        get
        {
            if (instances.Length == 0)
            {
                return false;
            }

            return instances[0].GetButtonDown(GvrControllerButton.App);
        }
    }

    /// @deprecated Replaced by `GvrControllerInputDevice.GetButtonUp(GvrControllerButton.App)`.
    /// <summary>
    /// Gets a value indicating whether this is the frame after the user stopped pressing down the
    /// dominant controller's app button.
    /// </summary>
    /// <remarks>
    /// Returns `false` if `GvrControllerInput` is uninitialized. Every `AppButtonUp` event is
    /// guaranteed to be preceded by exactly one `AppButtonDown` event in an earlier frame. Also,
    /// `AppButtonDown` and `AppButtonUp` will never both be `true` in the same frame.
    /// </remarks>
    /// <value>
    /// Value `true` if this is the frame after the user stopped pressing down the dominant
    /// controller's app button, `false` otherwise.
    /// </value>
    [System.Obsolete("Replaced by GvrControllerInputDevice.GetButtonUp(GvrControllerButton.App).")]
    public static bool AppButtonUp
    {
        get
        {
            if (instances.Length == 0)
            {
                return false;
            }

            return instances[0].GetButtonUp(GvrControllerButton.App);
        }
    }

    /// @deprecated Replaced by
    /// `GvrControllerInputDevice.GetButtonDown(GvrControllerButton.System)`.
    /// <summary>
    /// Gets a value indicating whether this is the frame the user started pressing down the
    /// dominant controller's system button.
    /// </summary>
    /// <remarks>Returns `false` if `GvrControllerInput` is uninitialized.</remarks>
    /// <value>
    /// Value `true` if this is the frame the user started pressing down the dominant controller's
    /// system button, `false` otherwise.
    /// </value>
    [System.Obsolete(
        "Replaced by GvrControllerInputDevice.GetButtonDown(GvrControllerButton.System).")]
    public static bool HomeButtonDown
    {
        get
        {
            if (instances.Length == 0)
            {
                return false;
            }

            return instances[0].GetButtonDown(GvrControllerButton.System);
        }
    }

    /// @deprecated Replaced by GvrControllerInputDevice.GetButton(GvrControllerButton.System).
    /// <summary>
    /// Gets a value indicating whether the user is holding down the dominant controller's system
    /// button.
    /// </summary>
    /// <remarks>Returns `false` if `GvrControllerInput` is uninitialized.</remarks>
    /// <value>
    /// Value `true` if the user is holding down the dominant controller's system button, `false`
    /// otherwise.
    /// </value>
    [System.Obsolete("Replaced by GvrControllerInputDevice.GetButton(GvrControllerButton.System).")]
    public static bool HomeButtonState
    {
        get
        {
            if (instances.Length == 0)
            {
                return false;
            }

            return instances[0].GetButton(GvrControllerButton.System);
        }
    }

    /// @deprecated Replaced by `GvrControllerInputDevice.ErrorDetails`.
    /// <summary>
    /// Gets details about the reasoning behind the Dominant Controller's error state.
    /// </summary>
    /// <remarks>
    /// If the dominant controller's state `== GvrConnectionState.Error`, this contains details
    /// about the error.  If `GvrControllerInput` is uninitialized this returns an error string
    /// describing the uninitialized state.
    /// </remarks>
    /// <value>Details about the reasoning behind the dominant controller's error state.</value>
    [System.Obsolete("Replaced by GvrControllerInputDevice.ErrorDetails.")]
    public static string ErrorDetails
    {
        get
        {
            if (instances.Length > 0)
            {
                return instances[0].ErrorDetails;
            }
            else
            {
                return "No GvrControllerInput initialized instance found in scene. "
                       + "It may be missing, or it might not have initialized yet.";
            }
        }
    }

    /// @deprecated Replaced by `GvrControllerInputDevice.StatePtr`.
    /// <summary>
    /// Gets the GVR C library controller state pointer (`gvr_controller_state*`) for the dominant
    /// controller.
    /// </summary>
    /// <remarks>Returns `IntPtr.Zero` if `GvrControllerInput` is uninitialized.</remarks>
    /// <value>
    /// The GVR C library controller state pointer (`gvr_controller_state*`) for the dominant
    /// controller.
    /// </value>
    [System.Obsolete("Replaced by GvrControllerInputDevice.StatePtr.")]
    public static IntPtr StatePtr
    {
        get
        {
            if (instances.Length == 0)
            {
                return IntPtr.Zero;
            }

            return instances[0].StatePtr;
        }
    }

    /// @deprecated Replaced by `GvrControllerInputDevice.IsCharging`.
    /// <summary>
    /// Gets a value indicating whether the dominant controller is currently being charged.
    /// </summary>
    /// <remarks>Returns `false` if `GvrControllerInput` is uninitialized.</remarks>
    /// <value>Value `true` if the dominant controller is charging.  Otherwise, `false`.</value>
    [System.Obsolete("Replaced by GvrControllerInputDevice.IsCharging.")]
    public static bool IsCharging
    {
        get
        {
            if (instances.Length == 0)
            {
                return false;
            }

            return instances[0].IsCharging;
        }
    }

    /// @deprecated Replaced by `GvrControllerInputDevice.BatteryLevel`.
    /// <summary>Gets the dominant controller's current battery charge level.</summary>
    /// <remarks>
    /// Returns `GvrControllerBatteryLevel.Error` if `GvrControllerInput` is uninitialized.
    /// </remarks>
    /// <value>The dominant controller's current battery charge level.</value>
    [System.Obsolete("Replaced by GvrControllerInputDevice.BatteryLevel.")]
    public static GvrControllerBatteryLevel BatteryLevel
    {
        get
        {
            if (instances.Length == 0)
            {
                return GvrControllerBatteryLevel.Error;
            }

            return instances[0].BatteryLevel;
        }
    }

    /// <summary>Returns a controller device for the specified hand.</summary>
    /// <returns>The controller input device.</returns>
    /// <param name="hand">The hand whose input device should be fetched.</param>
    public static GvrControllerInputDevice GetDevice(GvrControllerHand hand)
    {
        if (instances.Length == 0)
        {
            return null;
        }

        // Remap Right and Left to Dominant or NonDominant according to settings handedness.
        if (hand == GvrControllerHand.Left || hand == GvrControllerHand.Right)
        {
            if ((int)hand != (int)handedness)
            {
                hand = GvrControllerHand.NonDominant;
            }
            else
            {
                hand = GvrControllerHand.Dominant;
            }
        }

        if (hand == GvrControllerHand.NonDominant)
        {
            return instances[1];
        }
        else
        {
            // Dominant is always controller 0.
            return instances[0];
        }
    }

    private void Awake()
    {
        if (instances.Length > 0)
        {
            Debug.LogError(
                "More than one active GvrControllerInput instance was found in your scene. "
                + "Ensure that there is only one GvrControllerInput.");
            this.enabled = false;
            return;
        }

        if (controllerProvider == null)
        {
            controllerProvider = ControllerProviderFactory.CreateControllerProvider(this);
        }

        handedness = GvrSettings.Handedness;
        int controllerCount = 2;
        instances = new GvrControllerInputDevice[controllerCount];
        for (int i = 0; i < controllerCount; i++)
        {
            instances[i] = new GvrControllerInputDevice(controllerProvider, i);
        }

        if (onDevicesChangedInternal != null)
        {
            onDevicesChangedInternal();
        }

        // Keep screen on here, since GvrControllerInput must be in any GVR scene in order to enable
        // controller capabilities.
        Screen.sleepTimeout = SleepTimeout.NeverSleep;
    }

    private void Update()
    {
        foreach (var instance in instances)
        {
            if (instance != null)
            {
                instance.Update();
            }
        }

        if (OnControllerInputUpdated != null)
        {
            OnControllerInputUpdated();
        }

        if (OnPostControllerInputUpdated != null)
        {
            OnPostControllerInputUpdated();
        }
    }

    private void OnDestroy()
    {
        foreach (var instance in instances)
        {
            // Ensure this device will error if used again.
            instance.Invalidate();
        }

        instances = new GvrControllerInputDevice[0];
        if (onDevicesChangedInternal != null)
        {
            onDevicesChangedInternal();
        }
    }

    private void OnApplicationPause(bool paused)
    {
        if (controllerProvider == null)
        {
            return;
        }

        if (paused)
        {
            controllerProvider.OnPause();
        }
        else
        {
            handedness = GvrSettings.Handedness;
            controllerProvider.OnResume();
        }
    }
}