refactoring + fix player selection
This commit is contained in:
parent
d8376f7bec
commit
f944270602
|
@ -11,13 +11,6 @@ local watch = require("awful.widget.watch")
|
||||||
local wibox = require("wibox")
|
local wibox = require("wibox")
|
||||||
local gears = require("gears")
|
local gears = require("gears")
|
||||||
|
|
||||||
local GET_MPD_CMD = "playerctl -f '{{status}};{{xesam:artist}};{{xesam:title}};{{mpris:artUrl}};{{position}};{{mpris:length}}' metadata"
|
|
||||||
|
|
||||||
local TOGGLE_MPD_CMD = "playerctl play-pause"
|
|
||||||
local NEXT_MPD_CMD = "playerctl next"
|
|
||||||
local PREV_MPD_CMD = "playerctl previous"
|
|
||||||
local LIST_PLAYERS_CMD = "playerctl -l"
|
|
||||||
|
|
||||||
local PATH_TO_ICONS = "/usr/share/icons/Adwaita"
|
local PATH_TO_ICONS = "/usr/share/icons/Adwaita"
|
||||||
local PAUSE_ICON_NAME = PATH_TO_ICONS .. "/symbolic/actions/media-playback-pause-symbolic.svg"
|
local PAUSE_ICON_NAME = PATH_TO_ICONS .. "/symbolic/actions/media-playback-pause-symbolic.svg"
|
||||||
local PLAY_ICON_NAME = PATH_TO_ICONS .. "/symbolic/actions/media-playback-start-symbolic.svg"
|
local PLAY_ICON_NAME = PATH_TO_ICONS .. "/symbolic/actions/media-playback-start-symbolic.svg"
|
||||||
|
@ -26,7 +19,64 @@ local LIBRARY_ICON_NAME = PATH_TO_ICONS .. "/symbolic/places/folder-music-symbol
|
||||||
|
|
||||||
local FONT = 'Roboto Condensed 16px'
|
local FONT = 'Roboto Condensed 16px'
|
||||||
|
|
||||||
local default_player = 'mpv'
|
local playerctl = {
|
||||||
|
player_name = 'mpv',
|
||||||
|
}
|
||||||
|
|
||||||
|
function playerctl:set_player(name)
|
||||||
|
self.player_name = name
|
||||||
|
|
||||||
|
if self.timer ~= nil then
|
||||||
|
self.timer:stop()
|
||||||
|
playerctl:watch(self.watch_params.timeout, self.watch_params.callback, self.watch_params.widget)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function playerctl:cmd(cmd)
|
||||||
|
return "playerctl -p '" .. self.player_name .. "' " .. cmd
|
||||||
|
end
|
||||||
|
|
||||||
|
function playerctl:watch(timeout, callback, widget)
|
||||||
|
local cmd = self:cmd("-f '{{status}};{{xesam:artist}};{{xesam:title}};{{mpris:artUrl}};{{position}};{{mpris:length}};{{album}}' metadata")
|
||||||
|
|
||||||
|
self.watch_params = {timeout = timeout, callback = callback, widget = widget}
|
||||||
|
|
||||||
|
local cb = function(widget, stdout, _, _, _)
|
||||||
|
local words = gears.string.split(stdout, ';')
|
||||||
|
|
||||||
|
local progress
|
||||||
|
if words[5] ~= nil and words[6] ~= nil then
|
||||||
|
progress = tonumber(words[5]) / tonumber(words[6])
|
||||||
|
end
|
||||||
|
|
||||||
|
local metadata = {
|
||||||
|
status = words[1],
|
||||||
|
artist = words[2],
|
||||||
|
current_song = words[3],
|
||||||
|
art_url = words[4],
|
||||||
|
position = words[5],
|
||||||
|
length = words[6],
|
||||||
|
album = words[7],
|
||||||
|
progress = progress,
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(widget, metadata)
|
||||||
|
end
|
||||||
|
|
||||||
|
_, self.timer = awful.widget.watch(cmd, timeout, cb, widget)
|
||||||
|
end
|
||||||
|
|
||||||
|
function playerctl:toggle()
|
||||||
|
awful.spawn(self:cmd("play-pause"), false)
|
||||||
|
end
|
||||||
|
|
||||||
|
function playerctl:next()
|
||||||
|
awful.spawn(self:cmd("next"), false)
|
||||||
|
end
|
||||||
|
|
||||||
|
function playerctl:prev()
|
||||||
|
awful.spawn(self:cmd("previous"), false)
|
||||||
|
end
|
||||||
|
|
||||||
local icon = wibox.widget {
|
local icon = wibox.widget {
|
||||||
id = "icon",
|
id = "icon",
|
||||||
|
@ -84,10 +134,8 @@ local metadata_widget = wibox.widget {
|
||||||
forced_width = 300,
|
forced_width = 300,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
local player_selector_popup = {
|
||||||
local rows = { layout = wibox.layout.fixed.vertical }
|
popup = awful.popup {
|
||||||
|
|
||||||
local popup = awful.popup {
|
|
||||||
bg = beautiful.bg_normal,
|
bg = beautiful.bg_normal,
|
||||||
fg = beautiful.fg_normal,
|
fg = beautiful.fg_normal,
|
||||||
ontop = true,
|
ontop = true,
|
||||||
|
@ -98,16 +146,15 @@ local popup = awful.popup {
|
||||||
maximum_width = 400,
|
maximum_width = 400,
|
||||||
offset = { y = 5 },
|
offset = { y = 5 },
|
||||||
widget = {}
|
widget = {}
|
||||||
|
},
|
||||||
|
|
||||||
|
rows = { layout = wibox.layout.fixed.vertical },
|
||||||
}
|
}
|
||||||
|
|
||||||
local function rebuild_popup()
|
function player_selector_popup:add_radio_button(player_name)
|
||||||
awful.spawn.easy_async(LIST_PLAYERS_CMD, function(stdout, _, _, _)
|
|
||||||
for i = 0, #rows do rows[i] = nil end
|
|
||||||
for player_name in stdout:gmatch("[^\r\n]+") do
|
|
||||||
if player_name ~= '' and player_name ~= nil then
|
|
||||||
local checkbox = wibox.widget {
|
local checkbox = wibox.widget {
|
||||||
{
|
{
|
||||||
checked = player_name == default_player,
|
checked = player_name == playerctl.player_name,
|
||||||
color = beautiful.bg_normal,
|
color = beautiful.bg_normal,
|
||||||
paddings = 2,
|
paddings = 2,
|
||||||
shape = gears.shape.circle,
|
shape = gears.shape.circle,
|
||||||
|
@ -121,11 +168,11 @@ local function rebuild_popup()
|
||||||
}
|
}
|
||||||
|
|
||||||
checkbox:connect_signal("button::press", function()
|
checkbox:connect_signal("button::press", function()
|
||||||
default_player = player_name
|
playerctl:set_player(player_name)
|
||||||
rebuild_popup()
|
self:toggle()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
table.insert(rows, wibox.widget {
|
table.insert(self.rows, wibox.widget {
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
checkbox,
|
checkbox,
|
||||||
|
@ -149,19 +196,51 @@ local function rebuild_popup()
|
||||||
widget = wibox.container.background
|
widget = wibox.container.background
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function player_selector_popup:rebuild()
|
||||||
|
self.rows = { layout = wibox.layout.fixed.vertical }
|
||||||
|
awful.spawn.easy_async("playerctl -l", function(stdout, _, _, _)
|
||||||
|
for name in stdout:gmatch("[^\r\n]+") do
|
||||||
|
if name ~= '' and name ~= nil then
|
||||||
|
self:add_radio_button(name)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
self.popup:setup(self.rows)
|
||||||
|
self.popup.visible = true
|
||||||
|
self.popup:move_next_to(mouse.current_widget_geometry)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
popup:setup(rows)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function update_metadata(artist, current_song, progress, art_url)
|
function player_selector_popup:toggle()
|
||||||
artist_widget:set_text(artist)
|
if self.popup.visible then
|
||||||
title_widget:set_text(current_song)
|
self.popup.visible = false
|
||||||
progress_widget.value = progress
|
else
|
||||||
|
self:rebuild()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function duration(microseconds)
|
||||||
|
local seconds = microseconds / 1000000
|
||||||
|
local minutes = seconds / 60
|
||||||
|
seconds = seconds % 60
|
||||||
|
local hours = minutes / 60
|
||||||
|
minutes = minutes % 60
|
||||||
|
if hours >= 1 then
|
||||||
|
return string.format("%.f:%02.f:%02.f", hours, minutes, seconds)
|
||||||
|
end
|
||||||
|
return string.format("%.f:%02.f", minutes, seconds)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function worker()
|
||||||
|
local update_metadata = function(meta)
|
||||||
|
artist_widget:set_text(meta.artist)
|
||||||
|
title_widget:set_text(meta.current_song)
|
||||||
|
metadata_widget:set_text(string.format('%s - %s (%s/%s)', meta.album, meta.current_song, duration(meta.position), duration(meta.length)))
|
||||||
|
progress_widget.value = meta.progress
|
||||||
|
|
||||||
-- poor man's urldecode
|
-- poor man's urldecode
|
||||||
art_url = art_url:gsub("file://", "/")
|
local art_url = meta.art_url:gsub("file://", "/")
|
||||||
art_url = art_url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end)
|
art_url = art_url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end)
|
||||||
|
|
||||||
if art_url ~= nil and art_url ~= "" then
|
if art_url ~= nil and art_url ~= "" then
|
||||||
|
@ -173,39 +252,22 @@ local function update_metadata(artist, current_song, progress, art_url)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function worker()
|
local update_graphic = function(widget, metadata)
|
||||||
-- retrieve song info
|
if metadata.current_song ~= nil then
|
||||||
local current_song, artist, player_status, art_url, progress
|
if string.len(metadata.current_song) > 40 then
|
||||||
|
metadata.current_song = string.sub(metadata.current_song, 0, 38) .. "…"
|
||||||
local update_graphic = function(widget, stdout, _, _, _)
|
|
||||||
local words = gears.string.split(stdout, ';')
|
|
||||||
player_status = words[1]
|
|
||||||
artist = words[2]
|
|
||||||
current_song = words[3]
|
|
||||||
|
|
||||||
art_url = words[4]
|
|
||||||
|
|
||||||
if current_song ~= nil then
|
|
||||||
if string.len(current_song) > 40 then
|
|
||||||
current_song = string.sub(current_song, 0, 38) .. "…"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if player_status == "Playing" then
|
if metadata.status == "Playing" then
|
||||||
icon.image = PLAY_ICON_NAME
|
icon.image = PLAY_ICON_NAME
|
||||||
widget.colors = { beautiful.widget_main_color }
|
widget.colors = { beautiful.widget_main_color }
|
||||||
if words[5] ~= nil and words[6] ~= nil then
|
update_metadata(metadata)
|
||||||
progress = tonumber(words[5]) / tonumber(words[6])
|
elseif metadata.status == "Paused" then
|
||||||
end
|
|
||||||
update_metadata(artist, current_song, progress, art_url)
|
|
||||||
elseif player_status == "Paused" then
|
|
||||||
icon.image = PAUSE_ICON_NAME
|
icon.image = PAUSE_ICON_NAME
|
||||||
widget.colors = { beautiful.widget_main_color }
|
widget.colors = { beautiful.widget_main_color }
|
||||||
if words[5] ~= nil and words[6] ~= nil then
|
update_metadata(metadata)
|
||||||
progress = tonumber(words[5]) / tonumber(words[6])
|
elseif metadata.status == "Stopped" then
|
||||||
end
|
|
||||||
update_metadata(artist, current_song, progress, art_url)
|
|
||||||
elseif player_status == "Stopped" then
|
|
||||||
icon.image = STOP_ICON_NAME
|
icon.image = STOP_ICON_NAME
|
||||||
else -- no player is running
|
else -- no player is running
|
||||||
icon.image = LIBRARY_ICON_NAME
|
icon.image = LIBRARY_ICON_NAME
|
||||||
|
@ -215,34 +277,16 @@ local function worker()
|
||||||
|
|
||||||
mpris_widget:buttons(
|
mpris_widget:buttons(
|
||||||
awful.util.table.join(
|
awful.util.table.join(
|
||||||
awful.button({}, 3, function()
|
awful.button({}, 3, function() player_selector_popup:toggle() end),
|
||||||
if popup.visible then
|
awful.button({}, 4, function() playerctl:next() end),
|
||||||
popup.visible = not popup.visible
|
awful.button({}, 5, function() playerctl:prev() end),
|
||||||
else
|
awful.button({}, 1, function() playerctl:toggle() end)
|
||||||
rebuild_popup()
|
|
||||||
popup:move_next_to(mouse.current_widget_geometry)
|
|
||||||
end
|
|
||||||
end),
|
|
||||||
awful.button({}, 4, function() awful.spawn(NEXT_MPD_CMD, false) end),
|
|
||||||
awful.button({}, 5, function() awful.spawn(PREV_MPD_CMD, false) end),
|
|
||||||
awful.button({}, 1, function() awful.spawn(TOGGLE_MPD_CMD, false) end)
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
watch(GET_MPD_CMD, 1, update_graphic, mpris_widget)
|
playerctl:watch(1, update_graphic, mpris_widget)
|
||||||
|
|
||||||
local mpris_popup = awful.widget.watch(
|
local mpris_popup = awful.popup {
|
||||||
"playerctl metadata --format '{{ status }}: {{ artist }} - {{ title }}\n"
|
|
||||||
.. "Duration: {{ duration(position) }}/{{ duration(mpris:length) }}'",
|
|
||||||
1,
|
|
||||||
function(callback_popup, stdout)
|
|
||||||
local metadata = stdout
|
|
||||||
if callback_popup.visible then
|
|
||||||
metadata_widget:set_text(metadata)
|
|
||||||
callback_popup:move_next_to(mouse.current_widget_geometry)
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
awful.popup {
|
|
||||||
border_color = beautiful.border_color,
|
border_color = beautiful.border_color,
|
||||||
ontop = true,
|
ontop = true,
|
||||||
visible = false,
|
visible = false,
|
||||||
|
@ -252,11 +296,11 @@ local function worker()
|
||||||
layout = wibox.layout.fixed.vertical,
|
layout = wibox.layout.fixed.vertical,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
mpris_widget:connect_signal('mouse::enter',
|
mpris_widget:connect_signal('mouse::enter',
|
||||||
function()
|
function()
|
||||||
mpris_popup.visible = true
|
mpris_popup.visible = true
|
||||||
|
mpris_popup:move_next_to(mouse.current_widget_geometry)
|
||||||
end)
|
end)
|
||||||
mpris_widget:connect_signal('mouse::leave',
|
mpris_widget:connect_signal('mouse::leave',
|
||||||
function()
|
function()
|
||||||
|
|
Loading…
Reference in New Issue