Change key bindings to use dynamic allocation and remove the previous limit of 128 characters per key bind

git-svn-id: https://svn.eduke32.com/eduke32@3197 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2012-11-17 16:47:58 +00:00
parent a5ee23215d
commit 992de02101
6 changed files with 83 additions and 38 deletions

View file

@ -350,7 +350,7 @@ void CONFIG_MapKey(int32_t which, kb_scancode key1, kb_scancode oldkey1, kb_scan
CONTROL_KeyBinds[ii[k]].key=Bstrdup(ConsoleKeys[j].name);
CONTROL_KeyBinds[ii[k]].repeat = 1;
CONTROL_KeyBinds[ii[k]].cmd[0] = 0;
tempbuf[0] = 0;
for (i=NUMGAMEFUNCTIONS-1; i>=0; i--)
@ -362,11 +362,13 @@ void CONFIG_MapKey(int32_t which, kb_scancode key1, kb_scancode oldkey1, kb_scan
}
}
Bstrncpyz(CONTROL_KeyBinds[ii[k]].cmd, tempbuf, MAXBINDSTRINGLENGTH);
CONTROL_KeyBinds[ii[k]].cmdstr = (char *)Brealloc(CONTROL_KeyBinds[ii[k]].cmdstr, Bstrlen(tempbuf));
i = Bstrlen(CONTROL_KeyBinds[ii[k]].cmd);
Bstrncpyz(CONTROL_KeyBinds[ii[k]].cmdstr, tempbuf, Bstrlen(tempbuf));
i = Bstrlen(CONTROL_KeyBinds[ii[k]].cmdstr);
if (i)
CONTROL_KeyBinds[ii[k]].cmd[i-2] = 0; // cut off the trailing "; "
CONTROL_KeyBinds[ii[k]].cmdstr[i-2] = 0; // cut off the trailing "; "
}
}
@ -734,12 +736,14 @@ void CONFIG_WriteBinds(void) // save binds and aliases to <cfgname>_settings.cfg
Bfprintf(fp,"unbindall\n");
for (i=0; i<MAXBOUNDKEYS; i++)
if (CONTROL_KeyBinds[i].cmd[0] && CONTROL_KeyBinds[i].key)
Bfprintf(fp,"bind \"%s\"%s \"%s\"\n",CONTROL_KeyBinds[i].key,CONTROL_KeyBinds[i].repeat?"":" norepeat",CONTROL_KeyBinds[i].cmd);
if (CONTROL_KeyBinds[i].cmdstr && CONTROL_KeyBinds[i].key)
Bfprintf(fp,"bind \"%s\"%s \"%s\"\n",CONTROL_KeyBinds[i].key,
CONTROL_KeyBinds[i].repeat?"":" norepeat",CONTROL_KeyBinds[i].cmdstr);
for (i=0; i<MAXMOUSEBUTTONS; i++)
if (CONTROL_MouseBinds[i].cmd[0])
Bfprintf(fp,"bind \"%s\"%s \"%s\"\n",CONTROL_MouseBinds[i].key,CONTROL_MouseBinds[i].repeat?"":" norepeat",CONTROL_MouseBinds[i].cmd);
if (CONTROL_MouseBinds[i].cmdstr)
Bfprintf(fp,"bind \"%s\"%s \"%s\"\n",CONTROL_MouseBinds[i].key,
CONTROL_MouseBinds[i].repeat?"":" norepeat",CONTROL_MouseBinds[i].cmdstr);
for (symb=symbols; symb!=NULL; symb=symb->next)
if (symb->func == (void *)OSD_ALIAS)

View file

