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

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