GTK+: Human-readable bindings in config file.

This commit is contained in:
Brandon Wright 2018-10-30 19:26:01 -05:00
parent 8b49ab2f5f
commit e665f339f0
3 changed files with 65 additions and 9 deletions

View File

@ -172,6 +172,62 @@ Binding::is_negative (void)
return JOY_DIRECTION_UNMASK (value) == AXIS_NEG;
}
Binding::Binding (const char *raw_string)
{
value = 0;
if (!raw_string)
return;
char substr[80];
if (sscanf (raw_string, "Keyboard %79s", substr) == 1)
{
bool ctrl = false;
bool shift = false;
bool alt= false;
unsigned int keyval = 0;
char *key;
key = strtok (substr, "+");
while (key)
{
if (strstr (key, "Alt"))
alt = true;
else if (strstr (key, "Ctrl"))
ctrl = true;
else if (strstr (key, "Shift"))
shift = true;
else
{
keyval = gdk_keyval_from_name (key);
}
key = strtok (NULL, "+");
}
value = Binding(keyval, ctrl, shift, alt).value;
}
else if (!strncmp (raw_string, "Joystick", 8))
{
unsigned int axis;
unsigned int button;
unsigned int percent;
unsigned int device;
char posneg;
const char *substr = &raw_string[8];
if (sscanf (substr, "%u Axis #%u %c %u", &device, &axis, &posneg, &percent) == 4)
{
value = Binding(device - 1, JOY_AXIS (axis, posneg == '+' ? AXIS_POS : AXIS_NEG), percent).value;
}
else if (sscanf (substr, "%u Button %u", &device, &button) == 2)
{
value = Binding(device - 1, button, 0).value;
}
}
}
void
Binding::to_string (char *str)
{

View File

@ -5,7 +5,7 @@
#define AXIS_POS 1
#define AXIS_NEG 0
#define JOY_AXIS(axis, button) (512 + ((axis) * 2 + (button)))
#define JOY_AXIS(axis, direction) (512 + ((axis) * 2 + (direction)))
#define JOY_AXIS_UNMASK(bin) ((((bin) & 0x0000ffff) - 512) / 2)
#define JOY_DIRECTION_UNMASK(bin) ((((bin) & 0x0000ffff) - 512) % 2)
#define BINDING_KEY 0x10000000
@ -34,6 +34,7 @@ class Binding
Binding (GdkEventKey *event);
Binding (unsigned int);
Binding (void);
Binding (const char *str);
void to_string (char *str);
unsigned int hex (void);
unsigned int base_hex (void);

View File

@ -127,14 +127,11 @@ xml_out_float (xmlTextWriterPtr xml, const char *name, float value)
}
static void
xml_out_binding (xmlTextWriterPtr xml, const char *name, unsigned int value)
xml_out_binding (xmlTextWriterPtr xml, const char *name, const char *value)
{
char string[1024];
snprintf (string, 1024, "%u", value);
xmlTextWriterStartElement (xml, BAD_CAST ("binding"));
xmlTextWriterWriteAttribute (xml, BAD_CAST ("name"), BAD_CAST (name));
xmlTextWriterWriteAttribute (xml, BAD_CAST ("binding"), BAD_CAST (string));
xmlTextWriterWriteAttribute (xml, BAD_CAST ("binding"), BAD_CAST (value));
xmlTextWriterEndElement (xml);
}
@ -478,7 +475,8 @@ Snes9xConfig::save_config_file (void)
for (int j = 0; j < NUM_JOYPAD_LINKS; j++)
{
xml_out_binding (xml, b_links[j].snes9x_name, joypad[j].hex ());
joypad[j].to_string (buffer);
xml_out_binding (xml, b_links[j].snes9x_name, buffer);
}
xmlTextWriterEndElement (xml); /* joypad */
@ -486,9 +484,10 @@ Snes9xConfig::save_config_file (void)
for (int i = NUM_JOYPAD_LINKS; b_links[i].snes9x_name; i++)
{
shortcut[i - NUM_JOYPAD_LINKS].to_string (buffer);
xml_out_binding (xml,
b_links[i].snes9x_name,
shortcut[i - NUM_JOYPAD_LINKS].hex ());
buffer);
}
xmlTextWriterEndElement (xml); /* controls */
@ -1015,7 +1014,7 @@ Snes9xConfig::parse_binding (xmlNodePtr node, int joypad_number)
type = (char *) attr->children->content;
}
b = Binding ((unsigned int) strtoul (type, NULL, 10));
b = Binding (type);
if (joypad_number > -1 && joypad_number < NUM_JOYPAD_LINKS)
{