Skip to content

Commit

Permalink
Add Linked App Foreground-Only option + Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
SaifAqqad committed Jun 11, 2023
1 parent ab99f01 commit bc0c73f
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 99 deletions.
2 changes: 1 addition & 1 deletion src/AuraService.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ global parentPID := A_Args[1]
, lastTask
, A_IsDebug := A_Args[2] = "/debug"

global parentHwnd := util_getMainWindowHwnd(parentPID)
global parentHwnd := util_getAhkMainWindowHwnd(parentPID)

if (!parentPID || parentPID == servicePID)
ExitService(-1)
Expand Down
59 changes: 58 additions & 1 deletion src/Lib/WinUtils.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,57 @@ util_isProcessElevated(vPID){
return vRet ? vIsElevated : -1
}

util_getMainWindowHwnd(p_pid){
util_getRunningProcesses(includeHidden:=0) {
static _sysProcesses := new StackSet("svchost.exe", "explorer.exe", "Taskmgr.exe", "SystemSettings.exe")

_hiddenValue:= A_DetectHiddenWindows
if (includeHidden)
DetectHiddenWindows, On

pSet:= {}
WinGet, pList, List
loop %pList%
{
pHwnd:= pList%A_Index%

WinGet, pPath, ProcessPath, ahk_id %pHwnd%
if (!pPath)
continue
pSplitPath := util_splitPath(pPath)

if(pSplitPath.fileExt != "exe" || pSet.HasKey(pSplitPath.fileName) || _sysProcesses.exists(pSplitPath.fileName))
continue

WinGetTitle, pTitle, ahk_id %pHwnd%
pInfo:= util_getFileInfo(pPath)

pSet[pSplitPath.fileName] := { hwnd: pHwnd
, path: pPath
, name: pSplitPath.fileName
, title: pTitle
, description: pInfo.FileDescription }
}
DetectHiddenWindows, % _hiddenValue

return pSet
}

util_getFileInfo(lptstrFilename) {
List := "Comments InternalName ProductName CompanyName LegalCopyright ProductVersion"
. " FileDescription LegalTrademarks PrivateBuild FileVersion OriginalFilename SpecialBuild"
dwLen := DllCall("Version.dll\GetFileVersionInfoSize", "Str", lptstrFilename, "Ptr", 0)
dwLen := VarSetCapacity( lpData, dwLen + A_PtrSize)
DllCall("Version.dll\GetFileVersionInfo", "Str", lptstrFilename, "UInt", 0, "UInt", dwLen, "Ptr", &lpData)
DllCall("Version.dll\VerQueryValue", "Ptr", &lpData, "Str", "\VarFileInfo\Translation", "PtrP", lplpBuffer, "PtrP", puLen )
sLangCP := Format("{:04X}{:04X}", NumGet(lplpBuffer+0, "UShort"), NumGet(lplpBuffer+2, "UShort"))
i := {}
Loop, Parse, % List, %A_Space%
DllCall("Version.dll\VerQueryValue", "Ptr", &lpData, "Str", "\StringFileInfo\" sLangCp "\" A_LoopField, "PtrP", lplpBuffer, "PtrP", puLen )
? i[A_LoopField] := StrGet(lplpBuffer, puLen) : ""
return i
}

util_getAhkMainWindowHwnd(p_pid){
WinWait, % "ahk_pid " p_pid, , 1

; list all windows
Expand Down Expand Up @@ -196,6 +246,13 @@ util_toString(obj){
return output_str
}

util_firstNonEmpty(params*) {
for _, param in params
if (param && Trim(param))
return param
return ""
}

; VerCmp() for Windows by SKAN on D35T/D37L @ tiny.cc/vercmp
util_VerCmp(V1, V2) {
return ( ( V1 := Format("{:04X}{:04X}{:04X}{:04X}", StrSplit(V1 . "...", ".",, 5)*) )
Expand Down
42 changes: 30 additions & 12 deletions src/MicMute.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,6 @@ initilizeMicMute(default_profile:="", exportConfig:=1){
setTimer, checkConfigDiff, 3000
;update theme variables
updateSysTheme()
;initilize tray
tray_init()
if(config_obj.AllowUpdateChecker==-1){
MsgBox, 36, MicMute, Allow MicMute to connect to the internet and check for updates on startup?
IfMsgBox, Yes
Expand Down Expand Up @@ -421,24 +419,44 @@ checkConfigDiff(){

checkLinkedApps(){
if(watched_profile){
WinGet, minState, MinMax, % "ahk_exe " . watched_profile.LinkedApp ; -1 -> minimized
if(!WinExist("ahk_exe " . watched_profile.LinkedApp) || minState == -1){
if(!isAppActive(watched_profile.LinkedApp)){
util_log("[Main] Linked app closed: " . watched_profile.LinkedApp)
switchProfile(config_obj.DefaultProfile)
watched_profile:=""
switchProfile(config_obj.DefaultProfile)
}
return
}
for _i, prof in watched_profiles {
WinGet, minState, MinMax, % "ahk_exe " . prof.LinkedApp
if(WinExist("ahk_exe " . prof.LinkedApp) && (minState!="" && minState!=-1)){
util_log("[Main] Detected linked app: " . prof.LinkedApp)
watched_profile:= prof
switchProfile(prof.ProfileName)

for _i, p in watched_profiles {
if(isAppActive(p.LinkedApp)){
util_log("[Main] Detected linked app: " . p.LinkedApp)
watched_profile:= p
switchProfile(p.ProfileName)
break
}
}
}

isAppActive(appFile){
if (current_profile.ForegroundAppsOnly) {
windowExists := WinExist("ahk_exe " . appFile)
WinGet, minState, MinMax, ahk_exe %appFile%

; An app is active in the foreground if it has a window that's not hidden
; Minimized windows are considered hidden
return windowExists && minState !== "" && minState !== -1
} else {
_hiddenValue := A_DetectHiddenWindows
DetectHiddenWindows, On

; Try to get the PID
WinGet, appPid, PID, ahk_exe %appFile%

DetectHiddenWindows, %_hiddenValue%
return appPid !== ""
}
}

onUpdateState(microphone){
if (mic_controllers.Length() == 1) {
overlay_wnd.setState(microphone.state)
Expand Down Expand Up @@ -482,7 +500,7 @@ reloadMicMute(p_profile:=""){
}

parseArgs(){
arg_regex:= "i)\/([\w]+)(=(.+))?"
arg_regex:= "i)[\/\-]?([\w]+)(=(.+))?"
for _i, arg in A_Args {
match:= RegExMatch(arg, arg_regex, val)
if(!match)
Expand Down
Loading

0 comments on commit bc0c73f

Please sign in to comment.