kaccess.cpp 39.2 KB
Newer Older
Frederik Gladhorn's avatar
Frederik Gladhorn committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
    Copyright 2000 Matthias Hölzer-Klüpfel <hoelzer@kde.org>
    Copyright 2014 Frederik Gladhorn <gladhorn@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) version 3, or any
    later version accepted by the membership of KDE e.V. (or its
    successor approved by the membership of KDE e.V.), which shall
    act as a proxy defined in Section 6 of version 3 of the license.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

22
#include <unistd.h>
23
#include <cmath>
24 25 26

#include "kaccess.h"

27
#include <QtCore/qprocess.h>
28 29
#include <QTimer>
#include <QPainter>
Laurent Montel's avatar
Laurent Montel committed
30 31
#include <QMessageBox>
#include <QDesktopWidget>
32 33 34 35

#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
36 37
#include <QApplication>
#include <QAction>
38 39 40 41

#include <KComboBox>
#include <KNotification>
#include <KConfig>
42 43 44 45
#include <KConfigGroup>
#include <KSharedConfig>
#include <QDialog>
#include <QDialogButtonBox>
46
#include <kglobalaccel.h>
47
#include <KLocalizedString>
48
#include <ktoolinvocation.h>
49 50
#include <kwindowsystem.h>
#include <kkeyserver.h>
51 52
#include <KDBusService>
#include <KUserTimestamp>
53

54
#include <netwm.h>
55 56 57 58 59 60
#define XK_MISCELLANY
#define XK_XKB_KEYS
#include <X11/keysymdef.h>

#include <QX11Info>

61 62 63 64
#include <QLoggingCategory>

Q_LOGGING_CATEGORY(logKAccess, "kcm_kaccess")

65
struct ModifierKey {
Frederik Gladhorn's avatar
Frederik Gladhorn committed
66 67 68 69 70 71
    const unsigned int mask;
    const KeySym keysym;
    const char *name;
    const char *lockedText;
    const char *latchedText;
    const char *unlatchedText;
72 73
};

74
static const ModifierKey modifierKeys[] = {
Frederik Gladhorn's avatar
Frederik Gladhorn committed
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
    {
        ShiftMask, 0, "Shift",
        I18N_NOOP("The Shift key has been locked and is now active for all of the following keypresses."),
        I18N_NOOP("The Shift key is now active."),
        I18N_NOOP("The Shift key is now inactive.")
    },
    {
        ControlMask, 0, "Control",
        I18N_NOOP("The Control key has been locked and is now active for all of the following keypresses."),
        I18N_NOOP("The Control key is now active."),
        I18N_NOOP("The Control key is now inactive.")
    },
    {
        0, XK_Alt_L, "Alt",
        I18N_NOOP("The Alt key has been locked and is now active for all of the following keypresses."),
        I18N_NOOP("The Alt key is now active."),
        I18N_NOOP("The Alt key is now inactive.")
    },
    {
        0, 0, "Win",
        I18N_NOOP("The Win key has been locked and is now active for all of the following keypresses."),
        I18N_NOOP("The Win key is now active."),
        I18N_NOOP("The Win key is now inactive.")
    },
    {
        0, XK_Meta_L, "Meta",
        I18N_NOOP("The Meta key has been locked and is now active for all of the following keypresses."),
        I18N_NOOP("The Meta key is now active."),
        I18N_NOOP("The Meta key is now inactive.")
    },
    {
        0, XK_Super_L, "Super",
        I18N_NOOP("The Super key has been locked and is now active for all of the following keypresses."),
        I18N_NOOP("The Super key is now active."),
        I18N_NOOP("The Super key is now inactive.")
    },
    {
        0, XK_Hyper_L, "Hyper",
        I18N_NOOP("The Hyper key has been locked and is now active for all of the following keypresses."),
        I18N_NOOP("The Hyper key is now active."),
        I18N_NOOP("The Hyper key is now inactive.")
    },
    {
        0, 0, "Alt Graph",
        I18N_NOOP("The Alt Graph key has been locked and is now active for all of the following keypresses."),
        I18N_NOOP("The Alt Graph key is now active."),
        I18N_NOOP("The Alt Graph key is now inactive.")
    },
    {
        0, XK_Num_Lock, "Num Lock",
        I18N_NOOP("The Num Lock key has been activated."),
        "",
        I18N_NOOP("The Num Lock key is now inactive.")
    },
    {
        LockMask, 0, "Caps Lock",
        I18N_NOOP("The Caps Lock key has been activated."),
        "",
        I18N_NOOP("The Caps Lock key is now inactive.")
    },
    {
        0, XK_Scroll_Lock, "Scroll Lock",
        I18N_NOOP("The Scroll Lock key has been activated."),
        "",
        I18N_NOOP("The Scroll Lock key is now inactive.")
    },
141 142 143 144 145 146 147
    { 0, 0, "", "", "", "" }
};


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


148 149
KAccessApp::KAccessApp()
    : overlay(0), _player(0), toggleScreenReaderAction(new QAction(this))
150
{
151
    m_error = false;
Frederik Gladhorn's avatar
Frederik Gladhorn committed
152 153 154 155 156 157 158
    _activeWindow = KWindowSystem::activeWindow();
    connect(KWindowSystem::self(), &KWindowSystem::activeWindowChanged, this, &KAccessApp::activeWindowChanged);

    features = 0;
    requestedFeatures = 0;
    dialog = 0;

159 160 161 162 163
    if (!QX11Info::isPlatformX11()) {
        m_error = true;
        return;
    }

Frederik Gladhorn's avatar
Frederik Gladhorn committed
164 165 166 167 168 169
    initMasks();
    XkbStateRec state_return;
    XkbGetState(QX11Info::display(), XkbUseCoreKbd, &state_return);
    unsigned char latched = XkbStateMods(&state_return);
    unsigned char locked  = XkbModLocks(&state_return);
    state = ((int)locked) << 8 | latched;
170

171 172 173 174
    auto service = new KDBusService(KDBusService::Unique, this);
    connect(service, &KDBusService::activateRequested, this, &KAccessApp::newInstance);

    QTimer::singleShot(0, this, &KAccessApp::readSettings);
175 176
}

