summaryrefslogtreecommitdiffstats
path: root/khotkeys/shared/triggers.cpp
blob: 7cac2b98c82c67d0e37acf610d502a6e23b3a03d (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/****************************************************************************

 KHotKeys

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

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

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

#define _TRIGGERS_CPP_

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

#include "triggers.h"

#include <kglobalaccel.h>
#include <kconfig.h>
#include <kdebug.h>
#include <kwinmodule.h>
#include <klocale.h>
#include <netwm_def.h>
#include <kaccel.h>

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

#include "actions.h"
#include "action_data.h"
#include "input.h"
#include "gestures.h"
#include "windows.h"
#include "voices.h"

namespace KHotKeys
{

// Trigger

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

Trigger* Trigger::create_cfg_read( KConfig& cfg_P, Action_data* data_P )
    {
    TQString type = cfg_P.readEntry( "Type" );
    if( type == "SHORTCUT" || type == "SINGLE_SHORTCUT" )
        return new Shortcut_trigger( cfg_P, data_P );
    if( type == "WINDOW" )
        return new Window_trigger( cfg_P, data_P );
    if( type == "GESTURE" )
        return new Gesture_trigger(cfg_P, data_P );
    if( type == "VOICE" )
        return new Voice_trigger (cfg_P, data_P );

    kdWarning( 1217 ) << "Unknown Trigger type read from cfg file\n";
    return NULL;
    }

// Trigger_list

Trigger_list::Trigger_list( KConfig& cfg_P, Action_data* data_P )
    : TQPtrList< Trigger >()
    {
    setAutoDelete( true );
    _comment = cfg_P.readEntry( "Comment" );
    TQString save_cfg_group = cfg_P.group();
    int cnt = cfg_P.readNumEntry( "TriggersCount", 0 );
    for( int i = 0;
         i < cnt;
         ++i )
        {
        cfg_P.setGroup( save_cfg_group + TQString::number( i ));
        Trigger* trigger = Trigger::create_cfg_read( cfg_P, data_P );
        if( trigger )
            append( trigger );
        }
    cfg_P.setGroup( save_cfg_group );
    }

void Trigger_list::cfg_write( KConfig& cfg_P ) const
    {
    cfg_P.writeEntry( "Comment", comment());
    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( "TriggersCount", i );
    }

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

void Trigger_list::activate( bool activate_P )
    {
    for( Iterator it( *this );
         it;
         ++it )
        ( *it )->activate( activate_P );
    }

// Shortcut_trigger

Shortcut_trigger::Shortcut_trigger( Action_data* data_P, const KShortcut& shortcut_P )
    : Trigger( data_P ), _shortcut( shortcut_P )
    {
    keyboard_handler->insert_item( shortcut(), this );
    }

Shortcut_trigger::Shortcut_trigger( KConfig& cfg_P, Action_data* data_P )
    : Trigger( cfg_P, data_P ), _shortcut( cfg_P.readEntry( "Key", 0 ))
    {
    keyboard_handler->insert_item( shortcut(), this );
    }

Shortcut_trigger::~Shortcut_trigger()
    {
    keyboard_handler->remove_item( shortcut(), this );
    }

void Shortcut_trigger::cfg_write( KConfig& cfg_P ) const
    {
    base::cfg_write( cfg_P );
    cfg_P.writeEntry( "Key", _shortcut.toStringInternal());
    cfg_P.writeEntry( "Type", "SHORTCUT" ); // overwrites value set in base::cfg_write()
    }

Shortcut_trigger* Shortcut_trigger::copy( Action_data* data_P ) const
    {
    kdDebug( 1217 ) << "Shortcut_trigger::copy()" << endl;
    return new Shortcut_trigger( data_P ? data_P : data, shortcut());
    }

const TQString Shortcut_trigger::description() const
    {
    // CHECKME vice mods
    return i18n( "Shortcut trigger: " ) + _shortcut.toString();
    // CHECKME i18n pro toString() ?
    }

bool Shortcut_trigger::handle_key( const KShortcut& shortcut_P )
    {
    if( shortcut() == shortcut_P )
        {
        windows_handler->set_action_window( 0 ); // use active window
        data->execute();
        return true;
        }
    return false;
    }

void Shortcut_trigger::activate( bool activate_P )
    {
    if( activate_P && khotkeys_active())
        keyboard_handler->activate_receiver( this );
    else
        keyboard_handler->deactivate_receiver( this );
    }

// Window_trigger

Window_trigger::Window_trigger( KConfig& cfg_P, Action_data* data_P )
    : Trigger( cfg_P, data_P ), active( false )
    {
//    kdDebug( 1217 ) << "Window_trigger" << endl;
    TQString save_cfg_group = cfg_P.group();
    cfg_P.setGroup( save_cfg_group + "Windows" );
    _windows = new Windowdef_list( cfg_P );
    cfg_P.setGroup( save_cfg_group );
    window_actions = cfg_P.readNumEntry( "WindowActions" );
    init();
    }

Window_trigger::~Window_trigger()
    {
//    kdDebug( 1217 ) << "~Window_trigger :" << this << endl;
    disconnect( windows_handler, NULL, this, NULL );
    delete _windows;
    }

void Window_trigger::init()
    {
    kdDebug( 1217 ) << "Window_trigger::init()" << endl;
    connect( windows_handler, TQT_SIGNAL( window_added( WId )), this, TQT_SLOT( window_added( WId )));
    connect( windows_handler, TQT_SIGNAL( window_removed( WId )), this, TQT_SLOT( window_removed( WId )));
    if( window_actions & ( WINDOW_ACTIVATES | WINDOW_DEACTIVATES /*| WINDOW_DISAPPEARS*/ ))
        connect( windows_handler, TQT_SIGNAL( active_window_changed( WId )),
            this, TQT_SLOT( active_window_changed( WId )));
    connect( windows_handler, TQT_SIGNAL( window_changed( WId, unsigned int )),
        this, TQT_SLOT( window_changed( WId, unsigned int )));
    }

