Beispiel #1
0
        public void AddKeyToMappingBuffer(int controllerIndex, ControllerKeys controllerKey, Key newKey)
        {
            var controller = Controllers[controllerIndex];

            // remove old mappings
            var toRemove =
                MappingsBuffer
                .Where(pair => (pair.Value.Controller == controller &&
                                pair.Value.ControllerKey == controllerKey) || pair.Key == newKey)
                .Select(pair => pair.Key)
                .ToList();

            foreach (var key in toRemove)
            {
                MappingsBuffer.Remove(key);
            }

            MappingsBuffer.Add(
                newKey,
                new ControllerKeyInfo
            {
                Controller    = controller,
                ControllerKey = controllerKey
            }
                );
        }
Beispiel #2
0
        public bool GetKeyStatus(ControllerKeys key)
        {
            switch (key)
            {
            case ControllerKeys.Up:
                return(Up);

            case ControllerKeys.Down:
                return(Down);

            case ControllerKeys.Left:
                return(Left);

            case ControllerKeys.Right:
                return(Right);

            case ControllerKeys.Select:
                return(Select);

            case ControllerKeys.Start:
                return(Start);

            case ControllerKeys.A:
                return(A);

            case ControllerKeys.B:
                return(B);
            }
            return(false);
        }
Beispiel #3
0
 public void Execute(ControllerKeys pressedKeys)
 {
     if (IsEnabled)
     {
         _pressedKeys.Value = (long)pressedKeys;
         _entryPoint.Execute();
     }
 }
Beispiel #4
0
        public void AddKeyMapping(ControllerKeys controllerKey, Key newKey)
        {
            // add to local list
            Mappings[controllerKey] = newKey;
            dataGrid.Items.Refresh();

            // add to controller manager buffer
            ControllerManager.Instance.AddKeyToMappingBuffer(ControllerIndex, controllerKey, newKey);
        }
Beispiel #5
0
        /// <summary>
        /// Converts a virtual key to the corresponding hardware bits.
        /// </summary>
        /// <param name="key">The controller key for which hardware bits are desired.</param>
        /// <returns>The hardware bits.</returns>
        public static byte ToHardwareBits(this ControllerKeys key)
        {
            byte hardwareBits = 0;

            if (!key.HasFlag(ControllerKeys.NoneActive))
            {
                ControllerKeyHardwareBits.TryGetValue(key, out hardwareBits);
            }
            return(hardwareBits);
        }
Beispiel #6
0
        /// <summary>
        /// Gets a display string for a specific controller virtual key.
        /// </summary>
        /// <param name="key">The controller key.</param>
        /// <returns>The string to display.</returns>
        public static string ToDisplayString(this ControllerKeys key)
        {
            string keyName = null;

            if (!ControllerKeyNames.TryGetValue(key, out keyName))
            {
                keyName = string.Empty;
            }
            return(keyName);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="INTV.Shared.ViewModel.ControllerElementViewModel"/> class.
        /// </summary>
        /// <param name="key">The key represented by this instance.</param>
        public ControllerElementViewModel(ControllerKeys key)
        {
            Key  = key;
            Name = (key & ~ControllerKeys.NoneActive).ToDisplayString();
            string imageResource;

            if (ResourceStrings.TryGetValue(key, out imageResource))
            {
                Image = typeof(ControllerElementViewModel).LoadImageResource("ViewModel/Resources/Images/" + imageResource);
            }
        }
Beispiel #8
0
        public Controller(RenderWindow window, VirtualMachine virtualMachine, XElement config)
        {
            state = ControllerKeys.Presence;
            KeyBindings = new Dictionary<ControllerKeys, KeyboardKey>();

            var errorMsg = "";

            try
            {
                errorMsg = "Bad Port";
                devPort = short.Parse(Util.ElementValue(config, "Port", null));

                errorMsg = "Bad Key";
                KeyBindings[ControllerKeys.Up] = Util.EnumParse<KeyboardKey>(Util.ElementValue(config, "Up", "Up"));
                KeyBindings[ControllerKeys.Down] = Util.EnumParse<KeyboardKey>(Util.ElementValue(config, "Down", "Down"));
                KeyBindings[ControllerKeys.Left] = Util.EnumParse<KeyboardKey>(Util.ElementValue(config, "Left", "Left"));
                KeyBindings[ControllerKeys.Right] = Util.EnumParse<KeyboardKey>(Util.ElementValue(config, "Right", "Right"));
                KeyBindings[ControllerKeys.A] = Util.EnumParse<KeyboardKey>(Util.ElementValue(config, "A", "A"));
                KeyBindings[ControllerKeys.B] = Util.EnumParse<KeyboardKey>(Util.ElementValue(config, "B", "S"));
                KeyBindings[ControllerKeys.C] = Util.EnumParse<KeyboardKey>(Util.ElementValue(config, "C", "D"));
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("Controller: {0}", errorMsg), e);
            }

            window.KeyPressed += (sender, e) =>
            {
                foreach (var binding in KeyBindings)
                {
                    if (e.Code == binding.Value)
                    {
                        if (!state.HasFlag(binding.Key))
                            state |= binding.Key;
                    }
                }
            };

            window.KeyReleased += (sender, e) =>
            {
                foreach (var binding in KeyBindings)
                {
                    if (e.Code == binding.Value)
                    {
                        if (state.HasFlag(binding.Key))
                            state &= ~binding.Key;
                    }
                }
            };
        }
