Beispiel #1
0
        void ContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _contextMenuStrip.PerformSafely(() => _contextMenuStrip.Items.Clear());

            _contextMenuStrip.PerformSafely(() => _contextMenuStrip.Items.Add(_toolStripMenuItemVersion));
            _contextMenuStrip.PerformSafely(() => _contextMenuStrip.Items.Add(new ToolStripSeparator()));


            foreach (var controller in _gamepadHelper.GetControllers())
            {
                var item = new ToolStripMenuItem
                {
                    Text  = $@"Controller {(int)controller.UserIndex + 1} ({controller.GetBatteryInformation(BatteryDeviceType.Gamepad).BatteryLevel.ToString().ToUpperInvariant()})",
                    Image = _gamepadHelper.GetBatteryImage(controller.GetBatteryInformation(BatteryDeviceType.Gamepad).BatteryLevel)
                };
                item.Click += (o, args) => { _gamepadRemote.TurnOff(controller); };

                _contextMenuStrip.PerformSafely(() => _contextMenuStrip.Items.Add(item));
            }

            if (_gamepadHelper.GetControllers().Any())
            {
                _contextMenuStrip.PerformSafely(() => _contextMenuStrip.Items.Add(new ToolStripSeparator()));
            }

            _contextMenuStrip.PerformSafely(() => _contextMenuStrip.Items.Add(_toolStripMenuItemLowBatteryWarning));
            _contextMenuStrip.PerformSafely(() => _contextMenuStrip.Items.Add(_toolStripMenuItemUseGuideButton));
            _contextMenuStrip.PerformSafely(() => _contextMenuStrip.Items.Add(_toolStripMenuItemAutoStartup));
            _contextMenuStrip.PerformSafely(() => _contextMenuStrip.Items.Add(_toolStripMenuItemExit));
        }
        public void Start()
        {
            _isRunning = true;

            while (_isRunning)
            {
                foreach (var controller in _gamepadHelper.GetControllers())
                {
                    if (_enableLowBatteryWarning && controller.GetBatteryInformation(BatteryDeviceType.Gamepad).BatteryLevel <= BatteryLevel.Low)
                    {
                        var stopWatch = _stopWatches[controller.UserIndex];

                        if (!stopWatch.IsRunning)
                        {
                            _batterylowAction?.Invoke(controller);
                            stopWatch.Start();
                        }
                        else if (stopWatch.Elapsed.Minutes >= 15)
                        {
                            _batterylowAction?.Invoke(controller);
                            stopWatch.Reset();
                            stopWatch.Start();
                        }
                    }

                    XInputGetGamePad((int)controller.UserIndex, out var gamepadSecret);

                    if (gamepadSecret.WButtons == 0)
                    {
                        continue;
                    }

                    if (_useGuideButton && IsPressed(gamepadSecret.WButtons, XINPUT_GAMEPAD_GUIDE))
                    {
                        _remote.TurnOff(controller);
                    }

                    if (!_useGuideButton && IsPressed(gamepadSecret.WButtons, XINPUT_GAMEPAD_START, XINPUT_GAMEPAD_BACK))
                    {
                        _remote.TurnOff(controller);
                    }

                    if (IsPressed(gamepadSecret.WButtons, XINPUT_GAMEPAD_BACK, XINPUT_GAMEPAD_B))
                    {
                        _batteryAction?.Invoke(controller);
                    }
                }

                Thread.Sleep(1000);
            }
        }