TomiBelan / mute-on-focus-lost

Mute app on focus lost (AutoHotkey script)
8 stars 1 forks source link

Wake pc from sleep cause an error pop up (but mute function still works normally) #1

Closed WilliamLeGod closed 3 weeks ago

WilliamLeGod commented 2 months ago

Hi bro!

I love your tool so much, it works so well but I just noticed a pop error after wake my pc up from sleep and I really want it to not show up (please see attached image)

Could you help me?

Many thanks! ![Uploading Screenshot 2024-04-16 at 16.05.33.png…]()

WilliamLeGod commented 2 months ago
Screenshot 2024-04-16 at 16 05 33
TomiBelan commented 2 months ago

Thanks for the report! It works on my computer :(

Could you please click "Show call stack" next time it happens? I'm not sure I can fix it but I'll try.

WilliamLeGod commented 2 months ago

Thanks for the report! It works on my computer :(

Could you please click "Show call stack" next time it happens? I'm not sure I can fix it but I'll try.

Thanks for the quick reply bro! ![Uploading Screenshot 2024-04-16 at 17.00.17.png…]()

WilliamLeGod commented 2 months ago
Screenshot 2024-04-16 at 17 00 17
WilliamLeGod commented 2 months ago

Is there an option to just not report the error as pop up and make it silent? this error doesn't affect any function of the mute so I'm ok with it as long as it doesn't show up

TomiBelan commented 2 months ago

Nice. I have a theory but I'll need your help. Open AHK/mute_on_focus_lost_lib.ahk in an editor (such as notepad). Find this line: DAE := VA_GetDevice() (line 223) Paste this text between DAE := VA_GetDevice() and the next line ; activate the session manager

if (DAE = 0) {
  MsgBox("DAE is 0 WTF")
}

Restart the script (double click mute_on_focus_lost.ahk). Sleep.

If my theory is correct, it will show "DAE is 0 WTF" and then show the original error. In that case I know how to fix it (that will be the next step). If "DAE is 0 WTF" doesn't show up, then I'm wrong.

WilliamLeGod commented 2 months ago

Alright, Im on it. Will get back to u asap!

WilliamLeGod commented 2 months ago

I just added the line and test, after 5 sleep tries, no error pop up. wtf? haha

WilliamLeGod commented 2 months ago

Actually I now found a new issue:

I use your tool for mute specific game let's say i'm testing FF7 remake right now I put its .exe in the ahk and it works great but only 1 issue is that whenever I start the game the next time, It's mute!, I check sound setting and yes its sound has X mark on the icon which is mute and I unmute it, testing with focus, lost focus still find; quit game and start game and no sound again (mute)

Also, when I unmute the game and then back go game, sleep, wake pc up, game Mute again :(

Wierd, it happens to other games too though

TomiBelan commented 2 months ago

Neither error shows up? What the heck! Well, let me know when it comes back... Just to check - is the script even running? (reacts to key shortcuts, shows icon in system tray) Maybe try restarting the whole computer.

I'll try to think about the second issue.

WilliamLeGod commented 2 months ago

Yes it is running and I just tested for the last 30 min and no more pop up, also it still works!

WilliamLeGod commented 2 months ago

Have u found a fix yet?

WilliamLeGod commented 2 months ago

any update bro? I love this so much :(

WilliamLeGod commented 2 months ago

Sup bro! I think I know how we can fix this. By adding just 1 feature to unmute everyprogram on startup!

Is it possible to add it?

Looking forward to your reply

Thanks for your work bro!

WilliamLeGod commented 2 months ago

Please help bro :(

WilliamLeGod commented 2 months ago

Where are u bro :(

TomiBelan commented 2 months ago

I'm back.

How it works: The script is remembers which apps are in "mute on focus lost" mode, and Windows itself directly remembers which apps are currently muted. For example, for apps not using "mute on focus lost" mode: if you just toggle mute with Win+F1, then close the app, then later open the app, Windows itself will remember that it was muted last time.

If the app is muted whenever you start it, there are two explanations. Either Windows mutes it because it remembers it was muted last time, or Windows starts it unmuted but the script immediately mutes it for some reason. But neither explanation makes any sense. Assuming the game in the "mute on focus lost" mode, the script should unmute it immediately even if Windows muted it, because starting the game should count as it gaining focus. Basically, for "mute on focus lost" apps, it should already unmute them at startup. :/

Please run these tests. This first test is not a fix, it's just to help me check if I understand your report correctly.

  1. Open a game.
  2. Press Win+F1 a few times to test muting/unmuting. Leave it in unmuted state.
  3. Press Win+F2 a few times, and stop after the higher beep, to make sure "mute on focus lost" is turned on.
  4. Close the game normally (using "quit game" in its main menu) (NOT e.g. alt tab, right click, "Close window")
  5. Open the game again.
  6. Is it muted? (That's bad.)

Or did you mean it happens when you do something different?

Test 2: Like before, but between step 4 (close the game) and step 5 (reopen it), right click the script icon in the system tray and click "Exit". What happens now - does it still start muted? (This should tell us which explanation is more likely.)

Test 3: Restart the script. Right click its tray icon and click "MOFL Toggle Log". (Or toggle it with Win+F4 if configured.) In step 4, does it say "muting!"? In step 5 when you open the game, does its line say "not in MOFL mode" or "muting!" or "unmuting!", or not show up at all? It would be best if you could make a screenshot after step 4 and another screenshot after step 5.

WilliamLeGod commented 2 months ago

Thanks for getting back to me

So I've tested as you guided:

Test 1/ Tested with game FF7 remake, RDR2 and both have issue muted (step 6) but other games are unmuted! so probaby based on games

Test 2/ Yes it still start muted (Windows remember last time right?)

Test 3/ Yes it says in read text "7036 ...ff7remake_.exe muting!"

TomiBelan commented 2 months ago

Thank you and good work. We're getting closer! Let's compare some game that doesn't work (like FF7R) and some game that works. Tell me if there's anything different printed when you quit them. Tell me if there's anything different printed when you reopen them.

My prediction: When closing them, they will both say "muting!". When opening them, in both cases it'll show a long "event" line that mentions the correct game exe. But the lines below "event" will be different. For one game, it will say "unmuting!". For the other game, it won't even be in the list.

By the way, if it's more convenient for you, you can create screenshots with Win+Shift+S and attach the images on GitHub.

WilliamLeGod commented 2 months ago

FF7 muting when it quits! And not Unmute when Open image

Yugioh Masterduel no muting or unmute when quit! image

We found the issue right? !

TomiBelan commented 2 months ago

Yes. And I think I found a way to fix it.

Try it: https://github.com/TomiBelan/mute-on-focus-lost/archive/refs/heads/fix1.zip Only AHK/mute_on_focus_lost_lib.ahk has changed, no need to replace the rest.

If it works on FF7R and RDR2, please test it on as many other games as you can. (Press Win+F2 until high beep. Press alt+tab, game should mute. Switch back, game should unmute. Close game. Reopen game. Game shouldn't be muted.)

Technical details and root cause: When most games exit, they first close the window, then close the audio device. The script notices that the game no longer has focus and it still has enough time to mute it. When some games start, they open the audio device before or roughly in parallel with opening the window. The script unmutes the game (if it's on the list) masking the bug. Other games open the audio device much later after opening the window, so the script doesn't notice it, falling back to the last state remembered by Windows. The fix is that the script should not mute a program if that program doesn't have any open windows.

WilliamLeGod commented 2 months ago

Holy shit. It works bro! Seriously, you are really talented

I will be testing more games and give feedback to you Btw I found out helldivers 2 is really wierd. The script doesn't trigger its .exe or even window name: MOFL_Apps["HELLDIVERS™ 2"] := true MOFL_Apps["helldivers2.exe"] := true

If u have that game, can u test or have any suggestion?

Many thanks again for your hard work!

TomiBelan commented 2 months ago

Awesome. :)

I don't have it but I'd guess MOFL_Apps["helldivers2.exe"] := true should work... Try toggling it with Win+F2, if that works then it's easy, Win+F3 or Win+F4 should tell you the correct exe name to write. If Win+F2 doesn't work either, then the game must be doing something strange, in which case I'll need screenshots of Win+F3 and of Win+F4 to investigate.

WilliamLeGod commented 2 months ago

image Win+F2 = no active window (Someone told me this game PID = 0 haha) Win+F3 or F4 not showing anything

TomiBelan commented 2 months ago

Sounds like my script can't see the Helldivers window at all. So it can't tell when it gains or loses focus. I have no idea how that is possible. But I found some complaints online that say Helldivers is specifically blocking AutoHotkey (the program in which I made the script). It could be related to that, depending on how exactly they block it. Probably nothing I can do about that game.

WilliamLeGod commented 2 months ago

Yes this game is wierd but I have a script that can target helldivers 2 both exe and window name easily. Please take a look

Requires AutoHotkey v2

SingleInstance Force

Persistent

FlexPID := 0 NewProgram := 0 CurrProgram := 0

ProgramList := ["ahk_exe Diablo IV.exe", "ahk_exe cs2.exe", "ahk_exe masterduel.exe", "ahk_exe XDefiant.exe", "ahk_exe ACMirage.exe", "ahk_exe likeadragon8.exe", "ahk_exe Starfield.exe", "ahk_exe farcry5.exe", "ahk_exe armoredcore6.exe", "ahk_exe nioh.exe", "ahk_exe Judgment.exe", "ahk_exe Borderlands3.exe", "ahk_exe destiny2.exe", "ahk_exe SOPFFO.exe", "ahk_exe MetroExodus.exe", "ahk_exe RiftApart.exe", "ahk_exe Forspoken.exe", "ahk_exe hnk.exe", "ahk_exe FFXII_TZA.exe", "ahk_exe helldivers2.exe", "ahk_exe FFX.exe", "ahk_exe HorizonForbiddenWest.exe", "ahk_exe bg3.exe", "ahk_exe re4.exe", "ahk_exe StarRail.exe", "ahk_exe tlou-i.exe", "ahk_exe GenshinImpact.exe", "ahk_exe Dead Space.exe", "ahk_exe destiny2.exe", "ahk_exe MilesMorales.exe", "ahk_exe FallGuys_client_game.exe", "ahk_exe HaloInfinite.exe", "ahk_exe BF2042.exe", "ahk_exe r5apex.exe", "ahk_exe NBA2K24.exe", "ahk_exe ACOdyssey.exe", "ahk_exe witcher3.exe", "ahk_exe Overcooked All You Can Eat.exe", "ahk_exe u4.exe", "ahkexe ff7remake.exe", "ahk_exe ForzaHorizon5.exe", "ahk_exe GoW.exe", "ahk_exe HorizonZeroDawn.exe", "ahk_exe yuzu.exe", "ahk_exe BlackDesert64.exe", "ahk_exe Overwatch.exe", "ahk_exe RDR2.exe", "ahk_exe SOTTR.exe", "ahk_exe SO6.exe", "ahk_exe Spider-Man.exe", "ahk_exe Cyberpunk2077.exe", "ahk_exe FlightSimulator.exe", "ahk_exe ACValhalla.exe", "ahk_exe ds.exe", "ahk_exe DevilMayCry5.exe", "ahk_exe DRAGON QUEST XI S.exe", "ahk_exe ffxv_s.exe", "ahk_exe DyingLightGame_x64_rwdi.exe", "ahk_exe NieRAutomata.exe", "ahk_exe HogwartsLegacy.exe", "ahk_exe re8.exe", "ahk_exe P5R.exe", "ahk_exe Control_DX12.exe", "ahk_exe sekiro.exe", "ahk_exe Cemu.exe", "ahk_exe Gears5.exe", "ahk_exe bfv.exe", "ahk_exe TheDivision2.exe", "ahk_exe Ryujinx.exe", "ahk_exe starwarsbattlefrontii.exe", "ahk_exe tll.exe", "ahk_exe APlagueTaleRequiem_x64.exe", "ahk_exe ffxiv_dx11.exe", "ahk_exe NarakaBladepoint.exe", "ahk_exe ScarletNexus-Win64-Shipping.exe", "ahk_exe Remnant2-Win64-Shipping.exe", "ahk_exe FortniteClient-Win64-Shipping.exe", "ahk_exe Returnal-Win64-Shipping.exe", "ahk_exe DeadIsland-Win64-Shipping.exe", "ahk_exe Sackboy-Win64-Shipping.exe", "ahk_exe KartDrift-Win64-Shipping.exe", "ahk_exe JediSurvivor.exe", "ahk_exe LOP-WinGDK-Shipping.exe", "ahk_exe M1-Win64-Shipping.exe", "ahk_exe Tales of Arise.exe", "ahk_exe PAYDAY3Client-WinGDK-Shipping.exe", "HELLDIVERS™ 2", "ahk_exe SealWhatTheFun.exe", "ahk_exe ItTakesTwo.exe", "ahk_exe Fallout4.exe", "ELDEN RING™", "ahk_exe ItTakesTwo.exe", "ahk_exe AnotherCrabsTreasure.exe", "ahk_exe ItTakesTwo.exe", "ahk_exe ItTakesTwo.exe", "ahk_exe ItTakesTwo.exe", "ahk_exe SO2R.exe", "ahk_exe ItTakesTwo.exe", "ahk_exe ItTakesTwo.exe", "ahk_exe ItTakesTwo.exe", "ahk_exe ItTakesTwo.exe"]

for Program in ProgramList { GroupAdd("ProgramGroup", Program) }

SetTimer(WindowCheck, 1000) ; WindowCheck() { global NewProgram, CurrProgram

if !(WinExist("ahk_group ProgramGroup")) {
    NewProgram := CurrProgram := 0
    return
} else {
    NewProgram := WinGetID()
}

if (NewProgram) && (NewProgram != CurrProgram) {
    if (CurrProgram) {
        ProcessClose(WinGetPID(CurrProgram))
    }
    CurrProgram := NewProgram
}

}

HotIf (FlexPID = 0)

F9:: { Run("H:\Playnite\PC\Playnite.FullscreenApp.exe --hidesplashscreen") ; Run("C:\Program Files\LGTV Companion\LGTV Companion.exe -sethdmi1 Device1 -screenon Device1") ; if (CurrProgram) { WinActivate(CurrProgram) } }

HotIf

F10:: { global FlexPID if (WinExist("ahk_exe flex-launcher.exe")) { FlexPID := WinGetPID("ahk_exe flex-launcher.exe") } else { Run("H:\Playnite\Console\Launcher\focus.ahk",,, &FlexPID) } SetTimer(FlexCheck, 100) FlexCheck() { global FlexPID if (!WinExist("ahk_pid " FlexPID)) { FlexPID := 0 SetTimer(FlexCheck, 0) } else if (WinExist("ahk_pid " FlexPID) && !WinActive("ahk_pid " FlexPID)) { WinActivate("ahk_pid " FlexPID) } } }

HotIf WinActive("ahk_pid " FlexPID) && (FlexPID != 0)

^!Tab:: { Return }

F22:: { global CurrProgram, NewProgram ProcessClose(WinGetPID(CurrProgram)) NewProgram := CurrProgram := 0 }

HotIf

^!+Escape:: { ExitApp() }

TomiBelan commented 2 months ago

Interesting... Maybe it's not completely broken. Still, it's broken enough. I don't think there's anything I could do to fix it. It's like the window is completely invisible to my script.

WinGetProcessPath("A") returns nothing (therefore Win+F2 says "There is no active window") WinExist("ahk_exe H:\SteamLibrary\steamapps\common\Helldivers 2\bin\helldivers2.exe") returns false (therefore Win+F4 says "it's probably closing") When it gains focus, the event notification either doesn't arrive at all, or fails to winget (unclear which (but fatal either way)).

I think Helldivers 2 is just not possible. Though I'm still interested to hear if you test other games.

TomiBelan commented 3 weeks ago

It appears the experimental fix works. I merged fix1 into master. I'll close this issue now. Thanks again for your help with reporting and testing it.

Summary: The first reported subproblem (popup "Error: Invalid parameter(s)") disappeared, but let me know if it happens again. I believe the second subproblem (apps are muted next time they start) is now fixed. The third subproblem (Helldivers 2) is probably unfixable.