public void Update() { lock (_lock) { List <GamepadInput> hleInputStates = new List <GamepadInput>(); List <SixAxisInput> hleMotionStates = new List <SixAxisInput>(NpadDevices.MaxControllers); KeyboardInput?hleKeyboardInput = null; foreach (InputConfig inputConfig in _inputConfig) { GamepadInput inputState = default; SixAxisInput motionState = default; NpadController controller = _controllers[(int)inputConfig.PlayerIndex]; // Do we allow input updates and is a controller connected? if (!_blockInputUpdates && controller != null) { DriverConfigurationUpdate(ref controller, inputConfig); controller.UpdateUserConfiguration(inputConfig); controller.Update(); inputState = controller.GetHLEInputState(); inputState.Buttons |= _device.Hid.UpdateStickButtons(inputState.LStick, inputState.RStick); motionState = controller.GetHLEMotionState(); if (_enableKeyboard) { hleKeyboardInput = controller.GetHLEKeyboardInput(); } } else { // Ensure that orientation isn't null motionState.Orientation = new float[9]; } inputState.PlayerId = (Ryujinx.HLE.HOS.Services.Hid.PlayerIndex)inputConfig.PlayerIndex; motionState.PlayerId = (Ryujinx.HLE.HOS.Services.Hid.PlayerIndex)inputConfig.PlayerIndex; hleInputStates.Add(inputState); hleMotionStates.Add(motionState); } _device.Hid.Npads.Update(hleInputStates); _device.Hid.Npads.UpdateSixAxis(hleMotionStates); if (hleKeyboardInput.HasValue) { _device.Hid.Keyboard.Update(hleKeyboardInput.Value); } _device.TamperMachine.UpdateInput(hleInputStates); } }
private bool UpdateFrame() { if (!_isActive) { return(true); } if (_isStopped) { return(false); } if (_isFocused) { Gtk.Application.Invoke(delegate { KeyboardState keyboard = OpenTK.Input.Keyboard.GetState(); HandleScreenState(keyboard); if (keyboard.IsKeyDown(OpenTK.Input.Key.Delete)) { if (!ParentWindow.State.HasFlag(Gdk.WindowState.Fullscreen)) { Ptc.Continue(); } } }); } List <GamepadInput> gamepadInputs = new List <GamepadInput>(NpadDevices.MaxControllers); List <SixAxisInput> motionInputs = new List <SixAxisInput>(NpadDevices.MaxControllers); MotionDevice motionDevice = new MotionDevice(_dsuClient); foreach (InputConfig inputConfig in ConfigurationState.Instance.Hid.InputConfig.Value) { ControllerKeys currentButton = 0; JoystickPosition leftJoystick = new JoystickPosition(); JoystickPosition rightJoystick = new JoystickPosition(); KeyboardInput? hidKeyboard = null; int leftJoystickDx = 0; int leftJoystickDy = 0; int rightJoystickDx = 0; int rightJoystickDy = 0; if (inputConfig.EnableMotion) { motionDevice.RegisterController(inputConfig.PlayerIndex); } if (inputConfig is KeyboardConfig keyboardConfig) { if (_isFocused) { // Keyboard Input KeyboardController keyboardController = new KeyboardController(keyboardConfig); currentButton = keyboardController.GetButtons(); (leftJoystickDx, leftJoystickDy) = keyboardController.GetLeftStick(); (rightJoystickDx, rightJoystickDy) = keyboardController.GetRightStick(); leftJoystick = new JoystickPosition { Dx = leftJoystickDx, Dy = leftJoystickDy }; rightJoystick = new JoystickPosition { Dx = rightJoystickDx, Dy = rightJoystickDy }; if (ConfigurationState.Instance.Hid.EnableKeyboard) { hidKeyboard = keyboardController.GetKeysDown(); } if (!hidKeyboard.HasValue) { hidKeyboard = new KeyboardInput { Modifier = 0, Keys = new int[0x8] }; } if (ConfigurationState.Instance.Hid.EnableKeyboard) { _device.Hid.Keyboard.Update(hidKeyboard.Value); } } } else if (inputConfig is Common.Configuration.Hid.ControllerConfig controllerConfig) { // Controller Input JoystickController joystickController = new JoystickController(controllerConfig); currentButton |= joystickController.GetButtons(); (leftJoystickDx, leftJoystickDy) = joystickController.GetLeftStick(); (rightJoystickDx, rightJoystickDy) = joystickController.GetRightStick(); leftJoystick = new JoystickPosition { Dx = controllerConfig.LeftJoycon.InvertStickX ? -leftJoystickDx : leftJoystickDx, Dy = controllerConfig.LeftJoycon.InvertStickY ? -leftJoystickDy : leftJoystickDy }; rightJoystick = new JoystickPosition { Dx = controllerConfig.RightJoycon.InvertStickX ? -rightJoystickDx : rightJoystickDx, Dy = controllerConfig.RightJoycon.InvertStickY ? -rightJoystickDy : rightJoystickDy }; } currentButton |= _device.Hid.UpdateStickButtons(leftJoystick, rightJoystick); motionDevice.Poll(inputConfig, inputConfig.Slot); SixAxisInput sixAxisInput = new SixAxisInput() { PlayerId = (HLE.HOS.Services.Hid.PlayerIndex)inputConfig.PlayerIndex, Accelerometer = motionDevice.Accelerometer, Gyroscope = motionDevice.Gyroscope, Rotation = motionDevice.Rotation, Orientation = motionDevice.Orientation }; motionInputs.Add(sixAxisInput); gamepadInputs.Add(new GamepadInput { PlayerId = (HLE.HOS.Services.Hid.PlayerIndex)inputConfig.PlayerIndex, Buttons = currentButton, LStick = leftJoystick, RStick = rightJoystick }); if (inputConfig.ControllerType == Common.Configuration.Hid.ControllerType.JoyconPair) { if (!inputConfig.MirrorInput) { motionDevice.Poll(inputConfig, inputConfig.AltSlot); sixAxisInput = new SixAxisInput() { PlayerId = (HLE.HOS.Services.Hid.PlayerIndex)inputConfig.PlayerIndex, Accelerometer = motionDevice.Accelerometer, Gyroscope = motionDevice.Gyroscope, Rotation = motionDevice.Rotation, Orientation = motionDevice.Orientation }; } motionInputs.Add(sixAxisInput); } } _device.Hid.Npads.Update(gamepadInputs); _device.Hid.Npads.UpdateSixAxis(motionInputs); _device.TamperMachine.UpdateInput(gamepadInputs); if (_isFocused) { // Hotkeys HotkeyButtons currentHotkeyButtons = KeyboardController.GetHotkeyButtons(OpenTK.Input.Keyboard.GetState()); if (currentHotkeyButtons.HasFlag(HotkeyButtons.ToggleVSync) && !_prevHotkeyButtons.HasFlag(HotkeyButtons.ToggleVSync)) { _device.EnableDeviceVsync = !_device.EnableDeviceVsync; } _prevHotkeyButtons = currentHotkeyButtons; } //Touchscreen bool hasTouch = false; // Get screen touch position from left mouse click // OpenTK always captures mouse events, even if out of focus, so check if window is focused. if (_isFocused && _mousePressed) { float aspectWidth = SwitchPanelHeight * ConfigurationState.Instance.Graphics.AspectRatio.Value.ToFloat(); int screenWidth = AllocatedWidth; int screenHeight = AllocatedHeight; if (AllocatedWidth > AllocatedHeight * aspectWidth / SwitchPanelHeight) { screenWidth = (int)(AllocatedHeight * aspectWidth) / SwitchPanelHeight; } else { screenHeight = (AllocatedWidth * SwitchPanelHeight) / (int)aspectWidth; } int startX = (AllocatedWidth - screenWidth) >> 1; int startY = (AllocatedHeight - screenHeight) >> 1; int endX = startX + screenWidth; int endY = startY + screenHeight; if (_mouseX >= startX && _mouseY >= startY && _mouseX < endX && _mouseY < endY) { int screenMouseX = (int)_mouseX - startX; int screenMouseY = (int)_mouseY - startY; int mX = (screenMouseX * (int)aspectWidth) / screenWidth; int mY = (screenMouseY * SwitchPanelHeight) / screenHeight; TouchPoint currentPoint = new TouchPoint { X = (uint)mX, Y = (uint)mY, // Placeholder values till more data is acquired DiameterX = 10, DiameterY = 10, Angle = 90 }; hasTouch = true; _device.Hid.Touchscreen.Update(currentPoint); } } if (!hasTouch) { _device.Hid.Touchscreen.Update(); } _device.Hid.DebugPad.Update(); return(true); }
public void Update(float aspectRatio = 0) { lock (_lock) { List <GamepadInput> hleInputStates = new List <GamepadInput>(); List <SixAxisInput> hleMotionStates = new List <SixAxisInput>(NpadDevices.MaxControllers); KeyboardInput?hleKeyboardInput = null; foreach (InputConfig inputConfig in _inputConfig) { GamepadInput inputState = default; SixAxisInput motionState = default; NpadController controller = _controllers[(int)inputConfig.PlayerIndex]; Ryujinx.HLE.HOS.Services.Hid.PlayerIndex playerIndex = (Ryujinx.HLE.HOS.Services.Hid.PlayerIndex)inputConfig.PlayerIndex; // Do we allow input updates and is a controller connected? if (!_blockInputUpdates && controller != null) { DriverConfigurationUpdate(ref controller, inputConfig); controller.UpdateUserConfiguration(inputConfig); controller.Update(); if (!_device.Hid.Npads.RumbleQueues.TryGetValue(playerIndex, out ConcurrentQueue <(HidVibrationValue, HidVibrationValue)> rumbleQueue)) { rumbleQueue = new ConcurrentQueue <(HidVibrationValue, HidVibrationValue)>(); _device.Hid.Npads.RumbleQueues[playerIndex] = rumbleQueue; } controller.UpdateRumble(rumbleQueue); inputState = controller.GetHLEInputState(); inputState.Buttons |= _device.Hid.UpdateStickButtons(inputState.LStick, inputState.RStick); motionState = controller.GetHLEMotionState(); if (_enableKeyboard) { hleKeyboardInput = controller.GetHLEKeyboardInput(); } } else { // Ensure that orientation isn't null motionState.Orientation = new float[9]; } inputState.PlayerId = playerIndex; motionState.PlayerId = playerIndex; hleInputStates.Add(inputState); hleMotionStates.Add(motionState); } _device.Hid.Npads.Update(hleInputStates); _device.Hid.Npads.UpdateSixAxis(hleMotionStates); if (hleKeyboardInput.HasValue) { _device.Hid.Keyboard.Update(hleKeyboardInput.Value); } if (_enableMouse) { var mouse = _mouseDriver.GetGamepad("0") as IMouse; var mouseInput = IMouse.GetMouseStateSnapshot(mouse); uint buttons = 0; if (mouseInput.IsPressed(MouseButton.Button1)) { buttons |= 1 << 0; } if (mouseInput.IsPressed(MouseButton.Button2)) { buttons |= 1 << 1; } if (mouseInput.IsPressed(MouseButton.Button3)) { buttons |= 1 << 2; } if (mouseInput.IsPressed(MouseButton.Button4)) { buttons |= 1 << 3; } if (mouseInput.IsPressed(MouseButton.Button5)) { buttons |= 1 << 4; } var position = IMouse.GetScreenPosition(mouseInput.Position, mouse.ClientSize, aspectRatio); _device.Hid.Mouse.Update((int)position.X, (int)position.Y, buttons, (int)mouseInput.Scroll.X, (int)mouseInput.Scroll.Y, true); } else { _device.Hid.Mouse.Update(0, 0); } _device.TamperMachine.UpdateInput(hleInputStates); } }