/
options.go
167 lines (136 loc) · 3.42 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package open_in_mpv
import (
"encoding/json"
"fmt"
"net/url"
"strings"
)
// The Options struct defines a model for the data contained in the mpv://
// URL and acts as a command generator (both CLI and IPC) to spawn and
// communicate with an mpv player window.
type Options struct {
Flags string
Player string
Url string
Enqueue bool
Fullscreen bool
NewWindow bool
Pip bool
NeedsIpc bool
}
// Utility object to marshal an mpv-compatible JSON command. As defined in the
// documentation, a valid IPC command is a JSON array of strings
type enqueueCmd struct {
Command []string `json:"command,omitempty"`
}
// Default constructor for an Option object
func NewOptions() Options {
return Options{
Flags: "",
Player: "mpv",
Url: "",
Enqueue: false,
Fullscreen: false,
NewWindow: false,
Pip: false,
NeedsIpc: false,
}
}
// Parse a mpv:// URL and populate the current Options
func (o *Options) Parse(uri string) error {
u, err := url.Parse(uri)
if err != nil {
return err
}
if u.Scheme != "mpv" {
return fmt.Errorf("Unsupported protocol: %s", u.Scheme)
}
if u.Path != "/open" {
return fmt.Errorf("Unsupported method: %s", u.Path)
}
if len(u.RawQuery) < 2 {
return fmt.Errorf("Empty or malformed query: %s", u.RawQuery)
}
o.Flags, err = url.QueryUnescape(u.Query().Get("flags"))
if err != nil {
return err
}
o.Url, err = url.QueryUnescape(u.Query().Get("url"))
if err != nil {
return err
}
if p, ok := u.Query()["player"]; ok {
o.Player = p[0]
}
if GetPlayerInfo(o.Player) == nil {
return fmt.Errorf("Unsupported player: %s", o.Player)
}
o.Enqueue = u.Query().Get("enqueue") == "1"
o.Fullscreen = u.Query().Get("fullscreen") == "1"
o.NewWindow = u.Query().Get("new_window") == "1"
o.Pip = u.Query().Get("pip") == "1"
o.NeedsIpc = GetPlayerInfo(o.Player).NeedsIpc
return nil
}
// Parses flag overrides and returns the final flags
func (o Options) overrideFlags() string {
var (
ret []string
star bool
)
pInfo := GetPlayerInfo(o.Player)
if pInfo == nil {
return ""
}
_, star = pInfo.FlagOverrides["*"]
for _, flag := range strings.Split(o.Flags, " ") {
if star {
stripped := strings.TrimLeft(flag, "-")
replaced := strings.ReplaceAll(pInfo.FlagOverrides["*"], `%s`, stripped)
ret = append(ret, replaced)
} else {
if override, ok := pInfo.FlagOverrides[flag]; ok {
stripped := strings.TrimLeft(flag, "-")
ret = append(ret, strings.ReplaceAll(override, `%s`, stripped))
}
}
}
return strings.Join(ret, " ")
}
// Builds a CLI command used to invoke the player with the appropriate arguments
func (o Options) GenerateCommand() []string {
var ret []string
pInfo := GetPlayerInfo(o.Player)
if o.Fullscreen {
ret = append(ret, pInfo.Fullscreen)
}
if o.Pip {
ret = append(ret, pInfo.Pip)
}
if o.Flags != "" {
if len(pInfo.FlagOverrides) == 0 {
ret = append(ret, o.Flags)
} else {
ret = append(ret, o.overrideFlags())
}
}
ret = append(ret, o.Url)
return ret
}
// Builds the IPC command needed to enqueue videos if the player requires it
func (o Options) GenerateIPC() ([]byte, error) {
if !o.NeedsIpc {
return []byte{}, fmt.Errorf("This player does not need IPC")
}
cmd := enqueueCmd{
[]string{"loadfile", o.Url, "append-play"},
}
ret, err := json.Marshal(cmd)
if err != nil {
return []byte{}, err
}
if ret[len(ret)-1] != '\n' {
ret = append(ret, '\n')
}
return ret, nil
}