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

« back to all changes in this revision

Viewing changes to kcontrol/keyboard/keyboard_daemon.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 "keyboard_daemon.h"
 
20
 
 
21
#include <QtGui/QX11Info>
 
22
#include <QtDBus/QtDBus>
 
23
#include <QtCore/QProcess>
 
24
 
 
25
#include <kdebug.h>
 
26
#include <kpluginfactory.h>
 
27
#include <kaction.h>
 
28
#include <kactioncollection.h>
 
29
#include <kglobalsettings.h>
 
30
#include <klocale.h>
 
31
#include <klocalizedstring.h>
 
32
 
 
33
#include "x11_helper.h"
 
34
#include "xinput_helper.h"
 
35
#include "xkb_helper.h"
 
36
#include "keyboard_dbus.h"
 
37
#include "xkb_rules.h"
 
38
#include "bindings.h"
 
39
#include "keyboard_hardware.h"
 
40
#include "layout_tray_icon.h"
 
41
#include "layout_memory_persister.h"
 
42
 
 
43
 
 
44
K_PLUGIN_FACTORY(KeyboardFactory, registerPlugin<KeyboardDaemon>();)
 
45
K_EXPORT_PLUGIN(KeyboardFactory("keyboard", "kxkb"))
 
46
 
 
47
KeyboardDaemon::KeyboardDaemon(QObject *parent, const QList<QVariant>&)
 
48
        : KDEDModule(parent),
 
49
          actionCollection(NULL),
 
50
          xEventNotifier(NULL),
 
51
          layoutTrayIcon(NULL),
 
52
          layoutMemory(keyboardConfig),
 
53
          rules(Rules::readRules(Rules::READ_EXTRAS))
 
54
{
 
55
        if( ! X11Helper::xkbSupported(NULL) )
 
56
                return;         //TODO: shut down the daemon?
 
57
 
 
58
    QDBusConnection dbus = QDBusConnection::sessionBus();
 
59
        dbus.registerService(KEYBOARD_DBUS_SERVICE_NAME);
 
60
        dbus.registerObject(KEYBOARD_DBUS_OBJECT_PATH, this, QDBusConnection::ExportScriptableSlots | QDBusConnection::ExportScriptableSignals);
 
61
    dbus.connect(QString(), KEYBOARD_DBUS_OBJECT_PATH, KEYBOARD_DBUS_SERVICE_NAME, KEYBOARD_DBUS_CONFIG_RELOAD_MESSAGE, this, SLOT( configureKeyboard() ));
 
62
 
 
63
        configureKeyboard();
 
64
        registerListeners();
 
65
 
 
66
        LayoutMemoryPersister layoutMemoryPersister(layoutMemory);
 
67
        if( layoutMemoryPersister.restore(KGlobal::mainComponent().componentName()) ) {
 
68
                if( layoutMemoryPersister.getGlobalLayout().isValid() ) {
 
69
                        X11Helper::setLayout(layoutMemoryPersister.getGlobalLayout());
 
70
                }
 
71
        }
 
72
}
 
73
 
 
74
KeyboardDaemon::~KeyboardDaemon()
 
75
{
 
76
        LayoutMemoryPersister layoutMemoryPersister(layoutMemory);
 
77
        layoutMemoryPersister.setGlobalLayout(X11Helper::getCurrentLayout());
 
78
        layoutMemoryPersister.save(KGlobal::mainComponent().componentName());
 
79
 
 
80
    QDBusConnection dbus = QDBusConnection::sessionBus();
 
81
    dbus.disconnect(QString(), KEYBOARD_DBUS_OBJECT_PATH, KEYBOARD_DBUS_SERVICE_NAME, KEYBOARD_DBUS_CONFIG_RELOAD_MESSAGE, this, SLOT( configureKeyboard() ));
 
82
        dbus.unregisterObject(KEYBOARD_DBUS_OBJECT_PATH);
 
83
        dbus.unregisterService(KEYBOARD_DBUS_SERVICE_NAME);
 
84
 
 
85
        unregisterListeners();
 
86
        unregisterShortcut();
 
87
 
 
88
        delete xEventNotifier;
 
89
        delete layoutTrayIcon;
 
90
        delete rules;
 
91
}
 
92
 
 
93
void KeyboardDaemon::configureKeyboard()
 
94
{
 
95
        kDebug() << "Configuring keyboard";
 
96
        init_keyboard_hardware();
 
97
 
 
98
        keyboardConfig.load();
 
99
        XkbHelper::initializeKeyboardLayouts(keyboardConfig);
 
100
        layoutMemory.configChanged();
 
101
 
 
102
        setupTrayIcon();
 
103
 
 
104
        unregisterShortcut();
 
105
        registerShortcut();
 
106
}
 
107
 
 
108
void KeyboardDaemon::configureMouse()
 
109
{
 
110
    QStringList modules;
 
111
    modules << "mouse";
 
112
    QProcess::startDetached("kcminit", modules);
 
113
}
 
114
 
 
115
void KeyboardDaemon::setupTrayIcon()
 
116
{
 
117
        bool show = keyboardConfig.showIndicator
 
118
                        && ( keyboardConfig.showSingle || X11Helper::getLayoutsList().size() > 1 );
 
119
 
 
120
        if( show && ! layoutTrayIcon ) {
 
121
                layoutTrayIcon = new LayoutTrayIcon(rules, keyboardConfig);
 
122
        }
 
123
        else if( ! show && layoutTrayIcon ) {
 
124
                delete layoutTrayIcon;
 
125
                layoutTrayIcon = NULL;
 
126
        }
 
127
}
 
