Example #1
0
        private new void UpdateFrame()
        {
            HidHotkeyButtons     currentHotkeyButtons = 0;
            HidControllerButtons currentButton        = 0;
            HidJoystickPosition  leftJoystick;
            HidJoystickPosition  rightJoystick;
            HidKeyboard?         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 HidKeyboard
                {
                    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 HidJoystickPosition
            {
                Dx = leftJoystickDx,
                Dy = leftJoystickDy
            };

            rightJoystick = new HidJoystickPosition
            {
                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;

                    HidTouchPoint currentPoint = new HidTouchPoint
                    {
                        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);
            }

            HidControllerBase controller = _device.Hid.PrimaryController;

            controller.SendInput(currentButton, leftJoystick, rightJoystick);

            // Toggle vsync
            if (currentHotkeyButtons.HasFlag(HidHotkeyButtons.ToggleVSync) &&
                !_prevHotkeyButtons.HasFlag(HidHotkeyButtons.ToggleVSync))
            {
                _device.EnableDeviceVsync = !_device.EnableDeviceVsync;
            }

            _prevHotkeyButtons = currentHotkeyButtons;
        }
Example #2
0
        public void Init(long HidOffset)
        {
            unsafe
            {
                if (HidOffset == 0 || HidOffset + Horizon.HidSize > uint.MaxValue)
                {
                    return;
                }

                SharedMemOffset = HidOffset;

                uint InnerOffset = (uint)Marshal.SizeOf(typeof(HidSharedMemHeader));

                IntPtr HidPtr = new IntPtr(Ns.Ram.ToInt64() + (uint)SharedMemOffset + InnerOffset);

                HidTouchScreen TouchScreen = new HidTouchScreen();
                TouchScreen.Header.TimestampTicks = (ulong)Environment.TickCount;
                TouchScreen.Header.NumEntries     = (ulong)Hid_Num_Entries;
                TouchScreen.Header.LatestEntry    = 0;
                TouchScreen.Header.MaxEntryIndex  = (ulong)Hid_Num_Entries - 1;
                TouchScreen.Header.Timestamp      = (ulong)Environment.TickCount;

                //TODO: Write this structure when the input is implemented
                //Marshal.StructureToPtr(TouchScreen, HidPtr, false);

                InnerOffset += (uint)Marshal.SizeOf(typeof(HidTouchScreen));
                HidPtr       = new IntPtr(Ns.Ram.ToInt64() + (uint)SharedMemOffset + InnerOffset);

                HidMouse Mouse = new HidMouse();
                Mouse.Header.TimestampTicks = (ulong)Environment.TickCount;
                Mouse.Header.NumEntries     = (ulong)Hid_Num_Entries;
                Mouse.Header.LatestEntry    = 0;
                Mouse.Header.MaxEntryIndex  = (ulong)Hid_Num_Entries - 1;

                //TODO: Write this structure when the input is implemented
                //Marshal.StructureToPtr(Mouse, HidPtr, false);

                InnerOffset += (uint)Marshal.SizeOf(typeof(HidMouse));
                HidPtr       = new IntPtr(Ns.Ram.ToInt64() + (uint)SharedMemOffset + InnerOffset);

                HidKeyboard Keyboard = new HidKeyboard();
                Keyboard.Header.TimestampTicks = (ulong)Environment.TickCount;
                Keyboard.Header.NumEntries     = (ulong)Hid_Num_Entries;
                Keyboard.Header.LatestEntry    = 0;
                Keyboard.Header.MaxEntryIndex  = (ulong)Hid_Num_Entries - 1;

                //TODO: Write this structure when the input is implemented
                //Marshal.StructureToPtr(Keyboard, HidPtr, false);

                InnerOffset += (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));

                //Increase the loop to initialize more controller.
                for (int i = 8; i < Enum.GetNames(typeof(HidControllerID)).Length - 1; i++)
                {
                    HidPtr = new IntPtr(Ns.Ram.ToInt64() + (uint)SharedMemOffset + InnerOffset + (uint)(Marshal.SizeOf(typeof(HidController)) * i));

                    HidController Controller = new HidController();
                    Controller.Header.Type   = (uint)(HidControllerType.ControllerType_Handheld | HidControllerType.ControllerType_JoyconPair);
                    Controller.Header.IsHalf = 0;
                    Controller.Header.SingleColorsDescriptor = (uint)(HidControllerColorDescription.ColorDesc_ColorsNonexistent);
                    Controller.Header.SingleColorBody        = 0;
                    Controller.Header.SingleColorButtons     = 0;
                    Controller.Header.SplitColorsDescriptor  = 0;
                    Controller.Header.LeftColorBody          = (uint)JoyConColor.Body_Neon_Red;
                    Controller.Header.LeftColorButtons       = (uint)JoyConColor.Buttons_Neon_Red;
                    Controller.Header.RightColorBody         = (uint)JoyConColor.Body_Neon_Blue;
                    Controller.Header.RightColorButtons      = (uint)JoyConColor.Buttons_Neon_Blue;

                    Controller.Layouts = new HidControllerLayout[Enum.GetNames(typeof(HidControllerLayouts)).Length];
                    Controller.Layouts[(int)HidControllerLayouts.Main] = new HidControllerLayout();
                    Controller.Layouts[(int)HidControllerLayouts.Main].Header.LatestEntry = (ulong)Hid_Num_Entries;

                    Marshal.StructureToPtr(Controller, HidPtr, false);
                }

                Logging.Info("HID Initialized!");
            }
        }