Thanks @Striezel, your feedback pointed me in the right direction. After investigating your solution, I run into this post: Xlib: XGetWindowAttributes always returns 1x1?
Tweaking the answer from @Doug a little bit I've got following, which seems to be working as expected:
Window getToplevelParent(Display* display, Window window)
{
Window parentWindow;
Window rootWindow;
Window* childrenWindows;
unsigned int numberOfChildren;
while (1) {
if (XQueryTree(display, window, &rootWindow, &parentWindow, &childrenWindows,
&numberOfChildren) == 0) {
qCritical("ImageGrabber::getToplevelParent: XQueryTree Error");
return 0;
}
if (childrenWindows) {
XFree(childrenWindows);
}
if (window == rootWindow || parentWindow == rootWindow) {
return window;
} else {
window = parentWindow;
}
}
}
QRect ImageGrabber::getActiveWindowRect()
{
Display* display = XOpenDisplay(NULL);
Window focusWindow, parentOfFocusedWindow;
XWindowAttributes attrributes;
int revert;
XGetInputFocus(display, &focusWindow, &revert);
parentOfFocusedWindow = getToplevelParent(display, focusWindow);
if (!parentOfFocusedWindow) {
qCritical("ImageGrabber::getActiveWindowRect: Unable to get window, returning screen.");
return getCurrectScreenRect();
}
XGetWindowAttributes(display, parentOfFocusedWindow, &attrributes);
return QRect(attrributes.x, attrributes.y, attrributes.width, attrributes.height);
}