177
void KAccessApp::newInstance()
178
{
Frederik Gladhorn's avatar
Frederik Gladhorn committed
179 180
    KSharedConfig::openConfig()->reparseConfiguration();
    readSettings();
181 182 183 184
}

void KAccessApp::readSettings()
{
Frederik Gladhorn's avatar
Frederik Gladhorn committed
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
    KSharedConfig::Ptr _config = KSharedConfig::openConfig();
    KConfigGroup cg(_config, "Bell");

    // bell ---------------------------------------------------------------
    _systemBell = cg.readEntry("SystemBell", true);
    _artsBell = cg.readEntry("ArtsBell", false);
    _currentPlayerSource = cg.readPathEntry("ArtsBellFile", QString());
    _visibleBell = cg.readEntry("VisibleBell", false);
    _visibleBellInvert = cg.readEntry("VisibleBellInvert", false);
    _visibleBellColor = cg.readEntry("VisibleBellColor", QColor(Qt::red));
    _visibleBellPause = cg.readEntry("VisibleBellPause", 500);

    // select bell events if we need them
    int state = (_artsBell || _visibleBell) ? XkbBellNotifyMask : 0;
    XkbSelectEvents(QX11Info::display(), XkbUseCoreKbd, XkbBellNotifyMask, state);

    // deactivate system bell if not needed
    if (!_systemBell)
        XkbChangeEnabledControls(QX11Info::display(), XkbUseCoreKbd, XkbAudibleBellMask, 0);
    else
        XkbChangeEnabledControls(QX11Info::display(), XkbUseCoreKbd, XkbAudibleBellMask, XkbAudibleBellMask);

    // keyboard -------------------------------------------------------------
    KConfigGroup keyboardGroup(_config, "Keyboard");

    // get keyboard state
    XkbDescPtr xkb = XkbGetMap(QX11Info::display(), 0, XkbUseCoreKbd);
    if (!xkb)
        return;
    if (XkbGetControls(QX11Info::display(), XkbAllControlsMask, xkb) != Success)
        return;

    // sticky keys
    if (keyboardGroup.readEntry("StickyKeys", false)) {
        if (keyboardGroup.readEntry("StickyKeysLatch", true))
            xkb->ctrls->ax_options |= XkbAX_LatchToLockMask;
        else
            xkb->ctrls->ax_options &= ~XkbAX_LatchToLockMask;
        if (keyboardGroup.readEntry("StickyKeysAutoOff", false))
            xkb->ctrls->ax_options |= XkbAX_TwoKeysMask;
        else
            xkb->ctrls->ax_options &= ~XkbAX_TwoKeysMask;
        if (keyboardGroup.readEntry("StickyKeysBeep", false))
            xkb->ctrls->ax_options |= XkbAX_StickyKeysFBMask;
        else
            xkb->ctrls->ax_options &= ~XkbAX_StickyKeysFBMask;
        xkb->ctrls->enabled_ctrls |= XkbStickyKeysMask;
    } else
        xkb->ctrls->enabled_ctrls &= ~XkbStickyKeysMask;

    // toggle keys
    if (keyboardGroup.readEntry("ToggleKeysBeep", false))
        xkb->ctrls->ax_options |= XkbAX_IndicatorFBMask;
    else
        xkb->ctrls->ax_options &= ~XkbAX_IndicatorFBMask;

    // slow keys
    if (keyboardGroup.readEntry("SlowKeys", false)) {
        if (keyboardGroup.readEntry("SlowKeysPressBeep", false))
            xkb->ctrls->ax_options |= XkbAX_SKPressFBMask;
        else
            xkb->ctrls->ax_options &= ~XkbAX_SKPressFBMask;
        if (keyboardGroup.readEntry("SlowKeysAcceptBeep", false))
            xkb->ctrls->ax_options |= XkbAX_SKAcceptFBMask;
        else
            xkb->ctrls->ax_options &= ~XkbAX_SKAcceptFBMask;
        if (keyboardGroup.readEntry("SlowKeysRejectBeep", false))
            xkb->ctrls->ax_options |= XkbAX_SKRejectFBMask;
        else
            xkb->ctrls->ax_options &= ~XkbAX_SKRejectFBMask;
        xkb->ctrls->enabled_ctrls |= XkbSlowKeysMask;
    } else
        xkb->ctrls->enabled_ctrls &= ~XkbSlowKeysMask;
    xkb->ctrls->slow_keys_delay = keyboardGroup.readEntry("SlowKeysDelay", 500);

    // bounce keys
    if (keyboardGroup.readEntry("BounceKeys", false)) {
        if (keyboardGroup.readEntry("BounceKeysRejectBeep", false))
            xkb->ctrls->ax_options |= XkbAX_BKRejectFBMask;
        else
            xkb->ctrls->ax_options &= ~XkbAX_BKRejectFBMask;
        xkb->ctrls->enabled_ctrls |= XkbBounceKeysMask;
    } else
        xkb->ctrls->enabled_ctrls &= ~XkbBounceKeysMask;
    xkb->ctrls->debounce_delay = keyboardGroup.readEntry("BounceKeysDelay", 500);

    // gestures for enabling the other features
    _gestures = keyboardGroup.readEntry("Gestures", false);
    if (_gestures)
        xkb->ctrls->enabled_ctrls |= XkbAccessXKeysMask;
    else
        xkb->ctrls->enabled_ctrls &= ~XkbAccessXKeysMask;

    // timeout
    if (keyboardGroup.readEntry("AccessXTimeout", false)) {
        xkb->ctrls->ax_timeout = keyboardGroup.readEntry("AccessXTimeoutDelay", 30) * 60;
        xkb->ctrls->axt_opts_mask = 0;
        xkb->ctrls->axt_opts_values = 0;
        xkb->ctrls->axt_ctrls_mask = XkbStickyKeysMask | XkbSlowKeysMask;
        xkb->ctrls->axt_ctrls_values = 0;
        xkb->ctrls->enabled_ctrls |= XkbAccessXTimeoutMask;
    } else
        xkb->ctrls->enabled_ctrls &= ~XkbAccessXTimeoutMask;

    // gestures for enabling the other features
    if (keyboardGroup.readEntry("AccessXBeep", true))
        xkb->ctrls->ax_options |= XkbAX_FeatureFBMask | XkbAX_SlowWarnFBMask;
    else
        xkb->ctrls->ax_options &= ~(XkbAX_FeatureFBMask | XkbAX_SlowWarnFBMask);

    _gestureConfirmation = keyboardGroup.readEntry("GestureConfirmation", false);

    _kNotifyModifiers = keyboardGroup.readEntry("kNotifyModifiers", false);
    _kNotifyAccessX = keyboardGroup.readEntry("kNotifyAccessX", false);

    // mouse-by-keyboard ----------------------------------------------

    KConfigGroup mouseGroup(_config, "Mouse");

    if (mouseGroup.readEntry("MouseKeys", false)) {
        xkb->ctrls->mk_delay = mouseGroup.readEntry("MKDelay", 160);

        // Default for initial velocity: 200 pixels/sec
        int interval = mouseGroup.readEntry("MKInterval", 5);
        xkb->ctrls->mk_interval = interval;

        // Default time to reach maximum speed: 5000 msec
        xkb->ctrls->mk_time_to_max = mouseGroup.readEntry("MKTimeToMax",
                                     (5000 + interval / 2) / interval);

        // Default maximum speed: 1000 pixels/sec
        //     (The old default maximum speed from KDE <= 3.4
        //     (100000 pixels/sec) was way too fast)
        xkb->ctrls->mk_max_speed = mouseGroup.readEntry("MKMaxSpeed", interval);

        xkb->ctrls->mk_curve = mouseGroup.readEntry("MKCurve", 0);
        xkb->ctrls->mk_dflt_btn = mouseGroup.readEntry("MKDefaultButton", 0);

        xkb->ctrls->enabled_ctrls |= XkbMouseKeysMask;
    } else
        xkb->ctrls->enabled_ctrls &= ~XkbMouseKeysMask;

    features = xkb->ctrls->enabled_ctrls & (XkbSlowKeysMask | XkbBounceKeysMask | XkbStickyKeysMask | XkbMouseKeysMask);
    if (dialog == 0)
        requestedFeatures = features;
    // set state
    XkbSetControls(QX11Info::display(), XkbControlsEnabledMask | XkbMouseKeysAccelMask | XkbStickyKeysMask | XkbSlowKeysMask | XkbBounceKeysMask | XkbAccessXKeysMask | XkbAccessXTimeoutMask, xkb);

    // select AccessX events
    XkbSelectEvents(QX11Info::display(), XkbUseCoreKbd, XkbAllEventsMask, XkbAllEventsMask);

    if (!_artsBell && !_visibleBell && !(_gestures && _gestureConfirmation)
        && !_kNotifyModifiers && !_kNotifyAccessX) {

        uint ctrls = XkbStickyKeysMask | XkbSlowKeysMask | XkbBounceKeysMask | XkbMouseKeysMask | XkbAudibleBellMask | XkbControlsNotifyMask;
        uint values = xkb->ctrls->enabled_ctrls & ctrls;
        XkbSetAutoResetControls(QX11Info::display(), ctrls, &ctrls, &values);
    } else {
        // reset them after program exit
        uint ctrls = XkbStickyKeysMask | XkbSlowKeysMask | XkbBounceKeysMask | XkbMouseKeysMask | XkbAudibleBellMask | XkbControlsNotifyMask;
        uint values = XkbAudibleBellMask;
        XkbSetAutoResetControls(QX11Info::display(), ctrls, &ctrls, &values);
347 348
    }

Frederik Gladhorn's avatar
Frederik Gladhorn committed
349 350
    delete overlay;
    overlay = 0;
351 352 353 354 355 356 357 358

    KConfigGroup screenReaderGroup(_config, "ScreenReader");
    setScreenReaderEnabled(screenReaderGroup.readEntry("Enabled", false));

    QString shortcut = screenReaderGroup.readEntry("Shortcut", QStringLiteral("Meta+Alt+S"));

    toggleScreenReaderAction->setText(i18n("Toggle Screen Reader On and Off"));
    toggleScreenReaderAction->setObjectName("Toggle Screen Reader On and Off");
359
    toggleScreenReaderAction->setProperty("componentDisplayName", i18nc("Name for kaccess shortcuts category", "Accessibility"));
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    KGlobalAccel::self()->setGlobalShortcut(toggleScreenReaderAction,
                                      QKeySequence(shortcut));
    connect(toggleScreenReaderAction, &QAction::triggered, this, &KAccessApp::toggleScreenReader);
}