Beispiel #9
0
        private bool AdvanceTamperingsQueue()
        {
            if (!_programs.TryDequeue(out ITamperProgram program))
            {
                // No more programs in the queue.
                _programDictionary.Clear();

                return(false);
            }

            // Check if the process is still suitable for running the tamper program.
            if (!IsProcessValid(program.Process))
            {
                // Exit without re-enqueuing the program because the process is no longer valid.
                return(true);
            }

            // Re-enqueue the tampering program because the process is still valid.
            _programs.Enqueue(program);

            Logger.Debug?.Print(LogClass.TamperMachine, $"Running tampering program {program.Name}");

            try
            {
                ControllerKeys pressedKeys = (ControllerKeys)Thread.VolatileRead(ref _pressedKeys);
                program.Process.TamperedCodeMemory = false;
                program.Execute(pressedKeys);

                // Detect the first attempt to tamper memory and log it.
                if (!program.TampersCodeMemory && program.Process.TamperedCodeMemory)
                {
                    program.TampersCodeMemory = true;

                    Logger.Warning?.Print(LogClass.TamperMachine, $"Tampering program {program.Name} modifies code memory so it may not work properly");
                }
            }
            catch (Exception ex)
            {
                Logger.Debug?.Print(LogClass.TamperMachine, $"The tampering program {program.Name} crashed, this can happen while the game is starting");

                if (!string.IsNullOrEmpty(ex.Message))
                {
                    Logger.Debug?.Print(LogClass.TamperMachine, ex.Message);
                }
            }

            return(true);
        }
Beispiel #10
0
        private bool AdvanceTamperingsQueue()
        {
            if (!_programs.TryDequeue(out ITamperProgram program))
            {
                // No more programs in the queue.
                return(false);
            }

            // Check if the process is still suitable for running the tamper program.
            if (!IsProcessValid(program.Process))
            {
                // Exit without re-enqueuing the program because the process is no longer valid.
                return(true);
            }

            // Re-enqueue the tampering program because the process is still valid.
            _programs.Enqueue(program);

            Logger.Debug?.Print(LogClass.TamperMachine, "Running tampering program");

            try
            {
                ControllerKeys pressedKeys = (ControllerKeys)Thread.VolatileRead(ref _pressedKeys);
                program.Execute(pressedKeys);
            }
            catch (CodeRegionTamperedException ex)
            {
                Logger.Debug?.Print(LogClass.TamperMachine, $"Prevented tampering program from modifing code memory");

                if (!String.IsNullOrEmpty(ex.Message))
                {
                    Logger.Debug?.Print(LogClass.TamperMachine, ex.Message);
                }
            }
            catch (Exception ex)
            {
                Logger.Debug?.Print(LogClass.TamperMachine, $"The tampering program crashed, this can happen while the game is starting");

                if (!String.IsNullOrEmpty(ex.Message))
                {
                    Logger.Debug?.Print(LogClass.TamperMachine, ex.Message);
                }
            }

            return(true);
        }
