| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
local mpd = require 'obvious.lib.mpd' |
| 7 |
local hooks = require 'obvious.lib.hooks' |
| 8 |
|
| 9 |
|
| 10 |
local backend = {} |
| 11 |
|
| 12 |
function backend:clone(clone) |
| 13 |
return setmetatable(clone or {}, { __index = self }) |
| 14 |
end |
| 15 |
|
| 16 |
function backend:configure() |
| 17 |
end |
| 18 |
|
| 19 |
function backend:ontrackchange(callback) |
| 20 |
end |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
local mpd_backend = backend:clone { name = 'mpd' } |
| 25 |
local mpris_backend = backend:clone { name = 'mpris' } |
| 26 |
|
| 27 |
local backends = { |
| 28 |
mpd_backend, |
| 29 |
mpris_backend, |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
local NORMALIZED_MPD_STATES = { |
| 35 |
play = 'playing', |
| 36 |
stop = 'stopped', |
| 37 |
pause = 'paused', |
| 38 |
} |
| 39 |
|
| 40 |
function mpd_backend:configure() |
| 41 |
local connection = mpd.new() |
| 42 |
|
| 43 |
return mpd_backend:clone { |
| 44 |
connection = connection, |
| 45 |
} |
| 46 |
end |
| 47 |
|
| 48 |
function mpd_backend:ontrackchange(callback) |
| 49 |
local prev_state |
| 50 |
local prev_songid |
| 51 |
|
| 52 |
local function update() |
| 53 |
local status = self.connection:send 'status' |
| 54 |
|
| 55 |
if prev_state == status.state and prev_songid == status.songid then |
| 56 |
return |
| 57 |
end |
| 58 |
|
| 59 |
if not status.state then |
| 60 |
callback(nil) |
| 61 |
else |
| 62 |
callback { |
| 63 |
state = NORMALIZED_MPD_STATES[status.state], |
| 64 |
info = self.connection:send 'currentsong', |
| 65 |
} |
| 66 |
end |
| 67 |
|
| 68 |
prev_state = status.state |
| 69 |
prev_songid = status.songid |
| 70 |
end |
| 71 |
|
| 72 |
hooks.timer.register(1, 30, update, 'basic_mpd widget refresh rate') |
| 73 |
end |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
local NORMALIZED_MPRIS_STATES = { |
| 80 |
Playing = 'playing', |
| 81 |
Stopped = 'stopped', |
| 82 |
Paused = 'paused', |
| 83 |
} |
| 84 |
|
| 85 |
function mpris_backend:configure() |
| 86 |
return self |
| 87 |
end |
| 88 |
|
| 89 |
function mpris_backend:ontrackchange(callback) |
| 90 |
local current_status = 'stopped' |
| 91 |
local current_metadata = {} |
| 92 |
|
| 93 |
dbus.add_match('session', "path='/org/mpris/MediaPlayer2',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged'") |
| 94 |
dbus.connect_signal('org.freedesktop.DBus.Properties', function(metadata, name, payload) |
| 95 |
if name ~= 'org.mpris.MediaPlayer2.Player' then |
| 96 |
return |
| 97 |
end |
| 98 |
|
| 99 |
if payload.PlaybackStatus then |
| 100 |
current_status = NORMALIZED_MPRIS_STATES[payload.PlaybackStatus] |
| 101 |
end |
| 102 |
|
| 103 |
if payload.Metadata then |
| 104 |
current_metadata = {} |
| 105 |
for k, v in pairs(payload.Metadata) do |
| 106 |
if string.sub(k, 1, 6) == 'xesam:' then |
| 107 |
if type(v) == 'table' then |
| 108 |
v = table.concat(v, ' & ') |
| 109 |
end |
| 110 |
current_metadata[string.sub(k, 7)] = v |
| 111 |
end |
| 112 |
end |
| 113 |
end |
| 114 |
|
| 115 |
callback { |
| 116 |
state = current_status, |
| 117 |
info = current_metadata, |
| 118 |
} |
| 119 |
end) |
| 120 |
|
| 121 |
callback { |
| 122 |
state = current_status, |
| 123 |
info = current_metadata, |
| 124 |
} |
| 125 |
end |
| 126 |
|
| 127 |
|
| 128 |
local _M = {} |
| 129 |
|
| 130 |
for i = 1, #backends do |
| 131 |
local backend = backends[i] |
| 132 |
_M[backend.name] = backend |
| 133 |
end |
| 134 |
|
| 135 |
return _M |
| 136 |
|
| 137 |
|