Ghost1227 volume widget for oss

From awesome
Jump to: navigation, search

Contents

Introduction

This volume widget for Awesome 3 is based heavily on Farhavens volume widget. In fact, the only real difference is that mine is built for people using OSS instead of ALSA.

The widget works like this:

  • You'll have the volume displayed in your status bar, shown as the volume level followed by a % sign if the channel is unmuted or the word MUTE if it's muted.
  • Pressing the left mouse-button on the widget will toggle muting
  • Scrolling up or down will change the volume
  • (Optional) Your media keys (if you have any) will control the volume as long as you are inside your X session and it is not locked

You'll need the following for this widget to work:

  • ossmix
  • awesome 3 (a GIT version from after the stable Release)
  • ossvol (optional)

Note on point 3: Highly recommended - since ossmix doesn't support mute, you'll need this to make muting possible... besides, I wrote it!

Adding the widget

Add this widget definition to your rc.lua:

 volwidget = widget({ type = 'textbox', name = 'volwidget' })
 volwidget:buttons({
       button({ }, 4, function () volume("up", volwidget) end),
       button({ }, 5, function () volume("down", volwidget) end),
       button({ }, 1, function () volume("mute", volwidget) end)
 })
 volume("update", volwidget)

don't forget to add the widget volwidget to your widget box like this:

 mybottomwibox[s].widgets = {
       myiconbox,
       mpdwidget,
       volwidget,
       netwidget,
       mytextbox
 }

Bringing the widget to life

Now it's time to bring the widget to life, using the following code. Paste it right at the beginning of your rc.lua, below those lines starting with require. Change the value of cardid to the ID of your card and the value of channel to the channel you want to manage.

 channel = "vol"
 function volume (mode, widget)
	if mode == "update" then
             local fd = io.popen("ossmix " .. channel)
             local volume = fd:read("*all"):match("(%d+)")
             fd:close()
               
               volume = tonumber(volume)
               
		if volume == 0 then
			volume = 'VOL: MUTE'
		else
			volume = 'VOL: ' .. volume .. '%'
		end
		widget.text = volume
	elseif mode == "up" then
		awful.util.spawn("ossmix " .. channel .. " +5")
		--If you are using my ossvol script replace the previous line with the following one
		--awful.util.spawn("ossvol -i 5")
		volume("update", widget)
	elseif mode == "down" then
		awful.util.spawn("ossmix " .. channel .. " -- -5")
		--If you are using my ossvol script replace the previous line with the following one
		--awful.util.spawn("ossvol -d 5")
		volume("update", widget)
	else
		--The mute option is useless without ossvol, ossmix does not navitely support muting
		awful.util.spawn("ossvol -t")
		volume("update", widget)
	end
 end

Finally, we will be adding a timer hook to refresh the volume display every 10 seconds to reflect changes made by, for example, alsamixer:

 awful.hooks.timer.register(10, function () volume("update", volwidget) end)

This line should be pasted at the end of your rc.lua

Optional stuff

Setting keybindings

If you have multimedia keys on your keyboard which currently do nothing but look nice, you may want to add these lines to your rc.lua:

 keybinding({ }, "XF86AudioRaiseVolume", function () volume("up", volwidget) end):add()
 keybinding({ }, "XF86AudioLowerVolume", function () volume("down", volwidget) end):add()
 keybinding({ }, "XF86AudioMute", function () volume("mute", volwidget) end):add()
Personal tools