Beispiel #11
0
        public void SetKeyStatus(ControllerKeys key, bool value)
        {
            switch (key)
            {
            case ControllerKeys.Up:
                Up = value;
                break;

            case ControllerKeys.Down:
                Down = value;
                break;

            case ControllerKeys.Left:
                Left = value;
                break;

            case ControllerKeys.Right:
                Right = value;
                break;

            case ControllerKeys.Select:
                Select = value;
                break;

            case ControllerKeys.Start:
                Start = value;
                break;

            case ControllerKeys.A:
                A = value;
                break;

            case ControllerKeys.B:
                B = value;
                break;
            }
        }
Beispiel #12
0
 public HLEButtonMappingEntry(GamepadButtonInputId driverInputId, ControllerKeys hleInput)
 {
     DriverInputId = driverInputId;
     HLEInput      = hleInput;
 }
Beispiel #13
0
        public static ControllerKeys GetButtons(NpadKeyboard npad, KeyboardState keyboard)
        {
            ControllerKeys buttons = 0;

            if (keyboard[(Key)npad.LeftJoycon.StickButton])
            {
                buttons |= ControllerKeys.LStick;
            }
            if (keyboard[(Key)npad.LeftJoycon.DPadUp])
            {
                buttons |= ControllerKeys.DpadUp;
            }
            if (keyboard[(Key)npad.LeftJoycon.DPadDown])
            {
                buttons |= ControllerKeys.DpadDown;
            }
            if (keyboard[(Key)npad.LeftJoycon.DPadLeft])
            {
                buttons |= ControllerKeys.DpadLeft;
            }
            if (keyboard[(Key)npad.LeftJoycon.DPadRight])
            {
                buttons |= ControllerKeys.DpadRight;
            }
            if (keyboard[(Key)npad.LeftJoycon.ButtonMinus])
            {
                buttons |= ControllerKeys.Minus;
            }
            if (keyboard[(Key)npad.LeftJoycon.ButtonL])
            {
                buttons |= ControllerKeys.L | ControllerKeys.Sl;
            }
            if (keyboard[(Key)npad.LeftJoycon.ButtonZl])
            {
                buttons |= ControllerKeys.Zl;
            }

            if (keyboard[(Key)npad.RightJoycon.StickButton])
            {
                buttons |= ControllerKeys.RStick;
            }
            if (keyboard[(Key)npad.RightJoycon.ButtonA])
            {
                buttons |= ControllerKeys.A;
            }
            if (keyboard[(Key)npad.RightJoycon.ButtonB])
            {
                buttons |= ControllerKeys.B;
            }
            if (keyboard[(Key)npad.RightJoycon.ButtonX])
            {
                buttons |= ControllerKeys.X;
            }
            if (keyboard[(Key)npad.RightJoycon.ButtonY])
            {
                buttons |= ControllerKeys.Y;
            }
            if (keyboard[(Key)npad.RightJoycon.ButtonPlus])
            {
                buttons |= ControllerKeys.Plus;
            }
            if (keyboard[(Key)npad.RightJoycon.ButtonR])
            {
                buttons |= ControllerKeys.R | ControllerKeys.Sr;
            }
            if (keyboard[(Key)npad.RightJoycon.ButtonZr])
            {
                buttons |= ControllerKeys.Zr;
            }

            return(buttons);
        }
