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