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

« back to all changes in this revision

Viewing changes to kcontrol/keyboard/keyboard_hardware.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
 *  Copyright (C) 2010 Andriy Rysin (rysin@kde.org)
 
3
 *
 
4
 *  This program is free software; you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License as published by
 
6
 *  the Free Software Foundation; either version 2 of the License, or
 
7
 *  (at your option) any later version.
 
8
 *
 
9
 *  This program is distributed in the hope that it will be useful,
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *  GNU General Public License for more details.
 
13
 *
 
14
 *  You should have received a copy of the GNU General Public License
 
15
 *  along with this program; if not, write to the Free Software
 
16
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
17
 */
 
18
 
 
19
#include <kdebug.h>
 
20
#include <kconfiggroup.h>
 
21
#include <ksharedconfig.h>
 
22
 
 
23
#include <QtGui/QX11Info>
 
24
#include <QtGui/QCursor>        // WTF? - otherwise compiler complains
 
25
 
 
26
#include <X11/Xlib.h>
 
27
 
 
28
#include <math.h>
 
29
 
 
30
#include "x11_helper.h"
 
31
#include "kcmmisc.h"
 
32
 
 
33
// from numlockx.c
 
34
extern "C" void numlockx_change_numlock_state(Display* dpy, int state);
 
35
 
 
36
#include <X11/XKBlib.h>
 
37
#include <X11/keysym.h>
 
38
 
 
39
 
 
40
// This code is taken from xset utility from XFree 4.3 (http://www.xfree86.org/)
 
41
 
 
42
static
 
43
void set_repeatrate(int delay, double rate)
 
44
{
 
45
        if( !X11Helper::xkbSupported(NULL) ) {
 
46
                kError() << "Failed to set keyboard repeat rate: xkb is not supported";
 
47
                return;
 
48
        }
 
49
 
 
50
        XkbDescPtr xkb = XkbAllocKeyboard();
 
51
        if (xkb) {
 
52
                Display* dpy = QX11Info::display();
 
53
                int res = XkbGetControls(dpy, XkbRepeatKeysMask, xkb);
 
54
                xkb->ctrls->repeat_delay = delay;
 
55
                xkb->ctrls->repeat_interval = (int)floor(1000/rate + 0.5);
 
56
                res = XkbSetControls(dpy, XkbRepeatKeysMask, xkb);
 
57
                return;
 
58
        }
 
59
}
 
60
 
 
61
static
 
62
int set_volume(int clickVolumePercent, TriState keyboardRepeatMode)
 
63
{
 
64
        XKeyboardState   kbd;
 
65
        XKeyboardControl kbdc;
 
66
 
 
67
        XGetKeyboardControl(QX11Info::display(), &kbd);
 
68
 
 
69
        int flags = 0;
 
70
        if( clickVolumePercent != -1 ) {
 
71
                flags |= KBKeyClickPercent;
 
72
                kbdc.key_click_percent = clickVolumePercent;
 
73
        }
 
74
        if( keyboardRepeatMode != STATE_UNCHANGED ) {
 
75
                flags |= KBAutoRepeatMode;
 
76
                kbdc.auto_repeat_mode = (keyboardRepeatMode==STATE_ON ? AutoRepeatModeOn : AutoRepeatModeOff);
 
77
        }
 
78
 
 
79
        return XChangeKeyboardControl(QX11Info::display(), flags, &kbdc);
 
80
}
 
81
 
 
82
void init_keyboard_hardware()
 
83
{
 
84
    KConfigGroup config(KSharedConfig::openConfig( "kcminputrc" ), "Keyboard");
 
85
 
 
86
        QString keyRepeatStr = config.readEntry("KeyboardRepeating", TriStateHelper::getString(STATE_ON));
 
87
        TriState keyRepeat = STATE_UNCHANGED;
 
88
        if( keyRepeatStr == "true" || keyRepeatStr == TriStateHelper::getString(STATE_ON) ) {
 
89
                keyRepeat = STATE_ON;
 
90
        }
 
91
        else if( keyRepeatStr == "false" || keyRepeatStr == TriStateHelper::getString(STATE_OFF) ) {
 
92
                keyRepeat = STATE_OFF;
 
93
        }
 
94
 
 
95
        int clickVolumePercent = config.readEntry("ClickVolume", -1);
 
96
        if( clickVolumePercent != -1 && keyRepeat != STATE_UNCHANGED ) {
 
97
                set_volume(clickVolumePercent, keyRepeat);
 
98
        }
 
99
 
 
100
        if( keyRepeat == STATE_ON ) {
 
101
                int delay_ = config.readEntry("RepeatDelay", 250);
 
102
                double rate_ = config.readEntry("RepeatRate", 30.);
 
103
                set_repeatrate(delay_, rate_);
 
104
        }
 
105
 
 
106
 
 
107
        TriState numlockState = TriStateHelper::getTriState( config.readEntry( "NumLock", TriStateHelper::getInt(STATE_UNCHANGED) ) );
 
108
        if( numlockState != STATE_UNCHANGED ) {
 
109
                numlockx_change_numlock_state(QX11Info::display(), numlockState == STATE_ON );
 
110
        }
 
111
}