void Window_trigger::activate( bool activate_P )
    {
    active = activate_P && khotkeys_active();
    }

void Window_trigger::window_added( WId window_P )
    {
    bool matches = windows()->match( Window_data( window_P ));
    existing_windows[ window_P ] = matches;
    kdDebug( 1217 ) << "Window_trigger::w_added() : " << matches << endl;
    if( active && matches && ( window_actions & WINDOW_APPEARS ))
        {
        windows_handler->set_action_window( window_P );
        data->execute();
        }
    }

void Window_trigger::window_removed( WId window_P )
    {
    if( existing_windows.contains( window_P ))
        {
        bool matches = existing_windows[ window_P ];
        kdDebug( 1217 ) << "Window_trigger::w_removed() : " << matches << endl;
        if( active && matches && ( window_actions & WINDOW_DISAPPEARS ))
            {
            windows_handler->set_action_window( window_P );
            data->execute();
            }
        existing_windows.remove( window_P );
        // CHECKME jenze co kdyz se window_removed zavola pred active_window_changed ?
        }
    else
        kdDebug( 1217 ) << "Window_trigger::w_removed()" << endl;
    }

void Window_trigger::active_window_changed( WId window_P )
    {
    bool was_match = false;
    if( existing_windows.contains( last_active_window ))
        was_match = existing_windows[ last_active_window ];
    if( active && was_match && ( window_actions & WINDOW_DEACTIVATES ))
        {
        windows_handler->set_action_window( window_P );
        data->execute();
        }
/*    bool matches = windows()->match( Window_data( window_P ));
    existing_windows[ window_P ] = matches;*/
    bool matches = existing_windows.contains( window_P )
        ? existing_windows[ window_P ] : false;
    if( active && matches && ( window_actions & WINDOW_ACTIVATES ))
        {
        windows_handler->set_action_window( window_P );
        data->execute();
        }
    kdDebug( 1217 ) << "Window_trigger::a_w_changed() : " << was_match << "|" << matches << endl;
    last_active_window = window_P;
    }

void Window_trigger::window_changed( WId window_P, unsigned int dirty_P )
    { // CHECKME snad nebude mit vliv, kdyz budu kaslat na properties_P a zkratka
      // kontrolovat kazdou zmenu
      // CHECKME kdyz se zmeni okno z match na non-match, asi to nebrat jako DISAPPEAR
    if( ! ( dirty_P & ( NET::WMName | NET::WMWindowType )))
        return;
    kdDebug( 1217 ) << "Window_trigger::w_changed()" << endl;
    bool was_match = false;
    if( existing_windows.contains( window_P ))
        was_match = existing_windows[ window_P ];
    bool matches = windows()->match( Window_data( window_P ));
    existing_windows[ window_P ] = matches;
    if( active && matches && !was_match )
        if( window_actions & WINDOW_APPEARS )
            {
            windows_handler->set_action_window( window_P );
            data->execute();
            }
        else if( window_actions & WINDOW_ACTIVATES && window_P == windows_handler->active_window())
            {
            windows_handler->set_action_window( window_P );
            data->execute();
            }
    kdDebug( 1217 ) << "Window_trigger::w_changed() : " << was_match << "|" << matches << endl;
    }

