]> git.madduck.net Git - etc/awesome.git/blob - widgets/alsabar.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:

fde5eec5e5a9a9b67dd6f79142fec98b36c415b0
[etc/awesome.git] / widgets / alsabar.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013, Luke Bonham                     
6       * (c) 2013, Rman                            
7                                                   
8 --]]
9
10 local helpers      = require("lain.helpers")
11 local awful        = require("awful")
12 local naughty      = require("naughty")
13 local wibox        = require("wibox")
14 local math         = { modf   = math.modf }
15 local string       = { format = string.format,
16                        match  = string.match,
17                        rep    = string.rep }
18 local type         = type
19 local tonumber     = tonumber
20 local setmetatable = setmetatable
21
22 -- ALSA volume bar
23 -- lain.widgets.alsabar
24 local alsabar = {
25     colors = {
26         background = "#000000",
27         mute       = "#EB8F8F",
28         unmute     = "#A4CE8A"
29     },
30
31     _current_level = 0,
32     _muted         = false
33 }
34
35 local function worker(args)
36     local args       = args or {}
37     local timeout    = args.timeout or 5
38     local settings   = args.settings or function() end
39     local width      = args.width or 63
40     local height     = args.height or 1
41     local ticks      = args.ticks or false
42     local ticks_size = args.ticks_size or 7
43     local vertical   = args.vertical or false
44
45     alsabar.cmd                 = args.cmd or "amixer"
46     alsabar.channel             = args.channel or "Master"
47     alsabar.togglechannel       = args.togglechannel
48     alsabar.colors              = args.colors or alsabar.colors
49     alsabar.followtag           = args.followtag or false
50     alsabar._notify             = args.notify or "on"
51     alsabar.notification_preset = args.notification_preset
52
53     if not alsabar.notification_preset then
54         alsabar.notification_preset      = naughty.config.defaults
55         alsabar.notification_preset.font = "Monospace 11"
56     end
57
58     if alsabar.togglechannel then
59         alsabar.cmd = { awful.util.shell, "-c", string.format("%s get %s; %s get %s",
60         alsabar.cmd, alsabar.channel, alsabar.cmd, alsabar.togglechannel) }
61     end
62
63     alsabar.bar = wibox.widget {
64         forced_height    = height,
65         forced_width     = width,
66         color            = alsabar.colors.unmute,
67         background_color = alsabar.colors.background,
68         margins          = 1,
69         paddings         = 1,
70         ticks            = ticks,
71         ticks_size       = ticks_size,
72         widget           = wibox.widget.progressbar,
73         layout           = vertical and wibox.container.rotate
74     }
75
76     alsabar.tooltip = awful.tooltip({ objects = { alsabar.bar } })
77
78     function alsabar.update(callback)
79         helpers.async(alsabar.cmd, function(mixer)
80             local volu,mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
81             if (volu and tonumber(volu) ~= alsabar._current_level) or (mute and string.match(mute, "on") ~= alsabar._muted) then
82                 alsabar._current_level = tonumber(volu) or alsabar._current_level
83                 alsabar.bar:set_value(alsabar._current_level / 100)
84                 if (not mute and tonumber(volu) == 0) or mute == "off" then
85                     alsabar._muted = true
86                     alsabar.tooltip:set_text ("[Muted]")
87                     alsabar.bar.color = alsabar.colors.mute
88                 else
89                     alsabar._muted = false
90                     alsabar.tooltip:set_text(string.format("%s: %s", alsabar.channel, volu))
91                     alsabar.bar.color = alsabar.colors.unmute
92                 end
93
94                 volume_now = {}
95                 volume_now.level = tonumber(volu)
96                 volume_now.status = mute
97
98                 settings()
99
100                 if type(callback) == "function" then callback() end
101             end
102         end)
103     end
104
105     function alsabar.notify()
106         alsabar.update(function()
107             local preset = alsabar.notification_preset
108
109             if alsabar._muted then
110                 preset.title = string.format("%s - Muted", alsabar.channel)
111             else
112                 preset.title = string.format("%s - %s%%", alsabar.channel, alsabar._current_level)
113             end
114
115             int = math.modf((alsabar._current_level / 100) * awful.screen.focused().mywibox.height)
116             preset.text = string.format("[%s%s]", string.rep("|", int),
117                           string.rep(" ", awful.screen.focused().mywibox.height - int))
118
119             if alsabar.followtag then preset.screen = awful.screen.focused() end
120
121             alsabar.id = naughty.notify ({
122                 replaces_id = alsabar.id,
123                 preset      = preset
124             }).id
125         end)
126     end
127
128     timer_id = string.format("alsabar-%s-%s", alsabar.cmd, alsabar.channel)
129
130     helpers.newtimer(timer_id, timeout, alsabar.update)
131
132     return alsabar
133 end
134
135 return setmetatable(alsabar, { __call = function(_, ...) return worker(...) end })