1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * (c) 1995-98 Artsoft Entertainment *
8 * phone: ++49 +521 290471 *
9 * email: aeglos@valinor.owl.de *
10 *----------------------------------------------------------*
12 ***********************************************************/
18 #include <sys/types.h>
22 #if !defined(PLATFORM_WIN32)
24 #include <sys/param.h>
35 #if defined(PLATFORM_MSDOS)
36 volatile unsigned long counter = 0;
38 void increment_counter()
43 END_OF_FUNCTION(increment_counter);
47 /* maximal allowed length of a command line option */
48 #define MAX_OPTION_LEN 256
51 static unsigned long mainCounter(int mode)
53 static unsigned long base_ms = 0;
54 unsigned long current_ms;
55 unsigned long counter_ms;
57 current_ms = SDL_GetTicks();
59 /* reset base time in case of counter initializing or wrap-around */
60 if (mode == INIT_COUNTER || current_ms < base_ms)
63 counter_ms = current_ms - base_ms;
65 return counter_ms; /* return milliseconds since last init */
68 #else /* !TARGET_SDL */
70 #if defined(PLATFORM_UNIX)
71 static unsigned long mainCounter(int mode)
73 static struct timeval base_time = { 0, 0 };
74 struct timeval current_time;
75 unsigned long counter_ms;
77 gettimeofday(¤t_time, NULL);
79 /* reset base time in case of counter initializing or wrap-around */
80 if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec)
81 base_time = current_time;
83 counter_ms = (current_time.tv_sec - base_time.tv_sec) * 1000
84 + (current_time.tv_usec - base_time.tv_usec) / 1000;
86 return counter_ms; /* return milliseconds since last init */
88 #endif /* PLATFORM_UNIX */
89 #endif /* !TARGET_SDL */
91 void InitCounter() /* set counter back to zero */
93 #if !defined(PLATFORM_MSDOS)
94 mainCounter(INIT_COUNTER);
96 LOCK_VARIABLE(counter);
97 LOCK_FUNCTION(increment_counter);
98 install_int_ex(increment_counter, BPS_TO_TIMER(100));
102 unsigned long Counter() /* get milliseconds since last call of InitCounter() */
104 #if !defined(PLATFORM_MSDOS)
105 return mainCounter(READ_COUNTER);
107 return (counter * 10);
111 static void sleep_milliseconds(unsigned long milliseconds_delay)
113 boolean do_busy_waiting = (milliseconds_delay < 5 ? TRUE : FALSE);
115 #if defined(PLATFORM_MSDOS)
116 /* don't use select() to perform waiting operations under DOS/Windows
117 environment; always use a busy loop for waiting instead */
118 do_busy_waiting = TRUE;
123 /* we want to wait only a few ms -- if we assume that we have a
124 kernel timer resolution of 10 ms, we would wait far to long;
125 therefore it's better to do a short interval of busy waiting
126 to get our sleeping time more accurate */
128 unsigned long base_counter = Counter(), actual_counter = Counter();
130 while (actual_counter < base_counter + milliseconds_delay &&
131 actual_counter >= base_counter)
132 actual_counter = Counter();
136 #if defined(TARGET_SDL)
137 SDL_Delay(milliseconds_delay);
139 struct timeval delay;
141 delay.tv_sec = milliseconds_delay / 1000;
142 delay.tv_usec = 1000 * (milliseconds_delay % 1000);
144 if (select(0, NULL, NULL, NULL, &delay) != 0)
145 Error(ERR_WARN, "sleep_milliseconds(): select() failed");
150 void Delay(unsigned long delay) /* Sleep specified number of milliseconds */
152 sleep_milliseconds(delay);
155 boolean FrameReached(unsigned long *frame_counter_var,
156 unsigned long frame_delay)
158 unsigned long actual_frame_counter = FrameCounter;
160 if (actual_frame_counter < *frame_counter_var+frame_delay &&
161 actual_frame_counter >= *frame_counter_var)
164 *frame_counter_var = actual_frame_counter;
168 boolean DelayReached(unsigned long *counter_var,
171 unsigned long actual_counter = Counter();
173 if (actual_counter < *counter_var + delay &&
174 actual_counter >= *counter_var)
177 *counter_var = actual_counter;
181 void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
183 unsigned long actual_counter;
187 actual_counter = Counter();
189 if (actual_counter < *counter_var + delay &&
190 actual_counter >= *counter_var)
191 sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
196 *counter_var = actual_counter;
199 /* int2str() returns a number converted to a string;
200 the used memory is static, but will be overwritten by later calls,
201 so if you want to save the result, copy it to a private string buffer;
202 there can be 10 local calls of int2str() without buffering the result --
203 the 11th call will then destroy the result from the first call and so on.
206 char *int2str(int number, int size)
208 static char shift_array[10][40];
209 static int shift_counter = 0;
210 char *s = shift_array[shift_counter];
212 shift_counter = (shift_counter + 1) % 10;
219 sprintf(s, " %09d", number);
220 return &s[strlen(s) - size];
224 sprintf(s, "%d", number);
229 unsigned int SimpleRND(unsigned int max)
231 #if defined(TARGET_SDL)
232 static unsigned long root = 654321;
233 unsigned long current_ms;
235 current_ms = SDL_GetTicks();
236 root = root * 4253261 + current_ms;
239 static unsigned long root = 654321;
240 struct timeval current_time;
242 gettimeofday(¤t_time, NULL);
243 root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
249 static unsigned int last_RND_value = 0;
251 unsigned int last_RND()
253 return last_RND_value;
257 unsigned int RND(unsigned int max)
260 return (last_RND_value = random_linux_libc() % max);
262 return (random_linux_libc() % max);
266 unsigned int InitRND(long seed)
268 #if defined(TARGET_SDL)
269 unsigned long current_ms;
271 if (seed == NEW_RANDOMIZE)
273 current_ms = SDL_GetTicks();
274 srandom_linux_libc((unsigned int) current_ms);
275 return (unsigned int) current_ms;
279 srandom_linux_libc((unsigned int) seed);
280 return (unsigned int) seed;
283 struct timeval current_time;
285 if (seed == NEW_RANDOMIZE)
287 gettimeofday(¤t_time, NULL);
288 srandom_linux_libc((unsigned int) current_time.tv_usec);
289 return (unsigned int) current_time.tv_usec;
293 srandom_linux_libc((unsigned int) seed);
294 return (unsigned int) seed;
301 #if defined(PLATFORM_WIN32)
302 return ANONYMOUS_NAME;
306 if ((pwd = getpwuid(getuid())) == NULL)
307 return ANONYMOUS_NAME;
315 #if defined(PLATFORM_UNIX)
318 if ((pwd = getpwuid(getuid())) == NULL || strlen(pwd->pw_gecos) == 0)
319 return ANONYMOUS_NAME;
322 static char real_name[1024];
323 char *from_ptr = pwd->pw_gecos, *to_ptr = real_name;
325 if (strchr(pwd->pw_gecos, 'ß') == NULL)
326 return pwd->pw_gecos;
328 /* the user's real name contains a 'ß' character (german sharp s),
329 which has no equivalent in upper case letters (which our fonts use) */
330 while (*from_ptr != '\0' && (long)(to_ptr - real_name) < 1024 - 2)
332 if (*from_ptr != 'ß')
333 *to_ptr++ = *from_ptr++;
345 #else /* !PLATFORM_UNIX */
346 return ANONYMOUS_NAME;
352 #if defined(PLATFORM_UNIX)
353 static char *home_dir = NULL;
357 if (!(home_dir = getenv("HOME")))
361 if ((pwd = getpwuid(getuid())))
362 home_dir = pwd->pw_dir;
374 char *getPath2(char *path1, char *path2)
376 char *complete_path = checked_malloc(strlen(path1) + 1 +
379 sprintf(complete_path, "%s/%s", path1, path2);
380 return complete_path;
383 char *getPath3(char *path1, char *path2, char *path3)
385 char *complete_path = checked_malloc(strlen(path1) + 1 +
389 sprintf(complete_path, "%s/%s/%s", path1, path2, path3);
390 return complete_path;
393 char *getStringCopy(char *s)
400 s_copy = checked_malloc(strlen(s) + 1);
406 char *getStringToLower(char *s)
408 char *s_copy = checked_malloc(strlen(s) + 1);
409 char *s_ptr = s_copy;
412 *s_ptr++ = tolower(*s++);
418 void MarkTileDirty(int x, int y)
420 int xx = redraw_x1 + x;
421 int yy = redraw_y1 + y;
426 redraw[xx][yy] = TRUE;
427 redraw_mask |= REDRAW_TILES;
430 void SetBorderElement()
434 BorderElement = EL_LEERRAUM;
436 for(y=0; y<lev_fieldy && BorderElement == EL_LEERRAUM; y++)
438 for(x=0; x<lev_fieldx; x++)
440 if (!IS_MASSIVE(Feld[x][y]))
441 BorderElement = EL_BETON;
443 if (y != 0 && y != lev_fieldy - 1 && x != lev_fieldx - 1)
449 void GetOptions(char *argv[])
451 char **options_left = &argv[1];
453 /* initialize global program options */
454 options.display_name = NULL;
455 options.server_host = NULL;
456 options.server_port = 0;
457 options.ro_base_directory = RO_BASE_PATH;
458 options.rw_base_directory = RW_BASE_PATH;
459 options.level_directory = RO_BASE_PATH "/" LEVELS_DIRECTORY;
460 options.serveronly = FALSE;
461 options.network = FALSE;
462 options.verbose = FALSE;
463 options.debug = FALSE;
465 /* initialize some more global variables */
466 global.frames_per_second = 0;
467 global.fps_slowdown = FALSE;
468 global.fps_slowdown_factor = 1;
470 while (*options_left)
472 char option_str[MAX_OPTION_LEN];
473 char *option = options_left[0];
474 char *next_option = options_left[1];
475 char *option_arg = NULL;
476 int option_len = strlen(option);
478 if (option_len >= MAX_OPTION_LEN)
479 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
481 strcpy(option_str, option); /* copy argument into buffer */
484 if (strcmp(option, "--") == 0) /* stop scanning arguments */
487 if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */
490 option_arg = strchr(option, '=');
491 if (option_arg == NULL) /* no '=' in option */
492 option_arg = next_option;
495 *option_arg++ = '\0'; /* cut argument from option */
496 if (*option_arg == '\0') /* no argument after '=' */
497 Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
500 option_len = strlen(option);
502 if (strcmp(option, "-") == 0)
503 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
504 else if (strncmp(option, "-help", option_len) == 0)
506 printf("Usage: %s [options] [server.name [port]]\n"
508 " -d, --display machine:0 X server display\n"
509 " -b, --basepath directory alternative base directory\n"
510 " -l, --level directory alternative level directory\n"
511 " -s, --serveronly only start network server\n"
512 " -n, --network network multiplayer game\n"
513 " -v, --verbose verbose mode\n",
517 else if (strncmp(option, "-display", option_len) == 0)
519 if (option_arg == NULL)
520 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
522 options.display_name = option_arg;
523 if (option_arg == next_option)
526 else if (strncmp(option, "-basepath", option_len) == 0)
528 if (option_arg == NULL)
529 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
531 /* this should be extended to separate options for ro and rw data */
532 options.ro_base_directory = option_arg;
533 options.rw_base_directory = option_arg;
534 if (option_arg == next_option)
537 /* adjust path for level directory accordingly */
538 options.level_directory =
539 getPath2(options.ro_base_directory, LEVELS_DIRECTORY);
541 else if (strncmp(option, "-levels", option_len) == 0)
543 if (option_arg == NULL)
544 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
546 options.level_directory = option_arg;
547 if (option_arg == next_option)
550 else if (strncmp(option, "-network", option_len) == 0)
552 options.network = TRUE;
554 else if (strncmp(option, "-serveronly", option_len) == 0)
556 options.serveronly = TRUE;
558 else if (strncmp(option, "-verbose", option_len) == 0)
560 options.verbose = TRUE;
562 else if (strncmp(option, "-debug", option_len) == 0)
564 options.debug = TRUE;
566 else if (*option == '-')
568 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
570 else if (options.server_host == NULL)
572 options.server_host = *options_left;
574 else if (options.server_port == 0)
576 options.server_port = atoi(*options_left);
577 if (options.server_port < 1024)
578 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
581 Error(ERR_EXIT_HELP, "too many arguments");
587 void Error(int mode, char *format, ...)
589 char *process_name = "";
590 FILE *error = stderr;
592 /* display warnings only when running in verbose mode */
593 if (mode & ERR_WARN && !options.verbose)
596 #if !defined(PLATFORM_UNIX)
597 if ((error = openErrorFile()) == NULL)
599 printf("Cannot write to error output file!\n");
604 if (mode & ERR_SOUND_SERVER)
605 process_name = " sound server";
606 else if (mode & ERR_NETWORK_SERVER)
607 process_name = " network server";
608 else if (mode & ERR_NETWORK_CLIENT)
609 process_name = " network client **";
615 fprintf(error, "%s%s: ", program_name, process_name);
618 fprintf(error, "warning: ");
620 va_start(ap, format);
621 vfprintf(error, format, ap);
624 fprintf(error, "\n");
628 fprintf(error, "%s: Try option '--help' for more information.\n",
632 fprintf(error, "%s%s: aborting\n", program_name, process_name);
639 if (mode & ERR_FROM_SERVER)
640 exit(1); /* child process: normal exit */
642 CloseAllAndExit(1); /* main process: clean up stuff */
646 void *checked_malloc(unsigned long size)
653 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
658 void *checked_calloc(unsigned long size)
662 ptr = calloc(1, size);
665 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
670 short getFile16BitInteger(FILE *file, int byte_order)
672 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
673 return ((fgetc(file) << 8) |
675 else /* BYTE_ORDER_LITTLE_ENDIAN */
676 return ((fgetc(file) << 0) |
680 void putFile16BitInteger(FILE *file, short value, int byte_order)
682 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
684 fputc((value >> 8) & 0xff, file);
685 fputc((value >> 0) & 0xff, file);
687 else /* BYTE_ORDER_LITTLE_ENDIAN */
689 fputc((value >> 0) & 0xff, file);
690 fputc((value >> 8) & 0xff, file);
694 int getFile32BitInteger(FILE *file, int byte_order)
696 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
697 return ((fgetc(file) << 24) |
698 (fgetc(file) << 16) |
701 else /* BYTE_ORDER_LITTLE_ENDIAN */
702 return ((fgetc(file) << 0) |
704 (fgetc(file) << 16) |
705 (fgetc(file) << 24));
708 void putFile32BitInteger(FILE *file, int value, int byte_order)
710 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
712 fputc((value >> 24) & 0xff, file);
713 fputc((value >> 16) & 0xff, file);
714 fputc((value >> 8) & 0xff, file);
715 fputc((value >> 0) & 0xff, file);
717 else /* BYTE_ORDER_LITTLE_ENDIAN */
719 fputc((value >> 0) & 0xff, file);
720 fputc((value >> 8) & 0xff, file);
721 fputc((value >> 16) & 0xff, file);
722 fputc((value >> 24) & 0xff, file);
726 void getFileChunk(FILE *file, char *chunk_buffer, int *chunk_length,
729 const int chunk_identifier_length = 4;
731 /* read chunk identifier */
732 fgets(chunk_buffer, chunk_identifier_length + 1, file);
734 /* read chunk length */
735 *chunk_length = getFile32BitInteger(file, byte_order);
738 void putFileChunk(FILE *file, char *chunk_name, int chunk_length,
741 /* write chunk identifier */
742 fputs(chunk_name, file);
744 /* write chunk length */
745 putFile32BitInteger(file, chunk_length, byte_order);
748 #define TRANSLATE_KEYSYM_TO_KEYNAME 0
749 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
750 #define TRANSLATE_X11KEYNAME_TO_KEYSYM 2
752 void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
761 /* normal cursor keys */
762 { KSYM_Left, "XK_Left", "cursor left" },
763 { KSYM_Right, "XK_Right", "cursor right" },
764 { KSYM_Up, "XK_Up", "cursor up" },
765 { KSYM_Down, "XK_Down", "cursor down" },
767 /* keypad cursor keys */
769 { KSYM_KP_Left, "XK_KP_Left", "keypad left" },
770 { KSYM_KP_Right, "XK_KP_Right", "keypad right" },
771 { KSYM_KP_Up, "XK_KP_Up", "keypad up" },
772 { KSYM_KP_Down, "XK_KP_Down", "keypad down" },
775 /* other keypad keys */
777 { KSYM_KP_Enter, "XK_KP_Enter", "keypad enter" },
778 { KSYM_KP_Add, "XK_KP_Add", "keypad +" },
779 { KSYM_KP_Subtract, "XK_KP_Subtract", "keypad -" },
780 { KSYM_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
781 { KSYM_KP_Divide, "XK_KP_Divide", "keypad /" },
782 { KSYM_KP_Separator,"XK_KP_Separator", "keypad ," },
786 { KSYM_Shift_L, "XK_Shift_L", "left shift" },
787 { KSYM_Shift_R, "XK_Shift_R", "right shift" },
788 { KSYM_Control_L, "XK_Control_L", "left control" },
789 { KSYM_Control_R, "XK_Control_R", "right control" },
790 { KSYM_Meta_L, "XK_Meta_L", "left meta" },
791 { KSYM_Meta_R, "XK_Meta_R", "right meta" },
792 { KSYM_Alt_L, "XK_Alt_L", "left alt" },
793 { KSYM_Alt_R, "XK_Alt_R", "right alt" },
794 { KSYM_Super_L, "XK_Super_L", "left super" }, /* Win-L */
795 { KSYM_Super_R, "XK_Super_R", "right super" }, /* Win-R */
796 { KSYM_Mode_switch, "XK_Mode_switch", "mode switch" }, /* Alt-R */
797 { KSYM_Multi_key, "XK_Multi_key", "multi key" }, /* Ctrl-R */
799 /* some special keys */
800 { KSYM_BackSpace, "XK_BackSpace", "backspace" },
801 { KSYM_Delete, "XK_Delete", "delete" },
802 { KSYM_Insert, "XK_Insert", "insert" },
803 { KSYM_Tab, "XK_Tab", "tab" },
804 { KSYM_Home, "XK_Home", "home" },
805 { KSYM_End, "XK_End", "end" },
806 { KSYM_Page_Up, "XK_Page_Up", "page up" },
807 { KSYM_Page_Down, "XK_Page_Down", "page down" },
808 { KSYM_Menu, "XK_Menu", "menu" }, /* Win-Menu */
810 /* ASCII 0x20 to 0x40 keys (except numbers) */
811 { KSYM_space, "XK_space", "space" },
812 { KSYM_exclam, "XK_exclam", "!" },
813 { KSYM_quotedbl, "XK_quotedbl", "\"" },
814 { KSYM_numbersign, "XK_numbersign", "#" },
815 { KSYM_dollar, "XK_dollar", "$" },
816 { KSYM_percent, "XK_percent", "%" },
817 { KSYM_ampersand, "XK_ampersand", "&" },
818 { KSYM_apostrophe, "XK_apostrophe", "'" },
819 { KSYM_parenleft, "XK_parenleft", "(" },
820 { KSYM_parenright, "XK_parenright", ")" },
821 { KSYM_asterisk, "XK_asterisk", "*" },
822 { KSYM_plus, "XK_plus", "+" },
823 { KSYM_comma, "XK_comma", "," },
824 { KSYM_minus, "XK_minus", "-" },
825 { KSYM_period, "XK_period", "." },
826 { KSYM_slash, "XK_slash", "/" },
827 { KSYM_colon, "XK_colon", ":" },
828 { KSYM_semicolon, "XK_semicolon", ";" },
829 { KSYM_less, "XK_less", "<" },
830 { KSYM_equal, "XK_equal", "=" },
831 { KSYM_greater, "XK_greater", ">" },
832 { KSYM_question, "XK_question", "?" },
833 { KSYM_at, "XK_at", "@" },
835 /* more ASCII keys */
836 { KSYM_bracketleft, "XK_bracketleft", "[" },
837 { KSYM_backslash, "XK_backslash", "backslash" },
838 { KSYM_bracketright,"XK_bracketright", "]" },
839 { KSYM_asciicircum, "XK_asciicircum", "circumflex" },
840 { KSYM_underscore, "XK_underscore", "_" },
841 { KSYM_grave, "XK_grave", "grave" },
842 { KSYM_quoteleft, "XK_quoteleft", "quote left" },
843 { KSYM_braceleft, "XK_braceleft", "brace left" },
844 { KSYM_bar, "XK_bar", "bar" },
845 { KSYM_braceright, "XK_braceright", "brace right" },
846 { KSYM_asciitilde, "XK_asciitilde", "ascii tilde" },
848 /* special (non-ASCII) keys */
849 { KSYM_Adiaeresis, "XK_Adiaeresis", "Ä" },
850 { KSYM_Odiaeresis, "XK_Odiaeresis", "Ö" },
851 { KSYM_Udiaeresis, "XK_Udiaeresis", "Ü" },
852 { KSYM_adiaeresis, "XK_adiaeresis", "ä" },
853 { KSYM_odiaeresis, "XK_odiaeresis", "ö" },
854 { KSYM_udiaeresis, "XK_udiaeresis", "ü" },
855 { KSYM_ssharp, "XK_ssharp", "sharp s" },
857 /* end-of-array identifier */
863 if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
865 static char name_buffer[30];
868 if (key >= KSYM_A && key <= KSYM_Z)
869 sprintf(name_buffer, "%c", 'A' + (char)(key - KSYM_A));
870 else if (key >= KSYM_a && key <= KSYM_z)
871 sprintf(name_buffer, "%c", 'a' + (char)(key - KSYM_a));
872 else if (key >= KSYM_0 && key <= KSYM_9)
873 sprintf(name_buffer, "%c", '0' + (char)(key - KSYM_0));
874 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
875 sprintf(name_buffer, "keypad %c", '0' + (char)(key - KSYM_KP_0));
876 else if (key >= KSYM_F1 && key <= KSYM_F24)
877 sprintf(name_buffer, "function F%d", (int)(key - KSYM_F1 + 1));
878 else if (key == KSYM_UNDEFINED)
879 strcpy(name_buffer, "(undefined)");
886 if (key == translate_key[i].key)
888 strcpy(name_buffer, translate_key[i].name);
892 while (translate_key[++i].name);
894 if (!translate_key[i].name)
895 strcpy(name_buffer, "(unknown)");
900 else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME)
902 static char name_buffer[30];
905 if (key >= KSYM_A && key <= KSYM_Z)
906 sprintf(name_buffer, "XK_%c", 'A' + (char)(key - KSYM_A));
907 else if (key >= KSYM_a && key <= KSYM_z)
908 sprintf(name_buffer, "XK_%c", 'a' + (char)(key - KSYM_a));
909 else if (key >= KSYM_0 && key <= KSYM_9)
910 sprintf(name_buffer, "XK_%c", '0' + (char)(key - KSYM_0));
911 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
912 sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - KSYM_KP_0));
913 else if (key >= KSYM_F1 && key <= KSYM_F24)
914 sprintf(name_buffer, "XK_F%d", (int)(key - KSYM_F1 + 1));
915 else if (key == KSYM_UNDEFINED)
916 strcpy(name_buffer, "[undefined]");
923 if (key == translate_key[i].key)
925 strcpy(name_buffer, translate_key[i].x11name);
929 while (translate_key[++i].x11name);
931 if (!translate_key[i].x11name)
932 sprintf(name_buffer, "0x%04lx", (unsigned long)key);
935 *x11name = name_buffer;
937 else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
939 Key key = KSYM_UNDEFINED;
940 char *name_ptr = *x11name;
942 if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4)
944 char c = name_ptr[3];
946 if (c >= 'A' && c <= 'Z')
947 key = KSYM_A + (Key)(c - 'A');
948 else if (c >= 'a' && c <= 'z')
949 key = KSYM_a + (Key)(c - 'a');
950 else if (c >= '0' && c <= '9')
951 key = KSYM_0 + (Key)(c - '0');
953 else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7)
955 char c = name_ptr[6];
957 if (c >= '0' && c <= '9')
958 key = KSYM_0 + (Key)(c - '0');
960 else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
962 char c1 = name_ptr[4];
963 char c2 = name_ptr[5];
966 if ((c1 >= '0' && c1 <= '9') &&
967 ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
968 d = atoi(&name_ptr[4]);
970 if (d >=1 && d <= 24)
971 key = KSYM_F1 + (Key)(d - 1);
973 else if (strncmp(name_ptr, "XK_", 3) == 0)
979 if (strcmp(name_ptr, translate_key[i].x11name) == 0)
981 key = translate_key[i].key;
985 while (translate_key[++i].x11name);
987 else if (strncmp(name_ptr, "0x", 2) == 0)
989 unsigned long value = 0;
995 char c = *name_ptr++;
998 if (c >= '0' && c <= '9')
1000 else if (c >= 'a' && c <= 'f')
1001 d = (int)(c - 'a' + 10);
1002 else if (c >= 'A' && c <= 'F')
1003 d = (int)(c - 'A' + 10);
1011 value = value * 16 + d;
1022 char *getKeyNameFromKey(Key key)
1026 translate_keyname(&key, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME);
1030 char *getX11KeyNameFromKey(Key key)
1034 translate_keyname(&key, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME);
1038 Key getKeyFromX11KeyName(char *x11name)
1042 translate_keyname(&key, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM);
1046 char getCharFromKey(Key key)
1048 char *keyname = getKeyNameFromKey(key);
1051 if (strlen(keyname) == 1)
1052 letter = keyname[0];
1053 else if (strcmp(keyname, "space") == 0)
1055 else if (strcmp(keyname, "circumflex") == 0)
1061 #define TRANSLATE_JOYSYMBOL_TO_JOYNAME 0
1062 #define TRANSLATE_JOYNAME_TO_JOYSYMBOL 1
1064 void translate_joyname(int *joysymbol, char **name, int mode)
1072 { JOY_LEFT, "joystick_left" },
1073 { JOY_RIGHT, "joystick_right" },
1074 { JOY_UP, "joystick_up" },
1075 { JOY_DOWN, "joystick_down" },
1076 { JOY_BUTTON_1, "joystick_button_1" },
1077 { JOY_BUTTON_2, "joystick_button_2" },
1082 if (mode == TRANSLATE_JOYSYMBOL_TO_JOYNAME)
1084 *name = "[undefined]";
1088 if (*joysymbol == translate_joy[i].joysymbol)
1090 *name = translate_joy[i].name;
1095 else if (mode == TRANSLATE_JOYNAME_TO_JOYSYMBOL)
1101 if (strcmp(*name, translate_joy[i].name) == 0)
1103 *joysymbol = translate_joy[i].joysymbol;
1110 char *getJoyNameFromJoySymbol(int joysymbol)
1114 translate_joyname(&joysymbol, &name, TRANSLATE_JOYSYMBOL_TO_JOYNAME);
1118 int getJoySymbolFromJoyName(char *name)
1122 translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL);
1126 int getJoystickNrFromDeviceName(char *device_name)
1129 int joystick_nr = 0;
1131 if (device_name == NULL || device_name[0] == '\0')
1134 c = device_name[strlen(device_name) - 1];
1136 if (c >= '0' && c <= '9')
1137 joystick_nr = (int)(c - '0');
1139 if (joystick_nr < 0 || joystick_nr >= MAX_PLAYERS)
1145 /* ------------------------------------------------------------------------- */
1146 /* some functions to handle lists of level directories */
1147 /* ------------------------------------------------------------------------- */
1149 struct LevelDirInfo *newLevelDirInfo()
1151 return checked_calloc(sizeof(struct LevelDirInfo));
1154 void pushLevelDirInfo(struct LevelDirInfo **node_first,
1155 struct LevelDirInfo *node_new)
1157 node_new->next = *node_first;
1158 *node_first = node_new;
1161 int numLevelDirInfo(struct LevelDirInfo *node)
1174 boolean validLevelSeries(struct LevelDirInfo *node)
1176 return (node != NULL && !node->node_group && !node->parent_link);
1179 struct LevelDirInfo *getFirstValidLevelSeries(struct LevelDirInfo *node)
1183 if (leveldir_first) /* start with first level directory entry */
1184 return getFirstValidLevelSeries(leveldir_first);
1188 else if (node->node_group) /* enter level group (step down into tree) */
1189 return getFirstValidLevelSeries(node->node_group);
1190 else if (node->parent_link) /* skip start entry of level group */
1192 if (node->next) /* get first real level series entry */
1193 return getFirstValidLevelSeries(node->next);
1194 else /* leave empty level group and go on */
1195 return getFirstValidLevelSeries(node->node_parent->next);
1197 else /* this seems to be a regular level series */
1201 struct LevelDirInfo *getLevelDirInfoFirstGroupEntry(struct LevelDirInfo *node)
1206 if (node->node_parent == NULL) /* top level group */
1207 return leveldir_first;
1208 else /* sub level group */
1209 return node->node_parent->node_group;
1212 int numLevelDirInfoInGroup(struct LevelDirInfo *node)
1214 return numLevelDirInfo(getLevelDirInfoFirstGroupEntry(node));
1217 int posLevelDirInfo(struct LevelDirInfo *node)
1219 struct LevelDirInfo *node_cmp = getLevelDirInfoFirstGroupEntry(node);
1224 if (node_cmp == node)
1228 node_cmp = node_cmp->next;
1234 struct LevelDirInfo *getLevelDirInfoFromPos(struct LevelDirInfo *node, int pos)
1236 struct LevelDirInfo *node_default = node;
1248 return node_default;
1251 struct LevelDirInfo *getLevelDirInfoFromFilenameExt(struct LevelDirInfo *node,
1254 if (filename == NULL)
1259 if (node->node_group)
1261 struct LevelDirInfo *node_group;
1263 node_group = getLevelDirInfoFromFilenameExt(node->node_group, filename);
1268 else if (!node->parent_link)
1270 if (strcmp(filename, node->filename) == 0)
1280 struct LevelDirInfo *getLevelDirInfoFromFilename(char *filename)
1282 return getLevelDirInfoFromFilenameExt(leveldir_first, filename);
1285 void dumpLevelDirInfo(struct LevelDirInfo *node, int depth)
1291 for (i=0; i<depth * 3; i++)
1294 printf("filename == '%s'\n", node->filename);
1296 if (node->node_group != NULL)
1297 dumpLevelDirInfo(node->node_group, depth + 1);
1303 void sortLevelDirInfo(struct LevelDirInfo **node_first,
1304 int (*compare_function)(const void *, const void *))
1306 int num_nodes = numLevelDirInfo(*node_first);
1307 struct LevelDirInfo **sort_array;
1308 struct LevelDirInfo *node = *node_first;
1314 /* allocate array for sorting structure pointers */
1315 sort_array = checked_calloc(num_nodes * sizeof(struct LevelDirInfo *));
1317 /* writing structure pointers to sorting array */
1318 while (i < num_nodes && node) /* double boundary check... */
1320 sort_array[i] = node;
1326 /* sorting the structure pointers in the sorting array */
1327 qsort(sort_array, num_nodes, sizeof(struct LevelDirInfo *),
1330 /* update the linkage of list elements with the sorted node array */
1331 for (i=0; i<num_nodes - 1; i++)
1332 sort_array[i]->next = sort_array[i + 1];
1333 sort_array[num_nodes - 1]->next = NULL;
1335 /* update the linkage of the main list anchor pointer */
1336 *node_first = sort_array[0];
1340 /* now recursively sort the level group structures */
1344 if (node->node_group != NULL)
1345 sortLevelDirInfo(&node->node_group, compare_function);
1351 inline void swap_numbers(int *i1, int *i2)
1359 inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2)
1372 /* ------------------------------------------------------------------------- */
1373 /* the following is only for debugging purpose and normally not used */
1374 /* ------------------------------------------------------------------------- */
1376 #define DEBUG_NUM_TIMESTAMPS 3
1378 void debug_print_timestamp(int counter_nr, char *message)
1380 static long counter[DEBUG_NUM_TIMESTAMPS][2];
1382 if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
1383 Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
1385 counter[counter_nr][0] = Counter();
1388 printf("%s %.2f seconds\n", message,
1389 (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
1391 counter[counter_nr][1] = Counter();