Window position in Xlib
Asked Answered
H

4

9

How to get top-level window position relative to root window (i.e. whole screen) using plain ol' xlib (or brand new XCB)?

Hackler answered 27/9, 2010 at 18:55 Comment(0)
A
17

The x,y components of the structure returned by XGetWindowAttributes are relative to the origin of the window's parent. That's not the same as relative to the top left of the screen.

Calling XTranslateCoordinates passing the root window and 0,0 gives coordinates of the window relative to the screen.

I found that if I write:

int x, y;
Window child;
XWindowAttributes xwa;
XTranslateCoordinates( display, window, root_window, 0, 0, &x, &y, &child );
XGetWindowAttributes( display, window, &xwa );
printf( "%d %d\n", x - xwa.x, y - xwa.y );

The values displayed by the printf are those which, if passed to XMoveWindow, keep the window at its current position. Thus those coordinates are reasonably considered to be the position of the window.

Antisocial answered 29/5, 2014 at 18:55 Comment(1)
For me, the subtraction always resulted in 1. I used xwa.x and xwa.y instead.Jobie
F
8

Using Xlib:

XWindowAttributes xwa;
XGetWindowAttributes(display, window, &xwa);
printf("%d %d\n", xwa.x, xwa.y);

There are also lots of other informations that come with XWindowAttributes. See here.

Fencing answered 23/10, 2010 at 15:56 Comment(0)
J
5

Use XTranslateCoordinates (or xcb equivalent) to translate 0,0 on the window to root window coordinates.

Joeannjoed answered 27/9, 2010 at 19:56 Comment(0)
S
4

This is what you would do with XCB:

auto geom = xcb_get_geometry(xcb_connection(), window);
auto offset = xcb_translate_coordinate(xcb_connection(), window, rootwin, geom->x, geom->y);

offset->dst_x // top-level window's x offset on the screen
offset->dst_y // top-level window's y offset on the screen
geom->width   // top-level window's width
geom->height  // top-level window's height
Stinky answered 28/4, 2018 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.