Beispiel #14
0
        public void SetKey(ControllerKeys key, Keys bind)
        {
            KeyBinds.Clear();

            foreach (Keys k in _keyMapping.Keys)
            {
                KeyBinds.Add(k);
            }

            switch (key)
            {
            case ControllerKeys.A:
                Dictionary <Keys, int> save = new Dictionary <Keys, int>();
                save = _keyMapping;
                _keyMapping.Clear();
                _keyMapping.Add(bind, 7);
                _keyMapping.Add(KeyBinds[1], 6);
                _keyMapping.Add(KeyBinds[2], 5);
                _keyMapping.Add(KeyBinds[3], 4);
                _keyMapping.Add(KeyBinds[4], 3);
                _keyMapping.Add(KeyBinds[5], 2);
                _keyMapping.Add(KeyBinds[6], 1);
                _keyMapping.Add(KeyBinds[7], 0);
                break;

            case ControllerKeys.B:
                Dictionary <Keys, int> save2 = new Dictionary <Keys, int>();
                save2 = _keyMapping;
                _keyMapping.Clear();
                _keyMapping.Add(KeyBinds[0], 7);
                _keyMapping.Add(bind, 6);
                _keyMapping.Add(KeyBinds[2], 5);
                _keyMapping.Add(KeyBinds[3], 4);
                _keyMapping.Add(KeyBinds[4], 3);
                _keyMapping.Add(KeyBinds[5], 2);
                _keyMapping.Add(KeyBinds[6], 1);
                _keyMapping.Add(KeyBinds[7], 0);
                break;

            case ControllerKeys.SELECT:
                Dictionary <Keys, int> save3 = new Dictionary <Keys, int>();
                save3 = _keyMapping;
                _keyMapping.Clear();
                _keyMapping.Add(KeyBinds[0], 7);
                _keyMapping.Add(KeyBinds[1], 6);
                _keyMapping.Add(bind, 5);
                _keyMapping.Add(KeyBinds[3], 4);
                _keyMapping.Add(KeyBinds[4], 3);
                _keyMapping.Add(KeyBinds[5], 2);
                _keyMapping.Add(KeyBinds[6], 1);
                _keyMapping.Add(KeyBinds[7], 0);
                break;

            case ControllerKeys.START:
                Dictionary <Keys, int> save4 = new Dictionary <Keys, int>();
                save4 = _keyMapping;
                _keyMapping.Clear();
                _keyMapping.Add(KeyBinds[0], 7);
                _keyMapping.Add(KeyBinds[1], 6);
                _keyMapping.Add(KeyBinds[2], 5);
                _keyMapping.Add(bind, 4);
                _keyMapping.Add(KeyBinds[4], 3);
                _keyMapping.Add(KeyBinds[5], 2);
                _keyMapping.Add(KeyBinds[6], 1);
                _keyMapping.Add(KeyBinds[7], 0);
                break;

            case ControllerKeys.UP:
                Dictionary <Keys, int> save5 = new Dictionary <Keys, int>();
                save5 = _keyMapping;
                _keyMapping.Clear();
                _keyMapping.Add(KeyBinds[0], 7);
                _keyMapping.Add(KeyBinds[1], 6);
                _keyMapping.Add(KeyBinds[2], 5);
                _keyMapping.Add(KeyBinds[3], 4);
                _keyMapping.Add(bind, 3);
                _keyMapping.Add(KeyBinds[5], 2);
                _keyMapping.Add(KeyBinds[6], 1);
                _keyMapping.Add(KeyBinds[7], 0);
                break;

            case ControllerKeys.DOWN:
                Dictionary <Keys, int> save6 = new Dictionary <Keys, int>();
                save6 = _keyMapping;
                _keyMapping.Clear();
                _keyMapping.Add(KeyBinds[0], 7);
                _keyMapping.Add(KeyBinds[1], 6);
                _keyMapping.Add(KeyBinds[2], 5);
                _keyMapping.Add(KeyBinds[3], 4);
                _keyMapping.Add(KeyBinds[4], 3);
                _keyMapping.Add(bind, 2);
                _keyMapping.Add(KeyBinds[6], 1);
                _keyMapping.Add(KeyBinds[7], 0);
                break;

            case ControllerKeys.LEFT:
                Dictionary <Keys, int> save7 = new Dictionary <Keys, int>();
                save7 = _keyMapping;
                _keyMapping.Clear();
                _keyMapping.Add(KeyBinds[0], 7);
                _keyMapping.Add(KeyBinds[1], 6);
                _keyMapping.Add(KeyBinds[2], 5);
                _keyMapping.Add(KeyBinds[3], 4);
                _keyMapping.Add(KeyBinds[4], 3);
                _keyMapping.Add(KeyBinds[5], 2);
                _keyMapping.Add(bind, 1);
                _keyMapping.Add(KeyBinds[7], 0);
                break;

            case ControllerKeys.RIGHT:
                Dictionary <Keys, int> save8 = new Dictionary <Keys, int>();
                save8 = _keyMapping;
                _keyMapping.Clear();
                _keyMapping.Add(KeyBinds[0], 7);
                _keyMapping.Add(KeyBinds[1], 6);
                _keyMapping.Add(KeyBinds[2], 5);
                _keyMapping.Add(KeyBinds[3], 4);
                _keyMapping.Add(KeyBinds[4], 3);
                _keyMapping.Add(KeyBinds[5], 2);
                _keyMapping.Add(KeyBinds[6], 1);
                _keyMapping.Add(bind, 0);
                break;

            default:
                break;
            }
        }
 public void ControllerKeys_WithNoneActiveSetToHardwareBits_ReturnsValidBits(ControllerKeys key)
 {
     Assert.Equal((byte)0, key.ToHardwareBits());
 }
 public void ControllerKeys_ToHardwareBits_ReturnsValidBits(ControllerKeys key)
 {
     Assert.NotEqual((byte)0, key.ToHardwareBits());
 }
 public void ControllerKeys_ToDisplayStringForDeadKeys_DisplayStringEmpty(ControllerKeys key)
 {
     Assert.True(string.IsNullOrEmpty(key.ToDisplayString()));
 }
 public void ControllerKeys_ToDisplayStringForControllerKeys_DisplayStringNotEmpty(ControllerKeys key)
 {
     Assert.False(string.IsNullOrEmpty(key.ToDisplayString()));
 }
