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;
171 boolean DelayReached(unsigned long *counter_var,
174 unsigned long actual_counter = Counter();
176 if (actual_counter < *counter_var + delay &&
177 actual_counter >= *counter_var)
180 *counter_var = actual_counter;
185 void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
187 unsigned long actual_counter;
191 actual_counter = Counter();
193 if (actual_counter < *counter_var + delay &&
194 actual_counter >= *counter_var)
195 sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
200 *counter_var = actual_counter;
203 /* int2str() returns a number converted to a string;
204 the used memory is static, but will be overwritten by later calls,
205 so if you want to save the result, copy it to a private string buffer;
206 there can be 10 local calls of int2str() without buffering the result --
207 the 11th call will then destroy the result from the first call and so on.
210 char *int2str(int number, int size)
212 static char shift_array[10][40];
213 static int shift_counter = 0;
214 char *s = shift_array[shift_counter];
216 shift_counter = (shift_counter + 1) % 10;
223 sprintf(s, " %09d", number);
224 return &s[strlen(s) - size];
228 sprintf(s, "%d", number);
233 unsigned int SimpleRND(unsigned int max)
235 #if defined(TARGET_SDL)
236 static unsigned long root = 654321;
237 unsigned long current_ms;
239 current_ms = SDL_GetTicks();
240 root = root * 4253261 + current_ms;
243 static unsigned long root = 654321;
244 struct timeval current_time;
246 gettimeofday(¤t_time, NULL);
247 root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
253 static unsigned int last_RND_value = 0;
255 unsigned int last_RND()
257 return last_RND_value;
261 unsigned int RND(unsigned int max)
264 return (last_RND_value = random_linux_libc() % max);
266 return (random_linux_libc() % max);
270 unsigned int InitRND(long seed)
272 #if defined(TARGET_SDL)
273 unsigned long current_ms;
275 if (seed == NEW_RANDOMIZE)
277 current_ms = SDL_GetTicks();
278 srandom_linux_libc((unsigned int) current_ms);
279 return (unsigned int) current_ms;
283 srandom_linux_libc((unsigned int) seed);
284 return (unsigned int) seed;
287 struct timeval current_time;
289 if (seed == NEW_RANDOMIZE)
291 gettimeofday(¤t_time, NULL);
292 srandom_linux_libc((unsigned int) current_time.tv_usec);
293 return (unsigned int) current_time.tv_usec;
297 srandom_linux_libc((unsigned int) seed);
298 return (unsigned int) seed;
305 #if defined(PLATFORM_WIN32)
306 return ANONYMOUS_NAME;
310 if ((pwd = getpwuid(getuid())) == NULL)
311 return ANONYMOUS_NAME;
319 #if defined(PLATFORM_UNIX)
322 if ((pwd = getpwuid(getuid())) == NULL || strlen(pwd->pw_gecos) == 0)
323 return ANONYMOUS_NAME;
326 static char real_name[1024];
327 char *from_ptr = pwd->pw_gecos, *to_ptr = real_name;
329 if (strchr(pwd->pw_gecos, 'ß') == NULL)
330 return pwd->pw_gecos;
332 /* the user's real name contains a 'ß' character (german sharp s),
333 which has no equivalent in upper case letters (which our fonts use) */
334 while (*from_ptr != '\0' && (long)(to_ptr - real_name) < 1024 - 2)
336 if (*from_ptr != 'ß')
337 *to_ptr++ = *from_ptr++;
349 #else /* !PLATFORM_UNIX */
350 return ANONYMOUS_NAME;
356 #if defined(PLATFORM_UNIX)
357 static char *home_dir = NULL;
361 if (!(home_dir = getenv("HOME")))
365 if ((pwd = getpwuid(getuid())))
366 home_dir = pwd->pw_dir;
378 char *getPath2(char *path1, char *path2)
380 char *complete_path = checked_malloc(strlen(path1) + 1 +
383 sprintf(complete_path, "%s/%s", path1, path2);
384 return complete_path;
387 char *getPath3(char *path1, char *path2, char *path3)
389 char *complete_path = checked_malloc(strlen(path1) + 1 +
393 sprintf(complete_path, "%s/%s/%s", path1, path2, path3);
394 return complete_path;
397 char *getStringCopy(char *s)
404 s_copy = checked_malloc(strlen(s) + 1);
410 char *getStringToLower(char *s)
412 char *s_copy = checked_malloc(strlen(s) + 1);
413 char *s_ptr = s_copy;
416 *s_ptr++ = tolower(*s++);
422 void GetOptions(char *argv[])
424 char **options_left = &argv[1];
426 /* initialize global program options */
427 options.display_name = NULL;
428 options.server_host = NULL;
429 options.server_port = 0;
430 options.ro_base_directory = RO_BASE_PATH;
431 options.rw_base_directory = RW_BASE_PATH;
432 options.level_directory = RO_BASE_PATH "/" LEVELS_DIRECTORY;
433 options.graphics_directory = RO_BASE_PATH "/" GRAPHICS_DIRECTORY;
434 options.sounds_directory = RO_BASE_PATH "/" SOUNDS_DIRECTORY;
435 options.music_directory = RO_BASE_PATH "/" MUSIC_DIRECTORY;
436 options.serveronly = FALSE;
437 options.network = FALSE;
438 options.verbose = FALSE;
439 options.debug = FALSE;
441 while (*options_left)
443 char option_str[MAX_OPTION_LEN];
444 char *option = options_left[0];
445 char *next_option = options_left[1];
446 char *option_arg = NULL;
447 int option_len = strlen(option);
449 if (option_len >= MAX_OPTION_LEN)
450 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
452 strcpy(option_str, option); /* copy argument into buffer */
455 if (strcmp(option, "--") == 0) /* stop scanning arguments */
458 if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */
461 option_arg = strchr(option, '=');
462 if (option_arg == NULL) /* no '=' in option */
463 option_arg = next_option;
466 *option_arg++ = '\0'; /* cut argument from option */
467 if (*option_arg == '\0') /* no argument after '=' */
468 Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
471 option_len = strlen(option);
473 if (strcmp(option, "-") == 0)
474 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
475 else if (strncmp(option, "-help", option_len) == 0)
477 printf("Usage: %s [options] [server.name [port]]\n"
479 " -d, --display <host>[:<scr>] X server display\n"
480 " -b, --basepath <directory> alternative base directory\n"
481 " -l, --level <directory> alternative level directory\n"
482 " -g, --graphics <directory> alternative graphics directory\n"
483 " -s, --sounds <directory> alternative graphics directory\n"
484 " -m, --music <directory> alternative graphics directory\n"
485 " -n, --network network multiplayer game\n"
486 " --serveronly only start network server\n"
487 " -v, --verbose verbose mode\n"
488 " --debug display debugging information\n",
489 program.command_basename);
492 else if (strncmp(option, "-display", option_len) == 0)
494 if (option_arg == NULL)
495 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
497 options.display_name = option_arg;
498 if (option_arg == next_option)
501 else if (strncmp(option, "-basepath", option_len) == 0)
503 if (option_arg == NULL)
504 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
506 /* this should be extended to separate options for ro and rw data */
507 options.ro_base_directory = option_arg;
508 options.rw_base_directory = option_arg;
509 if (option_arg == next_option)
512 /* adjust path for level directory accordingly */
513 options.level_directory =
514 getPath2(options.ro_base_directory, LEVELS_DIRECTORY);
516 else if (strncmp(option, "-levels", option_len) == 0)
518 if (option_arg == NULL)
519 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
521 options.level_directory = option_arg;
522 if (option_arg == next_option)
525 else if (strncmp(option, "-graphics", option_len) == 0)
527 if (option_arg == NULL)
528 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
530 options.graphics_directory = option_arg;
531 if (option_arg == next_option)
534 else if (strncmp(option, "-sounds", option_len) == 0)
536 if (option_arg == NULL)
537 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
539 options.sounds_directory = option_arg;
540 if (option_arg == next_option)
543 else if (strncmp(option, "-music", option_len) == 0)
545 if (option_arg == NULL)
546 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
548 options.music_directory = option_arg;
549 if (option_arg == next_option)
552 else if (strncmp(option, "-network", option_len) == 0)
554 options.network = TRUE;
556 else if (strncmp(option, "-serveronly", option_len) == 0)
558 options.serveronly = TRUE;
560 else if (strncmp(option, "-verbose", option_len) == 0)
562 options.verbose = TRUE;
564 else if (strncmp(option, "-debug", option_len) == 0)
566 options.debug = TRUE;
568 else if (*option == '-')
570 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
572 else if (options.server_host == NULL)
574 options.server_host = *options_left;
576 else if (options.server_port == 0)
578 options.server_port = atoi(*options_left);
579 if (options.server_port < 1024)
580 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
583 Error(ERR_EXIT_HELP, "too many arguments");
589 /* used by SetError() and GetError() to store internal error messages */
590 static char internal_error[1024]; /* this is bad */
592 void SetError(char *format, ...)
596 va_start(ap, format);
597 vsprintf(internal_error, format, ap);
603 return internal_error;
606 void Error(int mode, char *format, ...)
608 char *process_name = "";
609 FILE *error = stderr;
610 char *newline = "\n";
612 /* display warnings only when running in verbose mode */
613 if (mode & ERR_WARN && !options.verbose)
616 #if !defined(PLATFORM_UNIX)
619 if ((error = openErrorFile()) == NULL)
621 printf("Cannot write to error output file!%s", newline);
622 program.exit_function(1);
626 if (mode & ERR_SOUND_SERVER)
627 process_name = " sound server";
628 else if (mode & ERR_NETWORK_SERVER)
629 process_name = " network server";
630 else if (mode & ERR_NETWORK_CLIENT)
631 process_name = " network client **";
637 fprintf(error, "%s%s: ", program.command_basename, process_name);
640 fprintf(error, "warning: ");
642 va_start(ap, format);
643 vfprintf(error, format, ap);
646 fprintf(error, "%s", newline);
650 fprintf(error, "%s: Try option '--help' for more information.%s",
651 program.command_basename, newline);
654 fprintf(error, "%s%s: aborting%s",
655 program.command_basename, process_name, newline);
662 if (mode & ERR_FROM_SERVER)
663 exit(1); /* child process: normal exit */
665 program.exit_function(1); /* main process: clean up stuff */
669 void *checked_malloc(unsigned long size)
676 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
681 void *checked_calloc(unsigned long size)
685 ptr = calloc(1, size);
688 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
693 void *checked_realloc(void *ptr, unsigned long size)
695 ptr = realloc(ptr, size);
698 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
703 inline void swap_numbers(int *i1, int *i2)
711 inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2)
723 short getFile16BitInteger(FILE *file, int byte_order)
725 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
726 return ((fgetc(file) << 8) |
728 else /* BYTE_ORDER_LITTLE_ENDIAN */
729 return ((fgetc(file) << 0) |
733 void putFile16BitInteger(FILE *file, short value, int byte_order)
735 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
737 fputc((value >> 8) & 0xff, file);
738 fputc((value >> 0) & 0xff, file);
740 else /* BYTE_ORDER_LITTLE_ENDIAN */
742 fputc((value >> 0) & 0xff, file);
743 fputc((value >> 8) & 0xff, file);
747 int getFile32BitInteger(FILE *file, int byte_order)
749 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
750 return ((fgetc(file) << 24) |
751 (fgetc(file) << 16) |
754 else /* BYTE_ORDER_LITTLE_ENDIAN */
755 return ((fgetc(file) << 0) |
757 (fgetc(file) << 16) |
758 (fgetc(file) << 24));
761 void putFile32BitInteger(FILE *file, int value, int byte_order)
763 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
765 fputc((value >> 24) & 0xff, file);
766 fputc((value >> 16) & 0xff, file);
767 fputc((value >> 8) & 0xff, file);
768 fputc((value >> 0) & 0xff, file);
770 else /* BYTE_ORDER_LITTLE_ENDIAN */
772 fputc((value >> 0) & 0xff, file);
773 fputc((value >> 8) & 0xff, file);
774 fputc((value >> 16) & 0xff, file);
775 fputc((value >> 24) & 0xff, file);
779 boolean getFileChunk(FILE *file, char *chunk_name, int *chunk_size,
782 const int chunk_name_length = 4;
784 /* read chunk name */
785 fgets(chunk_name, chunk_name_length + 1, file);
787 if (chunk_size != NULL)
789 /* read chunk size */
790 *chunk_size = getFile32BitInteger(file, byte_order);
793 return (feof(file) || ferror(file) ? FALSE : TRUE);
796 void putFileChunk(FILE *file, char *chunk_name, int chunk_size,
799 /* write chunk name */
800 fputs(chunk_name, file);
804 /* write chunk size */
805 putFile32BitInteger(file, chunk_size, byte_order);
809 void ReadUnusedBytesFromFile(FILE *file, unsigned long bytes)
811 while (bytes-- && !feof(file))
815 void WriteUnusedBytesToFile(FILE *file, unsigned long bytes)
821 #define TRANSLATE_KEYSYM_TO_KEYNAME 0
822 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
823 #define TRANSLATE_KEYNAME_TO_KEYSYM 2
824 #define TRANSLATE_X11KEYNAME_TO_KEYSYM 3
826 void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
835 /* normal cursor keys */
836 { KSYM_Left, "XK_Left", "cursor left" },
837 { KSYM_Right, "XK_Right", "cursor right" },
838 { KSYM_Up, "XK_Up", "cursor up" },
839 { KSYM_Down, "XK_Down", "cursor down" },
841 /* keypad cursor keys */
843 { KSYM_KP_Left, "XK_KP_Left", "keypad left" },
844 { KSYM_KP_Right, "XK_KP_Right", "keypad right" },
845 { KSYM_KP_Up, "XK_KP_Up", "keypad up" },
846 { KSYM_KP_Down, "XK_KP_Down", "keypad down" },
849 /* other keypad keys */
851 { KSYM_KP_Enter, "XK_KP_Enter", "keypad enter" },
852 { KSYM_KP_Add, "XK_KP_Add", "keypad +" },
853 { KSYM_KP_Subtract, "XK_KP_Subtract", "keypad -" },
854 { KSYM_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
855 { KSYM_KP_Divide, "XK_KP_Divide", "keypad /" },
856 { KSYM_KP_Separator,"XK_KP_Separator", "keypad ," },
860 { KSYM_Shift_L, "XK_Shift_L", "left shift" },
861 { KSYM_Shift_R, "XK_Shift_R", "right shift" },
862 { KSYM_Control_L, "XK_Control_L", "left control" },
863 { KSYM_Control_R, "XK_Control_R", "right control" },
864 { KSYM_Meta_L, "XK_Meta_L", "left meta" },
865 { KSYM_Meta_R, "XK_Meta_R", "right meta" },
866 { KSYM_Alt_L, "XK_Alt_L", "left alt" },
867 { KSYM_Alt_R, "XK_Alt_R", "right alt" },
868 { KSYM_Super_L, "XK_Super_L", "left super" }, /* Win-L */
869 { KSYM_Super_R, "XK_Super_R", "right super" }, /* Win-R */
870 { KSYM_Mode_switch, "XK_Mode_switch", "mode switch" }, /* Alt-R */
871 { KSYM_Multi_key, "XK_Multi_key", "multi key" }, /* Ctrl-R */
873 /* some special keys */
874 { KSYM_BackSpace, "XK_BackSpace", "backspace" },
875 { KSYM_Delete, "XK_Delete", "delete" },
876 { KSYM_Insert, "XK_Insert", "insert" },
877 { KSYM_Tab, "XK_Tab", "tab" },
878 { KSYM_Home, "XK_Home", "home" },
879 { KSYM_End, "XK_End", "end" },
880 { KSYM_Page_Up, "XK_Page_Up", "page up" },
881 { KSYM_Page_Down, "XK_Page_Down", "page down" },
882 { KSYM_Menu, "XK_Menu", "menu" }, /* Win-Menu */
884 /* ASCII 0x20 to 0x40 keys (except numbers) */
885 { KSYM_space, "XK_space", "space" },
886 { KSYM_exclam, "XK_exclam", "!" },
887 { KSYM_quotedbl, "XK_quotedbl", "\"" },
888 { KSYM_numbersign, "XK_numbersign", "#" },
889 { KSYM_dollar, "XK_dollar", "$" },
890 { KSYM_percent, "XK_percent", "%" },
891 { KSYM_ampersand, "XK_ampersand", "&" },
892 { KSYM_apostrophe, "XK_apostrophe", "'" },
893 { KSYM_parenleft, "XK_parenleft", "(" },
894 { KSYM_parenright, "XK_parenright", ")" },
895 { KSYM_asterisk, "XK_asterisk", "*" },
896 { KSYM_plus, "XK_plus", "+" },
897 { KSYM_comma, "XK_comma", "," },
898 { KSYM_minus, "XK_minus", "-" },
899 { KSYM_period, "XK_period", "." },
900 { KSYM_slash, "XK_slash", "/" },
901 { KSYM_colon, "XK_colon", ":" },
902 { KSYM_semicolon, "XK_semicolon", ";" },
903 { KSYM_less, "XK_less", "<" },
904 { KSYM_equal, "XK_equal", "=" },
905 { KSYM_greater, "XK_greater", ">" },
906 { KSYM_question, "XK_question", "?" },
907 { KSYM_at, "XK_at", "@" },
909 /* more ASCII keys */
910 { KSYM_bracketleft, "XK_bracketleft", "[" },
911 { KSYM_backslash, "XK_backslash", "backslash" },
912 { KSYM_bracketright,"XK_bracketright", "]" },
913 { KSYM_asciicircum, "XK_asciicircum", "circumflex" },
914 { KSYM_underscore, "XK_underscore", "_" },
915 { KSYM_grave, "XK_grave", "grave" },
916 { KSYM_quoteleft, "XK_quoteleft", "quote left" },
917 { KSYM_braceleft, "XK_braceleft", "brace left" },
918 { KSYM_bar, "XK_bar", "bar" },
919 { KSYM_braceright, "XK_braceright", "brace right" },
920 { KSYM_asciitilde, "XK_asciitilde", "ascii tilde" },
922 /* special (non-ASCII) keys */
923 { KSYM_Adiaeresis, "XK_Adiaeresis", "Ä" },
924 { KSYM_Odiaeresis, "XK_Odiaeresis", "Ö" },
925 { KSYM_Udiaeresis, "XK_Udiaeresis", "Ü" },
926 { KSYM_adiaeresis, "XK_adiaeresis", "ä" },
927 { KSYM_odiaeresis, "XK_odiaeresis", "ö" },
928 { KSYM_udiaeresis, "XK_udiaeresis", "ü" },
929 { KSYM_ssharp, "XK_ssharp", "sharp s" },
931 /* end-of-array identifier */
937 if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
939 static char name_buffer[30];
942 if (key >= KSYM_A && key <= KSYM_Z)
943 sprintf(name_buffer, "%c", 'A' + (char)(key - KSYM_A));
944 else if (key >= KSYM_a && key <= KSYM_z)
945 sprintf(name_buffer, "%c", 'a' + (char)(key - KSYM_a));
946 else if (key >= KSYM_0 && key <= KSYM_9)
947 sprintf(name_buffer, "%c", '0' + (char)(key - KSYM_0));
948 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
949 sprintf(name_buffer, "keypad %c", '0' + (char)(key - KSYM_KP_0));
950 else if (key >= KSYM_F1 && key <= KSYM_F24)
951 sprintf(name_buffer, "function F%d", (int)(key - KSYM_F1 + 1));
952 else if (key == KSYM_UNDEFINED)
953 strcpy(name_buffer, "(undefined)");
960 if (key == translate_key[i].key)
962 strcpy(name_buffer, translate_key[i].name);
966 while (translate_key[++i].name);
968 if (!translate_key[i].name)
969 strcpy(name_buffer, "(unknown)");
974 else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME)
976 static char name_buffer[30];
979 if (key >= KSYM_A && key <= KSYM_Z)
980 sprintf(name_buffer, "XK_%c", 'A' + (char)(key - KSYM_A));
981 else if (key >= KSYM_a && key <= KSYM_z)
982 sprintf(name_buffer, "XK_%c", 'a' + (char)(key - KSYM_a));
983 else if (key >= KSYM_0 && key <= KSYM_9)
984 sprintf(name_buffer, "XK_%c", '0' + (char)(key - KSYM_0));
985 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
986 sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - KSYM_KP_0));
987 else if (key >= KSYM_F1 && key <= KSYM_F24)
988 sprintf(name_buffer, "XK_F%d", (int)(key - KSYM_F1 + 1));
989 else if (key == KSYM_UNDEFINED)
990 strcpy(name_buffer, "[undefined]");
997 if (key == translate_key[i].key)
999 strcpy(name_buffer, translate_key[i].x11name);
1003 while (translate_key[++i].x11name);
1005 if (!translate_key[i].x11name)
1006 sprintf(name_buffer, "0x%04lx", (unsigned long)key);
1009 *x11name = name_buffer;
1011 else if (mode == TRANSLATE_KEYNAME_TO_KEYSYM)
1013 Key key = KSYM_UNDEFINED;
1018 if (strcmp(translate_key[i].name, *name) == 0)
1020 key = translate_key[i].key;
1024 while (translate_key[++i].x11name);
1026 if (key == KSYM_UNDEFINED)
1027 Error(ERR_WARN, "getKeyFromKeyName(): not completely implemented");
1031 else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
1033 Key key = KSYM_UNDEFINED;
1034 char *name_ptr = *x11name;
1036 if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4)
1038 char c = name_ptr[3];
1040 if (c >= 'A' && c <= 'Z')
1041 key = KSYM_A + (Key)(c - 'A');
1042 else if (c >= 'a' && c <= 'z')
1043 key = KSYM_a + (Key)(c - 'a');
1044 else if (c >= '0' && c <= '9')
1045 key = KSYM_0 + (Key)(c - '0');
1047 else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7)
1049 char c = name_ptr[6];
1051 if (c >= '0' && c <= '9')
1052 key = KSYM_0 + (Key)(c - '0');
1054 else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
1056 char c1 = name_ptr[4];
1057 char c2 = name_ptr[5];
1060 if ((c1 >= '0' && c1 <= '9') &&
1061 ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
1062 d = atoi(&name_ptr[4]);
1064 if (d >=1 && d <= 24)
1065 key = KSYM_F1 + (Key)(d - 1);
1067 else if (strncmp(name_ptr, "XK_", 3) == 0)
1073 if (strcmp(name_ptr, translate_key[i].x11name) == 0)
1075 key = translate_key[i].key;
1079 while (translate_key[++i].x11name);
1081 else if (strncmp(name_ptr, "0x", 2) == 0)
1083 unsigned long value = 0;
1089 char c = *name_ptr++;
1092 if (c >= '0' && c <= '9')
1094 else if (c >= 'a' && c <= 'f')
1095 d = (int)(c - 'a' + 10);
1096 else if (c >= 'A' && c <= 'F')
1097 d = (int)(c - 'A' + 10);
1105 value = value * 16 + d;
1116 char *getKeyNameFromKey(Key key)
1120 translate_keyname(&key, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME);
1124 char *getX11KeyNameFromKey(Key key)
1128 translate_keyname(&key, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME);
1132 Key getKeyFromKeyName(char *name)
1136 translate_keyname(&key, NULL, &name, TRANSLATE_KEYNAME_TO_KEYSYM);
1140 Key getKeyFromX11KeyName(char *x11name)
1144 translate_keyname(&key, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM);
1148 char getCharFromKey(Key key)
1150 char *keyname = getKeyNameFromKey(key);
1153 if (strlen(keyname) == 1)
1154 letter = keyname[0];
1155 else if (strcmp(keyname, "space") == 0)
1157 else if (strcmp(keyname, "circumflex") == 0)
1164 /* ========================================================================= */
1165 /* functions for checking filenames */
1166 /* ========================================================================= */
1168 boolean FileIsGraphic(char *filename)
1170 if (strlen(filename) > 4 &&
1171 strcmp(&filename[strlen(filename) - 4], ".pcx") == 0)
1177 boolean FileIsSound(char *filename)
1179 if (strlen(filename) > 4 &&
1180 strcmp(&filename[strlen(filename) - 4], ".wav") == 0)
1186 boolean FileIsMusic(char *filename)
1188 if (strlen(filename) > 4 &&
1189 (strcmp(&filename[strlen(filename) - 4], ".mod") == 0 ||
1190 strcmp(&filename[strlen(filename) - 4], ".MOD") == 0 ||
1191 strncmp(filename, "mod.", 4) == 0 ||
1192 strncmp(filename, "MOD.", 4) == 0))
1198 /* ========================================================================= */
1199 /* functions only needed for non-Unix (non-command-line) systems */
1200 /* ========================================================================= */
1202 #if !defined(PLATFORM_UNIX)
1204 #define ERROR_FILENAME "error.out"
1206 void initErrorFile()
1210 InitUserDataDirectory();
1212 filename = getPath2(getUserDataDir(), ERROR_FILENAME);
1217 FILE *openErrorFile()
1222 filename = getPath2(getUserDataDir(), ERROR_FILENAME);
1223 error_file = fopen(filename, MODE_APPEND);
1229 void dumpErrorFile()
1234 filename = getPath2(getUserDataDir(), ERROR_FILENAME);
1235 error_file = fopen(filename, MODE_READ);
1238 if (error_file != NULL)
1240 while (!feof(error_file))
1241 fputc(fgetc(error_file), stderr);
1249 /* ========================================================================= */
1250 /* the following is only for debugging purpose and normally not used */
1251 /* ========================================================================= */
1253 #define DEBUG_NUM_TIMESTAMPS 3
1255 void debug_print_timestamp(int counter_nr, char *message)
1257 static long counter[DEBUG_NUM_TIMESTAMPS][2];
1259 if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
1260 Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
1262 counter[counter_nr][0] = Counter();
1265 printf("%s %.2f seconds\n", message,
1266 (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
1268 counter[counter_nr][1] = Counter();