예제 #1
0
        public bool Connect()
        {
            bool success = false;

            if (_info.DevicePath == "Dummy")
            {
                _dummy = new Shared.DummyDevice(Calibrations.Defaults.ProControllerDefault);
                var dumWin = new Windows.DummyWindow(_dummy);
                dumWin.Show();
                _nintroller   = new Nintroller(_dummy);
                OnDisconnect += () =>
                {
                    dumWin.Close();
                };
            }
            else
            {
                _stream     = new WinBtStream(_info.DevicePath);
                _nintroller = new Nintroller(_stream);//, deviceInfo.Type);
            }
            _nintroller.StateUpdate     += _nintroller_StateUpdate;
            _nintroller.ExtensionChange += _nintroller_ExtensionChange;
            _nintroller.LowBattery      += _nintroller_LowBattery;

            // TODO: Seems OpenConnection() can still succeed on toshiba w/o device connected
            if (_dummy != null || _stream.OpenConnection())
            {
                _nintroller.BeginReading();
                _nintroller.GetStatus();
                _nintroller.SetPlayerLED(1);

                // We need a function we can await for the type to come back
                // But the hint type may be present
                CreateController(_nintroller.Type);

                if (_controller != null)
                {
                    SetupController();

                    success = true;
                    _nintroller.SetReportType(InputReport.ExtOnly, true);
                }
                else
                {
                    // Controller type not supported or unknown, teardown?
                    //success = false;
                    success = true;
                }

                _nintroller.Disconnected += _nintroller_Disconnected;
            }
#if DEBUGz
            else
            {
                _controller = new ProControl(Calibrations.Defaults.ProControllerDefault);
                SetupController();
                success = true;
            }
예제 #2
0
        private void Refresh()
        {
            hidList = WinBtStream.GetPaths();
            List <KeyValuePair <int, DeviceControl> > connectSeq = new List <KeyValuePair <int, DeviceControl> >();

            foreach (var hid in hidList)
            {
                DeviceControl existingDevice = null;

                foreach (DeviceControl d in deviceList)
                {
                    if (d.DevicePath == hid.DevicePath)
                    {
                        existingDevice = d;
                        break;
                    }
                }

                if (existingDevice != null)
                {
                    if (!existingDevice.Connected)
                    {
                        existingDevice.RefreshState();
                        if (existingDevice.properties.autoConnect && existingDevice.ConnectionState == DeviceState.Discovered)
                        {
                            connectSeq.Add(new KeyValuePair <int, DeviceControl>(existingDevice.properties.autoNum, existingDevice));
                        }
                    }
                }
                else
                {
                    var stream = new WinBtStream(
                        hid.DevicePath,
                        UserPrefs.Instance.toshibaMode ? WinBtStream.BtStack.Toshiba : WinBtStream.BtStack.Microsoft,
                        UserPrefs.Instance.greedyMode ? FileShare.None : FileShare.ReadWrite);
                    Nintroller n = new Nintroller(stream, hid.Type);

                    if (stream.OpenConnection() && stream.CanRead)
                    {
                        deviceList.Add(new DeviceControl(n, hid.DevicePath));
                        deviceList[deviceList.Count - 1].OnConnectStateChange += DeviceControl_OnConnectStateChange;
                        deviceList[deviceList.Count - 1].OnConnectionLost     += DeviceControl_OnConnectionLost;
                        deviceList[deviceList.Count - 1].RefreshState();
                        if (deviceList[deviceList.Count - 1].properties.autoConnect)
                        {
                            connectSeq.Add(new KeyValuePair <int, DeviceControl>(deviceList[deviceList.Count - 1].properties.autoNum, deviceList[deviceList.Count - 1]));
                        }
                    }
                }
            }

            int target = -1;

            for (int i = 0; i < 4; i++)
            {
                if (Holders.XInputHolder.availabe.Length > i)
                {
                    if (Holders.XInputHolder.availabe[i])
                    {
                        target = i;
                        break;
                    }
                }
            }

            if (target < 0)
            {
                return;
            }

            //while (!Holders.XInputHolder.availabe[target] && target < 4)
            //{
            //    target++;
            //}

            // Auto Connect First Available devices
            for (int a = 0; a < connectSeq.Count; a++)
            {
                var thingy = connectSeq[a];

                if (thingy.Key == 5)
                {
                    if (Holders.XInputHolder.availabe[target] && target < 4)
                    {
                        if (thingy.Value.Device.Connected || (thingy.Value.Device.DataStream as WinBtStream).OpenConnection())
                        {
                            thingy.Value.targetXDevice   = target + 1;
                            thingy.Value.ConnectionState = DeviceState.Connected_XInput;
                            thingy.Value.Device.BeginReading();
                            thingy.Value.Device.GetStatus();
                            thingy.Value.Device.SetPlayerLED(target + 1);
                            target++;
                        }
                    }

                    connectSeq.Remove(thingy);
                }
            }

            // Auto connect in preferred order
            for (int i = 1; i < connectSeq.Count; i++)
            {
                if (connectSeq[i].Key < connectSeq[i - 1].Key)
                {
                    var tmp = connectSeq[i];
                    connectSeq[i]     = connectSeq[i - 1];
                    connectSeq[i - 1] = tmp;
                    i = 0;
                }
            }

            foreach (KeyValuePair <int, DeviceControl> d in connectSeq)
            {
                if (Holders.XInputHolder.availabe[target] && target < 4)
                {
                    if (d.Value.Device.Connected || (d.Value.Device.DataStream as WinBtStream).OpenConnection())
                    {
                        d.Value.targetXDevice   = target + 1;
                        d.Value.ConnectionState = DeviceState.Connected_XInput;
                        d.Value.Device.BeginReading();
                        d.Value.Device.GetStatus();
                        d.Value.Device.SetPlayerLED(target + 1);
                        target++;
                    }
                }
            }
        }
예제 #3
0
        private void Refresh()
        {
            hidList = WinBtStream.GetPaths();
            List <KeyValuePair <int, DeviceControl> > connectSeq = new List <KeyValuePair <int, DeviceControl> >();

            //for each Nintendo (HID) device in device manager
            foreach (var hid in hidList)                        //will create new DeviceControl if not already present and execute actions
            {                                                   //if DeviceControl is already present will execute actions
                DeviceControl existingDevice = null;

                foreach (DeviceControl d in deviceList)
                {
                    if (d.DevicePath == hid.DevicePath)
                    {
                        existingDevice = d;
                        break;
                    }
                }

                if (existingDevice != null)                     // follow this procedure for devices that already have DeviceControl
                {                                               // Refreshstate and check for AutoConnect
                    if (!existingDevice.Connected)
                    {
                        existingDevice.RefreshState();
                    }

                    if (UserPrefs.Instance.autoXInput && existingDevice.properties.autoNum != 0 && existingDevice.ConnectionState == DeviceState.Discovered)
                    {
                        connectSeq.Add(new KeyValuePair <int, DeviceControl>(existingDevice.properties.autoNum, existingDevice));
                    }
                }
                else                                           // or follow this prodcedure that do not have DeviceControl
                {                                              // create DeviceControl, subscribes to DeviceControl's events, RefreshState, and check for AutoConnect
                    var stream = new WinBtStream(
                        hid.DevicePath,
                        UserPrefs.Instance.toshibaMode ? WinBtStream.BtStack.Toshiba : WinBtStream.BtStack.Microsoft,
                        UserPrefs.Instance.greedyMode ? FileShare.None : FileShare.ReadWrite);
                    Nintroller n = new Nintroller(stream, hid.Type);

                    if (stream.OpenConnection() && stream.CanRead)
                    {
                        deviceList.Add(new DeviceControl(n, hid.DevicePath, hid.Mac));
                        deviceList[deviceList.Count - 1].OnConnectStateChange += DeviceControl_OnConnectStateChange;
                        deviceList[deviceList.Count - 1].OnConnectionLost     += DeviceControl_OnConnectionLost;
                        deviceList[deviceList.Count - 1].RefreshState();
                        if (UserPrefs.Instance.autoXInput && deviceList[deviceList.Count - 1].properties.autoNum != 0)
                        {
                            connectSeq.Add(new KeyValuePair <int, DeviceControl>(deviceList[deviceList.Count - 1].properties.autoNum, deviceList[deviceList.Count - 1]));
                        }
                    }
                }
            }

            //for each existing DeviceControl
            foreach (DeviceControl dc in deviceList)                    //check if existing Device Control has its corresponding HID device still present
            {
                bool hidPresent = false;
                foreach (var hid in hidList)
                {
                    if (dc.DevicePath == hid.DevicePath)
                    {
                        hidPresent = true;
                        break;
                    }
                }
                if (hidPresent == false)                                // dispose of DeviceControl on if corresponding HID device is not present
                {
                    dc.setDisconnected();
                }
            }

            int target = 0;

            while (!Holders.XInputHolder.availabe[target] && target < 4)
            {
                target++;
            }

            // Auto Connect First Available devices
            for (int a = 0; a < connectSeq.Count; a++)
            {
                var thingy = connectSeq[a];

                if (thingy.Key == 5)
                {
                    if (Holders.XInputHolder.availabe[target] && target < 4)
                    {
                        if (thingy.Value.Device.Connected || (thingy.Value.Device.DataStream as WinBtStream).OpenConnection())
                        {
                            thingy.Value.targetXDevice   = target + 1;
                            thingy.Value.ConnectionState = DeviceState.Connected_XInput;
                            thingy.Value.Device.BeginReading();
                            thingy.Value.Device.GetStatus();
                            thingy.Value.Device.SetPlayerLED(target + 1);
                            target++;
                        }
                    }

                    connectSeq.Remove(thingy);
                }
            }

            // Auto connect in preferred order
            for (int i = 1; i < connectSeq.Count; i++)
            {
                if (connectSeq[i].Key < connectSeq[i - 1].Key)
                {
                    var tmp = connectSeq[i];
                    connectSeq[i]     = connectSeq[i - 1];
                    connectSeq[i - 1] = tmp;
                    i = 0;
                }
            }

            foreach (KeyValuePair <int, DeviceControl> d in connectSeq)
            {
                if (Holders.XInputHolder.availabe[target] && target < 4)
                {
                    if (d.Value.Device.Connected || (d.Value.Device.DataStream as WinBtStream).OpenConnection())
                    {
                        d.Value.targetXDevice   = target + 1;
                        d.Value.ConnectionState = DeviceState.Connected_XInput;
                        d.Value.Device.BeginReading();
                        d.Value.Device.GetStatus();
                        d.Value.Device.SetPlayerLED(target + 1);
                        target++;
                    }
                }
            }
        }
예제 #4
0
        public void Refresh()
        {
            // Prevent this method from being spammed
            if (DateTime.Now.Subtract(_lastRefreshTime).TotalSeconds < 1)
            {
                return;
            }
            _lastRefreshTime = DateTime.Now;

            // Bluetooth Devices
            var devices = WinBtStream.GetPaths();

            // Direct input Devices
            DInput = new SharpDX.DirectInput.DirectInput();
            var joys = DInput.GetDevices(SharpDX.DirectInput.DeviceClass.GameControl, SharpDX.DirectInput.DeviceEnumerationFlags.AllDevices);

            foreach (var j in joys)
            {
                string pid = j.ProductGuid.ToString().Substring(0, 4);
                string vid = j.ProductGuid.ToString().Substring(4, 4);

                // devices to ignore
                if (vid == "057e" && (pid == "0330" || pid == "0306" || pid == "0337"))
                {
                    continue;
                }
                else if (vid == "1234" && pid == "bead")
                {
                    continue;
                }

                devices.Add(new Shared.DeviceInfo()
                {
                    InstanceGUID = j.InstanceGuid,
                    PID          = pid,
                    VID          = vid
                });
            }

            // GCN Adapter
            WinUsbStream gcnStream = new WinUsbStream(new UsbDeviceFinder(0x057E, 0x0337));

            Shared.DeviceInfo gcnDevice = null;
            if (gcnStream.DeviceFound())
            {
                gcnDevice = new Shared.DeviceInfo()
                {
                    VID  = "057E",
                    PID  = "0337",
                    Type = NintrollerLib.ControllerType.Other
                };

                devices.Add(gcnDevice);
            }
#if DEBUG
            // Test GCN Device
            else
            {
                devices.Add(new Shared.DeviceInfo()
                {
                    DevicePath = "Dummy GCN", PID = "0337", Type = NintrollerLib.ControllerType.Other
                });
            }

            // Test Device
            devices.Add(new Shared.DeviceInfo()
            {
                DevicePath = "Dummy", Type = NintrollerLib.ControllerType.ProController
            });
            devices.Add(new Shared.DeviceInfo()
            {
                DevicePath = "Dummy Wiimote", Type = NintrollerLib.ControllerType.Wiimote
            });
#endif

            foreach (var info in devices)
            {
                // Check if we are already showing this one
                DeviceStatus existing = _availableDevices.Find((d) => d.Info.SameDevice(info.DeviceID));

                // If not add it
                if (existing == null)
                {
                    var status = new DeviceStatus(info, info == gcnDevice ? gcnStream : null);
                    status.ConnectClick = DoConnect;
                    status.TypeUpdated  = (s, t) =>
                    {
                        var    p     = AppPrefs.Instance.GetDevicePreferences(s.Info.DevicePath);
                        string title = "";

                        if (p != null && !string.IsNullOrWhiteSpace(p.nickname))
                        {
                            title = p.nickname;
                        }
                        else
                        {
                            title = t.ToString();
                        }

                        foreach (var tab in tabControl.Items)
                        {
                            if (tab is TabItem && (tab as TabItem).Content == s.Control)
                            {
                                ChangeIcon(tab as TabItem, t);
                                ChangeTitle(tab as TabItem, title);
                            }
                        }
                    };
                    status.CloseTab = (s) =>
                    {
                        // Find associated tab, skip first as it is home
                        for (int i = 1; i < tabControl.Items.Count; i++)
                        {
                            var tab = tabControl.Items[i];
                            if (tab is TabItem && (tab as TabItem).Content == s.Control)
                            {
                                tabControl.Items.RemoveAt(i);
                                break;
                            }
                        }
                    };
                    status.OnPrefsChange = (s, p) =>
                    {
                        if (!string.IsNullOrWhiteSpace(p.nickname))
                        {
                            foreach (var tab in tabControl.Items)
                            {
                                if (tab is TabItem && (tab as TabItem).Content == s.Control)
                                {
                                    ChangeTitle(tab as TabItem, p.nickname);
                                }
                            }
                        }
                    };
                    status.OnRumbleSubscriptionChange = RumbleSettingsChanged;
                    _availableDevices.Add(status);
                    statusStack.Children.Add(status);

                    DevicePrefs devicePrefs = AppPrefs.Instance.GetDevicePreferences(info.DeviceID);
                    if (devicePrefs.autoConnect)
                    {
                        status.AutoConnect();
                    }
                }
                else if (!existing.Connected)
                {
                    DevicePrefs existingPrefs = AppPrefs.Instance.GetDevicePreferences(info.DeviceID);
                    if (existingPrefs.autoConnect)
                    {
                        existing.AutoConnect();
                    }
                }
            }
        }
예제 #5
0
        public void Refresh()
        {
            // Prevent this method from being spammed
            if (DateTime.Now.Subtract(_lastRefreshTime).TotalSeconds < 1)
            {
                return;
            }
            _lastRefreshTime = DateTime.Now;

            var devices = WinBtStream.GetPaths();

            // Direct input Devices
            DInput = new SharpDX.DirectInput.DirectInput();
            var joys = DInput.GetDevices(SharpDX.DirectInput.DeviceClass.GameControl, SharpDX.DirectInput.DeviceEnumerationFlags.AllDevices);

            foreach (var j in joys)
            {
                string pid = j.ProductGuid.ToString().Substring(0, 4);
                string vid = j.ProductGuid.ToString().Substring(4, 4);

                // devices to ignore
                if (vid == "057e" && (pid == "0330" || pid == "0306"))
                {
                    continue;
                }
                else if (vid == "1234" && pid == "bead")
                {
                    continue;
                }

                devices.Add(new Shared.DeviceInfo()
                {
                    InstanceGUID = j.InstanceGuid,
                    PID          = pid,
                    VID          = vid
                });
            }

            foreach (var info in devices)
            {
                // Check if we are already showing this one
                DeviceStatus existing = _availableDevices.Find((d) => d.Info.SameDevice(info.DeviceID));

                // If not add it
                if (existing == null)
                {
                    var status = new DeviceStatus(info);
                    status.ConnectClick = DoConnect;
                    status.TypeUpdated  = (s, t) =>
                    {
                        var    p     = AppPrefs.Instance.GetDevicePreferences(s.Info.DevicePath);
                        string title = "";

                        if (p != null && !string.IsNullOrWhiteSpace(p.nickname))
                        {
                            title = p.nickname;
                        }
                        else
                        {
                            title = t.ToString();
                        }

                        foreach (var tab in tabControl.Items)
                        {
                            if (tab is TabItem && (tab as TabItem).Content == s.Control)
                            {
                                ChangeIcon(tab as TabItem, t);
                                ChangeTitle(tab as TabItem, title);
                            }
                        }
                    };
                    status.CloseTab = (s) =>
                    {
                        // Find associated tab, skip first as it is home
                        for (int i = 1; i < tabControl.Items.Count; i++)
                        {
                            var tab = tabControl.Items[i];
                            if (tab is TabItem && (tab as TabItem).Content == s.Control)
                            {
                                tabControl.Items.RemoveAt(i);
                                break;
                            }
                        }
                    };
                    status.OnPrefsChange = (s, p) =>
                    {
                        if (!string.IsNullOrWhiteSpace(p.nickname))
                        {
                            foreach (var tab in tabControl.Items)
                            {
                                if (tab is TabItem && (tab as TabItem).Content == s.Control)
                                {
                                    ChangeTitle(tab as TabItem, p.nickname);
                                }
                            }
                        }
                    };

                    _availableDevices.Add(status);
                    statusStack.Children.Add(status);

                    DevicePrefs devicePrefs = AppPrefs.Instance.GetDevicePreferences(info.DeviceID);
                    if (devicePrefs.autoConnect)
                    {
                        status.AutoConnect();
                    }
                }
                else if (!existing.Connected)
                {
                    DevicePrefs existingPrefs = AppPrefs.Instance.GetDevicePreferences(info.DeviceID);
                    if (existingPrefs.autoConnect)
                    {
                        existing.AutoConnect();
                    }
                }
            }
        }