Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Splitting X11/EWMH geometry strings

12 views
Skip to first unread message

Mirko K.

unread,
Jan 27, 2012, 7:12:42 PM1/27/12
to
Hello everybody.

I'm banging my head against this for the whole evening now (and
probably have a beer too much).

I need to split window geometry specifications as provided by
xwininfo. The problem is, that this happens with a viewport-based
window-manager (Compiz in this case) and the values are rather strange.

What I get from xwininfo looks like this:

# Easy case:
Corners: +395+228 -147+228 -147-330 +395-330

# Case where I despair:
Corners: +-1269+165 -1487+165 -1487--46 +-1269--46

I need to perform some calculation with the rectangles and thus need
the separate x and y values for the corners. So what I need is at
least like this:

+395 +228 -147 +228 -147 -330 +395 -330
+-1269 +165 -1487 +165 -1487 --46 +-1269 --46

But ultimately, I need something like

+ -1269 +165 -1487 +165 -1487 - -46 + -1269 - -46

Ie. I also want to split the +- and -- into separation.

I tried many awk and sed ways, but awk lacks back-referencing and
with sed I can't work with fields which makes the regex really complex.

I have a crude, lengthy and stupid sh+sed+cut+awk+... way, but I
look for something more clean and a little shorter.

Any suggestions?

Ed Morton

unread,
Jan 27, 2012, 7:27:19 PM1/27/12
to
How about you first surround all the +s and -s with a space:

$ cat file
# Easy case:
Corners: +395+228 -147+228 -147-330 +395-330

# Case where I despair:
Corners: +-1269+165 -1487+165 -1487--46 +-1269--46
$
$ sed -e 's/\([-+]\)/ \1 /g' file
# Easy case:
Corners: + 395 + 228 - 147 + 228 - 147 - 330 + 395 - 330

# Case where I despair:
Corners: + - 1269 + 165 - 1487 + 165 - 1487 - - 46 + - 1269 - - 46

and then get rid of all the extraneous spaces between the signs and the digits:

$ sed -e 's/\([-+]\)/ \1 /g' -e 's/\([-+]\) *\([0-9]\)/\1\2/g' file
# Easy case:
Corners: +395 +228 -147 +228 -147 -330 +395 -330

# Case where I despair:
Corners: + -1269 +165 -1487 +165 -1487 - -46 + -1269 - -46

and finally you can tidy up the multi-spaced parts if you like:

$ sed -e 's/\([-+]\)/ \1 /g' -e 's/\([-+]\) *\([0-9]\)/\1\2/g' -e 's/ */ /g' file
# Easy case:
Corners: +395 +228 -147 +228 -147 -330 +395 -330

# Case where I despair:
Corners: + -1269 +165 -1487 +165 -1487 - -46 + -1269 - -46

Regards,

Ed.

Ed Morton

unread,
Jan 27, 2012, 7:32:34 PM1/27/12
to
Actually if your data's as simple as you posted you probably just need this:

$ sed -e 's/\([-+]\)/ \1/g' file

Kaz Kylheku

unread,
Jan 27, 2012, 7:37:55 PM1/27/12
to
On 2012-01-28, Mirko K. <mirkok...@googlemail.com> wrote:
> What I get from xwininfo looks like this:
>
> # Easy case:
> Corners: +395+228 -147+228 -147-330 +395-330

These are not x geometry values, but evidently just a way of printing
out the corners. ++ means upper left, -- lower right, etc.
This notation is fixed.

> # Case where I despair:
> Corners: +-1269+165 -1487+165 -1487--46 +-1269--46

WTF is that? Here it looks like the same notation: ++ -+ -- +-
But some of the values are negative for whatever reason.

Corners: +(-1269)+(165) -(1487)+(165) -(1487)-(-46) +(-1269)-(-46)

((If this gives you grief, why don't you extract this same info from the other
output lines? xwininfo also puts out

Absolute upper-left X: <number>
Absolute upper-left Y: <number>
...
Width: <number>
Height: <number>

etc.))

> I tried many awk and sed ways, but awk lacks back-referencing and
> with sed I can't work with fields which makes the regex really complex.

TXR job (http://www.nongnu.org/txr):

$ cat xwin.txr
@(gather)
Width: @W
Height: @H
Depth: @D
Visual: @V
Visual Class: @VCLASS
Corners: +@X0+@Y0 -@X1+@Y0 -@X1-@Y1 +@X0-@Y1
@(end)

$ xwininfo | txr xwin.txr - # click on window
W="655"
H="435"
D="24"
V="0x21"
VCLASS="TrueColor"
X0="1"
Y0="53"
X1="616"
Y1="393"

Mirko K.

unread,
Jan 27, 2012, 7:48:08 PM1/27/12
to
On 28.01.2012 01:32, Ed Morton wrote:

> Actually if your data's as simple as you posted you probably just
> need this:
>
> $ sed -e 's/\([-+]\)/ \1/g' file
> # Easy case:
> Corners: +395 +228 -147 +228 -147 -330 +395 -330
>
> # Case where I despair:
> Corners: + -1269 +165 -1487 +165 -1487 - -46 + -1269 - -46
>
> Regards,
>
> Ed.

OH MY! I *did* know it's going to dead simple. Sometimes I should
just enjoy a longer rest, or is there another cure for acute
mind-blindness.

Does exactly what I want.

Thank you! :-)

Mirko K.

unread,
Jan 27, 2012, 8:25:05 PM1/27/12
to
On 28.01.2012 01:37, Kaz Kylheku wrote:
> On 2012-01-28, Mirko K.<mirkok...@googlemail.com> wrote:
>> What I get from xwininfo looks like this:
>>
>> # Easy case:
>> Corners: +395+228 -147+228 -147-330 +395-330
>
> These are not x geometry values, but evidently just a way of printing
> out the corners. ++ means upper left, -- lower right, etc.
> This notation is fixed.

Thanks for the info about ++ and --, and yes, these are corner
values, not actual X geometry specs.

>> # Case where I despair:
>> Corners: +-1269+165 -1487+165 -1487--46 +-1269--46
>
> WTF is that? Here it looks like the same notation: ++ -+ -- +-
> But some of the values are negative for whatever reason.

Yes, WTF. I suck at math and don't know much about X geometry, but
with a viewport-based WM like Compiz (maybe Compiz alone, later I
have to test this with others like Fvwm) the issue is more complex.
If you're curious, I can give you more examples. Some of them are
really strange (at least to me).

> Corners: +(-1269)+(165) -(1487)+(165) -(1487)-(-46) +(-1269)-(-46)
>
> ((If this gives you grief, why don't you extract this same info from the other
> output lines?

Because I need the rectangles that windows form relative to a) the
full virtual desktop, and b) the current viewport, and I thought (I
still think) that the Corners: specs are more close to this.

With the upper-left/width/height values, I have to do even more
calculations to get the rectangles.

But anyway, I got a perfect solution from Ed Morton.

> TXR job (http://www.nongnu.org/txr):

Interesting. Will look at this.

Thank you! :-)

0 new messages