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

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