PageRenderTime 45ms CodeModel.GetById 13ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 1ms

/indra/llwindow/llkeyboard.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 466 lines | 350 code | 58 blank | 58 comment | 73 complexity | 6da9103ebe1c894cdfa5b8769b500093 MD5 | raw file
  1/** 
  2 * @file llkeyboard.cpp
  3 * @brief Handler for assignable key bindings
  4 *
  5 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "linden_common.h"
 28#include "indra_constants.h"
 29#include "llkeyboard.h"
 30
 31#include "llwindowcallbacks.h"
 32
 33//
 34// Globals
 35//
 36
 37LLKeyboard *gKeyboard = NULL;
 38
 39//static
 40std::map<KEY,std::string> LLKeyboard::sKeysToNames;
 41std::map<std::string,KEY> LLKeyboard::sNamesToKeys;
 42LLKeyStringTranslatorFunc*	LLKeyboard::mStringTranslator = NULL;	// Used for l10n + PC/Mac/Linux accelerator labeling
 43
 44
 45//
 46// Class Implementation
 47//
 48
 49LLKeyboard::LLKeyboard() : mCallbacks(NULL), mNumpadDistinct(ND_NUMLOCK_OFF)
 50{
 51	S32 i;
 52
 53	// Constructor for LLTimer inits each timer. We want them to
 54	// be constructed without being initialized, so we shut them down here.
 55	for (i = 0; i < KEY_COUNT; i++)
 56	{
 57		mKeyLevelFrameCount[i] = 0;
 58		mKeyLevel[i] = FALSE;
 59		mKeyUp[i]    = FALSE;
 60		mKeyDown[i]  = FALSE;
 61		mKeyRepeated[i] = FALSE;
 62	}
 63
 64	mInsertMode = LL_KIM_INSERT;
 65	mCurTranslatedKey = KEY_NONE;
 66	mCurScanKey = KEY_NONE;
 67
 68	addKeyName(' ', "Space" );
 69	addKeyName(KEY_RETURN, "Enter" );
 70	addKeyName(KEY_LEFT, "Left" );
 71	addKeyName(KEY_RIGHT, "Right" );
 72	addKeyName(KEY_UP, "Up" );
 73	addKeyName(KEY_DOWN, "Down" );
 74	addKeyName(KEY_ESCAPE, "Esc" );
 75	addKeyName(KEY_HOME, "Home" );
 76	addKeyName(KEY_END, "End" );
 77	addKeyName(KEY_PAGE_UP, "PgUp" );
 78	addKeyName(KEY_PAGE_DOWN, "PgDn" );
 79	addKeyName(KEY_F1, "F1" );
 80	addKeyName(KEY_F2, "F2" );
 81	addKeyName(KEY_F3, "F3" );
 82	addKeyName(KEY_F4, "F4" );
 83	addKeyName(KEY_F5, "F5" );
 84	addKeyName(KEY_F6, "F6" );
 85	addKeyName(KEY_F7, "F7" );
 86	addKeyName(KEY_F8, "F8" );
 87	addKeyName(KEY_F9, "F9" );
 88	addKeyName(KEY_F10, "F10" );
 89	addKeyName(KEY_F11, "F11" );
 90	addKeyName(KEY_F12, "F12" );
 91	addKeyName(KEY_TAB, "Tab" );
 92	addKeyName(KEY_ADD, "Add" );
 93	addKeyName(KEY_SUBTRACT, "Subtract" );
 94	addKeyName(KEY_MULTIPLY, "Multiply" );
 95	addKeyName(KEY_DIVIDE, "Divide" );
 96	addKeyName(KEY_PAD_DIVIDE, "PAD_DIVIDE" );
 97	addKeyName(KEY_PAD_LEFT, "PAD_LEFT" );
 98	addKeyName(KEY_PAD_RIGHT, "PAD_RIGHT" );
 99	addKeyName(KEY_PAD_DOWN, "PAD_DOWN" );
100	addKeyName(KEY_PAD_UP, "PAD_UP" );
101	addKeyName(KEY_PAD_HOME, "PAD_HOME" );
102	addKeyName(KEY_PAD_END, "PAD_END" );
103	addKeyName(KEY_PAD_PGUP, "PAD_PGUP" );
104	addKeyName(KEY_PAD_PGDN, "PAD_PGDN" );
105	addKeyName(KEY_PAD_CENTER, "PAD_CENTER" );
106	addKeyName(KEY_PAD_INS, "PAD_INS" );
107	addKeyName(KEY_PAD_DEL, "PAD_DEL" );
108	addKeyName(KEY_PAD_RETURN, "PAD_Enter" );
109	addKeyName(KEY_BUTTON0, "PAD_BUTTON0" );
110	addKeyName(KEY_BUTTON1, "PAD_BUTTON1" );
111	addKeyName(KEY_BUTTON2, "PAD_BUTTON2" );
112	addKeyName(KEY_BUTTON3, "PAD_BUTTON3" );
113	addKeyName(KEY_BUTTON4, "PAD_BUTTON4" );
114	addKeyName(KEY_BUTTON5, "PAD_BUTTON5" );
115	addKeyName(KEY_BUTTON6, "PAD_BUTTON6" );
116	addKeyName(KEY_BUTTON7, "PAD_BUTTON7" );
117	addKeyName(KEY_BUTTON8, "PAD_BUTTON8" );
118	addKeyName(KEY_BUTTON9, "PAD_BUTTON9" );
119	addKeyName(KEY_BUTTON10, "PAD_BUTTON10" );
120	addKeyName(KEY_BUTTON11, "PAD_BUTTON11" );
121	addKeyName(KEY_BUTTON12, "PAD_BUTTON12" );
122	addKeyName(KEY_BUTTON13, "PAD_BUTTON13" );
123	addKeyName(KEY_BUTTON14, "PAD_BUTTON14" );
124	addKeyName(KEY_BUTTON15, "PAD_BUTTON15" );
125	
126	addKeyName(KEY_BACKSPACE, "Backsp" );
127	addKeyName(KEY_DELETE, "Del" );
128	addKeyName(KEY_SHIFT, "Shift" );
129	addKeyName(KEY_CONTROL, "Ctrl" );
130	addKeyName(KEY_ALT, "Alt" );
131	addKeyName(KEY_HYPHEN, "-" );
132	addKeyName(KEY_EQUALS, "=" );
133	addKeyName(KEY_INSERT, "Ins" );
134	addKeyName(KEY_CAPSLOCK, "CapsLock" );
135}
136
137
138LLKeyboard::~LLKeyboard()
139{
140	// nothing
141}
142
143void LLKeyboard::addKeyName(KEY key, const std::string& name)
144{
145	sKeysToNames[key] = name;
146	std::string nameuc = name;
147	LLStringUtil::toUpper(nameuc);
148	sNamesToKeys[nameuc] = key;
149}
150
151// BUG this has to be called when an OS dialog is shown, otherwise modifier key state
152// is wrong because the keyup event is never received by the main window. JC
153void LLKeyboard::resetKeys()
154{
155	S32 i;
156
157	for (i = 0; i < KEY_COUNT; i++)
158	{
159		if( mKeyLevel[i] )
160		{
161			mKeyLevel[i] = FALSE;
162		}
163	}
164
165	for (i = 0; i < KEY_COUNT; i++)
166	{
167		mKeyUp[i] = FALSE;
168	}
169
170	for (i = 0; i < KEY_COUNT; i++)
171	{
172		mKeyDown[i] = FALSE;
173	}
174
175	for (i = 0; i < KEY_COUNT; i++)
176	{
177		mKeyRepeated[i] = FALSE;
178	}
179}
180
181
182BOOL LLKeyboard::translateKey(const U16 os_key, KEY *out_key)
183{
184	std::map<U16, KEY>::iterator iter;
185
186	// Only translate keys in the map, ignore all other keys for now
187	iter = mTranslateKeyMap.find(os_key);
188	if (iter == mTranslateKeyMap.end())
189	{
190		//llwarns << "Unknown virtual key " << os_key << llendl;
191		*out_key = 0;
192		return FALSE;
193	}
194	else
195	{
196		*out_key = iter->second;
197		return TRUE;
198	}
199}
200
201
202U16 LLKeyboard::inverseTranslateKey(const KEY translated_key)
203{
204	std::map<KEY, U16>::iterator iter;
205	iter = mInvTranslateKeyMap.find(translated_key);
206	if (iter == mInvTranslateKeyMap.end())
207	{
208		return 0;
209	}
210	else
211	{
212		return iter->second;
213	}
214}
215
216
217BOOL LLKeyboard::handleTranslatedKeyDown(KEY translated_key, U32 translated_mask)
218{
219	BOOL handled = FALSE;
220	BOOL repeated = FALSE;
221
222	// is this the first time the key went down?
223	// if so, generate "character" message
224	if( !mKeyLevel[translated_key] )
225	{
226		mKeyLevel[translated_key] = TRUE;
227		mKeyLevelTimer[translated_key].reset();
228		mKeyLevelFrameCount[translated_key] = 0;
229		mKeyRepeated[translated_key] = FALSE;
230	}
231	else
232	{
233		// Level is already down, assume it's repeated.
234		repeated = TRUE;
235		mKeyRepeated[translated_key] = TRUE;
236	}
237	
238	mKeyDown[translated_key] = TRUE;
239	mCurTranslatedKey = (KEY)translated_key;
240	handled = mCallbacks->handleTranslatedKeyDown(translated_key, translated_mask, repeated);
241	return handled;
242}
243
244
245BOOL LLKeyboard::handleTranslatedKeyUp(KEY translated_key, U32 translated_mask)
246{	
247	BOOL handled = FALSE;
248	if( mKeyLevel[translated_key] )
249	{
250		mKeyLevel[translated_key] = FALSE;
251		
252		// Only generate key up events if the key is thought to
253		// be down.  This allows you to call resetKeys() in the
254		// middle of a frame and ignore subsequent KEY_UP
255		// messages in the same frame.  This was causing the
256		// sequence W<return> in chat to move agents forward. JC
257		mKeyUp[translated_key] = TRUE;
258		handled = mCallbacks->handleTranslatedKeyUp(translated_key, translated_mask);
259	}
260	
261	lldebugst(LLERR_USER_INPUT) << "keyup -" << translated_key << "-" << llendl;
262
263	return handled;
264}
265
266
267void LLKeyboard::toggleInsertMode()
268{
269	if (LL_KIM_INSERT == mInsertMode)
270	{
271		mInsertMode = LL_KIM_OVERWRITE;
272	}
273	else
274	{
275		mInsertMode = LL_KIM_INSERT;
276	}
277}
278
279
280// Returns time in seconds since key was pressed.
281F32 LLKeyboard::getKeyElapsedTime(KEY key)
282{
283	return mKeyLevelTimer[key].getElapsedTimeF32();
284}
285
286// Returns time in frames since key was pressed.
287S32 LLKeyboard::getKeyElapsedFrameCount(KEY key)
288{
289	return mKeyLevelFrameCount[key];
290}
291
292// static
293BOOL LLKeyboard::keyFromString(const std::string& str, KEY *key)
294{
295	std::string instring(str);
296	size_t length = instring.size();
297
298	if (length < 1)
299	{
300		return FALSE;
301	}
302	if (length == 1)
303	{
304		char ch = toupper(instring[0]);
305		if (('0' <= ch && ch <= '9') ||
306			('A' <= ch && ch <= 'Z') ||
307			('!' <= ch && ch <= '/') || // !"#$%&'()*+,-./
308			(':' <= ch && ch <= '@') || // :;<=>?@
309			('[' <= ch && ch <= '`') || // [\]^_`
310			('{' <= ch && ch <= '~'))   // {|}~
311		{
312			*key = ch;
313			return TRUE;
314		}
315	}
316
317	LLStringUtil::toUpper(instring);
318	KEY res = get_if_there(sNamesToKeys, instring, (KEY)0);
319	if (res != 0)
320	{
321		*key = res;
322		return TRUE;
323	}
324	llwarns << "keyFromString failed: " << str << llendl;
325	return FALSE;
326}
327
328
329// static
330std::string LLKeyboard::stringFromKey(KEY key)
331{
332	std::string res = get_if_there(sKeysToNames, key, std::string());
333	if (res.empty())
334	{
335		char buffer[2];		/* Flawfinder: ignore */
336		buffer[0] = key;
337		buffer[1] = '\0';
338		res = std::string(buffer);
339	}
340
341	LLKeyStringTranslatorFunc *trans = gKeyboard->mStringTranslator;
342	if (trans != NULL)
343	{
344		res = trans(res.c_str());
345	}
346
347	return res;
348}
349
350
351//static
352std::string LLKeyboard::stringFromAccelerator( MASK accel_mask, KEY key )
353{
354	std::string res;
355	
356	// break early if this is a silly thing to do.
357	if( KEY_NONE == key )
358	{
359		return res;
360	}
361	
362	LLKeyStringTranslatorFunc *trans = gKeyboard->mStringTranslator;
363	
364	if( trans == NULL )
365	{
366		llerrs << "No mKeyStringTranslator" << llendl;
367		return res;
368	}
369	
370	// Append any masks
371#ifdef LL_DARWIN
372	// Standard Mac names for modifier keys in menu equivalents
373	// We could use the symbol characters, but they only exist in certain fonts.
374	if( accel_mask & MASK_CONTROL )
375	{
376		if ( accel_mask & MASK_MAC_CONTROL )
377		{
378			res.append( trans("accel-mac-control") );
379		}
380		else
381		{
382			res.append( trans("accel-mac-command") );		// Symbol would be "\xE2\x8C\x98"
383		}
384	}
385	if( accel_mask & MASK_ALT )
386		res.append( trans("accel-mac-option") );		// Symbol would be "\xE2\x8C\xA5"
387	if( accel_mask & MASK_SHIFT )
388		res.append( trans("accel-mac-shift") );		// Symbol would be "\xE2\x8C\xA7"
389#else
390	if( accel_mask & MASK_CONTROL )
391		res.append( trans("accel-win-control") );
392	if( accel_mask & MASK_ALT )
393		res.append( trans("accel-win-alt") );
394	if( accel_mask & MASK_SHIFT )
395		res.append( trans("accel-win-shift") );
396#endif
397	std::string key_string = LLKeyboard::stringFromKey(key);
398	if ((accel_mask & MASK_NORMALKEYS) &&
399		(key_string[0] == '-' || key_string[0] == '=' || key_string[0] == '+'))
400	{
401		res.append( " " );
402	}
403
404	std::string keystr = stringFromKey( key );
405	res.append( keystr );
406	
407	return res;
408}
409
410
411//static
412BOOL LLKeyboard::maskFromString(const std::string& str, MASK *mask)
413{
414	std::string instring(str);
415	if (instring == "NONE")
416	{
417		*mask = MASK_NONE;
418		return TRUE;
419	}
420	else if (instring == "SHIFT")
421	{
422		*mask = MASK_SHIFT;
423		return TRUE;
424	}
425	else if (instring == "CTL")
426	{
427		*mask = MASK_CONTROL;
428		return TRUE;
429	}
430	else if (instring == "ALT")
431	{
432		*mask = MASK_ALT;
433		return TRUE;
434	}
435	else if (instring == "CTL_SHIFT")
436	{
437		*mask = MASK_CONTROL | MASK_SHIFT;
438		return TRUE;
439	}
440	else if (instring == "ALT_SHIFT")
441	{
442		*mask = MASK_ALT | MASK_SHIFT;
443		return TRUE;
444	}
445	else if (instring == "CTL_ALT")
446	{
447		*mask = MASK_CONTROL | MASK_ALT;
448		return TRUE;
449	}
450	else if (instring == "CTL_ALT_SHIFT")
451	{
452		*mask = MASK_CONTROL | MASK_ALT | MASK_SHIFT;
453		return TRUE;
454	}
455	else 
456	{
457		return FALSE;
458	}
459}
460
461
462//static
463void LLKeyboard::setStringTranslatorFunc( LLKeyStringTranslatorFunc *trans_func )
464{
465	mStringTranslator = trans_func;
466}