lolautoaccept/src/x11helper_x11.cpp

78 lines
1.6 KiB
C++

#include "x11helper.h"
#include <X11/Xlib.h>
const bool X11Helper::IsSupported = true;
X11Helper::X11Helper(QObject *parent) : QObject(parent), disp(XOpenDisplay(nullptr)) {
}
X11Helper::~X11Helper() {
XCloseDisplay(this->disp);
}
Window X11Helper::findWindow(const QString& name, float aspektRatio) {
return searchWindows(DefaultRootWindow(disp), name, aspektRatio);
}
Window X11Helper::searchWindows(Window top, const QString& search, float aspektRatio) {
{
char* window_name;
if (XFetchName(disp, top, &window_name)) {
QString winName(window_name);
XFree(window_name);
if (search == winName) {
if(aspektRatio == 0.0) {
return top; // dont look for kids
}
XWindowAttributes attribs;
if(XGetWindowAttributes(disp, top, &attribs)) {
const float winAspektRation = attribs.width / (float) attribs.height;
if(qAbs(winAspektRation - aspektRatio) < aspektRatio/10.0) {
return top;
}
}
}
}
}
Window* children = nullptr;
Window dummy;
unsigned int nchildren;
if (!XQueryTree(disp, top, &dummy, &dummy, &children, &nchildren)) {
return InvalidWindow;
}
for (unsigned int i = 0; i < nchildren; i++) {
Window res = searchWindows(children[i], search, aspektRatio);
if(res != InvalidWindow) {
XFree((char*) children);
return res;
}
}
if (children) {
XFree((char*) children);
}
return InvalidWindow;
}
void X11Helper::map(Window win) {
XMapRaised(disp, win);
}
void X11Helper::unmap(Window win) {
XUnmapWindow(disp, win);
}
void X11Helper::setMap(Window win, bool b) {
if(b) {
map(win);
} else {
unmap(win);
}
}