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>
24 #if !defined(PLATFORM_WIN32)
26 #include <sys/param.h>
34 #if defined(PLATFORM_MSDOS)
35 volatile unsigned long counter = 0;
37 void increment_counter()
42 END_OF_FUNCTION(increment_counter);
46 /* maximal allowed length of a command line option */
47 #define MAX_OPTION_LEN 256
50 static unsigned long mainCounter(int mode)
52 static unsigned long base_ms = 0;
53 unsigned long current_ms;
54 unsigned long counter_ms;
56 current_ms = SDL_GetTicks();
58 /* reset base time in case of counter initializing or wrap-around */
59 if (mode == INIT_COUNTER || current_ms < base_ms)
62 counter_ms = current_ms - base_ms;
64 return counter_ms; /* return milliseconds since last init */
67 #else /* !TARGET_SDL */
69 #if defined(PLATFORM_UNIX)
70 static unsigned long mainCounter(int mode)
72 static struct timeval base_time = { 0, 0 };
73 struct timeval current_time;
74 unsigned long counter_ms;
76 gettimeofday(¤t_time, NULL);
78 /* reset base time in case of counter initializing or wrap-around */
79 if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec)
80 base_time = current_time;
82 counter_ms = (current_time.tv_sec - base_time.tv_sec) * 1000
83 + (current_time.tv_usec - base_time.tv_usec) / 1000;
85 return counter_ms; /* return milliseconds since last init */
87 #endif /* PLATFORM_UNIX */
88 #endif /* !TARGET_SDL */
90 void InitCounter() /* set counter back to zero */
92 #if !defined(PLATFORM_MSDOS)
93 mainCounter(INIT_COUNTER);
95 LOCK_VARIABLE(counter);
96 LOCK_FUNCTION(increment_counter);
97 install_int_ex(increment_counter, BPS_TO_TIMER(100));
101 unsigned long Counter() /* get milliseconds since last call of InitCounter() */
103 #if !defined(PLATFORM_MSDOS)
104 return mainCounter(READ_COUNTER);
106 return (counter * 10);
110 static void sleep_milliseconds(unsigned long milliseconds_delay)
112 boolean do_busy_waiting = (milliseconds_delay < 5 ? TRUE : FALSE);
114 #if defined(PLATFORM_MSDOS)
115 /* don't use select() to perform waiting operations under DOS
116 environment; always use a busy loop for waiting instead */
117 do_busy_waiting = TRUE;
122 /* we want to wait only a few ms -- if we assume that we have a
123 kernel timer resolution of 10 ms, we would wait far to long;
124 therefore it's better to do a short interval of busy waiting
125 to get our sleeping time more accurate */
127 unsigned long base_counter = Counter(), actual_counter = Counter();
129 while (actual_counter < base_counter + milliseconds_delay &&
130 actual_counter >= base_counter)
131 actual_counter = Counter();
135 #if defined(TARGET_SDL)
136 SDL_Delay(milliseconds_delay);
138 struct timeval delay;
140 delay.tv_sec = milliseconds_delay / 1000;
141 delay.tv_usec = 1000 * (milliseconds_delay % 1000);
143 if (select(0, NULL, NULL, NULL, &delay) != 0)
144 Error(ERR_WARN, "sleep_milliseconds(): select() failed");
149 void Delay(unsigned long delay) /* Sleep specified number of milliseconds */
151 sleep_milliseconds(delay);
154 boolean FrameReached(unsigned long *frame_counter_var,
155 unsigned long frame_delay)
157 unsigned long actual_frame_counter = FrameCounter;
159 if (actual_frame_counter < *frame_counter_var + frame_delay &&
160 actual_frame_counter >= *frame_counter_var)
163 *frame_counter_var = actual_frame_counter;
168 boolean DelayReached(unsigned long *counter_var,
171 unsigned long actual_counter = Counter();
173 if (actual_counter < *counter_var + delay &&
174 actual_counter >= *counter_var)
177 *counter_var = actual_counter;
182 void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
184 unsigned long actual_counter;
188 actual_counter = Counter();
190 if (actual_counter < *counter_var + delay &&
191 actual_counter >= *counter_var)
192 sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
197 *counter_var = actual_counter;
200 /* int2str() returns a number converted to a string;
201 the used memory is static, but will be overwritten by later calls,
202 so if you want to save the result, copy it to a private string buffer;
203 there can be 10 local calls of int2str() without buffering the result --
204 the 11th call will then destroy the result from the first call and so on.
207 char *int2str(int number, int size)
209 static char shift_array[10][40];
210 static int shift_counter = 0;
211 char *s = shift_array[shift_counter];
213 shift_counter = (shift_counter + 1) % 10;
220 sprintf(s, " %09d", number);
221 return &s[strlen(s) - size];
225 sprintf(s, "%d", number);
230 unsigned int SimpleRND(unsigned int max)
232 #if defined(TARGET_SDL)
233 static unsigned long root = 654321;
234 unsigned long current_ms;
236 current_ms = SDL_GetTicks();
237 root = root * 4253261 + current_ms;
240 static unsigned long root = 654321;
241 struct timeval current_time;
243 gettimeofday(¤t_time, NULL);
244 root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
250 static unsigned int last_RND_value = 0;
252 unsigned int last_RND()
254 return last_RND_value;
258 unsigned int RND(unsigned int max)
261 return (last_RND_value = random_linux_libc() % max);
263 return (random_linux_libc() % max);
267 unsigned int InitRND(long seed)
269 #if defined(TARGET_SDL)
270 unsigned long current_ms;
272 if (seed == NEW_RANDOMIZE)
274 current_ms = SDL_GetTicks();
275 srandom_linux_libc((unsigned int) current_ms);
276 return (unsigned int) current_ms;
280 srandom_linux_libc((unsigned int) seed);
281 return (unsigned int) seed;
284 struct timeval current_time;
286 if (seed == NEW_RANDOMIZE)
288 gettimeofday(¤t_time, NULL);
289 srandom_linux_libc((unsigned int) current_time.tv_usec);
290 return (unsigned int) current_time.tv_usec;
294 srandom_linux_libc((unsigned int) seed);
295 return (unsigned int) seed;
302 #if defined(PLATFORM_WIN32)
303 return ANONYMOUS_NAME;
307 if ((pwd = getpwuid(getuid())) == NULL)
308 return ANONYMOUS_NAME;
316 #if defined(PLATFORM_UNIX)
319 if ((pwd = getpwuid(getuid())) == NULL || strlen(pwd->pw_gecos) == 0)
320 return ANONYMOUS_NAME;
323 static char real_name[1024];
324 char *from_ptr = pwd->pw_gecos, *to_ptr = real_name;
326 if (strchr(pwd->pw_gecos, 'ß') == NULL)
327 return pwd->pw_gecos;
329 /* the user's real name contains a 'ß' character (german sharp s),
330 which has no equivalent in upper case letters (which our fonts use) */
331 while (*from_ptr != '\0' && (long)(to_ptr - real_name) < 1024 - 2)
333 if (*from_ptr != 'ß')
334 *to_ptr++ = *from_ptr++;
346 #else /* !PLATFORM_UNIX */
347 return ANONYMOUS_NAME;
353 #if defined(PLATFORM_UNIX)
354 static char *home_dir = NULL;
358 if (!(home_dir = getenv("HOME")))
362 if ((pwd = getpwuid(getuid())))
363 home_dir = pwd->pw_dir;
375 char *getPath2(char *path1, char *path2)
377 char *complete_path = checked_malloc(strlen(path1) + 1 +
380 sprintf(complete_path, "%s/%s", path1, path2);
381 return complete_path;
384 char *getPath3(char *path1, char *path2, char *path3)
386 char *complete_path = checked_malloc(strlen(path1) + 1 +
390 sprintf(complete_path, "%s/%s/%s", path1, path2, path3);
391 return complete_path;
394 char *getStringCopy(char *s)
401 s_copy = checked_malloc(strlen(s) + 1);
407 char *getStringToLower(char *s)
409 char *s_copy = checked_malloc(strlen(s) + 1);
410 char *s_ptr = s_copy;
413 *s_ptr++ = tolower(*s++);
419 void GetOptions(char *argv[])
421 char **options_left = &argv[1];
423 /* initialize global program options */
424 options.display_name = NULL;
425 options.server_host = NULL;
426 options.server_port = 0;
427 options.ro_base_directory = RO_BASE_PATH;
428 options.rw_base_directory = RW_BASE_PATH;
429 options.level_directory = RO_BASE_PATH "/" LEVELS_DIRECTORY;
430 options.graphics_directory = RO_BASE_PATH "/" GRAPHICS_DIRECTORY;
431 options.sounds_directory = RO_BASE_PATH "/" SOUNDS_DIRECTORY;
432 options.music_directory = RO_BASE_PATH "/" MUSIC_DIRECTORY;
433 options.serveronly = FALSE;
434 options.network = FALSE;
435 options.verbose = FALSE;
436 options.debug = FALSE;
437 options.debug_command = NULL;
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 host> [<server 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 sounds directory\n"
482 " -m, --music <directory> alternative music 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 printf(" --debug-command <command> execute special command\n");
494 else if (strncmp(option, "-display", option_len) == 0)
496 if (option_arg == NULL)
497 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
499 options.display_name = option_arg;
500 if (option_arg == next_option)
503 else if (strncmp(option, "-basepath", option_len) == 0)
505 if (option_arg == NULL)
506 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
508 /* this should be extended to separate options for ro and rw data */
509 options.ro_base_directory = option_arg;
510 options.rw_base_directory = option_arg;
511 if (option_arg == next_option)
514 /* adjust path for level directory accordingly */
515 options.level_directory =
516 getPath2(options.ro_base_directory, LEVELS_DIRECTORY);
518 else if (strncmp(option, "-levels", option_len) == 0)
520 if (option_arg == NULL)
521 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
523 options.level_directory = option_arg;
524 if (option_arg == next_option)
527 else if (strncmp(option, "-graphics", option_len) == 0)
529 if (option_arg == NULL)
530 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
532 options.graphics_directory = option_arg;
533 if (option_arg == next_option)
536 else if (strncmp(option, "-sounds", option_len) == 0)
538 if (option_arg == NULL)
539 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
541 options.sounds_directory = option_arg;
542 if (option_arg == next_option)
545 else if (strncmp(option, "-music", option_len) == 0)
547 if (option_arg == NULL)
548 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
550 options.music_directory = option_arg;
551 if (option_arg == next_option)
554 else if (strncmp(option, "-network", option_len) == 0)
556 options.network = TRUE;
558 else if (strncmp(option, "-serveronly", option_len) == 0)
560 options.serveronly = TRUE;
562 else if (strncmp(option, "-verbose", option_len) == 0)
564 options.verbose = TRUE;
566 else if (strncmp(option, "-debug", option_len) == 0)
568 options.debug = TRUE;
570 else if (strncmp(option, "-debug-command", option_len) == 0)
572 if (option_arg == NULL)
573 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
575 options.debug_command = option_arg;
576 if (option_arg == next_option)
579 else if (*option == '-')
581 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
583 else if (options.server_host == NULL)
585 options.server_host = *options_left;
587 else if (options.server_port == 0)
589 options.server_port = atoi(*options_left);
590 if (options.server_port < 1024)
591 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
594 Error(ERR_EXIT_HELP, "too many arguments");
600 /* used by SetError() and GetError() to store internal error messages */
601 static char internal_error[1024]; /* this is bad */
603 void SetError(char *format, ...)
607 va_start(ap, format);
608 vsprintf(internal_error, format, ap);
614 return internal_error;
617 void Error(int mode, char *format, ...)
619 char *process_name = "";
620 FILE *error = stderr;
621 char *newline = "\n";
623 /* display warnings only when running in verbose mode */
624 if (mode & ERR_WARN && !options.verbose)
627 #if !defined(PLATFORM_UNIX)
630 if ((error = openErrorFile()) == NULL)
632 printf("Cannot write to error output file!%s", newline);
633 program.exit_function(1);
637 if (mode & ERR_SOUND_SERVER)
638 process_name = " sound server";
639 else if (mode & ERR_NETWORK_SERVER)
640 process_name = " network server";
641 else if (mode & ERR_NETWORK_CLIENT)
642 process_name = " network client **";
648 fprintf(error, "%s%s: ", program.command_basename, process_name);
651 fprintf(error, "warning: ");
653 va_start(ap, format);
654 vfprintf(error, format, ap);
657 fprintf(error, "%s", newline);
661 fprintf(error, "%s: Try option '--help' for more information.%s",
662 program.command_basename, newline);
665 fprintf(error, "%s%s: aborting%s",
666 program.command_basename, process_name, newline);
673 if (mode & ERR_FROM_SERVER)
674 exit(1); /* child process: normal exit */
676 program.exit_function(1); /* main process: clean up stuff */
680 void *checked_malloc(unsigned long size)
687 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
692 void *checked_calloc(unsigned long size)
696 ptr = calloc(1, size);
699 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
704 void *checked_realloc(void *ptr, unsigned long size)
706 ptr = realloc(ptr, size);
709 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
714 inline void swap_numbers(int *i1, int *i2)
722 inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2)
734 short getFile16BitInteger(FILE *file, int byte_order)
736 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
737 return ((fgetc(file) << 8) |
739 else /* BYTE_ORDER_LITTLE_ENDIAN */
740 return ((fgetc(file) << 0) |
744 void putFile16BitInteger(FILE *file, short value, int byte_order)
746 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
748 fputc((value >> 8) & 0xff, file);
749 fputc((value >> 0) & 0xff, file);
751 else /* BYTE_ORDER_LITTLE_ENDIAN */
753 fputc((value >> 0) & 0xff, file);
754 fputc((value >> 8) & 0xff, file);
758 int getFile32BitInteger(FILE *file, int byte_order)
760 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
761 return ((fgetc(file) << 24) |
762 (fgetc(file) << 16) |
765 else /* BYTE_ORDER_LITTLE_ENDIAN */
766 return ((fgetc(file) << 0) |
768 (fgetc(file) << 16) |
769 (fgetc(file) << 24));
772 void putFile32BitInteger(FILE *file, int value, int byte_order)
774 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
776 fputc((value >> 24) & 0xff, file);
777 fputc((value >> 16) & 0xff, file);
778 fputc((value >> 8) & 0xff, file);
779 fputc((value >> 0) & 0xff, file);
781 else /* BYTE_ORDER_LITTLE_ENDIAN */
783 fputc((value >> 0) & 0xff, file);
784 fputc((value >> 8) & 0xff, file);
785 fputc((value >> 16) & 0xff, file);
786 fputc((value >> 24) & 0xff, file);
790 boolean getFileChunk(FILE *file, char *chunk_name, int *chunk_size,
793 const int chunk_name_length = 4;
795 /* read chunk name */
796 fgets(chunk_name, chunk_name_length + 1, file);
798 if (chunk_size != NULL)
800 /* read chunk size */
801 *chunk_size = getFile32BitInteger(file, byte_order);
804 return (feof(file) || ferror(file) ? FALSE : TRUE);
807 void putFileChunk(FILE *file, char *chunk_name, int chunk_size,
810 /* write chunk name */
811 fputs(chunk_name, file);
815 /* write chunk size */
816 putFile32BitInteger(file, chunk_size, byte_order);
820 void ReadUnusedBytesFromFile(FILE *file, unsigned long bytes)
822 while (bytes-- && !feof(file))
826 void WriteUnusedBytesToFile(FILE *file, unsigned long bytes)
832 #define TRANSLATE_KEYSYM_TO_KEYNAME 0
833 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
834 #define TRANSLATE_KEYNAME_TO_KEYSYM 2
835 #define TRANSLATE_X11KEYNAME_TO_KEYSYM 3
837 void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
846 /* normal cursor keys */
847 { KSYM_Left, "XK_Left", "cursor left" },
848 { KSYM_Right, "XK_Right", "cursor right" },
849 { KSYM_Up, "XK_Up", "cursor up" },
850 { KSYM_Down, "XK_Down", "cursor down" },
852 /* keypad cursor keys */
854 { KSYM_KP_Left, "XK_KP_Left", "keypad left" },
855 { KSYM_KP_Right, "XK_KP_Right", "keypad right" },
856 { KSYM_KP_Up, "XK_KP_Up", "keypad up" },
857 { KSYM_KP_Down, "XK_KP_Down", "keypad down" },
860 /* other keypad keys */
862 { KSYM_KP_Enter, "XK_KP_Enter", "keypad enter" },
863 { KSYM_KP_Add, "XK_KP_Add", "keypad +" },
864 { KSYM_KP_Subtract, "XK_KP_Subtract", "keypad -" },
865 { KSYM_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
866 { KSYM_KP_Divide, "XK_KP_Divide", "keypad /" },
867 { KSYM_KP_Separator,"XK_KP_Separator", "keypad ," },
871 { KSYM_Shift_L, "XK_Shift_L", "left shift" },
872 { KSYM_Shift_R, "XK_Shift_R", "right shift" },
873 { KSYM_Control_L, "XK_Control_L", "left control" },
874 { KSYM_Control_R, "XK_Control_R", "right control" },
875 { KSYM_Meta_L, "XK_Meta_L", "left meta" },
876 { KSYM_Meta_R, "XK_Meta_R", "right meta" },
877 { KSYM_Alt_L, "XK_Alt_L", "left alt" },
878 { KSYM_Alt_R, "XK_Alt_R", "right alt" },
879 { KSYM_Super_L, "XK_Super_L", "left super" }, /* Win-L */
880 { KSYM_Super_R, "XK_Super_R", "right super" }, /* Win-R */
881 { KSYM_Mode_switch, "XK_Mode_switch", "mode switch" }, /* Alt-R */
882 { KSYM_Multi_key, "XK_Multi_key", "multi key" }, /* Ctrl-R */
884 /* some special keys */
885 { KSYM_BackSpace, "XK_BackSpace", "backspace" },
886 { KSYM_Delete, "XK_Delete", "delete" },
887 { KSYM_Insert, "XK_Insert", "insert" },
888 { KSYM_Tab, "XK_Tab", "tab" },
889 { KSYM_Home, "XK_Home", "home" },
890 { KSYM_End, "XK_End", "end" },
891 { KSYM_Page_Up, "XK_Page_Up", "page up" },
892 { KSYM_Page_Down, "XK_Page_Down", "page down" },
893 { KSYM_Menu, "XK_Menu", "menu" }, /* Win-Menu */
895 /* ASCII 0x20 to 0x40 keys (except numbers) */
896 { KSYM_space, "XK_space", "space" },
897 { KSYM_exclam, "XK_exclam", "!" },
898 { KSYM_quotedbl, "XK_quotedbl", "\"" },
899 { KSYM_numbersign, "XK_numbersign", "#" },
900 { KSYM_dollar, "XK_dollar", "$" },
901 { KSYM_percent, "XK_percent", "%" },
902 { KSYM_ampersand, "XK_ampersand", "&" },
903 { KSYM_apostrophe, "XK_apostrophe", "'" },
904 { KSYM_parenleft, "XK_parenleft", "(" },
905 { KSYM_parenright, "XK_parenright", ")" },
906 { KSYM_asterisk, "XK_asterisk", "*" },
907 { KSYM_plus, "XK_plus", "+" },
908 { KSYM_comma, "XK_comma", "," },
909 { KSYM_minus, "XK_minus", "-" },
910 { KSYM_period, "XK_period", "." },
911 { KSYM_slash, "XK_slash", "/" },
912 { KSYM_colon, "XK_colon", ":" },
913 { KSYM_semicolon, "XK_semicolon", ";" },
914 { KSYM_less, "XK_less", "<" },
915 { KSYM_equal, "XK_equal", "=" },
916 { KSYM_greater, "XK_greater", ">" },
917 { KSYM_question, "XK_question", "?" },
918 { KSYM_at, "XK_at", "@" },
920 /* more ASCII keys */
921 { KSYM_bracketleft, "XK_bracketleft", "[" },
922 { KSYM_backslash, "XK_backslash", "backslash" },
923 { KSYM_bracketright,"XK_bracketright", "]" },
924 { KSYM_asciicircum, "XK_asciicircum", "circumflex" },
925 { KSYM_underscore, "XK_underscore", "_" },
926 { KSYM_grave, "XK_grave", "grave" },
927 { KSYM_quoteleft, "XK_quoteleft", "quote left" },
928 { KSYM_braceleft, "XK_braceleft", "brace left" },
929 { KSYM_bar, "XK_bar", "bar" },
930 { KSYM_braceright, "XK_braceright", "brace right" },
931 { KSYM_asciitilde, "XK_asciitilde", "ascii tilde" },
933 /* special (non-ASCII) keys */
934 { KSYM_Adiaeresis, "XK_Adiaeresis", "Ä" },
935 { KSYM_Odiaeresis, "XK_Odiaeresis", "Ö" },
936 { KSYM_Udiaeresis, "XK_Udiaeresis", "Ü" },
937 { KSYM_adiaeresis, "XK_adiaeresis", "ä" },
938 { KSYM_odiaeresis, "XK_odiaeresis", "ö" },
939 { KSYM_udiaeresis, "XK_udiaeresis", "ü" },
940 { KSYM_ssharp, "XK_ssharp", "sharp s" },
942 /* end-of-array identifier */
948 if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
950 static char name_buffer[30];
953 if (key >= KSYM_A && key <= KSYM_Z)
954 sprintf(name_buffer, "%c", 'A' + (char)(key - KSYM_A));
955 else if (key >= KSYM_a && key <= KSYM_z)
956 sprintf(name_buffer, "%c", 'a' + (char)(key - KSYM_a));
957 else if (key >= KSYM_0 && key <= KSYM_9)
958 sprintf(name_buffer, "%c", '0' + (char)(key - KSYM_0));
959 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
960 sprintf(name_buffer, "keypad %c", '0' + (char)(key - KSYM_KP_0));
961 else if (key >= KSYM_F1 && key <= KSYM_F24)
962 sprintf(name_buffer, "function F%d", (int)(key - KSYM_F1 + 1));
963 else if (key == KSYM_UNDEFINED)
964 strcpy(name_buffer, "(undefined)");
971 if (key == translate_key[i].key)
973 strcpy(name_buffer, translate_key[i].name);
977 while (translate_key[++i].name);
979 if (!translate_key[i].name)
980 strcpy(name_buffer, "(unknown)");
985 else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME)
987 static char name_buffer[30];
990 if (key >= KSYM_A && key <= KSYM_Z)
991 sprintf(name_buffer, "XK_%c", 'A' + (char)(key - KSYM_A));
992 else if (key >= KSYM_a && key <= KSYM_z)
993 sprintf(name_buffer, "XK_%c", 'a' + (char)(key - KSYM_a));
994 else if (key >= KSYM_0 && key <= KSYM_9)
995 sprintf(name_buffer, "XK_%c", '0' + (char)(key - KSYM_0));
996 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
997 sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - KSYM_KP_0));
998 else if (key >= KSYM_F1 && key <= KSYM_F24)
999 sprintf(name_buffer, "XK_F%d", (int)(key - KSYM_F1 + 1));
1000 else if (key == KSYM_UNDEFINED)
1001 strcpy(name_buffer, "[undefined]");
1008 if (key == translate_key[i].key)
1010 strcpy(name_buffer, translate_key[i].x11name);
1014 while (translate_key[++i].x11name);
1016 if (!translate_key[i].x11name)
1017 sprintf(name_buffer, "0x%04lx", (unsigned long)key);
1020 *x11name = name_buffer;
1022 else if (mode == TRANSLATE_KEYNAME_TO_KEYSYM)
1024 Key key = KSYM_UNDEFINED;
1029 if (strcmp(translate_key[i].name, *name) == 0)
1031 key = translate_key[i].key;
1035 while (translate_key[++i].x11name);
1037 if (key == KSYM_UNDEFINED)
1038 Error(ERR_WARN, "getKeyFromKeyName(): not completely implemented");
1042 else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
1044 Key key = KSYM_UNDEFINED;
1045 char *name_ptr = *x11name;
1047 if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4)
1049 char c = name_ptr[3];
1051 if (c >= 'A' && c <= 'Z')
1052 key = KSYM_A + (Key)(c - 'A');
1053 else if (c >= 'a' && c <= 'z')
1054 key = KSYM_a + (Key)(c - 'a');
1055 else if (c >= '0' && c <= '9')
1056 key = KSYM_0 + (Key)(c - '0');
1058 else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7)
1060 char c = name_ptr[6];
1062 if (c >= '0' && c <= '9')
1063 key = KSYM_0 + (Key)(c - '0');
1065 else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
1067 char c1 = name_ptr[4];
1068 char c2 = name_ptr[5];
1071 if ((c1 >= '0' && c1 <= '9') &&
1072 ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
1073 d = atoi(&name_ptr[4]);
1075 if (d >=1 && d <= 24)
1076 key = KSYM_F1 + (Key)(d - 1);
1078 else if (strncmp(name_ptr, "XK_", 3) == 0)
1084 if (strcmp(name_ptr, translate_key[i].x11name) == 0)
1086 key = translate_key[i].key;
1090 while (translate_key[++i].x11name);
1092 else if (strncmp(name_ptr, "0x", 2) == 0)
1094 unsigned long value = 0;
1100 char c = *name_ptr++;
1103 if (c >= '0' && c <= '9')
1105 else if (c >= 'a' && c <= 'f')
1106 d = (int)(c - 'a' + 10);
1107 else if (c >= 'A' && c <= 'F')
1108 d = (int)(c - 'A' + 10);
1116 value = value * 16 + d;
1127 char *getKeyNameFromKey(Key key)
1131 translate_keyname(&key, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME);
1135 char *getX11KeyNameFromKey(Key key)
1139 translate_keyname(&key, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME);
1143 Key getKeyFromKeyName(char *name)
1147 translate_keyname(&key, NULL, &name, TRANSLATE_KEYNAME_TO_KEYSYM);
1151 Key getKeyFromX11KeyName(char *x11name)
1155 translate_keyname(&key, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM);
1159 char getCharFromKey(Key key)
1161 char *keyname = getKeyNameFromKey(key);
1164 if (strlen(keyname) == 1)
1165 letter = keyname[0];
1166 else if (strcmp(keyname, "space") == 0)
1168 else if (strcmp(keyname, "circumflex") == 0)
1175 /* ========================================================================= */
1176 /* functions for checking filenames */
1177 /* ========================================================================= */
1179 boolean FileIsGraphic(char *filename)
1181 if (strlen(filename) > 4 &&
1182 strcmp(&filename[strlen(filename) - 4], ".pcx") == 0)
1188 boolean FileIsSound(char *basename)
1190 if (strlen(basename) > 4 &&
1191 strcmp(&basename[strlen(basename) - 4], ".wav") == 0)
1197 boolean FileIsMusic(char *basename)
1199 if (strlen(basename) > 4 &&
1200 (strcmp(&basename[strlen(basename) - 4], ".mod") == 0 ||
1201 strcmp(&basename[strlen(basename) - 4], ".MOD") == 0 ||
1202 strncmp(basename, "mod.", 4) == 0 ||
1203 strncmp(basename, "MOD.", 4) == 0))
1209 boolean FileIsArtworkType(char *basename, int type)
1211 if ((type == TREE_TYPE_GRAPHICS_DIR && FileIsGraphic(basename)) ||
1212 (type == TREE_TYPE_SOUNDS_DIR && FileIsSound(basename)) ||
1213 (type == TREE_TYPE_MUSIC_DIR && FileIsMusic(basename)))
1220 /* ========================================================================= */
1221 /* functions only needed for non-Unix (non-command-line) systems */
1222 /* ========================================================================= */
1224 #if !defined(PLATFORM_UNIX)
1226 #define ERROR_FILENAME "error.out"
1228 void initErrorFile()
1232 InitUserDataDirectory();
1234 filename = getPath2(getUserDataDir(), ERROR_FILENAME);
1239 FILE *openErrorFile()
1244 filename = getPath2(getUserDataDir(), ERROR_FILENAME);
1245 error_file = fopen(filename, MODE_APPEND);
1251 void dumpErrorFile()
1256 filename = getPath2(getUserDataDir(), ERROR_FILENAME);
1257 error_file = fopen(filename, MODE_READ);
1260 if (error_file != NULL)
1262 while (!feof(error_file))
1263 fputc(fgetc(error_file), stderr);
1271 /* ========================================================================= */
1272 /* the following is only for debugging purpose and normally not used */
1273 /* ========================================================================= */
1275 #define DEBUG_NUM_TIMESTAMPS 3
1277 void debug_print_timestamp(int counter_nr, char *message)
1279 static long counter[DEBUG_NUM_TIMESTAMPS][2];
1281 if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
1282 Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
1284 counter[counter_nr][0] = Counter();
1287 printf("%s %.2f seconds\n", message,
1288 (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
1290 counter[counter_nr][1] = Counter();