Skip to content

Commit

Permalink
Add support for SuperAltF4 button on Linux
Browse files Browse the repository at this point in the history
- Added support for closing focused window on Linux using xdotool
- Updated close_window function to handle Linux-specific window closing
- Updated get_focused_window function to handle Linux-specific focused window retrieval
- Updated commands.py to prevent taskkill while not on windows
  • Loading branch information
Lenochxd committed Dec 17, 2024
1 parent 293e312 commit 05e929c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
4 changes: 4 additions & 0 deletions app/buttons/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ def handle_command(message=None):
hwnd = window.get_focused()
if hwnd:
window.close(hwnd)

if not is_win:
return jsonify({"success": True})
# Make sure the window is closed by killing the process if necessary (windows only)
subprocess.Popen(f"taskkill /f /im {hwnd}", shell=True)
subprocess.Popen(f"taskkill /f /im {hwnd}.exe", shell=True)

Expand Down
37 changes: 24 additions & 13 deletions app/buttons/window/close_window.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
from app.utils.platform import is_win
from app.utils.platform import is_windows, is_linux

if is_win:
import subprocess
if is_windows:
import win32gui
import win32con
from app.utils.logger import log


def close_window(window_title):
if not is_win:
log.error("This command is only available on Windows")
raise RuntimeError("This command is only available on Windows")
def close_window(window):
if is_windows:
# Find the window
window_name = window
hwnd = win32gui.FindWindow(None, window_name)
if hwnd == 0:
log.warning(f"Window with title '{window_name}' not found or already closed")
raise RuntimeError(f"Window with title '{window_name}' not found or already closed")

# Close the window
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)

# Find the window
hwnd = win32gui.FindWindow(None, window_title)
if hwnd == 0:
log.warning(f"Window with title '{window_title}' not found or already closed")
raise RuntimeError(f"Window with title '{window_title}' not found or already closed")
elif is_linux:
# While using Linux, the window name is the window ID
window_id = window
if subprocess.run(["xdotool", "windowclose", window_id]).returncode != 0:
log.warning(f"Window with ID '{window_id}' not found or already closed")
raise RuntimeError(f"Window with ID '{window_id}' not found or already closed")
log.success(f"Window with ID '{window_id}' has been closed")

# Close the window
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
else:
log.error("This command is not implemented for this platform.")
raise RuntimeError("This command is not implemented for this platform.")
36 changes: 24 additions & 12 deletions app/buttons/window/get_focused.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
from app.utils.platform import is_win
from app.utils.platform import is_windows, is_linux

if is_win: import win32gui
if is_windows: import win32gui
from app.utils.logger import log
import subprocess


def get_focused_window():
if not is_win:
log.error("This command is only available on Windows")
raise RuntimeError("This command is only available on Windows")
if is_windows:
hwnd = win32gui.GetForegroundWindow()
if hwnd == 0:
log.debug("No window has focus")
return None
else:
window_title = win32gui.GetWindowText(hwnd)
log.debug(f"Focused window: {window_title}")
return window_title

hwnd = win32gui.GetForegroundWindow()
if hwnd == 0:
log.debug("No window has focus")
return None
elif is_linux:
try:
window_id = subprocess.check_output(["xdotool", "getwindowfocus"], universal_newlines=True).strip()
window_title = subprocess.check_output(["xdotool", "getwindowname", window_id], universal_newlines=True).strip()
log.debug(f"Focused window: '{window_title}' ; window_id: '{window_id}'")
return window_id
except subprocess.CalledProcessError:
log.debug("No window has focus")
return None

else:
window_title = win32gui.GetWindowText(hwnd)
log.debug(f"Focused window: {window_title}")
return window_title
log.error("This command is not implemented for this platform.")
raise RuntimeError("This command is not implemented for this platform.")

0 comments on commit 05e929c

Please sign in to comment.