aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/programs/Xserver/hw/nxagent/Keystroke.c
blob: 281fec243c357cc732c458f944e7611f752d64c2 (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
/**************************************************************************/
/*                                                                        */
/* 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 "Keystroke.h"
#include "Drawable.h"
#include "Init.h" /* extern int nxagentX2go */

#include <unistd.h>

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

extern Bool nxagentWMIsRunning;
extern Bool nxagentIpaq;
extern char *nxagentKeystrokeFile;
Bool nxagentKeystrokeFileParsed = False;

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

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

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


/* 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",
       "minimize",
       "left",
       "up",
       "right",
       "down",
       "resize",
       "defer",
       "ignore",
       "force_synchronization",

       "debug_tree",
       "regions_on_screen",
       "test_input",
       "deactivate_input_devices_grab",

       "fullscreen",
       "viewport_move_left",
       "viewport_move_up",
       "viewport_move_right",
       "viewport_move_down",
       NULL,
};

struct nxagentSpecialKeystrokeMap default_map[] = {
  /* stroke, modifierMask, modifierAltMeta, keysym */
  {KEYSTROKE_DEBUG_TREE, ControlMask, True, XK_q},
  {KEYSTROKE_DEBUG_TREE, ControlMask, True, XK_Q},
  {KEYSTROKE_CLOSE_SESSION, ControlMask, True, XK_t},
  {KEYSTROKE_CLOSE_SESSION, ControlMask, True, XK_T},
  {KEYSTROKE_SWITCH_ALL_SCREENS, ControlMask, True, XK_f},
  {KEYSTROKE_SWITCH_ALL_SCREENS, ControlMask, True, XK_F},
  {KEYSTROKE_MINIMIZE, ControlMask, True, XK_m},
  {KEYSTROKE_MINIMIZE, ControlMask, True, XK_M},
  {KEYSTROKE_LEFT, ControlMask, True, XK_Left},
  {KEYSTROKE_LEFT, ControlMask, True, XK_KP_Left},
  {KEYSTROKE_UP, ControlMask, True, XK_Up},
  {KEYSTROKE_UP, ControlMask, True, XK_KP_Up},
  {KEYSTROKE_RIGHT, ControlMask, True, XK_Right},
  {KEYSTROKE_RIGHT, ControlMask, True, XK_KP_Right},
  {KEYSTROKE_DOWN, ControlMask, True, XK_Down},
  {KEYSTROKE_DOWN, ControlMask, True, XK_KP_Down},
  {KEYSTROKE_RESIZE, ControlMask, True, XK_r},
  {KEYSTROKE_RESIZE, ControlMask, True, XK_R},
  {KEYSTROKE_DEFER, ControlMask, True, XK_e},
  {KEYSTROKE_DEFER, ControlMask, True, XK_E},
  {KEYSTROKE_IGNORE, ControlMask, True, XK_BackSpace},
  {KEYSTROKE_IGNORE, 0, False, XK_Terminate_Server},
  {KEYSTROKE_FORCE_SYNCHRONIZATION, ControlMask, True, XK_j},
  {KEYSTROKE_FORCE_SYNCHRONIZATION, ControlMask, True, XK_J},
  {KEYSTROKE_REGIONS_ON_SCREEN, ControlMask, True, XK_a},
  {KEYSTROKE_REGIONS_ON_SCREEN, ControlMask, True, XK_A},
  {KEYSTROKE_TEST_INPUT, ControlMask, True, XK_x},
  {KEYSTROKE_TEST_INPUT, ControlMask, True, XK_X},
  {KEYSTROKE_DEACTIVATE_INPUT_DEVICES_GRAB, ControlMask, True, XK_y},
  {KEYSTROKE_DEACTIVATE_INPUT_DEVICES_GRAB, ControlMask, True, XK_Y},
  {KEYSTROKE_FULLSCREEN, ControlMask | ShiftMask, True, XK_f},
  {KEYSTROKE_FULLSCREEN, ControlMask | ShiftMask, True, XK_F},
  {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_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;
  }

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

  return ret;
}

static Bool read_binding_from_xmlnode(xmlNode *node, struct nxagentSpecialKeystrokeMap *ret)
{
  struct nxagentSpecialKeystrokeMap newkm = {
    .stroke = KEYSTROKE_END_MARKER,
    .modifierMask = 0,
    .modifierAltMeta = False,
    .keysym = NoSymbol
  };
  xmlAttr *attr;

  for (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_END_MARKER;
      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;
        }
      }
      continue;
    }
    else if (strcmp((char *)attr->name, "key") == 0)
    {
      if (strcmp((char *)attr->children->content, "0") != 0 && strcmp((char *)attr->children->content, "false") != 0)
	newkm.keysym = XStringToKeysym((char *)attr->children->content);

      continue;
    }

    /* 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, "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_END_MARKER && newkm.keysym != NoSymbol)
  {
    /* keysym and stroke are required, everything else is optional */
    memcpy(ret, &newkm, sizeof(struct nxagentSpecialKeystrokeMap));
    return True;
  }
  else
    return False;
}

/*
 * 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
 */
static void parse_keystroke_file(void)
{
  char *filename = NULL;

  char *homefile = "/.nx/config/keystrokes.cfg";
  char *etcfile = "/etc/nxagent/keystrokes.cfg";

  if (nxagentKeystrokeFileParsed)
    return;

  nxagentKeystrokeFileParsed = True;

  if (nxagentX2go) {
    homefile = "/.x2go/config/keystrokes.cfg";
    etcfile = "/etc/x2go/keystrokes.cfg";
  }

  if (nxagentKeystrokeFile != NULL && access(nxagentKeystrokeFile, R_OK) == 0)
  {
    filename = strdup(nxagentKeystrokeFile);
    if (filename == NULL)
    {
      fprintf(stderr, "malloc failed");
      exit(EXIT_FAILURE);
    }
  }
  else if ((filename = getenv("NXAGENT_KEYSTROKEFILE")) != NULL && access(filename, R_OK) == 0)
  {
    filename = strdup(filename);
    if (filename == NULL)
    {
      fprintf(stderr, "malloc failed");
      exit(EXIT_FAILURE);
    }
  }
  else
  {
    char *homedir = getenv("HOME");
    filename = NULL;
    if (homedir != NULL)
    {
      homedir = strdup(homedir);
      if (homedir == NULL)
      {
        fprintf(stderr, "malloc failed");
          exit(EXIT_FAILURE);
      }
      filename = calloc(1, strlen(homefile) + strlen(homedir) + 1);
      if (filename == NULL)
      {
        fprintf(stderr, "malloc failed");
        exit(EXIT_FAILURE);
      }
      strcpy(filename, homedir);
      strcpy(filename + strlen(homedir), homefile);
      if (homedir)
      {
        free(homedir);
      }
    }

    if (access(filename, R_OK) == 0)
    {
      /* empty */
    }
    else if (access(etcfile, R_OK) == 0)
    {
      if (filename)
        free(filename);
      filename = strdup(etcfile);
      if (filename == NULL)
      {
        fprintf(stderr, "malloc failed");
        exit(EXIT_FAILURE);
      }
    }
    else
    {
      if (filename)
        free(filename);
      filename = NULL;
    }
  }

  /* now we know which file to read, if any */
  if (filename)
  {
    xmlDoc *doc = NULL;
    xmlNode *root = NULL;
    LIBXML_TEST_VERSION
    doc = xmlReadFile(filename, NULL, 0);
    if (doc != NULL)
    {
      xmlNode *cur = NULL;
      root = xmlDocGetRootElement(doc);

      for (cur = root; cur; cur = cur->next)
      {
        if (cur->type == XML_ELEMENT_NODE && strcmp((char *)cur->name, "keystrokes") == 0)
        {
          xmlNode *bindings = NULL;
          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++;
            }
          }
          map = calloc((num + 1), sizeof(struct nxagentSpecialKeystrokeMap));
          if (map == NULL)
          {
            fprintf(stderr, "calloc failed");
            exit(EXIT_FAILURE);
          }

          for (bindings = cur->children; bindings; bindings = bindings->next)
          {
            if (bindings->type == XML_ELEMENT_NODE &&
                strcmp((char *)bindings->name, "keystroke") == 0 &&
                read_binding_from_xmlnode(bindings, &(map[idx])))
                  idx++;
          }

          map[idx].stroke = KEYSTROKE_END_MARKER;
        }
      }

      xmlFreeDoc(doc);
      xmlCleanupParser();
    }
    else
    {
      #ifdef DEBUG
      fprintf("XML parsing for %s failed\n", filename);
      #endif
    }
    free(filename);
  }
}

static enum nxagentSpecialKeystroke find_keystroke(XKeyEvent *X)
{
  enum nxagentSpecialKeystroke ret = KEYSTROKE_NOTHING;
  int keysyms_per_keycode_return;
  struct nxagentSpecialKeystrokeMap *cur = map;

  parse_keystroke_file();

  XlibKeySym *keysym = XGetKeyboardMapping(nxagentDisplay,
                                           X->keycode,
                                           1,
                                           &keysyms_per_keycode_return);

