]> git.madduck.net Git - etc/awesome.git/blob - widget/pulsebar.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

quake: iterate on all screens; #339
[etc/awesome.git] / widget / pulsebar.lua
1 --[[
2
3      Licensed under GNU General Public License v2
4       * (c) 2013, Luke Bonham
5       * (c) 2013, Rman
6
7 --]]
8
9 local helpers        = require("lain.helpers")
10 local awful          = require("awful")
11 local naughty        = require("naughty")
12 local wibox          = require("wibox")
13 local math           = { modf   = math.modf }
14 local string         = { format = string.format,
15                          match  = string.match,
16                          gmatch = string.gmatch,
17                          rep    = string.rep }
18 local type, tonumber = type, tonumber
19
20 -- Pulseaudio volume bar
21 -- lain.widget.pulsebar
22
23 local function factory(args)
24     local pulsebar = {
25         colors = {
26             background = "#000000",
27             mute       = "#EB8F8F",
28             unmute     = "#A4CE8A"
29         },
30
31         _current_level = 0,
32         _mute          = "no",
33     }
34
35     local args       = args or {}
36     local timeout    = args.timeout or 5
37     local settings   = args.settings or function() end
38     local width      = args.width or 63
39     local height     = args.heigth or 1
40     local ticks      = args.ticks or false
41     local ticks_size = args.ticks_size or 7
42     local scallback  = args.scallback
43
44     pulsebar.cmd                 = args.cmd or "pacmd list-sinks | sed -n -e '0,/*/d' -e '/base volume/d' -e '/volume:/p' -e '/muted:/p' -e '/device\\.string/p'"
45     pulsebar.sink                = args.sink or 0
46     pulsebar.colors              = args.colors or pulsebar.colors
47     pulsebar.followtag           = args.followtag or false
48     pulsebar.notification_preset = args.notification_preset
49     pulsebar.device              = "N/A"
50
51     if not pulsebar.notification_preset then
52         pulsebar.notification_preset      = {}
53         pulsebar.notification_preset.font = "Monospace 10"
54     end
55
56     pulsebar.bar = wibox.widget {
57         forced_height    = height,
58         forced_width     = width,
59         color            = pulsebar.colors.unmute,
60         background_color = pulsebar.colors.background,
61         margins          = 1,
62         paddings         = 1,
63         ticks            = ticks,
64         ticks_size       = ticks_size,
65         widget           = wibox.widget.progressbar,
66     }
67
68     pulsebar.tooltip = awful.tooltip({ objects = { pulsebar.bar } })
69
70     function pulsebar.update(callback)
71         if scallback then pulsebar.cmd = scallback() end
72
73         helpers.async({ awful.util.shell, "-c", pulsebar.cmd }, function(s)
74             volume_now = {
75                 index = string.match(s, "index: (%S+)") or "N/A",
76                 sink  = string.match(s, "device.string = \"(%S+)\"") or "N/A",
77                 muted = string.match(s, "muted: (%S+)") or "N/A"
78             }
79
80             pulsebar.device = volume_now.index
81
82             local ch = 1
83             volume_now.channel = {}
84             for v in string.gmatch(s, ":.-(%d+)%%") do
85               volume_now.channel[ch] = v
86               ch = ch + 1
87             end
88
89             volume_now.left  = volume_now.channel[1] or "N/A"
90             volume_now.right = volume_now.channel[2] or "N/A"
91
92             local volu = volume_now.left
93             local mute = volume_now.muted
94
95             if volu:match("N/A") or mute:match("N/A") then return end
96
97             if volu ~= pulsebar._current_level or mute ~= pulsebar._mute then
98                 pulsebar._current_level = tonumber(volu)
99                 pulsebar.bar:set_value(pulsebar._current_level / 100)
100                 if pulsebar._current_level == 0 or mute == "yes" then
101                     pulsebar._mute = mute
102                     pulsebar.tooltip:set_text ("[Muted]")
103                     pulsebar.bar.color = pulsebar.colors.mute
104                 else
105                     pulsebar._mute = "no"
106                     pulsebar.tooltip:set_text(string.format("%s: %s", pulsebar.sink, volu))
107                     pulsebar.bar.color = pulsebar.colors.unmute
108                 end
109
110                 settings()
111
112                 if type(callback) == "function" then callback() end
113             end
114         end)
115     end
116
117     function pulsebar.notify()
118         pulsebar.update(function()
119             local preset = pulsebar.notification_preset
120
121             preset.title = string.format("Sink %s - %s%%", pulsebar.sink, pulsebar._current_level)
122
123             if pulsebar._mute == "yes" then
124                 preset.title = preset.title .. " Muted"
125             end
126
127             int = math.modf((pulsebar._current_level / 100) * awful.screen.focused().mywibox.height)
128             preset.text = string.format("[%s%s]", string.rep("|", int),
129                           string.rep(" ", awful.screen.focused().mywibox.height - int))
130
131             if pulsebar.followtag then preset.screen = awful.screen.focused() end
132
133             if not pulsebar.notification then
134                 pulsebar.notification = naughty.notify {
135                     preset  = preset,
136                     destroy = function() pulsebar.notification = nil end
137                 }
138             else
139                 naughty.replace_text(pulsebar.notification, preset.title, preset.text)
140             end
141         end)
142     end
143
144     helpers.newtimer(string.format("pulsebar-%s", pulsebar.sink), timeout, pulsebar.update)
145
146     return pulsebar
147 end
148
149 return factory