From 10e2889cbb3eee00ce389f8a8fa81be3d994f0e3 Mon Sep 17 00:00:00 2001 From: Cam Date: Thu, 16 Mar 2023 07:54:27 -0400 Subject: [PATCH] Const update --- install.sh | 11 +++++---- keyMap.txt | 1 + src/naga.cpp | 65 ++++++++++++++++++++++++++++------------------------ 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/install.sh b/install.sh index c638b82..705288e 100755 --- a/install.sh +++ b/install.sh @@ -1,15 +1,17 @@ #!/bin/bash + sudo nohup killall naga > /dev/null 2>&1 & sudo echo "Installing requirements..." sudo apt install libx11-dev xdotool xinput g++ libxtst-dev libxmu-dev -sudo echo "Checking requirements..." -command -v xdotool >/dev/null 2>&1 || { tput setaf 1; echo >&2 "I require xdotool but it's not installed! Aborting."; tput sgr0; exit 1; } -command -v xinput >/dev/null 2>&1 || { tput setaf 1; echo >&2 "I require xinput but it's not installed! Aborting."; tput sgr0; exit 1; } -command -v g++ >/dev/null 2>&1 || { tput setaf 1; echo >&2 "I require g++ but it's not installed! Aborting."; tput sgr0; exit 1; } +echo "Checking requirements..." + +command -v xdotool >/dev/null 2>&1 || { tput setaf 1; echo >&2 "I require xdotool but it's not installed! Aborting."; exit 1; } +command -v xinput >/dev/null 2>&1 || { tput setaf 1; echo >&2 "I require xinput but it's not installed! Aborting."; exit 1; } +command -v g++ >/dev/null 2>&1 || { tput setaf 1; echo >&2 "I require g++ but it's not installed! Aborting."; exit 1; } reset @@ -19,7 +21,6 @@ g++ naga.cpp -o naga -pthread -Ofast --std=c++2a -lX11 -lXtst -lXmu if [ ! -f ./naga ]; then tput setaf 1; echo "Error at compile! Ensure you have g++ installed. !!!Aborting!!!" - tput sgr0; exit 1 fi diff --git a/keyMap.txt b/keyMap.txt index db2f761..530d0a8 100644 --- a/keyMap.txt +++ b/keyMap.txt @@ -51,6 +51,7 @@ configEnd configWindow=Gnome-terminal 1 - key=XF86AudioMute +3 - string=gnome@terminal.com 4 - run2=if [ $(pgrep -c spotify) -eq 0 ]; then setsid spotify; fi 5 - run=gnome-terminal 7 - key=XF86AudioLowerVolume diff --git a/src/naga.cpp b/src/naga.cpp index 83c1eaa..ae8e459 100644 --- a/src/naga.cpp +++ b/src/naga.cpp @@ -18,8 +18,8 @@ #include using namespace std; -typedef pair CharAndChar; -typedef pair CharAndFakeKey; +typedef pair CharAndChar; +typedef pair CharAndFakeKey; static mutex fakeKeyFollowUpsMutex, configSwitcherMutex; static vector *const fakeKeyFollowUps = new vector(); static int fakeKeyFollowCount = 0; @@ -132,6 +132,7 @@ class NagaDaemon void loadConf(const string configName, bool silent = false) { + configSwitcher->unScheduleReMap(); if (!macroEventsKeyMaps.contains(configName)) { ifstream in(conf_file.c_str(), ios::in); @@ -202,36 +203,41 @@ class NagaDaemon commandType = new string(commandType->substr(pos + 1)); // Isolate command type for (char &c : *commandType) c = tolower(c); - if (configKeysMap.contains(*commandType)) - { // filter out bad types - int buttonNumberI; + + const auto getNumber = [&](const string *const numberString) + { try { - buttonNumberI = stoi(*buttonNumber); + return stoi(*numberString); } catch (...) { clog << "At config line " << readingLine << ": expected a number" << endl; exit(1); } + }; + + if (configKeysMap.contains(*commandType)) + { // filter out bad types + + int buttonNumberI = getNumber(buttonNumber); - if (*commandType == "key") + if (*configKeysMap[*commandType]->Prefix() != "") + commandContent = *configKeysMap[*commandType]->Prefix() + commandContent; + macroEventsKeyMaps[configName][buttonNumberI][configKeysMap[*commandType]->IsOnKeyPressed()].emplace_back(new MacroEvent(configKeysMap[*commandType], &commandContent)); + // Encode and store mapping v3 + } + else if (*commandType == "key") + { + int buttonNumberI = getNumber(buttonNumber); + if (commandContent.size() == 1) { - if (commandContent.size() == 1) - { - commandContent = hexChar(commandContent[0]); - } - const string *const commandContent2 = new string(*configKeysMap["keyreleaseonrelease"]->Prefix() + commandContent); - commandContent = *configKeysMap["keypressonpress"]->Prefix() + commandContent; - macroEventsKeyMaps[configName][buttonNumberI][true].emplace_back(new MacroEvent(configKeysMap["keypressonpress"], &commandContent)); - macroEventsKeyMaps[configName][buttonNumberI][false].emplace_back(new MacroEvent(configKeysMap["keyreleaseonrelease"], commandContent2)); + commandContent = hexChar(commandContent[0]); } - else - { - if (*configKeysMap[*commandType]->Prefix() != "") - commandContent = *configKeysMap[*commandType]->Prefix() + commandContent; - macroEventsKeyMaps[configName][buttonNumberI][configKeysMap[*commandType]->IsOnKeyPressed()].emplace_back(new MacroEvent(configKeysMap[*commandType], &commandContent)); - } // Encode and store mapping v3 + const string *const commandContent2 = new string(*configKeysMap["keyreleaseonrelease"]->Prefix() + commandContent); + commandContent = *configKeysMap["keypressonpress"]->Prefix() + commandContent; + macroEventsKeyMaps[configName][buttonNumberI][true].emplace_back(new MacroEvent(configKeysMap["keypressonpress"], &commandContent)); + macroEventsKeyMaps[configName][buttonNumberI][false].emplace_back(new MacroEvent(configKeysMap["keyreleaseonrelease"], commandContent2)); } } } @@ -277,7 +283,6 @@ class NagaDaemon lock_guard guard(configSwitcherMutex); configSwitcher->scheduleWindowReMap(configWindowName); loadConf(configSwitcher->RemapString(), true); // change config for macroEvents[ii]->Content() - configSwitcher->unScheduleReMap(); found = true; break; } @@ -287,7 +292,6 @@ class NagaDaemon lock_guard guard(configSwitcherMutex); configSwitcher->scheduleReMap(&configSwitcher->getBackupConfigName()); loadConf(configSwitcher->RemapString(), true); // change config for macroEvents[ii]->Content() - configSwitcher->unScheduleReMap(); } } } @@ -303,7 +307,6 @@ class NagaDaemon { lock_guard guard(configSwitcherMutex); // remap loadConf(configSwitcher->RemapString()); // change config for macroEvents[ii]->Content() - configSwitcher->unScheduleReMap(); } FD_ZERO(&readset); @@ -376,17 +379,17 @@ class NagaDaemon deleteFakeKey(aKeyFaker); } - const static void specialPressNow(const string *macroContent) + const static void specialPressNow(const string *const macroContent) { lock_guard guard(fakeKeyFollowUpsMutex); FakeKey *const aKeyFaker = fakekey_init(XOpenDisplay(NULL)); fakekey_press(aKeyFaker, (unsigned char *)&(*macroContent)[0], 8, 0); XFlush(aKeyFaker->xdpy); - fakeKeyFollowUps->emplace_back(new CharAndFakeKey((*macroContent)[0], aKeyFaker)); + fakeKeyFollowUps->emplace_back(new CharAndFakeKey(&(*macroContent)[0], aKeyFaker)); fakeKeyFollowCount++; } - const static void specialReleaseNow(const string *macroContent) + const static void specialReleaseNow(const string *const macroContent) { if (fakeKeyFollowCount > 0) { @@ -394,7 +397,7 @@ class NagaDaemon for (int vectorId = fakeKeyFollowUps->size() - 1; vectorId >= 0; vectorId--) { CharAndFakeKey *const aKeyFollowUp = (*fakeKeyFollowUps)[vectorId]; - if (get<0>(*aKeyFollowUp) == (*macroContent)[0]) + if (*get<0>(*aKeyFollowUp) == (*macroContent)[0]) { FakeKey *const aKeyFaker = get<1>(*aKeyFollowUp); fakekey_release(aKeyFaker); @@ -436,6 +439,10 @@ class NagaDaemon } } + void newStrAndCfgKey(){ + + }; + public: NagaDaemon(const string mapConfig = "defaultConfig") { @@ -483,7 +490,6 @@ class NagaDaemon } // modulable options list to manage internals inside runActions method arg1:COMMAND, arg2:onKeyPressed?, arg3:function to send prefix+config content. - configKeysMap.insert(stringAndConfigKey("key", NULL)); // special one configKeysMap.insert(stringAndConfigKey("chmap", new configKey(true, chmapNow))); // change keymap configKeysMap.insert(stringAndConfigKey("chmaprelease", new configKey(false, chmapNow))); @@ -517,7 +523,6 @@ class NagaDaemon configSwitcher->scheduleReMap(&mapConfig); loadConf(mapConfig); // Initialize config - configSwitcher->unScheduleReMap(); run(); } };