Skip to content

Commit

Permalink
Const update
Browse files Browse the repository at this point in the history
  • Loading branch information
Cam authored Mar 16, 2023
1 parent 5e58a6c commit 10e2889
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
11 changes: 6 additions & 5 deletions install.sh
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions keyMap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 35 additions & 30 deletions src/naga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include <sstream>
using namespace std;

typedef pair<const char *, const char *> CharAndChar;
typedef pair<char, FakeKey *> CharAndFakeKey;
typedef pair<const char *const, const char *const> CharAndChar;
typedef pair<const char *const, FakeKey *const> CharAndFakeKey;
static mutex fakeKeyFollowUpsMutex, configSwitcherMutex;
static vector<CharAndFakeKey *> *const fakeKeyFollowUps = new vector<CharAndFakeKey *>();
static int fakeKeyFollowCount = 0;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}
}
}
Expand Down Expand Up @@ -277,7 +283,6 @@ class NagaDaemon
lock_guard<mutex> guard(configSwitcherMutex);
configSwitcher->scheduleWindowReMap(configWindowName);
loadConf(configSwitcher->RemapString(), true); // change config for macroEvents[ii]->Content()
configSwitcher->unScheduleReMap();
found = true;
break;
}
Expand All @@ -287,7 +292,6 @@ class NagaDaemon
lock_guard<mutex> guard(configSwitcherMutex);
configSwitcher->scheduleReMap(&configSwitcher->getBackupConfigName());
loadConf(configSwitcher->RemapString(), true); // change config for macroEvents[ii]->Content()
configSwitcher->unScheduleReMap();
}
}
}
Expand All @@ -303,7 +307,6 @@ class NagaDaemon
{
lock_guard<mutex> guard(configSwitcherMutex); // remap
loadConf(configSwitcher->RemapString()); // change config for macroEvents[ii]->Content()
configSwitcher->unScheduleReMap();
}

FD_ZERO(&readset);
Expand Down Expand Up @@ -376,25 +379,25 @@ class NagaDaemon
deleteFakeKey(aKeyFaker);
}

const static void specialPressNow(const string *macroContent)
const static void specialPressNow(const string *const macroContent)
{
lock_guard<mutex> 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)
{
lock_guard<mutex> guard(fakeKeyFollowUpsMutex);
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);
Expand Down Expand Up @@ -436,6 +439,10 @@ class NagaDaemon
}
}

void newStrAndCfgKey(){

};

public:
NagaDaemon(const string mapConfig = "defaultConfig")
{
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -517,7 +523,6 @@ class NagaDaemon

configSwitcher->scheduleReMap(&mapConfig);
loadConf(mapConfig); // Initialize config
configSwitcher->unScheduleReMap();
run();
}
};
Expand Down

0 comments on commit 10e2889

Please sign in to comment.