You are not logged in.

#1 2009-06-26 12:55:10

Allamgir
Member
Registered: 2009-06-11
Posts: 168

[SOLVED] Can xmobar display volume and network strength %?

I have been working on my xmobar setup recently, especially trying to get a good status bar working. I was using xmobar before, but didn't think it had enough features, so I tried to switch to dzen. At this point, I do not really like dzen and want to go back to xmobar. After some thinking, I realized I don't even need a system tray; all I need to get xmobar to do is display the volume percentage and mute state and the network strength percentage. I read somewhere that xmobar can display anything as long as you can write a script to show it, but I don't know how to write scripts.

I'm using wicd for network management and I use amixer to change the volume with my Fn keys (there must be some way amixer tells the current volume percentage).

How can I accomplish all of this?

Last edited by Allamgir (2009-06-29 23:22:49)


дɭɭɑӎɠїɾ

Offline

#2 2009-06-26 14:24:01

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] Can xmobar display volume and network strength %?

i had something in my xmobar to view network stuff.  the concept is simple, just write a shell script to grep/awk the right commands and echo back the word/number/whatever you want to appear in xmobar.  then call the script in your .xmobarrc

the relevant lines from xmobarrc:

, Run Com "/home/patrick/.bin/wireless.sh" [] "wifi" 30

, template = ... %wifi% ...

and wireless.sh:

#!/bin/sh

iwconfig wlan0 2>&1 | grep -q no\ wireless\ extensions\. && {
  echo wired
  exit 0
}

essid=`iwconfig wlan0 | awk -F '"' '/ESSID/ {print $2}'`
stngth=`iwconfig wlan0 | awk -F '=' '/Quality/ {print $2}' | cut -d '/' -f 1`
bars=`expr $stngth / 10`

case $bars in
  0)  bar='[----------]' ;;
  1)  bar='[/---------]' ;;
  2)  bar='[//--------]' ;;
  3)  bar='[///-------]' ;;
  4)  bar='[////------]' ;;
  5)  bar='[/////-----]' ;;
  6)  bar='[//////----]' ;;
  7)  bar='[///////---]' ;;
  8)  bar='[////////--]' ;;
  9)  bar='[/////////-]' ;;
  10) bar='[//////////]' ;;
  *)  bar='[----!!----]' ;;
esac

echo $essid $bar

exit 0

this nice bar feature was stolen from another forum post.  it outputs something like this:

> wireless.sh
atkins1 [///-------]

and i have a similar one for battery strength.  you could easily take out the whole bar idea and just echo a number/percent whatever.  you get the idea.

/edit.  one benefit of this is you could ultimately go with xmobar, dzen, conky, or really anything for you status bar and still pipe these little scripts into it and see the same output.

Last edited by brisbin33 (2009-06-26 14:31:30)

Offline

#3 2009-06-26 14:32:26

Allamgir
Member
Registered: 2009-06-11
Posts: 168

Re: [SOLVED] Can xmobar display volume and network strength %?

Wow, thanks! I had no idea xmobar could do progress bars at all!
Anyway, I'll be sure to experiment with that. Right now I'm out of town, so I can't work on my xmonad. Now, is it possible to have the volume (and mute state, preferably as MM in place of the percentage), but what is the amixer command for displaying the current state for Master?


дɭɭɑӎɠїɾ

Offline

#4 2009-06-26 14:52:56

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] Can xmobar display volume and network strength %?

Allamgir wrote:

Wow, thanks! I had no idea xmobar could do progress bars at all!
Anyway, I'll be sure to experiment with that. Right now I'm out of town, so I can't work on my xmonad. Now, is it possible to have the volume (and mute state, preferably as MM in place of the percentage), but what is the amixer command for displaying the current state for Master?

to be clear, in this case, xmobar is just printing characters; it just so happens that my script outputs the characters [, /, -, and ] to show something that 'looks' like a progress bar. (although i only have brief experience with xmobar, and it may in fact do all sorts of things).