void KAccessApp::toggleScreenReader()
{
    KSharedConfig::Ptr _config = KSharedConfig::openConfig();
    KConfigGroup screenReaderGroup(_config, "ScreenReader");
    bool enabled = !screenReaderGroup.readEntry("Enabled", false);
    screenReaderGroup.writeEntry("Enabled", enabled);
    setScreenReaderEnabled(enabled);
}

void KAccessApp::setScreenReaderEnabled(bool enabled)
{
    if (enabled) {
377
        QStringList args = { "set", "org.gnome.desktop.a11y.applications", "screen-reader-enabled", "true"};
378 379
        int ret = QProcess::execute("gsettings", args);
        if (ret == 0) {
380
            qint64 pid = 0;
381
            QProcess::startDetached("orca", {"--replace"}, QString(), &pid);
382
            qCDebug(logKAccess) << "Launching Orca, pid:" << pid;
383 384
        }
    } else {
385
        QProcess::startDetached("gsettings", { "set", "org.gnome.desktop.a11y.applications", "screen-reader-enabled", "false"});
386
    }
Frederik Gladhorn's avatar
Frederik Gladhorn committed
387
}
388

Frederik Gladhorn's avatar
Frederik Gladhorn committed
389 390 391 392 393 394 395
static int maskToBit(int mask)
{
    for (int i = 0; i < 8; i++)
        if (mask & (1 << i))
            return i;
    return -1;
}
396

