Skip to content

Commit

Permalink
Merge pull request #33 from lostallmymoney/NoUser
Browse files Browse the repository at this point in the history
No user
  • Loading branch information
Cam authored Mar 23, 2023
2 parents b37b948 + 2805a34 commit 5a43849
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 122 deletions.
38 changes: 11 additions & 27 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,47 +59,31 @@ sudo chmod 755 /usr/local/bin/Naga_Linux/nagaKillroot.sh
sudo cp -f ./src/nagaUninstall.sh /usr/local/bin/Naga_Linux/
sudo chmod 755 /usr/local/bin/Naga_Linux/nagaUninstall.sh

_dir="/home/razerInput/.naga"
sudo mkdir -p "$_dir"
_dir="/home/$USER/.naga"
mkdir -p "$_dir"
sudo cp -r -n -v "keyMap.txt" "$_dir"
sudo chown -R "root:root" "$_dir"
printf "%s\n%s" "$USER" "$(id -u "$USER")" | sudo tee /home/razerInput/.naga/user.txt >/dev/null
sudo chown -R "root:root" "$_dir"/keyMap.txt

sudo groupadd -f razerInputGroup
sudo bash -c "useradd razerInput > /dev/null 2>&1"
sudo usermod -a -G razerInputGroup razerInput >/dev/null

xhost +SI:localuser:razerInput
sudo setfacl -R -m g:razerInputGroup:rwx ~ >/dev/null
sudo setfacl -d -m g:razerInputGroup:rwx ~ >/dev/null
sudo setfacl -R -m g:razerInputGroup:rwx /run/user/$UID >/dev/null
sudo setfacl -d -m g:razerInputGroup:rwx /run/user/$UID >/dev/null
printf 'KERNEL=="event[0-9]*",SUBSYSTEM=="input",GROUP="razerInputGroup",MODE="640"' | sudo tee /etc/udev/rules.d/80-naga.rules > /dev/null

env | sudo tee /home/razerInput/.naga/envSetup >/dev/null

echo 'KERNEL=="event[0-9]*",SUBSYSTEM=="input",GROUP="razerInputGroup",MODE="640"' >/tmp/80-naga.rules
sudo cp -f src/naga.service /etc/systemd/system/

sudo mv /tmp/80-naga.rules /etc/udev/rules.d/80-naga.rules
env | tee ~/.naga/envSetup >/dev/null
grep -qF 'env | tee ~/.naga/envSetup' ~/.profile || printf '\n%s\n' 'env | tee ~/.naga/envSetup > /dev/null' | tee -a ~/.profile > /dev/null
grep -qF 'sudo systemctl start naga' ~/.profile || printf '\n%s\n' 'sudo systemctl start naga > /dev/null' | tee -a ~/.profile > /dev/null

sudo cp -f src/naga.service /etc/systemd/system/
sudo mkdir -p /etc/systemd/system/naga.service.d
sudo cp -f src/naga.conf /etc/systemd/system/naga.service.d/
printf "%s\n" "$DISPLAY" | sudo tee -a /etc/systemd/system/naga.service.d/naga.conf >/dev/null
printf "WorkingDirectory=/home/%s/\n" "$USER" | sudo tee -a /etc/systemd/system/naga.service.d/naga.conf >/dev/null
printf "Environment=DISPLAY=%s\n" "$DISPLAY" | sudo tee -a /etc/systemd/system/naga.service >/dev/null
printf "User=%s\n" "$USER" | sudo tee -a /etc/systemd/system/naga.service >/dev/null
printf "EnvironmentFile=/home/%s/.naga/envSetup\n" "$USER" | sudo tee -a /etc/systemd/system/naga.service >/dev/null


sudo udevadm control --reload-rules && sudo udevadm trigger

