5da20e36bdd3cba70de1d1ef759c5cf3f9bfaae8
[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 =
365         getPath2(options.base_directory, LEVELS_DIRECTORY);
366     }
367     else if (strncmp(option, "-levels", option_len) == 0)
368     {
369       if (option_arg == NULL)
370         Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
371
372       options.level_directory = option_arg;
373       if (option_arg == next_option)
374         options_left++;
375
376       printf("--levels == '%s'\n", options.level_directory);
377     }
378     else if (strncmp(option, "-network", option_len) == 0)
379     {
380       printf("--network\n");
381
382       options.network = TRUE;
383     }
384     else if (strncmp(option, "-serveronly", option_len) == 0)
385     {
386       printf("--serveronly\n");
387
388       options.serveronly = TRUE;
389     }
390     else if (strncmp(option, "-verbose", option_len) == 0)
391     {
392       printf("--verbose\n");
393
394       options.verbose = TRUE;
395     }
396     else if (*option == '-')
397       Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
398     else if (options.server_host == NULL)
399     {
400       options.server_host = *options_left;
401
402       printf("server.name == '%s'\n", options.server_host);
403     }
404     else if (options.server_port == 0)
405     {
406       options.server_port = atoi(*options_left);
407       if (options.server_port < 1024)
408         Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
409
410       printf("port == %d\n", options.server_port);
411     }
412     else
413       Error(ERR_EXIT_HELP, "too many arguments");
414
415     options_left++;
416   }
417 }
418
419 void Error(int mode, char *format, ...)
420 {
421   FILE *output_stream = stderr;
422   char *process_name = "";
423
424   if (mode & ERR_SOUND_SERVER)
425     process_name = " sound server";
426   else if (mode & ERR_NETWORK_SERVER)
427     process_name = " network server";
428   else if (mode & ERR_NETWORK_CLIENT)
429     process_name = " network client **";
430
431   if (format)
432   {
433     va_list ap;
434
435     fprintf(output_stream, "%s%s: ", program_name, process_name);
436
437     if (mode & ERR_WARN)
438       fprintf(output_stream, "warning: ");
439
440     va_start(ap, format);
441     vfprintf(output_stream, format, ap);
442     va_end(ap);
443   
444     fprintf(output_stream, "\n");
445   }
446   
447   if (mode & ERR_HELP)
448     fprintf(output_stream, "%s: Try option '--help' for more information.\n",
449             program_name);
450
451   if (mode & ERR_EXIT)
452   {
453     fprintf(output_stream, "%s%s: aborting\n", program_name, process_name);
454     CloseAllAndExit(1);
455   }
456 }
457
458 void *checked_malloc(unsigned long size)
459 {
460   void *ptr;
461
462   ptr = malloc(size);
463
464   if (ptr == NULL)
465     Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
466
467   return ptr;
468 }
469
470 void *checked_calloc(unsigned long size)
471 {
472   void *ptr;
473
474   ptr = calloc(1, size);
475
476   if (ptr == NULL)
477     Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
478
479   return ptr;
480 }
481
482 #define TRANSLATE_KEYSYM_TO_KEYNAME     0
483 #define TRANSLATE_KEYSYM_TO_X11KEYNAME  1
484 #define TRANSLATE_X11KEYNAME_TO_KEYSYM  2
485
486 void translate_keyname(KeySym *keysym, char **x11name, char **name, int mode)
487 {
488   static struct
489   {
490     KeySym keysym;
491     char *x11name;
492     char *name;
493   } translate_key[] =
494   {
495     /* normal cursor keys */
496     { XK_Left,          "XK_Left",              "cursor left" },
497     { XK_Right,         "XK_Right",             "cursor right" },
498     { XK_Up,            "XK_Up",                "cursor up" },
499     { XK_Down,          "XK_Down",              "cursor down" },
500
501     /* keypad cursor keys */
502 #ifdef XK_KP_Left
503     { XK_KP_Left,       "XK_KP_Left",           "keypad left" },
504     { XK_KP_Right,      "XK_KP_Right",          "keypad right" },
505     { XK_KP_Up,         "XK_KP_Up",             "keypad up" },
506     { XK_KP_Down,       "XK_KP_Down",           "keypad down" },
507 #endif
508
509     /* other keypad keys */
510 #ifdef XK_KP_Enter
511     { XK_KP_Enter,      "XK_KP_Enter",          "keypad enter" },
512     { XK_KP_Add,        "XK_KP_Add",            "keypad +" },
513     { XK_KP_Subtract,   "XK_KP_Subtract",       "keypad -" },
514     { XK_KP_Multiply,   "XK_KP_Multiply",       "keypad mltply" },
515     { XK_KP_Divide,     "XK_KP_Divide",         "keypad /" },
516     { XK_KP_Separator,  "XK_KP_Separator",      "keypad ," },
517 #endif
518
519     /* modifier keys */
520     { XK_Shift_L,       "XK_Shift_L",           "left shift" },
521     { XK_Shift_R,       "XK_Shift_R",           "right shift" },
522     { XK_Control_L,     "XK_Control_L",         "left control" },
523     { XK_Control_R,     "XK_Control_R",         "right control" },
524     { XK_Meta_L,        "XK_Meta_L",            "left meta" },
525     { XK_Meta_R,        "XK_Meta_R",            "right meta" },
526     { XK_Alt_L,         "XK_Alt_L",             "left alt" },
527     { XK_Alt_R,         "XK_Alt_R",             "right alt" },
528     { XK_Mode_switch,   "XK_Mode_switch",       "mode switch" },
529     { XK_Multi_key,     "XK_Multi_key",         "multi key" },
530
531     /* some special keys */
532     { XK_BackSpace,     "XK_BackSpace",         "backspace" },
533     { XK_Delete,        "XK_Delete",            "delete" },
534     { XK_Insert,        "XK_Insert",            "insert" },
535     { XK_Tab,           "XK_Tab",               "tab" },
536     { XK_Home,          "XK_Home",              "home" },
537     { XK_End,           "XK_End",               "end" },
538     { XK_Page_Up,       "XK_Page_Up",           "page up" },
539     { XK_Page_Down,     "XK_Page_Down",         "page down" },
540     { XK_space,         "XK_space",             "space" },
541
542     /* even more special keys */
543     { XK_adiaeresis,    "XK_adiaeresis",        "ä" },
544     { XK_odiaeresis,    "XK_odiaeresis",        "ö" },
545     { XK_udiaeresis,    "XK_udiaeresis",        "ü" },
546     { XK_apostrophe,    "XK_apostrophe",        "'" },
547     { XK_plus,          "XK_plus",              "+" },
548     { XK_minus,         "XK_minus",             "-" },
549     { XK_comma,         "XK_comma",             "," },
550     { XK_period,        "XK_period",            "." },
551     { XK_numbersign,    "XK_numbersign",        "#" },
552     { XK_less,          "XK_less",              "less" },
553     { XK_greater,       "XK_greater",           "greater" },
554     { XK_asciicircum,   "XK_asciicircum",       "circumflex" },
555     { XK_ssharp,        "XK_ssharp",            "sharp s" },
556
557     /* end-of-array identifier */
558     { 0,                NULL,                   NULL }
559   };
560
561   int i;
562
563   if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
564   {
565     static char name_buffer[30];
566     KeySym key = *keysym;
567
568     if (key >= XK_A && key <= XK_Z)
569       sprintf(name_buffer, "%c", 'A' + (char)(key - XK_A));
570     else if (key >= XK_a && key <= XK_z)
571       sprintf(name_buffer, "%c", 'a' + (char)(key - XK_a));
572     else if (key >= XK_0 && key <= XK_9)
573       sprintf(name_buffer, "%c", '0' + (char)(key - XK_0));
574     else if (key >= XK_KP_0 && key <= XK_KP_9)
575       sprintf(name_buffer, "keypad %c", '0' + (char)(key - XK_KP_0));
576     else if (key >= XK_F1 && key <= XK_F24)
577       sprintf(name_buffer, "function F%d", (int)(key - XK_F1 + 1));
578     else if (key == KEY_UNDEFINDED)
579       strcpy(name_buffer, "(undefined)");
580     else
581     {
582       i = 0;
583
584       do
585       {
586         if (key == translate_key[i].keysym)
587         {
588           strcpy(name_buffer, translate_key[i].name);
589           break;
590         }
591       }
592       while (translate_key[++i].name);
593
594       if (!translate_key[i].name)
595         strcpy(name_buffer, "(unknown)");
596     }
597
598     *name = name_buffer;
599   }
600   else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME)
601   {
602     static char name_buffer[30];
603     KeySym key = *keysym;
604
605     if (key >= XK_A && key <= XK_Z)
606       sprintf(name_buffer, "XK_%c", 'A' + (char)(key - XK_A));
607     else if (key >= XK_a && key <= XK_z)
608       sprintf(name_buffer, "XK_%c", 'a' + (char)(key - XK_a));
609     else if (key >= XK_0 && key <= XK_9)
610       sprintf(name_buffer, "XK_%c", '0' + (char)(key - XK_0));
611     else if (key >= XK_KP_0 && key <= XK_KP_9)
612       sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - XK_KP_0));
613     else if (key >= XK_F1 && key <= XK_F24)
614       sprintf(name_buffer, "XK_F%d", (int)(key - XK_F1 + 1));
615     else if (key == KEY_UNDEFINDED)
616       strcpy(name_buffer, "[undefined]");
617     else
618     {
619       i = 0;
620
621       do
622       {
623         if (key == translate_key[i].keysym)
624         {
625           strcpy(name_buffer, translate_key[i].x11name);
626           break;
627         }
628       }
629       while (translate_key[++i].x11name);
630
631       if (!translate_key[i].x11name)
632         sprintf(name_buffer, "0x%04lx", (unsigned long)key);
633     }
634
635     *x11name = name_buffer;
636   }
637   else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
638   {
639     KeySym key = XK_VoidSymbol;
640     char *name_ptr = *x11name;
641
642     if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4)
643     {
644       char c = name_ptr[3];
645
646       if (c >= 'A' && c <= 'Z')
647         key = XK_A + (KeySym)(c - 'A');
648       else if (c >= 'a' && c <= 'z')
649         key = XK_a + (KeySym)(c - 'a');
650       else if (c >= '0' && c <= '9')
651         key = XK_0 + (KeySym)(c - '0');
652     }
653     else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7)
654     {
655       char c = name_ptr[6];
656
657       if (c >= '0' && c <= '9')
658         key = XK_0 + (KeySym)(c - '0');
659     }
660     else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
661     {
662       char c1 = name_ptr[4];
663       char c2 = name_ptr[5];
664       int d = 0;
665
666       if ((c1 >= '0' && c1 <= '9') &&
667           ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
668         d = atoi(&name_ptr[4]);
669
670       if (d >=1 && d <= 24)
671         key = XK_F1 + (KeySym)(d - 1);
672     }
673     else if (strncmp(name_ptr, "XK_", 3) == 0)
674     {
675       i = 0;
676
677       do
678       {
679         if (strcmp(name_ptr, translate_key[i].x11name) == 0)
680         {
681           key = translate_key[i].keysym;
682           break;
683         }
684       }
685       while (translate_key[++i].x11name);
686     }
687     else if (strncmp(name_ptr, "0x", 2) == 0)
688     {
689       unsigned long value = 0;
690
691       name_ptr += 2;
692
693       while (name_ptr)
694       {
695         char c = *name_ptr++;
696         int d = -1;
697
698         if (c >= '0' && c <= '9')
699           d = (int)(c - '0');
700         else if (c >= 'a' && c <= 'f')
701           d = (int)(c - 'a' + 10);
702         else if (c >= 'A' && c <= 'F')
703           d = (int)(c - 'A' + 10);
704
705         if (d == -1)
706         {
707           value = -1;
708           break;
709         }
710
711         value = value * 16 + d;
712       }
713
714       if (value != -1)
715         key = (KeySym)value;
716     }
717
718     *keysym = key;
719   }
720 }
721
722 char *getKeyNameFromKeySym(KeySym keysym)
723 {
724   char *name;
725
726   translate_keyname(&keysym, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME);
727   return name;
728 }
729
730 char *getX11KeyNameFromKeySym(KeySym keysym)
731 {
732   char *x11name;
733
734   translate_keyname(&keysym, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME);
735   return x11name;
736 }
737
738 KeySym getKeySymFromX11KeyName(char *x11name)
739 {
740   KeySym keysym;
741
742   translate_keyname(&keysym, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM);
743   return keysym;
744 }
745
746 #define TRANSLATE_JOYSYMBOL_TO_JOYNAME  0
747 #define TRANSLATE_JOYNAME_TO_JOYSYMBOL  1
748
749 void translate_joyname(int *joysymbol, char **name, int mode)
750 {
751   static struct
752   {
753     int joysymbol;
754     char *name;
755   } translate_joy[] =
756   {
757     { JOY_LEFT,         "joystick_left" },
758     { JOY_RIGHT,        "joystick_right" },
759     { JOY_UP,           "joystick_up" },
760     { JOY_DOWN,         "joystick_down" },
761     { JOY_BUTTON_1,     "joystick_button_1" },
762     { JOY_BUTTON_2,     "joystick_button_2" },
763   };
764
765   int i;
766
767   if (mode == TRANSLATE_JOYSYMBOL_TO_JOYNAME)
768   {
769     *name = "[undefined]";
770
771     for (i=0; i<6; i++)
772     {
773       if (*joysymbol == translate_joy[i].joysymbol)
774       {
775         *name = translate_joy[i].name;
776         break;
777       }
778     }
779   }
780   else if (mode == TRANSLATE_JOYNAME_TO_JOYSYMBOL)
781   {
782     *joysymbol = 0;
783
784     for (i=0; i<6; i++)
785     {
786       if (strcmp(*name, translate_joy[i].name) == 0)
787       {
788         *joysymbol = translate_joy[i].joysymbol;
789         break;
790       }
791     }
792   }
793 }
794
795 char *getJoyNameFromJoySymbol(int joysymbol)
796 {
797   char *name;
798
799   translate_joyname(&joysymbol, &name, TRANSLATE_JOYSYMBOL_TO_JOYNAME);
800   return name;
801 }
802
803 int getJoySymbolFromJoyName(char *name)
804 {
805   int joysymbol;
806
807   translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL);
808   return joysymbol;
809 }
810
811 /* the following is only for debugging purpose and normally not used */
812 #define DEBUG_NUM_TIMESTAMPS    3
813
814 void debug_print_timestamp(int counter_nr, char *message)
815 {
816   static long counter[DEBUG_NUM_TIMESTAMPS][2];
817
818   if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
819     Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
820
821   counter[counter_nr][0] = Counter();
822
823   if (message)
824     printf("%s %.2f seconds\n", message,
825            (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
826
827   counter[counter_nr][1] = Counter();
828 }