mpvipc/commands.go

60 lines
1.0 KiB
Go
Raw Permalink Normal View History

2024-01-10 19:30:48 +01:00
package mpvipc
2024-01-10 22:25:48 +01:00
import (
"context"
"net"
"time"
)
2024-01-10 19:30:48 +01:00
type LoadFileFlag string
const (
LoadFileReplace LoadFileFlag = "replace"
LoadFileAppend LoadFileFlag = "append"
LoadFileAppendPlay LoadFileFlag = "append-play"
)
func LoadFile(socket string, file string, flags LoadFileFlag) error {
if flags == "" {
flags = LoadFileReplace
}
cmd := &Command{[]interface{}{"loadfile", file, flags}}
_, err := SendCommand[any](socket, cmd)
return err
}
2024-01-10 22:25:48 +01:00
func Quit(socket string) error {
cmd := &Command{Command: []interface{}{"quit"}}
_, err := SendCommand[any](socket, cmd)
return err
}
func IsReady(ctx context.Context, socket string) <-chan bool {
out := make(chan bool, 1)
go func() {
defer close(out)
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
conn, err := net.Dial("unix", socket)
if err == nil {
defer conn.Close()
out <- true
return
}
case <-ctx.Done():
out <- false
return
}
}
}()
return out
}