Guest User

Untitled

a guest
Feb 21st, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.55 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # Written by Mason Loring Bliss.
  4. # This program is explicitly in the public domain.
  5.  
  6. use strict;
  7. use warnings;
  8.  
  9. my $wmctrl = '/usr/bin/wmctrl';
  10. my $xprop = '/usr/bin/xprop';
  11.  
  12. my $aw;
  13. my $ad;
  14. my @potentials;
  15. my %overlaps;
  16. my %w;
  17.  
  18. my $command = shift || 'nothing';
  19. if ($command ne "left" && $command ne "right"
  20.     && $command ne "up" && $command ne "down") {
  21.     print "Sorry, need one of 'left', 'right', 'up', or 'down'.\n";
  22.     exit 1;
  23. }
  24.  
  25. if (! -f $xprop) {
  26.     print "Sorry, we need $xprop to continue.\n";
  27.     exit 1;
  28. }
  29.  
  30. if (! -f $wmctrl) {
  31.     print "Sorry, we need $wmctrl to continue.\n";
  32.     exit 1;
  33. }
  34.  
  35. # Find active window.
  36. open my $query, "$xprop -root|";
  37. while (<$query>) {
  38.     last if /_NET_ACTIVE_WINDOW/;
  39. }
  40. (undef, undef, undef, undef, $aw) = split;
  41. close $query;
  42. $aw = sprintf("0x%08x", hex($aw));
  43.  
  44. # Gather list of desktops, and note active desktop.
  45. open $query, "$wmctrl -d|";
  46. while (<$query>) {
  47.     my ($dnum, $starred, undef) = split;
  48.     if ($starred eq '*') {
  49.         $ad = $dnum;
  50.         last;
  51.     }
  52. }
  53. close $query;
  54.  
  55. # Gather list of windows on the active desktop, and their geometry.
  56. open $query, "$wmctrl -l -G|";
  57. while (<$query>) {
  58.     my ($win, $desktop, $x, $y, $w, $h, undef) = split;
  59.     next if $desktop ne $ad;
  60.     # XXX possibly consider excluding gnome-panel
  61.  
  62.     $w{$win}{x} = $x;
  63.     $w{$win}{y} = $y;
  64.     $w{$win}{w} = $w;
  65.     $w{$win}{h} = $h;
  66. }
  67. close $query;
  68.  
  69. # Identify windows to consider.
  70. if ($command eq 'left') {
  71.     @potentials = grep {$w{$_}{x} < $w{$aw}{x}} keys %w;
  72. } elsif ($command eq 'right') {
  73.     @potentials =
  74.             grep {$w{$_}{x} + $w{$_}{w} > $w{$aw}{x} + $w{$aw}{w}} keys %w;
  75. } elsif ($command eq 'up') {
  76.     @potentials = grep {$w{$_}{y} < $w{$aw}{y}} keys %w;
  77. } else { # $command eq 'down'
  78.     @potentials =
  79.             grep {$w{$_}{y} + $w{$_}{h} > $w{$aw}{y} + $w{$aw}{h}} keys %w;
  80. }
  81. for (@potentials) {
  82.     # Find x overlap.
  83.     if ($w{$_}{x} < $w{$aw}{x}
  84.             && $w{$_}{x} + $w{$_}{w} > $w{$aw}{x} + $w{$aw}{w}) {
  85.         $overlaps{$_}{x} = $w{$aw}{w};
  86.     } elsif ($w{$_}{x} > $w{$aw}{x}
  87.             && $w{$_}{x} + $w{$_}{w} < $w{$aw}{x} + $w{$aw}{w}) {
  88.         $overlaps{$_}{x} = $w{$aw}{w};
  89.     } elsif ($w{$_}{x} < $w{$aw}{x}) {
  90.         $overlaps{$_}{x} = $w{$_}{x} + $w{$_}{w} - $w{$aw}{x};
  91.     } else {
  92.         $overlaps{$_}{x} = $w{$aw}{x} + $w{$aw}{w} - $w{$_}{x};
  93.     }
  94.  
  95.     # Find y overlap.
  96.     if ($w{$_}{y} < $w{$aw}{y}
  97.             && $w{$_}{y} + $w{$_}{h} > $w{$aw}{y} + $w{$aw}{h}) {
  98.         $overlaps{$_}{y} = $w{$aw}{h};
  99.     } elsif ($w{$_}{y} > $w{$aw}{y}
  100.             && $w{$_}{y} + $w{$_}{h} < $w{$aw}{y} + $w{$aw}{h}) {
  101.         $overlaps{$_}{y} = $w{$aw}{h};
  102.     } elsif ($w{$_}{y} < $w{$aw}{y}) {
  103.         $overlaps{$_}{y} = $w{$_}{y} + $w{$_}{h} - $w{$aw}{y};
  104.     } else {
  105.         $overlaps{$_}{y} = $w{$aw}{y} + $w{$aw}{h} - $w{$_}{y};
  106.     }
  107.  
  108.     if ($command eq 'left' || $command eq 'right') {
  109.         $overlaps{$_}{value} = $overlaps{$_}{y} * 1.5 + $overlaps{$_}{x};
  110.     } else {
  111.         $overlaps{$_}{value} = $overlaps{$_}{y} + $overlaps{$_}{x} * 1.5;
  112.     }
  113. }
  114.  
  115. # If there are no potentials, don't move. Otherwise, find the largest value.
  116. if (@potentials) {
  117.     my $winner;
  118.     my $high_water_mark;
  119.     for (@potentials) {
  120.         $winner //= $_;
  121.         $high_water_mark //= $overlaps{$_}{value};
  122.         if ($overlaps{$_}{value} > $high_water_mark) {
  123.             $winner = $_;
  124.             $high_water_mark = $overlaps{$_}{value};
  125.         }
  126.     }
  127.     system("$wmctrl -i -a $winner");
  128. }
Add Comment
Please, Sign In to add comment