summaryrefslogtreecommitdiffstats
path: root/kxkb/kxkb.cpp
blob: fd8db27d59b9c09c50f953533b02a7d52c797882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
    Copyright (C) 2001, S.R.Haque <srhaque@iee.org>. Derived from an
    original by Matthias H�zer-Klpfel released under the QPL.
    This file is part of the KDE project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

DESCRIPTION

    KDE Keyboard Tool. Manages XKB keyboard mappings.
*/

#include <unistd.h>
#include <stdlib.h>
#include <assert.h>

#include <tqregexp.h>
#include <tqfile.h>
#include <tqstringlist.h>
#include <tqimage.h>

#include <tdeaboutdata.h>
#include <tdecmdlineargs.h>
#include <tdeglobal.h>
#include <kglobalaccel.h>
#include <tdelocale.h>
#include <tdeprocess.h>
#include <twinmodule.h>
#include <twin.h>
#include <tdetempfile.h>
#include <kstandarddirs.h>
#include <kipc.h>
#include <tdeaction.h>
#include <tdepopupmenu.h>
#include <kdebug.h>
#include <tdeconfig.h>
#include <knotifyclient.h>
#include <dcopclient.h>
#include <dcopref.h>

#include "x11helper.h"
#include "kxkb.h"
#include "extension.h"
#include "rules.h"
#include "kxkbconfig.h"
#include "layoutmap.h"

#include "kxkb.moc"


KXKBApp::KXKBApp(bool allowStyles, bool GUIenabled)
    : TDEUniqueApplication(allowStyles, GUIenabled),
    m_prevWinId(X11Helper::UNKNOWN_WINDOW_ID),
    m_rules(NULL),
    m_tray(NULL),
    kWinModule(NULL)
{
    X11Helper::initializeTranslations();
	m_extension = new XKBExtension();
    if( !m_extension->init() ) {
		kdDebug() << "xkb initialization failed, exiting..." << endl;
		::exit(1);
    }
    connect(m_extension, TQ_SIGNAL(groupChanged(uint)), this, TQ_SLOT(slotGroupChanged(uint)));

	m_layoutOwnerMap = new LayoutMap(kxkbConfig);

	// keep in sync with kcmlayout.cpp
	keys = new TDEGlobalAccel(this);
#include "kxkbbindings.cpp"

    connect( this, TQ_SIGNAL(settingsChanged(int)), TQ_SLOT(slotSettingsChanged(int)) );
    addKipcEventMask( KIPC::SettingsChanged );
}


KXKBApp::~KXKBApp()
{
	delete m_tray;
	delete m_rules;
	delete m_extension;
	delete m_layoutOwnerMap;
	delete kWinModule;
	delete keys;
}

int KXKBApp::newInstance()
{
    if (settingsRead()) {
        layoutApply();
    }

    return 0;
}

bool KXKBApp::settingsRead()
{
	XkbOptions options = kxkbConfig.getKXkbOptions();
	if( !m_extension->setXkbOptions(options) ) {
		kdDebug() << "Setting XKB options failed!" << endl;
	}

    if ( kxkbConfig.m_useKxkb == false ) {
        kapp->quit();
        return false;
    }
	
	m_prevWinId = X11Helper::UNKNOWN_WINDOW_ID;
	
	if( kxkbConfig.m_switchingPolicy == SWITCH_POLICY_GLOBAL ) {
		delete kWinModule;
		kWinModule = NULL;
	}
	else {
		TQDesktopWidget desktopWidget;
		if( desktopWidget.numScreens() > 1 && desktopWidget.isVirtualDesktop() == false ) {
			kdWarning() << "With non-virtual desktop only global switching policy supported on non-primary screens" << endl;
			//TODO: find out how to handle that
		}
		
		if( kWinModule == NULL ) {
			kWinModule = new KWinModule(0, KWinModule::INFO_DESKTOP);
			connect(kWinModule, TQ_SIGNAL(activeWindowChanged(WId)), TQ_SLOT(windowChanged(WId)));
		}
		m_prevWinId = kWinModule->activeWindow();
		kdDebug() << "Active window " << m_prevWinId << endl;
	}
	
	m_layoutOwnerMap->reset();
	m_layoutOwnerMap->setCurrentWindow( m_prevWinId );

	if( m_rules == NULL )
		m_rules = new XkbRules(false);
	
	for(int ii=0; ii<(int)kxkbConfig.m_layouts.count(); ii++) {
		LayoutUnit& layoutUnit = kxkbConfig.m_layouts[ii];
	}
	
	m_currentLayout = kxkbConfig.m_layouts[0];
	kdDebug() << "default layout is " << m_currentLayout.toPair() << endl;
	
	if( kxkbConfig.m_layouts.count() == 1 && !kxkbConfig.m_showSingle) {
		kapp->quit();
		return false;
 	}

	initTray();
	
	TDEGlobal::config()->reparseConfiguration(); // kcontrol modified kdeglobals
	keys->readSettings();
	keys->updateConnections();

	return true;
}

