summaryrefslogtreecommitdiff
path: root/indra/llwindow/llkeyboardsdl.cpp
blob: 8a0b1de98c517df265b01247012dce6a0633b779 (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
/** 
 * @file llkeyboardsdl.cpp
 * @brief Handler for assignable key bindings
 *
 * $LicenseInfo:firstyear=2001&license=viewergpl$
 * 
 * Copyright (c) 2001-2009, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at
 * http://secondlifegrid.net/programs/open_source/licensing/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#if LL_SDL

#include "linden_common.h"
#include "llkeyboardsdl.h"
#include "llwindowcallbacks.h"
#include "SDL/SDL.h"

LLKeyboardSDL::LLKeyboardSDL()
{
	// Set up key mapping for SDL - eventually can read this from a file?
	// Anything not in the key map gets dropped
	// Add default A-Z

	// Virtual key mappings from SDL_keysym.h ...

	// SDL maps the letter keys to the ASCII you'd expect, but it's lowercase...
	U16 cur_char;
	for (cur_char = 'A'; cur_char <= 'Z'; cur_char++)
	{
		mTranslateKeyMap[cur_char] = cur_char;
	}
	for (cur_char = 'a'; cur_char <= 'z'; cur_char++)
	{
		mTranslateKeyMap[cur_char] = (cur_char - 'a') + 'A';
	}

	for (cur_char = '0'; cur_char <= '9'; cur_char++)
	{
		mTranslateKeyMap[cur_char] = cur_char;
	}

	// These ones are translated manually upon keydown/keyup because
	// SDL doesn't handle their numlock transition.
	//mTranslateKeyMap[SDLK_KP4] = KEY_PAD_LEFT;
	//mTranslateKeyMap[SDLK_KP6] = KEY_PAD_RIGHT;
	//mTranslateKeyMap[SDLK_KP8] = KEY_PAD_UP;
	//mTranslateKeyMap[SDLK_KP2] = KEY_PAD_DOWN;
	//mTranslateKeyMap[SDLK_KP_PERIOD] = KEY_DELETE;
	//mTranslateKeyMap[SDLK_KP7] = KEY_HOME;
	//mTranslateKeyMap[SDLK_KP1] = KEY_END;
	//mTranslateKeyMap[SDLK_KP9] = KEY_PAGE_UP;
	//mTranslateKeyMap[SDLK_KP3] = KEY_PAGE_DOWN;
	//mTranslateKeyMap[SDLK_KP0] = KEY_INSERT;

	mTranslateKeyMap[SDLK_SPACE] = ' ';
	mTranslateKeyMap[SDLK_RETURN] = KEY_RETURN;
	mTranslateKeyMap[SDLK_LEFT] = KEY_LEFT;
	mTranslateKeyMap[SDLK_RIGHT] = KEY_RIGHT;
	mTranslateKeyMap[SDLK_UP] = KEY_UP;
	mTranslateKeyMap[SDLK_DOWN] = KEY_DOWN;
	mTranslateKeyMap[SDLK_ESCAPE] = KEY_ESCAPE;
	mTranslateKeyMap[SDLK_KP_ENTER] = KEY_RETURN;
	mTranslateKeyMap[SDLK_ESCAPE] = KEY_ESCAPE;
	mTranslateKeyMap[SDLK_BACKSPACE] = KEY_BACKSPACE;
	mTranslateKeyMap[SDLK_DELETE] = KEY_DELETE;
	mTranslateKeyMap[SDLK_LSHIFT] = KEY_SHIFT;
	mTranslateKeyMap[SDLK_RSHIFT] = KEY_SHIFT;
	mTranslateKeyMap[SDLK_LCTRL] = KEY_CONTROL;
	mTranslateKeyMap[SDLK_RCTRL] = KEY_CONTROL;
	mTranslateKeyMap[SDLK_LALT] = KEY_ALT;
	mTranslateKeyMap[SDLK_RALT] = KEY_ALT;
	mTranslateKeyMap[SDLK_HOME] = KEY_HOME;
	mTranslateKeyMap[SDLK_END] = KEY_END;
	mTranslateKeyMap[SDLK_PAGEUP] = KEY_PAGE_UP;
	mTranslateKeyMap[SDLK_PAGEDOWN] = KEY_PAGE_DOWN;
	mTranslateKeyMap[SDLK_MINUS] = KEY_HYPHEN;
	mTranslateKeyMap[SDLK_EQUALS] = KEY_EQUALS;
	mTranslateKeyMap[SDLK_KP_EQUALS] = KEY_EQUALS;
	mTranslateKeyMap[SDLK_INSERT] = KEY_INSERT;
	mTranslateKeyMap[SDLK_CAPSLOCK] = KEY_CAPSLOCK;
	mTranslateKeyMap[SDLK_TAB] = KEY_TAB;
	mTranslateKeyMap[SDLK_KP_PLUS] = KEY_ADD;
	mTranslateKeyMap[SDLK_KP_MINUS] = KEY_SUBTRACT;
	mTranslateKeyMap[SDLK_KP_MULTIPLY] = KEY_MULTIPLY;
	mTranslateKeyMap[SDLK_KP_DIVIDE] = KEY_PAD_DIVIDE;
	mTranslateKeyMap[SDLK_F1] = KEY_F1;
	mTranslateKeyMap[SDLK_F2] = KEY_F2;
	mTranslateKeyMap[SDLK_F3] = KEY_F3;
	mTranslateKeyMap[SDLK_F4] = KEY_F4;
	mTranslateKeyMap[SDLK_F5] = KEY_F5;
	mTranslateKeyMap[SDLK_F6] = KEY_F6;
	mTranslateKeyMap[SDLK_F7] = KEY_F7;
	mTranslateKeyMap[SDLK_F8] = KEY_F8;
	mTranslateKeyMap[SDLK_F9] = KEY_F9;
	mTranslateKeyMap[SDLK_F10] = KEY_F10;
	mTranslateKeyMap[SDLK_F11] = KEY_F11;
	mTranslateKeyMap[SDLK_F12] = KEY_F12;
	mTranslateKeyMap[SDLK_PLUS]   = '=';
	mTranslateKeyMap[SDLK_COMMA]  = ',';
	mTranslateKeyMap[SDLK_MINUS]  = '-';
	mTranslateKeyMap[SDLK_PERIOD] = '.';
	mTranslateKeyMap[SDLK_BACKQUOTE] = '`';
	mTranslateKeyMap[SDLK_SLASH] = KEY_DIVIDE;
	mTranslateKeyMap[SDLK_SEMICOLON] = ';';
	mTranslateKeyMap[SDLK_LEFTBRACKET] = '[';
	mTranslateKeyMap[SDLK_BACKSLASH] = '\\';
	mTranslateKeyMap[SDLK_RIGHTBRACKET] = ']';
	mTranslateKeyMap[SDLK_QUOTE] = '\'';

	// Build inverse map
	std::map<U16, KEY>::iterator iter;
	for (iter = mTranslateKeyMap.begin(); iter != mTranslateKeyMap.end(); iter++)
	{
		mInvTranslateKeyMap[iter->second] = iter->first;
	}

	// numpad map
	mTranslateNumpadMap[SDLK_KP0] = KEY_PAD_INS;
	mTranslateNumpadMap[SDLK_KP1] = KEY_PAD_END;
	mTranslateNumpadMap[SDLK_KP2] = KEY_PAD_DOWN;
	mTranslateNumpadMap[SDLK_KP3] = KEY_PAD_PGDN;
	mTranslateNumpadMap[SDLK_KP4] = KEY_PAD_LEFT;
	mTranslateNumpadMap[SDLK_KP5] = KEY_PAD_CENTER;
	mTranslateNumpadMap[SDLK_KP6] = KEY_PAD_RIGHT;
	mTranslateNumpadMap[SDLK_KP7] = KEY_PAD_HOME;
	mTranslateNumpadMap[SDLK_KP8] = KEY_PAD_UP;
	mTranslateNumpadMap[SDLK_KP9] = KEY_PAD_PGUP;
	mTranslateNumpadMap[SDLK_KP_PERIOD] = KEY_PAD_DEL;

	// build inverse numpad map
	for (iter = mTranslateNumpadMap.begin();
	     iter != mTranslateNumpadMap.end();
	     iter++)
	{
		mInvTranslateNumpadMap[iter->second] = iter->first;
	}
}

void LLKeyboardSDL::resetMaskKeys()
{
	SDLMod mask = SDL_GetModState();

	// MBW -- XXX -- This mirrors the operation of the Windows version of resetMaskKeys().
	//    It looks a bit suspicious, as it won't correct for keys that have been released.
	//    Is this the way it's supposed to work?

	if(mask & KMOD_SHIFT)
	{
		mKeyLevel[KEY_SHIFT] = TRUE;
	}

	if(mask & KMOD_CTRL)
	{
		mKeyLevel[KEY_CONTROL] = TRUE;
	}

	if(mask & KMOD_ALT)
	{
		mKeyLevel[KEY_ALT] = TRUE;
	}
}


MASK LLKeyboardSDL::updateModifiers(const U32 mask)
{
	// translate the mask
	MASK out_mask = MASK_NONE;

	if(mask & KMOD_SHIFT)
	{
		out_mask |= MASK_SHIFT;
	}

	if(mask & KMOD_CTRL)
	{
		out_mask |= MASK_CONTROL;
	}

	if(mask & KMOD_ALT)
	{
		out_mask |= MASK_ALT;
	}

	return out_mask;
}


static U16 adjustNativekeyFromUnhandledMask(const U16 key, const U32 mask)
{
	// SDL doesn't automatically adjust the keysym according to
	// whether NUMLOCK is engaged, so we massage the keysym manually.
	U16 rtn = key;
	if (!(mask & KMOD_NUM))
	{
		switch (key)
		{
		case SDLK_KP_PERIOD: rtn = SDLK_DELETE; break;
		case SDLK_KP0: rtn = SDLK_INSERT; break;
		case SDLK_KP1: rtn = SDLK_END; break;
		case SDLK_KP2: rtn = SDLK_DOWN; break;
		case SDLK_KP3: rtn = SDLK_PAGEDOWN; break;
		case SDLK_KP4: rtn = SDLK_LEFT; break;
		case SDLK_KP6: rtn = SDLK_RIGHT; break;
		case SDLK_KP7: rtn = SDLK_HOME; break;
		case SDLK_KP8: rtn = SDLK_UP; break;
		case SDLK_KP9: rtn = SDLK_PAGEUP; break;
		}
	}
	return rtn;
}


BOOL LLKeyboardSDL::handleKeyDown(const U16 key, const U32 mask)
{
	U16     adjusted_nativekey;
	KEY	translated_key = 0;
	U32	translated_mask = MASK_NONE;
	BOOL	handled = FALSE;

	adjusted_nativekey = adjustNativekeyFromUnhandledMask(key, mask);

	translated_mask = updateModifiers(mask);
	
	if(translateNumpadKey(adjusted_nativekey, &translated_key))
	{
		handled = handleTranslatedKeyDown(translated_key, translated_mask);
	}

	return handled;
}


BOOL LLKeyboardSDL::handleKeyUp(const U16 key, const U32 mask)
{
	U16     adjusted_nativekey;
	KEY	translated_key = 0;
	U32	translated_mask = MASK_NONE;
	BOOL	handled = FALSE;

	adjusted_nativekey = adjustNativekeyFromUnhandledMask(key, mask);

	translated_mask = updateModifiers(mask);

	if(translateNumpadKey(adjusted_nativekey, &translated_key))
	{
		handled = handleTranslatedKeyUp(translated_key, translated_mask);
	}

	return handled;
}

MASK LLKeyboardSDL::currentMask(BOOL for_mouse_event)
{
	MASK result = MASK_NONE;
	SDLMod mask = SDL_GetModState();

	if (mask & KMOD_SHIFT)			result |= MASK_SHIFT;
	if (mask & KMOD_CTRL)			result |= MASK_CONTROL;
	if (mask & KMOD_ALT)			result |= MASK_ALT;

	// For keyboard events, consider Meta keys equivalent to Control
	if (!for_mouse_event)
	{
		if (mask & KMOD_META) result |= MASK_CONTROL;
	}

	return result;
}

void LLKeyboardSDL::scanKeyboard()
{
	for (S32 key = 0; key < KEY_COUNT; key++)
	{
		// Generate callback if any event has occurred on this key this frame.
		// Can't just test mKeyLevel, because this could be a slow frame and
		// key might have gone down then up. JC
		if (mKeyLevel[key] || mKeyDown[key] || mKeyUp[key])
		{
			mCurScanKey = key;
			mCallbacks->handleScanKey(key, mKeyDown[key], mKeyUp[key], mKeyLevel[key]);
		}
	}

	// Reset edges for next frame
	for (S32 key = 0; key < KEY_COUNT; key++)
	{
		mKeyUp[key] = FALSE;
		mKeyDown[key] = FALSE;
		if (mKeyLevel[key])
		{
			mKeyLevelFrameCount[key]++;
		}
	}
}

 
BOOL LLKeyboardSDL::translateNumpadKey( const U16 os_key, KEY *translated_key)
{
	if(mNumpadDistinct == ND_NUMLOCK_ON)
	{
		std::map<U16, KEY>::iterator iter= mTranslateNumpadMap.find(os_key);
		if(iter != mTranslateNumpadMap.end())
		{
			*translated_key = iter->second;
			return TRUE;
		}
	}
	BOOL success = translateKey(os_key, translated_key);
	return success;	
}

U16 LLKeyboardSDL::inverseTranslateNumpadKey(const KEY translated_key)
{
	if(mNumpadDistinct == ND_NUMLOCK_ON)
	{
		std::map<KEY, U16>::iterator iter= mInvTranslateNumpadMap.find(translated_key);
		if(iter != mInvTranslateNumpadMap.end())
		{
			return iter->second;
		}
	}
	return inverseTranslateKey(translated_key);
}

#endif