local wibox = require('wibox') local awful = require('awful') local lgi = require('lgi') local theme = require('src.theme') local mpris = require('src.util.mpris') local mpris_icon = wibox.widget.imagebox(theme.widget_pause) local mpris_widget = wibox.widget.textbox() local function variant_strip(v) if not tostring(v):find("GLib%.Variant$") then if type(v) == "table" and #v > 0 then v.n = nil end return v end if v:is_container() and not v:is_of_type(lgi.GLib.VariantType.VARIANT) then local out = {} local n_children = v:n_children() local idx = 0 local is_dict = v:is_of_type(lgi.GLib.VariantType.DICTIONARY) while idx < n_children do local val = v:get_child_value(idx) idx = idx + 1 if is_dict then local key = val[1] local value = variant_strip(val[2]) out[key] = variant_strip(value) else out[idx] = variant_strip(val) end end return out else return variant_strip(v.value) end end local function signal_cbk(_conn, _sender, _object_path, _interface_name, _signal_name, params, _user_data) local data = variant_strip(params) if type(data) ~= 'table' or type(data[2]) ~= 'table' then return end data = data[2] if data['PlaybackStatus'] then if data['PlaybackStatus'] == 'Playing' then mpris_icon:set_image(theme.widget_play) elseif data['PlaybackStatus'] == 'Paused' then mpris_icon:set_image(theme.widget_pause) else mpris_icon:set_image(theme.widget_stop) mpris_widget.text = 'Nothing playing' end elseif data['Metadata'] then data = data['Metadata'] local title = data['xesam:title'] or '' local artist = (data['xesam:artist'] or {})[1] or '' mpris_widget.text = artist .. ' | ' .. title end end lgi.Gio.bus_get_sync(lgi.Gio.BusType.SESSION):signal_subscribe( nil, 'org.freedesktop.DBus.Properties', 'PropertiesChanged', '/org/mpris/MediaPlayer2', nil, lgi.Gio.DBusSignalFlags.NONE, signal_cbk ) local mpris_buttons = awful.util.table.join( awful.button({}, 1, mpris.play_pause), awful.button({}, 2, mpris.previous), awful.button({}, 3, mpris.skip) ) mpris_icon:buttons(mpris_buttons) mpris_widget:buttons(mpris_buttons) return { wibox.layout.fixed.horizontal(mpris_icon, mpris_widget) }