1

I wrote a couple of scripts to maximize a window to half the size of the screen (to make it easy to place windows side-by-side) using xrandr, grep, and wmctrl as follows:

#!/bin/bash

w=`xrandr 2> /dev/null | grep '*' | grep -Po '\d+(?=x)'`
h=`xrandr 2> /dev/null | grep '*' | grep -Po '(?<=x)\d+'`
wmctrl -r :ACTIVE: -b remove,maximized_horz,maximized,vert
wmctrl -r :ACTIVE: -e 0,0,0,$((w / 2)),$h

Is there a way to do this more natively? The script works well on my desktop, but on my laptop there's a half-second lag that is kind of annoying.

jonderry
  • 23,013
  • 32
  • 104
  • 171
  • can you show us the output from xrandr? It should be possible to re-write this as w_h=$( xrander ...| nawk '....') thus reducing the number of processes started significantly. But, I am skeptical that it will make much difference in what you experience. 1/2 sec lag sounds like OS paging that you may not be able to eliminate. – shellter Mar 24 '11 at 23:29
  • The xrandr output looks roughly like ` 1280x1024 0.0*` and then some additional lines, not starred. – jonderry Mar 25 '11 at 00:08

1 Answers1

1

test code

# w_h="$(print -- "1280x1024 0.0*" | awk '/.*\*$/{sub(/ .*$/, "");sub("x"," ");$1=$1/2 ;print}')"
# w="${w_h% *}" ; h="${w_h#* }"

actual code

# awk matches only line ending with '*', remove everything from last space to EOL, replace X with " "
# w_H looks like "640 1040", below splits on space char populating correct var
w_h="$(xrandr | awk '/\*/{sub(/[0-9\.*\+]*$/, ""); sub("x", " "); $1=$1/2; print}')"
w="${w_h% *}" ; h="${w_h#* }"
wmctrl -r :ACTIVE: -b remove,maximized_horz,maximized,vert
wmctrl -r :ACTIVE: -e 0,0,0,${w},${h}

Note that I have done the div on W inside awk. Also, backticks are deprecated in posix shells. Make your life easier and use $() for command substituton ;-)

I hope this helps.

shellter
  • 36,525
  • 7
  • 83
  • 90
  • I had to adapt this code slightly to get it to work with my xrandr output (and removing the maximized state is necessary to be able to resize the window), but it did get the running time of the script down to 0.253 seconds from 0.574. My goal is to get it to 0.1 seconds so the lag is imperceptible. – jonderry Mar 26 '11 at 18:23
  • @jonderry : 1. can you post the change you made, I will incorporate it into the solution. 2. so you're saying the first line is Not a comment. Can't you just munge the two lines together?wmctrl ... -b remove... -e 0,0... ?. There is a program awkcc that will take the awk code and turn it into C code that you can then compile, possibly reducing your run time some more, but I think you have to compile awkcc before you can do that. – shellter Mar 26 '11 at 19:34
  • #!/bin/bash w_h=$(xrandr | awk '/\*/{sub(/[0-9\.\*\+]*$/, ""); sub("x", " "); $1=$1/2; print}') w=${w_h% *} ; h=${w_h#* } wmctrl -r :ACTIVE: -b remove,maximized_horz,maximized,vert wmctrl -r :ACTIVE: -e 0,${w},0,${w},${h} – jonderry Mar 26 '11 at 20:04