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++);
478 void setString(char **old_value, char *new_value)
480 if (*old_value != NULL)
483 *old_value = getStringCopy(new_value);
487 /* ------------------------------------------------------------------------- */
488 /* command line option handling functions */
489 /* ------------------------------------------------------------------------- */
491 static void printUsage()
494 "Usage: %s [OPTION]... [HOSTNAME [PORT]]\n"
497 " -d, --display HOSTNAME[:SCREEN] specify X server display\n"
498 " -b, --basepath DIRECTORY alternative base DIRECTORY\n"
499 " -l, --level DIRECTORY alternative level DIRECTORY\n"
500 " -g, --graphics DIRECTORY alternative graphics DIRECTORY\n"
501 " -s, --sounds DIRECTORY alternative sounds DIRECTORY\n"
502 " -m, --music DIRECTORY alternative music DIRECTORY\n"
503 " -n, --network network multiplayer game\n"
504 " --serveronly only start network server\n"
505 " -v, --verbose verbose mode\n"
506 " --debug display debugging information\n"
507 " -e, --execute COMMAND execute batch COMMAND:\n"
509 "Valid commands for '--execute' option:\n"
510 " \"print graphicsinfo.conf\" print default graphics config\n"
511 " \"print soundsinfo.conf\" print default sounds config\n"
512 " \"print musicinfo.conf\" print default music config\n"
513 " \"dump level FILE\" dump level data from FILE\n"
514 " \"dump tape FILE\" dump tape data from FILE\n"
515 " \"autoplay LEVELDIR\" play level tapes for LEVELDIR\n"
517 program.command_basename);
520 void GetOptions(char *argv[])
522 char **options_left = &argv[1];
524 /* initialize global program options */
525 options.display_name = NULL;
526 options.server_host = NULL;
527 options.server_port = 0;
528 options.ro_base_directory = RO_BASE_PATH;
529 options.rw_base_directory = RW_BASE_PATH;
530 options.level_directory = RO_BASE_PATH "/" LEVELS_DIRECTORY;
531 options.graphics_directory = RO_BASE_PATH "/" GRAPHICS_DIRECTORY;
532 options.sounds_directory = RO_BASE_PATH "/" SOUNDS_DIRECTORY;
533 options.music_directory = RO_BASE_PATH "/" MUSIC_DIRECTORY;
534 options.execute_command = NULL;
535 options.serveronly = FALSE;
536 options.network = FALSE;
537 options.verbose = FALSE;
538 options.debug = FALSE;
540 while (*options_left)
542 char option_str[MAX_OPTION_LEN];
543 char *option = options_left[0];
544 char *next_option = options_left[1];
545 char *option_arg = NULL;
546 int option_len = strlen(option);
548 if (option_len >= MAX_OPTION_LEN)
549 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
551 strcpy(option_str, option); /* copy argument into buffer */
554 if (strcmp(option, "--") == 0) /* stop scanning arguments */
557 if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */
560 option_arg = strchr(option, '=');
561 if (option_arg == NULL) /* no '=' in option */
562 option_arg = next_option;
565 *option_arg++ = '\0'; /* cut argument from option */
566 if (*option_arg == '\0') /* no argument after '=' */
567 Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
570 option_len = strlen(option);
572 if (strcmp(option, "-") == 0)
573 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
574 else if (strncmp(option, "-help", option_len) == 0)
580 else if (strncmp(option, "-display", option_len) == 0)
582 if (option_arg == NULL)
583 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
585 options.display_name = option_arg;
586 if (option_arg == next_option)
589 else if (strncmp(option, "-basepath", option_len) == 0)
591 if (option_arg == NULL)
592 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
594 /* this should be extended to separate options for ro and rw data */
595 options.ro_base_directory = option_arg;
596 options.rw_base_directory = option_arg;
597 if (option_arg == next_option)
600 /* adjust path for level directory accordingly */
601 options.level_directory =
602 getPath2(options.ro_base_directory, LEVELS_DIRECTORY);
604 else if (strncmp(option, "-levels", option_len) == 0)
606 if (option_arg == NULL)
607 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
609 options.level_directory = option_arg;
610 if (option_arg == next_option)
613 else if (strncmp(option, "-graphics", option_len) == 0)
615 if (option_arg == NULL)
616 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
618 options.graphics_directory = option_arg;
619 if (option_arg == next_option)
622 else if (strncmp(option, "-sounds", option_len) == 0)
624 if (option_arg == NULL)
625 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
627 options.sounds_directory = option_arg;
628 if (option_arg == next_option)
631 else if (strncmp(option, "-music", option_len) == 0)
633 if (option_arg == NULL)
634 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
636 options.music_directory = option_arg;
637 if (option_arg == next_option)
640 else if (strncmp(option, "-network", option_len) == 0)
642 options.network = TRUE;
644 else if (strncmp(option, "-serveronly", option_len) == 0)
646 options.serveronly = TRUE;
648 else if (strncmp(option, "-verbose", option_len) == 0)
650 options.verbose = TRUE;
652 else if (strncmp(option, "-debug", option_len) == 0)
654 options.debug = TRUE;
656 else if (strncmp(option, "-execute", option_len) == 0)
658 if (option_arg == NULL)
659 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
661 options.execute_command = option_arg;
662 if (option_arg == next_option)
665 else if (*option == '-')
667 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
669 else if (options.server_host == NULL)
671 options.server_host = *options_left;
673 else if (options.server_port == 0)
675 options.server_port = atoi(*options_left);
676 if (options.server_port < 1024)
677 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
680 Error(ERR_EXIT_HELP, "too many arguments");
687 /* ------------------------------------------------------------------------- */
688 /* error handling functions */
689 /* ------------------------------------------------------------------------- */
691 /* used by SetError() and GetError() to store internal error messages */
692 static char internal_error[1024]; /* this is bad */
694 void SetError(char *format, ...)
698 va_start(ap, format);
699 vsprintf(internal_error, format, ap);
705 return internal_error;
708 void Error(int mode, char *format, ...)
710 static boolean last_line_was_separator = FALSE;
711 char *process_name = "";
712 FILE *error = stderr;
713 char *newline = "\n";
715 /* display warnings only when running in verbose mode */
716 if (mode & ERR_WARN && !options.verbose)
719 if (mode == ERR_RETURN_LINE)
721 if (!last_line_was_separator)
722 fprintf_line(error, format, 79);
724 last_line_was_separator = TRUE;
729 last_line_was_separator = FALSE;
731 #if defined(PLATFORM_MSDOS)
734 if ((error = openErrorFile()) == NULL)
736 printf("Cannot write to error output file!%s", newline);
737 program.exit_function(1);
741 if (mode & ERR_SOUND_SERVER)
742 process_name = " sound server";
743 else if (mode & ERR_NETWORK_SERVER)
744 process_name = " network server";
745 else if (mode & ERR_NETWORK_CLIENT)
746 process_name = " network client **";
752 fprintf(error, "%s%s: ", program.command_basename, process_name);
755 fprintf(error, "warning: ");
757 va_start(ap, format);
758 vfprintf(error, format, ap);
761 fprintf(error, "%s", newline);
765 fprintf(error, "%s: Try option '--help' for more information.%s",
766 program.command_basename, newline);
769 fprintf(error, "%s%s: aborting%s",
770 program.command_basename, process_name, newline);
777 if (mode & ERR_FROM_SERVER)
778 exit(1); /* child process: normal exit */
780 program.exit_function(1); /* main process: clean up stuff */
785 /* ------------------------------------------------------------------------- */
786 /* memory allocation functions */
787 /* ------------------------------------------------------------------------- */
789 void *checked_malloc(unsigned long size)
796 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
801 void *checked_calloc(unsigned long size)
805 ptr = calloc(1, size);
808 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
813 void *checked_realloc(void *ptr, unsigned long size)
815 ptr = realloc(ptr, size);
818 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
824 /* ------------------------------------------------------------------------- */
825 /* various helper functions */
826 /* ------------------------------------------------------------------------- */
828 inline void swap_numbers(int *i1, int *i2)
836 inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2)
848 short getFile16BitInteger(FILE *file, int byte_order)
850 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
851 return ((fgetc(file) << 8) |
853 else /* BYTE_ORDER_LITTLE_ENDIAN */
854 return ((fgetc(file) << 0) |
858 void putFile16BitInteger(FILE *file, short value, int byte_order)
860 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
862 fputc((value >> 8) & 0xff, file);
863 fputc((value >> 0) & 0xff, file);
865 else /* BYTE_ORDER_LITTLE_ENDIAN */
867 fputc((value >> 0) & 0xff, file);
868 fputc((value >> 8) & 0xff, file);
872 int getFile32BitInteger(FILE *file, int byte_order)
874 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
875 return ((fgetc(file) << 24) |
876 (fgetc(file) << 16) |
879 else /* BYTE_ORDER_LITTLE_ENDIAN */
880 return ((fgetc(file) << 0) |
882 (fgetc(file) << 16) |
883 (fgetc(file) << 24));
886 void putFile32BitInteger(FILE *file, int value, int byte_order)
888 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
890 fputc((value >> 24) & 0xff, file);
891 fputc((value >> 16) & 0xff, file);
892 fputc((value >> 8) & 0xff, file);
893 fputc((value >> 0) & 0xff, file);
895 else /* BYTE_ORDER_LITTLE_ENDIAN */
897 fputc((value >> 0) & 0xff, file);
898 fputc((value >> 8) & 0xff, file);
899 fputc((value >> 16) & 0xff, file);
900 fputc((value >> 24) & 0xff, file);
904 boolean getFileChunk(FILE *file, char *chunk_name, int *chunk_size,
907 const int chunk_name_length = 4;
909 /* read chunk name */
910 fgets(chunk_name, chunk_name_length + 1, file);
912 if (chunk_size != NULL)
914 /* read chunk size */
915 *chunk_size = getFile32BitInteger(file, byte_order);
918 return (feof(file) || ferror(file) ? FALSE : TRUE);
921 void putFileChunk(FILE *file, char *chunk_name, int chunk_size,
924 /* write chunk name */
925 fputs(chunk_name, file);
929 /* write chunk size */
930 putFile32BitInteger(file, chunk_size, byte_order);
934 int getFileVersion(FILE *file)
936 int version_major, version_minor, version_patch;
938 version_major = fgetc(file);
939 version_minor = fgetc(file);
940 version_patch = fgetc(file);
941 fgetc(file); /* not used */
943 return VERSION_IDENT(version_major, version_minor, version_patch);
946 void putFileVersion(FILE *file, int version)
948 int version_major = VERSION_MAJOR(version);
949 int version_minor = VERSION_MINOR(version);
950 int version_patch = VERSION_PATCH(version);
952 fputc(version_major, file);
953 fputc(version_minor, file);
954 fputc(version_patch, file);
955 fputc(0, file); /* not used */
958 void ReadUnusedBytesFromFile(FILE *file, unsigned long bytes)
960 while (bytes-- && !feof(file))
964 void WriteUnusedBytesToFile(FILE *file, unsigned long bytes)
971 /* ------------------------------------------------------------------------- */
972 /* functions to translate key identifiers between different format */
973 /* ------------------------------------------------------------------------- */
975 #define TRANSLATE_KEYSYM_TO_KEYNAME 0
976 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
977 #define TRANSLATE_KEYNAME_TO_KEYSYM 2
978 #define TRANSLATE_X11KEYNAME_TO_KEYSYM 3
980 void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
989 /* normal cursor keys */
990 { KSYM_Left, "XK_Left", "cursor left" },
991 { KSYM_Right, "XK_Right", "cursor right" },
992 { KSYM_Up, "XK_Up", "cursor up" },
993 { KSYM_Down, "XK_Down", "cursor down" },
995 /* keypad cursor keys */
997 { KSYM_KP_Left, "XK_KP_Left", "keypad left" },
998 { KSYM_KP_Right, "XK_KP_Right", "keypad right" },
999 { KSYM_KP_Up, "XK_KP_Up", "keypad up" },
1000 { KSYM_KP_Down, "XK_KP_Down", "keypad down" },
1003 /* other keypad keys */
1004 #ifdef KSYM_KP_Enter
1005 { KSYM_KP_Enter, "XK_KP_Enter", "keypad enter" },
1006 { KSYM_KP_Add, "XK_KP_Add", "keypad +" },
1007 { KSYM_KP_Subtract, "XK_KP_Subtract", "keypad -" },
1008 { KSYM_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
1009 { KSYM_KP_Divide, "XK_KP_Divide", "keypad /" },
1010 { KSYM_KP_Separator,"XK_KP_Separator", "keypad ," },
1014 { KSYM_Shift_L, "XK_Shift_L", "left shift" },
1015 { KSYM_Shift_R, "XK_Shift_R", "right shift" },
1016 { KSYM_Control_L, "XK_Control_L", "left control" },
1017 { KSYM_Control_R, "XK_Control_R", "right control" },
1018 { KSYM_Meta_L, "XK_Meta_L", "left meta" },
1019 { KSYM_Meta_R, "XK_Meta_R", "right meta" },
1020 { KSYM_Alt_L, "XK_Alt_L", "left alt" },
1021 { KSYM_Alt_R, "XK_Alt_R", "right alt" },
1022 { KSYM_Super_L, "XK_Super_L", "left super" }, /* Win-L */
1023 { KSYM_Super_R, "XK_Super_R", "right super" }, /* Win-R */
1024 { KSYM_Mode_switch, "XK_Mode_switch", "mode switch" }, /* Alt-R */
1025 { KSYM_Multi_key, "XK_Multi_key", "multi key" }, /* Ctrl-R */
1027 /* some special keys */
1028 { KSYM_BackSpace, "XK_BackSpace", "backspace" },
1029 { KSYM_Delete, "XK_Delete", "delete" },
1030 { KSYM_Insert, "XK_Insert", "insert" },
1031 { KSYM_Tab, "XK_Tab", "tab" },
1032 { KSYM_Home, "XK_Home", "home" },
1033 { KSYM_End, "XK_End", "end" },
1034 { KSYM_Page_Up, "XK_Page_Up", "page up" },
1035 { KSYM_Page_Down, "XK_Page_Down", "page down" },
1036 { KSYM_Menu, "XK_Menu", "menu" }, /* Win-Menu */
1038 /* ASCII 0x20 to 0x40 keys (except numbers) */
1039 { KSYM_space, "XK_space", "space" },
1040 { KSYM_exclam, "XK_exclam", "!" },
1041 { KSYM_quotedbl, "XK_quotedbl", "\"" },
1042 { KSYM_numbersign, "XK_numbersign", "#" },
1043 { KSYM_dollar, "XK_dollar", "$" },
1044 { KSYM_percent, "XK_percent", "%" },
1045 { KSYM_ampersand, "XK_ampersand", "&" },
1046 { KSYM_apostrophe, "XK_apostrophe", "'" },
1047 { KSYM_parenleft, "XK_parenleft", "(" },
1048 { KSYM_parenright, "XK_parenright", ")" },
1049 { KSYM_asterisk, "XK_asterisk", "*" },
1050 { KSYM_plus, "XK_plus", "+" },
1051 { KSYM_comma, "XK_comma", "," },
1052 { KSYM_minus, "XK_minus", "-" },
1053 { KSYM_period, "XK_period", "." },
1054 { KSYM_slash, "XK_slash", "/" },
1055 { KSYM_colon, "XK_colon", ":" },
1056 { KSYM_semicolon, "XK_semicolon", ";" },
1057 { KSYM_less, "XK_less", "<" },
1058 { KSYM_equal, "XK_equal", "=" },
1059 { KSYM_greater, "XK_greater", ">" },
1060 { KSYM_question, "XK_question", "?" },
1061 { KSYM_at, "XK_at", "@" },
1063 /* more ASCII keys */
1064 { KSYM_bracketleft, "XK_bracketleft", "[" },
1065 { KSYM_backslash, "XK_backslash", "backslash" },
1066 { KSYM_bracketright,"XK_bracketright", "]" },
1067 { KSYM_asciicircum, "XK_asciicircum", "circumflex" },
1068 { KSYM_underscore, "XK_underscore", "_" },
1069 { KSYM_grave, "XK_grave", "grave" },
1070 { KSYM_quoteleft, "XK_quoteleft", "quote left" },
1071 { KSYM_braceleft, "XK_braceleft", "brace left" },
1072 { KSYM_bar, "XK_bar", "bar" },
1073 { KSYM_braceright, "XK_braceright", "brace right" },
1074 { KSYM_asciitilde, "XK_asciitilde", "ascii tilde" },
1076 /* special (non-ASCII) keys */
1077 { KSYM_Adiaeresis, "XK_Adiaeresis", "Ä" },
1078 { KSYM_Odiaeresis, "XK_Odiaeresis", "Ö" },
1079 { KSYM_Udiaeresis, "XK_Udiaeresis", "Ü" },
1080 { KSYM_adiaeresis, "XK_adiaeresis", "ä" },
1081 { KSYM_odiaeresis, "XK_odiaeresis", "ö" },
1082 { KSYM_udiaeresis, "XK_udiaeresis", "ü" },
1083 { KSYM_ssharp, "XK_ssharp", "sharp s" },
1085 /* end-of-array identifier */
1091 if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
1093 static char name_buffer[30];
1096 if (key >= KSYM_A && key <= KSYM_Z)
1097 sprintf(name_buffer, "%c", 'A' + (char)(key - KSYM_A));
1098 else if (key >= KSYM_a && key <= KSYM_z)
1099 sprintf(name_buffer, "%c", 'a' + (char)(key - KSYM_a));
1100 else if (key >= KSYM_0 && key <= KSYM_9)
1101 sprintf(name_buffer, "%c", '0' + (char)(key - KSYM_0));
1102 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
1103 sprintf(name_buffer, "keypad %c", '0' + (char)(key - KSYM_KP_0));
1104 else if (key >= KSYM_FKEY_FIRST && key <= KSYM_FKEY_LAST)
1105 sprintf(name_buffer, "function F%d", (int)(key - KSYM_FKEY_FIRST + 1));
1106 else if (key == KSYM_UNDEFINED)
1107 strcpy(name_buffer, "(undefined)");
1114 if (key == translate_key[i].key)
1116 strcpy(name_buffer, translate_key[i].name);
1120 while (translate_key[++i].name);
1122 if (!translate_key[i].name)
1123 strcpy(name_buffer, "(unknown)");
1126 *name = name_buffer;
1128 else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME)
1130 static char name_buffer[30];
1133 if (key >= KSYM_A && key <= KSYM_Z)
1134 sprintf(name_buffer, "XK_%c", 'A' + (char)(key - KSYM_A));
1135 else if (key >= KSYM_a && key <= KSYM_z)
1136 sprintf(name_buffer, "XK_%c", 'a' + (char)(key - KSYM_a));
1137 else if (key >= KSYM_0 && key <= KSYM_9)
1138 sprintf(name_buffer, "XK_%c", '0' + (char)(key - KSYM_0));
1139 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
1140 sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - KSYM_KP_0));
1141 else if (key >= KSYM_FKEY_FIRST && key <= KSYM_FKEY_LAST)
1142 sprintf(name_buffer, "XK_F%d", (int)(key - KSYM_FKEY_FIRST + 1));
1143 else if (key == KSYM_UNDEFINED)
1144 strcpy(name_buffer, "[undefined]");
1151 if (key == translate_key[i].key)
1153 strcpy(name_buffer, translate_key[i].x11name);
1157 while (translate_key[++i].x11name);
1159 if (!translate_key[i].x11name)
1160 sprintf(name_buffer, "0x%04lx", (unsigned long)key);
1163 *x11name = name_buffer;
1165 else if (mode == TRANSLATE_KEYNAME_TO_KEYSYM)
1167 Key key = KSYM_UNDEFINED;
1172 if (strcmp(translate_key[i].name, *name) == 0)
1174 key = translate_key[i].key;
1178 while (translate_key[++i].x11name);
1180 if (key == KSYM_UNDEFINED)
1181 Error(ERR_WARN, "getKeyFromKeyName(): not completely implemented");
1185 else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
1187 Key key = KSYM_UNDEFINED;
1188 char *name_ptr = *x11name;
1190 if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4)
1192 char c = name_ptr[3];
1194 if (c >= 'A' && c <= 'Z')
1195 key = KSYM_A + (Key)(c - 'A');
1196 else if (c >= 'a' && c <= 'z')
1197 key = KSYM_a + (Key)(c - 'a');
1198 else if (c >= '0' && c <= '9')
1199 key = KSYM_0 + (Key)(c - '0');
1201 else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7)
1203 char c = name_ptr[6];
1205 if (c >= '0' && c <= '9')
1206 key = KSYM_0 + (Key)(c - '0');
1208 else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
1210 char c1 = name_ptr[4];
1211 char c2 = name_ptr[5];
1214 if ((c1 >= '0' && c1 <= '9') &&
1215 ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
1216 d = atoi(&name_ptr[4]);
1218 if (d >= 1 && d <= KSYM_NUM_FKEYS)
1219 key = KSYM_F1 + (Key)(d - 1);
1221 else if (strncmp(name_ptr, "XK_", 3) == 0)
1227 if (strcmp(name_ptr, translate_key[i].x11name) == 0)
1229 key = translate_key[i].key;
1233 while (translate_key[++i].x11name);
1235 else if (strncmp(name_ptr, "0x", 2) == 0)
1237 unsigned long value = 0;
1243 char c = *name_ptr++;
1246 if (c >= '0' && c <= '9')
1248 else if (c >= 'a' && c <= 'f')
1249 d = (int)(c - 'a' + 10);
1250 else if (c >= 'A' && c <= 'F')
1251 d = (int)(c - 'A' + 10);
1259 value = value * 16 + d;
1270 char *getKeyNameFromKey(Key key)
1274 translate_keyname(&key, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME);
1278 char *getX11KeyNameFromKey(Key key)
1282 translate_keyname(&key, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME);
1286 Key getKeyFromKeyName(char *name)
1290 translate_keyname(&key, NULL, &name, TRANSLATE_KEYNAME_TO_KEYSYM);
1294 Key getKeyFromX11KeyName(char *x11name)
1298 translate_keyname(&key, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM);
1302 char getCharFromKey(Key key)
1304 char *keyname = getKeyNameFromKey(key);
1307 if (strlen(keyname) == 1)
1308 letter = keyname[0];
1309 else if (strcmp(keyname, "space") == 0)
1311 else if (strcmp(keyname, "circumflex") == 0)
1318 /* ------------------------------------------------------------------------- */
1319 /* functions to translate string identifiers to integer or boolean value */
1320 /* ------------------------------------------------------------------------- */
1322 int get_integer_from_string(char *s)
1324 static char *number_text[][3] =
1326 { "0", "zero", "null", },
1327 { "1", "one", "first" },
1328 { "2", "two", "second" },
1329 { "3", "three", "third" },
1330 { "4", "four", "fourth" },
1331 { "5", "five", "fifth" },
1332 { "6", "six", "sixth" },
1333 { "7", "seven", "seventh" },
1334 { "8", "eight", "eighth" },
1335 { "9", "nine", "ninth" },
1336 { "10", "ten", "tenth" },
1337 { "11", "eleven", "eleventh" },
1338 { "12", "twelve", "twelfth" },
1342 char *s_lower = getStringToLower(s);
1345 for (i=0; i<13; i++)
1347 if (strcmp(s_lower, number_text[i][j]) == 0)
1358 boolean get_boolean_from_string(char *s)
1360 char *s_lower = getStringToLower(s);
1361 boolean result = FALSE;
1363 if (strcmp(s_lower, "true") == 0 ||
1364 strcmp(s_lower, "yes") == 0 ||
1365 strcmp(s_lower, "on") == 0 ||
1366 get_integer_from_string(s) == 1)
1375 /* ------------------------------------------------------------------------- */
1376 /* functions for generic lists */
1377 /* ------------------------------------------------------------------------- */
1379 ListNode *newListNode()
1381 return checked_calloc(sizeof(ListNode));
1384 void addNodeToList(ListNode **node_first, char *key, void *content)
1386 ListNode *node_new = newListNode();
1389 printf("LIST: adding node with key '%s'\n", key);
1392 node_new->key = getStringCopy(key);
1393 node_new->content = content;
1394 node_new->next = *node_first;
1395 *node_first = node_new;
1398 void deleteNodeFromList(ListNode **node_first, char *key,
1399 void (*destructor_function)(void *))
1401 if (node_first == NULL || *node_first == NULL)
1405 printf("[CHECKING LIST KEY '%s' == '%s']\n",
1406 (*node_first)->key, key);
1409 if (strcmp((*node_first)->key, key) == 0)
1412 printf("[DELETING LIST ENTRY]\n");
1415 free((*node_first)->key);
1416 if (destructor_function)
1417 destructor_function((*node_first)->content);
1418 *node_first = (*node_first)->next;
1421 deleteNodeFromList(&(*node_first)->next, key, destructor_function);
1424 ListNode *getNodeFromKey(ListNode *node_first, char *key)
1426 if (node_first == NULL)
1429 if (strcmp(node_first->key, key) == 0)
1432 return getNodeFromKey(node_first->next, key);
1435 int getNumNodes(ListNode *node_first)
1437 return (node_first ? 1 + getNumNodes(node_first->next) : 0);
1440 void dumpList(ListNode *node_first)
1442 ListNode *node = node_first;
1446 printf("['%s' (%d)]\n", node->key,
1447 ((struct ListNodeInfo *)node->content)->num_references);
1451 printf("[%d nodes]\n", getNumNodes(node_first));
1455 /* ------------------------------------------------------------------------- */
1456 /* functions for checking filenames */
1457 /* ------------------------------------------------------------------------- */
1459 boolean FileIsGraphic(char *filename)
1461 if (strlen(filename) > 4 &&
1462 strcmp(&filename[strlen(filename) - 4], ".pcx") == 0)
1468 boolean FileIsSound(char *basename)
1470 if (strlen(basename) > 4 &&
1471 strcmp(&basename[strlen(basename) - 4], ".wav") == 0)
1477 boolean FileIsMusic(char *basename)
1479 /* "music" can be a WAV (loop) file or (if compiled with SDL) a MOD file */
1481 if (FileIsSound(basename))
1484 #if defined(TARGET_SDL)
1485 if (strlen(basename) > 4 &&
1486 (strcmp(&basename[strlen(basename) - 4], ".mod") == 0 ||
1487 strcmp(&basename[strlen(basename) - 4], ".MOD") == 0 ||
1488 strncmp(basename, "mod.", 4) == 0 ||
1489 strncmp(basename, "MOD.", 4) == 0))
1496 boolean FileIsArtworkType(char *basename, int type)
1498 if ((type == TREE_TYPE_GRAPHICS_DIR && FileIsGraphic(basename)) ||
1499 (type == TREE_TYPE_SOUNDS_DIR && FileIsSound(basename)) ||
1500 (type == TREE_TYPE_MUSIC_DIR && FileIsMusic(basename)))
1506 /* ------------------------------------------------------------------------- */
1507 /* functions for loading artwork configuration information */
1508 /* ------------------------------------------------------------------------- */
1510 int get_parameter_value(int type, char *value)
1512 return (strcmp(value, ARG_UNDEFINED) == 0 ? ARG_UNDEFINED_VALUE :
1513 type == TYPE_INTEGER ? get_integer_from_string(value) :
1514 type == TYPE_BOOLEAN ? get_boolean_from_string(value) :
1515 ARG_UNDEFINED_VALUE);
1518 static void FreeCustomArtworkList(struct ArtworkListInfo *,
1519 struct ListNodeInfo ***, int *);
1521 struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list,
1522 struct ConfigInfo *suffix_list,
1523 int num_file_list_entries)
1525 struct FileInfo *file_list;
1526 int num_file_list_entries_found = 0;
1527 int num_suffix_list_entries = 0;
1531 file_list = checked_calloc(num_file_list_entries * sizeof(struct FileInfo));
1533 for (i=0; suffix_list[i].token != NULL; i++)
1534 num_suffix_list_entries++;
1536 /* always start with reliable default values */
1537 for (i=0; i<num_file_list_entries; i++)
1539 file_list[i].token = NULL;
1541 file_list[i].default_filename = NULL;
1542 file_list[i].filename = NULL;
1544 if (num_suffix_list_entries > 0)
1546 int parameter_array_size = num_suffix_list_entries * sizeof(char *);
1548 file_list[i].default_parameter = checked_calloc(parameter_array_size);
1549 file_list[i].parameter = checked_calloc(parameter_array_size);
1551 for (j=0; j<num_suffix_list_entries; j++)
1553 setString(&file_list[i].default_parameter[j], suffix_list[j].value);
1554 setString(&file_list[i].parameter[j], suffix_list[j].value);
1560 for (i=0; config_list[i].token != NULL; i++)
1562 int len_config_token = strlen(config_list[i].token);
1563 int len_config_value = strlen(config_list[i].value);
1564 boolean is_file_entry = TRUE;
1566 for (j=0; suffix_list[j].token != NULL; j++)
1568 int len_suffix = strlen(suffix_list[j].token);
1570 if (len_suffix < len_config_token &&
1571 strcmp(&config_list[i].token[len_config_token - len_suffix],
1572 suffix_list[j].token) == 0)
1574 setString(&file_list[list_pos].default_parameter[j],
1575 config_list[i].value);
1577 is_file_entry = FALSE;
1582 /* the following tokens are no file definitions, but other config tokens */
1583 if (strcmp(config_list[i].token, "global.num_toons") == 0 ||
1584 strcmp(config_list[i].token, "menu.main.hide_static_text") == 0)
1585 is_file_entry = FALSE;
1592 if (list_pos >= num_file_list_entries)
1595 /* simple sanity check if this is really a file definition */
1596 if (strcmp(&config_list[i].value[len_config_value - 4], ".pcx") != 0 &&
1597 strcmp(&config_list[i].value[len_config_value - 4], ".wav") != 0 &&
1598 strcmp(config_list[i].value, UNDEFINED_FILENAME) != 0)
1600 Error(ERR_RETURN, "Configuration directive '%s' -> '%s':",
1601 config_list[i].token, config_list[i].value);
1602 Error(ERR_EXIT, "This seems to be no valid definition -- please fix");
1605 file_list[list_pos].token = config_list[i].token;
1606 file_list[list_pos].default_filename = config_list[i].value;
1610 num_file_list_entries_found = list_pos + 1;
1611 if (num_file_list_entries_found != num_file_list_entries)
1613 Error(ERR_RETURN_LINE, "-");
1614 Error(ERR_RETURN, "inconsistant config list information:");
1615 Error(ERR_RETURN, "- should be: %d (according to 'src/conf_gfx.h')",
1616 num_file_list_entries);
1617 Error(ERR_RETURN, "- found to be: %d (according to 'src/conf_gfx.c')",
1618 num_file_list_entries_found);
1619 Error(ERR_EXIT, "please fix");
1625 static boolean token_suffix_match(char *token, char *suffix, int start_pos)
1627 int len_token = strlen(token);
1628 int len_suffix = strlen(suffix);
1631 if (IS_PARENT_PROCESS())
1632 printf(":::::::::: check '%s' for '%s' ::::::::::\n", token, suffix);
1635 if (start_pos < 0) /* compare suffix from end of string */
1636 start_pos += len_token;
1638 if (start_pos < 0 || start_pos + len_suffix > len_token)
1641 if (strncmp(&token[start_pos], suffix, len_suffix) != 0)
1644 if (token[start_pos + len_suffix] == '\0')
1647 if (token[start_pos + len_suffix] == '.')
1653 #define KNOWN_TOKEN_VALUE "[KNOWN_TOKEN]"
1655 static void read_token_parameters(struct SetupFileList *setup_file_list,
1656 struct ConfigInfo *suffix_list,
1657 struct FileInfo *file_list_entry)
1659 /* check for config token that is the base token without any suffixes */
1660 char *filename = getTokenValue(setup_file_list, file_list_entry->token);
1661 char *known_token_value = KNOWN_TOKEN_VALUE;
1664 if (filename != NULL)
1666 setString(&file_list_entry->filename, filename);
1668 /* when file definition found, set all parameters to default values */
1669 for (i=0; suffix_list[i].token != NULL; i++)
1670 setString(&file_list_entry->parameter[i], suffix_list[i].value);
1672 file_list_entry->redefined = TRUE;
1674 /* mark config file token as well known from default config */
1675 setTokenValue(setup_file_list, file_list_entry->token, known_token_value);
1678 setString(&file_list_entry->filename, file_list_entry->default_filename);
1680 /* check for config tokens that can be build by base token and suffixes */
1681 for (i=0; suffix_list[i].token != NULL; i++)
1683 char *token = getStringCat2(file_list_entry->token, suffix_list[i].token);
1684 char *value = getTokenValue(setup_file_list, token);
1688 setString(&file_list_entry->parameter[i], value);
1690 /* mark config file token as well known from default config */
1691 setTokenValue(setup_file_list, token, known_token_value);
1698 static void add_dynamic_file_list_entry(struct FileInfo **list,
1699 int *num_list_entries,
1700 struct SetupFileList *extra_file_list,
1701 struct ConfigInfo *suffix_list,
1702 int num_suffix_list_entries,
1705 struct FileInfo *new_list_entry;
1706 int parameter_array_size = num_suffix_list_entries * sizeof(char *);
1709 if (IS_PARENT_PROCESS())
1710 printf("===> found dynamic definition '%s'\n", token);
1713 (*num_list_entries)++;
1714 *list = checked_realloc(*list, *num_list_entries * sizeof(struct FileInfo));
1715 new_list_entry = &(*list)[*num_list_entries - 1];
1717 new_list_entry->token = getStringCopy(token);
1718 new_list_entry->filename = NULL;
1719 new_list_entry->parameter = checked_calloc(parameter_array_size);
1721 read_token_parameters(extra_file_list, suffix_list, new_list_entry);
1724 static void add_property_mapping(struct PropertyMapping **list,
1725 int *num_list_entries,
1726 int base_index, int ext1_index,
1727 int ext2_index, int ext3_index,
1730 struct PropertyMapping *new_list_entry;
1732 (*num_list_entries)++;
1733 *list = checked_realloc(*list,
1734 *num_list_entries * sizeof(struct PropertyMapping));
1735 new_list_entry = &(*list)[*num_list_entries - 1];
1737 new_list_entry->base_index = base_index;
1738 new_list_entry->ext1_index = ext1_index;
1739 new_list_entry->ext2_index = ext2_index;
1740 new_list_entry->ext3_index = ext3_index;
1742 new_list_entry->artwork_index = artwork_index;
1745 void LoadArtworkConfig(struct ArtworkListInfo *artwork_info)
1747 struct FileInfo *file_list = artwork_info->file_list;
1748 struct ConfigInfo *suffix_list = artwork_info->suffix_list;
1749 char **base_prefixes = artwork_info->base_prefixes;
1750 char **ext1_suffixes = artwork_info->ext1_suffixes;
1751 char **ext2_suffixes = artwork_info->ext2_suffixes;
1752 char **ext3_suffixes = artwork_info->ext3_suffixes;
1753 char **ignore_tokens = artwork_info->ignore_tokens;
1754 int num_file_list_entries = artwork_info->num_file_list_entries;
1755 int num_suffix_list_entries = artwork_info->num_suffix_list_entries;
1756 int num_base_prefixes = artwork_info->num_base_prefixes;
1757 int num_ext1_suffixes = artwork_info->num_ext1_suffixes;
1758 int num_ext2_suffixes = artwork_info->num_ext2_suffixes;
1759 int num_ext3_suffixes = artwork_info->num_ext3_suffixes;
1760 int num_ignore_tokens = artwork_info->num_ignore_tokens;
1761 char *filename = getCustomArtworkConfigFilename(artwork_info->type);
1762 struct SetupFileList *setup_file_list;
1763 struct SetupFileList *extra_file_list = NULL;
1764 struct SetupFileList *list;
1765 char *known_token_value = KNOWN_TOKEN_VALUE;
1769 printf("GOT CUSTOM ARTWORK CONFIG FILE '%s'\n", filename);
1772 /* always start with reliable default values */
1773 for (i=0; i<num_file_list_entries; i++)
1775 setString(&file_list[i].filename, file_list[i].default_filename);
1777 for (j=0; j<num_suffix_list_entries; j++)
1778 setString(&file_list[i].parameter[j], file_list[i].default_parameter[j]);
1780 file_list[i].redefined = FALSE;
1783 /* free previous dynamic artwork file array */
1784 if (artwork_info->dynamic_file_list != NULL)
1786 for (i=0; i<artwork_info->num_dynamic_file_list_entries; i++)
1788 free(artwork_info->dynamic_file_list[i].token);
1789 free(artwork_info->dynamic_file_list[i].filename);
1790 free(artwork_info->dynamic_file_list[i].parameter);
1793 free(artwork_info->dynamic_file_list);
1794 artwork_info->dynamic_file_list = NULL;
1796 FreeCustomArtworkList(artwork_info, &artwork_info->dynamic_artwork_list,
1797 &artwork_info->num_dynamic_file_list_entries);
1800 /* free previous property mapping */
1801 if (artwork_info->property_mapping != NULL)
1803 free(artwork_info->property_mapping);
1805 artwork_info->property_mapping = NULL;
1806 artwork_info->num_property_mapping_entries = 0;
1809 if (filename == NULL)
1812 if ((setup_file_list = loadSetupFileList(filename)) == NULL)
1815 /* read parameters for all known config file tokens */
1816 for (i=0; i<num_file_list_entries; i++)
1817 read_token_parameters(setup_file_list, suffix_list, &file_list[i]);
1819 /* set all tokens that can be ignored here to "known" keyword */
1820 for (i=0; i < num_ignore_tokens; i++)
1821 setTokenValue(setup_file_list, ignore_tokens[i], known_token_value);
1823 /* copy all unknown config file tokens to extra config list */
1824 for (list = setup_file_list; list != NULL; list = list->next)
1826 if (strcmp(list->value, known_token_value) != 0)
1828 if (extra_file_list == NULL)
1829 extra_file_list = newSetupFileList(list->token, list->value);
1831 setTokenValue(extra_file_list, list->token, list->value);
1835 /* at this point, we do not need the config file list anymore -- free it */
1836 freeSetupFileList(setup_file_list);
1838 /* now try to determine valid, dynamically defined config tokens */
1840 for (list = extra_file_list; list != NULL; list = list->next)
1842 struct FileInfo **dynamic_file_list =
1843 &artwork_info->dynamic_file_list;
1844 int *num_dynamic_file_list_entries =
1845 &artwork_info->num_dynamic_file_list_entries;
1846 struct PropertyMapping **property_mapping =
1847 &artwork_info->property_mapping;
1848 int *num_property_mapping_entries =
1849 &artwork_info->num_property_mapping_entries;
1850 int current_summarized_file_list_entry =
1851 artwork_info->num_file_list_entries +
1852 artwork_info->num_dynamic_file_list_entries;
1853 char *token = list->token;
1854 int len_token = strlen(token);
1856 boolean base_prefix_found = FALSE;
1857 boolean parameter_suffix_found = FALSE;
1859 /* skip all parameter definitions (handled by read_token_parameters()) */
1860 for (i=0; i < num_suffix_list_entries && !parameter_suffix_found; i++)
1862 int len_suffix = strlen(suffix_list[i].token);
1864 if (token_suffix_match(token, suffix_list[i].token, -len_suffix))
1865 parameter_suffix_found = TRUE;
1869 if (IS_PARENT_PROCESS())
1871 if (parameter_suffix_found)
1872 printf("---> skipping token '%s' (parameter token)\n", token);
1874 printf("---> examining token '%s': search prefix ...\n", token);
1878 if (parameter_suffix_found)
1881 /* ---------- step 0: search for matching base prefix ---------- */
1884 for (i=0; i<num_base_prefixes && !base_prefix_found; i++)
1886 char *base_prefix = base_prefixes[i];
1887 int len_base_prefix = strlen(base_prefix);
1888 boolean ext1_suffix_found = FALSE;
1889 boolean ext2_suffix_found = FALSE;
1890 boolean ext3_suffix_found = FALSE;
1891 boolean exact_match = FALSE;
1892 int base_index = -1;
1893 int ext1_index = -1;
1894 int ext2_index = -1;
1895 int ext3_index = -1;
1897 base_prefix_found = token_suffix_match(token, base_prefix, start_pos);
1899 if (!base_prefix_found)
1904 if (start_pos + len_base_prefix == len_token) /* exact match */
1908 add_dynamic_file_list_entry(dynamic_file_list,
1909 num_dynamic_file_list_entries,
1912 num_suffix_list_entries,
1914 add_property_mapping(property_mapping,
1915 num_property_mapping_entries,
1916 base_index, -1, -1, -1,
1917 current_summarized_file_list_entry);
1922 if (IS_PARENT_PROCESS())
1923 printf("---> examining token '%s': search 1st suffix ...\n", token);
1926 /* ---------- step 1: search for matching first suffix ---------- */
1928 start_pos += len_base_prefix;
1929 for (j=0; j<num_ext1_suffixes && !ext1_suffix_found; j++)
1931 char *ext1_suffix = ext1_suffixes[j];
1932 int len_ext1_suffix = strlen(ext1_suffix);
1934 ext1_suffix_found = token_suffix_match(token, ext1_suffix, start_pos);
1936 if (!ext1_suffix_found)
1941 if (start_pos + len_ext1_suffix == len_token) /* exact match */
1945 add_dynamic_file_list_entry(dynamic_file_list,
1946 num_dynamic_file_list_entries,
1949 num_suffix_list_entries,
1951 add_property_mapping(property_mapping,
1952 num_property_mapping_entries,
1953 base_index, ext1_index, -1, -1,
1954 current_summarized_file_list_entry);
1958 start_pos += len_ext1_suffix;
1965 if (IS_PARENT_PROCESS())
1966 printf("---> examining token '%s': search 2nd suffix ...\n", token);
1969 /* ---------- step 2: search for matching second suffix ---------- */
1971 for (k=0; k<num_ext2_suffixes && !ext2_suffix_found; k++)
1973 char *ext2_suffix = ext2_suffixes[k];
1974 int len_ext2_suffix = strlen(ext2_suffix);
1976 ext2_suffix_found = token_suffix_match(token, ext2_suffix,start_pos);
1978 if (!ext2_suffix_found)
1983 if (start_pos + len_ext2_suffix == len_token) /* exact match */
1987 add_dynamic_file_list_entry(dynamic_file_list,
1988 num_dynamic_file_list_entries,
1991 num_suffix_list_entries,
1993 add_property_mapping(property_mapping,
1994 num_property_mapping_entries,
1995 base_index, ext1_index, ext2_index, -1,
1996 current_summarized_file_list_entry);
2000 start_pos += len_ext2_suffix;
2007 if (IS_PARENT_PROCESS())
2008 printf("---> examining token '%s': search 3rd suffix ...\n",token);
2011 /* ---------- step 3: search for matching third suffix ---------- */
2013 for (l=0; l<num_ext3_suffixes && !ext3_suffix_found; l++)
2015 char *ext3_suffix = ext3_suffixes[l];
2016 int len_ext3_suffix = strlen(ext3_suffix);
2018 ext3_suffix_found =token_suffix_match(token,ext3_suffix,start_pos);
2020 if (!ext3_suffix_found)
2025 if (start_pos + len_ext3_suffix == len_token) /* exact match */
2029 add_dynamic_file_list_entry(dynamic_file_list,
2030 num_dynamic_file_list_entries,
2033 num_suffix_list_entries,
2035 add_property_mapping(property_mapping,
2036 num_property_mapping_entries,
2037 base_index, ext1_index, ext2_index, ext3_index,
2038 current_summarized_file_list_entry);
2045 if (artwork_info->num_dynamic_file_list_entries > 0)
2047 artwork_info->dynamic_artwork_list =
2048 checked_calloc(artwork_info->num_dynamic_file_list_entries *
2049 artwork_info->sizeof_artwork_list_entry);
2052 if (extra_file_list != NULL && options.verbose && IS_PARENT_PROCESS())
2054 boolean dynamic_tokens_found = FALSE;
2055 boolean unknown_tokens_found = FALSE;
2057 for (list = extra_file_list; list != NULL; list = list->next)
2059 if (strcmp(list->value, known_token_value) == 0)
2060 dynamic_tokens_found = TRUE;
2062 unknown_tokens_found = TRUE;
2066 if (dynamic_tokens_found)
2068 Error(ERR_RETURN_LINE, "-");
2069 Error(ERR_RETURN, "dynamic token(s) found:");
2071 for (list = extra_file_list; list != NULL; list = list->next)
2072 if (strcmp(list->value, known_token_value) == 0)
2073 Error(ERR_RETURN, "- dynamic token: '%s'", list->token);
2075 Error(ERR_RETURN_LINE, "-");
2079 if (unknown_tokens_found)
2081 Error(ERR_RETURN_LINE, "-");
2082 Error(ERR_RETURN, "warning: unknown token(s) found in config file:");
2083 Error(ERR_RETURN, "- config file: '%s'", filename);
2085 for (list = extra_file_list; list != NULL; list = list->next)
2086 if (strcmp(list->value, known_token_value) != 0)
2087 Error(ERR_RETURN, "- unknown token: '%s'", list->token);
2089 Error(ERR_RETURN_LINE, "-");
2093 freeSetupFileList(extra_file_list);
2096 for (i=0; i<num_file_list_entries; i++)
2098 printf("'%s' ", file_list[i].token);
2099 if (file_list[i].filename)
2100 printf("-> '%s'\n", file_list[i].filename);
2102 printf("-> UNDEFINED [-> '%s']\n", file_list[i].default_filename);
2107 static void deleteArtworkListEntry(struct ArtworkListInfo *artwork_info,
2108 struct ListNodeInfo **listnode)
2112 char *filename = (*listnode)->source_filename;
2115 printf("[decrementing reference counter of artwork '%s']\n", filename);
2118 if (--(*listnode)->num_references <= 0)
2121 printf("[deleting artwork '%s']\n", filename);
2124 deleteNodeFromList(&artwork_info->content_list, filename,
2125 artwork_info->free_artwork);
2132 static void replaceArtworkListEntry(struct ArtworkListInfo *artwork_info,
2133 struct ListNodeInfo **listnode,
2138 "Loading graphics:",
2144 char *filename = getCustomArtworkFilename(basename, artwork_info->type);
2146 if (filename == NULL)
2148 int error_mode = ERR_WARN;
2150 /* we can get away without sounds and music, but not without graphics */
2151 if (*listnode == NULL && artwork_info->type == ARTWORK_TYPE_GRAPHICS)
2152 error_mode = ERR_EXIT;
2154 Error(error_mode, "cannot find artwork file '%s'", basename);
2158 /* check if the old and the new artwork file are the same */
2159 if (*listnode && strcmp((*listnode)->source_filename, filename) == 0)
2161 /* The old and new artwork are the same (have the same filename and path).
2162 This usually means that this artwork does not exist in this artwork set
2163 and a fallback to the existing artwork is done. */
2166 printf("[artwork '%s' already exists (same list entry)]\n", filename);
2172 /* delete existing artwork file entry */
2173 deleteArtworkListEntry(artwork_info, listnode);
2175 /* check if the new artwork file already exists in the list of artworks */
2176 if ((node = getNodeFromKey(artwork_info->content_list, filename)) != NULL)
2179 printf("[artwork '%s' already exists (other list entry)]\n", filename);
2182 *listnode = (struct ListNodeInfo *)node->content;
2183 (*listnode)->num_references++;
2188 DrawInitText(init_text[artwork_info->type], 120, FC_GREEN);
2189 DrawInitText(basename, 150, FC_YELLOW);
2191 if ((*listnode = artwork_info->load_artwork(filename)) != NULL)
2194 printf("[adding new artwork '%s']\n", filename);
2197 (*listnode)->num_references = 1;
2198 addNodeToList(&artwork_info->content_list, (*listnode)->source_filename,
2203 int error_mode = ERR_WARN;
2205 /* we can get away without sounds and music, but not without graphics */
2206 if (artwork_info->type == ARTWORK_TYPE_GRAPHICS)
2207 error_mode = ERR_EXIT;
2209 Error(error_mode, "cannot load artwork file '%s'", basename);
2214 static void LoadCustomArtwork(struct ArtworkListInfo *artwork_info,
2215 struct ListNodeInfo **listnode,
2219 printf("GOT CUSTOM ARTWORK FILE '%s'\n", filename);
2222 if (strcmp(basename, UNDEFINED_FILENAME) == 0)
2224 deleteArtworkListEntry(artwork_info, listnode);
2228 replaceArtworkListEntry(artwork_info, listnode, basename);
2231 static void LoadArtworkToList(struct ArtworkListInfo *artwork_info,
2232 struct ListNodeInfo **listnode,
2233 char *basename, int list_pos)
2236 if (artwork_info->artwork_list == NULL ||
2237 list_pos >= artwork_info->num_file_list_entries)
2242 printf("loading artwork '%s' ... [%d]\n",
2243 basename, getNumNodes(artwork_info->content_list));
2247 LoadCustomArtwork(artwork_info, listnode, basename);
2249 LoadCustomArtwork(artwork_info, &artwork_info->artwork_list[list_pos],
2254 printf("loading artwork '%s' done [%d]\n",
2255 basename, getNumNodes(artwork_info->content_list));
2259 void ReloadCustomArtworkList(struct ArtworkListInfo *artwork_info)
2261 struct FileInfo *file_list = artwork_info->file_list;
2262 struct FileInfo *dynamic_file_list = artwork_info->dynamic_file_list;
2263 int num_file_list_entries = artwork_info->num_file_list_entries;
2264 int num_dynamic_file_list_entries =
2265 artwork_info->num_dynamic_file_list_entries;
2269 printf("DEBUG: reloading %d static artwork files ...\n",
2270 num_file_list_entries);
2273 for(i=0; i<num_file_list_entries; i++)
2274 LoadArtworkToList(artwork_info, &artwork_info->artwork_list[i],
2275 file_list[i].filename, i);
2278 printf("DEBUG: reloading %d dynamic artwork files ...\n",
2279 num_dynamic_file_list_entries);
2282 for(i=0; i<num_dynamic_file_list_entries; i++)
2283 LoadArtworkToList(artwork_info, &artwork_info->dynamic_artwork_list[i],
2284 dynamic_file_list[i].filename, i);
2287 dumpList(artwork_info->content_list);
2291 static void FreeCustomArtworkList(struct ArtworkListInfo *artwork_info,
2292 struct ListNodeInfo ***list,
2293 int *num_list_entries)
2300 for(i=0; i<*num_list_entries; i++)
2301 deleteArtworkListEntry(artwork_info, &(*list)[i]);
2305 *num_list_entries = 0;
2308 void FreeCustomArtworkLists(struct ArtworkListInfo *artwork_info)
2310 if (artwork_info == NULL)
2314 printf("%s: FREEING ARTWORK ...\n",
2315 IS_CHILD_PROCESS() ? "CHILD" : "PARENT");
2318 FreeCustomArtworkList(artwork_info, &artwork_info->artwork_list,
2319 &artwork_info->num_file_list_entries);
2321 FreeCustomArtworkList(artwork_info, &artwork_info->dynamic_artwork_list,
2322 &artwork_info->num_dynamic_file_list_entries);
2325 printf("%s: FREEING ARTWORK -- DONE\n",
2326 IS_CHILD_PROCESS() ? "CHILD" : "PARENT");
2331 /* ------------------------------------------------------------------------- */
2332 /* functions only needed for non-Unix (non-command-line) systems */
2333 /* (MS-DOS only; SDL/Windows creates files "stdout.txt" and "stderr.txt") */
2334 /* ------------------------------------------------------------------------- */
2336 #if defined(PLATFORM_MSDOS)
2338 #define ERROR_FILENAME "stderr.txt"
2340 void initErrorFile()
2342 unlink(ERROR_FILENAME);
2345 FILE *openErrorFile()
2347 return fopen(ERROR_FILENAME, MODE_APPEND);
2350 void dumpErrorFile()
2352 FILE *error_file = fopen(ERROR_FILENAME, MODE_READ);
2354 if (error_file != NULL)
2356 while (!feof(error_file))
2357 fputc(fgetc(error_file), stderr);
2365 /* ------------------------------------------------------------------------- */
2366 /* the following is only for debugging purpose and normally not used */
2367 /* ------------------------------------------------------------------------- */
2369 #define DEBUG_NUM_TIMESTAMPS 3
2371 void debug_print_timestamp(int counter_nr, char *message)
2373 static long counter[DEBUG_NUM_TIMESTAMPS][2];
2375 if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
2376 Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
2378 counter[counter_nr][0] = Counter();
2381 printf("%s %.2f seconds\n", message,
2382 (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
2384 counter[counter_nr][1] = Counter();
2387 void debug_print_parent_only(char *format, ...)
2389 if (!IS_PARENT_PROCESS())
2396 va_start(ap, format);
2397 vprintf(format, ap);