Started migrating to module architecture
This commit is contained in:
6
modules/mpc/init.lua
Normal file
6
modules/mpc/init.lua
Normal file
@ -0,0 +1,6 @@
|
||||
return {
|
||||
widgets = {
|
||||
mpc = require("modules.mpc.widget").widget
|
||||
},
|
||||
keybinds = require("modules.mpc.keybinds")
|
||||
}
|
53
modules/mpc/keybinds.lua
Executable file
53
modules/mpc/keybinds.lua
Executable file
@ -0,0 +1,53 @@
|
||||
local mpc = req_rel(..., "util")
|
||||
|
||||
return gears.table.join(
|
||||
awful.key( {}, "XF86AudioPrev",
|
||||
function ()
|
||||
mpc.mpc_command("prev")
|
||||
end,
|
||||
{
|
||||
description = "Previous track",
|
||||
group = "MPC"
|
||||
}
|
||||
),
|
||||
|
||||
awful.key( {}, "XF86AudioPlay",
|
||||
function ()
|
||||
mpc.mpc_command("toggle")
|
||||
end,
|
||||
{
|
||||
description = "Play/Pause",
|
||||
group = "MPC"
|
||||
}
|
||||
),
|
||||
|
||||
awful.key( {}, "XF86AudioNext",
|
||||
function ()
|
||||
mpc.mpc_command("next")
|
||||
end,
|
||||
{
|
||||
description = "Next track",
|
||||
group = "MPC"
|
||||
}
|
||||
),
|
||||
|
||||
awful.key( {"Mod4"}, "XF86AudioRaiseVolume",
|
||||
function ()
|
||||
mpc.mpc_command("vol +5")
|
||||
end,
|
||||
{
|
||||
description = "Volume up",
|
||||
group = "MPC"
|
||||
}
|
||||
),
|
||||
|
||||
awful.key( {"Mod4"}, "XF86AudioLowerVolume",
|
||||
function ()
|
||||
mpc.mpc_command("vol -5")
|
||||
end,
|
||||
{
|
||||
description = "Volume down",
|
||||
group = "MPC"
|
||||
}
|
||||
)
|
||||
)
|
19
modules/mpc/util.lua
Normal file
19
modules/mpc/util.lua
Normal file
@ -0,0 +1,19 @@
|
||||
local util = {}
|
||||
|
||||
util.mpc_command = function(command, callback)
|
||||
awful.spawn.easy_async(
|
||||
"mpc --host " .. conf.mpd_host .. " " .. command,
|
||||
callback
|
||||
)
|
||||
end
|
||||
|
||||
util.mpc_watch = function(command, timeout, callback, widget)
|
||||
awful.widget.watch(
|
||||
"mpc --host " .. conf.mpd_host .. " " .. command,
|
||||
timeout,
|
||||
callback,
|
||||
widget
|
||||
)
|
||||
end
|
||||
|
||||
return util
|
198
modules/mpc/widget.lua
Normal file
198
modules/mpc/widget.lua
Normal file
@ -0,0 +1,198 @@
|
||||
local mpc = req_rel(..., "util")
|
||||
local mpc_widget = {}
|
||||
|
||||
mpc_widget.title = wibox.widget.textbox("MPD is not")
|
||||
mpc_widget.title.valign = "center"
|
||||
mpc_widget.title.align = "left"
|
||||
mpc_widget.title.font = "Hack NF 12"
|
||||
mpc_widget.title.ellipsize = "end"
|
||||
mpc_widget.title.forced_width = beautiful.dpi(conf.mpc_width)
|
||||
|
||||
mpc_widget.artist = wibox.widget.textbox("connected")
|
||||
mpc_widget.artist.valign = "center"
|
||||
mpc_widget.artist.align = "left"
|
||||
mpc_widget.artist.font = "Hack NF 12"
|
||||
mpc_widget.artist.ellipsize = "end"
|
||||
mpc_widget.artist.forced_width = beautiful.dpi(conf.mpc_width)
|
||||
|
||||
mpc_widget.volume = wibox.widget.textbox("??")
|
||||
mpc_widget.volume.valign = "center"
|
||||
mpc_widget.volume.align = "left"
|
||||
mpc_widget.volume.font = "Hack NF 10"
|
||||
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,
|
||||
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 {
|
||||
{
|
||||
{ -- Right spacer
|
||||
widget = wibox.widget.separator,
|
||||
color = beautiful.color.transparent,
|
||||
forced_width = beautiful.dpi(3)
|
||||
},
|
||||
|
||||
mpc_widget.left,
|
||||
mpc_widget.right,
|
||||
|
||||
{ -- Left spacer
|
||||
widget = wibox.widget.separator,
|
||||
color = beautiful.color.transparent,
|
||||
forced_width = beautiful.dpi(3)
|
||||
},
|
||||
layout = wibox.layout.align.horizontal,
|
||||
},
|
||||
layout = wibox.container.background,
|
||||
}
|
||||
|
||||
|
||||
mpc_widget.widget:connect_signal("mouse::enter",function(result)
|
||||
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")
|
||||
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")
|
||||
elseif (button == 5) then -- Scroll down
|
||||
mpc.mpc_command("next")
|
||||
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")
|
||||
elseif (button == 5) then -- Scroll down
|
||||
mpc.mpc_command("volume -5")
|
||||
end
|
||||
mpc_widget.readupdate()
|
||||
end
|
||||
)
|
||||
|
||||
mpc_widget.readupdate = function()
|
||||
-- Update title and artist
|
||||
mpc.mpc_command("current --format \"title=%title% artist=%artist%\"",
|
||||
function(stdout, stderr, exitreason, exitcode)
|
||||
if (stdout == "") then
|
||||
return
|
||||
end
|
||||
|
||||
mpc_widget.title.markup = string.match(stdout, "title=(.*)artist=")
|
||||
mpc_widget.artist.markup = string.match(stdout, "artist=(.*)")
|
||||
end
|
||||
)
|
||||
|
||||
-- Update status
|
||||
mpc.mpc_command("status",
|
||||
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
|
||||
|
||||
local volume = string.match(stdout, "volume: (%d?%d?%d)%%")
|
||||
if (volume) then
|
||||
mpc_widget.volume.markup = volume .. "%"
|
||||
else
|
||||
mpc_widget.volume.markup = ""
|
||||
end
|
||||
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
|
||||
)
|
||||
|
||||
|
||||
-- Make sure you do an initial read, otherwise the widget will
|
||||
-- not update until a change occurs.
|
||||
mpc_widget.readupdate()
|
||||
|
||||
|
||||
return mpc_widget
|
Reference in New Issue
Block a user