~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to khotkeys/libkhotkeysprivate/windows_handler.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
 
 
3
 KHotKeys
 
4
 
 
5
 Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org>
 
6
 
 
7
 Distributed under the terms of the GNU General Public License version 2.
 
8
 
 
9
****************************************************************************/
 
10
 
 
11
#define _WINDOWS_CPP_
 
12
 
 
13
#include "windows_handler.h"
 
14
 
 
15
#include "windows_helper/window_selection_rules.h"
 
16
#include "windows_helper/window_selection_list.h"
 
17
 
 
18
#include <QRegExp>
 
19
 
 
20
#include <kconfig.h>
 
21
#include <kconfiggroup.h>
 
22
#include <kdebug.h>
 
23
#include <kwindowsystem.h>
 
24
#include <klocale.h>
 
25
 
 
26
#include "khotkeysglobal.h"
 
27
 
 
28
#include <X11/Xlib.h>
 
29
#include <X11/Xutil.h>
 
30
#include <QX11Info>
 
31
 
 
32
namespace KHotKeys
 
33
{
 
34
 
 
35
// WindowsHandler
 
36
 
 
37
WindowsHandler::WindowsHandler( bool enable_signal_P, QObject* parent_P )
 
38
    : QObject( parent_P ), signals_enabled( enable_signal_P ),
 
39
        _action_window( 0 )
 
40
    {
 
41
    if( signals_enabled )
 
42
        {
 
43
        connect( KWindowSystem::self(), SIGNAL( windowAdded( WId )), SLOT( window_added_slot( WId )));
 
44
        connect( KWindowSystem::self(), SIGNAL( windowRemoved( WId )), SLOT( window_removed_slot( WId )));
 
45
        connect( KWindowSystem::self(), SIGNAL( activeWindowChanged( WId )),
 
46
            SLOT( active_window_changed_slot( WId )));
 
47
        }
 
48
    }
 
49
 
 
50
WindowsHandler::~WindowsHandler()
 
51
    {
 
52
    }
 
53
 
 
54
void WindowsHandler::window_added_slot( WId window_P )
 
55
    {
 
56
    if( signals_enabled )
 
57
        emit window_added( window_P );
 
58
    // CHECKME tyhle i dalsi by asi mely jit nastavit, jestli aktivuji vsechny, nebo jen jeden
 
59
    // pripojeny slot ( stejne jako u Kdb, kde by to take melo jit nastavit )
 
60
    }
 
61
 
 
62
void WindowsHandler::window_removed_slot( WId window_P )
 
63
    {
 
64
    if( signals_enabled )
 
65
        emit window_removed( window_P );
 
66
    if( window_P == _action_window )
 
67
        _action_window = 0;
 
68
    }
 
69
 
 
70
void WindowsHandler::active_window_changed_slot( WId window_P )
 
71
    {
 
72
    if( signals_enabled )
 
73
        emit active_window_changed( window_P );
 
74
    }
 
75
 
 
76
void WindowsHandler::window_changed_slot( WId window_P )
 
77
    {
 
78
    if( signals_enabled )
 
79
        emit window_changed( window_P );
 
80
    }
 
81
 
 
82
void WindowsHandler::window_changed_slot( WId window_P, unsigned int flags_P )
 
83
    {
 
84
    if( signals_enabled )
 
85
        emit window_changed( window_P, flags_P );
 
86
    }
 
87
 
 
88
QString WindowsHandler::get_window_role( WId id_P )
 
89
    {
 
90
    // TODO this is probably just a hack
 
91
    return KWindowSystem::windowInfo( id_P, 0, NET::WM2WindowRole ).windowRole();
 
92
    }
 
93
 
 
94
QString WindowsHandler::get_window_class( WId id_P )
 
95
    {
 
96
    XClassHint hints_ret;
 
97
    if( XGetClassHint( QX11Info::display(), id_P, &hints_ret ) == 0 ) // 0 means error
 
98
        return "";
 
99
    QString ret( hints_ret.res_name );
 
100
    ret += ' ';
 
101
    ret += hints_ret.res_class;
 
102
    XFree( hints_ret.res_name );
 
103
    XFree( hints_ret.res_class );
 
104
    return ret;
 
105
    }
 
106
 
 
107
WId WindowsHandler::active_window()
 
108
    {
 
109
    return KWindowSystem::activeWindow();
 
110
    }
 
111
 
 
112
WId WindowsHandler::action_window()
 
113
    {
 
114
    return _action_window;
 
115
    }
 
116
 
 
117
void WindowsHandler::set_action_window( WId window_P )
 
118
    {
 
119
    _action_window = window_P;
 
120
    }
 
121
 
 
122
WId WindowsHandler::find_window( const Windowdef_list* window_P )
 
123
    {
 
124
    for( QList< WId >::const_iterator it = KWindowSystem::windows().begin();
 
125
         it != KWindowSystem::windows().end();
 
126
         ++it )
 
127
        {
 
128
        Window_data tmp( *it );
 
129
        if( window_P->match( tmp ))
 
130
            return *it;
 
131
        }
 
132
    return None;
 
133
    }
 
134
 
 
135
WId WindowsHandler::window_at_position( int x, int y )
 
136
    {
 
137
    Window child, dummy;
 
138
    Window parent = QX11Info::appRootWindow();
 
139
    Atom wm_state = XInternAtom( QX11Info::display(), "WM_STATE", False );
 
140
    for( int i = 0;
 
141
         i < 10;
 
142
         ++i )
 
143
        {
 
144
        int destx, desty;
 
145
        // find child at that position
 
146
        if( !XTranslateCoordinates( QX11Info::display(), parent, parent, x, y, &destx, &desty, &child )
 
147
            || child == None )
 
148
            return 0;
 
149
        // and now transform coordinates to the child
 
150
        if( !XTranslateCoordinates( QX11Info::display(), parent, child, x, y, &destx, &desty, &dummy ))
 
151
            return 0;
 
152
        x = destx;
 
153
        y = desty;
 
154
        Atom type;
 
155
        int format;
 
156
        unsigned long nitems, after;
 
157
        unsigned char* prop;
 
158
        if( XGetWindowProperty( QX11Info::display(), child, wm_state, 0, 0, False, AnyPropertyType,
 
159
            &type, &format, &nitems, &after, &prop ) == Success )
 
160
            {
 
161
            if( prop != NULL )
 
162
                XFree( prop );
 
163
            if( type != None )
 
164
                return child;
 
165
            }
 
166
        parent = child;
 
167
        }
 
168
    return 0;
 
169
 
 
170
    }
 
171
 
 
172
void WindowsHandler::activate_window( WId id_P )
 
173
    {
 
174
    KWindowSystem::forceActiveWindow( id_P );
 
175
    }
 
176
 
 
177
// Window_data
 
178
 
 
179
Window_data::Window_data( WId id_P )
 
180
    : type( NET::Unknown )
 
181
    {
 
182
    KWindowInfo kwin_info = KWindowSystem::windowInfo( id_P, NET::WMName | NET::WMWindowType ); // TODO optimize
 
183
    if( kwin_info.valid())
 
184
        {
 
185
        title = kwin_info.name();
 
186
        role = windows_handler->get_window_role( id_P );
 
187
        wclass = windows_handler->get_window_class( id_P );
 
188
        type = kwin_info.windowType( SUPPORTED_WINDOW_TYPES_MASK );
 
189
        if( type == NET::Override ) // HACK consider non-NETWM fullscreens to be normal too
 
190
            type = NET::Normal;
 
191
        if( type == NET::Unknown )
 
192
            type = NET::Normal;
 
193
        }
 
194
    }
 
195
 
 
196
} // namespace KHotKeys
 
197
 
 
198
#include "moc_windows_handler.cpp"
 
199