@ -118,10 +118,10 @@ int32_t CONTROL_KeyboardFunctionPressed(int32_t which)
if (!CONTROL_Flags[which].used) return FALSE;
if (CONTROL_KeyMapping[which].key1 != KEYUNDEFINED && !KeyBindings[CONTROL_KeyMapping[which].key1].cmd[0])
if (CONTROL_KeyMapping[which].key1 != KEYUNDEFINED && !KeyBindings[CONTROL_KeyMapping[which].key1].cmdstr)
key1 = KB_KeyDown[ CONTROL_KeyMapping[which].key1 ] ? TRUE : FALSE;
if (CONTROL_KeyMapping[which].key2 != KEYUNDEFINED && !KeyBindings[CONTROL_KeyMapping[which].key2].cmd[0])
if (CONTROL_KeyMapping[which].key2 != KEYUNDEFINED && !KeyBindings[CONTROL_KeyMapping[which].key2].cmdstr)
key2 = KB_KeyDown[ CONTROL_KeyMapping[which].key2 ] ? TRUE : FALSE;
return (key1 | key2);
@ -663,7 +663,7 @@ static void CONTROL_ButtonFunctionState(int32_t *p1)
do
{
if (!CONTROL_MouseBinds[i].cmd[0])
if (!CONTROL_MouseBinds[i].cmdstr)
{
j = CONTROL_MouseButtonMapping[i].doubleclicked;
if (j != KEYUNDEFINED)
@ -677,10 +677,10 @@ static void CONTROL_ButtonFunctionState(int32_t *p1)
if (!CONTROL_BindsEnabled)
continue;
if (CONTROL_MouseBinds[i].cmd[0] && CONTROL_MouseButtonState[i])
if (CONTROL_MouseBinds[i].cmdstr && CONTROL_MouseButtonState[i])
{
if (CONTROL_MouseBinds[i].repeat || (CONTROL_MouseBinds[i].laststate == 0))
OSD_Dispatch(CONTROL_MouseBinds[i].cmd);
OSD_Dispatch(CONTROL_MouseBinds[i].cmdstr);
}
CONTROL_MouseBinds[i].laststate = CONTROL_MouseButtonState[i];
}
@ -721,12 +721,10 @@ void CONTROL_ProcessBinds(void)
do
{
if (CONTROL_KeyBinds[i].cmd[0])
if (CONTROL_KeyBinds[i].cmdstr)
{
if (KB_KeyPressed(i) && (CONTROL_KeyBinds[i].repeat || (CONTROL_KeyBinds[i].laststate == 0)))
{
OSD_Dispatch(CONTROL_KeyBinds[i].cmd);
}
OSD_Dispatch(CONTROL_KeyBinds[i].cmdstr);
CONTROL_KeyBinds[i].laststate = KB_KeyPressed(i);
}
@ -830,9 +828,23 @@ int32_t CONTROL_Startup(controltype which, int32_t(*TimeFunction)(void), int32_t
void CONTROL_Shutdown(void)
{
int32_t i;
if (!CONTROL_Started) return;
CONTROL_JoyPresent = FALSE;
for (i=0; i<MAXBOUNDKEYS; i++)
if (CONTROL_KeyBinds[i].cmdstr)
{
Bfree(CONTROL_KeyBinds[i].cmdstr);
CONTROL_KeyBinds[i].cmdstr = NULL;
}
for (i=0; i<MAXMOUSEBUTTONS; i++)
if (CONTROL_MouseBinds[i].cmdstr)
{
Bfree(CONTROL_MouseBinds[i].cmdstr);
CONTROL_MouseBinds[i].cmdstr = NULL;
}
MOUSE_Shutdown();
uninitinput();

View file

@ -198,12 +198,11 @@ void CONTROL_PrintKeyMap(void);
void CONTROL_PrintControlFlag(int32_t which);
void CONTROL_PrintAxes( void );
#define MAXBINDSTRINGLENGTH 128
#define MAXBOUNDKEYS 256
typedef struct binding {
const char *key;
char cmd[MAXBINDSTRINGLENGTH];
char *cmdstr;
char repeat;
char laststate;
} keybind;

View file

@ -78,7 +78,7 @@ extern kb_scancode KB_LastScan;
#define KB_ClearKeyDown( scan ) { keystatus[ ( scan ) ] = FALSE; }
#define KB_UnBoundKeyPressed( scan ) ( keystatus[ ( scan ) ] != 0 && !CONTROL_KeyBinds[scan].cmd[0])
#define KB_UnBoundKeyPressed( scan ) ( keystatus[ ( scan ) ] != 0 && !CONTROL_KeyBinds[scan].cmdstr)
/*
=============================================================================

View file

@ -3973,13 +3973,22 @@ cheat_for_port_credits2:
{
ud.config.MouseFunctions[whichkey>>1][whichkey&1] = x;
CONTROL_MapButton(x, whichkey>>1, whichkey&1, controldevice_mouse);
CONTROL_MouseBinds[whichkey>>1].cmd[0] = 0; // kill the bind when changing the button in the menu
if (CONTROL_MouseBinds[whichkey>>1].cmdstr)
{
Bfree(CONTROL_MouseBinds[whichkey>>1].cmdstr);
CONTROL_MouseBinds[whichkey>>1].cmdstr = NULL;
}
}
else
{
ud.config.MouseFunctions[whichkey-NUMDOUBLEMBTNS][0] = x;
CONTROL_MapButton(x, whichkey-NUMDOUBLEMBTNS, 0, controldevice_mouse);
CONTROL_MouseBinds[whichkey-NUMDOUBLEMBTNS].cmd[0] = 0;
if (CONTROL_MouseBinds[whichkey-NUMDOUBLEMBTNS].cmdstr)
{
Bfree(CONTROL_MouseBinds[whichkey-NUMDOUBLEMBTNS].cmdstr);
CONTROL_MouseBinds[whichkey-NUMDOUBLEMBTNS].cmdstr = NULL;
}
}
M_ChangeMenu(205);
probey = whichkey;

View file

@ -921,19 +921,19 @@ static int32_t osdcmd_bind(const osdfuncparm_t *parm)
OSD_Printf("Current key bindings:\n");
for (i=0; i<MAXBOUNDKEYS; i++)
if (CONTROL_KeyBinds[i].cmd[0] && CONTROL_KeyBinds[i].key)
if (CONTROL_KeyBinds[i].cmdstr && CONTROL_KeyBinds[i].key)
{
j++;
OSD_Printf("%-9s %s\"%s\"\n", CONTROL_KeyBinds[i].key, CONTROL_KeyBinds[i].repeat?"":"norepeat ",
CONTROL_KeyBinds[i].cmd);
CONTROL_KeyBinds[i].cmdstr);
}
for (i=0; i<MAXMOUSEBUTTONS; i++)
if (CONTROL_MouseBinds[i].cmd[0] && CONTROL_MouseBinds[i].key)
if (CONTROL_MouseBinds[i].cmdstr && CONTROL_MouseBinds[i].key)
{
j++;
OSD_Printf("%-9s %s\"%s\"\n", CONTROL_MouseBinds[i].key, CONTROL_MouseBinds[i].repeat?"":"norepeat ",
CONTROL_MouseBinds[i].cmd);
CONTROL_MouseBinds[i].cmdstr);
}
if (j == 0)
@ -956,8 +956,9 @@ static int32_t osdcmd_bind(const osdfuncparm_t *parm)
if (parm->numparms < 2)
{
OSD_Printf("%-9s %s\"%s\"\n", ConsoleButtons[i], CONTROL_MouseBinds[i].repeat?"":"norepeat ",
CONTROL_MouseBinds[i].cmd);
if (CONTROL_MouseBinds[i].cmdstr && CONTROL_MouseBinds[i].key)
OSD_Printf("%-9s %s\"%s\"\n", ConsoleButtons[i], CONTROL_MouseBinds[i].repeat?"":"norepeat ",
CONTROL_MouseBinds[i].cmdstr);
return OSDCMD_OK;
}
@ -976,7 +977,10 @@ static int32_t osdcmd_bind(const osdfuncparm_t *parm)
Bstrcat(tempbuf," ");
Bstrcat(tempbuf,parm->parms[j++]);
}
Bstrncpy(CONTROL_MouseBinds[i].cmd,tempbuf, MAXBINDSTRINGLENGTH-1);
CONTROL_MouseBinds[i].cmdstr = (char *)Brealloc(CONTROL_MouseBinds[i].cmdstr, Bstrlen(tempbuf));
Bstrncpyz(CONTROL_MouseBinds[i].cmdstr, tempbuf, Bstrlen(tempbuf));
CONTROL_MouseBinds[i].key=ConsoleButtons[i];
if (!OSD_ParsingScript())
@ -986,8 +990,9 @@ static int32_t osdcmd_bind(const osdfuncparm_t *parm)
if (parm->numparms < 2)
{
if (CONTROL_KeyBinds[ConsoleKeys[i].id].cmdstr && CONTROL_KeyBinds[ConsoleKeys[i].id].key)
OSD_Printf("%-9s %s\"%s\"\n", ConsoleKeys[i].name, CONTROL_KeyBinds[ConsoleKeys[i].id].repeat?"":"norepeat ",
CONTROL_KeyBinds[ConsoleKeys[i].id].cmd);
CONTROL_KeyBinds[ConsoleKeys[i].id].cmdstr);
return OSDCMD_OK;
}
@ -1006,7 +1011,9 @@ static int32_t osdcmd_bind(const osdfuncparm_t *parm)
Bstrcat(tempbuf," ");
Bstrcat(tempbuf,parm->parms[j++]);
}
Bstrncpy(CONTROL_KeyBinds[ConsoleKeys[i].id].cmd,tempbuf, MAXBINDSTRINGLENGTH-1);
CONTROL_KeyBinds[ConsoleKeys[i].id].cmdstr = (char *)Brealloc(CONTROL_KeyBinds[ConsoleKeys[i].id].cmdstr, Bstrlen(tempbuf));
Bstrncpyz(CONTROL_KeyBinds[ConsoleKeys[i].id].cmdstr,tempbuf, Bstrlen(tempbuf));
CONTROL_KeyBinds[ConsoleKeys[i].id].key=ConsoleKeys[i].name;
@ -1055,12 +1062,18 @@ static int32_t osdcmd_unbindall(const osdfuncparm_t *parm)
UNREFERENCED_PARAMETER(parm);
for (i=0; i<MAXBOUNDKEYS; i++)
if (CONTROL_KeyBinds[i].cmd[0])
CONTROL_KeyBinds[i].cmd[0] = 0;
if (CONTROL_KeyBinds[i].cmdstr)
{
Bfree(CONTROL_KeyBinds[i].cmdstr);
CONTROL_KeyBinds[i].cmdstr = NULL;
}
for (i=0; i<MAXMOUSEBUTTONS; i++)
if (CONTROL_MouseBinds[i].cmd[0])
CONTROL_MouseBinds[i].cmd[0] = 0;
if (CONTROL_MouseBinds[i].cmdstr)
{
Bfree(CONTROL_MouseBinds[i].cmdstr);
CONTROL_MouseBinds[i].cmdstr = NULL;
}
for (i=0; i<NUMGAMEFUNCTIONS; i++)
{
@ -1094,7 +1107,11 @@ static int32_t osdcmd_unbind(const osdfuncparm_t *parm)
return OSDCMD_SHOWHELP;
CONTROL_MouseBinds[i].repeat = 0;
CONTROL_MouseBinds[i].cmd[0] = 0;
if (CONTROL_MouseBinds[i].cmdstr)
{
Bfree(CONTROL_MouseBinds[i].cmdstr);
CONTROL_MouseBinds[i].cmdstr = NULL;
}
OSD_Printf("unbound %s\n",ConsoleButtons[i]);
@ -1102,7 +1119,11 @@ static int32_t osdcmd_unbind(const osdfuncparm_t *parm)
}
CONTROL_KeyBinds[ConsoleKeys[i].id].repeat = 0;
CONTROL_KeyBinds[ConsoleKeys[i].id].cmd[0] = 0;
if (CONTROL_KeyBinds[ConsoleKeys[i].id].cmdstr)
{
Bfree(CONTROL_KeyBinds[ConsoleKeys[i].id].cmdstr);
CONTROL_KeyBinds[ConsoleKeys[i].id].cmdstr = NULL;
}
OSD_Printf("unbound key %s\n",ConsoleKeys[i].name);