blob: b4c2793c9d20a13acc1953667cd274638ad42ff8 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* 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
19#include <stdio.h>
Pierre Ossman338e73a2016-07-07 15:35:13 +020020
21#include <os/Mutex.h>
22
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000023#include <rfb/KeyRemapper.h>
24#include <rfb/Configuration.h>
25#include <rfb/LogWriter.h>
26
27using namespace rfb;
28
29static LogWriter vlog("KeyRemapper");
30
31KeyRemapper KeyRemapper::defInstance;
32
Pierre Ossman338e73a2016-07-07 15:35:13 +020033KeyRemapper::KeyRemapper(const char* m)
34{
35 mutex = new os::Mutex;
36
37 setMapping(m);
38}
39
40KeyRemapper::~KeyRemapper()
41{
42 delete mutex;
43}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000044
45void KeyRemapper::setMapping(const char* m) {
Pierre Ossman338e73a2016-07-07 15:35:13 +020046 os::AutoMutex a(mutex);
47
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000048 mapping.clear();
49 while (m[0]) {
50 int from, to;
51 char bidi;
52 const char* nextComma = strchr(m, ',');
53 if (!nextComma)
54 nextComma = m + strlen(m);
55 if (sscanf(m, "0x%x%c>0x%x", &from,
56 &bidi, &to) == 3) {
57 if (bidi != '-' && bidi != '<')
58 vlog.error("warning: unknown operation %c>, assuming ->", bidi);
59 mapping[from] = to;
60 if (bidi == '<')
61 mapping[to] = from;
62 } else {
Pierre Ossmanfb450fb2015-03-03 16:34:56 +010063 vlog.error("warning: bad mapping %.*s", (int)(nextComma-m), m);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000064 }
65 m = nextComma;
66 if (nextComma[0])
67 m++;
68 }
69}
70
71rdr::U32 KeyRemapper::remapKey(rdr::U32 key) const {
Pierre Ossman338e73a2016-07-07 15:35:13 +020072 os::AutoMutex a(mutex);
73
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000074 std::map<rdr::U32,rdr::U32>::const_iterator i = mapping.find(key);
75 if (i != mapping.end())
76 return i->second;
77 return key;
78}
79
80
81class KeyMapParameter : public StringParameter {
82public:
83 KeyMapParameter()
84 : StringParameter("RemapKeys", "Comma-separated list of incoming keysyms to remap. Mappings are expressed as two hex values, prefixed by 0x, and separated by ->", "") {
85 setParam(value);
86 }
87 bool setParam(const char* v) {
88 KeyRemapper::defInstance.setMapping(v);
89 return StringParameter::setParam(v);
90 }
91} defaultParam;
92
93