void KXKBApp::initTray()
{
	if( !m_tray )
	{
		KSystemTray* sysTray = new KxkbSystemTray();
		TDEPopupMenu* popupMenu = sysTray->contextMenu();
	//	popupMenu->insertTitle( kapp->miniIcon(), kapp->caption() );

		m_tray = new KxkbLabelController(sysTray, popupMenu);
 		connect(popupMenu, TQ_SIGNAL(activated(int)), this, TQ_SLOT(menuActivated(int)));
		connect(sysTray, TQ_SIGNAL(toggled()), this, TQ_SLOT(nextLayout()));
	}
	
	m_tray->setShowFlag(kxkbConfig.m_showFlag);
	m_tray->initLayoutList(kxkbConfig.m_layouts, *m_rules);
	m_tray->setCurrentLayout(m_currentLayout);
	m_tray->show();
}

// This function activates the keyboard layout specified by the
// configuration members (m_currentLayout)
void KXKBApp::layoutApply()
{
    setLayout(m_currentLayout);
}

// kdcop
bool KXKBApp::setLayout(const TQString& layoutPair)
{
	const LayoutUnit layoutUnitKey(layoutPair);
	if( kxkbConfig.m_layouts.contains(layoutUnitKey) ) {
		return setLayout( *kxkbConfig.m_layouts.find(layoutUnitKey) );
	}
	return false;
}

// Activates the keyboard layout specified by 'layoutUnit'
bool KXKBApp::setLayout(const LayoutUnit& layoutUnit)
{
	uint group = kxkbConfig.m_layouts.findIndex(layoutUnit);
	bool res = m_extension->setGroup(group);
	if (res) {
		m_currentLayout = layoutUnit;
		maybeShowLayoutNotification();
	}

	if (m_tray) {
		if (res) {
			m_tray->setCurrentLayout(layoutUnit);
		} else {
			m_tray->setError(layoutUnit.toPair());
		}
	}

	return res;
}
// Activates the keyboard layout specified by group number
bool KXKBApp::setLayout(const uint group)
{
	bool res = m_extension->setGroup(group);
	if (res) {
		m_currentLayout = kxkbConfig.m_layouts[group];
	}

	if (m_tray) {
		if (res)
			m_tray->setCurrentLayout(m_currentLayout);
		else
			m_tray->setError(m_currentLayout.toPair());
	}

	return res;
}


void KXKBApp::nextLayout()
{
	const LayoutUnit& layout = m_layoutOwnerMap->getNextLayout().layoutUnit;
	setLayout(layout);
}

void KXKBApp::prevLayout()
{
	const LayoutUnit& layout = m_layoutOwnerMap->getPrevLayout().layoutUnit;
	setLayout(layout);
}

void KXKBApp::menuActivated(int id)
{
	if( KxkbLabelController::START_MENU_ID <= id 
		   && id < KxkbLabelController::START_MENU_ID + (int)kxkbConfig.m_layouts.count() )
	{
		const LayoutUnit& layout = kxkbConfig.m_layouts[id - KxkbLabelController::START_MENU_ID];
		m_layoutOwnerMap->setCurrentLayout( layout );
		setLayout( layout );
	}
	else if (id == KxkbLabelController::CONFIG_MENU_ID)
    {
        TDEProcess p;
        p << "tdecmshell" << "keyboard_layout";
        p.start(TDEProcess::DontCare);
	}
	else if (id == KxkbLabelController::HELP_MENU_ID)
	{
		TDEApplication::kApplication()->invokeHelp(0, "kxkb");
	}
//	else
//	{
//		quit();
//	}
}

