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/param.h>
19 #include <sys/types.h>
32 volatile unsigned long counter = 0;
34 void increment_counter()
39 END_OF_FUNCTION(increment_counter);
44 /* maximal allowed length of a command line option */
45 #define MAX_OPTION_LEN 256
48 static unsigned long mainCounter(int mode)
50 static struct timeval base_time = { 0, 0 };
51 struct timeval current_time;
52 unsigned long counter_ms;
54 gettimeofday(¤t_time, NULL);
56 if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec)
57 base_time = current_time;
59 counter_ms = (current_time.tv_sec - base_time.tv_sec) * 1000
60 + (current_time.tv_usec - base_time.tv_usec) / 1000;
62 return counter_ms; /* return milliseconds since last init */
66 void InitCounter() /* set counter back to zero */
69 mainCounter(INIT_COUNTER);
71 LOCK_VARIABLE(counter);
72 LOCK_FUNCTION(increment_counter);
73 install_int_ex(increment_counter, BPS_TO_TIMER(100));
77 unsigned long Counter() /* get milliseconds since last call of InitCounter() */
80 return mainCounter(READ_COUNTER);
82 return (counter * 10);
86 static void sleep_milliseconds(unsigned long milliseconds_delay)
88 boolean do_busy_waiting = (milliseconds_delay < 5 ? TRUE : FALSE);
91 /* donït use select() to perform waiting operations under DOS/Windows
92 environment; always use a busy loop for waiting instead */
93 do_busy_waiting = TRUE;
98 /* we want to wait only a few ms -- if we assume that we have a
99 kernel timer resolution of 10 ms, we would wait far to long;
100 therefore it's better to do a short interval of busy waiting
101 to get our sleeping time more accurate */
103 unsigned long base_counter = Counter(), actual_counter = Counter();
105 while (actual_counter < base_counter + milliseconds_delay &&
106 actual_counter >= base_counter)
107 actual_counter = Counter();
111 struct timeval delay;
113 delay.tv_sec = milliseconds_delay / 1000;
114 delay.tv_usec = 1000 * (milliseconds_delay % 1000);
116 if (select(0, NULL, NULL, NULL, &delay) != 0)
117 Error(ERR_WARN, "sleep_milliseconds(): select() failed");
121 void Delay(unsigned long delay) /* Sleep specified number of milliseconds */
123 sleep_milliseconds(delay);
126 boolean FrameReached(unsigned long *frame_counter_var,
127 unsigned long frame_delay)
129 unsigned long actual_frame_counter = FrameCounter;
131 if (actual_frame_counter < *frame_counter_var+frame_delay &&
132 actual_frame_counter >= *frame_counter_var)
135 *frame_counter_var = actual_frame_counter;
139 boolean DelayReached(unsigned long *counter_var,
142 unsigned long actual_counter = Counter();
144 if (actual_counter < *counter_var + delay &&
145 actual_counter >= *counter_var)
148 *counter_var = actual_counter;
152 void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
154 unsigned long actual_counter;
158 actual_counter = Counter();
160 if (actual_counter < *counter_var + delay &&
161 actual_counter >= *counter_var)
162 sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
167 *counter_var = actual_counter;
170 /* int2str() returns a number converted to a string;
171 the used memory is static, but will be overwritten by later calls,
172 so if you want to save the result, copy it to a private string buffer;
173 there can be 10 local calls of int2str() without buffering the result --
174 the 11th call will then destroy the result from the first call and so on.
177 char *int2str(int number, int size)
179 static char shift_array[10][40];
180 static int shift_counter = 0;
181 char *s = shift_array[shift_counter];
183 shift_counter = (shift_counter + 1) % 10;
190 sprintf(s, " %09d", number);
191 return &s[strlen(s) - size];
195 sprintf(s, "%d", number);
200 unsigned int SimpleRND(unsigned int max)
202 static unsigned long root = 654321;
203 struct timeval current_time;
205 gettimeofday(¤t_time,NULL);
206 root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
211 static unsigned int last_RND_value = 0;
213 unsigned int last_RND()
215 return last_RND_value;
219 unsigned int RND(unsigned int max)
222 return (last_RND_value = random_linux_libc() % max);
224 return (random_linux_libc() % max);
228 unsigned int InitRND(long seed)
230 struct timeval current_time;
232 if (seed == NEW_RANDOMIZE)
234 gettimeofday(¤t_time,NULL);
235 srandom_linux_libc((unsigned int) current_time.tv_usec);
236 return (unsigned int)current_time.tv_usec;
240 srandom_linux_libc((unsigned int) seed);
241 return (unsigned int)seed;
249 if ((pwd = getpwuid(getuid())) == NULL)
250 return ANONYMOUS_NAME;
260 if ((pwd = getpwuid(getuid())) == NULL || strlen(pwd->pw_gecos) == 0)
261 return ANONYMOUS_NAME;
264 static char real_name[1024];
265 char *from_ptr = pwd->pw_gecos, *to_ptr = real_name;
267 if (strchr(pwd->pw_gecos, 'ß') == NULL)
268 return pwd->pw_gecos;
270 /* the user's real name contains a 'ß' character (german sharp s),
271 which has no equivalent in upper case letters (which our fonts use) */
272 while (*from_ptr != '\0' && (long)(to_ptr - real_name) < 1024 - 2)
274 if (*from_ptr != 'ß')
275 *to_ptr++ = *from_ptr++;
288 return ANONYMOUS_NAME;
295 static char *home_dir = NULL;
299 if (!(home_dir = getenv("HOME")))
303 if ((pwd = getpwuid(getuid())))
304 home_dir = pwd->pw_dir;
316 char *getPath2(char *path1, char *path2)
318 char *complete_path = checked_malloc(strlen(path1) + 1 +
321 sprintf(complete_path, "%s/%s", path1, path2);
322 return complete_path;
325 char *getPath3(char *path1, char *path2, char *path3)
327 char *complete_path = checked_malloc(strlen(path1) + 1 +
331 sprintf(complete_path, "%s/%s/%s", path1, path2, path3);
332 return complete_path;
335 char *getStringCopy(char *s)
337 char *s_copy = checked_malloc(strlen(s) + 1);
343 char *getStringToLower(char *s)
345 char *s_copy = checked_malloc(strlen(s) + 1);
346 char *s_ptr = s_copy;
349 *s_ptr++ = tolower(*s++);
355 void MarkTileDirty(int x, int y)
357 int xx = redraw_x1 + x;
358 int yy = redraw_y1 + y;
363 redraw[xx][yy] = TRUE;
364 redraw_mask |= REDRAW_TILES;
367 void SetBorderElement()
371 BorderElement = EL_LEERRAUM;
373 for(y=0; y<lev_fieldy && BorderElement == EL_LEERRAUM; y++)
375 for(x=0; x<lev_fieldx; x++)
377 if (!IS_MASSIVE(Feld[x][y]))
378 BorderElement = EL_BETON;
380 if (y != 0 && y != lev_fieldy - 1 && x != lev_fieldx - 1)
386 void GetOptions(char *argv[])
388 char **options_left = &argv[1];
390 /* initialize global program options */
391 options.display_name = NULL;
392 options.server_host = NULL;
393 options.server_port = 0;
394 options.ro_base_directory = RO_BASE_PATH;
395 options.rw_base_directory = RW_BASE_PATH;
396 options.level_directory = RO_BASE_PATH "/" LEVELS_DIRECTORY;
397 options.serveronly = FALSE;
398 options.network = FALSE;
399 options.verbose = FALSE;
401 while (*options_left)
403 char option_str[MAX_OPTION_LEN];
404 char *option = options_left[0];
405 char *next_option = options_left[1];
406 char *option_arg = NULL;
407 int option_len = strlen(option);
409 if (option_len >= MAX_OPTION_LEN)
410 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
412 strcpy(option_str, option); /* copy argument into buffer */
415 if (strcmp(option, "--") == 0) /* stop scanning arguments */
418 if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */
421 option_arg = strchr(option, '=');
422 if (option_arg == NULL) /* no '=' in option */
423 option_arg = next_option;
426 *option_arg++ = '\0'; /* cut argument from option */
427 if (*option_arg == '\0') /* no argument after '=' */
428 Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
431 option_len = strlen(option);
433 if (strcmp(option, "-") == 0)
434 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
435 else if (strncmp(option, "-help", option_len) == 0)
437 printf("Usage: %s [options] [server.name [port]]\n"
439 " -d, --display machine:0 X server display\n"
440 " -b, --basepath directory alternative base directory\n"
441 " -l, --levels directory alternative level directory\n"
442 " -s, --serveronly only start network server\n"
443 " -n, --network network multiplayer game\n"
444 " -v, --verbose verbose mode\n",
448 else if (strncmp(option, "-display", option_len) == 0)
450 if (option_arg == NULL)
451 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
453 options.display_name = option_arg;
454 if (option_arg == next_option)
457 else if (strncmp(option, "-basepath", option_len) == 0)
459 if (option_arg == NULL)
460 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
462 /* this should be extended to separate options for ro and rw data */
463 options.ro_base_directory = option_arg;
464 options.rw_base_directory = option_arg;
465 if (option_arg == next_option)
468 /* adjust path for level directory accordingly */
469 options.level_directory =
470 getPath2(options.ro_base_directory, LEVELS_DIRECTORY);
472 else if (strncmp(option, "-levels", option_len) == 0)
474 if (option_arg == NULL)
475 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
477 options.level_directory = option_arg;
478 if (option_arg == next_option)
481 else if (strncmp(option, "-network", option_len) == 0)
483 options.network = TRUE;
485 else if (strncmp(option, "-serveronly", option_len) == 0)
487 options.serveronly = TRUE;
489 else if (strncmp(option, "-verbose", option_len) == 0)
491 options.verbose = TRUE;
493 else if (*option == '-')
495 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
497 else if (options.server_host == NULL)
499 options.server_host = *options_left;
501 else if (options.server_port == 0)
503 options.server_port = atoi(*options_left);
504 if (options.server_port < 1024)
505 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
508 Error(ERR_EXIT_HELP, "too many arguments");
514 void Error(int mode, char *format, ...)
516 char *process_name = "";
517 FILE *error = stderr;
519 /* display warnings only when running in verbose mode */
520 if (mode & ERR_WARN && !options.verbose)
524 if ((error = openErrorFile()) == NULL)
526 printf("Cannot write to error output file!\n");
531 if (mode & ERR_SOUND_SERVER)
532 process_name = " sound server";
533 else if (mode & ERR_NETWORK_SERVER)
534 process_name = " network server";
535 else if (mode & ERR_NETWORK_CLIENT)
536 process_name = " network client **";
542 fprintf(error, "%s%s: ", program_name, process_name);
545 fprintf(error, "warning: ");
547 va_start(ap, format);
548 vfprintf(error, format, ap);
551 fprintf(error, "\n");
555 fprintf(error, "%s: Try option '--help' for more information.\n",
559 fprintf(error, "%s%s: aborting\n", program_name, process_name);
566 if (mode & ERR_FROM_SERVER)
567 exit(1); /* child process: normal exit */
569 CloseAllAndExit(1); /* main process: clean up stuff */
573 void *checked_malloc(unsigned long size)
580 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
585 void *checked_calloc(unsigned long size)
589 ptr = calloc(1, size);
592 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
597 short getFile16BitInteger(FILE *file, int byte_order)
599 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
600 return ((fgetc(file) << 8) |
602 else /* BYTE_ORDER_LITTLE_ENDIAN */
603 return ((fgetc(file) << 0) |
607 void putFile16BitInteger(FILE *file, short value, int byte_order)
609 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
611 fputc((value >> 8) & 0xff, file);
612 fputc((value >> 0) & 0xff, file);
614 else /* BYTE_ORDER_LITTLE_ENDIAN */
616 fputc((value >> 0) & 0xff, file);
617 fputc((value >> 8) & 0xff, file);
621 int getFile32BitInteger(FILE *file, int byte_order)
623 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
624 return ((fgetc(file) << 24) |
625 (fgetc(file) << 16) |
628 else /* BYTE_ORDER_LITTLE_ENDIAN */
629 return ((fgetc(file) << 0) |
631 (fgetc(file) << 16) |
632 (fgetc(file) << 24));
635 void putFile32BitInteger(FILE *file, int value, int byte_order)
637 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
639 fputc((value >> 24) & 0xff, file);
640 fputc((value >> 16) & 0xff, file);
641 fputc((value >> 8) & 0xff, file);
642 fputc((value >> 0) & 0xff, file);
644 else /* BYTE_ORDER_LITTLE_ENDIAN */
646 fputc((value >> 0) & 0xff, file);
647 fputc((value >> 8) & 0xff, file);
648 fputc((value >> 16) & 0xff, file);
649 fputc((value >> 24) & 0xff, file);
653 void getFileChunk(FILE *file, char *chunk_buffer, int *chunk_length,
656 const int chunk_identifier_length = 4;
658 /* read chunk identifier */
659 fgets(chunk_buffer, chunk_identifier_length + 1, file);
661 /* read chunk length */
662 *chunk_length = getFile32BitInteger(file, byte_order);
665 void putFileChunk(FILE *file, char *chunk_name, int chunk_length,
668 /* write chunk identifier */
669 fputs(chunk_name, file);
671 /* write chunk length */
672 putFile32BitInteger(file, chunk_length, byte_order);
675 #define TRANSLATE_KEYSYM_TO_KEYNAME 0
676 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
677 #define TRANSLATE_X11KEYNAME_TO_KEYSYM 2
679 void translate_keyname(KeySym *keysym, char **x11name, char **name, int mode)
688 /* normal cursor keys */
689 { XK_Left, "XK_Left", "cursor left" },
690 { XK_Right, "XK_Right", "cursor right" },
691 { XK_Up, "XK_Up", "cursor up" },
692 { XK_Down, "XK_Down", "cursor down" },
694 /* keypad cursor keys */
696 { XK_KP_Left, "XK_KP_Left", "keypad left" },
697 { XK_KP_Right, "XK_KP_Right", "keypad right" },
698 { XK_KP_Up, "XK_KP_Up", "keypad up" },
699 { XK_KP_Down, "XK_KP_Down", "keypad down" },
702 /* other keypad keys */
704 { XK_KP_Enter, "XK_KP_Enter", "keypad enter" },
705 { XK_KP_Add, "XK_KP_Add", "keypad +" },
706 { XK_KP_Subtract, "XK_KP_Subtract", "keypad -" },
707 { XK_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
708 { XK_KP_Divide, "XK_KP_Divide", "keypad /" },
709 { XK_KP_Separator, "XK_KP_Separator", "keypad ," },
713 { XK_Shift_L, "XK_Shift_L", "left shift" },
714 { XK_Shift_R, "XK_Shift_R", "right shift" },
715 { XK_Control_L, "XK_Control_L", "left control" },
716 { XK_Control_R, "XK_Control_R", "right control" },
717 { XK_Meta_L, "XK_Meta_L", "left meta" },
718 { XK_Meta_R, "XK_Meta_R", "right meta" },
719 { XK_Alt_L, "XK_Alt_L", "left alt" },
720 { XK_Alt_R, "XK_Alt_R", "right alt" },
721 { XK_Mode_switch, "XK_Mode_switch", "mode switch" },
722 { XK_Multi_key, "XK_Multi_key", "multi key" },
724 /* some special keys */
725 { XK_BackSpace, "XK_BackSpace", "backspace" },
726 { XK_Delete, "XK_Delete", "delete" },
727 { XK_Insert, "XK_Insert", "insert" },
728 { XK_Tab, "XK_Tab", "tab" },
729 { XK_Home, "XK_Home", "home" },
730 { XK_End, "XK_End", "end" },
731 { XK_Page_Up, "XK_Page_Up", "page up" },
732 { XK_Page_Down, "XK_Page_Down", "page down" },
735 /* ASCII 0x20 to 0x40 keys (except numbers) */
736 { XK_space, "XK_space", "space" },
737 { XK_exclam, "XK_exclam", "!" },
738 { XK_quotedbl, "XK_quotedbl", "\"" },
739 { XK_numbersign, "XK_numbersign", "#" },
740 { XK_dollar, "XK_dollar", "$" },
741 { XK_percent, "XK_percent", "%" },
742 { XK_ampersand, "XK_ampersand", "&" },
743 { XK_apostrophe, "XK_apostrophe", "'" },
744 { XK_parenleft, "XK_parenleft", "(" },
745 { XK_parenright, "XK_parenright", ")" },
746 { XK_asterisk, "XK_asterisk", "*" },
747 { XK_plus, "XK_plus", "+" },
748 { XK_comma, "XK_comma", "," },
749 { XK_minus, "XK_minus", "-" },
750 { XK_period, "XK_period", "." },
751 { XK_slash, "XK_slash", "/" },
752 { XK_colon, "XK_colon", ":" },
753 { XK_semicolon, "XK_semicolon", ";" },
754 { XK_less, "XK_less", "<" },
755 { XK_equal, "XK_equal", "=" },
756 { XK_greater, "XK_greater", ">" },
757 { XK_question, "XK_question", "?" },
758 { XK_at, "XK_at", "@" },
760 /* more ASCII keys */
761 { XK_bracketleft, "XK_bracketleft", "[" },
762 { XK_backslash, "XK_backslash", "backslash" },
763 { XK_bracketright, "XK_bracketright", "]" },
764 { XK_asciicircum, "XK_asciicircum", "circumflex" },
765 { XK_underscore, "XK_underscore", "_" },
766 { XK_grave, "XK_grave", "grave" },
767 { XK_quoteleft, "XK_quoteleft", "quote left" },
768 { XK_braceleft, "XK_braceleft", "brace left" },
769 { XK_bar, "XK_bar", "bar" },
770 { XK_braceright, "XK_braceright", "brace right" },
771 { XK_asciitilde, "XK_asciitilde", "ascii tilde" },
773 /* special (non-ASCII) keys */
774 { XK_Adiaeresis, "XK_Adiaeresis", "Ä" },
775 { XK_Odiaeresis, "XK_Odiaeresis", "Ö" },
776 { XK_Udiaeresis, "XK_Udiaeresis", "Ü" },
777 { XK_adiaeresis, "XK_adiaeresis", "ä" },
778 { XK_odiaeresis, "XK_odiaeresis", "ö" },
779 { XK_udiaeresis, "XK_udiaeresis", "ü" },
780 { XK_ssharp, "XK_ssharp", "sharp s" },
782 /* end-of-array identifier */
788 if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
790 static char name_buffer[30];
791 KeySym key = *keysym;
793 if (key >= XK_A && key <= XK_Z)
794 sprintf(name_buffer, "%c", 'A' + (char)(key - XK_A));
795 else if (key >= XK_a && key <= XK_z)
796 sprintf(name_buffer, "%c", 'a' + (char)(key - XK_a));
797 else if (key >= XK_0 && key <= XK_9)
798 sprintf(name_buffer, "%c", '0' + (char)(key - XK_0));
799 else if (key >= XK_KP_0 && key <= XK_KP_9)
800 sprintf(name_buffer, "keypad %c", '0' + (char)(key - XK_KP_0));
801 else if (key >= XK_F1 && key <= XK_F24)
802 sprintf(name_buffer, "function F%d", (int)(key - XK_F1 + 1));
803 else if (key == KEY_UNDEFINDED)
804 strcpy(name_buffer, "(undefined)");
811 if (key == translate_key[i].keysym)
813 strcpy(name_buffer, translate_key[i].name);
817 while (translate_key[++i].name);
819 if (!translate_key[i].name)
820 strcpy(name_buffer, "(unknown)");
825 else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME)
827 static char name_buffer[30];
828 KeySym key = *keysym;
830 if (key >= XK_A && key <= XK_Z)
831 sprintf(name_buffer, "XK_%c", 'A' + (char)(key - XK_A));
832 else if (key >= XK_a && key <= XK_z)
833 sprintf(name_buffer, "XK_%c", 'a' + (char)(key - XK_a));
834 else if (key >= XK_0 && key <= XK_9)
835 sprintf(name_buffer, "XK_%c", '0' + (char)(key - XK_0));
836 else if (key >= XK_KP_0 && key <= XK_KP_9)
837 sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - XK_KP_0));
838 else if (key >= XK_F1 && key <= XK_F24)
839 sprintf(name_buffer, "XK_F%d", (int)(key - XK_F1 + 1));
840 else if (key == KEY_UNDEFINDED)
841 strcpy(name_buffer, "[undefined]");
848 if (key == translate_key[i].keysym)
850 strcpy(name_buffer, translate_key[i].x11name);
854 while (translate_key[++i].x11name);
856 if (!translate_key[i].x11name)
857 sprintf(name_buffer, "0x%04lx", (unsigned long)key);
860 *x11name = name_buffer;
862 else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
864 KeySym key = XK_VoidSymbol;
865 char *name_ptr = *x11name;
867 if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4)
869 char c = name_ptr[3];
871 if (c >= 'A' && c <= 'Z')
872 key = XK_A + (KeySym)(c - 'A');
873 else if (c >= 'a' && c <= 'z')
874 key = XK_a + (KeySym)(c - 'a');
875 else if (c >= '0' && c <= '9')
876 key = XK_0 + (KeySym)(c - '0');
878 else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7)
880 char c = name_ptr[6];
882 if (c >= '0' && c <= '9')
883 key = XK_0 + (KeySym)(c - '0');
885 else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
887 char c1 = name_ptr[4];
888 char c2 = name_ptr[5];
891 if ((c1 >= '0' && c1 <= '9') &&
892 ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
893 d = atoi(&name_ptr[4]);
895 if (d >=1 && d <= 24)
896 key = XK_F1 + (KeySym)(d - 1);
898 else if (strncmp(name_ptr, "XK_", 3) == 0)
904 if (strcmp(name_ptr, translate_key[i].x11name) == 0)
906 key = translate_key[i].keysym;
910 while (translate_key[++i].x11name);
912 else if (strncmp(name_ptr, "0x", 2) == 0)
914 unsigned long value = 0;
920 char c = *name_ptr++;
923 if (c >= '0' && c <= '9')
925 else if (c >= 'a' && c <= 'f')
926 d = (int)(c - 'a' + 10);
927 else if (c >= 'A' && c <= 'F')
928 d = (int)(c - 'A' + 10);
936 value = value * 16 + d;
947 char *getKeyNameFromKeySym(KeySym keysym)
951 translate_keyname(&keysym, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME);
955 char *getX11KeyNameFromKeySym(KeySym keysym)
959 translate_keyname(&keysym, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME);
963 KeySym getKeySymFromX11KeyName(char *x11name)
967 translate_keyname(&keysym, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM);
971 char getCharFromKeySym(KeySym keysym)
973 char *keyname = getKeyNameFromKeySym(keysym);
976 if (strlen(keyname) == 1)
978 else if (strcmp(keyname, "space") == 0)
980 else if (strcmp(keyname, "circumflex") == 0)
986 #define TRANSLATE_JOYSYMBOL_TO_JOYNAME 0
987 #define TRANSLATE_JOYNAME_TO_JOYSYMBOL 1
989 void translate_joyname(int *joysymbol, char **name, int mode)
997 { JOY_LEFT, "joystick_left" },
998 { JOY_RIGHT, "joystick_right" },
999 { JOY_UP, "joystick_up" },
1000 { JOY_DOWN, "joystick_down" },
1001 { JOY_BUTTON_1, "joystick_button_1" },
1002 { JOY_BUTTON_2, "joystick_button_2" },
1007 if (mode == TRANSLATE_JOYSYMBOL_TO_JOYNAME)
1009 *name = "[undefined]";
1013 if (*joysymbol == translate_joy[i].joysymbol)
1015 *name = translate_joy[i].name;
1020 else if (mode == TRANSLATE_JOYNAME_TO_JOYSYMBOL)
1026 if (strcmp(*name, translate_joy[i].name) == 0)
1028 *joysymbol = translate_joy[i].joysymbol;
1035 char *getJoyNameFromJoySymbol(int joysymbol)
1039 translate_joyname(&joysymbol, &name, TRANSLATE_JOYSYMBOL_TO_JOYNAME);
1043 int getJoySymbolFromJoyName(char *name)
1047 translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL);
1051 int getJoystickNrFromDeviceName(char *device_name)
1054 int joystick_nr = 0;
1056 if (device_name == NULL || device_name[0] == '\0')
1059 c = device_name[strlen(device_name) - 1];
1061 if (c >= '0' && c <= '9')
1062 joystick_nr = (int)(c - '0');
1064 if (joystick_nr < 0 || joystick_nr >= MAX_PLAYERS)
1070 /* ------------------------------------------------------------------------- */
1071 /* some functions to handle lists of level directories */
1072 /* ------------------------------------------------------------------------- */
1074 struct LevelDirInfo *newLevelDirInfo()
1076 return checked_calloc(sizeof(struct LevelDirInfo));
1079 void pushLevelDirInfo(struct LevelDirInfo **node_first,
1080 struct LevelDirInfo *node_new)
1082 node_new->next = *node_first;
1083 *node_first = node_new;
1086 int numLevelDirInfo(struct LevelDirInfo *node)
1099 boolean validLevelSeries(struct LevelDirInfo *node)
1101 return (node != NULL && !node->node_group && !node->parent_link);
1104 struct LevelDirInfo *getFirstValidLevelSeries(struct LevelDirInfo *node)
1106 if (node == NULL) /* start with first level directory entry */
1107 return getFirstValidLevelSeries(leveldir_first);
1108 else if (node->node_group) /* enter level group (step down into tree) */
1109 return getFirstValidLevelSeries(node->node_group);
1110 else if (node->parent_link) /* skip start entry of level group */
1112 if (node->next) /* get first real level series entry */
1113 return getFirstValidLevelSeries(node->next);
1114 else /* leave empty level group and go on */
1115 return getFirstValidLevelSeries(node->node_parent->next);
1117 else /* this seems to be a regular level series */
1121 struct LevelDirInfo *getLevelDirInfoFirstGroupEntry(struct LevelDirInfo *node)
1126 if (node->node_parent == NULL) /* top level group */
1127 return leveldir_first;
1128 else /* sub level group */
1129 return node->node_parent->node_group;
1132 int numLevelDirInfoInGroup(struct LevelDirInfo *node)
1134 return numLevelDirInfo(getLevelDirInfoFirstGroupEntry(node));
1137 int posLevelDirInfo(struct LevelDirInfo *node)
1139 struct LevelDirInfo *node_cmp = getLevelDirInfoFirstGroupEntry(node);
1144 if (node_cmp == node)
1148 node_cmp = node_cmp->next;
1154 struct LevelDirInfo *getLevelDirInfoFromPos(struct LevelDirInfo *node, int pos)
1156 struct LevelDirInfo *node_default = node;
1168 return node_default;
1171 struct LevelDirInfo *getLevelDirInfoFromFilenameExt(struct LevelDirInfo *node,
1174 if (filename == NULL)
1179 if (node->node_group)
1181 struct LevelDirInfo *node_group;
1183 node_group = getLevelDirInfoFromFilenameExt(node->node_group, filename);
1188 else if (!node->parent_link)
1190 if (strcmp(filename, node->filename) == 0)
1200 struct LevelDirInfo *getLevelDirInfoFromFilename(char *filename)
1202 return getLevelDirInfoFromFilenameExt(leveldir_first, filename);
1205 void dumpLevelDirInfo(struct LevelDirInfo *node, int depth)
1211 for (i=0; i<depth * 3; i++)
1214 printf("filename == '%s'\n", node->filename);
1216 if (node->node_group != NULL)
1217 dumpLevelDirInfo(node->node_group, depth + 1);
1223 void sortLevelDirInfo(struct LevelDirInfo **node_first,
1224 int (*compare_function)(const void *, const void *))
1226 int num_nodes = numLevelDirInfo(*node_first);
1227 struct LevelDirInfo **sort_array;
1228 struct LevelDirInfo *node = *node_first;
1234 /* allocate array for sorting structure pointers */
1235 sort_array = checked_calloc(num_nodes * sizeof(struct LevelDirInfo *));
1237 /* writing structure pointers to sorting array */
1238 while (i < num_nodes && node) /* double boundary check... */
1240 sort_array[i] = node;
1246 /* sorting the structure pointers in the sorting array */
1247 qsort(sort_array, num_nodes, sizeof(struct LevelDirInfo *),
1250 /* update the linkage of list elements with the sorted node array */
1251 for (i=0; i<num_nodes - 1; i++)
1252 sort_array[i]->next = sort_array[i + 1];
1253 sort_array[num_nodes - 1]->next = NULL;
1255 /* update the linkage of the main list anchor pointer */
1256 *node_first = sort_array[0];
1260 /* now recursively sort the level group structures */
1264 if (node->node_group != NULL)
1265 sortLevelDirInfo(&node->node_group, compare_function);
1272 /* ------------------------------------------------------------------------- */
1273 /* the following is only for debugging purpose and normally not used */
1274 /* ------------------------------------------------------------------------- */
1276 #define DEBUG_NUM_TIMESTAMPS 3
1278 void debug_print_timestamp(int counter_nr, char *message)
1280 static long counter[DEBUG_NUM_TIMESTAMPS][2];
1282 if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
1283 Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
1285 counter[counter_nr][0] = Counter();
1288 printf("%s %.2f seconds\n", message,
1289 (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
1291 counter[counter_nr][1] = Counter();