Merge branch 'master' of github.com:streetturtle/awesome-wm-widgets

This commit is contained in:
streetturtle 2020-04-02 22:22:16 -04:00
commit 6bee8b6880
3 changed files with 146 additions and 103 deletions

View File

@ -115,10 +115,8 @@ local function worker(args)
local battery_info = {} local battery_info = {}
local capacities = {} local capacities = {}
for s in stdout:gmatch("[^\r\n]+") do for s in stdout:gmatch("[^\r\n]+") do
local status, charge_str, time = string.match(s, '.+: (%a+), (%d?%d?%d)%%,?.*') local status, charge_str, time = string.match(s, '.+: (%a+), (%d?%d?%d)%%,?(.*)')
if string.match(s, 'rate information') then if status ~= nil then
-- ignore such line
elseif status ~= nil then
table.insert(battery_info, {status = status, charge = tonumber(charge_str)}) table.insert(battery_info, {status = status, charge = tonumber(charge_str)})
else else
local cap_str = string.match(s, '.+:.+last full capacity (%d+)') local cap_str = string.match(s, '.+:.+last full capacity (%d+)')

View File

@ -10,9 +10,9 @@ It is possible to customize widget by providing a table with all or some of the
| Name | Default | Description | | Name | Default | Description |
|---|---|---| |---|---|---|
| `volume_audio_controller` | `pulse` | audio device | | `volume_audio_controller`| `pulse` | audio device |
| `display_notification` | `false` | Display a notification on mouseover | | `display_notification` | `false` | Display a notification on mouseover and keypress |
| `notification_position` | `top_right` | The notification position | | `notification_position` | `top_right`| The notification position |
## Installation ## Installation
@ -21,15 +21,46 @@ It is possible to customize widget by providing a table with all or some of the
- include `volume.lua` and add volume widget to your wibox in rc.lua: - include `volume.lua` and add volume widget to your wibox in rc.lua:
```lua ```lua
require("volume") local volume_widget = require("awesome-wm-widgets.volume-widget.volume")
... ...
s.mytasklist, -- Middle widget s.mytasklist, -- Middle widget
{ -- Right widgets { -- Right widgets
layout = wibox.layout.fixed.horizontal, ...
... volume_widget({display_notification = true}),
volume_widget(), ...
...
``` ```
### Control volume
To mute/unmute click on the widget. To increase/decrease volume scroll up or down when mouse cursor is over the widget.
If you want to control volume level by keyboard shortcuts add following lines in shortcut section of the **rc.lua**:
IF you have notification activated, a notification will pop-up on key press
```lua
-- Key bindings
globalkeys = gears.table.join(
awful.key(
{},
'XF86AudioRaiseVolume',
volume_widget.raise,
{description = 'volume up', group = 'hotkeys'}
),
awful.key(
{},
'XF86AudioLowerVolume',
volume_widget.lower,
{description = 'volume down', group = 'hotkeys'}
),
awful.key(
{},
'XF86AudioMute',
volume_widget.toggle,
{description = 'toggle mute', group = 'hotkeys'}
),
```
### Icons
- _Optional step._ In Arc icon theme the muted audio level icon (![Volume-widget](./audio-volume-muted-symbolic.png)) looks like 0 level icon, which could be a bit misleading. - _Optional step._ In Arc icon theme the muted audio level icon (![Volume-widget](./audio-volume-muted-symbolic.png)) looks like 0 level icon, which could be a bit misleading.
So I decided to use original muted icon for low audio level, and the same icon, but colored in red for muted audio level. Fortunately icons are in svg format, so you can easily recolor them with `sed`, so it would look like this (![Volume Widget](./audio-volume-muted-symbolic_red.png)): So I decided to use original muted icon for low audio level, and the same icon, but colored in red for muted audio level. Fortunately icons are in svg format, so you can easily recolor them with `sed`, so it would look like this (![Volume Widget](./audio-volume-muted-symbolic_red.png)):
@ -74,17 +105,3 @@ volume_widget({
volume_audio_controller = 'alsa_only' volume_audio_controller = 'alsa_only'
}) })
``` ```
.
## Control volume
To mute/unmute click on the widget. To increase/decrease volume scroll up or down when mouse cursor is over the widget.
If you want to control volume level by keyboard shortcuts add following lines in shortcut section of the **rc.lua** (the commands could be slightly different depending on your PC configuration):
```lua
awful.key({ modkey}, "[", function () awful.spawn("amixer -D pulse sset Master 5%+") end, {description = "increase volume", group = "custom"}),
awful.key({ modkey}, "]", function () awful.spawn("amixer -D pulse sset Master 5%-") end, {description = "decrease volume", group = "custom"}),
awful.key({ modkey}, "\", function () awful.spawn("amixer -D pulse set Master +1 toggle") end, {description = "mute volume", group = "custom"}),
```

