Re: How to kill all the windows on a desktop?

Bernd Eggink <[email protected]>
Newsgroups gmane.comp.window-managers.icewm.user
Message-ID <[email protected]>
Am 18.07.2010 03:21, schrieb Peng Yu:
> On Mon, Jul 12, 2010 at 9:11 AM, Bernd Eggink<[email protected]>  wrote:
>> Am 12.07.2010 02:15, schrieb Peng Yu:
>>
>> Sorry, I forgot to attach the script. Here it is.
>
> I have three gnome-terminal open on a desktop. But I get the same same
> pid for the three gnome-terminal. Is there a bug?
>
> $ ./desktop-pids
> 11097
> 11097
> 11097

No idea, I don't use gnome-terminal. Try the attached, somewhat more 
elaborated script 'desktop-tool'. "desktop-tool -h" prints a short help, 
"desktop-tool -k" tries to kill all processes on the current desktop. 
Note that it is not always possible to get the PID of an X11 client. 
Java programs, for example, can't be killed this way.

Regards,
Bernd

-- 
Bernd Eggink
http://sudrala.de

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first

_______________________________________________
IceWM-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/icewm-user
desktop-tool (text/plain, 5.2 KB)
#!/bin/bash

# desktop-tool
# Print information about desktop(s) or kill all clients on a desktop
# (C) Bernd Eggink, [email protected]

function usage
{
    cat <<EOF
Usage: ${0##*/} [option...] [desktop...]
option:     -a          all desktops [current]
            -h          this help
            -i m        print information
                        m: one or more of
                            a   all 
                            c   command
                            d   desktop
                            n   name
                            p   PID
            -k          kill all processes on desktop
            -s sig      signal to use with kill
            -t          test mode, only print commands
            -x pattern  exclude clients with names matching pattern
desktop:    Desktop index (0,1,...) No argument: current desktop   
EOF
}

#-----------------------------------------------------------------------

function getValue   # {-r | id} name
{
    local arg

    if [[ $1 == -r ]]
    then 
        arg=-root
    else
        arg="-id $1"
    fi

    local s=$(xprop $arg $2)

    [[ $s != *not\ found* ]] && echo ${s#* = }
}

#-----------------------------------------------------------------------

function getStringValue     # {-r | id} name
{
    local arg

    if [[ $1 == -r ]]
    then 
        arg=-root
    else
        arg="-id $1"
    fi

    local v=$(xprop $arg $2)

    [[ $v != *not\ found* ]] && 
    {
        v=${v#* = } v=${v#*\"} v=${v%\"*}
        echo $v
    }
}

#-----------------------------------------------------------------------

function getStringValues  # name
{
    local a e v

    v=$(xprop -root $1)

    [[ $v != *not\ found* ]] && 
    {
        v=${v#*= }
        IFS=, read -a a <<<"$v"
        
        for e in "${a[@]}"
        do
            eval echo "$e"
        done
    }
}

#-----------------------------------------------------------------------

function getWords   # id name
{
    local words i n word
    local v=$(xprop -id $1 $2)
    
    [[ $v != *not\ found* ]] && 
    {
        v=${v#*\{} v=${v%\}*} v=${v//,}

        eval words=($v)
        n=${#words[*]}
        
        for (( i = 0; i < n; ++i ))
        do
            word=${words[i]}

            if [[ $word == *\ * ]] 
            then
                printf "'%s'" "$word"
            else
                printf "%s" $word
            fi

            (( i < n  - 1 )) && printf ' '
        done

        printf '\n'
    }
}

#-----------------------------------------------------------------------

function main
{
    local client clients clientDesktop clientPID 
    local cmd desktop desktopNames infomode mode=pid name nm opt signal T 
    local -a exclude

    while getopts "ahi:ks:tvx:" opt
    do
        case $opt in
        (a)     all=1
                ;;
        (h)     usage
                exit 0
                ;;
        (i)     mode=info
                
                if [[ $OPTARG == a ]]
                then
                    infomode=cdnp
                else
                    infomode=$OPTARG
                fi
                ;;
        (k)     mode=kill
                ;;
        (s)     signal=-$OPTARG
                ;;
        (t)     T=echo
                ;;
        (x)     exclude+=($OPTARG)
                ;;
        (*)     usage
                exit 1
        esac
    done

    shift $((OPTIND - 1))

    if (( $# > 0 ))
    then
        desktops=("$@")
    else
        desktops=($(getValue -r _WIN_WORKSPACE))
    fi

    IFS=$'\n';
    desktopNames=($(getStringValues _WIN_WORKSPACE_NAMES))
    clients=($(getStringValues _WIN_CLIENT_LIST))
    IFS=$' \t\n'

    for client in "${clients[@]}"
    do
        clientDesktop=$(getValue $client _NET_WM_DESKTOP)

        [[ ! $clientDesktop ]] && continue

        name=$(getStringValue $client WM_NAME)

        for nm in "${exclude[@]}"
        do 
            [[ $name == $nm ]] && continue 2
        done

        cmd=$(getWords $client WM_COMMAND)
        clientPID=$(getValue $client _NET_WM_PID)

        [[ $clientPID == $$ ]] && continue

        ok=$all

        [[ ! $ok ]] &&
        {
            for desktop in "${desktops[@]}"
            do
                [[ $clientDesktop == $desktop ]] &&
                {
                    ok=1
                    break
                }
            done
        }

        [[ ! $ok ]] && continue

        case $mode in
        (kill)  $T kill $signal "$clientPID"
                ;;
        (info)  [[ $infomode == *d* ]] && out[clientDesktop]+=$(printf "[%d]" $clientDesktop)
                [[ $infomode == *p* ]] && 
                    if [[ $clientPID ]]
                    then
                        out[clientDesktop]+=$(printf "%6d" $clientPID)
                    else
                        out[clientDesktop]+="     -"
                    fi

                [[ $infomode == *n* ]] && out[clientDesktop]+=$(printf " '%s'" "$name")
                [[ $infomode == *c* ]] && out[clientDesktop]+=$(printf " \"%s\"" "$cmd")
                [[ $infomode ]] && out[clientDesktop]+=$'\n'
                ;;
        esac
    done

    [[ $mode == info ]] && 
        for line in "${out[@]}"
        do
            echo -n "$line"
        done
}

#-----------------------------------------------------------------------

main "$@"
exit 0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.