You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

KeyRemapper.cxx 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <os/Mutex.h>
  23. #include <rfb/KeyRemapper.h>
  24. #include <rfb/Configuration.h>
  25. #include <rfb/LogWriter.h>
  26. using namespace rfb;
  27. static LogWriter vlog("KeyRemapper");
  28. KeyRemapper KeyRemapper::defInstance;
  29. KeyRemapper::KeyRemapper(const char* m)
  30. {
  31. mutex = new os::Mutex;
  32. setMapping(m);
  33. }
  34. KeyRemapper::~KeyRemapper()
  35. {
  36. delete mutex;
  37. }
  38. void KeyRemapper::setMapping(const char* m) {
  39. os::AutoMutex a(mutex);
  40. mapping.clear();
  41. while (m[0]) {
  42. int from, to;
  43. char bidi;
  44. const char* nextComma = strchr(m, ',');
  45. if (!nextComma)
  46. nextComma = m + strlen(m);
  47. if (sscanf(m, "0x%x%c>0x%x", &from,
  48. &bidi, &to) == 3) {
  49. if (bidi != '-' && bidi != '<')
  50. vlog.error("warning: unknown operation %c>, assuming ->", bidi);
  51. mapping[from] = to;
  52. if (bidi == '<')
  53. mapping[to] = from;
  54. } else {
  55. vlog.error("warning: bad mapping %.*s", (int)(nextComma-m), m);
  56. }
  57. m = nextComma;
  58. if (nextComma[0])
  59. m++;
  60. }
  61. }
  62. rdr::U32 KeyRemapper::remapKey(rdr::U32 key) const {
  63. os::AutoMutex a(mutex);
  64. std::map<rdr::U32,rdr::U32>::const_iterator i = mapping.find(key);
  65. if (i != mapping.end())
  66. return i->second;
  67. return key;
  68. }
  69. class KeyMapParameter : public StringParameter {
  70. public:
  71. KeyMapParameter()
  72. : StringParameter("RemapKeys", "Comma-separated list of incoming keysyms to remap. Mappings are expressed as two hex values, prefixed by 0x, and separated by ->", "") {
  73. setParam(value);
  74. }
  75. bool setParam(const char* v) {
  76. KeyRemapper::defInstance.setMapping(v);
  77. return StringParameter::setParam(v);
  78. }
  79. } defaultParam;