2 * (C) GraÅžvydas "notaz" Ignotas, 2011-2012
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
9 * See the COPYING file in the top-level directory.
17 #include "config_file.h"
20 #define array_size(x) (sizeof(x) / sizeof(x[0]))
22 static char *mystrip(char *str)
27 for (i = 0; i < len; i++)
28 if (str[i] != ' ') break;
29 if (i > 0) memmove(str, str + i, len - i + 1);
32 for (i = len - 1; i >= 0; i--)
33 if (str[i] != ' ' && str[i] != '\r' && str[i] != '\n') break;
39 static void get_line(char *d, size_t size, const char *s)
44 for (pe = s; *pe != '\r' && *pe != '\n' && *pe != 0; pe++)
55 void config_write_keys(FILE *f)
59 for (d = 0; d < IN_MAX_DEVS; d++)
61 const int *binds = in_get_dev_binds(d);
62 const char *name = in_get_dev_name(d, 0, 0);
65 if (binds == NULL || name == NULL)
68 fprintf(f, "binddev = %s\n", name);
69 in_get_config(d, IN_CFG_BIND_COUNT, &count);
71 for (k = 0; k < count; k++)
77 name = in_get_key_name(d, k);
79 kbinds = binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)];
80 for (i = 0; kbinds && me_ctrl_actions[i].name != NULL; i++) {
81 mask = me_ctrl_actions[i].mask;
83 strncpy(act, me_ctrl_actions[i].name, 31);
84 fprintf(f, "bind %s = player1 %s\n", name, mystrip(act));
87 mask = me_ctrl_actions[i].mask << 16;
89 strncpy(act, me_ctrl_actions[i].name, 31);
90 fprintf(f, "bind %s = player2 %s\n", name, mystrip(act));
95 kbinds = binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER34)];
96 for (i = 0; kbinds && me_ctrl_actions[i].name != NULL; i++) {
97 mask = me_ctrl_actions[i].mask;
99 strncpy(act, me_ctrl_actions[i].name, 31);
100 fprintf(f, "bind %s = player3 %s\n", name, mystrip(act));
103 mask = me_ctrl_actions[i].mask << 16;
105 strncpy(act, me_ctrl_actions[i].name, 31);
106 fprintf(f, "bind %s = player4 %s\n", name, mystrip(act));
111 kbinds = binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)];
112 for (i = 0; kbinds && emuctrl_actions[i].name != NULL; i++) {
113 mask = emuctrl_actions[i].mask;
115 strncpy(act, emuctrl_actions[i].name, 31);
116 fprintf(f, "bind %s = %s\n", name, mystrip(act));
123 for (k = 0; k < array_size(in_adev); k++)
126 fprintf(f, "bind_analog = %d\n", k);
132 static int parse_bind_val(const char *val, int *type)
136 *type = IN_BINDTYPE_NONE;
140 if (strncasecmp(val, "player", 6) == 0)
142 int player, shift = 0;
143 player = atoi(val + 6) - 1;
145 if ((unsigned int)player > 3)
150 *type = IN_BINDTYPE_PLAYER12 + (player >> 1);
151 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
152 if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
153 return me_ctrl_actions[i].mask << shift;
156 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
157 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
158 *type = IN_BINDTYPE_EMU;
159 return emuctrl_actions[i].mask;
166 void config_read_keys(const char *cfg_content)
168 char dev[256], key[128], *act;
174 while (p != NULL && (p = strstr(p, "binddev = ")) != NULL) {
177 get_line(dev, sizeof(dev), p);
178 dev_id = in_config_parse_dev(dev);
180 printf("input: can't handle dev: %s\n", dev);
184 in_unbind_all(dev_id, -1, -1);
185 while ((p = strstr(p, "bind"))) {
186 if (strncmp(p, "binddev = ", 10) == 0)
190 if (strncmp(p, "bind_analog", 11) == 0) {
191 int ret = sscanf(p, "bind_analog = %d", &bind);
194 printf("input: parse error: %16s..\n", p);
197 if ((unsigned int)bind >= array_size(in_adev)) {
198 printf("input: analog id %d out of range\n", bind);
201 in_adev[bind] = dev_id;
208 printf("input: parse error: %16s..\n", p);
212 get_line(key, sizeof(key), p);
213 act = strchr(key, '=');
215 printf("parse failed: %16s..\n", p);
223 bind = parse_bind_val(act, &bindtype);
224 if (bind != -1 && bind != 0) {
225 //printf("bind #%d '%s' %08x (%s)\n", dev_id, key, bind, act);
226 in_config_bind_key(dev_id, key, bind, bindtype);
229 lprintf("config: unhandled action \"%s\"\n", act);