From 896ee037d6379e43429a4d7f4f4463f50f272012 Mon Sep 17 00:00:00 2001 From: Cam Date: Sat, 25 Mar 2023 14:40:59 -0400 Subject: [PATCH] Cleanup --- src/nagaWayland.cpp | 164 ++++++++++++++++---------------------- src/nagaX11.cpp | 187 ++++++++++++++++++-------------------------- 2 files changed, 140 insertions(+), 211 deletions(-) diff --git a/src/nagaWayland.cpp b/src/nagaWayland.cpp index 12e46eb..3ca5bdc 100644 --- a/src/nagaWayland.cpp +++ b/src/nagaWayland.cpp @@ -88,71 +88,86 @@ class configKey } }; -class MacroEvent -{ -private: - const configKey *const keyType; - const string *const content; +typedef const pair MacroEvent; -public: - const configKey *const KeyType() const { return keyType; } - const string *const Content() const { return content; } - MacroEvent(const configKey *tkeyType, const string *tcontent) : keyType(tkeyType), content(new string(*tcontent)) - { - } -}; +static map>>> macroEventsKeyMaps; class configSwitchScheduler { private: bool scheduledReMap = false, aWindowConfigActive = false; - const string * temporaryWindowConfigName = NULL; - const string * backupConfigName = NULL; - const string * scheduledReMapString = NULL; + const string *temporaryWindowConfigName = NULL; + const string *backupConfigName = NULL; + const string *scheduledReMapString = NULL; + const string *currentConfigName = NULL; public: - vector configWindowsNamesVector; + vector configWindowsNamesVector; + map>> *currentConfigPtr; - const string *RemapString() + void loadConf(bool silent = false) { - return scheduledReMapString; - } - const string *temporaryWindowName() - { - return temporaryWindowConfigName; - } - const string *getBackupConfigName() - { - return backupConfigName; + if (!macroEventsKeyMaps.contains(*scheduledReMapString)) + { + clog << "Undefined profile : " << *scheduledReMapString << endl; + return; + } + + scheduledReMap = false; + currentConfigName = scheduledReMapString; + + currentConfigPtr = ¯oEventsKeyMaps[*scheduledReMapString]; + if (!silent) + { + (void)!(system(notifySendMap[*scheduledReMapString]->c_str())); + } } - bool isAWindowConfigActive() + void checkForWindowConfig() { - return aWindowConfigActive; + const string currentAppTitle = getTitle(); + clog << "WindowNameLog : " << currentAppTitle << endl; + if (!aWindowConfigActive || currentAppTitle != *temporaryWindowConfigName) + { + bool found = false; + for (const string *const configWindowName : configWindowsNamesVector) + { + if (currentAppTitle == *configWindowName) + { + if (!aWindowConfigActive) + { + backupConfigName = scheduledReMapString; + } + scheduledReMapString = temporaryWindowConfigName = configWindowName; + scheduledReMap = aWindowConfigActive = true; + loadConf(true); // change config for macroEvents[ii]->Content() + found = true; + break; + } + } + if (!found && aWindowConfigActive) + { + scheduledReMapString = backupConfigName; + scheduledReMap = true, aWindowConfigActive = false; + temporaryWindowConfigName = backupConfigName = NULL; + loadConf(true); // change config for macroEvents[ii]->Content() + } + } } - bool isRemapScheduled() + void isRemapScheduledCheck() { - return scheduledReMap; + if (scheduledReMap) + { + loadConf(); // change config for macroEvents[ii]->Content() + } } void scheduleReMap(const string *const reMapString) { + lock_guard guard(configSwitcherMutex); scheduledReMapString = reMapString; scheduledReMap = true, aWindowConfigActive = false; temporaryWindowConfigName = backupConfigName = NULL; } - void scheduleWindowReMap(const string *reMapString) - { - if (!aWindowConfigActive) - { - backupConfigName = scheduledReMapString; - } - scheduledReMapString = temporaryWindowConfigName = reMapString; - scheduledReMap = aWindowConfigActive = true; - } - void unScheduleReMap() - { - scheduledReMap = false; - } }; static configSwitchScheduler *const configSwitcher = new configSwitchScheduler(); @@ -161,7 +176,6 @@ class NagaDaemon { private: map configKeysMap; - map>>> macroEventsKeyMaps; string currentConfigName; struct input_event ev1[64]; @@ -236,15 +250,15 @@ class NagaDaemon if (!configKeysMap[commandType]->Suffix()->empty()) commandContent = commandContent + *configKeysMap[commandType]->Suffix(); - (*iteratedButtonConfig)[configKeysMap[commandType]->IsOnKeyPressed()].emplace_back(new MacroEvent(configKeysMap[commandType], &commandContent)); + (*iteratedButtonConfig)[configKeysMap[commandType]->IsOnKeyPressed()].emplace_back(new MacroEvent(configKeysMap[commandType], new string(commandContent))); // Encode and store mapping v3 } else if (commandType == "key") { string *commandContent2 = new string(*configKeysMap["keyreleaseonrelease"]->Prefix() + commandContent + *configKeysMap["keyreleaseonrelease"]->Suffix()); commandContent = *configKeysMap["keypressonpress"]->Prefix() + commandContent + *configKeysMap["keypressonpress"]->Suffix(); - (*iteratedButtonConfig)[true].emplace_back(new MacroEvent(configKeysMap["keypressonpress"], &commandContent)); - (*iteratedButtonConfig)[false].emplace_back(new MacroEvent(configKeysMap["keyreleaseonrelease"], commandContent2)); + (*iteratedButtonConfig)[true].emplace_back(new MacroEvent(configKeysMap["keypressonpress"], new string(commandContent))); + (*iteratedButtonConfig)[false].emplace_back(new MacroEvent(configKeysMap["keyreleaseonrelease"], new string(*commandContent2))); } else { @@ -271,51 +285,10 @@ class NagaDaemon in.close(); } - void loadConf(const string *const configName, const bool silent = false) - { - if (!macroEventsKeyMaps.contains(*configName)) - { - clog << "Undefined profile : " << configName << endl; - return; - } - configSwitcher->unScheduleReMap(); - - currentConfigName = *configName; - currentConfigPtr = ¯oEventsKeyMaps[currentConfigName]; - if (!silent) - { - (void)!(system(notifySendMap[currentConfigName]->c_str())); - } - } - int side_btn_fd, extra_btn_fd; input_event *ev11; fd_set readset; - void checkForWindowConfig() - { - const string currentAppTitle = getTitle(); - if (!configSwitcher->isAWindowConfigActive() || currentAppTitle != *configSwitcher->temporaryWindowName()) - { - bool found = false; - for (string *configWindowName : configSwitcher->configWindowsNamesVector) - { - if (currentAppTitle == *configWindowName) - { - lock_guard guard(configSwitcherMutex); - configSwitcher->scheduleWindowReMap(configWindowName); - loadConf(configSwitcher->RemapString(), true); // change config for macroEvents[ii]->Content() - found = true; - } - } - if (!found && configSwitcher->isAWindowConfigActive()) - { - lock_guard guard(configSwitcherMutex); - configSwitcher->scheduleReMap(configSwitcher->getBackupConfigName()); - loadConf(configSwitcher->RemapString(), true); // change config for macroEvents[ii]->Content() - } - } - } void run() { @@ -324,11 +297,7 @@ class NagaDaemon ev11 = &ev1[1]; while (1) { - if (configSwitcher->isRemapScheduled()) - { - lock_guard guard(configSwitcherMutex); // remap - loadConf(configSwitcher->RemapString()); // change config for macroEvents[ii]->Content() - } + configSwitcher->isRemapScheduledCheck(); FD_ZERO(&readset); if (areSideBtnEnabled) @@ -347,7 +316,7 @@ class NagaDaemon switch (ev11->code) { case 2 ... 13: - checkForWindowConfig(); + configSwitcher->checkForWindowConfig(); thread(runActions, &(*currentConfigPtr)[ev11->code - 1][ev11->value == 1]).detach(); // real key number = ev11->code - 1 break; } @@ -362,7 +331,7 @@ class NagaDaemon switch (ev11->code) { case 275 ... 276: - checkForWindowConfig(); + configSwitcher->checkForWindowConfig(); thread(runActions, &(*currentConfigPtr)[ev11->code - 262][ev11->value == 1]).detach(); // real key number = ev11->code - OFFSET (#262) break; } @@ -398,7 +367,7 @@ class NagaDaemon { for (MacroEvent *const macroEventPointer : *relativeMacroEventsPointer) { // run all the events at Key - macroEventPointer->KeyType()->runInternal(macroEventPointer->Content()); + macroEventPointer->first->runInternal(macroEventPointer->second); } } @@ -410,7 +379,6 @@ class NagaDaemon public: NagaDaemon(const string mapConfig = "defaultConfig") { - // modulable device files list devices.emplace_back("/dev/input/by-id/usb-Razer_Razer_Naga_Epic-if01-event-kbd", "/dev/input/by-id/usb-Razer_Razer_Naga_Epic-event-mouse"); // NAGA EPIC devices.emplace_back("/dev/input/by-id/usb-Razer_Razer_Naga_Epic_Dock-if01-event-kbd", "/dev/input/by-id/usb-Razer_Razer_Naga_Epic_Dock-event-mouse"); // NAGA EPIC DOCK @@ -488,7 +456,7 @@ class NagaDaemon initConf(); configSwitcher->scheduleReMap(&mapConfig); - loadConf(&mapConfig); // Initialize config + configSwitcher->loadConf(); // Initialize config run(); } diff --git a/src/nagaX11.cpp b/src/nagaX11.cpp index 00c6c62..ac800a7 100644 --- a/src/nagaX11.cpp +++ b/src/nagaX11.cpp @@ -18,12 +18,11 @@ #include using namespace std; -typedef pair CharAndChar; static mutex fakeKeyFollowUpsMutex, configSwitcherMutex; static map *const fakeKeyFollowUps = new map(); -const string conf_file = string(getenv("HOME")) + "/.naga/keyMap.txt"; +static const string conf_file = string(getenv("HOME")) + "/.naga/keyMap.txt"; static int fakeKeyFollowCount = 0; -map notifySendMap; +static map notifySendMap; class configKey { @@ -44,71 +43,85 @@ class configKey } }; -class MacroEvent -{ -private: - const configKey *const keyType; - const string *const content; +typedef const pair MacroEvent; -public: - const configKey *const KeyType() const { return keyType; } - const string *const Content() const { return content; } - - MacroEvent(const configKey *tkeyType, const string *tcontent) : keyType(tkeyType), content(new string(*tcontent)) - { - } -}; +static map>>> macroEventsKeyMaps; class configSwitchScheduler { private: bool scheduledReMap = false, aWindowConfigActive = false; - const string * temporaryWindowConfigName = NULL; - const string * backupConfigName = NULL; - const string * scheduledReMapString = NULL; + const string *temporaryWindowConfigName = NULL; + const string *backupConfigName = NULL; + const string *scheduledReMapString = NULL; + const string *currentConfigName = NULL; public: - vector configWindowsNamesVector; + vector configWindowsNamesVector; + map>> *currentConfigPtr; - const string *RemapString() - { - return scheduledReMapString; - } - const string *temporaryWindowName() + void loadConf(bool silent = false) { - return temporaryWindowConfigName; - } - const string *getBackupConfigName() - { - return backupConfigName; + if (!macroEventsKeyMaps.contains(*scheduledReMapString)) + { + clog << "Undefined profile : " << *scheduledReMapString << endl; + return; + } + + scheduledReMap = false; + currentConfigName = scheduledReMapString; + + currentConfigPtr = ¯oEventsKeyMaps[*scheduledReMapString]; + if (!silent) + { + (void)!(system(notifySendMap[*scheduledReMapString]->c_str())); + } } - bool isAWindowConfigActive() + void checkForWindowConfig() { - return aWindowConfigActive; + char *currentAppTitle = getActiveWindow(); + clog << "WindowNameLog : " << currentAppTitle << endl; + if (!aWindowConfigActive || strcmp(currentAppTitle, temporaryWindowConfigName->c_str()) != 0) + { + bool found = false; + for (const string *configWindowName : configWindowsNamesVector) + { + if (strcmp(currentAppTitle, configWindowName->c_str()) == 0) + { + if (!aWindowConfigActive) + { + backupConfigName = scheduledReMapString; + } + scheduledReMapString = temporaryWindowConfigName = configWindowName; + scheduledReMap = aWindowConfigActive = true; + loadConf(true); // change config for macroEvents[ii]->Content() + found = true; + break; + } + } + if (!found && aWindowConfigActive) + { + scheduledReMapString = backupConfigName; + scheduledReMap = true, aWindowConfigActive = false; + temporaryWindowConfigName = backupConfigName = NULL; + loadConf(true); // change config for macroEvents[ii]->Content() + } + } } - bool isRemapScheduled() + void isRemapScheduledCheck() { - return scheduledReMap; + if (scheduledReMap) + { + loadConf(); // change config for macroEvents[ii]->Content() + } } void scheduleReMap(const string *const reMapString) { + lock_guard guard(configSwitcherMutex); scheduledReMapString = reMapString; scheduledReMap = true, aWindowConfigActive = false; temporaryWindowConfigName = backupConfigName = NULL; } - void scheduleWindowReMap(const string *reMapString) - { - if (!aWindowConfigActive) - { - backupConfigName = scheduledReMapString; - } - scheduledReMapString = temporaryWindowConfigName = reMapString; - scheduledReMap = aWindowConfigActive = true; - } - void unScheduleReMap() - { - scheduledReMap = false; - } }; static configSwitchScheduler *const configSwitcher = new configSwitchScheduler(); @@ -117,14 +130,11 @@ class NagaDaemon { private: map configKeysMap; - map>>> macroEventsKeyMaps; - string currentConfigName; struct input_event ev1[64]; const int size = sizeof(ev1); - vector devices; + vector> devices; bool areSideBtnEnabled = true, areExtraBtnEnabled = true; - map>> *currentConfigPtr; const string *applyBashCommand(string c) { @@ -192,7 +202,7 @@ class NagaDaemon if (!configKeysMap[commandType]->Suffix()->empty()) commandContent = commandContent + *configKeysMap[commandType]->Suffix(); - (*iteratedButtonConfig)[configKeysMap[commandType]->IsOnKeyPressed()].emplace_back(new MacroEvent(configKeysMap[commandType], &commandContent)); + (*iteratedButtonConfig)[configKeysMap[commandType]->IsOnKeyPressed()].emplace_back(new MacroEvent(configKeysMap[commandType], new string(commandContent))); // Encode and store mapping v3 } else if (commandType == "key") @@ -203,13 +213,13 @@ class NagaDaemon } string *commandContent2 = new string(*configKeysMap["keyreleaseonrelease"]->Prefix() + commandContent + *configKeysMap["keyreleaseonrelease"]->Suffix()); commandContent = *configKeysMap["keypressonpress"]->Prefix() + commandContent + *configKeysMap["keypressonpress"]->Suffix(); - (*iteratedButtonConfig)[true].emplace_back(new MacroEvent(configKeysMap["keypressonpress"], &commandContent)); - (*iteratedButtonConfig)[false].emplace_back(new MacroEvent(configKeysMap["keyreleaseonrelease"], commandContent2)); + (*iteratedButtonConfig)[true].emplace_back(new MacroEvent(configKeysMap["keypressonpress"], new string(commandContent))); + (*iteratedButtonConfig)[false].emplace_back(new MacroEvent(configKeysMap["keyreleaseonrelease"], new string(*commandContent2))); } else if (commandType == "specialkey") { - (*iteratedButtonConfig)[true].emplace_back(new MacroEvent(configKeysMap["specialpressonpress"], &commandContent)); - (*iteratedButtonConfig)[false].emplace_back(new MacroEvent(configKeysMap["specialreleaseonrelease"], &commandContent)); + (*iteratedButtonConfig)[true].emplace_back(new MacroEvent(configKeysMap["specialpressonpress"], new string(commandContent))); + (*iteratedButtonConfig)[false].emplace_back(new MacroEvent(configKeysMap["specialreleaseonrelease"], new string(commandContent))); } else { @@ -235,24 +245,6 @@ class NagaDaemon } in.close(); } - - void loadConf(const string *const configName, const bool silent = false) - { - if (!macroEventsKeyMaps.contains(*configName)) - { - clog << "Undefined profile : " << configName << endl; - return; - } - configSwitcher->unScheduleReMap(); - - currentConfigName = *configName; - currentConfigPtr = ¯oEventsKeyMaps[currentConfigName]; - if (!silent) - { - (void)!(system(notifySendMap[currentConfigName]->c_str())); - } - } - string hexChar(const char a) { ostringstream hexedChar; @@ -264,32 +256,6 @@ class NagaDaemon input_event *ev11; fd_set readset; - void checkForWindowConfig() - { - char *currentAppTitle = getActiveWindow(); - clog << "WindowNameLog : " << currentAppTitle << endl; - if (!configSwitcher->isAWindowConfigActive() || strcmp(currentAppTitle, configSwitcher->temporaryWindowName()->c_str()) != 0) - { - bool found = false; - for (string *configWindowName : configSwitcher->configWindowsNamesVector) - { - if (strcmp(currentAppTitle, configWindowName->c_str()) == 0) - { - lock_guard guard(configSwitcherMutex); - configSwitcher->scheduleWindowReMap(configWindowName); - loadConf(configSwitcher->RemapString(), true); // change config for macroEvents[ii]->Content() - found = true; - } - } - if (!found && configSwitcher->isAWindowConfigActive()) - { - lock_guard guard(configSwitcherMutex); - configSwitcher->scheduleReMap(configSwitcher->getBackupConfigName()); - loadConf(configSwitcher->RemapString(), true); // change config for macroEvents[ii]->Content() - } - } - } - void run() { if (areSideBtnEnabled) @@ -297,11 +263,7 @@ class NagaDaemon ev11 = &ev1[1]; while (1) { - if (configSwitcher->isRemapScheduled()) - { - lock_guard guard(configSwitcherMutex); // remap - loadConf(configSwitcher->RemapString()); // change config for macroEvents[ii]->Content() - } + configSwitcher->isRemapScheduledCheck(); FD_ZERO(&readset); if (areSideBtnEnabled) @@ -320,8 +282,8 @@ class NagaDaemon switch (ev11->code) { case 2 ... 13: - checkForWindowConfig(); - thread(runActions, &(*currentConfigPtr)[ev11->code - 1][ev11->value == 1]).detach(); // real key number = ev11->code - 1 + configSwitcher->checkForWindowConfig(); + thread(runActions, &(*configSwitcher->currentConfigPtr)[ev11->code - 1][ev11->value == 1]).detach(); // real key number = ev11->code - 1 break; } } @@ -335,8 +297,8 @@ class NagaDaemon switch (ev11->code) { case 275 ... 276: - checkForWindowConfig(); - thread(runActions, &(*currentConfigPtr)[ev11->code - 262][ev11->value == 1]).detach(); // real key number = ev11->code - OFFSET (#262) + configSwitcher->checkForWindowConfig(); + thread(runActions, &(*configSwitcher->currentConfigPtr)[ev11->code - 262][ev11->value == 1]).detach(); // real key number = ev11->code - OFFSET (#262) break; } } @@ -396,7 +358,6 @@ class NagaDaemon const static void chmapNow(const string *const macroContent) { - lock_guard guard(configSwitcherMutex); configSwitcher->scheduleReMap(macroContent); // schedule config switch/change } @@ -419,7 +380,7 @@ class NagaDaemon { for (MacroEvent *const macroEventPointer : *relativeMacroEventsPointer) { // run all the events at Key - macroEventPointer->KeyType()->runInternal(macroEventPointer->Content()); + macroEventPointer->first->runInternal(macroEventPointer->second); } } @@ -447,7 +408,7 @@ class NagaDaemon devices.emplace_back("/dev/input/by-id/usb-1532_Razer_Naga_Pro_000000000000-if02-event-kbd", "/dev/input/by-id/usb-1532_Razer_Naga_Pro_000000000000-event-mouse"); // NAGA PRO // devices.emplace_back("/dev/input/by-id/YOUR_DEVICE_FILE", "/dev/input/by-id/YOUR_DEVICE_FILE#2"); // DUMMY EXAMPLE, ONE CAN BE EMPTY LIKE SUCH : "" (for devices with no extra buttons) - for (CharAndChar &device : devices) + for (pair &device : devices) { // Setup check side_btn_fd = open(device.first, O_RDONLY), extra_btn_fd = open(device.second, O_RDONLY); if (side_btn_fd != -1 || extra_btn_fd != -1) @@ -514,7 +475,7 @@ class NagaDaemon initConf(); configSwitcher->scheduleReMap(&mapConfig); - loadConf(&mapConfig); // Initialize config + configSwitcher->loadConf(); // Initialize config run(); }