summaryrefslogtreecommitdiff
path: root/noncore/applets/keyhelper/keyhelperapplet/misc/KeyMappings.cpp
blob: 0151d390fac1473455e3aace77424ce96f17a9a4 (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
#include "KeyMappings.h"

static QIntDict<QWSServer::KeyMap> g_mapCache(127);

MapInfo::MapInfo(int code, int mod, int uni,
	int shift_uni, int ctrl_uni)
{
	const QWSServer::KeyMap* map;
	if(uni == 0 || shift_uni == 0 || ctrl_uni == 0){
		map = MapInfo::findKeyMap(code);
		if(map != NULL){
			isDefined = true;
			unicode = map->unicode;
			shift_unicode = map->shift_unicode;
			ctrl_unicode = map->ctrl_unicode;
		} else {
			isDefined = false;
			unicode = 0;
			shift_unicode = 0;
			ctrl_unicode = 0;
		}
	} else {
		isDefined = true;
	}
	keycode = code;
	modifiers = mod;
	if(uni != 0){
		unicode = uni;
	}
	if(shift_uni != 0){
		shift_unicode = shift_uni;
	}
	if(ctrl_uni != 0){
		ctrl_unicode = ctrl_uni;
	}
}

const QWSServer::KeyMap* MapInfo::findKeyMap(int keycode)
{
	const QWSServer::KeyMap* m = QWSServer::keyMap();

	while(m->key_code != 0){
		if(m->key_code == keycode){
			return(m);
		}
		m++;
	}
	return(NULL);
}

KeyMappings::KeyMappings()
{
	qDebug("KeyMappings::KeyMappings()");
	init();
}

KeyMappings::~KeyMappings()
{
	qDebug("KeyMappings::~KeyMappings()");
	clear();
}

void KeyMappings::init()
{
	m_capslock = false;
}

void KeyMappings::reset()
{
	clear();
}

void KeyMappings::clear()
{
	for(QMap<int, CodeMaps*>::Iterator it = m_keymaps.begin();
			it!=m_keymaps.end(); ++it){
		delete (*it);
	}
	m_keymaps.clear();
	g_mapCache.setAutoDelete(true);
	g_mapCache.clear();
}

void KeyMappings::assign(int keycode, int keymask, int mapcode,
	int mapmodifiers, int unicode, int shift_unicode, int ctrl_unicode)
{
	CodeMaps* map;
	if(m_keymaps.contains(keycode)){
		map = m_keymaps[keycode];
	} else {
		map = new CodeMaps();
		m_keymaps.insert(keycode, map);
	}

	MapInfo info(mapcode, mapmodifiers, unicode, shift_unicode, ctrl_unicode);
	m_it = map->insert(keymask, info);
}

void KeyMappings::assignModifier(const QString& type, const QString& state)
{
	int maskbit = 0;
	QString str;
	str = type.lower();
	if(str == "shift"){
		maskbit = Qt::ShiftButton;
	} else if(str == "control"){
		maskbit = Qt::ControlButton;
	} else if(str == "alt"){
		maskbit = Qt::AltButton;
	}
	str = state.lower();
	if(str == "off"){
		maskbit = (maskbit << 16) & 0xFFFF0000;
	}
	(*m_it).modifiers |= maskbit;
}

void KeyMappings::assignUnicode(const QString& kind, const QString& ch)
{
	QString str;
	int code = ch[0].unicode();
	str = kind.lower();
	if(str == "unicode"){
		(*m_it).unicode = code;
	} else if(str == "shift_unicode"){
		(*m_it).shift_unicode = code;
	} else if(str == "ctrl_unicode"){
		(*m_it).ctrl_unicode = code;
	}
}

void KeyMappings::assignUnicode(int unicode)
{
	(*m_it).unicode = (*m_it).shift_unicode = 
		(*m_it).ctrl_unicode = unicode;
}

void KeyMappings::statistics()
{
	qWarning("KeyMappings::statistics()");
	for(QMap<int, CodeMaps*>::Iterator it=m_keymaps.begin();
			it!=m_keymaps.end(); ++it){
		qWarning(" code = %x", it.key());
		for(QMap<int, MapInfo>::Iterator it2=(*it)->begin();
				it2!=(*it)->end(); ++it2){
			qDebug("  [%x]-[%x][%x][%x][%x][%x]", it2.key(),
				(*it2).keycode,
				(*it2).modifiers,
				(*it2).unicode,
				(*it2).shift_unicode,
				(*it2).ctrl_unicode);
		}
	}
}

bool KeyMappings::apply(int unicode, int keycode, int modifiers,
	int keymask, bool isPress)
{
	CodeMaps* map;
	m_isMapped = false;

	if(m_keymaps.contains(keycode)){
		map = m_keymaps[keycode];
		if(map->contains(keymask)){
			m_isMapped = true;
			m_keyinfo = (*map)[keymask];
		} else {
			int mask = -1;
			for(CodeMaps::Iterator it=map->begin();
					it!=map->end(); ++it){
				if((keymask & it.key()) == it.key()
					&& it.key() > mask){
					mask = it.key();
				}
			}
			if(mask != -1){
				m_isMapped = true;
				m_keyinfo = (*map)[mask];
			}
		}
	}

	if(m_isMapped == false){
		QWSServer::KeyMap* cache = g_mapCache[keycode];
		if(cache == NULL){
			cache = new QWSServer::KeyMap();
			g_mapCache.insert(keycode, cache);
			cache->unicode = cache->shift_unicode = cache->ctrl_unicode = 0;
		}
		if(cache->unicode == 0 || cache->shift_unicode == 0 || cache->ctrl_unicode == 0){
			QChar ch(unicode);
			if(modifiers & Qt::ControlButton){
				cache->ctrl_unicode = unicode;
			} else if(modifiers & Qt::ShiftButton){
				cache->shift_unicode = ch.upper().unicode();
			} else {
				cache->unicode = ch.lower().unicode();
			}
		}
		m_keyinfo = MapInfo(keycode, 0,
			cache->unicode, cache->shift_unicode, cache->ctrl_unicode);
		if(m_keyinfo.isDefined){
			setOriginal(unicode, modifiers);
		} else {
			setUnicode(unicode);
		}
	}

#if 1
	if(isPress){
		if(m_keyinfo.keycode == Qt::Key_CapsLock){
			m_capslock = !m_capslock;
		}
	}
#endif
	return(m_isMapped);
}

bool KeyMappings::apply(int keycode, int keymask, bool isPress)
{
	CodeMaps* map;
	m_isMapped = false;

	if(m_keymaps.contains(keycode)){
		map = m_keymaps[keycode];
		if(map->contains(keymask)){
			m_isMapped = true;
			m_keyinfo = (*map)[keymask];
		} else {
			int mask = -1;
			for(CodeMaps::Iterator it=map->begin();
					it!=map->end(); ++it){
				if((keymask & it.key()) == it.key()
					&& it.key() > mask){
					mask = it.key();
				}
			}
			if(mask != -1){
				m_isMapped = true;
				m_keyinfo = (*map)[mask];
			}
		}
	}
	if(m_isMapped == false){
		m_keyinfo = MapInfo(keycode);
	}
#if 1
	if(isPress){
		if(m_keyinfo.keycode == Qt::Key_CapsLock){
			m_capslock = !m_capslock;
		}
	}
#endif
	return(m_isMapped);
}

/**
 * set original unicode
 */
void KeyMappings::setOriginal(int unicode, int modifiers)
{
	if(modifiers & Qt::ControlButton){
		m_keyinfo.ctrl_unicode = unicode;
	} else if(modifiers & Qt::ShiftButton){
		m_keyinfo.shift_unicode = unicode;
	} else {
		m_keyinfo.unicode = unicode;
	}
}

void KeyMappings::setModifiers(int modifiers)
{
	m_modifiers = modifiers;
	m_modifiers |= (m_keyinfo.modifiers & 0xFFFF);
	m_modifiers &= ~((m_keyinfo.modifiers >> 16) & 0xFFFF);
}

void KeyMappings::setUnicode(int unicode)
{
	m_keyinfo.unicode = unicode;
	m_keyinfo.shift_unicode = unicode;
	m_keyinfo.ctrl_unicode = unicode;
}

void KeyMappings::setKeycode(int keycode)
{
	m_keyinfo.keycode = keycode;
}

int KeyMappings::getUnicode()
{
	int unicode;
	if(m_modifiers & Qt::ControlButton){
		unicode = m_keyinfo.ctrl_unicode;
	} else if(m_modifiers & Qt::ShiftButton){
		unicode = m_keyinfo.shift_unicode;
		QChar ch(unicode);
		if(m_capslock){
			unicode = ch.lower().unicode();
		} else {
			unicode = ch.upper().unicode();
		}
	} else {
		unicode = m_keyinfo.unicode;
		QChar ch(unicode);
		if(m_capslock){
			unicode = ch.upper().unicode();
		} else {
			unicode = ch.lower().unicode();
		}
	}
	qDebug("UNICODE[%d][%x][%x]", m_capslock, unicode, m_keyinfo.unicode);
#if 0
	if(m_isMapped && m_capslock){
		QChar ch(unicode);
		QChar::Category category = ch.category();
		if(category == QChar::Letter_Uppercase){
			unicode = ch.lower().unicode();
		} else if(category == QChar::Letter_Lowercase){
			unicode = ch.upper().unicode();
		}
	}
#endif
	return(unicode);
}

int KeyMappings::getKeycode()
{
	return(m_keyinfo.keycode);
}

int KeyMappings::getModifiers()
{
	return(m_modifiers);
}

bool KeyMappings::isDefined()
{
	return(m_keyinfo.isDefined);
}