private void DeviceControl_OnConnectionLost(DeviceControl sender) { if (groupAvailable.Children.Contains(sender)) { groupAvailable.Children.Remove(sender); } else if (groupXinput.Children.Contains(sender)) { groupXinput.Children.Remove(sender); } deviceList.Remove(sender); AutoRefresh(menu_AutoRefresh.IsChecked); }
private void DeviceControl_OnConnectionLost(DeviceControl sender) { if (groupAvailable.Children.Contains(sender)) { groupAvailable.Children.Remove(sender); } else if (groupXinput.Children.Contains(sender)) { groupXinput.Children.Remove(sender); } deviceList.Remove(sender); Delay(100).ContinueWith(o => Refresh()); Delay(200).ContinueWith(o => AutoRefresh(menu_AutoRefresh.IsChecked && deviceList.Count == 0)); }
private void DeviceControl_OnConnectStateChange(DeviceControl sender, DeviceState oldState, DeviceState newState) { if (oldState == newState) { return; } switch (oldState) { case DeviceState.Discovered: groupAvailable.Children.Remove(sender); break; case DeviceState.Connected_XInput: groupXinput.Children.Remove(sender); break; //case DeviceState.Connected_VJoy: // groupXinput.Children.Remove(sender); // break; } switch (newState) { case DeviceState.Discovered: groupAvailable.Children.Add(sender); break; case DeviceState.Connected_XInput: groupXinput.Children.Add(sender); break; //case DeviceState.Connected_VJoy: // groupXinput.Children.Add(sender); // break; } if (menu_AutoRefresh.IsChecked) { AutoRefresh(ApplicationIsActivated()); } }
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++; } } } }
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++; } } } }