diff --git a/polymer/eduke32/source/config.c b/polymer/eduke32/source/config.c index e0b385adb..eb917f3fb 100644 --- a/polymer/eduke32/source/config.c +++ b/polymer/eduke32/source/config.c @@ -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 _settings.cfg Bfprintf(fp,"unbindall\n"); for (i=0; icmdstr); + 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>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); diff --git a/polymer/eduke32/source/osdcmds.c b/polymer/eduke32/source/osdcmds.c index 7f335bf46..85dea9790 100644 --- a/polymer/eduke32/source/osdcmds.c +++ b/polymer/eduke32/source/osdcmds.c @@ -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; inumparms >= 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= 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); diff --git a/polymer/eduke32/source/osdcmds.h b/polymer/eduke32/source/osdcmds.h index c4adfdfc4..efb7c6efb 100644 --- a/polymer/eduke32/source/osdcmds.h +++ b/polymer/eduke32/source/osdcmds.h @@ -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__