+ if (mode & ERR_HELP)
+ fprintf(error, "%s: Try option '--help' for more information.\n",
+ program_name);
+
+ if (mode & ERR_EXIT)
+ fprintf(error, "%s%s: aborting\n", program_name, process_name);
+
+ if (error != stderr)
+ fclose(error);
+
+ if (mode & ERR_EXIT)
+ {
+ if (mode & ERR_FROM_SERVER)
+ exit(1); /* child process: normal exit */
+ else
+ CloseAllAndExit(1); /* main process: clean up stuff */
+ }
+}
+
+void *checked_malloc(unsigned long size)
+{
+ void *ptr;
+
+ ptr = malloc(size);
+
+ if (ptr == NULL)
+ Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
+
+ return ptr;
+}
+
+void *checked_calloc(unsigned long size)
+{
+ void *ptr;
+
+ ptr = calloc(1, size);
+
+ if (ptr == NULL)
+ Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
+
+ return ptr;
+}
+
+short getFile16BitInteger(FILE *file, int byte_order)
+{
+ if (byte_order == BYTE_ORDER_BIG_ENDIAN)
+ return ((fgetc(file) << 8) |
+ (fgetc(file) << 0));
+ else /* BYTE_ORDER_LITTLE_ENDIAN */
+ return ((fgetc(file) << 0) |
+ (fgetc(file) << 8));
+}
+
+void putFile16BitInteger(FILE *file, short value, int byte_order)
+{
+ if (byte_order == BYTE_ORDER_BIG_ENDIAN)
+ {
+ fputc((value >> 8) & 0xff, file);
+ fputc((value >> 0) & 0xff, file);
+ }
+ else /* BYTE_ORDER_LITTLE_ENDIAN */
+ {
+ fputc((value >> 0) & 0xff, file);
+ fputc((value >> 8) & 0xff, file);
+ }
+}
+
+int getFile32BitInteger(FILE *file, int byte_order)
+{
+ if (byte_order == BYTE_ORDER_BIG_ENDIAN)
+ return ((fgetc(file) << 24) |
+ (fgetc(file) << 16) |
+ (fgetc(file) << 8) |
+ (fgetc(file) << 0));
+ else /* BYTE_ORDER_LITTLE_ENDIAN */
+ return ((fgetc(file) << 0) |
+ (fgetc(file) << 8) |
+ (fgetc(file) << 16) |
+ (fgetc(file) << 24));
+}
+
+void putFile32BitInteger(FILE *file, int value, int byte_order)
+{
+ if (byte_order == BYTE_ORDER_BIG_ENDIAN)
+ {
+ fputc((value >> 24) & 0xff, file);
+ fputc((value >> 16) & 0xff, file);
+ fputc((value >> 8) & 0xff, file);
+ fputc((value >> 0) & 0xff, file);
+ }
+ else /* BYTE_ORDER_LITTLE_ENDIAN */
+ {
+ fputc((value >> 0) & 0xff, file);
+ fputc((value >> 8) & 0xff, file);
+ fputc((value >> 16) & 0xff, file);
+ fputc((value >> 24) & 0xff, file);
+ }
+}
+
+void getFileChunk(FILE *file, char *chunk_buffer, int *chunk_length,
+ int byte_order)
+{
+ const int chunk_identifier_length = 4;
+
+ /* read chunk identifier */
+ fgets(chunk_buffer, chunk_identifier_length + 1, file);
+
+ /* read chunk length */
+ *chunk_length = getFile32BitInteger(file, byte_order);
+}
+
+void putFileChunk(FILE *file, char *chunk_name, int chunk_length,
+ int byte_order)
+{
+ /* write chunk identifier */
+ fputs(chunk_name, file);
+
+ /* write chunk length */
+ putFile32BitInteger(file, chunk_length, byte_order);
+}
+
+#define TRANSLATE_KEY_TO_KEYNAME 0
+#define TRANSLATE_KEY_TO_X11KEYNAME 1
+#define TRANSLATE_X11KEYNAME_TO_KEY 2
+
+void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
+{
+ static struct
+ {
+ Key key;
+ char *x11name;
+ char *name;
+ } translate_key[] =
+ {
+ /* normal cursor keys */
+ { KEY_Left, "XK_Left", "cursor left" },
+ { KEY_Right, "XK_Right", "cursor right" },
+ { KEY_Up, "XK_Up", "cursor up" },
+ { KEY_Down, "XK_Down", "cursor down" },
+
+ /* keypad cursor keys */
+#ifdef KEY_KP_Left
+ { KEY_KP_Left, "XK_KP_Left", "keypad left" },
+ { KEY_KP_Right, "XK_KP_Right", "keypad right" },
+ { KEY_KP_Up, "XK_KP_Up", "keypad up" },
+ { KEY_KP_Down, "XK_KP_Down", "keypad down" },
+#endif
+
+ /* other keypad keys */
+#ifdef KEY_KP_Enter
+ { KEY_KP_Enter, "XK_KP_Enter", "keypad enter" },
+ { KEY_KP_Add, "XK_KP_Add", "keypad +" },
+ { KEY_KP_Subtract, "XK_KP_Subtract", "keypad -" },
+ { KEY_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
+ { KEY_KP_Divide, "XK_KP_Divide", "keypad /" },
+ { KEY_KP_Separator, "XK_KP_Separator", "keypad ," },
+#endif
+
+ /* modifier keys */
+ { KEY_Shift_L, "XK_Shift_L", "left shift" },
+ { KEY_Shift_R, "XK_Shift_R", "right shift" },
+ { KEY_Control_L, "XK_Control_L", "left control" },
+ { KEY_Control_R, "XK_Control_R", "right control" },
+ { KEY_Meta_L, "XK_Meta_L", "left meta" },
+ { KEY_Meta_R, "XK_Meta_R", "right meta" },
+ { KEY_Alt_L, "XK_Alt_L", "left alt" },
+ { KEY_Alt_R, "XK_Alt_R", "right alt" },
+ { KEY_Super_L, "XK_Super_L", "left super" }, /* Win-L */
+ { KEY_Super_R, "XK_Super_R", "right super" }, /* Win-R */
+ { KEY_Mode_switch, "XK_Mode_switch", "mode switch" }, /* Alt-R */
+ { KEY_Multi_key, "XK_Multi_key", "multi key" }, /* Ctrl-R */
+
+ /* some special keys */
+ { KEY_BackSpace, "XK_BackSpace", "backspace" },
+ { KEY_Delete, "XK_Delete", "delete" },
+ { KEY_Insert, "XK_Insert", "insert" },
+ { KEY_Tab, "XK_Tab", "tab" },
+ { KEY_Home, "XK_Home", "home" },
+ { KEY_End, "XK_End", "end" },
+ { KEY_Page_Up, "XK_Page_Up", "page up" },
+ { KEY_Page_Down, "XK_Page_Down", "page down" },
+ { KEY_Menu, "XK_Menu", "menu" }, /* Win-Menu */
+
+ /* ASCII 0x20 to 0x40 keys (except numbers) */
+ { KEY_space, "XK_space", "space" },
+ { KEY_exclam, "XK_exclam", "!" },
+ { KEY_quotedbl, "XK_quotedbl", "\"" },
+ { KEY_numbersign, "XK_numbersign", "#" },
+ { KEY_dollar, "XK_dollar", "$" },
+ { KEY_percent, "XK_percent", "%" },
+ { KEY_ampersand, "XK_ampersand", "&" },
+ { KEY_apostrophe, "XK_apostrophe", "'" },
+ { KEY_parenleft, "XK_parenleft", "(" },
+ { KEY_parenright, "XK_parenright", ")" },
+ { KEY_asterisk, "XK_asterisk", "*" },
+ { KEY_plus, "XK_plus", "+" },
+ { KEY_comma, "XK_comma", "," },
+ { KEY_minus, "XK_minus", "-" },
+ { KEY_period, "XK_period", "." },
+ { KEY_slash, "XK_slash", "/" },
+ { KEY_colon, "XK_colon", ":" },
+ { KEY_semicolon, "XK_semicolon", ";" },
+ { KEY_less, "XK_less", "<" },
+ { KEY_equal, "XK_equal", "=" },
+ { KEY_greater, "XK_greater", ">" },
+ { KEY_question, "XK_question", "?" },
+ { KEY_at, "XK_at", "@" },
+
+ /* more ASCII keys */
+ { KEY_bracketleft, "XK_bracketleft", "[" },
+ { KEY_backslash, "XK_backslash", "backslash" },
+ { KEY_bracketright, "XK_bracketright", "]" },
+ { KEY_asciicircum, "XK_asciicircum", "circumflex" },
+ { KEY_underscore, "XK_underscore", "_" },
+ { KEY_grave, "XK_grave", "grave" },
+ { KEY_quoteleft, "XK_quoteleft", "quote left" },
+ { KEY_braceleft, "XK_braceleft", "brace left" },
+ { KEY_bar, "XK_bar", "bar" },
+ { KEY_braceright, "XK_braceright", "brace right" },
+ { KEY_asciitilde, "XK_asciitilde", "ascii tilde" },
+
+ /* special (non-ASCII) keys */
+ { KEY_Adiaeresis, "XK_Adiaeresis", "Ä" },
+ { KEY_Odiaeresis, "XK_Odiaeresis", "Ö" },
+ { KEY_Udiaeresis, "XK_Udiaeresis", "Ü" },
+ { KEY_adiaeresis, "XK_adiaeresis", "ä" },
+ { KEY_odiaeresis, "XK_odiaeresis", "ö" },
+ { KEY_udiaeresis, "XK_udiaeresis", "ü" },
+ { KEY_ssharp, "XK_ssharp", "sharp s" },
+
+ /* end-of-array identifier */
+ { 0, NULL, NULL }
+ };
+
+ int i;
+
+ if (mode == TRANSLATE_KEY_TO_KEYNAME)
+ {
+ static char name_buffer[30];
+ Key key = *keysym;
+
+ if (key >= KEY_A && key <= KEY_Z)
+ sprintf(name_buffer, "%c", 'A' + (char)(key - KEY_A));
+ else if (key >= KEY_a && key <= KEY_z)
+ sprintf(name_buffer, "%c", 'a' + (char)(key - KEY_a));
+ else if (key >= KEY_0 && key <= KEY_9)
+ sprintf(name_buffer, "%c", '0' + (char)(key - KEY_0));
+ else if (key >= KEY_KP_0 && key <= KEY_KP_9)
+ sprintf(name_buffer, "keypad %c", '0' + (char)(key - KEY_KP_0));
+ else if (key >= KEY_F1 && key <= KEY_F24)
+ sprintf(name_buffer, "function F%d", (int)(key - KEY_F1 + 1));
+ else if (key == KEY_UNDEFINED)
+ strcpy(name_buffer, "(undefined)");
+ else