Frederik Gladhorn's avatar
Frederik Gladhorn committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
void KAccessApp::initMasks()
{
    for (int i = 0; i < 8; i++)
        keys [i] = -1;
    state = 0;

    for (int i = 0; strcmp(modifierKeys[i].name, "") != 0; i++) {
        int mask = modifierKeys[i].mask;
        if (mask == 0) {
            if (modifierKeys[i].keysym != 0) {
                mask = XkbKeysymToModifiers(QX11Info::display(), modifierKeys[i].keysym);
            } else {
                if (!strcmp(modifierKeys[i].name, "Win")) {
                    mask = KKeyServer::modXMeta();
                } else {
                    mask = XkbKeysymToModifiers(QX11Info::display(), XK_Mode_switch)
                           | XkbKeysymToModifiers(QX11Info::display(), XK_ISO_Level3_Shift)
                           | XkbKeysymToModifiers(QX11Info::display(), XK_ISO_Level3_Latch)
                           | XkbKeysymToModifiers(QX11Info::display(), XK_ISO_Level3_Lock);
                }
            }
        }
419 420


Frederik Gladhorn's avatar
Frederik Gladhorn committed
421 422 423
        int bit = maskToBit(mask);
        if (bit != -1 && keys[bit] == -1)
            keys[bit] = i;
424 425 426
    }
}

427 428 429 430 431 432 433 434 435
struct xkb_any_ {
    uint8_t response_type;
    uint8_t xkbType;
    uint16_t sequence;
    xcb_timestamp_t time;
    uint8_t deviceID;
};

bool KAccessApp::nativeEventFilter(const QByteArray& eventType, void* message, long int* result)
436
{
437 438 439 440
    if (eventType == "xcb_generic_event_t") {
        xcb_generic_event_t* event = static_cast<xcb_generic_event_t *>(message);
        if ((event->response_type & ~0x80) == XkbEventCode + xkb_opcode) {
            xkb_any_ *ev = reinterpret_cast<xkb_any_*>(event);
441 442 443 444 445 446 447 448
            // Workaround for an XCB bug. xkbType comes from an EventType that is defined with bits, like
            // <item name="BellNotify">             <bit>8</bit>
            // while the generated XCB event type enum is defined as a bitmask, like
            //     XCB_XKB_EVENT_TYPE_BELL_NOTIFY = 256
            // This means if xbkType is 8, we need to set the 8th bit to 1, thus raising 2 to power of 8.
            // See also https://bugs.freedesktop.org/show_bug.cgi?id=51295
            const int eventType = pow(2, ev->xkbType);
            switch (eventType) {
449 450 451 452 453 454 455 456 457 458 459
                case XCB_XKB_EVENT_TYPE_STATE_NOTIFY:
                    xkbStateNotify();
                    break;
                case XCB_XKB_EVENT_TYPE_BELL_NOTIFY:
                    xkbBellNotify(reinterpret_cast<xcb_xkb_bell_notify_event_t*>(event));
                    break;
                case XCB_XKB_EVENT_TYPE_CONTROLS_NOTIFY:
                    xkbControlsNotify(reinterpret_cast<xcb_xkb_controls_notify_event_t*>(event));
                    break;
            }
            return true;
Frederik Gladhorn's avatar
Frederik Gladhorn committed
460
        }
461
    }
462
    return false;
463 464 465 466 467
}


void VisualBell::paintEvent(QPaintEvent *event)
{
Frederik Gladhorn's avatar
Frederik Gladhorn committed
468
    QWidget::paintEvent(event);
469
    QTimer::singleShot(_pause, this, &QWidget::hide);
470 471 472 473 474
}


void KAccessApp::activeWindowChanged(WId wid)
{
Frederik Gladhorn's avatar
Frederik Gladhorn committed
475
    _activeWindow = wid;
476 477 478
}


Frederik Gladhorn's avatar
Frederik Gladhorn committed
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
void KAccessApp::xkbStateNotify()
{
    XkbStateRec state_return;
    XkbGetState(QX11Info::display(), XkbUseCoreKbd, &state_return);
    unsigned char latched = XkbStateMods(&state_return);
    unsigned char locked  = XkbModLocks(&state_return);
    int mods = ((int)locked) << 8 | latched;

    if (state != mods) {
        if (_kNotifyModifiers)
            for (int i = 0; i < 8; i++) {
                if (keys[i] != -1) {
                    if (!strcmp(modifierKeys[keys[i]].latchedText, "")
                        && ((((mods >> i) & 0x101) != 0) != (((state >> i) & 0x101) != 0))) {
                        if ((mods >> i) & 1) {
                            KNotification::event("lockkey-locked", i18n(modifierKeys[keys[i]].lockedText));
                        } else {
                            KNotification::event("lockkey-unlocked", i18n(modifierKeys[keys[i]].unlatchedText));
                        }
                    } else if (strcmp(modifierKeys[keys[i]].latchedText, "")
                               && (((mods >> i) & 0x101) != ((state >> i) & 0x101))) {
                        if ((mods >> i) & 0x100) {
                            KNotification::event("modifierkey-locked", i18n(modifierKeys[keys[i]].lockedText));
                        } else if ((mods >> i) & 1) {
                            KNotification::event("modifierkey-latched", i18n(modifierKeys[keys[i]].latchedText));
                        } else {
                            KNotification::event("modifierkey-unlatched", i18n(modifierKeys[keys[i]].unlatchedText));
                        }
                    }
                }
509
            }
Frederik Gladhorn's avatar
Frederik Gladhorn committed
510 511
        state = mods;
    }
512 513
}

