input: kill "merge with defaults" behavior
[libpicofe.git] / common / input.c
index a6d331e..2140461 100644 (file)
@@ -1,3 +1,13 @@
+/*
+ * (C) GraÅžvydas "notaz" Ignotas, 2008-2010
+ *
+ * This work is licensed under the terms of any of these licenses
+ * (at your option):
+ *  - GNU GPL, version 2 or later.
+ *  - GNU LGPL, version 2.1 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -213,8 +223,14 @@ void in_probe(void)
        int i;
 
        in_have_async_devs = 0;
-       for (i = 0; i < in_dev_count; i++)
-               in_devices[i].probed = 0;
+       for (i = 0; i < in_dev_count; i++) {
+               in_dev_t *dev = &in_devices[i];
+               if (dev->probed) {
+                       DRV(dev->drv_id).free(dev->drv_data);
+                       dev->drv_data = NULL;
+                       dev->probed = 0;
+               }
+       }
 
        for (i = 1; i < IN_DRVID_COUNT; i++)
                in_drivers[i].probe();
@@ -608,6 +624,28 @@ const char *in_get_key_name(int dev_id, int keycode)
        return xname;
 }
 
+int in_get_key_code(int dev_id, const char *key_name)
+{
+       in_dev_t *dev;
+       int i;
+
+       if (dev_id < 0)         /* want last used dev? */
+               dev_id = menu_last_used_dev;
+
+       if (dev_id < 0 || dev_id >= IN_MAX_DEVS)
+               return -1;
+
+       dev = &in_devices[dev_id];
+       if (dev->key_names == NULL)
+               return -1;
+
+       for (i = 0; i < dev->key_count; i++)
+               if (dev->key_names[i] && strcasecmp(dev->key_names[i], key_name) == 0)
+                       return i;
+
+       return -1;
+}
+
 int in_bind_key(int dev_id, int keycode, int mask, int bind_type, int force_unbind)
 {
        int ret, count;
@@ -645,22 +683,39 @@ int in_bind_key(int dev_id, int keycode, int mask, int bind_type, int force_unbi
        return 0;
 }
 
-void in_unbind_all(int dev_id, int act_mask, int bind_type)
+/*
+ * Unbind act_mask on binds with type bind_type
+ * - if dev_id_ < 0, affects all devices
+ *   else only affects dev_id_
+ * - if act_mask == -1, unbind all keys
+ *   else only actions in mask
+ */
+void in_unbind_all(int dev_id_, int act_mask, int bind_type)
 {
+       int dev_id = 0, dev_last = IN_MAX_DEVS - 1;
        int i, count;
        in_dev_t *dev;
 
-       if (dev_id < 0 || dev_id >= IN_MAX_DEVS || bind_type >= IN_BINDTYPE_COUNT)
+       if (dev_id_ >= 0)
+               dev_id = dev_last = dev_id_;
+
+       if (bind_type >= IN_BINDTYPE_COUNT)
                return;
 
-       dev = &in_devices[dev_id];
-       count = dev->key_count;
+       for (; dev_id <= dev_last; dev_id++) {
+               dev = &in_devices[dev_id];
+               count = dev->key_count;
 
-       if (dev->binds == NULL)
-               return;
+               if (dev->binds == NULL)
+                       continue;
 
-       for (i = 0; i < count; i++)
-               dev->binds[IN_BIND_OFFS(i, bind_type)] &= ~act_mask;
+               if (act_mask != -1) {
+                       for (i = 0; i < count; i++)
+                               dev->binds[IN_BIND_OFFS(i, bind_type)] &= ~act_mask;
+               }
+               else
+                       memset(dev->binds, 0, sizeof(dev->binds[0]) * count * IN_BINDTYPE_COUNT);
+       }
 }
 
 /* returns device id, or -1 on error */
@@ -715,32 +770,6 @@ int in_config_parse_dev(const char *name)
        return i;
 }
 
-/*
- * To reduce size of game specific configs, default binds are not saved.
- * So we mark default binds in in_config_start(), override them in in_config_bind_key(),
- * and restore whatever default binds are left in in_config_end().
- */
-void in_config_start(void)
-{
-       int i;
-
-       /* mark all default binds, so they get overwritten by func below */
-       for (i = 0; i < IN_MAX_DEVS; i++) {
-               int n, count, *binds, *def_binds;
-
-               binds = in_devices[i].binds;
-               if (binds == NULL)
-                       continue;
-
-               count = in_devices[i].key_count;
-               def_binds = binds + count * IN_BINDTYPE_COUNT;
-
-               for (n = 0; n < count * IN_BINDTYPE_COUNT; n++)
-                       if (binds[n] == def_binds[n])
-                               binds[n] = -1;
-       }
-}
-
 int in_config_bind_key(int dev_id, const char *key, int acts, int bind_type)
 {
        in_dev_t *dev;
@@ -763,7 +792,6 @@ int in_config_bind_key(int dev_id, const char *key, int acts, int bind_type)
                        dev->binds = in_alloc_binds(dev->drv_id, dev->key_count);
                        if (dev->binds == NULL)
                                return -1;
-                       in_config_start();
                }
 
                kc = -1;
@@ -803,37 +831,21 @@ int in_config_bind_key(int dev_id, const char *key, int acts, int bind_type)
        return 0;
 }
 
-void in_config_end(void)
+void in_clean_binds(void)
 {
        int i;
 
        for (i = 0; i < IN_MAX_DEVS; i++) {
-               int n, t, ret, count, *binds, *def_binds;
+               int ret, count, *binds, *def_binds;
                in_dev_t *dev = &in_devices[i];
 
-               if (dev->binds == NULL)
+               if (dev->binds == NULL || dev->drv_data == NULL)
                        continue;
 
                count = dev->key_count;
                binds = dev->binds;
                def_binds = binds + count * IN_BINDTYPE_COUNT;
 
-               for (n = 0; n < count; n++) {
-                       int is_default = 1;
-                       for (t = 0; t < IN_BINDTYPE_COUNT; t++)
-                               if (binds[IN_BIND_OFFS(n, t)] == -1)
-                                       binds[IN_BIND_OFFS(n, t)] = 0;
-                               else
-                                       is_default = 0;
-
-                       if (is_default)
-                               for (t = 0; t < IN_BINDTYPE_COUNT; t++)
-                                       binds[IN_BIND_OFFS(n, t)] = def_binds[IN_BIND_OFFS(n, t)];
-               }
-
-               if (dev->drv_data == NULL)
-                       continue;
-
                ret = DRV(dev->drv_id).clean_binds(dev->drv_data, binds, def_binds);
                if (ret == 0) {
                        /* no useable binds */