public void SendControllerButtons(HidControllerID ControllerId, HidControllerLayouts Layout, HidControllerKeys Buttons, JoystickPosition LeftJoystick, JoystickPosition RightJoystick) { uint InnerOffset = (uint)Marshal.SizeOf(typeof(HidSharedMemHeader)) + (uint)Marshal.SizeOf(typeof(HidTouchScreen)) + (uint)Marshal.SizeOf(typeof(HidMouse)) + (uint)Marshal.SizeOf(typeof(HidKeyboard)) + (uint)Marshal.SizeOf(typeof(HidUnknownSection1)) + (uint)Marshal.SizeOf(typeof(HidUnknownSection2)) + (uint)Marshal.SizeOf(typeof(HidUnknownSection3)) + (uint)Marshal.SizeOf(typeof(HidUnknownSection4)) + (uint)Marshal.SizeOf(typeof(HidUnknownSection5)) + (uint)Marshal.SizeOf(typeof(HidUnknownSection6)) + (uint)Marshal.SizeOf(typeof(HidUnknownSection7)) + (uint)Marshal.SizeOf(typeof(HidUnknownSection8)) + (uint)Marshal.SizeOf(typeof(HidControllerSerials)) + ((uint)(Marshal.SizeOf(typeof(HidController)) * (int)ControllerId)) + (uint)Marshal.SizeOf(typeof(HidControllerHeader)) + (uint)Layout * (uint)Marshal.SizeOf(typeof(HidControllerLayout)); IntPtr HidPtr = new IntPtr(Ns.Ram.ToInt64() + (uint)SharedMemOffset + InnerOffset); HidControllerLayoutHeader OldControllerHeaderLayout = (HidControllerLayoutHeader)Marshal.PtrToStructure(HidPtr, typeof(HidControllerLayoutHeader)); HidControllerLayoutHeader ControllerLayoutHeader = new HidControllerLayoutHeader { TimestampTicks = (ulong)Environment.TickCount, NumEntries = (ulong)Hid_Num_Entries, MaxEntryIndex = (ulong)Hid_Num_Entries - 1, LatestEntry = (OldControllerHeaderLayout.LatestEntry < (ulong)Hid_Num_Entries ? OldControllerHeaderLayout.LatestEntry + 1 : 0) }; Marshal.StructureToPtr(ControllerLayoutHeader, HidPtr, false); InnerOffset += (uint)Marshal.SizeOf(typeof(HidControllerLayoutHeader)) + (uint)((uint)(ControllerLayoutHeader.LatestEntry) * Marshal.SizeOf(typeof(HidControllerInputEntry))); HidPtr = new IntPtr(Ns.Ram.ToInt64() + (uint)SharedMemOffset + InnerOffset); HidControllerInputEntry ControllerInputEntry = new HidControllerInputEntry(); ControllerInputEntry.Timestamp = (ulong)Environment.TickCount; ControllerInputEntry.Timestamp_2 = (ulong)Environment.TickCount; ControllerInputEntry.Buttons = (ulong)Buttons; ControllerInputEntry.Joysticks = new JoystickPosition[(int)HidControllerJoystick.Joystick_Num_Sticks]; ControllerInputEntry.Joysticks[(int)HidControllerJoystick.Joystick_Left] = LeftJoystick; ControllerInputEntry.Joysticks[(int)HidControllerJoystick.Joystick_Right] = RightJoystick; ControllerInputEntry.ConnectionState = (ulong)(HidControllerConnectionState.Controller_State_Connected | HidControllerConnectionState.Controller_State_Wired); Marshal.StructureToPtr(ControllerInputEntry, HidPtr, false); }
private new void UpdateFrame() { HotkeyButtons currentHotkeyButtons = 0; ControllerButtons currentButton = 0; JoystickPosition leftJoystick; JoystickPosition rightJoystick; HLE.Input.Keyboard?hidKeyboard = null; int leftJoystickDx = 0; int leftJoystickDy = 0; int rightJoystickDx = 0; int rightJoystickDy = 0; // Keyboard Input if (_keyboard.HasValue) { KeyboardState keyboard = _keyboard.Value; #if USE_PROFILING // Profiler input, lets the profiler get access to the main windows keyboard state _profileWindow.UpdateKeyInput(keyboard); #endif // Normal Input currentHotkeyButtons = Configuration.Instance.KeyboardControls.GetHotkeyButtons(keyboard); currentButton = Configuration.Instance.KeyboardControls.GetButtons(keyboard); if (Configuration.Instance.EnableKeyboard) { hidKeyboard = Configuration.Instance.KeyboardControls.GetKeysDown(keyboard); } (leftJoystickDx, leftJoystickDy) = Configuration.Instance.KeyboardControls.GetLeftStick(keyboard); (rightJoystickDx, rightJoystickDy) = Configuration.Instance.KeyboardControls.GetRightStick(keyboard); } if (!hidKeyboard.HasValue) { hidKeyboard = new HLE.Input.Keyboard { Modifier = 0, Keys = new int[0x8] }; } currentButton |= Configuration.Instance.GamepadControls.GetButtons(); // Keyboard has priority stick-wise if (leftJoystickDx == 0 && leftJoystickDy == 0) { (leftJoystickDx, leftJoystickDy) = Configuration.Instance.GamepadControls.GetLeftStick(); } if (rightJoystickDx == 0 && rightJoystickDy == 0) { (rightJoystickDx, rightJoystickDy) = Configuration.Instance.GamepadControls.GetRightStick(); } leftJoystick = new JoystickPosition { Dx = leftJoystickDx, Dy = leftJoystickDy }; rightJoystick = new JoystickPosition { Dx = rightJoystickDx, Dy = rightJoystickDy }; currentButton |= _device.Hid.UpdateStickButtons(leftJoystick, rightJoystick); 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 (Focused && _mouse?.LeftButton == ButtonState.Pressed) { MouseState mouse = _mouse.Value; int scrnWidth = Width; int scrnHeight = Height; if (Width > (Height * TouchScreenWidth) / TouchScreenHeight) { scrnWidth = (Height * TouchScreenWidth) / TouchScreenHeight; } else { scrnHeight = (Width * TouchScreenHeight) / TouchScreenWidth; } int startX = (Width - scrnWidth) >> 1; int startY = (Height - scrnHeight) >> 1; int endX = startX + scrnWidth; int endY = startY + scrnHeight; if (mouse.X >= startX && mouse.Y >= startY && mouse.X < endX && mouse.Y < endY) { int scrnMouseX = mouse.X - startX; int scrnMouseY = mouse.Y - startY; int mX = (scrnMouseX * TouchScreenWidth) / scrnWidth; int mY = (scrnMouseY * TouchScreenHeight) / scrnHeight; TouchPoint currentPoint = new TouchPoint { X = mX, Y = mY, // Placeholder values till more data is acquired DiameterX = 10, DiameterY = 10, Angle = 90 }; hasTouch = true; _device.Hid.SetTouchPoints(currentPoint); } } if (!hasTouch) { _device.Hid.SetTouchPoints(); } if (Configuration.Instance.EnableKeyboard && hidKeyboard.HasValue) { _device.Hid.WriteKeyboard(hidKeyboard.Value); } BaseController controller = _device.Hid.PrimaryController; controller.SendInput(currentButton, leftJoystick, rightJoystick); // Toggle vsync if (currentHotkeyButtons.HasFlag(HotkeyButtons.ToggleVSync) && !_prevHotkeyButtons.HasFlag(HotkeyButtons.ToggleVSync)) { _device.EnableDeviceVsync = !_device.EnableDeviceVsync; } _prevHotkeyButtons = currentHotkeyButtons; }
protected override void OnUpdateFrame(FrameEventArgs e) { HidControllerKeys CurrentButton = 0; JoystickPosition LeftJoystick; JoystickPosition RightJoystick; if (Keyboard[OpenTK.Input.Key.Escape]) { this.Exit(); } //RightJoystick int LeftJoystickDX = 0; int LeftJoystickDY = 0; if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickUp]) { LeftJoystickDY = short.MaxValue; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickDown]) { LeftJoystickDY = -short.MaxValue; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickLeft]) { LeftJoystickDX = -short.MaxValue; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickRight]) { LeftJoystickDX = short.MaxValue; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickButton]) { CurrentButton |= HidControllerKeys.KEY_LSTICK; } //LeftButtons if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadUp]) { CurrentButton |= HidControllerKeys.KEY_DUP; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadDown]) { CurrentButton |= HidControllerKeys.KEY_DDOWN; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadLeft]) { CurrentButton |= HidControllerKeys.KEY_DLEFT; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadRight]) { CurrentButton |= HidControllerKeys.KEY_DRIGHT; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.ButtonMinus]) { CurrentButton |= HidControllerKeys.KEY_MINUS; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.ButtonL]) { CurrentButton |= HidControllerKeys.KEY_L; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.ButtonZL]) { CurrentButton |= HidControllerKeys.KEY_ZL; } //RightJoystick int RightJoystickDX = 0; int RightJoystickDY = 0; if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickUp]) { RightJoystickDY = short.MaxValue; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickDown]) { RightJoystickDY = -short.MaxValue; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickLeft]) { RightJoystickDX = -short.MaxValue; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickRight]) { RightJoystickDX = short.MaxValue; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickButton]) { CurrentButton |= HidControllerKeys.KEY_RSTICK; } //RightButtons if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonA]) { CurrentButton |= HidControllerKeys.KEY_A; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonB]) { CurrentButton |= HidControllerKeys.KEY_B; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonX]) { CurrentButton |= HidControllerKeys.KEY_X; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonY]) { CurrentButton |= HidControllerKeys.KEY_Y; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonPlus]) { CurrentButton |= HidControllerKeys.KEY_PLUS; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonR]) { CurrentButton |= HidControllerKeys.KEY_R; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonZR]) { CurrentButton |= HidControllerKeys.KEY_ZR; } LeftJoystick = new JoystickPosition { DX = LeftJoystickDX, DY = LeftJoystickDY }; RightJoystick = new JoystickPosition { DX = RightJoystickDX, DY = RightJoystickDY }; //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 (Mouse != null && Focused) { if (Mouse.GetState().LeftButton == OpenTK.Input.ButtonState.Pressed) { HidTouchScreenEntryTouch CurrentPoint = new HidTouchScreenEntryTouch { Timestamp = (uint)Environment.TickCount, X = (uint)Mouse.X, Y = (uint)Mouse.Y, //Placeholder values till more data is acquired DiameterX = 10, DiameterY = 10, Angle = 90, //Only support single touch TouchIndex = 0, }; if (Mouse.X > -1 && Mouse.Y > -1) { Ns.SendTouchScreenEntry(CurrentPoint); } } } //We just need one pair of JoyCon because it's emulate by the keyboard. Ns.SendControllerButtons(HidControllerID.CONTROLLER_HANDHELD, HidControllerLayouts.Main, CurrentButton, LeftJoystick, RightJoystick); }
protected override void OnUpdateFrame(FrameEventArgs e) { HidControllerKeys CurrentButton = 0; JoystickPosition LeftJoystick; JoystickPosition RightJoystick; if (Keyboard[OpenTK.Input.Key.Escape]) { this.Exit(); } //RightJoystick int LeftJoystickDX = 0; int LeftJoystickDY = 0; if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickUp]) { LeftJoystickDY = short.MaxValue; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickDown]) { LeftJoystickDY = -short.MaxValue; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickLeft]) { LeftJoystickDX = -short.MaxValue; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickRight]) { LeftJoystickDX = short.MaxValue; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickButton]) { CurrentButton |= HidControllerKeys.KEY_LSTICK; } //LeftButtons if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadUp]) { CurrentButton |= HidControllerKeys.KEY_DUP; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadDown]) { CurrentButton |= HidControllerKeys.KEY_DDOWN; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadLeft]) { CurrentButton |= HidControllerKeys.KEY_DLEFT; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadRight]) { CurrentButton |= HidControllerKeys.KEY_DRIGHT; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.ButtonMinus]) { CurrentButton |= HidControllerKeys.KEY_MINUS; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.ButtonL]) { CurrentButton |= HidControllerKeys.KEY_L; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.ButtonZL]) { CurrentButton |= HidControllerKeys.KEY_ZL; } //RightJoystick int RightJoystickDX = 0; int RightJoystickDY = 0; if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickUp]) { RightJoystickDY = short.MaxValue; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickDown]) { RightJoystickDY = -short.MaxValue; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickLeft]) { RightJoystickDX = -short.MaxValue; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickRight]) { RightJoystickDX = short.MaxValue; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickButton]) { CurrentButton |= HidControllerKeys.KEY_RSTICK; } //RightButtons if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonA]) { CurrentButton |= HidControllerKeys.KEY_A; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonB]) { CurrentButton |= HidControllerKeys.KEY_B; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonX]) { CurrentButton |= HidControllerKeys.KEY_X; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonY]) { CurrentButton |= HidControllerKeys.KEY_Y; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonPlus]) { CurrentButton |= HidControllerKeys.KEY_PLUS; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonR]) { CurrentButton |= HidControllerKeys.KEY_R; } if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonZR]) { CurrentButton |= HidControllerKeys.KEY_ZR; } LeftJoystick = new JoystickPosition { DX = LeftJoystickDX, DY = LeftJoystickDY }; RightJoystick = new JoystickPosition { DX = RightJoystickDX, DY = RightJoystickDY }; //We just need one pair of JoyCon because it's emulate by the keyboard. Ns.Hid.SendControllerButtons(HidControllerID.CONTROLLER_HANDHELD, HidControllerLayouts.Main, CurrentButton, LeftJoystick, RightJoystick); }