#!/bin/sh DEFAULT_PROFILE="auto" SAVEPATH="/tmp/wpos" CHECKFILE="$SAVEPATH/.tmp.check" ## Check and create main profile direcotry if [ ! -d "$SAVEPATH" ]; then mkdir -p "$SAVEPATH" chmod 700 "$SAVEPATH" fi ## Choose profile PROFILE="$DEFAULT_PROFILE" if [ -n "$2" ]; then PROFILE="$2" fi query() { wmctrl -lxG $@ } parse() { LINE="$1" ID="$(echo "$LINE" | awk '{ print $1 }')" GRAVITY="$(echo "$LINE" | awk '{ print $2 }')" POSX="$(echo "$LINE" | awk '{ print $3 }')" POSY="$(echo "$LINE" | awk '{ print $4 }')" WIDTH="$(echo "$LINE" | awk '{ print $5 }')" HEIGHT="$(echo "$LINE" | awk '{ print $6 }')" } if [ "$1" = "save" ]; then query > "$SAVEPATH/$PROFILE" exit 0 elif [ "$1" = "list" ]; then query -p exit 0 elif [ "$1" = "restore" ]; then cat "$SAVEPATH/$PROFILE" | while read LINE; do ## Parse records in savefile parse "$LINE" ## Check few important values [ "$GRAVITY" -lt 0 ] && continue ## And run command wmctrl -i -r "$ID" -e "$GRAVITY","$POSX","$POSY","$WIDTH","$HEIGHT" done query > "$CHECKFILE" cat "$SAVEPATH/$PROFILE" | while read LINE; do parse "$LINE" [ "$GRAVITY" -lt 0 ] && continue NEWX="$(grep "$ID" "$CHECKFILE" | awk '{print $3 }')" NEWY="$(grep "$ID" "$CHECKFILE" | awk '{print $4 }')" DIFFY="$(($NEWY - $POSY))" DIFFX="$(($NEWX - $POSX))" [ "$DIFFY" -ge 0 ] && POSY=$(($POSY - $DIFFY)) [ "$DIFFX" -ge 0 ] && POSX=$(($POSX - $DIFFX)) wmctrl -i -r "$ID" -e "$GRAVITY","$POSX","$POSY","$WIDTH","$HEIGHT" done fi