#!/usr/local/bin/bash
# vdm - Virtual Desktop Management (using vdesk) by Root Sr
# Do not use with window managers that already have virtual desktops!
# Usage: simply bind this script in your window manager to a key/button
# in the root window
# This is the big idea: for every invocation, cycle through the
# dynamically constructed set of all virtual desktops (vd's) that have
# at least 1 window, plus one empty vd.
# Read this until you understand this before reading further...
# Set to "YES" to see what's happening (in.xsession-errors)
DEBUG="NO"
# Current virtual desktop (1-9, 0 contains any sticky windows)
cvd=`vdesk`
# Find highest virtual desktop. Don't start with 0 because the current
# vd will not been seen in the next loop if it has no windows. So start
# with the current vd.
hvd=$cvd
# Make array of virtual desktops and their windows.
# Don't use the obvious xlsclients! It gives the wrong level in the
# windows hierarchy. The next command should be tailored to your
# window manager. This is for twm. For Mwm it is different.
for w in `xwininfo -root -tree | sed -n 's/ \(0x[0-9a-f]*\) ".*/\1/p'`
do vd=`xprop -id $w -notype VDESK_DESKTOP | cut -b 19`
# If the vd is not found for a window, the window is new and hasn't been
# switched to or from. But we know that it's on the current desktop so
# we can set the vd explicitly.
[ "$vd" = "t" ] && {
vdesk $cvd $w
vd=$cvd
}
# Append the window to the list of windows on this vd
# or make it sticky if it's gkrellm or some clock
if xprop -id $w WM_CLASS | egrep -q 'Gkrellm|Clock'
then vdesk 0 $w
else wins[$vd]="${wins[$vd]} $w"
# Find the highest vd
[ $vd -gt $hvd ] && hvd=$vd
fi
done
# Count the number of empty vd's and show the content of nonempty vd's
# (if debugging)
evds=0
for (( i=1 ; i = hvd ; i++ ))
do if [ -z "${wins[$i]}" ]
then let evds++
else [ "$DEBUG" = "YES" ] && echo -n "vd${i}: ${wins[$i]}, "
fi
done
[ "$DEBUG" = "YES" ] && echo -n "evds=${evds}, hvd=${hvd}, "
# If there are no empty vd's, simply go to the next (possibly new and empty)
if [ $evds -eq 0 ]
then vdesk $(($cvd + 1))
# If the current vd is empty, search for a nonempty vd (if any!)
else i=$cvd
while [ -z "${wins[$i]}" ]
do i=$(($i % $hvd + 1))
[ $i -eq $cvd ] && break
done
[ $i -eq $cvd ] && i=$(($i % $hvd + 1))
vdesk $i
fi
[ "$DEBUG" = "YES" ] && echo now on `vdesk`
#!/usr/local/bin/bash # vdm - Virtual Desktop Management (using vdesk) by Root Sr # Do not use with window managers that already have virtual desktops! # Usage: simply bind this script in your window manager to a key/button # in the root window # This is the big idea: for every invocation, cycle through the # dynamically constructed set of all virtual desktops (vd's) that have # at least 1 window, plus one empty vd. # Read this until you understand this before reading further... # Set to "YES" to see what's happening (in .xsession-errors)
DEBUG="NO"
# Current virtual desktop (1-9, 0 contains any sticky windows)
cvd=`vdesk`
# Find highest virtual desktop. Don't start with 0 because the current
# vd will not been seen in the next loop if it has no windows. So start
# with the current vd.
hvd=$cvd
# Make array of virtual desktops and their windows.
# Don't use the obvious xlsclients! It gives the wrong level in the
# windows hierarchy. The next command should be tailored to your
# window manager. This is for twm. For Mwm it is different.
for w in `xwininfo -root -tree | sed -n 's/ \(0x[0-9a-f]*\) ".*/\1/p'`
do vd=`xprop -id $w -notype VDESK_DESKTOP | cut -b 19`
# If the vd is not found for a window, the window is new and hasn't been
# switched to or from. But we know that it's on the current desktop so
# we can set the vd explicitly.
[ "$vd" = "t" ] && {
vdesk $cvd $w
vd=$cvd
}
# Append the window to the list of windows on this vd
# or make it sticky if it's gkrellm or some clock
if xprop -id $w WM_CLASS | egrep -q 'Gkrellm|Clock'
then vdesk 0 $w
else wins[$vd]="${wins[$vd]} $w"
# Find the highest vd
[ $vd -gt $hvd ] && hvd=$vd
fi
done
# Count the number of empty vd's and show the content of nonempty vd's
# (if debugging)
evds=0
for (( i=1 ; i = hvd ; i++ ))
do if [ -z "${wins[$i]}" ]
then let evds++
else [ "$DEBUG" = "YES" ] && echo -n "vd${i}: ${wins[$i]}, "
fi
done
[ "$DEBUG" = "YES" ] && echo -n "evds=${evds}, hvd=${hvd}, "
# If there are no empty vd's, simply go to the next (possibly new and empty)
if [ $evds -eq 0 ]
then vdesk $(($cvd + 1))
# If the current vd is empty, search for a nonempty vd (if any!)
else i=$cvd
while [ -z "${wins[$i]}" ]
do i=$(($i % $hvd + 1))
[ $i -eq $cvd ] && break
done
[ $i -eq $cvd ] && i=$(($i % $hvd + 1))
vdesk $i
fi
[ "$DEBUG" = "YES" ] && echo now on `vdesk`