]> 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:

alsabar: revert #275; remove buttons and beautiful deps
[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 tonumber     = tonumber
19 local type         = type
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.colors              = args.colors or alsabar.colors
48     alsabar.followtag           = args.followtag or false
49     alsabar._notify             = args.notify or "on"
50     alsabar.notification_preset = args.notification_preset
51
52     if not alsabar.notification_preset then
53         alsabar.notification_preset      = naughty.config.defaults
54         alsabar.notification_preset.font = "Monospace 11"
55     end
56
57     alsabar.bar = wibox.widget {
58         forced_height    = height,
59         forced_width     = width,
60         color            = alsabar.colors.unmute,
61         background_color = alsabar.colors.background,
62         margins          = 1,
63         paddings         = 1,
64         ticks            = ticks,
65         ticks_size       = ticks_size,
66         widget           = wibox.widget.progressbar,
67         layout           = vertical and wibox.container.rotate
68     }
69
70     alsabar.tooltip = awful.tooltip({ objects = { alsabar.bar } })
71
72     function alsabar.update(callback)
73         helpers.async(alsabar.cmd, function(mixer)
74             local volu,mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
75             if (volu and tonumber(volu) ~= alsabar._current_level) or (mute and string.match(mute, "on") ~= alsabar._muted)
76             then
77                 alsabar._current_level = tonumber(volu) or alsabar._current_level
78                 alsabar.bar:set_value(alsabar._current_level / 100)
79                 if (not mute and tonumber(volu) == 0) or mute == "off" then
80                     alsabar._muted = true
81                     alsabar.tooltip:set_text ("[Muted]")
82                     alsabar.bar.color = alsabar.colors.mute
83                 else
84                     alsabar._muted = false
85                     alsabar.tooltip:set_text(string.format("%s: %s", alsabar.channel, volu))
86                     alsabar.bar.color = alsabar.colors.unmute
87                 end
88
89                 volume_now = {}
90                 volume_now.level = tonumber(volu)
91                 volume_now.status = mute
92
93                 settings()
94
95                 if type(callback) == "function" then callback() end
96             end
97         end)
98     end
99
100     function alsabar.notify()
101         alsabar.update(function()
102             local preset = alsabar.notification_preset
103
104             if alsabar._muted then
105                 preset.title = string.format("%s - Muted", alsabar.channel)
106             else
107                 preset.title = string.format("%s - %s%%", alsabar.channel, alsabar._current_level)
108             end
109
110             int = math.modf((alsabar._current_level / 100) * awful.screen.focused().mywibox.height)
111             preset.text = string.format("[%s%s]", string.rep("|", int),
112                           string.rep(" ", awful.screen.focused().mywibox.height - int))
113
114             if alsabar.followtag then preset.screen = awful.screen.focused() end
115
116             if alsabar._notify == "on" then
117                 alsabar.id = naughty.notify ({
118                     replaces_id = alsabar.id,
119                     preset      = preset
120                 }).id
121             end
122         end)
123     end
124
125     timer_id = string.format("alsabar-%s-%s", alsabar.cmd, alsabar.channel)
126
127     helpers.newtimer(timer_id, timeout, alsabar.update)
128
129     return alsabar
130 end
131
132 return setmetatable(alsabar, { __call = function(_, ...) return worker(...) end })