Beispiel #19
0
        public ControllerKeys GetButtons()
        {
            KeyboardState keyboard = GetKeyboardState(_config.Index);

            ControllerKeys buttons = 0;

            if (keyboard[(Key)_config.LeftJoycon.StickButton])
            {
                buttons |= ControllerKeys.LStick;
            }
            if (keyboard[(Key)_config.LeftJoycon.DPadUp])
            {
                buttons |= ControllerKeys.DpadUp;
            }
            if (keyboard[(Key)_config.LeftJoycon.DPadDown])
            {
                buttons |= ControllerKeys.DpadDown;
            }
            if (keyboard[(Key)_config.LeftJoycon.DPadLeft])
            {
                buttons |= ControllerKeys.DpadLeft;
            }
            if (keyboard[(Key)_config.LeftJoycon.DPadRight])
            {
                buttons |= ControllerKeys.DpadRight;
            }
            if (keyboard[(Key)_config.LeftJoycon.ButtonMinus])
            {
                buttons |= ControllerKeys.Minus;
            }
            if (keyboard[(Key)_config.LeftJoycon.ButtonL])
            {
                buttons |= ControllerKeys.L;
            }
            if (keyboard[(Key)_config.LeftJoycon.ButtonZl])
            {
                buttons |= ControllerKeys.Zl;
            }
            if (keyboard[(Key)_config.LeftJoycon.ButtonSl])
            {
                buttons |= ControllerKeys.SlLeft;
            }
            if (keyboard[(Key)_config.LeftJoycon.ButtonSr])
            {
                buttons |= ControllerKeys.SlRight;
            }

            if (keyboard[(Key)_config.RightJoycon.StickButton])
            {
                buttons |= ControllerKeys.RStick;
            }
            if (keyboard[(Key)_config.RightJoycon.ButtonA])
            {
                buttons |= ControllerKeys.A;
            }
            if (keyboard[(Key)_config.RightJoycon.ButtonB])
            {
                buttons |= ControllerKeys.B;
            }
            if (keyboard[(Key)_config.RightJoycon.ButtonX])
            {
                buttons |= ControllerKeys.X;
            }
            if (keyboard[(Key)_config.RightJoycon.ButtonY])
            {
                buttons |= ControllerKeys.Y;
            }
            if (keyboard[(Key)_config.RightJoycon.ButtonPlus])
            {
                buttons |= ControllerKeys.Plus;
            }
            if (keyboard[(Key)_config.RightJoycon.ButtonR])
            {
                buttons |= ControllerKeys.R;
            }
            if (keyboard[(Key)_config.RightJoycon.ButtonZr])
            {
                buttons |= ControllerKeys.Zr;
            }
            if (keyboard[(Key)_config.RightJoycon.ButtonSl])
            {
                buttons |= ControllerKeys.SrLeft;
            }
            if (keyboard[(Key)_config.RightJoycon.ButtonSr])
            {
                buttons |= ControllerKeys.SrRight;
            }

            return(buttons);
        }
