1/*
2
3Copyright 1988, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included
12in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall
23not be used in advertising or otherwise to promote the sale, use or
24other dealings in this Software without prior written authorization
25from The Open Group.
26
27*/
28
29/*
30 * Copyright (c) 1987, Oracle and/or its affiliates. All rights reserved.
31 *
32 * Permission is hereby granted, free of charge, to any person obtaining a
33 * copy of this software and associated documentation files (the "Software"),
34 * to deal in the Software without restriction, including without limitation
35 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36 * and/or sell copies of the Software, and to permit persons to whom the
37 * Software is furnished to do so, subject to the following conditions:
38 *
39 * The above copyright notice and this permission notice (including the next
40 * paragraph) shall be included in all copies or substantial portions of the
41 * Software.
42 *
43 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
46 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
47 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
48 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
49 * DEALINGS IN THE SOFTWARE.
50 */
51
52/*
53 * Author:  Jim Fulton, MIT X Consortium; derived from parts of the
54 * original xmodmap, written by David Rosenthal, of Sun Microsystems.
55 */
56
57#ifdef HAVE_CONFIG_H
58# include "config.h"
59#endif
60
61#ifdef WIN32
62#include <X11/Xwindows.h>
63#endif
64
65#include <X11/Xos.h>
66#include <X11/Xlib.h>
67#include <stdio.h>
68#include "xmodmap.h"
69#include "wq.h"
70
71static void
72mapping_busy_key(int timeout)
73{
74    int i;
75    unsigned char keymap[32];
76    static unsigned int masktable[8] = {
77	0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
78
79    XQueryKeymap (dpy, (char *) keymap);
80
81    fprintf (stderr,
82	     "%s:  please release the following keys within %d seconds:\n",
83	     ProgramName, timeout);
84    for (i = 0; i < 256; i++) {
85	if (keymap[i >> 3] & masktable[i & 7]) {
86	    KeySym ks = XKeycodeToKeysym (dpy, (KeyCode) i, 0);
87	    char *cp = XKeysymToString (ks);
88	    fprintf (stderr, "    %s (keysym 0x%x, keycode %d)\n",
89		     cp ? cp : "UNNAMED", (unsigned int)ks, i);
90	}
91    }
92    sleep (timeout);
93    return;
94}
95
96static void
97mapping_busy_pointer(int timeout)
98{
99    int i;
100    Window root, child;			/* dummy variables */
101    int rx, ry, wx, wy;			/* dummy variables */
102    unsigned int mask;
103    static unsigned int masks[5] = {
104	Button1Mask, Button2Mask, Button3Mask, Button4Mask, Button5Mask };
105
106    if (!XQueryPointer (dpy, RootWindow(dpy,DefaultScreen(dpy)),
107			&root, &child, &rx, &ry, &wx, &wy, &mask))
108      mask = 0;
109
110    fprintf (stderr,
111	     "%s:  please release the following buttons within %d seconds:\n",
112	     ProgramName, timeout);
113    for (i = 0; i < 5; i++) {
114	if (mask & masks[i])
115	  fprintf (stderr, "    Button%d\n", i+1);
116    }
117    sleep (timeout);
118    return;
119}
120
121
122/*
123 * UpdateModifierMapping - this sends the modifier map to the server
124 * and deals with retransmissions due to the keyboard being busy.
125 */
126
127int
128UpdateModifierMapping(XModifierKeymap *map)
129{
130    int retries, timeout;
131
132    for (retries = 5, timeout = 2; retries > 0; retries--, timeout *= 2) {
133	int result;
134
135	result = XSetModifierMapping (dpy, map);
136	switch (result) {
137	  case MappingSuccess:	/* Success */
138	    return (0);
139	  case MappingBusy:		/* Busy */
140	    mapping_busy_key (timeout);
141	    continue;
142	  case MappingFailed:
143	    fprintf (stderr, "%s: bad set modifier mapping.\n",
144		     ProgramName);
145	    return (-1);
146	  default:
147	    fprintf (stderr, "%s:  bad return %d from XSetModifierMapping\n",
148		     ProgramName, result);
149	    return (-1);
150	}
151    }
152    fprintf (stderr,
153	     "%s:  unable to set modifier mapping, keyboard problem\n",
154	     ProgramName);
155    return (-1);
156}
157
158
159/*
160 * AddModifier - this adds a keycode to the modifier list
161 */
162
163int
164AddModifier(XModifierKeymap **mapp, KeyCode keycode, int modifier)
165{
166    if (keycode) {
167	*mapp = XInsertModifiermapEntry (*mapp, keycode, modifier);
168	return (0);
169    } else {
170	return (-1);
171    }
172    /*NOTREACHED*/
173}
174
175
176/*
177 * DeleteModifier - this removes a keycode from the modifier list
178 */
179
180int
181RemoveModifier(XModifierKeymap **mapp, KeyCode keycode, int modifier)
182{
183    if (keycode) {
184	*mapp = XDeleteModifiermapEntry (*mapp, keycode, modifier);
185	return (0);
186    } else {
187	return (-1);
188    }
189    /*NOTREACHED*/
190}
191
192
193/*
194 * ClearModifier - this removes all entries from the modifier list
195 */
196
197int
198ClearModifier(XModifierKeymap **mapp, int modifier)
199{
200    int i;
201    XModifierKeymap *map = *mapp;
202    KeyCode *kcp;
203
204    kcp = &map->modifiermap[modifier * map->max_keypermod];
205    for (i = 0; i < map->max_keypermod; i++) {
206	*kcp++ = (KeyCode) 0;
207    }
208    return (0);
209}
210
211static int
212GetKeysymsPerKeycode(void)
213{
214    int min_keycode, max_keycode, keysyms_per_keycode = 0;
215    KeySym *m;
216
217    XDisplayKeycodes(dpy, &min_keycode, &max_keycode);
218    m = XGetKeyboardMapping(dpy, min_keycode, (max_keycode - min_keycode + 1),
219                            &keysyms_per_keycode);
220    XFree(m);
221    return keysyms_per_keycode;
222}
223
224/*
225 * print the contents of the map
226 */
227void
228PrintModifierMapping(XModifierKeymap *map, FILE *fp)
229{
230    int k = 0;
231    int keysyms_per_keycode = GetKeysymsPerKeycode();
232
233    fprintf (fp,
234    	     "%s:  up to %d keys per modifier, (keycodes in parentheses):\n\n",
235    	     ProgramName, map->max_keypermod);
236    for (int i = 0; i < 8; i++) {
237	fprintf(fp, "%-10s", modifier_table[i].name);
238
239	for (int j = 0; j < map->max_keypermod; j++) {
240	    if (map->modifiermap[k]) {
241		KeySym ks;
242		int index = 0;
243		char *nm;
244		do {
245		    ks = XKeycodeToKeysym(dpy, map->modifiermap[k], index);
246		    index++;
247		} while ( !ks && index < keysyms_per_keycode);
248		nm = XKeysymToString(ks);
249
250		fprintf (fp, "%s  %s (0x%0x)", (j > 0 ? "," : ""),
251			 (nm ? nm : "BadKey"), map->modifiermap[k]);
252	    }
253	    k++;
254	}
255	fprintf(fp, "\n");
256    }
257    fprintf (fp, "\n");
258    return;
259}
260
261void
262PrintKeyTable(Bool exprs, FILE *fp)
263{
264    int         i;
265    int min_keycode, max_keycode, keysyms_per_keycode;
266    KeySym *keymap, *origkeymap;
267
268    XDisplayKeycodes (dpy, &min_keycode, &max_keycode);
269    origkeymap = XGetKeyboardMapping (dpy, min_keycode,
270				      (max_keycode - min_keycode + 1),
271				      &keysyms_per_keycode);
272
273    if (!origkeymap) {
274	fprintf (stderr, "%s:  unable to get keyboard mapping table.\n",
275		 ProgramName);
276	return;
277    }
278    if (!exprs) {
279	fprintf (fp,
280		 "There are %d KeySyms per KeyCode; KeyCodes range from %d to %d.\n\n",
281		 keysyms_per_keycode, min_keycode, max_keycode);
282	fprintf (fp, "    KeyCode\tKeysym (Keysym)\t...\n");
283	fprintf (fp, "    Value  \tValue   (Name) \t...\n\n");
284    }
285    keymap = origkeymap;
286    for (i = min_keycode; i <= max_keycode; i++) {
287	int  j, max;
288
289	if (exprs)
290	    fprintf(fp, "keycode %3d =", i);
291	else
292	    fprintf(fp, "    %3d    \t", i);
293	max = keysyms_per_keycode - 1;
294	while ((max >= 0) && (keymap[max] == NoSymbol))
295	    max--;
296	for (j = 0; j <= max; j++) {
297	    register KeySym ks = keymap[j];
298	    const char *s;
299	    if (ks != NoSymbol)
300		s = XKeysymToString (ks);
301	    else
302		s = "NoSymbol";
303	    if (!exprs)
304		fprintf (fp, "0x%04x (%s)\t",
305			 (unsigned int)ks, s ? s : "no name");
306	    else if (s)
307		fprintf (fp, " %s", s);
308	    else
309		fprintf (fp, " 0x%04x", (unsigned int)ks);
310	}
311	keymap += keysyms_per_keycode;
312	fprintf (fp, "\n");
313    }
314
315    XFree ((char *) origkeymap);
316    return;
317}
318
319void
320PrintPointerMap(FILE *fp)
321{
322    unsigned char pmap[256];		/* there are 8 bits of buttons */
323    int count, i;
324
325    count = XGetPointerMapping (dpy, pmap, 256);
326
327    fprintf (fp, "There are %d pointer buttons defined.\n\n", count);
328    fprintf (fp, "    Physical        Button\n");
329    fprintf (fp, "     Button          Code\n");
330/*               "      ###            ###\n"               */
331    for (i = 0; i < count; i++) {
332	fprintf (fp, "      %3u            %3u\n",
333		 i+1, (unsigned int) pmap[i]);
334    }
335    fprintf (fp, "\n");
336    return;
337}
338
339
340/*
341 * SetPointerMap - set the pointer map
342 */
343
344int
345SetPointerMap(unsigned char *map, int n)
346{
347    unsigned char defmap[MAXBUTTONCODES];
348    int j;
349    int retries, timeout;
350
351    if (n == 0) {				/* reset to default */
352	n = XGetPointerMapping (dpy, defmap, MAXBUTTONCODES);
353	for (j = 0; j < n; j++) defmap[j] = (unsigned char) (j + 1);
354	map = defmap;
355    }
356
357    for (retries = 5, timeout = 2; retries > 0; retries--, timeout *= 2) {
358	int result;
359
360	switch (result = XSetPointerMapping (dpy, map, n)) {
361	  case MappingSuccess:
362	    return 0;
363	  case MappingBusy:
364	    mapping_busy_pointer (timeout);
365	    continue;
366	  case MappingFailed:
367	    fprintf (stderr, "%s:  bad pointer mapping\n", ProgramName);
368	    return -1;
369	  default:
370	    fprintf (stderr, "%s:  bad return %d from XSetPointerMapping\n",
371		     ProgramName, result);
372	    return -1;
373	}
374    }
375    fprintf (stderr, "%s:  unable to set pointer mapping\n", ProgramName);
376    return -1;
377}
378