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