Source tree in arch made more compact
[AROS.git] / arch / all-hosted / hidd / x11 / makexkeytable.c
blobd594dc0eb4a4ac8c6796a59cb5538fef680c4f7d
1 /* $XConsortium: xev.c,v 1.15 94/04/17 20:45:20 keith Exp $ */
2 /*
4 Copyright (c) 1988 X Consortium
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name of the X Consortium shall
26 not be used in advertising or otherwise to promote the sale, use or
27 other dealings in this Software without prior written authorization
28 from the X Consortium.
33 * Author: Jim Fulton, MIT X Consortium
36 /***************************************************************************/
38 #include <stdio.h>
39 #include <X11/Xos.h>
40 #include <X11/Xlib.h>
41 #include <X11/Xutil.h>
42 #include <X11/Xproto.h>
43 #include <X11/keysym.h>
44 #include <ctype.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
49 #define OUTER_WINDOW_MIN_WIDTH 100
50 #define OUTER_WINDOW_MIN_HEIGHT 100
51 #define OUTER_WINDOW_DEF_WIDTH 800
52 #define OUTER_WINDOW_DEF_HEIGHT 100
53 #define OUTER_WINDOW_DEF_X 100
54 #define OUTER_WINDOW_DEF_Y 100
56 /***************************************************************************/
58 static void
59 set_sizehints (XSizeHints *hintp, int min_width, int min_height,
60 int defwidth, int defheight, int defx, int defy, char *geom);
62 typedef unsigned long Pixel;
64 static char *ProgramName;
65 static Display *dpy;
66 static int screen;
67 static FILE *fh;
68 static Window w;
69 static GC gc;
71 static unsigned char table[256];
73 /***************************************************************************/
75 static struct _asktable
77 char *title;
78 int rawkeystart;
79 int numkeys;
80 KeySym nokey_ks;
82 asktable [] =
84 {"1st row: Press ESCAPE Key", 0x45, 1, XK_VoidSymbol},
85 {"1st row: Press F1 .. F10", 0x50, 10, XK_VoidSymbol},
86 {"1st row: Press F11 (or ESCAPE if key doesn't exist)", 0x4B, 1, XK_Escape},
87 {"1st row: Press F12 (or ESCAPE if key doesn't exist)", 0x6F, 1, XK_Escape},
89 {"2nd row: Press the key left of 1, or ESCAPE if there's no key left of 1", 0x00, 1, XK_Escape},
90 {"2nd row: Press 1 .. 0", 0x1, 10, XK_VoidSymbol},
91 {"2nd row: Press the three normal (!!) keys right of 0. Press ESCAPE instead of third key if you only have two keys right of 0", 0X0B, 3, XK_Escape},
92 {"2nd row: Press BACKSPACE", 0x41, 1, XK_VoidSymbol},
94 {"3rd row: Press TAB", 0x42, 1, XK_VoidSymbol},
95 {"3rd row: Press the twelve keys right of TAB (= upto and excl. RETURN)", 0x10, 12, XK_VoidSymbol},
96 {"3rd row: Press RETURN", 0x44, 1, XK_VoidSymbol},
98 {"4th row: Press the (left) CONTROL key which might also be in 6th row", 0x63, 1, XK_VoidSymbol},
99 {"4th row: Press CAPS LOCK", 0x62, 1, XK_VoidSymbol},
100 {"4th row: Press the twelve keys right of CAPS LOCK. Press ESCAPE for last if you only have eleven keys there", 0x20, 12, XK_Escape},
102 {"5th row: Press LEFT SHIFT", 0x60, 1, XK_VoidSymbol},
103 {"5th row: If in this row you have eleven normal keys, press the key right of LEFT SHIFT, otherwise ESCAPE", 0x30, 1, XK_Escape},
104 {"5th row: Press the next ten keys in this row (= upto and excl. RIGHT SHIFT)", 0x31, 10, XK_VoidSymbol},
105 {"5th row: Press RIGHT SHIFT", 0x61, 1, XK_VoidSymbol},
107 {"6th row: Press left ALT", 0x64, 1, XK_VoidSymbol},
108 {"6th row: Press left AMIGA", 0x66, 1, XK_VoidSymbol},
109 {"6th row: Press SPACE", 0x40, 1, XK_VoidSymbol},
110 {"6th row: Press right AMIGA", 0x67, 1, XK_VoidSymbol},
111 {"6th row: Press right ALT", 0x65, 1, XK_VoidSymbol},
112 {"6th row: Press right CONTROL (or ESCAPE if key doesn't exist)", 0x63, 1, XK_VoidSymbol},
114 {"Above cursor keys: Press INSERT (or ESCAPE if key doesn't exist)", 0x47, 1, XK_Escape},
115 {"Above cursor keys: Press HOME (or ESCAPE if key doesn't exist)", 0x70, 1, XK_Escape},
116 {"Above cursor keys: Press PAGE UP (or ESCAPE if key doesn't exist)", 0x48, 1, XK_Escape},
117 {"Above cursor keys: Press DELETE", 0x46, 1, XK_VoidSymbol},
118 {"Above cursor keys: Press END (or ESCAPE if key doesn't exist)", 0x71, 1, XK_Escape},
119 {"Above cursor keys: Press PAGE DOWN (or ESCAPE if key doesn't exist)", 0x49, 1, XK_Escape},
120 {"Above cursor keys: Press HELP (or key you want to use for HELP if it doesn't exist)", 0x5F, 1, XK_VoidSymbol},
122 {"Cursor keys: Press CURSOR UP", 0x4C, 1, XK_VoidSymbol},
123 {"Cursor keys: Press CURSOR LEFT", 0x4F, 1, XK_VoidSymbol},
124 {"Cursor keys: Press CURSOR DOWN", 0x4D, 1, XK_VoidSymbol},
125 {"Cursor keys: Press CURSOR RIGHT", 0x4E, 1, XK_VoidSymbol},
127 {"Numeric pad 1st row: Press the four keys above (!!!!) 7 8 9", 0x5A, 4, XK_VoidSymbol},
128 {"Numeric pad 2nd row: Press 7 8 9", 0x3D, 3, XK_VoidSymbol},
129 {"Numeric pad 2nd row: If key right of 9 is a normal size key press it, otherwise ESCAPE", 0x4A, 1, XK_Escape},
130 {"Numeric pad 3rd row: Press 4 5 6", 0x2D, 3, XK_VoidSymbol},
131 {"Numeric pad 3rd row: Press +", 0x5e, 1, XK_VoidSymbol},
132 {"Numeric pad 4th row: Press 1 2 3", 0x1D, 3, XK_VoidSymbol},
133 {"Numeric pad 5th row: Press 0", 0x0F, 1, XK_VoidSymbol},
134 {"Numeric pad 5th row: Press COMMA", 0x3C, 1, XK_VoidSymbol},
135 {"Numeric pad 5th row: Press ENTER", 0x43, 1, XK_VoidSymbol},
136 {0, 0, 0, XK_VoidSymbol}
139 /***************************************************************************/
141 void doaskuser(char *title,int rawkeystart, int numkeys, KeySym k);
143 /***************************************************************************/
145 void usage ()
147 static char *msg[] = {
148 " -display displayname X server to contact",
149 " -geometry geom size and location of window",
150 " -o <filename> Table output file name",
152 NULL};
153 char **cpp;
155 fprintf (stderr, "usage: %s [-options ...]\n", ProgramName);
156 fprintf (stderr, "where options include:\n");
158 for (cpp = msg; *cpp; cpp++) {
159 fprintf (stderr, "%s\n", *cpp);
162 exit (1);
165 /***************************************************************************/
167 static int MySysErrorHandler (Display * display)
169 perror ("X11-Error");
170 fflush (stderr);
172 XAutoRepeatOn(display);
173 XCloseDisplay(display);
174 exit(0);
177 /***************************************************************************/
179 int main (int argc, char **argv)
181 char *displayname = NULL;
182 char *geom = NULL;
183 char *tablefilename = NULL;
184 int i;
185 XSizeHints hints;
186 int borderwidth = 2;
187 XSetWindowAttributes attr;
188 XWindowAttributes wattr;
189 unsigned long mask = 0L;
190 int done;
191 char *name = "X11 Keycode to Rawkey table generation tool";
192 Bool reverse = False;
193 unsigned long back, fore;
194 struct _asktable *ask;
196 w = 0;
197 ProgramName = argv[0];
198 for (i = 1; i < argc; i++) {
199 char *arg = argv[i];
201 if (arg[0] == '-') {
202 switch (arg[1]) {
203 case 'd': /* -display host:dpy */
204 if (++i >= argc) usage ();
205 displayname = argv[i];
206 continue;
207 case 'g': /* -geometry geom */
208 if (++i >= argc) usage ();
209 geom = argv[i];
210 continue;
212 case 'o': /* table file name */
213 if (++i >= argc) usage ();
214 tablefilename = argv[i];
215 continue;
217 default:
218 usage ();
219 } /* end switch on - */
220 } else
221 usage ();
222 } /* end for over argc */
224 if (!tablefilename)
226 fprintf (stderr, "%s: output filename missing\n",
227 ProgramName);
228 exit(1);
232 memset(table, 0xFF, 256);
234 dpy = XOpenDisplay (displayname);
235 if (!dpy) {
236 fprintf (stderr, "%s: unable to open display '%s'\n",
237 ProgramName, XDisplayName (displayname));
238 exit (1);
241 XSetIOErrorHandler (MySysErrorHandler);
243 XAutoRepeatOff(dpy);
245 screen = DefaultScreen (dpy);
247 /* select for all events */
248 attr.event_mask = KeyPressMask;
250 set_sizehints (&hints, OUTER_WINDOW_MIN_WIDTH, OUTER_WINDOW_MIN_HEIGHT,
251 OUTER_WINDOW_DEF_WIDTH, OUTER_WINDOW_DEF_HEIGHT,
252 OUTER_WINDOW_DEF_X, OUTER_WINDOW_DEF_Y, geom);
254 if (reverse) {
255 back = BlackPixel(dpy,screen);
256 fore = WhitePixel(dpy,screen);
257 } else {
258 back = WhitePixel(dpy,screen);
259 fore = BlackPixel(dpy,screen);
262 attr.background_pixel = back;
263 attr.border_pixel = fore;
264 mask |= (CWBackPixel | CWBorderPixel | CWEventMask);
266 w = XCreateWindow (dpy, RootWindow (dpy, screen), hints.x, hints.y,
267 hints.width, hints.height, borderwidth, 0,
268 InputOutput, (Visual *)CopyFromParent,
269 mask, &attr);
271 XSetStandardProperties (dpy, w, name, NULL, (Pixmap) 0,
272 argv, argc, &hints);
274 gc = XCreateGC (dpy, w, 0, 0);
276 XMapWindow (dpy, w);
278 XStoreName(dpy, w, name);
279 XFlush(dpy);
281 sleep(3);
283 for(ask = asktable; ask->title; ask++)
285 doaskuser(ask->title, ask->rawkeystart, ask->numkeys, ask->nokey_ks);
288 fh = fopen(tablefilename,"wb");
289 if (!fh)
291 fprintf (stderr, "%s: unable to open \"%s\" in write mode\n",
292 ProgramName, tablefilename);
293 exit(1);
296 i = fwrite(table, 1, 256, fh);
297 if (i != 256)
299 fprintf (stderr, "%s: writing to \"%s\" failed\n",
300 ProgramName, tablefilename);
301 exit(1);
303 fclose(fh);fh = 0;
306 XAutoRepeatOn(dpy);
308 XCloseDisplay (dpy);
309 exit (0);
312 /***************************************************************************/
314 void doaskuser(char *title,int rawkeystart, int numkeys, KeySym k)
316 int i;
318 XStoreName(dpy, w, title);
320 for(i = 0;i < numkeys;)
322 XEvent event;
323 char buffer[10];
324 KeySym ks;
326 XNextEvent (dpy, &event);
328 switch(((XKeyEvent *)&event)->type)
330 case KeyPress:
331 ks = XK_VoidSymbol;
332 XLookupString ((XKeyEvent *)&event, buffer, 10, &ks, NULL);
334 i++;
335 XSetForeground(dpy, gc, WhitePixel(dpy, screen));
336 XFillRectangle(dpy, w, gc, 0, 0, 1000, 1000);
337 XSetForeground(dpy, gc, BlackPixel(dpy,screen));
339 if ((k == XK_VoidSymbol) || (ks != k))
341 char s[6];
343 unsigned char kc = (unsigned char)((XKeyEvent *)&event)->keycode;
344 table[kc] = rawkeystart++;
346 sprintf(s, "%03ld",(int)((XKeyEvent *)&event)->keycode);
347 XDrawString(dpy, w, gc, 50, 20, s, strlen(s));
349 } else {
350 XDrawString(dpy, w, gc, 50, 20, "NOP", 3);
352 XFlush(dpy);
353 break;
355 } /* switch(event.type) */
357 } /* for(i = 0;i < numkeys;) */
360 /***************************************************************************/
362 static void
363 set_sizehints (XSizeHints *hintp, int min_width, int min_height,
364 int defwidth, int defheight, int defx, int defy, char *geom)
366 int geom_result;
368 /* set the size hints, algorithm from xlib xbiff */
370 hintp->width = hintp->min_width = min_width;
371 hintp->height = hintp->min_height = min_height;
372 hintp->flags = PMinSize;
373 hintp->x = hintp->y = 0;
374 geom_result = NoValue;
375 if (geom != NULL) {
376 geom_result = XParseGeometry (geom, &hintp->x, &hintp->y,
377 (unsigned int *)&hintp->width,
378 (unsigned int *)&hintp->height);
379 if ((geom_result & WidthValue) && (geom_result & HeightValue)) {
380 #define max(a,b) ((a) > (b) ? (a) : (b))
381 hintp->width = max (hintp->width, hintp->min_width);
382 hintp->height = max (hintp->height, hintp->min_height);
383 hintp->flags |= USSize;
385 if ((geom_result & XValue) && (geom_result & YValue)) {
386 hintp->flags += USPosition;
389 if (!(hintp->flags & USSize)) {
390 hintp->width = defwidth;
391 hintp->height = defheight;
392 hintp->flags |= PSize;
395 if (!(hintp->flags & USPosition)) {
396 hintp->x = defx;
397 hintp->y = defy;
398 hintp->flags |= PPosition;
401 if (geom_result & XNegative) {
402 hintp->x = DisplayWidth (dpy, DefaultScreen (dpy)) + hintp->x -
403 hintp->width;
405 if (geom_result & YNegative) {
406 hintp->y = DisplayHeight (dpy, DefaultScreen (dpy)) + hintp->y -
407 hintp->height;
409 return;