public void Stop() { foreach (var slot in this.Slots) { slot.Gamepad.Disconnected -= this.OnGamepadDisconnected; if (slot.Gamepad.LedNumber != 0) { XinputController.RetrieveController((int)slot.Gamepad.LedNumber - 1).Tag = null; slot.Gamepad.LedNumber = 0; } slot.Unlock(); if (slot.Gamepad.IsOwned) { if (slot.Gamepad.Unplug(true)) { LogWriter.Write(string.Format("Successfully unplugged {0}", slot.Gamepad.FriendlyName)); } else { LogWriter.Write(string.Format("Unplug failed: {0}", slot.Gamepad.FriendlyName)); } } this.lastStartOrStopTime = DateTime.Now; } LogWriter.Write("Emulation stopped"); this.IsEmulationStarted = false; if (this.EmulationStopped != null) { this.EmulationStopped(this, EventArgs.Empty); } }
private void OnTimerTick(object sender, EventArgs e) { this.timer.Stop(); this.SizeToContent = SizeToContent.Manual; this.panel.Children.Clear(); for (int i = 0; i < 4; i++) { var xinputController = XinputController.RetrieveController(i); xinputController.PluggedChanged += this.OnXinputControllerPluggedChanged; if (xinputController.IsConnected) { var slot = new XboxTestSlot(xinputController); this.panel.Children.Add(slot); } } this.SizeToContent = SizeToContent.WidthAndHeight; this.button.IsEnabled = true; this.Cursor = Cursors.Arrow; }
public void Start(bool forced = false) { if (!forced) { if (this.lastStartOrStopTime == null) { this.lastStartOrStopTime = DateTime.Now; } else { if (DateTime.Now - this.lastStartOrStopTime < TimeSpan.FromSeconds(5)) { throw new KeyboardSplitterExceptionBase("You should wait at least 5 seconds, between each emulation start/stop!"); } } this.lastStartOrStopTime = DateTime.Now; } try { this.CheckForInvalidatedSlots(); this.CheckXinputBus(); } catch (Exception) { throw; } // Releasing xinput controller tags for (int i = 0; i < 4; i++) { XinputController.RetrieveController(i).Tag = null; } // Plugging in the virtual controllers foreach (var slot in this.Slots) { if (slot.Keyboard == null && slot.Mouse == null) { slot.InvalidateReason = SlotInvalidationReason.No_Input_Device_Selected; throw new SlotInvalidatedException(string.Format("Slot #{0} has no valid input device selected", slot.SlotNumber)); } string errorMessage = string.Format("Plug in {0} failed!", slot.Gamepad.FriendlyName); bool success = false; try { success = slot.Gamepad.PlugIn(); } catch (VirtualBusNotInstalledException) { slot.InvalidateReason = SlotInvalidationReason.VirtualBus_Not_Installed; errorMessage += " " + slot.InvalidateReason.ToString(); LogWriter.Write(errorMessage); throw; } catch (XboxAccessoriesNotInstalledException) { slot.InvalidateReason = SlotInvalidationReason.Additional_Drivers_Not_Installed; errorMessage += " " + slot.InvalidateReason.ToString(); LogWriter.Write(errorMessage); throw; } catch (VirtualBusFullException) { slot.InvalidateReason = SlotInvalidationReason.VirtualBus_Full; errorMessage += " " + slot.InvalidateReason.ToString(); LogWriter.Write(errorMessage); throw; } catch (GamepadIsInUseException) { slot.InvalidateReason = SlotInvalidationReason.Controller_Already_Plugged_In; errorMessage += " " + slot.InvalidateReason.ToString(); LogWriter.Write(errorMessage); throw; } catch (GamepadOwnedException) { slot.InvalidateReason = SlotInvalidationReason.Controller_In_Use; errorMessage += " " + slot.InvalidateReason.ToString(); LogWriter.Write(errorMessage); throw; } catch (XinputSlotsFullException) { slot.InvalidateReason = SlotInvalidationReason.XinputBus_Full; errorMessage += " " + slot.InvalidateReason.ToString(); LogWriter.Write(errorMessage); throw; } if (success) { slot.Lock(); LogWriter.Write(string.Format( "Plug in {0} - OK | User Index: {1} | {2} | {3} | Slot #{4}", slot.Gamepad.FriendlyName, slot.Gamepad.UserIndex, slot.Keyboard.StrongName, slot.Mouse.StrongName, slot.SlotNumber)); slot.Gamepad.Disconnected += this.OnGamepadDisconnected; } else { slot.InvalidateReason = SlotInvalidationReason.Controller_Plug_In_Failed; LogWriter.Write(string.Format("Plug in {0} - Failed!", slot.Gamepad.FriendlyName)); // Releasing xinput controller tags for (int i = 0; i < 4; i++) { XinputController.RetrieveController(i).Tag = null; } return; } } LogWriter.Write(string.Format("Emulation started. Slots count: {0}", this.Slots.Count)); this.IsEmulationStarted = true; if (this.EmulationStarted != null) { this.EmulationStarted(this, EventArgs.Empty); } }
/// <summary> /// Plugs the xbox controller into the virtual bus. /// </summary> /// <returns>Returns true if the process is successfull</returns> /// <exception cref="XboxAccessoriesNotInstalledException">XboxAccessoriesNotInstalledException</exception> /// <exception cref="VirtualBusNotInstalledException">VirtualBusNotInstalledException</exception> /// <exception cref="GamepadIsInUseException">GamepadIsInUseException</exception> /// <exception cref="GamepadOwnedException">GamepadOwnedException</exception> public bool PlugIn() { try { this.CheckDrivers(); } catch (Exception) { throw; } if (VirtualXboxBus.EmptySlotsCount == 0) { throw new VirtualBusFullException("There is no free slot in the virtual bus"); } if (this.Exsists) { throw new GamepadIsInUseException(this.FriendlyName + " is already mounted!"); } var xinputControllers = new System.Collections.Generic.List <XinputController>(); for (int i = 0; i < 4; i++) { xinputControllers.Add(XinputController.RetrieveController(i)); } if (xinputControllers.TrueForAll(x => x.Tag != null)) { throw new KeyboardSplitterExceptionBase("Internal Error: all xinput controllers are marked as virtual!"); } this.xinputController = xinputControllers.FirstOrDefault(x => !x.IsConnected && x.Tag == null); if (this.xinputController != null) { this.xinputController.Tag = this; this.xinputController.PluggedChanged += this.OnXinputControllerPluggedChanged; } else { LogWriter.Write("--- Xinput controllers information ---"); foreach (var xinputController in xinputControllers) { LogWriter.Write( string.Format( "Controller #{0} [IsConnected:{1}] [IsVirtual:{2}]", xinputController.LedNumber, xinputController.IsConnected, xinputController.Tag != null)); } LogWriter.Write("--- End of Xinput controllers information---"); throw new InvalidOperationException("Unknown error occured, just before plugging in the virtual controller!"); } this.connectionRequestTime = DateTime.Now; if (!VirtualXboxController.PlugIn(this.UserIndex)) { this.LedNumber = 0; this.xinputController.Tag = null; this.xinputController.PluggedChanged -= this.OnXinputControllerPluggedChanged; this.xinputController = null; return(false); } return(true); }