Add playerctl lib signals debounce as a better workaround for #77 (#95)

* A better workaround for BlingCorp/bling#77

* whoops.....
This commit is contained in:
Kasper 2021-08-23 08:04:40 +03:00 committed by GitHub
parent 5531371501
commit 048cf41e0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 19 deletions

View File

@ -22,6 +22,7 @@ local beautiful = require("beautiful")
local Playerctl = nil local Playerctl = nil
local manager = nil local manager = nil
local metadata_timer = nil
local position_timer = nil local position_timer = nil
local ignore = {} local ignore = {}
@ -74,7 +75,6 @@ local last_player = nil
local last_title = "" local last_title = ""
local last_artist = "" local last_artist = ""
local last_artUrl = "" local last_artUrl = ""
local index = 0
local function metadata_cb(player, metadata) local function metadata_cb(player, metadata)
if update_on_activity then if update_on_activity then
manager:move_player_to_top(player) manager:move_player_to_top(player)
@ -96,33 +96,46 @@ local function metadata_cb(player, metadata)
if player == manager.players[1] then if player == manager.players[1] then
-- Callback can be called even though values we care about haven't -- Callback can be called even though values we care about haven't
-- changed, so check to see if they have -- changed, so check to see if they have
index = index + 1 if player ~= last_player or title ~= last_title or
if (player ~= last_player or title ~= last_title or artist ~= last_artist or artUrl ~= last_artUrl
artist ~= last_artist) and (artUrl ~= "" or index >= 2)
then then
if (title == "" and artist == "" and artUrl == "") then return end if (title == "" and artist == "" and artUrl == "") then return end
index = 0
if artUrl ~= "" then if metadata_timer ~= nil then
awful.spawn.with_line_callback(get_album_art(artUrl), { if metadata_timer.started then
stdout = function(line) metadata_timer:stop()
end
end
metadata_timer = gears.timer {
timeout = 0.3,
autostart = true,
single_shot = true,
callback = function()
if artUrl ~= "" then
awful.spawn.with_line_callback(get_album_art(artUrl), {
stdout = function(line)
awesome.emit_signal(
"bling::playerctl::title_artist_album",
title,
artist,
line,
player.player_name
)
end
})
else
awesome.emit_signal( awesome.emit_signal(
"bling::playerctl::title_artist_album", "bling::playerctl::title_artist_album",
title, title,
artist, artist,
line, "",
player.player_name player.player_name
) )
end end
}) end
else }
awesome.emit_signal(
"bling::playerctl::title_artist_album",
title,
artist,
"",
player.player_name
)
end
-- Re-sync with position timer when track changes -- Re-sync with position timer when track changes
position_timer:again() position_timer:again()
last_player = player last_player = player
@ -248,6 +261,7 @@ local function start_manager()
-- Callback to check if all players have exited -- Callback to check if all players have exited
function manager:on_name_vanished(name) function manager:on_name_vanished(name)
if #manager.players == 0 then if #manager.players == 0 then
metadata_timer:stop()
position_timer:stop() position_timer:stop()
awesome.emit_signal("bling::playerctl::no_players") awesome.emit_signal("bling::playerctl::no_players")
end end
@ -296,6 +310,8 @@ end
local function playerctl_disable() local function playerctl_disable()
-- Remove manager and timer -- Remove manager and timer
manager = nil manager = nil
metadata_timer:stop()
metadata_timer = nil
position_timer:stop() position_timer:stop()
position_timer = nil position_timer = nil
-- Restore default settings -- Restore default settings