From d7d97176a88ee47f893055480c76b9c05a253d05 Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Sun, 25 Oct 1998 01:35:43 +0200 Subject: [PATCH] rnd-19981025-1 --- src/events.c | 9 +- src/files.c | 631 +++++++++++++++++++++++++++++++++++++++++++++++++- src/files.h | 7 + src/game.c | 19 -- src/init.c | 13 ++ src/main.c | 2 +- src/main.h | 29 ++- src/misc.c | 364 +++++++++++++++++++++++------ src/misc.h | 6 +- src/screens.c | 9 +- 10 files changed, 973 insertions(+), 116 deletions(-) diff --git a/src/events.c b/src/events.c index dc398df7..5fd56c0b 100644 --- a/src/events.c +++ b/src/events.c @@ -720,6 +720,10 @@ void HandleKey(KeySym key, int key_status) local_player->dynamite = 1000; break; + + +#if 0 + case XK_x: { @@ -775,8 +779,6 @@ void HandleKey(KeySym key, int key_status) break; - -#if 0 case XK_y: /* { @@ -795,8 +797,6 @@ void HandleKey(KeySym key, int key_status) printf("direct_draw_on == %d\n", setup.direct_draw_on); break; -#endif - case XK_z: { @@ -828,6 +828,7 @@ void HandleKey(KeySym key, int key_status) } break; +#endif #endif default: diff --git a/src/files.c b/src/files.c index e1a4b80c..a77b4463 100644 --- a/src/files.c +++ b/src/files.c @@ -11,6 +11,8 @@ * files.h * ***********************************************************/ +#include + #include "files.h" #include "tools.h" #include "misc.h" @@ -83,7 +85,7 @@ boolean LoadLevelInfo() if (!(file=fopen(filename,"r"))) { - Error(ERR_RETURN, "cannot load level info '%s'", filename); + Error(ERR_RETURN, "cannot read level info '%s'", filename); return(FALSE); } @@ -129,7 +131,7 @@ void LoadLevel(int level_nr) level_directory,leveldir[leveldir_nr].filename,level_nr); if (!(file = fopen(filename,"r"))) - Error(ERR_RETURN, "cannot load level '%s' - creating new level", filename); + Error(ERR_RETURN, "cannot read level '%s' - creating new level", filename); else { fgets(cookie,LEVEL_COOKIE_LEN,file); @@ -302,7 +304,7 @@ void LoadScore(int level_nr) if (!CreateNewScoreFile()) Error(ERR_RETURN, "cannot create score file '%s'", filename); else if (!(file = fopen(filename,"r"))) - Error(ERR_RETURN, "cannot load score for level %d", level_nr); + Error(ERR_RETURN, "cannot read score for level %d", level_nr); } if (file) @@ -371,7 +373,7 @@ void LoadPlayerInfo(int mode) if (!CreateNewNamesFile(mode)) Error(ERR_RETURN, "cannot create names file '%s'", filename); else if (!(file = fopen(filename,"r"))) - Error(ERR_RETURN, "cannot load player information file '%s'", filename); + Error(ERR_RETURN, "cannot read player information file '%s'", filename); } if (file) @@ -620,7 +622,14 @@ void SavePlayerInfo(int mode) struct PlayerInfo default_player; int version_10_file = FALSE; - if (mode==PLAYER_LEVEL) + + + if (mode == PLAYER_SETUP) + SaveSetup(); + + + + if (mode == PLAYER_LEVEL) sprintf(filename,"%s/%s/%s", level_directory,leveldir[leveldir_nr].filename,NAMES_FILENAME); else @@ -763,3 +772,615 @@ void SaveJoystickData() #endif } + +/* ------------------------------------------------------------------------- */ +/* new setup functions */ +/* ------------------------------------------------------------------------- */ + +#define SETUP_TOKEN_SOUND 0 +#define SETUP_TOKEN_SOUND_LOOPS 1 +#define SETUP_TOKEN_SOUND_MUSIC 2 +#define SETUP_TOKEN_SOUND_SIMPLE 3 +#define SETUP_TOKEN_TOONS 4 +#define SETUP_TOKEN_DIRECT_DRAW 5 +#define SETUP_TOKEN_SCROLL_DELAY 6 +#define SETUP_TOKEN_SOFT_SCROLLING 7 +#define SETUP_TOKEN_FADING 8 +#define SETUP_TOKEN_AUTORECORD 9 +#define SETUP_TOKEN_QUICK_DOORS 10 +#define SETUP_TOKEN_USE_JOYSTICK 11 +#define SETUP_TOKEN_JOYSTICK_NR 12 +#define SETUP_TOKEN_JOY_SNAP 13 +#define SETUP_TOKEN_JOY_BOMB 14 +#define SETUP_TOKEN_KEY_LEFT 15 +#define SETUP_TOKEN_KEY_RIGHT 16 +#define SETUP_TOKEN_KEY_UP 17 +#define SETUP_TOKEN_KEY_DOWN 18 +#define SETUP_TOKEN_KEY_SNAP 19 +#define SETUP_TOKEN_KEY_BOMB 20 + +#define NUM_SETUP_TOKENS 21 + +#define SETUP_TOKEN_PLAYER_PREFIX "player_" + +static struct +{ + char *token, *value_true, *value_false; +} setup_info[] = +{ + { "sound", "on", "off" }, + { "repeating_sound_loops", "on", "off" }, + { "background_music", "on", "off" }, + { "simple_sound_effects", "on", "off" }, + { "toons", "on", "off" }, + { "double_buffering", "off", "on" }, + { "scroll_delay", "on", "off" }, + { "soft_scrolling", "on", "off" }, + { "screen_fading", "on", "off" }, + { "automatic_tape_recording", "on", "off" }, + { "quick_doors", "on", "off" }, + + /* for each player: */ + { ".use_joystick", "true", "false" }, + { ".joystick_device", "second", "first" }, + { ".joy.snap_field", "", "" }, + { ".joy.place_bomb", "", "" }, + { ".key.move_left", "", "" }, + { ".key.move_right", "", "" }, + { ".key.move_up", "", "" }, + { ".key.move_down", "", "" }, + { ".key.snap_field", "", "" }, + { ".key.place_bomb", "", "" } +}; + +static char *string_tolower(char *s) +{ + static char s_lower[100]; + int i; + + if (strlen(s) >= 100) + return s; + + strcpy(s_lower, s); + + for (i=0; itoken) + free(setup_file_info->token); + if (setup_file_info->value) + free(setup_file_info->value); + if (setup_file_info->next) + freeSetupFileInfo(setup_file_info->next); + free(setup_file_info); +} + +static struct SetupFileInfo *newSetupFileInfo(char *token, char *value) +{ + struct SetupFileInfo *new = checked_malloc(sizeof(struct SetupFileInfo)); + + new->token = checked_malloc(strlen(token) + 1); + strcpy(new->token, token); + + new->value = checked_malloc(strlen(value) + 1); + strcpy(new->value, value); + + new->next = NULL; + + return new; +} + +static char *lookupSetupFileValue(struct SetupFileInfo *setup_file_info, + char *token) +{ + if (!setup_file_info) + return NULL; + + if (strcmp(setup_file_info->token, token) == 0) + return setup_file_info->value; + else + return lookupSetupFileValue(setup_file_info->next, token); +} + +#ifdef DEBUG +static void printSetupFileInfo(struct SetupFileInfo *setup_file_info) +{ + if (!setup_file_info) + return; + + printf("token: '%s'\n", setup_file_info->token); + printf("value: '%s'\n", setup_file_info->value); + + printSetupFileInfo(setup_file_info->next); +} +#endif + +static void decodeSetupFileInfo(struct SetupFileInfo *setup_file_info) +{ + int i; + int token_nr = -1; + int player_nr = 0; + char *token; + char *token_value; + int token_integer_value; + boolean token_boolean_value; + int token_player_prefix_len; + + if (!setup_file_info) + return; + + token = setup_file_info->token; + token_value = setup_file_info->value; + token_integer_value = get_string_integer_value(token_value); + token_boolean_value = get_string_boolean_value(token_value); + + token_player_prefix_len = strlen(SETUP_TOKEN_PLAYER_PREFIX); + + if (strncmp(token, SETUP_TOKEN_PLAYER_PREFIX, token_player_prefix_len) == 0) + { + token += token_player_prefix_len; + + if (*token >= '0' && *token <= '9') + { + player_nr = ((int)(*token - '0') - 1 + MAX_PLAYERS) % MAX_PLAYERS; + token++; + } + } + + for (i=0; i 1) + token_integer_value = 1; + setup.input[player_nr].joystick_nr = token_integer_value - 1; + break; + case SETUP_TOKEN_JOY_SNAP: + setup.input[player_nr].joy.snap = getJoySymbolFromJoyName(token_value); + break; + case SETUP_TOKEN_JOY_BOMB : + setup.input[player_nr].joy.bomb = getJoySymbolFromJoyName(token_value); + break; + case SETUP_TOKEN_KEY_LEFT: + setup.input[player_nr].key.left = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_RIGHT: + setup.input[player_nr].key.right = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_UP: + setup.input[player_nr].key.up = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_DOWN: + setup.input[player_nr].key.down = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_SNAP: + setup.input[player_nr].key.snap = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_BOMB: + setup.input[player_nr].key.bomb = getKeySymFromX11KeyName(token_value); + break; + default: + break; + } + + decodeSetupFileInfo(setup_file_info->next); +} + +void LoadSetup() +{ + int line_len; + char filename[MAX_FILENAME_LEN]; + char line[MAX_LINE_LEN]; + char *token, *value, *line_ptr; + struct SetupFileInfo *setup_file_info, **next_entry = &setup_file_info; + FILE *file; + + + + printf("LoadSetup\n"); + + + + sprintf(filename, "%s/%s", SETUP_PATH, SETUP_FILENAME); + + if (!(file = fopen(filename, "r"))) + { + int i; + + Error(ERR_RETURN, "cannot open setup file '%s'", filename); + + /* use default values for setup */ + + setup.sound_on = TRUE; + setup.sound_loops_on = FALSE; + setup.sound_music_on = FALSE; + setup.sound_simple_on = FALSE; + setup.toons_on = TRUE; + setup.direct_draw_on = FALSE; + setup.scroll_delay_on = FALSE; + setup.soft_scrolling_on = TRUE; + setup.fading_on = FALSE; + setup.autorecord_on = FALSE; + setup.quick_doors = FALSE; + + for (i=0; i line; line_ptr--) + if ((*line_ptr == ' ' || *line_ptr == '\t') && line_ptr[1] == '\0') + *line_ptr = '\0'; + + /* ignore empty lines */ + if (*line == '\0') + continue; + + line_len = strlen(line); + + + /* + printf("line: '%s'\n", line); + */ + + + /* cut leading whitespaces from token */ + for (token = line; *token; token++) + if (*token != ' ' && *token != '\t') + break; + + /* find end of token */ + for (line_ptr = token; *line_ptr; line_ptr++) + { + if (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == ':') + { + *line_ptr = '\0'; + break; + } + } + + if (line_ptr < line + line_len) + value = line_ptr + 1; + else + value = "\0"; + + /* cut leading whitespaces from value */ + for (; *value; value++) + if (*value != ' ' && *value != '\t') + break; + + + /* + printf("token / value: '%s' / '%s'\n", token, value); + */ + + + if (*token && *value) + { + /* allocate new token/value pair */ + + *next_entry = newSetupFileInfo(token, value); + next_entry = &((*next_entry)->next); + } + } + + fclose(file); + +#if 0 + printf("Content of setup file info:\n"); + + printSetupFileInfo(setup_file_info); +#endif + + + + printf("decodeSetupFileInfo\n"); + + + + decodeSetupFileInfo(setup_file_info); + freeSetupFileInfo(setup_file_info); +} + +void SaveSetup() +{ + int i; + char filename[MAX_FILENAME_LEN]; + FILE *file; + + + + printf("SaveSetup\n"); + + + + sprintf(filename, "%s/%s", SETUP_PATH, SETUP_FILENAME); + + if (!(file = fopen(filename, "w"))) + { + Error(ERR_RETURN, "cannot write setup file '%s'", filename); + return; + } + + fprintf(file, "file_identifier: %s\n", + SETUP_COOKIE); + + fprintf(file, "\n"); + + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOUND, + setup.sound_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOUND_LOOPS, + setup.sound_loops_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOUND_MUSIC, + setup.sound_music_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOUND_SIMPLE, + setup.sound_simple_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_TOONS, + setup.toons_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_DIRECT_DRAW, + setup.direct_draw_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SCROLL_DELAY, + setup.scroll_delay_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOFT_SCROLLING, + setup.soft_scrolling_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_FADING, + setup.fading_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_AUTORECORD, + setup.autorecord_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_QUICK_DOORS, + setup.quick_doors)); + + for (i=0; iindex_nr); - - - /* actual player has left the screen -- scroll in that direction */ if (jx != old_jx) /* player has moved horizontally */ scroll_x += (jx - old_jx); @@ -3456,23 +3449,11 @@ boolean MoveFigure(struct PlayerInfo *player, int dx, int dy) { if (!options.network && !AllPlayersInVisibleScreen()) { - - - printf("oops! not all players visible if we scroll now\n"); - - - scroll_x = old_scroll_x; scroll_y = old_scroll_y; } else { - - - printf("ok, scrolling screen...\n"); - - - ScrollScreen(player, SCROLL_INIT); ScrollLevel(old_scroll_x - scroll_x, old_scroll_y - scroll_y); } diff --git a/src/init.c b/src/init.c index c3798d5a..211e66e3 100644 --- a/src/init.c +++ b/src/init.c @@ -94,6 +94,9 @@ void InitLevelAndPlayerInfo() { int i; + +#if 0 + /* initialize local setup */ setup.sound_on = TRUE; setup.sound_loops_on = FALSE; @@ -120,12 +123,22 @@ void InitLevelAndPlayerInfo() setup.input[i].key.bomb = (i == 0 ? DEFAULT_KEY_BOMB : KEY_UNDEFINDED); } +#endif + + + /* choose default local player */ local_player = &stored_player[0]; if (!LoadLevelInfo()) /* global level info */ Error(ERR_EXIT, NULL); + + + LoadSetup(); + + + LoadPlayerInfo(PLAYER_SETUP); /* global setup info */ LoadPlayerInfo(PLAYER_LEVEL); /* level specific info */ diff --git a/src/main.c b/src/main.c index 4a713ecf..509e038f 100644 --- a/src/main.c +++ b/src/main.c @@ -99,7 +99,7 @@ int TestPlayer = 0; struct LevelDirInfo leveldir[MAX_LEVDIR_ENTRIES]; struct LevelInfo level; -struct PlayerInfo stored_player[MAX_PLAYERS], *local_player; +struct PlayerInfo stored_player[MAX_PLAYERS], *local_player = NULL; struct HiScore highscore[MAX_SCORE_ENTRIES]; struct SoundInfo Sound[NUM_SOUNDS]; struct RecordingInfo tape; diff --git a/src/main.h b/src/main.h index 16ff5403..a43543e5 100644 --- a/src/main.h +++ b/src/main.h @@ -217,6 +217,12 @@ struct OptionInfo boolean verbose; }; +struct SetupJoystickInfo +{ + int snap; + int bomb; +}; + struct SetupKeyboardInfo { KeySym left; @@ -227,12 +233,6 @@ struct SetupKeyboardInfo KeySym bomb; }; -struct SetupJoystickInfo -{ - int snap; - int bomb; -}; - struct SetupInfo { boolean sound_on; @@ -255,6 +255,13 @@ struct SetupInfo } input[MAX_PLAYERS]; }; +struct SetupFileInfo +{ + char *token; + char *value; + struct SetupFileInfo *next; +}; + struct PlayerInfo { boolean present; /* player present in level playfield */ @@ -1012,7 +1019,7 @@ extern int num_bg_loops; (s)==SND_TWILIGHT) /* default input keys */ -#define KEY_UNDEFINDED 0 +#define KEY_UNDEFINDED XK_VoidSymbol #define DEFAULT_KEY_LEFT XK_Left #define DEFAULT_KEY_RIGHT XK_Right #define DEFAULT_KEY_UP XK_Up @@ -1083,17 +1090,22 @@ extern int num_bg_loops; #ifndef JOYDAT_PATH #define JOYDAT_PATH GAME_DIR #endif +#ifndef SETUP_PATH +#define SETUP_PATH GAME_DIR +#endif #ifndef MSDOS #define SCORE_FILENAME "ROCKS.score" #define NAMES_FILENAME "ROCKS.names" #define LEVDIR_FILENAME "ROCKS.levelinfo" #define JOYDAT_FILENAME "ROCKS.joystick" +#define SETUP_FILENAME "ROCKS.setup" #else #define SCORE_FILENAME "ROCKS.sco" #define NAMES_FILENAME "ROCKS.nam" #define LEVDIR_FILENAME "ROCKS.lev" #define JOYDAT_FILENAME "ROCKS.joy" +#define SETUP_FILENAME "ROCKS.set" #endif #define JOYDAT_FILE JOYDAT_PATH "/" JOYDAT_FILENAME @@ -1104,6 +1116,7 @@ extern int num_bg_loops; #define LEVDIR_PERMS LEVEL_PERMS #define LEVREC_PERMS LEVEL_PERMS #define JOYDAT_PERMS LEVEL_PERMS +#define SETUP_PERMS LEVEL_PERMS /* old cookies */ #define NAMES_COOKIE_10 "ROCKSNDIAMONDS_NAMES_FILE_VERSION_1.0" @@ -1115,12 +1128,14 @@ extern int num_bg_loops; #define LEVELDIR_COOKIE "ROCKSNDIAMONDS_LEVELDIR_FILE_VERSION_1.0" #define LEVELREC_COOKIE "ROCKSNDIAMONDS_LEVELREC_FILE_VERSION_1.2" #define JOYSTICK_COOKIE "ROCKSNDIAMONDS_JOYSTICK_FILE_VERSION_1.0" +#define SETUP_COOKIE "ROCKSNDIAMONDS_SETUP_FILE_VERSION_1.2" #define LEVEL_COOKIE_LEN (strlen(LEVEL_COOKIE)+1) #define SCORE_COOKIE_LEN (strlen(SCORE_COOKIE)+1) #define NAMES_COOKIE_LEN (strlen(NAMES_COOKIE)+1) #define LEVELDIR_COOKIE_LEN (strlen(LEVELDIR_COOKIE)+1) #define LEVELREC_COOKIE_LEN (strlen(LEVELREC_COOKIE)+1) #define JOYSTICK_COOKIE_LEN (strlen(JOYSTICK_COOKIE)+1) +#define SETUP_COOKIE_LEN (strlen(SETUP_COOKIE)+1) #define VERSION_STRING "1.2" #define GAMETITLE_STRING "Rocks'n'Diamonds" diff --git a/src/misc.c b/src/misc.c index be689499..7a790ce1 100644 --- a/src/misc.c +++ b/src/misc.c @@ -24,6 +24,7 @@ #include "tools.h" #include "sound.h" #include "random.h" +#include "joystick.h" static unsigned long mainCounter(int mode) { @@ -392,118 +393,331 @@ void *checked_malloc(unsigned long size) return ptr; } -char *getKeySymName(KeySym key) +#define TRANSLATE_KEYSYM_TO_KEYNAME 0 +#define TRANSLATE_KEYSYM_TO_X11KEYNAME 1 +#define TRANSLATE_X11KEYNAME_TO_KEYSYM 2 + +void translate_keyname(KeySym *keysym, char **x11name, char **name, int mode) { - static char key_name[20]; static struct { KeySym keysym; + char *x11name; char *name; - } translate[] = + } translate_key[] = { /* normal cursor keys */ - { XK_Left, "cursor left" }, - { XK_Right, "cursor right" }, - { XK_Up, "cursor up" }, - { XK_Down, "cursor down" }, + { XK_Left, "XK_Left", "cursor left" }, + { XK_Right, "XK_Right", "cursor right" }, + { XK_Up, "XK_Up", "cursor up" }, + { XK_Down, "XK_Down", "cursor down" }, /* keypad cursor keys */ #ifdef XK_KP_Left - { XK_KP_Left, "keypad left" }, - { XK_KP_Right, "keypad right" }, - { XK_KP_Up, "keypad up" }, - { XK_KP_Down, "keypad down" }, + { XK_KP_Left, "XK_KP_Left", "keypad left" }, + { XK_KP_Right, "XK_KP_Right", "keypad right" }, + { XK_KP_Up, "XK_KP_Up", "keypad up" }, + { XK_KP_Down, "XK_KP_Down", "keypad down" }, #endif /* other keypad keys */ #ifdef XK_KP_Enter - { XK_KP_Enter, "keypad enter" }, - { XK_KP_Add, "keypad +" }, - { XK_KP_Subtract, "keypad -" }, - { XK_KP_Multiply, "keypad mltply" }, - { XK_KP_Divide, "keypad /" }, - { XK_KP_Separator, "keypad ," }, + { XK_KP_Enter, "XK_KP_Enter", "keypad enter" }, + { XK_KP_Add, "XK_KP_Add", "keypad +" }, + { XK_KP_Subtract, "XK_KP_Subtract", "keypad -" }, + { XK_KP_Multiply, "XK_KP_Multiply", "keypad mltply" }, + { XK_KP_Divide, "XK_KP_Divide", "keypad /" }, + { XK_KP_Separator, "XK_KP_Separator", "keypad ," }, #endif /* modifier keys */ - { XK_Shift_L, "left shift" }, - { XK_Shift_R, "right shift" }, - { XK_Control_L, "left control" }, - { XK_Control_R, "right control" }, - { XK_Meta_L, "left meta" }, - { XK_Meta_R, "right meta" }, - { XK_Alt_L, "left alt" }, - { XK_Alt_R, "right alt" }, - { XK_Mode_switch, "mode switch" }, - { XK_Multi_key, "multi key" }, + { XK_Shift_L, "XK_Shift_L", "left shift" }, + { XK_Shift_R, "XK_Shift_R", "right shift" }, + { XK_Control_L, "XK_Control_L", "left control" }, + { XK_Control_R, "XK_Control_R", "right control" }, + { XK_Meta_L, "XK_Meta_L", "left meta" }, + { XK_Meta_R, "XK_Meta_R", "right meta" }, + { XK_Alt_L, "XK_Alt_L", "left alt" }, + { XK_Alt_R, "XK_Alt_R", "right alt" }, + { XK_Mode_switch, "XK_Mode_switch", "mode switch" }, + { XK_Multi_key, "XK_Multi_key", "multi key" }, /* some special keys */ - { XK_BackSpace, "backspace" }, - { XK_Delete, "delete" }, - { XK_Insert, "insert" }, - { XK_Tab, "tab" }, - { XK_Home, "home" }, - { XK_End, "end" }, - { XK_Page_Up, "page up" }, - { XK_Page_Down, "page down" }, - { XK_space, "space" }, + { XK_BackSpace, "XK_BackSpace", "backspace" }, + { XK_Delete, "XK_Delete", "delete" }, + { XK_Insert, "XK_Insert", "insert" }, + { XK_Tab, "XK_Tab", "tab" }, + { XK_Home, "XK_Home", "home" }, + { XK_End, "XK_End", "end" }, + { XK_Page_Up, "XK_Page_Up", "page up" }, + { XK_Page_Down, "XK_Page_Down", "page down" }, + { XK_space, "XK_space", "space" }, /* even more special keys */ - { XK_adiaeresis, "ä" }, - { XK_odiaeresis, "ö" }, - { XK_udiaeresis, "ü" }, - { XK_apostrophe, "'" }, - { XK_plus, "+" }, - { XK_minus, "-" }, - { XK_comma, "," }, - { XK_period, "." }, - { XK_numbersign, "#" }, - { XK_less, "less" }, - { XK_greater, "greater" }, - { XK_asciicircum, "circumflex" }, - { XK_ssharp, "sharp s" }, + { XK_adiaeresis, "XK_adiaeresis", "ä" }, + { XK_odiaeresis, "XK_odiaeresis", "ö" }, + { XK_udiaeresis, "XK_udiaeresis", "ü" }, + { XK_apostrophe, "XK_apostrophe", "'" }, + { XK_plus, "XK_plus", "+" }, + { XK_minus, "XK_minus", "-" }, + { XK_comma, "XK_comma", "," }, + { XK_period, "XK_period", "." }, + { XK_numbersign, "XK_numbersign", "#" }, + { XK_less, "XK_less", "less" }, + { XK_greater, "XK_greater", "greater" }, + { XK_asciicircum, "XK_asciicircum", "circumflex" }, + { XK_ssharp, "XK_ssharp", "sharp s" }, /* end-of-array identifier */ - { 0, NULL } + { 0, NULL, NULL } }; - if (key >= XK_A && key <= XK_Z) + int i; + + if (mode == TRANSLATE_KEYSYM_TO_KEYNAME) { - sprintf(key_name, "%c", 'A' + (char)(key - XK_A)); - return key_name; + static char name_buffer[30]; + KeySym key = *keysym; + + if (key >= XK_A && key <= XK_Z) + sprintf(name_buffer, "%c", 'A' + (char)(key - XK_A)); + else if (key >= XK_a && key <= XK_z) + sprintf(name_buffer, "%c", 'a' + (char)(key - XK_a)); + else if (key >= XK_0 && key <= XK_9) + sprintf(name_buffer, "%c", '0' + (char)(key - XK_0)); + else if (key >= XK_KP_0 && key <= XK_KP_9) + sprintf(name_buffer, "keypad %c", '0' + (char)(key - XK_KP_0)); + else if (key >= XK_F1 && key <= XK_F24) + sprintf(name_buffer, "function F%d", (int)(key - XK_F1 + 1)); + else if (key == KEY_UNDEFINDED) + strcpy(name_buffer, "(undefined)"); + else + { + i = 0; + + do + { + if (key == translate_key[i].keysym) + { + strcpy(name_buffer, translate_key[i].name); + break; + } + } + while (translate_key[++i].name); + + if (!translate_key[i].name) + strcpy(name_buffer, "(unknown)"); + } + + *name = name_buffer; } - else if (key >= XK_a && key <= XK_z) + else if (mode == TRANSLATE_KEYSYM_TO_X11KEYNAME) { - sprintf(key_name, "%c", 'a' + (char)(key - XK_a)); - return key_name; + static char name_buffer[30]; + KeySym key = *keysym; + + if (key >= XK_A && key <= XK_Z) + sprintf(name_buffer, "XK_%c", 'A' + (char)(key - XK_A)); + else if (key >= XK_a && key <= XK_z) + sprintf(name_buffer, "XK_%c", 'a' + (char)(key - XK_a)); + else if (key >= XK_0 && key <= XK_9) + sprintf(name_buffer, "XK_%c", '0' + (char)(key - XK_0)); + else if (key >= XK_KP_0 && key <= XK_KP_9) + sprintf(name_buffer, "XK_KP_%c", '0' + (char)(key - XK_KP_0)); + else if (key >= XK_F1 && key <= XK_F24) + sprintf(name_buffer, "XK_F%d", (int)(key - XK_F1 + 1)); + else if (key == KEY_UNDEFINDED) + strcpy(name_buffer, "[undefined]"); + else + { + i = 0; + + do + { + if (key == translate_key[i].keysym) + { + strcpy(name_buffer, translate_key[i].x11name); + break; + } + } + while (translate_key[++i].x11name); + + if (!translate_key[i].x11name) + sprintf(name_buffer, "0x%04lx", (unsigned long)key); + } + + *x11name = name_buffer; } - else if (key >= XK_0 && key <= XK_9) + else if (mode == TRANSLATE_X11KEYNAME_TO_KEYSYM) { - sprintf(key_name, "%c", '0' + (char)(key - XK_0)); - return key_name; + KeySym key = XK_VoidSymbol; + char *name_ptr = *x11name; + + if (strncmp(name_ptr, "XK_", 3) == 0 && strlen(name_ptr) == 4) + { + char c = name_ptr[3]; + + if (c >= 'A' && c <= 'Z') + key = XK_A + (KeySym)(c - 'A'); + else if (c >= 'a' && c <= 'z') + key = XK_a + (KeySym)(c - 'a'); + else if (c >= '0' && c <= '9') + key = XK_0 + (KeySym)(c - '0'); + } + else if (strncmp(name_ptr, "XK_KP_", 6) == 0 && strlen(name_ptr) == 7) + { + char c = name_ptr[6]; + + if (c >= '0' && c <= '9') + key = XK_0 + (KeySym)(c - '0'); + } + else if (strncmp(name_ptr, "XK_F", 4) == 0 && strlen(name_ptr) <= 6) + { + char c1 = name_ptr[4]; + char c2 = name_ptr[5]; + int d = 0; + + if ((c1 >= '0' && c1 <= '9') && + ((c2 >= '0' && c1 <= '9') || c2 == '\0')) + d = atoi(&name_ptr[4]); + + if (d >=1 && d <= 24) + key = XK_F1 + (KeySym)(d - 1); + } + else if (strncmp(name_ptr, "XK_", 3) == 0) + { + i = 0; + + do + { + if (strcmp(name_ptr, translate_key[i].x11name) == 0) + { + key = translate_key[i].keysym; + break; + } + } + while (translate_key[++i].x11name); + } + else if (strncmp(name_ptr, "0x", 2) == 0) + { + unsigned long value = 0; + + name_ptr += 2; + + while (name_ptr) + { + char c = *name_ptr++; + int d = -1; + + if (c >= '0' && c <= '9') + d = (int)(c - '0'); + else if (c >= 'a' && c <= 'f') + d = (int)(c - 'a' + 10); + else if (c >= 'A' && c <= 'F') + d = (int)(c - 'A' + 10); + + if (d == -1) + { + value = -1; + break; + } + + value = value * 16 + d; + } + + if (value != -1) + key = (KeySym)value; + } + + *keysym = key; } - else if (key >= XK_KP_0 && key <= XK_KP_9) +} + +char *getKeyNameFromKeySym(KeySym keysym) +{ + char *name; + + translate_keyname(&keysym, NULL, &name, TRANSLATE_KEYSYM_TO_KEYNAME); + return name; +} + +char *getX11KeyNameFromKeySym(KeySym keysym) +{ + char *x11name; + + translate_keyname(&keysym, &x11name, NULL, TRANSLATE_KEYSYM_TO_X11KEYNAME); + return x11name; +} + +KeySym getKeySymFromX11KeyName(char *x11name) +{ + KeySym keysym; + + translate_keyname(&keysym, &x11name, NULL, TRANSLATE_X11KEYNAME_TO_KEYSYM); + return keysym; +} + +#define TRANSLATE_JOYSYMBOL_TO_JOYNAME 0 +#define TRANSLATE_JOYNAME_TO_JOYSYMBOL 1 + +void translate_joyname(int *joysymbol, char **name, int mode) +{ + static struct { - sprintf(key_name, "keypad %c", '0' + (char)(key - XK_KP_0)); - return key_name; - } - else if (key >= XK_F1 && key <= XK_F24) + int joysymbol; + char *name; + } translate_joy[] = { - sprintf(key_name, "function F%d", (int)(key - XK_F1 + 1)); - return key_name; - } - else + { JOY_LEFT, "joystick_left" }, + { JOY_RIGHT, "joystick_right" }, + { JOY_UP, "joystick_up" }, + { JOY_DOWN, "joystick_down" }, + { JOY_BUTTON_1, "joystick_button_1" }, + { JOY_BUTTON_2, "joystick_button_2" }, + }; + + int i; + + if (mode == TRANSLATE_JOYSYMBOL_TO_JOYNAME) { - int i = 0; + *name = "[undefined]"; - do + for (i=0; i<6; i++) { - if (key == translate[i].keysym) - return translate[i].name; + if (*joysymbol == translate_joy[i].joysymbol) + { + *name = translate_joy[i].name; + break; + } } - while (translate[++i].name); + } + else if (mode == TRANSLATE_JOYNAME_TO_JOYSYMBOL) + { + *joysymbol = 0; - sprintf(key_name, "(unknown)"); - return key_name; + for (i=0; i<6; i++) + { + if (strcmp(*name, translate_joy[i].name) == 0) + { + *joysymbol = translate_joy[i].joysymbol; + break; + } + } } } + +char *getJoyNameFromJoySymbol(int joysymbol) +{ + char *name; + + translate_joyname(&joysymbol, &name, TRANSLATE_JOYSYMBOL_TO_JOYNAME); + return name; +} + +int getJoySymbolFromJoyName(char *name) +{ + int joysymbol; + + translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL); + return joysymbol; +} diff --git a/src/misc.h b/src/misc.h index 5c23eb76..2e979a09 100644 --- a/src/misc.h +++ b/src/misc.h @@ -41,6 +41,10 @@ void MarkTileDirty(int, int); void GetOptions(char **); void Error(int, char *, ...); void *checked_malloc(unsigned long); -char *getKeySymName(KeySym); +char *getKeyNameFromKeySym(KeySym); +char *getX11KeyNameFromKeySym(KeySym); +KeySym getKeySymFromX11KeyName(char *); +char *getJoyNameFromJoySymbol(int); +int getJoySymbolFromJoyName(char *); #endif diff --git a/src/screens.c b/src/screens.c index ed3ca8f9..ab56a797 100644 --- a/src/screens.c +++ b/src/screens.c @@ -1211,7 +1211,7 @@ static void drawPlayerSetupInputInfo(int player_nr) DrawText(SX + 3*32, SY + ypos*32, (setup.input[player_nr].use_joystick ? custom[i].text : - getKeySymName(*custom[i].keysym)), + getKeyNameFromKeySym(*custom[i].keysym)), FS_BIG, FC_YELLOW); } } @@ -1383,7 +1383,8 @@ void CustomizeKeyboard(int player_nr) DrawText(SX, SY + (2+2*step_nr+1)*32, "Key:", FS_BIG, FC_RED); DrawText(SX + 4*32, SY + (2+2*step_nr+1)*32, - getKeySymName(*customize_step[step_nr].keysym), FS_BIG, FC_BLUE); + getKeyNameFromKeySym(*customize_step[step_nr].keysym), + FS_BIG, FC_BLUE); while(!finished) { @@ -1422,7 +1423,7 @@ void CustomizeKeyboard(int player_nr) DrawText(SX + 4*32, SY + (2+2*step_nr+1)*32, " ", FS_BIG, FC_YELLOW); DrawText(SX + 4*32, SY + (2+2*step_nr+1)*32, - getKeySymName(key), FS_BIG, FC_YELLOW); + getKeyNameFromKeySym(key), FS_BIG, FC_YELLOW); step_nr++; /* un-highlight last query */ @@ -1445,7 +1446,7 @@ void CustomizeKeyboard(int player_nr) DrawText(SX, SY+(2+2*step_nr+1)*32, "Key:", FS_BIG, FC_RED); DrawText(SX + 4*32, SY+(2+2*step_nr+1)*32, - getKeySymName(*customize_step[step_nr].keysym), + getKeyNameFromKeySym(*customize_step[step_nr].keysym), FS_BIG, FC_BLUE); } break; -- 2.34.1