as for your sound state, i use oss so i can't come up with something alsa for you, but can try to point you in the right direction.  you'll most likely just run the amixer command that reports back your sound state (amixer info?), pipe through awk to just print the volume level percent (similar to how i get ESSID or Quality in my script) then you could pipe through `sed 's/\ 0%/MM/g'` for when it's muted. 

i'm sure someone can come along with a ready-made script, or you could spend some time with the manuals (sed and awk's are a bit confusing though hmm) to figure something out.  a good learning experience indeed.

the nice thing about shell scripts, you can just sit at your bash prompt and play with different piped commands until (hopefully) you get the output you're looking for.  then just put that command into a file and run it.

good luck!

Offline

#5 2009-06-26 15:32:23

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED] Can xmobar display volume and network strength %?

This should work, and put the expected value in $bars (0-10/MM)

bars=$(amixer get Front | awk -F'[]%[]' '/%/ {if ($7 == "off") { print "MM" } else { print $2/10 }}' | head -n 1)

Offline

#6 2009-06-26 17:31:42

Allamgir
Member
Registered: 2009-06-11
Posts: 168

Re: [SOLVED] Can xmobar display volume and network strength %?

Thank you all so much. I'll try all of this as soon as I get home, which is probably after this weekend, and I'll post if I have any questions.


дɭɭɑӎɠїɾ

Offline

#7 2009-06-29 02:22:45

Allamgir
Member
Registered: 2009-06-11
Posts: 168

Re: [SOLVED] Can xmobar display volume and network strength %?

Ok now I've got the network info set up, but I would just like to know, from brisbin's script, how do I make it so there are only 5 progress bars instead of 10? I'd like to save as many characters as I can and 5 seems like plenty to indicate wireless strength. I've tried changing "bars=`expr $stngth / 10`" to "bars=`expr $stngth / 5'" and commenting out bars 6-10, but that does not work. I only get the error bar. What do I need to do?


дɭɭɑӎɠїɾ

Offline

#8 2009-06-29 03:00:51

JT
Member
Registered: 2009-01-16
Posts: 21

Re: [SOLVED] Can xmobar display volume and network strength %?

Allamgir wrote:

. . . I've tried changing "bars=`expr $stngth / 10`" to "bars=`expr $stngth / 5'" . . .

