+ ptr = calloc(1, size);
+
+ if (ptr == NULL)
+ Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
+
+ return ptr;
+}
+
+#define TRANSLATE_KEYSYM_TO_KEYNAME 0
+#define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
+#define TRANSLATE_X11KEYNAME_TO_KEYSYM 2
+
+void translate_keyname(KeySym *keysym, char **x11name, char **name, int mode)
+{
+ static struct
+ {
+ KeySym keysym;
+ char *x11name;
+ char *name;
+ } translate_key[] =
+ {
+ /* normal cursor keys */
+ { XK_Left, "XK_Left", "cursor left" },
+ { XK_Right, "XK_Right", "cursor right" },
+ { XK_Up, "XK_Up", "cursor up" },
+ { XK_Down, "XK_Down", "cursor down" },
+
+ /* keypad cursor keys */
+#ifdef XK_KP_Left
+ { XK_KP_Left, "XK_KP_Left", "keypad left" },
+ { XK_KP_Right, "XK_KP_Right", "keypad right" },
+ { XK_KP_Up, "XK_KP_Up", "keypad up" },
+ { XK_KP_Down, "XK_KP_Down", "keypad down" },
+#endif
+
+ /* other keypad keys */
+#ifdef XK_KP_Enter
+ { XK_KP_Enter, "XK_KP_Enter", "keypad enter" },
+ { XK_KP_Add, "XK_KP_Add", "keypad +" },
+ { XK_KP_Subtract, "XK_KP_Subtract", "keypad -" },
+ { XK_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
+ { XK_KP_Divide, "XK_KP_Divide", "keypad /" },
+ { XK_KP_Separator, "XK_KP_Separator", "keypad ," },
+#endif
+
+ /* modifier keys */
+ { XK_Shift_L, "XK_Shift_L", "left shift" },
+ { XK_Shift_R, "XK_Shift_R", "right shift" },
+ { XK_Control_L, "XK_Control_L", "left control" },
+ { XK_Control_R, "XK_Control_R", "right control" },
+ { XK_Meta_L, "XK_Meta_L", "left meta" },
+ { XK_Meta_R, "XK_Meta_R", "right meta" },
+ { XK_Alt_L, "XK_Alt_L", "left alt" },
+ { XK_Alt_R, "XK_Alt_R", "right alt" },
+ { XK_Mode_switch, "XK_Mode_switch", "mode switch" },
+ { XK_Multi_key, "XK_Multi_key", "multi key" },
+
+ /* some special keys */
+ { XK_BackSpace, "XK_BackSpace", "backspace" },
+ { XK_Delete, "XK_Delete", "delete" },
+ { XK_Insert, "XK_Insert", "insert" },
+ { XK_Tab, "XK_Tab", "tab" },
+ { XK_Home, "XK_Home", "home" },
+ { XK_End, "XK_End", "end" },
+ { XK_Page_Up, "XK_Page_Up", "page up" },
+ { XK_Page_Down, "XK_Page_Down", "page down" },
+ { XK_space, "XK_space", "space" },
+
+ /* even more special keys */
+ { XK_adiaeresis, "XK_adiaeresis", "ä" },
+ { XK_odiaeresis, "XK_odiaeresis", "ö" },
+ { XK_udiaeresis, "XK_udiaeresis", "ü" },
+ { XK_apostrophe, "XK_apostrophe", "'" },
+ { XK_plus, "XK_plus", "+" },
+ { XK_minus, "XK_minus", "-" },
+ { XK_equal, "XK_equal", "equal" },
+ { XK_comma, "XK_comma", "," },
+ { XK_period, "XK_period", "." },
+ { XK_colon, "XK_colon", ";" },
+ { XK_slash, "XK_slash", "/" },
+ { XK_numbersign, "XK_numbersign", "#" },
+ { XK_backslash, "XK_backslash", "backslash" },
+ { XK_braceleft, "XK_braceleft", "brace left" },
+ { XK_braceright, "XK_braceright", "brace right" },
+ { XK_less, "XK_less", "less" },
+ { XK_greater, "XK_greater", "greater" },
+ { XK_asciicircum, "XK_asciicircum", "circumflex" },
+ { XK_ssharp, "XK_ssharp", "sharp s" },
+
+ /* end-of-array identifier */
+ { 0, NULL, NULL }
+ };
+
+ int i;
+
+ if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
+ {
+ static char name_buffer[30];
+ KeySym key = *keysym;
+
+ if (key >= XK_A && key <= XK_Z)
+ sprintf(name_buffer, "%c", 'A' + (char)(key - XK_A));
+ else if (key >= XK_a && key <= XK_z)
+ sprintf(name_buffer, "%c", 'a' + (char)(key - XK_a));
+ else if (key >= XK_0 && key <= XK_9)
+ sprintf(name_buffer, "%c", '0' + (char)(key - XK_0));
+ else if (key >= XK_KP_0 && key <= XK_KP_9)
+ sprintf(name_buffer, "keypad %c", '0' + (char)(key - XK_KP_0));
+ else if (key >= XK_F1 && key <= XK_F24)
+ sprintf(name_buffer, "function F%d", (int)(key - XK_F1 + 1));
+ else if (key == KEY_UNDEFINDED)
+ strcpy(name_buffer, "(undefined)");
+ else