Beispiel #20
0
 public override void Reset()
 {
     state = ControllerKeys.Presence;
 }
Beispiel #21
0
        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>();

            foreach (InputConfig inputConfig in ConfigurationState.Instance.Hid.InputConfig.Value.ToArray())
            {
                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 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);

                gamepadInputs.Add(new GamepadInput
                {
                    PlayerId = (HLE.HOS.Services.Hid.PlayerIndex)inputConfig.PlayerIndex,
                    Buttons  = currentButton,
                    LStick   = leftJoystick,
                    RStick   = rightJoystick
                });
            }

            _device.Hid.Npads.SetGamepadsInput(gamepadInputs.ToArray());

            // 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)
            {
                int screenWidth  = AllocatedWidth;
                int screenHeight = AllocatedHeight;

                if (AllocatedWidth > (AllocatedHeight * SwitchPanelWidth) / SwitchPanelHeight)
                {
                    screenWidth = (AllocatedHeight * SwitchPanelWidth) / SwitchPanelHeight;
                }
                else
                {
                    screenHeight = (AllocatedWidth * SwitchPanelHeight) / SwitchPanelWidth;
                }

                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 * SwitchPanelWidth) / 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);
        }
Beispiel #22
0
        public ControllerKeys GetButtons()
        {
            // NOTE: This should be initialized AFTER GTK for compat reasons with OpenTK SDL2 backend and GTK on Linux.
            // BODY: Usage of Joystick.GetState must be defer to after GTK full initialization. Otherwise, GTK will segfault because SDL2 was already init *sighs*
            if (!IsEnabled())
            {
                return(0);
            }

            JoystickState joystickState = Joystick.GetState(_config.Index);

            ControllerKeys buttons = 0;

            if (IsActivated(joystickState, _config.LeftJoycon.DPadUp))
            {
                buttons |= ControllerKeys.DpadUp;
            }
            if (IsActivated(joystickState, _config.LeftJoycon.DPadDown))
            {
                buttons |= ControllerKeys.DpadDown;
            }
            if (IsActivated(joystickState, _config.LeftJoycon.DPadLeft))
            {
                buttons |= ControllerKeys.DpadLeft;
            }
            if (IsActivated(joystickState, _config.LeftJoycon.DPadRight))
            {
                buttons |= ControllerKeys.DpadRight;
            }
            if (IsActivated(joystickState, _config.LeftJoycon.StickButton))
            {
                buttons |= ControllerKeys.LStick;
            }
            if (IsActivated(joystickState, _config.LeftJoycon.ButtonMinus))
            {
                buttons |= ControllerKeys.Minus;
            }
            if (IsActivated(joystickState, _config.LeftJoycon.ButtonL))
            {
                buttons |= ControllerKeys.L;
            }
            if (IsActivated(joystickState, _config.LeftJoycon.ButtonZl))
            {
                buttons |= ControllerKeys.Zl;
            }
            if (IsActivated(joystickState, _config.LeftJoycon.ButtonSl))
            {
                buttons |= ControllerKeys.SlLeft;
            }
            if (IsActivated(joystickState, _config.LeftJoycon.ButtonSr))
            {
                buttons |= ControllerKeys.SrLeft;
            }

            if (IsActivated(joystickState, _config.RightJoycon.ButtonA))
            {
                buttons |= ControllerKeys.A;
            }
            if (IsActivated(joystickState, _config.RightJoycon.ButtonB))
            {
                buttons |= ControllerKeys.B;
            }
            if (IsActivated(joystickState, _config.RightJoycon.ButtonX))
            {
                buttons |= ControllerKeys.X;
            }
            if (IsActivated(joystickState, _config.RightJoycon.ButtonY))
            {
                buttons |= ControllerKeys.Y;
            }
            if (IsActivated(joystickState, _config.RightJoycon.StickButton))
            {
                buttons |= ControllerKeys.RStick;
            }
            if (IsActivated(joystickState, _config.RightJoycon.ButtonPlus))
            {
                buttons |= ControllerKeys.Plus;
            }
            if (IsActivated(joystickState, _config.RightJoycon.ButtonR))
            {
                buttons |= ControllerKeys.R;
            }
            if (IsActivated(joystickState, _config.RightJoycon.ButtonZr))
            {
                buttons |= ControllerKeys.Zr;
            }
            if (IsActivated(joystickState, _config.RightJoycon.ButtonSl))
            {
                buttons |= ControllerKeys.SlRight;
            }
            if (IsActivated(joystickState, _config.RightJoycon.ButtonSr))
            {
                buttons |= ControllerKeys.SrRight;
            }

            return(buttons);
        }