void KXKBApp::slotGroupChanged(uint group)
{
	if (group >= kxkbConfig.m_layouts.count())
	{
		group = 0;
	}
	m_currentLayout = kxkbConfig.m_layouts[group];
	m_tray->setCurrentLayout(m_currentLayout);
	maybeShowLayoutNotification();
}

void KXKBApp::maybeShowLayoutNotification() {
	if (!kxkbConfig.m_enableNotify) return;

	TQString layoutName(m_rules->getLayoutName(m_currentLayout));
	bool useKMilo = kxkbConfig.m_notifyUseKMilo;
	bool notificationSent = false;

	// Query KDED whether KMiloD is loaded
	if (useKMilo) {
		QCStringList modules;
		TQCString replyType;
		TQByteArray replyData;
		if (kapp->dcopClient()->call("kded", "kded", "loadedModules()",
			TQByteArray(), replyType, replyData))
		{
			if (replyType == "QCStringList") {
				TQDataStream reply(replyData, IO_ReadOnly);
				reply >> modules;

				if (!modules.contains("kmilod")) {
					useKMilo = false;
				}
			}
		}
	}

	if (useKMilo) {
		DCOPRef kmilo("kded", "kmilod");
		if (kmilo.send("displayText(TQString,TQPixmap)", layoutName, kapp->miniIcon()))
			notificationSent = true;
	}

	if (!notificationSent) {
		KNotifyClient::event(m_tray->winId(), "LayoutChange", layoutName);
	}
}

// TODO: we also have to handle deleted windows
void KXKBApp::windowChanged(WId winId)
{
//	kdDebug() << "window switch" << endl;
	if( kxkbConfig.m_switchingPolicy == SWITCH_POLICY_GLOBAL ) { // should not happen actually
		kdDebug() << "windowChanged() signal in GLOBAL switching policy" << endl;
		return;
	}

	kdDebug() << "old WinId: " << m_prevWinId << ", new WinId: " << winId << endl;
	
	if( m_prevWinId != X11Helper::UNKNOWN_WINDOW_ID ) {	// saving layout from previous window
// 		m_layoutOwnerMap->setCurrentWindow(m_prevWinId);
		m_layoutOwnerMap->setCurrentLayout(m_currentLayout);
	}
 
	m_prevWinId = winId;

	if( winId != X11Helper::UNKNOWN_WINDOW_ID ) {
		m_layoutOwnerMap->setCurrentWindow(winId);
		const LayoutState& layoutState = m_layoutOwnerMap->getCurrentLayout();
		
		if( layoutState.layoutUnit != m_currentLayout ) {
			kdDebug() << "switching to " << layoutState.layoutUnit.toPair() << " for "  << winId << endl;
			setLayout(layoutState.layoutUnit);
		}
	}
}


void KXKBApp::slotSettingsChanged(int category)
{
	if (category == TDEApplication::SETTINGS_SHORTCUTS) {
		TDEGlobal::config()->reparseConfiguration(); // kcontrol modified kdeglobals
		keys->readSettings();
		keys->updateConnections();
    }
}

bool KXKBApp::x11EventFilter(XEvent *e) {
    // let the extension process the event and emit signals if necessary
    m_extension->processXEvent(e);
    return TDEApplication::x11EventFilter(e);
}

const char *DESCRIPTION = I18N_NOOP("A utility to switch keyboard maps");

extern "C" TDE_EXPORT int kdemain(int argc, char *argv[])
{
    TDEAboutData about("kxkb", I18N_NOOP("TDE Keyboard Tool"), "1.0",
                     DESCRIPTION, TDEAboutData::License_LGPL,
                     "Copyright (C) 2001, S.R.Haque\n(C) 2002-2003, 2006 Andriy Rysin");
    TDECmdLineArgs::init(argc, argv, &about);
    KXKBApp::addCmdLineOptions();

    if (!KXKBApp::start())
        return 0;

    KXKBApp app;
    app.disableSessionManagement();
    app.exec();
    return 0;
}