PageRenderTime 709ms CodeModel.GetById 691ms RepoModel.GetById 1ms app.codeStats 0ms

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