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 ***********************************************************/
16 #include <sys/types.h>
22 #include <sys/param.h>
34 volatile unsigned long counter = 0;
36 void increment_counter()
41 END_OF_FUNCTION(increment_counter);
45 /* maximal allowed length of a command line option */
46 #define MAX_OPTION_LEN 256
48 #ifdef USE_SDL_LIBRARY
50 static unsigned long mainCounter(int mode)
52 static unsigned long base_ms = 0;
53 unsigned long current_ms;
54 unsigned long counter_ms;
56 current_ms = SDL_GetTicks();
58 /* reset base time in case of counter initializing or wrap-around */
59 if (mode == INIT_COUNTER || current_ms < base_ms)
62 counter_ms = current_ms - base_ms;
64 return counter_ms; /* return milliseconds since last init */
67 #else /* !USE_SDL_LIBRARY */
70 static unsigned long mainCounter(int mode)
72 static struct timeval base_time = { 0, 0 };
73 struct timeval current_time;
74 unsigned long counter_ms;
76 gettimeofday(¤t_time, NULL);
78 /* reset base time in case of counter initializing or wrap-around */
79 if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec)
80 base_time = current_time;
82 counter_ms = (current_time.tv_sec - base_time.tv_sec) * 1000
83 + (current_time.tv_usec - base_time.tv_usec) / 1000;
85 return counter_ms; /* return milliseconds since last init */
89 #endif /* !USE_SDL_LIBRARY */
91 void InitCounter() /* set counter back to zero */
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() */
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);
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 #ifdef USE_SDL_LIBRARY
137 SDL_Delay(milliseconds_delay);
138 #else /* !USE_SDL_LIBRARY */
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");
146 #endif /* !USE_SDL_LIBRARY */
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 #ifdef USE_SDL_LIBRARY
233 static unsigned long root = 654321;
234 unsigned long current_ms;
236 current_ms = SDL_GetTicks();
237 root = root * 4253261 + current_ms;
240 #else /* !USE_SDL_LIBRARY */
242 static unsigned long root = 654321;
243 struct timeval current_time;
245 gettimeofday(¤t_time, NULL);
246 root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
249 #endif /* !USE_SDL_LIBRARY */
253 static unsigned int last_RND_value = 0;
255 unsigned int last_RND()
257 return last_RND_value;
261 unsigned int RND(unsigned int max)
264 return (last_RND_value = random_linux_libc() % max);
266 return (random_linux_libc() % max);
270 unsigned int InitRND(long seed)
272 #ifdef USE_SDL_LIBRARY
273 unsigned long current_ms;
275 if (seed == NEW_RANDOMIZE)
277 current_ms = SDL_GetTicks();
278 srandom_linux_libc((unsigned int) current_ms);
279 return (unsigned int) current_ms;
283 srandom_linux_libc((unsigned int) seed);
284 return (unsigned int) seed;
286 #else /* !USE_SDL_LIBRARY */
287 struct timeval current_time;
289 if (seed == NEW_RANDOMIZE)
291 gettimeofday(¤t_time, NULL);
292 srandom_linux_libc((unsigned int) current_time.tv_usec);
293 return (unsigned int) current_time.tv_usec;
297 srandom_linux_libc((unsigned int) seed);
298 return (unsigned int) seed;
300 #endif /* !USE_SDL_LIBRARY */
306 return ANONYMOUS_NAME;
310 if ((pwd = getpwuid(getuid())) == NULL)
311 return ANONYMOUS_NAME;
319 #if defined(MSDOS) || defined(WIN32)
320 return ANONYMOUS_NAME;
324 if ((pwd = getpwuid(getuid())) == NULL || strlen(pwd->pw_gecos) == 0)
325 return ANONYMOUS_NAME;
328 static char real_name[1024];
329 char *from_ptr = pwd->pw_gecos, *to_ptr = real_name;
331 if (strchr(pwd->pw_gecos, 'ß') == NULL)
332 return pwd->pw_gecos;
334 /* the user's real name contains a 'ß' character (german sharp s),
335 which has no equivalent in upper case letters (which our fonts use) */
336 while (*from_ptr != '\0' && (long)(to_ptr - real_name) < 1024 - 2)
338 if (*from_ptr != 'ß')
339 *to_ptr++ = *from_ptr++;
356 #if defined(MSDOS) || defined(WIN32)
359 static char *home_dir = NULL;
363 if (!(home_dir = getenv("HOME")))
367 if ((pwd = getpwuid(getuid())))
368 home_dir = pwd->pw_dir;
378 char *getPath2(char *path1, char *path2)
380 char *complete_path = checked_malloc(strlen(path1) + 1 +
383 sprintf(complete_path, "%s/%s", path1, path2);
384 return complete_path;
387 char *getPath3(char *path1, char *path2, char *path3)
389 char *complete_path = checked_malloc(strlen(path1) + 1 +
393 sprintf(complete_path, "%s/%s/%s", path1, path2, path3);
394 return complete_path;
397 char *getStringCopy(char *s)
404 s_copy = checked_malloc(strlen(s) + 1);
410 char *getStringToLower(char *s)
412 char *s_copy = checked_malloc(strlen(s) + 1);
413 char *s_ptr = s_copy;
416 *s_ptr++ = tolower(*s++);
422 void MarkTileDirty(int x, int y)
424 int xx = redraw_x1 + x;
425 int yy = redraw_y1 + y;
430 redraw[xx][yy] = TRUE;
431 redraw_mask |= REDRAW_TILES;
434 void SetBorderElement()
438 BorderElement = EL_LEERRAUM;
440 for(y=0; y<lev_fieldy && BorderElement == EL_LEERRAUM; y++)
442 for(x=0; x<lev_fieldx; x++)
444 if (!IS_MASSIVE(Feld[x][y]))
445 BorderElement = EL_BETON;
447 if (y != 0 && y != lev_fieldy - 1 && x != lev_fieldx - 1)
453 void GetOptions(char *argv[])
455 char **options_left = &argv[1];
457 /* initialize global program options */
458 options.display_name = NULL;
459 options.server_host = NULL;
460 options.server_port = 0;
461 options.ro_base_directory = RO_BASE_PATH;
462 options.rw_base_directory = RW_BASE_PATH;
463 options.level_directory = RO_BASE_PATH "/" LEVELS_DIRECTORY;
464 options.serveronly = FALSE;
465 options.network = FALSE;
466 options.verbose = FALSE;
468 while (*options_left)
470 char option_str[MAX_OPTION_LEN];
471 char *option = options_left[0];
472 char *next_option = options_left[1];
473 char *option_arg = NULL;
474 int option_len = strlen(option);
476 if (option_len >= MAX_OPTION_LEN)
477 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
479 strcpy(option_str, option); /* copy argument into buffer */
482 if (strcmp(option, "--") == 0) /* stop scanning arguments */
485 if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */
488 option_arg = strchr(option, '=');
489 if (option_arg == NULL) /* no '=' in option */
490 option_arg = next_option;
493 *option_arg++ = '\0'; /* cut argument from option */
494 if (*option_arg == '\0') /* no argument after '=' */
495 Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
498 option_len = strlen(option);
500 if (strcmp(option, "-") == 0)
501 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
502 else if (strncmp(option, "-help", option_len) == 0)
504 printf("Usage: %s [options] [server.name [port]]\n"
506 " -d, --display machine:0 X server display\n"
507 " -b, --basepath directory alternative base directory\n"
508 " -l, --level directory alternative level directory\n"
509 " -s, --serveronly only start network server\n"
510 " -n, --network network multiplayer game\n"
511 " -v, --verbose verbose mode\n",
515 else if (strncmp(option, "-display", option_len) == 0)
517 if (option_arg == NULL)
518 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
520 options.display_name = option_arg;
521 if (option_arg == next_option)
524 else if (strncmp(option, "-basepath", option_len) == 0)
526 if (option_arg == NULL)
527 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
529 /* this should be extended to separate options for ro and rw data */
530 options.ro_base_directory = option_arg;
531 options.rw_base_directory = option_arg;
532 if (option_arg == next_option)
535 /* adjust path for level directory accordingly */
536 options.level_directory =
537 getPath2(options.ro_base_directory, LEVELS_DIRECTORY);
539 else if (strncmp(option, "-levels", option_len) == 0)
541 if (option_arg == NULL)
542 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
544 options.level_directory = option_arg;
545 if (option_arg == next_option)
548 else if (strncmp(option, "-network", option_len) == 0)
550 options.network = TRUE;
552 else if (strncmp(option, "-serveronly", option_len) == 0)
554 options.serveronly = TRUE;
556 else if (strncmp(option, "-verbose", option_len) == 0)
558 options.verbose = TRUE;
560 else if (*option == '-')
562 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
564 else if (options.server_host == NULL)
566 options.server_host = *options_left;
568 else if (options.server_port == 0)
570 options.server_port = atoi(*options_left);
571 if (options.server_port < 1024)
572 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
575 Error(ERR_EXIT_HELP, "too many arguments");
581 void Error(int mode, char *format, ...)
583 char *process_name = "";
584 FILE *error = stderr;
586 /* display warnings only when running in verbose mode */
587 if (mode & ERR_WARN && !options.verbose)
590 #if defined(MSDOS) || defined(WIN32)
591 if ((error = openErrorFile()) == NULL)
593 printf("Cannot write to error output file!\n");
598 if (mode & ERR_SOUND_SERVER)
599 process_name = " sound server";
600 else if (mode & ERR_NETWORK_SERVER)
601 process_name = " network server";
602 else if (mode & ERR_NETWORK_CLIENT)
603 process_name = " network client **";
609 fprintf(error, "%s%s: ", program_name, process_name);
612 fprintf(error, "warning: ");
614 va_start(ap, format);
615 vfprintf(error, format, ap);
618 fprintf(error, "\n");
622 fprintf(error, "%s: Try option '--help' for more information.\n",
626 fprintf(error, "%s%s: aborting\n", program_name, process_name);
633 if (mode & ERR_FROM_SERVER)
634 exit(1); /* child process: normal exit */
636 CloseAllAndExit(1); /* main process: clean up stuff */
640 void *checked_malloc(unsigned long size)
647 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
652 void *checked_calloc(unsigned long size)
656 ptr = calloc(1, size);
659 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
664 short getFile16BitInteger(FILE *file, int byte_order)
666 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
667 return ((fgetc(file) << 8) |
669 else /* BYTE_ORDER_LITTLE_ENDIAN */
670 return ((fgetc(file) << 0) |
674 void putFile16BitInteger(FILE *file, short value, int byte_order)
676 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
678 fputc((value >> 8) & 0xff, file);
679 fputc((value >> 0) & 0xff, file);
681 else /* BYTE_ORDER_LITTLE_ENDIAN */
683 fputc((value >> 0) & 0xff, file);
684 fputc((value >> 8) & 0xff, file);
688 int getFile32BitInteger(FILE *file, int byte_order)
690 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
691 return ((fgetc(file) << 24) |
692 (fgetc(file) << 16) |
695 else /* BYTE_ORDER_LITTLE_ENDIAN */
696 return ((fgetc(file) << 0) |
698 (fgetc(file) << 16) |
699 (fgetc(file) << 24));
702 void putFile32BitInteger(FILE *file, int value, int byte_order)
704 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
706 fputc((value >> 24) & 0xff, file);
707 fputc((value >> 16) & 0xff, file);
708 fputc((value >> 8) & 0xff, file);
709 fputc((value >> 0) & 0xff, file);
711 else /* BYTE_ORDER_LITTLE_ENDIAN */
713 fputc((value >> 0) & 0xff, file);
714 fputc((value >> 8) & 0xff, file);
715 fputc((value >> 16) & 0xff, file);
716 fputc((value >> 24) & 0xff, file);
720 void getFileChunk(FILE *file, char *chunk_buffer, int *chunk_length,
723 const int chunk_identifier_length = 4;
725 /* read chunk identifier */
726 fgets(chunk_buffer, chunk_identifier_length + 1, file);
728 /* read chunk length */
729 *chunk_length = getFile32BitInteger(file, byte_order);
732 void putFileChunk(FILE *file, char *chunk_name, int chunk_length,
735 /* write chunk identifier */
736 fputs(chunk_name, file);
738 /* write chunk length */
739 putFile32BitInteger(file, chunk_length, byte_order);
742 #define TRANSLATE_KEY_TO_KEYNAME 0
743 #define TRANSLATE_KEY_TO_X11KEYNAME 1
744 #define TRANSLATE_X11KEYNAME_TO_KEY 2
746 void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
755 /* normal cursor keys */
756 { KEY_Left, "XK_Left", "cursor left" },
757 { KEY_Right, "XK_Right", "cursor right" },
758 { KEY_Up, "XK_Up", "cursor up" },
759 { KEY_Down, "XK_Down", "cursor down" },
761 /* keypad cursor keys */
763 { KEY_KP_Left, "XK_KP_Left", "keypad left" },
764 { KEY_KP_Right, "XK_KP_Right", "keypad right" },
765 { KEY_KP_Up, "XK_KP_Up", "keypad up" },
766 { KEY_KP_Down, "XK_KP_Down", "keypad down" },
769 /* other keypad keys */
771 { KEY_KP_Enter, "XK_KP_Enter", "keypad enter" },
772 { KEY_KP_Add, "XK_KP_Add", "keypad +" },
773 { KEY_KP_Subtract, "XK_KP_Subtract", "keypad -" },
774 { KEY_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
775 { KEY_KP_Divide, "XK_KP_Divide", "keypad /" },
776 { KEY_KP_Separator, "XK_KP_Separator", "keypad ," },
780 { KEY_Shift_L, "XK_Shift_L", "left shift" },
781 { KEY_Shift_R, "XK_Shift_R", "right shift" },
782 { KEY_Control_L, "XK_Control_L", "left control" },
783 { KEY_Control_R, "XK_Control_R", "right control" },
784 { KEY_Meta_L, "XK_Meta_L", "left meta" },
785 { KEY_Meta_R, "XK_Meta_R", "right meta" },
786 { KEY_Alt_L, "XK_Alt_L", "left alt" },
787 { KEY_Alt_R, "XK_Alt_R", "right alt" },
788 { KEY_Super_L, "XK_Super_L", "left super" }, /* Win-L */
789 { KEY_Super_R, "XK_Super_R", "right super" }, /* Win-R */
790 { KEY_Mode_switch, "XK_Mode_switch", "mode switch" }, /* Alt-R */
791 { KEY_Multi_key, "XK_Multi_key", "multi key" }, /* Ctrl-R */
793 /* some special keys */
794 { KEY_BackSpace, "XK_BackSpace", "backspace" },
795 { KEY_Delete, "XK_Delete", "delete" },
796 { KEY_Insert, "XK_Insert", "insert" },
797 { KEY_Tab, "XK_Tab", "tab" },
798 { KEY_Home, "XK_Home", "home" },
799 { KEY_End, "XK_End", "end" },
800 { KEY_Page_Up, "XK_Page_Up", "page up" },
801 { KEY_Page_Down, "XK_Page_Down", "page down" },
802 { KEY_Menu, "XK_Menu", "menu" }, /* Win-Menu */
804 /* ASCII 0x20 to 0x40 keys (except numbers) */
805 { KEY_space, "XK_space", "space" },
806 { KEY_exclam, "XK_exclam", "!" },
807 { KEY_quotedbl, "XK_quotedbl", "\"" },
808 { KEY_numbersign, "XK_numbersign", "#" },
809 { KEY_dollar, "XK_dollar", "$" },
810 { KEY_percent, "XK_percent", "%" },
811 { KEY_ampersand, "XK_ampersand", "&" },
812 { KEY_apostrophe, "XK_apostrophe", "'" },
813 { KEY_parenleft, "XK_parenleft", "(" },
814 { KEY_parenright, "XK_parenright", ")" },
815 { KEY_asterisk, "XK_asterisk", "*" },
816 { KEY_plus, "XK_plus", "+" },
817 { KEY_comma, "XK_comma", "," },
818 { KEY_minus, "XK_minus", "-" },
819 { KEY_period, "XK_period", "." },
820 { KEY_slash, "XK_slash", "/" },
821 { KEY_colon, "XK_colon", ":" },
822 { KEY_semicolon, "XK_semicolon", ";" },
823 { KEY_less, "XK_less", "<" },
824 { KEY_equal, "XK_equal", "=" },
825 { KEY_greater, "XK_greater", ">" },
826 { KEY_question, "XK_question", "?" },
827 { KEY_at, "XK_at", "@" },
829 /* more ASCII keys */
830 { KEY_bracketleft, "XK_bracketleft", "[" },
831 { KEY_backslash, "XK_backslash", "backslash" },
832 { KEY_bracketright, "XK_bracketright", "]" },
833 { KEY_asciicircum, "XK_asciicircum", "circumflex" },
834 { KEY_underscore, "XK_underscore", "_" },
835 { KEY_grave, "XK_grave", "grave" },
836 { KEY_quoteleft, "XK_quoteleft", "quote left" },
837 { KEY_braceleft, "XK_braceleft", "brace left" },
838 { KEY_bar, "XK_bar", "bar" },
839 { KEY_braceright, "XK_braceright", "brace right" },
840 { KEY_asciitilde, "XK_asciitilde", "ascii tilde" },
842 /* special (non-ASCII) keys */
843 { KEY_Adiaeresis, "XK_Adiaeresis", "Ä" },
844 { KEY_Odiaeresis, "XK_Odiaeresis", "Ö" },
845 { KEY_Udiaeresis, "XK_Udiaeresis", "Ü" },
846 { KEY_adiaeresis, "XK_adiaeresis", "ä" },
847 { KEY_odiaeresis, "XK_odiaeresis", "ö" },
848 { KEY_udiaeresis, "XK_udiaeresis", "ü" },
849 { KEY_ssharp, "XK_ssharp", "sharp s" },
851 /* end-of-array identifier */
857 if (mode == TRANSLATE_KEY_TO_KEYNAME)
859 static char name_buffer[30];
862 if (key >= KEY_A && key <= KEY_Z)
863 sprintf(name_buffer, "%c", 'A' + (char)(key - KEY_A));
864 else if (key >= KEY_a && key <= KEY_z)
865 sprintf(name_buffer, "%c", 'a' + (char)(key - KEY_a));
866 else if (key >= KEY_0 && key <= KEY_9)
867 sprintf(name_buffer, "%c", '0' + (char)(key - KEY_0));
868 else if (key >= KEY_KP_0 && key <= KEY_KP_9)
869 sprintf(name_buffer, "keypad %c", '0' + (char)(key - KEY_KP_0));
870 else if (key >= KEY_F1 && key <= KEY_F24)
871 sprintf(name_buffer, "function F%d", (int)(key - KEY_F1 + 1));
872 else if (key == KEY_UNDEFINED)
873 strcpy(name_buffer, "(undefined)");
880 if (key == translate_key[i].key)
882 strcpy(name_buffer, translate_key[i].name);
886 while (translate_key[++i].name);
888 if (!translate_key[i].name)
889 strcpy(name_buffer, "(unknown)");
894 else if (mode == TRANSLATE_KEY_TO_X11KEYNAME)
896 static char name_buffer[30];
899 if (key >= KEY_A && key <= KEY_Z)
900 sprintf(name_buffer, "XK_%c", 'A' + (char)(key - KEY_A));
901 else if (key >= KEY_a && key <= KEY_z)
902 sprintf(name_buffer, "XK_%c", 'a' + (char)(key - KEY_a));
903 else if (key >= KEY_0 && key <= KEY_9)
904 sprintf(name_buffer, "XK_%c", '0' + (char)(key - KEY_0));
905 else if (key >= KEY_KP_0 && key <= KEY_KP_9)
906 sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - KEY_KP_0));
907 else if (key >= KEY_F1 && key <= KEY_F24)
908 sprintf(name_buffer, "XK_F%d", (int)(key - KEY_F1 + 1));
909 else if (key == KEY_UNDEFINED)
910 strcpy(name_buffer, "[undefined]");
917 if (key == translate_key[i].key)
919 strcpy(name_buffer, translate_key[i].x11name);
923 while (translate_key[++i].x11name);
925 if (!translate_key[i].x11name)
926 sprintf(name_buffer, "0x%04lx", (unsigned long)key);
929 *x11name = name_buffer;
931 else if (mode == TRANSLATE_X11KEYNAME_TO_KEY)
933 Key key = KEY_UNDEFINED;
934 char *name_ptr = *x11name;
936 if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4)
938 char c = name_ptr[3];
940 if (c >= 'A' && c <= 'Z')
941 key = KEY_A + (Key)(c - 'A');
942 else if (c >= 'a' && c <= 'z')
943 key = KEY_a + (Key)(c - 'a');
944 else if (c >= '0' && c <= '9')
945 key = KEY_0 + (Key)(c - '0');
947 else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7)
949 char c = name_ptr[6];
951 if (c >= '0' && c <= '9')
952 key = KEY_0 + (Key)(c - '0');
954 else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
956 char c1 = name_ptr[4];
957 char c2 = name_ptr[5];
960 if ((c1 >= '0' && c1 <= '9') &&
961 ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
962 d = atoi(&name_ptr[4]);
964 if (d >=1 && d <= 24)
965 key = KEY_F1 + (Key)(d - 1);
967 else if (strncmp(name_ptr, "XK_", 3) == 0)
973 if (strcmp(name_ptr, translate_key[i].x11name) == 0)
975 key = translate_key[i].key;
979 while (translate_key[++i].x11name);
981 else if (strncmp(name_ptr, "0x", 2) == 0)
983 unsigned long value = 0;
989 char c = *name_ptr++;
992 if (c >= '0' && c <= '9')
994 else if (c >= 'a' && c <= 'f')
995 d = (int)(c - 'a' + 10);
996 else if (c >= 'A' && c <= 'F')
997 d = (int)(c - 'A' + 10);
1005 value = value * 16 + d;
1016 char *getKeyNameFromKey(Key key)
1020 translate_keyname(&key, NULL, &name, TRANSLATE_KEY_TO_KEYNAME);
1024 char *getX11KeyNameFromKey(Key key)
1028 translate_keyname(&key, &x11name, NULL, TRANSLATE_KEY_TO_X11KEYNAME);
1032 Key getKeyFromX11KeyName(char *x11name)
1036 translate_keyname(&key, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEY);
1040 char getCharFromKey(Key key)
1042 char *keyname = getKeyNameFromKey(key);
1045 if (strlen(keyname) == 1)
1046 letter = keyname[0];
1047 else if (strcmp(keyname, "space") == 0)
1049 else if (strcmp(keyname, "circumflex") == 0)
1055 #define TRANSLATE_JOYSYMBOL_TO_JOYNAME 0
1056 #define TRANSLATE_JOYNAME_TO_JOYSYMBOL 1
1058 void translate_joyname(int *joysymbol, char **name, int mode)
1066 { JOY_LEFT, "joystick_left" },
1067 { JOY_RIGHT, "joystick_right" },
1068 { JOY_UP, "joystick_up" },
1069 { JOY_DOWN, "joystick_down" },
1070 { JOY_BUTTON_1, "joystick_button_1" },
1071 { JOY_BUTTON_2, "joystick_button_2" },
1076 if (mode == TRANSLATE_JOYSYMBOL_TO_JOYNAME)
1078 *name = "[undefined]";
1082 if (*joysymbol == translate_joy[i].joysymbol)
1084 *name = translate_joy[i].name;
1089 else if (mode == TRANSLATE_JOYNAME_TO_JOYSYMBOL)
1095 if (strcmp(*name, translate_joy[i].name) == 0)
1097 *joysymbol = translate_joy[i].joysymbol;
1104 char *getJoyNameFromJoySymbol(int joysymbol)
1108 translate_joyname(&joysymbol, &name, TRANSLATE_JOYSYMBOL_TO_JOYNAME);
1112 int getJoySymbolFromJoyName(char *name)
1116 translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL);
1120 int getJoystickNrFromDeviceName(char *device_name)
1123 int joystick_nr = 0;
1125 if (device_name == NULL || device_name[0] == '\0')
1128 c = device_name[strlen(device_name) - 1];
1130 if (c >= '0' && c <= '9')
1131 joystick_nr = (int)(c - '0');
1133 if (joystick_nr < 0 || joystick_nr >= MAX_PLAYERS)
1139 /* ------------------------------------------------------------------------- */
1140 /* some functions to handle lists of level directories */
1141 /* ------------------------------------------------------------------------- */
1143 struct LevelDirInfo *newLevelDirInfo()
1145 return checked_calloc(sizeof(struct LevelDirInfo));
1148 void pushLevelDirInfo(struct LevelDirInfo **node_first,
1149 struct LevelDirInfo *node_new)
1151 node_new->next = *node_first;
1152 *node_first = node_new;
1155 int numLevelDirInfo(struct LevelDirInfo *node)
1168 boolean validLevelSeries(struct LevelDirInfo *node)
1170 return (node != NULL && !node->node_group && !node->parent_link);
1173 struct LevelDirInfo *getFirstValidLevelSeries(struct LevelDirInfo *node)
1175 if (node == NULL) /* start with first level directory entry */
1176 return getFirstValidLevelSeries(leveldir_first);
1177 else if (node->node_group) /* enter level group (step down into tree) */
1178 return getFirstValidLevelSeries(node->node_group);
1179 else if (node->parent_link) /* skip start entry of level group */
1181 if (node->next) /* get first real level series entry */
1182 return getFirstValidLevelSeries(node->next);
1183 else /* leave empty level group and go on */
1184 return getFirstValidLevelSeries(node->node_parent->next);
1186 else /* this seems to be a regular level series */
1190 struct LevelDirInfo *getLevelDirInfoFirstGroupEntry(struct LevelDirInfo *node)
1195 if (node->node_parent == NULL) /* top level group */
1196 return leveldir_first;
1197 else /* sub level group */
1198 return node->node_parent->node_group;
1201 int numLevelDirInfoInGroup(struct LevelDirInfo *node)
1203 return numLevelDirInfo(getLevelDirInfoFirstGroupEntry(node));
1206 int posLevelDirInfo(struct LevelDirInfo *node)
1208 struct LevelDirInfo *node_cmp = getLevelDirInfoFirstGroupEntry(node);
1213 if (node_cmp == node)
1217 node_cmp = node_cmp->next;
1223 struct LevelDirInfo *getLevelDirInfoFromPos(struct LevelDirInfo *node, int pos)
1225 struct LevelDirInfo *node_default = node;
1237 return node_default;
1240 struct LevelDirInfo *getLevelDirInfoFromFilenameExt(struct LevelDirInfo *node,
1243 if (filename == NULL)
1248 if (node->node_group)
1250 struct LevelDirInfo *node_group;
1252 node_group = getLevelDirInfoFromFilenameExt(node->node_group, filename);
1257 else if (!node->parent_link)
1259 if (strcmp(filename, node->filename) == 0)
1269 struct LevelDirInfo *getLevelDirInfoFromFilename(char *filename)
1271 return getLevelDirInfoFromFilenameExt(leveldir_first, filename);
1274 void dumpLevelDirInfo(struct LevelDirInfo *node, int depth)
1280 for (i=0; i<depth * 3; i++)
1283 printf("filename == '%s'\n", node->filename);
1285 if (node->node_group != NULL)
1286 dumpLevelDirInfo(node->node_group, depth + 1);
1292 void sortLevelDirInfo(struct LevelDirInfo **node_first,
1293 int (*compare_function)(const void *, const void *))
1295 int num_nodes = numLevelDirInfo(*node_first);
1296 struct LevelDirInfo **sort_array;
1297 struct LevelDirInfo *node = *node_first;
1303 /* allocate array for sorting structure pointers */
1304 sort_array = checked_calloc(num_nodes * sizeof(struct LevelDirInfo *));
1306 /* writing structure pointers to sorting array */
1307 while (i < num_nodes && node) /* double boundary check... */
1309 sort_array[i] = node;
1315 /* sorting the structure pointers in the sorting array */
1316 qsort(sort_array, num_nodes, sizeof(struct LevelDirInfo *),
1319 /* update the linkage of list elements with the sorted node array */
1320 for (i=0; i<num_nodes - 1; i++)
1321 sort_array[i]->next = sort_array[i + 1];
1322 sort_array[num_nodes - 1]->next = NULL;
1324 /* update the linkage of the main list anchor pointer */
1325 *node_first = sort_array[0];
1329 /* now recursively sort the level group structures */
1333 if (node->node_group != NULL)
1334 sortLevelDirInfo(&node->node_group, compare_function);
1340 inline void swap_numbers(int *i1, int *i2)
1348 inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2)
1361 /* ------------------------------------------------------------------------- */
1362 /* the following is only for debugging purpose and normally not used */
1363 /* ------------------------------------------------------------------------- */
1365 #define DEBUG_NUM_TIMESTAMPS 3
1367 void debug_print_timestamp(int counter_nr, char *message)
1369 static long counter[DEBUG_NUM_TIMESTAMPS][2];
1371 if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
1372 Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
1374 counter[counter_nr][0] = Counter();
1377 printf("%s %.2f seconds\n", message,
1378 (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
1380 counter[counter_nr][1] = Counter();