avoid duplicate device names

This commit is contained in:
vaxerski 2022-12-03 20:36:52 +00:00
parent 6aa26582f6
commit 409ac12f23
2 changed files with 31 additions and 4 deletions

View file

@ -465,7 +465,7 @@ void CInputManager::newKeyboard(wlr_input_device* keyboard) {
PNEWKEYBOARD->keyboard = keyboard; PNEWKEYBOARD->keyboard = keyboard;
try { try {
PNEWKEYBOARD->name = deviceNameToInternalString(keyboard->name); PNEWKEYBOARD->name = getNameForNewDevice(keyboard->name);
} catch (std::exception& e) { } catch (std::exception& e) {
Debug::log(ERR, "Keyboard had no name???"); // logic error Debug::log(ERR, "Keyboard had no name???"); // logic error
} }
@ -501,7 +501,7 @@ void CInputManager::newVirtualKeyboard(wlr_input_device* keyboard) {
PNEWKEYBOARD->isVirtual = true; PNEWKEYBOARD->isVirtual = true;
try { try {
PNEWKEYBOARD->name = deviceNameToInternalString(keyboard->name); PNEWKEYBOARD->name = getNameForNewDevice(keyboard->name);
} catch (std::exception& e) { } catch (std::exception& e) {
Debug::log(ERR, "Keyboard had no name???"); // logic error Debug::log(ERR, "Keyboard had no name???"); // logic error
} }
@ -661,7 +661,7 @@ void CInputManager::newMouse(wlr_input_device* mouse, bool virt) {
PMOUSE->mouse = mouse; PMOUSE->mouse = mouse;
PMOUSE->virt = virt; PMOUSE->virt = virt;
try { try {
PMOUSE->name = deviceNameToInternalString(mouse->name); PMOUSE->name = getNameForNewDevice(mouse->name);
} catch(std::exception& e) { } catch(std::exception& e) {
Debug::log(ERR, "Mouse had no name???"); // logic error Debug::log(ERR, "Mouse had no name???"); // logic error
} }
@ -1082,7 +1082,7 @@ void CInputManager::newTouchDevice(wlr_input_device* pDevice) {
PNEWDEV->pWlrDevice = pDevice; PNEWDEV->pWlrDevice = pDevice;
try { try {
PNEWDEV->name = deviceNameToInternalString(pDevice->name); PNEWDEV->name = getNameForNewDevice(pDevice->name);
} catch(std::exception& e) { } catch(std::exception& e) {
Debug::log(ERR, "Touch Device had no name???"); // logic error Debug::log(ERR, "Touch Device had no name???"); // logic error
} }
@ -1204,3 +1204,29 @@ std::string CInputManager::deviceNameToInternalString(std::string in) {
std::transform(in.begin(), in.end(), in.begin(), ::tolower); std::transform(in.begin(), in.end(), in.begin(), ::tolower);
return in; return in;
} }
std::string CInputManager::getNameForNewDevice(std::string internalName) {
auto proposedNewName = deviceNameToInternalString(internalName);
int dupeno = 0;
while (std::find_if(m_lKeyboards.begin(), m_lKeyboards.end(), [&] (const SKeyboard& other) { return other.name == proposedNewName + (dupeno == 0 ? "" : ("-" + std::to_string(dupeno))); }) != m_lKeyboards.end())
dupeno++;
while (std::find_if(m_lMice.begin(), m_lMice.end(), [&] (const SMouse& other) { return other.name == proposedNewName + (dupeno == 0 ? "" : ("-" + std::to_string(dupeno))); }) != m_lMice.end())
dupeno++;
while (std::find_if(m_lTouchDevices.begin(), m_lTouchDevices.end(), [&] (const STouchDevice& other) { return other.name == proposedNewName + (dupeno == 0 ? "" : ("-" + std::to_string(dupeno))); }) != m_lTouchDevices.end())
dupeno++;
while (std::find_if(m_lTabletPads.begin(), m_lTabletPads.end(), [&] (const STabletPad& other) { return other.name == proposedNewName + (dupeno == 0 ? "" : ("-" + std::to_string(dupeno))); }) != m_lTabletPads.end())
dupeno++;
while (std::find_if(m_lTablets.begin(), m_lTablets.end(), [&] (const STablet& other) { return other.name == proposedNewName + (dupeno == 0 ? "" : ("-" + std::to_string(dupeno))); }) != m_lTablets.end())
dupeno++;
while (std::find_if(m_lTabletTools.begin(), m_lTabletTools.end(), [&] (const STabletTool& other) { return other.name == proposedNewName + (dupeno == 0 ? "" : ("-" + std::to_string(dupeno))); }) != m_lTabletTools.end())
dupeno++;
return proposedNewName + (dupeno == 0 ? "" : ("-" + std::to_string(dupeno)));
}

View file

@ -132,6 +132,7 @@ public:
void unsetCursorImage(); void unsetCursorImage();
std::string deviceNameToInternalString(std::string); std::string deviceNameToInternalString(std::string);
std::string getNameForNewDevice(std::string);
private: private: