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

Merge pull request #96 from worron/master
[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     channel = "Master",
29     step    = "5%",
30
31     colors = {
32         background = beautiful.bg_normal,
33         mute       = "#EB8F8F",
34         unmute     = "#A4CE8A"
35     },
36
37     terminal = terminal or "xterm",
38     mixer    = terminal .. " -e alsamixer",
39
40     notifications = {
41         font      = beautiful.font:sub(beautiful.font:find(""), beautiful.font:find(" ")),
42         font_size = "11",
43         color     = beautiful.fg_normal,
44         bar_size  = 18,
45         screen    = 1
46     },
47
48     _current_level = 0,
49     _muted         = false
50 }
51
52 function alsabar.notify()
53     alsabar.update()
54
55     local preset = {
56         title   = "",
57         text    = "",
58         timeout = 4,
59         screen  = alsabar.notifications.screen,
60         font    = alsabar.notifications.font .. " " ..
61                   alsabar.notifications.font_size,
62         fg      = alsabar.notifications.color
63     }
64
65     if alsabar._muted
66     then
67         preset.title = alsabar.channel .. " - Muted"
68     else
69         preset.title = alsabar.channel .. " - " .. alsabar._current_level .. "%"
70     end
71
72     int = math.modf((alsabar._current_level / 100) * alsabar.notifications.bar_size)
73     preset.text = "["
74                 .. string.rep("|", int)
75                 .. string.rep(" ", alsabar.notifications.bar_size - int)
76                 .. "]"
77
78     if alsabar._notify ~= nil then
79         alsabar._notify = naughty.notify ({
80             replaces_id = alsabar._notify.id,
81             preset      = preset,
82         })
83     else
84         alsabar._notify = naughty.notify ({
85             preset = preset,
86         })
87     end
88 end
89
90 local function worker(args)
91     local args = args or {}
92     local timeout = args.timeout or 4
93     local settings = args.settings or function() end
94     local width = args.width or 63
95     local height = args.heigth or 1
96     local ticks = args.ticks or false
97     local ticks_size = args.ticks_size or 7
98     local vertical = args.vertical or false
99
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 = io.popen("amixer -M get " .. 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
133         if not mute and tonumber(volu) == 0 or mute == "off"
134         then
135             alsabar._muted = true
136             alsabar.tooltip:set_text (" [Muted] ")
137             alsabar.bar:set_color(alsabar.colors.mute)
138         else
139             alsabar._muted = false
140             alsabar.tooltip:set_text(string.format(" %s:%s ", alsabar.channel, volu))
141             alsabar.bar:set_color(alsabar.colors.unmute)
142         end
143
144         volume_now = {}
145         volume_now.level = tonumber(volu)
146         volume_now.status = mute
147         settings()
148     end
149
150     newtimer("alsabar", timeout, alsabar.update)
151
152     alsabar.bar:buttons (awful.util.table.join (
153           awful.button ({}, 1, function()
154             awful.util.spawn(alsabar.mixer)
155           end),
156           awful.button ({}, 3, function()
157             awful.util.spawn(string.format("amixer set %s toggle", alsabar.channel))
158             alsabar.update()
159           end),
160           awful.button ({}, 4, function()
161             awful.util.spawn(string.format("amixer set %s %s+", alsabar.channel, alsabar.step))
162             alsabar.update()
163           end),
164           awful.button ({}, 5, function()
165             awful.util.spawn(string.format("amixer set %s %s-", alsabar.channel, alsabar.step))
166             alsabar.update()
167           end)
168     ))
169
170     return alsabar
171 end
172
173 return setmetatable(alsabar, { __call = function(_, ...) return worker(...) end })