alsabar.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. --[[
  2. Licensed under GNU General Public License v2
  3. * (c) 2013, Luca CPZ
  4. * (c) 2013, Rman
  5. --]]
  6. local helpers = require("lain.helpers")
  7. local awful = require("awful")
  8. local naughty = require("naughty")
  9. local wibox = require("wibox")
  10. local math = math
  11. local string = string
  12. local type = type
  13. local tonumber = tonumber
  14. -- ALSA volume bar
  15. -- lain.widget.alsabar
  16. local function factory(args)
  17. local alsabar = {
  18. colors = {
  19. background = "#000000",
  20. mute = "#EB8F8F",
  21. unmute = "#A4CE8A"
  22. },
  23. _current_level = 0,
  24. _playback = "off"
  25. }
  26. args = args or {}
  27. local timeout = args.timeout or 5
  28. local settings = args.settings or function() end
  29. local width = args.width or 63
  30. local height = args.height or 1
  31. local margins = args.margins or 1
  32. local ticks = args.ticks or false
  33. local ticks_size = args.ticks_size or 7
  34. local tick = args.tick or "|"
  35. local tick_pre = args.tick_pre or "["
  36. local tick_post = args.tick_post or "]"
  37. local tick_none = args.tick_none or " "
  38. alsabar.cmd = args.cmd or "amixer"
  39. alsabar.channel = args.channel or "Master"
  40. alsabar.togglechannel = args.togglechannel
  41. alsabar.colors = args.colors or alsabar.colors
  42. alsabar.followtag = args.followtag or false
  43. alsabar.notification_preset = args.notification_preset
  44. if not alsabar.notification_preset then
  45. alsabar.notification_preset = { font = "Monospace 10" }
  46. end
  47. local format_cmd = string.format("%s get %s", alsabar.cmd, alsabar.channel)
  48. if alsabar.togglechannel then
  49. format_cmd = { awful.util.shell, "-c", string.format("%s get %s; %s get %s",
  50. alsabar.cmd, alsabar.channel, alsabar.cmd, alsabar.togglechannel) }
  51. end
  52. alsabar.bar = wibox.widget {
  53. color = alsabar.colors.unmute,
  54. background_color = alsabar.colors.background,
  55. forced_height = height,
  56. forced_width = width,
  57. margins = margins,
  58. paddings = margins,
  59. ticks = ticks,
  60. ticks_size = ticks_size,
  61. widget = wibox.widget.progressbar
  62. }
  63. alsabar.tooltip = awful.tooltip({ objects = { alsabar.bar } })
  64. function alsabar.update(callback)
  65. helpers.async(format_cmd, function(mixer)
  66. local vol, playback = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
  67. if not vol or not playback then return end
  68. if vol ~= alsabar._current_level or playback ~= alsabar._playback then
  69. alsabar._current_level = tonumber(vol)
  70. alsabar.bar:set_value(alsabar._current_level / 100)
  71. if alsabar._current_level == 0 or playback == "off" then
  72. alsabar._playback = playback
  73. alsabar.tooltip:set_text("[Muted]")
  74. alsabar.bar.color = alsabar.colors.mute
  75. else
  76. alsabar._playback = "on"
  77. alsabar.tooltip:set_text(string.format("%s: %s", alsabar.channel, vol))
  78. alsabar.bar.color = alsabar.colors.unmute
  79. end
  80. volume_now = {
  81. level = alsabar._current_level,
  82. status = alsabar._playback
  83. }
  84. settings()
  85. if type(callback) == "function" then callback() end
  86. end
  87. end)
  88. end
  89. function alsabar.notify()
  90. alsabar.update(function()
  91. local preset = alsabar.notification_preset
  92. preset.title = string.format("%s - %s%%", alsabar.channel, alsabar._current_level)
  93. if alsabar._playback == "off" then
  94. preset.title = preset.title .. " Muted"
  95. end
  96. -- tot is the maximum number of ticks to display in the notification
  97. local tot = alsabar.notification_preset.max_ticks
  98. if not tot then
  99. local wib = awful.screen.focused().mywibox
  100. -- if we can grab mywibox, tot is defined as its height if
  101. -- horizontal, or width otherwise
  102. if wib then
  103. if wib.position == "left" or wib.position == "right" then
  104. tot = wib.width
  105. else
  106. tot = wib.height
  107. end
  108. -- fallback: default horizontal wibox height
  109. else
  110. tot = 20
  111. end
  112. end
  113. local int = math.modf((alsabar._current_level / 100) * tot)
  114. preset.text = string.format(
  115. "%s%s%s%s",
  116. tick_pre,
  117. string.rep(tick, int),
  118. string.rep(tick_none, tot - int),
  119. tick_post
  120. )
  121. if alsabar.followtag then preset.screen = awful.screen.focused() end
  122. if not alsabar.notification then
  123. alsabar.notification = naughty.notify {
  124. preset = preset,
  125. destroy = function() alsabar.notification = nil end
  126. }
  127. else
  128. naughty.replace_text(alsabar.notification, preset.title, preset.text)
  129. end
  130. end)
  131. end
  132. helpers.newtimer(string.format("alsabar-%s-%s", alsabar.cmd, alsabar.channel), timeout, alsabar.update)
  133. return alsabar
  134. end
  135. return factory