514
void KAccessApp::xkbBellNotify(xcb_xkb_bell_notify_event_t *event)
515
{
Frederik Gladhorn's avatar
Frederik Gladhorn committed
516
    // bail out if we should not really ring
517
    if (event->eventOnly)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
518
        return;
519

Frederik Gladhorn's avatar
Frederik Gladhorn committed
520 521 522 523 524
    // flash the visible bell
    if (_visibleBell) {
        // create overlay widget
        if (!overlay)
            overlay = new VisualBell(_visibleBellPause);
525

Frederik Gladhorn's avatar
Frederik Gladhorn committed
526
        WId id = _activeWindow;
527

Frederik Gladhorn's avatar
Frederik Gladhorn committed
528
        NETRect frame, window;
529
        NETWinInfo net(QX11Info::connection(), id, qApp->desktop()->winId(), 0);
530

Frederik Gladhorn's avatar
Frederik Gladhorn committed
531
        net.kdeGeometry(frame, window);
532

Frederik Gladhorn's avatar
Frederik Gladhorn committed
533
        overlay->setGeometry(window.pos.x, window.pos.y, window.size.width, window.size.height);
534

Frederik Gladhorn's avatar
Frederik Gladhorn committed
535 536
        if (_visibleBellInvert) {
            QPixmap screen = QPixmap::grabWindow(id, 0, 0, window.size.width, window.size.height);
537 538 539
#ifdef __GNUC__
#warning is this the best way to invert a pixmap?
#endif
Frederik Gladhorn's avatar
Frederik Gladhorn committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
//    QPixmap invert(window.size.width, window.size.height);
            QImage i = screen.toImage();
            i.invertPixels();
            QPalette pal = overlay->palette();
            pal.setBrush(overlay->backgroundRole(), QBrush(QPixmap::fromImage(i)));
            overlay->setPalette(pal);
            /*
                  QPainter p(&invert);
                  p.setRasterOp(QPainter::NotCopyROP);
                  p.drawPixmap(0, 0, screen);
                  overlay->setBackgroundPixmap(invert);
            */
        } else {
            QPalette pal = overlay->palette();
            pal.setColor(overlay->backgroundRole(), _visibleBellColor);
            overlay->setPalette(pal);
556 557
        }

Frederik Gladhorn's avatar
Frederik Gladhorn committed
558 559 560
        // flash the overlay widget
        overlay->raise();
        overlay->show();
561
        qApp->flush();
562 563
    }

Frederik Gladhorn's avatar
Frederik Gladhorn committed
564 565 566 567 568 569 570 571
    // ask Phonon to ring a nice bell
    if (_artsBell) {
        if (!_player) { // as creating the player is expensive, delay the creation
            _player = Phonon::createPlayer(Phonon::AccessibilityCategory);
            _player->setParent(this);
            _player->setCurrentSource(_currentPlayerSource);
        }
        _player->play();
572 573 574
    }
}

Frederik Gladhorn's avatar
Frederik Gladhorn committed
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
QString mouseKeysShortcut(Display *display)
{
    // Calculate the keycode
    KeySym sym = XK_MouseKeys_Enable;
    KeyCode code = XKeysymToKeycode(display, sym);
    if (code == 0) {
        sym = XK_Pointer_EnableKeys;
        code = XKeysymToKeycode(display, sym);
        if (code == 0)
            return QString(); // No shortcut available?
    }

    // Calculate the modifiers by searching the keysym in the X keyboard mapping
    XkbDescPtr xkbdesc = XkbGetMap(display, XkbKeyTypesMask | XkbKeySymsMask, XkbUseCoreKbd);

    if (!xkbdesc)
        return QString(); // Failed to obtain the mapping from server

    bool found = false;
    unsigned char modifiers = 0;
    int groups = XkbKeyNumGroups(xkbdesc, code);
    for (int grp = 0; grp < groups && !found; grp++) {
        int levels = XkbKeyGroupWidth(xkbdesc, code, grp);
        for (int level = 0; level < levels && !found; level++) {
            if (sym == XkbKeySymEntry(xkbdesc, code, level, grp)) {
                // keysym found => determine modifiers
                int typeIdx = xkbdesc->map->key_sym_map[code].kt_index[grp];
                XkbKeyTypePtr type = &(xkbdesc->map->types[typeIdx]);
                for (int i = 0; i < type->map_count && !found; i++) {
                    if (type->map[i].active && (type->map[i].level == level)) {
                        modifiers = type->map[i].mods.mask;
                        found = true;
                    }
                }
            }
610
        }
Frederik Gladhorn's avatar
Frederik Gladhorn committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
    }
    XkbFreeClientMap(xkbdesc, 0, true);

    if (!found)
        return QString(); // Somehow the keycode -> keysym mapping is flawed

    XEvent ev;
    ev.type = KeyPress;
    ev.xkey.display = display;
    ev.xkey.keycode = code;
    ev.xkey.state = 0;
    int key;
    KKeyServer::xEventToQt(&ev, &key);
    QString keyname = QKeySequence(key).toString();

    unsigned int AltMask   = KKeyServer::modXAlt();
    unsigned int WinMask   = KKeyServer::modXMeta();
    unsigned int NumMask   = KKeyServer::modXNumLock();
    unsigned int ScrollMask = KKeyServer::modXScrollLock();

    unsigned int MetaMask  = XkbKeysymToModifiers(display, XK_Meta_L);
    unsigned int SuperMask = XkbKeysymToModifiers(display, XK_Super_L);
    unsigned int HyperMask = XkbKeysymToModifiers(display, XK_Hyper_L);
    unsigned int AltGrMask = XkbKeysymToModifiers(display, XK_Mode_switch)
                             | XkbKeysymToModifiers(display, XK_ISO_Level3_Shift)
                             | XkbKeysymToModifiers(display, XK_ISO_Level3_Latch)
                             | XkbKeysymToModifiers(display, XK_ISO_Level3_Lock);

    unsigned int mods = ShiftMask | ControlMask | AltMask | WinMask
                        | LockMask | NumMask | ScrollMask;

    AltGrMask &= ~mods;
    MetaMask  &= ~(mods | AltGrMask);
    SuperMask &= ~(mods | AltGrMask | MetaMask);
    HyperMask &= ~(mods | AltGrMask | MetaMask | SuperMask);

    if ((modifiers & AltGrMask) != 0)
        keyname = i18n("AltGraph") + '+' + keyname;
    if ((modifiers & HyperMask) != 0)
        keyname = i18n("Hyper") + '+' + keyname;
    if ((modifiers & SuperMask) != 0)
        keyname = i18n("Super") + '+' + keyname;
    if ((modifiers & WinMask) != 0)
        keyname = i18n("Meta") + '+' + keyname;
    if ((modifiers & WinMask) != 0)
        keyname = QKeySequence(Qt::META).toString() + '+' + keyname;
    if ((modifiers & AltMask) != 0)
        keyname = QKeySequence(Qt::ALT).toString() + '+' + keyname;
    if ((modifiers & ControlMask) != 0)
        keyname = QKeySequence(Qt::CTRL).toString() + '+' + keyname;
    if ((modifiers & ShiftMask) != 0)
        keyname = QKeySequence(Qt::SHIFT).toString() + '+' + keyname;

    return keyname;
665 666
}