It seems like you've typed a single quote (') as opposed to a grave accent (`).
Shift tilde should solve the first problem.

Second problem:
If you only want to use digits 0-5, you have to divide by 20.
Or, you could simply redraw the bars in the script like so:

case $bars in
  0)  bar='[-----]' ;;
  1)  bar='[/----]' ;;
  2)  bar='[/----]' ;;
  3)  bar='[//---]' ;;
  4)  bar='[//---]' ;;
  5)  bar='[///--]' ;;
  6)  bar='[///--]' ;;
  7)  bar='[////-]' ;;
  8)  bar='[////-]' ;;
  9)  bar='[/////]' ;;
  10) bar='[/////]' ;;
  *)  bar='[--!--]' ;;
esac

Offline

#9 2009-06-29 13:41:40

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] Can xmobar display volume and network strength %?

i'd prefer the script a little cleaner, like this:

case $bars in
  0)     bar='[-----]' ;;
  1|2)   bar='[/----]' ;;
  3|4)   bar='[//---]' ;;
  5|6)   bar='[///--]' ;;
  7|8)   bar='[////-]' ;;
  9|10)  bar='[/////]' ;;
  *)     bar='[--!--]' ;;
esac

Last edited by brisbin33 (2009-06-29 13:43:36)

Offline

#10 2009-06-29 20:41:08

Allamgir
Member
Registered: 2009-06-11
Posts: 168

Re: [SOLVED] Can xmobar display volume and network strength %?

Thank you all for your help. I got the network status just how I want it on xmobar smile

EDIT: Ok so I checked out Procyon's post for a script and this is what I have (I didn't want to use the bars, so I commented them out; I just want a percentage):

vol=$(amixer get Master | awk -F'[]%[]' '/%/ {if ($7 == "off") { print "MM" } else { print $2/10 }}' | head -n 1)

#case $bars in
 # 0)  bar='[----------]' ;;
 # 1)  bar='[#---------]' ;;
 # 2)  bar='[##--------]' ;;
 # 3)  bar='[###-------]' ;;
 # 4)  bar='[####------]' ;;
 # 5)  bar='[#####-----]' ;;
 # 6)  bar='[######----]' ;;
 # 7)  bar='[#######---]' ;;
 # 8)  bar='[########--]' ;;
 # 9)  bar='[#########-]' ;;
 # 10) bar='[##########]' ;;
 # *)  bar='[----!!----]' ;;
#esac

echo Vol: $vol

exit 0

But when I try to run the script I get 2.2, when I have the volume at currently 79%, which is what I want to see.

$ sh ~/.bin/volume.sh
Vol: 2.2

How do I edit procyon's script to show the volume percentage? (and display MM when muted?)

Last edited by Allamgir (2009-06-29 20:51:55)


дɭɭɑӎɠїɾ

Offline

#11 2009-06-29 20:51:38

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] Can xmobar display volume and network strength %?

glad to see you're making an effort.

see that part in procyons script that says print $2/10} ... that's how you're getting 2.2 just chang that to print $2} and you'll get 22.  or even $2 %} if you want 22% to print

edit.

edit 2: removed edit 1 big_smile

Last edited by brisbin33 (2009-06-29 21:10:06)

Offline

#12 2009-06-29 21:05:17

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED] Can xmobar display volume and network strength %?

I guess the awk script needed an int() for the bar script.

Are you sure it's at 79%? Have a look at alsamixer

Offline

#13 2009-06-29 21:10:32

Allamgir
Member
Registered: 2009-06-11
Posts: 168

Re: [SOLVED] Can xmobar display volume and network strength %?

oh, wow. It was actually at 22%! This means all I have to do is get it to multiply the number by 10 to get the percentage! I know I've been asking a lot of noobish questions, but how do I do this?

Here is my script. I removed all the bar stuff because a percentage would be sufficient. I do have another problem here, though. The parts I've emboldened show up as red when I open the script in vim, which usually indicates a syntax error. Is there anything wrong here? The script works fine by displaying the percentage (although divided by 10)

vol=$(amixer get Master | awk -F'[]%[]' '/%/ {if ($7 == "off") { print "MM" } else { print $2/10 }}' | head -n 1)

echo Vol: $vol

exit 0

дɭɭɑӎɠїɾ

Offline

#14 2009-06-29 21:22:20

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] Can xmobar display volume and network strength %?

brisbin33 wrote:

see that part in procyons script that says print $2/10} ... that's how you're getting 2.2 just change that to print $2} and you'll get 22.  or even $2 %} if you want 22% to print

big_smile

Offline

#15 2009-06-29 21:30:50

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED] Can xmobar display volume and network strength %?

By the way, -F'[]%[]' <- remove the % there and it will be added to $2.
and "| head -n 1" isn't needed because amixer get Master doesn't return a left and right channel like Front.

Offline

#16 2009-06-29 21:33:03

Allamgir
Member
Registered: 2009-06-11
Posts: 168

Re: [SOLVED] Can xmobar display volume and network strength %?

YES! Finally! I got the volume to display just how I want it! I just have one problem...

Whenever I change the volume (I use Fn keybindings for it), there is a 1-2 second delay between the actual volume state and the display on xmobar. It's not huge, but it gets pretty confusing if I can't see exactly which percentage my volume is on and I'm trying to adjust it. If I go to alsamixer and change the volume with the Fn keys, the changes appear instantly. Is there any way to speed up the response time of the shell script/xmobar?


дɭɭɑӎɠїɾ

Offline

#17 2009-06-29 21:40:14

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] Can xmobar display volume and network strength %?

, Run Com "/home/patrick/.bin/wireless.sh" [] "wifi" 30

that 30 on the end is the interval for the script in 1/10 seconds.  so my wifi updates every 3 seconds.

you can make this as small as you want.  but remember it uses resources to run that shell script every 1/10th of a second even if you don't adjust the volume but once a few minutes.

Last edited by brisbin33 (2009-06-29 21:40:37)

Offline

#18 2009-06-29 21:41:23

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED] Can xmobar display volume and network strength %?

I've never used xmobar, does it not just use a 1 second timer, or you call it with a script that uses sleep 1 in a while true loop, like dzen2?

The shell script runs at milliseconds. Try to run this a few times:

time amixer get Master | awk -F'[][]' '/%/ {if ($7 == "off") { print "Muted" } else { print "Vol:", $2 }}'

Offline

#19 2009-06-29 21:43:42

JT
Member
Registered: 2009-01-16
Posts: 21

Re: [SOLVED] Can xmobar display volume and network strength %?

brisbin33 wrote:
case $bars in
  0)     bar='[-----]' ;;
  1|2)   bar='[/----]' ;;
  3|4)   bar='[//---]' ;;
  5|6)   bar='[///--]' ;;
  7|8)   bar='[////-]' ;;
  9|10)  bar='[/////]' ;;
  *)     bar='[--!--]' ;;
esac

You learn something new everyday. I need to pick up speed with my bash.
Thanks, brisbin.

Last edited by JT (2009-06-29 21:44:03)

Offline

#20 2009-06-29 22:02:24

Allamgir
Member
Registered: 2009-06-11
Posts: 168

Re: [SOLVED] Can xmobar display volume and network strength %?

Procyon, when I use your last script, do I just use that line? No echos or exits or anything? Because when I run the script in the terminal like that, it just spits out this:

$ sh ~/.bin/vol.sh
Vol: 75%

real    0m0.017s
user    0m0.013s
sys    0m0.000s

and then it ends. It's not like if I adjust the volume the script is still running and I can see the change instantaneously. When I put this in my xmobarrc, do I indicate an interval? In general, what exactly is this script doing differently from my old one(see below)?

vol=$(amixer get Master | awk -F'[]%[]' '/%/ {if ($7 == "off") { print "MM" } else { print $2 }}' | head -n 1)

echo Vol: $vol%

exit 0

I tried setting the interval to every half second, but that made my Cpu percentage when I'm not using anything except firefox and a terminal jump from 3-5% to 6-10%. Does that much really matter as far as anything, durability of hardware or power savings, goes?


дɭɭɑӎɠїɾ

Offline

#21 2009-06-29 22:21:19

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED] Can xmobar display volume and network strength %?

No, what you use is fine. I just meant you can't really optimize the script.

It also shouldn't consume a single percent of CPU even at ten times per second. I am testing that with your script:
while true; do sleep 0.1; vol=$(amixer get Master | awk -F'[]%[]' '/%/ {if ($7 == "off") { print "MM" } else { print $2 }}' | head -n 1);  echo -n -e '\r'Vol: $vol%; done

xmobar should be able to handle it fine too, right?

Offline

#22 2009-06-29 22:56:39

Allamgir
Member
Registered: 2009-06-11
Posts: 168

Re: [SOLVED] Can xmobar display volume and network strength %?

Well the command works perfectly, and I'm not sure if xmobar can handle it. I'm trying to get it working in my xmobarrc, and even after looking at this website (http://code.haskell.org/~arossato/xmoba … -arguments), I can't seem to find a way to run a command that doesn't need a refresh interval.

This is my xmobarrc, if it helps.

Config { font = " -*-terminus-*-r-normal-*-*-120-*-*-*-*-iso8859-*"
       , bgColor = "#1C1D1F"
       , fgColor = "#6c9eab"
       , position = TopW L 100
       , lowerOnStart = True
       , commands = [ Run Cpu ["-L","3","-H","50","--normal","green","--high","red"] 10
                    , Run Memory ["-t","Mem: <usedratio>%"] 10
            , Run Com "/home/agi/.bin/wireless.sh" [] "wifi" 30
            , Run Com "/home/agi/.bin/volume.sh" [] "vol" 
                    , Run Swap [] 10
                    , Run Date "%a %b %_d %l:%M" "date" 10
            , Run Battery ["-L","10","-H","100","--low","red","--high","green"] 10
                    , Run StdinReader
                    ]
       , sepChar = "%"
       , alignSep = "}{"
       , template = "%StdinReader% }{ %cpu% | %memory% * <fc=#ffffff>%battery%</fc> %wifi% <fc=#ffffff>%vol%</fc> <fc=#e04613>%date%</fc> |"
       }

However, I'm not sure if you really need to do anything (unless you want to) because if having the script update every .3 seconds or whatever doesn't really consume enough of my resources to make any impact to usage, then who cares? I'm not really sure which would be lighter, anyway. For all I know, having a continuously running script might use more of my Cpu/Mem.


Anyway, thanks for all of the help. I finally got xmobar pretty much just how I like it! Perhaps it's probably a good idea to learn how to write bash scripts so I can extend xmobar to whatever I need wink. I'm going to mark this thread as [SOLVED], but please feel free to post anything you think might help or be interesting.


дɭɭɑӎɠїɾ

Offline

#23 2009-07-03 21:28:01

valadil
Member
Registered: 2009-06-04
Posts: 6

Re: [SOLVED] Can xmobar display volume and network strength %?

Allamgir wrote:

YES! Finally! I got the volume to display just how I want it! I just have one problem...

Whenever I change the volume (I use Fn keybindings for it), there is a 1-2 second delay between the actual volume state and the display on xmobar. It's not huge, but it gets pretty confusing if I can't see exactly which percentage my volume is on and I'm trying to adjust it. If I go to alsamixer and change the volume with the Fn keys, the changes appear instantly. Is there any way to speed up the response time of the shell script/xmobar?

Instead of running the command to check volume over and over, why not read the volume from a fifo?  A fifo is kinda like a file, but that only exists in memory.  Another way to think of it is as a named pipe (where | would be an unnamed pipe) that lives on the filesystem.  xmobar comes with an option for watching a pipe and updating as soon as data is written to it.  This seems like the right way to get infrequent updates immediately.

Anyway, here's how I made a volume pipe:
In .xsession add this line:
rm /home/USERNAME/.vol ; mkfifo /home/USERNAME/.vol ; echo "--" > /home/USERNAME/.vol &

The echo at the end writes into the fifo so that you have something to read when it is first opened. 

In your .xmobarrc, add:
, Run PipeReader "/home/USERNAME/.vol" "vol"
and add %vol% to your bar somewhere.  This is the line that will keep xmobar reading from the fifo.

Finally you need to update the fifo when you change your volume.  The way I did this was to call scripts instead of amixer when I change the volume.   My scripts call amixer and then they put the current volume into the pipe with this line:
amixer -c 0 sget Master,0 | grep dB | head -n1 | cut -f 7 -d " " | sed -e's/\[//g' -e 's/\]//g' > /home/USERNAME/.vol
There's probably a cleaner way to get that line, but I fail at awk.

I'll leave it as an exercise to you to fill in the volume up/down/mute scripts.

There are a couple caveats with this method. 

It may not work out of the box.  In Ubuntu I had to get a new version from darcs and apply a patch.

I haven't figured out how to get a good update for mute yet because I use toggle.  I'm sure there's a way to run amixer to find out if the device is muted and then update with either "--" or the current volume, but I'm lazy and haven't found it yet (and don't use mute very often).

This works great when all your changes come from scripts.  Not so convenient when changes will come from a variety of sources.  For example I use rhythmbox and like having song updates in my xmobar.  I could bind my next button to a script that causes rhythmbox-client to drop and update in the fifo.  The problem is that rhythmbox changes songs on its own.  I'd probably have to write a plugin to get rb to send updates when it does that and (once again) I'm too lazy, so I use Com every couple seconds to check the song.  But the pipe reader is arguably the right way to get updates like this.

Offline

Board footer

Powered by FluxBB