Fix bound keys' commands being truncated in settings.cfg, control* cleanup.

- provide functions instead of messing with CONTROL_*Binds directly
- comment out a few more unused functions
- make clear what memory (alloc'd or const char *) 'keybind' members use
- for keys with no name, use "<?>"

git-svn-id: https://svn.eduke32.com/eduke32@3209 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2012-11-18 15:44:18 +00:00
parent 878e41c34b
commit d8cd41fa94
6 changed files with 121 additions and 97 deletions

View File

@ -151,8 +151,7 @@ void CONFIG_SetDefaultKeys(const char (*keyptr)[MAXGAMEFUNCLEN])
Bmemset(ud.config.KeyboardKeys, 0xff, sizeof(ud.config.KeyboardKeys));
Bmemset(&CONTROL_KeyBinds,0,sizeof(CONTROL_KeyBinds));
Bmemset(&CONTROL_MouseBinds,0,sizeof(CONTROL_MouseBinds));
CONTROL_ClearAllBinds();
for (i=0; i < (int32_t)(sizeof(keydefaults)/sizeof(keydefaults[0])); i+=3)
{
@ -331,7 +330,7 @@ void CONFIG_MapKey(int32_t which, kb_scancode key1, kb_scancode oldkey1, kb_scan
{
int32_t i, j, k;
int32_t ii[] = { key1, key2, oldkey1, oldkey2 };
char buf[64];
char buf[2*MAXGAMEFUNCLEN];
UNREFERENCED_PARAMETER(which);
// CONTROL_MapKey(which, key1, key2);
@ -341,15 +340,12 @@ void CONFIG_MapKey(int32_t which, kb_scancode key1, kb_scancode oldkey1, kb_scan
for (k = 0; (unsigned)k < (sizeof(ii) / sizeof(ii[0])); k++)
{
if (ii[k] == 0xff || !ii[k]) continue;
if (ii[k] == 0xff || !ii[k])
continue;
for (j=0; ConsoleKeys[j].name; j++)
if (ii[k] == ConsoleKeys[j].id)
break;
if (ConsoleKeys[j].name)
CONTROL_KeyBinds[ii[k]].key=Bstrdup(ConsoleKeys[j].name);
CONTROL_KeyBinds[ii[k]].repeat = 1;
tempbuf[0] = 0;
@ -362,13 +358,16 @@ void CONFIG_MapKey(int32_t which, kb_scancode key1, kb_scancode oldkey1, kb_scan
}
}
CONTROL_KeyBinds[ii[k]].cmdstr = (char *)Brealloc(CONTROL_KeyBinds[ii[k]].cmdstr, Bstrlen(tempbuf));
Bstrncpyz(CONTROL_KeyBinds[ii[k]].cmdstr, tempbuf, Bstrlen(tempbuf));
i = Bstrlen(CONTROL_KeyBinds[ii[k]].cmdstr);
if (i)
CONTROL_KeyBinds[ii[k]].cmdstr[i-2] = 0; // cut off the trailing "; "
i = Bstrlen(tempbuf);
if (i >= 2)
{
tempbuf[i-2] = 0; // cut off the trailing "; "
CONTROL_BindKey(ii[k], tempbuf, 1, ConsoleKeys[j].name ? ConsoleKeys[j].name : "<?>");
}
else
{
CONTROL_FreeKeyBind(ii[k]);
}
}
}
@ -736,12 +735,12 @@ 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].cmdstr && CONTROL_KeyBinds[i].key)
if (CONTROL_KeyIsBound(i))
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].cmdstr)
if (CONTROL_MouseIsBound(i))
Bfprintf(fp,"bind \"%s\"%s \"%s\"\n",CONTROL_MouseBinds[i].key,
CONTROL_MouseBinds[i].repeat?"":" norepeat",CONTROL_MouseBinds[i].cmdstr);

View File

