// // Input // public void SetPlayerToGamepad(int gamepad) { gamepadAssigned = (Gamepads)gamepad; if (gamepad == 0) { SetAnimDirection(false, false, false, true); } }
private void OnGamepadRemoved(object?sender, Gamepad gamepad) { if (gamepads.TryGetValue(gamepad, out UwpGamepad? currentGamepad)) { gamepads.Remove(gamepad); Gamepads.Remove(currentGamepad); } }
private void OnGamepadAdded(object?sender, Gamepad gamepad) { if (!gamepads.ContainsKey(gamepad)) { UwpGamepad newGamepad = new UwpGamepad(gamepad); gamepads.Add(gamepad, newGamepad); Gamepads.Add(newGamepad); } }
public bool SetVibration(Gamepads gamepad, float leftMotor, float rightMotor) { if (!gamepads[(int)gamepad].playerIndexSet) { return(false); } GamePad.SetVibration(gamepads[(int)gamepad].playerIndex, leftMotor, rightMotor); return(true); }
static void Main(string[] args) { if (!IsSingleInstance()) { Environment.Exit(0); } NIcon = new NotifyIcon(); //ScpBus scpBus = null; ScpBus scpBus = new ScpBus(); scpBus.UnplugAll(); global_scpBus = scpBus; handler = new ConsoleEventDelegate(ConsoleEventCallback); SetConsoleCtrlHandler(handler, true); Thread.Sleep(400); var controllersManager = new Thread(() => ManageControllers(scpBus)); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); try { try { using (var pi = new ProcessIcon()) { pi.Display(); controllersManager.Start(); Application.Run(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Program Terminated Unexpectedly", MessageBoxButtons.OK, MessageBoxIcon.Error); } controllersManager.Abort(); scpBus.UnplugAll(); foreach (var device in Gamepads.Select(g => g.Device)) { device.CloseDevice(); } singleInstanceMutex.ReleaseMutex(); } finally { Environment.Exit(0); } }
public bool GetButtonDown(Gamepads gamepad, Buttons button) { if (!gamepads[(int)gamepad].playerIndexSet) { return(false); } bool ret = false; switch (button) { case Buttons.A: ret = gamepads[(int)gamepad].prevState.Buttons.A == ButtonState.Released && gamepads[(int)gamepad].state.Buttons.A == ButtonState.Pressed; break; case Buttons.B: ret = gamepads[(int)gamepad].prevState.Buttons.B == ButtonState.Released && gamepads[(int)gamepad].state.Buttons.B == ButtonState.Pressed; break; case Buttons.X: ret = gamepads[(int)gamepad].prevState.Buttons.X == ButtonState.Released && gamepads[(int)gamepad].state.Buttons.X == ButtonState.Pressed; break; case Buttons.Y: ret = gamepads[(int)gamepad].prevState.Buttons.Y == ButtonState.Released && gamepads[(int)gamepad].state.Buttons.Y == ButtonState.Pressed; break; case Buttons.DPad_up: ret = gamepads[(int)gamepad].prevState.DPad.Up == ButtonState.Released && gamepads[(int)gamepad].state.DPad.Up == ButtonState.Pressed; break; case Buttons.Dpad_down: ret = gamepads[(int)gamepad].prevState.DPad.Down == ButtonState.Released && gamepads[(int)gamepad].state.DPad.Down == ButtonState.Pressed; break; case Buttons.Dpad_left: ret = gamepads[(int)gamepad].prevState.DPad.Left == ButtonState.Released && gamepads[(int)gamepad].state.DPad.Left == ButtonState.Pressed; break; case Buttons.Dpad_right: ret = gamepads[(int)gamepad].prevState.DPad.Right == ButtonState.Released && gamepads[(int)gamepad].state.DPad.Right == ButtonState.Pressed; break; } return(ret); }
public int Input(int Port, int tact) { int returnvalue = 0xFF; if ((Port & 0xff) == 0x1f) { returnvalue = 0x0; this.Gamepad = Gamepads.FirstOrDefault(); if (Gamepad == null) { return(returnvalue); } //GamePadState currentState = GamePad.GetState(playerIndex); //000FUDLR if (Gamepad.Buttons.Count() > 0) { if (Gamepad.Buttons[0].Pressed) { returnvalue |= 16; } if ((Gamepad.Buttons.Count >= 12 && Gamepad.Buttons[12].Pressed) || (Gamepad.Axes.Count > 1 && Gamepad.Axes[1] > 0.5))//up { returnvalue |= 8; } if ((Gamepad.Buttons.Count >= 13 && Gamepad.Buttons[13].Pressed) || (Gamepad.Axes.Count > 1 && Gamepad.Axes[1] < -0.5))//down { returnvalue |= 4; } if ((Gamepad.Buttons.Count >= 14 && Gamepad.Buttons[14].Pressed) || (Gamepad.Axes.Count > 0 && Gamepad.Axes[0] < -0.5))//left { returnvalue |= 2; } if ((Gamepad.Buttons.Count >= 15 && Gamepad.Buttons[15].Pressed) || (Gamepad.Axes.Count > 0 && Gamepad.Axes[0] > 0.5))//right { returnvalue |= 1; } } } return(returnvalue); }
private static void ManageControllers(ScpBus scpBus) { var nrConnected = 0; //while (true) { var compatibleDevices = HidDevices.Enumerate(0x18D1, 0x9400).ToList(); var existingDevices = Gamepads.Select(g => g.Device).ToList(); var newDevices = compatibleDevices.Where(d => !existingDevices.Select(e => e.DevicePath).Contains(d.DevicePath)); foreach (var gamepad in Gamepads.ToList()) { if (!gamepad.check_connected()) { gamepad.unplug(); Gamepads.Remove(gamepad); } } foreach (var deviceInstance in newDevices) { var device = deviceInstance; try { device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.Exclusive); } catch { InformUser("Could not open gamepad in exclusive mode. Try reconnecting the device."); var instanceId = devicePathToInstanceId(deviceInstance.DevicePath); if (TryReEnableDevice(instanceId)) { try { device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.Exclusive); InformUser("Opened in exclusive mode."); } catch { device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.ShareRead | ShareMode.ShareWrite); InformUser("Opened in shared mode."); } } else { device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.ShareRead | ShareMode.ShareWrite); InformUser("Opened in shared mode."); } } //byte[] vibration = { 0x05, 0x00, 0x00, 0x00, 0x00 }; //if (device.Write(vibration) == false) //{ // InformUser("Could not write to gamepad (is it closed?), skipping"); // device.CloseDevice(); // continue; //} byte[] serialNumber; byte[] product; device.ReadSerialNumber(out serialNumber); device.ReadProduct(out product); var usedIndexes = Gamepads.Select(g => g.Index); var index = 1; while (usedIndexes.Contains(index)) { index++; } Gamepads.Add(new StadiaController(device, scpBus, index)); } if (Gamepads.Count != nrConnected) { InformUser($"{Gamepads.Count} controllers connected"); nrConnected = Gamepads.Count; } //Thread.Sleep(1000); } }
public void Update(float dt) { dt = Math.Min(dt, 0.1f); // Bind OpenGL m_window.MakeCurrent(); // Update input m_keyboard.Update(); m_mouse.Update(); m_gamepads.Update(); if (m_steamControllers != null) { m_steamControllers.Update(); } if (m_steamControllers != null) { // Choose an active steampad if (ActiveSteamController == null || !ActiveSteamController.Connected) { // No steampad connected if (User.Settings.EnableSteamController && m_currentState.EnableGamepad) { ActiveSteamController = SteamControllers.FirstOrDefault(pad => pad.Connected); if (ActiveSteamController != null && !(m_currentState is TestState)) { Screen.InputMethod = InputMethod.SteamController; } } else { ActiveSteamController = null; } } else { // Steampad connected if (!User.Settings.EnableSteamController || !m_currentState.EnableGamepad) { // Disable pad if necessary ActiveSteamController = null; } else { // Switch pad if Accept pressed foreach (var steampad in SteamControllers) { if (steampad.Connected && steampad.Buttons[SteamControllerButton.MenuSelect.GetID()].Pressed) { ActiveSteamController = steampad; Screen.InputMethod = InputMethod.SteamController; break; } } } } } // Choose an active gamepad if (ActiveGamepad == null || !ActiveGamepad.Connected) { // No Pad connected if (User.Settings.EnableGamepad && m_currentState.EnableGamepad) { ActiveGamepad = Gamepads.FirstOrDefault(pad => pad.Connected); if (ActiveGamepad != null) { ActiveGamepad.EnableRumble = User.Settings.EnableGamepadRumble; if (User.Settings.GamepadPromptType != GamepadType.Unknown) { ActiveGamepad.Type = User.Settings.GamepadPromptType; } if (!(m_currentState is TestState) && Screen.InputMethod != InputMethod.SteamController) { Screen.InputMethod = InputMethod.Gamepad; } } } else { ActiveGamepad = null; } } else { // Pad connected if (!User.Settings.EnableGamepad || !m_currentState.EnableGamepad) { // Disable pad if necessary ActiveGamepad = null; } else { // Switch pad if Start or A pressed foreach (var gamepad in Gamepads) { if (gamepad.Connected && gamepad.Buttons[GamepadButton.Start].Pressed || gamepad.Buttons[GamepadButton.A].Pressed) { ActiveGamepad = gamepad; ActiveGamepad.EnableRumble = User.Settings.EnableGamepadRumble; if (User.Settings.GamepadPromptType != GamepadType.Unknown) { ActiveGamepad.Type = User.Settings.GamepadPromptType; } if (Screen.InputMethod != InputMethod.SteamController) { Screen.InputMethod = InputMethod.Gamepad; } break; } } } } // Update tasks for (int i = m_promiseTasks.Count - 1; i >= 0; --i) { var task = m_promiseTasks[i]; if (task.Promise.Status != Status.Waiting) { task.OnComplete.Invoke(); m_promiseTasks.RemoveAt(i); } } // Update sound if (m_audio is OpenALAudio) { ((OpenALAudio)m_audio).Update(dt); } // Toggle fullscreen if (( (Keyboard.Keys[Key.LeftAlt].Held || Keyboard.Keys[Key.RightAlt].Held) && Keyboard.Keys[Key.Return].Pressed ) || ( App.Platform == Platform.OSX && (Keyboard.Keys[Key.LeftGUI].Held || Keyboard.Keys[Key.RightGUI].Held) && Keyboard.Keys[Key.W].Pressed )) { Window.Fullscreen = !Window.Fullscreen; User.Settings.Fullscreen = Window.Fullscreen; User.Settings.Save(); } // Update screen m_screen.Update(dt); // Update state if (m_pendingPendingState != null) { m_pendingState = m_pendingPendingState; m_pendingState.PreInit(m_currentState, m_pendingTransition); m_currentState.Shutdown(); m_pendingPendingState = null; m_transition = m_pendingTransition; m_transition.Init(m_currentState, m_pendingState); m_pendingTransition = null; } if (m_transition != null) { if (m_transition.Complete) { m_currentState.PostShutdown(); m_pendingState.Init(); m_transition.Shutdown(); m_currentState = m_pendingState; m_pendingState = null; m_transition = null; } else { m_currentState.PostUpdate(dt); m_pendingState.PreUpdate(dt); m_transition.Update(dt); } } if (m_transition == null) { m_currentState.Update(dt); m_currentState.PopulateCamera(Camera); } else { m_transition.PopulateCamera(Camera); } var cameraTransInv = Camera.Transform; MathUtils.FastInvert(ref cameraTransInv); Audio.Listener.Transform = cameraTransInv; m_cameraAxisMarker.Transform = cameraTransInv; // Update sky if (m_sky != null) { m_sky.Animate(); } // Update sound m_gameAudio.Update(); // Update debug camera if (UseDebugCamera) { m_debugCameraController.Update(dt); m_debugCameraController.Populate(Camera); } }
private static void ManageControllers(ScpBus scpBus) { var nrConnected = 0; while (true) { var compatibleDevices = HidDevices.Enumerate(0x2717, 0x3144).ToList(); var existingDevices = Gamepads.Select(g => g.Device).ToList(); var newDevices = compatibleDevices.Where(d => !existingDevices.Contains(d)); foreach (var deviceInstance in newDevices) { var device = deviceInstance; try { device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.Exclusive); } catch { InformUser("Could not open gamepad in exclusive mode. Try reconnecting the device."); var instanceId = devicePathToInstanceId(deviceInstance.DevicePath); if (TryReEnableDevice(instanceId)) { try { device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.Exclusive); //InformUser("Opened in exclusive mode."); } catch { device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.ShareRead | ShareMode.ShareWrite); //InformUser("Opened in shared mode."); } } else { device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.ShareRead | ShareMode.ShareWrite); //InformUser("Opened in shared mode."); } } byte[] vibration = { 0x20, 0x00, 0x00 }; if (device.WriteFeatureData(vibration) == false) { InformUser("Could not write to gamepad (is it closed?), skipping"); device.CloseDevice(); continue; } byte[] serialNumber; byte[] product; device.ReadSerialNumber(out serialNumber); device.ReadProduct(out product); Gamepads.Add(new Xiaomi_gamepad(device, scpBus, Gamepads.Count + 1)); } if (Gamepads.Count != nrConnected) { InformUser($"{Gamepads.Count} controllers connected"); } nrConnected = Gamepads.Count; if (nrConnected == 4) { Thread.Sleep(10000); continue; } Thread.Sleep(5000); } }