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

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