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

contrib.kbdlayout: asynchronous
[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
12 local awful        = require("awful")
13 local beautiful    = require("beautiful")
14 local naughty      = require("naughty")
15 local wibox        = require("wibox")
16
17 local math         = { modf   = math.modf }
18 local string       = { format = string.format,
19                        match  = string.match,
20                        rep    = string.rep }
21 local tonumber     = tonumber
22 local type         = type
23
24 local setmetatable = setmetatable
25
26 -- ALSA volume bar
27 -- lain.widgets.alsabar
28 local alsabar = {
29     channel = "Master",
30     step    = "1%",
31
32     colors = {
33         background = beautiful.bg_normal,
34         mute       = "#EB8F8F",
35         unmute     = "#A4CE8A"
36     },
37
38     notifications = {
39         font      = beautiful.font:sub(beautiful.font:find(""), beautiful.font:find(" ")),
40         font_size = "11",
41         color     = beautiful.fg_normal,
42         screen    = 1
43     },
44
45     _current_level = 0,
46     _muted         = false
47 }
48
49 local function worker(args)
50     local args         = args or {}
51     local timeout      = args.timeout or 5
52     local settings     = args.settings or function() end
53     local width        = args.width or 63
54     local height       = args.heigth or 1
55     local ticks        = args.ticks or false
56     local ticks_size   = args.ticks_size or 7
57     local vertical     = args.vertical or false
58
59     alsabar.cmd           = args.cmd or "amixer"
60     alsabar.channel       = args.channel or alsabar.channel
61     alsabar.step          = args.step or alsabar.step
62     alsabar.colors        = args.colors or alsabar.colors
63     alsabar.notifications = args.notifications or alsabar.notifications
64     alsabar.followtag     = args.followtag or false
65
66     alsabar.bar = wibox.widget {
67         forced_height    = height,
68         forced_width     = width,
69         color            = alsabar.colors.unmute,
70         background_color = alsabar.colors.background,
71         margins          = 1,
72         paddings         = 1,
73         ticks            = ticks,
74         ticks_size       = ticks_size,
75         widget           = wibox.widget.progressbar,
76         layout           = vertical and wibox.container.rotate
77     }
78
79     alsabar.tooltip = awful.tooltip({ objects = { alsabar.bar } })
80
81     function alsabar.update(callback)
82         helpers.async(alsabar.cmd, function(mixer)
83             local volu,mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
84             if (volu and tonumber(volu) ~= alsabar._current_level) or (mute and string.match(mute, "on") ~= alsabar._muted)
85             then
86                 alsabar._current_level = tonumber(volu) or alsabar._current_level
87                 alsabar.bar:set_value(alsabar._current_level / 100)
88                 if (not mute and tonumber(volu) == 0) or mute == "off"
89                 then
90                     alsabar._muted = true
91                     alsabar.tooltip:set_text ("[Muted]")
92                     alsabar.bar.color = alsabar.colors.mute
93                 else
94                     alsabar._muted = false
95                     alsabar.tooltip:set_text(string.format("%s: %s", alsabar.channel, volu))
96                     alsabar.bar.color = alsabar.colors.unmute
97                 end
98
99                 volume_now = {}
100                 volume_now.level = tonumber(volu)
101                 volume_now.status = mute
102
103                 settings()
104
105                 if type(callback) == "function" then callback() end
106             end
107         end)
108     end
109
110     function alsabar.notify()
111         alsabar.update(function()
112             local preset = {
113                 title   = "",
114                 text    = "",
115                 timeout = 5,
116                 screen  = alsabar.notifications.screen,
117                 font    = string.format("%s %s", alsabar.notifications.font,
118                           alsabar.notifications.font_size),
119                 fg      = alsabar.notifications.color
120             }
121
122             if alsabar._muted then
123                 preset.title = string.format("%s - Muted", alsabar.channel)
124             else
125                 preset.title = string.format("%s - %s%%", alsabar.channel, alsabar._current_level)
126             end
127
128             int = math.modf((alsabar._current_level / 100) * awful.screen.focused().mywibox.height)
129             preset.text = string.format("[%s%s]", string.rep("|", int),
130                           string.rep(" ", awful.screen.focused().mywibox.height - int))
131
132             if alsabar.followtag then preset.screen = awful.screen.focused() end
133
134             if alsabar._notify then
135                 alsabar._notify = naughty.notify ({
136                     replaces_id = alsabar._notify.id,
137                     preset      = preset,
138                 })
139             else
140                 alsabar._notify = naughty.notify ({ preset = preset })
141             end
142         end)
143     end
144
145     timer_id = string.format("alsabar-%s-%s", alsabar.cmd, alsabar.channel)
146     helpers.newtimer(timer_id, timeout, alsabar.update)
147
148     return alsabar
149 end
150
151 return setmetatable(alsabar, { __call = function(_, ...) return worker(...) end })