Inheritance: IDisposable
Example #1
0
        private void tmrEnumerate_Tick(object sender, EventArgs e)
        {
            // Most of the HidDevice methods call Enumerate behind the scenes,
            //  which talks to the plug&play service, devouring CPU time.
            // As a result, we only look for a new controller here when none is available,
            //  and we do this at a slow rate.

            lock (this)
            if (CurrentDualShock == null) {
                var dualshocks = DualShock4Info.Enumerate();

                if (dualshocks.Length > 0)
                    CurrentDualShock = new DualShock4(dualshocks[0], false);

                foreach (var info in dualshocks) {
                    if ((CurrentDualShock == null) || !Object.ReferenceEquals(info.Device, CurrentDualShock.Device))
                        info.Dispose();
                }
            }
        }
Example #2
0
        private void UpdaterThreadFunc()
        {
            var previousJoystick = JoystickToUse;
            var gestureTimes = new DateTime[4];

            while (true) {
                uint? activeJoystick;

                // Lock the main window to guard its state
                lock (this) {
                    if (CurrentDualShock != null) {
                        if (!CurrentDualShock.TryUpdate()) {
                            // If updating the state of the controller failed, we take this
                            //  as an indication that the controller is disconnected.
                            // We can't use HidDevice.IsConnected because it gets chatty with
                            //  the plug&play service for no good reason.
                            // Don't put expensive operations in property getters, kids.
                            CurrentDualShock.Dispose();
                            CurrentDualShock = null;
                        }
                    }

                    activeJoystick = JoystickToUse;
                }

                if (previousJoystick != activeJoystick) {
                    if (previousJoystick.HasValue) {
                        DeInitJoystick(previousJoystick.Value);
                        Console.WriteLine("Released joystick #{0}", previousJoystick.Value);
                    }

                    if (activeJoystick.HasValue) {
                        if (!InitJoystick(activeJoystick.Value)) {
                            Console.WriteLine("Could not initialize joystick #{0}", activeJoystick.Value);
                            activeJoystick = null;
                            JoystickFailed = true;
                        } else {
                            Console.WriteLine("Initialized joystick #{0}", activeJoystick.Value);
                        }
                    }
                }

                if (CurrentDualShock != null) {
                    HandleGestures(gestureTimes);
                }

                if (activeJoystick.HasValue && (CurrentDualShock != null))
                    UpdateJoystick(activeJoystick.Value, gestureTimes);

                previousJoystick = activeJoystick;

                // DS4 sends updates every ~4 milliseconds. This will probably sleep too long, but
                //  Thread.Sleep(0) doesn't sleep long enough.
                // In practice as long as we don't sleep for more than 15ms, this should be fast enough
                //  to pump a new update into the joystick for a game.
                Thread.Sleep(UpdateInterval);
            }
        }
Example #3
0
        public void Update(DualShock4 controller, string latestText)
        {
            TouchHistory.Add(controller.Touchpad[0]);

            if (TouchHistory.Count > HistoryLength)
                TouchHistory.RemoveAt(0);

            if (latestText != null) {
                TextHistory.Insert(0, new TextEntry {
                    When = DateTime.UtcNow,
                    Text = latestText
                });

                if (TextHistory.Count > TextHistoryLength)
                    TextHistory.RemoveAt(TextHistory.Count - 1);
            }
        }