View File

@ -4,7 +4,7 @@
-- More details could be found here: -- More details could be found here:
-- https://github.com/streetturtle/awesome-wm-widgets/tree/master/volume-widget -- https://github.com/streetturtle/awesome-wm-widgets/tree/master/volume-widget
-- @author Pavel Makhov -- @author Pavel Makhov, Aurélien Lajoie
-- @copyright 2018 Pavel Makhov -- @copyright 2018 Pavel Makhov
------------------------------------------------- -------------------------------------------------
@ -16,35 +16,101 @@ local gfs = require("gears.filesystem")
local dpi = require('beautiful').xresources.apply_dpi local dpi = require('beautiful').xresources.apply_dpi
local PATH_TO_ICONS = "/usr/share/icons/Arc/status/symbolic/" local PATH_TO_ICONS = "/usr/share/icons/Arc/status/symbolic/"
local volume_icon_name="audio-volume-high-symbolic"
local GET_VOLUME_CMD = 'amixer sget Master'
local volume_widget = {} local volume = {device = '', display_notification = false, notification = nil}
function volume:toggle()
volume:_cmd('amixer ' .. volume.device .. ' sset Master toggle')
end
function volume:raise()
volume:_cmd('amixer ' .. volume.device .. ' sset Master 5%+')
end
function volume:lower()
volume:_cmd('amixer ' .. volume.device .. ' sset Master 5%-')
end
--{{{ Icon and notification update
--------------------------------------------------
-- Set the icon and return the message to display
-- base on sound level and mute
--------------------------------------------------
local function parse_output(stdout)
local level = string.match(stdout, "(%d?%d?%d)%%")
if stdout:find("%[off%]") then
volume_icon_name="audio-volume-muted-symbolic_red"
return level.."% <span color=\"red\"><b>Mute</b></span>"
end
level = tonumber(string.format("% 3d", level))
if (level >= 0 and level < 25) then
volume_icon_name="audio-volume-muted-symbolic"
elseif (level < 50) then
volume_icon_name="audio-volume-low-symbolic"
elseif (level < 75) then
volume_icon_name="audio-volume-medium-symbolic"
else
volume_icon_name="audio-volume-high-symbolic"
end
return level.."%"
end
--------------------------------------------------------
--Update the icon and the notification if needed
--------------------------------------------------------
local function update_graphic(widget, stdout, _, _, _)
local txt = parse_output(stdout)
widget.image = PATH_TO_ICONS .. volume_icon_name .. ".svg"
if volume.display_notification then
volume.notification.iconbox.image = PATH_TO_ICONS .. volume_icon_name .. ".svg"
naughty.replace_text(volume.notification, "Volume", txt)
end
end
local function notif(msg, keep)
if volume.display_notification then
naughty.destroy(volume.notification)
volume.notification= naughty.notify{
text = msg,
icon=PATH_TO_ICONS .. volume_icon_name .. ".svg",
icon_size = dpi(16),
title = "Volume",
position = volume.position,
timeout = keep and 0 or 2, hover_timeout = 0.5,
width = 200,
}
end
end
--}}}
local function worker(args) local function worker(args)
--{{{ Args
local args = args or {} local args = args or {}
local volume_audio_controller = args.volume_audio_controller or 'pulse' local volume_audio_controller = args.volume_audio_controller or 'pulse'
local display_notification = args.display_notification or false volume.display_notification = args.display_notification or 'false'
local position = args.notification_position or "top_right" volume.position = args.notification_position or "top_right"
local device_arg = ''
if volume_audio_controller == 'pulse' then if volume_audio_controller == 'pulse' then
device_arg = '-D pulse' volume.device = '-D pulse'
end end
GET_VOLUME_CMD = 'amixer ' .. volume.device.. ' sget Master'
local GET_VOLUME_CMD = 'amixer ' .. device_arg .. ' sget Master' --}}}
local INC_VOLUME_CMD = 'amixer ' .. device_arg .. ' sset Master 5%+' --{{{ Check for icon path
local DEC_VOLUME_CMD = 'amixer ' .. device_arg .. ' sset Master 5%-'
local TOG_VOLUME_CMD = 'amixer ' .. device_arg .. ' sset Master toggle'
if not gfs.dir_readable(PATH_TO_ICONS) then if not gfs.dir_readable(PATH_TO_ICONS) then
naughty.notify{ naughty.notify{
title = "Volume Widget", title = "Volume Widget",
text = "Folder with icons doesn't exist: " .. PATH_TO_ICONS, text = "Folder with icons doesn't exist: " .. PATH_TO_ICONS,
preset = naughty.config.presets.critical preset = naughty.config.presets.critical
} }
return
end end
--}}}
volume_widget = wibox.widget { --{{{ Widget creation
volume.widget = wibox.widget {
{ {
id = "icon", id = "icon",
image = PATH_TO_ICONS .. "audio-volume-muted-symbolic.svg", image = PATH_TO_ICONS .. "audio-volume-muted-symbolic.svg",
@ -56,79 +122,41 @@ local function worker(args)
self.icon.image = path self.icon.image = path
end end
} }
--}}}
local notification --{{{ Spawn functions
local volume_icon_name="audio-volume-high-symbolic" function volume:_cmd(cmd)
notif("")
local function get_notification_text(txt) spawn.easy_async(cmd, function(stdout, stderr, exitreason, exitcode)
local mute = string.match(txt, "%[(o%a%a?)%]") update_graphic(volume.widget, stdout, stderr, exitreason, exitcode)
local volume = string.match(txt, "(%d?%d?%d)%%") end)
volume = tonumber(string.format("% 3d", volume))
if mute == "off" then
return volume.."% <span color=\"red\"><b>Mute</b></span>"
else
return volume .."%"
end
end end
local function show_volume(val) local function show()
spawn.easy_async(GET_VOLUME_CMD, spawn.easy_async(GET_VOLUME_CMD,
function(stdout, _, _, _) function(stdout, _, _, _)
naughty.destroy(notification) txt = parse_output(stdout)
notification = naughty.notify{ notif(txt, true)
text = get_notification_text(stdout),
icon=PATH_TO_ICONS .. val .. ".svg",
icon_size = dpi(16),
title = "Volume",
position = position,
timeout = 0, hover_timeout = 0.5,
width = 200,
}
end end
) )
end end
--}}}
--{{{ Mouse event
local update_graphic = function(widget, stdout, _, _, _)
local mute = string.match(stdout, "%[(o%D%D?)%]")
local volume = string.match(stdout, "(%d?%d?%d)%%")
volume = tonumber(string.format("% 3d", volume))
if mute == "off" then volume_icon_name="audio-volume-muted-symbolic_red"
elseif (volume >= 0 and volume < 25) then volume_icon_name="audio-volume-muted-symbolic"
elseif (volume < 50) then volume_icon_name="audio-volume-low-symbolic"
elseif (volume < 75) then volume_icon_name="audio-volume-medium-symbolic"
elseif (volume <= 100) then volume_icon_name="audio-volume-high-symbolic"
end
widget.image = PATH_TO_ICONS .. volume_icon_name .. ".svg"
if display_notification then
notification.iconbox.image = PATH_TO_ICONS .. volume_icon_name .. ".svg"
naughty.replace_text(notification, "Volume", get_notification_text(stdout))
end
end
--[[ allows control volume level by: --[[ allows control volume level by:
- clicking on the widget to mute/unmute - clicking on the widget to mute/unmute
- scrolling when cursor is over the widget - scrolling when cursor is over the widget
]] ]]
volume_widget:connect_signal("button::press", function(_,_,_,button) volume.widget:connect_signal("button::press", function(_,_,_,button)
if (button == 4) then spawn(INC_VOLUME_CMD, false) if (button == 4) then volume.raise()
elseif (button == 5) then spawn(DEC_VOLUME_CMD, false) elseif (button == 5) then volume.lower()
elseif (button == 1) then spawn(TOG_VOLUME_CMD, false) elseif (button == 1) then volume.toggle()
end end
spawn.easy_async(GET_VOLUME_CMD, function(stdout, stderr, exitreason, exitcode)
update_graphic(volume_widget, stdout, stderr, exitreason, exitcode)
end)
end) end)
if volume.display_notification then
if display_notification then volume.widget:connect_signal("mouse::enter", function() show() end)
volume_widget:connect_signal("mouse::enter", function() show_volume(volume_icon_name) end) volume.widget:connect_signal("mouse::leave", function() naughty.destroy(volume.notification) end)
volume_widget:connect_signal("mouse::leave", function() naughty.destroy(notification) end)
end end
--}}}
watch(GET_VOLUME_CMD, 1, update_graphic, volume_widget) return volume.widget
return volume_widget
end end
return setmetatable(volume_widget, { __call = function(_, ...) return worker(...) end }) return setmetatable(volume, { __call = function(_, ...) return worker(...) end })