1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * (c) 1995-98 Artsoft Entertainment *
8 * phone: ++49 +521 290471 *
9 * email: aeglos@valinor.owl.de *
10 *----------------------------------------------------------*
12 ***********************************************************/
16 #include <sys/types.h>
22 #if !defined(PLATFORM_WIN32)
24 #include <sys/param.h>
34 #include "joystick_TMP.h"
37 #if defined(PLATFORM_MSDOS)
38 volatile unsigned long counter = 0;
40 void increment_counter()
45 END_OF_FUNCTION(increment_counter);
49 /* maximal allowed length of a command line option */
50 #define MAX_OPTION_LEN 256
53 static unsigned long mainCounter(int mode)
55 static unsigned long base_ms = 0;
56 unsigned long current_ms;
57 unsigned long counter_ms;
59 current_ms = SDL_GetTicks();
61 /* reset base time in case of counter initializing or wrap-around */
62 if (mode == INIT_COUNTER || current_ms < base_ms)
65 counter_ms = current_ms - base_ms;
67 return counter_ms; /* return milliseconds since last init */
70 #else /* !TARGET_SDL */
72 #if defined(PLATFORM_UNIX)
73 static unsigned long mainCounter(int mode)
75 static struct timeval base_time = { 0, 0 };
76 struct timeval current_time;
77 unsigned long counter_ms;
79 gettimeofday(¤t_time, NULL);
81 /* reset base time in case of counter initializing or wrap-around */
82 if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec)
83 base_time = current_time;
85 counter_ms = (current_time.tv_sec - base_time.tv_sec) * 1000
86 + (current_time.tv_usec - base_time.tv_usec) / 1000;
88 return counter_ms; /* return milliseconds since last init */
90 #endif /* PLATFORM_UNIX */
91 #endif /* !TARGET_SDL */
93 void InitCounter() /* set counter back to zero */
95 #if !defined(PLATFORM_MSDOS)
96 mainCounter(INIT_COUNTER);
98 LOCK_VARIABLE(counter);
99 LOCK_FUNCTION(increment_counter);
100 install_int_ex(increment_counter, BPS_TO_TIMER(100));
104 unsigned long Counter() /* get milliseconds since last call of InitCounter() */
106 #if !defined(PLATFORM_MSDOS)
107 return mainCounter(READ_COUNTER);
109 return (counter * 10);
113 static void sleep_milliseconds(unsigned long milliseconds_delay)
115 boolean do_busy_waiting = (milliseconds_delay < 5 ? TRUE : FALSE);
117 #if defined(PLATFORM_MSDOS)
118 /* don't use select() to perform waiting operations under DOS/Windows
119 environment; always use a busy loop for waiting instead */
120 do_busy_waiting = TRUE;
125 /* we want to wait only a few ms -- if we assume that we have a
126 kernel timer resolution of 10 ms, we would wait far to long;
127 therefore it's better to do a short interval of busy waiting
128 to get our sleeping time more accurate */
130 unsigned long base_counter = Counter(), actual_counter = Counter();
132 while (actual_counter < base_counter + milliseconds_delay &&
133 actual_counter >= base_counter)
134 actual_counter = Counter();
138 #if defined(TARGET_SDL)
139 SDL_Delay(milliseconds_delay);
141 struct timeval delay;
143 delay.tv_sec = milliseconds_delay / 1000;
144 delay.tv_usec = 1000 * (milliseconds_delay % 1000);
146 if (select(0, NULL, NULL, NULL, &delay) != 0)
147 Error(ERR_WARN, "sleep_milliseconds(): select() failed");
152 void Delay(unsigned long delay) /* Sleep specified number of milliseconds */
154 sleep_milliseconds(delay);
157 boolean FrameReached(unsigned long *frame_counter_var,
158 unsigned long frame_delay)
160 unsigned long actual_frame_counter = FrameCounter;
162 if (actual_frame_counter < *frame_counter_var+frame_delay &&
163 actual_frame_counter >= *frame_counter_var)
166 *frame_counter_var = actual_frame_counter;
170 boolean DelayReached(unsigned long *counter_var,
173 unsigned long actual_counter = Counter();
175 if (actual_counter < *counter_var + delay &&
176 actual_counter >= *counter_var)
179 *counter_var = actual_counter;
183 void WaitUntilDelayReached(unsigned long *counter_var, unsigned long delay)
185 unsigned long actual_counter;
189 actual_counter = Counter();
191 if (actual_counter < *counter_var + delay &&
192 actual_counter >= *counter_var)
193 sleep_milliseconds((*counter_var + delay - actual_counter) / 2);
198 *counter_var = actual_counter;
201 /* int2str() returns a number converted to a string;
202 the used memory is static, but will be overwritten by later calls,
203 so if you want to save the result, copy it to a private string buffer;
204 there can be 10 local calls of int2str() without buffering the result --
205 the 11th call will then destroy the result from the first call and so on.
208 char *int2str(int number, int size)
210 static char shift_array[10][40];
211 static int shift_counter = 0;
212 char *s = shift_array[shift_counter];
214 shift_counter = (shift_counter + 1) % 10;
221 sprintf(s, " %09d", number);
222 return &s[strlen(s) - size];
226 sprintf(s, "%d", number);
231 unsigned int SimpleRND(unsigned int max)
233 #if defined(TARGET_SDL)
234 static unsigned long root = 654321;
235 unsigned long current_ms;
237 current_ms = SDL_GetTicks();
238 root = root * 4253261 + current_ms;
241 static unsigned long root = 654321;
242 struct timeval current_time;
244 gettimeofday(¤t_time, NULL);
245 root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
251 static unsigned int last_RND_value = 0;
253 unsigned int last_RND()
255 return last_RND_value;
259 unsigned int RND(unsigned int max)
262 return (last_RND_value = random_linux_libc() % max);
264 return (random_linux_libc() % max);
268 unsigned int InitRND(long seed)
270 #if defined(TARGET_SDL)
271 unsigned long current_ms;
273 if (seed == NEW_RANDOMIZE)
275 current_ms = SDL_GetTicks();
276 srandom_linux_libc((unsigned int) current_ms);
277 return (unsigned int) current_ms;
281 srandom_linux_libc((unsigned int) seed);
282 return (unsigned int) seed;
285 struct timeval current_time;
287 if (seed == NEW_RANDOMIZE)
289 gettimeofday(¤t_time, NULL);
290 srandom_linux_libc((unsigned int) current_time.tv_usec);
291 return (unsigned int) current_time.tv_usec;
295 srandom_linux_libc((unsigned int) seed);
296 return (unsigned int) seed;
303 #if defined(PLATFORM_WIN32)
304 return ANONYMOUS_NAME;
308 if ((pwd = getpwuid(getuid())) == NULL)
309 return ANONYMOUS_NAME;
317 #if defined(PLATFORM_UNIX)
320 if ((pwd = getpwuid(getuid())) == NULL || strlen(pwd->pw_gecos) == 0)
321 return ANONYMOUS_NAME;
324 static char real_name[1024];
325 char *from_ptr = pwd->pw_gecos, *to_ptr = real_name;
327 if (strchr(pwd->pw_gecos, 'ß') == NULL)
328 return pwd->pw_gecos;
330 /* the user's real name contains a 'ß' character (german sharp s),
331 which has no equivalent in upper case letters (which our fonts use) */
332 while (*from_ptr != '\0' && (long)(to_ptr - real_name) < 1024 - 2)
334 if (*from_ptr != 'ß')
335 *to_ptr++ = *from_ptr++;
347 #else /* !PLATFORM_UNIX */
348 return ANONYMOUS_NAME;
354 #if defined(PLATFORM_UNIX)
355 static char *home_dir = NULL;
359 if (!(home_dir = getenv("HOME")))
363 if ((pwd = getpwuid(getuid())))
364 home_dir = pwd->pw_dir;
376 char *getPath2(char *path1, char *path2)
378 char *complete_path = checked_malloc(strlen(path1) + 1 +
381 sprintf(complete_path, "%s/%s", path1, path2);
382 return complete_path;
385 char *getPath3(char *path1, char *path2, char *path3)
387 char *complete_path = checked_malloc(strlen(path1) + 1 +
391 sprintf(complete_path, "%s/%s/%s", path1, path2, path3);
392 return complete_path;
395 char *getStringCopy(char *s)
402 s_copy = checked_malloc(strlen(s) + 1);
408 char *getStringToLower(char *s)
410 char *s_copy = checked_malloc(strlen(s) + 1);
411 char *s_ptr = s_copy;
414 *s_ptr++ = tolower(*s++);
420 void MarkTileDirty(int x, int y)
422 int xx = redraw_x1 + x;
423 int yy = redraw_y1 + y;
428 redraw[xx][yy] = TRUE;
429 redraw_mask |= REDRAW_TILES;
432 void SetBorderElement()
436 BorderElement = EL_LEERRAUM;
438 for(y=0; y<lev_fieldy && BorderElement == EL_LEERRAUM; y++)
440 for(x=0; x<lev_fieldx; x++)
442 if (!IS_MASSIVE(Feld[x][y]))
443 BorderElement = EL_BETON;
445 if (y != 0 && y != lev_fieldy - 1 && x != lev_fieldx - 1)
451 void GetOptions(char *argv[])
453 char **options_left = &argv[1];
455 /* initialize global program options */
456 options.display_name = NULL;
457 options.server_host = NULL;
458 options.server_port = 0;
459 options.ro_base_directory = RO_BASE_PATH;
460 options.rw_base_directory = RW_BASE_PATH;
461 options.level_directory = RO_BASE_PATH "/" LEVELS_DIRECTORY;
462 options.serveronly = FALSE;
463 options.network = FALSE;
464 options.verbose = FALSE;
465 options.debug = FALSE;
467 /* initialize some more global variables */
468 global.frames_per_second = 0;
469 global.fps_slowdown = FALSE;
470 global.fps_slowdown_factor = 1;
472 while (*options_left)
474 char option_str[MAX_OPTION_LEN];
475 char *option = options_left[0];
476 char *next_option = options_left[1];
477 char *option_arg = NULL;
478 int option_len = strlen(option);
480 if (option_len >= MAX_OPTION_LEN)
481 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
483 strcpy(option_str, option); /* copy argument into buffer */
486 if (strcmp(option, "--") == 0) /* stop scanning arguments */
489 if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */
492 option_arg = strchr(option, '=');
493 if (option_arg == NULL) /* no '=' in option */
494 option_arg = next_option;
497 *option_arg++ = '\0'; /* cut argument from option */
498 if (*option_arg == '\0') /* no argument after '=' */
499 Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str);
502 option_len = strlen(option);
504 if (strcmp(option, "-") == 0)
505 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option);
506 else if (strncmp(option, "-help", option_len) == 0)
508 printf("Usage: %s [options] [server.name [port]]\n"
510 " -d, --display machine:0 X server display\n"
511 " -b, --basepath directory alternative base directory\n"
512 " -l, --level directory alternative level directory\n"
513 " -s, --serveronly only start network server\n"
514 " -n, --network network multiplayer game\n"
515 " -v, --verbose verbose mode\n",
519 else if (strncmp(option, "-display", option_len) == 0)
521 if (option_arg == NULL)
522 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
524 options.display_name = option_arg;
525 if (option_arg == next_option)
528 else if (strncmp(option, "-basepath", option_len) == 0)
530 if (option_arg == NULL)
531 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
533 /* this should be extended to separate options for ro and rw data */
534 options.ro_base_directory = option_arg;
535 options.rw_base_directory = option_arg;
536 if (option_arg == next_option)
539 /* adjust path for level directory accordingly */
540 options.level_directory =
541 getPath2(options.ro_base_directory, LEVELS_DIRECTORY);
543 else if (strncmp(option, "-levels", option_len) == 0)
545 if (option_arg == NULL)
546 Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str);
548 options.level_directory = option_arg;
549 if (option_arg == next_option)
552 else if (strncmp(option, "-network", option_len) == 0)
554 options.network = TRUE;
556 else if (strncmp(option, "-serveronly", option_len) == 0)
558 options.serveronly = TRUE;
560 else if (strncmp(option, "-verbose", option_len) == 0)
562 options.verbose = TRUE;
564 else if (strncmp(option, "-debug", option_len) == 0)
566 options.debug = TRUE;
568 else if (*option == '-')
570 Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str);
572 else if (options.server_host == NULL)
574 options.server_host = *options_left;
576 else if (options.server_port == 0)
578 options.server_port = atoi(*options_left);
579 if (options.server_port < 1024)
580 Error(ERR_EXIT_HELP, "bad port number '%d'", options.server_port);
583 Error(ERR_EXIT_HELP, "too many arguments");
589 void Error(int mode, char *format, ...)
591 char *process_name = "";
592 FILE *error = stderr;
594 /* display warnings only when running in verbose mode */
595 if (mode & ERR_WARN && !options.verbose)
598 #if !defined(PLATFORM_UNIX)
599 if ((error = openErrorFile()) == NULL)
601 printf("Cannot write to error output file!\n");
606 if (mode & ERR_SOUND_SERVER)
607 process_name = " sound server";
608 else if (mode & ERR_NETWORK_SERVER)
609 process_name = " network server";
610 else if (mode & ERR_NETWORK_CLIENT)
611 process_name = " network client **";
617 fprintf(error, "%s%s: ", program_name, process_name);
620 fprintf(error, "warning: ");
622 va_start(ap, format);
623 vfprintf(error, format, ap);
626 fprintf(error, "\n");
630 fprintf(error, "%s: Try option '--help' for more information.\n",
634 fprintf(error, "%s%s: aborting\n", program_name, process_name);
641 if (mode & ERR_FROM_SERVER)
642 exit(1); /* child process: normal exit */
644 CloseAllAndExit(1); /* main process: clean up stuff */
648 void *checked_malloc(unsigned long size)
655 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
660 void *checked_calloc(unsigned long size)
664 ptr = calloc(1, size);
667 Error(ERR_EXIT, "cannot allocate %d bytes -- out of memory", size);
672 short getFile16BitInteger(FILE *file, int byte_order)
674 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
675 return ((fgetc(file) << 8) |
677 else /* BYTE_ORDER_LITTLE_ENDIAN */
678 return ((fgetc(file) << 0) |
682 void putFile16BitInteger(FILE *file, short value, int byte_order)
684 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
686 fputc((value >> 8) & 0xff, file);
687 fputc((value >> 0) & 0xff, file);
689 else /* BYTE_ORDER_LITTLE_ENDIAN */
691 fputc((value >> 0) & 0xff, file);
692 fputc((value >> 8) & 0xff, file);
696 int getFile32BitInteger(FILE *file, int byte_order)
698 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
699 return ((fgetc(file) << 24) |
700 (fgetc(file) << 16) |
703 else /* BYTE_ORDER_LITTLE_ENDIAN */
704 return ((fgetc(file) << 0) |
706 (fgetc(file) << 16) |
707 (fgetc(file) << 24));
710 void putFile32BitInteger(FILE *file, int value, int byte_order)
712 if (byte_order == BYTE_ORDER_BIG_ENDIAN)
714 fputc((value >> 24) & 0xff, file);
715 fputc((value >> 16) & 0xff, file);
716 fputc((value >> 8) & 0xff, file);
717 fputc((value >> 0) & 0xff, file);
719 else /* BYTE_ORDER_LITTLE_ENDIAN */
721 fputc((value >> 0) & 0xff, file);
722 fputc((value >> 8) & 0xff, file);
723 fputc((value >> 16) & 0xff, file);
724 fputc((value >> 24) & 0xff, file);
728 void getFileChunk(FILE *file, char *chunk_buffer, int *chunk_length,
731 const int chunk_identifier_length = 4;
733 /* read chunk identifier */
734 fgets(chunk_buffer, chunk_identifier_length + 1, file);
736 /* read chunk length */
737 *chunk_length = getFile32BitInteger(file, byte_order);
740 void putFileChunk(FILE *file, char *chunk_name, int chunk_length,
743 /* write chunk identifier */
744 fputs(chunk_name, file);
746 /* write chunk length */
747 putFile32BitInteger(file, chunk_length, byte_order);
750 #define TRANSLATE_KEYSYM_TO_KEYNAME 0
751 #define TRANSLATE_KEYSYM_TO_X11KEYNAME 1
752 #define TRANSLATE_X11KEYNAME_TO_KEYSYM 2
754 void translate_keyname(Key *keysym, char **x11name, char **name, int mode)
763 /* normal cursor keys */
764 { KSYM_Left, "XK_Left", "cursor left" },
765 { KSYM_Right, "XK_Right", "cursor right" },
766 { KSYM_Up, "XK_Up", "cursor up" },
767 { KSYM_Down, "XK_Down", "cursor down" },
769 /* keypad cursor keys */
771 { KSYM_KP_Left, "XK_KP_Left", "keypad left" },
772 { KSYM_KP_Right, "XK_KP_Right", "keypad right" },
773 { KSYM_KP_Up, "XK_KP_Up", "keypad up" },
774 { KSYM_KP_Down, "XK_KP_Down", "keypad down" },
777 /* other keypad keys */
779 { KSYM_KP_Enter, "XK_KP_Enter", "keypad enter" },
780 { KSYM_KP_Add, "XK_KP_Add", "keypad +" },
781 { KSYM_KP_Subtract, "XK_KP_Subtract", "keypad -" },
782 { KSYM_KP_Multiply, "XK_KP_Multiply", "keypad mltply" },
783 { KSYM_KP_Divide, "XK_KP_Divide", "keypad /" },
784 { KSYM_KP_Separator,"XK_KP_Separator", "keypad ," },
788 { KSYM_Shift_L, "XK_Shift_L", "left shift" },
789 { KSYM_Shift_R, "XK_Shift_R", "right shift" },
790 { KSYM_Control_L, "XK_Control_L", "left control" },
791 { KSYM_Control_R, "XK_Control_R", "right control" },
792 { KSYM_Meta_L, "XK_Meta_L", "left meta" },
793 { KSYM_Meta_R, "XK_Meta_R", "right meta" },
794 { KSYM_Alt_L, "XK_Alt_L", "left alt" },
795 { KSYM_Alt_R, "XK_Alt_R", "right alt" },
796 { KSYM_Super_L, "XK_Super_L", "left super" }, /* Win-L */
797 { KSYM_Super_R, "XK_Super_R", "right super" }, /* Win-R */
798 { KSYM_Mode_switch, "XK_Mode_switch", "mode switch" }, /* Alt-R */
799 { KSYM_Multi_key, "XK_Multi_key", "multi key" }, /* Ctrl-R */
801 /* some special keys */
802 { KSYM_BackSpace, "XK_BackSpace", "backspace" },
803 { KSYM_Delete, "XK_Delete", "delete" },
804 { KSYM_Insert, "XK_Insert", "insert" },
805 { KSYM_Tab, "XK_Tab", "tab" },
806 { KSYM_Home, "XK_Home", "home" },
807 { KSYM_End, "XK_End", "end" },
808 { KSYM_Page_Up, "XK_Page_Up", "page up" },
809 { KSYM_Page_Down, "XK_Page_Down", "page down" },
810 { KSYM_Menu, "XK_Menu", "menu" }, /* Win-Menu */
812 /* ASCII 0x20 to 0x40 keys (except numbers) */
813 { KSYM_space, "XK_space", "space" },
814 { KSYM_exclam, "XK_exclam", "!" },
815 { KSYM_quotedbl, "XK_quotedbl", "\"" },
816 { KSYM_numbersign, "XK_numbersign", "#" },
817 { KSYM_dollar, "XK_dollar", "$" },
818 { KSYM_percent, "XK_percent", "%" },
819 { KSYM_ampersand, "XK_ampersand", "&" },
820 { KSYM_apostrophe, "XK_apostrophe", "'" },
821 { KSYM_parenleft, "XK_parenleft", "(" },
822 { KSYM_parenright, "XK_parenright", ")" },
823 { KSYM_asterisk, "XK_asterisk", "*" },
824 { KSYM_plus, "XK_plus", "+" },
825 { KSYM_comma, "XK_comma", "," },
826 { KSYM_minus, "XK_minus", "-" },
827 { KSYM_period, "XK_period", "." },
828 { KSYM_slash, "XK_slash", "/" },
829 { KSYM_colon, "XK_colon", ":" },
830 { KSYM_semicolon, "XK_semicolon", ";" },
831 { KSYM_less, "XK_less", "<" },
832 { KSYM_equal, "XK_equal", "=" },
833 { KSYM_greater, "XK_greater", ">" },
834 { KSYM_question, "XK_question", "?" },
835 { KSYM_at, "XK_at", "@" },
837 /* more ASCII keys */
838 { KSYM_bracketleft, "XK_bracketleft", "[" },
839 { KSYM_backslash, "XK_backslash", "backslash" },
840 { KSYM_bracketright,"XK_bracketright", "]" },
841 { KSYM_asciicircum, "XK_asciicircum", "circumflex" },
842 { KSYM_underscore, "XK_underscore", "_" },
843 { KSYM_grave, "XK_grave", "grave" },
844 { KSYM_quoteleft, "XK_quoteleft", "quote left" },
845 { KSYM_braceleft, "XK_braceleft", "brace left" },
846 { KSYM_bar, "XK_bar", "bar" },
847 { KSYM_braceright, "XK_braceright", "brace right" },
848 { KSYM_asciitilde, "XK_asciitilde", "ascii tilde" },
850 /* special (non-ASCII) keys */
851 { KSYM_Adiaeresis, "XK_Adiaeresis", "Ä" },
852 { KSYM_Odiaeresis, "XK_Odiaeresis", "Ö" },
853 { KSYM_Udiaeresis, "XK_Udiaeresis", "Ü" },
854 { KSYM_adiaeresis, "XK_adiaeresis", "ä" },
855 { KSYM_odiaeresis, "XK_odiaeresis", "ö" },
856 { KSYM_udiaeresis, "XK_udiaeresis", "ü" },
857 { KSYM_ssharp, "XK_ssharp", "sharp s" },
859 /* end-of-array identifier */
865 if (mode == TRANSLATE_KEYSYM_TO_KEYNAME)
867 static char name_buffer[30];
870 if (key >= KSYM_A && key <= KSYM_Z)
871 sprintf(name_buffer, "%c", 'A' + (char)(key - KSYM_A));
872 else if (key >= KSYM_a && key <= KSYM_z)
873 sprintf(name_buffer, "%c", 'a' + (char)(key - KSYM_a));
874 else if (key >= KSYM_0 && key <= KSYM_9)
875 sprintf(name_buffer, "%c", '0' + (char)(key - KSYM_0));
876 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
877 sprintf(name_buffer, "keypad %c", '0' + (char)(key - KSYM_KP_0));
878 else if (key >= KSYM_F1 && key <= KSYM_F24)
879 sprintf(name_buffer, "function F%d", (int)(key - KSYM_F1 + 1));
880 else if (key == KSYM_UNDEFINED)
881 strcpy(name_buffer, "(undefined)");
888 if (key == translate_key[i].key)
890 strcpy(name_buffer, translate_key[i].name);
894 while (translate_key[++i].name);
896 if (!translate_key[i].name)
897 strcpy(name_buffer, "(unknown)");
902 else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME)
904 static char name_buffer[30];
907 if (key >= KSYM_A && key <= KSYM_Z)
908 sprintf(name_buffer, "XK_%c", 'A' + (char)(key - KSYM_A));
909 else if (key >= KSYM_a && key <= KSYM_z)
910 sprintf(name_buffer, "XK_%c", 'a' + (char)(key - KSYM_a));
911 else if (key >= KSYM_0 && key <= KSYM_9)
912 sprintf(name_buffer, "XK_%c", '0' + (char)(key - KSYM_0));
913 else if (key >= KSYM_KP_0 && key <= KSYM_KP_9)
914 sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - KSYM_KP_0));
915 else if (key >= KSYM_F1 && key <= KSYM_F24)
916 sprintf(name_buffer, "XK_F%d", (int)(key - KSYM_F1 + 1));
917 else if (key == KSYM_UNDEFINED)
918 strcpy(name_buffer, "[undefined]");
925 if (key == translate_key[i].key)
927 strcpy(name_buffer, translate_key[i].x11name);
931 while (translate_key[++i].x11name);
933 if (!translate_key[i].x11name)
934 sprintf(name_buffer, "0x%04lx", (unsigned long)key);
937 *x11name = name_buffer;
939 else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM)
941 Key key = KSYM_UNDEFINED;
942 char *name_ptr = *x11name;
944 if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4)
946 char c = name_ptr[3];
948 if (c >= 'A' && c <= 'Z')
949 key = KSYM_A + (Key)(c - 'A');
950 else if (c >= 'a' && c <= 'z')
951 key = KSYM_a + (Key)(c - 'a');
952 else if (c >= '0' && c <= '9')
953 key = KSYM_0 + (Key)(c - '0');
955 else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7)
957 char c = name_ptr[6];
959 if (c >= '0' && c <= '9')
960 key = KSYM_0 + (Key)(c - '0');
962 else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6)
964 char c1 = name_ptr[4];
965 char c2 = name_ptr[5];
968 if ((c1 >= '0' && c1 <= '9') &&
969 ((c2 >= '0' && c1 <= '9') || c2 == '\0'))
970 d = atoi(&name_ptr[4]);
972 if (d >=1 && d <= 24)
973 key = KSYM_F1 + (Key)(d - 1);
975 else if (strncmp(name_ptr, "XK_", 3) == 0)
981 if (strcmp(name_ptr, translate_key[i].x11name) == 0)
983 key = translate_key[i].key;
987 while (translate_key[++i].x11name);
989 else if (strncmp(name_ptr, "0x", 2) == 0)
991 unsigned long value = 0;
997 char c = *name_ptr++;
1000 if (c >= '0' && c <= '9')
1002 else if (c >= 'a' && c <= 'f')
1003 d = (int)(c - 'a' + 10);
1004 else if (c >= 'A' && c <= 'F')
1005 d = (int)(c - 'A' + 10);
1013 value = value * 16 + d;
1024 char *getKeyNameFromKey(Key key)
1028 translate_keyname(&key, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME);
1032 char *getX11KeyNameFromKey(Key key)
1036 translate_keyname(&key, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME);
1040 Key getKeyFromX11KeyName(char *x11name)
1044 translate_keyname(&key, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM);
1048 char getCharFromKey(Key key)
1050 char *keyname = getKeyNameFromKey(key);
1053 if (strlen(keyname) == 1)
1054 letter = keyname[0];
1055 else if (strcmp(keyname, "space") == 0)
1057 else if (strcmp(keyname, "circumflex") == 0)
1063 /* ------------------------------------------------------------------------- */
1064 /* some functions to handle lists of level directories */
1065 /* ------------------------------------------------------------------------- */
1067 struct LevelDirInfo *newLevelDirInfo()
1069 return checked_calloc(sizeof(struct LevelDirInfo));
1072 void pushLevelDirInfo(struct LevelDirInfo **node_first,
1073 struct LevelDirInfo *node_new)
1075 node_new->next = *node_first;
1076 *node_first = node_new;
1079 int numLevelDirInfo(struct LevelDirInfo *node)
1092 boolean validLevelSeries(struct LevelDirInfo *node)
1094 return (node != NULL && !node->node_group && !node->parent_link);
1097 struct LevelDirInfo *getFirstValidLevelSeries(struct LevelDirInfo *node)
1101 if (leveldir_first) /* start with first level directory entry */
1102 return getFirstValidLevelSeries(leveldir_first);
1106 else if (node->node_group) /* enter level group (step down into tree) */
1107 return getFirstValidLevelSeries(node->node_group);
1108 else if (node->parent_link) /* skip start entry of level group */
1110 if (node->next) /* get first real level series entry */
1111 return getFirstValidLevelSeries(node->next);
1112 else /* leave empty level group and go on */
1113 return getFirstValidLevelSeries(node->node_parent->next);
1115 else /* this seems to be a regular level series */
1119 struct LevelDirInfo *getLevelDirInfoFirstGroupEntry(struct LevelDirInfo *node)
1124 if (node->node_parent == NULL) /* top level group */
1125 return leveldir_first;
1126 else /* sub level group */
1127 return node->node_parent->node_group;
1130 int numLevelDirInfoInGroup(struct LevelDirInfo *node)
1132 return numLevelDirInfo(getLevelDirInfoFirstGroupEntry(node));
1135 int posLevelDirInfo(struct LevelDirInfo *node)
1137 struct LevelDirInfo *node_cmp = getLevelDirInfoFirstGroupEntry(node);
1142 if (node_cmp == node)
1146 node_cmp = node_cmp->next;
1152 struct LevelDirInfo *getLevelDirInfoFromPos(struct LevelDirInfo *node, int pos)
1154 struct LevelDirInfo *node_default = node;
1166 return node_default;
1169 struct LevelDirInfo *getLevelDirInfoFromFilenameExt(struct LevelDirInfo *node,
1172 if (filename == NULL)
1177 if (node->node_group)
1179 struct LevelDirInfo *node_group;
1181 node_group = getLevelDirInfoFromFilenameExt(node->node_group, filename);
1186 else if (!node->parent_link)
1188 if (strcmp(filename, node->filename) == 0)
1198 struct LevelDirInfo *getLevelDirInfoFromFilename(char *filename)
1200 return getLevelDirInfoFromFilenameExt(leveldir_first, filename);
1203 void dumpLevelDirInfo(struct LevelDirInfo *node, int depth)
1209 for (i=0; i<depth * 3; i++)
1212 printf("filename == '%s'\n", node->filename);
1214 if (node->node_group != NULL)
1215 dumpLevelDirInfo(node->node_group, depth + 1);
1221 void sortLevelDirInfo(struct LevelDirInfo **node_first,
1222 int (*compare_function)(const void *, const void *))
1224 int num_nodes = numLevelDirInfo(*node_first);
1225 struct LevelDirInfo **sort_array;
1226 struct LevelDirInfo *node = *node_first;
1232 /* allocate array for sorting structure pointers */
1233 sort_array = checked_calloc(num_nodes * sizeof(struct LevelDirInfo *));
1235 /* writing structure pointers to sorting array */
1236 while (i < num_nodes && node) /* double boundary check... */
1238 sort_array[i] = node;
1244 /* sorting the structure pointers in the sorting array */
1245 qsort(sort_array, num_nodes, sizeof(struct LevelDirInfo *),
1248 /* update the linkage of list elements with the sorted node array */
1249 for (i=0; i<num_nodes - 1; i++)
1250 sort_array[i]->next = sort_array[i + 1];
1251 sort_array[num_nodes - 1]->next = NULL;
1253 /* update the linkage of the main list anchor pointer */
1254 *node_first = sort_array[0];
1258 /* now recursively sort the level group structures */
1262 if (node->node_group != NULL)
1263 sortLevelDirInfo(&node->node_group, compare_function);
1269 inline void swap_numbers(int *i1, int *i2)
1277 inline void swap_number_pairs(int *x1, int *y1, int *x2, int *y2)
1290 /* ------------------------------------------------------------------------- */
1291 /* the following is only for debugging purpose and normally not used */
1292 /* ------------------------------------------------------------------------- */
1294 #define DEBUG_NUM_TIMESTAMPS 3
1296 void debug_print_timestamp(int counter_nr, char *message)
1298 static long counter[DEBUG_NUM_TIMESTAMPS][2];
1300 if (counter_nr >= DEBUG_NUM_TIMESTAMPS)
1301 Error(ERR_EXIT, "debugging: increase DEBUG_NUM_TIMESTAMPS in misc.c");
1303 counter[counter_nr][0] = Counter();
1306 printf("%s %.2f seconds\n", message,
1307 (float)(counter[counter_nr][0] - counter[counter_nr][1]) / 1000);
1309 counter[counter_nr][1] = Counter();