aboutsummaryrefslogtreecommitdiff
path: root/.config/awesome/services/mpris/init.lua
blob: 3659a3a77b82ac0a857dca5042abcfafb52e6fbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
local gobject = require "gears.object"
local playerctl = require "services.mpris.playerctl"
local qpersistent = require "quarrel.persistent"

---@class ServiceMpris : gears.object
---@field inner Playerctl
---@field index number the index of the currently active player
local M = {}

---@param self ServiceMpris
---@param player Playerctl.data
local function update_player(self, player)
    self:emit_signal("player::metadata", player)
    self:emit_signal("player::position", player)
    -- handle_position(nil, player)
end

---@param diff_player Playerctl.data
local function recalculate_active_player(diff_player, vanished)
    if type(diff_player) ~= "table" then
        return
    end
    -- if #layout.children == 0 then
    --     M.active_player_index = 1
    --     update_player()
    --     return
    -- end

    local active_player = players[M.active_player_index]
    if not active_player then -- we're recovering from a state with no players
        update_player(diff_player)
        return
    end

    if diff_player.instance == active_player.instance and vanished then -- active player vanished; fall back to previous player
        M.previous_player()
    else -- non-active player appeared/vanished; try to find active player
        for i, p in ipairs(playerctl:list()) do
            if p.instance == active_player.instance then
                M.active_player_index = i
                update_player(p)
                return
            end
        end

        gdebug.print_warning(
            "failed to find active player:\n    " .. gdebug.dump_return(active_player, nil, 2):gsub("\n", "\n    ")
        )
        M.active_player_index = 1
        update_player(playerctl:list()[M.active_player_index])
    end
end

function M:next_player()
    local players = self.inner:list()

    if #players == 0 then
        return
    elseif self.index + 1 > #players then
        self.index = 1
    else
        self.index = self.index + 1
    end

    -- update_player(playerctl:list()[M.active_player_index])
    local player = players[self.index]
    self:emit_signal("player::metadata", player)
    self:emit_signal("player::position", player)
end

function M:previous_player()
    local players = self.inner:list()
    if #players == 0 then
        return
    elseif self.index - 1 < #players then
        self.index = #players
    else
        self.index = self.index - 1
    end

    local player = players[self.index]
    self:emit_signal("player::metadata", player)
    self:emit_signal("player::position", player)
end

M.inner = playerctl.new {
    players = {},
    metadata = {
        album = "xesam:album",
        title = "xesam:title",
        artist = "xesam:artist",
        art = "mpris:artUrl",
    },
}

local instance = gobject { class = M }
instance:connect_signal("property::index", function(self, index)
    qpersistent.store("active_player_index", self.index)
end)
return instance