  while (cur->stroke != KEYSTROKE_END_MARKER) {
    if (cur->keysym == keysym[0] && modifier_matches(cur->modifierMask, cur->modifierAltMeta, X->state)) {
      free(keysym);
      return cur->stroke;
    }
    cur++;
  }

  free(keysym);
  return ret;
}

Bool nxagentCheckSpecialKeystroke(XKeyEvent *X, enum HandleEventResult *result)
{
  enum nxagentSpecialKeystroke stroke = find_keystroke(X);
  *result = doNothing;

  if (stroke == KEYSTROKE_NOTHING)
    return False;

  #ifdef TEST
  fprintf(stderr, "nxagentCheckSpecialKeystroke: got code %x - state %x - stroke %d\n",
    X -> keycode, X -> state, stroke);
  #endif

  /*
   * Check special keys.
   */

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

  if (X -> keycode == 130 && nxagentIpaq)
  {
    *result = doStartKbd;

    return True;
  }

  switch (stroke) {
    case KEYSTROKE_DEBUG_TREE:
      #ifdef DEBUG_TREE
      *result = doDebugTree;
      #endif
      break;
    case KEYSTROKE_CLOSE_SESSION:
      *result = doCloseSession;
      break;
    case KEYSTROKE_SWITCH_ALL_SCREENS:
      if (nxagentOption(Rootless) == False) {
        *result = doSwitchAllScreens;
      }
      break;
    case KEYSTROKE_MINIMIZE:
      if (nxagentOption(Rootless) == False) {
        *result = doMinimize;
      }
      break;
    case KEYSTROKE_LEFT:
      if (nxagentOption(Rootless) == False &&
          nxagentOption(DesktopResize) == False) {
        *result = doViewportLeft;
      }
      break;
    case KEYSTROKE_UP:
      if (nxagentOption(Rootless) == False &&
          nxagentOption(DesktopResize) == False) {
        *result = doViewportUp;
      }
      break;
    case KEYSTROKE_RIGHT:
      if (nxagentOption(Rootless) == False &&
          nxagentOption(DesktopResize) == False) {
        *result = doViewportRight;
      }
      break;
    case KEYSTROKE_DOWN:
      if (nxagentOption(Rootless) == False &&
          nxagentOption(DesktopResize) == False) {
        *result = doViewportDown;
      }
      break;
    case KEYSTROKE_RESIZE:
      if (nxagentOption(Rootless) == False) {
        *result = doSwitchResizeMode;
      }
      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 = 1;
      break;
    case KEYSTROKE_REGIONS_ON_SCREEN:
      #ifdef DUMP
      nxagentRegionsOnScreen();
      #endif
      break;
    case KEYSTROKE_TEST_INPUT:
      /*
       * Used to test the input devices state.
       */
      #ifdef NX_DEBUG_INPUT
      if (X -> type == KeyPress) {
        if (nxagentDebugInputDevices == 0) {
          fprintf(stderr, "Info: Turning input devices debug ON.\n");
          nxagentDebugInputDevices = 1;
        } else {
          fprintf(stderr, "Info: Turning input devices debug OFF.\n");
          nxagentDebugInputDevices = 0;
          nxagentLastInputDevicesDumpTime = 0;
        }
      }
      return True;
      #endif
      break;
    case KEYSTROKE_DEACTIVATE_INPUT_DEVICES_GRAB:
      #ifdef NX_DEBUG_INPUT
      if (X->type == KeyPress) {
        nxagentDeactivateInputDevicesGrab();
      }
      return True;
      #endif
      break;
    case KEYSTROKE_FULLSCREEN:
      if (nxagentOption(Rootless) == 0) {
        *result = doSwitchFullscreen;
      }
      break;
    case KEYSTROKE_VIEWPORT_MOVE_LEFT:
      if (nxagentOption(Rootless) == 0 &&
          nxagentOption(DesktopResize) == 0) {
        *result = doViewportMoveLeft;
      }
      break;
    case KEYSTROKE_VIEWPORT_MOVE_UP:
      if (nxagentOption(Rootless) == 0 &&
          nxagentOption(DesktopResize) == 0) {
        *result = doViewportMoveUp;
      }
      break;
    case KEYSTROKE_VIEWPORT_MOVE_RIGHT:
      if (nxagentOption(Rootless) == 0 &&
          nxagentOption(DesktopResize) == 0) {
        *result = doViewportMoveRight;
      }
      break;
    case KEYSTROKE_VIEWPORT_MOVE_DOWN:
      if (nxagentOption(Rootless) == 0 &&
          nxagentOption(DesktopResize) == 0) {
        *result = doViewportMoveDown;
      }
      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);
}