@ -60,6 +60,55 @@ keybind CONTROL_MouseBinds[MAXMOUSEBUTTONS];
int32_t CONTROL_BindsEnabled = 0;
int32_t CONTROL_SmoothMouse = 0;
void CONTROL_ClearAllBinds(void)
{
int32_t i;
for (i=0; i<MAXBOUNDKEYS; i++)
CONTROL_FreeKeyBind(i);
for (i=0; i<MAXMOUSEBUTTONS; i++)
CONTROL_FreeMouseBind(i);
}
void CONTROL_BindKey(int32_t i, const char *cmd, int32_t repeat, const char *keyname)
{
keybind *kb = &CONTROL_KeyBinds[i];
Bfree(kb->cmdstr);
kb->cmdstr = Bstrdup(cmd);
kb->repeat = repeat;
kb->key = keyname;
}
void CONTROL_BindMouse(int32_t i, const char *cmd, int32_t repeat, const char *keyname)
{
keybind *mb = &CONTROL_MouseBinds[i];
Bfree(mb->cmdstr);
mb->cmdstr = Bstrdup(cmd);
mb->repeat = repeat;
mb->key = keyname;
}
void CONTROL_FreeKeyBind(int32_t i)
{
keybind *kb = &CONTROL_KeyBinds[i];
Bfree(kb->cmdstr);
kb->cmdstr = NULL;
kb->repeat = 0;
kb->key = NULL;
}
void CONTROL_FreeMouseBind(int32_t i)
{
keybind *mb = &CONTROL_MouseBinds[i];
Bfree(mb->cmdstr);
mb->cmdstr = NULL;
mb->repeat = 0;
mb->key = NULL;
}
static void CONTROL_GetMouseDelta(void)
{
int32_t x,y;
@ -177,7 +226,6 @@ void CONTROL_PrintKeyMap(void)
i, CONTROL_KeyMapping[i].key1, CONTROL_KeyMapping[i].key2);
}
}
#endif
void CONTROL_PrintControlFlag(int32_t which)
{
@ -207,6 +255,7 @@ void CONTROL_PrintAxes(void)
CONTROL_JoyAxesMap[i].minmap, CONTROL_JoyAxesMap[i].maxmap);
}
}
#endif
void CONTROL_MapButton(int32_t whichfunction, int32_t whichbutton, int32_t doubleclicked, controldevice device)
{
@ -826,23 +875,10 @@ int32_t CONTROL_Startup(controltype which, int32_t(*TimeFunction)(void), int32_t
void CONTROL_Shutdown(void)
{
int32_t i;
if (!CONTROL_Started)
return;
if (!CONTROL_Started) return;
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;
}
CONTROL_ClearAllBinds();
MOUSE_Shutdown();
uninitinput();

View File

@ -166,26 +166,48 @@ void CONTROL_MapAnalogAxis(int32_t whichaxis, int32_t whichanalog, controldevice
void CONTROL_MapDigitalAxis(int32_t whichaxis, int32_t whichfunction, int32_t direction, controldevice device);
void CONTROL_SetAnalogAxisScale(int32_t whichaxis, int32_t axisscale, controldevice device);
void CONTROL_PrintKeyMap(void);
void CONTROL_PrintControlFlag(int32_t which);
void CONTROL_PrintAxes( void );
//void CONTROL_PrintKeyMap(void);
//void CONTROL_PrintControlFlag(int32_t which);
//void CONTROL_PrintAxes( void );
////////// KEY/MOUSE BIND STUFF //////////
#define MAXBOUNDKEYS MAXKEYBOARDSCAN
#define MAXMOUSEBUTTONS 10
typedef struct binding {
const char *key;
char *cmdstr;
const char *key; // always set to const char *
char *cmdstr; // alloc'd
char repeat;
char laststate;
} keybind;
#define MAXMOUSEBUTTONS 10
// Direct use DEPRECATED:
extern keybind CONTROL_KeyBinds[MAXBOUNDKEYS], CONTROL_MouseBinds[MAXMOUSEBUTTONS];
extern int32_t CONTROL_BindsEnabled;
void CONTROL_ClearAllBinds(void);
void CONTROL_BindKey(int32_t i, const char *cmd, int32_t repeat, const char *keyname);
void CONTROL_BindMouse(int32_t i, const char *cmd, int32_t repeat, const char *keyname);
void CONTROL_FreeKeyBind(int32_t i);
void CONTROL_FreeMouseBind(int32_t i);
static inline int32_t CONTROL_KeyIsBound(int32_t i)
{
return (CONTROL_KeyBinds[i].cmdstr && CONTROL_KeyBinds[i].key);
}
static inline int32_t CONTROL_MouseIsBound(int32_t i)
{
return (CONTROL_MouseBinds[i].cmdstr && CONTROL_MouseBinds[i].key);
}
void CONTROL_ProcessBinds(void);
////////////////////
#define CONTROL_NUM_FLAGS 64
extern int32_t CONTROL_OSDInput[CONTROL_NUM_FLAGS];
extern int32_t CONTROL_SmoothMouse;

View File

@ -3973,21 +3973,13 @@ cheat_for_port_credits2:
{
ud.config.MouseFunctions[whichkey>>1][whichkey&1] = x;
CONTROL_MapButton(x, whichkey>>1, whichkey&1, controldevice_mouse);
if (CONTROL_MouseBinds[whichkey>>1].cmdstr)
{
Bfree(CONTROL_MouseBinds[whichkey>>1].cmdstr);
CONTROL_MouseBinds[whichkey>>1].cmdstr = NULL;
}
CONTROL_FreeMouseBind(whichkey>>1);
}
else
{
ud.config.MouseFunctions[whichkey-NUMDOUBLEMBTNS][0] = x;
CONTROL_MapButton(x, whichkey-NUMDOUBLEMBTNS, 0, controldevice_mouse);
if (CONTROL_MouseBinds[whichkey-NUMDOUBLEMBTNS].cmdstr)
{
Bfree(CONTROL_MouseBinds[whichkey-NUMDOUBLEMBTNS].cmdstr);
CONTROL_MouseBinds[whichkey-NUMDOUBLEMBTNS].cmdstr = NULL;
}
CONTROL_FreeMouseBind(whichkey-NUMDOUBLEMBTNS);
}
M_ChangeMenu(205);

View File

@ -789,7 +789,7 @@ static int32_t osdcmd_button(const osdfuncparm_t *parm)
return OSDCMD_OK;
}
keydef_t ConsoleKeys[]=
const keydef_t ConsoleKeys[]=
{
{ "Escape", 0x1 },
{ "1", 0x2 },
@ -896,7 +896,7 @@ keydef_t ConsoleKeys[]=
{0,0}
};
const char *ConsoleButtons[] =
const char *const ConsoleButtons[] =
{
"mouse1", "mouse2", "mouse3", "mouse4", "mwheelup",
"mwheeldn", "mouse5", "mouse6", "mouse7", "mouse8"
@ -904,7 +904,7 @@ const char *ConsoleButtons[] =
static int32_t osdcmd_bind(const osdfuncparm_t *parm)
{
int32_t i, j;
int32_t i, j, repeat;
if (parm->numparms==1 && !Bstrcasecmp(parm->parms[0],"showkeys"))
{
@ -920,8 +920,9 @@ static int32_t osdcmd_bind(const osdfuncparm_t *parm)
int32_t j=0;
OSD_Printf("Current key bindings:\n");
for (i=0; i<MAXBOUNDKEYS; i++)
if (CONTROL_KeyBinds[i].cmdstr && CONTROL_KeyBinds[i].key)
if (CONTROL_KeyIsBound(i))
{
j++;
OSD_Printf("%-9s %s\"%s\"\n", CONTROL_KeyBinds[i].key, CONTROL_KeyBinds[i].repeat?"":"norepeat ",
@ -929,7 +930,7 @@ static int32_t osdcmd_bind(const osdfuncparm_t *parm)
}
for (i=0; i<MAXMOUSEBUTTONS; i++)
if (CONTROL_MouseBinds[i].cmdstr && CONTROL_MouseBinds[i].key)
if (CONTROL_MouseIsBound(i))
{
j++;
OSD_Printf("%-9s %s\"%s\"\n", CONTROL_MouseBinds[i].key, CONTROL_MouseBinds[i].repeat?"":"norepeat ",
@ -964,10 +965,10 @@ static int32_t osdcmd_bind(const osdfuncparm_t *parm)
j = 1;
CONTROL_MouseBinds[i].repeat = 1;
repeat = 1;
if (parm->numparms >= 2 && !Bstrcasecmp(parm->parms[j],"norepeat"))
{
CONTROL_MouseBinds[i].repeat = 0;
repeat = 0;
j++;
}
@ -978,11 +979,8 @@ static int32_t osdcmd_bind(const osdfuncparm_t *parm)
Bstrcat(tempbuf,parm->parms[j++]);
}
CONTROL_MouseBinds[i].cmdstr = (char *)Brealloc(CONTROL_MouseBinds[i].cmdstr, Bstrlen(tempbuf));
CONTROL_BindMouse(i, tempbuf, repeat, ConsoleButtons[i]);
Bstrncpyz(CONTROL_MouseBinds[i].cmdstr, tempbuf, Bstrlen(tempbuf));
CONTROL_MouseBinds[i].key=ConsoleButtons[i];
if (!OSD_ParsingScript())
OSD_Printf("%s\n",parm->raw);
return OSDCMD_OK;
@ -990,18 +988,18 @@ 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].cmdstr);
if (CONTROL_KeyIsBound(ConsoleKeys[i].id))
OSD_Printf("%-9s %s\"%s\"\n", ConsoleKeys[i].name, CONTROL_KeyBinds[ConsoleKeys[i].id].repeat?"":"norepeat ",
CONTROL_KeyBinds[ConsoleKeys[i].id].cmdstr);
return OSDCMD_OK;
}
j = 1;
CONTROL_KeyBinds[ConsoleKeys[i].id].repeat = 1;
repeat = 1;
if (parm->numparms >= 2 && !Bstrcasecmp(parm->parms[j],"norepeat"))
{
CONTROL_KeyBinds[ConsoleKeys[i].id].repeat = 0;
repeat = 0;
j++;
}
@ -1012,10 +1010,7 @@ static int32_t osdcmd_bind(const osdfuncparm_t *parm)
Bstrcat(tempbuf,parm->parms[j++]);
}
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;
CONTROL_BindKey(ConsoleKeys[i].id, tempbuf, repeat, ConsoleKeys[i].name);
{
char *cp = tempbuf;
@ -1062,18 +1057,10 @@ static int32_t osdcmd_unbindall(const osdfuncparm_t *parm)
UNREFERENCED_PARAMETER(parm);
for (i=0; i<MAXBOUNDKEYS; i++)
if (CONTROL_KeyBinds[i].cmdstr)
{
Bfree(CONTROL_KeyBinds[i].cmdstr);
CONTROL_KeyBinds[i].cmdstr = NULL;
}
CONTROL_FreeKeyBind(i);
for (i=0; i<MAXMOUSEBUTTONS; i++)
if (CONTROL_MouseBinds[i].cmdstr)
{
Bfree(CONTROL_MouseBinds[i].cmdstr);
CONTROL_MouseBinds[i].cmdstr = NULL;
}
CONTROL_FreeMouseBind(i);
for (i=0; i<NUMGAMEFUNCTIONS; i++)
{
@ -1106,24 +1093,14 @@ static int32_t osdcmd_unbind(const osdfuncparm_t *parm)
if (i >= MAXMOUSEBUTTONS)
return OSDCMD_SHOWHELP;
CONTROL_MouseBinds[i].repeat = 0;
if (CONTROL_MouseBinds[i].cmdstr)
{
Bfree(CONTROL_MouseBinds[i].cmdstr);
CONTROL_MouseBinds[i].cmdstr = NULL;
}
CONTROL_FreeMouseBind(i);
OSD_Printf("unbound %s\n",ConsoleButtons[i]);
return OSDCMD_OK;
}
CONTROL_KeyBinds[ConsoleKeys[i].id].repeat = 0;
if (CONTROL_KeyBinds[ConsoleKeys[i].id].cmdstr)
{
Bfree(CONTROL_KeyBinds[ConsoleKeys[i].id].cmdstr);
CONTROL_KeyBinds[ConsoleKeys[i].id].cmdstr = NULL;
}
CONTROL_FreeKeyBind(ConsoleKeys[i].id);
OSD_Printf("unbound key %s\n",ConsoleKeys[i].name);

View File

@ -35,16 +35,14 @@ void onvideomodechange(int32_t newmode);
extern float r_ambientlight,r_ambientlightrecip;
#pragma pack(push,1)
// key bindings stuff
typedef struct {
const char *name;
int32_t id;
} keydef_t;
extern keydef_t ConsoleKeys[];
extern const char *ConsoleButtons[];
#pragma pack(pop)
extern const keydef_t ConsoleKeys[];
extern const char *const ConsoleButtons[];
#endif // __osdcmds_h__