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(ud.config.KeyboardKeys, 0xff, sizeof(ud.config.KeyboardKeys));
Bmemset(&CONTROL_KeyBinds,0,sizeof(CONTROL_KeyBinds)); CONTROL_ClearAllBinds();
Bmemset(&CONTROL_MouseBinds,0,sizeof(CONTROL_MouseBinds));
for (i=0; i < (int32_t)(sizeof(keydefaults)/sizeof(keydefaults[0])); i+=3) 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 i, j, k;
int32_t ii[] = { key1, key2, oldkey1, oldkey2 }; int32_t ii[] = { key1, key2, oldkey1, oldkey2 };
char buf[64]; char buf[2*MAXGAMEFUNCLEN];
UNREFERENCED_PARAMETER(which); UNREFERENCED_PARAMETER(which);
// CONTROL_MapKey(which, key1, key2); // 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++) 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++) for (j=0; ConsoleKeys[j].name; j++)
if (ii[k] == ConsoleKeys[j].id) if (ii[k] == ConsoleKeys[j].id)
break; break;
if (ConsoleKeys[j].name)
CONTROL_KeyBinds[ii[k]].key=Bstrdup(ConsoleKeys[j].name);
CONTROL_KeyBinds[ii[k]].repeat = 1;
tempbuf[0] = 0; 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)); i = Bstrlen(tempbuf);
if (i >= 2)
Bstrncpyz(CONTROL_KeyBinds[ii[k]].cmdstr, tempbuf, Bstrlen(tempbuf)); {
tempbuf[i-2] = 0; // cut off the trailing "; "
i = Bstrlen(CONTROL_KeyBinds[ii[k]].cmdstr); CONTROL_BindKey(ii[k], tempbuf, 1, ConsoleKeys[j].name ? ConsoleKeys[j].name : "<?>");
if (i) }
CONTROL_KeyBinds[ii[k]].cmdstr[i-2] = 0; // cut off the trailing "; " 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"); Bfprintf(fp,"unbindall\n");
for (i=0; i<MAXBOUNDKEYS; i++) 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, Bfprintf(fp,"bind \"%s\"%s \"%s\"\n",CONTROL_KeyBinds[i].key,
CONTROL_KeyBinds[i].repeat?"":" norepeat",CONTROL_KeyBinds[i].cmdstr); CONTROL_KeyBinds[i].repeat?"":" norepeat",CONTROL_KeyBinds[i].cmdstr);
for (i=0; i<MAXMOUSEBUTTONS; i++) 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, Bfprintf(fp,"bind \"%s\"%s \"%s\"\n",CONTROL_MouseBinds[i].key,
CONTROL_MouseBinds[i].repeat?"":" norepeat",CONTROL_MouseBinds[i].cmdstr); 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_BindsEnabled = 0;
int32_t CONTROL_SmoothMouse = 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) static void CONTROL_GetMouseDelta(void)
{ {
int32_t x,y; int32_t x,y;
@ -177,7 +226,6 @@ void CONTROL_PrintKeyMap(void)
i, CONTROL_KeyMapping[i].key1, CONTROL_KeyMapping[i].key2); i, CONTROL_KeyMapping[i].key1, CONTROL_KeyMapping[i].key2);
} }
} }
#endif
void CONTROL_PrintControlFlag(int32_t which) void CONTROL_PrintControlFlag(int32_t which)
{ {
@ -207,6 +255,7 @@ void CONTROL_PrintAxes(void)
CONTROL_JoyAxesMap[i].minmap, CONTROL_JoyAxesMap[i].maxmap); CONTROL_JoyAxesMap[i].minmap, CONTROL_JoyAxesMap[i].maxmap);
} }
} }
#endif
void CONTROL_MapButton(int32_t whichfunction, int32_t whichbutton, int32_t doubleclicked, controldevice device) 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) void CONTROL_Shutdown(void)
{ {
int32_t i; if (!CONTROL_Started)
return;
if (!CONTROL_Started) return; CONTROL_ClearAllBinds();
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(); MOUSE_Shutdown();
uninitinput(); 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_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_SetAnalogAxisScale(int32_t whichaxis, int32_t axisscale, controldevice device);
void CONTROL_PrintKeyMap(void); //void CONTROL_PrintKeyMap(void);
void CONTROL_PrintControlFlag(int32_t which); //void CONTROL_PrintControlFlag(int32_t which);
void CONTROL_PrintAxes( void ); //void CONTROL_PrintAxes( void );
////////// KEY/MOUSE BIND STUFF //////////
#define MAXBOUNDKEYS MAXKEYBOARDSCAN #define MAXBOUNDKEYS MAXKEYBOARDSCAN
#define MAXMOUSEBUTTONS 10
typedef struct binding { typedef struct binding {
const char *key; const char *key; // always set to const char *
char *cmdstr; char *cmdstr; // alloc'd
char repeat; char repeat;
char laststate; char laststate;
} keybind; } keybind;
#define MAXMOUSEBUTTONS 10 // Direct use DEPRECATED:
extern keybind CONTROL_KeyBinds[MAXBOUNDKEYS], CONTROL_MouseBinds[MAXMOUSEBUTTONS]; extern keybind CONTROL_KeyBinds[MAXBOUNDKEYS], CONTROL_MouseBinds[MAXMOUSEBUTTONS];
extern int32_t CONTROL_BindsEnabled; 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); void CONTROL_ProcessBinds(void);
////////////////////
#define CONTROL_NUM_FLAGS 64 #define CONTROL_NUM_FLAGS 64
extern int32_t CONTROL_OSDInput[CONTROL_NUM_FLAGS]; extern int32_t CONTROL_OSDInput[CONTROL_NUM_FLAGS];
extern int32_t CONTROL_SmoothMouse; extern int32_t CONTROL_SmoothMouse;

View file

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

View file

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