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

5fe74c4ca27f9cde70ad41b5d32ac78be8dc21d1
[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    = "2%",
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 = 5,
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 5
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.cmd           = args.cmd or "amixer"
101     alsabar.channel       = args.channel or alsabar.channel
102     alsabar.step          = args.step or alsabar.step
103     alsabar.colors        = args.colors or alsabar.colors
104     alsabar.notifications = args.notifications or alsabar.notifications
105
106     alsabar.bar = awful.widget.progressbar()
107
108     alsabar.bar:set_background_color(alsabar.colors.background)
109     alsabar.bar:set_color(alsabar.colors.unmute)
110     alsabar.tooltip = awful.tooltip({ objects = { alsabar.bar } })
111     alsabar.bar:set_width(width)
112     alsabar.bar:set_height(height)
113     alsabar.bar:set_ticks(ticks)
114     alsabar.bar:set_ticks_size(ticks_size)
115     alsabar.bar:set_vertical(vertical)
116
117     function alsabar.update()
118         -- Get mixer control contents
119         local f = assert(io.popen(string.format("%s get %s", alsabar.cmd, alsabar.channel)))
120         local mixer = f:read("*all")
121         f:close()
122
123         -- Capture mixer control state:          [5%] ... ... [on]
124         local volu, mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
125
126         if volu == nil then
127             volu = 0
128             mute = "off"
129         end
130
131         alsabar._current_level = tonumber(volu)
132         alsabar.bar:set_value(alsabar._current_level / 100)
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     alsabar.bar:buttons (awful.util.table.join (
151           awful.button ({}, 1, function()
152             awful.util.spawn(alsabar.mixer)
153           end),
154           awful.button ({}, 3, function()
155             awful.util.spawn(string.format("%s set %s toggle", alsabar.cmd, alsabar.channel))
156             alsabar.update()
157           end),
158           awful.button ({}, 4, function()
159             awful.util.spawn(string.format("%s set %s %s+", alsabar.cmd, alsabar.channel, alsabar.step))
160             alsabar.update()
161           end),
162           awful.button ({}, 5, function()
163             awful.util.spawn(string.format("%s set %s %s-", alsabar.cmd, alsabar.channel, alsabar.step))
164             alsabar.update()
165           end)
166     ))
167
168     timer_id = string.format("alsabar-%s-%s", alsabar.cmd, alsabar.channel)
169
170     newtimer(timer_id, timeout, alsabar.update)
171
172     return alsabar
173 end
174
175 return setmetatable(alsabar, { __call = function(_, ...) return worker(...) end })