sleep 0.5
sudo cat /etc/sudoers | grep -qxF "razerInput ALL=($USER) NOPASSWD:ALL" || printf "\nrazerInput ALL=(%s) NOPASSWD:ALL\n" "$USER" | sudo EDITOR='tee -a' visudo >/dev/null
sudo cat /etc/sudoers | grep -qxF "$USER ALL=(ALL) NOPASSWD:/bin/systemctl start naga" || printf "\n%s ALL=(ALL) NOPASSWD:/bin/systemctl start naga\n" "$USER" | sudo EDITOR='tee -a' visudo >/dev/null
sudo cat /etc/sudoers | grep -qxF "$USER ALL=(ALL) NOPASSWD:/usr/bin/tee /home/razerInput/.naga/envSetup" || printf "\n%s ALL=(ALL) NOPASSWD:/usr/bin/tee /home/razerInput/.naga/envSetup\n" "$USER" | sudo EDITOR='tee -a' visudo >/dev/null

grep -qF 'xhost +SI:localuser:razerInput' ~/.profile || printf '\n%s\n' 'xhost +SI:localuser:razerInput > /dev/null' | tee -a ~/.profile
grep -qF 'sudo systemctl start naga' ~/.profile || printf '\n%s\n' 'sudo systemctl start naga > /dev/null' | tee -a ~/.profile
grep -qF 'env | sudo tee /home/razerInput/.naga/envSetup' ~/.profile || printf '\n%s\n' 'env | sudo tee /home/razerInput/.naga/envSetup > /dev/null' | tee -a ~/.profile

sudo chown -R razerInput:razerInputGroup /home/razerInput

sudo systemctl enable naga
sudo systemctl start naga
Expand Down
2 changes: 0 additions & 2 deletions src/naga.conf

This file was deleted.

9 changes: 4 additions & 5 deletions src/naga.service
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
Description=Custom key bindings for the side panel of razer mouses.
After=default.target

[Install]
WantedBy=default.target

[Service]
Type=simple
EnvironmentFile=/home/razerInput/.naga/envSetup
ExecStart=/usr/local/bin/naga serviceHelper
TimeoutStartSec=20
User=razerInput

[Install]
WantedBy=default.target
Group=razerInputGroup
34 changes: 13 additions & 21 deletions src/nagaKillroot.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
#!/bin/sh
# shellcheck disable=SC2046
# shellcheck disable=SC2009

