Guest User

Untitled

a guest
Feb 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.58 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 $command = shift || 'nothing';
  13. if ($command ne "left" && $command ne "right"
  14.     && $command ne "up" && $command ne "down") {
  15.     print "Sorry, need one of 'left', 'right', 'up', or 'down'.\n";
  16.     exit 1;
  17. }
  18.  
  19. my $aw;
  20. my $ad;
  21. my $target_x;
  22. my $target_y;
  23. my %w;
  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. my %overlaps;
  71. my @potentials;
  72.  
  73. if ($command eq 'left') {
  74.     @potentials = grep {$w{$_}{x} < $w{$aw}{x}} keys %w;
  75. } elsif ($command eq 'right') {
  76.     @potentials =
  77.             grep {$w{$_}{x} + $w{$_}{w} > $w{$aw}{x} + $w{$aw}{w}} keys %w;
  78. } elsif ($command eq 'up') {
  79.     @potentials = grep {$w{$_}{y} < $w{$aw}{y}} keys %w;
  80. } else { # $command eq 'down'
  81.     @potentials =
  82.             grep {$w{$_}{y} + $w{$_}{h} > $w{$aw}{y} + $w{$aw}{h}} keys %w;
  83. }
  84. for (@potentials) {
  85.     # Find x overlap.
  86.     if ($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.             && $w{$_}{x} + $w{$_}{w} < $w{$aw}{x} + $w{$aw}{w}) {
  91.         $overlaps{$_}{x} = $w{$aw}{w};
  92.     } elsif ($w{$_}{x} < $w{$aw}{x}) {
  93.         $overlaps{$_}{x} = $w{$_}{x} + $w{$_}{w} - $w{$aw}{x};
  94.     } else {
  95.         $overlaps{$_}{x} = $w{$aw}{x} + $w{$aw}{w} - $w{$_}{x};
  96.     }
  97.  
  98.     # Find y overlap.
  99.     if ($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.             && $w{$_}{y} + $w{$_}{h} < $w{$aw}{y} + $w{$aw}{h}) {
  104.         $overlaps{$_}{y} = $w{$aw}{h};
  105.     } elsif ($w{$_}{y} < $w{$aw}{y}) {
  106.         $overlaps{$_}{y} = $w{$_}{y} + $w{$_}{h} - $w{$aw}{y};
  107.     } else {
  108.         $overlaps{$_}{y} = $w{$aw}{y} + $w{$aw}{h} - $w{$_}{y};
  109.     }
  110.  
  111.     if ($command eq 'left' || $command eq 'right') {
  112.         $overlaps{$_}{value} = $overlaps{$_}{y} * 1.5 + $overlaps{$_}{x};
  113.     } else {
  114.         $overlaps{$_}{value} = $overlaps{$_}{y} + $overlaps{$_}{x} * 1.5;
  115.     }
  116. }
  117.  
  118. # If there are no potentials, don't move. Otherwise, find the largest value.
  119. if (@potentials) {
  120.     my $winner;
  121.     my $high_water_mark;
  122.     for (@potentials) {
  123.         $winner //= $_;
  124.         $high_water_mark //= $overlaps{$_}{value};
  125.         if ($overlaps{$_}{value} > $high_water_mark) {
  126.             $winner = $_;
  127.             $high_water_mark = $overlaps{$_}{value};
  128.         }
  129.     }
  130.     system("$wmctrl -i -a $winner");
  131. }
Add Comment
Please, Sign In to add comment