void Window_trigger::cfg_write( KConfig& cfg_P ) const
    {
    base::cfg_write( cfg_P );
    TQString save_cfg_group = cfg_P.group();
    cfg_P.setGroup( save_cfg_group + "Windows" );
    windows()->cfg_write( cfg_P );
    cfg_P.setGroup( save_cfg_group );
    cfg_P.writeEntry( "WindowActions", window_actions );
    cfg_P.writeEntry( "Type", "WINDOW" ); // overwrites value set in base::cfg_write()
    }

#ifndef COVARIANT_RETURN_BROKEN    // stupid gcc, it doesn't even warn it can't do this
Window_trigger* Window_trigger::copy( Action_data* data_P ) const
#else
Trigger* Window_trigger::copy( Action_data* data_P ) const
#endif
    {
    Window_trigger* ret = new Window_trigger( data_P ? data_P : data, windows()->copy(),
        window_actions );
    ret->existing_windows = existing_windows; // CHECKME je tohle vazne treba ?
    return ret;
    }

const TQString Window_trigger::description() const
    {
    return i18n( "Window trigger: " ) + windows()->comment();
    }

// Gesture_trigger

Gesture_trigger::Gesture_trigger( Action_data* data_P, const TQString &gesturecode_P )
    : Trigger( data_P ), _gesturecode( gesturecode_P )
    {
    }

Gesture_trigger::Gesture_trigger( KConfig& cfg_P, Action_data* data_P )
    : Trigger( cfg_P, data_P )
    {
    _gesturecode = cfg_P.readEntry( "Gesture" );
    }

Gesture_trigger::~Gesture_trigger()
    {
    gesture_handler->unregister_handler( this, TQT_SLOT( handle_gesture( const TQString&, WId )));
    }

void Gesture_trigger::cfg_write( KConfig& cfg_P ) const
    {
    base::cfg_write( cfg_P );
    cfg_P.writeEntry( "Gesture", gesturecode());
    cfg_P.writeEntry( "Type", "GESTURE" ); // overwrites value set in base::cfg_write()
    }

Trigger* Gesture_trigger::copy( Action_data* data_P ) const
    {
    kdDebug( 1217 ) << "Gesture_trigger::copy()" << endl;
    return new Gesture_trigger( data_P ? data_P : data, gesturecode());
    }

const TQString Gesture_trigger::description() const
    {
    return i18n( "Gesture trigger: " ) + gesturecode();
    }

void Gesture_trigger::handle_gesture( const TQString &gesture_P, WId window_P )
    {
    if( gesturecode() == gesture_P )
        {
        windows_handler->set_action_window( window_P );
        data->execute();
        }
    }

void Gesture_trigger::activate( bool activate_P )
    {
    if( activate_P )
        gesture_handler->register_handler( this, TQT_SLOT( handle_gesture( const TQString&, WId )));
    else
        gesture_handler->unregister_handler( this, TQT_SLOT( handle_gesture( const TQString&, WId )));
    }


// Voice_trigger

	Voice_trigger::Voice_trigger( Action_data* data_P, const TQString &Voicecode_P, const VoiceSignature& signature1_P, const VoiceSignature& signature2_P )
	: Trigger( data_P ), _voicecode( Voicecode_P )
    {
		_voicesignature[0]=signature1_P;
		_voicesignature[1]=signature2_P;
    }

Voice_trigger::Voice_trigger( KConfig& cfg_P, Action_data* data_P )
	: Trigger( cfg_P, data_P )
    {
    _voicecode = cfg_P.readEntry( "Name" );
	_voicesignature[0].read( &cfg_P , "Signature1" );
	_voicesignature[1].read( &cfg_P , "Signature2" );
    }

Voice_trigger::~Voice_trigger()
    {
    voice_handler->unregister_handler( this );
    }

void Voice_trigger::cfg_write( KConfig& cfg_P ) const
    {
    base::cfg_write( cfg_P );
    cfg_P.writeEntry( "Name", voicecode());
    cfg_P.writeEntry( "Type", "VOICE" ); // overwrites value set in base::cfg_write()
	_voicesignature[0].write( &cfg_P , "Signature1" );
	_voicesignature[1].write( &cfg_P , "Signature2" );
	}

Trigger* Voice_trigger::copy( Action_data* data_P ) const
    {
    kdDebug( 1217 ) << "Voice_trigger::copy()" << endl;
	return new Voice_trigger( data_P ? data_P : data, voicecode(), voicesignature(1) , voicesignature(2) );
    }

const TQString Voice_trigger::description() const
    {
    return i18n( "Voice trigger: " ) + voicecode();
    }

void Voice_trigger::handle_Voice(  )
    {
        windows_handler->set_action_window( 0 ); // use active window
        data->execute();

    }

void Voice_trigger::activate( bool activate_P )
    {
    if( activate_P && khotkeys_active())
		voice_handler->register_handler( this );
    else
        voice_handler->unregister_handler( this );
    }


} // namespace KHotKeys

#include "triggers.moc"