1 #include "x11_tools.h"
2 
3 #if defined(QMC2_OS_UNIX)
4 
5 #include <QX11Info>
6 #include <QRegExp>
7 #include <X11/Xlib.h>
8 #include <X11/Xutil.h>
9 
x11SearchTree(Window window,QString titlePattern,QString classHintPattern)10 Window x11SearchTree(Window window, QString titlePattern, QString classHintPattern)
11 {
12 	Window root_win, parent_win, *child_list, win_return = 0;
13 	unsigned int num_children;
14 	if ( XQueryTree(QX11Info::display(), window, &root_win, &parent_win, &child_list, &num_children) ) {
15 		char *win_name;
16 		QRegExp rxTitle(titlePattern, Qt::CaseSensitive, QRegExp::RegExp2);
17 		QRegExp rxClassHint(classHintPattern, Qt::CaseSensitive, QRegExp::RegExp2);
18 		for (int i = num_children - 1; i >= 0 && !win_return; i--) {
19 			if ( XFetchName(QX11Info::display(), child_list[i], &win_name) ) {
20 				if ( win_name ) {
21 					if ( rxTitle.indexIn(QString(win_name)) >= 0 ) {
22 						XClassHint class_hint;
23 						if ( XGetClassHint(QX11Info::display(), child_list[i], &class_hint)) {
24 							if ( class_hint.res_name ) {
25 								if ( rxClassHint.indexIn(QString(class_hint.res_name)) >= 0 )
26 									win_return = child_list[i];
27 								XFree(class_hint.res_name);
28 							}
29 							if ( class_hint.res_class ) {
30 								if ( !win_return && rxClassHint.indexIn(QString(class_hint.res_class)) >= 0 )
31 									win_return = child_list[i];
32 								XFree(class_hint.res_class);
33 							}
34 						}
35 					}
36 					XFree(win_name);
37 				}
38 			}
39 			if ( !win_return )
40 				win_return = x11SearchTree(child_list[i], titlePattern, classHintPattern);
41 		}
42 		XFree((char *)child_list);
43 	}
44 	return win_return;
45 }
46 
x11FindWindowId(QString titlePattern,QString classHintPattern)47 WId x11FindWindowId(QString titlePattern, QString classHintPattern)
48 {
49 	return (WId)x11SearchTree(QX11Info::appRootWindow(), titlePattern, classHintPattern);
50 }
51 
52 #endif
53