media_control.lua: make volume management fully async

This commit is contained in:
Alice Gaudon 2021-04-08 12:42:13 +02:00
parent e8a91d98ce
commit 0bda31d908
2 changed files with 184 additions and 165 deletions

View File

@ -187,6 +187,7 @@ theme.volume_osd_border_color = theme.border_normal
theme.volume_osd_border_width = dpi(0) theme.volume_osd_border_width = dpi(0)
theme.volume_osd_progress_bg = theme.bg_minimize theme.volume_osd_progress_bg = theme.bg_minimize
theme.volume_osd_progress_color = "#ffffff" theme.volume_osd_progress_color = "#ffffff"
theme.volume_osd_progress_color_muted = "#555555"
theme.volume_osd_progress_border_color = theme.volume_osd_border_color theme.volume_osd_progress_border_color = theme.volume_osd_border_color
theme.volume_osd_progress_border_width = dpi(0) theme.volume_osd_progress_border_width = dpi(0)
theme.volume_osd_image_color = "#ffffff" theme.volume_osd_image_color = "#ffffff"

View File

@ -23,46 +23,53 @@ local config = require("config")
local hasPlayerctl = os.execute("playerctl -v") == true local hasPlayerctl = os.execute("playerctl -v") == true
-- playerctl -- playerctl
function sendToPlayerctl(command) function sendToPlayerctl(command, callback)
os.execute("playerctl " .. command) callback = callback or function()
end
awful.spawn.easy_async("playerctl " .. command, callback)
end end
-- Spotify -- Spotify
function sendToSpotify(command) function sendToSpotify(command, callback)
awful.util.spawn_with_shell("dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player." .. command) callback = callback or function()
end
awful.spawn.easy_async_with_shell("dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player." .. command, callback)
end end
-- Brightness control -- Brightness control
function getBrightness() function getBrightness(callback)
local handle = io.popen("xbacklight -get") callback = callback or function()
local brightness = handle:read("*a") end
handle:close()
return brightness awful.spawn.easy_async("xbacklight -get", function(stdout, stderr, reason, exit_code)
callback(stdout)
end)
end end
function increaseBrightness() function increaseBrightness(callback)
os.execute("xbacklight -inc 2") callback = callback or function()
end
awful.spawn.easy_async("xbacklight -inc 2", callback)
end end
function decreaseBrightness() function decreaseBrightness(callback)
os.execute("xbacklight -dec 2") callback = callback or function()
end
awful.spawn.easy_async("xbacklight -dec 2", callback)
end end
-- Volume control -- Volume control
function getVolume() function isMuted(callback)
if isMuted() then return 0 end awful.spawn.easy_async_with_shell("amixer " .. config.audio.card .. " sget '" .. config.audio.device .. "' | egrep 'Playback.*?\\[o' | egrep -o '\\[o.+\\]'", function(stdout, stderr, reason, exit_code)
local handle = io.popen("amixer " .. config.audio.card .. " sget '" .. config.audio.device .. "' -M | awk -F\"[][]\" '/%/ { print $2 }' | head -n 1", "r") callback(string.match(string.gsub(stdout, "[\n]+", ""), "off"))
local volume = string.gsub(handle:read("*a"), "[\n%%]+", "") / 100 end)
handle:close()
return volume
end end
function isMuted() function getVolume(callback)
local handle = io.popen("amixer " .. config.audio.card .. " sget '" .. config.audio.device .. "' | egrep 'Playback.*?\\[o' | egrep -o '\\[o.+\\]'", "r") awful.spawn.easy_async_with_shell("amixer " .. config.audio.card .. " sget '" .. config.audio.device .. "' -M | awk -F\"[][]\" '/%/ { print $2 }' | head -n 1", function(stdout, stderr, reason, exit_code)
local muted = string.match(string.gsub(handle:read("*a"), "[\n]+", ""), "off") callback(string.gsub(stdout, "[\n%%]+", "") / 100)
handle:close() end)
return muted
end end
function getVolumeImage(volume) function getVolumeImage(volume)
@ -159,6 +166,18 @@ local previousTrack = function()
end end
local displayVolume = function() local displayVolume = function()
isMuted(function(muted)
getVolume(function(volume)
-- Update values
imagebox.image = getVolumeImage(muted and 0 or volume)
progressbar.value = volume
progressbar.color = muted and beautiful.volume_osd_progress_color_muted or beautiful.volume_osd_progress_color
if timer ~= nil and timer.started then
timer:again()
return
end
-- Update screen -- Update screen
local screen = awful.screen:focused() local screen = awful.screen:focused()
volumeWibox.screen = screen volumeWibox.screen = screen
@ -170,13 +189,6 @@ local displayVolume = function()
y = (screen.geometry.y + position[2]) y = (screen.geometry.y + position[2])
}) })
-- Get volume
local volume = getVolume()
-- Update values
imagebox.image = getVolumeImage(volume)
progressbar.value = volume
-- Show -- Show
volumeWibox.visible = true volumeWibox.visible = true
@ -193,19 +205,25 @@ local displayVolume = function()
end end
} }
timer:start() timer:start()
end)
end)
end end
local raiseVolume = function() local raiseVolume = function()
os.execute("amixer " .. config.audio.card .. " set '" .. config.audio.device .. "' 5%+ -M") awful.spawn.easy_async("amixer " .. config.audio.card .. " set '" .. config.audio.device .. "' 5%+ -M", function(stdout, stderr, reason, exit_code)
displayVolume() displayVolume()
end)
end end
local lowerVolume = function() local lowerVolume = function()
os.execute("amixer " .. config.audio.card .. " set '" .. config.audio.device .. "' 5%- -M") awful.spawn.easy_async("amixer " .. config.audio.card .. " set '" .. config.audio.device .. "' 5%- -M", function(stdout, stderr, reason, exit_code)
displayVolume() displayVolume()
end)
end end
local toggleMute = function() local toggleMute = function()
local muted = isMuted() isMuted(function(muted)
os.execute("amixer " .. config.audio.card .. " set '" .. config.audio.device .. "' " .. (muted and 'on' or 'off')) awful.spawn.easy_async("amixer " .. config.audio.card .. " set '" .. config.audio.device .. "' " .. (muted and 'on' or 'off'), function(stdout, stderr, reason, exit_code)
displayVolume() displayVolume()
end)
end)
end end
-- }}} -- }}}
@ -240,9 +258,9 @@ function getKeys(keys)
-- Backlight control -- Backlight control
hotkey.make(keys.brightnessUp, increaseBrightness, hotkey.make(keys.brightnessUp, increaseBrightness,
{ description = "Increase screen backlight brightness", group = "media control"}), { description = "Increase screen backlight brightness", group = "media control" }),
hotkey.make(keys.brightnessDown, decreaseBrightness, hotkey.make(keys.brightnessDown, decreaseBrightness,
{ description = "Decrease screen backlight brightness", group = "media control"}) { description = "Decrease screen backlight brightness", group = "media control" })
end end
return { return {