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

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