From ff0fe9ca044f3dcbfcb2702eaf153cb739bf719e Mon Sep 17 00:00:00 2001 From: lnu Date: Sat, 14 Nov 2020 09:49:40 +0100 Subject: [PATCH] feat: explanations for api call in spotify windows --- environment_windows_win32.go | 7 +++++++ segment_spotify_windows.go | 2 +- segment_spotify_windows_test.go | 3 +-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/environment_windows_win32.go b/environment_windows_win32.go index 01c9411a..4ffaae98 100644 --- a/environment_windows_win32.go +++ b/environment_windows_win32.go @@ -118,6 +118,7 @@ var ( ) // EnumWindows call EnumWindows from user32 and returns all active windows +// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumwindows func EnumWindows(enumFunc, lparam uintptr) (err error) { r1, _, e1 := syscall.Syscall(procEnumWindows.Addr(), 2, enumFunc, lparam, 0) if r1 == 0 { @@ -131,6 +132,7 @@ func EnumWindows(enumFunc, lparam uintptr) (err error) { } // GetWindowText returns the title and text of a window from a window handle +// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowtextw func GetWindowText(hwnd syscall.Handle, str *uint16, maxCount int32) (length int32, err error) { r0, _, e1 := syscall.Syscall(procGetWindowTextW.Addr(), 3, uintptr(hwnd), uintptr(unsafe.Pointer(str)), uintptr(maxCount)) length = int32(r0) @@ -167,6 +169,8 @@ func GetWindowTitle(pid int, windowTitleRegex string) (syscall.Handle, string, e } title = syscall.UTF16ToString(b) if compiledRegex.MatchString(title) { + // will cause EnumWindows to return 0 (error) + // but we don't want to enumerate all windows since we got what we want hwnd = h return 0 } @@ -175,6 +179,9 @@ func GetWindowTitle(pid int, windowTitleRegex string) (syscall.Handle, string, e return 1 // continue enumeration }) // Enumerates all top-level windows on the screen + // The error is not checked because if EnumWindows is stopped bofere enumerating all windows + // it returns 0(error occurred) instead of 1(success) + // In our case, title will equal "" or the title of the window anyway _ = EnumWindows(cb, 0) return hwnd, title, nil } diff --git a/segment_spotify_windows.go b/segment_spotify_windows.go index aa1a144e..138448d6 100644 --- a/segment_spotify_windows.go +++ b/segment_spotify_windows.go @@ -16,7 +16,7 @@ func (s *spotify) enabled() bool { if !strings.Contains(spotifyWindowTitle, " - ") { s.status = "stopped" - return true + return false } infos := strings.Split(spotifyWindowTitle, " - ") diff --git a/segment_spotify_windows_test.go b/segment_spotify_windows_test.go index 3d378cf1..6b63b8ca 100644 --- a/segment_spotify_windows_test.go +++ b/segment_spotify_windows_test.go @@ -47,6 +47,5 @@ func TestSpotifyWindowsEnabledAndSpotifyStopped(t *testing.T) { title: "Spotify premium", } s := bootStrapSpotifyWindowsTest(args) - assert.Equal(t, true, s.enabled()) - assert.Equal(t, "\uf04d ", s.string()) + assert.Equal(t, false, s.enabled()) }