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_EMU)];
96 for (i = 0; kbinds && emuctrl_actions[i].name != NULL; i++) {
97 mask = emuctrl_actions[i].mask;
99 strncpy(act, emuctrl_actions[i].name, 31);
100 fprintf(f, "bind %s = %s\n", name, mystrip(act));
107 for (k = 0; k < array_size(in_adev); k++)
110 fprintf(f, "bind_analog = %d\n", k);
116 static int parse_bind_val(const char *val, int *type)
120 *type = IN_BINDTYPE_NONE;
124 if (strncasecmp(val, "player", 6) == 0)
126 int player, shift = 0;
127 player = atoi(val + 6) - 1;
129 if ((unsigned int)player > 1)
134 *type = IN_BINDTYPE_PLAYER12;
135 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
136 if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
137 return me_ctrl_actions[i].mask << shift;
140 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
141 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
142 *type = IN_BINDTYPE_EMU;
143 return emuctrl_actions[i].mask;
150 void config_read_keys(const char *cfg_content)
152 char dev[256], key[128], *act;
158 while (p != NULL && (p = strstr(p, "binddev = ")) != NULL) {
161 get_line(dev, sizeof(dev), p);
162 dev_id = in_config_parse_dev(dev);
164 printf("input: can't handle dev: %s\n", dev);
168 in_unbind_all(dev_id, -1, -1);
169 while ((p = strstr(p, "bind"))) {
170 if (strncmp(p, "binddev = ", 10) == 0)
174 if (strncmp(p, "bind_analog", 11) == 0) {
175 int ret = sscanf(p, "bind_analog = %d", &bind);
178 printf("input: parse error: %16s..\n", p);
181 if ((unsigned int)bind >= array_size(in_adev)) {
182 printf("input: analog id %d out of range\n", bind);
185 in_adev[bind] = dev_id;
192 printf("input: parse error: %16s..\n", p);
196 get_line(key, sizeof(key), p);
197 act = strchr(key, '=');
199 printf("parse failed: %16s..\n", p);
207 bind = parse_bind_val(act, &bindtype);
208 if (bind != -1 && bind != 0) {
209 //printf("bind #%d '%s' %08x (%s)\n", dev_id, key, bind, act);
210 in_config_bind_key(dev_id, key, bind, bindtype);
213 lprintf("config: unhandled action \"%s\"\n", act);