You are not logged in.

#1 2007-08-22 13:29:00

burk
Member
From: Norway
Registered: 2007-06-17
Posts: 46

C dwm statusbar app

Hey

I'm trying to configure my dwm setup, and I read the posts on this forum and found out that you have to pipe things to dwm to show things on the statusbar. Trying to find a suitable script I found a C program at http://spaceinvader.rofl.org.uk/. It showed the date, time and load, and some other stuff. I modified it to show the song played by mpd, the date and time, the temperature of the processor and the load. Here's the modified version: http://pastebin.archlinux.org/13104

My problem is the mpd part, nothing mpd-related is showed at all, but I cannot figure out why, so I was wondering if you could help me fix it smile

Here's the mpd-related part of the program, the problem is most likely in here, and it may well be a stupid error because my C knowledge is limited smile

//... snip ...//

#define MPC_INFO    "/usr/bin/mpc"

//... snip ...//

  FILE *dwm, *mpd, *tempP;
  //... snip ...//

  for (;;) {
    getloadavg(&load, 1);

    // Run mpc command to get song playing
    if ((mpd = popen(MPC_INFO, "r")) == NULL) {
      perror("couldn't execute "MPC_INFO);
      exit(EXIT_FAILURE);
    }
    setvbuf(dwm, NULL, _IOLBF, BUFSIZ);

    // Check info read
    if (fgets(song_buf, SONG_BUF_SIZE, mpd) == NULL) {
      if (ferror(mpd)) {
    printf("couldn't read line");
    sprintf(song_buf, "error");
    clearerr(mpd);
      }
    }
    pclose(mpd);
    
    //... snip ...//

Offline

#2 2007-08-22 13:31:53

harlekin
Member
From: Germany
Registered: 2006-07-13
Posts: 408

Re: C dwm statusbar app

I am sorry for being some kind of off-topic. If you plan to use a C program for that, I'd query the mpd server directly as it is really easy and then your program wouldn't depend on mpc. If you want to use mpc, as shell script is much easier.

But I will take a look into your code now.

I've written an example which is working for me:

#include <stdio.h>
#include <stdlib.h>

#define MPC_BIN "/usr/bin/mpc"

int main() {
    FILE* mpc = NULL;
    char  buf[512];

    if ((mpc = popen(MPC_BIN, "r")) == NULL) {
        perror("Failed to execute mpc:");
        exit(1);
    }

    if (!fgets(buf, 512-1, mpc))
        fprintf(stderr, "fgets() failed");

    if (pclose(mpc) == -1) {
        perror("Failed to close mpc process:");
        exit(1);
    }

    exit(0);
}

Maybe this will be helpful. Maybe you should run your C program like this to catch errors that go to stderr:

./program 2&>1 | dwm

Btw. where in your code are you passing what you've read from mpc's output to dwm? I cannot find a line there.

edit: I've tested your program and it's working for me, too. There has to be something wrong when you pass song_buf to dwm, as song_buf i filled correctly. See mpc.c for working code.

Last edited by harlekin (2007-08-22 13:49:27)


Hail to the thief!

Offline

#3 2007-08-22 13:53:42

harlekin
Member
From: Germany
Registered: 2006-07-13
Posts: 408

Re: C dwm statusbar app

Not a link at all, but as mpd is a server, you write a small client and connect to it.

The protocol is documented here: https://svn.musicpd.org/mpd/trunk/doc/COMMANDS

Just give me a little time and I can write a example program. I edit this post later.

edit: Okay. I've written a small program that connects to the mpd server, queries information about the currently playing song and gives a nice output. Actually the code become a little longer than I expected, but it's less error-prone than I expected, too. This doesn't have to mean, it doesn't contain any errors. I've documented all dangerous sections in the code except these:
* there's no checking being done if the command we've sent was OK for the server
* parsing is quick and dirty

But I hope you get the idea of how this can be done. But if you stick to your method, I'll understand that because the code has become longer than I expected.

Code

And some output:

$ gcc -o mpc mpc.c
$ ./mpc
Chieko Kawabe - [Be Your Girl] Be Your Girl

Last edited by harlekin (2007-08-22 14:40:10)


Hail to the thief!

Offline

#4 2007-08-22 15:34:07

burk
Member
From: Norway
Registered: 2007-06-17
Posts: 46

Re: C dwm statusbar app

spaceinvader wrote:

Hi,

Didn't notice there were many people using this.
I have updated it myself to support MPD with sockets but the code is very messy.
Expect a release "soonish".

spaceinvader.

P.S.: Website down for a while, I got some new hardware which everything is being migrated onto.

Cool, thanks smile

And thanks very much to you too harlekin smile, writing all that code.. I will definitely read it to learn something about sockets in C, and test it before spaceinvader releases his code

Offline

#5 2007-08-26 17:39:08

burk
Member
From: Norway
Registered: 2007-06-17
Posts: 46

Re: C dwm statusbar app

While waiting for your new program my brother helped me create this python script:

#!/usr/bin/python

from time import strftime, localtime, sleep
from mpdclient import *

mpd = connect()

while True:

    # get the time
    time = strftime("%d %b %Y %H:%M:%S", localtime())

    # cpu temperature
    temp_file = open("/sys/bus/i2c/devices/9191-0290/temp2_input", "r")
    temp = temp_file.readline().rstrip('\n')[0:2]
    temp_file.close()

    # mpd info
    song_info = mpd.currentsong()
    playing = song_info.artist + " - " + song_info.title

    # print to dwm
    print playing + " | " + temp + u'\xb0'.encode("UTF8") + "C | " + time 

    sleep(5)

It works fine and prints what it should every 5th second. My problem is how to start it with dwm. I start dwm with SLiM, and I've tried numerous ways of starting it, like:

~/.dwm/status.py | exec dwm
exec ~/.dwm/status.py | dwm

, but I always get some error, often an EOF in the status bar. The closest I got was when it displayed three random characters from the status string hmm I read in the bottom of the dwm manual that it can be because some login managers close stdin. Is that my problem? How can I fix it?

Offline

#6 2007-08-26 18:25:02

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: C dwm statusbar app

burk wrote:

, but I always get some error, often an EOF in the status bar. The closest I got was when it displayed three random characters from the status string hmm I read in the bottom of the dwm manual that it can be because some login managers close stdin. Is that my problem? How can I fix it?

What about checking using ~/.xinitrc and startx ? Then you'll know whether it is your problem or not.


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#7 2007-08-27 14:40:33

burk
Member
From: Norway
Registered: 2007-06-17
Posts: 46

Re: C dwm statusbar app

I tried starting it with startx and both "~/.dwm/status.py | exec dwm" and "exec ~/.dwm/status.py | dwm", and I've tried using popen in python. After starting dwm i got dwm 4.4 in the statusbar and after a minute or so it started showind 3-4 characters of the output (with all the different methods)...

Offline

#8 2007-08-27 17:42:53

Sigi
Member
From: Thurgau, Switzerland
Registered: 2005-09-22
Posts: 1,131

Re: C dwm statusbar app

I'm far OT here but I'm really impressed to see so many other Archers use the great combo of dwm & mpd!

Here's my .xinitrc. It's ugly but works great. Output:

[ Moby - Bodyrock @ 71% ] [ /:688M ~/:574M c:358M d:616M e:531M ] [ 86% = 1:55 ] [ 800MHz ] [ 27.08.07 - 19:41 ]
#!/bin/sh
#######
# DWM #
#######
space=`echo " "`
timeout=0.2

while true
    do
    # Battery calc
    rem=`cat /proc/acpi/battery/BAT0/state | grep remaining | tr -s " " | cut -d " " -f 3`
    voll=`cat /proc/acpi/battery/BAT0/info | grep "design capacity:" | tr -s " " | cut -d " " -f 3`
    bat=`echo "100*$rem/$voll" | bc` &> /dev/null
    # Battery time
    capacity=`cat /proc/acpi/battery/BAT0/state | grep "capacity:" | tr -s " " | cut -d " " -f 3`
    rate=`cat /proc/acpi/battery/BAT0/state | grep "rate:" | tr -s " " | cut -d " " -f 3` 
    thour=`echo "scale=0;$capacity/$rate" | bc` &> /dev/null
    tmin=`echo "scale=0;60*$capacity/$rate-60*$thour" | bc` &> /dev/null
    
    if [ `cat /proc/acpi/battery/BAT0/info | grep tech | wc -l` == 0 ];then
        batt=""
        elif [ `expr $tmin` -le 9 ]; then
            tmin="0$tmin"
        elfi
        elif [ `expr $rate` -gt 0 ];then
            batt=`echo "[ ${bat}% = $thour:$tmin ] "`
        elfi
    fi

    # cpufreqd
    freq=`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq`
    cpuf=`echo "scale=2;$freq/1000000" | bc`
        if [ $freq == 800000 ];then
                cpu=`echo " [ 800MHz ] "`
    else
                cpu=`echo " [ ${cpuf}GHz ] "`
    fi
        if [ $freq == 1866000 ];then
        cpu=""
    fi

        echo [ `mpc | sed -n '1,1p' | cut -c -70` @ `mpc volume | sed -e 's#volume: ##'` ]${space}[ /:`df -h | grep sda7 | tr -s " " | cut -d " " -f 4` \~/:`df -h | grep home | tr -s " " | cut -d " " -f 4` c:`df -h | grep win/c | tr -s " " | cut -d " " -f 4` d:`df -h | grep win/d | tr -s " " | cut -d " " -f 4` e:`df -h | grep win/e | tr -s " " | cut -d " " -f 4` ]${space}${batt}${cpu}[ `date +%d.%m.%y` - `date +%k:%M` ]
        sleep $timeout
    done 2> /dev/null | dwm | eval `cat ~/.fehbg`

Haven't been here in a while. Still rocking Arch. smile

Offline

#9 2007-08-27 17:54:13

harlekin
Member
From: Germany
Registered: 2006-07-13
Posts: 408

Re: C dwm statusbar app

dwm displays it's version number if there's nothing it can read from stdin.

Try removing the loop from your python script and run it from .xinitrc like this:

while true; do
    ~/.dwm/status.py 
done | exec dwm

But your method should work, too. As it doesn't, you can try it to narrow down the error or to use it as work-around.


Hail to the thief!

Offline

#10 2007-08-27 18:53:40

azerty
Member
Registered: 2007-08-23
Posts: 90
Website

Re: C dwm statusbar app

burk wrote:

Hey

I'm trying to configure my dwm setup, and I read the posts on this forum and found out that you have to pipe things to dwm to show things on the statusbar. Trying to find a suitable script I found a C program at http://spaceinvader.rofl.org.uk/. It showed the date, time and load, and some other stuff. I modified it to show the song played by mpd, the date and time, the temperature of the processor and the load. Here's the modified version: http://pastebin.archlinux.org/13104

My problem is the mpd part, nothing mpd-related is showed at all, but I cannot figure out why, so I was wondering if you could help me fix it smile

I don't really understand why you're making it so complicated.

To get some nice Date, Load and Mail information in you dwm statusbar, simply add the following to your .xinitrc:

exec
   while true
   do

     # Date, Load, Mail:
     echo `date '+%A %d/%m/%Y %T'` Load: `uptime | sed 's/.*,//'` Mail: `ls $HOME/Mail/inbox/new/ | wc -l`

   sleep 1
   done | dwm

As I remember, displaying CPU-temperature and some mpd-stuff shouldn't be that difficult.
I don't think you have to make weird C-programs to display some stuff in the statusbar.
If you need help, you could also write to the dwm-mailinglist.

Last edited by azerty (2007-08-27 18:54:30)


Why are we here? What is the sense of life?
INVITATION TO THE TRUTH

Offline

#11 2007-08-27 19:28:51

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: C dwm statusbar app

I use sh also, but mine is a bit more complicated, based on this one :
http://www.mail-archive.com/dwm@suckles … 01722.html

.xinitrc :

  [ -p .dwm-status ] || mkfifo $HOME/.dwm-status
  $HOME/.dwm.in &
  until dwm <> $HOME/.dwm-status; do
    true
  done

.dwm.in

#!/bin/sh

while true
do
  echo "`checkmail.sh 2>/dev/null``date '+%Y-%m-%d %H:%M'``uptime | sed 's/.*,//'``acpi -b | cut -d',' -f2`" > ~/.dwm-status
  sleep 30
done

checkmail.sh

#!/bin/zsh
# vim:ft=zsh ts=4
#
# (c) 2007 by Robert Manea <rob dot manea at gmail dot com>
# Check for new mails in maildir style folders
#

#Maildir folder
MAILDIR='/data/share/mail'

smail() { 
    local -A counts; local i

    for i in "${MAILDIR:-${HOME}/Mail}"/*/new/*
        { (( counts[${i:h:h:t}]++ )) }
    for i in ${(k)counts}
        { print -n $i=$counts[$i]' ' }
}

print "${$(smail):-"No new mail. "}"

pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#12 2007-08-28 02:47:36

rab
Member
Registered: 2006-06-15
Posts: 185

Re: C dwm statusbar app

Just get conky-cli in AUR then in .xinitrc have conky | dwm


rawr

Offline

#13 2007-08-28 15:56:59

finferflu
Forum Fellow
From: Manchester, UK
Registered: 2007-06-21
Posts: 1,899
Website

Re: C dwm statusbar app

rab wrote:

Just get conky-cli in AUR then in .xinitrc have conky | dwm

Wow thanks! that works like a charm big_smile


Have you Syued today?
Free music for free people! | Earthlings

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- A. de Saint-Exupery

Offline

Board footer

Powered by FluxBB