From 3cc40154b0c9785953172bf5b122ed3f42871743 Mon Sep 17 00:00:00 2001
From: Boris Yumankulov <boria138@altlinux.org>
Date: Sun, 15 Jun 2025 14:00:06 +0500
Subject: [PATCH] fix: disable gamepad handling on game start thanks to
 @Vector_null

Signed-off-by: Boris Yumankulov <boria138@altlinux.org>
---
 portprotonqt/input_manager.py | 15 +++++++++++++++
 portprotonqt/main_window.py   |  9 +++++++++
 2 files changed, 24 insertions(+)

diff --git a/portprotonqt/input_manager.py b/portprotonqt/input_manager.py
index 310faea..d41a238 100644
--- a/portprotonqt/input_manager.py
+++ b/portprotonqt/input_manager.py
@@ -72,6 +72,7 @@ class InputManager(QObject):
     ):
         super().__init__(cast(QObject, main_window))
         self._parent = main_window
+        self._gamepad_handling_enabled = True
         # Ensure attributes exist on main_window
         self._parent.currentDetailPage = getattr(self._parent, 'currentDetailPage', None)
         self._parent.current_exec_line = getattr(self._parent, 'current_exec_line', None)
@@ -132,6 +133,16 @@ class InputManager(QObject):
         except Exception as e:
             logger.error(f"Error in handle_fullscreen_slot: {e}", exc_info=True)
 
+    def disable_gamepad_handling(self) -> None:
+        """Отключает обработку событий геймпада."""
+        self._gamepad_handling_enabled = False
+        self.stop_rumble()
+        self.dpad_timer.stop()
+
+    def enable_gamepad_handling(self) -> None:
+        """Включает обработку событий геймпада."""
+        self._gamepad_handling_enabled = True
+
     def trigger_rumble(self, duration_ms: int = 200, strong_magnitude: int = 0x8000, weak_magnitude: int = 0x8000) -> None:
         """Trigger a rumble effect on the gamepad if supported."""
         if not read_rumble_config():
@@ -176,6 +187,8 @@ class InputManager(QObject):
 
     @Slot(int)
     def handle_button_slot(self, button_code: int) -> None:
+        if not self._gamepad_handling_enabled:
+            return
         try:
             # Ignore gamepad events if a game is launched
             if getattr(self._parent, '_gameLaunched', False):
@@ -318,6 +331,8 @@ class InputManager(QObject):
 
     @Slot(int, int, float)
     def handle_dpad_slot(self, code: int, value: int, current_time: float) -> None:
+        if not self._gamepad_handling_enabled:
+            return
         try:
             # Ignore gamepad events if a game is launched
             if getattr(self._parent, '_gameLaunched', False):
diff --git a/portprotonqt/main_window.py b/portprotonqt/main_window.py
index a040ce1..9af33e8 100644
--- a/portprotonqt/main_window.py
+++ b/portprotonqt/main_window.py
@@ -1726,6 +1726,8 @@ class MainWindow(QMainWindow):
         elif not child_running:
             # Игра завершилась – сбрасываем флаг, сбрасываем кнопку и останавливаем таймер
             self._gameLaunched = False
+            if hasattr(self, 'input_manager'):
+                self.input_manager.enable_gamepad_handling()
             self.resetPlayButton()
             #self._uninhibit_screensaver()
             if hasattr(self, 'checkProcessTimer') and self.checkProcessTimer is not None:
@@ -1782,6 +1784,9 @@ class MainWindow(QMainWindow):
 
         # Если игра уже запущена для этого exe – останавливаем её по нажатию кнопки
         if self.game_processes and self.target_exe == current_exe:
+            if hasattr(self, 'input_manager'):
+                self.input_manager.enable_gamepad_handling()
+
             for proc in self.game_processes:
                 try:
                     parent = psutil.Process(proc.pid)
@@ -1821,6 +1826,10 @@ class MainWindow(QMainWindow):
             self.target_exe = current_exe
             exe_name = os.path.splitext(current_exe)[0]
             env_vars = os.environ.copy()
+
+            if hasattr(self, 'input_manager'):
+                self.input_manager.disable_gamepad_handling()
+
             if entry_exec_split[0] == "env" and len(entry_exec_split) > 1 and 'data/scripts/start.sh' in entry_exec_split[1]:
                 env_vars['START_FROM_STEAM'] = '1'
             elif entry_exec_split[0] == "flatpak":