Skip to content

Instantly share code, notes, and snippets.

@macieksk
Last active December 1, 2020 14:13
Show Gist options
  • Save macieksk/dfea1ede887187b3ef39 to your computer and use it in GitHub Desktop.
Save macieksk/dfea1ede887187b3ef39 to your computer and use it in GitHub Desktop.
Grid window placement in KDE, Plasma. To use create appropriate keyboard shortcuts to KDE and enjoy quick window placement. Originally created by Anpu and posted on KDE forum https://forum.kde.org/viewtopic.php?f=111&t=108024
#!/bin/bash
# number of monitors
MONITORS=`xrandr | grep -w connected | wc -l`
# make a gap because of top panel
GAP_TOP=0
GAP_BOTTOM=15
#find x position of active window
GET_X_POS=`xdpyinfo | grep focus | grep -E -o 0x[0-9a-f]+`
X_POS=`xwininfo -id $GET_X_POS | grep -w "Absolute upper-left X:" | awk '{print $4}'`
if [ "$MONITORS" -eq "2" ]; then
#monitor 1 X size and Y size
X_RES1=`xrandr | grep "*+" | awk '{print $1}' | cut -d'x' -f 1 | sed -n 1p`
Y_RES1=`xrandr | grep "*+" | awk '{print $1}' | cut -d'x' -f 2 | sed -n 1p`
#monitor 2 X size and Y size
X_RES2=`xrandr | grep "*+" | awk '{print $1}' | cut -d'x' -f 1 | sed -n 2p`
Y_RES2=`xrandr | grep "*+" | awk '{print $1}' | cut -d'x' -f 2 | sed -n 2p`
#desktop width
WIDTH=$((X_RES1+X_RES2))
# if [ "$Y_RES1" -ge "$Y_RES2" ]; then
# HEIGHT=$Y_RES1
# else
# HEIGHT=$Y_RES2
# fi
else
X_RES=`xrandr | grep "*+" | awk '{print $1}' | cut -d'x' -f 1 | sed -n 1p`
Y_RES=`xrandr | grep "*+" | awk '{print $1}' | cut -d'x' -f 2 | sed -n 1p`
#desktop width
WIDTH=$X_RES
#desktop height
HEIGHT=$Y_RES
fi
#get the desktop parameters
HEIGHT=`xwininfo -root | grep 'Height:' | awk '{print $2}'`
WIDTH=`xwininfo -root | grep 'Width:' | awk '{print $2}'`
#see what the user wants to do
case $1 in
"-l")
MODE="LEFT"
;;
"-r")
MODE="RIGHT"
;;
"-ll")
MODE="BIGLEFT"
;;
"-rr")
MODE="BIGRIGHT"
;;
"-t")
MODE="TOP"
;;
"-b")
MODE="BOTTOM"
;;
"-y")
MODE="TOP_LEFT"
;;
"-i")
MODE="TOP_RIGHT"
;;
"-n")
MODE="BOTTOM_LEFT"
;;
"-m")
MODE="BOTTOM_RIGHT"
;;
"-c")
MODE="CENTER"
;;
*)
MODE="LEFT"
;;
esac
case $MODE in
"LEFT")
if [ "$MONITORS" == "2" ]; then
# left monitor
if [ "$X_POS" -lt "$X_RES1" ]; then
X=$(($X_RES1/2))
Y=$(($Y_RES1-$GAP_BOTTOM-$GAP_TOP))
wmctrl -r :ACTIVE: -e "1,0,$GAP_TOP,$X,$Y"
#right monitor
else
X=$(($X_RES2/2))
Y=$(($Y_RES2-$GAP_BOTTOM-$GAP_TOP))
wmctrl -r :ACTIVE: -e "1,$X_RES1,$GAP_TOP,$X,$Y"
fi
else
X=$(($WIDTH/2))
Y=$(($HEIGHT-$GAP_BOTTOM-$GAP_TOP))
wmctrl -r :ACTIVE: -e "1,0,$GAP_TOP,$X,$Y"
fi
;;
"BIGLEFT")
if [ "$MONITORS" == "2" ]; then
# left monitor
if [ "$X_POS" -lt "$X_RES1" ]; then
#X=$(($X_RES1/2))
X=$((($X_RES1/3)*2))
Y=$(($Y_RES1-$GAP_BOTTOM-$GAP_TOP))
wmctrl -r :ACTIVE: -e "1,0,$GAP_TOP,$X,$Y"
#right monitor
else
#X=$(($X_RES2/2))
X=$((($X_RES2/3)*2))
Y=$(($Y_RES2-$GAP_BOTTOM-$GAP_TOP))
wmctrl -r :ACTIVE: -e "1,$X_RES1,$GAP_TOP,$X,$Y"
fi
else
X=$((($WIDTH/3)*2))
Y=$(($HEIGHT-$GAP_BOTTOM-$GAP_TOP))
wmctrl -r :ACTIVE: -e "1,0,$GAP_TOP,$X,$Y"
fi
;;
"RIGHT")
if [ "$MONITORS" == "2" ]; then
# left monitor
if [ "$X_POS" -lt "$X_RES1" ]; then
X=$(($X_RES1/2))
Y=$(($Y_RES1-$GAP_BOTTOM-$GAP_TOP))
wmctrl -r :ACTIVE: -e "1,$X,$GAP_TOP,$X,$Y"
#right monitor
else
X=$(($X_RES2/2))
Y=$(($Y_RES2-$GAP_BOTTOM-$GAP_TOP))
wmctrl -r :ACTIVE: -e "1,$(($X+$X_RES1)),$GAP_TOP,$X,$Y"
fi
else
X=$(($WIDTH/2))
Y=$(($HEIGHT-$GAP_BOTTOM-$GAP_TOP))
wmctrl -r :ACTIVE: -e "1,$X,$GAP_TOP,$X,$Y"
fi
;;
"BIGRIGHT")
if [ "$MONITORS" == "2" ]; then
# left monitor
if [ "$X_POS" -lt "$X_RES1" ]; then
X=$(($X_RES1/3))
XX=$((($X_RES1/3)*2))
Y=$(($Y_RES1-$GAP_BOTTOM-$GAP_TOP))
wmctrl -r :ACTIVE: -e "1,$X,$GAP_TOP,$XX,$Y"
#right monitor
#echo "wmctrl -r :ACTIVE: -e 1,$X,$GAP_TOP,$XX,$Y"
else
X=$(($X_RES2/3))
XX=$((($X_RES2/3)*2))
Y=$(($Y_RES2-$GAP_BOTTOM-$GAP_TOP))
wmctrl -r :ACTIVE: -e "1,$(($X+$X_RES1)),$GAP_TOP,$XX,$Y"
#echo "wmctrl -r :ACTIVE: -e 1,$(($X+$X_RES1)),$GAP_TOP,$XX,$Y"
fi
else
X=$(($WIDTH/3))
XX=$((($WIDTH/3)*2))
Y=$(($HEIGHT-$GAP_BOTTOM-$GAP_TOP))
wmctrl -r :ACTIVE: -e "1,$X,$GAP_TOP,$XX,$Y"
#echo "wmctrl -r :ACTIVE: -e 1,$X,$GAP_TOP,$XX,$Y"
fi
;;
"TOP")
if [ "$MONITORS" == "2" ]; then
# left monitor
if [ "$X_POS" -lt "$X_RES1" ]; then
X=$X_RES1
Y=$(($Y_RES1/2))
wmctrl -r :ACTIVE: -e "1,0,$GAP_TOP,$X,$(($Y-$GAP_BOTTOM-$GAP_TOP))"
#right monitor
else
X=$X_RES2
Y=$(($Y_RES2/2))
wmctrl -r :ACTIVE: -e "1,$X,$GAP_TOP,$X,$Y"
fi
else
X=$WIDTH
Y=$(($HEIGHT/2-$GAP_BOTTOM-$GAP_TOP))
wmctrl -r :ACTIVE: -e "1,0,$GAP_TOP,$X,$Y"
fi
;;
"BOTTOM")
if [ "$MONITORS" == "2" ]; then
# left monitor
if [ "$X_POS" -lt "$X_RES1" ]; then
X=$X_RES1
Y=$(($Y_RES1/2))
wmctrl -r :ACTIVE: -e "1,0,$(($Y-$GAP_TOP)),$X,$(($Y-$GAP_BOTTOM))"
#right monitor
else
X=$X_RES2
Y=$(($Y_RES2/2))
wmctrl -r :ACTIVE: -e "1,$X,$Y,$X,$(($Y-$GAP_BOTTOM))"
fi
else
X=$WIDTH
Y=$(($HEIGHT/2))
wmctrl -r :ACTIVE: -e "1,0,$Y,$X,$(($Y-$GAP_BOTTOM))"
fi
;;
"TOP_LEFT")
if [ "$MONITORS" == "2" ]; then
# left monitor
if [ "$X_POS" -lt "$X_RES1" ]; then
X=$(($X_RES1/2))
Y=$(($Y_RES1/2))
wmctrl -r :ACTIVE: -e "1,0,$GAP_TOP,$X,$Y"
#right monitor
else
X=$X_RES1
Y=$(($Y_RES2/2))
wmctrl -r :ACTIVE: -e "1,$X,$GAP_BOTTOM,$(($X_RES2/2)),$Y"
fi
else
X=$(($WIDTH/2))
Y=$(($HEIGHT/2))
wmctrl -r :ACTIVE: -e "1,0,$GAP_TOP,$X,$Y"
fi
;;
"TOP_RIGHT")
if [ "$MONITORS" == "2" ]; then
# left monitor
if [ "$X_POS" -lt "$X_RES1" ]; then
X=$(($X_RES1/2))
Y=$(($Y_RES1/2))
wmctrl -r :ACTIVE: -e "1,$X,$GAP_TOP,$X,$Y"
#right monitor
else
X=$X_RES1
Y=$(($Y_RES2/2))
wmctrl -r :ACTIVE: -e "1,$(($X+($X_RES2/2))),$GAP_TOP,$(($X_RES2/2)),$Y"
fi
else
X=$((2))
Y=$(($HEIGHT/2))
wmctrl -r :ACTIVE: -e "1,$X,$GAP_TOP,$X,$Y"
fi
;;
"BOTTOM_LEFT")
if [ "$MONITORS" == "2" ]; then
# left monitor
if [ "$X_POS" -lt "$X_RES1" ]; then
X=$(($X_RES1/2))
Y=$(($Y_RES1/2))
wmctrl -r :ACTIVE: -e "1,0,$(($Y-$GAP_BOTTOM-$GAP_TOP)),$X,$Y"
#right monitor
else
X=$X_RES1
Y=$(($Y_RES2/2))
wmctrl -r :ACTIVE: -e "1,$X,$(($Y-$GAP_BOTTOM-$GAP_TOP)),$(($X_RES2/2)),$Y"
fi
else
X=$(($WIDTH/2))
Y=$(($HEIGHT/2))
wmctrl -r :ACTIVE: -e "1,0,$(($Y-$GAP_BOTTOM)),$X,$Y"
fi
;;
"BOTTOM_RIGHT")
if [ "$MONITORS" == "2" ]; then
# left monitor
if [ "$X_POS" -lt "$X_RES1" ]; then
X=$(($X_RES1/2))
Y=$(($Y_RES1/2))
wmctrl -r :ACTIVE: -e "1,$X,$(($Y-$GAP_BOTTOM-$GAP_TOP)),$X,$Y"
#right monitor
else
X=$(($X_RES1+($X_RES2/2)))
Y=$(($Y_RES2/2))
wmctrl -r :ACTIVE: -e "1,$X,$(($Y-$GAP_BOTTOM-$GAP_TOP)),$(($X_RES2/2)),$Y"
fi
else
X=$(($WIDTH/2))
Y=$(($HEIGHT/2))
wmctrl -r :ACTIVE: -e "1,$X,$(($Y-$GAP_BOTTOM)),$X,$Y"
fi
;;
"CENTER")
if [ "$MONITORS" == "2" ]; then
# left monitor
if [ "$X_POS" -lt "$X_RES1" ]; then
X=$X_RES1
Y=$Y_RES1
wmctrl -r :ACTIVE: -e "1,0,$GAP_TOP,$X,$(($Y-$GAP_BOTTOM-$GAP_TOP))"
#right monitor
else
X=$X_RES1
Y=$Y_RES2
wmctrl -r :ACTIVE: -e "1,$X,$GAP_TOP,$X_RES2,$(($Y-$GAP_BOTTOM-$GAP_TOP))"
fi
else
X=$WIDTH
Y=$(($HEIGHT-$GAP_BOTTOM-$GAP_TOP))
wmctrl -r :ACTIVE: -e "1,0,$GAP_TOP,$X,$Y"
fi
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment