aboutsummaryrefslogtreecommitdiffstats
path: root/vncviewer/ShortcutHandler.cxx
blob: aa17a6d149a3fda45f138fc793c6706264551468 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* Copyright 2021-2025 Pierre Ossman for Cendio AB
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define XK_MISCELLANY
#include <rfb/keysymdef.h>

#include "ShortcutHandler.h"
#include "i18n.h"

ShortcutHandler::ShortcutHandler() :
  modifierMask(0), state(Idle)
{
}

void ShortcutHandler::setModifiers(unsigned mask)
{
  modifierMask = mask;
  reset();
}

ShortcutHandler::KeyAction ShortcutHandler::handleKeyPress(int keyCode,
                                                           uint32_t keySym)
{
  unsigned modifier, pressedMask;
  std::map<int, uint32_t>::const_iterator iter;

  pressedKeys[keyCode] = keySym;

  if (modifierMask == 0)
    return KeyNormal;

  modifier = keySymToModifier(keySym);

  pressedMask = 0;
  for (iter = pressedKeys.begin(); iter != pressedKeys.end(); ++iter)
    pressedMask |= keySymToModifier(iter->second);

  switch (state) {
  case Idle:
  case Arming:
  case Rearming:
    if (pressedMask == modifierMask) {
      // All triggering modifier keys are pressed
      state = Armed;
    } if (modifier && ((modifier & modifierMask) == modifier)) {
      // The new key is part of the triggering set
      if (state == Idle)
        state = Arming;
    } else {
      // The new key was something else
      state = Wedged;
    }
    return KeyNormal;
  case Armed:
    if (modifier && ((modifier & modifierMask) == modifier)) {
      // The new key is part of the triggering set
      return KeyNormal;
    } else if (modifier) {
      // The new key is some other modifier
      state = Wedged;
      return KeyNormal;
    } else {
      // The new key was something else
      state = Firing;
      firedKeys.insert(keyCode);
      return KeyShortcut;
    }
    break;
  case Firing:
    if (modifier) {
      // The new key is a modifier (may or may not be part of the
      // triggering set)
      return KeyIgnore;
    } else {
      // The new key was something else
      firedKeys.insert(keyCode);
      return KeyShortcut;
    }
  default:
    break;
  }

  return KeyNormal;
}

ShortcutHandler::KeyAction ShortcutHandler::handleKeyRelease(int keyCode)
{
  bool firedKey;
  unsigned pressedMask;
  std::map<int, uint32_t>::const_iterator iter;
  KeyAction action;

  firedKey = firedKeys.count(keyCode) != 0;

  firedKeys.erase(keyCode);
  pressedKeys.erase(keyCode);

  pressedMask = 0;
  for (iter = pressedKeys.begin(); iter != pressedKeys.end(); ++iter)
    pressedMask |= keySymToModifier(iter->second);

  switch (state) {
  case Arming:
    action = KeyNormal;
    break;
  case Armed:
    if (pressedKeys.empty())
      action = KeyUnarm;
    else if (pressedMask == modifierMask)
      action = KeyNormal;
    else {
      action = KeyNormal;
      state = Rearming;
    }
    break;
  case Rearming:
    if (pressedKeys.empty())
      action = KeyUnarm;
    else
      action = KeyNormal;
    break;
  case Firing:
    if (firedKey)
      action = KeyShortcut;
    else
      action = KeyIgnore;
    break;
  default:
    action = KeyNormal;
  }

  if (pressedKeys.empty())
    state = Idle;

  return action;
}

void ShortcutHandler::reset()
{
  state = Idle;
  firedKeys.clear();
  pressedKeys.clear();
}

// Keep list of valid values in sync with shortcutModifiers
unsigned ShortcutHandler::parseModifier(const char* key)
{
  if (strcasecmp(key, "Ctrl") == 0)
    return Control;
  else if (strcasecmp(key, "Shift") == 0)
    return Shift;
  else if (strcasecmp(key, "Alt") == 0)
    return Alt;
  else if (strcasecmp(key, "Win") == 0)
    return Super;
  else if (strcasecmp(key, "Super") == 0)
    return Super;
  else if (strcasecmp(key, "Option") == 0)
    return Alt;
  else if (strcasecmp(key, "Cmd") == 0)
    return Super;
  else
    return 0;
}

const char* ShortcutHandler::modifierString(unsigned key)
{
  if (key == Control)
    return "Ctrl";
  if (key == Shift)
    return "Shift";
  if (key == Alt)
    return "Alt";
  if (key == Super)
    return "Super";

  return "";
}

const char* ShortcutHandler::modifierPrefix(unsigned mask,
                                            bool justPrefix)
{
  static char prefix[256];

  prefix[0] = '\0';
  if (mask & Control) {
#ifdef __APPLE__
    strcat(prefix, "⌃");
#else
    strcat(prefix, _("Ctrl"));
    strcat(prefix, "+");
#endif
  }
  if (mask & Shift) {
#ifdef __APPLE__
    strcat(prefix, "⇧");
#else
    strcat(prefix, _("Shift"));
    strcat(prefix, "+");
#endif
  }
  if (mask & Alt) {
#ifdef __APPLE__
    strcat(prefix, "⌥");
#else
    strcat(prefix, _("Alt"));
    strcat(prefix, "+");
#endif
  }
  if (mask & Super) {
#ifdef __APPLE__
    strcat(prefix, "⌘");
#else
    strcat(prefix, _("Win"));
    strcat(prefix, "+");
#endif
  }

  if (prefix[0] == '\0')
    return "";

  if (justPrefix) {
#ifndef __APPLE__
    prefix[strlen(prefix)-1] = '\0';
#endif
    return prefix;
  }

#ifdef __APPLE__
  strcat(prefix, "\xc2\xa0"); // U+00A0 NO-BREAK SPACE
#endif

  return prefix;
}

unsigned ShortcutHandler::keySymToModifier(uint32_t keySym)
{
  switch (keySym) {
  case XK_Control_L:
  case XK_Control_R:
    return Control;
  case XK_Shift_L:
  case XK_Shift_R:
    return Shift;
  case XK_Alt_L:
  case XK_Alt_R:
    return Alt;
  case XK_Super_L:
  case XK_Super_R:
  case XK_Hyper_L:
  case XK_Hyper_R:
    return Super;
  }

  return 0;
}