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.
8 * See the COPYING file in the top-level directory.
16 #include "config_file.h"
19 #define array_size(x) (sizeof(x) / sizeof(x[0]))
21 static char *mystrip(char *str)
26 for (i = 0; i < len; i++)
27 if (str[i] != ' ') break;
28 if (i > 0) memmove(str, str + i, len - i + 1);
31 for (i = len - 1; i >= 0; i--)
32 if (str[i] != ' ' && str[i] != '\r' && str[i] != '\n') break;
38 static void get_line(char *d, size_t size, const char *s)
43 for (pe = s; *pe != '\r' && *pe != '\n' && *pe != 0; pe++)
54 void config_write_keys(FILE *f)
58 for (d = 0; d < IN_MAX_DEVS; d++)
60 const int *binds = in_get_dev_binds(d);
61 const char *name = in_get_dev_name(d, 0, 0);
64 if (binds == NULL || name == NULL)
67 fprintf(f, "binddev = %s\n", name);
68 in_get_config(d, IN_CFG_BIND_COUNT, &count);
70 for (k = 0; k < count; k++)
76 name = in_get_key_name(d, k);
78 kbinds = binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER12)];
79 for (i = 0; kbinds && me_ctrl_actions[i].name != NULL; i++) {
80 mask = me_ctrl_actions[i].mask;
82 strncpy(act, me_ctrl_actions[i].name, 31);
83 fprintf(f, "bind %s = player1 %s\n", name, mystrip(act));
86 mask = me_ctrl_actions[i].mask << 16;
88 strncpy(act, me_ctrl_actions[i].name, 31);
89 fprintf(f, "bind %s = player2 %s\n", name, mystrip(act));
94 kbinds = binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)];
95 for (i = 0; kbinds && emuctrl_actions[i].name != NULL; i++) {
96 mask = emuctrl_actions[i].mask;
98 strncpy(act, emuctrl_actions[i].name, 31);
99 fprintf(f, "bind %s = %s\n", name, mystrip(act));
106 for (k = 0; k < array_size(in_adev); k++)
109 fprintf(f, "bind_analog = %d\n", k);
115 static int parse_bind_val(const char *val, int *type)
119 *type = IN_BINDTYPE_NONE;
123 if (strncasecmp(val, "player", 6) == 0)
125 int player, shift = 0;
126 player = atoi(val + 6) - 1;
128 if ((unsigned int)player > 1)
133 *type = IN_BINDTYPE_PLAYER12;
134 for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
135 if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
136 return me_ctrl_actions[i].mask << shift;
139 for (i = 0; emuctrl_actions[i].name != NULL; i++) {
140 if (strncasecmp(emuctrl_actions[i].name, val, strlen(val)) == 0) {
141 *type = IN_BINDTYPE_EMU;
142 return emuctrl_actions[i].mask;
149 void config_read_keys(const char *cfg)
151 char dev[256], key[128], *act;
157 while (p != NULL && (p = strstr(p, "binddev = ")) != NULL) {
160 get_line(dev, sizeof(dev), p);
161 dev_id = in_config_parse_dev(dev);
163 printf("input: can't handle dev: %s\n", dev);
167 in_unbind_all(dev_id, -1, -1);
168 while ((p = strstr(p, "bind"))) {
169 if (strncmp(p, "binddev = ", 10) == 0)
173 if (strncmp(p, "bind_analog", 11) == 0) {
174 int ret = sscanf(p, "bind_analog = %d", &bind);
177 printf("input: parse error: %16s..\n", p);
180 if ((unsigned int)bind >= array_size(in_adev)) {
181 printf("input: analog id %d out of range\n", bind);
184 in_adev[bind] = dev_id;
191 printf("input: parse error: %16s..\n", p);
195 get_line(key, sizeof(key), p);
196 act = strchr(key, '=');
198 printf("parse failed: %16s..\n", p);
206 bind = parse_bind_val(act, &bindtype);
207 if (bind != -1 && bind != 0) {
208 //printf("bind #%d '%s' %08x (%s)\n", dev_id, key, bind, act);
209 in_config_bind_key(dev_id, key, bind, bindtype);
212 lprintf("config: unhandled action \"%s\"\n", act);