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

b2cba9e1675b502c93a07ea26be6369844c0decc
[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 newtimer     = require("lain.helpers").newtimer
11
12 local awful        = require("awful")
13 local beautiful    = require("beautiful")
14 local naughty      = require("naughty")
15
16 local io           = { popen  = io.popen }
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     card    = "0",
29     channel = "Master",
30     step    = "2%",
31
32     colors = {
33         background = beautiful.bg_normal,
34         mute       = "#EB8F8F",
35         unmute     = "#A4CE8A"
36     },
37
38     terminal = terminal or "xterm",
39     mixer    = terminal .. " -e alsamixer",
40
41     notifications = {
42         font      = beautiful.font:sub(beautiful.font:find(""), beautiful.font:find(" ")),
43         font_size = "11",
44         color     = beautiful.fg_normal,
45         bar_size  = 18,
46         screen    = 1
47     },
48
49     _current_level = 0,
50     _muted         = false
51 }
52
53 function alsabar.notify()
54     alsabar.update()
55
56     local preset = {
57         title   = "",
58         text    = "",
59         timeout = 4,
60         screen  = alsabar.notifications.screen,
61         font    = alsabar.notifications.font .. " " ..
62                   alsabar.notifications.font_size,
63         fg      = alsabar.notifications.color
64     }
65
66     if alsabar._muted
67     then
68         preset.title = alsabar.channel .. " - Muted"
69     else
70         preset.title = alsabar.channel .. " - " .. alsabar._current_level .. "%"
71     end
72
73     local int = math.modf((alsabar._current_level / 100) * alsabar.notifications.bar_size)
74     preset.text = string.format("[%s%s]", string.rep("|", int),
75                   string.rep(" ", alsabar.notifications.bar_size - int))
76
77     if alsabar._notify ~= nil then
78         alsabar._notify = naughty.notify ({
79             replaces_id = alsabar._notify.id,
80             preset      = preset,
81         })
82     else
83         alsabar._notify = naughty.notify ({
84             preset = preset,
85         })
86     end
87 end
88
89 local function worker(args)
90     local args = args or {}
91     local timeout = args.timeout or 4
92     local settings = args.settings or function() end
93     local width = args.width or 63
94     local height = args.heigth or 1
95     local ticks = args.ticks or false
96     local ticks_size = args.ticks_size or 7
97     local vertical = args.vertical or false
98
99     alsabar.card = args.card or alsabar.card
100     alsabar.channel = args.channel or alsabar.channel
101     alsabar.step = args.step or alsabar.step
102     alsabar.colors = args.colors or alsabar.colors
103     alsabar.notifications = args.notifications or alsabar.notifications
104
105     alsabar.bar = awful.widget.progressbar()
106
107     alsabar.bar:set_background_color(alsabar.colors.background)
108     alsabar.bar:set_color(alsabar.colors.unmute)
109     alsabar.tooltip = awful.tooltip({ objects = { alsabar.bar } })
110     alsabar.bar:set_width(width)
111     alsabar.bar:set_height(height)
112     alsabar.bar:set_ticks(ticks)
113     alsabar.bar:set_ticks_size(ticks_size)
114     alsabar.bar:set_vertical(vertical)
115
116     function alsabar.update()
117         -- Get mixer control contents
118         local f = assert(io.popen(string.format("amixer -c %s -M get %s", alsabar.card, alsabar.channel)))
119         local mixer = f:read("*a")
120         f:close()
121
122         -- Capture mixer control state:          [5%] ... ... [on]
123         local volu, mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
124
125         if volu == nil then
126             volu = 0
127             mute = "off"
128         end
129
130         alsabar._current_level = tonumber(volu)
131         alsabar.bar:set_value(alsabar._current_level / 100)
132         if not mute and tonumber(volu) == 0 or mute == "off"
133         then
134             alsabar._muted = true
135             alsabar.tooltip:set_text (" [Muted] ")
136             alsabar.bar:set_color(alsabar.colors.mute)
137         else
138             alsabar._muted = false
139             alsabar.tooltip:set_text(string.format(" %s:%s ", alsabar.channel, volu))
140             alsabar.bar:set_color(alsabar.colors.unmute)
141         end
142
143         volume_now = {}
144         volume_now.level = tonumber(volu)
145         volume_now.status = mute
146         settings()
147     end
148
149     newtimer("alsabar", timeout, alsabar.update)
150
151     alsabar.bar:buttons (awful.util.table.join (
152           awful.button ({}, 1, function()
153             awful.util.spawn(alsabar.mixer)
154           end),
155           awful.button ({}, 3, function()
156             awful.util.spawn(string.format("amixer -c %s set %s toggle", alsabar.card, alsabar.channel))
157             alsabar.update()
158           end),
159           awful.button ({}, 4, function()
160             awful.util.spawn(string.format("amixer -c %s set %s %s+", alsabar.card, alsabar.channel, alsabar.step))
161             alsabar.update()
162           end),
163           awful.button ({}, 5, function()
164             awful.util.spawn(string.format("amixer -c %s set %s %s-", alsabar.card, alsabar.channel, alsabar.step))
165             alsabar.update()
166           end)
167     ))
168
169     return alsabar
170 end
171
172 return setmetatable(alsabar, { __call = function(_, ...) return worker(...) end })