2
* Copyright (C) 2010 Andriy Rysin (rysin@kde.org)
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.
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.
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.
19
#include "keyboard_daemon.h"
21
#include <QtGui/QX11Info>
22
#include <QtDBus/QtDBus>
23
#include <QtCore/QProcess>
26
#include <kpluginfactory.h>
28
#include <kactioncollection.h>
29
#include <kglobalsettings.h>
31
#include <klocalizedstring.h>
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"
39
#include "keyboard_hardware.h"
40
#include "layout_tray_icon.h"
41
#include "layout_memory_persister.h"
44
K_PLUGIN_FACTORY(KeyboardFactory, registerPlugin<KeyboardDaemon>();)
45
K_EXPORT_PLUGIN(KeyboardFactory("keyboard", "kxkb"))
47
KeyboardDaemon::KeyboardDaemon(QObject *parent, const QList<QVariant>&)
49
actionCollection(NULL),
52
layoutMemory(keyboardConfig),
53
rules(Rules::readRules(Rules::READ_EXTRAS))
55
if( ! X11Helper::xkbSupported(NULL) )
56
return; //TODO: shut down the daemon?
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() ));
66
LayoutMemoryPersister layoutMemoryPersister(layoutMemory);
67
if( layoutMemoryPersister.restore(KGlobal::mainComponent().componentName()) ) {
68
if( layoutMemoryPersister.getGlobalLayout().isValid() ) {
69
X11Helper::setLayout(layoutMemoryPersister.getGlobalLayout());
74
KeyboardDaemon::~KeyboardDaemon()
76
LayoutMemoryPersister layoutMemoryPersister(layoutMemory);
77
layoutMemoryPersister.setGlobalLayout(X11Helper::getCurrentLayout());
78
layoutMemoryPersister.save(KGlobal::mainComponent().componentName());
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);
85
unregisterListeners();
88
delete xEventNotifier;
89
delete layoutTrayIcon;
93
void KeyboardDaemon::configureKeyboard()
95
kDebug() << "Configuring keyboard";
96
init_keyboard_hardware();
98
keyboardConfig.load();
99
XkbHelper::initializeKeyboardLayouts(keyboardConfig);
100
layoutMemory.configChanged();
104
unregisterShortcut();
108
void KeyboardDaemon::configureMouse()
112
QProcess::startDetached("kcminit", modules);
115
void KeyboardDaemon::setupTrayIcon()
117
bool show = keyboardConfig.showIndicator
118
&& ( keyboardConfig.showSingle || X11Helper::getLayoutsList().size() > 1 );
120
if( show && ! layoutTrayIcon ) {
121
layoutTrayIcon = new LayoutTrayIcon(rules, keyboardConfig);
123
else if( ! show && layoutTrayIcon ) {
124
delete layoutTrayIcon;
125
layoutTrayIcon = NULL;
129
void KeyboardDaemon::registerShortcut()
131
if( actionCollection == NULL ) {
132
actionCollection = new KeyboardLayoutActionCollection(this, false);
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*)));
139
connect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)), this, SLOT(globalSettingsChanged(int)));
143
void KeyboardDaemon::unregisterShortcut()
145
// register KDE keyboard shortcut for switching layouts
146
if( actionCollection != NULL ) {
147
disconnect(KGlobalSettings::self(), SIGNAL(settingsChanged(int)), this, SLOT(globalSettingsChanged(int)));
149
disconnect(actionCollection, SIGNAL(actionTriggered(QAction*)), this, SLOT(setLayout(QAction*)));
150
disconnect(actionCollection->getToggeAction(), SIGNAL(triggered()), this, SLOT(switchToNextLayout()));
152
delete actionCollection;
153
actionCollection = NULL;
157
void KeyboardDaemon::registerListeners()
159
if( xEventNotifier == NULL ) {
160
xEventNotifier = new XInputEventNotifier();
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();
169
void KeyboardDaemon::unregisterListeners()
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()));
180
void KeyboardDaemon::globalSettingsChanged(int category)
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();
189
void KeyboardDaemon::layoutChanged()
191
//TODO: pass newLayout into layoutTrayIcon?
192
LayoutUnit newLayout = X11Helper::getCurrentLayout();
194
layoutMemory.layoutChanged();
195
if( layoutTrayIcon != NULL ) {
196
layoutTrayIcon->layoutChanged();
199
if( newLayout != currentLayout ) {
200
currentLayout = newLayout;
201
emit currentLayoutChanged(newLayout.toString());
205
void KeyboardDaemon::layoutMapChanged()
207
keyboardConfig.load();
208
layoutMemory.layoutMapChanged();
209
if( layoutTrayIcon != NULL ) {
210
layoutTrayIcon->layoutMapChanged();
214
void KeyboardDaemon::switchToNextLayout()
216
X11Helper::switchToNextLayout();
219
bool KeyboardDaemon::setLayout(QAction* action)
221
return X11Helper::setLayout(LayoutUnit(action->data().toString()));
224
bool KeyboardDaemon::setLayout(const QString& layout)
226
return X11Helper::setLayout(LayoutUnit(layout));
229
QString KeyboardDaemon::getCurrentLayout()
231
return X11Helper::getCurrentLayout().toString();
234
QStringList KeyboardDaemon::getLayoutsList()
236
return X11Helper::getLayoutsListAsString( X11Helper::getLayoutsList() );