Beispiel #23
0
        public ControllerKeys GetButtons()
        {
            if (!IsEnabled())
            {
                return(0);
            }

            JoystickState joystickState = Joystick.GetState(_inner.Index);

            ControllerKeys buttons = 0;

            if (IsActivated(joystickState, _inner.LeftJoycon.DPadUp))
            {
                buttons |= ControllerKeys.DpadUp;
            }
            if (IsActivated(joystickState, _inner.LeftJoycon.DPadDown))
            {
                buttons |= ControllerKeys.DpadDown;
            }
            if (IsActivated(joystickState, _inner.LeftJoycon.DPadLeft))
            {
                buttons |= ControllerKeys.DpadLeft;
            }
            if (IsActivated(joystickState, _inner.LeftJoycon.DPadRight))
            {
                buttons |= ControllerKeys.DpadRight;
            }
            if (IsActivated(joystickState, _inner.LeftJoycon.StickButton))
            {
                buttons |= ControllerKeys.LStick;
            }
            if (IsActivated(joystickState, _inner.LeftJoycon.ButtonMinus))
            {
                buttons |= ControllerKeys.Minus;
            }
            if (IsActivated(joystickState, _inner.LeftJoycon.ButtonL))
            {
                buttons |= ControllerKeys.L | ControllerKeys.Sl;
            }
            if (IsActivated(joystickState, _inner.LeftJoycon.ButtonZl))
            {
                buttons |= ControllerKeys.Zl;
            }

            if (IsActivated(joystickState, _inner.RightJoycon.ButtonA))
            {
                buttons |= ControllerKeys.A;
            }
            if (IsActivated(joystickState, _inner.RightJoycon.ButtonB))
            {
                buttons |= ControllerKeys.B;
            }
            if (IsActivated(joystickState, _inner.RightJoycon.ButtonX))
            {
                buttons |= ControllerKeys.X;
            }
            if (IsActivated(joystickState, _inner.RightJoycon.ButtonY))
            {
                buttons |= ControllerKeys.Y;
            }
            if (IsActivated(joystickState, _inner.RightJoycon.StickButton))
            {
                buttons |= ControllerKeys.RStick;
            }
            if (IsActivated(joystickState, _inner.RightJoycon.ButtonPlus))
            {
                buttons |= ControllerKeys.Plus;
            }
            if (IsActivated(joystickState, _inner.RightJoycon.ButtonR))
            {
                buttons |= ControllerKeys.R | ControllerKeys.Sr;
            }
            if (IsActivated(joystickState, _inner.RightJoycon.ButtonZr))
            {
                buttons |= ControllerKeys.Zr;
            }

            return(buttons);
        }
