awesomewm/modules/mpc/widget.lua

219 lines
4.8 KiB
Lua
Raw Normal View History

local mpc = req_rel(..., "util")
2021-08-01 07:24:26 -07:00
local mpc_widget = {}
mpc_widget.title = wibox.widget.textbox("MPD is not")
mpc_widget.title.valign = "center"
mpc_widget.title.align = "left"
2023-08-21 19:00:27 -07:00
mpc_widget.title.font = config.core.font.normal
2021-08-01 07:24:26 -07:00
mpc_widget.title.ellipsize = "end"
mpc_widget.title.forced_width = beautiful.dpi(config.mpc.width)
2021-08-01 07:24:26 -07:00
mpc_widget.artist = wibox.widget.textbox("connected")
mpc_widget.artist.valign = "center"
mpc_widget.artist.align = "left"
2023-08-21 19:00:27 -07:00
mpc_widget.artist.font = config.core.font.normal
2021-08-01 07:24:26 -07:00
mpc_widget.artist.ellipsize = "end"
mpc_widget.artist.forced_width = beautiful.dpi(config.mpc.width)
2021-08-01 07:24:26 -07:00
mpc_widget.volume = wibox.widget.textbox("??")
mpc_widget.volume.valign = "center"
mpc_widget.volume.align = "left"
2023-08-23 09:16:33 -07:00
mpc_widget.volume.font = config.core.font.normal_small
2021-08-01 07:24:26 -07:00
mpc_widget.volume.ellipsize = "end"
mpc_widget.volume.forced_width = beautiful.dpi(10)
mpc_widget.icon_play = wibox.widget {
resize = true,
image = beautiful.icons.music.blue.playing,
2021-08-01 07:24:26 -07:00
widget = wibox.widget.imagebox
}
mpc_widget.left = wibox.widget {
wibox.widget {
{
mpc_widget.icon_play,
top = beautiful.dpi(2),
bottom = beautiful.dpi(2),
left = 0, right = 0,
layout = wibox.container.margin,
},
mpc_widget.volume,
forced_num_cols = 1,
forced_num_rows = 2,
homogeneous = true,
expand = true,
layout = wibox.layout.grid
},
layout = wibox.container.background
}
mpc_widget.right = wibox.widget {
wibox.widget {
mpc_widget.title,
mpc_widget.artist,
forced_num_cols = 1,
forced_num_rows = 2,
homogeneous = true,
expand = true,
layout = wibox.layout.grid
},
layout = wibox.container.background,
}
mpc_widget.widget = wibox.widget {
{
2023-08-23 09:16:33 -07:00
{
mpc_widget.left,
mpc_widget.right,
forced_num_cols = 2,
forced_num_rows = 1,
homogeneous = false,
vertical_homogenous = true;
horizontal_homogenous = false;
expand = true,
layout = wibox.layout.grid
2021-08-01 07:24:26 -07:00
},
2023-08-23 09:16:33 -07:00
top = 0, bottom = 0,
left = beautiful.dpi(2),
right = beautiful.dpi(2),
layout = wibox.container.margin,
2021-08-01 07:24:26 -07:00
},
layout = wibox.container.background,
}
mpc_widget.widget:connect_signal("mouse::enter",function(result)
2021-08-01 07:24:26 -07:00
mpc_widget.widget.bg = beautiful.color.bar.hover_bg
end)
mpc_widget.widget:connect_signal("mouse::leave", function(result)
mpc_widget.widget.bg = beautiful.color.transparent
end)
mpc_widget.widget:connect_signal("button::press",
function(_, _, _, button, mods)
if (button == 3) or (button == 1)then
mpc.mpc_command("toggle")
2021-08-01 07:24:26 -07:00
end
mpc_widget.readupdate()
end
)
mpc_widget.right:connect_signal("button::press",
function(_, _, _, button, mods)
if (button == 4) then -- Scroll up
mpc.mpc_command("prev")
2021-08-01 07:24:26 -07:00
elseif (button == 5) then -- Scroll down
mpc.mpc_command("next")
2021-08-01 07:24:26 -07:00
end
mpc_widget.readupdate()
end
)
mpc_widget.left:connect_signal("button::press",
function(_, _, _, button, mods)
if (button == 4) then -- Scroll up
mpc.mpc_command("volume +5")
2021-08-01 07:24:26 -07:00
elseif (button == 5) then -- Scroll down
mpc.mpc_command("volume -5")
2021-08-01 07:24:26 -07:00
end
mpc_widget.readupdate()
end
)
2023-09-02 18:26:22 -07:00
--[[
function pango.escape(text)
local replacements = {
["&"] = "&",
["<"] = "&lt;",
[">"] = "&gt;",
["\""] = "&quot;",
["'"] = "&apos;",
}
return string.gsub(text, "[&<>'\"]", replacements)
end
]]--
2021-08-01 07:24:26 -07:00
mpc_widget.readupdate = function()
-- Update title and artist
mpc.mpc_command("current --format \"title=%title% artist=%artist%\"",
2021-08-01 07:24:26 -07:00
function(stdout, stderr, exitreason, exitcode)
if (stdout == "") then
return
end
2023-09-02 18:26:22 -07:00
--debug_message(stdout);
--debug_message(string.match(stdout, "title=(.*)artist="));
mpc_widget.title.text = string.match(stdout, "title=(.*)artist=")
mpc_widget.artist.text = string.match(stdout, "artist=(.*)")
2021-08-01 07:24:26 -07:00
end
)
-- Update status
mpc.mpc_command("status",
2021-08-01 07:24:26 -07:00
function(stdout, stderr, exitreason, exitcode)
local play = string.match(stdout, "(%[playing)") or false
local pause = string.match(stdout, "(%[paused)") or false
local stop = not (play or pause)
if (play) then
mpc_widget.icon_play.image = beautiful.icons.music.blue.playing
elseif (pause) then
mpc_widget.icon_play.image = beautiful.icons.music.blue.paused
elseif (stop) then
mpc_widget.icon_play.image = beautiful.icons.music.blue.stopped
end
2023-09-02 18:26:22 -07:00
local volume = string.match(stdout, "volume: ?(%d?%d?%d)%%")
if (volume) then
2023-09-02 18:26:22 -07:00
mpc_widget.volume.text = volume .. "%"
else
2023-09-02 18:26:22 -07:00
mpc_widget.volume.text = ""
end
2021-08-01 07:24:26 -07:00
end
)
end
-- Short delay here, the --wait option makes mpc block until
-- a change is detected.
mpc.mpc_watch(
"current --wait",
0.01,
function(_, stdout)
mpc_widget.readupdate()
end,
mpc_widget.widget
)
mpc.mpc_watch(
"current",
1,
function(_, stdout)
mpc_widget.readupdate()
end,
mpc_widget.widget
)
2021-08-01 07:24:26 -07:00
-- Make sure you do an initial read, otherwise the widget will
-- not update until a change occurs.
2021-08-01 07:24:26 -07:00
mpc_widget.readupdate()
return mpc_widget