]> git.madduck.net Git - etc/awesome.git/blob - widgets/alsabar.lua.orig

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:

Delete asyncshell_BACKUP_1080.lua
[etc/awesome.git] / widgets / alsabar.lua.orig
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 <<<<<<< HEAD
12 =======
13 local read_pipe    = require("lain.helpers").read_pipe
14 >>>>>>> upstream/master
15
16 local awful        = require("awful")
17 local beautiful    = require("beautiful")
18 local naughty      = require("naughty")
19
20 <<<<<<< HEAD
21 local io           = { popen  = io.popen }
22 local math         = { modf   = math.modf }
23 =======
24 local math         = { modf   = math.modf }
25 local mouse        = mouse
26 >>>>>>> upstream/master
27 local string       = { format = string.format,
28                        match  = string.match,
29                        rep    = string.rep }
30 local tonumber     = tonumber
31
32 local setmetatable = setmetatable
33
34 -- ALSA volume bar
35 -- lain.widgets.alsabar
36 local alsabar = {
37 <<<<<<< HEAD
38     card    = "0",
39 =======
40 >>>>>>> upstream/master
41     channel = "Master",
42     step    = "2%",
43
44     colors = {
45         background = beautiful.bg_normal,
46         mute       = "#EB8F8F",
47         unmute     = "#A4CE8A"
48     },
49
50     terminal = terminal or "xterm",
51     mixer    = terminal .. " -e alsamixer",
52
53     notifications = {
54         font      = beautiful.font:sub(beautiful.font:find(""), beautiful.font:find(" ")),
55         font_size = "11",
56         color     = beautiful.fg_normal,
57         bar_size  = 18,
58         screen    = 1
59     },
60
61     _current_level = 0,
62     _muted         = false
63 }
64
65 function alsabar.notify()
66     alsabar.update()
67
68     local preset = {
69         title   = "",
70         text    = "",
71         timeout = 5,
72         screen  = alsabar.notifications.screen,
73         font    = alsabar.notifications.font .. " " ..
74                   alsabar.notifications.font_size,
75         fg      = alsabar.notifications.color
76     }
77
78     if alsabar._muted
79     then
80         preset.title = alsabar.channel .. " - Muted"
81     else
82         preset.title = alsabar.channel .. " - " .. alsabar._current_level .. "%"
83     end
84
85     int = math.modf((alsabar._current_level / 100) * alsabar.notifications.bar_size)
86     preset.text = "["
87                 .. string.rep("|", int)
88                 .. string.rep(" ", alsabar.notifications.bar_size - int)
89                 .. "]"
90
91 <<<<<<< HEAD
92 =======
93     if alsabar.followmouse then
94         preset.screen = mouse.screen
95     end
96
97 >>>>>>> upstream/master
98     if alsabar._notify ~= nil then
99         alsabar._notify = naughty.notify ({
100             replaces_id = alsabar._notify.id,
101             preset      = preset,
102         })
103     else
104         alsabar._notify = naughty.notify ({
105             preset = preset,
106         })
107     end
108 end
109
110 local function worker(args)
111     local args       = args or {}
112     local timeout    = args.timeout or 5
113     local settings   = args.settings or function() end
114     local width      = args.width or 63
115     local height     = args.heigth or 1
116     local ticks      = args.ticks or false
117     local ticks_size = args.ticks_size or 7
118     local vertical   = args.vertical or false
119
120     alsabar.cmd           = args.cmd or "amixer"
121     alsabar.channel       = args.channel or alsabar.channel
122     alsabar.step          = args.step or alsabar.step
123     alsabar.colors        = args.colors or alsabar.colors
124     alsabar.notifications = args.notifications or alsabar.notifications
125 <<<<<<< HEAD
126 =======
127     alsabar.followmouse   = args.followmouse or false
128 >>>>>>> upstream/master
129
130     alsabar.bar = awful.widget.progressbar()
131
132     alsabar.bar:set_background_color(alsabar.colors.background)
133     alsabar.bar:set_color(alsabar.colors.unmute)
134     alsabar.tooltip = awful.tooltip({ objects = { alsabar.bar } })
135     alsabar.bar:set_width(width)
136     alsabar.bar:set_height(height)
137     alsabar.bar:set_ticks(ticks)
138     alsabar.bar:set_ticks_size(ticks_size)
139     alsabar.bar:set_vertical(vertical)
140
141     function alsabar.update()
142         -- Get mixer control contents
143 <<<<<<< HEAD
144         local f = assert(io.popen(string.format("%s get %s", alsabar.cmd, alsabar.channel)))
145         local mixer = f:read("*a")
146         f:close()
147 =======
148         local mixer = read_pipe(string.format("%s get %s", alsabar.cmd, alsabar.channel))
149 >>>>>>> upstream/master
150
151         -- Capture mixer control state:          [5%] ... ... [on]
152         local volu, mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
153
154         if volu == nil then
155             volu = 0
156             mute = "off"
157         end
158
159         alsabar._current_level = tonumber(volu)
160         alsabar.bar:set_value(alsabar._current_level / 100)
161         if not mute and tonumber(volu) == 0 or mute == "off"
162         then
163             alsabar._muted = true
164             alsabar.tooltip:set_text (" [Muted] ")
165             alsabar.bar:set_color(alsabar.colors.mute)
166         else
167             alsabar._muted = false
168             alsabar.tooltip:set_text(string.format(" %s:%s ", alsabar.channel, volu))
169             alsabar.bar:set_color(alsabar.colors.unmute)
170         end
171
172         volume_now = {}
173         volume_now.level = tonumber(volu)
174         volume_now.status = mute
175         settings()
176     end
177
178     alsabar.bar:buttons (awful.util.table.join (
179           awful.button ({}, 1, function()
180             awful.util.spawn(alsabar.mixer)
181           end),
182           awful.button ({}, 3, function()
183 <<<<<<< HEAD
184             awful.util.spawn(string.format("amixer -c %s set %s toggle", alsabar.card, alsabar.channel))
185             alsabar.update()
186           end),
187           awful.button ({}, 4, function()
188             awful.util.spawn(string.format("amixer -c %s set %s %s+", alsabar.card, alsabar.channel, alsabar.step))
189             alsabar.update()
190           end),
191           awful.button ({}, 5, function()
192             awful.util.spawn(string.format("amixer -c %s set %s %s-", alsabar.card, alsabar.channel, alsabar.step))
193 =======
194             awful.util.spawn(string.format("%s set %s toggle", alsabar.cmd, alsabar.channel))
195             alsabar.update()
196           end),
197           awful.button ({}, 4, function()
198             awful.util.spawn(string.format("%s set %s %s+", alsabar.cmd, alsabar.channel, alsabar.step))
199             alsabar.update()
200           end),
201           awful.button ({}, 5, function()
202             awful.util.spawn(string.format("%s set %s %s-", alsabar.cmd, alsabar.channel, alsabar.step))
203 >>>>>>> upstream/master
204             alsabar.update()
205           end)
206     ))
207
208     timer_id = string.format("alsabar-%s-%s", alsabar.cmd, alsabar.channel)
209
210     newtimer(timer_id, timeout, alsabar.update)
211
212     return alsabar
213 end
214
215 return setmetatable(alsabar, { __call = function(_, ...) return worker(...) end })