Guest User

Untitled

a guest
Feb 10th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.87 KB | None | 0 0
  1. local wibox = require("wibox")
  2. local awful = require("awful")
  3.  
  4. volume_widget = wibox.widget.textbox()
  5. volume_widget:set_align("right")
  6. volume_widget:set_font("DejaVu Sans Mono 9")
  7.  
  8. function update_volume(widget)
  9.     -- The following approach uses io.popen() to launch external application
  10.     -- It is not good, because it causes application lock
  11.     -- We will use awful.util.pread() instead
  12.     --local fd = io.popen("amixer sget Master")
  13.     --local status = fd:read("*all")
  14.     --fd:close()
  15.     local status = awful.util.pread("amixer sget Master")
  16.  
  17.     -- Parse it
  18.     local volume = string.match(status, "(%d?%d?%d)%%")
  19.     volume = string.format("% 3d", volume)
  20.     status = string.match(status, "%[(o[^%]]*)%]")
  21.  
  22.     if string.find(status, "on", 1, true) then
  23.         -- For the volume numbers
  24.         volume = volume .. "%"
  25.     else
  26.         -- For the mute button
  27.         volume = volume .. "M"
  28.     end
  29.    
  30.     widget:set_markup(volume)
  31.     widget:buttons(awful.util.table.join(
  32.             -- Left-click to toggle
  33.             awful.button({ }, 1, function () awful.util.spawn_with_shell('amixer set Master toggle && echo "update_volume(volume_widget)" | awesome-client') end),
  34.             -- Right-click to launch pavucontrol
  35.             awful.button({ }, 3, function () awful.util.spawn("pavucontrol") end),
  36.             -- Scroll to change volume
  37.             awful.button({ }, 4, function () awful.util.spawn_with_shell('amixer set Master on 4%+ && echo "update_volume(volume_widget)" | awesome-client') end),
  38.             awful.button({ }, 5, function () awful.util.spawn_with_shell('amixer set Master on 4%- && echo "update_volume(volume_widget)" | awesome-client') end)))
  39. end
  40.  
  41. update_volume(volume_widget)
  42.  
  43. -- Update every 5 seconds (widget data is updated automatically, when you change volume using keybinds defined in rc.lua. See Multimedia section in rc.lua)
  44. mytimer = timer({ timeout = 5 })
  45. mytimer:connect_signal("timeout", function () update_volume(volume_widget) end)
  46. mytimer:start()
Add Comment
Please, Sign In to add comment