From ff5c54895b20ef79c706733b5add447f74b9933a Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Sun, 7 Dec 2025 16:01:14 +0100 Subject: [PATCH] Fix focus app detection to use window titles instead of process names - Changed from pgrep -f (matches any process with 'code' in cmdline) to xdotool window detection (only matches visible windows) - VS Code background services (code-tunnel, etc.) no longer trigger blocking - Music is only blocked when VS Code window is actually open - Split detection into FOCUS_APPS_WINDOWS and FOCUS_APPS_PROCESSES arrays --- .../digital_wellbeing/music_parallelism.sh | 102 +++++++++++------- .../youtube-music-wrapper.sh | 46 ++++++-- 2 files changed, 99 insertions(+), 49 deletions(-) diff --git a/scripts/digital_wellbeing/music_parallelism.sh b/scripts/digital_wellbeing/music_parallelism.sh index 2cea48c..3c2ec06 100755 --- a/scripts/digital_wellbeing/music_parallelism.sh +++ b/scripts/digital_wellbeing/music_parallelism.sh @@ -15,32 +15,36 @@ mkdir -p "$LOG_DIR" 2>/dev/null || true LOG_FILE="$LOG_DIR/music-parallelism.log" CHECK_INTERVAL=3 -# Focus applications - window class names or process names -# These are apps that require focus and shouldn't have music playing -FOCUS_APPS=( - # IDEs and code editors - "code" # VS Code - "Code" # VS Code (window class) - "vscodium" # VSCodium - "cursor" # Cursor IDE - "jetbrains" # JetBrains IDEs - "idea" # IntelliJ IDEA - "pycharm" # PyCharm - "webstorm" # WebStorm - "clion" # CLion - "rider" # Rider - "sublime_text" # Sublime Text - "atom" # Atom - "neovide" # Neovide (Neovim GUI) +# Focus applications - window class names for xdotool detection +# Only apps with VISIBLE WINDOWS should block music +# We use window detection, not process detection, to avoid matching background services +FOCUS_APPS_WINDOWS=( + # IDEs and code editors - match window titles + "Visual Studio Code" + "VSCodium" + "Cursor" + "IntelliJ IDEA" + "PyCharm" + "WebStorm" + "CLion" + "Rider" + "Sublime Text" + "Atom" + "Neovide" # Gaming - "steam_app" # Steam games (they run as steam_app_XXXXX) - "steamwebhelper" # Steam client - "gamescope" # Gamescope (Steam Deck compositor) - # Other focus apps (add more as needed) - "blender" # Blender - "godot" # Godot Engine - "unity" # Unity Editor - "UnrealEditor" # Unreal Engine + "Steam" + # Creative apps + "Blender" + "Godot" + "Unity" + "Unreal Editor" +) + +# Process patterns that definitively indicate focus apps +# These are checked with pgrep -x (exact match) to avoid false positives +FOCUS_APPS_PROCESSES=( + "steam_app_" # Steam games + "gamescope" # Gamescope compositor ) # Music streaming services - browser tabs or electron apps @@ -78,21 +82,26 @@ log_message() { } # Check if any focus application is running +# Uses window detection primarily to avoid matching background services is_focus_app_running() { - for app in "${FOCUS_APPS[@]}"; do - # Check running processes - if pgrep -i -f "$app" &>/dev/null; then - echo "$app" - return 0 - fi - # Check window names using xdotool if available - if command -v xdotool &>/dev/null; then + # First check for visible windows using xdotool + if command -v xdotool &>/dev/null; then + for app in "${FOCUS_APPS_WINDOWS[@]}"; do if xdotool search --name "$app" &>/dev/null 2>&1; then echo "$app" return 0 fi + done + fi + + # Then check for specific process patterns (like steam games) + for app in "${FOCUS_APPS_PROCESSES[@]}"; do + if pgrep -f "$app" &>/dev/null; then + echo "$app" + return 0 fi done + return 1 } @@ -213,7 +222,8 @@ notify_user() { # This runs every 0.5 seconds for near-instant detection instant_monitor_loop() { log_message "=== Music Parallelism INSTANT Monitor Started ===" - log_message "Focus apps monitored: ${FOCUS_APPS[*]}" + log_message "Focus apps (windows): ${FOCUS_APPS_WINDOWS[*]}" + log_message "Focus apps (processes): ${FOCUS_APPS_PROCESSES[*]}" log_message "Polling every 0.5 seconds for instant kill" while true; do @@ -238,7 +248,8 @@ instant_monitor_loop() { # Main monitoring loop monitor_loop() { log_message "=== Music Parallelism Monitor Started ===" - log_message "Focus apps monitored: ${FOCUS_APPS[*]}" + log_message "Focus apps (windows): ${FOCUS_APPS_WINDOWS[*]}" + log_message "Focus apps (processes): ${FOCUS_APPS_PROCESSES[*]}" log_message "Music services monitored: ${MUSIC_SERVICES[*]}" log_message "Check interval: ${CHECK_INTERVAL}s" @@ -269,14 +280,27 @@ show_status() { echo "=================================" echo "" - echo "Focus Applications:" + echo "Focus Applications (window-based detection):" local focus_running=false - for app in "${FOCUS_APPS[@]}"; do - if pgrep -i -f "$app" &>/dev/null; then - echo " ✓ $app (RUNNING)" + + # Check windows + if command -v xdotool &>/dev/null; then + for app in "${FOCUS_APPS_WINDOWS[@]}"; do + if xdotool search --name "$app" &>/dev/null 2>&1; then + echo " ✓ $app (WINDOW OPEN)" + focus_running=true + fi + done + fi + + # Check processes + for app in "${FOCUS_APPS_PROCESSES[@]}"; do + if pgrep -f "$app" &>/dev/null; then + echo " ✓ $app (PROCESS RUNNING)" focus_running=true fi done + if ! $focus_running; then echo " (none detected)" fi diff --git a/scripts/digital_wellbeing/youtube-music-wrapper.sh b/scripts/digital_wellbeing/youtube-music-wrapper.sh index bed3923..375a629 100644 --- a/scripts/digital_wellbeing/youtube-music-wrapper.sh +++ b/scripts/digital_wellbeing/youtube-music-wrapper.sh @@ -14,23 +14,49 @@ log_message() { echo "$msg" >>"$LOG_FILE" 2>/dev/null || true } -# Focus apps that block music -FOCUS_APPS=( - "code" "Code" "vscodium" "cursor" - "jetbrains" "idea" "pycharm" "webstorm" "clion" "rider" - "sublime_text" "atom" "neovide" - "steam_app" "steamwebhelper" "gamescope" - "blender" "godot" "unity" "UnrealEditor" +# Focus apps - window titles to check (only visible windows count) +FOCUS_APPS_WINDOWS=( + "Visual Studio Code" + "VSCodium" + "Cursor" + "IntelliJ IDEA" + "PyCharm" + "WebStorm" + "CLion" + "Rider" + "Sublime Text" + "Blender" + "Godot" + "Unity" + "Unreal Editor" ) -# Check if any focus app is running +# Focus apps - process patterns to check +FOCUS_APPS_PROCESSES=( + "steam_app_" + "gamescope" +) + +# Check if any focus app is running (window-based detection) is_focus_app_running() { - for app in "${FOCUS_APPS[@]}"; do - if pgrep -i -f "$app" &>/dev/null; then + # Check windows first + if command -v xdotool &>/dev/null; then + for app in "${FOCUS_APPS_WINDOWS[@]}"; do + if xdotool search --name "$app" &>/dev/null 2>&1; then + echo "$app" + return 0 + fi + done + fi + + # Check specific processes + for app in "${FOCUS_APPS_PROCESSES[@]}"; do + if pgrep -f "$app" &>/dev/null; then echo "$app" return 0 fi done + return 1 }