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

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