2008-08-30 21:01:57 +00:00
|
|
|
#include <stdlib.h> // atoi
|
|
|
|
#include <string.h> // strcmp
|
2008-08-27 19:50:33 +00:00
|
|
|
#include <engine/e_client_interface.h>
|
2008-08-27 15:48:50 +00:00
|
|
|
#include "binds.hpp"
|
|
|
|
|
2008-08-30 09:34:55 +00:00
|
|
|
bool BINDS::BINDS_SPECIAL::on_input(INPUT_EVENT e)
|
|
|
|
{
|
|
|
|
// don't handle invalid events and keys that arn't set to anything
|
2008-10-20 17:47:42 +00:00
|
|
|
if(e.key >= KEY_F1 && e.key <= KEY_F15 && binds->keybindings[e.key][0] != 0)
|
2008-08-30 09:34:55 +00:00
|
|
|
{
|
|
|
|
int stroke = 0;
|
|
|
|
if(e.flags&INPFLAG_PRESS)
|
|
|
|
stroke = 1;
|
|
|
|
console_execute_line_stroked(stroke, binds->keybindings[e.key]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
BINDS::BINDS()
|
|
|
|
{
|
|
|
|
mem_zero(keybindings, sizeof(keybindings));
|
2008-08-30 09:34:55 +00:00
|
|
|
special_binds.binds = this;
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BINDS::bind(int keyid, const char *str)
|
|
|
|
{
|
|
|
|
if(keyid < 0 && keyid >= KEY_LAST)
|
|
|
|
return;
|
|
|
|
|
|
|
|
str_copy(keybindings[keyid], str, sizeof(keybindings[keyid]));
|
|
|
|
if(!keybindings[keyid][0])
|
|
|
|
dbg_msg("binds", "unbound %s (%d)", inp_key_name(keyid), keyid);
|
|
|
|
else
|
|
|
|
dbg_msg("binds", "bound %s (%d) = %s", inp_key_name(keyid), keyid, keybindings[keyid]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool BINDS::on_input(INPUT_EVENT e)
|
|
|
|
{
|
|
|
|
// don't handle invalid events and keys that arn't set to anything
|
|
|
|
if(e.key <= 0 || e.key >= KEY_LAST || keybindings[e.key][0] == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int stroke = 0;
|
|
|
|
if(e.flags&INPFLAG_PRESS)
|
|
|
|
stroke = 1;
|
|
|
|
console_execute_line_stroked(stroke, keybindings[e.key]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BINDS::unbindall()
|
|
|
|
{
|
|
|
|
for(int i = 0; i < KEY_LAST; i++)
|
|
|
|
keybindings[i][0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *BINDS::get(int keyid)
|
|
|
|
{
|
|
|
|
if(keyid > 0 && keyid < KEY_LAST)
|
|
|
|
return keybindings[keyid];
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2008-09-25 14:04:02 +00:00
|
|
|
const char *BINDS::get_key(const char *bindstr)
|
|
|
|
{
|
|
|
|
for(int keyid = 0; keyid < KEY_LAST; keyid++)
|
|
|
|
{
|
|
|
|
const char *bind = get(keyid);
|
|
|
|
if(!bind[0])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(strcmp(bind, bindstr) == 0)
|
|
|
|
return inp_key_name(keyid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
void BINDS::set_defaults()
|
|
|
|
{
|
|
|
|
// set default key bindings
|
2008-08-30 21:01:57 +00:00
|
|
|
unbindall();
|
2008-08-27 15:48:50 +00:00
|
|
|
bind(KEY_F1, "toggle_local_console");
|
|
|
|
bind(KEY_F2, "toggle_remote_console");
|
|
|
|
bind(KEY_TAB, "+scoreboard");
|
|
|
|
bind(KEY_F10, "screenshot");
|
2008-10-20 17:47:42 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_NO_SDL
|
2008-08-27 15:48:50 +00:00
|
|
|
bind('A', "+left");
|
|
|
|
bind('D', "+right");
|
2008-10-20 17:47:42 +00:00
|
|
|
#else
|
|
|
|
bind('a', "+left");
|
|
|
|
bind('d', "+right");
|
|
|
|
#endif
|
2008-08-27 15:48:50 +00:00
|
|
|
bind(KEY_SPACE, "+jump");
|
|
|
|
bind(KEY_MOUSE_1, "+fire");
|
|
|
|
bind(KEY_MOUSE_2, "+hook");
|
|
|
|
bind(KEY_LSHIFT, "+emote");
|
|
|
|
|
|
|
|
bind('1', "+weapon1");
|
|
|
|
bind('2', "+weapon2");
|
|
|
|
bind('3', "+weapon3");
|
|
|
|
bind('4', "+weapon4");
|
|
|
|
bind('5', "+weapon5");
|
|
|
|
|
|
|
|
bind(KEY_MOUSE_WHEEL_UP, "+prevweapon");
|
|
|
|
bind(KEY_MOUSE_WHEEL_DOWN, "+nextweapon");
|
|
|
|
|
2008-10-20 17:47:42 +00:00
|
|
|
#ifdef CONFIG_NO_SDL
|
2008-08-27 15:48:50 +00:00
|
|
|
bind('T', "chat all");
|
|
|
|
bind('Y', "chat team");
|
2008-10-20 17:47:42 +00:00
|
|
|
#else
|
|
|
|
bind('t', "chat all");
|
|
|
|
bind('y', "chat team");
|
|
|
|
#endif
|
2008-09-25 14:04:02 +00:00
|
|
|
|
|
|
|
bind(KEY_F3, "vote yes");
|
|
|
|
bind(KEY_F4, "vote no");
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
|
|
|
|
2008-09-04 21:36:44 +00:00
|
|
|
void BINDS::on_console_init()
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2008-08-30 21:01:57 +00:00
|
|
|
// bindings
|
|
|
|
MACRO_REGISTER_COMMAND("bind", "sr", con_bind, this);
|
|
|
|
MACRO_REGISTER_COMMAND("unbind", "s", con_unbind, this);
|
|
|
|
MACRO_REGISTER_COMMAND("unbindall", "", con_unbindall, this);
|
|
|
|
MACRO_REGISTER_COMMAND("dump_binds", "", con_dump_binds, this);
|
|
|
|
|
|
|
|
// default bindings
|
2008-08-27 15:48:50 +00:00
|
|
|
set_defaults();
|
|
|
|
}
|
|
|
|
|
2008-08-30 21:01:57 +00:00
|
|
|
void BINDS::con_bind(void *result, void *user_data)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2008-08-30 21:01:57 +00:00
|
|
|
BINDS *binds = (BINDS *)user_data;
|
2008-08-27 15:48:50 +00:00
|
|
|
const char *key_name = console_arg_string(result, 0);
|
2008-08-30 21:01:57 +00:00
|
|
|
int id = binds->get_key_id(key_name);
|
2008-08-27 15:48:50 +00:00
|
|
|
|
|
|
|
if(!id)
|
|
|
|
{
|
|
|
|
dbg_msg("binds", "key %s not found", key_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-30 21:01:57 +00:00
|
|
|
binds->bind(id, console_arg_string(result, 1));
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-30 21:01:57 +00:00
|
|
|
void BINDS::con_unbind(void *result, void *user_data)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2008-08-30 21:01:57 +00:00
|
|
|
BINDS *binds = (BINDS *)user_data;
|
2008-08-27 15:48:50 +00:00
|
|
|
const char *key_name = console_arg_string(result, 0);
|
2008-08-30 21:01:57 +00:00
|
|
|
int id = binds->get_key_id(key_name);
|
2008-08-27 15:48:50 +00:00
|
|
|
|
|
|
|
if(!id)
|
|
|
|
{
|
|
|
|
dbg_msg("binds", "key %s not found", key_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-30 21:01:57 +00:00
|
|
|
binds->bind(id, "");
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-30 21:01:57 +00:00
|
|
|
void BINDS::con_unbindall(void *result, void *user_data)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2008-08-30 21:01:57 +00:00
|
|
|
BINDS *binds = (BINDS *)user_data;
|
|
|
|
binds->unbindall();
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-30 21:01:57 +00:00
|
|
|
void BINDS::con_dump_binds(void *result, void *user_data)
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2008-08-30 21:01:57 +00:00
|
|
|
BINDS *binds = (BINDS *)user_data;
|
2008-08-27 15:48:50 +00:00
|
|
|
for(int i = 0; i < KEY_LAST; i++)
|
|
|
|
{
|
2008-08-30 21:01:57 +00:00
|
|
|
if(binds->keybindings[i][0] == 0)
|
2008-08-27 15:48:50 +00:00
|
|
|
continue;
|
2008-08-30 21:01:57 +00:00
|
|
|
dbg_msg("binds", "%s (%d) = %s", inp_key_name(i), i, binds->keybindings[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int BINDS::get_key_id(const char *key_name)
|
|
|
|
{
|
|
|
|
// check for numeric
|
2008-09-25 14:04:02 +00:00
|
|
|
if(key_name[0] == '&')
|
2008-08-30 21:01:57 +00:00
|
|
|
{
|
|
|
|
int i = atoi(key_name+1);
|
|
|
|
if(i > 0 && i < KEY_LAST)
|
|
|
|
return i; // numeric
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
2008-08-30 21:01:57 +00:00
|
|
|
|
|
|
|
// search for key
|
|
|
|
for(int i = 0; i < KEY_LAST; i++)
|
|
|
|
{
|
|
|
|
if(strcmp(key_name, inp_key_name(i)) == 0)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
|
|
|
|
2008-09-04 21:36:44 +00:00
|
|
|
void BINDS::on_save()
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
|
|
|
char buffer[256];
|
|
|
|
char *end = buffer+sizeof(buffer)-8;
|
|
|
|
client_save_line("unbindall");
|
|
|
|
for(int i = 0; i < KEY_LAST; i++)
|
|
|
|
{
|
|
|
|
if(keybindings[i][0] == 0)
|
|
|
|
continue;
|
|
|
|
str_format(buffer, sizeof(buffer), "bind %s ", inp_key_name(i));
|
|
|
|
|
|
|
|
// process the string. we need to escape some characters
|
|
|
|
const char *src = keybindings[i];
|
|
|
|
char *dst = buffer + strlen(buffer);
|
|
|
|
*dst++ = '"';
|
|
|
|
while(*src && dst < end)
|
|
|
|
{
|
|
|
|
if(*src == '"' || *src == '\\') // escape \ and "
|
|
|
|
*dst++ = '\\';
|
|
|
|
*dst++ = *src++;
|
|
|
|
}
|
|
|
|
*dst++ = '"';
|
|
|
|
*dst++ = 0;
|
|
|
|
|
|
|
|
client_save_line(buffer);
|
|
|
|
}
|
|
|
|
}
|