2021-02-17 08:59:40 +01:00
|
|
|
--
|
2021-02-09 15:15:20 +01:00
|
|
|
-- Provides:
|
|
|
|
-- bling::playerctl::status
|
|
|
|
-- playing (boolean)
|
2021-02-17 02:14:26 +01:00
|
|
|
-- bling::playerctl::title_artist_album
|
2021-02-13 03:37:45 +01:00
|
|
|
-- title (string)
|
|
|
|
-- artist (string)
|
2021-02-17 02:14:26 +01:00
|
|
|
-- album_path (string)
|
2021-02-13 03:37:45 +01:00
|
|
|
-- bling::playerctl::position
|
2021-02-09 15:15:20 +01:00
|
|
|
-- interval_sec (number)
|
|
|
|
-- length_sec (number)
|
2021-02-17 08:59:40 +01:00
|
|
|
-- bling::playerctl::player_stopped
|
2021-02-09 15:15:20 +01:00
|
|
|
--
|
|
|
|
local awful = require("awful")
|
|
|
|
local beautiful = require("beautiful")
|
|
|
|
|
2021-02-12 12:16:24 +01:00
|
|
|
local interval = beautiful.playerctl_position_update_interval or 1
|
2021-02-09 15:15:20 +01:00
|
|
|
|
|
|
|
local function emit_player_status()
|
2021-02-12 12:16:24 +01:00
|
|
|
local status_cmd = "playerctl status -F"
|
|
|
|
|
|
|
|
-- Follow status
|
2021-02-17 15:08:34 +01:00
|
|
|
awful.spawn.easy_async({
|
|
|
|
"pkill", "--full", "--uid", os.getenv("USER"), "^playerctl status"
|
|
|
|
}, function()
|
|
|
|
awful.spawn.with_line_callback(status_cmd, {
|
|
|
|
stdout = function(line)
|
|
|
|
local playing = false
|
|
|
|
if line:find("Playing") then
|
|
|
|
playing = true
|
|
|
|
else
|
|
|
|
playing = false
|
2021-02-12 12:16:24 +01:00
|
|
|
end
|
2021-02-17 15:08:34 +01:00
|
|
|
awesome.emit_signal("bling::playerctl::status", playing)
|
|
|
|
end
|
|
|
|
})
|
2021-03-11 13:35:50 +01:00
|
|
|
collectgarbage("collect")
|
2021-02-17 15:08:34 +01:00
|
|
|
end)
|
2021-02-09 15:15:20 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local function emit_player_info()
|
|
|
|
local art_script = [[
|
|
|
|
sh -c '
|
|
|
|
|
2021-02-11 15:12:19 +01:00
|
|
|
tmp_dir="$XDG_CACHE_HOME/awesome/"
|
|
|
|
|
|
|
|
if [ -z ${XDG_CACHE_HOME} ]; then
|
|
|
|
tmp_dir="$HOME/.cache/awesome/"
|
|
|
|
fi
|
|
|
|
|
2021-02-09 15:15:20 +01:00
|
|
|
tmp_cover_path=${tmp_dir}"cover.png"
|
|
|
|
|
|
|
|
if [ ! -d $tmp_dir ]; then
|
|
|
|
mkdir -p $tmp_dir
|
|
|
|
fi
|
|
|
|
|
2021-02-16 02:20:44 +01:00
|
|
|
link="$(playerctl metadata mpris:artUrl)"
|
2021-02-09 15:15:20 +01:00
|
|
|
|
|
|
|
curl -s "$link" --output $tmp_cover_path
|
|
|
|
|
2021-02-13 03:37:45 +01:00
|
|
|
echo "$tmp_cover_path"
|
2021-02-09 15:15:20 +01:00
|
|
|
']]
|
|
|
|
|
2021-02-12 12:16:24 +01:00
|
|
|
-- Command that lists artist and title in a format to find and follow
|
|
|
|
local song_follow_cmd =
|
|
|
|
"playerctl metadata --format 'artist_{{artist}}title_{{title}}' -F"
|
2021-02-09 15:15:20 +01:00
|
|
|
|
2021-02-12 12:16:24 +01:00
|
|
|
-- Progress Cmds
|
2021-02-09 15:15:20 +01:00
|
|
|
local prog_cmd = "playerctl position"
|
|
|
|
local length_cmd = "playerctl metadata mpris:length"
|
|
|
|
|
2021-02-12 12:16:24 +01:00
|
|
|
awful.widget.watch(prog_cmd, interval, function(_, interval)
|
2021-02-09 15:15:20 +01:00
|
|
|
awful.spawn.easy_async_with_shell(length_cmd, function(length)
|
|
|
|
local length_sec = tonumber(length) -- in microseconds
|
|
|
|
local interval_sec = tonumber(interval) -- in seconds
|
|
|
|
if length_sec and interval_sec then
|
|
|
|
if interval_sec >= 0 and length_sec > 0 then
|
|
|
|
awesome.emit_signal("bling::playerctl::position",
|
|
|
|
interval_sec, length_sec / 1000000)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
2021-03-11 13:35:50 +01:00
|
|
|
collectgarbage("collect")
|
2021-02-09 15:15:20 +01:00
|
|
|
end)
|
|
|
|
|
2021-02-12 12:16:24 +01:00
|
|
|
-- Follow title
|
2021-02-17 15:08:34 +01:00
|
|
|
awful.spawn.easy_async({
|
|
|
|
"pkill", "--full", "--uid", os.getenv("USER"), "^playerctl metadata"
|
|
|
|
}, function()
|
|
|
|
awful.spawn.with_line_callback(song_follow_cmd, {
|
|
|
|
stdout = function(line)
|
|
|
|
local album_path = ""
|
|
|
|
awful.spawn.easy_async_with_shell(art_script, function(out)
|
|
|
|
-- Get album path
|
|
|
|
album_path = out:gsub('%\n', '')
|
|
|
|
-- Get title and artist
|
|
|
|
local artist = line:match('artist_(.*)title_')
|
|
|
|
local title = line:match('title_(.*)')
|
|
|
|
-- If the title is nil or empty then the players stopped
|
|
|
|
if title and title ~= "" then
|
|
|
|
awesome.emit_signal(
|
|
|
|
"bling::playerctl::title_artist_album", title,
|
|
|
|
artist, album_path)
|
|
|
|
else
|
|
|
|
awesome.emit_signal("bling::playerctl::player_stopped")
|
|
|
|
end
|
|
|
|
end)
|
2021-03-11 13:35:50 +01:00
|
|
|
collectgarbage("collect")
|
2021-02-17 15:08:34 +01:00
|
|
|
end
|
|
|
|
})
|
2021-03-11 13:35:50 +01:00
|
|
|
collectgarbage("collect")
|
2021-02-17 15:08:34 +01:00
|
|
|
end)
|
2021-02-09 15:15:20 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Emit info
|
|
|
|
-- emit_player_status()
|
|
|
|
-- emit_player_info()
|
|
|
|
|
|
|
|
local enable = function()
|
|
|
|
emit_player_status()
|
|
|
|
emit_player_info()
|
|
|
|
end
|
|
|
|
|
2021-03-10 21:52:31 +01:00
|
|
|
local disable = function()
|
|
|
|
awful.spawn.with_shell("pkill --full --uid " .. os.getenv("USER") ..
|
|
|
|
" '^playerctl status -F'")
|
|
|
|
|
|
|
|
awful.spawn.with_shell("pkill --full --uid " .. os.getenv("USER") ..
|
|
|
|
" '^playerctl metadata --format'")
|
|
|
|
end
|
|
|
|
|
|
|
|
return {enable = enable, disable = disable}
|