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