Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 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 | |
| 19 | #include <stdio.h> |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 20 | |
| 21 | #include <os/Mutex.h> |
| 22 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 23 | #include <rfb/KeyRemapper.h> |
| 24 | #include <rfb/Configuration.h> |
| 25 | #include <rfb/LogWriter.h> |
| 26 | |
| 27 | using namespace rfb; |
| 28 | |
| 29 | static LogWriter vlog("KeyRemapper"); |
| 30 | |
| 31 | KeyRemapper KeyRemapper::defInstance; |
| 32 | |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 33 | KeyRemapper::KeyRemapper(const char* m) |
| 34 | { |
| 35 | mutex = new os::Mutex; |
| 36 | |
| 37 | setMapping(m); |
| 38 | } |
| 39 | |
| 40 | KeyRemapper::~KeyRemapper() |
| 41 | { |
| 42 | delete mutex; |
| 43 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 44 | |
| 45 | void KeyRemapper::setMapping(const char* m) { |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 46 | os::AutoMutex a(mutex); |
| 47 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 48 | 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 Ossman | fb450fb | 2015-03-03 16:34:56 +0100 | [diff] [blame] | 63 | vlog.error("warning: bad mapping %.*s", (int)(nextComma-m), m); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 64 | } |
| 65 | m = nextComma; |
| 66 | if (nextComma[0]) |
| 67 | m++; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | rdr::U32 KeyRemapper::remapKey(rdr::U32 key) const { |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 72 | os::AutoMutex a(mutex); |
| 73 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 74 | 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 | |
| 81 | class KeyMapParameter : public StringParameter { |
| 82 | public: |
| 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 | |