Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window position in Xlib

Tags:

x11

xcb

xlib

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

like image 915
el.pescado - нет войне Avatar asked Sep 27 '10 18:09

el.pescado - нет войне


1 Answers

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.

like image 139
Robert J. Harwick Avatar answered Sep 17 '22 15:09

Robert J. Harwick