Frederik Gladhorn's avatar
Frederik Gladhorn committed
667 668 669
void KAccessApp::createDialogContents()
{
    if (dialog == 0) {
670 671
        dialog = new QDialog(nullptr);
        dialog->setWindowTitle(i18n("Warning"));
Frederik Gladhorn's avatar
Frederik Gladhorn committed
672 673 674
        dialog->setObjectName("AccessXWarning");
        dialog->setModal(true);

675
        QVBoxLayout *topLayout = new QVBoxLayout();
676

677
        QHBoxLayout * lay = new QHBoxLayout();
678

679 680
        QLabel *label1 = new QLabel();
        QIcon icon = QIcon::fromTheme(QStringLiteral("dialog-warning"));
681 682 683
        if (icon.isNull())
            icon = QMessageBox::standardIcon(QMessageBox::Warning);
        label1->setPixmap(icon.pixmap(64, 64));
684

Frederik Gladhorn's avatar
Frederik Gladhorn committed
685
        lay->addWidget(label1, 0, Qt::AlignCenter);
686

Frederik Gladhorn's avatar
Frederik Gladhorn committed
687 688
        QVBoxLayout * vlay = new QVBoxLayout();
        lay->addItem(vlay);
689

690
        featuresLabel = new QLabel();
Frederik Gladhorn's avatar
Frederik Gladhorn committed
691 692 693 694
        featuresLabel->setAlignment(Qt::AlignVCenter);
        featuresLabel->setWordWrap(true);
        vlay->addWidget(featuresLabel);
        vlay->addStretch();
695

Frederik Gladhorn's avatar
Frederik Gladhorn committed
696 697
        QHBoxLayout * hlay = new QHBoxLayout();
        vlay->addItem(hlay);
698

699
        QLabel *showModeLabel = new QLabel(i18n("&When a gesture was used:"));
Frederik Gladhorn's avatar
Frederik Gladhorn committed
700
        hlay->addWidget(showModeLabel);
701

702
        showModeCombobox = new KComboBox();
Frederik Gladhorn's avatar
Frederik Gladhorn committed
703 704 705 706 707 708
        hlay->addWidget(showModeCombobox);
        showModeLabel->setBuddy(showModeCombobox);
        showModeCombobox->insertItem(0, i18n("Change Settings Without Asking"));
        showModeCombobox->insertItem(1, i18n("Show This Confirmation Dialog"));
        showModeCombobox->insertItem(2, i18n("Deactivate All AccessX Features & Gestures"));
        showModeCombobox->setCurrentIndex(1);
709
        topLayout->addLayout(lay);
710

711
        auto buttons = new QDialogButtonBox(QDialogButtonBox::Yes | QDialogButtonBox::No, dialog);
712 713 714

        topLayout->addWidget(buttons);
        dialog->setLayout(topLayout);
715

716 717 718 719
        connect(buttons, &QDialogButtonBox::accepted, dialog, &QDialog::accept);
        connect(buttons, &QDialogButtonBox::rejected, dialog, &QDialog::reject);
        connect(dialog, &QDialog::accepted, this, &KAccessApp::yesClicked);
        connect(dialog, &QDialog::rejected, this, &KAccessApp::noClicked);
Frederik Gladhorn's avatar
Frederik Gladhorn committed
720
    }
721 722
}

