aa979009c85922dad85b0a6e4c1f03ec1cb60165
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / gui / qxtdesktopwidget_x11.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
4 **
5 ** This file is part of the QxtGui module of the Qt eXTension library
6 **
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
9 ** IBM.
10 **
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
15 **
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
20 **
21 ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>
22 **
23 ****************************************************************************/
24 #include "qxtdesktopwidget.h"
25 #include <QX11Info>
26 #include <X11/Xutil.h>
27
28 static void qxt_getWindowProperty(Window wid, Atom prop, int maxlen, Window** data, int* count)
29 {
30     Atom type = 0;
31     int format = 0;
32     unsigned long after = 0;
33     XGetWindowProperty(QX11Info::display(), wid, prop, 0, maxlen / 4, False, AnyPropertyType,
34                        &type, &format, (unsigned long*) count, &after, (unsigned char**) data);
35 }
36
37 static QRect qxt_getWindowRect(Window wid)
38 {
39     QRect rect;
40     XWindowAttributes attr;
41     if (XGetWindowAttributes(QX11Info::display(), wid, &attr))
42     {
43         rect = QRect(attr.x, attr.y, attr.width, attr.height);
44     }
45     return rect;
46 }
47
48 WindowList QxtDesktopWidget::windows()
49 {
50     static Atom net_clients = 0;
51     if (!net_clients)
52         net_clients = XInternAtom(QX11Info::display(), "_NET_CLIENT_LIST_STACKING", True);
53
54     int count = 0;
55     Window* list = 0;
56     qxt_getWindowProperty(QX11Info::appRootWindow(), net_clients, 1024 * sizeof(Window), &list, &count);
57
58     WindowList res;
59     for (int i = 0; i < count; ++i)
60         res += list[i];
61     XFree(list);
62     return res;
63 }
64
65 WId QxtDesktopWidget::activeWindow()
66 {
67     Window focus;
68     int revert = 0;
69     Display* display = QX11Info::display();
70     XGetInputFocus(display, &focus, &revert);
71     return focus;
72 }
73
74 WId QxtDesktopWidget::findWindow(const QString& title)
75 {
76     Window result = 0;
77     WindowList list = windows();
78     foreach (Window wid, list)
79     {
80         if (windowTitle(wid) == title)
81         {
82             result = wid;
83             break;
84         }
85     }
86     return result;
87 }
88
89 WId QxtDesktopWidget::windowAt(const QPoint& pos)
90 {
91     Window result = 0;
92     WindowList list = windows();
93     for (int i = list.size() - 1; i >= 0; --i)
94     {
95         WId wid = list.at(i);
96         if (windowGeometry(wid).contains(pos))
97         {
98             result = wid;
99             break;
100         }
101     }
102     return result;
103 }
104
105 QString QxtDesktopWidget::windowTitle(WId window)
106 {
107     QString name;
108     char* str = 0;
109     if (XFetchName(QX11Info::display(), window, &str))
110     {
111         name = QString::fromLatin1(str);
112     }
113     XFree(str);
114     return name;
115 }
116
117 QRect QxtDesktopWidget::windowGeometry(WId window)
118 {
119     QRect rect = qxt_getWindowRect(window);
120
121     Window root = 0;
122     Window parent = 0;
123     Window* children = 0;
124     unsigned int count = 0;
125     Display* display = QX11Info::display();
126     if (XQueryTree(display, window, &root, &parent, &children, &count))
127     {
128         window = parent; // exclude decoration
129         XFree(children);
130         while (window != 0 && XQueryTree(display, window, &root, &parent, &children, &count))
131         {
132             XWindowAttributes attr;
133             if (parent != 0 && XGetWindowAttributes(display, parent, &attr))
134                 rect.translate(QRect(attr.x, attr.y, attr.width, attr.height).topLeft());
135             window = parent;
136             XFree(children);
137         }
138     }
139     return rect;
140 }