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 char *int2str(int number, int size)
179 sprintf(s, " %09d", number);
180 return &s[strlen(s) - size];
184 sprintf(s, "%d", number);
189 unsigned int SimpleRND(unsigned int max)
191 static unsigned long root = 654321;
192 struct timeval current_time;
194 gettimeofday(¤t_time,NULL);
195 root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
200 static unsigned int last_RND_value = 0;
202 unsigned int last_RND()
204 return last_RND_value;
208 unsigned int RND(unsigned int max)
211 return (last_RND_value = random_linux_libc() % max);
213 return (random_linux_libc() % max);
217 unsigned int InitRND(long seed)
219 struct timeval current_time;
221 if (seed == NEW_RANDOMIZE)
223 gettimeofday(¤t_time,NULL);
224 srandom_linux_libc((unsigned int) current_time.tv_usec);
225 return (unsigned int)current_time.tv_usec;
229 srandom_linux_libc((unsigned int) seed);
230 return (unsigned int)seed;
238 if ((pwd = getpwuid(getuid())) == NULL)
239 return ANONYMOUS_NAME;
249 if ((pwd = getpwuid(getuid())) == NULL || strlen(pwd->pw_gecos) == 0)
250 return ANONYMOUS_NAME;
253 static char real_name[1024];
254 char *from_ptr = pwd->pw_gecos, *to_ptr = real_name;
256 if (strchr(pwd->pw_gecos, 'ĂŸ') == NULL)
257 return pwd->pw_gecos;
259 /* the user's real name contains a 'ĂŸ' character (german sharp s),
260 which has no equivalent in upper case letters (which our fonts use) */
261 while (*from_ptr != '\0' && (long)(to_ptr - real_name) < 1024 - 2)
263 if (*from_ptr != 'ĂŸ')
264 *to_ptr++ = *from_ptr++;
277 return ANONYMOUS_NAME;
284 static char *home_dir = NULL;
288 if (!(home_dir = getenv("HOME")))
292 if ((pwd = getpwuid(getuid())))
293 home_dir = pwd->pw_dir;
305 char *getPath2(char *path1, char *path2)
307 char *complete_path = checked_malloc(strlen(path1) + 1 +
310 sprintf(complete_path, "%s/%s", path1, path2);
311 return complete_path;
314 char *getPath3(char *path1, char *path2, char *path3)
316 char *complete_path = checked_malloc(strlen(path1) + 1 +
320 sprintf(complete_path, "%s/%s/%s", path1, path2, path3);
321 return complete_path;
324 char *getStringCopy(char *s)
326 char *s_copy = checked_malloc(strlen(s) + 1);
332 char *getStringToLower(char *s)
334 char *s_copy = checked_malloc(strlen(s) + 1);
335 char *s_ptr = s_copy;
338 *s_ptr++ = tolower(*s++);
343 void MarkTileDirty(int x, int y)
345 int xx = redraw_x1 + x;
346 int yy = redraw_y1 + y;
351 redraw[xx][yy] = TRUE;
352 redraw_mask |= REDRAW_TILES;
355 void SetBorderElement()
359 BorderElement = EL_LEERRAUM;
361 for(y=0; y<lev_fieldy && BorderElement == EL_LEERRAUM; y++)
363 for(x=0; x<lev_fieldx; x++)
365 if (!IS_MASSIVE(Feld[x][y]))
366 BorderElement = EL_BETON;
368 if (y != 0 && y != lev_fieldy - 1 && x != lev_fieldx - 1)
374 void GetOptions(char *argv[])
376 char **options_left = &argv[1];
378 /* initialize global program options */
379 options.display_name = NULL;
380 options.server_host = NULL;
381 options.server_port = 0;
382 options.ro_base_directory = RO_BASE_PATH;
383 options.rw_base_directory = RW_BASE_PATH;
384 options.level_directory = RO_BASE_PATH "/" LEVELS_DIRECTORY;
385 options.serveronly = FALSE;
386 options.network = FALSE;
387 options.verbose = FALSE;
389 while (*options_left)
391 char option_str[MAX_OPTION_LEN];
392 char *option = options_left[0];
393 char *next_option = options_left[1];
394 char *option_arg = NULL;
395 int option_len = strlen(option);
397 if (option_len >= MAX_OPTION_LEN)
398 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
400 strcpy(option_str, option); /* copy argument into buffer */
403 if (strcmp(option, "--") == 0) /* stop scanning arguments */
406 if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */
409 option_arg = strchr(option, '=');
410 if (option_arg == NULL) /* no '=' in option */
411 option_arg = next_option;
414 *option_arg++ = '\0'; /* cut argument from option */
415 if (*option_arg == '\0') /* no argument after '=' */
416 Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
419 option_len = strlen(option);
421 if (strcmp(option, "-") == 0)
422 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
423 else if (strncmp(option, "-help", option_len) == 0)
425 printf("Usage: %s [options] [server.name [port]]\n"
427 " -d, --display machine:0 X server display\n"
428 " -b, --basepath directory alternative base directory\n"
429 " -l, --levels directory alternative level directory\n"
430 " -s, --serveronly only start network server\n"
431 " -n, --network network multiplayer game\n"
432 " -v, --verbose verbose mode\n",
436 else if (strncmp(option, "-display", option_len) == 0)
438 if (option_arg == NULL)
439 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
441 options.display_name = option_arg;
442 if (option_arg == next_option)
445 else if (strncmp(option, "-basepath", option_len) == 0)
447 if (option_arg == NULL)
448 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
450 /* this should be extended to separate options for ro and rw data */
451 options.ro_base_directory = option_arg;
452 options.rw_base_directory = option_arg;
453 if (option_arg == next_option)
456 /* adjust path for level directory accordingly */
457 options.level_directory =
458 getPath2(options.ro_base_directory, LEVELS_DIRECTORY);
460 else if (strncmp(option, "-levels", option_len) == 0)
462 if (option_arg == NULL)
463 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
465 options.level_directory = option_arg;
466 if (option_arg == next_option)
469 else if (strncmp(option, "-network", option_len) == 0)
471 options.network = TRUE;
473 else if (strncmp(option, "-serveronly", option_len) == 0)
475 options.serveronly = TRUE;
477 else if (strncmp(option, "-verbose", option_len) == 0)
479 options.verbose = TRUE;
481 else if (*option == '-')
483 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
485 else if (options.server_host == NULL)
487 options.server_host = *options_left;
489 else if (options.server_port == 0)
491 options.server_port = atoi(*options_left);
492 if (options.server_port < 1024)
493 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
496 Error(ERR_EXIT_HELP, "too many arguments");
502 void Error(int mode, char *format, ...)
504 char *process_name = "";
505 FILE *error = stderr;
507 /* display warnings only when running in verbose mode */
508 if (mode & ERR_WARN && !options.verbose)
512 if ((error = openErrorFile()) == NULL)
514 printf("Cannot write to error output file!\n");
519 if (mode & ERR_SOUND_SERVER)
520 process_name = " sound server";
521 else if (mode & ERR_NETWORK_SERVER)
522 process_name = " network server";
523 else if (mode & ERR_NETWORK_CLIENT)
524 process_name = " network client **";
530 fprintf(error, "%s%s: ", program_name, process_name);
533 fprintf(error, "warning: ");
535 va_start(ap, format);
536 vfprintf(error, format, ap);
539 fprintf(error, "\n");
543 fprintf(error, "%s: Try option '--help' for more information.\n",
547 fprintf(error, "%s%s: aborting\n", program_name, process_name);
554 if (mode & ERR_FROM_SERVER)
555 exit(1); /* child process: normal exit */
557 CloseAllAndExit(1); /* main process: clean up stuff */
561 void *checked_malloc(unsigned long size)
568 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
573 void *checked_calloc(unsigned long size)
577 ptr = calloc(1, size);
580 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
585 short getFile16BitInteger(FILE *file, int byte_order)
587 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
588 return ((fgetc(file) << 8) |
590 else /* BYTE_ORDER_LITTLE_ENDIAN */
591 return ((fgetc(file) << 0) |
595 void putFile16BitInteger(FILE *file, short value, int byte_order)
597 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
599 fputc((value >> 8) & 0xff, file);
600 fputc((value >> 0) & 0xff, file);
602 else /* BYTE_ORDER_LITTLE_ENDIAN */
604 fputc((value >> 0) & 0xff, file);
605 fputc((value >> 8) & 0xff, file);
609 int getFile32BitInteger(FILE *file, int byte_order)
611 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
612 return ((fgetc(file) << 24) |
613 (fgetc(file) << 16) |
616 else /* BYTE_ORDER_LITTLE_ENDIAN */
617 return ((fgetc(file) << 0) |
619 (fgetc(file) << 16) |
620 (fgetc(file) << 24));
623 void putFile32BitInteger(FILE *file, int value, int byte_order)
625 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
627 fputc((value >> 24) & 0xff, file);
628 fputc((value >> 16) & 0xff, file);
629 fputc((value >> 8) & 0xff, file);
630 fputc((value >> 0) & 0xff, file);
632 else /* BYTE_ORDER_LITTLE_ENDIAN */
634 fputc((value >> 0) & 0xff, file);
635 fputc((value >> 8) & 0xff, file);
636 fputc((value >> 16) & 0xff, file);
637 fputc((value >> 24) & 0xff, file);
641 void getFileChunk(FILE *file, char *chunk_buffer, int *chunk_length,
644 const int chunk_identifier_length = 4;
646 /* read chunk identifier */
647 fgets(chunk_buffer, chunk_identifier_length + 1, file);
649 /* read chunk length */
650 *chunk_length = getFile32BitInteger(file, byte_order);
653 void putFileChunk(FILE *file, char *chunk_name, int chunk_length,
656 /* write chunk identifier */
657 fputs(chunk_name, file);
659 /* write chunk length */
660 putFile32BitInteger(file, chunk_length, byte_order);
663 #define TRANSLATE_KEYSYM_TO_KEYNAME 0
664 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
665 #define TRANSLATE_X11KEYNAME_TO_KEYSYM 2
667 void translate_keyname(KeySym *keysym, char **x11name, char **name, int mode)
676 /* normal cursor keys */
677 { XK_Left, "XK_Left", "cursor left" },
678 { XK_Right, "XK_Right", "cursor right" },
679 { XK_Up, "XK_Up", "cursor up" },
680 { XK_Down, "XK_Down", "cursor down" },
682 /* keypad cursor keys */
684 { XK_KP_Left, "XK_KP_Left", "keypad left" },
685 { XK_KP_Right, "XK_KP_Right", "keypad right" },
686 { XK_KP_Up, "XK_KP_Up", "keypad up" },
687 { XK_KP_Down, "XK_KP_Down", "keypad down" },
690 /* other keypad keys */
692 { XK_KP_Enter, "XK_KP_Enter", "keypad enter" },
693 { XK_KP_Add, "XK_KP_Add", "keypad +" },
694 { XK_KP_Subtract, "XK_KP_Subtract", "keypad -" },
695 { XK_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
696 { XK_KP_Divide, "XK_KP_Divide", "keypad /" },
697 { XK_KP_Separator, "XK_KP_Separator", "keypad ," },
701 { XK_Shift_L, "XK_Shift_L", "left shift" },
702 { XK_Shift_R, "XK_Shift_R", "right shift" },
703 { XK_Control_L, "XK_Control_L", "left control" },
704 { XK_Control_R, "XK_Control_R", "right control" },
705 { XK_Meta_L, "XK_Meta_L", "left meta" },
706 { XK_Meta_R, "XK_Meta_R", "right meta" },
707 { XK_Alt_L, "XK_Alt_L", "left alt" },
708 { XK_Alt_R, "XK_Alt_R", "right alt" },
709 { XK_Mode_switch, "XK_Mode_switch", "mode switch" },
710 { XK_Multi_key, "XK_Multi_key", "multi key" },
712 /* some special keys */
713 { XK_BackSpace, "XK_BackSpace", "backspace" },
714 { XK_Delete, "XK_Delete", "delete" },
715 { XK_Insert, "XK_Insert", "insert" },
716 { XK_Tab, "XK_Tab", "tab" },
717 { XK_Home, "XK_Home", "home" },
718 { XK_End, "XK_End", "end" },
719 { XK_Page_Up, "XK_Page_Up", "page up" },
720 { XK_Page_Down, "XK_Page_Down", "page down" },
723 /* ASCII 0x20 to 0x40 keys (except numbers) */
724 { XK_space, "XK_space", "space" },
725 { XK_exclam, "XK_exclam", "!" },
726 { XK_quotedbl, "XK_quotedbl", "\"" },
727 { XK_numbersign, "XK_numbersign", "#" },
728 { XK_dollar, "XK_dollar", "$" },
729 { XK_percent, "XK_percent", "%" },
730 { XK_ampersand, "XK_ampersand", "&" },
731 { XK_apostrophe, "XK_apostrophe", "'" },
732 { XK_parenleft, "XK_parenleft", "(" },
733 { XK_parenright, "XK_parenright", ")" },
734 { XK_asterisk, "XK_asterisk", "*" },
735 { XK_plus, "XK_plus", "+" },
736 { XK_comma, "XK_comma", "," },
737 { XK_minus, "XK_minus", "-" },
738 { XK_period, "XK_period", "." },
739 { XK_slash, "XK_slash", "/" },
740 { XK_colon, "XK_colon", ":" },
741 { XK_semicolon, "XK_semicolon", ";" },
742 { XK_less, "XK_less", "<" },
743 { XK_equal, "XK_equal", "=" },
744 { XK_greater, "XK_greater", ">" },
745 { XK_question, "XK_question", "?" },
746 { XK_at, "XK_at", "@" },
748 /* more ASCII keys */
749 { XK_bracketleft, "XK_bracketleft", "[" },
750 { XK_backslash, "XK_backslash", "backslash" },
751 { XK_bracketright, "XK_bracketright", "]" },
752 { XK_asciicircum, "XK_asciicircum", "circumflex" },
753 { XK_underscore, "XK_underscore", "_" },
754 { XK_grave, "XK_grave", "grave" },
755 { XK_quoteleft, "XK_quoteleft", "quote left" },
756 { XK_braceleft, "XK_braceleft", "brace left" },
757 { XK_bar, "XK_bar", "bar" },
758 { XK_braceright, "XK_braceright", "brace right" },
759 { XK_asciitilde, "XK_asciitilde", "ascii tilde" },
761 /* special (non-ASCII) keys */
762 { XK_Adiaeresis, "XK_Adiaeresis", "Ă„" },
763 { XK_Odiaeresis, "XK_Odiaeresis", "Ă–" },
764 { XK_Udiaeresis, "XK_Udiaeresis", "Ăœ" },
765 { XK_adiaeresis, "XK_adiaeresis", "ä" },
766 { XK_odiaeresis, "XK_odiaeresis", "ö" },
767 { XK_udiaeresis, "XK_udiaeresis", "Ă¼" },
768 { XK_ssharp, "XK_ssharp", "sharp s" },
770 /* end-of-array identifier */
776 if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
778 static char name_buffer[30];
779 KeySym key = *keysym;
781 if (key >= XK_A && key <= XK_Z)
782 sprintf(name_buffer, "%c", 'A' + (char)(key - XK_A));
783 else if (key >= XK_a && key <= XK_z)
784 sprintf(name_buffer, "%c", 'a' + (char)(key - XK_a));
785 else if (key >= XK_0 && key <= XK_9)
786 sprintf(name_buffer, "%c", '0' + (char)(key - XK_0));
787 else if (key >= XK_KP_0 && key <= XK_KP_9)
788 sprintf(name_buffer, "keypad %c", '0' + (char)(key - XK_KP_0));
789 else if (key >= XK_F1 && key <= XK_F24)
790 sprintf(name_buffer, "function F%d", (int)(key - XK_F1 + 1));
791 else if (key == KEY_UNDEFINDED)
792 strcpy(name_buffer, "(undefined)");
799 if (key == translate_key[i].keysym)
801 strcpy(name_buffer, translate_key[i].name);
805 while (translate_key[++i].name);
807 if (!translate_key[i].name)
808 strcpy(name_buffer, "(unknown)");
813 else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME)
815 static char name_buffer[30];
816 KeySym key = *keysym;
818 if (key >= XK_A && key <= XK_Z)
819 sprintf(name_buffer, "XK_%c", 'A' + (char)(key - XK_A));
820 else if (key >= XK_a && key <= XK_z)
821 sprintf(name_buffer, "XK_%c", 'a' + (char)(key - XK_a));
822 else if (key >= XK_0 && key <= XK_9)
823 sprintf(name_buffer, "XK_%c", '0' + (char)(key - XK_0));
824 else if (key >= XK_KP_0 && key <= XK_KP_9)
825 sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - XK_KP_0));
826 else if (key >= XK_F1 && key <= XK_F24)
827 sprintf(name_buffer, "XK_F%d", (int)(key - XK_F1 + 1));
828 else if (key == KEY_UNDEFINDED)
829 strcpy(name_buffer, "[undefined]");
836 if (key == translate_key[i].keysym)
838 strcpy(name_buffer, translate_key[i].x11name);
842 while (translate_key[++i].x11name);
844 if (!translate_key[i].x11name)
845 sprintf(name_buffer, "0x%04lx", (unsigned long)key);
848 *x11name = name_buffer;
850 else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
852 KeySym key = XK_VoidSymbol;
853 char *name_ptr = *x11name;
855 if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4)
857 char c = name_ptr[3];
859 if (c >= 'A' && c <= 'Z')
860 key = XK_A + (KeySym)(c - 'A');
861 else if (c >= 'a' && c <= 'z')
862 key = XK_a + (KeySym)(c - 'a');
863 else if (c >= '0' && c <= '9')
864 key = XK_0 + (KeySym)(c - '0');
866 else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7)
868 char c = name_ptr[6];
870 if (c >= '0' && c <= '9')
871 key = XK_0 + (KeySym)(c - '0');
873 else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
875 char c1 = name_ptr[4];
876 char c2 = name_ptr[5];
879 if ((c1 >= '0' && c1 <= '9') &&
880 ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
881 d = atoi(&name_ptr[4]);
883 if (d >=1 && d <= 24)
884 key = XK_F1 + (KeySym)(d - 1);
886 else if (strncmp(name_ptr, "XK_", 3) == 0)
892 if (strcmp(name_ptr, translate_key[i].x11name) == 0)
894 key = translate_key[i].keysym;
898 while (translate_key[++i].x11name);
900 else if (strncmp(name_ptr, "0x", 2) == 0)
902 unsigned long value = 0;
908 char c = *name_ptr++;
911 if (c >= '0' && c <= '9')
913 else if (c >= 'a' && c <= 'f')
914 d = (int)(c - 'a' + 10);
915 else if (c >= 'A' && c <= 'F')
916 d = (int)(c - 'A' + 10);
924 value = value * 16 + d;
935 char *getKeyNameFromKeySym(KeySym keysym)
939 translate_keyname(&keysym, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME);
943 char *getX11KeyNameFromKeySym(KeySym keysym)
947 translate_keyname(&keysym, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME);
951 KeySym getKeySymFromX11KeyName(char *x11name)
955 translate_keyname(&keysym, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM);
959 char getCharFromKeySym(KeySym keysym)
961 char *keyname = getKeyNameFromKeySym(keysym);
964 if (strlen(keyname) == 1)
966 else if (strcmp(keyname, "space") == 0)
968 else if (strcmp(keyname, "circumflex") == 0)
974 #define TRANSLATE_JOYSYMBOL_TO_JOYNAME 0
975 #define TRANSLATE_JOYNAME_TO_JOYSYMBOL 1
977 void translate_joyname(int *joysymbol, char **name, int mode)
985 { JOY_LEFT, "joystick_left" },
986 { JOY_RIGHT, "joystick_right" },
987 { JOY_UP, "joystick_up" },
988 { JOY_DOWN, "joystick_down" },
989 { JOY_BUTTON_1, "joystick_button_1" },
990 { JOY_BUTTON_2, "joystick_button_2" },
995 if (mode == TRANSLATE_JOYSYMBOL_TO_JOYNAME)
997 *name = "[undefined]";
1001 if (*joysymbol == translate_joy[i].joysymbol)
1003 *name = translate_joy[i].name;
1008 else if (mode == TRANSLATE_JOYNAME_TO_JOYSYMBOL)
1014 if (strcmp(*name, translate_joy[i].name) == 0)
1016 *joysymbol = translate_joy[i].joysymbol;
1023 char *getJoyNameFromJoySymbol(int joysymbol)
1027 translate_joyname(&joysymbol, &name, TRANSLATE_JOYSYMBOL_TO_JOYNAME);
1031 int getJoySymbolFromJoyName(char *name)
1035 translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL);
1039 int getJoystickNrFromDeviceName(char *device_name)
1042 int joystick_nr = 0;
1044 if (device_name == NULL || device_name[0] == '\0')
1047 c = device_name[strlen(device_name) - 1];
1049 if (c >= '0' && c <= '9')
1050 joystick_nr = (int)(c - '0');
1052 if (joystick_nr < 0 || joystick_nr >= MAX_PLAYERS)
1058 /* ----------------------------------------------------------------- */
1059 /* the following is only for debugging purpose and normally not used */
1060 /* ----------------------------------------------------------------- */
1062 #define DEBUG_NUM_TIMESTAMPS 3
1064 void debug_print_timestamp(int counter_nr, char *message)
1066 static long counter[DEBUG_NUM_TIMESTAMPS][2];
1068 if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
1069 Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
1071 counter[counter_nr][0] = Counter();
1074 printf("%s %.2f seconds\n", message,
1075 (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
1077 counter[counter_nr][1] = Counter();