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