1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1994-2002 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>
35 /* ------------------------------------------------------------------------- */
36 /* some generic helper functions */
37 /* ------------------------------------------------------------------------- */
39 void fprintf_line(FILE *stream, char *line_string, int line_length)
43 for (i=0; i<line_length; i++)
44 fprintf(stream, "%s", line_string);
46 fprintf(stream, "\n");
49 void printf_line(char *line_string, int line_length)
51 fprintf_line(stdout, line_string, line_length);
54 /* int2str() returns a number converted to a string;
55 the used memory is static, but will be overwritten by later calls,
56 so if you want to save the result, copy it to a private string buffer;
57 there can be 10 local calls of int2str() without buffering the result --
58 the 11th call will then destroy the result from the first call and so on.
61 char *int2str(int number, int size)
63 static char shift_array[10][40];
64 static int shift_counter = 0;
65 char *s = shift_array[shift_counter];
67 shift_counter = (shift_counter + 1) % 10;
74 sprintf(s, " %09d", number);
75 return &s[strlen(s) - size];
79 sprintf(s, "%d", number);
85 /* ------------------------------------------------------------------------- */
86 /* counter functions */
87 /* ------------------------------------------------------------------------- */
89 #if defined(PLATFORM_MSDOS)
90 volatile unsigned long counter = 0;
92 void increment_counter()
97 END_OF_FUNCTION(increment_counter);
101 /* maximal allowed length of a command line option */
102 #define MAX_OPTION_LEN 256
105 static unsigned long mainCounter(int mode)
107 static unsigned long base_ms = 0;
108 unsigned long current_ms;
109 unsigned long counter_ms;
111 current_ms = SDL_GetTicks();
113 /* reset base time in case of counter initializing or wrap-around */
114 if (mode == INIT_COUNTER || current_ms < base_ms)
115 base_ms = current_ms;
117 counter_ms = current_ms - base_ms;
119 return counter_ms; /* return milliseconds since last init */
122 #else /* !TARGET_SDL */
124 #if defined(PLATFORM_UNIX)
125 static unsigned long mainCounter(int mode)
127 static struct timeval base_time = { 0, 0 };
128 struct timeval current_time;
129 unsigned long counter_ms;
131 gettimeofday(¤t_time, NULL);
133 /* reset base time in case of counter initializing or wrap-around */
134 if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec)
135 base_time = current_time;
137 counter_ms = (current_time.tv_sec - base_time.tv_sec) * 1000
138 + (current_time.tv_usec - base_time.tv_usec) / 1000;
140 return counter_ms; /* return milliseconds since last init */
142 #endif /* PLATFORM_UNIX */
143 #endif /* !TARGET_SDL */
145 void InitCounter() /* set counter back to zero */
147 #if !defined(PLATFORM_MSDOS)
148 mainCounter(INIT_COUNTER);
150 LOCK_VARIABLE(counter);
151 LOCK_FUNCTION(increment_counter);
152 install_int_ex(increment_counter, BPS_TO_TIMER(100));
156 unsigned long Counter() /* get milliseconds since last call of InitCounter() */
158 #if !defined(PLATFORM_MSDOS)
159 return mainCounter(READ_COUNTER);
161 return (counter * 10);
165 static void sleep_milliseconds(unsigned long milliseconds_delay)
167 boolean do_busy_waiting = (milliseconds_delay < 5 ? TRUE : FALSE);
170 #if defined(PLATFORM_MSDOS)
171 /* don't use select() to perform waiting operations under DOS
172 environment; always use a busy loop for waiting instead */
173 do_busy_waiting = TRUE;
179 /* we want to wait only a few ms -- if we assume that we have a
180 kernel timer resolution of 10 ms, we would wait far to long;
181 therefore it's better to do a short interval of busy waiting
182 to get our sleeping time more accurate */
184 unsigned long base_counter = Counter(), actual_counter = Counter();
186 while (actual_counter < base_counter + milliseconds_delay &&
187 actual_counter >= base_counter)
188 actual_counter = Counter();
192 #if defined(TARGET_SDL)
193 SDL_Delay(milliseconds_delay);
194 #elif defined(TARGET_ALLEGRO)
195 rest(milliseconds_delay);
197 struct timeval delay;
199 delay.tv_sec = milliseconds_delay / 1000;
200 delay.tv_usec = 1000 * (milliseconds_delay % 1000);
202 if (select(0, NULL, NULL, NULL, &delay) != 0)
203 Error(ERR_WARN, "sleep_milliseconds(): select() failed");
208 void Delay(unsigned long delay) /* Sleep specified number of milliseconds */
210 sleep_milliseconds(delay);
213 boolean FrameReached(unsigned long *frame_counter_var,
214 unsigned long frame_delay)
216 unsigned long actual_frame_counter = FrameCounter;
218 if (actual_frame_counter < *frame_counter_var + frame_delay &&
219 actual_frame_counter >= *frame_counter_var)
222 *frame_counter_var = actual_frame_counter;
227 boolean DelayReached(unsigned long *counter_var,
230 unsigned long actual_counter = Counter();
232 if (actual_counter < *counter_var + delay &&
233 actual_counter >= *counter_var)
236 *counter_var = actual_counter;
241 void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
243 unsigned long actual_counter;
247 actual_counter = Counter();
249 if (actual_counter < *counter_var + delay &&
250 actual_counter >= *counter_var)
251 sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
256 *counter_var = actual_counter;
260 /* ------------------------------------------------------------------------- */
261 /* random generator functions */
262 /* ------------------------------------------------------------------------- */
264 unsigned int SimpleRND(unsigned int max)
266 #if defined(TARGET_SDL)
267 static unsigned long root = 654321;
268 unsigned long current_ms;
270 current_ms = SDL_GetTicks();
271 root = root * 4253261 + current_ms;
274 static unsigned long root = 654321;
275 struct timeval current_time;
277 gettimeofday(¤t_time, NULL);
278 root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
284 static unsigned int last_RND_value = 0;
286 unsigned int last_RND()
288 return last_RND_value;
292 unsigned int RND(unsigned int max)
295 return (last_RND_value = random_linux_libc() % max);
297 return (random_linux_libc() % max);
301 unsigned int InitRND(long seed)
303 #if defined(TARGET_SDL)
304 unsigned long current_ms;
306 if (seed == NEW_RANDOMIZE)
308 current_ms = SDL_GetTicks();
309 srandom_linux_libc((unsigned int) current_ms);
310 return (unsigned int) current_ms;
314 srandom_linux_libc((unsigned int) seed);
315 return (unsigned int) seed;
318 struct timeval current_time;
320 if (seed == NEW_RANDOMIZE)
322 gettimeofday(¤t_time, NULL);
323 srandom_linux_libc((unsigned int) current_time.tv_usec);
324 return (unsigned int) current_time.tv_usec;
328 srandom_linux_libc((unsigned int) seed);
329 return (unsigned int) seed;
335 /* ------------------------------------------------------------------------- */
336 /* system info functions */
337 /* ------------------------------------------------------------------------- */
341 #if defined(PLATFORM_WIN32)
342 return ANONYMOUS_NAME;
344 static char *login_name = NULL;
346 if (login_name == NULL)
350 if ((pwd = getpwuid(getuid())) == NULL)
351 login_name = ANONYMOUS_NAME;
353 login_name = getStringCopy(pwd->pw_name);
362 #if defined(PLATFORM_UNIX)
365 if ((pwd = getpwuid(getuid())) == NULL || strlen(pwd->pw_gecos) == 0)
366 return ANONYMOUS_NAME;
369 static char real_name[1024];
370 char *from_ptr = pwd->pw_gecos, *to_ptr = real_name;
372 if (strchr(pwd->pw_gecos, 'ß') == NULL)
373 return pwd->pw_gecos;
375 /* the user's real name contains a 'ß' character (german sharp s),
376 which has no equivalent in upper case letters (which our fonts use) */
377 while (*from_ptr != '\0' && (long)(to_ptr - real_name) < 1024 - 2)
379 if (*from_ptr != 'ß')
380 *to_ptr++ = *from_ptr++;
392 #else /* !PLATFORM_UNIX */
393 return ANONYMOUS_NAME;
399 #if defined(PLATFORM_UNIX)
400 static char *home_dir = NULL;
402 if (home_dir == NULL)
404 if ((home_dir = getenv("HOME")) == NULL)
408 if ((pwd = getpwuid(getuid())) == NULL)
411 home_dir = getStringCopy(pwd->pw_dir);
422 /* ------------------------------------------------------------------------- */
423 /* various string functions */
424 /* ------------------------------------------------------------------------- */
426 char *getPath2(char *path1, char *path2)
428 char *complete_path = checked_malloc(strlen(path1) + 1 +
431 sprintf(complete_path, "%s/%s", path1, path2);
432 return complete_path;
435 char *getPath3(char *path1, char *path2, char *path3)
437 char *complete_path = checked_malloc(strlen(path1) + 1 +
441 sprintf(complete_path, "%s/%s/%s", path1, path2, path3);
442 return complete_path;
445 char *getStringCat2(char *s1, char *s2)
447 char *complete_string = checked_malloc(strlen(s1) + strlen(s2) + 1);
449 sprintf(complete_string, "%s%s", s1, s2);
450 return complete_string;
453 char *getStringCopy(char *s)
460 s_copy = checked_malloc(strlen(s) + 1);
466 char *getStringToLower(char *s)
468 char *s_copy = checked_malloc(strlen(s) + 1);
469 char *s_ptr = s_copy;
472 *s_ptr++ = tolower(*s++);
479 /* ------------------------------------------------------------------------- */
480 /* command line option handling functions */
481 /* ------------------------------------------------------------------------- */
483 static void printUsage()
486 "Usage: %s [OPTION]... [HOSTNAME [PORT]]\n"
489 " -d, --display HOSTNAME[:SCREEN] specify X server display\n"
490 " -b, --basepath DIRECTORY alternative base DIRECTORY\n"
491 " -l, --level DIRECTORY alternative level DIRECTORY\n"
492 " -g, --graphics DIRECTORY alternative graphics DIRECTORY\n"
493 " -s, --sounds DIRECTORY alternative sounds DIRECTORY\n"
494 " -m, --music DIRECTORY alternative music DIRECTORY\n"
495 " -n, --network network multiplayer game\n"
496 " --serveronly only start network server\n"
497 " -v, --verbose verbose mode\n"
498 " --debug display debugging information\n"
499 " -e, --execute COMMAND execute batch COMMAND:\n"
501 "Valid commands for '--execute' option:\n"
502 " \"print graphicsinfo.conf\" print default graphics config\n"
503 " \"print soundsinfo.conf\" print default sounds config\n"
504 " \"print musicinfo.conf\" print default music config\n"
505 " \"dump level FILE\" dump level data from FILE\n"
506 " \"dump tape FILE\" dump tape data from FILE\n"
507 " \"autoplay LEVELDIR\" play level tapes for LEVELDIR\n"
509 program.command_basename);
512 void GetOptions(char *argv[])
514 char **options_left = &argv[1];
516 /* initialize global program options */
517 options.display_name = NULL;
518 options.server_host = NULL;
519 options.server_port = 0;
520 options.ro_base_directory = RO_BASE_PATH;
521 options.rw_base_directory = RW_BASE_PATH;
522 options.level_directory = RO_BASE_PATH "/" LEVELS_DIRECTORY;
523 options.graphics_directory = RO_BASE_PATH "/" GRAPHICS_DIRECTORY;
524 options.sounds_directory = RO_BASE_PATH "/" SOUNDS_DIRECTORY;
525 options.music_directory = RO_BASE_PATH "/" MUSIC_DIRECTORY;
526 options.execute_command = NULL;
527 options.serveronly = FALSE;
528 options.network = FALSE;
529 options.verbose = FALSE;
530 options.debug = FALSE;
532 while (*options_left)
534 char option_str[MAX_OPTION_LEN];
535 char *option = options_left[0];
536 char *next_option = options_left[1];
537 char *option_arg = NULL;
538 int option_len = strlen(option);
540 if (option_len >= MAX_OPTION_LEN)
541 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
543 strcpy(option_str, option); /* copy argument into buffer */
546 if (strcmp(option, "--") == 0) /* stop scanning arguments */
549 if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */
552 option_arg = strchr(option, '=');
553 if (option_arg == NULL) /* no '=' in option */
554 option_arg = next_option;
557 *option_arg++ = '\0'; /* cut argument from option */
558 if (*option_arg == '\0') /* no argument after '=' */
559 Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
562 option_len = strlen(option);
564 if (strcmp(option, "-") == 0)
565 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
566 else if (strncmp(option, "-help", option_len) == 0)
572 else if (strncmp(option, "-display", option_len) == 0)
574 if (option_arg == NULL)
575 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
577 options.display_name = option_arg;
578 if (option_arg == next_option)
581 else if (strncmp(option, "-basepath", option_len) == 0)
583 if (option_arg == NULL)
584 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
586 /* this should be extended to separate options for ro and rw data */
587 options.ro_base_directory = option_arg;
588 options.rw_base_directory = option_arg;
589 if (option_arg == next_option)
592 /* adjust path for level directory accordingly */
593 options.level_directory =
594 getPath2(options.ro_base_directory, LEVELS_DIRECTORY);
596 else if (strncmp(option, "-levels", option_len) == 0)
598 if (option_arg == NULL)
599 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
601 options.level_directory = option_arg;
602 if (option_arg == next_option)
605 else if (strncmp(option, "-graphics", option_len) == 0)
607 if (option_arg == NULL)
608 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
610 options.graphics_directory = option_arg;
611 if (option_arg == next_option)
614 else if (strncmp(option, "-sounds", option_len) == 0)
616 if (option_arg == NULL)
617 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
619 options.sounds_directory = option_arg;
620 if (option_arg == next_option)
623 else if (strncmp(option, "-music", option_len) == 0)
625 if (option_arg == NULL)
626 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
628 options.music_directory = option_arg;
629 if (option_arg == next_option)
632 else if (strncmp(option, "-network", option_len) == 0)
634 options.network = TRUE;
636 else if (strncmp(option, "-serveronly", option_len) == 0)
638 options.serveronly = TRUE;
640 else if (strncmp(option, "-verbose", option_len) == 0)
642 options.verbose = TRUE;
644 else if (strncmp(option, "-debug", option_len) == 0)
646 options.debug = TRUE;
648 else if (strncmp(option, "-execute", option_len) == 0)
650 if (option_arg == NULL)
651 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
653 options.execute_command = option_arg;
654 if (option_arg == next_option)
657 else if (*option == '-')
659 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
661 else if (options.server_host == NULL)
663 options.server_host = *options_left;
665 else if (options.server_port == 0)
667 options.server_port = atoi(*options_left);
668 if (options.server_port < 1024)
669 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
672 Error(ERR_EXIT_HELP, "too many arguments");
679 /* ------------------------------------------------------------------------- */
680 /* error handling functions */
681 /* ------------------------------------------------------------------------- */
683 /* used by SetError() and GetError() to store internal error messages */
684 static char internal_error[1024]; /* this is bad */
686 void SetError(char *format, ...)
690 va_start(ap, format);
691 vsprintf(internal_error, format, ap);
697 return internal_error;
700 void Error(int mode, char *format, ...)
702 static boolean last_line_was_separator = FALSE;
703 char *process_name = "";
704 FILE *error = stderr;
705 char *newline = "\n";
707 /* display warnings only when running in verbose mode */
708 if (mode & ERR_WARN && !options.verbose)
711 if (mode == ERR_RETURN_LINE)
713 if (!last_line_was_separator)
714 fprintf_line(error, format, 79);
716 last_line_was_separator = TRUE;
721 last_line_was_separator = FALSE;
723 #if defined(PLATFORM_MSDOS)
726 if ((error = openErrorFile()) == NULL)
728 printf("Cannot write to error output file!%s", newline);
729 program.exit_function(1);
733 if (mode & ERR_SOUND_SERVER)
734 process_name = " sound server";
735 else if (mode & ERR_NETWORK_SERVER)
736 process_name = " network server";
737 else if (mode & ERR_NETWORK_CLIENT)
738 process_name = " network client **";
744 fprintf(error, "%s%s: ", program.command_basename, process_name);
747 fprintf(error, "warning: ");
749 va_start(ap, format);
750 vfprintf(error, format, ap);
753 fprintf(error, "%s", newline);
757 fprintf(error, "%s: Try option '--help' for more information.%s",
758 program.command_basename, newline);
761 fprintf(error, "%s%s: aborting%s",
762 program.command_basename, process_name, newline);
769 if (mode & ERR_FROM_SERVER)
770 exit(1); /* child process: normal exit */
772 program.exit_function(1); /* main process: clean up stuff */
777 /* ------------------------------------------------------------------------- */
778 /* memory allocation functions */
779 /* ------------------------------------------------------------------------- */
781 void *checked_malloc(unsigned long size)
788 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
793 void *checked_calloc(unsigned long size)
797 ptr = calloc(1, size);
800 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
805 void *checked_realloc(void *ptr, unsigned long size)
807 ptr = realloc(ptr, size);
810 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
816 /* ------------------------------------------------------------------------- */
817 /* various helper functions */
818 /* ------------------------------------------------------------------------- */
820 inline void swap_numbers(int *i1, int *i2)
828 inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2)
840 short getFile16BitInteger(FILE *file, int byte_order)
842 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
843 return ((fgetc(file) << 8) |
845 else /* BYTE_ORDER_LITTLE_ENDIAN */
846 return ((fgetc(file) << 0) |
850 void putFile16BitInteger(FILE *file, short value, int byte_order)
852 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
854 fputc((value >> 8) & 0xff, file);
855 fputc((value >> 0) & 0xff, file);
857 else /* BYTE_ORDER_LITTLE_ENDIAN */
859 fputc((value >> 0) & 0xff, file);
860 fputc((value >> 8) & 0xff, file);
864 int getFile32BitInteger(FILE *file, int byte_order)
866 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
867 return ((fgetc(file) << 24) |
868 (fgetc(file) << 16) |
871 else /* BYTE_ORDER_LITTLE_ENDIAN */
872 return ((fgetc(file) << 0) |
874 (fgetc(file) << 16) |
875 (fgetc(file) << 24));
878 void putFile32BitInteger(FILE *file, int value, int byte_order)
880 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
882 fputc((value >> 24) & 0xff, file);
883 fputc((value >> 16) & 0xff, file);
884 fputc((value >> 8) & 0xff, file);
885 fputc((value >> 0) & 0xff, file);
887 else /* BYTE_ORDER_LITTLE_ENDIAN */
889 fputc((value >> 0) & 0xff, file);
890 fputc((value >> 8) & 0xff, file);
891 fputc((value >> 16) & 0xff, file);
892 fputc((value >> 24) & 0xff, file);
896 boolean getFileChunk(FILE *file, char *chunk_name, int *chunk_size,
899 const int chunk_name_length = 4;
901 /* read chunk name */
902 fgets(chunk_name, chunk_name_length + 1, file);
904 if (chunk_size != NULL)
906 /* read chunk size */
907 *chunk_size = getFile32BitInteger(file, byte_order);
910 return (feof(file) || ferror(file) ? FALSE : TRUE);
913 void putFileChunk(FILE *file, char *chunk_name, int chunk_size,
916 /* write chunk name */
917 fputs(chunk_name, file);
921 /* write chunk size */
922 putFile32BitInteger(file, chunk_size, byte_order);
926 int getFileVersion(FILE *file)
928 int version_major, version_minor, version_patch;
930 version_major = fgetc(file);
931 version_minor = fgetc(file);
932 version_patch = fgetc(file);
933 fgetc(file); /* not used */
935 return VERSION_IDENT(version_major, version_minor, version_patch);
938 void putFileVersion(FILE *file, int version)
940 int version_major = VERSION_MAJOR(version);
941 int version_minor = VERSION_MINOR(version);
942 int version_patch = VERSION_PATCH(version);
944 fputc(version_major, file);
945 fputc(version_minor, file);
946 fputc(version_patch, file);
947 fputc(0, file); /* not used */
950 void ReadUnusedBytesFromFile(FILE *file, unsigned long bytes)
952 while (bytes-- && !feof(file))
956 void WriteUnusedBytesToFile(FILE *file, unsigned long bytes)
963 /* ------------------------------------------------------------------------- */
964 /* functions to translate key identifiers between different format */
965 /* ------------------------------------------------------------------------- */
967 #define TRANSLATE_KEYSYM_TO_KEYNAME 0
968 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
969 #define TRANSLATE_KEYNAME_TO_KEYSYM 2
970 #define TRANSLATE_X11KEYNAME_TO_KEYSYM 3
972 void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
981 /* normal cursor keys */
982 { KSYM_Left, "XK_Left", "cursor left" },
983 { KSYM_Right, "XK_Right", "cursor right" },
984 { KSYM_Up, "XK_Up", "cursor up" },
985 { KSYM_Down, "XK_Down", "cursor down" },
987 /* keypad cursor keys */
989 { KSYM_KP_Left, "XK_KP_Left", "keypad left" },
990 { KSYM_KP_Right, "XK_KP_Right", "keypad right" },
991 { KSYM_KP_Up, "XK_KP_Up", "keypad up" },
992 { KSYM_KP_Down, "XK_KP_Down", "keypad down" },
995 /* other keypad keys */
997 { KSYM_KP_Enter, "XK_KP_Enter", "keypad enter" },
998 { KSYM_KP_Add, "XK_KP_Add", "keypad +" },
999 { KSYM_KP_Subtract, "XK_KP_Subtract", "keypad -" },
1000 { KSYM_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
1001 { KSYM_KP_Divide, "XK_KP_Divide", "keypad /" },
1002 { KSYM_KP_Separator,"XK_KP_Separator", "keypad ," },
1006 { KSYM_Shift_L, "XK_Shift_L", "left shift" },
1007 { KSYM_Shift_R, "XK_Shift_R", "right shift" },
1008 { KSYM_Control_L, "XK_Control_L", "left control" },
1009 { KSYM_Control_R, "XK_Control_R", "right control" },
1010 { KSYM_Meta_L, "XK_Meta_L", "left meta" },
1011 { KSYM_Meta_R, "XK_Meta_R", "right meta" },
1012 { KSYM_Alt_L, "XK_Alt_L", "left alt" },
1013 { KSYM_Alt_R, "XK_Alt_R", "right alt" },
1014 { KSYM_Super_L, "XK_Super_L", "left super" }, /* Win-L */
1015 { KSYM_Super_R, "XK_Super_R", "right super" }, /* Win-R */
1016 { KSYM_Mode_switch, "XK_Mode_switch", "mode switch" }, /* Alt-R */
1017 { KSYM_Multi_key, "XK_Multi_key", "multi key" }, /* Ctrl-R */
1019 /* some special keys */
1020 { KSYM_BackSpace, "XK_BackSpace", "backspace" },
1021 { KSYM_Delete, "XK_Delete", "delete" },
1022 { KSYM_Insert, "XK_Insert", "insert" },
1023 { KSYM_Tab, "XK_Tab", "tab" },
1024 { KSYM_Home, "XK_Home", "home" },
1025 { KSYM_End, "XK_End", "end" },
1026 { KSYM_Page_Up, "XK_Page_Up", "page up" },
1027 { KSYM_Page_Down, "XK_Page_Down", "page down" },
1028 { KSYM_Menu, "XK_Menu", "menu" }, /* Win-Menu */
1030 /* ASCII 0x20 to 0x40 keys (except numbers) */
1031 { KSYM_space, "XK_space", "space" },
1032 { KSYM_exclam, "XK_exclam", "!" },
1033 { KSYM_quotedbl, "XK_quotedbl", "\"" },
1034 { KSYM_numbersign, "XK_numbersign", "#" },
1035 { KSYM_dollar, "XK_dollar", "$" },
1036 { KSYM_percent, "XK_percent", "%" },
1037 { KSYM_ampersand, "XK_ampersand", "&" },
1038 { KSYM_apostrophe, "XK_apostrophe", "'" },
1039 { KSYM_parenleft, "XK_parenleft", "(" },
1040 { KSYM_parenright, "XK_parenright", ")" },
1041 { KSYM_asterisk, "XK_asterisk", "*" },
1042 { KSYM_plus, "XK_plus", "+" },
1043 { KSYM_comma, "XK_comma", "," },
1044 { KSYM_minus, "XK_minus", "-" },
1045 { KSYM_period, "XK_period", "." },
1046 { KSYM_slash, "XK_slash", "/" },
1047 { KSYM_colon, "XK_colon", ":" },
1048 { KSYM_semicolon, "XK_semicolon", ";" },
1049 { KSYM_less, "XK_less", "<" },
1050 { KSYM_equal, "XK_equal", "=" },
1051 { KSYM_greater, "XK_greater", ">" },
1052 { KSYM_question, "XK_question", "?" },
1053 { KSYM_at, "XK_at", "@" },
1055 /* more ASCII keys */
1056 { KSYM_bracketleft, "XK_bracketleft", "[" },
1057 { KSYM_backslash, "XK_backslash", "backslash" },
1058 { KSYM_bracketright,"XK_bracketright", "]" },
1059 { KSYM_asciicircum, "XK_asciicircum", "circumflex" },
1060 { KSYM_underscore, "XK_underscore", "_" },
1061 { KSYM_grave, "XK_grave", "grave" },
1062 { KSYM_quoteleft, "XK_quoteleft", "quote left" },
1063 { KSYM_braceleft, "XK_braceleft", "brace left" },
1064 { KSYM_bar, "XK_bar", "bar" },
1065 { KSYM_braceright, "XK_braceright", "brace right" },
1066 { KSYM_asciitilde, "XK_asciitilde", "ascii tilde" },
1068 /* special (non-ASCII) keys */
1069 { KSYM_Adiaeresis, "XK_Adiaeresis", "Ä" },
1070 { KSYM_Odiaeresis, "XK_Odiaeresis", "Ö" },
1071 { KSYM_Udiaeresis, "XK_Udiaeresis", "Ü" },
1072 { KSYM_adiaeresis, "XK_adiaeresis", "ä" },
1073 { KSYM_odiaeresis, "XK_odiaeresis", "ö" },
1074 { KSYM_udiaeresis, "XK_udiaeresis", "ü" },
1075 { KSYM_ssharp, "XK_ssharp", "sharp s" },
1077 /* end-of-array identifier */
1083 if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
1085 static char name_buffer[30];
1088 if (key >= KSYM_A && key <= KSYM_Z)
1089 sprintf(name_buffer, "%c", 'A' + (char)(key - KSYM_A));
1090 else if (key >= KSYM_a && key <= KSYM_z)
1091 sprintf(name_buffer, "%c", 'a' + (char)(key - KSYM_a));
1092 else if (key >= KSYM_0 && key <= KSYM_9)
1093 sprintf(name_buffer, "%c", '0' + (char)(key - KSYM_0));
1094 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
1095 sprintf(name_buffer, "keypad %c", '0' + (char)(key - KSYM_KP_0));
1096 else if (key >= KSYM_FKEY_FIRST && key <= KSYM_FKEY_LAST)
1097 sprintf(name_buffer, "function F%d", (int)(key - KSYM_FKEY_FIRST + 1));
1098 else if (key == KSYM_UNDEFINED)
1099 strcpy(name_buffer, "(undefined)");
1106 if (key == translate_key[i].key)
1108 strcpy(name_buffer, translate_key[i].name);
1112 while (translate_key[++i].name);
1114 if (!translate_key[i].name)
1115 strcpy(name_buffer, "(unknown)");
1118 *name = name_buffer;
1120 else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME)
1122 static char name_buffer[30];
1125 if (key >= KSYM_A && key <= KSYM_Z)
1126 sprintf(name_buffer, "XK_%c", 'A' + (char)(key - KSYM_A));
1127 else if (key >= KSYM_a && key <= KSYM_z)
1128 sprintf(name_buffer, "XK_%c", 'a' + (char)(key - KSYM_a));
1129 else if (key >= KSYM_0 && key <= KSYM_9)
1130 sprintf(name_buffer, "XK_%c", '0' + (char)(key - KSYM_0));
1131 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
1132 sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - KSYM_KP_0));
1133 else if (key >= KSYM_FKEY_FIRST && key <= KSYM_FKEY_LAST)
1134 sprintf(name_buffer, "XK_F%d", (int)(key - KSYM_FKEY_FIRST + 1));
1135 else if (key == KSYM_UNDEFINED)
1136 strcpy(name_buffer, "[undefined]");
1143 if (key == translate_key[i].key)
1145 strcpy(name_buffer, translate_key[i].x11name);
1149 while (translate_key[++i].x11name);
1151 if (!translate_key[i].x11name)
1152 sprintf(name_buffer, "0x%04lx", (unsigned long)key);
1155 *x11name = name_buffer;
1157 else if (mode == TRANSLATE_KEYNAME_TO_KEYSYM)
1159 Key key = KSYM_UNDEFINED;
1164 if (strcmp(translate_key[i].name, *name) == 0)
1166 key = translate_key[i].key;
1170 while (translate_key[++i].x11name);
1172 if (key == KSYM_UNDEFINED)
1173 Error(ERR_WARN, "getKeyFromKeyName(): not completely implemented");
1177 else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
1179 Key key = KSYM_UNDEFINED;
1180 char *name_ptr = *x11name;
1182 if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4)
1184 char c = name_ptr[3];
1186 if (c >= 'A' && c <= 'Z')
1187 key = KSYM_A + (Key)(c - 'A');
1188 else if (c >= 'a' && c <= 'z')
1189 key = KSYM_a + (Key)(c - 'a');
1190 else if (c >= '0' && c <= '9')
1191 key = KSYM_0 + (Key)(c - '0');
1193 else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7)
1195 char c = name_ptr[6];
1197 if (c >= '0' && c <= '9')
1198 key = KSYM_0 + (Key)(c - '0');
1200 else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
1202 char c1 = name_ptr[4];
1203 char c2 = name_ptr[5];
1206 if ((c1 >= '0' && c1 <= '9') &&
1207 ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
1208 d = atoi(&name_ptr[4]);
1210 if (d >= 1 && d <= KSYM_NUM_FKEYS)
1211 key = KSYM_F1 + (Key)(d - 1);
1213 else if (strncmp(name_ptr, "XK_", 3) == 0)
1219 if (strcmp(name_ptr, translate_key[i].x11name) == 0)
1221 key = translate_key[i].key;
1225 while (translate_key[++i].x11name);
1227 else if (strncmp(name_ptr, "0x", 2) == 0)
1229 unsigned long value = 0;
1235 char c = *name_ptr++;
1238 if (c >= '0' && c <= '9')
1240 else if (c >= 'a' && c <= 'f')
1241 d = (int)(c - 'a' + 10);
1242 else if (c >= 'A' && c <= 'F')
1243 d = (int)(c - 'A' + 10);
1251 value = value * 16 + d;
1262 char *getKeyNameFromKey(Key key)
1266 translate_keyname(&key, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME);
1270 char *getX11KeyNameFromKey(Key key)
1274 translate_keyname(&key, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME);
1278 Key getKeyFromKeyName(char *name)
1282 translate_keyname(&key, NULL, &name, TRANSLATE_KEYNAME_TO_KEYSYM);
1286 Key getKeyFromX11KeyName(char *x11name)
1290 translate_keyname(&key, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM);
1294 char getCharFromKey(Key key)
1296 char *keyname = getKeyNameFromKey(key);
1299 if (strlen(keyname) == 1)
1300 letter = keyname[0];
1301 else if (strcmp(keyname, "space") == 0)
1303 else if (strcmp(keyname, "circumflex") == 0)
1310 /* ------------------------------------------------------------------------- */
1311 /* functions to translate string identifiers to integer or boolean value */
1312 /* ------------------------------------------------------------------------- */
1314 int get_integer_from_string(char *s)
1316 static char *number_text[][3] =
1318 { "0", "zero", "null", },
1319 { "1", "one", "first" },
1320 { "2", "two", "second" },
1321 { "3", "three", "third" },
1322 { "4", "four", "fourth" },
1323 { "5", "five", "fifth" },
1324 { "6", "six", "sixth" },
1325 { "7", "seven", "seventh" },
1326 { "8", "eight", "eighth" },
1327 { "9", "nine", "ninth" },
1328 { "10", "ten", "tenth" },
1329 { "11", "eleven", "eleventh" },
1330 { "12", "twelve", "twelfth" },
1334 char *s_lower = getStringToLower(s);
1337 for (i=0; i<13; i++)
1339 if (strcmp(s_lower, number_text[i][j]) == 0)
1350 boolean get_boolean_from_string(char *s)
1352 char *s_lower = getStringToLower(s);
1353 boolean result = FALSE;
1355 if (strcmp(s_lower, "true") == 0 ||
1356 strcmp(s_lower, "yes") == 0 ||
1357 strcmp(s_lower, "on") == 0 ||
1358 get_integer_from_string(s) == 1)
1367 /* ------------------------------------------------------------------------- */
1368 /* functions for generic lists */
1369 /* ------------------------------------------------------------------------- */
1371 ListNode *newListNode()
1373 return checked_calloc(sizeof(ListNode));
1376 void addNodeToList(ListNode **node_first, char *key, void *content)
1378 ListNode *node_new = newListNode();
1381 printf("LIST: adding node with key '%s'\n", key);
1384 node_new->key = getStringCopy(key);
1385 node_new->content = content;
1386 node_new->next = *node_first;
1387 *node_first = node_new;
1390 void deleteNodeFromList(ListNode **node_first, char *key,
1391 void (*destructor_function)(void *))
1393 if (node_first == NULL || *node_first == NULL)
1397 printf("[CHECKING LIST KEY '%s' == '%s']\n",
1398 (*node_first)->key, key);
1401 if (strcmp((*node_first)->key, key) == 0)
1404 printf("[DELETING LIST ENTRY]\n");
1407 free((*node_first)->key);
1408 if (destructor_function)
1409 destructor_function((*node_first)->content);
1410 *node_first = (*node_first)->next;
1413 deleteNodeFromList(&(*node_first)->next, key, destructor_function);
1416 ListNode *getNodeFromKey(ListNode *node_first, char *key)
1418 if (node_first == NULL)
1421 if (strcmp(node_first->key, key) == 0)
1424 return getNodeFromKey(node_first->next, key);
1427 int getNumNodes(ListNode *node_first)
1429 return (node_first ? 1 + getNumNodes(node_first->next) : 0);
1432 void dumpList(ListNode *node_first)
1434 ListNode *node = node_first;
1438 printf("['%s' (%d)]\n", node->key,
1439 ((struct ListNodeInfo *)node->content)->num_references);
1443 printf("[%d nodes]\n", getNumNodes(node_first));
1447 /* ------------------------------------------------------------------------- */
1448 /* functions for checking filenames */
1449 /* ------------------------------------------------------------------------- */
1451 boolean FileIsGraphic(char *filename)
1453 if (strlen(filename) > 4 &&
1454 strcmp(&filename[strlen(filename) - 4], ".pcx") == 0)
1460 boolean FileIsSound(char *basename)
1462 if (strlen(basename) > 4 &&
1463 strcmp(&basename[strlen(basename) - 4], ".wav") == 0)
1469 boolean FileIsMusic(char *basename)
1471 /* "music" can be a WAV (loop) file or (if compiled with SDL) a MOD file */
1473 if (FileIsSound(basename))
1476 #if defined(TARGET_SDL)
1477 if (strlen(basename) > 4 &&
1478 (strcmp(&basename[strlen(basename) - 4], ".mod") == 0 ||
1479 strcmp(&basename[strlen(basename) - 4], ".MOD") == 0 ||
1480 strncmp(basename, "mod.", 4) == 0 ||
1481 strncmp(basename, "MOD.", 4) == 0))
1488 boolean FileIsArtworkType(char *basename, int type)
1490 if ((type == TREE_TYPE_GRAPHICS_DIR && FileIsGraphic(basename)) ||
1491 (type == TREE_TYPE_SOUNDS_DIR && FileIsSound(basename)) ||
1492 (type == TREE_TYPE_MUSIC_DIR && FileIsMusic(basename)))
1498 /* ------------------------------------------------------------------------- */
1499 /* functions for loading artwork configuration information */
1500 /* ------------------------------------------------------------------------- */
1502 static void FreeCustomArtworkList(struct ArtworkListInfo *,
1503 struct ListNodeInfo ***, int *);
1505 static int get_parameter_value(int type, char *value)
1507 return (strcmp(value, ARG_UNDEFINED) == 0 ? ARG_UNDEFINED_VALUE :
1508 type == TYPE_INTEGER ? get_integer_from_string(value) :
1509 type == TYPE_BOOLEAN ? get_boolean_from_string(value) :
1513 struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list,
1514 struct ConfigInfo *suffix_list,
1515 int num_file_list_entries)
1517 struct FileInfo *file_list;
1518 int num_file_list_entries_found = 0;
1519 int num_suffix_list_entries = 0;
1523 file_list = checked_calloc(num_file_list_entries * sizeof(struct FileInfo));
1525 for (i=0; suffix_list[i].token != NULL; i++)
1526 num_suffix_list_entries++;
1528 /* always start with reliable default values */
1529 for (i=0; i<num_file_list_entries; i++)
1531 file_list[i].token = NULL;
1532 file_list[i].default_filename = NULL;
1533 file_list[i].filename = NULL;
1535 if (num_suffix_list_entries > 0)
1537 int parameter_array_size = num_suffix_list_entries * sizeof(int);
1539 file_list[i].default_parameter = checked_calloc(parameter_array_size);
1540 file_list[i].parameter = checked_calloc(parameter_array_size);
1542 for (j=0; j<num_suffix_list_entries; j++)
1544 int default_parameter =
1545 get_parameter_value(suffix_list[j].type, suffix_list[j].value);
1547 file_list[i].default_parameter[j] = default_parameter;
1548 file_list[i].parameter[j] = default_parameter;
1553 for (i=0; config_list[i].token != NULL; i++)
1555 int len_config_token = strlen(config_list[i].token);
1556 int len_config_value = strlen(config_list[i].value);
1557 boolean is_file_entry = TRUE;
1559 for (j=0; suffix_list[j].token != NULL; j++)
1561 int len_suffix = strlen(suffix_list[j].token);
1563 if (len_suffix < len_config_token &&
1564 strcmp(&config_list[i].token[len_config_token - len_suffix],
1565 suffix_list[j].token) == 0)
1567 file_list[list_pos].default_parameter[j] =
1568 get_parameter_value(suffix_list[j].type, config_list[i].value);
1570 is_file_entry = FALSE;
1580 if (list_pos >= num_file_list_entries)
1583 /* simple sanity check if this is really a file definition */
1584 if (strcmp(&config_list[i].value[len_config_value - 4], ".pcx") != 0 &&
1585 strcmp(&config_list[i].value[len_config_value - 4], ".wav") != 0 &&
1586 strcmp(config_list[i].value, UNDEFINED_FILENAME) != 0)
1588 Error(ERR_RETURN, "Configuration directive '%s' -> '%s':",
1589 config_list[i].token, config_list[i].value);
1590 Error(ERR_EXIT, "This seems to be no valid definition -- please fix");
1593 file_list[list_pos].token = config_list[i].token;
1594 file_list[list_pos].default_filename = config_list[i].value;
1598 num_file_list_entries_found = list_pos + 1;
1599 if (num_file_list_entries_found != num_file_list_entries)
1601 Error(ERR_RETURN_LINE, "-");
1602 Error(ERR_RETURN, "inconsistant config list information:");
1603 Error(ERR_RETURN, "- should be: %d (according to 'src/conf_gfx.h')",
1604 num_file_list_entries);
1605 Error(ERR_RETURN, "- found to be: %d (according to 'src/conf_gfx.c')",
1606 num_file_list_entries_found);
1607 Error(ERR_EXIT, "please fix");
1613 static boolean token_suffix_match(char *token, char *suffix, int start_pos)
1615 int len_token = strlen(token);
1616 int len_suffix = strlen(suffix);
1619 if (IS_PARENT_PROCESS(audio.mixer_pid))
1620 printf(":::::::::: check '%s' for '%s' ::::::::::\n", token, suffix);
1623 if (start_pos < 0) /* compare suffix from end of string */
1624 start_pos += len_token;
1626 if (start_pos < 0 || start_pos + len_suffix > len_token)
1629 if (strncmp(&token[start_pos], suffix, len_suffix) != 0)
1632 if (token[start_pos + len_suffix] == '\0')
1635 if (token[start_pos + len_suffix] == '.')
1641 #define KNOWN_TOKEN_VALUE "[KNOWN_TOKEN]"
1643 static void read_token_parameters(struct SetupFileList *setup_file_list,
1644 struct ConfigInfo *suffix_list,
1645 struct FileInfo *file_list_entry)
1647 /* check for config token that is the base token without any suffixes */
1648 char *filename = getTokenValue(setup_file_list, file_list_entry->token);
1649 char *known_token_value = KNOWN_TOKEN_VALUE;
1652 if (filename != NULL)
1654 /* when file definition found, set all parameters to default values */
1655 for (i=0; suffix_list[i].token != NULL; i++)
1656 file_list_entry->parameter[i] =
1657 get_parameter_value(suffix_list[i].type, suffix_list[i].value);
1659 file_list_entry->filename = getStringCopy(filename);
1661 /* mark config file token as well known from default config */
1662 setTokenValue(setup_file_list, file_list_entry->token, known_token_value);
1665 file_list_entry->filename =
1666 getStringCopy(file_list_entry->default_filename);
1668 /* check for config tokens that can be build by base token and suffixes */
1669 for (i=0; suffix_list[i].token != NULL; i++)
1671 char *token = getStringCat2(file_list_entry->token, suffix_list[i].token);
1672 char *value = getTokenValue(setup_file_list, token);
1676 file_list_entry->parameter[i] =
1677 get_parameter_value(suffix_list[i].type, value);
1679 /* mark config file token as well known from default config */
1680 setTokenValue(setup_file_list, token, known_token_value);
1687 static void add_dynamic_file_list_entry(struct FileInfo **list,
1688 int *num_list_entries,
1689 struct SetupFileList *extra_file_list,
1690 struct ConfigInfo *suffix_list,
1691 int num_suffix_list_entries,
1694 struct FileInfo *new_list_entry;
1695 int parameter_array_size = num_suffix_list_entries * sizeof(int);
1698 if (IS_PARENT_PROCESS(audio.mixer_pid))
1699 printf("===> found dynamic definition '%s'\n", token);
1702 (*num_list_entries)++;
1703 *list = checked_realloc(*list, *num_list_entries * sizeof(struct FileInfo));
1704 new_list_entry = &(*list)[*num_list_entries - 1];
1706 new_list_entry->token = getStringCopy(token);
1707 new_list_entry->parameter = checked_calloc(parameter_array_size);
1709 read_token_parameters(extra_file_list, suffix_list, new_list_entry);
1712 static void add_property_mapping(struct PropertyMapping **list,
1713 int *num_list_entries,
1714 int base_index, int ext1_index,int ext2_index,
1717 struct PropertyMapping *new_list_entry;
1719 (*num_list_entries)++;
1720 *list = checked_realloc(*list,
1721 *num_list_entries * sizeof(struct PropertyMapping));
1722 new_list_entry = &(*list)[*num_list_entries - 1];
1724 new_list_entry->base_index = base_index;
1725 new_list_entry->ext1_index = ext1_index;
1726 new_list_entry->ext2_index = ext2_index;
1728 new_list_entry->artwork_index = artwork_index;
1731 void LoadArtworkConfig(struct ArtworkListInfo *artwork_info)
1733 struct FileInfo *file_list = artwork_info->file_list;
1734 struct ConfigInfo *suffix_list = artwork_info->suffix_list;
1735 char **base_prefixes = artwork_info->base_prefixes;
1736 char **ext1_suffixes = artwork_info->ext1_suffixes;
1737 char **ext2_suffixes = artwork_info->ext2_suffixes;
1738 int num_file_list_entries = artwork_info->num_file_list_entries;
1739 int num_suffix_list_entries = artwork_info->num_suffix_list_entries;
1740 int num_base_prefixes = artwork_info->num_base_prefixes;
1741 int num_ext1_suffixes = artwork_info->num_ext1_suffixes;
1742 int num_ext2_suffixes = artwork_info->num_ext2_suffixes;
1743 char *filename = getCustomArtworkConfigFilename(artwork_info->type);
1744 struct SetupFileList *setup_file_list;
1745 struct SetupFileList *extra_file_list = NULL;
1746 struct SetupFileList *list;
1747 char *known_token_value = KNOWN_TOKEN_VALUE;
1751 printf("GOT CUSTOM ARTWORK CONFIG FILE '%s'\n", filename);
1754 /* always start with reliable default values */
1755 for (i=0; i<num_file_list_entries; i++)
1757 if (file_list[i].filename != NULL)
1758 free(file_list[i].filename);
1759 file_list[i].filename = NULL;
1761 for (j=0; j<num_suffix_list_entries; j++)
1762 file_list[i].parameter[j] = file_list[i].default_parameter[j];
1765 /* free previous dynamic artwork file array */
1766 if (artwork_info->dynamic_file_list != NULL)
1768 for (i=0; i<artwork_info->num_dynamic_file_list_entries; i++)
1770 free(artwork_info->dynamic_file_list[i].token);
1771 free(artwork_info->dynamic_file_list[i].filename);
1772 free(artwork_info->dynamic_file_list[i].parameter);
1775 free(artwork_info->dynamic_file_list);
1777 artwork_info->dynamic_file_list = NULL;
1778 artwork_info->num_dynamic_file_list_entries = 0;
1780 FreeCustomArtworkList(artwork_info, &artwork_info->dynamic_artwork_list,
1781 &artwork_info->num_dynamic_file_list_entries);
1784 /* free previous property mapping */
1785 if (artwork_info->property_mapping != NULL)
1787 free(artwork_info->property_mapping);
1789 artwork_info->property_mapping = NULL;
1790 artwork_info->num_property_mapping_entries = 0;
1793 if (filename == NULL)
1796 if ((setup_file_list = loadSetupFileList(filename)) == NULL)
1799 /* read parameters for all known config file tokens */
1800 for (i=0; i<num_file_list_entries; i++)
1801 read_token_parameters(setup_file_list, suffix_list, &file_list[i]);
1803 /* set all known tokens to "known" keyword */
1804 setTokenValue(setup_file_list, "name", known_token_value);
1805 setTokenValue(setup_file_list, "sort_priority", known_token_value);
1807 /* copy all unknown config file tokens to extra config list */
1808 for (list = setup_file_list; list != NULL; list = list->next)
1810 if (strcmp(list->value, known_token_value) != 0)
1812 if (extra_file_list == NULL)
1813 extra_file_list = newSetupFileList(list->token, list->value);
1815 setTokenValue(extra_file_list, list->token, list->value);
1819 /* at this point, we do not need the config file list anymore -- free it */
1820 freeSetupFileList(setup_file_list);
1822 /* now try to determine valid, dynamically defined config tokens */
1824 for (list = extra_file_list; list != NULL; list = list->next)
1826 struct FileInfo **dynamic_file_list =
1827 &artwork_info->dynamic_file_list;
1828 int *num_dynamic_file_list_entries =
1829 &artwork_info->num_dynamic_file_list_entries;
1830 struct PropertyMapping **property_mapping =
1831 &artwork_info->property_mapping;
1832 int *num_property_mapping_entries =
1833 &artwork_info->num_property_mapping_entries;
1834 int current_summarized_file_list_entry =
1835 artwork_info->num_file_list_entries +
1836 artwork_info->num_dynamic_file_list_entries;
1837 char *token = list->token;
1838 int len_token = strlen(token);
1840 boolean base_prefix_found = FALSE;
1841 boolean parameter_suffix_found = FALSE;
1843 /* skip all parameter definitions (handled by read_token_parameters()) */
1844 for (i=0; i < num_suffix_list_entries && !parameter_suffix_found; i++)
1846 int len_suffix = strlen(suffix_list[i].token);
1848 if (token_suffix_match(token, suffix_list[i].token, -len_suffix))
1849 parameter_suffix_found = TRUE;
1853 if (IS_PARENT_PROCESS(audio.mixer_pid))
1855 if (parameter_suffix_found)
1856 printf("---> skipping token '%s' (parameter token)\n", token);
1858 printf("---> examining token '%s': search prefix ...\n", token);
1862 if (parameter_suffix_found)
1865 /* ---------- step 1: search for matching base prefix ---------- */
1868 for (i=0; i<num_base_prefixes && !base_prefix_found; i++)
1870 char *base_prefix = base_prefixes[i];
1871 int len_base_prefix = strlen(base_prefix);
1872 boolean ext1_suffix_found = FALSE;
1874 base_prefix_found = token_suffix_match(token, base_prefix, start_pos);
1876 if (!base_prefix_found)
1879 if (start_pos + len_base_prefix == len_token) /* exact match */
1881 add_dynamic_file_list_entry(dynamic_file_list,
1882 num_dynamic_file_list_entries,
1885 num_suffix_list_entries,
1887 add_property_mapping(property_mapping,
1888 num_property_mapping_entries,
1890 current_summarized_file_list_entry);
1895 if (IS_PARENT_PROCESS(audio.mixer_pid))
1896 printf("---> examining token '%s': search 1st suffix ...\n", token);
1899 /* ---------- step 2: search for matching first suffix ---------- */
1901 start_pos += len_base_prefix;
1902 for (j=0; j<num_ext1_suffixes && !ext1_suffix_found; j++)
1904 char *ext1_suffix = ext1_suffixes[j];
1905 int len_ext1_suffix = strlen(ext1_suffix);
1906 boolean ext2_suffix_found = FALSE;
1908 ext1_suffix_found = token_suffix_match(token, ext1_suffix, start_pos);
1910 if (!ext1_suffix_found)
1913 if (start_pos + len_ext1_suffix == len_token) /* exact match */
1915 add_dynamic_file_list_entry(dynamic_file_list,
1916 num_dynamic_file_list_entries,
1919 num_suffix_list_entries,
1921 add_property_mapping(property_mapping,
1922 num_property_mapping_entries,
1924 current_summarized_file_list_entry);
1929 if (IS_PARENT_PROCESS(audio.mixer_pid))
1930 printf("---> examining token '%s': search 2nd suffix ...\n", token);
1933 /* ---------- step 3: search for matching second suffix ---------- */
1935 start_pos += len_ext1_suffix;
1936 for (k=0; k<num_ext2_suffixes && !ext2_suffix_found; k++)
1938 char *ext2_suffix = ext2_suffixes[k];
1939 int len_ext2_suffix = strlen(ext2_suffix);
1941 ext2_suffix_found = token_suffix_match(token, ext2_suffix,start_pos);
1943 if (!ext2_suffix_found)
1946 if (start_pos + len_ext2_suffix == len_token) /* exact match */
1948 add_dynamic_file_list_entry(dynamic_file_list,
1949 num_dynamic_file_list_entries,
1952 num_suffix_list_entries,
1954 add_property_mapping(property_mapping,
1955 num_property_mapping_entries,
1957 current_summarized_file_list_entry);
1965 if (artwork_info->num_dynamic_file_list_entries > 0)
1967 artwork_info->dynamic_artwork_list =
1968 checked_calloc(artwork_info->num_dynamic_file_list_entries *
1969 artwork_info->sizeof_artwork_list_entry);
1972 if (extra_file_list != NULL &&
1973 options.verbose && IS_PARENT_PROCESS(audio.mixer_pid))
1975 boolean dynamic_tokens_found = FALSE;
1976 boolean unknown_tokens_found = FALSE;
1978 for (list = extra_file_list; list != NULL; list = list->next)
1980 if (strcmp(list->value, known_token_value) == 0)
1981 dynamic_tokens_found = TRUE;
1983 unknown_tokens_found = TRUE;
1987 if (dynamic_tokens_found)
1989 Error(ERR_RETURN_LINE, "-");
1990 Error(ERR_RETURN, "dynamic token(s) found:");
1992 for (list = extra_file_list; list != NULL; list = list->next)
1993 if (strcmp(list->value, known_token_value) == 0)
1994 Error(ERR_RETURN, "- dynamic token: '%s'", list->token);
1996 Error(ERR_RETURN_LINE, "-");
2000 if (unknown_tokens_found)
2002 Error(ERR_RETURN_LINE, "-");
2003 Error(ERR_RETURN, "warning: unknown token(s) found in config file:");
2004 Error(ERR_RETURN, "- config file: '%s'", filename);
2006 for (list = extra_file_list; list != NULL; list = list->next)
2007 if (strcmp(list->value, known_token_value) != 0)
2008 Error(ERR_RETURN, "- unknown token: '%s'", list->token);
2010 Error(ERR_RETURN_LINE, "-");
2014 freeSetupFileList(extra_file_list);
2017 for (i=0; i<num_file_list_entries; i++)
2019 printf("'%s' ", file_list[i].token);
2020 if (file_list[i].filename)
2021 printf("-> '%s'\n", file_list[i].filename);
2023 printf("-> UNDEFINED [-> '%s']\n", file_list[i].default_filename);
2028 static void deleteArtworkListEntry(struct ArtworkListInfo *artwork_info,
2029 struct ListNodeInfo **listnode)
2033 char *filename = (*listnode)->source_filename;
2036 printf("[decrementing reference counter of artwork '%s']\n", filename);
2039 if (--(*listnode)->num_references <= 0)
2042 printf("[deleting artwork '%s']\n", filename);
2045 deleteNodeFromList(&artwork_info->content_list, filename,
2046 artwork_info->free_artwork);
2053 static void replaceArtworkListEntry(struct ArtworkListInfo *artwork_info,
2054 struct ListNodeInfo **listnode,
2059 "Loading graphics:",
2065 char *filename = getCustomArtworkFilename(basename, artwork_info->type);
2067 if (filename == NULL)
2069 int error_mode = ERR_WARN;
2071 /* we can get away without sounds and music, but not without graphics */
2072 if (*listnode == NULL && artwork_info->type == ARTWORK_TYPE_GRAPHICS)
2073 error_mode = ERR_EXIT;
2075 Error(error_mode, "cannot find artwork file '%s'", basename);
2079 /* check if the old and the new artwork file are the same */
2080 if (*listnode && strcmp((*listnode)->source_filename, filename) == 0)
2082 /* The old and new artwork are the same (have the same filename and path).
2083 This usually means that this artwork does not exist in this artwork set
2084 and a fallback to the existing artwork is done. */
2087 printf("[artwork '%s' already exists (same list entry)]\n", filename);
2093 /* delete existing artwork file entry */
2094 deleteArtworkListEntry(artwork_info, listnode);
2096 /* check if the new artwork file already exists in the list of artworks */
2097 if ((node = getNodeFromKey(artwork_info->content_list, filename)) != NULL)
2100 printf("[artwork '%s' already exists (other list entry)]\n", filename);
2103 *listnode = (struct ListNodeInfo *)node->content;
2104 (*listnode)->num_references++;
2109 DrawInitText(init_text[artwork_info->type], 120, FC_GREEN);
2110 DrawInitText(basename, 150, FC_YELLOW);
2112 if ((*listnode = artwork_info->load_artwork(filename)) != NULL)
2115 printf("[adding new artwork '%s']\n", filename);
2118 (*listnode)->num_references = 1;
2119 addNodeToList(&artwork_info->content_list, (*listnode)->source_filename,
2124 int error_mode = ERR_WARN;
2126 /* we can get away without sounds and music, but not without graphics */
2127 if (artwork_info->type == ARTWORK_TYPE_GRAPHICS)
2128 error_mode = ERR_EXIT;
2130 Error(error_mode, "cannot load artwork file '%s'", basename);
2135 static void LoadCustomArtwork(struct ArtworkListInfo *artwork_info,
2136 struct ListNodeInfo **listnode,
2140 printf("GOT CUSTOM ARTWORK FILE '%s'\n", filename);
2143 if (strcmp(basename, UNDEFINED_FILENAME) == 0)
2145 deleteArtworkListEntry(artwork_info, listnode);
2149 replaceArtworkListEntry(artwork_info, listnode, basename);
2152 static void LoadArtworkToList(struct ArtworkListInfo *artwork_info,
2153 struct ListNodeInfo **listnode,
2154 char *basename, int list_pos)
2157 if (artwork_info->artwork_list == NULL ||
2158 list_pos >= artwork_info->num_file_list_entries)
2163 printf("loading artwork '%s' ... [%d]\n",
2164 basename, getNumNodes(artwork_info->content_list));
2168 LoadCustomArtwork(artwork_info, listnode, basename);
2170 LoadCustomArtwork(artwork_info, &artwork_info->artwork_list[list_pos],
2175 printf("loading artwork '%s' done [%d]\n",
2176 basename, getNumNodes(artwork_info->content_list));
2180 void ReloadCustomArtworkList(struct ArtworkListInfo *artwork_info)
2182 struct FileInfo *file_list = artwork_info->file_list;
2183 struct FileInfo *dynamic_file_list = artwork_info->dynamic_file_list;
2184 int num_file_list_entries = artwork_info->num_file_list_entries;
2185 int num_dynamic_file_list_entries =
2186 artwork_info->num_dynamic_file_list_entries;
2190 printf("DEBUG: reloading %d static artwork files ...\n",
2191 num_file_list_entries);
2192 printf("DEBUG: reloading %d dynamic artwork files ...\n",
2193 num_dynamic_file_list_entries);
2196 for(i=0; i<num_file_list_entries; i++)
2197 LoadArtworkToList(artwork_info, &artwork_info->artwork_list[i],
2198 file_list[i].filename, i);
2200 for(i=0; i<num_dynamic_file_list_entries; i++)
2201 LoadArtworkToList(artwork_info, &artwork_info->dynamic_artwork_list[i],
2202 dynamic_file_list[i].filename, i);
2205 dumpList(artwork_info->content_list);
2209 static void FreeCustomArtworkList(struct ArtworkListInfo *artwork_info,
2210 struct ListNodeInfo ***list,
2211 int *num_list_entries)
2218 for(i=0; i<*num_list_entries; i++)
2219 deleteArtworkListEntry(artwork_info, &(*list)[i]);
2223 *num_list_entries = 0;
2226 void FreeCustomArtworkLists(struct ArtworkListInfo *artwork_info)
2228 if (artwork_info == NULL)
2232 printf("%s: FREEING ARTWORK ...\n",
2233 IS_CHILD_PROCESS(audio.mixer_pid) ? "CHILD" : "PARENT");
2236 FreeCustomArtworkList(artwork_info, &artwork_info->artwork_list,
2237 &artwork_info->num_file_list_entries);
2239 FreeCustomArtworkList(artwork_info, &artwork_info->dynamic_artwork_list,
2240 &artwork_info->num_dynamic_file_list_entries);
2243 printf("%s: FREEING ARTWORK -- DONE\n",
2244 IS_CHILD_PROCESS(audio.mixer_pid) ? "CHILD" : "PARENT");
2249 /* ------------------------------------------------------------------------- */
2250 /* functions only needed for non-Unix (non-command-line) systems */
2251 /* (MS-DOS only; SDL/Windows creates files "stdout.txt" and "stderr.txt") */
2252 /* ------------------------------------------------------------------------- */
2254 #if defined(PLATFORM_MSDOS)
2256 #define ERROR_FILENAME "stderr.txt"
2258 void initErrorFile()
2260 unlink(ERROR_FILENAME);
2263 FILE *openErrorFile()
2265 return fopen(ERROR_FILENAME, MODE_APPEND);
2268 void dumpErrorFile()
2270 FILE *error_file = fopen(ERROR_FILENAME, MODE_READ);
2272 if (error_file != NULL)
2274 while (!feof(error_file))
2275 fputc(fgetc(error_file), stderr);
2283 /* ------------------------------------------------------------------------- */
2284 /* the following is only for debugging purpose and normally not used */
2285 /* ------------------------------------------------------------------------- */
2287 #define DEBUG_NUM_TIMESTAMPS 3
2289 void debug_print_timestamp(int counter_nr, char *message)
2291 static long counter[DEBUG_NUM_TIMESTAMPS][2];
2293 if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
2294 Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
2296 counter[counter_nr][0] = Counter();
2299 printf("%s %.2f seconds\n", message,
2300 (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
2302 counter[counter_nr][1] = Counter();