summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/input.cpp
diff options
context:
space:
mode:
author Tomer Verona <tverona@hotmail.com>2016-09-12 18:35:36 -0700
committer Tomer Verona <tverona@hotmail.com>2016-09-12 18:35:36 -0700
commit2bd18d5fea66bedcde02da1b2c17d83c4640a782 (patch)
tree21a026c276f83cd6a3619d49ca85cba088485e5b /src/emu/input.cpp
parent597626d0593e84868d5d9887c1f44b96222bdd49 (diff)
Support for configuring device to conrtoller id
This change adds support for configuring device to conrtoller id. This allows for stable controller ids even if USB devices are plugged / unplugged, system is rebooted, etc. See documentation for additional context.
Diffstat (limited to 'src/emu/input.cpp')
-rw-r--r--src/emu/input.cpp103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/emu/input.cpp b/src/emu/input.cpp
index bbae55fcd34..560e9271797 100644
--- a/src/emu/input.cpp
+++ b/src/emu/input.cpp
@@ -941,6 +941,21 @@ void input_device::apply_steadykey() const
}
}
+//-------------------------------------------------
+// match_device_name - match device name via
+// substring search
+//-------------------------------------------------
+
+bool input_device::match_device_name(const char *devicename)
+{
+ std::string devicenameupper(devicename);
+ std::string nameupper(m_name);
+
+ strmakeupper(devicenameupper);
+ strmakeupper(nameupper);
+
+ return std::string::npos == nameupper.find(devicenameupper) ? false : true;
+}
//**************************************************************************
@@ -1020,6 +1035,29 @@ input_item_class input_class::standard_item_class(input_item_id itemid)
//-------------------------------------------------
+// remap_device_index - remaps device index by
+// mapping oldindex to newindex
+//-------------------------------------------------
+
+void input_class::remap_device_index(int oldindex, int newindex)
+{
+ assert(oldindex >= 0 && oldindex < DEVICE_INDEX_MAXIMUM);
+ assert(newindex >= 0 && newindex < DEVICE_INDEX_MAXIMUM);
+
+ // swap indexes in m_device array
+ m_device[oldindex].swap(m_device[newindex]);
+
+ // update device indexes
+ m_device[oldindex]->set_devindex(oldindex);
+ m_device[newindex]->set_devindex(newindex);
+
+ // update the maximum index found, since newindex may
+ // exceed current m_maxindex
+ m_maxindex = std::max(m_maxindex, newindex);
+}
+
+
+//-------------------------------------------------
// frame_callback - per-frame callback for various
// bookkeeping
//-------------------------------------------------
@@ -2058,6 +2096,71 @@ void input_manager::seq_from_tokens(input_seq &seq, const char *string)
}
}
+//-------------------------------------------------
+// map_device_to_controller - map device to
+// controller based on device map table
+//-------------------------------------------------
+
+bool input_manager::map_device_to_controller(const devicemap_table_type *devicemap_table)
+{
+ if (nullptr == devicemap_table)
+ return true;
+
+ for (devicemap_table_type::const_iterator it = devicemap_table->begin(); it != devicemap_table->end(); it++)
+ {
+ const char *devicename = it->first.c_str();
+ const char *controllername = it->second.c_str();
+
+ // tokenize the controller name into device class and index (i.e. controller name should be of the form "GUNCODE_1")
+ std::string token[2];
+ int numtokens;
+ const char *_token = controllername;
+ for (numtokens = 0; numtokens < ARRAY_LENGTH(token); )
+ {
+ // make a token up to the next underscore
+ char *score = (char *)strchr(_token, '_');
+ token[numtokens++].assign(_token, (score == nullptr) ? strlen(_token) : (score - _token));
+
+ // if we hit the end, we're done, else advance our pointer
+ if (score == nullptr)
+ break;
+ _token = score + 1;
+ }
+ if (2 != numtokens)
+ return false;
+
+ // first token should be the devclass
+ input_device_class devclass = input_device_class((*devclass_token_table)[strmakeupper(token[0]).c_str()]);
+ if (devclass == ~input_device_class(0))
+ return false;
+
+ // second token should be the devindex
+ int devindex = 0;
+ if (1 != sscanf(token[1].c_str(), "%d", &devindex))
+ return false;
+ devindex--;
+
+ if (devindex >= DEVICE_INDEX_MAXIMUM)
+ return false;
+
+ // enumerate through devices and look for a match
+ input_class *input_devclass = m_class[devclass];
+ for (int devnum = 0; devnum <= input_devclass->maxindex(); devnum++)
+ {
+ input_device *device = input_devclass->device(devnum);
+ if (device != nullptr && device->match_device_name(devicename))
+ {
+ // remap devindex
+ input_devclass->remap_device_index(device->devindex(), devindex);
+ osd_printf_info("Mapped device '%s' to %s #%d\n", device->name(), (*devclass_string_table)[input_devclass->devclass()], devindex);
+ break;
+ }
+ }
+ }
+
+ return true;
+}
+
//-------------------------------------------------
// set_global_joystick_map - set the joystick map