File: keymap.c

package info (click to toggle)
nawm 0.0.20030130-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 360 kB
  • ctags: 496
  • sloc: ansic: 3,158; sh: 1,582; yacc: 408; lex: 406; makefile: 153
file content (177 lines) | stat: -rw-r--r-- 4,550 bytes parent folder | download | duplicates (3)
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
/* keymap.c: watches the X keymaps and does clever things with keys */

/* Copyright (C) 1999 by the Massachusetts Institute of Technology.
 *
 * Permission to use, copy, modify, and distribute this
 * software and its documentation for any purpose and without
 * fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting
 * documentation, and that the name of M.I.T. not be used in
 * advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 */

#include "nawm.h"
#include <X11/extensions/XTest.h>

extern Display *dpy;

static KeySym *syms;
static XModifierKeymap *mods;
static int keysyms_per_keycode, min_keycode, max_keycode;

static char *modname[] = { "Shift", "Lock", "Control", "Meta",
			   "Mod2", "Mod3", "Mod4", "Mod5" };

void initkeymap(void)
{
  XDisplayKeycodes(dpy, &min_keycode, &max_keycode);
  syms = XGetKeyboardMapping(dpy, min_keycode, max_keycode - min_keycode + 1,
			     &keysyms_per_keycode);
  mods = XGetModifierMapping(dpy);
}

void update_keymap(void)
{
  XFree(syms);
  XFreeModifiermap(mods);
  initkeymap();
}

struct modstate {
  KeyCode kc;
  int pressed;
};

void *set_modifier_state(int setmods)
{
  int nmods, modsize, mod, key, state, wantstate;
  struct modstate *old;
  char keys[32];
  KeyCode kc;

  nmods = 0;
  modsize = 10;
  old = xmalloc(modsize * sizeof(struct modstate));
  memset(old, 0, modsize * sizeof(struct modstate));

  XQueryKeymap(dpy, keys);

  /* For each modifier... */
  for (mod = 0; mod < 8; mod++)
    {
      wantstate = setmods & (1 << mod);

      /* For each key that generates that modifier... */
      for (key = 0; key < mods->max_keypermod; key++)
	{
	  kc = mods->modifiermap[mod * mods->max_keypermod + key];
	  if (!kc)
	    break;
	  state = KEYSTATE(keys, kc);

	  /* If the key isn't in the state we want, fix it and record
	   * that we changed it.
	   */
	  if (state != wantstate)
	    {
	      if (nmods == modsize - 1)
		{
		  old = xrealloc(old, modsize * 2 * sizeof(struct modstate));
		  memset(old + modsize * sizeof(struct modstate), 0,
			 modsize * sizeof(struct modstate));
		  modsize *= 2;
		}
	      old[nmods].kc = kc;
	      old[nmods].pressed = state;
	      nmods++;

	      if (wantstate)
		XTestFakeKeyEvent(dpy, kc, True, CurrentTime);
	      else
		XTestFakeKeyEvent(dpy, kc, False, CurrentTime);
	    }

	  /* If we want the modifier to be set, we only need to look
	   * at the first key, since we'll have set it if it wasn't
	   * already set. (If we want it unset, we need to check each
	   * key and unset any that are set.)
	   */
	  if (wantstate)
	    break;
	}
    }
  return (void *)old;
}

void reset_modifier_state(void *oldstate)
{
  struct modstate *old = (struct modstate *)oldstate;
  int i;

  for (i = 0; old[i].kc; i++)
    {
      if (!old[i].pressed)
	XTestFakeKeyEvent(dpy, old[i].kc, False, CurrentTime);
      else
	XTestFakeKeyEvent(dpy, old[i].kc, True, CurrentTime);
    }
  free(old);
}

void typechar(KeySym ks)
{
  KeyCode kc;
  int mod, xmod = -1, control = 0, start;
  void *old;

  if (ks < 32)
    {
      control = ControlMask;
      ks += 96;
    }
  kc = XKeysymToKeycode(dpy, ks);
  start = (kc - min_keycode) * keysyms_per_keycode;

  for (mod = 0; mod < 2; mod++)
    {
      /* This sucks, but OpenWindows does this wrong. (It lists
       * the uppercase letter in the 0 position and doesn't list
       * the lowercase letter at all.)
       */
      if (isalpha(ks))
	{
	  if (mod == 0 && islower(ks) && tolower(syms[start]) == ks)
	    {
	      /* Looking at unshifted position, want a lowercase
	       * letter, and this is the letter we're looking for.
	       */
	      xmod = 0;
	    }
	  else if (mod == 1 && isupper(ks) &&
		   (syms[start + mod] == ks || syms[start] == ks))
	    {
	      /* Looking at shifted position, want an uppercase
	       * letter, and either this is the letter we're looking
	       * for or the unshifted one was.
	       */
	      xmod = 1;
	    }
	}
      else if (syms[start + mod] == ks)
	xmod = mod;

      if (xmod != -1)
	{
	  old = set_modifier_state(xmod + control);
	  XTestFakeKeyEvent(dpy, kc, True, CurrentTime);
	  XTestFakeKeyEvent(dpy, kc, False, CurrentTime);
	  reset_modifier_state(old);
	  break;
	}
    }
}