723
void KAccessApp::xkbControlsNotify(xcb_xkb_controls_notify_event_t *event)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
724
{
725 726 727 728 729
    unsigned int newFeatures = event->enabledControls & (
        XCB_XKB_BOOL_CTRL_SLOW_KEYS |
        XCB_XKB_BOOL_CTRL_BOUNCE_KEYS |
        XCB_XKB_BOOL_CTRL_STICKY_KEYS  |
        XCB_XKB_BOOL_CTRL_MOUSE_KEYS);
Frederik Gladhorn's avatar
Frederik Gladhorn committed
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

    if (newFeatures != features) {
        unsigned int enabled  = newFeatures & ~features;
        unsigned int disabled = features & ~newFeatures;

        if (!_gestureConfirmation) {
            requestedFeatures = enabled | (requestedFeatures & ~disabled);
            notifyChanges();
            features = newFeatures;
        } else {
            // set the AccessX features back to what they were. We will
            // apply the changes later if the user allows us to do that.
            readSettings();

            requestedFeatures = enabled | (requestedFeatures & ~disabled);

            enabled  = requestedFeatures & ~features;
            disabled = features & ~requestedFeatures;

            QStringList enabledFeatures;
            QStringList disabledFeatures;

752
            if (enabled & XCB_XKB_BOOL_CTRL_SLOW_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
753
                enabledFeatures << i18n("Slow keys");
754
            else if (disabled & XCB_XKB_BOOL_CTRL_SLOW_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
755 756
                disabledFeatures << i18n("Slow keys");

757
            if (enabled & XCB_XKB_BOOL_CTRL_BOUNCE_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
758
                enabledFeatures << i18n("Bounce keys");
759
            else if (disabled & XCB_XKB_BOOL_CTRL_BOUNCE_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
760 761
                disabledFeatures << i18n("Bounce keys");

762 763 764 765 766 767
            if (enabled & XCB_XKB_BOOL_CTRL_STICKY_KEYS)
                enabledFeatures << i18n("Sticky keys");
            else if (disabled & XCB_XKB_BOOL_CTRL_STICKY_KEYS)
                disabledFeatures << i18n("Sticky keys");

            if (enabled & XCB_XKB_BOOL_CTRL_MOUSE_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
768
                enabledFeatures << i18n("Mouse keys");
769
            else if (disabled & XCB_XKB_BOOL_CTRL_MOUSE_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
                disabledFeatures << i18n("Mouse keys");

            QString question;
            switch (enabledFeatures.count()) {
            case 0: switch (disabledFeatures.count()) {
                case 1: question = i18n("Do you really want to deactivate \"%1\"?",
                                            disabledFeatures[0]);
                    break;
                case 2: question = i18n("Do you really want to deactivate \"%1\" and \"%2\"?",
                                            disabledFeatures[0], disabledFeatures[1]);
                    break;
                case  3: question = i18n("Do you really want to deactivate \"%1\", \"%2\" and \"%3\"?",
                                             disabledFeatures[0], disabledFeatures[1],
                                             disabledFeatures[2]);
                    break;
                case 4: question = i18n("Do you really want to deactivate \"%1\", \"%2\", \"%3\" and \"%4\"?",
                                            disabledFeatures[0], disabledFeatures[1],
                                            disabledFeatures[2], disabledFeatures[3]);
                    break;
                }
                break;
            case 1: switch (disabledFeatures.count()) {
                case 0: question = i18n("Do you really want to activate \"%1\"?",
                                            enabledFeatures[0]);
                    break;
                case 1: question = i18n("Do you really want to activate \"%1\" and to deactivate \"%2\"?",
                                            enabledFeatures[0], disabledFeatures[0]);
                    break;
                case 2: question = i18n("Do you really want to activate \"%1\" and to deactivate \"%2\" and \"%3\"?",
                                            enabledFeatures[0], disabledFeatures[0],
                                            disabledFeatures[1]);
                    break;
                case 3: question = i18n("Do you really want to activate \"%1\" and to deactivate \"%2\", \"%3\" and \"%4\"?",
                                            enabledFeatures[0], disabledFeatures[0],
                                            disabledFeatures[1], disabledFeatures[2]);
                    break;
                }
                break;
            case 2: switch (disabledFeatures.count()) {
                case 0: question = i18n("Do you really want to activate \"%1\" and \"%2\"?",
                                            enabledFeatures[0], enabledFeatures[1]);
                    break;
                case 1: question = i18n("Do you really want to activate \"%1\" and \"%2\" and to deactivate \"%3\"?",
                                            enabledFeatures[0], enabledFeatures[1],
                                            disabledFeatures[0]);
                    break;
                case 2: question = i18n("Do you really want to activate \"%1\", and \"%2\" and to deactivate \"%3\" and \"%4\"?",
                                            enabledFeatures[0], enabledFeatures[1],
                                            enabledFeatures[0], disabledFeatures[1]);
                    break;
                }
                break;
            case 3: switch (disabledFeatures.count()) {
                case 0: question = i18n("Do you really want to activate \"%1\", \"%2\" and \"%3\"?",
                                            enabledFeatures[0], enabledFeatures[1],
                                            enabledFeatures[2]);
                    break;
                case 1: question = i18n("Do you really want to activate \"%1\", \"%2\" and \"%3\" and to deactivate \"%4\"?",
                                            enabledFeatures[0], enabledFeatures[1],
                                            enabledFeatures[2], disabledFeatures[0]);
                    break;
                }
                break;
            case 4: question = i18n("Do you really want to activate \"%1\", \"%2\", \"%3\" and \"%4\"?",
                                        enabledFeatures[0], enabledFeatures[1],
                                        enabledFeatures[2], enabledFeatures[3]);
                break;
            }
            QString explanation;
            if (enabledFeatures.count() + disabledFeatures.count() == 1) {
                explanation = i18n("An application has requested to change this setting.");

                if (_gestures) {
843
                    if ((enabled | disabled) == XCB_XKB_BOOL_CTRL_SLOW_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
844
                        explanation = i18n("You held down the Shift key for 8 seconds or an application has requested to change this setting.");
845
                    else if ((enabled | disabled) == XCB_XKB_BOOL_CTRL_STICKY_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
846
                        explanation = i18n("You pressed the Shift key 5 consecutive times or an application has requested to change this setting.");
847
                    else if ((enabled | disabled) == XCB_XKB_BOOL_CTRL_MOUSE_KEYS) {
Frederik Gladhorn's avatar
Frederik Gladhorn committed
848 849 850 851 852 853 854 855 856 857 858
                        QString shortcut = mouseKeysShortcut(QX11Info::display());
                        if (!shortcut.isEmpty() && !shortcut.isNull())
                            explanation = i18n("You pressed %1 or an application has requested to change this setting.", shortcut);
                    }
                }
            } else {
                if (_gestures)
                    explanation = i18n("An application has requested to change these settings, or you used a combination of several keyboard gestures.");
                else
                    explanation = i18n("An application has requested to change these settings.");
            }
859

Frederik Gladhorn's avatar
Frederik Gladhorn committed
860 861 862
            createDialogContents();
            featuresLabel->setText(question + "\n\n" + explanation
                                   + " " + i18n("These AccessX settings are needed for some users with motion impairments and can be configured in the KDE System Settings. You can also turn them on and off with standardized keyboard gestures.\n\nIf you do not need them, you can select \"Deactivate all AccessX features and gestures\"."));
863

Frederik Gladhorn's avatar
Frederik Gladhorn committed
864
            KWindowSystem::setState(dialog->winId(), NET::KeepAbove);
865
            KUserTimestamp::updateUserTimestamp(0);
Frederik Gladhorn's avatar
Frederik Gladhorn committed
866 867 868 869
            dialog->show();
        }
    }
}
870

Frederik Gladhorn's avatar
Frederik Gladhorn committed
871 872 873 874 875 876 877 878
void KAccessApp::notifyChanges()
{
    if (!_kNotifyAccessX)
        return;

    unsigned int enabled  = requestedFeatures & ~features;
    unsigned int disabled = features & ~requestedFeatures;

879
    if (enabled & XCB_XKB_BOOL_CTRL_SLOW_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
880
        KNotification::event("slowkeys", i18n("Slow keys has been enabled. From now on, you need to press each key for a certain length of time before it gets accepted."));
881
    else if (disabled & XCB_XKB_BOOL_CTRL_SLOW_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
882 883
        KNotification::event("slowkeys", i18n("Slow keys has been disabled."));

884
    if (enabled & XCB_XKB_BOOL_CTRL_BOUNCE_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
885
        KNotification::event("bouncekeys", i18n("Bounce keys has been enabled. From now on, each key will be blocked for a certain length of time after it was used."));
886
    else if (disabled & XCB_XKB_BOOL_CTRL_BOUNCE_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
887 888
        KNotification::event("bouncekeys", i18n("Bounce keys has been disabled."));

889
    if (enabled & XCB_XKB_BOOL_CTRL_STICKY_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
890
        KNotification::event("stickykeys", i18n("Sticky keys has been enabled. From now on, modifier keys will stay latched after you have released them."));
891
    else if (disabled & XCB_XKB_BOOL_CTRL_STICKY_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
892 893
        KNotification::event("stickykeys", i18n("Sticky keys has been disabled."));

894
    if (enabled & XCB_XKB_BOOL_CTRL_MOUSE_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
895
        KNotification::event("mousekeys", i18n("Mouse keys has been enabled. From now on, you can use the number pad of your keyboard in order to control the mouse."));
896
    else if (disabled & XCB_XKB_BOOL_CTRL_MOUSE_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
897 898
        KNotification::event("mousekeys", i18n("Mouse keys has been disabled."));
}
899

Frederik Gladhorn's avatar
Frederik Gladhorn committed
900 901 902 903 904 905 906 907
void KAccessApp::applyChanges()
{
    notifyChanges();
    unsigned int enabled  = requestedFeatures & ~features;
    unsigned int disabled = features & ~requestedFeatures;

    KConfigGroup config(KSharedConfig::openConfig(), "Keyboard");

908
    if (enabled & XCB_XKB_BOOL_CTRL_SLOW_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
909
        config.writeEntry("SlowKeys", true);
910
    else if (disabled & XCB_XKB_BOOL_CTRL_SLOW_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
911 912
        config.writeEntry("SlowKeys", false);

913
    if (enabled & XCB_XKB_BOOL_CTRL_BOUNCE_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
914
        config.writeEntry("BounceKeys", true);
915
    else if (disabled & XCB_XKB_BOOL_CTRL_BOUNCE_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
916 917
        config.writeEntry("BounceKeys", false);

918
    if (enabled & XCB_XKB_BOOL_CTRL_STICKY_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
919
        config.writeEntry("StickyKeys", true);
920
    else if (disabled & XCB_XKB_BOOL_CTRL_STICKY_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
921 922 923 924
        config.writeEntry("StickyKeys", false);

    KConfigGroup mousegrp(KSharedConfig::openConfig(), "Mouse");

925
    if (enabled & XCB_XKB_BOOL_CTRL_MOUSE_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
926
        mousegrp.writeEntry("MouseKeys", true);
927
    else if (disabled & XCB_XKB_BOOL_CTRL_MOUSE_KEYS)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
928 929 930 931
        mousegrp.writeEntry("MouseKeys", false);
    mousegrp.sync();
    config.sync();
}
932

Frederik Gladhorn's avatar
Frederik Gladhorn committed
933 934
void KAccessApp::yesClicked()
{
935
    if (dialog)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
936
        dialog->deleteLater();
937
    dialog = nullptr;
Frederik Gladhorn's avatar
Frederik Gladhorn committed
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954

    KConfigGroup config(KSharedConfig::openConfig(), "Keyboard");
    switch (showModeCombobox->currentIndex()) {
    case 0:
        config.writeEntry("Gestures", true);
        config.writeEntry("GestureConfirmation", false);
        break;
    default:
        config.writeEntry("Gestures", true);
        config.writeEntry("GestureConfirmation", true);
        break;
    case 2:
        requestedFeatures = 0;
        config.writeEntry("Gestures", false);
        config.writeEntry("GestureConfirmation", true);
    }
    config.sync();
955

Frederik Gladhorn's avatar
Frederik Gladhorn committed
956 957 958 959 960
    if (features != requestedFeatures) {
        notifyChanges();
        applyChanges();
    }
    readSettings();
961 962
}

Frederik Gladhorn's avatar
Frederik Gladhorn committed
963 964
void KAccessApp::noClicked()
{
965
    if (dialog)
Frederik Gladhorn's avatar
Frederik Gladhorn committed
966
        dialog->deleteLater();
967
    dialog = nullptr;
Frederik Gladhorn's avatar
Frederik Gladhorn committed
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
    requestedFeatures = features;

    KConfigGroup config(KSharedConfig::openConfig(), "Keyboard");
    switch (showModeCombobox->currentIndex()) {
    case 0:
        config.writeEntry("Gestures", true);
        config.writeEntry("GestureConfirmation", false);
        break;
    default:
        config.writeEntry("Gestures", true);
        config.writeEntry("GestureConfirmation", true);
        break;
    case 2:
        requestedFeatures = 0;
        config.writeEntry("Gestures", false);
        config.writeEntry("GestureConfirmation", true);
    }
    config.sync();
986

Frederik Gladhorn's avatar
Frederik Gladhorn committed
987 988 989
    if (features != requestedFeatures)
        applyChanges();
    readSettings();
990 991
}

Frederik Gladhorn's avatar
Frederik Gladhorn committed
992 993 994 995 996
void KAccessApp::dialogClosed()
{
    if (dialog != 0)
        dialog->deleteLater();
    dialog = 0;
997

Frederik Gladhorn's avatar
Frederik Gladhorn committed
998
    requestedFeatures = features;
999 1000
}

Frederik Gladhorn's avatar
Frederik Gladhorn committed
1001 1002 1003
void KAccessApp::setXkbOpcode(int opcode)
{
    xkb_opcode = opcode;
1004 1005
}