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

430063be3b3caee5e676093e19e677dcee292d4e
[etc/awesome.git] / widgets / alsabar.lua
1 --[[
2      Licensed under GNU General Public License v2
3       * (c) 2013, Luke Bonham
4       * (c) 2013, Rman
5 --]]
6
7 local helpers      = require("lain.helpers")
8 local awful        = require("awful")
9 local naughty      = require("naughty")
10 local wibox        = require("wibox")
11 local math         = { modf   = math.modf }
12 local string       = { format = string.format,
13                        match  = string.match,
14                        rep    = string.rep }
15 local tonumber     = tonumber
16 local type         = type
17 local setmetatable = setmetatable
18 local terminal     = "urxvtc" or "xterm"
19
20 -- ALSA volume bar
21 -- lain.widgets.alsabar
22 local alsabar = {
23 <<<<<<< HEAD
24 =======
25     channel = "Master",
26     step    = "1%",
27     mixer   = "amixer",
28
29 >>>>>>> 09c0a3f27b6c0b61a55e7875b9a967e98cd3daf8
30     colors = {
31         background = "#000000",
32         mute       = "#EB8F8F",
33         unmute     = "#A4CE8A"
34     },
35
36     _current_level = 0,
37     _muted         = false
38 }
39
40 local function worker(args)
41     local args         = args or {}
42     local timeout      = args.timeout or 5
43     local settings     = args.settings or function() end
44     local width        = args.width or 63
45     local height       = args.height or 1
46     local ticks        = args.ticks or false
47     local ticks_size   = args.ticks_size or 7
48     local vertical     = args.vertical or false
49
50 <<<<<<< HEAD
51     alsabar.cmd                 = args.cmd or "amixer"
52     alsabar.channel             = args.channel or "Master"
53     alsabar.colors              = args.colors or alsabar.colors
54     alsabar.followtag           = args.followtag or false
55     alsabar._notify             = args.notify or "on"
56     alsabar.notification_preset = args.notification_preset
57
58     if not alsabar.notification_preset then
59         alsabar.notification_preset      = naughty.config.defaults
60         alsabar.notification_preset.font = "Monospace 11"
61 =======
62     alsabar.mixer         = args.mixer or alsabar.mixer
63     alsabar.channel       = args.channel or alsabar.channel
64     alsabar.togglechannel = args.togglechannel or alsabar.togglechannel
65     alsabar.cmd           = args.cmd or {"bash", "-c", string.format("%s get %s", alsabar.mixer, alsabar.channel)}
66     alsabar.step          = args.step or alsabar.step
67     alsabar.colors        = args.colors or alsabar.colors
68     alsabar.notifications = args.notifications or alsabar.notifications
69     alsabar.followtag     = args.followtag or false
70     if alsabar.togglechannel then
71             alsabar.cmd   = args.cmd or { "bash", "-c", string.format("%s get %s; %s get %s", alsabar.mixer, alsabar.channel, alsabar.mixer, alsabar.togglechannel)}
72 >>>>>>> 09c0a3f27b6c0b61a55e7875b9a967e98cd3daf8
73     end
74
75     alsabar.bar = wibox.widget {
76         forced_height    = height,
77         forced_width     = width,
78         color            = alsabar.colors.unmute,
79         background_color = alsabar.colors.background,
80         margins          = 1,
81         paddings         = 1,
82         ticks            = ticks,
83         ticks_size       = ticks_size,
84         widget           = wibox.widget.progressbar,
85         layout           = vertical and wibox.container.rotate
86     }
87
88     alsabar.bar:buttons (awful.util.table.join(
89         awful.button({}, 1, function()
90                 awful.spawn(string.format('%s -e alsamixer', terminal))
91         end),
92         awful.button({}, 2, function()
93                 awful.spawn(string.format("%s set %s 100%%", alsabar.mixer, alsabar.channel))
94                 alsabar.update()
95         end),
96         awful.button({}, 3, function()
97                 awful.spawn(string.format("%s set %s toggle", alsabar.mixer, alsabar.togglechannel or alsabar.channel))
98                 alsabar.update()
99         end),
100         awful.button({}, 4, function()
101                 awful.spawn(string.format("%s set %s %s+", alsabar.mixer, alsabar.channel, alsabar.step))
102                 alsabar.update()
103         end),
104         awful.button({}, 5, function()
105                 awful.spawn(string.format("%s set %s %s-", alsabar.mixer, alsabar.channel, alsabar.step))
106                 alsabar.update()
107         end)))
108
109     alsabar.tooltip = awful.tooltip({ objects = { alsabar.bar } })
110
111     function alsabar.update(callback)
112         helpers.async(alsabar.cmd, function(mixer)
113             local volu,mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
114             if (volu and tonumber(volu) ~= alsabar._current_level) or (mute and string.match(mute, "on") ~= alsabar._muted)
115             then
116                 alsabar._current_level = tonumber(volu) or alsabar._current_level
117                 alsabar.bar:set_value(alsabar._current_level / 100)
118                 if (not mute and tonumber(volu) == 0) or mute == "off" then
119                     alsabar._muted = true
120                     alsabar.tooltip:set_text ("[Muted]")
121                     alsabar.bar.color = alsabar.colors.mute
122                 else
123                     alsabar._muted = false
124                     alsabar.tooltip:set_text(string.format("%s: %s", alsabar.channel, volu))
125                     alsabar.bar.color = alsabar.colors.unmute
126                 end
127
128                 volume_now = {}
129                 volume_now.level = tonumber(volu)
130                 volume_now.status = mute
131
132                 settings()
133
134                 if type(callback) == "function" then callback() end
135             end
136         end)
137     end
138
139     function alsabar.notify()
140         alsabar.update(function()
141             local preset = alsabar.notification_preset
142
143             if alsabar._muted then
144                 preset.title = string.format("%s - Muted", alsabar.channel)
145             else
146                 preset.title = string.format("%s - %s%%", alsabar.channel, alsabar._current_level)
147             end
148
149             int = math.modf((alsabar._current_level / 100) * awful.screen.focused().mywibox.height)
150             preset.text = string.format("[%s%s]", string.rep("|", int),
151                           string.rep(" ", awful.screen.focused().mywibox.height - int))
152
153             if alsabar.followtag then preset.screen = awful.screen.focused() end
154
155             if alsabar._notify == "on" then
156                 alsabar.id = naughty.notify ({
157                     replaces_id = alsabar.id,
158                     preset      = preset
159                 }).id
160             end
161         end)
162     end
163
164     timer_id = string.format("alsabar-%s-%s", alsabar.cmd, alsabar.channel)
165
166     helpers.newtimer(timer_id, timeout, alsabar.update)
167
168     return alsabar
169 end
170
171 return setmetatable(alsabar, { __call = function(_, ...) return worker(...) end })