if [ "$(id -u)" -ne 0 ];then
if [ $# -eq 0 ]; then
kill $(pgrep -f "naga start" -G "$USER") > /dev/null 2>&1
if [ "$(pgrep -f "naga start" -G 0 -c)" -ne 0 ]; then
pkexec --user root kill $(pgrep -f "naga start" -G 0)
kill $(pgrep -f "naga serviceHelper") > /dev/null 2>&1
if [ "$(pgrep -f "naga serviceHelper" -G 0 -c)" -ne 0 ]; then
pkexec --user root kill $(pgrep -f "naga serviceHelper" -G 0)
fi
if [ "$(pgrep -f "naga service" -c)" -ne 0 ]; then
pkexec --user root systemctl stop naga
fi
else
kill $(pgrep -f "naga start" -G "$USER" | grep -v "$1") > /dev/null 2>&1
if [ "$(pgrep -f "naga start" -G 0 | grep -v "$1" -c)" -ne 0 ]; then
pkexec --user root kill $(pgrep -f "naga start" -G 0 | grep -v "$1")
fi
if [ "$(pgrep -f "naga service" | grep -v "$1" -c)" -ne 0 ]; then
pkexec --user root systemctl stop naga
else
kill $(pgrep -f "naga serviceHelper" | grep -v "$1") > /dev/null 2>&1
if [ "$(pgrep -f "naga serviceHelper" -G 0 | grep -v "$1" -c)" -ne 0 ]; then
pkexec --user root kill $(pgrep -f "naga serviceHelper" -G 0 | grep -v "$1")
fi
fi
else
systemctl stop naga
if [ $# -eq 0 ]; then
kill $(ps aux | grep "naga start" | grep -v root | grep -v grep | awk '{print $2}') > /dev/null 2>&1
if [ "$(pgrep -f "naga start" -G 0 -c)" -ne 0 ]; then
kill $(pgrep -f "naga start" -G 0)
kill $(ps aux | grep "naga serviceHelper" | grep -v root | grep -v grep | awk '{print $2}') > /dev/null 2>&1
if [ "$(pgrep -f "naga serviceHelper" -G 0 -c)" -ne 0 ]; then
kill $(pgrep -f "naga serviceHelper" -G 0)
fi
else
kill $(ps aux | grep "naga start" | grep -v root | grep -v "$1" | grep -v grep | awk '{print $2}') > /dev/null 2>&1
if [ "$(pgrep -f "naga start" -G 0 | grep -v "$1" -c)" -ne 0 ]; then
kill $(pgrep -f "naga start" -G 0 | grep -v "$1")
kill $(ps aux | grep "naga serviceHelper" | grep -v root | grep -v "$1" | grep -v grep | awk '{print $2}') > /dev/null 2>&1
if [ "$(pgrep -f "naga serviceHelper" -G 0 | grep -v "$1" -c)" -ne 0 ]; then
kill $(pgrep -f "naga serviceHelper" -G 0 | grep -v "$1")
fi
fi
fi
101 changes: 34 additions & 67 deletions src/nagaX11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,15 @@
#include <iomanip>
#include <mutex>
#include <map>
#include <unordered_map>
#include <sstream>
using namespace std;

typedef pair<const char *const, const char *const> CharAndChar;
static mutex fakeKeyFollowUpsMutex, configSwitcherMutex;
static map<const char *const, FakeKey *const> *const fakeKeyFollowUps = new map<const char *const, FakeKey *const>();
const string conf_file = string(getenv("HOME")) + "/.naga/keyMap.txt";
static int fakeKeyFollowCount = 0;
map<string, string *> notifySendMap;
string userString = "", userID = "";
char **envp;

const string *applyUserString(string c)
{
return new string("sudo -EHu " + userString + " bash -c '" + c + "'");
}

const void fetchUserString()
{
ifstream file("/home/razerInput/.naga/user.txt");
string line;
if (getline(file, line))
{
line.erase(std::remove_if(line.begin(), line.end(), [](unsigned char c)
{ return std::isspace(c); }),
line.end()); // nuke all whitespaces
userString = line;
if (getline(file, line))
{
line.erase(std::remove_if(line.begin(), line.end(), [](unsigned char c)
{ return std::isspace(c); }),
line.end()); // nuke all whitespaces
userID = line;
}
else
exit(1);
}
else
exit(1);
file.close();
}

class configKey
{
Expand Down Expand Up @@ -91,8 +59,6 @@ class MacroEvent
}
};

typedef vector<MacroEvent *> MacroEventVector;

class configSwitchScheduler
{
private:
Expand Down Expand Up @@ -148,22 +114,25 @@ static configSwitchScheduler *const configSwitcher = new configSwitchScheduler()
class NagaDaemon
{
private:
const string conf_file = "/home/razerInput/.naga/keyMap.txt";

map<string, configKey *const> configKeysMap;
map<string, map<int, map<bool, MacroEventVector>>> macroEventsKeyMaps;
map<string, map<int, map<bool, vector<MacroEvent *>>>> macroEventsKeyMaps;

string currentConfigName;
struct input_event ev1[64];
const int size = sizeof(struct input_event) * 64;
const int size = sizeof(ev1);
vector<CharAndChar> devices;
bool areSideBtnEnabled = true, areExtraBtnEnabled = true;
map<int, std::map<bool, MacroEventVector>> *currentConfigPtr;
map<int, std::map<bool, vector<MacroEvent *>>> *currentConfigPtr;

const string *applyBashCommand(string c)
{
return new string("bash -c '" + c + "'");
}

void initConf()
{
string commandContent;
map<int, std::map<bool, MacroEventVector>> *iteratedConfig;
map<int, std::map<bool, vector<MacroEvent *>>> *iteratedConfig;
bool isIteratingConfig = false;

ifstream in(conf_file.c_str(), ios::in);
Expand Down Expand Up @@ -209,19 +178,19 @@ class NagaDaemon
exit(1);
}

map<bool, MacroEventVector> *iteratedButtonConfig = &(*iteratedConfig)[buttonNumberInt];
map<bool, vector<MacroEvent *>> *iteratedButtonConfig = &(*iteratedConfig)[buttonNumberInt];

if (configKeysMap.contains(commandType))
{ // filter out bad types

if (commandType == "run" || commandType == "run2") // This makes you able to run complete bash inside run
commandContent = *applyBashCommand(commandContent);
if (!configKeysMap[commandType]->Prefix()->empty())
commandContent = *configKeysMap[commandType]->Prefix() + commandContent;
if (!configKeysMap[commandType]->Suffix()->empty())
commandContent = commandContent + *configKeysMap[commandType]->Suffix();

if (configKeysMap[commandType]->InternalFunction() == executeNow)
(*iteratedButtonConfig)[configKeysMap[commandType]->IsOnKeyPressed()].emplace_back(new MacroEvent(configKeysMap[commandType], applyUserString(commandContent)));
else
(*iteratedButtonConfig)[configKeysMap[commandType]->IsOnKeyPressed()].emplace_back(new MacroEvent(configKeysMap[commandType], &commandContent));
(*iteratedButtonConfig)[configKeysMap[commandType]->IsOnKeyPressed()].emplace_back(new MacroEvent(configKeysMap[commandType], &commandContent));
// Encode and store mapping v3
}
else if (commandType == "key")
Expand All @@ -232,8 +201,8 @@ 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"], applyUserString(commandContent)));
(*iteratedButtonConfig)[false].emplace_back(new MacroEvent(configKeysMap["keyreleaseonrelease"], applyUserString(*commandContent2)));
(*iteratedButtonConfig)[true].emplace_back(new MacroEvent(configKeysMap["keypressonpress"], &commandContent));
(*iteratedButtonConfig)[false].emplace_back(new MacroEvent(configKeysMap["keyreleaseonrelease"], commandContent2));
}
else if (commandType == "specialkey")
{
Expand All @@ -252,14 +221,14 @@ class NagaDaemon
commandContent.erase(0, 13);
iteratedConfig = &macroEventsKeyMaps[commandContent];
configSwitcher->configWindowsNamesVector.emplace_back(new string(commandContent));
notifySendMap.emplace(commandContent, new string(*applyUserString("notify-send \"Profile : " + commandContent + "\"")));
notifySendMap.emplace(commandContent, new string("notify-send \"Profile : " + commandContent + "\""));
}
else if (commandContent.substr(0, 7) == "config=")
{
isIteratingConfig = true;
commandContent.erase(0, 7);
iteratedConfig = &macroEventsKeyMaps[commandContent];
notifySendMap.emplace(commandContent, new string(*applyUserString("notify-send \"Profile : " + commandContent + "\"")));
notifySendMap.emplace(commandContent, new string("notify-send \"Profile : " + commandContent + "\""));
}
}
in.close();
Expand Down Expand Up @@ -343,7 +312,8 @@ class NagaDaemon

if (areSideBtnEnabled && FD_ISSET(side_btn_fd, &readset)) // Side buttons
{
if (ev1[0].value == ' '){
if (ev1[0].value == ' ')
{
writeStringNow(new string("A SPACE DETECTED"));
}

Expand Down Expand Up @@ -445,7 +415,7 @@ class NagaDaemon
}
// end of configKeys functions

static void runActions(MacroEventVector *const relativeMacroEventsPointer)
static void runActions(vector<MacroEvent *> *const relativeMacroEventsPointer)
{
for (MacroEvent *const macroEventPointer : *relativeMacroEventsPointer)
{ // run all the events at Key
Expand Down Expand Up @@ -475,7 +445,7 @@ class NagaDaemon
devices.emplace_back("/dev/input/by-id/usb-Razer_Razer_Naga_Left_Handed_Edition-if02-event-kbd", "/dev/input/by-id/usb-Razer_Razer_Naga_Left_Handed_Edition-event-mouse"); // NAGA Left Handed
devices.emplace_back("/dev/input/by-id/usb-Razer_Razer_Naga_Pro_000000000000-if02-event-kbd", "/dev/input/by-id/usb-Razer_Razer_Naga_Pro_000000000000-event-mouse"); // NAGA PRO WIRELESS
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)
// 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)
{ // Setup check
Expand Down Expand Up @@ -505,7 +475,6 @@ class NagaDaemon
}

// modulable options list to manage internals inside runActions method arg1:COMMAND, arg2:onKeyPressed?, arg3:function to send prefix+config content.
fetchUserString();
#define ONKEYPRESSED true
#define ONKEYRELEASED false

Expand All @@ -515,23 +484,23 @@ class NagaDaemon
emplaceConfigKey("sleep", ONKEYPRESSED, sleepNow);
emplaceConfigKey("sleeprelease", ONKEYRELEASED, sleepNow);

emplaceConfigKey("run", ONKEYPRESSED, executeNow, "", "&");
emplaceConfigKey("run", ONKEYPRESSED, executeNow, "setsid ", "&");
emplaceConfigKey("run2", ONKEYPRESSED, executeNow);

emplaceConfigKey("runrelease", ONKEYRELEASED, executeNow, "", "&");
emplaceConfigKey("runrelease", ONKEYRELEASED, executeNow, "setsid ", "&");
emplaceConfigKey("runrelease2", ONKEYRELEASED, executeNow);

emplaceConfigKey("launch", ONKEYRELEASED, executeNow, "gtk-launch ", "&");
emplaceConfigKey("launch", ONKEYRELEASED, executeNow, "setsid gtk-launch ", "&");
emplaceConfigKey("launch2", ONKEYRELEASED, executeNow, "gtk-launch ");

emplaceConfigKey("keypressonpress", ONKEYPRESSED, executeNow, "xdotool keydown --window getactivewindow ", "&");
emplaceConfigKey("keypressonrelease", ONKEYRELEASED, executeNow, "xdotool keydown --window getactivewindow ", "&");
emplaceConfigKey("keypressonpress", ONKEYPRESSED, executeNow, "setsid xdotool keydown --window getactivewindow ", "&");
emplaceConfigKey("keypressonrelease", ONKEYRELEASED, executeNow, "setsid xdotool keydown --window getactivewindow ", "&");

emplaceConfigKey("keyreleaseonpress", ONKEYPRESSED, executeNow, "xdotool keyup --window getactivewindow ", "&");
emplaceConfigKey("keyreleaseonrelease", ONKEYRELEASED, executeNow, "xdotool keyup --window getactivewindow ", "&");
emplaceConfigKey("keyreleaseonpress", ONKEYPRESSED, executeNow, "setsid xdotool keyup --window getactivewindow ", "&");
emplaceConfigKey("keyreleaseonrelease", ONKEYRELEASED, executeNow, "setsid xdotool keyup --window getactivewindow ", "&");

emplaceConfigKey("keyclick", ONKEYPRESSED, executeNow, "xdotool key --window getactivewindow ", "&");
emplaceConfigKey("keyclickrelease", ONKEYRELEASED, executeNow, "xdotool key --window getactivewindow ", "&");
emplaceConfigKey("keyclick", ONKEYPRESSED, executeNow, "setsid xdotool key --window getactivewindow ", "&");
emplaceConfigKey("keyclickrelease", ONKEYRELEASED, executeNow, "setsid xdotool key --window getactivewindow ", "&");

emplaceConfigKey("string", ONKEYPRESSED, writeStringNow);
emplaceConfigKey("stringrelease", ONKEYRELEASED, writeStringNow);
Expand All @@ -558,13 +527,12 @@ void stopD()
};

// arguments manage
int main(const int argc, const char *const argv[], char **envpI)
int main(const int argc, const char *const argv[])
{
if (argc > 1)
{
if (strstr(argv[1], "serviceHelper") != NULL)
{
envp = envpI;
(void)!(system("/usr/local/bin/Naga_Linux/nagaXinputStart.sh"));
if (argc > 2)
NagaDaemon(string(argv[2]).c_str());
Expand Down Expand Up @@ -602,7 +570,6 @@ int main(const int argc, const char *const argv[], char **envpI)
clog << "Starting naga debug, logs :" << endl;
usleep(100000);
(void)!(system("/usr/local/bin/Naga_Linux/nagaXinputStart.sh"));
envp = envpI;
NagaDaemon();
}
else
Expand All @@ -622,7 +589,7 @@ int main(const int argc, const char *const argv[], char **envpI)
}
else if (strstr(argv[1], "edit") != NULL)
{
(void)!(system("sudo bash -c \"nano /home/razerInput/.naga/keyMap.txt && systemctl restart naga\""));
(void)!(system(("sudo bash -c \"nano " + conf_file + " && systemctl restart naga\"").c_str()));
}
else if (strstr(argv[1], "uninstall") != NULL)
{
Expand Down

0 comments on commit 5a43849

Please sign in to comment.