Advertisement
Guest User

winfuncs.sh

a guest
Nov 8th, 2021
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 13.97 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #todo:
  4. # cancel for tile function
  5. # determine what windows are maximized and re-max after the "window select" function
  6. # determine what windows are non-resizable by the user so that the script doesn't resize them
  7. # cascade also shaded windows
  8.  
  9. # winfuncs.sh select
  10. # winfuncs.sh tile
  11. # winfuncs.sh tiletwo
  12. # winfuncs.sh tiletwol
  13. # winfuncs.sh tiletwor
  14. # winfuncs.sh tilethree
  15. # winfuncs.sh tilethreev
  16. # winfuncs.sh stacktwo
  17. # winfuncs.sh cascade
  18. # winfuncs.sh showdesktop
  19. # winfuncs.sh maximize_with_gaps
  20. # winfuncs.sh small_win_fifty
  21. # winfuncs.sh small_win_seventy
  22.  
  23. # set gaps (0 removes gaps)
  24. outer_gaps=20
  25. inner_gaps=10
  26.  
  27. # set gaps for 'select' mode
  28. expose_gaps=20
  29.  
  30. # set desktop dimensions
  31. display_width=$(xdotool getdisplaygeometry | cut -d" " -f1)
  32. display_height=$(xdotool getdisplaygeometry | cut -d" " -f2)
  33.  
  34. # desktop height without panel(s)
  35. desktop_height=$(xprop -root _NET_WORKAREA | awk '{ print $6 }' | cut -d"," -f1)
  36.  
  37. # window decorations
  38. window_id=$(xdotool getactivewindow)
  39. titlebar_offset=$(xwininfo -id "$window_id" | awk '/Relative upper-left Y:/ { print $4 }')
  40.  
  41. # top panel
  42. top_bar=$(xprop -root _NET_WORKAREA | awk '{ print $4 }' | cut -d"," -f1)
  43.  
  44. # bottom panel (not needed)
  45. bottom_bar=`expr $display_height - $desktop_height - $top_bar`
  46.  
  47. function get_desktop_dim { 
  48.     if (( ${#DIM[@]} == 0 )) ; then        
  49.         DIM=(`expr $display_width - $outer_gaps \* 2` `expr $desktop_height - $outer_gaps \* 2`)
  50.     fi
  51. }
  52.  
  53. # which workspace we're on
  54. function get_workspace {
  55.     if [[ "$DTOP" == "" ]] ; then
  56.         DTOP=`xdotool get_desktop`
  57.     fi
  58. }
  59.  
  60. function is_desktop {
  61.     xwininfo -id "$*" | grep '"Desktop"'
  62.     return "$?"
  63. }
  64.  
  65. function get_visible_window_ids {
  66.     if (( ${#WDOWS[@]} == 0 )) ; then
  67.         WDOWS=(`xdotool search --desktop $DTOP --onlyvisible "" 2>/dev/null`)
  68.     fi
  69. }
  70.  
  71. function win_showdesktop {
  72.     get_workspace
  73.     get_visible_window_ids
  74.  
  75.     command="search --desktop $DTOP \"\""
  76.  
  77.     if (( ${#WDOWS[@]} > 0 )) ; then
  78.         command="$command windowminimize %@"
  79.     else
  80.         command="$command windowraise %@"
  81.     fi
  82.  
  83.     echo "$command" | xdotool -
  84. }
  85.  
  86. function win_tile_two {
  87.     get_desktop_dim
  88.  
  89.     wid1=`xdotool selectwindow 2>/dev/null`
  90.  
  91.     is_desktop "$wid1" && return
  92.  
  93.     wid2=`xdotool selectwindow 2>/dev/null`
  94.  
  95.     is_desktop "$wid2" && return
  96.  
  97.     half_w=`expr ${DIM[0]} / 2`
  98.     win_h=${DIM[1]}
  99.  
  100.     commands="windowsize $wid1 `expr $half_w - $inner_gaps` `expr $win_h - $titlebar_offset`"
  101.     commands="$commands windowsize $wid2 `expr $half_w - $inner_gaps` `expr $win_h - $titlebar_offset`"
  102.     commands="$commands windowmove $wid1 $outer_gaps `expr $top_bar + $outer_gaps`"
  103.     commands="$commands windowmove $wid2 `expr $half_w + $outer_gaps + $inner_gaps` `expr $top_bar + $outer_gaps`"
  104.     commands="$commands windowraise $wid1"
  105.     commands="$commands windowraise $wid2"
  106.  
  107.     wmctrl -i -r $wid1 -b remove,maximized_vert,maximized_horz
  108.     wmctrl -i -r $wid2 -b remove,maximized_vert,maximized_horz
  109.  
  110.     echo "$commands" | xdotool -
  111. }
  112.  
  113. function win_tile_two_left {
  114.     get_desktop_dim
  115.  
  116.     wid1=`xdotool selectwindow 2>/dev/null`
  117.  
  118.     is_desktop "$wid1" && return
  119.  
  120.     wid2=`xdotool selectwindow 2>/dev/null`
  121.  
  122.     is_desktop "$wid2" && return
  123.  
  124.     half_w=`expr ${DIM[0]} / 3`
  125.     win_h=${DIM[1]}
  126.  
  127.     commands="windowsize  $wid1 `expr $half_w \* 2 - $inner_gaps` `expr $win_h - $titlebar_offset`"
  128.     commands="$commands windowsize $wid2 `expr $half_w - $inner_gaps` `expr $win_h - $titlebar_offset`"
  129.     commands="$commands windowmove $wid1 $outer_gaps `expr $top_bar + $outer_gaps`"
  130.     commands="$commands windowmove $wid2 `expr $half_w \* 2 + $outer_gaps + $inner_gaps` `expr $top_bar + $outer_gaps`"
  131.     commands="$commands windowraise $wid1"
  132.     commands="$commands windowraise $wid2"
  133.  
  134.     wmctrl -i -r $wid1 -b remove,maximized_vert,maximized_horz
  135.     wmctrl -i -r $wid2 -b remove,maximized_vert,maximized_horz
  136.  
  137.     echo "$commands" | xdotool -
  138. }
  139.  
  140. function win_tile_two_right {
  141.     get_desktop_dim
  142.  
  143.     wid1=`xdotool selectwindow 2>/dev/null`
  144.  
  145.     is_desktop "$wid1" && return
  146.  
  147.     wid2=`xdotool selectwindow 2>/dev/null`
  148.  
  149.     is_desktop "$wid2" && return
  150.  
  151.     half_w=`expr ${DIM[0]} / 3`
  152.     win_h=${DIM[1]}
  153.  
  154.     commands="windowsize  $wid1 `expr $half_w - $inner_gaps` `expr $win_h - $titlebar_offset`"
  155.     commands="$commands windowsize $wid2 `expr $half_w \* 2 - $inner_gaps` `expr $win_h - $titlebar_offset`"
  156.     commands="$commands windowmove $wid1 $outer_gaps `expr $top_bar + $outer_gaps`"
  157.     commands="$commands windowmove $wid2 `expr $half_w + $outer_gaps + $inner_gaps` `expr $top_bar + $outer_gaps`"
  158.     commands="$commands windowraise $wid1"
  159.     commands="$commands windowraise $wid2"
  160.  
  161.     wmctrl -i -r $wid1 -b remove,maximized_vert,maximized_horz
  162.     wmctrl -i -r $wid2 -b remove,maximized_vert,maximized_horz
  163.  
  164.     echo "$commands" | xdotool -
  165. }
  166.  
  167. function win_stack_two {
  168.     get_desktop_dim
  169.  
  170.     wid1=`xdotool selectwindow 2>/dev/null`
  171.  
  172.     is_desktop "$wid1" && return
  173.  
  174.     wid2=`xdotool selectwindow 2>/dev/null`
  175.  
  176.     is_desktop "$wid2" && return
  177.  
  178.     win_w=${DIM[0]}
  179.         half_h=`expr ${DIM[1]} / 2`
  180.  
  181.     commands="windowsize $wid1 `expr $win_w` `expr $half_h - $titlebar_offset - $inner_gaps`"
  182.     commands="$commands windowsize $wid2 `expr $win_w` `expr $half_h - $titlebar_offset - $inner_gaps`"
  183.     commands="$commands windowmove $wid1 $outer_gaps `expr $top_bar + $outer_gaps`"
  184.     commands="$commands windowmove $wid2 $outer_gaps `expr $half_h + $top_bar + $outer_gaps + $inner_gaps`"
  185.     commands="$commands windowraise $wid1"
  186.     commands="$commands windowraise $wid2"
  187.  
  188.     wmctrl -i -r $wid1 -b remove,maximized_vert,maximized_horz
  189.     wmctrl -i -r $wid2 -b remove,maximized_vert,maximized_horz
  190.  
  191.     echo "$commands" | xdotool -
  192. }
  193.  
  194.  
  195. function win_tile_three {
  196.     get_desktop_dim
  197.  
  198.     wid1=`xdotool selectwindow 2>/dev/null`
  199.  
  200.     is_desktop "$wid1" && return
  201.  
  202.     wid2=`xdotool selectwindow 2>/dev/null`
  203.  
  204.     is_desktop "$wid2" && return
  205.    
  206.     wid3=`xdotool selectwindow 2>/dev/null`
  207.  
  208.     is_desktop "$wid3" && return
  209.  
  210.         win_h=${DIM[1]}
  211.         half_w=`expr ${DIM[0]} / 2`
  212.     half_h=`expr ${win_h} / 2`
  213.  
  214.     commands="windowsize $wid1 `expr $half_w - $inner_gaps` `expr $win_h - $titlebar_offset`"
  215.     commands="$commands windowsize $wid2 `expr $half_w - $inner_gaps` `expr $half_h - $titlebar_offset - $inner_gaps`"
  216.     commands="$commands windowsize $wid3 `expr $half_w - $inner_gaps` `expr $half_h - $titlebar_offset - $inner_gaps`"
  217.     commands="$commands windowmove $wid1 $outer_gaps `expr $top_bar + $outer_gaps`"
  218.     commands="$commands windowmove $wid2 `expr $half_w + $outer_gaps + $inner_gaps` `expr $top_bar + $outer_gaps`"
  219.     commands="$commands windowmove $wid3 `expr $half_w + $outer_gaps + $inner_gaps` `expr $half_h + $top_bar + $outer_gaps + $inner_gaps`"
  220.     commands="$commands windowraise $wid1"
  221.     commands="$commands windowraise $wid2"
  222.     commands="$commands windowraise $wid3"
  223.  
  224.     wmctrl -i -r $wid1 -b remove,maximized_vert,maximized_horz
  225.     wmctrl -i -r $wid2 -b remove,maximized_vert,maximized_horz
  226.     wmctrl -i -r $wid3 -b remove,maximized_vert,maximized_horz
  227.  
  228.     echo "$commands" | xdotool -
  229. }
  230.  
  231. function win_tile_three_v {
  232.     get_desktop_dim
  233.  
  234.     wid1=`xdotool selectwindow 2>/dev/null`
  235.  
  236.     is_desktop "$wid1" && return
  237.  
  238.     wid2=`xdotool selectwindow 2>/dev/null`
  239.  
  240.     is_desktop "$wid2" && return
  241.    
  242.     wid3=`xdotool selectwindow 2>/dev/null`
  243.  
  244.     is_desktop "$wid3" && return
  245.  
  246.         win_h=${DIM[1]}
  247.         half_w=`expr ${DIM[0]} / 3`
  248.  
  249.     commands="windowsize $wid1 `expr $half_w - $inner_gaps` `expr $win_h - $titlebar_offset`"
  250.     commands="$commands windowsize $wid2 `expr $half_w - $inner_gaps` `expr $win_h - $titlebar_offset`"
  251.     commands="$commands windowsize $wid3 `expr $half_w - $inner_gaps` `expr $win_h - $titlebar_offset`"
  252.     commands="$commands windowmove $wid1 $outer_gaps `expr $top_bar + $outer_gaps`"
  253.     commands="$commands windowmove $wid2 `expr $half_w + $outer_gaps + $inner_gaps / 2` `expr $top_bar + $outer_gaps`"
  254.     commands="$commands windowmove $wid3 `expr $half_w \* 2 + $outer_gaps + $inner_gaps` `expr $top_bar + $outer_gaps`"
  255.     commands="$commands windowraise $wid1"
  256.     commands="$commands windowraise $wid2"
  257.     commands="$commands windowraise $wid3"
  258.  
  259.     wmctrl -i -r $wid1 -b remove,maximized_vert,maximized_horz
  260.     wmctrl -i -r $wid2 -b remove,maximized_vert,maximized_horz
  261.     wmctrl -i -r $wid3 -b remove,maximized_vert,maximized_horz
  262.  
  263.     echo "$commands" | xdotool -
  264. }
  265.  
  266. function maximize_with_gaps {
  267.   get_desktop_dim
  268.    
  269.   wid=`xdotool getwindowfocus`
  270.    
  271.     win_w=`expr ${DIM[0]}`
  272.     win_h=`expr ${DIM[1]}`
  273.  
  274.   gaps_win_w=`expr $win_w - $outer_gaps \* 2`
  275.   gaps_win_h=`expr $win_h - $titlebar_offset - $top_bar - $outer_gaps \* 2`
  276.  
  277.   pos_x=$(( (display_width - gaps_win_w) / 2 ))
  278.   pos_y=$(( (display_height - gaps_win_h) / 2 ))
  279.  
  280.   wmctrl -i -r $wid -b remove,maximized_vert,maximized_horz
  281.  
  282.   x=0; y=0;
  283.  
  284.   commands="$commands windowsize $wid $gaps_win_w $gaps_win_h"
  285.   commands="$commands windowmove $wid $pos_x $pos_y"
  286.  
  287.   echo "$commands" | xdotool -
  288. }
  289.  
  290. function resize_to_percentage {
  291.   get_desktop_dim
  292.    
  293.   wid=`xdotool getwindowfocus`
  294.  
  295.   small_win_w=$((display_width * $1 / 100))
  296.   small_win_h=$(($display_height * $1 / 100))
  297.  
  298.   pos_x=$(( (display_width - small_win_w) / 2 ))
  299.   pos_y=$(( (display_height - small_win_h) / 2 ))
  300.  
  301.   wmctrl -i -r $wid -b remove,maximized_vert,maximized_horz
  302.  
  303.   x=0; y=0;
  304.  
  305.   commands="$commands windowsize $wid $small_win_w $small_win_h"
  306.   commands="$commands windowmove $wid $pos_x $pos_y"
  307.  
  308.   echo "$commands" | xdotool -
  309. }
  310.  
  311. function win_tile {
  312.     get_workspace
  313.     get_visible_window_ids
  314.  
  315.     (( ${#WDOWS[@]} < 1 )) && return;
  316.  
  317.     get_desktop_dim
  318.  
  319.     # determine how many rows and columns we need
  320.     cols=`echo "sqrt(${#WDOWS[@]})" | bc`
  321.     rows=$cols
  322.     wins=`expr $rows \* $cols`
  323.  
  324.     if (( "$wins" < "${#WDOWS[@]}" )) ; then
  325.         cols=`expr $cols + 1`
  326.         wins=`expr $rows \* $cols`
  327.         if (( "$wins" < "${#WDOWS[@]}" )) ; then
  328.         rows=`expr $rows + 1`
  329.         wins=`expr $rows \* $cols`
  330.         fi
  331.     fi
  332.  
  333.     (( $cols < 1 )) && cols=1;
  334.     (( $rows < 1 )) && rows=1;
  335.  
  336.     win_w=`expr ${DIM[0]} / $cols`
  337.     win_h=`expr ${DIM[1]} / $rows`
  338.  
  339.     # do tiling
  340.     x=0; y=0; commands=""
  341.     for window in ${WDOWS[@]} ; do
  342.         wmctrl -i -r $window -b remove,maximized_vert,maximized_horz
  343.  
  344.         commands="$commands windowsize $window `expr $win_w - $inner_gaps \* 2` `expr $win_h - $titlebar_offset - $inner_gaps \* 2`"
  345.         commands="$commands windowmove $window `expr $x \* $win_w + $outer_gaps` `expr $y \* $win_h + $top_bar + $outer_gaps`"
  346.  
  347.         x=`expr $x + 1`
  348.         if (( $x > `expr $cols - 1` )) ; then
  349.                   x=0
  350.                   y=`expr $y + 1`
  351.         fi
  352.     done
  353.  
  354.     echo "$commands" | xdotool -
  355. }
  356.  
  357. function expose {
  358.     get_workspace
  359.     get_visible_window_ids
  360.  
  361.     (( ${#WDOWS[@]} < 1 )) && return;
  362.  
  363.     get_desktop_dim
  364.  
  365.     # determine how many rows and columns we need
  366.     cols=`echo "sqrt(${#WDOWS[@]})" | bc`
  367.     rows=$cols
  368.     wins=`expr $rows \* $cols`
  369.  
  370.     if (( "$wins" < "${#WDOWS[@]}" )) ; then
  371.         cols=`expr $cols + 1`
  372.         wins=`expr $rows \* $cols`
  373.         if (( "$wins" < "${#WDOWS[@]}" )) ; then
  374.         rows=`expr $rows + 1`
  375.         wins=`expr $rows \* $cols`
  376.         fi
  377.     fi
  378.  
  379.     (( $cols < 1 )) && cols=1;
  380.     (( $rows < 1 )) && rows=1;
  381.  
  382.     win_w=`expr ${DIM[0]} / $cols`
  383.     win_h=`expr ${DIM[1]} / $rows`
  384.        
  385.     # do tiling
  386.     x=0; y=0; commands=""
  387.     for window in ${WDOWS[@]} ; do
  388.         wmctrl -i -r $window -b remove,maximized_vert,maximized_horz
  389.  
  390.         commands="$commands windowsize $window `expr $win_w - $expose_gaps \* 2` `expr $win_h - $titlebar_offset - $expose_gaps \* 2`"
  391.         commands="$commands windowmove $window `expr $x \* $win_w + $expose_gaps` `expr $y \* $win_h + $top_bar + $expose_gaps`"
  392.  
  393.         x=`expr $x + 1`
  394.         if (( $x > `expr $cols - 1` )) ; then
  395.                   x=0
  396.                   y=`expr $y + 1`
  397.         fi
  398.     done
  399.  
  400.     echo "$commands" | xdotool -
  401. }
  402.  
  403. function win_cascade {
  404.     get_workspace
  405.     get_visible_window_ids
  406.  
  407.     (( ${#WDOWS[@]} < 1 )) && return;
  408.  
  409.     x=0; y=0; commands=""
  410.     for window in ${WDOWS[@]} ; do
  411.         wmctrl -i -r $window -b remove,maximized_vert,maximized_horz
  412.  
  413.         commands="$commands windowsize $window 1024 640"
  414.         commands="$commands windowmove $window `expr $x + $outer_gaps` `expr $y + $top_bar + $outer_gaps`"
  415.  
  416.         x=`expr $x + 100`
  417.       y=`expr $y + 80`
  418.     done
  419.  
  420.     echo "$commands" | xdotool -
  421. }
  422.  
  423. function win_select {
  424.     get_workspace
  425.     get_visible_window_ids
  426.  
  427.     (( ${#WDOWS[@]} < 1 )) && return;
  428.  
  429.     # store window positions and widths
  430.     i=0
  431.     for window in ${WDOWS[@]} ; do
  432.         GEO=`xdotool getwindowgeometry $window | grep Geometry | sed 's/.* \([0-9].*\)/\1/g'`;
  433.         height[$i]=`echo $GEO | sed 's/\(.*\)x.*/\1/g'`
  434.         width[$i]=`echo $GEO | sed 's/.*x\(.*\)/\1/g'`
  435.  
  436.         # ( xwininfo gives position not ignoring titlebars and borders, unlike xdotool )
  437.         POS=(`xwininfo -stats -id $window | grep 'geometry ' | sed 's/.*[-+]\([0-9]*[-+][0-9*]\)/\1/g' | sed 's/[+-]/ /g'`)
  438.         posx[$i]=${POS[0]}
  439.         posy[$i]=${POS[1]}
  440.  
  441.         i=`expr $i + 1`
  442.     done
  443.  
  444.     # tile windows
  445.     expose
  446.  
  447.     # select a window
  448.     wid=`xdotool selectwindow 2>/dev/null`
  449.  
  450.     is_desktop "$wid" && return
  451.  
  452.     # restore window positions and widths
  453.     i=0; commands=""
  454.     for (( i=0; $i<${#WDOWS[@]}; i++ )) ; do
  455.         commands="$commands windowsize ${WDOWS[i]} ${height[$i]} ${width[$i]}"
  456.         commands="$commands windowmove ${WDOWS[i]} ${posx[$i]} ${posy[$i]}"
  457.     done
  458.  
  459.     commands="$commands windowraise $wid"
  460.  
  461.     echo "$commands" | xdotool -
  462. }
  463.  
  464. for command in ${@} ; do
  465.     if [[ "$command" == "tile" ]] ; then
  466.         win_tile
  467.     elif [[ "$command" == "select" ]] ; then
  468.         win_select
  469.   elif [[ "$command" == "maximize_with_gaps" ]] ; then
  470.     maximize_with_gaps
  471.   elif [[ "$command" == "small_win_fifty" ]] ; then
  472.     resize_to_percentage 55
  473.   elif [[ "$command" == "small_win_seventy" ]] ; then
  474.     resize_to_percentage 75
  475.     elif [[ "$command" == "tiletwo" ]] ; then
  476.         win_tile_two
  477.     elif [[ "$command" == "tiletwol" ]] ; then
  478.         win_tile_two_left
  479.     elif [[ "$command" == "tiletwor" ]] ; then
  480.         win_tile_two_right
  481.     elif [[ "$command" == "stacktwo" ]] ; then
  482.         win_stack_two
  483.     elif [[ "$command" == "tilethree" ]] ; then
  484.         win_tile_three
  485.     elif [[ "$command" == "tilethreev" ]] ; then
  486.         win_tile_three_v
  487.         elif [[ "$command" == "cascade" ]] ; then
  488.         win_cascade
  489.     elif [[ "$command" == "showdesktop" ]] ; then
  490.         win_showdesktop
  491.     fi
  492. done
  493.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement