mabox-tools/bin/deskmngr

117 lines
4.8 KiB
Bash
Executable File

#!/bin/bash
# Copyright (C) Daniel Napora 2021 <napcok@gmail.com>
#: deskmngr - save all programs (windows) running on current desktop in session file.
#: With windows positions and state (decorated or not)
#: Usage:
#: -s|save - save session from current desktop
#: -r|restore sesionfile desktop_ID - restore session from sessionfile on desktop
#:
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
SESSIONDIR="$HOME/.config/deskmngr/"
mkdir -p $SESSIONDIR
savesession() {
curdesk=$(wmctrl -d | grep "*" | awk '{print $1}')
if [ $(wmctrl -l -p -G | awk -v c=$curdesk '$2 == c {print $2}' | wc -l) == "0" ];then notify-send -i dialog-warning "Nothing to save" "\nStart some programs and try again.\n";exit 1;fi
filename=$(yad --center --title "Save session as:" --entry --entry-label="Session name" --entry-text="Session name") || exit 1
windows=()
wmctrl -l -p -G | {
while IFS= read -r line
do
read -r ID DESK PID x_offset y_offset width height rest< <(echo $line)
if [[ "$DESK" != "-1" ]]; then
if [[ "$DESK" == "$curdesk" ]]; then
cmdline=$(ps -fp $PID -o command=)
if [[ $cmdline != *quake-term* ]]; then
xwininfo -id "$ID" -wm | grep -q Undecorated && D="u" || D="d"
##
frameY=$(xwininfo -id "$ID" -stats | grep "Relative upper-left Y:" | cut -d: -f2)
frameX=$(xwininfo -id "$ID" -stats | grep "Relative upper-left X:" | cut -d: -f2)
windows+=("$D $width $height $((x_offset-frameX)) $((y_offset-2*frameY)) $cmdline")
fi
fi
fi
done
if [ ${#windows[@]} -eq 0 ]; then
notify-send -i dialog-warning "Nothing to save" "Opps, looks like there are no windows to save on current desktop.\n Run some programs and try again ;)"
else
printf "%s\n" "${windows[@]}" > "$SESSIONDIR/${filename// /_}.desk"
# Serch replace
if command -v sd 1>/dev/null; then
sd -s "/usr/lib/chromium/chromium" "chromium --new-window" "$SESSIONDIR/${filename// /_}.desk"
sd -s "/usr/lib/firefox/firefox" "firefox --new-window" "$SESSIONDIR/${filename// /_}.desk"
sd -s "/usr/bin/python3 /usr/bin/terminator" "terminator" "$SESSIONDIR/${filename// /_}.desk"
sd -s "geany" "geany -i" "$SESSIONDIR/${filename// /_}.desk"
sd -s "cherrytree" "cherrytree --new-window" "$SESSIONDIR/${filename// /_}.desk"
sd -s "pcmanfm -d" "pcmanfm -n" "$SESSIONDIR/${filename// /_}.desk"
sd -s "Thunar --daemon" "thunar" "$SESSIONDIR/${filename// /_}.desk"
else
sed -i '/usr/lib/chromium/chromium,chromium --new-window,' "$SESSIONDIR/${filename// /_}.desk"
sed -i '/usr/lib/firefox/firefox,firefox --new-window,' "$SESSIONDIR/${filename// /_}.desk"
sed -i '/usr/bin/python3 /usr/bin/terminator,terminator,' "$SESSIONDIR/${filename// /_}.desk"
sed -i 'geany,geany -i,' "$SESSIONDIR/${filename// /_}.desk"
sed -i 'cherrytree,cherrytree --new-window,' "$SESSIONDIR/${filename// /_}.desk"
sed -i 'pcmanfm -d,pcmanfm -n,' "$SESSIONDIR/${filename// /_}.desk"
sed -i 'Thunar --daemon,thunar,' "$SESSIONDIR/${filename// /_}.desk"
fi
if command -v geany 1>/dev/null; then
geany "$SESSIONDIR/${filename// /_}.desk"
fi
fi
}
}
restoresession() {
sessfile="$1"
DESK="$2"
echo "$sessfile"
cat "$SESSIONDIR/$sessfile" | {
while IFS= read -r line
do
read -r D width height x y cmdline< <(echo $line)
wmctrl -s ${DESK}
${cmdline} > /dev/null 2>&1 &
sleep 1.5
wmctrl -r :ACTIVE: -b remove,maximized_vert,maximized_horz
if [[ "$D" = "u" ]]; then xdotool key super+b; fi
wmctrl -r :ACTIVE: -e 0,${x},${y},${width},${height}
done
}
}
listsessions() {
ls -1 ${SESSIONDIR}
}
usage() {
grep "^#:" $0 | while read DOC; do printf '%s\n' "${DOC###:}"; done
exit
}
case "$1" in
-s|save) savesession "$2";;
-r|restore) restoresession "$2" "$3";;
-o|overwrite) overwrite "$2" ;;
-l|list) listsessions ;;
-h|--help) usage;;
*) usage;;
esac
exit 0