awesomewm/classes/mpc/mpc.lua

171 lines
3.3 KiB
Lua
Raw Normal View History

2022-04-24 12:22:44 -07:00
local widget_types = {
basic = require("classes/mpc/widget"),
}
local P = {
name = "mpc"
}
2022-04-24 12:27:54 -07:00
P.__index = P
2022-04-24 12:22:44 -07:00
---
-- Internal methods
---
function P:_cmd(command)
-- Build complete pamixer arguments, combining sink and options.
-- Must have a leading space.
return "mpc --host " .. self.mpd_host .. " " .. command
end
function P:_get_status()
-- Update mute status and volume.
-- Values in this table are updated.
awful.spawn.easy_async(
self:_cmd("current --format \"title=%title% artist=%artist%\""),
function(stdout, stderr, exitreason, exitcode)
if (stdout == "") then
return
end
self.current_title = string.match(stdout, "title=(.*)artist=")
self.current_artist = string.match(stdout, "artist=(.*)")
self:_update_widget()
end
)
awful.spawn.easy_async(
self:_cmd("status"),
function(stdout, stderr, exitreason, exitcode)
self.is_playing = string.match(stdout, "(%[playing)") or false
self.is_paused = string.match(stdout, "(%[paused)") or false
self.is_stopped = not (self.is_playing or self.is_paused)
self.volume = string.match(stdout, "volume: ?(%d?%d?%d)%%")
self:_update_widget()
end
)
-- This is used inside a timer, so return true to keep it running.
return true
end
function P:_update_widget()
self.widget:update{
title = self.current_title,
artist = self.current_artist,
play = self.is_playing,
pause = self.is_paused,
stop = self.is_stopped,
volume = self.volume or 0
}
end
---
-- Simple actions
---
function P:volume_up()
awful.spawn(self:_cmd("volume +5"), false)
self:_get_status()
end
function P:volume_down()
awful.spawn(self:_cmd("volume -5"), false)
self:_get_status()
end
function P:toggle()
awful.spawn(self:_cmd("toggle"), false)
self:_get_status()
end
function P:next()
awful.spawn(self:_cmd("next"), false)
self:_get_status()
end
function P:prev()
awful.spawn(self:_cmd("prev"), false)
self:_get_status()
end
---
-- Create new volume_interface
---
function P:new(args)
-- Arguments
local m = {
update_interval = args.update_interal or 1,
widget_type = args.widget_type or "basic",
mpd_host = args.mpd_host or "localhost",
}
setmetatable(m, self)
-- Create widget for this volume interface
m.widget = widget_types[m.widget_type]:new()
m.widget.widget:connect_signal("button::press",
function(_, _, _, button, mods)
if (button == 3) or (button == 1)then
m:toggle()
end
end
)
m.widget.right:connect_signal("button::press",
function(_, _, _, button, mods)
if (button == 4) then -- Scroll up
m:prev()
elseif (button == 5) then -- Scroll down
m:next()
end
end
)
m.widget.left:connect_signal("button::press",
function(_, _, _, button, mods)
if (button == 4) then -- Scroll up
m:volume_up()
elseif (button == 5) then -- Scroll down
m:volume_down()
end
end
)
-- Polling timer
m.timer_poll = gears.timer {
timeout = m.update_interval,
call_now = true,
autostart = true,
callback = function()
m:_get_status()
end
}
--[[
-- Timer waiting for changes
-- DO NOT USE, WILL DOS MPD
m.timer_wait = gears.timer {
timeout = 0.1,
call_now = true,
autostart = true,
callback = function()
awful.spawn.easy_async(
m:_cmd("current --wait"),
function(stdout, stderr, exitreason, exitcode)
m:_get_status()
end
)
end
}
--]]
return m
end
return P