2023-12-20 20:50:11 +01:00
|
|
|
package mpvipc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net"
|
2024-01-10 21:56:00 +01:00
|
|
|
|
|
|
|
"git.milar.in/milarin/channel"
|
2023-12-20 20:50:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Command struct {
|
|
|
|
Command []interface{} `json:"command"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response[T any] struct {
|
|
|
|
Data T `json:"data"`
|
|
|
|
RequestID int `json:"request_id"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Event[T any] struct {
|
2024-01-10 21:56:00 +01:00
|
|
|
Data T `json:"data"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
Reason string `json:"reason"`
|
2023-12-20 20:50:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func SendCommand[T any](socket string, cmd *Command) (*Response[T], error) {
|
|
|
|
conn, err := net.Dial("unix", socket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
if err := json.NewEncoder(conn).Encode(cmd); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &Response[T]{}
|
|
|
|
if err := json.NewDecoder(conn).Decode(resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetProperty[T any](socket string, propertyName string) (T, error) {
|
|
|
|
cmd := &Command{[]interface{}{"get_property", propertyName}}
|
|
|
|
resp, err := SendCommand[T](socket, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return *new(T), err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Error != "success" {
|
|
|
|
return *new(T), errors.New(resp.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetProperty[T any](socket string, propertyName string, propertyValue T) error {
|
|
|
|
cmd := &Command{[]interface{}{"set_property", propertyName, propertyValue}}
|
|
|
|
resp, err := SendCommand[T](socket, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Error != "success" {
|
|
|
|
return errors.New(resp.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ObserveProperty[T any](ctx context.Context, socket string, propertyName string) (<-chan T, error) {
|
2024-01-10 21:56:00 +01:00
|
|
|
ch, err := ObserveEvent[T](ctx, socket, propertyName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return channel.MapSuccessive(ch, func(e Event[T]) T { return e.Data }), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ObserveEvent[T any](ctx context.Context, socket string, propertyName string) (<-chan Event[T], error) {
|
|
|
|
out := make(chan Event[T], 10)
|
2023-12-20 20:50:11 +01:00
|
|
|
|
|
|
|
conn, err := net.Dial("unix", socket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
enc := json.NewEncoder(conn)
|
|
|
|
dec := json.NewDecoder(conn)
|
|
|
|
|
|
|
|
cmd := &Command{[]interface{}{"observe_property", 1, propertyName}}
|
|
|
|
if err := enc.Encode(cmd); err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &Response[T]{}
|
|
|
|
if err := json.NewDecoder(conn).Decode(resp); err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Error != "success" {
|
|
|
|
conn.Close()
|
|
|
|
return nil, errors.New(resp.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer conn.Close()
|
|
|
|
defer enc.Encode(&Command{[]interface{}{"unobserve_property", 0}})
|
|
|
|
<-ctx.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
|
|
|
|
for ctx.Err() == nil {
|
|
|
|
event := Event[T]{}
|
|
|
|
if err := dec.Decode(&event); err != nil {
|
|
|
|
break
|
|
|
|
}
|
2024-01-10 21:56:00 +01:00
|
|
|
out <- event
|
2023-12-20 20:50:11 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|