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.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 <string.h>
  23. #include <os/Mutex.h>
  24. #include <rfb/KeyRemapper.h>
  25. #include <rfb/Configuration.h>
  26. #include <rfb/LogWriter.h>
  27. using namespace rfb;
  28. static LogWriter vlog("KeyRemapper");
  29. KeyRemapper KeyRemapper::defInstance;
  30. KeyRemapper::KeyRemapper(const char* m)
  31. {
  32. mutex = new os::Mutex;
  33. setMapping(m);
  34. }
  35. KeyRemapper::~KeyRemapper()
  36. {
  37. delete mutex;
  38. }
  39. void KeyRemapper::setMapping(const char* m) {
  40. os::AutoMutex a(mutex);
  41. mapping.clear();
  42. while (m[0]) {
  43. int from, to;
  44. char bidi;
  45. const char* nextComma = strchr(m, ',');
  46. if (!nextComma)
  47. nextComma = m + strlen(m);
  48. if (sscanf(m, "0x%x%c>0x%x", &from,
  49. &bidi, &to) == 3) {
  50. if (bidi != '-' && bidi != '<')
  51. vlog.error("warning: unknown operation %c>, assuming ->", bidi);
  52. mapping[from] = to;
  53. if (bidi == '<')
  54. mapping[to] = from;
  55. } else {
  56. vlog.error("warning: bad mapping %.*s", (int)(nextComma-m), m);
  57. }
  58. m = nextComma;
  59. if (nextComma[0])
  60. m++;
  61. }
  62. }
  63. uint32_t KeyRemapper::remapKey(uint32_t key) const {
  64. os::AutoMutex a(mutex);
  65. std::map<uint32_t,uint32_t>::const_iterator i = mapping.find(key);
  66. if (i != mapping.end())
  67. return i->second;
  68. return key;
  69. }
  70. class KeyMapParameter : public StringParameter {
  71. public:
  72. KeyMapParameter()
  73. : StringParameter("RemapKeys", "Comma-separated list of incoming keysyms to remap. Mappings are expressed as two hex values, prefixed by 0x, and separated by ->", "") {
  74. KeyRemapper::defInstance.setMapping("");
  75. }
  76. bool setParam(const char* v) {
  77. KeyRemapper::defInstance.setMapping(v);
  78. return StringParameter::setParam(v);
  79. }
  80. } defaultParam;