mabox-tools/bin/deskmngr

151 lines
5.7 KiB
Plaintext
Raw Normal View History

2021-05-18 16:49:07 +02:00
#!/bin/bash
2022-02-12 23:19:13 +01:00
# Copyright (C) Daniel Napora 2021-22 <napcok@gmail.com>
# https://maboxlinux.org
2021-05-18 16:49:07 +02:00
#: deskmngr - save all programs (windows) running on current desktop in session file.
2021-05-31 15:05:05 +02:00
#: With windows positions and state (decorated or not)
2021-05-18 16:49:07 +02:00
#: Usage:
2021-05-31 15:05:05 +02:00
#: -l|listsessions - list saved sessions
#: -s|save - save session from current desktop
#: -r|restore sesionfile desktop_ID - restore session from sessionfile on desktop
2021-05-18 16:49:07 +02:00
#:
2021-05-20 14:15:45 +02:00
# 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/>.
2021-05-18 16:49:07 +02:00
SESSIONDIR="$HOME/.config/deskmngr/"
2022-02-12 23:19:13 +01:00
CONFIG_FILE="$SESSIONDIR/deskmngr.conf"
2021-05-18 16:49:07 +02:00
mkdir -p $SESSIONDIR
2022-02-12 23:19:13 +01:00
# If config file not exist create one with defaults
if [ ! -f $CONFIG_FILE ]; then
cat <<EOF > ${CONFIG_FILE}
waitsec=1
EOF
fi
2021-05-18 16:49:07 +02:00
2022-02-12 23:19:13 +01:00
source <(grep = $CONFIG_FILE)
waittime=${waitsec:-1}
2021-05-31 15:05:05 +02:00
case "$LANG" in
pl*)
TITLE="Nie ma nic do zapisania"
TEXT="\nWygląda na to że nie ma otwartych okien na bieżącym pulpicie.\nUruchom jakieś programy i spróbuj ponownie.\n"
SAVE_AS="Zapisz sesję jako:"
SESSNAME="Nazwa sesji"
;;
*)
TITLE="Nothing to save"
TEXT="\nLooks like there are no windows to save on current desktop.\n Run some programs and try again.\n"
SAVE_AS="Save session as:"
SESSNAME="Session name"
;;
esac
2021-05-18 16:49:07 +02:00
savesession() {
curdesk=$(wmctrl -d | grep "*" | awk '{print $1}')
2021-05-31 15:05:05 +02:00
if [ $(wmctrl -l -p -G | awk -v c=$curdesk '$2 == c {print $2}' | wc -l) == "0" ];then notify-send -i dialog-warning "$TITLE" "$TEXT";exit 1;fi
2021-10-11 09:27:02 +02:00
if [ -n "$1" ]; then
filename="$1"
else
2022-12-11 14:09:51 +01:00
filename=$(yad --center --width=300 --title "$SAVE_AS" --entry --entry-label="$SESSNAME" --entry-text="$SESSNAME") || exit 1
2021-10-11 09:27:02 +02:00
fi
2021-05-18 16:49:07 +02:00
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=)
2021-05-19 02:35:11 +02:00
if [[ $cmdline != *quake-term* ]]; then
2021-05-18 16:49:07 +02:00
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")
2021-05-19 02:35:11 +02:00
fi
2021-05-18 16:49:07 +02:00
fi
fi
done
2021-05-21 23:06:41 +02:00
if [ ${#windows[@]} -eq 0 ]; then
2021-05-31 15:05:05 +02:00
notify-send -i dialog-warning "$TITLE" "$TEXT"
2021-05-20 14:15:45 +02:00
else
2021-05-21 23:06:41 +02:00
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"
2021-05-23 13:18:24 +02:00
sd -s "/usr/bin/python /usr/bin/terminator" "terminator" "$SESSIONDIR/${filename// /_}.desk"
2021-05-21 23:06:41 +02:00
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"
2021-05-23 13:18:24 +02:00
sed -i '/usr/bin/python /usr/bin/terminator,terminator,' "$SESSIONDIR/${filename// /_}.desk"
2021-05-21 23:06:41 +02:00
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
2021-05-20 14:15:45 +02:00
fi
2021-05-18 16:49:07 +02:00
}
}
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}
2022-02-12 23:19:13 +01:00
${cmdline} > /dev/null 2>&1 &
# We need to wait for window to appear
sleep ${waittime}
2021-05-18 16:49:07 +02:00
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
}
}
2021-05-31 15:05:05 +02:00
overwrite() {
echo "Not implemented"
}
2021-05-18 16:49:07 +02:00
listsessions() {
2022-12-11 14:09:51 +01:00
ls -1 ${SESSIONDIR}| grep .desk
2021-05-18 16:49:07 +02:00
}
usage() {
grep "^#:" $0 | while read DOC; do printf '%s\n' "${DOC###:}"; done
exit
}
case "$1" in
-s|save) savesession "$2";;
2021-05-21 23:06:41 +02:00
-r|restore) restoresession "$2" "$3";;
-o|overwrite) overwrite "$2" ;;
2021-05-18 16:49:07 +02:00
-l|list) listsessions ;;
-h|--help) usage;;
*) usage;;
esac
exit 0