Beispiel #24
0
        private bool UpdateFrame()
        {
            if (!IsActive)
            {
                return(true);
            }

            if (IsStopped)
            {
                return(false);
            }

            HotkeyButtons    currentHotkeyButtons = 0;
            ControllerKeys   currentButton        = 0;
            JoystickPosition leftJoystick;
            JoystickPosition rightJoystick;
            KeyboardInput?   hidKeyboard = null;

            int leftJoystickDx  = 0;
            int leftJoystickDy  = 0;
            int rightJoystickDx = 0;
            int rightJoystickDy = 0;

            // OpenTK always captures keyboard events, even if out of focus, so check if window is focused.
            if (IsFocused)
            {
                KeyboardState keyboard = OpenTK.Input.Keyboard.GetState();

                Gtk.Application.Invoke(delegate
                {
                    HandleScreenState(keyboard);
                });

                // Normal Input
                currentHotkeyButtons = KeyboardControls.GetHotkeyButtons(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
                currentButton        = KeyboardControls.GetButtons(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);

                if (ConfigurationState.Instance.Hid.EnableKeyboard)
                {
                    hidKeyboard = KeyboardControls.GetKeysDown(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
                }

                (leftJoystickDx, leftJoystickDy)   = KeyboardControls.GetLeftStick(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
                (rightJoystickDx, rightJoystickDy) = KeyboardControls.GetRightStick(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
            }

            if (!hidKeyboard.HasValue)
            {
                hidKeyboard = new KeyboardInput
                {
                    Modifier = 0,
                    Keys     = new int[0x8]
                };
            }

            currentButton |= _primaryController.GetButtons();

            // Keyboard has priority stick-wise
            if (leftJoystickDx == 0 && leftJoystickDy == 0)
            {
                (leftJoystickDx, leftJoystickDy) = _primaryController.GetLeftStick();
            }

            if (rightJoystickDx == 0 && rightJoystickDy == 0)
            {
                (rightJoystickDx, rightJoystickDy) = _primaryController.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 (IsFocused && _mousePressed)
            {
                int screenWidth  = AllocatedWidth;
                int screenHeight = AllocatedHeight;

                if (AllocatedWidth > (AllocatedHeight * SwitchPanelWidth) / SwitchPanelHeight)
                {
                    screenWidth = (AllocatedHeight * SwitchPanelWidth) / SwitchPanelHeight;
                }
                else
                {
                    screenHeight = (AllocatedWidth * SwitchPanelHeight) / SwitchPanelWidth;
                }

                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 * SwitchPanelWidth) / screenWidth;
                    int mY = (screenMouseY * SwitchPanelHeight) / screenHeight;

                    TouchPoint currentPoint = new TouchPoint
                    {
                        X = (uint)mX,   // can be -ve?
                        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();
            }

            if (ConfigurationState.Instance.Hid.EnableKeyboard && hidKeyboard.HasValue)
            {
                _device.Hid.Keyboard.Update(hidKeyboard.Value);
            }

            _device.Hid.DebugPad.Update();

            _device.Hid.Npads.SetGamepadsInput(new GamepadInput {
                PlayerId = HidControllerID.Auto,
                Buttons  = currentButton,
                LStick   = leftJoystick,
                RStick   = rightJoystick
            });

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

            _prevHotkeyButtons = currentHotkeyButtons;

            return(true);
        }
Beispiel #25
0
        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;

                    Logger.Info?.Print(LogClass.Application, $"Vsync toggled to: {_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);
        }