summaryrefslogtreecommitdiffstats
path: root/khotkeys/kcontrol/windowselector.cpp
blob: 745f85682bf59d1fcc2fdc9adf6ab3ab162d0346 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/****************************************************************************

 KHotKeys
 
 Copyright (C) 2003 Lubos Lunak <l.lunak@kde.org>

 Distributed under the terms of the GNU General Public License version 2.
 
****************************************************************************/

#define _WINDOWSELECTOR_CPP_

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "windowselector.h"

#include "voices.h"

#include <tqcursor.h>
#include <kdebug.h>
#include <tdeapplication.h>
#include <X11/Xlib.h>
#include <fixx11h.h>

namespace KHotKeys
{

WindowSelector::WindowSelector( TQObject* receiver_P, const char* slot_P )
    {
    connect( this, TQT_SIGNAL( selected_signal( WId )), receiver_P, slot_P );
    }

void WindowSelector::select()
    {
    kapp->desktop()->grabMouse( TQCursor( TQt::crossCursor ));
    kapp->installX11EventFilter( TQT_TQWIDGET(this) );
    }

bool WindowSelector::x11Event( XEvent* e )
    {
    if( e->type != ButtonPress )
        return false;
    kapp->desktop()->releaseMouse();
    if( e->xbutton.button == Button1 )
        {
        WId window = findRealWindow( e->xbutton.subwindow );
        if( window )
            selected_signal( window );
        }
    delete this;
    return true;
    }

WId WindowSelector::findRealWindow( WId w, int depth )
    {
    if( depth > 5 )
	return None;
    static Atom wm_state = XInternAtom( tqt_xdisplay(), "WM_STATE", False );
    Atom type;
    int format;
    unsigned long nitems, after;
    unsigned char* prop;
    if( XGetWindowProperty( tqt_xdisplay(), w, wm_state, 0, 0, False, AnyPropertyType,
	&type, &format, &nitems, &after, &prop ) == Success )
        {
	if( prop != NULL )
	    XFree( prop );
	if( type != None )
	    return w;
        }
    Window root, parent;
    Window* children;
    unsigned int nchildren;
    Window ret = None;
    if( XQueryTree( tqt_xdisplay(), w, &root, &parent, &children, &nchildren ) != 0 )
        {
	for( unsigned int i = 0;
	     i < nchildren && ret == None;
	     ++i )
	    ret = findRealWindow( children[ i ], depth + 1 );
	if( children != NULL )
	    XFree( children );
        }
    return ret;
}


} // namespace KHotKeys

#include "windowselector.moc"