session shown name parsing, pending refactor

This commit is contained in:
Hane 2024-08-08 15:04:17 +02:00
commit e42dbaa194
2 changed files with 31 additions and 14 deletions

View file

@ -128,11 +128,14 @@ Session::Session(Endpoint* ep, IAudioSessionControl2* sessionControl, size_t idx
this->sessionControl->GetDisplayName(&sessionDisplayName); this->sessionControl->GetDisplayName(&sessionDisplayName);
if (!wcscmp(sessionDisplayName, L"")) { if (!wcscmp(sessionDisplayName, L"")) {
std::wstring exePath; std::wstring exePath;
if (getExePath(pid, &exePath)) if (getExePath(pid, &exePath)) {
fetchName(exePath, pid); if (fetchName(exePath, pid)) goto nameFound;
else this->sessionName = std::wstring(LSTRING_UNNAMED_SESSION); }
if (fetchNameViaWindowName(pid, &this->sessionName)) goto nameFound;
} else } else
this->sessionName = std::wstring(sessionDisplayName); this->sessionName = std::wstring(sessionDisplayName);
nameFound:
CoTaskMemFree(sessionDisplayName); CoTaskMemFree(sessionDisplayName);
} }
} }
@ -207,7 +210,7 @@ bool Session::getExePath(DWORD pid, std::wstring *exePath) {
HANDLE processList = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid); HANDLE processList = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
if (processList == INVALID_HANDLE_VALUE) { if (processList == INVALID_HANDLE_VALUE) {
log_wdebugcpp(L"aye no procname."); log_wdebugcpp(L"aye no procname. -> " + std::to_wstring(GetLastError()));
return false; return false;
} }
@ -351,7 +354,7 @@ bool Session::fetchNameViaMSIX(std::wstring exePath, DWORD pid, std::wstring *se
} }
bool Session::fetchNameViaWindowName(std::wstring exePath, DWORD pid, std::wstring *sessionName) { bool Session::fetchNameViaWindowName(DWORD pid, std::wstring *sessionName) {
//lParam is documented as in, so... Beware of future explosions, ig? //lParam is documented as in, so... Beware of future explosions, ig?
std::pair<HWND, DWORD> params = { 0, pid }; std::pair<HWND, DWORD> params = { 0, pid };
@ -359,7 +362,11 @@ bool Session::fetchNameViaWindowName(std::wstring exePath, DWORD pid, std::wstri
auto pParams = (std::pair<HWND, DWORD>*)(lParam); auto pParams = (std::pair<HWND, DWORD>*)(lParam);
DWORD processId; DWORD processId;
if (GetWindowThreadProcessId(hwnd, &processId) && processId == pParams->second) { //&& GetWindow(hwnd, GW_OWNER) == 0
if (IsWindowVisible(hwnd) && GetWindowThreadProcessId(hwnd, &processId) && processId == pParams->second) {
int length = GetWindowTextLength(hwnd);
if (!length) return TRUE;
// Stop enumerating // Stop enumerating
SetLastError(-1); SetLastError(-1);
pParams->first = hwnd; pParams->first = hwnd;
@ -371,6 +378,7 @@ bool Session::fetchNameViaWindowName(std::wstring exePath, DWORD pid, std::wstri
} , (LPARAM)&params); } , (LPARAM)&params);
if(!result && GetLastError() == -1 && params.first) { if(!result && GetLastError() == -1 && params.first) {
//todo: double-dipping length... Not a fan
int length = GetWindowTextLength(params.first); int length = GetWindowTextLength(params.first);
wchar_t* buffer = new wchar_t[length + 1]; wchar_t* buffer = new wchar_t[length + 1];
GetWindowTextW(params.first, buffer, length + 1); GetWindowTextW(params.first, buffer, length + 1);
@ -381,13 +389,22 @@ bool Session::fetchNameViaWindowName(std::wstring exePath, DWORD pid, std::wstri
return false; return false;
} }
void Session::fetchName(std::wstring exePath, DWORD pid) { bool Session::fetchName(std::wstring exePath, DWORD pid) {
/*
* if(fetchNameViaWindowName(exePath, pid, &this->sessionName))
* return;
* else if(fetchNameViaMSIX(exePath, pid, &this->sessionName))
* return;
* else if(!fetchNameViaFD(exePath, pid, &this->sessionName))
* this->sessionName = exePath;
*/
if(fetchNameViaFD(exePath, pid, &this->sessionName)) if(fetchNameViaFD(exePath, pid, &this->sessionName))
return; return true;
else if(fetchNameViaMSIX(exePath, pid, &this->sessionName)) return fetchNameViaMSIX(exePath, pid, &this->sessionName);
return; //else if(!fetchNameViaWindowName(exePath, pid, &this->sessionName))
else if(!fetchNameViaWindowName(exePath, pid, &this->sessionName)) /// this->sessionName = exePath;
this->sessionName = exePath;
} }
//todo: conflicting names. change callback name //todo: conflicting names. change callback name

View file

@ -49,10 +49,10 @@ class Session {
private: private:
bool getExePath(DWORD pid, /*out*/ std::wstring *exePath); bool getExePath(DWORD pid, /*out*/ std::wstring *exePath);
void fetchName(std::wstring exePath, DWORD pid); bool fetchName(std::wstring exePath, DWORD pid);
bool fetchNameViaFD(std::wstring exePath, DWORD pid, /*out*/ std::wstring *sessionName); bool fetchNameViaFD(std::wstring exePath, DWORD pid, /*out*/ std::wstring *sessionName);
bool fetchNameViaMSIX(std::wstring exePath, DWORD pid, /*out*/ std::wstring *sessionName); bool fetchNameViaMSIX(std::wstring exePath, DWORD pid, /*out*/ std::wstring *sessionName);
bool fetchNameViaWindowName(std::wstring exePath, DWORD pid, /*out*/ std::wstring *sessionName); bool fetchNameViaWindowName(DWORD pid, /*out*/ std::wstring *sessionName);
/* std::wstring fetchProcessName(DWORD pid); */ /* std::wstring fetchProcessName(DWORD pid); */
std::wstring sessionName; std::wstring sessionName;
SessionState sessionState; SessionState sessionState;