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 static char *getStringCat2(char *s1, char *s2)
447 char *complete_string = checked_malloc(strlen(s1) + strlen(s2) + 1);
449 sprintf(complete_string, "%s%s", s1, s2);
450 return complete_string;
453 char *getStringCopy(char *s)
460 s_copy = checked_malloc(strlen(s) + 1);
466 char *getStringToLower(char *s)
468 char *s_copy = checked_malloc(strlen(s) + 1);
469 char *s_ptr = s_copy;
472 *s_ptr++ = tolower(*s++);
479 /* ------------------------------------------------------------------------- */
480 /* command line option handling functions */
481 /* ------------------------------------------------------------------------- */
483 static void printUsage()
486 "Usage: %s [OPTION]... [HOSTNAME [PORT]]\n"
489 " -d, --display HOSTNAME[:SCREEN] specify X server display\n"
490 " -b, --basepath DIRECTORY alternative base DIRECTORY\n"
491 " -l, --level DIRECTORY alternative level DIRECTORY\n"
492 " -g, --graphics DIRECTORY alternative graphics DIRECTORY\n"
493 " -s, --sounds DIRECTORY alternative sounds DIRECTORY\n"
494 " -m, --music DIRECTORY alternative music DIRECTORY\n"
495 " -n, --network network multiplayer game\n"
496 " --serveronly only start network server\n"
497 " -v, --verbose verbose mode\n"
498 " --debug display debugging information\n"
499 " -e, --execute COMMAND execute batch COMMAND:\n"
501 "Valid commands for '--execute' option:\n"
502 " \"print graphicsinfo.conf\" print default graphics config\n"
503 " \"print soundsinfo.conf\" print default sounds config\n"
504 " \"print musicinfo.conf\" print default music config\n"
505 " \"dump level FILE\" dump level data from FILE\n"
506 " \"dump tape FILE\" dump tape data from FILE\n"
507 " \"autoplay LEVELDIR\" play level tapes for LEVELDIR\n"
509 program.command_basename);
512 void GetOptions(char *argv[])
514 char **options_left = &argv[1];
516 /* initialize global program options */
517 options.display_name = NULL;
518 options.server_host = NULL;
519 options.server_port = 0;
520 options.ro_base_directory = RO_BASE_PATH;
521 options.rw_base_directory = RW_BASE_PATH;
522 options.level_directory = RO_BASE_PATH "/" LEVELS_DIRECTORY;
523 options.graphics_directory = RO_BASE_PATH "/" GRAPHICS_DIRECTORY;
524 options.sounds_directory = RO_BASE_PATH "/" SOUNDS_DIRECTORY;
525 options.music_directory = RO_BASE_PATH "/" MUSIC_DIRECTORY;
526 options.execute_command = NULL;
527 options.serveronly = FALSE;
528 options.network = FALSE;
529 options.verbose = FALSE;
530 options.debug = FALSE;
532 while (*options_left)
534 char option_str[MAX_OPTION_LEN];
535 char *option = options_left[0];
536 char *next_option = options_left[1];
537 char *option_arg = NULL;
538 int option_len = strlen(option);
540 if (option_len >= MAX_OPTION_LEN)
541 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
543 strcpy(option_str, option); /* copy argument into buffer */
546 if (strcmp(option, "--") == 0) /* stop scanning arguments */
549 if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */
552 option_arg = strchr(option, '=');
553 if (option_arg == NULL) /* no '=' in option */
554 option_arg = next_option;
557 *option_arg++ = '\0'; /* cut argument from option */
558 if (*option_arg == '\0') /* no argument after '=' */
559 Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
562 option_len = strlen(option);
564 if (strcmp(option, "-") == 0)
565 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
566 else if (strncmp(option, "-help", option_len) == 0)
572 else if (strncmp(option, "-display", option_len) == 0)
574 if (option_arg == NULL)
575 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
577 options.display_name = option_arg;
578 if (option_arg == next_option)
581 else if (strncmp(option, "-basepath", option_len) == 0)
583 if (option_arg == NULL)
584 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
586 /* this should be extended to separate options for ro and rw data */
587 options.ro_base_directory = option_arg;
588 options.rw_base_directory = option_arg;
589 if (option_arg == next_option)
592 /* adjust path for level directory accordingly */
593 options.level_directory =
594 getPath2(options.ro_base_directory, LEVELS_DIRECTORY);
596 else if (strncmp(option, "-levels", option_len) == 0)
598 if (option_arg == NULL)
599 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
601 options.level_directory = option_arg;
602 if (option_arg == next_option)
605 else if (strncmp(option, "-graphics", option_len) == 0)
607 if (option_arg == NULL)
608 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
610 options.graphics_directory = option_arg;
611 if (option_arg == next_option)
614 else if (strncmp(option, "-sounds", option_len) == 0)
616 if (option_arg == NULL)
617 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
619 options.sounds_directory = option_arg;
620 if (option_arg == next_option)
623 else if (strncmp(option, "-music", option_len) == 0)
625 if (option_arg == NULL)
626 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
628 options.music_directory = option_arg;
629 if (option_arg == next_option)
632 else if (strncmp(option, "-network", option_len) == 0)
634 options.network = TRUE;
636 else if (strncmp(option, "-serveronly", option_len) == 0)
638 options.serveronly = TRUE;
640 else if (strncmp(option, "-verbose", option_len) == 0)
642 options.verbose = TRUE;
644 else if (strncmp(option, "-debug", option_len) == 0)
646 options.debug = TRUE;
648 else if (strncmp(option, "-execute", option_len) == 0)
650 if (option_arg == NULL)
651 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
653 options.execute_command = option_arg;
654 if (option_arg == next_option)
657 else if (*option == '-')
659 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
661 else if (options.server_host == NULL)
663 options.server_host = *options_left;
665 else if (options.server_port == 0)
667 options.server_port = atoi(*options_left);
668 if (options.server_port < 1024)
669 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
672 Error(ERR_EXIT_HELP, "too many arguments");
679 /* ------------------------------------------------------------------------- */
680 /* error handling functions */
681 /* ------------------------------------------------------------------------- */
683 /* used by SetError() and GetError() to store internal error messages */
684 static char internal_error[1024]; /* this is bad */
686 void SetError(char *format, ...)
690 va_start(ap, format);
691 vsprintf(internal_error, format, ap);
697 return internal_error;
700 void Error(int mode, char *format, ...)
702 static boolean last_line_was_separator = FALSE;
703 char *process_name = "";
704 FILE *error = stderr;
705 char *newline = "\n";
707 /* display warnings only when running in verbose mode */
708 if (mode & ERR_WARN && !options.verbose)
711 if (mode == ERR_RETURN_LINE)
713 if (!last_line_was_separator)
714 fprintf_line(error, format, 79);
716 last_line_was_separator = TRUE;
721 last_line_was_separator = FALSE;
723 #if defined(PLATFORM_MSDOS)
726 if ((error = openErrorFile()) == NULL)
728 printf("Cannot write to error output file!%s", newline);
729 program.exit_function(1);
733 if (mode & ERR_SOUND_SERVER)
734 process_name = " sound server";
735 else if (mode & ERR_NETWORK_SERVER)
736 process_name = " network server";
737 else if (mode & ERR_NETWORK_CLIENT)
738 process_name = " network client **";
744 fprintf(error, "%s%s: ", program.command_basename, process_name);
747 fprintf(error, "warning: ");
749 va_start(ap, format);
750 vfprintf(error, format, ap);
753 fprintf(error, "%s", newline);
757 fprintf(error, "%s: Try option '--help' for more information.%s",
758 program.command_basename, newline);
761 fprintf(error, "%s%s: aborting%s",
762 program.command_basename, process_name, newline);
769 if (mode & ERR_FROM_SERVER)
770 exit(1); /* child process: normal exit */
772 program.exit_function(1); /* main process: clean up stuff */
777 /* ------------------------------------------------------------------------- */
778 /* memory allocation functions */
779 /* ------------------------------------------------------------------------- */
781 void *checked_malloc(unsigned long size)
788 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
793 void *checked_calloc(unsigned long size)
797 ptr = calloc(1, size);
800 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
805 void *checked_realloc(void *ptr, unsigned long size)
807 ptr = realloc(ptr, size);
810 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
816 /* ------------------------------------------------------------------------- */
817 /* various helper functions */
818 /* ------------------------------------------------------------------------- */
820 inline void swap_numbers(int *i1, int *i2)
828 inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2)
840 short getFile16BitInteger(FILE *file, int byte_order)
842 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
843 return ((fgetc(file) << 8) |
845 else /* BYTE_ORDER_LITTLE_ENDIAN */
846 return ((fgetc(file) << 0) |
850 void putFile16BitInteger(FILE *file, short value, int byte_order)
852 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
854 fputc((value >> 8) & 0xff, file);
855 fputc((value >> 0) & 0xff, file);
857 else /* BYTE_ORDER_LITTLE_ENDIAN */
859 fputc((value >> 0) & 0xff, file);
860 fputc((value >> 8) & 0xff, file);
864 int getFile32BitInteger(FILE *file, int byte_order)
866 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
867 return ((fgetc(file) << 24) |
868 (fgetc(file) << 16) |
871 else /* BYTE_ORDER_LITTLE_ENDIAN */
872 return ((fgetc(file) << 0) |
874 (fgetc(file) << 16) |
875 (fgetc(file) << 24));
878 void putFile32BitInteger(FILE *file, int value, int byte_order)
880 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
882 fputc((value >> 24) & 0xff, file);
883 fputc((value >> 16) & 0xff, file);
884 fputc((value >> 8) & 0xff, file);
885 fputc((value >> 0) & 0xff, file);
887 else /* BYTE_ORDER_LITTLE_ENDIAN */
889 fputc((value >> 0) & 0xff, file);
890 fputc((value >> 8) & 0xff, file);
891 fputc((value >> 16) & 0xff, file);
892 fputc((value >> 24) & 0xff, file);
896 boolean getFileChunk(FILE *file, char *chunk_name, int *chunk_size,
899 const int chunk_name_length = 4;
901 /* read chunk name */
902 fgets(chunk_name, chunk_name_length + 1, file);
904 if (chunk_size != NULL)
906 /* read chunk size */
907 *chunk_size = getFile32BitInteger(file, byte_order);
910 return (feof(file) || ferror(file) ? FALSE : TRUE);
913 void putFileChunk(FILE *file, char *chunk_name, int chunk_size,
916 /* write chunk name */
917 fputs(chunk_name, file);
921 /* write chunk size */
922 putFile32BitInteger(file, chunk_size, byte_order);
926 int getFileVersion(FILE *file)
928 int version_major, version_minor, version_patch;
930 version_major = fgetc(file);
931 version_minor = fgetc(file);
932 version_patch = fgetc(file);
933 fgetc(file); /* not used */
935 return VERSION_IDENT(version_major, version_minor, version_patch);
938 void putFileVersion(FILE *file, int version)
940 int version_major = VERSION_MAJOR(version);
941 int version_minor = VERSION_MINOR(version);
942 int version_patch = VERSION_PATCH(version);
944 fputc(version_major, file);
945 fputc(version_minor, file);
946 fputc(version_patch, file);
947 fputc(0, file); /* not used */
950 void ReadUnusedBytesFromFile(FILE *file, unsigned long bytes)
952 while (bytes-- && !feof(file))
956 void WriteUnusedBytesToFile(FILE *file, unsigned long bytes)
963 /* ------------------------------------------------------------------------- */
964 /* functions to translate key identifiers between different format */
965 /* ------------------------------------------------------------------------- */
967 #define TRANSLATE_KEYSYM_TO_KEYNAME 0
968 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
969 #define TRANSLATE_KEYNAME_TO_KEYSYM 2
970 #define TRANSLATE_X11KEYNAME_TO_KEYSYM 3
972 void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
981 /* normal cursor keys */
982 { KSYM_Left, "XK_Left", "cursor left" },
983 { KSYM_Right, "XK_Right", "cursor right" },
984 { KSYM_Up, "XK_Up", "cursor up" },
985 { KSYM_Down, "XK_Down", "cursor down" },
987 /* keypad cursor keys */
989 { KSYM_KP_Left, "XK_KP_Left", "keypad left" },
990 { KSYM_KP_Right, "XK_KP_Right", "keypad right" },
991 { KSYM_KP_Up, "XK_KP_Up", "keypad up" },
992 { KSYM_KP_Down, "XK_KP_Down", "keypad down" },
995 /* other keypad keys */
997 { KSYM_KP_Enter, "XK_KP_Enter", "keypad enter" },
998 { KSYM_KP_Add, "XK_KP_Add", "keypad +" },
999 { KSYM_KP_Subtract, "XK_KP_Subtract", "keypad -" },
1000 { KSYM_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
1001 { KSYM_KP_Divide, "XK_KP_Divide", "keypad /" },
1002 { KSYM_KP_Separator,"XK_KP_Separator", "keypad ," },
1006 { KSYM_Shift_L, "XK_Shift_L", "left shift" },
1007 { KSYM_Shift_R, "XK_Shift_R", "right shift" },
1008 { KSYM_Control_L, "XK_Control_L", "left control" },
1009 { KSYM_Control_R, "XK_Control_R", "right control" },
1010 { KSYM_Meta_L, "XK_Meta_L", "left meta" },
1011 { KSYM_Meta_R, "XK_Meta_R", "right meta" },
1012 { KSYM_Alt_L, "XK_Alt_L", "left alt" },
1013 { KSYM_Alt_R, "XK_Alt_R", "right alt" },
1014 { KSYM_Super_L, "XK_Super_L", "left super" }, /* Win-L */
1015 { KSYM_Super_R, "XK_Super_R", "right super" }, /* Win-R */
1016 { KSYM_Mode_switch, "XK_Mode_switch", "mode switch" }, /* Alt-R */
1017 { KSYM_Multi_key, "XK_Multi_key", "multi key" }, /* Ctrl-R */
1019 /* some special keys */
1020 { KSYM_BackSpace, "XK_BackSpace", "backspace" },
1021 { KSYM_Delete, "XK_Delete", "delete" },
1022 { KSYM_Insert, "XK_Insert", "insert" },
1023 { KSYM_Tab, "XK_Tab", "tab" },
1024 { KSYM_Home, "XK_Home", "home" },
1025 { KSYM_End, "XK_End", "end" },
1026 { KSYM_Page_Up, "XK_Page_Up", "page up" },
1027 { KSYM_Page_Down, "XK_Page_Down", "page down" },
1028 { KSYM_Menu, "XK_Menu", "menu" }, /* Win-Menu */
1030 /* ASCII 0x20 to 0x40 keys (except numbers) */
1031 { KSYM_space, "XK_space", "space" },
1032 { KSYM_exclam, "XK_exclam", "!" },
1033 { KSYM_quotedbl, "XK_quotedbl", "\"" },
1034 { KSYM_numbersign, "XK_numbersign", "#" },
1035 { KSYM_dollar, "XK_dollar", "$" },
1036 { KSYM_percent, "XK_percent", "%" },
1037 { KSYM_ampersand, "XK_ampersand", "&" },
1038 { KSYM_apostrophe, "XK_apostrophe", "'" },
1039 { KSYM_parenleft, "XK_parenleft", "(" },
1040 { KSYM_parenright, "XK_parenright", ")" },
1041 { KSYM_asterisk, "XK_asterisk", "*" },
1042 { KSYM_plus, "XK_plus", "+" },
1043 { KSYM_comma, "XK_comma", "," },
1044 { KSYM_minus, "XK_minus", "-" },
1045 { KSYM_period, "XK_period", "." },
1046 { KSYM_slash, "XK_slash", "/" },
1047 { KSYM_colon, "XK_colon", ":" },
1048 { KSYM_semicolon, "XK_semicolon", ";" },
1049 { KSYM_less, "XK_less", "<" },
1050 { KSYM_equal, "XK_equal", "=" },
1051 { KSYM_greater, "XK_greater", ">" },
1052 { KSYM_question, "XK_question", "?" },
1053 { KSYM_at, "XK_at", "@" },
1055 /* more ASCII keys */
1056 { KSYM_bracketleft, "XK_bracketleft", "[" },
1057 { KSYM_backslash, "XK_backslash", "backslash" },
1058 { KSYM_bracketright,"XK_bracketright", "]" },
1059 { KSYM_asciicircum, "XK_asciicircum", "circumflex" },
1060 { KSYM_underscore, "XK_underscore", "_" },
1061 { KSYM_grave, "XK_grave", "grave" },
1062 { KSYM_quoteleft, "XK_quoteleft", "quote left" },
1063 { KSYM_braceleft, "XK_braceleft", "brace left" },
1064 { KSYM_bar, "XK_bar", "bar" },
1065 { KSYM_braceright, "XK_braceright", "brace right" },
1066 { KSYM_asciitilde, "XK_asciitilde", "ascii tilde" },
1068 /* special (non-ASCII) keys */
1069 { KSYM_Adiaeresis, "XK_Adiaeresis", "Ä" },
1070 { KSYM_Odiaeresis, "XK_Odiaeresis", "Ö" },
1071 { KSYM_Udiaeresis, "XK_Udiaeresis", "Ü" },
1072 { KSYM_adiaeresis, "XK_adiaeresis", "ä" },
1073 { KSYM_odiaeresis, "XK_odiaeresis", "ö" },
1074 { KSYM_udiaeresis, "XK_udiaeresis", "ü" },
1075 { KSYM_ssharp, "XK_ssharp", "sharp s" },
1077 /* end-of-array identifier */
1083 if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
1085 static char name_buffer[30];
1088 if (key >= KSYM_A && key <= KSYM_Z)
1089 sprintf(name_buffer, "%c", 'A' + (char)(key - KSYM_A));
1090 else if (key >= KSYM_a && key <= KSYM_z)
1091 sprintf(name_buffer, "%c", 'a' + (char)(key - KSYM_a));
1092 else if (key >= KSYM_0 && key <= KSYM_9)
1093 sprintf(name_buffer, "%c", '0' + (char)(key - KSYM_0));
1094 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
1095 sprintf(name_buffer, "keypad %c", '0' + (char)(key - KSYM_KP_0));
1096 else if (key >= KSYM_FKEY_FIRST && key <= KSYM_FKEY_LAST)
1097 sprintf(name_buffer, "function F%d", (int)(key - KSYM_FKEY_FIRST + 1));
1098 else if (key == KSYM_UNDEFINED)
1099 strcpy(name_buffer, "(undefined)");
1106 if (key == translate_key[i].key)
1108 strcpy(name_buffer, translate_key[i].name);
1112 while (translate_key[++i].name);
1114 if (!translate_key[i].name)
1115 strcpy(name_buffer, "(unknown)");
1118 *name = name_buffer;
1120 else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME)
1122 static char name_buffer[30];
1125 if (key >= KSYM_A && key <= KSYM_Z)
1126 sprintf(name_buffer, "XK_%c", 'A' + (char)(key - KSYM_A));
1127 else if (key >= KSYM_a && key <= KSYM_z)
1128 sprintf(name_buffer, "XK_%c", 'a' + (char)(key - KSYM_a));
1129 else if (key >= KSYM_0 && key <= KSYM_9)
1130 sprintf(name_buffer, "XK_%c", '0' + (char)(key - KSYM_0));
1131 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
1132 sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - KSYM_KP_0));
1133 else if (key >= KSYM_FKEY_FIRST && key <= KSYM_FKEY_LAST)
1134 sprintf(name_buffer, "XK_F%d", (int)(key - KSYM_FKEY_FIRST + 1));
1135 else if (key == KSYM_UNDEFINED)
1136 strcpy(name_buffer, "[undefined]");
1143 if (key == translate_key[i].key)
1145 strcpy(name_buffer, translate_key[i].x11name);
1149 while (translate_key[++i].x11name);
1151 if (!translate_key[i].x11name)
1152 sprintf(name_buffer, "0x%04lx", (unsigned long)key);
1155 *x11name = name_buffer;
1157 else if (mode == TRANSLATE_KEYNAME_TO_KEYSYM)
1159 Key key = KSYM_UNDEFINED;
1164 if (strcmp(translate_key[i].name, *name) == 0)
1166 key = translate_key[i].key;
1170 while (translate_key[++i].x11name);
1172 if (key == KSYM_UNDEFINED)
1173 Error(ERR_WARN, "getKeyFromKeyName(): not completely implemented");
1177 else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
1179 Key key = KSYM_UNDEFINED;
1180 char *name_ptr = *x11name;
1182 if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4)
1184 char c = name_ptr[3];
1186 if (c >= 'A' && c <= 'Z')
1187 key = KSYM_A + (Key)(c - 'A');
1188 else if (c >= 'a' && c <= 'z')
1189 key = KSYM_a + (Key)(c - 'a');
1190 else if (c >= '0' && c <= '9')
1191 key = KSYM_0 + (Key)(c - '0');
1193 else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7)
1195 char c = name_ptr[6];
1197 if (c >= '0' && c <= '9')
1198 key = KSYM_0 + (Key)(c - '0');
1200 else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
1202 char c1 = name_ptr[4];
1203 char c2 = name_ptr[5];
1206 if ((c1 >= '0' && c1 <= '9') &&
1207 ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
1208 d = atoi(&name_ptr[4]);
1210 if (d >= 1 && d <= KSYM_NUM_FKEYS)
1211 key = KSYM_F1 + (Key)(d - 1);
1213 else if (strncmp(name_ptr, "XK_", 3) == 0)
1219 if (strcmp(name_ptr, translate_key[i].x11name) == 0)
1221 key = translate_key[i].key;
1225 while (translate_key[++i].x11name);
1227 else if (strncmp(name_ptr, "0x", 2) == 0)
1229 unsigned long value = 0;
1235 char c = *name_ptr++;
1238 if (c >= '0' && c <= '9')
1240 else if (c >= 'a' && c <= 'f')
1241 d = (int)(c - 'a' + 10);
1242 else if (c >= 'A' && c <= 'F')
1243 d = (int)(c - 'A' + 10);
1251 value = value * 16 + d;
1262 char *getKeyNameFromKey(Key key)
1266 translate_keyname(&key, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME);
1270 char *getX11KeyNameFromKey(Key key)
1274 translate_keyname(&key, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME);
1278 Key getKeyFromKeyName(char *name)
1282 translate_keyname(&key, NULL, &name, TRANSLATE_KEYNAME_TO_KEYSYM);
1286 Key getKeyFromX11KeyName(char *x11name)
1290 translate_keyname(&key, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM);
1294 char getCharFromKey(Key key)
1296 char *keyname = getKeyNameFromKey(key);
1299 if (strlen(keyname) == 1)
1300 letter = keyname[0];
1301 else if (strcmp(keyname, "space") == 0)
1303 else if (strcmp(keyname, "circumflex") == 0)
1310 /* ------------------------------------------------------------------------- */
1311 /* functions to translate string identifiers to integer or boolean value */
1312 /* ------------------------------------------------------------------------- */
1314 int get_integer_from_string(char *s)
1316 static char *number_text[][3] =
1318 { "0", "zero", "null", },
1319 { "1", "one", "first" },
1320 { "2", "two", "second" },
1321 { "3", "three", "third" },
1322 { "4", "four", "fourth" },
1323 { "5", "five", "fifth" },
1324 { "6", "six", "sixth" },
1325 { "7", "seven", "seventh" },
1326 { "8", "eight", "eighth" },
1327 { "9", "nine", "ninth" },
1328 { "10", "ten", "tenth" },
1329 { "11", "eleven", "eleventh" },
1330 { "12", "twelve", "twelfth" },
1334 char *s_lower = getStringToLower(s);
1337 for (i=0; i<13; i++)
1339 if (strcmp(s_lower, number_text[i][j]) == 0)
1350 boolean get_boolean_from_string(char *s)
1352 char *s_lower = getStringToLower(s);
1353 boolean result = FALSE;
1355 if (strcmp(s_lower, "true") == 0 ||
1356 strcmp(s_lower, "yes") == 0 ||
1357 strcmp(s_lower, "on") == 0 ||
1358 get_integer_from_string(s) == 1)
1367 /* ------------------------------------------------------------------------- */
1368 /* functions for generic lists */
1369 /* ------------------------------------------------------------------------- */
1371 ListNode *newListNode()
1373 return checked_calloc(sizeof(ListNode));
1376 void addNodeToList(ListNode **node_first, char *key, void *content)
1378 ListNode *node_new = newListNode();
1381 printf("LIST: adding node with key '%s'\n", key);
1384 node_new->key = getStringCopy(key);
1385 node_new->content = content;
1386 node_new->next = *node_first;
1387 *node_first = node_new;
1390 void deleteNodeFromList(ListNode **node_first, char *key,
1391 void (*destructor_function)(void *))
1393 if (node_first == NULL || *node_first == NULL)
1397 printf("[CHECKING LIST KEY '%s' == '%s']\n",
1398 (*node_first)->key, key);
1401 if (strcmp((*node_first)->key, key) == 0)
1404 printf("[DELETING LIST ENTRY]\n");
1407 free((*node_first)->key);
1408 if (destructor_function)
1409 destructor_function((*node_first)->content);
1410 *node_first = (*node_first)->next;
1413 deleteNodeFromList(&(*node_first)->next, key, destructor_function);
1416 ListNode *getNodeFromKey(ListNode *node_first, char *key)
1418 if (node_first == NULL)
1421 if (strcmp(node_first->key, key) == 0)
1424 return getNodeFromKey(node_first->next, key);
1427 int getNumNodes(ListNode *node_first)
1429 return (node_first ? 1 + getNumNodes(node_first->next) : 0);
1432 void dumpList(ListNode *node_first)
1434 ListNode *node = node_first;
1438 printf("['%s' (%d)]\n", node->key,
1439 ((struct ListNodeInfo *)node->content)->num_references);
1443 printf("[%d nodes]\n", getNumNodes(node_first));
1447 /* ------------------------------------------------------------------------- */
1448 /* functions for checking filenames */
1449 /* ------------------------------------------------------------------------- */
1451 boolean FileIsGraphic(char *filename)
1453 if (strlen(filename) > 4 &&
1454 strcmp(&filename[strlen(filename) - 4], ".pcx") == 0)
1460 boolean FileIsSound(char *basename)
1462 if (strlen(basename) > 4 &&
1463 strcmp(&basename[strlen(basename) - 4], ".wav") == 0)
1469 boolean FileIsMusic(char *basename)
1471 /* "music" can be a WAV (loop) file or (if compiled with SDL) a MOD file */
1473 if (FileIsSound(basename))
1476 #if defined(TARGET_SDL)
1477 if (strlen(basename) > 4 &&
1478 (strcmp(&basename[strlen(basename) - 4], ".mod") == 0 ||
1479 strcmp(&basename[strlen(basename) - 4], ".MOD") == 0 ||
1480 strncmp(basename, "mod.", 4) == 0 ||
1481 strncmp(basename, "MOD.", 4) == 0))
1488 boolean FileIsArtworkType(char *basename, int type)
1490 if ((type == TREE_TYPE_GRAPHICS_DIR && FileIsGraphic(basename)) ||
1491 (type == TREE_TYPE_SOUNDS_DIR && FileIsSound(basename)) ||
1492 (type == TREE_TYPE_MUSIC_DIR && FileIsMusic(basename)))
1498 /* ------------------------------------------------------------------------- */
1499 /* functions for loading artwork configuration information */
1500 /* ------------------------------------------------------------------------- */
1502 static int get_parameter_value(int type, char *value)
1504 return (strcmp(value, ARG_UNDEFINED) == 0 ? ARG_UNDEFINED_VALUE :
1505 type == TYPE_INTEGER ? get_integer_from_string(value) :
1506 type == TYPE_BOOLEAN ? get_boolean_from_string(value) :
1510 struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list,
1511 struct ConfigInfo *suffix_list,
1512 int num_file_list_entries)
1514 struct FileInfo *file_list;
1515 int num_file_list_entries_found = 0;
1516 int num_suffix_list_entries = 0;
1520 file_list = checked_calloc(num_file_list_entries * sizeof(struct FileInfo));
1522 for (i=0; suffix_list[i].token != NULL; i++)
1523 num_suffix_list_entries++;
1525 /* always start with reliable default values */
1526 for (i=0; i<num_file_list_entries; i++)
1528 file_list[i].token = NULL;
1529 file_list[i].default_filename = NULL;
1530 file_list[i].filename = NULL;
1532 if (num_suffix_list_entries > 0)
1534 int parameter_array_size = num_suffix_list_entries * sizeof(int);
1536 file_list[i].default_parameter = checked_calloc(parameter_array_size);
1537 file_list[i].parameter = checked_calloc(parameter_array_size);
1539 for (j=0; j<num_suffix_list_entries; j++)
1541 int default_parameter =
1542 get_parameter_value(suffix_list[j].type, suffix_list[j].value);
1544 file_list[i].default_parameter[j] = default_parameter;
1545 file_list[i].parameter[j] = default_parameter;
1550 for (i=0; config_list[i].token != NULL; i++)
1552 int len_config_token = strlen(config_list[i].token);
1553 int len_config_value = strlen(config_list[i].value);
1554 boolean is_file_entry = TRUE;
1556 for (j=0; suffix_list[j].token != NULL; j++)
1558 int len_suffix = strlen(suffix_list[j].token);
1560 if (len_suffix < len_config_token &&
1561 strcmp(&config_list[i].token[len_config_token - len_suffix],
1562 suffix_list[j].token) == 0)
1564 file_list[list_pos].default_parameter[j] =
1565 get_parameter_value(suffix_list[j].type, config_list[i].value);
1567 is_file_entry = FALSE;
1577 if (list_pos >= num_file_list_entries)
1580 /* simple sanity check if this is really a file definition */
1581 if (strcmp(&config_list[i].value[len_config_value - 4], ".pcx") != 0 &&
1582 strcmp(&config_list[i].value[len_config_value - 4], ".wav") != 0 &&
1583 strcmp(config_list[i].value, UNDEFINED_FILENAME) != 0)
1585 Error(ERR_RETURN, "Configuration directive '%s' -> '%s':",
1586 config_list[i].token, config_list[i].value);
1587 Error(ERR_EXIT, "This seems to be no valid definition -- please fix");
1590 file_list[list_pos].token = config_list[i].token;
1591 file_list[list_pos].default_filename = config_list[i].value;
1595 num_file_list_entries_found = list_pos + 1;
1596 if (num_file_list_entries_found != num_file_list_entries)
1598 Error(ERR_RETURN, "inconsistant config list information:");
1599 Error(ERR_RETURN, "- should be: %d (according to 'src/conf_gfx.h')",
1600 num_file_list_entries);
1601 Error(ERR_RETURN, "- found to be: %d (according to 'src/conf_gfx.c')",
1602 num_file_list_entries_found);
1603 Error(ERR_EXIT, "please fix");
1609 void LoadArtworkConfig(struct ArtworkListInfo *artwork_info)
1611 struct FileInfo *file_list = artwork_info->file_list;
1612 struct ConfigInfo *suffix_list = artwork_info->suffix_list;
1613 int num_file_list_entries = artwork_info->num_file_list_entries;
1614 int num_suffix_list_entries = artwork_info->num_suffix_list_entries;
1615 char *filename = getCustomArtworkConfigFilename(artwork_info->type);
1616 struct SetupFileList *setup_file_list;
1617 struct SetupFileList *extra_file_list = NULL;
1618 char *known_token_value = "[KNOWN_TOKEN]";
1622 printf("GOT CUSTOM ARTWORK CONFIG FILE '%s'\n", filename);
1625 /* always start with reliable default values */
1626 for (i=0; i<num_file_list_entries; i++)
1628 if (file_list[i].filename != NULL)
1629 free(file_list[i].filename);
1630 file_list[i].filename = NULL;
1632 for (j=0; j<num_suffix_list_entries; j++)
1633 file_list[i].parameter[j] = file_list[i].default_parameter[j];
1636 if (filename == NULL)
1639 if ((setup_file_list = loadSetupFileList(filename)) == NULL)
1642 for (i=0; i<num_file_list_entries; i++)
1644 /* check for config token that is the base token without any suffixes */
1645 char *filename = getTokenValue(setup_file_list, file_list[i].token);
1647 if (filename != NULL)
1649 for (j=0; j<num_suffix_list_entries; j++)
1650 file_list[i].parameter[j] =
1651 get_parameter_value(suffix_list[j].type, suffix_list[j].value);
1653 file_list[i].filename = getStringCopy(filename);
1655 /* mark token as well known from default config */
1656 setTokenValue(setup_file_list, file_list[i].token, known_token_value);
1659 file_list[i].filename = getStringCopy(file_list[i].default_filename);
1661 /* check for config tokens that can be build by base token and suffixes */
1662 for (j=0; j<num_suffix_list_entries; j++)
1664 char *token = getStringCat2(file_list[i].token, suffix_list[j].token);
1665 char *value = getTokenValue(setup_file_list, token);
1669 file_list[i].parameter[j] =
1670 get_parameter_value(suffix_list[j].type, value);
1672 /* mark token as well known from default config */
1673 setTokenValue(setup_file_list, token, known_token_value);
1680 /* set some additional tokens to "known" */
1681 setTokenValue(setup_file_list, "name", known_token_value);
1682 setTokenValue(setup_file_list, "sort_priority", known_token_value);
1684 if (options.verbose && !IS_CHILD_PROCESS(audio.mixer_pid))
1686 boolean unknown_tokens_found = FALSE;
1688 /* check each token in config file if it is defined in default config */
1689 while (setup_file_list != NULL)
1691 if (strcmp(setup_file_list->value, known_token_value) != 0)
1693 if (extra_file_list == NULL)
1694 extra_file_list = newSetupFileList(setup_file_list->token,
1695 setup_file_list->value);
1697 setTokenValue(extra_file_list, setup_file_list->token,
1698 setup_file_list->value);
1700 if (!unknown_tokens_found)
1702 Error(ERR_RETURN_LINE, "-");
1703 Error(ERR_RETURN, "warning: unknown token(s) found in config file:");
1704 Error(ERR_RETURN, "- config file: '%s'", filename);
1706 unknown_tokens_found = TRUE;
1709 Error(ERR_RETURN, "- unknown token: '%s'", setup_file_list->token);
1712 setup_file_list = setup_file_list->next;
1715 if (unknown_tokens_found)
1716 Error(ERR_RETURN_LINE, "-");
1719 freeSetupFileList(setup_file_list);
1721 freeSetupFileList(extra_file_list);
1724 for (i=0; i<num_file_list_entries; i++)
1726 printf("'%s' ", file_list[i].token);
1727 if (file_list[i].filename)
1728 printf("-> '%s'\n", file_list[i].filename);
1730 printf("-> UNDEFINED [-> '%s']\n", file_list[i].default_filename);
1735 static void deleteArtworkListEntry(struct ArtworkListInfo *artwork_info,
1736 struct ListNodeInfo **listnode)
1740 char *filename = (*listnode)->source_filename;
1743 printf("[decrementing reference counter of artwork '%s']\n", filename);
1746 if (--(*listnode)->num_references <= 0)
1749 printf("[deleting artwork '%s']\n", filename);
1752 deleteNodeFromList(&artwork_info->content_list, filename,
1753 artwork_info->free_artwork);
1760 static void replaceArtworkListEntry(struct ArtworkListInfo *artwork_info,
1761 struct ListNodeInfo **listnode,
1766 "Loading graphics:",
1772 char *filename = getCustomArtworkFilename(basename, artwork_info->type);
1774 if (filename == NULL)
1776 int error_mode = ERR_WARN;
1778 /* we can get away without sounds and music, but not without graphics */
1779 if (*listnode == NULL && artwork_info->type == ARTWORK_TYPE_GRAPHICS)
1780 error_mode = ERR_EXIT;
1782 Error(error_mode, "cannot find artwork file '%s'", basename);
1786 /* check if the old and the new artwork file are the same */
1787 if (*listnode && strcmp((*listnode)->source_filename, filename) == 0)
1789 /* The old and new artwork are the same (have the same filename and path).
1790 This usually means that this artwork does not exist in this artwork set
1791 and a fallback to the existing artwork is done. */
1794 printf("[artwork '%s' already exists (same list entry)]\n", filename);
1800 /* delete existing artwork file entry */
1801 deleteArtworkListEntry(artwork_info, listnode);
1803 /* check if the new artwork file already exists in the list of artworks */
1804 if ((node = getNodeFromKey(artwork_info->content_list, filename)) != NULL)
1807 printf("[artwork '%s' already exists (other list entry)]\n", filename);
1810 *listnode = (struct ListNodeInfo *)node->content;
1811 (*listnode)->num_references++;
1816 DrawInitText(init_text[artwork_info->type], 120, FC_GREEN);
1817 DrawInitText(basename, 150, FC_YELLOW);
1819 if ((*listnode = artwork_info->load_artwork(filename)) != NULL)
1822 printf("[adding new artwork '%s']\n", filename);
1825 (*listnode)->num_references = 1;
1826 addNodeToList(&artwork_info->content_list, (*listnode)->source_filename,
1831 int error_mode = ERR_WARN;
1833 /* we can get away without sounds and music, but not without graphics */
1834 if (artwork_info->type == ARTWORK_TYPE_GRAPHICS)
1835 error_mode = ERR_EXIT;
1837 Error(error_mode, "cannot load artwork file '%s'", basename);
1842 static void LoadCustomArtwork(struct ArtworkListInfo *artwork_info,
1843 struct ListNodeInfo **listnode,
1847 char *filename = getCustomArtworkFilename(basename, artwork_info->type);
1851 printf("GOT CUSTOM ARTWORK FILE '%s'\n", filename);
1854 if (strcmp(basename, UNDEFINED_FILENAME) == 0)
1856 deleteArtworkListEntry(artwork_info, listnode);
1861 if (filename == NULL)
1863 Error(ERR_WARN, "cannot find artwork file '%s'", basename);
1867 replaceArtworkListEntry(artwork_info, listnode, filename);
1869 replaceArtworkListEntry(artwork_info, listnode, basename);
1873 static void LoadArtworkToList(struct ArtworkListInfo *artwork_info,
1874 char *basename, int list_pos)
1876 if (artwork_info->artwork_list == NULL ||
1877 list_pos >= artwork_info->num_file_list_entries)
1881 printf("loading artwork '%s' ... [%d]\n",
1882 basename, getNumNodes(artwork_info->content_list));
1885 LoadCustomArtwork(artwork_info, &artwork_info->artwork_list[list_pos],
1889 printf("loading artwork '%s' done [%d]\n",
1890 basename, getNumNodes(artwork_info->content_list));
1894 void ReloadCustomArtworkList(struct ArtworkListInfo *artwork_info)
1905 { "Loading graphics:", TRUE },
1906 { "Loading sounds:", TRUE },
1907 { "Loading music:", TRUE }
1911 int num_file_list_entries = artwork_info->num_file_list_entries;
1912 struct FileInfo *file_list = artwork_info->file_list;
1916 LoadArtworkConfig(artwork_info);
1920 if (draw_init[artwork_info->type].do_it)
1921 DrawInitText(draw_init[artwork_info->type].text, 120, FC_GREEN);
1925 printf("DEBUG: reloading %d artwork files ...\n", num_file_list_entries);
1928 for(i=0; i<num_file_list_entries; i++)
1931 if (draw_init[artwork_info->type].do_it)
1932 DrawInitText(file_list[i].token, 150, FC_YELLOW);
1935 LoadArtworkToList(artwork_info, file_list[i].filename, i);
1938 printf("DEBUG: loading artwork file '%s'...\n", file_list[i].filename);
1943 draw_init[artwork_info->type].do_it = FALSE;
1947 printf("list size == %d\n", getNumNodes(artwork_info->content_list));
1951 dumpList(artwork_info->content_list);
1955 void FreeCustomArtworkList(struct ArtworkListInfo *artwork_info)
1959 if (artwork_info == NULL || artwork_info->artwork_list == NULL)
1963 printf("%s: FREEING ARTWORK ...\n",
1964 IS_CHILD_PROCESS(audio.mixer_pid) ? "CHILD" : "PARENT");
1967 for(i=0; i<artwork_info->num_file_list_entries; i++)
1968 deleteArtworkListEntry(artwork_info, &artwork_info->artwork_list[i]);
1971 printf("%s: FREEING ARTWORK -- DONE\n",
1972 IS_CHILD_PROCESS(audio.mixer_pid) ? "CHILD" : "PARENT");
1975 free(artwork_info->artwork_list);
1977 artwork_info->artwork_list = NULL;
1978 artwork_info->num_file_list_entries = 0;
1982 /* ------------------------------------------------------------------------- */
1983 /* functions only needed for non-Unix (non-command-line) systems */
1984 /* (MS-DOS only; SDL/Windows creates files "stdout.txt" and "stderr.txt") */
1985 /* ------------------------------------------------------------------------- */
1987 #if defined(PLATFORM_MSDOS)
1989 #define ERROR_FILENAME "stderr.txt"
1991 void initErrorFile()
1993 unlink(ERROR_FILENAME);
1996 FILE *openErrorFile()
1998 return fopen(ERROR_FILENAME, MODE_APPEND);
2001 void dumpErrorFile()
2003 FILE *error_file = fopen(ERROR_FILENAME, MODE_READ);
2005 if (error_file != NULL)
2007 while (!feof(error_file))
2008 fputc(fgetc(error_file), stderr);
2016 /* ------------------------------------------------------------------------- */
2017 /* the following is only for debugging purpose and normally not used */
2018 /* ------------------------------------------------------------------------- */
2020 #define DEBUG_NUM_TIMESTAMPS 3
2022 void debug_print_timestamp(int counter_nr, char *message)
2024 static long counter[DEBUG_NUM_TIMESTAMPS][2];
2026 if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
2027 Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
2029 counter[counter_nr][0] = Counter();
2032 printf("%s %.2f seconds\n", message,
2033 (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
2035 counter[counter_nr][1] = Counter();