Skip to content

Commit

Permalink
feat: explanations for api call in spotify windows
Browse files Browse the repository at this point in the history
  • Loading branch information
lnu authored and JanDeDobbeleer committed Nov 14, 2020
1 parent b361804 commit ff0fe9c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
7 changes: 7 additions & 0 deletions environment_windows_win32.go
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion segment_spotify_windows.go
Expand Up @@ -16,7 +16,7 @@ func (s *spotify) enabled() bool {

if !strings.Contains(spotifyWindowTitle, " - ") {
s.status = "stopped"
return true
return false
}

infos := strings.Split(spotifyWindowTitle, " - ")
Expand Down
3 changes: 1 addition & 2 deletions segment_spotify_windows_test.go
Expand Up @@ -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())
}

0 comments on commit ff0fe9c

Please sign in to comment.