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 &&
219 actual_frame_counter < *frame_counter_var + frame_delay)
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 &&
233 actual_counter < *counter_var + delay)
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 &&
250 actual_counter < *counter_var + delay)
251 sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
256 *counter_var = actual_counter;
260 /* ------------------------------------------------------------------------- */
261 /* random generator functions */
262 /* ------------------------------------------------------------------------- */
265 unsigned int SimpleRND(unsigned int max)
267 return (random_linux_libc(RND_FREE) % max);
270 unsigned int InitSimpleRND(long seed)
272 if (seed == NEW_RANDOMIZE)
274 struct timeval current_time;
276 gettimeofday(¤t_time, NULL);
277 seed = (long)current_time.tv_usec;
280 srandom_linux_libc(RND_FREE, (unsigned int) seed);
282 return (unsigned int) seed;
285 unsigned int RND(unsigned int max)
287 return (random_linux_libc(RND_GAME) % max);
290 unsigned int InitRND(long seed)
292 if (seed == NEW_RANDOMIZE)
294 struct timeval current_time;
296 gettimeofday(¤t_time, NULL);
297 seed = (long)current_time.tv_usec;
300 srandom_linux_libc(RND_GAME, (unsigned int) seed);
302 return (unsigned int) seed;
306 unsigned int init_random_number(int nr, long seed)
308 if (seed == NEW_RANDOMIZE)
310 #if defined(TARGET_SDL)
311 seed = (long)SDL_GetTicks();
313 struct timeval current_time;
315 gettimeofday(¤t_time, NULL);
316 seed = (long)current_time.tv_usec;
320 srandom_linux_libc(nr, (unsigned int) seed);
322 return (unsigned int) seed;
325 unsigned int get_random_number(int nr, unsigned int max)
327 return (random_linux_libc(nr) % max);
331 /* ------------------------------------------------------------------------- */
332 /* system info functions */
333 /* ------------------------------------------------------------------------- */
335 static char *get_corrected_real_name(char *real_name)
337 char *real_name_new = checked_malloc(MAX_USERNAME_LEN + 1);
338 char *from_ptr = real_name;
339 char *to_ptr = real_name_new;
341 if (strchr(real_name, 'ß') == NULL) /* name does not contain 'ß' */
343 strncpy(real_name_new, real_name, MAX_USERNAME_LEN);
344 real_name_new[MAX_USERNAME_LEN] = '\0';
346 return real_name_new;
349 /* the user's real name may contain a 'ß' character (german sharp s),
350 which has no equivalent in upper case letters (which our fonts use) */
351 while (*from_ptr && (long)(to_ptr - real_name_new) < MAX_USERNAME_LEN - 1)
353 if (*from_ptr != 'ß')
354 *to_ptr++ = *from_ptr++;
365 return real_name_new;
370 static char *login_name = NULL;
372 #if defined(PLATFORM_WIN32)
373 if (login_name == NULL)
375 unsigned long buffer_size = MAX_USERNAME_LEN + 1;
376 login_name = checked_malloc(buffer_size);
378 if (GetUserName(login_name, &buffer_size) == 0)
379 strcpy(login_name, ANONYMOUS_NAME);
382 if (login_name == NULL)
386 if ((pwd = getpwuid(getuid())) == NULL)
387 login_name = ANONYMOUS_NAME;
389 login_name = getStringCopy(pwd->pw_name);
398 static char *real_name = NULL;
400 #if defined(PLATFORM_WIN32)
401 if (real_name == NULL)
403 static char buffer[MAX_USERNAME_LEN + 1];
404 unsigned long buffer_size = MAX_USERNAME_LEN + 1;
406 if (GetUserName(buffer, &buffer_size) != 0)
407 real_name = get_corrected_real_name(buffer);
409 real_name = ANONYMOUS_NAME;
411 #elif defined(PLATFORM_UNIX)
412 if (real_name == NULL)
416 if ((pwd = getpwuid(getuid())) != NULL && strlen(pwd->pw_gecos) != 0)
417 real_name = get_corrected_real_name(pwd->pw_gecos);
419 real_name = ANONYMOUS_NAME;
422 real_name = ANONYMOUS_NAME;
430 static char *dir = NULL;
432 #if defined(PLATFORM_WIN32)
435 dir = checked_malloc(MAX_PATH + 1);
437 if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, dir)))
440 #elif defined(PLATFORM_UNIX)
443 if ((dir = getenv("HOME")) == NULL)
447 if ((pwd = getpwuid(getuid())) != NULL)
448 dir = getStringCopy(pwd->pw_dir);
461 /* ------------------------------------------------------------------------- */
462 /* various string functions */
463 /* ------------------------------------------------------------------------- */
465 char *getPath2(char *path1, char *path2)
467 char *complete_path = checked_malloc(strlen(path1) + 1 +
470 sprintf(complete_path, "%s/%s", path1, path2);
472 return complete_path;
475 char *getPath3(char *path1, char *path2, char *path3)
477 char *complete_path = checked_malloc(strlen(path1) + 1 +
481 sprintf(complete_path, "%s/%s/%s", path1, path2, path3);
483 return complete_path;
486 char *getStringCat2(char *s1, char *s2)
488 char *complete_string = checked_malloc(strlen(s1) + strlen(s2) + 1);
490 sprintf(complete_string, "%s%s", s1, s2);
492 return complete_string;
495 char *getStringCopy(char *s)
502 s_copy = checked_malloc(strlen(s) + 1);
508 char *getStringToLower(char *s)
510 char *s_copy = checked_malloc(strlen(s) + 1);
511 char *s_ptr = s_copy;
514 *s_ptr++ = tolower(*s++);
520 void setString(char **old_value, char *new_value)
522 if (*old_value != NULL)
525 *old_value = getStringCopy(new_value);
529 /* ------------------------------------------------------------------------- */
530 /* command line option handling functions */
531 /* ------------------------------------------------------------------------- */
533 static void printUsage()
536 "Usage: %s [OPTION]... [HOSTNAME [PORT]]\n"
539 " -d, --display HOSTNAME[:SCREEN] specify X server display\n"
540 " -b, --basepath DIRECTORY alternative base DIRECTORY\n"
541 " -l, --level DIRECTORY alternative level DIRECTORY\n"
542 " -g, --graphics DIRECTORY alternative graphics DIRECTORY\n"
543 " -s, --sounds DIRECTORY alternative sounds DIRECTORY\n"
544 " -m, --music DIRECTORY alternative music DIRECTORY\n"
545 " -n, --network network multiplayer game\n"
546 " --serveronly only start network server\n"
547 " -v, --verbose verbose mode\n"
548 " --debug display debugging information\n"
549 " -e, --execute COMMAND execute batch COMMAND:\n"
551 "Valid commands for '--execute' option:\n"
552 " \"print graphicsinfo.conf\" print default graphics config\n"
553 " \"print soundsinfo.conf\" print default sounds config\n"
554 " \"print musicinfo.conf\" print default music config\n"
555 " \"dump level FILE\" dump level data from FILE\n"
556 " \"dump tape FILE\" dump tape data from FILE\n"
557 " \"autoplay LEVELDIR\" play level tapes for LEVELDIR\n"
559 program.command_basename);
562 void GetOptions(char *argv[])
564 char **options_left = &argv[1];
566 /* initialize global program options */
567 options.display_name = NULL;
568 options.server_host = NULL;
569 options.server_port = 0;
570 options.ro_base_directory = RO_BASE_PATH;
571 options.rw_base_directory = RW_BASE_PATH;
572 options.level_directory = RO_BASE_PATH "/" LEVELS_DIRECTORY;
573 options.graphics_directory = RO_BASE_PATH "/" GRAPHICS_DIRECTORY;
574 options.sounds_directory = RO_BASE_PATH "/" SOUNDS_DIRECTORY;
575 options.music_directory = RO_BASE_PATH "/" MUSIC_DIRECTORY;
576 options.docs_directory = RO_BASE_PATH "/" DOCS_DIRECTORY;
577 options.execute_command = NULL;
578 options.serveronly = FALSE;
579 options.network = FALSE;
580 options.verbose = FALSE;
581 options.debug = FALSE;
583 #if !defined(PLATFORM_UNIX)
584 if (*options_left == NULL) /* no options given -- enable verbose mode */
585 options.verbose = TRUE;
588 while (*options_left)
590 char option_str[MAX_OPTION_LEN];
591 char *option = options_left[0];
592 char *next_option = options_left[1];
593 char *option_arg = NULL;
594 int option_len = strlen(option);
596 if (option_len >= MAX_OPTION_LEN)
597 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
599 strcpy(option_str, option); /* copy argument into buffer */
602 if (strcmp(option, "--") == 0) /* stop scanning arguments */
605 if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */
608 option_arg = strchr(option, '=');
609 if (option_arg == NULL) /* no '=' in option */
610 option_arg = next_option;
613 *option_arg++ = '\0'; /* cut argument from option */
614 if (*option_arg == '\0') /* no argument after '=' */
615 Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
618 option_len = strlen(option);
620 if (strcmp(option, "-") == 0)
621 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
622 else if (strncmp(option, "-help", option_len) == 0)
628 else if (strncmp(option, "-display", option_len) == 0)
630 if (option_arg == NULL)
631 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
633 options.display_name = option_arg;
634 if (option_arg == next_option)
637 else if (strncmp(option, "-basepath", option_len) == 0)
639 if (option_arg == NULL)
640 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
642 /* this should be extended to separate options for ro and rw data */
643 options.ro_base_directory = option_arg;
644 options.rw_base_directory = option_arg;
645 if (option_arg == next_option)
648 /* adjust path for level directory accordingly */
649 options.level_directory =
650 getPath2(options.ro_base_directory, LEVELS_DIRECTORY);
652 else if (strncmp(option, "-levels", option_len) == 0)
654 if (option_arg == NULL)
655 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
657 options.level_directory = option_arg;
658 if (option_arg == next_option)
661 else if (strncmp(option, "-graphics", option_len) == 0)
663 if (option_arg == NULL)
664 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
666 options.graphics_directory = option_arg;
667 if (option_arg == next_option)
670 else if (strncmp(option, "-sounds", option_len) == 0)
672 if (option_arg == NULL)
673 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
675 options.sounds_directory = option_arg;
676 if (option_arg == next_option)
679 else if (strncmp(option, "-music", option_len) == 0)
681 if (option_arg == NULL)
682 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
684 options.music_directory = option_arg;
685 if (option_arg == next_option)
688 else if (strncmp(option, "-network", option_len) == 0)
690 options.network = TRUE;
692 else if (strncmp(option, "-serveronly", option_len) == 0)
694 options.serveronly = TRUE;
696 else if (strncmp(option, "-verbose", option_len) == 0)
698 options.verbose = TRUE;
700 else if (strncmp(option, "-debug", option_len) == 0)
702 options.debug = TRUE;
704 else if (strncmp(option, "-execute", option_len) == 0)
706 if (option_arg == NULL)
707 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
709 options.execute_command = option_arg;
710 if (option_arg == next_option)
713 else if (*option == '-')
715 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
717 else if (options.server_host == NULL)
719 options.server_host = *options_left;
721 else if (options.server_port == 0)
723 options.server_port = atoi(*options_left);
724 if (options.server_port < 1024)
725 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
728 Error(ERR_EXIT_HELP, "too many arguments");
735 /* ------------------------------------------------------------------------- */
736 /* error handling functions */
737 /* ------------------------------------------------------------------------- */
739 /* used by SetError() and GetError() to store internal error messages */
740 static char internal_error[1024]; /* this is bad */
742 void SetError(char *format, ...)
746 va_start(ap, format);
747 vsprintf(internal_error, format, ap);
753 return internal_error;
756 void Error(int mode, char *format, ...)
758 static boolean last_line_was_separator = FALSE;
759 char *process_name = "";
760 FILE *error = stderr;
761 char *newline = "\n";
763 /* display warnings only when running in verbose mode */
764 if (mode & ERR_WARN && !options.verbose)
767 if (mode == ERR_RETURN_LINE)
769 if (!last_line_was_separator)
770 fprintf_line(error, format, 79);
772 last_line_was_separator = TRUE;
777 last_line_was_separator = FALSE;
779 #if defined(PLATFORM_MSDOS)
782 if ((error = openErrorFile()) == NULL)
784 printf("Cannot write to error output file!%s", newline);
785 program.exit_function(1);
789 if (mode & ERR_SOUND_SERVER)
790 process_name = " sound server";
791 else if (mode & ERR_NETWORK_SERVER)
792 process_name = " network server";
793 else if (mode & ERR_NETWORK_CLIENT)
794 process_name = " network client **";
800 fprintf(error, "%s%s: ", program.command_basename, process_name);
803 fprintf(error, "warning: ");
805 va_start(ap, format);
806 vfprintf(error, format, ap);
809 fprintf(error, "%s", newline);
813 fprintf(error, "%s: Try option '--help' for more information.%s",
814 program.command_basename, newline);
817 fprintf(error, "%s%s: aborting%s",
818 program.command_basename, process_name, newline);
825 if (mode & ERR_FROM_SERVER)
826 exit(1); /* child process: normal exit */
828 program.exit_function(1); /* main process: clean up stuff */
833 /* ------------------------------------------------------------------------- */
834 /* memory allocation functions */
835 /* ------------------------------------------------------------------------- */
837 void *checked_malloc(unsigned long size)
844 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
849 void *checked_calloc(unsigned long size)
853 ptr = calloc(1, size);
856 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
861 void *checked_realloc(void *ptr, unsigned long size)
863 ptr = realloc(ptr, size);
866 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
872 /* ------------------------------------------------------------------------- */
873 /* various helper functions */
874 /* ------------------------------------------------------------------------- */
876 inline void swap_numbers(int *i1, int *i2)
884 inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2)
896 short getFile16BitInteger(FILE *file, int byte_order)
898 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
899 return ((fgetc(file) << 8) |
901 else /* BYTE_ORDER_LITTLE_ENDIAN */
902 return ((fgetc(file) << 0) |
906 void putFile16BitInteger(FILE *file, short value, int byte_order)
908 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
910 fputc((value >> 8) & 0xff, file);
911 fputc((value >> 0) & 0xff, file);
913 else /* BYTE_ORDER_LITTLE_ENDIAN */
915 fputc((value >> 0) & 0xff, file);
916 fputc((value >> 8) & 0xff, file);
920 int getFile32BitInteger(FILE *file, int byte_order)
922 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
923 return ((fgetc(file) << 24) |
924 (fgetc(file) << 16) |
927 else /* BYTE_ORDER_LITTLE_ENDIAN */
928 return ((fgetc(file) << 0) |
930 (fgetc(file) << 16) |
931 (fgetc(file) << 24));
934 void putFile32BitInteger(FILE *file, int value, int byte_order)
936 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
938 fputc((value >> 24) & 0xff, file);
939 fputc((value >> 16) & 0xff, file);
940 fputc((value >> 8) & 0xff, file);
941 fputc((value >> 0) & 0xff, file);
943 else /* BYTE_ORDER_LITTLE_ENDIAN */
945 fputc((value >> 0) & 0xff, file);
946 fputc((value >> 8) & 0xff, file);
947 fputc((value >> 16) & 0xff, file);
948 fputc((value >> 24) & 0xff, file);
952 boolean getFileChunk(FILE *file, char *chunk_name, int *chunk_size,
955 const int chunk_name_length = 4;
957 /* read chunk name */
958 fgets(chunk_name, chunk_name_length + 1, file);
960 if (chunk_size != NULL)
962 /* read chunk size */
963 *chunk_size = getFile32BitInteger(file, byte_order);
966 return (feof(file) || ferror(file) ? FALSE : TRUE);
969 void putFileChunk(FILE *file, char *chunk_name, int chunk_size,
972 /* write chunk name */
973 fputs(chunk_name, file);
977 /* write chunk size */
978 putFile32BitInteger(file, chunk_size, byte_order);
982 int getFileVersion(FILE *file)
984 int version_major, version_minor, version_patch;
986 version_major = fgetc(file);
987 version_minor = fgetc(file);
988 version_patch = fgetc(file);
989 fgetc(file); /* not used */
991 return VERSION_IDENT(version_major, version_minor, version_patch);
994 void putFileVersion(FILE *file, int version)
996 int version_major = VERSION_MAJOR(version);
997 int version_minor = VERSION_MINOR(version);
998 int version_patch = VERSION_PATCH(version);
1000 fputc(version_major, file);
1001 fputc(version_minor, file);
1002 fputc(version_patch, file);
1003 fputc(0, file); /* not used */
1006 void ReadUnusedBytesFromFile(FILE *file, unsigned long bytes)
1008 while (bytes-- && !feof(file))
1012 void WriteUnusedBytesToFile(FILE *file, unsigned long bytes)
1019 /* ------------------------------------------------------------------------- */
1020 /* functions to translate key identifiers between different format */
1021 /* ------------------------------------------------------------------------- */
1023 #define TRANSLATE_KEYSYM_TO_KEYNAME 0
1024 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
1025 #define TRANSLATE_KEYNAME_TO_KEYSYM 2
1026 #define TRANSLATE_X11KEYNAME_TO_KEYSYM 3
1028 void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
1037 /* normal cursor keys */
1038 { KSYM_Left, "XK_Left", "cursor left" },
1039 { KSYM_Right, "XK_Right", "cursor right" },
1040 { KSYM_Up, "XK_Up", "cursor up" },
1041 { KSYM_Down, "XK_Down", "cursor down" },
1043 /* keypad cursor keys */
1045 { KSYM_KP_Left, "XK_KP_Left", "keypad left" },
1046 { KSYM_KP_Right, "XK_KP_Right", "keypad right" },
1047 { KSYM_KP_Up, "XK_KP_Up", "keypad up" },
1048 { KSYM_KP_Down, "XK_KP_Down", "keypad down" },
1051 /* other keypad keys */
1052 #ifdef KSYM_KP_Enter
1053 { KSYM_KP_Enter, "XK_KP_Enter", "keypad enter" },
1054 { KSYM_KP_Add, "XK_KP_Add", "keypad +" },
1055 { KSYM_KP_Subtract, "XK_KP_Subtract", "keypad -" },
1056 { KSYM_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
1057 { KSYM_KP_Divide, "XK_KP_Divide", "keypad /" },
1058 { KSYM_KP_Separator,"XK_KP_Separator", "keypad ," },
1062 { KSYM_Shift_L, "XK_Shift_L", "left shift" },
1063 { KSYM_Shift_R, "XK_Shift_R", "right shift" },
1064 { KSYM_Control_L, "XK_Control_L", "left control" },
1065 { KSYM_Control_R, "XK_Control_R", "right control" },
1066 { KSYM_Meta_L, "XK_Meta_L", "left meta" },
1067 { KSYM_Meta_R, "XK_Meta_R", "right meta" },
1068 { KSYM_Alt_L, "XK_Alt_L", "left alt" },
1069 { KSYM_Alt_R, "XK_Alt_R", "right alt" },
1070 { KSYM_Super_L, "XK_Super_L", "left super" }, /* Win-L */
1071 { KSYM_Super_R, "XK_Super_R", "right super" }, /* Win-R */
1072 { KSYM_Mode_switch, "XK_Mode_switch", "mode switch" }, /* Alt-R */
1073 { KSYM_Multi_key, "XK_Multi_key", "multi key" }, /* Ctrl-R */
1075 /* some special keys */
1076 { KSYM_BackSpace, "XK_BackSpace", "backspace" },
1077 { KSYM_Delete, "XK_Delete", "delete" },
1078 { KSYM_Insert, "XK_Insert", "insert" },
1079 { KSYM_Tab, "XK_Tab", "tab" },
1080 { KSYM_Home, "XK_Home", "home" },
1081 { KSYM_End, "XK_End", "end" },
1082 { KSYM_Page_Up, "XK_Page_Up", "page up" },
1083 { KSYM_Page_Down, "XK_Page_Down", "page down" },
1084 { KSYM_Menu, "XK_Menu", "menu" }, /* Win-Menu */
1086 /* ASCII 0x20 to 0x40 keys (except numbers) */
1087 { KSYM_space, "XK_space", "space" },
1088 { KSYM_exclam, "XK_exclam", "!" },
1089 { KSYM_quotedbl, "XK_quotedbl", "\"" },
1090 { KSYM_numbersign, "XK_numbersign", "#" },
1091 { KSYM_dollar, "XK_dollar", "$" },
1092 { KSYM_percent, "XK_percent", "%" },
1093 { KSYM_ampersand, "XK_ampersand", "&" },
1094 { KSYM_apostrophe, "XK_apostrophe", "'" },
1095 { KSYM_parenleft, "XK_parenleft", "(" },
1096 { KSYM_parenright, "XK_parenright", ")" },
1097 { KSYM_asterisk, "XK_asterisk", "*" },
1098 { KSYM_plus, "XK_plus", "+" },
1099 { KSYM_comma, "XK_comma", "," },
1100 { KSYM_minus, "XK_minus", "-" },
1101 { KSYM_period, "XK_period", "." },
1102 { KSYM_slash, "XK_slash", "/" },
1103 { KSYM_colon, "XK_colon", ":" },
1104 { KSYM_semicolon, "XK_semicolon", ";" },
1105 { KSYM_less, "XK_less", "<" },
1106 { KSYM_equal, "XK_equal", "=" },
1107 { KSYM_greater, "XK_greater", ">" },
1108 { KSYM_question, "XK_question", "?" },
1109 { KSYM_at, "XK_at", "@" },
1111 /* more ASCII keys */
1112 { KSYM_bracketleft, "XK_bracketleft", "[" },
1113 { KSYM_backslash, "XK_backslash", "backslash" },
1114 { KSYM_bracketright,"XK_bracketright", "]" },
1115 { KSYM_asciicircum, "XK_asciicircum", "circumflex" },
1116 { KSYM_underscore, "XK_underscore", "_" },
1117 { KSYM_grave, "XK_grave", "grave" },
1118 { KSYM_quoteleft, "XK_quoteleft", "quote left" },
1119 { KSYM_braceleft, "XK_braceleft", "brace left" },
1120 { KSYM_bar, "XK_bar", "bar" },
1121 { KSYM_braceright, "XK_braceright", "brace right" },
1122 { KSYM_asciitilde, "XK_asciitilde", "ascii tilde" },
1124 /* special (non-ASCII) keys */
1125 { KSYM_Adiaeresis, "XK_Adiaeresis", "Ä" },
1126 { KSYM_Odiaeresis, "XK_Odiaeresis", "Ö" },
1127 { KSYM_Udiaeresis, "XK_Udiaeresis", "Ü" },
1128 { KSYM_adiaeresis, "XK_adiaeresis", "ä" },
1129 { KSYM_odiaeresis, "XK_odiaeresis", "ö" },
1130 { KSYM_udiaeresis, "XK_udiaeresis", "ü" },
1131 { KSYM_ssharp, "XK_ssharp", "sharp s" },
1133 /* end-of-array identifier */
1139 if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
1141 static char name_buffer[30];
1144 if (key >= KSYM_A && key <= KSYM_Z)
1145 sprintf(name_buffer, "%c", 'A' + (char)(key - KSYM_A));
1146 else if (key >= KSYM_a && key <= KSYM_z)
1147 sprintf(name_buffer, "%c", 'a' + (char)(key - KSYM_a));
1148 else if (key >= KSYM_0 && key <= KSYM_9)
1149 sprintf(name_buffer, "%c", '0' + (char)(key - KSYM_0));
1150 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
1151 sprintf(name_buffer, "keypad %c", '0' + (char)(key - KSYM_KP_0));
1152 else if (key >= KSYM_FKEY_FIRST && key <= KSYM_FKEY_LAST)
1153 sprintf(name_buffer, "function F%d", (int)(key - KSYM_FKEY_FIRST + 1));
1154 else if (key == KSYM_UNDEFINED)
1155 strcpy(name_buffer, "(undefined)");
1162 if (key == translate_key[i].key)
1164 strcpy(name_buffer, translate_key[i].name);
1168 while (translate_key[++i].name);
1170 if (!translate_key[i].name)
1171 strcpy(name_buffer, "(unknown)");
1174 *name = name_buffer;
1176 else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME)
1178 static char name_buffer[30];
1181 if (key >= KSYM_A && key <= KSYM_Z)
1182 sprintf(name_buffer, "XK_%c", 'A' + (char)(key - KSYM_A));
1183 else if (key >= KSYM_a && key <= KSYM_z)
1184 sprintf(name_buffer, "XK_%c", 'a' + (char)(key - KSYM_a));
1185 else if (key >= KSYM_0 && key <= KSYM_9)
1186 sprintf(name_buffer, "XK_%c", '0' + (char)(key - KSYM_0));
1187 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
1188 sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - KSYM_KP_0));
1189 else if (key >= KSYM_FKEY_FIRST && key <= KSYM_FKEY_LAST)
1190 sprintf(name_buffer, "XK_F%d", (int)(key - KSYM_FKEY_FIRST + 1));
1191 else if (key == KSYM_UNDEFINED)
1192 strcpy(name_buffer, "[undefined]");
1199 if (key == translate_key[i].key)
1201 strcpy(name_buffer, translate_key[i].x11name);
1205 while (translate_key[++i].x11name);
1207 if (!translate_key[i].x11name)
1208 sprintf(name_buffer, "0x%04lx", (unsigned long)key);
1211 *x11name = name_buffer;
1213 else if (mode == TRANSLATE_KEYNAME_TO_KEYSYM)
1215 Key key = KSYM_UNDEFINED;
1220 if (strcmp(translate_key[i].name, *name) == 0)
1222 key = translate_key[i].key;
1226 while (translate_key[++i].x11name);
1228 if (key == KSYM_UNDEFINED)
1229 Error(ERR_WARN, "getKeyFromKeyName(): not completely implemented");
1233 else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
1235 Key key = KSYM_UNDEFINED;
1236 char *name_ptr = *x11name;
1238 if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4)
1240 char c = name_ptr[3];
1242 if (c >= 'A' && c <= 'Z')
1243 key = KSYM_A + (Key)(c - 'A');
1244 else if (c >= 'a' && c <= 'z')
1245 key = KSYM_a + (Key)(c - 'a');
1246 else if (c >= '0' && c <= '9')
1247 key = KSYM_0 + (Key)(c - '0');
1249 else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7)
1251 char c = name_ptr[6];
1253 if (c >= '0' && c <= '9')
1254 key = KSYM_0 + (Key)(c - '0');
1256 else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
1258 char c1 = name_ptr[4];
1259 char c2 = name_ptr[5];
1262 if ((c1 >= '0' && c1 <= '9') &&
1263 ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
1264 d = atoi(&name_ptr[4]);
1266 if (d >= 1 && d <= KSYM_NUM_FKEYS)
1267 key = KSYM_F1 + (Key)(d - 1);
1269 else if (strncmp(name_ptr, "XK_", 3) == 0)
1275 if (strcmp(name_ptr, translate_key[i].x11name) == 0)
1277 key = translate_key[i].key;
1281 while (translate_key[++i].x11name);
1283 else if (strncmp(name_ptr, "0x", 2) == 0)
1285 unsigned long value = 0;
1291 char c = *name_ptr++;
1294 if (c >= '0' && c <= '9')
1296 else if (c >= 'a' && c <= 'f')
1297 d = (int)(c - 'a' + 10);
1298 else if (c >= 'A' && c <= 'F')
1299 d = (int)(c - 'A' + 10);
1307 value = value * 16 + d;
1318 char *getKeyNameFromKey(Key key)
1322 translate_keyname(&key, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME);
1326 char *getX11KeyNameFromKey(Key key)
1330 translate_keyname(&key, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME);
1334 Key getKeyFromKeyName(char *name)
1338 translate_keyname(&key, NULL, &name, TRANSLATE_KEYNAME_TO_KEYSYM);
1342 Key getKeyFromX11KeyName(char *x11name)
1346 translate_keyname(&key, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM);
1350 char getCharFromKey(Key key)
1352 char *keyname = getKeyNameFromKey(key);
1355 if (strlen(keyname) == 1)
1356 letter = keyname[0];
1357 else if (strcmp(keyname, "space") == 0)
1359 else if (strcmp(keyname, "circumflex") == 0)
1366 /* ------------------------------------------------------------------------- */
1367 /* functions to translate string identifiers to integer or boolean value */
1368 /* ------------------------------------------------------------------------- */
1370 int get_integer_from_string(char *s)
1372 static char *number_text[][3] =
1374 { "0", "zero", "null", },
1375 { "1", "one", "first" },
1376 { "2", "two", "second" },
1377 { "3", "three", "third" },
1378 { "4", "four", "fourth" },
1379 { "5", "five", "fifth" },
1380 { "6", "six", "sixth" },
1381 { "7", "seven", "seventh" },
1382 { "8", "eight", "eighth" },
1383 { "9", "nine", "ninth" },
1384 { "10", "ten", "tenth" },
1385 { "11", "eleven", "eleventh" },
1386 { "12", "twelve", "twelfth" },
1390 char *s_lower = getStringToLower(s);
1393 for (i=0; i<13; i++)
1395 if (strcmp(s_lower, number_text[i][j]) == 0)
1406 boolean get_boolean_from_string(char *s)
1408 char *s_lower = getStringToLower(s);
1409 boolean result = FALSE;
1411 if (strcmp(s_lower, "true") == 0 ||
1412 strcmp(s_lower, "yes") == 0 ||
1413 strcmp(s_lower, "on") == 0 ||
1414 get_integer_from_string(s) == 1)
1423 /* ------------------------------------------------------------------------- */
1424 /* functions for generic lists */
1425 /* ------------------------------------------------------------------------- */
1427 ListNode *newListNode()
1429 return checked_calloc(sizeof(ListNode));
1432 void addNodeToList(ListNode **node_first, char *key, void *content)
1434 ListNode *node_new = newListNode();
1437 printf("LIST: adding node with key '%s'\n", key);
1440 node_new->key = getStringCopy(key);
1441 node_new->content = content;
1442 node_new->next = *node_first;
1443 *node_first = node_new;
1446 void deleteNodeFromList(ListNode **node_first, char *key,
1447 void (*destructor_function)(void *))
1449 if (node_first == NULL || *node_first == NULL)
1453 printf("[CHECKING LIST KEY '%s' == '%s']\n",
1454 (*node_first)->key, key);
1457 if (strcmp((*node_first)->key, key) == 0)
1460 printf("[DELETING LIST ENTRY]\n");
1463 free((*node_first)->key);
1464 if (destructor_function)
1465 destructor_function((*node_first)->content);
1466 *node_first = (*node_first)->next;
1469 deleteNodeFromList(&(*node_first)->next, key, destructor_function);
1472 ListNode *getNodeFromKey(ListNode *node_first, char *key)
1474 if (node_first == NULL)
1477 if (strcmp(node_first->key, key) == 0)
1480 return getNodeFromKey(node_first->next, key);
1483 int getNumNodes(ListNode *node_first)
1485 return (node_first ? 1 + getNumNodes(node_first->next) : 0);
1488 void dumpList(ListNode *node_first)
1490 ListNode *node = node_first;
1494 printf("['%s' (%d)]\n", node->key,
1495 ((struct ListNodeInfo *)node->content)->num_references);
1499 printf("[%d nodes]\n", getNumNodes(node_first));
1503 /* ------------------------------------------------------------------------- */
1504 /* functions for checking files and filenames */
1505 /* ------------------------------------------------------------------------- */
1507 boolean fileExists(char *filename)
1510 printf("checking file '%s'\n", filename);
1513 return (access(filename, F_OK) == 0);
1516 boolean FileIsGraphic(char *filename)
1518 if (strlen(filename) > 4 &&
1519 strcmp(&filename[strlen(filename) - 4], ".pcx") == 0)
1525 boolean FileIsSound(char *basename)
1527 if (strlen(basename) > 4 &&
1528 strcmp(&basename[strlen(basename) - 4], ".wav") == 0)
1534 boolean FileIsMusic(char *basename)
1536 /* "music" can be a WAV (loop) file or (if compiled with SDL) a MOD file */
1538 if (FileIsSound(basename))
1541 #if defined(TARGET_SDL)
1542 if (strlen(basename) > 4 &&
1543 (strcmp(&basename[strlen(basename) - 4], ".mod") == 0 ||
1544 strcmp(&basename[strlen(basename) - 4], ".MOD") == 0 ||
1545 strncmp(basename, "mod.", 4) == 0 ||
1546 strncmp(basename, "MOD.", 4) == 0))
1553 boolean FileIsArtworkType(char *basename, int type)
1555 if ((type == TREE_TYPE_GRAPHICS_DIR && FileIsGraphic(basename)) ||
1556 (type == TREE_TYPE_SOUNDS_DIR && FileIsSound(basename)) ||
1557 (type == TREE_TYPE_MUSIC_DIR && FileIsMusic(basename)))
1563 /* ------------------------------------------------------------------------- */
1564 /* functions for loading artwork configuration information */
1565 /* ------------------------------------------------------------------------- */
1567 /* This function checks if a string <s> of the format "string1, string2, ..."
1568 exactly contains a string <s_contained>. */
1570 static boolean string_has_parameter(char *s, char *s_contained)
1574 if (s == NULL || s_contained == NULL)
1577 if (strlen(s_contained) > strlen(s))
1580 if (strncmp(s, s_contained, strlen(s_contained)) == 0)
1582 char next_char = s[strlen(s_contained)];
1584 /* check if next character is delimiter or whitespace */
1585 return (next_char == ',' || next_char == '\0' ||
1586 next_char == ' ' || next_char == '\t' ? TRUE : FALSE);
1589 /* check if string contains another parameter string after a comma */
1590 substring = strchr(s, ',');
1591 if (substring == NULL) /* string does not contain a comma */
1594 /* advance string pointer to next character after the comma */
1597 /* skip potential whitespaces after the comma */
1598 while (*substring == ' ' || *substring == '\t')
1601 return string_has_parameter(substring, s_contained);
1604 int get_parameter_value(char *token, char *value_raw, int type)
1606 char *value = getStringToLower(value_raw);
1607 int result = 0; /* probably a save default value */
1609 if (strcmp(token, ".direction") == 0)
1611 result = (strcmp(value, "left") == 0 ? MV_LEFT :
1612 strcmp(value, "right") == 0 ? MV_RIGHT :
1613 strcmp(value, "up") == 0 ? MV_UP :
1614 strcmp(value, "down") == 0 ? MV_DOWN : MV_NO_MOVING);
1616 else if (strcmp(token, ".anim_mode") == 0)
1618 result = (string_has_parameter(value, "loop") ? ANIM_LOOP :
1619 string_has_parameter(value, "linear") ? ANIM_LINEAR :
1620 string_has_parameter(value, "pingpong") ? ANIM_PINGPONG :
1621 string_has_parameter(value, "pingpong2") ? ANIM_PINGPONG2 :
1622 string_has_parameter(value, "random") ? ANIM_RANDOM :
1623 string_has_parameter(value, "none") ? ANIM_NONE :
1626 if (string_has_parameter(value, "reverse"))
1627 result |= ANIM_REVERSE;
1629 else /* generic parameter of type integer or boolean */
1631 result = (strcmp(value, ARG_UNDEFINED) == 0 ? ARG_UNDEFINED_VALUE :
1632 type == TYPE_INTEGER ? get_integer_from_string(value) :
1633 type == TYPE_BOOLEAN ? get_boolean_from_string(value) :
1634 ARG_UNDEFINED_VALUE);
1642 static void FreeCustomArtworkList(struct ArtworkListInfo *,
1643 struct ListNodeInfo ***, int *);
1645 struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list,
1646 struct ConfigInfo *suffix_list,
1647 char **ignore_tokens,
1648 int num_file_list_entries)
1650 struct FileInfo *file_list;
1651 int num_file_list_entries_found = 0;
1652 int num_suffix_list_entries = 0;
1656 file_list = checked_calloc(num_file_list_entries * sizeof(struct FileInfo));
1658 for (i=0; suffix_list[i].token != NULL; i++)
1659 num_suffix_list_entries++;
1661 /* always start with reliable default values */
1662 for (i=0; i<num_file_list_entries; i++)
1664 file_list[i].token = NULL;
1666 file_list[i].default_filename = NULL;
1667 file_list[i].filename = NULL;
1669 if (num_suffix_list_entries > 0)
1671 int parameter_array_size = num_suffix_list_entries * sizeof(char *);
1673 file_list[i].default_parameter = checked_calloc(parameter_array_size);
1674 file_list[i].parameter = checked_calloc(parameter_array_size);
1676 for (j=0; j<num_suffix_list_entries; j++)
1678 setString(&file_list[i].default_parameter[j], suffix_list[j].value);
1679 setString(&file_list[i].parameter[j], suffix_list[j].value);
1685 for (i=0; config_list[i].token != NULL; i++)
1687 int len_config_token = strlen(config_list[i].token);
1688 int len_config_value = strlen(config_list[i].value);
1689 boolean is_file_entry = TRUE;
1691 for (j=0; suffix_list[j].token != NULL; j++)
1693 int len_suffix = strlen(suffix_list[j].token);
1695 if (len_suffix < len_config_token &&
1696 strcmp(&config_list[i].token[len_config_token - len_suffix],
1697 suffix_list[j].token) == 0)
1699 setString(&file_list[list_pos].default_parameter[j],
1700 config_list[i].value);
1702 is_file_entry = FALSE;
1707 /* the following tokens are no file definitions, but other config tokens */
1708 for (j=0; ignore_tokens[j] != NULL; j++)
1709 if (strcmp(config_list[i].token, ignore_tokens[j]) == 0)
1710 is_file_entry = FALSE;
1717 if (list_pos >= num_file_list_entries)
1720 /* simple sanity check if this is really a file definition */
1721 if (strcmp(&config_list[i].value[len_config_value - 4], ".pcx") != 0 &&
1722 strcmp(&config_list[i].value[len_config_value - 4], ".wav") != 0 &&
1723 strcmp(config_list[i].value, UNDEFINED_FILENAME) != 0)
1725 Error(ERR_RETURN, "Configuration directive '%s' -> '%s':",
1726 config_list[i].token, config_list[i].value);
1727 Error(ERR_EXIT, "This seems to be no valid definition -- please fix");
1730 file_list[list_pos].token = config_list[i].token;
1731 file_list[list_pos].default_filename = config_list[i].value;
1735 num_file_list_entries_found = list_pos + 1;
1736 if (num_file_list_entries_found != num_file_list_entries)
1738 Error(ERR_RETURN_LINE, "-");
1739 Error(ERR_RETURN, "inconsistant config list information:");
1740 Error(ERR_RETURN, "- should be: %d (according to 'src/conf_gfx.h')",
1741 num_file_list_entries);
1742 Error(ERR_RETURN, "- found to be: %d (according to 'src/conf_gfx.c')",
1743 num_file_list_entries_found);
1744 Error(ERR_EXIT, "please fix");
1750 static boolean token_suffix_match(char *token, char *suffix, int start_pos)
1752 int len_token = strlen(token);
1753 int len_suffix = strlen(suffix);
1756 if (IS_PARENT_PROCESS())
1757 printf(":::::::::: check '%s' for '%s' ::::::::::\n", token, suffix);
1760 if (start_pos < 0) /* compare suffix from end of string */
1761 start_pos += len_token;
1763 if (start_pos < 0 || start_pos + len_suffix > len_token)
1766 if (strncmp(&token[start_pos], suffix, len_suffix) != 0)
1769 if (token[start_pos + len_suffix] == '\0')
1772 if (token[start_pos + len_suffix] == '.')
1778 #define KNOWN_TOKEN_VALUE "[KNOWN_TOKEN]"
1780 static void read_token_parameters(struct SetupFileList *setup_file_list,
1781 struct ConfigInfo *suffix_list,
1782 struct FileInfo *file_list_entry)
1784 /* check for config token that is the base token without any suffixes */
1785 char *filename = getTokenValue(setup_file_list, file_list_entry->token);
1786 char *known_token_value = KNOWN_TOKEN_VALUE;
1789 if (filename != NULL)
1791 setString(&file_list_entry->filename, filename);
1793 /* when file definition found, set all parameters to default values */
1794 for (i=0; suffix_list[i].token != NULL; i++)
1795 setString(&file_list_entry->parameter[i], suffix_list[i].value);
1797 file_list_entry->redefined = TRUE;
1799 /* mark config file token as well known from default config */
1800 setTokenValue(setup_file_list, file_list_entry->token, known_token_value);
1803 setString(&file_list_entry->filename, file_list_entry->default_filename);
1805 /* check for config tokens that can be build by base token and suffixes */
1806 for (i=0; suffix_list[i].token != NULL; i++)
1808 char *token = getStringCat2(file_list_entry->token, suffix_list[i].token);
1809 char *value = getTokenValue(setup_file_list, token);
1813 setString(&file_list_entry->parameter[i], value);
1815 /* mark config file token as well known from default config */
1816 setTokenValue(setup_file_list, token, known_token_value);
1823 static void add_dynamic_file_list_entry(struct FileInfo **list,
1824 int *num_list_entries,
1825 struct SetupFileList *extra_file_list,
1826 struct ConfigInfo *suffix_list,
1827 int num_suffix_list_entries,
1830 struct FileInfo *new_list_entry;
1831 int parameter_array_size = num_suffix_list_entries * sizeof(char *);
1834 if (IS_PARENT_PROCESS())
1835 printf("===> found dynamic definition '%s'\n", token);
1838 (*num_list_entries)++;
1839 *list = checked_realloc(*list, *num_list_entries * sizeof(struct FileInfo));
1840 new_list_entry = &(*list)[*num_list_entries - 1];
1842 new_list_entry->token = getStringCopy(token);
1843 new_list_entry->filename = NULL;
1844 new_list_entry->parameter = checked_calloc(parameter_array_size);
1846 read_token_parameters(extra_file_list, suffix_list, new_list_entry);
1849 static void add_property_mapping(struct PropertyMapping **list,
1850 int *num_list_entries,
1851 int base_index, int ext1_index,
1852 int ext2_index, int ext3_index,
1855 struct PropertyMapping *new_list_entry;
1857 (*num_list_entries)++;
1858 *list = checked_realloc(*list,
1859 *num_list_entries * sizeof(struct PropertyMapping));
1860 new_list_entry = &(*list)[*num_list_entries - 1];
1862 new_list_entry->base_index = base_index;
1863 new_list_entry->ext1_index = ext1_index;
1864 new_list_entry->ext2_index = ext2_index;
1865 new_list_entry->ext3_index = ext3_index;
1867 new_list_entry->artwork_index = artwork_index;
1870 void LoadArtworkConfig(struct ArtworkListInfo *artwork_info)
1872 struct FileInfo *file_list = artwork_info->file_list;
1873 struct ConfigInfo *suffix_list = artwork_info->suffix_list;
1874 char **base_prefixes = artwork_info->base_prefixes;
1875 char **ext1_suffixes = artwork_info->ext1_suffixes;
1876 char **ext2_suffixes = artwork_info->ext2_suffixes;
1877 char **ext3_suffixes = artwork_info->ext3_suffixes;
1878 char **ignore_tokens = artwork_info->ignore_tokens;
1879 int num_file_list_entries = artwork_info->num_file_list_entries;
1880 int num_suffix_list_entries = artwork_info->num_suffix_list_entries;
1881 int num_base_prefixes = artwork_info->num_base_prefixes;
1882 int num_ext1_suffixes = artwork_info->num_ext1_suffixes;
1883 int num_ext2_suffixes = artwork_info->num_ext2_suffixes;
1884 int num_ext3_suffixes = artwork_info->num_ext3_suffixes;
1885 int num_ignore_tokens = artwork_info->num_ignore_tokens;
1886 char *filename = getCustomArtworkConfigFilename(artwork_info->type);
1887 struct SetupFileList *setup_file_list;
1888 struct SetupFileList *extra_file_list = NULL;
1889 struct SetupFileList *list;
1890 char *known_token_value = KNOWN_TOKEN_VALUE;
1894 printf("GOT CUSTOM ARTWORK CONFIG FILE '%s'\n", filename);
1897 /* always start with reliable default values */
1898 for (i=0; i<num_file_list_entries; i++)
1900 setString(&file_list[i].filename, file_list[i].default_filename);
1902 for (j=0; j<num_suffix_list_entries; j++)
1903 setString(&file_list[i].parameter[j], file_list[i].default_parameter[j]);
1905 file_list[i].redefined = FALSE;
1908 /* free previous dynamic artwork file array */
1909 if (artwork_info->dynamic_file_list != NULL)
1911 for (i=0; i<artwork_info->num_dynamic_file_list_entries; i++)
1913 free(artwork_info->dynamic_file_list[i].token);
1914 free(artwork_info->dynamic_file_list[i].filename);
1915 free(artwork_info->dynamic_file_list[i].parameter);
1918 free(artwork_info->dynamic_file_list);
1919 artwork_info->dynamic_file_list = NULL;
1921 FreeCustomArtworkList(artwork_info, &artwork_info->dynamic_artwork_list,
1922 &artwork_info->num_dynamic_file_list_entries);
1925 /* free previous property mapping */
1926 if (artwork_info->property_mapping != NULL)
1928 free(artwork_info->property_mapping);
1930 artwork_info->property_mapping = NULL;
1931 artwork_info->num_property_mapping_entries = 0;
1934 if (filename == NULL)
1937 if ((setup_file_list = loadSetupFileList(filename)) == NULL)
1940 /* read parameters for all known config file tokens */
1941 for (i=0; i<num_file_list_entries; i++)
1942 read_token_parameters(setup_file_list, suffix_list, &file_list[i]);
1944 /* set all tokens that can be ignored here to "known" keyword */
1945 for (i=0; i < num_ignore_tokens; i++)
1946 setTokenValue(setup_file_list, ignore_tokens[i], known_token_value);
1948 /* copy all unknown config file tokens to extra config list */
1949 for (list = setup_file_list; list != NULL; list = list->next)
1951 if (strcmp(list->value, known_token_value) != 0)
1953 if (extra_file_list == NULL)
1954 extra_file_list = newSetupFileList(list->token, list->value);
1956 setTokenValue(extra_file_list, list->token, list->value);
1960 /* at this point, we do not need the config file list anymore -- free it */
1961 freeSetupFileList(setup_file_list);
1963 /* now try to determine valid, dynamically defined config tokens */
1965 for (list = extra_file_list; list != NULL; list = list->next)
1967 struct FileInfo **dynamic_file_list =
1968 &artwork_info->dynamic_file_list;
1969 int *num_dynamic_file_list_entries =
1970 &artwork_info->num_dynamic_file_list_entries;
1971 struct PropertyMapping **property_mapping =
1972 &artwork_info->property_mapping;
1973 int *num_property_mapping_entries =
1974 &artwork_info->num_property_mapping_entries;
1975 int current_summarized_file_list_entry =
1976 artwork_info->num_file_list_entries +
1977 artwork_info->num_dynamic_file_list_entries;
1978 char *token = list->token;
1979 int len_token = strlen(token);
1981 boolean base_prefix_found = FALSE;
1982 boolean parameter_suffix_found = FALSE;
1984 /* skip all parameter definitions (handled by read_token_parameters()) */
1985 for (i=0; i < num_suffix_list_entries && !parameter_suffix_found; i++)
1987 int len_suffix = strlen(suffix_list[i].token);
1989 if (token_suffix_match(token, suffix_list[i].token, -len_suffix))
1990 parameter_suffix_found = TRUE;
1994 if (IS_PARENT_PROCESS())
1996 if (parameter_suffix_found)
1997 printf("---> skipping token '%s' (parameter token)\n", token);
1999 printf("---> examining token '%s': search prefix ...\n", token);
2003 if (parameter_suffix_found)
2006 /* ---------- step 0: search for matching base prefix ---------- */
2009 for (i=0; i<num_base_prefixes && !base_prefix_found; i++)
2011 char *base_prefix = base_prefixes[i];
2012 int len_base_prefix = strlen(base_prefix);
2013 boolean ext1_suffix_found = FALSE;
2014 boolean ext2_suffix_found = FALSE;
2015 boolean ext3_suffix_found = FALSE;
2016 boolean exact_match = FALSE;
2017 int base_index = -1;
2018 int ext1_index = -1;
2019 int ext2_index = -1;
2020 int ext3_index = -1;
2022 base_prefix_found = token_suffix_match(token, base_prefix, start_pos);
2024 if (!base_prefix_found)
2029 if (start_pos + len_base_prefix == len_token) /* exact match */
2033 add_dynamic_file_list_entry(dynamic_file_list,
2034 num_dynamic_file_list_entries,
2037 num_suffix_list_entries,
2039 add_property_mapping(property_mapping,
2040 num_property_mapping_entries,
2041 base_index, -1, -1, -1,
2042 current_summarized_file_list_entry);
2047 if (IS_PARENT_PROCESS())
2048 printf("---> examining token '%s': search 1st suffix ...\n", token);
2051 /* ---------- step 1: search for matching first suffix ---------- */
2053 start_pos += len_base_prefix;
2054 for (j=0; j<num_ext1_suffixes && !ext1_suffix_found; j++)
2056 char *ext1_suffix = ext1_suffixes[j];
2057 int len_ext1_suffix = strlen(ext1_suffix);
2059 ext1_suffix_found = token_suffix_match(token, ext1_suffix, start_pos);
2061 if (!ext1_suffix_found)
2066 if (start_pos + len_ext1_suffix == len_token) /* exact match */
2070 add_dynamic_file_list_entry(dynamic_file_list,
2071 num_dynamic_file_list_entries,
2074 num_suffix_list_entries,
2076 add_property_mapping(property_mapping,
2077 num_property_mapping_entries,
2078 base_index, ext1_index, -1, -1,
2079 current_summarized_file_list_entry);
2083 start_pos += len_ext1_suffix;
2090 if (IS_PARENT_PROCESS())
2091 printf("---> examining token '%s': search 2nd suffix ...\n", token);
2094 /* ---------- step 2: search for matching second suffix ---------- */
2096 for (k=0; k<num_ext2_suffixes && !ext2_suffix_found; k++)
2098 char *ext2_suffix = ext2_suffixes[k];
2099 int len_ext2_suffix = strlen(ext2_suffix);
2101 ext2_suffix_found = token_suffix_match(token, ext2_suffix,start_pos);
2103 if (!ext2_suffix_found)
2108 if (start_pos + len_ext2_suffix == len_token) /* exact match */
2112 add_dynamic_file_list_entry(dynamic_file_list,
2113 num_dynamic_file_list_entries,
2116 num_suffix_list_entries,
2118 add_property_mapping(property_mapping,
2119 num_property_mapping_entries,
2120 base_index, ext1_index, ext2_index, -1,
2121 current_summarized_file_list_entry);
2125 start_pos += len_ext2_suffix;
2132 if (IS_PARENT_PROCESS())
2133 printf("---> examining token '%s': search 3rd suffix ...\n",token);
2136 /* ---------- step 3: search for matching third suffix ---------- */
2138 for (l=0; l<num_ext3_suffixes && !ext3_suffix_found; l++)
2140 char *ext3_suffix = ext3_suffixes[l];
2141 int len_ext3_suffix = strlen(ext3_suffix);
2143 ext3_suffix_found =token_suffix_match(token,ext3_suffix,start_pos);
2145 if (!ext3_suffix_found)
2150 if (start_pos + len_ext3_suffix == len_token) /* exact match */
2154 add_dynamic_file_list_entry(dynamic_file_list,
2155 num_dynamic_file_list_entries,
2158 num_suffix_list_entries,
2160 add_property_mapping(property_mapping,
2161 num_property_mapping_entries,
2162 base_index, ext1_index, ext2_index, ext3_index,
2163 current_summarized_file_list_entry);
2170 if (artwork_info->num_dynamic_file_list_entries > 0)
2172 artwork_info->dynamic_artwork_list =
2173 checked_calloc(artwork_info->num_dynamic_file_list_entries *
2174 artwork_info->sizeof_artwork_list_entry);
2177 if (extra_file_list != NULL && options.verbose && IS_PARENT_PROCESS())
2179 boolean dynamic_tokens_found = FALSE;
2180 boolean unknown_tokens_found = FALSE;
2182 for (list = extra_file_list; list != NULL; list = list->next)
2184 if (strcmp(list->value, known_token_value) == 0)
2185 dynamic_tokens_found = TRUE;
2187 unknown_tokens_found = TRUE;
2191 if (dynamic_tokens_found)
2193 Error(ERR_RETURN_LINE, "-");
2194 Error(ERR_RETURN, "dynamic token(s) found:");
2196 for (list = extra_file_list; list != NULL; list = list->next)
2197 if (strcmp(list->value, known_token_value) == 0)
2198 Error(ERR_RETURN, "- dynamic token: '%s'", list->token);
2200 Error(ERR_RETURN_LINE, "-");
2204 if (unknown_tokens_found)
2206 Error(ERR_RETURN_LINE, "-");
2207 Error(ERR_RETURN, "warning: unknown token(s) found in config file:");
2208 Error(ERR_RETURN, "- config file: '%s'", filename);
2210 for (list = extra_file_list; list != NULL; list = list->next)
2211 if (strcmp(list->value, known_token_value) != 0)
2212 Error(ERR_RETURN, "- unknown token: '%s'", list->token);
2214 Error(ERR_RETURN_LINE, "-");
2218 freeSetupFileList(extra_file_list);
2221 for (i=0; i<num_file_list_entries; i++)
2223 printf("'%s' ", file_list[i].token);
2224 if (file_list[i].filename)
2225 printf("-> '%s'\n", file_list[i].filename);
2227 printf("-> UNDEFINED [-> '%s']\n", file_list[i].default_filename);
2232 static void deleteArtworkListEntry(struct ArtworkListInfo *artwork_info,
2233 struct ListNodeInfo **listnode)
2237 char *filename = (*listnode)->source_filename;
2240 printf("[decrementing reference counter of artwork '%s']\n", filename);
2243 if (--(*listnode)->num_references <= 0)
2246 printf("[deleting artwork '%s']\n", filename);
2249 deleteNodeFromList(&artwork_info->content_list, filename,
2250 artwork_info->free_artwork);
2257 static void replaceArtworkListEntry(struct ArtworkListInfo *artwork_info,
2258 struct ListNodeInfo **listnode,
2263 "Loading graphics:",
2269 char *filename = getCustomArtworkFilename(basename, artwork_info->type);
2271 if (filename == NULL)
2273 int error_mode = ERR_WARN;
2275 /* we can get away without sounds and music, but not without graphics */
2276 if (*listnode == NULL && artwork_info->type == ARTWORK_TYPE_GRAPHICS)
2277 error_mode = ERR_EXIT;
2279 Error(error_mode, "cannot find artwork file '%s'", basename);
2283 /* check if the old and the new artwork file are the same */
2284 if (*listnode && strcmp((*listnode)->source_filename, filename) == 0)
2286 /* The old and new artwork are the same (have the same filename and path).
2287 This usually means that this artwork does not exist in this artwork set
2288 and a fallback to the existing artwork is done. */
2291 printf("[artwork '%s' already exists (same list entry)]\n", filename);
2297 /* delete existing artwork file entry */
2298 deleteArtworkListEntry(artwork_info, listnode);
2300 /* check if the new artwork file already exists in the list of artworks */
2301 if ((node = getNodeFromKey(artwork_info->content_list, filename)) != NULL)
2304 printf("[artwork '%s' already exists (other list entry)]\n", filename);
2307 *listnode = (struct ListNodeInfo *)node->content;
2308 (*listnode)->num_references++;
2313 DrawInitText(init_text[artwork_info->type], 120, FC_GREEN);
2314 DrawInitText(basename, 150, FC_YELLOW);
2316 if ((*listnode = artwork_info->load_artwork(filename)) != NULL)
2319 printf("[adding new artwork '%s']\n", filename);
2322 (*listnode)->num_references = 1;
2323 addNodeToList(&artwork_info->content_list, (*listnode)->source_filename,
2328 int error_mode = ERR_WARN;
2330 /* we can get away without sounds and music, but not without graphics */
2331 if (artwork_info->type == ARTWORK_TYPE_GRAPHICS)
2332 error_mode = ERR_EXIT;
2334 Error(error_mode, "cannot load artwork file '%s'", basename);
2339 static void LoadCustomArtwork(struct ArtworkListInfo *artwork_info,
2340 struct ListNodeInfo **listnode,
2344 printf("GOT CUSTOM ARTWORK FILE '%s'\n", filename);
2347 if (strcmp(basename, UNDEFINED_FILENAME) == 0)
2349 deleteArtworkListEntry(artwork_info, listnode);
2353 replaceArtworkListEntry(artwork_info, listnode, basename);
2356 static void LoadArtworkToList(struct ArtworkListInfo *artwork_info,
2357 struct ListNodeInfo **listnode,
2358 char *basename, int list_pos)
2361 if (artwork_info->artwork_list == NULL ||
2362 list_pos >= artwork_info->num_file_list_entries)
2367 printf("loading artwork '%s' ... [%d]\n",
2368 basename, getNumNodes(artwork_info->content_list));
2372 LoadCustomArtwork(artwork_info, listnode, basename);
2374 LoadCustomArtwork(artwork_info, &artwork_info->artwork_list[list_pos],
2379 printf("loading artwork '%s' done [%d]\n",
2380 basename, getNumNodes(artwork_info->content_list));
2384 void ReloadCustomArtworkList(struct ArtworkListInfo *artwork_info)
2386 struct FileInfo *file_list = artwork_info->file_list;
2387 struct FileInfo *dynamic_file_list = artwork_info->dynamic_file_list;
2388 int num_file_list_entries = artwork_info->num_file_list_entries;
2389 int num_dynamic_file_list_entries =
2390 artwork_info->num_dynamic_file_list_entries;
2394 printf("DEBUG: reloading %d static artwork files ...\n",
2395 num_file_list_entries);
2398 for(i=0; i<num_file_list_entries; i++)
2399 LoadArtworkToList(artwork_info, &artwork_info->artwork_list[i],
2400 file_list[i].filename, i);
2403 printf("DEBUG: reloading %d dynamic artwork files ...\n",
2404 num_dynamic_file_list_entries);
2407 for(i=0; i<num_dynamic_file_list_entries; i++)
2408 LoadArtworkToList(artwork_info, &artwork_info->dynamic_artwork_list[i],
2409 dynamic_file_list[i].filename, i);
2412 dumpList(artwork_info->content_list);
2416 static void FreeCustomArtworkList(struct ArtworkListInfo *artwork_info,
2417 struct ListNodeInfo ***list,
2418 int *num_list_entries)
2425 for(i=0; i<*num_list_entries; i++)
2426 deleteArtworkListEntry(artwork_info, &(*list)[i]);
2430 *num_list_entries = 0;
2433 void FreeCustomArtworkLists(struct ArtworkListInfo *artwork_info)
2435 if (artwork_info == NULL)
2439 printf("%s: FREEING ARTWORK ...\n",
2440 IS_CHILD_PROCESS() ? "CHILD" : "PARENT");
2443 FreeCustomArtworkList(artwork_info, &artwork_info->artwork_list,
2444 &artwork_info->num_file_list_entries);
2446 FreeCustomArtworkList(artwork_info, &artwork_info->dynamic_artwork_list,
2447 &artwork_info->num_dynamic_file_list_entries);
2450 printf("%s: FREEING ARTWORK -- DONE\n",
2451 IS_CHILD_PROCESS() ? "CHILD" : "PARENT");
2456 /* ------------------------------------------------------------------------- */
2457 /* functions only needed for non-Unix (non-command-line) systems */
2458 /* (MS-DOS only; SDL/Windows creates files "stdout.txt" and "stderr.txt") */
2459 /* ------------------------------------------------------------------------- */
2461 #if defined(PLATFORM_MSDOS)
2463 #define ERROR_FILENAME "stderr.txt"
2465 void initErrorFile()
2467 unlink(ERROR_FILENAME);
2470 FILE *openErrorFile()
2472 return fopen(ERROR_FILENAME, MODE_APPEND);
2475 void dumpErrorFile()
2477 FILE *error_file = fopen(ERROR_FILENAME, MODE_READ);
2479 if (error_file != NULL)
2481 while (!feof(error_file))
2482 fputc(fgetc(error_file), stderr);
2490 /* ------------------------------------------------------------------------- */
2491 /* the following is only for debugging purpose and normally not used */
2492 /* ------------------------------------------------------------------------- */
2494 #define DEBUG_NUM_TIMESTAMPS 3
2496 void debug_print_timestamp(int counter_nr, char *message)
2498 static long counter[DEBUG_NUM_TIMESTAMPS][2];
2500 if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
2501 Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
2503 counter[counter_nr][0] = Counter();
2506 printf("%s %.2f seconds\n", message,
2507 (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
2509 counter[counter_nr][1] = Counter();
2512 void debug_print_parent_only(char *format, ...)
2514 if (!IS_PARENT_PROCESS())
2521 va_start(ap, format);
2522 vprintf(format, ap);