Skip to content

Instantly share code, notes, and snippets.

@jonhoo
Last active June 3, 2016 21:58
Show Gist options
  • Save jonhoo/6053203 to your computer and use it in GitHub Desktop.
Save jonhoo/6053203 to your computer and use it in GitHub Desktop.
Script for slicing and aerosnapping X windows in an EWMH compatible WM
#!/bin/bash
if [[ $# -eq 0 ]]; then
echo "Usage: $0 top|right|bottom|left [maximized]"
exit 1
fi
active=`xprop -root _NET_ACTIVE_WINDOW | sed 's/^.*# //'`
if [[ $active == "0x0" ]]; then
echo "No active window found (_NET_ACTIVE_WINDOW == 0x0)"
exit 2
fi
ismh=`xwininfo -wm -id $active | grep "Maximized Horz"`
ismv=`xwininfo -wm -id $active | grep "Maximized Vert"`
if [[ $2 == "maximized" ]]; then
rootgeo=`xprop -root _NET_DESKTOP_GEOMETRY | sed 's/^.* = \([0-9]*\), \([0-9]*\)/\1 \2/'`
w=`echo "$rootgeo" | cut -d' ' -f1`
h=`echo "$rootgeo" | cut -d' ' -f2`
x=0
y=0
echo "Base geometry: ${w}x${h}+${x}+${y}"
else
geometry=`xwininfo -id $active`
w=`echo "$geometry" | grep "Width" | sed 's/^.*:\s*//'`
h=`echo "$geometry" | grep "Height" | sed 's/^.*:\s*//'`
x=`echo "$geometry" | grep "Absolute upper-left X" | sed 's/^.*:\s*//'`
y=`echo "$geometry" | grep "Absolute upper-left Y" | sed 's/^.*:\s*//'`
echo "Base geometry: ${w}x${h}+${x}+${y}"
rx=`echo "$geometry" | grep "Relative upper-left X" | sed 's/^.*:\s*//'`
ry=`echo "$geometry" | grep "Relative upper-left Y" | sed 's/^.*:\s*//'`
x=`echo $x-$rx | bc`
y=`echo $y-$ry | bc`
echo "Adjusted geometry: ${w}x${h}+${x}+${y}"
fi
if [[ $1 == "top" ]]; then
h=`printf %d $(echo "$h/2" | bc)`
elif [[ $1 == "bottom" ]]; then
h=`printf %d $(echo "$h/2" | bc)`
y=`printf %d $(echo "$y+$h" | bc)`
elif [[ $1 == "left" ]]; then
w=`printf %d $(echo "$w/2" | bc)`
elif [[ $1 == "right" ]]; then
w=`printf %d $(echo "$w/2" | bc)`
x=`printf %d $(echo "$x+$w" | bc)`
fi
echo "New geometry: ${w}x${h}+${x}+${y}"
wmctrl -r :ACTIVE: -e 0,$x,$y,$w,$h
@jonhoo
Copy link
Author

jonhoo commented Jul 22, 2013

Example pekwm config:

KeyPress = "Mod4 Left" { Actions = "Exec slicewin left maximized" }
KeyPress = "Mod4 Right" { Actions = "Exec slicewin right maximized" }
KeyPress = "Mod4 Up" { Actions = "Exec slicewin top maximized" }
KeyPress = "Mod4 Down" { Actions = "Exec slicewin bottom maximized" }
KeyPress = "Mod4 Shift Left" { Actions = "Exec slicewin left" }
KeyPress = "Mod4 Shift Right" { Actions = "Exec slicewin right" }
KeyPress = "Mod4 Shift Up" { Actions = "Exec slicewin top" }
KeyPress = "Mod4 Shift Down" { Actions = "Exec slicewin bottom" }

@jonhoo
Copy link
Author

jonhoo commented Jul 22, 2013

For "Aero Snap"-like functionality, append this to .pekwm/mouse:

ScreenEdge {
  Down {
    EnterMoving = "Any Any" { Actions = "Exec slicewin bottom maximized" }
  }
  Up {
    EnterMoving = "Any Any" { Actions = "Exec slicewin top maximized" }
  }
  Left {
    EnterMoving = "Any Any" { Actions = "Exec slicewin left maximized" }
  }
  Right {
    EnterMoving = "Any Any" { Actions = "Exec slicewin right maximized" }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment