2019-08-13 21:57:54 +02:00
|
|
|
--
|
2019-07-24 07:08:20 +02:00
|
|
|
-- Control playing music/media and sound volume
|
|
|
|
-- Currently supports :
|
|
|
|
-- - playerctl
|
|
|
|
-- - Spotify
|
2019-08-13 21:57:54 +02:00
|
|
|
--
|
2019-07-24 07:08:20 +02:00
|
|
|
|
2019-08-13 21:57:54 +02:00
|
|
|
--
|
2019-07-24 07:08:20 +02:00
|
|
|
-- DEBUG
|
2019-08-13 21:57:54 +02:00
|
|
|
--
|
2019-08-13 23:03:08 +02:00
|
|
|
local inspect = require("simple/debug/inspect")
|
2019-07-24 07:08:20 +02:00
|
|
|
|
|
|
|
local awful = require("awful")
|
2019-07-24 11:33:49 +02:00
|
|
|
local beautiful = require("beautiful")
|
|
|
|
local gears = require("gears")
|
2019-07-24 07:08:20 +02:00
|
|
|
local naughty = require("naughty")
|
2019-07-24 11:33:49 +02:00
|
|
|
local wibox = require("wibox")
|
|
|
|
|
2019-08-13 23:58:50 +02:00
|
|
|
local hotkey = require("simple/core/hotkey")
|
|
|
|
|
2019-07-24 11:33:49 +02:00
|
|
|
local config = require("config")
|
2019-07-24 07:08:20 +02:00
|
|
|
|
|
|
|
local hasPlayerctl = os.execute("playerctl -v") == true
|
|
|
|
|
|
|
|
-- playerctl
|
|
|
|
function sendToPlayerctl(command)
|
|
|
|
os.execute("playerctl " .. command)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Spotify
|
|
|
|
function sendToSpotify(command)
|
|
|
|
awful.util.spawn_with_shell("dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player." .. command)
|
|
|
|
end
|
|
|
|
|
2020-07-22 17:07:53 +02:00
|
|
|
-- Brightness control
|
|
|
|
function getBrightness()
|
|
|
|
local handle = io.popen("xbacklight -get")
|
|
|
|
local brightness = handle:read("*a")
|
|
|
|
handle:close()
|
|
|
|
return brightness
|
|
|
|
end
|
|
|
|
|
|
|
|
function increaseBrightness()
|
|
|
|
os.execute("xbacklight -inc 2")
|
|
|
|
end
|
|
|
|
|
|
|
|
function decreaseBrightness()
|
|
|
|
os.execute("xbacklight -dec 2")
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-07-24 11:33:49 +02:00
|
|
|
-- Volume control
|
|
|
|
function getVolume()
|
2019-07-25 16:26:23 +02:00
|
|
|
if isMuted() then return 0 end
|
2020-03-28 19:48:04 +01:00
|
|
|
local handle = io.popen("amixer " .. config.audio.card .. " sget '" .. config.audio.device .. "' -M | awk -F\"[][]\" '/%/ { print $2 }' | head -n 1", "r")
|
2019-07-24 11:33:49 +02:00
|
|
|
local volume = string.gsub(handle:read("*a"), "[\n%%]+", "") / 100
|
|
|
|
handle:close()
|
|
|
|
return volume
|
|
|
|
end
|
2019-08-13 21:57:54 +02:00
|
|
|
|
2019-07-25 16:26:23 +02:00
|
|
|
function isMuted()
|
2020-03-28 19:48:04 +01:00
|
|
|
local handle = io.popen("amixer " .. config.audio.card .. " sget '" .. config.audio.device .. "' | egrep 'Playback.*?\\[o' | egrep -o '\\[o.+\\]'", "r")
|
2020-04-12 14:59:17 +02:00
|
|
|
local muted = string.match(string.gsub(handle:read("*a"), "[\n]+", ""), "off")
|
2019-07-25 16:26:23 +02:00
|
|
|
handle:close()
|
|
|
|
return muted
|
|
|
|
end
|
2019-07-24 11:33:49 +02:00
|
|
|
|
|
|
|
function getVolumeImage(volume)
|
|
|
|
local path = beautiful.volume_osd_icon_3
|
|
|
|
if volume < 0.667 then
|
|
|
|
path = beautiful.volume_osd_icon_2
|
|
|
|
end
|
|
|
|
if volume < 0.334 then
|
|
|
|
path = beautiful.volume_osd_icon_1
|
|
|
|
end
|
|
|
|
if volume <= 0 then
|
|
|
|
path = beautiful.volume_osd_icon_0
|
|
|
|
end
|
|
|
|
return gears.color.recolor_image(path, beautiful.volume_osd_image_color)
|
|
|
|
end
|
|
|
|
|
|
|
|
local volumeWibox = wibox({
|
2019-08-13 21:57:54 +02:00
|
|
|
ontop = true,
|
2019-07-24 11:33:49 +02:00
|
|
|
bg = beautiful.volume_osd_bg,
|
|
|
|
border_width = beautiful.volume_osd_border_width,
|
|
|
|
border_color = beautiful.volume_osd_border_color,
|
|
|
|
shape = beautiful.volume_osd_shape,
|
|
|
|
width = beautiful.volume_osd_width,
|
2019-08-27 23:39:14 +02:00
|
|
|
height = beautiful.volume_osd_bar_height + beautiful.volume_osd_padding * 3,
|
2019-07-24 11:33:49 +02:00
|
|
|
widget = wibox.widget {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
image = getVolumeImage(1),
|
|
|
|
widget = wibox.widget.imagebox
|
|
|
|
},
|
2019-08-27 23:39:14 +02:00
|
|
|
left = beautiful.volume_osd_padding,
|
|
|
|
right = beautiful.volume_osd_padding,
|
|
|
|
top = beautiful.volume_osd_padding,
|
|
|
|
bottom = 0,
|
|
|
|
widget = wibox.container.margin,
|
2019-07-24 11:33:49 +02:00
|
|
|
},
|
|
|
|
{
|
2019-08-27 23:39:14 +02:00
|
|
|
{
|
|
|
|
{
|
|
|
|
max_value = 1,
|
|
|
|
value = 0.33,
|
|
|
|
shape = gears.shape.rounded_bar,
|
|
|
|
bar_shape = gears.shape.rounded_bar,
|
|
|
|
margins = beautiful.volume_osd_padding + 4,
|
|
|
|
color = beautiful.volume_osd_progress_color,
|
|
|
|
background_color = beautiful.volume_osd_progress_bg,
|
|
|
|
border_color = beautiful.volume_osd_progress_border_color,
|
|
|
|
border_width = beautiful.volume_osd_progress_border_width,
|
|
|
|
widget = wibox.widget.progressbar
|
|
|
|
},
|
|
|
|
forced_width = 80,
|
|
|
|
forced_height = 80,
|
|
|
|
direction = "east",
|
|
|
|
layout = wibox.container.rotate,
|
|
|
|
},
|
|
|
|
left = beautiful.volume_osd_padding,
|
|
|
|
right = beautiful.volume_osd_padding,
|
|
|
|
top = 0,
|
|
|
|
bottom = beautiful.volume_osd_padding,
|
|
|
|
widget = wibox.container.margin,
|
2019-07-24 11:33:49 +02:00
|
|
|
},
|
|
|
|
layout = wibox.layout.align.vertical
|
2019-08-27 23:39:14 +02:00
|
|
|
},
|
2019-07-24 11:33:49 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
local imagebox = volumeWibox.widget:get_children()[1]:get_children()[1]
|
2019-08-27 23:39:14 +02:00
|
|
|
local progressbar = volumeWibox.widget:get_children()[2]:get_children()[1]:get_children()[1]
|
2019-07-24 11:33:49 +02:00
|
|
|
|
|
|
|
local timer = nil
|
|
|
|
|
2019-07-24 07:08:20 +02:00
|
|
|
-- {{{ Control
|
2019-07-24 11:33:49 +02:00
|
|
|
local playPause = function()
|
|
|
|
if hasPlayerctl then
|
|
|
|
sendToPlayerctl("play-pause")
|
|
|
|
else
|
|
|
|
sendToSpotify("PlayPause")
|
2019-07-24 07:08:20 +02:00
|
|
|
end
|
2019-07-24 11:33:49 +02:00
|
|
|
end
|
2019-07-24 07:08:20 +02:00
|
|
|
|
2019-07-24 11:33:49 +02:00
|
|
|
local nextTrack = function()
|
|
|
|
if hasPlayerctl then
|
|
|
|
sendToPlayerctl("next")
|
|
|
|
else
|
|
|
|
sendToSpotify("Next")
|
2019-07-24 07:08:20 +02:00
|
|
|
end
|
2019-07-24 11:33:49 +02:00
|
|
|
end
|
2019-07-24 07:08:20 +02:00
|
|
|
|
2019-07-24 11:33:49 +02:00
|
|
|
local previousTrack = function()
|
|
|
|
if hasPlayerctl then
|
|
|
|
sendToPlayerctl("previous")
|
|
|
|
else
|
|
|
|
sendToSpotify("Previous")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local displayVolume = function()
|
|
|
|
-- Update screen
|
|
|
|
local screen = awful.screen:focused()
|
|
|
|
volumeWibox.screen = screen
|
|
|
|
|
|
|
|
-- Position
|
2019-08-27 23:39:14 +02:00
|
|
|
local position = beautiful.volume_osd_position(screen.geometry.width, screen.geometry.height, beautiful.volume_osd_width, beautiful.volume_osd_height)
|
2019-07-24 11:33:49 +02:00
|
|
|
volumeWibox:geometry({
|
2019-08-27 23:39:14 +02:00
|
|
|
x = (screen.geometry.x + position[1]),
|
|
|
|
y = (screen.geometry.y + position[2])
|
2019-07-24 11:33:49 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
-- Get volume
|
|
|
|
local volume = getVolume()
|
|
|
|
|
|
|
|
-- Update values
|
|
|
|
imagebox.image = getVolumeImage(volume)
|
|
|
|
progressbar.value = volume
|
|
|
|
|
|
|
|
-- Show
|
|
|
|
volumeWibox.visible = true
|
|
|
|
|
|
|
|
-- Schedule hide
|
|
|
|
if timer ~= nil then
|
|
|
|
timer:stop()
|
2019-07-24 07:08:20 +02:00
|
|
|
end
|
2019-07-24 11:33:49 +02:00
|
|
|
timer = gears.timer {
|
|
|
|
timeout = config.volume_osd_timeout,
|
2019-08-13 21:57:54 +02:00
|
|
|
callback = function()
|
2019-07-24 11:33:49 +02:00
|
|
|
volumeWibox.visible = false
|
|
|
|
timer:stop()
|
|
|
|
timer = nil
|
|
|
|
end
|
|
|
|
}
|
|
|
|
timer:start()
|
|
|
|
end
|
|
|
|
local raiseVolume = function()
|
2020-03-28 19:48:04 +01:00
|
|
|
os.execute("amixer " .. config.audio.card .. " set '" .. config.audio.device .. "' 5%+ -M")
|
2019-07-24 11:33:49 +02:00
|
|
|
displayVolume()
|
|
|
|
end
|
|
|
|
local lowerVolume = function()
|
2020-03-28 19:48:04 +01:00
|
|
|
os.execute("amixer " .. config.audio.card .. " set '" .. config.audio.device .. "' 5%- -M")
|
2019-07-24 11:33:49 +02:00
|
|
|
displayVolume()
|
|
|
|
end
|
2019-07-25 16:26:23 +02:00
|
|
|
local toggleMute = function()
|
|
|
|
local muted = isMuted()
|
2020-03-28 19:48:04 +01:00
|
|
|
os.execute("amixer " .. config.audio.card .. " set '" .. config.audio.device .. "' " .. (muted and 'on' or 'off'))
|
2019-07-25 16:26:23 +02:00
|
|
|
displayVolume()
|
|
|
|
end
|
2019-07-24 07:08:20 +02:00
|
|
|
-- }}}
|
|
|
|
|
2020-10-02 18:38:44 +02:00
|
|
|
function getKeys(keys)
|
|
|
|
if config.audio.card == 'disabled' then
|
|
|
|
return
|
|
|
|
-- Media control
|
|
|
|
hotkey.make(keys.playPause, playPause,
|
|
|
|
{ description = "Toggle Play / Pause", group = "media control" }),
|
|
|
|
hotkey.make(keys.nextTrack, nextTrack,
|
|
|
|
{ description = "Next track", group = "media control" }),
|
|
|
|
hotkey.make(keys.previousTrack, previousTrack,
|
|
|
|
{ description = "Previous track", group = "media control" })
|
|
|
|
end
|
|
|
|
|
2019-08-13 21:57:54 +02:00
|
|
|
return
|
|
|
|
-- Volume control
|
2020-10-02 18:38:44 +02:00
|
|
|
hotkey.make(keys.raiseVolume, raiseVolume,
|
2019-08-13 23:58:50 +02:00
|
|
|
{ description = "Raise volume", group = "media control" }),
|
2020-10-02 18:38:44 +02:00
|
|
|
hotkey.make(keys.lowerVolume, lowerVolume,
|
2019-08-13 23:58:50 +02:00
|
|
|
{ description = "Lower volume", group = "media control" }),
|
2020-10-02 18:38:44 +02:00
|
|
|
hotkey.make(keys.toggleMute, toggleMute,
|
2019-08-13 23:58:50 +02:00
|
|
|
{ description = "Toggle mute audio", group = "media control" }),
|
|
|
|
|
|
|
|
-- Media control
|
2020-10-02 18:38:44 +02:00
|
|
|
hotkey.make(keys.playPause, playPause,
|
2019-08-13 23:58:50 +02:00
|
|
|
{ description = "Toggle Play / Pause", group = "media control" }),
|
2020-10-02 18:38:44 +02:00
|
|
|
hotkey.make(keys.nextTrack, nextTrack,
|
2019-08-13 23:58:50 +02:00
|
|
|
{ description = "Next track", group = "media control" }),
|
2020-10-02 18:38:44 +02:00
|
|
|
hotkey.make(keys.previousTrack, previousTrack,
|
2020-07-22 17:07:53 +02:00
|
|
|
{ description = "Previous track", group = "media control" }),
|
|
|
|
|
|
|
|
-- Backlight control
|
2020-10-02 18:51:24 +02:00
|
|
|
hotkey.make(keys.brightnessUp, increaseBrightness,
|
2020-07-22 17:07:53 +02:00
|
|
|
{ description = "Increase screen backlight brightness", group = "media control"}),
|
2020-10-02 18:51:24 +02:00
|
|
|
hotkey.make(keys.brightnessDown, decreaseBrightness,
|
2020-07-22 17:07:53 +02:00
|
|
|
{ description = "Decrease screen backlight brightness", group = "media control"})
|
2019-08-13 21:57:54 +02:00
|
|
|
end
|
|
|
|
|
2019-07-24 07:08:20 +02:00
|
|
|
return {
|
2019-08-13 21:57:54 +02:00
|
|
|
getKeys = getKeys,
|
2019-07-24 07:08:20 +02:00
|
|
|
}
|