128
 
 
129
void KeyboardDaemon::registerShortcut()
 
130
{
 
131
        if( actionCollection == NULL ) {
 
132
                actionCollection = new KeyboardLayoutActionCollection(this, false);
 
133
 
 
134
                KAction* toggleLayoutAction = actionCollection->getToggeAction();
 
135
                connect(toggleLayoutAction, SIGNAL(triggered()), this, SLOT(switchToNextLayout()));
 
136
                actionCollection->loadLayoutShortcuts(keyboardConfig.layouts, rules);
 
137
                connect(actionCollection, SIGNAL(actionTriggered(QAction*)), this, SLOT(setLayout(QAction*)));
 
138
 
 
139
                connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)), this, SLOT(globalSettingsChanged(int)));
 
140
    }
 
141
}
 
142
 
 
143
void KeyboardDaemon::unregisterShortcut()
 
144
{
 
145
        // register KDE keyboard shortcut for switching layouts
 
146
    if( actionCollection != NULL ) {
 
147
        disconnect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)), this, SLOT(globalSettingsChanged(int)));
 
148
 
 
149
                disconnect(actionCollection, SIGNAL(actionTriggered(QAction*)), this, SLOT(setLayout(QAction*)));
 
150
        disconnect(actionCollection->getToggeAction(), SIGNAL(triggered()), this, SLOT(switchToNextLayout()));
 
151
 
 
152
        delete actionCollection;
 
153
        actionCollection = NULL;
 
154
    }
 
155
}
 
156
 
 
157
void KeyboardDaemon::registerListeners()
 
158
{
 
159
        if( xEventNotifier == NULL ) {
 
160
                xEventNotifier = new XInputEventNotifier();
 
161
        }
 
162
        connect(xEventNotifier, SIGNAL(newPointerDevice()), this, SLOT(configureMouse()));
 
163
        connect(xEventNotifier, SIGNAL(newKeyboardDevice()), this, SLOT(configureKeyboard()));
 
164
        connect(xEventNotifier, SIGNAL(layoutMapChanged()), this, SLOT(layoutMapChanged()));
 
165
        connect(xEventNotifier, SIGNAL(layoutChanged()), this, SLOT(layoutChanged()));
 
166
        xEventNotifier->start();
 
167
}
 
168
 
 
169
void KeyboardDaemon::unregisterListeners()
 
170
{
 
171
        if( xEventNotifier != NULL ) {
 
172
                xEventNotifier->stop();
 
173
                disconnect(xEventNotifier, SIGNAL(newPointerDevice()), this, SLOT(configureMouse()));
 
174
                disconnect(xEventNotifier, SIGNAL(newKeyboardDevice()), this, SLOT(configureKeyboard()));
 
175
                disconnect(xEventNotifier, SIGNAL(layoutChanged()), this, SLOT(layoutChanged()));
 
176
                disconnect(xEventNotifier, SIGNAL(layoutMapChanged()), this, SLOT(layoutMapChanged()));
 
177
        }
 
178
}
 
179
 
 
180
void KeyboardDaemon::globalSettingsChanged(int category)
 
181
{
 
182
        if ( category == KGlobalSettings::SETTINGS_SHORTCUTS) {
 
183
//TODO: optimize this? seems like we'll get configReload and globalShortcuts from kcm so we'll reconfigure twice
 
184
                unregisterShortcut();
 
185
                registerShortcut();
 
186
        }
 
187
}
 
188
 
 
189
void KeyboardDaemon::layoutChanged()
 
190
{
 
191
        //TODO: pass newLayout into layoutTrayIcon?
 
192
        LayoutUnit newLayout = X11Helper::getCurrentLayout();
 
193
 
 
194
        layoutMemory.layoutChanged();
 
195
        if( layoutTrayIcon != NULL ) {
 
196
                layoutTrayIcon->layoutChanged();
 
197
        }
 
198
 
 
199
        if( newLayout != currentLayout ) {
 
200
                currentLayout = newLayout;
 
201
                emit currentLayoutChanged(newLayout.toString());
 
202
        }
 
203
}
 
204
 
 
205
void KeyboardDaemon::layoutMapChanged()
 
206
{
 
207
        keyboardConfig.load();
 
208
        layoutMemory.layoutMapChanged();
 
209
        if( layoutTrayIcon != NULL ) {
 
210
                layoutTrayIcon->layoutMapChanged();
 
211
        }
 
212
}
 
213
 
 
214
void KeyboardDaemon::switchToNextLayout()
 
215
{
 
216
        X11Helper::switchToNextLayout();
 
217
}
 
218
 
 
219
bool KeyboardDaemon::setLayout(QAction* action)
 
220
{
 
221
        return X11Helper::setLayout(LayoutUnit(action->data().toString()));
 
222
}
 
223
 
 
224
bool KeyboardDaemon::setLayout(const QString& layout)
 
225
{
 
226
        return X11Helper::setLayout(LayoutUnit(layout));
 
227
}
 
228
 
 
229
QString KeyboardDaemon::getCurrentLayout()
 
230
{
 
231
        return X11Helper::getCurrentLayout().toString();
 
232
}
 
233
 
 
234
QStringList KeyboardDaemon::getLayoutsList()
 
235
{
 
236
        return X11Helper::getLayoutsListAsString( X11Helper::getLayoutsList() );
 
237
}