1 /* $XConsortium: xev.c,v 1.15 94/04/17 20:45:20 keith Exp $ */
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 /***************************************************************************/
41 #include <X11/Xutil.h>
42 #include <X11/Xproto.h>
43 #include <X11/keysym.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 /***************************************************************************/
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
;
71 static unsigned char table
[256];
73 /***************************************************************************/
75 static struct _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
},
137 {"Press PRINTSCREEN (or ESCAPE if key doesn't exist)", 0x6c, 1, XK_Escape
},
138 {"Press SCROLL LOCK (or ESCAPE if key doesn't exist)", 0x6b, 1, XK_Escape
},
139 {"Press PAUSE (or ESCAPE if key doesn't exist)", 0x6e, 1, XK_Escape
},
141 {0, 0, 0, XK_VoidSymbol
}
144 /***************************************************************************/
146 void doaskuser(char *title
,int rawkeystart
, int numkeys
, KeySym k
);
148 /***************************************************************************/
152 static char *msg
[] = {
153 " -display displayname X server to contact",
154 " -geometry geom size and location of window",
155 " -o <filename> Table output file name",
160 fprintf (stderr
, "usage: %s [-options ...]\n", ProgramName
);
161 fprintf (stderr
, "where options include:\n");
163 for (cpp
= msg
; *cpp
; cpp
++) {
164 fprintf (stderr
, "%s\n", *cpp
);
170 /***************************************************************************/
172 static int MySysErrorHandler (Display
* display
)
174 perror ("X11-Error");
177 XAutoRepeatOn(display
);
178 XCloseDisplay(display
);
182 /***************************************************************************/
184 int main (int argc
, char **argv
)
186 char *displayname
= NULL
;
188 char *tablefilename
= NULL
;
192 XSetWindowAttributes attr
;
193 XWindowAttributes wattr
;
194 unsigned long mask
= 0L;
196 char *name
= "X11 Keycode to Rawkey table generation tool";
197 Bool reverse
= False
;
198 unsigned long back
, fore
;
199 struct _asktable
*ask
;
202 ProgramName
= argv
[0];
203 for (i
= 1; i
< argc
; i
++) {
208 case 'd': /* -display host:dpy */
209 if (++i
>= argc
) usage ();
210 displayname
= argv
[i
];
212 case 'g': /* -geometry geom */
213 if (++i
>= argc
) usage ();
217 case 'o': /* table file name */
218 if (++i
>= argc
) usage ();
219 tablefilename
= argv
[i
];
224 } /* end switch on - */
227 } /* end for over argc */
231 fprintf (stderr
, "%s: output filename missing\n",
237 memset(table
, 0xFF, 256);
239 dpy
= XOpenDisplay (displayname
);
241 fprintf (stderr
, "%s: unable to open display '%s'\n",
242 ProgramName
, XDisplayName (displayname
));
246 XSetIOErrorHandler (MySysErrorHandler
);
250 screen
= DefaultScreen (dpy
);
252 /* select for all events */
253 attr
.event_mask
= KeyPressMask
;
255 set_sizehints (&hints
, OUTER_WINDOW_MIN_WIDTH
, OUTER_WINDOW_MIN_HEIGHT
,
256 OUTER_WINDOW_DEF_WIDTH
, OUTER_WINDOW_DEF_HEIGHT
,
257 OUTER_WINDOW_DEF_X
, OUTER_WINDOW_DEF_Y
, geom
);
260 back
= BlackPixel(dpy
,screen
);
261 fore
= WhitePixel(dpy
,screen
);
263 back
= WhitePixel(dpy
,screen
);
264 fore
= BlackPixel(dpy
,screen
);
267 attr
.background_pixel
= back
;
268 attr
.border_pixel
= fore
;
269 mask
|= (CWBackPixel
| CWBorderPixel
| CWEventMask
);
271 w
= XCreateWindow (dpy
, RootWindow (dpy
, screen
), hints
.x
, hints
.y
,
272 hints
.width
, hints
.height
, borderwidth
, 0,
273 InputOutput
, (Visual
*)CopyFromParent
,
276 XSetStandardProperties (dpy
, w
, name
, NULL
, (Pixmap
) 0,
279 gc
= XCreateGC (dpy
, w
, 0, 0);
283 XStoreName(dpy
, w
, name
);
288 for(ask
= asktable
; ask
->title
; ask
++)
290 doaskuser(ask
->title
, ask
->rawkeystart
, ask
->numkeys
, ask
->nokey_ks
);
293 fh
= fopen(tablefilename
,"wb");
296 fprintf (stderr
, "%s: unable to open \"%s\" in write mode\n",
297 ProgramName
, tablefilename
);
301 i
= fwrite(table
, 1, 256, fh
);
304 fprintf (stderr
, "%s: writing to \"%s\" failed\n",
305 ProgramName
, tablefilename
);
317 /***************************************************************************/
319 void doaskuser(char *title
,int rawkeystart
, int numkeys
, KeySym k
)
323 XStoreName(dpy
, w
, title
);
325 for(i
= 0;i
< numkeys
;)
331 XNextEvent (dpy
, &event
);
333 switch(((XKeyEvent
*)&event
)->type
)
337 XLookupString ((XKeyEvent
*)&event
, buffer
, 10, &ks
, NULL
);
340 XSetForeground(dpy
, gc
, WhitePixel(dpy
, screen
));
341 XFillRectangle(dpy
, w
, gc
, 0, 0, 1000, 1000);
342 XSetForeground(dpy
, gc
, BlackPixel(dpy
,screen
));
344 if ((k
== XK_VoidSymbol
) || (ks
!= k
))
348 unsigned char kc
= (unsigned char)((XKeyEvent
*)&event
)->keycode
;
349 table
[kc
] = rawkeystart
++;
351 sprintf(s
, "%03ld",(long)((XKeyEvent
*)&event
)->keycode
);
352 XDrawString(dpy
, w
, gc
, 50, 20, s
, strlen(s
));
355 XDrawString(dpy
, w
, gc
, 50, 20, "NOP", 3);
360 } /* switch(event.type) */
362 } /* for(i = 0;i < numkeys;) */
365 /***************************************************************************/
368 set_sizehints (XSizeHints
*hintp
, int min_width
, int min_height
,
369 int defwidth
, int defheight
, int defx
, int defy
, char *geom
)
373 /* set the size hints, algorithm from xlib xbiff */
375 hintp
->width
= hintp
->min_width
= min_width
;
376 hintp
->height
= hintp
->min_height
= min_height
;
377 hintp
->flags
= PMinSize
;
378 hintp
->x
= hintp
->y
= 0;
379 geom_result
= NoValue
;
381 geom_result
= XParseGeometry (geom
, &hintp
->x
, &hintp
->y
,
382 (unsigned int *)&hintp
->width
,
383 (unsigned int *)&hintp
->height
);
384 if ((geom_result
& WidthValue
) && (geom_result
& HeightValue
)) {
385 #define max(a,b) ((a) > (b) ? (a) : (b))
386 hintp
->width
= max (hintp
->width
, hintp
->min_width
);
387 hintp
->height
= max (hintp
->height
, hintp
->min_height
);
388 hintp
->flags
|= USSize
;
390 if ((geom_result
& XValue
) && (geom_result
& YValue
)) {
391 hintp
->flags
+= USPosition
;
394 if (!(hintp
->flags
& USSize
)) {
395 hintp
->width
= defwidth
;
396 hintp
->height
= defheight
;
397 hintp
->flags
|= PSize
;
400 if (!(hintp->flags & USPosition)) {
403 hintp->flags |= PPosition;
406 if (geom_result
& XNegative
) {
407 hintp
->x
= DisplayWidth (dpy
, DefaultScreen (dpy
)) + hintp
->x
-
410 if (geom_result
& YNegative
) {
411 hintp
->y
= DisplayHeight (dpy
, DefaultScreen (dpy
)) + hintp
->y
-