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

f504cc0213d3d4e755059b383467b61510c50b56
[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 mouse        = mouse
19 local string       = { format = string.format,
20                        match  = string.match,
21                        rep    = string.rep }
22 local tonumber     = tonumber
23
24 local setmetatable = setmetatable
25
26 -- ALSA volume bar
27 -- lain.widgets.alsabar
28 local alsabar = {
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 = 5,
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     int = math.modf((alsabar._current_level / 100) * alsabar.notifications.bar_size)
74     preset.text = "["
75                 .. string.rep("|", int)
76                 .. string.rep(" ", alsabar.notifications.bar_size - int)
77                 .. "]"
78
79     if alsabar.followmouse then
80         preset.screen = mouse.screen
81     end
82
83     if alsabar._notify ~= nil then
84         alsabar._notify = naughty.notify ({
85             replaces_id = alsabar._notify.id,
86             preset      = preset,
87         })
88     else
89         alsabar._notify = naughty.notify ({
90             preset = preset,
91         })
92     end
93 end
94
95 local function worker(args)
96     local args       = args or {}
97     local timeout    = args.timeout or 5
98     local settings   = args.settings or function() end
99     local width      = args.width or 63
100     local height     = args.heigth or 1
101     local ticks      = args.ticks or false
102     local ticks_size = args.ticks_size or 7
103     local vertical   = args.vertical or false
104
105     alsabar.cmd           = args.cmd or "amixer"
106     alsabar.channel       = args.channel or alsabar.channel
107     alsabar.step          = args.step or alsabar.step
108     alsabar.colors        = args.colors or alsabar.colors
109     alsabar.notifications = args.notifications or alsabar.notifications
110     alsabar.followmouse   = args.followmouse or false
111
112     alsabar.bar = awful.widget.progressbar()
113
114     alsabar.bar:set_background_color(alsabar.colors.background)
115     alsabar.bar:set_color(alsabar.colors.unmute)
116     alsabar.tooltip = awful.tooltip({ objects = { alsabar.bar } })
117     alsabar.bar:set_width(width)
118     alsabar.bar:set_height(height)
119     alsabar.bar:set_ticks(ticks)
120     alsabar.bar:set_ticks_size(ticks_size)
121     alsabar.bar:set_vertical(vertical)
122
123     function alsabar.update()
124         -- Get mixer control contents
125         local f = assert(io.popen(string.format("%s get %s", alsabar.cmd, alsabar.channel)))
126         local mixer = f:read("*all")
127         f:close()
128
129         -- Capture mixer control state:          [5%] ... ... [on]
130         local volu, mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
131
132         if volu == nil then
133             volu = 0
134             mute = "off"
135         end
136
137         alsabar._current_level = tonumber(volu)
138         alsabar.bar:set_value(alsabar._current_level / 100)
139         if not mute and tonumber(volu) == 0 or mute == "off"
140         then
141             alsabar._muted = true
142             alsabar.tooltip:set_text (" [Muted] ")
143             alsabar.bar:set_color(alsabar.colors.mute)
144         else
145             alsabar._muted = false
146             alsabar.tooltip:set_text(string.format(" %s:%s ", alsabar.channel, volu))
147             alsabar.bar:set_color(alsabar.colors.unmute)
148         end
149
150         volume_now = {}
151         volume_now.level = tonumber(volu)
152         volume_now.status = mute
153         settings()
154     end
155
156     alsabar.bar:buttons (awful.util.table.join (
157           awful.button ({}, 1, function()
158             awful.util.spawn(alsabar.mixer)
159           end),
160           awful.button ({}, 3, function()
161             awful.util.spawn(string.format("%s set %s toggle", alsabar.cmd, alsabar.channel))
162             alsabar.update()
163           end),
164           awful.button ({}, 4, function()
165             awful.util.spawn(string.format("%s set %s %s+", alsabar.cmd, alsabar.channel, alsabar.step))
166             alsabar.update()
167           end),
168           awful.button ({}, 5, function()
169             awful.util.spawn(string.format("%s set %s %s-", alsabar.cmd, alsabar.channel, alsabar.step))
170             alsabar.update()
171           end)
172     ))
173
174     timer_id = string.format("alsabar-%s-%s", alsabar.cmd, alsabar.channel)
175
176     newtimer(timer_id, timeout, alsabar.update)
177
178     return alsabar
179 end
180
181 return setmetatable(alsabar, { __call = function(_, ...) return worker(...) end })