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