1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1994-2001 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
16 #include <sys/types.h>
27 #if !defined(PLATFORM_WIN32)
29 #include <sys/param.h>
37 #if defined(PLATFORM_MSDOS)
38 volatile unsigned long counter = 0;
40 void increment_counter()
45 END_OF_FUNCTION(increment_counter);
49 /* maximal allowed length of a command line option */
50 #define MAX_OPTION_LEN 256
53 static unsigned long mainCounter(int mode)
55 static unsigned long base_ms = 0;
56 unsigned long current_ms;
57 unsigned long counter_ms;
59 current_ms = SDL_GetTicks();
61 /* reset base time in case of counter initializing or wrap-around */
62 if (mode == INIT_COUNTER || current_ms < base_ms)
65 counter_ms = current_ms - base_ms;
67 return counter_ms; /* return milliseconds since last init */
70 #else /* !TARGET_SDL */
72 #if defined(PLATFORM_UNIX)
73 static unsigned long mainCounter(int mode)
75 static struct timeval base_time = { 0, 0 };
76 struct timeval current_time;
77 unsigned long counter_ms;
79 gettimeofday(¤t_time, NULL);
81 /* reset base time in case of counter initializing or wrap-around */
82 if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec)
83 base_time = current_time;
85 counter_ms = (current_time.tv_sec - base_time.tv_sec) * 1000
86 + (current_time.tv_usec - base_time.tv_usec) / 1000;
88 return counter_ms; /* return milliseconds since last init */
90 #endif /* PLATFORM_UNIX */
91 #endif /* !TARGET_SDL */
93 void InitCounter() /* set counter back to zero */
95 #if !defined(PLATFORM_MSDOS)
96 mainCounter(INIT_COUNTER);
98 LOCK_VARIABLE(counter);
99 LOCK_FUNCTION(increment_counter);
100 install_int_ex(increment_counter, BPS_TO_TIMER(100));
104 unsigned long Counter() /* get milliseconds since last call of InitCounter() */
106 #if !defined(PLATFORM_MSDOS)
107 return mainCounter(READ_COUNTER);
109 return (counter * 10);
113 static void sleep_milliseconds(unsigned long milliseconds_delay)
115 boolean do_busy_waiting = (milliseconds_delay < 5 ? TRUE : FALSE);
117 #if defined(PLATFORM_MSDOS)
118 /* don't use select() to perform waiting operations under DOS/Windows
119 environment; always use a busy loop for waiting instead */
120 do_busy_waiting = TRUE;
125 /* we want to wait only a few ms -- if we assume that we have a
126 kernel timer resolution of 10 ms, we would wait far to long;
127 therefore it's better to do a short interval of busy waiting
128 to get our sleeping time more accurate */
130 unsigned long base_counter = Counter(), actual_counter = Counter();
132 while (actual_counter < base_counter + milliseconds_delay &&
133 actual_counter >= base_counter)
134 actual_counter = Counter();
138 #if defined(TARGET_SDL)
139 SDL_Delay(milliseconds_delay);
141 struct timeval delay;
143 delay.tv_sec = milliseconds_delay / 1000;
144 delay.tv_usec = 1000 * (milliseconds_delay % 1000);
146 if (select(0, NULL, NULL, NULL, &delay) != 0)
147 Error(ERR_WARN, "sleep_milliseconds(): select() failed");
152 void Delay(unsigned long delay) /* Sleep specified number of milliseconds */
154 sleep_milliseconds(delay);
157 boolean FrameReached(unsigned long *frame_counter_var,
158 unsigned long frame_delay)
160 unsigned long actual_frame_counter = FrameCounter;
162 if (actual_frame_counter < *frame_counter_var+frame_delay &&
163 actual_frame_counter >= *frame_counter_var)
166 *frame_counter_var = actual_frame_counter;
170 boolean DelayReached(unsigned long *counter_var,
173 unsigned long actual_counter = Counter();
175 if (actual_counter < *counter_var + delay &&
176 actual_counter >= *counter_var)
179 *counter_var = actual_counter;
183 void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
185 unsigned long actual_counter;
189 actual_counter = Counter();
191 if (actual_counter < *counter_var + delay &&
192 actual_counter >= *counter_var)
193 sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
198 *counter_var = actual_counter;
201 /* int2str() returns a number converted to a string;
202 the used memory is static, but will be overwritten by later calls,
203 so if you want to save the result, copy it to a private string buffer;
204 there can be 10 local calls of int2str() without buffering the result --
205 the 11th call will then destroy the result from the first call and so on.
208 char *int2str(int number, int size)
210 static char shift_array[10][40];
211 static int shift_counter = 0;
212 char *s = shift_array[shift_counter];
214 shift_counter = (shift_counter + 1) % 10;
221 sprintf(s, " %09d", number);
222 return &s[strlen(s) - size];
226 sprintf(s, "%d", number);
231 unsigned int SimpleRND(unsigned int max)
233 #if defined(TARGET_SDL)
234 static unsigned long root = 654321;
235 unsigned long current_ms;
237 current_ms = SDL_GetTicks();
238 root = root * 4253261 + current_ms;
241 static unsigned long root = 654321;
242 struct timeval current_time;
244 gettimeofday(¤t_time, NULL);
245 root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
251 static unsigned int last_RND_value = 0;
253 unsigned int last_RND()
255 return last_RND_value;
259 unsigned int RND(unsigned int max)
262 return (last_RND_value = random_linux_libc() % max);
264 return (random_linux_libc() % max);
268 unsigned int InitRND(long seed)
270 #if defined(TARGET_SDL)
271 unsigned long current_ms;
273 if (seed == NEW_RANDOMIZE)
275 current_ms = SDL_GetTicks();
276 srandom_linux_libc((unsigned int) current_ms);
277 return (unsigned int) current_ms;
281 srandom_linux_libc((unsigned int) seed);
282 return (unsigned int) seed;
285 struct timeval current_time;
287 if (seed == NEW_RANDOMIZE)
289 gettimeofday(¤t_time, NULL);
290 srandom_linux_libc((unsigned int) current_time.tv_usec);
291 return (unsigned int) current_time.tv_usec;
295 srandom_linux_libc((unsigned int) seed);
296 return (unsigned int) seed;
303 #if defined(PLATFORM_WIN32)
304 return ANONYMOUS_NAME;
308 if ((pwd = getpwuid(getuid())) == NULL)
309 return ANONYMOUS_NAME;
317 #if defined(PLATFORM_UNIX)
320 if ((pwd = getpwuid(getuid())) == NULL || strlen(pwd->pw_gecos) == 0)
321 return ANONYMOUS_NAME;
324 static char real_name[1024];
325 char *from_ptr = pwd->pw_gecos, *to_ptr = real_name;
327 if (strchr(pwd->pw_gecos, 'ß') == NULL)
328 return pwd->pw_gecos;
330 /* the user's real name contains a 'ß' character (german sharp s),
331 which has no equivalent in upper case letters (which our fonts use) */
332 while (*from_ptr != '\0' && (long)(to_ptr - real_name) < 1024 - 2)
334 if (*from_ptr != 'ß')
335 *to_ptr++ = *from_ptr++;
347 #else /* !PLATFORM_UNIX */
348 return ANONYMOUS_NAME;
354 #if defined(PLATFORM_UNIX)
355 static char *home_dir = NULL;
359 if (!(home_dir = getenv("HOME")))
363 if ((pwd = getpwuid(getuid())))
364 home_dir = pwd->pw_dir;
376 char *getPath2(char *path1, char *path2)
378 char *complete_path = checked_malloc(strlen(path1) + 1 +
381 sprintf(complete_path, "%s/%s", path1, path2);
382 return complete_path;
385 char *getPath3(char *path1, char *path2, char *path3)
387 char *complete_path = checked_malloc(strlen(path1) + 1 +
391 sprintf(complete_path, "%s/%s/%s", path1, path2, path3);
392 return complete_path;
395 char *getStringCopy(char *s)
402 s_copy = checked_malloc(strlen(s) + 1);
408 char *getStringToLower(char *s)
410 char *s_copy = checked_malloc(strlen(s) + 1);
411 char *s_ptr = s_copy;
414 *s_ptr++ = tolower(*s++);
420 void GetOptions(char *argv[])
422 char **options_left = &argv[1];
424 /* initialize global program options */
425 options.display_name = NULL;
426 options.server_host = NULL;
427 options.server_port = 0;
428 options.ro_base_directory = RO_BASE_PATH;
429 options.rw_base_directory = RW_BASE_PATH;
430 options.level_directory = RO_BASE_PATH "/" LEVELS_DIRECTORY;
431 options.graphics_directory = RO_BASE_PATH "/" GRAPHICS_DIRECTORY;
432 options.sounds_directory = RO_BASE_PATH "/" SOUNDS_DIRECTORY;
433 options.music_directory = RO_BASE_PATH "/" MUSIC_DIRECTORY;
434 options.serveronly = FALSE;
435 options.network = FALSE;
436 options.verbose = FALSE;
437 options.debug = FALSE;
439 while (*options_left)
441 char option_str[MAX_OPTION_LEN];
442 char *option = options_left[0];
443 char *next_option = options_left[1];
444 char *option_arg = NULL;
445 int option_len = strlen(option);
447 if (option_len >= MAX_OPTION_LEN)
448 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
450 strcpy(option_str, option); /* copy argument into buffer */
453 if (strcmp(option, "--") == 0) /* stop scanning arguments */
456 if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */
459 option_arg = strchr(option, '=');
460 if (option_arg == NULL) /* no '=' in option */
461 option_arg = next_option;
464 *option_arg++ = '\0'; /* cut argument from option */
465 if (*option_arg == '\0') /* no argument after '=' */
466 Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
469 option_len = strlen(option);
471 if (strcmp(option, "-") == 0)
472 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
473 else if (strncmp(option, "-help", option_len) == 0)
475 printf("Usage: %s [options] [server.name [port]]\n"
477 " -d, --display <host>[:<scr>] X server display\n"
478 " -b, --basepath <directory> alternative base directory\n"
479 " -l, --level <directory> alternative level directory\n"
480 " -g, --graphics <directory> alternative graphics directory\n"
481 " -s, --sounds <directory> alternative graphics directory\n"
482 " -m, --music <directory> alternative graphics directory\n"
483 " -n, --network network multiplayer game\n"
484 " --serveronly only start network server\n"
485 " -v, --verbose verbose mode\n"
486 " --debug display debugging information\n",
487 program.command_basename);
490 else if (strncmp(option, "-display", option_len) == 0)
492 if (option_arg == NULL)
493 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
495 options.display_name = option_arg;
496 if (option_arg == next_option)
499 else if (strncmp(option, "-basepath", option_len) == 0)
501 if (option_arg == NULL)
502 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
504 /* this should be extended to separate options for ro and rw data */
505 options.ro_base_directory = option_arg;
506 options.rw_base_directory = option_arg;
507 if (option_arg == next_option)
510 /* adjust path for level directory accordingly */
511 options.level_directory =
512 getPath2(options.ro_base_directory, LEVELS_DIRECTORY);
514 else if (strncmp(option, "-levels", option_len) == 0)
516 if (option_arg == NULL)
517 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
519 options.level_directory = option_arg;
520 if (option_arg == next_option)
523 else if (strncmp(option, "-graphics", option_len) == 0)
525 if (option_arg == NULL)
526 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
528 options.graphics_directory = option_arg;
529 if (option_arg == next_option)
532 else if (strncmp(option, "-sounds", option_len) == 0)
534 if (option_arg == NULL)
535 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
537 options.sounds_directory = option_arg;
538 if (option_arg == next_option)
541 else if (strncmp(option, "-music", option_len) == 0)
543 if (option_arg == NULL)
544 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
546 options.music_directory = option_arg;
547 if (option_arg == next_option)
550 else if (strncmp(option, "-network", option_len) == 0)
552 options.network = TRUE;
554 else if (strncmp(option, "-serveronly", option_len) == 0)
556 options.serveronly = TRUE;
558 else if (strncmp(option, "-verbose", option_len) == 0)
560 options.verbose = TRUE;
562 else if (strncmp(option, "-debug", option_len) == 0)
564 options.debug = TRUE;
566 else if (*option == '-')
568 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
570 else if (options.server_host == NULL)
572 options.server_host = *options_left;
574 else if (options.server_port == 0)
576 options.server_port = atoi(*options_left);
577 if (options.server_port < 1024)
578 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
581 Error(ERR_EXIT_HELP, "too many arguments");
587 /* used by SetError() and GetError() to store internal error messages */
588 static char internal_error[1024]; /* this is bad */
590 void SetError(char *format, ...)
594 va_start(ap, format);
595 vsprintf(internal_error, format, ap);
601 return internal_error;
604 void Error(int mode, char *format, ...)
606 char *process_name = "";
607 FILE *error = stderr;
608 char *newline = "\n";
610 /* display warnings only when running in verbose mode */
611 if (mode & ERR_WARN && !options.verbose)
614 #if !defined(PLATFORM_UNIX)
617 if ((error = openErrorFile()) == NULL)
619 printf("Cannot write to error output file!%s", newline);
620 program.exit_function(1);
624 if (mode & ERR_SOUND_SERVER)
625 process_name = " sound server";
626 else if (mode & ERR_NETWORK_SERVER)
627 process_name = " network server";
628 else if (mode & ERR_NETWORK_CLIENT)
629 process_name = " network client **";
635 fprintf(error, "%s%s: ", program.command_basename, process_name);
638 fprintf(error, "warning: ");
640 va_start(ap, format);
641 vfprintf(error, format, ap);
644 fprintf(error, "%s", newline);
648 fprintf(error, "%s: Try option '--help' for more information.%s",
649 program.command_basename, newline);
652 fprintf(error, "%s%s: aborting%s",
653 program.command_basename, process_name, newline);
660 if (mode & ERR_FROM_SERVER)
661 exit(1); /* child process: normal exit */
663 program.exit_function(1); /* main process: clean up stuff */
667 void *checked_malloc(unsigned long size)
674 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
679 void *checked_calloc(unsigned long size)
683 ptr = calloc(1, size);
686 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
691 void *checked_realloc(void *ptr, unsigned long size)
693 ptr = realloc(ptr, size);
696 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
701 inline void swap_numbers(int *i1, int *i2)
709 inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2)
721 short getFile16BitInteger(FILE *file, int byte_order)
723 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
724 return ((fgetc(file) << 8) |
726 else /* BYTE_ORDER_LITTLE_ENDIAN */
727 return ((fgetc(file) << 0) |
731 void putFile16BitInteger(FILE *file, short value, int byte_order)
733 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
735 fputc((value >> 8) & 0xff, file);
736 fputc((value >> 0) & 0xff, file);
738 else /* BYTE_ORDER_LITTLE_ENDIAN */
740 fputc((value >> 0) & 0xff, file);
741 fputc((value >> 8) & 0xff, file);
745 int getFile32BitInteger(FILE *file, int byte_order)
747 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
748 return ((fgetc(file) << 24) |
749 (fgetc(file) << 16) |
752 else /* BYTE_ORDER_LITTLE_ENDIAN */
753 return ((fgetc(file) << 0) |
755 (fgetc(file) << 16) |
756 (fgetc(file) << 24));
759 void putFile32BitInteger(FILE *file, int value, int byte_order)
761 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
763 fputc((value >> 24) & 0xff, file);
764 fputc((value >> 16) & 0xff, file);
765 fputc((value >> 8) & 0xff, file);
766 fputc((value >> 0) & 0xff, file);
768 else /* BYTE_ORDER_LITTLE_ENDIAN */
770 fputc((value >> 0) & 0xff, file);
771 fputc((value >> 8) & 0xff, file);
772 fputc((value >> 16) & 0xff, file);
773 fputc((value >> 24) & 0xff, file);
777 boolean getFileChunk(FILE *file, char *chunk_name, int *chunk_size,
780 const int chunk_name_length = 4;
782 /* read chunk name */
783 fgets(chunk_name, chunk_name_length + 1, file);
785 if (chunk_size != NULL)
787 /* read chunk size */
788 *chunk_size = getFile32BitInteger(file, byte_order);
791 return (feof(file) || ferror(file) ? FALSE : TRUE);
794 void putFileChunk(FILE *file, char *chunk_name, int chunk_size,
797 /* write chunk name */
798 fputs(chunk_name, file);
802 /* write chunk size */
803 putFile32BitInteger(file, chunk_size, byte_order);
807 void ReadUnusedBytesFromFile(FILE *file, unsigned long bytes)
813 void WriteUnusedBytesToFile(FILE *file, unsigned long bytes)
819 #define TRANSLATE_KEYSYM_TO_KEYNAME 0
820 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
821 #define TRANSLATE_X11KEYNAME_TO_KEYSYM 2
823 void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
832 /* normal cursor keys */
833 { KSYM_Left, "XK_Left", "cursor left" },
834 { KSYM_Right, "XK_Right", "cursor right" },
835 { KSYM_Up, "XK_Up", "cursor up" },
836 { KSYM_Down, "XK_Down", "cursor down" },
838 /* keypad cursor keys */
840 { KSYM_KP_Left, "XK_KP_Left", "keypad left" },
841 { KSYM_KP_Right, "XK_KP_Right", "keypad right" },
842 { KSYM_KP_Up, "XK_KP_Up", "keypad up" },
843 { KSYM_KP_Down, "XK_KP_Down", "keypad down" },
846 /* other keypad keys */
848 { KSYM_KP_Enter, "XK_KP_Enter", "keypad enter" },
849 { KSYM_KP_Add, "XK_KP_Add", "keypad +" },
850 { KSYM_KP_Subtract, "XK_KP_Subtract", "keypad -" },
851 { KSYM_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
852 { KSYM_KP_Divide, "XK_KP_Divide", "keypad /" },
853 { KSYM_KP_Separator,"XK_KP_Separator", "keypad ," },
857 { KSYM_Shift_L, "XK_Shift_L", "left shift" },
858 { KSYM_Shift_R, "XK_Shift_R", "right shift" },
859 { KSYM_Control_L, "XK_Control_L", "left control" },
860 { KSYM_Control_R, "XK_Control_R", "right control" },
861 { KSYM_Meta_L, "XK_Meta_L", "left meta" },
862 { KSYM_Meta_R, "XK_Meta_R", "right meta" },
863 { KSYM_Alt_L, "XK_Alt_L", "left alt" },
864 { KSYM_Alt_R, "XK_Alt_R", "right alt" },
865 { KSYM_Super_L, "XK_Super_L", "left super" }, /* Win-L */
866 { KSYM_Super_R, "XK_Super_R", "right super" }, /* Win-R */
867 { KSYM_Mode_switch, "XK_Mode_switch", "mode switch" }, /* Alt-R */
868 { KSYM_Multi_key, "XK_Multi_key", "multi key" }, /* Ctrl-R */
870 /* some special keys */
871 { KSYM_BackSpace, "XK_BackSpace", "backspace" },
872 { KSYM_Delete, "XK_Delete", "delete" },
873 { KSYM_Insert, "XK_Insert", "insert" },
874 { KSYM_Tab, "XK_Tab", "tab" },
875 { KSYM_Home, "XK_Home", "home" },
876 { KSYM_End, "XK_End", "end" },
877 { KSYM_Page_Up, "XK_Page_Up", "page up" },
878 { KSYM_Page_Down, "XK_Page_Down", "page down" },
879 { KSYM_Menu, "XK_Menu", "menu" }, /* Win-Menu */
881 /* ASCII 0x20 to 0x40 keys (except numbers) */
882 { KSYM_space, "XK_space", "space" },
883 { KSYM_exclam, "XK_exclam", "!" },
884 { KSYM_quotedbl, "XK_quotedbl", "\"" },
885 { KSYM_numbersign, "XK_numbersign", "#" },
886 { KSYM_dollar, "XK_dollar", "$" },
887 { KSYM_percent, "XK_percent", "%" },
888 { KSYM_ampersand, "XK_ampersand", "&" },
889 { KSYM_apostrophe, "XK_apostrophe", "'" },
890 { KSYM_parenleft, "XK_parenleft", "(" },
891 { KSYM_parenright, "XK_parenright", ")" },
892 { KSYM_asterisk, "XK_asterisk", "*" },
893 { KSYM_plus, "XK_plus", "+" },
894 { KSYM_comma, "XK_comma", "," },
895 { KSYM_minus, "XK_minus", "-" },
896 { KSYM_period, "XK_period", "." },
897 { KSYM_slash, "XK_slash", "/" },
898 { KSYM_colon, "XK_colon", ":" },
899 { KSYM_semicolon, "XK_semicolon", ";" },
900 { KSYM_less, "XK_less", "<" },
901 { KSYM_equal, "XK_equal", "=" },
902 { KSYM_greater, "XK_greater", ">" },
903 { KSYM_question, "XK_question", "?" },
904 { KSYM_at, "XK_at", "@" },
906 /* more ASCII keys */
907 { KSYM_bracketleft, "XK_bracketleft", "[" },
908 { KSYM_backslash, "XK_backslash", "backslash" },
909 { KSYM_bracketright,"XK_bracketright", "]" },
910 { KSYM_asciicircum, "XK_asciicircum", "circumflex" },
911 { KSYM_underscore, "XK_underscore", "_" },
912 { KSYM_grave, "XK_grave", "grave" },
913 { KSYM_quoteleft, "XK_quoteleft", "quote left" },
914 { KSYM_braceleft, "XK_braceleft", "brace left" },
915 { KSYM_bar, "XK_bar", "bar" },
916 { KSYM_braceright, "XK_braceright", "brace right" },
917 { KSYM_asciitilde, "XK_asciitilde", "ascii tilde" },
919 /* special (non-ASCII) keys */
920 { KSYM_Adiaeresis, "XK_Adiaeresis", "Ä" },
921 { KSYM_Odiaeresis, "XK_Odiaeresis", "Ö" },
922 { KSYM_Udiaeresis, "XK_Udiaeresis", "Ü" },
923 { KSYM_adiaeresis, "XK_adiaeresis", "ä" },
924 { KSYM_odiaeresis, "XK_odiaeresis", "ö" },
925 { KSYM_udiaeresis, "XK_udiaeresis", "ü" },
926 { KSYM_ssharp, "XK_ssharp", "sharp s" },
928 /* end-of-array identifier */
934 if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
936 static char name_buffer[30];
939 if (key >= KSYM_A && key <= KSYM_Z)
940 sprintf(name_buffer, "%c", 'A' + (char)(key - KSYM_A));
941 else if (key >= KSYM_a && key <= KSYM_z)
942 sprintf(name_buffer, "%c", 'a' + (char)(key - KSYM_a));
943 else if (key >= KSYM_0 && key <= KSYM_9)
944 sprintf(name_buffer, "%c", '0' + (char)(key - KSYM_0));
945 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
946 sprintf(name_buffer, "keypad %c", '0' + (char)(key - KSYM_KP_0));
947 else if (key >= KSYM_F1 && key <= KSYM_F24)
948 sprintf(name_buffer, "function F%d", (int)(key - KSYM_F1 + 1));
949 else if (key == KSYM_UNDEFINED)
950 strcpy(name_buffer, "(undefined)");
957 if (key == translate_key[i].key)
959 strcpy(name_buffer, translate_key[i].name);
963 while (translate_key[++i].name);
965 if (!translate_key[i].name)
966 strcpy(name_buffer, "(unknown)");
971 else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME)
973 static char name_buffer[30];
976 if (key >= KSYM_A && key <= KSYM_Z)
977 sprintf(name_buffer, "XK_%c", 'A' + (char)(key - KSYM_A));
978 else if (key >= KSYM_a && key <= KSYM_z)
979 sprintf(name_buffer, "XK_%c", 'a' + (char)(key - KSYM_a));
980 else if (key >= KSYM_0 && key <= KSYM_9)
981 sprintf(name_buffer, "XK_%c", '0' + (char)(key - KSYM_0));
982 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
983 sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - KSYM_KP_0));
984 else if (key >= KSYM_F1 && key <= KSYM_F24)
985 sprintf(name_buffer, "XK_F%d", (int)(key - KSYM_F1 + 1));
986 else if (key == KSYM_UNDEFINED)
987 strcpy(name_buffer, "[undefined]");
994 if (key == translate_key[i].key)
996 strcpy(name_buffer, translate_key[i].x11name);
1000 while (translate_key[++i].x11name);
1002 if (!translate_key[i].x11name)
1003 sprintf(name_buffer, "0x%04lx", (unsigned long)key);
1006 *x11name = name_buffer;
1008 else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
1010 Key key = KSYM_UNDEFINED;
1011 char *name_ptr = *x11name;
1013 if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4)
1015 char c = name_ptr[3];
1017 if (c >= 'A' && c <= 'Z')
1018 key = KSYM_A + (Key)(c - 'A');
1019 else if (c >= 'a' && c <= 'z')
1020 key = KSYM_a + (Key)(c - 'a');
1021 else if (c >= '0' && c <= '9')
1022 key = KSYM_0 + (Key)(c - '0');
1024 else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7)
1026 char c = name_ptr[6];
1028 if (c >= '0' && c <= '9')
1029 key = KSYM_0 + (Key)(c - '0');
1031 else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
1033 char c1 = name_ptr[4];
1034 char c2 = name_ptr[5];
1037 if ((c1 >= '0' && c1 <= '9') &&
1038 ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
1039 d = atoi(&name_ptr[4]);
1041 if (d >=1 && d <= 24)
1042 key = KSYM_F1 + (Key)(d - 1);
1044 else if (strncmp(name_ptr, "XK_", 3) == 0)
1050 if (strcmp(name_ptr, translate_key[i].x11name) == 0)
1052 key = translate_key[i].key;
1056 while (translate_key[++i].x11name);
1058 else if (strncmp(name_ptr, "0x", 2) == 0)
1060 unsigned long value = 0;
1066 char c = *name_ptr++;
1069 if (c >= '0' && c <= '9')
1071 else if (c >= 'a' && c <= 'f')
1072 d = (int)(c - 'a' + 10);
1073 else if (c >= 'A' && c <= 'F')
1074 d = (int)(c - 'A' + 10);
1082 value = value * 16 + d;
1093 char *getKeyNameFromKey(Key key)
1097 translate_keyname(&key, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME);
1101 char *getX11KeyNameFromKey(Key key)
1105 translate_keyname(&key, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME);
1109 Key getKeyFromX11KeyName(char *x11name)
1113 translate_keyname(&key, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM);
1117 char getCharFromKey(Key key)
1119 char *keyname = getKeyNameFromKey(key);
1122 if (strlen(keyname) == 1)
1123 letter = keyname[0];
1124 else if (strcmp(keyname, "space") == 0)
1126 else if (strcmp(keyname, "circumflex") == 0)
1133 /* ========================================================================= */
1134 /* functions only needed for non-Unix (non-command-line) systems */
1135 /* ========================================================================= */
1137 #if !defined(PLATFORM_UNIX)
1139 #define ERROR_FILENAME "error.out"
1141 void initErrorFile()
1145 InitUserDataDirectory();
1147 filename = getPath2(getUserDataDir(), ERROR_FILENAME);
1152 FILE *openErrorFile()
1157 filename = getPath2(getUserDataDir(), ERROR_FILENAME);
1158 error_file = fopen(filename, MODE_APPEND);
1164 void dumpErrorFile()
1169 filename = getPath2(getUserDataDir(), ERROR_FILENAME);
1170 error_file = fopen(filename, MODE_READ);
1173 if (error_file != NULL)
1175 while (!feof(error_file))
1176 fputc(fgetc(error_file), stderr);
1184 /* ========================================================================= */
1185 /* the following is only for debugging purpose and normally not used */
1186 /* ========================================================================= */
1188 #define DEBUG_NUM_TIMESTAMPS 3
1190 void debug_print_timestamp(int counter_nr, char *message)
1192 static long counter[DEBUG_NUM_TIMESTAMPS][2];
1194 if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
1195 Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
1197 counter[counter_nr][0] = Counter();
1200 printf("%s %.2f seconds\n", message,
1201 (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
1203 counter[counter_nr][1] = Counter();