"La educación es
el arma más poderosa que existe
para cambiar el mundo"
Nelson Mandela


#!/bin/bash
mute=0
Program=$@
program_workingDir=`dirname $(readlink -f $0)`
status_fname="$program_workingDir/.mdesktop_status"
program_systemWindows="$program_workingDir/.mdesktop_systemWindows"
n_visible=0
window_id=0
window_name=""
window_hidden=0
window_isignorable=0
window_vd=0 #vd means virtual desktop
vd_activeId=0
#-----------------------------------------------------------------------------------------------
function debug() {
if [[ $mute == 0 ]]
then
echo "DEBUG: $1"
fi
}
#-----------------------------------------------------------------------------------------------
function vd_getActiveId() {
local vd_id=-1
local vd_status="-"
while read vDesktop
do
vd_id=`echo "$vDesktop" | tr -s ' ' | cut -f 1 -d " "`
vd_status=`echo "$vDesktop" | tr -s ' ' | cut -f 2 -d " "`
if [ "$vd_status" == "*" ]
then
echo $vd_id
break
fi
done < <(wmctrl -d)
}
#-----------------------------------------------------------------------------------------------
#isSystemWindow:
# All windows in mdestop_systemWindows and also the Desktop
# How to detect the Desktop window?
# Because if you ask xwininfo -id <window_id> -all and you grep Window type: Desktop
# System Windows are marked to vDesktop -1
function isSystemWindow() {
local window_isSystem=0
local window_name=$1
local window_id=$2
local window_isDesktop=0
window_isSystem=$(grep "$window_name" $program_systemWindows | wc -l)
if [[ $window_isSystem == 1 ]]
then
echo $window_isSystem
return
fi
window_isDesktop=$(xwininfo -id $window_id -all | grep "Desktop" | wc -l)
if [[ $window_isDesktop == 1 ]]
then
echo "1"
else
echo "0"
fi
}
#-----------------------------------------------------------------------------------------------
# window_activeId
# Window id which actually is focused, it is, the one which is active
# This value will be saved in .desktop_status so it can be restored
#-----------------------------------------------------------------------------------------------
function windows_minimize_all() {
local window_activeId=-1
debug "windows_minimize_all..."
window_activeId=$(xdotool getactivewindow)
echo "$window_activeId" > $status_fname
while read Window
do
# window_name=`echo "$Window" | tr -s ' ' | cut -f 4 -d " "`
window_id=`echo "$Window" | tr -s ' ' | cut -f 1 -d " "`
window_vd=`echo "$Window" | tr -s ' ' | cut -f 2 -d " "`
if [[ $(isSystemWindow $window_name $window_id) == 1 ]]
then
debug "NOT minimize $window_id: system window"
continue
fi
if [[ $window_vd != $vd_activeId ]]
then
debug "NOT minimize $window_id: in another desktop"
continue
fi
debug "MINIMIZE $window_id"
xdotool windowminimize $window_id
done < <(wmctrl -l)
}
#-----------------------------------------------------------------------------------------------
function windows_restore_all() {
while read Window
do
# window_name=`echo "$Window" | tr -s ' ' | cut -f 4 -d " "`
window_id=`echo "$Window" | tr -s ' ' | cut -f 1 -d " "`
window_vd=`echo "$Window" | tr -s ' ' | cut -f 2 -d " "`
if [[ $(isSystemWindow $window_name $window_id) == 1 ]]
then
debug "NOT RESTORE $window_id: system window"
continue
fi
if [[ $window_vd != $vd_activeId ]]
then
debug "NOT RESTORE $window_id: in another desktop"
continue
fi
xdotool windowactivate $window_id
done < <(wmctrl -l)
# Set focus to the window was active before minimizing all
window_activeId=$(cat $status_fname)
debug "SETTING FOCUS TO: $window_activeId"
xdotool windowactivate $window_activeId
}
#-----------------------------------------------------------------------------------------------
debug "###############################################"
debug "###############################################"
debug "# MAIN ##########################################"
debug "###############################################"
debug "###############################################"
vd_activeId=$(vd_getActiveId)
debug "current virtual desktop=$vd_activeId"
while read Window
do
debug "-----------------------------------------------------------------------"
debug "### 1"
debug "$Window"
window_id=`echo "$Window" | tr -s ' ' | cut -f 1 -d " "`
window_vd=`echo "$Window" | tr -s ' ' | cut -f 2 -d " "`
window_name=`echo "$Window" | tr -s ' ' | cut -f 4 -d " "`
window_hidden=`xwininfo -all -id $window_id | grep "Hidden" | wc -l`
debug "Window Id: $window_id"
debug "Window vd: $window_vd"
debug "Window Hidden: $window_hidden"
debug "Window Name: $window_name"
if [[ $window_vd == "-1" ]]
then
debug "Window virtual desktip is -1, ignore actual window"
continue
fi
window_isignorable=$(isSystemWindow $window_name $window_id)
debug "Window is system: $window_isignorable"
# is current window an ignore Window?
if [[ $window_isignorable == 1 ]]
then
debug "Ignore actual Window"
continue
fi
# is current active virtual desktop?
if [[ $window_vd != $vd_activeId ]]
then
debug "Ignore actual Window: in another desktop"
continue
fi
# Is there any visible window?
if [[ $window_hidden == 0 ]]
then
debug "show_desktop"
let n_visible++
fi
debug "n_visible = $n_visible"
debug "### 4"
done < <(wmctrl -l)
# We must exec every window and minimize or maximize, depending wether it was already minimized or not
debug "###n_visible = $n_visible"
# windows_restore_all
if [[ $n_visible == 0 ]]
then
debug "restore all windows"
windows_restore_all
else
debug "minimize all windows"
windows_minimize_all
fi
|