rnd-19981123-2
[rocksndiamonds.git] / src / misc.c
1 /***********************************************************
2 *  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
3 *----------------------------------------------------------*
4 *  (c) 1995-98 Artsoft Entertainment                       *
5 *              Holger Schemel                              *
6 *              Oststrasse 11a                              *
7 *              33604 Bielefeld                             *
8 *              phone: ++49 +521 290471                     *
9 *              email: aeglos@valinor.owl.de                *
10 *----------------------------------------------------------*
11 *  misc.c                                                  *
12 ***********************************************************/
13
14 #include <pwd.h>
15 #include <unistd.h>
16 #include <time.h>
17 #include <sys/time.h>
18 #include <sys/param.h>
19 #include <sys/types.h>
20 #include <stdarg.h>
21 #include <ctype.h>
22
23 #include "misc.h"
24 #include "init.h"
25 #include "tools.h"
26 #include "sound.h"
27 #include "random.h"
28 #include "joystick.h"
29
30 /* maximal allowed length of a command line option */
31 #define MAX_OPTION_LEN          256
32
33 static unsigned long mainCounter(int mode)
34 {
35   static struct timeval base_time = { 0, 0 };
36   struct timeval current_time;
37   unsigned long counter_ms;
38
39   gettimeofday(&current_time, NULL);
40
41   if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec)
42     base_time = current_time;
43
44   counter_ms = (current_time.tv_sec  - base_time.tv_sec)  * 1000
45              + (current_time.tv_usec - base_time.tv_usec) / 1000;
46
47   return counter_ms;            /* return milliseconds since last init */
48 }
49
50 void InitCounter()              /* set counter back to zero */
51 {
52   mainCounter(INIT_COUNTER);
53 }
54
55 unsigned long Counter() /* get milliseconds since last call of InitCounter() */
56 {
57   return(mainCounter(READ_COUNTER));
58 }
59
60 static void sleep_milliseconds(unsigned long milliseconds_delay)
61 {
62   if (milliseconds_delay < 5)
63   {
64     /* we want to wait only a few ms -- if we assume that we have a
65        kernel timer resolution of 10 ms, we would wait far to long;
66        therefore it's better to do a short interval of busy waiting
67        to get our sleeping time more accurate */
68
69     unsigned long base_counter = Counter(), actual_counter = Counter();
70
71     while (actual_counter < base_counter + milliseconds_delay &&
72            actual_counter >= base_counter)
73       actual_counter = Counter();
74   }
75   else
76   {
77     struct timeval delay;
78
79     delay.tv_sec  = milliseconds_delay / 1000;
80     delay.tv_usec = 1000 * (milliseconds_delay % 1000);
81
82     if (select(0, NULL, NULL, NULL, &delay) != 0)
83       Error(ERR_WARN, "sleep_milliseconds(): select() failed");
84   }
85 }
86
87 void Delay(unsigned long delay) /* Sleep specified number of milliseconds */
88 {
89   sleep_milliseconds(delay);
90 }
91
92 boolean FrameReached(unsigned long *frame_counter_var,
93                      unsigned long frame_delay)
94 {
95   unsigned long actual_frame_counter = FrameCounter;
96
97   if (actual_frame_counter < *frame_counter_var+frame_delay &&
98       actual_frame_counter >= *frame_counter_var)
99     return(FALSE);
100
101   *frame_counter_var = actual_frame_counter;
102   return(TRUE);
103 }
104
105 boolean DelayReached(unsigned long *counter_var,
106                      unsigned long delay)
107 {
108   unsigned long actual_counter = Counter();
109
110   if (actual_counter < *counter_var + delay &&
111       actual_counter >= *counter_var)
112     return(FALSE);
113
114   *counter_var = actual_counter;
115   return(TRUE);
116 }
117
118 void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
119 {
120   unsigned long actual_counter;
121
122   while(1)
123   {
124     actual_counter = Counter();
125
126     if (actual_counter < *counter_var + delay &&
127         actual_counter >= *counter_var)
128       sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
129     else
130       break;
131   }
132
133   *counter_var = actual_counter;
134 }
135
136 char *int2str(int number, int size)
137 {
138   static char s[40];
139
140   if (size > 20)
141     size = 20;
142
143   if (size)
144   {
145     sprintf(s, "                    %09d", number);
146     return &s[strlen(s) - size];
147   }
148   else
149   {
150     sprintf(s, "%d", number);
151     return s;
152   }
153 }
154
155 unsigned int SimpleRND(unsigned int max)
156 {
157   static unsigned long root = 654321;
158   struct timeval current_time;
159
160   gettimeofday(&current_time,NULL);
161   root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
162   return (root % max);
163 }
164
165 #ifdef DEBUG
166 static unsigned int last_RND_value = 0;
167
168 unsigned int last_RND()
169 {
170   return last_RND_value;
171 }
172 #endif
173
174 unsigned int RND(unsigned int max)
175 {
176 #ifdef DEBUG
177   return (last_RND_value = random_linux_libc() % max);
178 #else
179   return (random_linux_libc() % max);
180 #endif
181 }
182
183 unsigned int InitRND(long seed)
184 {
185   struct timeval current_time;
186
187   if (seed == NEW_RANDOMIZE)
188   {
189     gettimeofday(&current_time,NULL);
190     srandom_linux_libc((unsigned int) current_time.tv_usec);
191     return (unsigned int)current_time.tv_usec;
192   }
193   else
194   {
195     srandom_linux_libc((unsigned int) seed);
196     return (unsigned int)seed;
197   }
198 }
199
200 char *getLoginName()
201 {
202   struct passwd *pwd;
203
204   if (!(pwd = getpwuid(getuid())))
205     return "ANONYMOUS";
206   else
207     return pwd->pw_name;
208 }
209
210 char *getHomeDir()
211 {
212   static char *home_dir = NULL;
213
214   if (!home_dir)
215   {
216     if (!(home_dir = getenv("HOME")))
217     {
218       struct passwd *pwd;
219
220       if ((pwd = getpwuid(getuid())))
221         home_dir = pwd->pw_dir;
222       else
223         home_dir = ".";
224     }
225   }
226
227   return home_dir;
228 }
229
230 char *getPath2(char *path1, char *path2)
231 {
232   char *complete_path = checked_malloc(strlen(path1) + strlen(path2) + 2);
233
234   sprintf(complete_path, "%s/%s", path1, path2);
235   return complete_path;
236 }
237
238 char *getStringCopy(char *s)
239 {
240   char *s_copy = checked_malloc(strlen(s) + 1);
241
242   strcpy(s_copy, s);
243   return s_copy;
244 }
245
246 char *getStringToLower(char *s)
247 {
248   char *s_copy = checked_malloc(strlen(s) + 1);
249   char *s_ptr = s_copy;
250
251   while (*s)
252     *s_ptr++ = tolower(*s++);
253
254   return s_copy;
255 }
256
257 void MarkTileDirty(int x, int y)
258 {
259   int xx = redraw_x1 + x;
260   int yy = redraw_y1 + y;
261
262   if (!redraw[xx][yy])
263     redraw_tiles++;
264
265   redraw[xx][yy] = TRUE;
266   redraw_mask |= REDRAW_TILES;
267 }
268
269 void GetOptions(char *argv[])
270 {
271   char **options_left = &argv[1];
272
273   /* initialize global program options */
274   options.display_name = NULL;
275   options.server_host = NULL;
276   options.server_port = 0;
277   options.base_directory = BASE_PATH;
278   options.level_directory = BASE_PATH "/" LEVELS_DIRECTORY;
279   options.serveronly = FALSE;
280   options.network = FALSE;
281   options.verbose = FALSE;
282
283   while (*options_left)
284   {
285     char option_str[MAX_OPTION_LEN];
286     char *option = options_left[0];
287     char *next_option = options_left[1];
288     char *option_arg = NULL;
289     int option_len = strlen(option);
290
291     strcpy(option_str, option);                 /* copy argument into buffer */
292     option = option_str;
293
294     if (strcmp(option, "--") == 0)              /* stop scanning arguments */
295       break;
296
297     if (option_len >= MAX_OPTION_LEN)
298       Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
299
300     if (strncmp(option, "--", 2) == 0)          /* treat '--' like '-' */
301       option++;
302
303     option_arg = strchr(option, '=');
304     if (option_arg == NULL)                     /* no '=' in option */
305       option_arg = next_option;
306     else
307     {
308       *option_arg++ = '\0';                     /* cut argument from option */
309       if (*option_arg == '\0')                  /* no argument after '=' */
310         Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
311     }
312
313     option_len = strlen(option);
314
315     if (strcmp(option, "-") == 0)
316       Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
317     else if (strncmp(option, "-help", option_len) == 0)
318     {
319       printf("Usage: %s [options] [server.name [port]]\n"
320              "Options:\n"
321              "  -d, --display machine:0       X server display\n"
322              "  -b, --basepath directory      alternative base directory\n"
323              "  -l, --levels directory        alternative level directory\n"
324              "  -s, --serveronly              only start network server\n"
325              "  -n, --network                 network multiplayer game\n"
326              "  -v, --verbose                 verbose mode\n",
327              program_name);
328       exit(0);
329     }
330     else if (strncmp(option, "-display", option_len) == 0)
331     {
332       if (option_arg == NULL)
333         Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
334
335       options.display_name = option_arg;
336       if (option_arg == next_option)
337         options_left++;
338
339       printf("--display == '%s'\n", options.display_name);
340     }
341     else if (strncmp(option, "-basepath", option_len) == 0)
342     {
343       if (option_arg == NULL)
344         Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
345
346       options.base_directory = option_arg;
347       if (option_arg == next_option)
348         options_left++;
349
350       printf("--basepath == '%s'\n", options.base_directory);
351
352       /* adjust path for level directory accordingly */
353       options.level_directory = checked_malloc(strlen(options.base_directory) +
354                                                strlen(LEVELS_DIRECTORY) + 2);
355       sprintf(options.level_directory, "%s/%s",
356               options.base_directory, LEVELS_DIRECTORY);
357     }
358     else if (strncmp(option, "-levels", option_len) == 0)
359     {
360       if (option_arg == NULL)
361         Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
362
363       options.level_directory = option_arg;
364       if (option_arg == next_option)
365         options_left++;
366
367       printf("--levels == '%s'\n", options.level_directory);
368     }
369     else if (strncmp(option, "-network", option_len) == 0)
370     {
371       printf("--network\n");
372
373       options.network = TRUE;
374     }
375     else if (strncmp(option, "-serveronly", option_len) == 0)
376     {
377       printf("--serveronly\n");
378
379       options.serveronly = TRUE;
380     }
381     else if (strncmp(option, "-verbose", option_len) == 0)
382     {
383       printf("--verbose\n");
384
385       options.verbose = TRUE;
386     }
387     else if (*option == '-')
388       Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
389     else if (options.server_host == NULL)
390     {
391       options.server_host = *options_left;
392
393       printf("server.name == '%s'\n", options.server_host);
394     }
395     else if (options.server_port == 0)
396     {
397       options.server_port = atoi(*options_left);
398       if (options.server_port < 1024)
399         Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
400
401       printf("port == %d\n", options.server_port);
402     }
403     else
404       Error(ERR_EXIT_HELP, "too many arguments");
405
406     options_left++;
407   }
408 }
409
410 void Error(int mode, char *format, ...)
411 {
412   FILE *output_stream = stderr;
413   char *process_name = "";
414
415   if (mode & ERR_SOUNDSERVER)
416     process_name = " sound server";
417
418   if (format)
419   {
420     va_list ap;
421
422     fprintf(output_stream, "%s%s: ", program_name, process_name);
423
424     if (mode & ERR_WARN)
425       fprintf(output_stream, "warning: ");
426
427     va_start(ap, format);
428     vfprintf(output_stream, format, ap);
429     va_end(ap);
430   
431     fprintf(output_stream, "\n");
432   }
433   
434   if (mode & ERR_HELP)
435     fprintf(output_stream, "%s: Try option '--help' for more information.\n",
436             program_name);
437
438   if (mode & ERR_EXIT)
439   {
440     fprintf(output_stream, "%s%s: aborting\n", program_name, process_name);
441     CloseAllAndExit(1);
442   }
443 }
444
445 void *checked_malloc(unsigned long size)
446 {
447   void *ptr;
448
449   ptr = malloc(size);
450
451   if (ptr == NULL)
452     Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
453
454   return ptr;
455 }
456
457 void *checked_calloc(unsigned long size)
458 {
459   void *ptr;
460
461   ptr = calloc(1, size);
462
463   if (ptr == NULL)
464     Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
465
466   return ptr;
467 }
468
469 #define TRANSLATE_KEYSYM_TO_KEYNAME     0
470 #define TRANSLATE_KEYSYM_TO_X11KEYNAME  1
471 #define TRANSLATE_X11KEYNAME_TO_KEYSYM  2
472
473 void translate_keyname(KeySym *keysym, char **x11name, char **name, int mode)
474 {
475   static struct
476   {
477     KeySym keysym;
478     char *x11name;
479     char *name;
480   } translate_key[] =
481   {
482     /* normal cursor keys */
483     { XK_Left,          "XK_Left",              "cursor left" },
484     { XK_Right,         "XK_Right",             "cursor right" },
485     { XK_Up,            "XK_Up",                "cursor up" },
486     { XK_Down,          "XK_Down",              "cursor down" },
487
488     /* keypad cursor keys */
489 #ifdef XK_KP_Left
490     { XK_KP_Left,       "XK_KP_Left",           "keypad left" },
491     { XK_KP_Right,      "XK_KP_Right",          "keypad right" },
492     { XK_KP_Up,         "XK_KP_Up",             "keypad up" },
493     { XK_KP_Down,       "XK_KP_Down",           "keypad down" },
494 #endif
495
496     /* other keypad keys */
497 #ifdef XK_KP_Enter
498     { XK_KP_Enter,      "XK_KP_Enter",          "keypad enter" },
499     { XK_KP_Add,        "XK_KP_Add",            "keypad +" },
500     { XK_KP_Subtract,   "XK_KP_Subtract",       "keypad -" },
501     { XK_KP_Multiply,   "XK_KP_Multiply",       "keypad mltply" },
502     { XK_KP_Divide,     "XK_KP_Divide",         "keypad /" },
503     { XK_KP_Separator,  "XK_KP_Separator",      "keypad ," },
504 #endif
505
506     /* modifier keys */
507     { XK_Shift_L,       "XK_Shift_L",           "left shift" },
508     { XK_Shift_R,       "XK_Shift_R",           "right shift" },
509     { XK_Control_L,     "XK_Control_L",         "left control" },
510     { XK_Control_R,     "XK_Control_R",         "right control" },
511     { XK_Meta_L,        "XK_Meta_L",            "left meta" },
512     { XK_Meta_R,        "XK_Meta_R",            "right meta" },
513     { XK_Alt_L,         "XK_Alt_L",             "left alt" },
514     { XK_Alt_R,         "XK_Alt_R",             "right alt" },
515     { XK_Mode_switch,   "XK_Mode_switch",       "mode switch" },
516     { XK_Multi_key,     "XK_Multi_key",         "multi key" },
517
518     /* some special keys */
519     { XK_BackSpace,     "XK_BackSpace",         "backspace" },
520     { XK_Delete,        "XK_Delete",            "delete" },
521     { XK_Insert,        "XK_Insert",            "insert" },
522     { XK_Tab,           "XK_Tab",               "tab" },
523     { XK_Home,          "XK_Home",              "home" },
524     { XK_End,           "XK_End",               "end" },
525     { XK_Page_Up,       "XK_Page_Up",           "page up" },
526     { XK_Page_Down,     "XK_Page_Down",         "page down" },
527     { XK_space,         "XK_space",             "space" },
528
529     /* even more special keys */
530     { XK_adiaeresis,    "XK_adiaeresis",        "ä" },
531     { XK_odiaeresis,    "XK_odiaeresis",        "ö" },
532     { XK_udiaeresis,    "XK_udiaeresis",        "ü" },
533     { XK_apostrophe,    "XK_apostrophe",        "'" },
534     { XK_plus,          "XK_plus",              "+" },
535     { XK_minus,         "XK_minus",             "-" },
536     { XK_comma,         "XK_comma",             "," },
537     { XK_period,        "XK_period",            "." },
538     { XK_numbersign,    "XK_numbersign",        "#" },
539     { XK_less,          "XK_less",              "less" },
540     { XK_greater,       "XK_greater",           "greater" },
541     { XK_asciicircum,   "XK_asciicircum",       "circumflex" },
542     { XK_ssharp,        "XK_ssharp",            "sharp s" },
543
544     /* end-of-array identifier */
545     { 0,                NULL,                   NULL }
546   };
547
548   int i;
549
550   if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
551   {
552     static char name_buffer[30];
553     KeySym key = *keysym;
554
555     if (key >= XK_A && key <= XK_Z)
556       sprintf(name_buffer, "%c", 'A' + (char)(key - XK_A));
557     else if (key >= XK_a && key <= XK_z)
558       sprintf(name_buffer, "%c", 'a' + (char)(key - XK_a));
559     else if (key >= XK_0 && key <= XK_9)
560       sprintf(name_buffer, "%c", '0' + (char)(key - XK_0));
561     else if (key >= XK_KP_0 && key <= XK_KP_9)
562       sprintf(name_buffer, "keypad %c", '0' + (char)(key - XK_KP_0));
563     else if (key >= XK_F1 && key <= XK_F24)
564       sprintf(name_buffer, "function F%d", (int)(key - XK_F1 + 1));
565     else if (key == KEY_UNDEFINDED)
566       strcpy(name_buffer, "(undefined)");
567     else
568     {
569       i = 0;
570
571       do
572       {
573         if (key == translate_key[i].keysym)
574         {
575           strcpy(name_buffer, translate_key[i].name);
576           break;
577         }
578       }
579       while (translate_key[++i].name);
580
581       if (!translate_key[i].name)
582         strcpy(name_buffer, "(unknown)");
583     }
584
585     *name = name_buffer;
586   }
587   else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME)
588   {
589     static char name_buffer[30];
590     KeySym key = *keysym;
591
592     if (key >= XK_A && key <= XK_Z)
593       sprintf(name_buffer, "XK_%c", 'A' + (char)(key - XK_A));
594     else if (key >= XK_a && key <= XK_z)
595       sprintf(name_buffer, "XK_%c", 'a' + (char)(key - XK_a));
596     else if (key >= XK_0 && key <= XK_9)
597       sprintf(name_buffer, "XK_%c", '0' + (char)(key - XK_0));
598     else if (key >= XK_KP_0 && key <= XK_KP_9)
599       sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - XK_KP_0));
600     else if (key >= XK_F1 && key <= XK_F24)
601       sprintf(name_buffer, "XK_F%d", (int)(key - XK_F1 + 1));
602     else if (key == KEY_UNDEFINDED)
603       strcpy(name_buffer, "[undefined]");
604     else
605     {
606       i = 0;
607
608       do
609       {
610         if (key == translate_key[i].keysym)
611         {
612           strcpy(name_buffer, translate_key[i].x11name);
613           break;
614         }
615       }
616       while (translate_key[++i].x11name);
617
618       if (!translate_key[i].x11name)
619         sprintf(name_buffer, "0x%04lx", (unsigned long)key);
620     }
621
622     *x11name = name_buffer;
623   }
624   else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
625   {
626     KeySym key = XK_VoidSymbol;
627     char *name_ptr = *x11name;
628
629     if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4)
630     {
631       char c = name_ptr[3];
632
633       if (c >= 'A' && c <= 'Z')
634         key = XK_A + (KeySym)(c - 'A');
635       else if (c >= 'a' && c <= 'z')
636         key = XK_a + (KeySym)(c - 'a');
637       else if (c >= '0' && c <= '9')
638         key = XK_0 + (KeySym)(c - '0');
639     }
640     else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7)
641     {
642       char c = name_ptr[6];
643
644       if (c >= '0' && c <= '9')
645         key = XK_0 + (KeySym)(c - '0');
646     }
647     else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
648     {
649       char c1 = name_ptr[4];
650       char c2 = name_ptr[5];
651       int d = 0;
652
653       if ((c1 >= '0' && c1 <= '9') &&
654           ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
655         d = atoi(&name_ptr[4]);
656
657       if (d >=1 && d <= 24)
658         key = XK_F1 + (KeySym)(d - 1);
659     }
660     else if (strncmp(name_ptr, "XK_", 3) == 0)
661     {
662       i = 0;
663
664       do
665       {
666         if (strcmp(name_ptr, translate_key[i].x11name) == 0)
667         {
668           key = translate_key[i].keysym;
669           break;
670         }
671       }
672       while (translate_key[++i].x11name);
673     }
674     else if (strncmp(name_ptr, "0x", 2) == 0)
675     {
676       unsigned long value = 0;
677
678       name_ptr += 2;
679
680       while (name_ptr)
681       {
682         char c = *name_ptr++;
683         int d = -1;
684
685         if (c >= '0' && c <= '9')
686           d = (int)(c - '0');
687         else if (c >= 'a' && c <= 'f')
688           d = (int)(c - 'a' + 10);
689         else if (c >= 'A' && c <= 'F')
690           d = (int)(c - 'A' + 10);
691
692         if (d == -1)
693         {
694           value = -1;
695           break;
696         }
697
698         value = value * 16 + d;
699       }
700
701       if (value != -1)
702         key = (KeySym)value;
703     }
704
705     *keysym = key;
706   }
707 }
708
709 char *getKeyNameFromKeySym(KeySym keysym)
710 {
711   char *name;
712
713   translate_keyname(&keysym, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME);
714   return name;
715 }
716
717 char *getX11KeyNameFromKeySym(KeySym keysym)
718 {
719   char *x11name;
720
721   translate_keyname(&keysym, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME);
722   return x11name;
723 }
724
725 KeySym getKeySymFromX11KeyName(char *x11name)
726 {
727   KeySym keysym;
728
729   translate_keyname(&keysym, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM);
730   return keysym;
731 }
732
733 #define TRANSLATE_JOYSYMBOL_TO_JOYNAME  0
734 #define TRANSLATE_JOYNAME_TO_JOYSYMBOL  1
735
736 void translate_joyname(int *joysymbol, char **name, int mode)
737 {
738   static struct
739   {
740     int joysymbol;
741     char *name;
742   } translate_joy[] =
743   {
744     { JOY_LEFT,         "joystick_left" },
745     { JOY_RIGHT,        "joystick_right" },
746     { JOY_UP,           "joystick_up" },
747     { JOY_DOWN,         "joystick_down" },
748     { JOY_BUTTON_1,     "joystick_button_1" },
749     { JOY_BUTTON_2,     "joystick_button_2" },
750   };
751
752   int i;
753
754   if (mode == TRANSLATE_JOYSYMBOL_TO_JOYNAME)
755   {
756     *name = "[undefined]";
757
758     for (i=0; i<6; i++)
759     {
760       if (*joysymbol == translate_joy[i].joysymbol)
761       {
762         *name = translate_joy[i].name;
763         break;
764       }
765     }
766   }
767   else if (mode == TRANSLATE_JOYNAME_TO_JOYSYMBOL)
768   {
769     *joysymbol = 0;
770
771     for (i=0; i<6; i++)
772     {
773       if (strcmp(*name, translate_joy[i].name) == 0)
774       {
775         *joysymbol = translate_joy[i].joysymbol;
776         break;
777       }
778     }
779   }
780 }
781
782 char *getJoyNameFromJoySymbol(int joysymbol)
783 {
784   char *name;
785
786   translate_joyname(&joysymbol, &name, TRANSLATE_JOYSYMBOL_TO_JOYNAME);
787   return name;
788 }
789
790 int getJoySymbolFromJoyName(char *name)
791 {
792   int joysymbol;
793
794   translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL);
795   return joysymbol;
796 }
797
798 /* the following is only for debugging purpose and normally not used */
799 #define DEBUG_NUM_TIMESTAMPS    3
800
801 void debug_print_timestamp(int counter_nr, char *message)
802 {
803   static long counter[DEBUG_NUM_TIMESTAMPS][2];
804
805   if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
806     Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
807
808   counter[counter_nr][0] = Counter();
809
810   if (message)
811     printf("%s %.2f seconds\n", message,
812            (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
813
814   counter[counter_nr][1] = Counter();
815 }