summaryrefslogtreecommitdiffstats
path: root/kxkb/kcmmisc.cpp
blob: 358d104500d14dbbed7b50cf595ae0e870480322 (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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*
 * kcmmisc.cpp
 *
 * Copyright (c) 1997 Patrick Dowler [email protected]
 *
 * Layout management, cleanups:
 * Copyright (c) 1999 Dirk A. Mueller <[email protected]>
 *
 * Requires the Qt widget libraries, available at no cost at
 * http://www.troll.no/
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */


#include <config.h>
#include <math.h>

#include <tqslider.h>
#include <tqfileinfo.h>
#include <tqcheckbox.h>
#include <tqstring.h>
#include <tqlayout.h>
#include <tqwhatsthis.h>
#include <tqvbuttongroup.h>
#include <tqradiobutton.h>

#include <tdelocale.h>
#include <tdeconfig.h>
#include <knuminput.h>
#include <tdeapplication.h>
#include <tdeglobal.h>
#include <kstandarddirs.h>
#include <kprocess.h>
#include <kdialog.h>

#include "kcmmisc.h"
#include "kcmmiscwidget.h"
#include <X11/Xlib.h>

KeyboardConfig::KeyboardConfig (TQWidget * parent, const char *)
    : TDECModule (parent, "kcmlayout")
{
  TQString wtstr;
  TQBoxLayout* lay = new TQVBoxLayout(this, 0, KDialog::spacingHint());
  ui = new KeyboardConfigWidget(this, "ui");
  lay->addWidget(ui);
  lay->addStretch();

  ui->click->setRange(0, 100, 10);
  ui->delay->setRange(100, 5000, 50, false);
  ui->rate->setRange(5, 50, 5, false);

  ui->delaySlider->setRange(1000, 50000);
  ui->delaySlider->setSteps(50, 500);
  ui->delaySlider->setTickInterval(2500);

  ui->rateSlider->setRange(500, 5000);
  ui->rateSlider->setSteps(50, 500);
  ui->rateSlider->setTickInterval(500);

  connect(ui->repeatBox, TQT_SIGNAL(clicked()), this, TQT_SLOT(changed()));
  connect(ui->delay, TQT_SIGNAL(valueChanged(int)), this, TQT_SLOT(delaySpinboxChanged(int)));
  connect(ui->delaySlider, TQT_SIGNAL(valueChanged(int)), this, TQT_SLOT(delaySliderChanged(int)));
  connect(ui->rate, TQT_SIGNAL(valueChanged(double)), this, TQT_SLOT(rateSpinboxChanged(double)));
  connect(ui->rateSlider, TQT_SIGNAL(valueChanged(int)), this, TQT_SLOT(rateSliderChanged(int)));

  connect(ui->click, TQT_SIGNAL(valueChanged(int)), this, TQT_SLOT(changed()));
  connect(ui->numlockGroup, TQT_SIGNAL(released(int)), this, TQT_SLOT(changed()));

#if !defined(HAVE_XTEST) && !defined(HAVE_XKB)
  ui->numlockGroup->setDisabled( true );
#endif
  load();
}

int  KeyboardConfig::getClick()
{
    return ui->click->value();
}

// set the slider and LCD values
void KeyboardConfig::setRepeat(int r, int delay_, double rate_)
{
    ui->repeatBox->setChecked(r == AutoRepeatModeOn);
    ui->delay->setValue(delay_);
    ui->rate->setValue(rate_);
}

void KeyboardConfig::setClick(int v)
{
    ui->click->setValue(v);
}

int KeyboardConfig::getNumLockState()
{
    TQButton* selected = ui->numlockGroup->selected();
    if( selected == NULL )
        return 2;
    int ret = ui->numlockGroup->id( selected );
    if( ret == -1 )
        ret = 2;
    return ret;
}

void KeyboardConfig::setNumLockState( int s )
{
    ui->numlockGroup->setButton( s );
}

void KeyboardConfig::load()
{
  TDEConfig config("kcminputrc");

  XKeyboardState kbd;

  XGetKeyboardControl(kapp->getDisplay(), &kbd);

  config.setGroup("Keyboard");
  bool key = config.readBoolEntry("KeyboardRepeating", true);
  keyboardRepeat = (key ? AutoRepeatModeOn : AutoRepeatModeOff);
  ui->delay->setValue(config.readNumEntry( "RepeatDelay", 500 ));
  ui->rate->setValue(config.readDoubleNumEntry( "RepeatRate", 30 ));
  clickVolume = config.readNumEntry("ClickVolume", kbd.key_click_percent);
  numlockState = config.readNumEntry( "NumLock", 2 );

  setClick(kbd.key_click_percent);
  setRepeat(kbd.global_auto_repeat, ui->delay->value(), ui->rate->value());
  setNumLockState( numlockState );
}

void KeyboardConfig::save()
{
  TDEConfig config("kcminputrc");

  XKeyboardControl kbd;

  clickVolume = getClick();
  keyboardRepeat = ui->repeatBox->isChecked() ? AutoRepeatModeOn : AutoRepeatModeOff;
  numlockState = getNumLockState();

  kbd.key_click_percent = clickVolume;
  kbd.auto_repeat_mode = keyboardRepeat;
  XChangeKeyboardControl(kapp->getDisplay(),
                           KBKeyClickPercent | KBAutoRepeatMode,
                           &kbd);
  if( keyboardRepeat ) {
    set_repeatrate(ui->delay->value(), ui->rate->value());
  }

  config.setGroup("Keyboard");
  config.writeEntry("ClickVolume",clickVolume);
  config.writeEntry("KeyboardRepeating", (keyboardRepeat == AutoRepeatModeOn));
  config.writeEntry("RepeatRate", ui->rate->value() );
  config.writeEntry("RepeatDelay", ui->delay->value() );
  config.writeEntry("NumLock", numlockState );
  config.sync();
}

void KeyboardConfig::defaults()
{
    setClick(50);
    setRepeat(true, 500, 30);
    setNumLockState( 2 );
}

TQString KeyboardConfig::quickHelp() const
{
  return TQString::null;

  /* "<h1>Keyboard</h1> This module allows you to choose options"
     " for the way in which your keyboard works. The actual effect of"
     " setting these options depends upon the features provided by your"
     " keyboard hardware and the X server on which KDE is running.<p>"
     " For example, you may find that changing the key click volume"
     " has no effect because this feature is not available on your system." */
}

void KeyboardConfig::delaySliderChanged (int value) {
	ui->delay->setValue(value/10);

	emit TDECModule::changed(true);
}

void KeyboardConfig::delaySpinboxChanged (int value) {
	ui->delaySlider->setValue ((int)(value*10));

	emit TDECModule::changed(true);
}

void KeyboardConfig::rateSliderChanged (int value) {
	ui->rate->setValue(value/100);

	emit TDECModule::changed(true);
}

void KeyboardConfig::rateSpinboxChanged (double value) {
	ui->rateSlider->setValue ((int)(value*100));

	emit TDECModule::changed(true);
}

void KeyboardConfig::changed()
{
  emit TDECModule::changed(true);
}

/*
 Originally comes from NumLockX http://dforce.sh.cvut.cz/~seli/en/numlockx

 NumLockX
 
 $Id$

 Copyright (C) 2000-2001 Lubos Lunak        <[email protected]>
 Copyright (C) 2001      Oswald Buddenhagen <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

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

#include <X11/Xlib.h>

#ifdef HAVE_XTEST
#include <X11/extensions/XTest.h>
#endif

#ifdef HAVE_XKB
#include <X11/XKBlib.h>
#endif

#include <X11/keysym.h>

#if defined(HAVE_XTEST) || defined(HAVE_XKB)

Display* dpy = tqt_xdisplay();

/* the XKB stuff is based on code created by Oswald Buddenhagen <[email protected]> */
#ifdef HAVE_XKB
int xkb_init()
    {
    int xkb_opcode, xkb_event, xkb_error;
    int xkb_lmaj = XkbMajorVersion;
    int xkb_lmin = XkbMinorVersion;
    return XkbLibraryVersion( &xkb_lmaj, &xkb_lmin )
        && XkbQueryExtension( dpy, &xkb_opcode, &xkb_event, &xkb_error,
			       &xkb_lmaj, &xkb_lmin );
    }

unsigned int xkb_mask_modifier( XkbDescPtr xkb, const char *name )
{
    int i;
    if( !xkb || !xkb->names )
	return 0;
    for( i = 0; i < XkbNumVirtualMods; i++ ) {
	char* modStr = XGetAtomName( xkb->dpy, xkb->names->vmods[i] );
	if( modStr == NULL ) {
	    continue;
	}
	if( strcmp(name, modStr) == 0 ) {
	    unsigned int mask;
	    XkbVirtualModsToReal( xkb, 1 << i, &mask );
	    XFree(modStr);
	    return mask;
	}
	XFree(modStr);
    }
    return 0;
}

unsigned int xkb_numlock_mask()
    {
    XkbDescPtr xkb;
    if(( xkb = XkbGetKeyboard( dpy, XkbAllComponentsMask, XkbUseCoreKbd )) != NULL )
	{
        unsigned int mask = xkb_mask_modifier( xkb, "NumLock" );
        XkbFreeKeyboard( xkb, 0, True );
        return mask;
        }
    return 0;
    }
        
int xkb_set_on()
    {
    unsigned int mask;
    if( !xkb_init())
        return 0;
    mask = xkb_numlock_mask();
    if( mask == 0 )
        return 0;
    XkbLockModifiers ( dpy, XkbUseCoreKbd, mask, mask);
    return 1;
    }
    
int xkb_set_off()
    {
    unsigned int mask;
    if( !xkb_init())
        return 0;
    mask = xkb_numlock_mask();
    if( mask == 0 )
        return 0;
    XkbLockModifiers ( dpy, XkbUseCoreKbd, mask, 0);
    return 1;
    }

int xkb_state()
    {
    unsigned int mask;
    unsigned int numlockState;
    XkbStateRec xkbState;
    if( !xkb_init())
        return 0;
    mask = xkb_numlock_mask();
    if( mask == 0 )
        return 0;
    XkbGetState( dpy, XkbUseCoreKbd, &xkbState);
    numlockState = xkbState.locked_mods & mask;
    return numlockState;
    }

int xkb_toggle()
    {
    unsigned int mask;
    unsigned int numlockState;
    XkbStateRec xkbState;
    if( !xkb_init())
        return 0;
    mask = xkb_numlock_mask();
    if( mask == 0 )
        return 0;
    XkbGetState( dpy, XkbUseCoreKbd, &xkbState);
    numlockState = xkbState.locked_mods & mask;
    if (numlockState)
        XkbLockModifiers ( dpy, XkbUseCoreKbd, mask, 0);
    else
        XkbLockModifiers ( dpy, XkbUseCoreKbd, mask, mask);
    return 1;
    }
    
#endif

#ifdef HAVE_XTEST
int xtest_get_numlock_state()
    {
    int i;
    int numlock_mask = 0;
    Window dummy1, dummy2;
    int dummy3, dummy4, dummy5, dummy6;
    unsigned int mask;
    XModifierKeymap* map = XGetModifierMapping( dpy );
    KeyCode numlock_keycode = XKeysymToKeycode( dpy, XK_Num_Lock );
    if( numlock_keycode == NoSymbol )
        return 0;
    for( i = 0;
         i < 8;
         ++i )
        {
	if( map->modifiermap[ map->max_keypermod * i ] == numlock_keycode )
		numlock_mask = 1 << i;
	}
    XQueryPointer( dpy, DefaultRootWindow( dpy ), &dummy1, &dummy2,
        &dummy3, &dummy4, &dummy5, &dummy6, &mask );
    XFreeModifiermap( map );
    return mask & numlock_mask;
    }

void xtest_change_numlock()
    {
    XTestFakeKeyEvent( dpy, XKeysymToKeycode( dpy, XK_Num_Lock ), True, CurrentTime );
    XTestFakeKeyEvent( dpy, XKeysymToKeycode( dpy, XK_Num_Lock ), False, CurrentTime );
    }

void xtest_set_on()
    {
    if( !xtest_get_numlock_state())
        xtest_change_numlock();
    }

void xtest_set_off()
    {
    if( xtest_get_numlock_state())
        xtest_change_numlock();
    }

void xtest_toggle()
    {
    xtest_change_numlock();
    }
#endif

void numlock_set_on()
    {
#ifdef HAVE_XKB
    if( xkb_set_on())
        return;
#endif
#ifdef HAVE_XTEST
    xtest_set_on();
#endif
    }

void numlock_set_off()
    {
#ifdef HAVE_XKB
    if( xkb_set_off())
        return;
#endif
#ifdef HAVE_XTEST
    xtest_set_off();
#endif
    }

void numlock_toggle()
    {
#ifdef HAVE_XKB
    if( xkb_toggle())
        return;
#endif
#ifdef HAVE_XTEST
#ifdef HAVE_XKB
    if (xkb_state())
        xtest_set_on();
    else
        xtest_set_off();
#else // HAVE_XKB
    xtest_toggle();
#endif // HAVE_XKB
#endif
    }
// This code is taken from xset utility from XFree 4.3 (http://www.xfree86.org/)

void numlockx_change_numlock_state( bool set_P )
    {
    if( set_P )
        numlock_set_on();
    else
        numlock_set_off();
    }
#else
void numlockx_change_numlock_state( bool ) {} // dummy
#endif // defined(HAVE_XTEST) || defined(HAVE_XKB)


#if 0
//HAVE_XF86MISC
#include <X11/extensions/xf86misc.h>
void set_repeatrate(int delay, double rate)
{
  Display* dpy = tqt_xdisplay();
  XF86MiscKbdSettings values;

  XF86MiscGetKbdSettings(dpy, &values);
  values.delay = delay;
  values.rate = rate;
  XF86MiscSetKbdSettings(dpy, &values);
  return;
}
#else
void set_repeatrate(int delay, double rate)
{
#if HAVE_XKB
  Display* dpy = tqt_xdisplay();
  int xkbmajor = XkbMajorVersion, xkbminor = XkbMinorVersion;
  int xkbopcode, xkbevent, xkberror;

  if (XkbQueryExtension(dpy, &xkbopcode, &xkbevent, &xkberror, &xkbmajor,
				&xkbminor)) {
     XkbDescPtr xkb = XkbAllocKeyboard();
     if (xkb) {
        int res = XkbGetControls(dpy, XkbRepeatKeysMask, xkb);
        xkb->ctrls->repeat_delay = delay;
        xkb->ctrls->repeat_interval = (int)floor(1000/rate + 0.5);
        res = XkbSetControls(dpy, XkbRepeatKeysMask, xkb);
        return;
     }
  }
#endif
  // Fallback: use the xset utility.

  // Unfortunately xset does only support int parameters, so
  // really slow repeat rates cannot be supported this way.
  // (the FSG Accessibility standard requires support for repeat rates
  // of several seconds per character)
  int r;
  if (rate < 1)
     r = 1;
  else
     r = (int)floor(rate + 0.5);

  TQString exe = TDEGlobal::dirs()->findExe("xset");
  if (exe.isEmpty())
    return;

  TDEProcess p;
  p << exe << "r" << "rate" << TQString::number(delay) << TQString::number(r);
  
  p.start(TDEProcess::Block);
}
#endif

void KeyboardConfig::init_keyboard()
{
	TDEConfig *config = new TDEConfig("kcminputrc", true); // Read-only, no globals
	config->setGroup("Keyboard");

	XKeyboardState   kbd;
	XKeyboardControl kbdc;

	XGetKeyboardControl(kapp->getDisplay(), &kbd);
	bool key = config->readBoolEntry("KeyboardRepeating", true);
	kbdc.key_click_percent = config->readNumEntry("ClickVolume", kbd.key_click_percent);
	kbdc.auto_repeat_mode = (key ? AutoRepeatModeOn : AutoRepeatModeOff);

	XChangeKeyboardControl(kapp->getDisplay(),
						   KBKeyClickPercent | KBAutoRepeatMode,
						   &kbdc);

	if( key ) {
		int delay_ = config->readNumEntry("RepeatDelay", 500);
		double rate_ = config->readDoubleNumEntry("RepeatRate", 30);
		set_repeatrate(delay_, rate_);
	}


	int numlockState = config->readNumEntry( "NumLock", 2 );
	if( numlockState != 2 )
		numlockx_change_numlock_state( numlockState == 0 );

	delete config;
}

#include "kcmmisc.moc"