[mpd] Add ability to send arbitrary commands
This commit is contained in:
parent
81f8a80774
commit
830d624f35
|
@ -290,6 +290,11 @@ Supported platforms: platform independent (required tools: ``curl``).
|
||||||
``${random}``, ``${repeat}``, ``${state}``, ``${Artist}``, ``${Title}``,
|
``${random}``, ``${repeat}``, ``${state}``, ``${Artist}``, ``${Title}``,
|
||||||
``${Album}``, ``${Genre}`` and optionally ``${Name}`` and ``${file}``.
|
``${Album}``, ``${Genre}`` and optionally ``${Name}`` and ``${file}``.
|
||||||
|
|
||||||
|
In addition, some common mpd commands are available as functions:
|
||||||
|
``playpause``, ``play``, ``pause``, ``stop``, ``next``, ``previous``.
|
||||||
|
Arguments are of the same form as above, and no value is returned,
|
||||||
|
e.g. ``vicious.widgets.mpd.playpause()``.
|
||||||
|
|
||||||
vicious.widgets.net
|
vicious.widgets.net
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
-- Copyright (C) 2018-2019 Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
|
-- Copyright (C) 2018-2019 Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
|
||||||
-- Copyright (C) 2019 Juan Carlos Menonita <JuanKman94@users.noreply.github.com>
|
-- Copyright (C) 2019 Juan Carlos Menonita <JuanKman94@users.noreply.github.com>
|
||||||
-- Copyright (C) 2019 Lorenzo Gaggini <lg@lgaggini.net>
|
-- Copyright (C) 2019 Lorenzo Gaggini <lg@lgaggini.net>
|
||||||
|
-- Copyright (C) 2022 Constantin Piber <cp.piber@gmail.com>
|
||||||
--
|
--
|
||||||
-- This file is part of Vicious.
|
-- This file is part of Vicious.
|
||||||
--
|
--
|
||||||
|
@ -75,6 +76,40 @@ local function format_progress_percentage(elapsed, duration)
|
||||||
end
|
end
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
|
-- {{{ Build shell command from mpd command string
|
||||||
|
local function build_cmd(warg, q)
|
||||||
|
-- Construct MPD client options, fallback to defaults when necessary
|
||||||
|
local query = ("printf 'password %s\n%sclose\n'"):format(
|
||||||
|
warg and (warg.password or warg[1]) or '""',
|
||||||
|
q)
|
||||||
|
local connect = ("curl --connect-timeout 1 -fsm 3 telnet://%s:%s"):format(
|
||||||
|
warg and (warg.host or warg[2]) or "127.0.0.1",
|
||||||
|
warg and (warg.port or warg[3]) or "6600")
|
||||||
|
return query .. "|" .. connect
|
||||||
|
end
|
||||||
|
-- }}}
|
||||||
|
|
||||||
|
-- {{{ Common MPD commands
|
||||||
|
function mpd_all.playpause(warg)
|
||||||
|
spawn.with_shell(build_cmd(warg, "pause\n"))
|
||||||
|
end
|
||||||
|
function mpd_all.play(warg)
|
||||||
|
spawn.with_shell(build_cmd(warg, "pause 0\n"))
|
||||||
|
end
|
||||||
|
function mpd_all.pause(warg)
|
||||||
|
spawn.with_shell(build_cmd(warg, "pause 1\n"))
|
||||||
|
end
|
||||||
|
function mpd_all.stop(warg)
|
||||||
|
spawn.with_shell(build_cmd(warg, "stop\n"))
|
||||||
|
end
|
||||||
|
function mpd_all.next(warg)
|
||||||
|
spawn.with_shell(build_cmd(warg, "next\n"))
|
||||||
|
end
|
||||||
|
function mpd_all.previous(warg)
|
||||||
|
spawn.with_shell(build_cmd(warg, "previous\n"))
|
||||||
|
end
|
||||||
|
-- }}}
|
||||||
|
|
||||||
-- {{{ MPD widget type
|
-- {{{ MPD widget type
|
||||||
function mpd_all.async(format, warg, callback)
|
function mpd_all.async(format, warg, callback)
|
||||||
-- Fallback values
|
-- Fallback values
|
||||||
|
@ -83,8 +118,8 @@ function mpd_all.async(format, warg, callback)
|
||||||
["{bitrate}"] = 0,
|
["{bitrate}"] = 0,
|
||||||
["{elapsed}"] = 0,
|
["{elapsed}"] = 0,
|
||||||
["{duration}"] = 0,
|
["{duration}"] = 0,
|
||||||
["{repeat}"] = false,
|
["{repeat}"] = 0,
|
||||||
["{random}"] = false,
|
["{random}"] = 0,
|
||||||
["{state}"] = "N/A",
|
["{state}"] = "N/A",
|
||||||
["{Artist}"] = "N/A",
|
["{Artist}"] = "N/A",
|
||||||
["{Title}"] = "N/A",
|
["{Title}"] = "N/A",
|
||||||
|
@ -94,15 +129,10 @@ function mpd_all.async(format, warg, callback)
|
||||||
--["{file}"] = "N/A",
|
--["{file}"] = "N/A",
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Construct MPD client options, fallback to defaults when necessary
|
local cmd = build_cmd(warg, "status\ncurrentsong\n")
|
||||||
local query = ("printf 'password %s\nstatus\ncurrentsong\nclose\n'"):format(
|
|
||||||
warg and (warg.password or warg[1]) or '""')
|
|
||||||
local connect = ("curl --connect-timeout 1 -fsm 3 telnet://%s:%s"):format(
|
|
||||||
warg and (warg.host or warg[2]) or "127.0.0.1",
|
|
||||||
warg and (warg.port or warg[3]) or "6600")
|
|
||||||
|
|
||||||
-- Get data from MPD server
|
-- Get data from MPD server
|
||||||
spawn.with_line_callback_with_shell(query .. "|" .. connect, {
|
spawn.with_line_callback_with_shell(cmd, {
|
||||||
stdout = function (line)
|
stdout = function (line)
|
||||||
for k, v in line:gmatch"([%w]+):[%s](.*)$" do
|
for k, v in line:gmatch"([%w]+):[%s](.*)$" do
|
||||||
local key = "{" .. k .. "}"
|
local key = "{" .. k .. "}"
|
||||||
|
|
Loading…
Reference in New Issue