| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
> |
| 5 |
m> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
local tonumber = tonumber |
| 27 |
local math = { floor = math.floor } |
| 28 |
local type = type |
| 29 |
|
| 30 |
local helpers = require"vicious.helpers" |
| 31 |
local spawn = require"vicious.spawn" |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
local mpd_all = {} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
local function cbool(number) |
| 41 |
return type(number) == "number" and number ~= 0 or number |
| 42 |
end |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
local function format_progress(elapsed, duration) |
| 47 |
local em, es = math.floor(elapsed / 60), math.floor(elapsed % 60) |
| 48 |
local dm, ds = math.floor(duration / 60), math.floor(duration % 60) |
| 49 |
|
| 50 |
if dm < 10 then |
| 51 |
return ("%d:%02d"):format(em, es), ("%d:%02d"):format(dm, ds) |
| 52 |
elseif dm < 60 then |
| 53 |
return ("%02d:%02d"):format(em, es), ("%02d:%02d"):format(dm, ds) |
| 54 |
end |
| 55 |
|
| 56 |
local eh, dh = math.floor(em / 60), math.floor(dm / 60) |
| 57 |
em, dm = math.floor(em % 60), math.floor(dm % 60) |
| 58 |
if dm < 600 then |
| 59 |
return ("%d:%02d:%02d"):format(eh, em, es), |
| 60 |
("%d:%02d:%02d"):format(dh, dm, ds) |
| 61 |
else |
| 62 |
return ("%02d:%02d:%02d"):format(eh, em, es), |
| 63 |
("%02d:%02d:%02d"):format(dh, dm, ds) |
| 64 |
end |
| 65 |
end |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
local function format_progress_percentage(elapsed, duration) |
| 70 |
if duration > 0 then |
| 71 |
local percentage = math.floor((elapsed / duration) * 100 + 0.5) |
| 72 |
return ("%d%%"):format(percentage) |
| 73 |
else |
| 74 |
return("0%") |
| 75 |
end |
| 76 |
end |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
local function build_cmd(warg, q) |
| 81 |
|
| 82 |
local query = ("printf 'password %s\n%sclose\n'"):format( |
| 83 |
warg and (warg.password or warg[1]) or '""', |
| 84 |
q) |
| 85 |
local connect = ("curl --connect-timeout 1 -fsm 3 telnet://%s:%s"):format( |
| 86 |
warg and (warg.host or warg[2]) or "127.0.0.1", |
| 87 |
warg and (warg.port or warg[3]) or "6600") |
| 88 |
return query .. "|" .. connect |
| 89 |
end |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
function mpd_all.playpause(warg) |
| 94 |
spawn.with_shell(build_cmd(warg, "pause\n")) |
| 95 |
end |
| 96 |
function mpd_all.play(warg) |
| 97 |
spawn.with_shell(build_cmd(warg, "pause 0\n")) |
| 98 |
end |
| 99 |
function mpd_all.pause(warg) |
| 100 |
spawn.with_shell(build_cmd(warg, "pause 1\n")) |
| 101 |
end |
| 102 |
function mpd_all.stop(warg) |
| 103 |
spawn.with_shell(build_cmd(warg, "stop\n")) |
| 104 |
end |
| 105 |
function mpd_all.next(warg) |
| 106 |
spawn.with_shell(build_cmd(warg, "next\n")) |
| 107 |
end |
| 108 |
function mpd_all.previous(warg) |
| 109 |
spawn.with_shell(build_cmd(warg, "previous\n")) |
| 110 |
end |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
function mpd_all.async(format, warg, callback) |
| 115 |
|
| 116 |
local mpd_state = { |
| 117 |
["{volume}"] = 0, |
| 118 |
["{bitrate}"] = 0, |
| 119 |
["{elapsed}"] = 0, |
| 120 |
["{duration}"] = 0, |
| 121 |
["{repeat}"] = 0, |
| 122 |
["{random}"] = 0, |
| 123 |
["{state}"] = "N/A", |
| 124 |
["{Artist}"] = "N/A", |
| 125 |
["{Title}"] = "N/A", |
| 126 |
["{Album}"] = "N/A", |
| 127 |
["{Genre}"] = "N/A", |
| 128 |
|
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
local cmd = build_cmd(warg, "status\ncurrentsong\n") |
| 133 |
|
| 134 |
|
| 135 |
spawn.with_line_callback_with_shell(cmd, { |
| 136 |
stdout = function (line) |
| 137 |
for k, v in line:gmatch"([%w]+):[%s](.*)$" do |
| 138 |
local key = "{" .. k .. "}" |
| 139 |
if k == "volume" or k == "bitrate" or |
| 140 |
k == "elapsed" or k == "duration" then |
| 141 |
mpd_state[key] = v and tonumber(v) |
| 142 |
elseif k == "repeat" or k == "random" then |
| 143 |
mpd_state[key] = cbool(v) |
| 144 |
elseif k == "state" then |
| 145 |
mpd_state[key] = helpers.capitalize(v) |
| 146 |
elseif k == "Artist" or k == "Title" or |
| 147 |
|
| 148 |
k == "Album" or k == "Genre" then |
| 149 |
mpd_state[key] = v |
| 150 |
end |
| 151 |
end |
| 152 |
end, |
| 153 |
output_done = function () |
| 154 |
|
| 155 |
mpd_state["{Elapsed}"], mpd_state["{Duration}"] = format_progress( |
| 156 |
mpd_state["{elapsed}"], mpd_state["{duration}"]) |
| 157 |
|
| 158 |
mpd_state["{Progress}"] = format_progress_percentage( |
| 159 |
mpd_state["{elapsed}"], mpd_state["{duration}"]) |
| 160 |
callback(mpd_state) |
| 161 |
end }) |
| 162 |
end |
| 163 |
|
| 164 |
|
| 165 |
return helpers.setasyncall(mpd_all) |