-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
271 lines (237 loc) · 5.33 KB
/
main.lua
File metadata and controls
271 lines (237 loc) · 5.33 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
local uv = require("uv")
local ffi = require("ffi")
local isWindows = ffi.os == "Windows"
local function split(str, sep)
local ret = {}
local pos = 1
for i = 1, string.len(str) do
local s, e = string.find(str, sep, pos)
if not s then break end
ret[i] = string.sub(str, pos, s - 1)
pos = e + 1
end
ret[#ret + 1] = string.sub(str, pos)
return ret
end
local sockPath
local configDir = os.getenv("MPV_HOME")
if isWindows then
sockPath = "\\\\.\\pipe\\umpv"
-- scan for mpv in path and check for portable_config
if configDir == nil then
for _, path in ipairs(split(os.getenv("PATH"), ";")) do
local f = uv.fs_stat(path .. "/mpv.exe")
if f ~= nil then
local f2 = uv.fs_stat(path .. "/portable_config/umpv.conf")
if f2 ~= nil then
configDir = path .. "/portable_config"
break
end
end
end
end
-- check global
if configDir == nil then
local path = os.getenv("APPDATA") .. "/mpv"
local f = uv.fs_stat(path .. "/umpv.conf")
if f ~= nil then
configDir = path
end
end
else
local sockDir = os.getenv("UMPV_SOCKET_DIR") or os.getenv("XDG_RUNTIME_DIR") or os.getenv("HOME") or
os.getenv("TMPDIR")
if sockDir == nil then
print("Could not determine a base directory for the socket. " ..
"Ensure that one of the following environment variables is set: " ..
"UMPV_SOCKET_DIR, XDG_RUNTIME_DIR, HOME or TMPDIR.")
os.exit(1)
return
end
sockPath = sockDir .. "/.umpv"
-- check global
if configDir == nil then
local confDir = os.getenv("XDG_CONFIG_DIR")
if confDir == nil then
confDir = os.getenv("HOME") .. "/.config"
end
local path = confDir .. "/mpv"
local f = uv.fs_stat(path .. "/umpv.conf")
if f ~= nil then
configDir = path
end
end
end
-- check next to exe
if configDir == nil then
local path = uv.cwd()
local f = uv.fs_stat(path .. "/umpv.conf")
if f ~= nil then
configDir = path
end
end
local sock = uv.new_pipe(false)
local function canSend()
local stat = uv.fs_stat(sockPath)
return stat ~= nil
end
if not isWindows and canSend() then
sock:connect(sockPath, function(err)
if err ~= nil then
local ok = uv.fs_unlink(sockPath)
assert(ok, "failed to remove socket")
else
sock:close()
sock = uv.new_pipe(false)
end
end)
uv.run()
end
local function send(data)
sock:connect(sockPath, function(err)
assert(not err, err)
if type(data) == "table" then
for _, d in pairs(data) do
sock:write(d .. "\n")
end
else
sock:write(data .. "\n")
end
sock:close()
end)
end
local function sanitize(str)
return str:gsub("\\", "\\\\"):gsub('"', '\\"'):gsub("\n", "\\n")
end
local loadFlag = "append-play"
local function formatFile(path, args)
local msg = string.format('raw loadfile "%s" %s', sanitize(path), loadFlag)
if #args > 0 then
msg = msg .. string.format(' 0 "%s"', sanitize(table.concat(args, ",")))
end
return msg
end
local function sendFile(path, args)
for i, v in ipairs(args) do
args[i] = v:sub(3)
end
if type(path) == "table" then
local commands = {}
for _, p in ipairs(path) do
commands[#commands + 1] = formatFile(p, args)
end
send(commands)
else
send(formatFile(path, args))
end
end
local function merge(...)
local tbl = {}
for _, t in ipairs({...}) do
if type(t) == "table" then
for _, v in ipairs(t) do
tbl[#tbl + 1] = v
end
else
tbl[#tbl + 1] = t
end
end
return tbl
end
local mpvArgs = {}
local keepProcess = false
local function start(files)
local exe = os.getenv("MPV") or "mpv"
mpvArgs[#mpvArgs + 1] = "--input-ipc-server=" .. sockPath
mpvArgs[#mpvArgs + 1] = "--"
for i, f in ipairs(files) do
files[i] = '"' .. f .. '"'
end
if keepProcess then
os.execute(table.concat(merge(exe, mpvArgs, files), " "))
else
uv.spawn(exe, {
args = merge(mpvArgs, files),
verbatim = true,
detached = true,
hide = true
})
end
end
local validLoadFlags = {
replace = true,
append = true,
["append-play"] = true,
["insert-next"] = true,
["insert-next-play"] = true,
}
local parseLocalArg = function(arg, isConfig) end
local argSwitch = {
["ipc-server"] = function(v)
sockPath = v
end,
["loadfile-flag"] = function(v)
if validLoadFlags[v] then
loadFlag = v
end
end,
["config"] = function(v)
for line in io.lines(v) do
if line:match("^#") then goto continue end
parseLocalArg(line, true)
::continue::
end
end,
["keep-process"] = function(v)
keepProcess = v == "yes"
end,
}
parseLocalArg = function(arg, isConfig)
local eq = split(arg, "=")
local k, v = eq[1], eq[2]
if isConfig == true and k == "config" then return end
local cb = argSwitch[k]
if cb then
pcall(cb, v)
end
end
if configDir ~= nil then
pcall(argSwitch.config, configDir .. "/umpv.conf")
end
local argsLocal = true
local hasFlags = table.concat(args, " "):find("%-%-") ~= nil
local paths = {}
for _, arg in ipairs(args) do
if hasFlags then
if arg:match("^%-%-") then
if arg == "--" then
argsLocal = false
elseif argsLocal then
parseLocalArg(arg:sub(3), false)
else
mpvArgs[#mpvArgs + 1] = arg
end
else
paths[#paths + 1] = arg
end
else
paths[#paths + 1] = arg
end
end
if canSend() then
pcall(sendFile, paths, mpvArgs)
if keepProcess then
-- NB: process exits if nothing is sent to stdout whyyyyyyy
print("")
uv.new_idle():start(function()
if not canSend() then
os.exit(0)
else
uv.sleep(1000)
end
end)
end
uv.run()
else
pcall(start, paths)
end