Skip to content

Instantly share code, notes, and snippets.

@jpouellet
Last active October 13, 2016 03:58
Show Gist options
  • Select an option

  • Save jpouellet/48181bcdc0afbcdffef84bd1e16fa021 to your computer and use it in GitHub Desktop.

Select an option

Save jpouellet/48181bcdc0afbcdffef84bd1e16fa021 to your computer and use it in GitHub Desktop.
ShiftIt for X11 (deps: EWMH-compliant WM, xprop, wmctrl) -- see https://github.com/fikovnik/ShiftIt
#!/usr/bin/env perl
use strict;
use warnings;
use POSIX qw(floor ceil);
my %geoms = (
'full' => '(0,$x,$y,$w,$h)',
'left' => '(0,$x,$y,$w/2,$h)',
'right' => '(0,$x+$w/2,$y,$w/2,$h)',
'top' => '(0,$x,$y,$w,$h/2)',
'bottom' => '(0,$x,$y+$h/2,$w,$h/2)',
'1' => '(0,$x,$y,$w/2,$h/2)',
'2' => '(0,$x+$w/2,$y,$w/2,$h/2)',
'3' => '(0,$x,$y+$h/2,$w/2,$h/2)',
'4' => '(0,$x+$w/2,$y+$h/2,$w/2,$h/2)',
);
sub usage {
die "Usage: $0 position [window_id]\n".
"\twhere position is one of: ".join(', ', sort keys %geoms)."\n";
}
defined $ARGV[0] or usage;
exists $geoms{$ARGV[0]} or usage;
my $id;
if (defined $ARGV[1]) {
$id = $ARGV[1];
} else {
$_ = `xprop -notype -root _NET_ACTIVE_WINDOW`;
/^_NET_ACTIVE_WINDOW: window id # (0x[0-9a-f]+), 0x0$/m or die 'unable to get active window\n';
$id = $1;
}
$_ = `xprop -id "$id" -notype _NET_FRAME_EXTENTS`;
/^_NET_FRAME_EXTENTS = (\d+), (\d+), (\d+), (\d+)$/m or die 'unable to get frame extents';
my ($left, $right, $top, $bottom) = ($1, $2, $3, $4);
$_ = `wmctrl -d`;
/^(?<ws>\d+)\s+\*\sDG:\s+(?<dgw>\d+)x(?<dgh>\d+)\s+VP:\s+(?<vpx>\d+),(?<vpy>\d+)\s+WA:\s+(?<wax>\d+),(?<way>\d+)\s+(?<waw>\d+)x(?<wah>\d+)\s+(?<name>.*)$/m or die 'unable to get desktop geometry\n';
my ($x, $y, $w, $h) = ($+{wax}, $+{way}, $+{waw}, $+{wah});
my @geom = eval $geoms{$ARGV[0]};
# round windows away from each other
@geom = ($geom[0], ceil($geom[1]), ceil($geom[2]), floor($geom[3]), floor($geom[4]));
# adjust for window border
$geom[3] -= $left + $right;
$geom[4] -= $top + $bottom;
# resize window
exec {'wmctrl'} ('wmctrl', '-ir', $id, '-e', join(',', @geom));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment