Changeset View
Changeset View
Standalone View
Standalone View
kcms/keyboard/daemon/keyboard_hardware.cpp
- This file was added.
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 <KModifierKeyInfo> | ||||
20 | #include <KConfigGroup> | ||||
21 | #include <KSharedConfig> | ||||
22 | | ||||
23 | #include <QDebug> | ||||
24 | #include <QX11Info> | ||||
25 | | ||||
26 | #include <math.h> | ||||
27 | | ||||
28 | #include "debug.h" | ||||
29 | #include "x11_helper.h" | ||||
30 | | ||||
31 | #include <X11/XKBlib.h> | ||||
32 | #include <X11/Xlib.h> | ||||
33 | #include <X11/keysym.h> | ||||
34 | | ||||
35 | // This code is taken from xset utility from XFree 4.3 (http://www.xfree86.org/) | ||||
36 | | ||||
37 | static void set_repeatrate(int delay, double rate) | ||||
38 | { | ||||
39 | if (!X11Helper::xkbSupported(nullptr)) { | ||||
40 | qCCritical(KCM_KEYBOARD) << "Failed to set keyboard repeat rate: xkb is not supported"; | ||||
41 | return; | ||||
42 | } | ||||
43 | | ||||
44 | XkbDescPtr xkb = XkbAllocKeyboard(); | ||||
45 | if (xkb) { | ||||
46 | Display* dpy = QX11Info::display(); | ||||
47 | | ||||
48 | XkbGetControls(dpy, XkbRepeatKeysMask, xkb); | ||||
49 | xkb->ctrls->repeat_delay = static_cast<unsigned short>(delay); | ||||
50 | xkb->ctrls->repeat_interval = static_cast<unsigned short>(floor(1000 / rate + 0.5)); | ||||
51 | | ||||
52 | XkbSetControls(dpy, XkbRepeatKeysMask, xkb); | ||||
53 | XkbFreeKeyboard(xkb, 0, true); | ||||
54 | return; | ||||
55 | } | ||||
56 | } | ||||
57 | | ||||
58 | enum class TriState { | ||||
59 | STATE_ON = 0, | ||||
60 | STATE_OFF = 1, | ||||
61 | STATE_UNCHANGED = 2 | ||||
62 | }; | ||||
63 | | ||||
64 | const int DEFAULT_REPEAT_DELAY = 600; | ||||
65 | const double DEFAULT_REPEAT_RATE = 25.0; | ||||
66 | | ||||
67 | static int set_repeat_mode(TriState keyboardRepeatMode) | ||||
68 | { | ||||
69 | XKeyboardState kbd; | ||||
70 | XKeyboardControl kbdc; | ||||
71 | | ||||
72 | XGetKeyboardControl(QX11Info::display(), &kbd); | ||||
73 | | ||||
74 | unsigned long flags = 0; | ||||
75 | if (keyboardRepeatMode != TriState::STATE_UNCHANGED) { | ||||
76 | flags |= KBAutoRepeatMode; | ||||
77 | kbdc.auto_repeat_mode = (keyboardRepeatMode == TriState::STATE_ON ? AutoRepeatModeOn : AutoRepeatModeOff); | ||||
78 | } | ||||
79 | | ||||
80 | return XChangeKeyboardControl(QX11Info::display(), flags, &kbdc); | ||||
81 | } | ||||
82 | | ||||
83 | void init_keyboard_hardware() | ||||
84 | { | ||||
85 | KConfigGroup config(KSharedConfig::openConfig(QStringLiteral("kcminputrc")), "Keyboard"); | ||||
86 | | ||||
87 | QString keyRepeatStr = config.readEntry("KeyboardRepeating", "0"); | ||||
88 | TriState keyRepeat = TriState::STATE_UNCHANGED; | ||||
89 | if (keyRepeatStr == QLatin1String("true") || keyRepeatStr == "0") { | ||||
90 | keyRepeat = TriState::STATE_ON; | ||||
91 | } else if (keyRepeatStr == QLatin1String("false") || keyRepeatStr == "1") { | ||||
92 | keyRepeat = TriState::STATE_OFF; | ||||
93 | } | ||||
94 | | ||||
95 | if (keyRepeat == TriState::STATE_ON) { | ||||
96 | int delay_ = config.readEntry<int>("RepeatDelay", DEFAULT_REPEAT_DELAY); | ||||
97 | double rate_ = config.readEntry<double>("RepeatRate", DEFAULT_REPEAT_RATE); | ||||
98 | set_repeatrate(delay_, rate_); | ||||
99 | } | ||||
100 | | ||||
101 | set_repeat_mode(keyRepeat); | ||||
102 | | ||||
103 | TriState numlockState = TriState(config.readEntry("NumLock", int(TriState::STATE_UNCHANGED))); | ||||
104 | if (numlockState != TriState::STATE_UNCHANGED) { | ||||
105 | KModifierKeyInfo keyInfo; | ||||
106 | keyInfo.setKeyLocked(Qt::Key_NumLock, numlockState == TriState::STATE_ON); | ||||
107 | } | ||||
108 | } |