line endings switch, and add missed stubs / go.mod update

This commit is contained in:
Drew Weymouth
2025-01-15 17:49:36 -08:00
parent cb01703776
commit 7f3895975e
4 changed files with 223 additions and 208 deletions
+159 -159
View File
@@ -1,159 +1,159 @@
//go:build windows //go:build windows
package backend package backend
/* /*
#cgo CFLAGS: -I . #cgo CFLAGS: -I .
void btn_callback_cgo(int in); void btn_callback_cgo(int in);
void seek_callback_cgo(int in); void seek_callback_cgo(int in);
*/ */
import "C" import "C"
import ( import (
"errors" "errors"
"fmt" "fmt"
"unsafe" "unsafe"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
) )
type SMTCPlaybackState int type SMTCPlaybackState int
type SMTCButton int type SMTCButton int
const ( const (
// constants from smtc.h in github.com/supersonic-app/smtc-dll // constants from smtc.h in github.com/supersonic-app/smtc-dll
SMTCPlaybackStateStopped SMTCPlaybackState = 2 SMTCPlaybackStateStopped SMTCPlaybackState = 2
SMTCPlaybackStatePlaying SMTCPlaybackState = 3 SMTCPlaybackStatePlaying SMTCPlaybackState = 3
SMTCPlaybackStatePaused SMTCPlaybackState = 4 SMTCPlaybackStatePaused SMTCPlaybackState = 4
SMTCButtonPlay SMTCButton = 0 SMTCButtonPlay SMTCButton = 0
SMTCButtonPause SMTCButton = 1 SMTCButtonPause SMTCButton = 1
SMTCButtonStop SMTCButton = 2 SMTCButtonStop SMTCButton = 2
SMTCButtonPrevious SMTCButton = 4 SMTCButtonPrevious SMTCButton = 4
SMTCButtonNext SMTCButton = 5 SMTCButtonNext SMTCButton = 5
) )
type SMTC struct { type SMTC struct {
dll *windows.DLL dll *windows.DLL
onButtonPressed func(SMTCButton) onButtonPressed func(SMTCButton)
onSeek func(int) onSeek func(int)
} }
var smtcInstance *SMTC var smtcInstance *SMTC
func InitSMTCForWindow(hwnd uintptr) (*SMTC, error) { func InitSMTCForWindow(hwnd uintptr) (*SMTC, error) {
dll, err := windows.LoadDLL("smtc.dll") dll, err := windows.LoadDLL("smtc.dll")
if err != nil { if err != nil {
return nil, err return nil, err
} }
proc, err := dll.FindProc("InitializeForWindow") proc, err := dll.FindProc("InitializeForWindow")
if err != nil { if err != nil {
return nil, err return nil, err
} }
hr, _, _ := proc.Call(hwnd, uintptr(unsafe.Pointer(C.btn_callback_cgo)), uintptr(unsafe.Pointer(C.seek_callback_cgo))) hr, _, _ := proc.Call(hwnd, uintptr(unsafe.Pointer(C.btn_callback_cgo)), uintptr(unsafe.Pointer(C.seek_callback_cgo)))
if hr < 0 { if hr < 0 {
return nil, fmt.Errorf("InitializeForWindow failed with HRESULT=%d", hr) return nil, fmt.Errorf("InitializeForWindow failed with HRESULT=%d", hr)
} }
smtcInstance = &SMTC{dll: dll} smtcInstance = &SMTC{dll: dll}
return smtcInstance, nil return smtcInstance, nil
} }
func (s *SMTC) OnButtonPressed(f func(SMTCButton)) { func (s *SMTC) OnButtonPressed(f func(SMTCButton)) {
s.onButtonPressed = f s.onButtonPressed = f
} }
func (s *SMTC) OnSeek(f func(millis int)) { func (s *SMTC) OnSeek(f func(millis int)) {
s.onSeek = f s.onSeek = f
} }
func (s *SMTC) Shutdown() { func (s *SMTC) Shutdown() {
if s.dll == nil { if s.dll == nil {
return return
} }
proc, err := s.dll.FindProc("Destroy") proc, err := s.dll.FindProc("Destroy")
if err == nil { if err == nil {
proc.Call() proc.Call()
} }
s.dll.Release() s.dll.Release()
s.dll = nil s.dll = nil
smtcInstance = nil smtcInstance = nil
} }
func (s *SMTC) UpdatePlaybackState(state SMTCPlaybackState) error { func (s *SMTC) UpdatePlaybackState(state SMTCPlaybackState) error {
if s.dll == nil { if s.dll == nil {
return errors.New("SMTC DLL not available") return errors.New("SMTC DLL not available")
} }
proc, err := s.dll.FindProc("UpdatePlaybackState") proc, err := s.dll.FindProc("UpdatePlaybackState")
if err != nil { if err != nil {
return err return err
} }
if hr, _, _ := proc.Call(uintptr(state)); hr < 0 { if hr, _, _ := proc.Call(uintptr(state)); hr < 0 {
return fmt.Errorf("UpdatePlaybackState failed with HRESULT=%d", hr) return fmt.Errorf("UpdatePlaybackState failed with HRESULT=%d", hr)
} }
return nil return nil
} }
func (s *SMTC) UpdateMetadata(title, artist string) error { func (s *SMTC) UpdateMetadata(title, artist string) error {
if s.dll == nil { if s.dll == nil {
return errors.New("SMTC DLL not available") return errors.New("SMTC DLL not available")
} }
utfTitle, err := windows.UTF16PtrFromString(title) utfTitle, err := windows.UTF16PtrFromString(title)
if err != nil { if err != nil {
return err return err
} }
utfArtist, err := windows.UTF16PtrFromString(artist) utfArtist, err := windows.UTF16PtrFromString(artist)
if err != nil { if err != nil {
return err return err
} }
proc, err := s.dll.FindProc("UpdateMetadata") proc, err := s.dll.FindProc("UpdateMetadata")
if err != nil { if err != nil {
return err return err
} }
hr, _, _ := proc.Call(uintptr(unsafe.Pointer(utfTitle)), uintptr(unsafe.Pointer(utfArtist))) hr, _, _ := proc.Call(uintptr(unsafe.Pointer(utfTitle)), uintptr(unsafe.Pointer(utfArtist)))
if hr < 0 { if hr < 0 {
return fmt.Errorf("UpdateMetadata failed with HRESULT=%d", hr) return fmt.Errorf("UpdateMetadata failed with HRESULT=%d", hr)
} }
return nil return nil
} }
func (s *SMTC) UpdatePosition(positionMillis, durationMillis int) error { func (s *SMTC) UpdatePosition(positionMillis, durationMillis int) error {
if s.dll == nil { if s.dll == nil {
return errors.New("SMTC DLL not available") return errors.New("SMTC DLL not available")
} }
proc, err := s.dll.FindProc("UpdatePosition") proc, err := s.dll.FindProc("UpdatePosition")
if err != nil { if err != nil {
return err return err
} }
hr, _, _ := proc.Call(uintptr(positionMillis), uintptr(durationMillis)) hr, _, _ := proc.Call(uintptr(positionMillis), uintptr(durationMillis))
if hr < 0 { if hr < 0 {
return fmt.Errorf("UpdatePosition failed with HRESULT=%d", hr) return fmt.Errorf("UpdatePosition failed with HRESULT=%d", hr)
} }
return nil return nil
} }
//export btnCallback //export btnCallback
func btnCallback(in int) { func btnCallback(in int) {
if smtcInstance != nil && smtcInstance.onButtonPressed != nil { if smtcInstance != nil && smtcInstance.onButtonPressed != nil {
smtcInstance.onButtonPressed(SMTCButton(in)) smtcInstance.onButtonPressed(SMTCButton(in))
} }
} }
//export seekCallback //export seekCallback
func seekCallback(millis int) { func seekCallback(millis int) {
if smtcInstance != nil && smtcInstance.onSeek != nil { if smtcInstance != nil && smtcInstance.onSeek != nil {
smtcInstance.onSeek(millis) smtcInstance.onSeek(millis)
} }
} }
+16 -16
View File
@@ -1,16 +1,16 @@
//go:build windows //go:build windows
package backend package backend
/* /*
void btn_callback_cgo(int in) { void btn_callback_cgo(int in) {
void btnCallback(int); void btnCallback(int);
btnCallback(in); btnCallback(in);
} }
void seek_callback_cgo(int in) { void seek_callback_cgo(int in) {
void seekCallback(int); void seekCallback(int);
seekCallback(in); seekCallback(in);
} }
*/ */
import "C" import "C"
+47 -32
View File
@@ -1,32 +1,47 @@
//go:build !windows //go:build !windows
package backend package backend
import "errors" import "errors"
type SMTCPlaybackState int type SMTCPlaybackState int
type SMTCButton int
const (
SMTCPlaybackStateStopped SMTCPlaybackState = 0 const (
SMTCPlaybackStatePlaying SMTCPlaybackState = 1 // constants from smtc.h in github.com/supersonic-app/smtc-dll
SMTCPlaybackStatePaused SMTCPlaybackState = 2 SMTCPlaybackStateStopped SMTCPlaybackState = 2
) SMTCPlaybackStatePlaying SMTCPlaybackState = 3
SMTCPlaybackStatePaused SMTCPlaybackState = 4
type SMTC struct{}
SMTCButtonPlay SMTCButton = 0
var smtcUnsupportedErr = errors.New("SMTC is not supported on this platformo") SMTCButtonPause SMTCButton = 1
SMTCButtonStop SMTCButton = 2
func InitSMTCForWindow(hwnd uintptr) (*SMTC, error) { SMTCButtonPrevious SMTCButton = 4
return nil, smtcUnsupportedErr SMTCButtonNext SMTCButton = 5
} )
func (s *SMTC) UpdatePlaybackState(state SMTCPlaybackState) error { type SMTC struct{}
return smtcUnsupportedErr
} var smtcUnsupportedErr = errors.New("SMTC is not supported on this platformo")
func (s *SMTC) UpdateMetadata(title, artist string) error { func InitSMTCForWindow(hwnd uintptr) (*SMTC, error) {
return smtcUnsupportedErr return nil, smtcUnsupportedErr
} }
func (s *SMTC) Shutdown() { func (s *SMTC) OnButtonPressed(func(SMTCButton)) {}
}
func (s *SMTC) OnSeek(f func(millis int)) {}
func (s *SMTC) Shutdown() {}
func (s *SMTC) UpdatePlaybackState(state SMTCPlaybackState) error {
return smtcUnsupportedErr
}
func (s *SMTC) UpdateMetadata(title, artist string) error {
return smtcUnsupportedErr
}
func (s *SMTC) UpdatePosition(positionMillis, durationMillis int) error {
return smtcUnsupportedErr
}
+1 -1
View File
@@ -20,6 +20,7 @@ require (
github.com/supersonic-app/go-subsonic v0.0.0-20241224013245-9b2841f3711d github.com/supersonic-app/go-subsonic v0.0.0-20241224013245-9b2841f3711d
github.com/zalando/go-keyring v0.2.1 github.com/zalando/go-keyring v0.2.1
golang.org/x/net v0.25.0 golang.org/x/net v0.25.0
golang.org/x/sys v0.20.0
golang.org/x/text v0.16.0 golang.org/x/text v0.16.0
) )
@@ -50,7 +51,6 @@ require (
github.com/yuin/goldmark v1.7.1 // indirect github.com/yuin/goldmark v1.7.1 // indirect
golang.org/x/image v0.18.0 // indirect golang.org/x/image v0.18.0 // indirect
golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a // indirect golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a // indirect
golang.org/x/sys v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )