aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/programs/Xserver/hw/nxagent/Keystroke.c
blob: 9a7c1cd652cec3c1de4c6b479e461cddf9f38232 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
/**************************************************************************/
/*                                                                        */
/* Copyright (c) 2001, 2011 NoMachine (http://www.nomachine.com)          */
/* Copyright (c) 2008-2014 Oleksandr Shneyder <o.shneyder@phoca-gmbh.de>  */
/* Copyright (c) 2011-2016 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>*/
/* Copyright (c) 2014-2016 Mihai Moldovan <ionic@ionic.de>                */
/* Copyright (c) 2014-2016 Ulrich Sibiller <uli42@gmx.de>                 */
/* Copyright (c) 2015-2016 Qindel Group (http://www.qindel.com)           */
/*                                                                        */
/* NXAGENT, NX protocol compression and NX extensions to this software    */
/* are copyright of the aforementioned persons and companies.             */
/*                                                                        */
/* Redistribution and use of the present software is allowed according    */
/* to terms specified in the file LICENSE which comes in the source       */
/* distribution.                                                          */
/*                                                                        */
/* All rights reserved.                                                   */
/*                                                                        */
/* NOTE: This software has received contributions from various other      */
/* contributors, only the core maintainers and supporters are listed as   */
/* copyright holders. Please contact us, if you feel you should be listed */
/* as copyright holder, as well.                                          */
/*                                                                        */
/**************************************************************************/

#include "X.h"
#include "keysym.h"

#include "screenint.h"
#include "scrnintstr.h"

#include "Agent.h"
#include "Display.h"
#include "Events.h"
#include "Options.h"
#include "Keyboard.h"
#include "Drawable.h"
#include "Init.h" /* extern int nxagentX2go */
#include "Utils.h"

#include <unistd.h>

#include <libxml/parser.h>
#include <libxml/tree.h>

extern Bool nxagentWMIsRunning;
extern char *nxagentKeystrokeFile;

#ifdef NX_DEBUG_INPUT
int nxagentDebugInputDevices = False;
unsigned long nxagentLastInputDevicesDumpTime = 0;
extern void nxagentDeactivateInputDevicesGrabs();
#endif

/*
 * Set here the required log level.
 */

#define PANIC
#define WARNING
#undef  TEST
#undef  DEBUG
#undef  DUMP

/* must be included _after_ DUMP */
#include "Keystroke.h"


/* this table is used to parse actions given on the command line or in the
 * config file, therefore indices have to match the enum in Keystroke.h */
char * nxagentSpecialKeystrokeNames[] = {
       "end_marker",
       "close_session",
       "switch_all_screens",
       "fullscreen",
       "minimize",
       "defer",
       "ignore",
       "force_synchronization",

#ifdef DEBUG_TREE
       "debug_tree",
#endif
#ifdef DUMP
       "regions_on_screen",
#endif
#ifdef NX_DEBUG_INPUT
       "test_input",
       "deactivate_input_devices_grab",
#endif
       "resize",
       "viewport_move_left",
       "viewport_move_up",
       "viewport_move_right",
       "viewport_move_down",
       "viewport_scroll_left",
       "viewport_scroll_up",
       "viewport_scroll_right",
       "viewport_scroll_down",

       "reread_keystrokes",

       "autograb",

       "dump_clipboard",

       NULL,
};

struct nxagentSpecialKeystrokeMap default_map[] = {
  /* stroke, modifierMask, modifierAltMeta, keysym */
#ifdef DEBUG_TREE
  {KEYSTROKE_DEBUG_TREE, ControlMask, True, XK_q},
#endif
  {KEYSTROKE_CLOSE_SESSION, ControlMask, True, XK_t},
  {KEYSTROKE_SWITCH_ALL_SCREENS, ControlMask, True, XK_f},
  {KEYSTROKE_FULLSCREEN, ControlMask | ShiftMask, True, XK_f},
  {KEYSTROKE_MINIMIZE, ControlMask, True, XK_m},
  {KEYSTROKE_DEFER, ControlMask, True, XK_e},
  {KEYSTROKE_FORCE_SYNCHRONIZATION, ControlMask, True, XK_j},
#ifdef DUMP
  {KEYSTROKE_REGIONS_ON_SCREEN, ControlMask, True, XK_a},
#endif
#ifdef NX_DEBUG_INPUT
  {KEYSTROKE_TEST_INPUT, ControlMask, True, XK_x},
  {KEYSTROKE_DEACTIVATE_INPUT_DEVICES_GRAB, ControlMask, True, XK_y},
#endif
  {KEYSTROKE_RESIZE, ControlMask, True, XK_r},
  {KEYSTROKE_VIEWPORT_MOVE_LEFT, ControlMask | ShiftMask, True, XK_Left},
  {KEYSTROKE_VIEWPORT_MOVE_LEFT, ControlMask | ShiftMask, True, XK_KP_Left},
  {KEYSTROKE_VIEWPORT_MOVE_UP, ControlMask | ShiftMask, True, XK_Up},
  {KEYSTROKE_VIEWPORT_MOVE_UP, ControlMask | ShiftMask, True, XK_KP_Up},
  {KEYSTROKE_VIEWPORT_MOVE_RIGHT, ControlMask | ShiftMask, True, XK_Right},
  {KEYSTROKE_VIEWPORT_MOVE_RIGHT, ControlMask | ShiftMask, True, XK_KP_Right},
  {KEYSTROKE_VIEWPORT_MOVE_DOWN, ControlMask | ShiftMask, True, XK_Down},
  {KEYSTROKE_VIEWPORT_MOVE_DOWN, ControlMask | ShiftMask, True, XK_KP_Down},
  {KEYSTROKE_VIEWPORT_SCROLL_LEFT, ControlMask, True, XK_Left},
  {KEYSTROKE_VIEWPORT_SCROLL_LEFT, ControlMask, True, XK_KP_Left},
  {KEYSTROKE_VIEWPORT_SCROLL_UP, ControlMask, True, XK_Up},
  {KEYSTROKE_VIEWPORT_SCROLL_UP, ControlMask, True, XK_KP_Up},
  {KEYSTROKE_VIEWPORT_SCROLL_RIGHT, ControlMask, True, XK_Right},
  {KEYSTROKE_VIEWPORT_SCROLL_RIGHT, ControlMask, True, XK_KP_Right},
  {KEYSTROKE_VIEWPORT_SCROLL_DOWN, ControlMask, True, XK_Down},
  {KEYSTROKE_VIEWPORT_SCROLL_DOWN, ControlMask, True, XK_KP_Down},
  {KEYSTROKE_REREAD_KEYSTROKES, ControlMask, True, XK_k},
  {KEYSTROKE_AUTOGRAB, ControlMask, True, XK_g},
  {KEYSTROKE_DUMP_CLIPBOARD, ControlMask | ShiftMask, True, XK_c},
  {KEYSTROKE_END_MARKER, 0, False, NoSymbol},
};
struct nxagentSpecialKeystrokeMap *map = default_map;

static Bool modifier_matches(unsigned int mask, int compare_alt_meta, unsigned int state)
{
  /* nxagentAltMetaMask needs special handling
   * it seems to me its an and-ed mask of all possible meta and alt keys
   * somehow...
   *
   * otherwise this function would be just a simple bitop
   */
  Bool ret = True;

  if (compare_alt_meta) {
    if (! (state & nxagentAltMetaMask)) {
      ret = False;
    }

    mask &= ~nxagentAltMetaMask;
    state &= ~nxagentAltMetaMask;
  }

  /* ignore CapsLock and/or Numlock if the keystroke does not
     explicitly require them */

  if ( !(mask & nxagentCapsMask) )
    state &= ~nxagentCapsMask;

  if ( !(mask & nxagentNumlockMask) )
    state &= ~nxagentNumlockMask;

  /* all modifiers except meta/alt have to match exactly, extra bits are evil */
  if (mask != state) {
    ret = False;
  }

  return ret;
}

static Bool read_binding_from_xmlnode(xmlNode *node, struct nxagentSpecialKeystrokeMap *ret)
{
  /* init the struct to have proper values in case not all attributes are found */
  struct nxagentSpecialKeystrokeMap newkm = {
    .stroke = KEYSTROKE_NOTHING,
    .modifierMask = 0,
    .modifierAltMeta = False,
    .keysym = NoSymbol
  };

  for (xmlAttr *attr = node->properties; attr; attr = attr->next)
  {
    /* ignore attributes without data (which should never happen anyways) */
    if (attr->children->content == NULL)
    {
      #ifdef DEBUG
      char *aname = (attr->name)?((char *)attr->name):"unknown";
      fprintf(stderr, "attribute %s with NULL value", aname);
      #endif
      continue;
    }

    if (strcmp((char *)attr->name, "action") == 0)
    {
      newkm.stroke = KEYSTROKE_NOTHING;
      for (int i = 0; nxagentSpecialKeystrokeNames[i] != NULL; i++)
      {
        if (strcmp(nxagentSpecialKeystrokeNames[i], (char *)attr->children->content) == 0)
        {
          /* this relies on the values of enum nxagentSpecialKeystroke and the
           * indices of nxagentSpecialKeystrokeNames being in sync */
          newkm.stroke = i;
          break;
        }
      }
      if (newkm.stroke == KEYSTROKE_NOTHING)
        fprintf(stderr, "Info: ignoring unknown keystroke action '%s'.\n", (char *)attr->children->content);
      continue;
    }
    else if (strcmp((char *)attr->name, "key") == 0)
    {
      newkm.keysym = XStringToKeysym((char *)attr->children->content);
      continue;
    }
    else
    {
      /* ignore attributes with value="0" or "false", everything else is interpreted as true */
      if (strcmp((char *)attr->children->content, "0") == 0 || strcmp((char *)attr->children->content, "false") == 0)
        continue;

           if (strcmp((char *)attr->name, "Mod1") == 0)    { newkm.modifierMask |= Mod1Mask; }
      else if (strcmp((char *)attr->name, "Mod2") == 0)    { newkm.modifierMask |= Mod2Mask; }
      else if (strcmp((char *)attr->name, "Mod3") == 0)    { newkm.modifierMask |= Mod3Mask; }
      else if (strcmp((char *)attr->name, "Mod4") == 0)    { newkm.modifierMask |= Mod4Mask; }
      else if (strcmp((char *)attr->name, "Mod5") == 0)    { newkm.modifierMask |= Mod5Mask; }
      else if (strcmp((char *)attr->name, "Control") == 0) { newkm.modifierMask |= ControlMask; }
      else if (strcmp((char *)attr->name, "Shift") == 0)   { newkm.modifierMask |= ShiftMask; }
      else if (strcmp((char *)attr->name, "Lock") == 0)    { newkm.modifierMask |= LockMask;  }
      else if (strcmp((char *)attr->name, "AltMeta") == 0) { newkm.modifierAltMeta = True; }
    }
  }

  if (newkm.stroke != KEYSTROKE_NOTHING && newkm.keysym != NoSymbol)
  {
    /* keysym and stroke are required, everything else is optional */
    memcpy(ret, &newkm, sizeof(struct nxagentSpecialKeystrokeMap));
    return True;
  }
  else
    return False;
}

char *checkKeystrokeFile(char *filename)
{
  if (!filename)
    return NULL;

  if (access(filename, R_OK) == 0)
  {
    return filename;
  }
  else
  {
    #ifdef WARNING
    fprintf(stderr, "Warning: Cannot read keystroke file '%s'.\n", filename);
    #endif
    return NULL;
  }
}

/*
 * searches a keystroke xml file
 *
 * search order:
 *  - '-keystrokefile' commandline parameter
 *  - $NXAGENT_KEYSTROKEFILE environment variable
 *  - $HOME/.nx/config/keystrokes.cfg
 *  - /etc/nxagent/keystrokes.cfg
 *  - hardcoded traditional NX default settings
 * If run in x2go flavour different filenames and varnames are used.
 */
void nxagentInitKeystrokes(Bool force)
{
  char *filename = NULL;

  char *homefile;
  char *etcfile;
  char *envvar;

  /* used for tracking if the config file parsing has already been
     done (regardless of the result) */
  static Bool done = False;

  if (force) {
    if (map != default_map)
    {
      SAFE_free(map);
      map = default_map;
    }
    fprintf(stderr, "Info: re-reading keystrokes configuration\n");
  }
  else
  {
    if (done)
      return;
  }

  done = True;

  if (nxagentX2go) {
    homefile = "/.x2go/config/keystrokes.cfg";
    etcfile = "/etc/x2go/keystrokes.cfg";
    envvar = "X2GO_KEYSTROKEFILE";
  } else {
    homefile = "/.nx/config/keystrokes.cfg";
    etcfile = "/etc/nxagent/keystrokes.cfg";
    envvar = "NXAGENT_KEYSTROKEFILE";
  }

  char *homepath = NULL;
  char *homedir = getenv("HOME");
  if (homedir)
  {
    if (-1 == asprintf(&homepath, "%s%s", homedir, homefile))
    {
      fprintf(stderr, "malloc failed");
      exit(EXIT_FAILURE);
    }
  }

  /* if any of the files can be read we have our candidate */
  if ((filename = checkKeystrokeFile(nxagentKeystrokeFile)) ||
      (filename = checkKeystrokeFile(getenv(envvar))) ||
      (filename = checkKeystrokeFile(homepath)) ||
      (filename = checkKeystrokeFile(etcfile)))
  {
    LIBXML_TEST_VERSION
    xmlDoc *doc = xmlReadFile(filename, NULL, 0);
    if (doc)
    {
      fprintf(stderr, "Info: using keystrokes file '%s'\n", filename);
      for (xmlNode *cur = xmlDocGetRootElement(doc); cur; cur = cur->next)
      {
        if (cur->type == XML_ELEMENT_NODE && strcmp((char *)cur->name, "keystrokes") == 0)
        {
          xmlNode *bindings;
          int num = 0;
          int idx = 0;

          for (bindings = cur->children; bindings; bindings = bindings->next)
          {
            if (bindings->type == XML_ELEMENT_NODE && strcmp((char *)bindings->name, "keystroke") == 0)
            {
              num++;
            }
          }
          #ifdef DEBUG
          fprintf(stderr, "%s: found %d keystrokes in %s\n", __func__, num, filename);
          #endif
          if (!(map = calloc(num+1, sizeof(struct nxagentSpecialKeystrokeMap))))
          {
            fprintf(stderr, "calloc failed");
            exit(EXIT_FAILURE);
          }

          for (bindings = cur->children; bindings; bindings = bindings->next)
          {
            map[idx].stroke = KEYSTROKE_NOTHING;
            if (bindings->type == XML_ELEMENT_NODE &&
                strcmp((char *)bindings->name, "keystroke") == 0 &&
                read_binding_from_xmlnode(bindings, &(map[idx])))
              {
                Bool store = True;
                for (int j = 0; j < idx; j++)
                {
                  if (map[j].stroke != KEYSTROKE_NOTHING &&
                      map[idx].keysym != NoSymbol &&
                      map[j].keysym == map[idx].keysym &&
                      map[j].modifierMask == map[idx].modifierMask &&
                      map[j].modifierAltMeta == map[idx].modifierAltMeta)
                  {
                      #ifdef WARNING
                      fprintf(stderr, "Warning: ignoring keystroke '%s' (already in use by '%s')\n",
                              nxagentSpecialKeystrokeNames[map[idx].stroke],
                              nxagentSpecialKeystrokeNames[map[j].stroke]);
                      #endif
                      store = False;
                      break;
                  }
                }

                if (store)
                  idx++;
                else
                  map[idx].stroke = KEYSTROKE_NOTHING;
              }
          }
          #ifdef DEBUG
          fprintf(stderr, "%s: read %d keystrokes", __func__, idx);
          #endif

          map[idx].stroke = KEYSTROKE_END_MARKER;
        }
      }

      xmlFreeDoc(doc);
      xmlCleanupParser();
    }
    #ifdef WARNING
    else /* if (doc) */
    {
      fprintf(stderr, "Warning: could not read/parse keystrokes file '%s'\n", filename);
    }
    #endif
    filename = NULL;
  }
  SAFE_free(homepath);

  if (map == default_map)
  {
    fprintf(stderr, "Info: Using builtin keystrokes.\n");
  }

  nxagentDumpKeystrokes();
}

static char *nxagentGetSingleKeystrokeString(struct nxagentSpecialKeystrokeMap *cur)
{
  if (!cur)
    return strdup(""); /* caller is expected to free the returned string */

  char *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9, *s10, *s11;
  s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = s9 = s10 = s11 = "";

  unsigned int mask = cur->modifierMask;

  if (mask & ControlMask)        {s1  = "Ctrl+";     mask &= ~ControlMask;}
  if (mask & ShiftMask)          {s2  = "Shift+";    mask &= ~ShiftMask;}

  /* these are only here for better readable modifier names. Normally
     they are covered by the Mod<n> and Lock lines below */
  if (cur->modifierAltMeta)      {s3  = "Alt+";      mask &= ~(cur->modifierAltMeta);}
  if (mask & nxagentCapsMask)    {s4  = "CapsLock+"; mask &= ~nxagentCapsMask;}
  if (mask & nxagentNumlockMask) {s5  = "NumLock+";  mask &= ~nxagentNumlockMask;}

  if (mask & Mod1Mask)           {s6  = "Mod1+";     mask &= ~Mod1Mask;}
  if (mask & Mod2Mask)           {s7  = "Mod2+";     mask &= ~Mod2Mask;}
  if (mask & Mod3Mask)           {s8  = "Mod3+";     mask &= ~Mod3Mask;}
  if (mask & Mod4Mask)           {s9  = "Mod4+";     mask &= ~Mod4Mask;}
  if (mask & Mod5Mask)           {s10 = "Mod5+";     mask &= ~Mod5Mask;}
  if (mask & LockMask)           {s11 = "Lock+";     mask &= ~LockMask;}

  char *ret = NULL;
  asprintf(&ret, "%s%s%s%s%s%s%s%s%s%s%s%s", s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, XKeysymToString(cur->keysym));
  return ret;
}

/*
 * return the _first_ keystroke for the passed keystroke name
 *
 * e.g. nxagentFindFirstKeystroke("resize") -> "Ctrl+Alt+r"
 *
 * result must be free()d after use.
 */
char *nxagentFindFirstKeystroke(char *name)
{
  for (struct nxagentSpecialKeystrokeMap *cur = map; cur->stroke != KEYSTROKE_END_MARKER; cur++)
  {
    if (nxagentSpecialKeystrokeNames[cur->stroke] &&
            strcmp(nxagentSpecialKeystrokeNames[cur->stroke], name) == 0)
    {
      return nxagentGetSingleKeystrokeString(cur);
    }
  }
  return NULL;
}

/*
 * return a string with linefeeds of all keystrokes who's name starts
 * with the the passed string,
 *
 * e.g. nxagentFindKeystrokeString("viewport_scroll_")
 * ->
 * "  viewport_scroll_left  : Ctrl+Alt+Left
 *   viewport_scroll_left  : Ctrl+Alt+KP_Left
 *   viewport_scroll_up    : Ctrl+Alt+Up
 *   viewport_scroll_up    : Ctrl+Alt+KP_Up
 *   viewport_scroll_right : Ctrl+Alt+Right
 *   viewport_scroll_right : Ctrl+Alt+KP_Right
 *   viewport_scroll_down  : Ctrl+Alt+Down
 *   viewport_scroll_down  : Ctrl+Alt+KP_Down
 * "
 * result must be free()d after use.
 */
char *nxagentFindMatchingKeystrokes(char *name)
{
  int maxlen = 0;
  for (int i = 0; nxagentSpecialKeystrokeNames[i]; i++)
    maxlen = max(maxlen, strlen(nxagentSpecialKeystrokeNames[i]));

  char * res = strdup(""); /* let the caller free the string */
  for (struct nxagentSpecialKeystrokeMap *cur = map; cur->stroke != KEYSTROKE_END_MARKER; cur++)
  {
    if (nxagentSpecialKeystrokeNames[cur->stroke] &&
        strncmp(nxagentSpecialKeystrokeNames[cur->stroke], name, strlen(name)) == 0)
    {
      char *tmp;
      char *tmp1 = nxagentGetSingleKeystrokeString(cur);
      if (-1 == asprintf(&tmp, "%s  %-*s : %s\n", res, maxlen,
                         nxagentSpecialKeystrokeNames[cur->stroke],
                         tmp1))
      {
        SAFE_free(tmp1);
        #ifdef TEST
        fprintf(stderr, "%s: returning incomplete result:\n%s", __func__, res);
        #endif
        return res;
      }
      else
      {
        SAFE_free(tmp1);
        free(res);
        res = tmp;
      }
    }
  }
  #ifdef TEST
  fprintf(stderr, "%s: returning result:\n%s", __func__, res);
  #endif
  return res;
}

void nxagentDumpKeystrokes(void)
{
  char *s = nxagentFindMatchingKeystrokes("");
  fprintf(stderr, "Currently known keystrokes:\n%s", s);
  SAFE_free(s);
}

static enum nxagentSpecialKeystroke find_keystroke(XKeyEvent *X)
{
  enum nxagentSpecialKeystroke ret = KEYSTROKE_NOTHING;

  KeySym keysym = XKeycodeToKeysym(nxagentDisplay, X->keycode, 0);

  #ifdef DEBUG
  fprintf(stderr, "%s: got keysym '%c' (%d)\n", __func__, keysym, keysym);
  #endif
  for (struct nxagentSpecialKeystrokeMap *cur = map; cur->stroke != KEYSTROKE_END_MARKER; cur++) {
    #ifdef DEBUG
    fprintf(stderr,"%s: keysym %d stroke %d, type %d\n", __func__, cur->keysym, cur->stroke, X->type);
    #endif
    if (cur->keysym == keysym && modifier_matches(cur->modifierMask, cur->modifierAltMeta, X->state)) {
      #ifdef DEBUG
      fprintf(stderr, "%s: match including modifiers for keysym '%c' (%d), stroke %d (%s)\n", __func__, cur->keysym, cur->keysym, cur->stroke, nxagentSpecialKeystrokeNames[cur->stroke]);
      #endif
      return cur->stroke;
    }
  }

  return ret;
}

/*
 * returns True if a special keystroke has been pressed. *result will contain the action.
 */
Bool nxagentCheckSpecialKeystroke(XKeyEvent *X, enum HandleEventResult *result)
{
  enum nxagentSpecialKeystroke stroke = find_keystroke(X);
  *result = doNothing;

  #ifdef TEST
  if (stroke != KEYSTROKE_NOTHING && stroke != KEYSTROKE_END_MARKER)
    fprintf(stderr, "nxagentCheckSpecialKeystroke: got code %x - state %x - stroke %d (%s)\n",
            X -> keycode, X -> state, stroke, nxagentSpecialKeystrokeNames[stroke]);
  else
    fprintf(stderr, "nxagentCheckSpecialKeystroke: got code %x - state %x - stroke %d (unused)\n",
            X -> keycode, X -> state, stroke);
  #endif

  if (stroke == KEYSTROKE_NOTHING)
    return False;

  /*
   * Check special keys.
   */

  /*
   * FIXME: We should use the keysym instead that the keycode
   *        here.
   */

  switch (stroke) {
#ifdef DEBUG_TREE
    case KEYSTROKE_DEBUG_TREE:
      *result = doDebugTree;
      break;
#endif
    case KEYSTROKE_CLOSE_SESSION:
      *result = doCloseSession;
      break;
    case KEYSTROKE_SWITCH_ALL_SCREENS:
      if (!nxagentOption(Rootless)) {
        *result = doSwitchAllScreens;
      }
      break;
    case KEYSTROKE_MINIMIZE:
      if (!nxagentOption(Rootless)) {
        *result = doMinimize;
      }
      break;
    case KEYSTROKE_DEFER:
      *result = doSwitchDeferMode;
      break;
    case KEYSTROKE_IGNORE:
      /* this is used e.g. to ignore C-A-Backspace aka XK_Terminate_Server */
      return True;
      break;
    case KEYSTROKE_FORCE_SYNCHRONIZATION:
      nxagentForceSynchronization = True;
      break;
#ifdef DUMP
    case KEYSTROKE_REGIONS_ON_SCREEN:
      nxagentRegionsOnScreen();
      break;
#endif
#ifdef NX_DEBUG_INPUT
    case KEYSTROKE_TEST_INPUT:
      /*
       * Used to test the input devices state.
       */
      if (X -> type == KeyPress) {
        if (!nxagentDebugInputDevices) {
          fprintf(stderr, "Info: Turning input devices debug ON.\n");
          nxagentDebugInputDevices = True;
        } else {
          fprintf(stderr, "Info: Turning input devices debug OFF.\n");
          nxagentDebugInputDevices = False;
          nxagentLastInputDevicesDumpTime = 0;
        }
      }
      return True;
      break;
    case KEYSTROKE_DEACTIVATE_INPUT_DEVICES_GRAB:
      if (X->type == KeyPress) {
        nxagentDeactivateInputDevicesGrabs();
      }
      return True;
      break;
#endif
    case KEYSTROKE_FULLSCREEN:
      if (!nxagentOption(Rootless)) {
        *result = doSwitchFullscreen;
      }
      break;
    case KEYSTROKE_RESIZE:
      if (!nxagentOption(Rootless)) {
        *result = doSwitchResizeMode;
      }
      break;
    case KEYSTROKE_VIEWPORT_MOVE_LEFT:
      if (!nxagentOption(Rootless) && !nxagentOption(DesktopResize)) {
        *result = doViewportMoveLeft;
      }
      break;
    case KEYSTROKE_VIEWPORT_MOVE_UP:
      if (!nxagentOption(Rootless) && !nxagentOption(DesktopResize)) {
        *result = doViewportMoveUp;
      }
      break;
    case KEYSTROKE_VIEWPORT_MOVE_RIGHT:
      if (!nxagentOption(Rootless) && !nxagentOption(DesktopResize)) {
        *result = doViewportMoveRight;
      }
      break;
    case KEYSTROKE_VIEWPORT_MOVE_DOWN:
      if (!nxagentOption(Rootless) && !nxagentOption(DesktopResize)) {
        *result = doViewportMoveDown;
      }
      break;
    case KEYSTROKE_VIEWPORT_SCROLL_LEFT:
      if (!nxagentOption(Rootless) && !nxagentOption(DesktopResize)) {
        *result = doViewportLeft;
      }
      break;
    case KEYSTROKE_VIEWPORT_SCROLL_UP:
      if (!nxagentOption(Rootless) && !nxagentOption(DesktopResize)) {
        *result = doViewportUp;
      }
      break;
    case KEYSTROKE_VIEWPORT_SCROLL_RIGHT:
      if (!nxagentOption(Rootless) && !nxagentOption(DesktopResize)) {
        *result = doViewportRight;
      }
      break;
    case KEYSTROKE_VIEWPORT_SCROLL_DOWN:
      if (!nxagentOption(Rootless) && !nxagentOption(DesktopResize)) {
        *result = doViewportDown;
      }
      break;
    case KEYSTROKE_REREAD_KEYSTROKES:
      /* two reasons to check on KeyRelease:
         - this code is called for KeyPress and KeyRelease, so we
           would read the keystroke file twice
         - if the keystroke file changes settings for this key this
           might lead to unexpected behaviour
      */
      if (X->type == KeyRelease)
        nxagentInitKeystrokes(True);
      break;
    case KEYSTROKE_AUTOGRAB:
      *result = doAutoGrab;
      break;
    case KEYSTROKE_DUMP_CLIPBOARD:
      *result = doDumpClipboard;
      break;
    case KEYSTROKE_NOTHING: /* do nothing. difference to KEYSTROKE_IGNORE is the return value */
    case KEYSTROKE_END_MARKER: /* just to make gcc STFU */
    case KEYSTROKE_MAX:
      break;
  }
  return (*result != doNothing);
}