Debian Codegraph @

vicious/widgets/mpd_all.lua

raw symbols SCIP
1 -- Music Player Daemon widget type
2 -- Copyright (C) 2010 Adrian C. <[email protected]>
3 -- Copyright (C) 2017,2019 mutlusun <[email protected]>
4 -- Copyright (C) 2018 Jörg Thalheim <[email protected]>
5 -- Copyright (C) 2018-2019 Nguyễn Gia Phong <[email protected]m>
6 -- Copyright (C) 2019 Juan Carlos Menonita <[email protected]>
7 -- Copyright (C) 2019 Lorenzo Gaggini <[email protected]>
8 -- Copyright (C) 2022 Constantin Piber <[email protected]>
9 --
10 -- This file is part of Vicious.
11 --
12 -- Vicious is free software: you can redistribute it and/or modify
13 -- it under the terms of the GNU General Public License as
14 -- published by the Free Software Foundation, either version 2 of the
15 -- License, or (at your option) any later version.
16 --
17 -- Vicious is distributed in the hope that it will be useful,
18 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
19 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 -- GNU General Public License for more details.
21 --
22 -- You should have received a copy of the GNU General Public License
23 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
24
25 -- {{{ Grab environment
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 -- Mpd: provides Music Player Daemon information
35 -- vicious.widgets.mpd
36 local mpd_all = {}
37
38
39 -- {{{ Return true if number is nonzero
40 local function cbool(number)
41 return type(number) == "number" and number ~= 0 or number
42 end
43 -- }}}
44
45 -- {{{ Format playing progress
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 -- {{{ Format playing progress (percentage)
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 -- {{{ Build shell command from mpd command string
80 local function build_cmd(warg, q)
81 -- Construct MPD client options, fallback to defaults when necessary
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 -- {{{ Common MPD commands
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 -- {{{ MPD widget type
114 function mpd_all.async(format, warg, callback)
115 -- Fallback values
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 --["{Name}"] = "N/A",
129 --["{file}"] = "N/A",
130 }
131
132 local cmd = build_cmd(warg, "status\ncurrentsong\n")
133
134 -- Get data from MPD server
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 --k == "Name" or k == "file" or
148 k == "Album" or k == "Genre" then
149 mpd_state[key] = v
150 end
151 end
152 end,
153 output_done = function ()
154 -- Formatted elapsed and duration
155 mpd_state["{Elapsed}"], mpd_state["{Duration}"] = format_progress(
156 mpd_state["{elapsed}"], mpd_state["{duration}"])
157 -- Formatted playing progress percentage
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)