volume.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. --[[
  2. Awesome WM Volumen Widget
  3. Distopico Vegan <distopico [at] riseup [dot] net>
  4. Licensed under GPL3
  5. Original from: https://github.com/mrzapp/awesomerc
  6. --]]
  7. local wibox = require("wibox")
  8. local awful = require("awful")
  9. local gears = require("gears")
  10. local naughty = require("naughty")
  11. local helpers = require("widgets/helpers")
  12. local config = awful.util.getdir("config")
  13. local widget = {}
  14. local popup = nil
  15. local volumetext = "--"
  16. local volumecurrent = nil
  17. local iconpath = ""
  18. -- {{{ Define subwidgets
  19. widget.text = wibox.widget.textbox()
  20. widget._icon = wibox.widget.imagebox()
  21. -- {{{ Define interactive behaviour
  22. widget._icon:buttons(gears.table.join(
  23. awful.button({ }, 1, function ()
  24. awful.spawn("pavucontrol -t 4", {
  25. floating = true,
  26. tag = mouse.screen.selected_tag,
  27. placement = awful.placement.centered,
  28. })
  29. end)
  30. ))
  31. -- }}}
  32. -- {{{ Update method
  33. function widget:update()
  34. -- TODO: change amixer to pactl
  35. -- see: https://unix.stackexchange.com/questions/132230/read-out-pulseaudio-volume-from-commandline-i-want-pactl-get-sink-volume
  36. -- https://gist.github.com/Ropid/3a08d70a807e35c7e8c688d54b725e8e
  37. local status = helpers:run("amixer sget Master")
  38. local volume = tonumber(string.match(status, "(%d?%d?%d)%%")) or 0
  39. volumetext = volume .. "% Volume"
  40. widget.text:set_markup(volumetext)
  41. iconpath = config.."/theme/icons/status/audio-volume"
  42. if string.find(status, "[off]", 1, true) or volume <= 0.0 then
  43. iconpath = iconpath .. "-muted"
  44. elseif volume < 25 then
  45. iconpath = iconpath .. "-low"
  46. elseif volume > 75 then
  47. iconpath = iconpath .. "-high"
  48. else
  49. iconpath = iconpath .. "-medium"
  50. end
  51. iconpath = iconpath .. "-symbolic.svg"
  52. if volumecurrent ~= volume and volumecurrent ~= nil then
  53. widget.showPopup(0.1)
  54. end
  55. widget._icon:set_image(iconpath)
  56. widget.icon = helpers:set_draw_method(widget._icon)
  57. volumecurrent = volume
  58. end
  59. function widget:showPopup(timeout)
  60. widget.hidePopup()
  61. popup = naughty.notify({
  62. icon = iconpath,
  63. icon_size = 16,
  64. text = volumetext,
  65. timeout = timeout, hover_timeout = 0.5,
  66. screen = mouse.screen,
  67. ignore_suspend = true
  68. })
  69. end
  70. function widget:hidePopup()
  71. if popup ~= nil then
  72. naughty.destroy(popup)
  73. popup = nil
  74. end
  75. end
  76. function widget:raise()
  77. awful.spawn("amixer set Master 1%+", false)
  78. helpers:delay(widget.update, 0.1)
  79. end
  80. function widget:lower()
  81. awful.spawn("amixer set Master 1%-", false)
  82. helpers:delay(widget.update, 0.1)
  83. end
  84. function widget:mute()
  85. awful.spawn("amixer -D pulse set Master 1+ toggle", false)
  86. helpers:delay(widget.update, 0.1)
  87. end
  88. -- }}}
  89. -- {{{ Listen
  90. helpers:listen(widget, 40)
  91. widget._icon:connect_signal("mouse::enter", function() widget:showPopup() end)
  92. widget._icon:connect_signal("mouse::leave", function() widget:hidePopup() end)
  93. -- }}}
  94. return widget