summaryrefslogtreecommitdiffstats
path: root/khotkeys/shared/windows.cpp
blob: 4b009ace3ae69efc691a176f69757f8c89240410 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/****************************************************************************

 KHotKeys

 Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org>

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

****************************************************************************/

#define _WINDOWS_CPP_

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

#include "windows.h"

#include <assert.h>
#include <tqregexp.h>

#include <kconfig.h>
#include <kdebug.h>
#include <twinmodule.h>
#include <twin.h>
#include <klocale.h>

#include "khotkeysglobal.h"

#include <X11/Xlib.h>
#include <X11/Xutil.h>

extern Atom tqt_window_role;

namespace KHotKeys
{

// Windows

Windows::Windows( bool enable_signal_P, TQObject* parent_P )
    : TQObject( parent_P ), signals_enabled( enable_signal_P ),
        twin_module( new KWinModule( this )), _action_window( 0 )
    {
    assert( windows_handler == NULL );
    windows_handler = this;
    if( signals_enabled )
        {
        connect( twin_module, TQT_SIGNAL( windowAdded( WId )), TQT_SLOT( window_added_slot( WId )));
        connect( twin_module, TQT_SIGNAL( windowRemoved( WId )), TQT_SLOT( window_removed_slot( WId )));
        connect( twin_module, TQT_SIGNAL( activeWindowChanged( WId )),
            TQT_SLOT( active_window_changed_slot( WId )));
        }
    }

Windows::~Windows()
    {
    windows_handler = NULL;
    }

void Windows::window_added_slot( WId window_P )
    {
    if( signals_enabled )
        emit window_added( window_P );
    // CHECKME tyhle i dalsi by asi mely jit nastavit, jestli aktivuji vsechny, nebo jen jeden
    // pripojeny slot ( stejne jako u Kdb, kde by to take melo jit nastavit )
    }

void Windows::window_removed_slot( WId window_P )
    {
    if( signals_enabled )
        emit window_removed( window_P );
    if( window_P == _action_window )
        _action_window = 0;
    }

void Windows::active_window_changed_slot( WId window_P )
    {
    if( signals_enabled )
        emit active_window_changed( window_P );
    }

void Windows::window_changed_slot( WId window_P )
    {
    if( signals_enabled )
        emit window_changed( window_P );
    }

void Windows::window_changed_slot( WId window_P, unsigned int flags_P )
    {
    if( signals_enabled )
        emit window_changed( window_P, flags_P );
    }

TQString Windows::get_window_role( WId id_P )
    {
    // TODO this is probably just a hack
    return KWin::readNameProperty( id_P, tqt_window_role );
    }

TQString Windows::get_window_class( WId id_P )
    {
    XClassHint hints_ret;
    if( XGetClassHint( tqt_xdisplay(), id_P, &hints_ret ) == 0 ) // 0 means error
	return "";
    TQString ret( hints_ret.res_name );
    ret += ' ';
    ret += hints_ret.res_class;
    XFree( hints_ret.res_name );
    XFree( hints_ret.res_class );
    return ret;
    }

WId Windows::active_window()
    {
    return twin_module->activeWindow();
    }

WId Windows::action_window()
    {
    return _action_window;
    }

void Windows::set_action_window( WId window_P )
    {
    _action_window = window_P;
    }

WId Windows::find_window( const Windowdef_list* window_P )
    {
    for( TQValueList< WId >::ConstIterator it = twin_module->windows().begin();
         it != twin_module->windows().end();
         ++it )
        {
        Window_data tmp( *it );
        if( window_P->match( tmp ))
            return *it;
        }
    return None;
    }

WId Windows::window_at_position( int x, int y )
    {
    Window child, dummy;
    Window parent = tqt_xrootwin();
    Atom wm_state = XInternAtom( tqt_xdisplay(), "WM_STATE", False );
    for( int i = 0;
         i < 10;
         ++i )
        {
        int destx, desty;
        // find child at that position
        if( !XTranslateCoordinates( tqt_xdisplay(), parent, parent, x, y, &destx, &desty, &child )
            || child == None )
            return 0;
        // and now transform coordinates to the child
        if( !XTranslateCoordinates( tqt_xdisplay(), parent, child, x, y, &destx, &desty, &dummy ))
            return 0;
        x = destx;
        y = desty;
        Atom type;
        int format;
        unsigned long nitems, after;
        unsigned char* prop;
        if( XGetWindowProperty( tqt_xdisplay(), child, wm_state, 0, 0, False, AnyPropertyType,
	    &type, &format, &nitems, &after, &prop ) == Success )
            {
	    if( prop != NULL )
	        XFree( prop );
	    if( type != None )
	        return child;
            }
        parent = child;
        }
    return 0;
    
    }

void Windows::activate_window( WId id_P )
    {
    KWin::forceActiveWindow( id_P );
    }

// Window_data

Window_data::Window_data( WId id_P )
    : type( NET::Unknown )
    {
    KWin::WindowInfo twin_info = KWin::windowInfo( id_P, NET::WMName | NET::WMWindowType ); // TODO optimize
    if( twin_info.valid())
        {
        title = twin_info.name();
        role = windows_handler->get_window_role( id_P );
        wclass = windows_handler->get_window_class( id_P );
        type = twin_info.windowType( SUPPORTED_WINDOW_TYPES_MASK );
        if( type == NET::Override ) // HACK consider non-NETWM fullscreens to be normal too
            type = NET::Normal;
        if( type == NET::Unknown )
            type = NET::Normal;
        }
    }

// Windowdef

void Windowdef::cfg_write( KConfig& cfg_P ) const
    {
    cfg_P.writeEntry( "Type", "ERROR" );
    cfg_P.writeEntry( "Comment", comment());
    }

Windowdef::Windowdef( KConfig& cfg_P )
    {
    _comment = cfg_P.readEntry( "Comment" );
    }

Windowdef* Windowdef::create_cfg_read( KConfig& cfg_P )
    {
    TQString type = cfg_P.readEntry( "Type" );
    if( type == "SIMPLE" )
        return new Windowdef_simple( cfg_P );
    kdWarning( 1217 ) << "Unknown Windowdef type read from cfg file\n";
    return NULL;
    }

// Windowdef_list

Windowdef_list::Windowdef_list( KConfig& cfg_P )
    : TQPtrList< Windowdef >()
    {
    setAutoDelete( true );
    TQString save_cfg_group = cfg_P.group();
    _comment = cfg_P.readEntry( "Comment" );
    int cnt = cfg_P.readNumEntry( "WindowsCount", 0 );
    for( int i = 0;
         i < cnt;
         ++i )
        {
        cfg_P.setGroup( save_cfg_group + TQString::number( i ));
        Windowdef* window = Windowdef::create_cfg_read( cfg_P );
        if( window )
            append( window );
        }
    cfg_P.setGroup( save_cfg_group );
    }

void Windowdef_list::cfg_write( KConfig& cfg_P ) const
    {
    TQString save_cfg_group = cfg_P.group();
    int i = 0;
    for( Iterator it( *this );
         it;
         ++it, ++i )
        {
        cfg_P.setGroup( save_cfg_group + TQString::number( i ));
        it.current()->cfg_write( cfg_P );
        }
    cfg_P.setGroup( save_cfg_group );
    cfg_P.writeEntry( "WindowsCount", i );
    cfg_P.writeEntry( "Comment", comment());
    }

Windowdef_list* Windowdef_list::copy() const
    {
    Windowdef_list* ret = new Windowdef_list( comment());
    for( Iterator it( *this );
         it;
         ++it )
        ret->append( it.current()->copy());
    return ret;
    }


bool Windowdef_list::match( const Window_data& window_P ) const
    {
    if( count() == 0 ) // CHECKME no windows to match => ok
        return true;
    for( Iterator it( *this );
         it;
         ++it )
        if( it.current()->match( window_P ))
            return true;
    return false;
    }

// Windowdef_simple

Windowdef_simple::Windowdef_simple( const TQString& comment_P, const TQString& title_P,
    substr_type_t title_type_P, const TQString& wclass_P, substr_type_t wclass_type_P,
    const TQString& role_P, substr_type_t role_type_P, int window_types_P )
    : Windowdef( comment_P ), _title( title_P ), title_type( title_type_P ),
    _wclass( wclass_P ), wclass_type( wclass_type_P ), _role( role_P ),
    role_type( role_type_P ), _window_types( window_types_P )
    {
    }

Windowdef_simple::Windowdef_simple( KConfig& cfg_P )
    : Windowdef( cfg_P )
    {
    _title = cfg_P.readEntry( "Title" );
    title_type = static_cast< substr_type_t >( cfg_P.readNumEntry( "TitleType" ));
    _wclass = cfg_P.readEntry( "Class" );
    wclass_type = static_cast< substr_type_t >( cfg_P.readNumEntry( "ClassType" ));
    _role = cfg_P.readEntry( "Role" );
    role_type = static_cast< substr_type_t >( cfg_P.readNumEntry( "RoleType" ));
    _window_types = cfg_P.readNumEntry( "WindowTypes" );
    }

void Windowdef_simple::cfg_write( KConfig& cfg_P ) const
    {
    base::cfg_write( cfg_P );
    cfg_P.writeEntry( "Title", title());
    cfg_P.writeEntry( "TitleType", title_type );
    cfg_P.writeEntry( "Class", wclass());
    cfg_P.writeEntry( "ClassType", wclass_type );
    cfg_P.writeEntry( "Role", role());
    cfg_P.writeEntry( "RoleType", role_type );
    cfg_P.writeEntry( "WindowTypes", window_types());
    cfg_P.writeEntry( "Type", "SIMPLE" ); // overwrites value set in base::cfg_write()
    }

bool Windowdef_simple::match( const Window_data& window_P )
    {
    if( !type_match( window_P.type ))
        return false;
    if( !is_substr_match( window_P.title, title(), title_type ))
        return false;
    if( !is_substr_match( window_P.wclass, wclass(), wclass_type ))
        return false;
    if( !is_substr_match( window_P.role, role(), role_type ))
        return false;
    kdDebug( 1217 ) << "window match:" << window_P.title << ":OK" << endl;
    return true;
    }

bool Windowdef_simple::is_substr_match( const TQString& str1_P, const TQString& str2_P,
    substr_type_t type_P )
    {
    switch( type_P )
        {
        case NOT_IMPORTANT :
          return true;
        case CONTAINS :
          return str1_P.contains( str2_P ) > 0;
        case IS :
          return str1_P == str2_P;
        case REGEXP :
            {
            TQRegExp rg( str2_P );
          return rg.search( str1_P ) >= 0;
            }
        case CONTAINS_NOT :
          return str1_P.contains( str2_P ) == 0;
        case IS_NOT :
          return str1_P != str2_P;
        case REGEXP_NOT :
            {
            TQRegExp rg( str2_P );
          return rg.search( str1_P ) < 0;
            }
        }
    return false;
    }

Windowdef* Windowdef_simple::copy() const
    {
    return new Windowdef_simple( comment(), title(), title_match_type(), wclass(),
        wclass_match_type(), role(), role_match_type(), window_types());
    }

const TQString Windowdef_simple::description() const
    {
    return i18n( "Window simple: " ) + comment();
    }

} // namespace KHotKeys

#include "windows.moc"