By the way, I've been using the module interface for a similar
task.  One of the buttons in FvwmButtons displays the time and
date, another one current memory usage and a third one the top
cpu using process.  This is updated every fifteen seconds.
Because I don't like processes (FvwmCommand) to be started all the
time I wrote the ModuleListenOnly fvwm command and implemented the
module as a zsh script (attached).  Note that the module interface
uses the "long" C type, and the script is written for systems
where sizeof(long) is four.  It needs to be adapted of eight byte
longs, i.e. 64-bit systems.

The command to start the script is

 + I ModuleListenOnly fvwm_periodic -u 15 -d 3 -c clock -t topproc -M mempct

in the InitFunction and the RestartFunction.  The ModuleListenOnly
allows to write one way modules.  They can send packets to fvwm,
but fvwm does not send them any packets.

Ciao

Dominik ^_^  ^_^

-- 

Dominik Vogt
#!/usr/bin/zsh

emulate zsh
setopt HUP
zmodload zsh/zselect
LC_ALL=de_DE@euro

MODULE="FvwmButtons"
IDCLOCK=""
IDMEM=""
IDPROC=""
SECS="15"
ISECS="3"

usage () {
        echo "usage: module-args $0 [-c id] [-t id] [-M id] [-m module] [-u 
secs] [-d delay]"
        echo "Update certain button titles in FvwmButtons"
        echo " module-args:  filled out by fvwm when started as a module"
        echo " -h:           display this help message"
        echo " -c buttonid:  update clock at the given button id"
        echo " -M buttonid:  update used memory at the given button id"
        echo " -t buttonid:  update topproc at the given button id"
        echo " -m module:    name of the module [$MODULE]"
        echo " -u secs:      update buttons every secs seconds [$SECS]"
        echo " -d secs:      initial delay [$ISECS]"
}

send () {
        printf \\00\\00\\00\\00\\xa0\\00\\00\\00%159s\\00\\00\\00\\00\\n "$1" \
                >&$OF || exit 1
}

changebutton () {
        send "SendToModule '$MODULE' ChangeButton $1"
}

update_proc () {
        CM='?'
        PM='?'
        NM='?'
        ps -A --format "%C %P %c" | {
                # skip header
                read
                while read C P N; do
                        test $[CM < C] = 1 && CM="$C" && PM="$P" && NM="$N"
                done
        }
        changebutton "'$IDPROC' Title '$PM $CM% $NM'"
}

update_mem () {
        TOTAL=1
        FREE=0
        while read T V R; do
                test x"$T" = x"MemTotal:" && TOTAL="$V" && break
        done < /proc/meminfo
        while read T V R; do
                test x"$T" = x"MemFree:" && FREE="$V" && break
        done < /proc/meminfo
        PCTM="$[(100 * (TOTAL - FREE)) / TOTAL]"
        changebutton "'$IDMEM' Title 'mem: ${PCTM}%'"
}

update_clock () {
        T=`strftime '%R  %a %d.%m.' $EPOCHSECONDS`
        changebutton "'$IDCLOCK' Title '$T'"
}

#
# parse options
#

test $# -le 4 && echo "fvwm_periodic must be started by fvwm" 1>&2 && exit 1
OF="$1"
IF="$2"
shift 5
while test ! $# = 0; do
        case "$1" in
        -h|-\?|--help)
                usage
                exit 0
                ;;
        -c|--clock)
                test $# = 1 && usage && exit 1
                shift
                IDCLOCK="$1"
                test ! x"$IDCLOCK" = x && zmodload zsh/datetime
                ;;
        -M|--mem)
                test $# = 1 && usage && exit 1
                shift
                IDMEM="$1"
                ;;
        -t|--topproc)
                test $# = 1 && usage && exit 1
                shift
                IDPROC="$1"
                ;;
        -m|--module-name)
                test $# = 1 && usage && exit 1
                shift
                MODULE="$1"
                test x"$MODULE" = x && usage && exit 1
                ;;
        -u|--update-secs)
                test $# = 1 && usage && exit 1
                shift
                SECS="$1"
                SECS="$[SECS]"
                test "$[SECS]" -le 0 && usage && exit 1
                ;;
        -d|--delay-secs)
                test $# = 1 && usage && exit 1
                shift
                ISECS="$1"
                ISECS="$[ISECS]"
                test "$[ISECS]" -lt 0 && usage && exit 1
                ;;
        **)
                break
                ;;
        esac
        shift
done
test $# -gt 1 && usage && echo "error: unknown option $1" && exit 1


#
# main body
#

trap exit HUP INT QUIT KILL PIPE TERM

# sleep is not a builtin, so prefer zselect
test "$[ISECS > 0]" = 1 && zselect -t ${ISECS}00
while true; do
        test ! x"$IDCLOCK" = x && update_clock
        test ! x"$IDMEM" = x && update_mem
        test ! x"$IDPROC" = x && update_proc
        zselect -t ${SECS}00
done

Reply via email to