From 2dbc2be653129f46f381eee817924632f2354cab Mon Sep 17 00:00:00 2001 From: Rico Tiongson Date: Mon, 13 Nov 2017 20:32:47 +0800 Subject: [PATCH] [feature-systemd] Run comfortable-swipe as a service --- .gitignore | 1 + README.md | 48 +++++---------------------- install | 69 ++++++++++++++++++++++++++++++--------- src/comfortable-swipe.cpp | 15 +++------ uninstall | 25 +++++++++++--- 5 files changed, 88 insertions(+), 70 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cdeb27b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.log \ No newline at end of file diff --git a/README.md b/README.md index 59c215d..4678d43 100644 --- a/README.md +++ b/README.md @@ -26,45 +26,9 @@ Comfortable, seamless, and fast 3-finger (and 4-finger) touchpad swipe gestures 4. You may delete the downloaded `comfortable-swipe-ubuntu` folder after installation. -## How to Run - -1. Make sure `~/.local/bin/` is added to your PATH. -2. Run - - ``` - comfortable-swipe start - ``` - -3. Optional: Automatically run on startup - - ``` - comfortable-swipe autostart - ``` - -### Permissions -Sometimes, you'll need some permissions to read touchpad input data. - -1. Find out your permission group with `ls -l /dev/input/event*` - ```bash - $ ls -l /dev/input/event* - - crw-rw---- 1 root input 13, 64 Oct 23 23:09 /dev/input/event0 - crw-rw---- 1 root input 13, 65 Oct 23 23:09 /dev/input/event1 - crw-rw---- 1 root input 13, 66 Oct 23 23:09 /dev/input/event2 - crw-rw---- 1 root input 13, 67 Oct 23 23:09 /dev/input/event3 - ``` - -2. Check the fourth column (e.g. `input`) then run: - ```bash - sudo gpasswd -a $USER input - ``` - > Note: Don't forget to input your `sudo` password! - -3. ***Important***: Log out / Log back in - ## Configurations -The configuration file is located at `~/.config/comfortable-swipe.conf`. -Make sure to run `comfortable-swipe restart` after making changes. +The configuration file is located at `/usr/local/share/comfortable-swipe.conf`. +Run `sudo systemctl restart comfortable-swipe.service` after making changes. Property | Description | Default Value | Default Behavior --------- | ----------- | -------------- | ----- @@ -79,7 +43,11 @@ down3 | 3-finger swipe down | ctrl+shift+Down | switch to above workspace down4 | 4-finger swipe down | ctrl+alt+shift+Up | move window to above workpace ## Uninstall -Download the `uninstall` script then run `bash uninstall` +Clone the repository then run `bash uninstall`. ## Bug Reports -Create an issue [here](https://github.com/Hikari9/comfortable-swipe-ubuntu/issues/new) to report a bug. +Create an issue [here](https://github.com/Hikari9/comfortable-swipe-ubuntu/issues/new) to report a bug. Please make sure +to add the following to your issue: + +1. Content of `.log` (found in the cloned folder) +2. Output of `sudo systemctl status comfortable-swipe.service` diff --git a/install b/install index 2108fd1..27fab2a 100755 --- a/install +++ b/install @@ -1,20 +1,33 @@ #!/bin/bash +# prefer running as root +if [ $(id -u) != "0" ]; then + sudo bash "$0" "$@" + exit $? +fi +echo "Install $(date)" >> .log DIR=$(dirname $0) PROGRAM=/usr/local/bin/comfortable-swipe +CONF_PATH=/usr/local/share/comfortable-swipe.conf +OLD_CONF_PATH=${XDG_CONFIG_HOME:-$HOME/.config}/comfortable-swipe.conf +DCONF_PATH=$DIR/src/defaults.conf if [ -x "$(command -v $PROGRAM)" ]; then # stop any running comfortable-swipe if it exists - $PROGRAM stop - # remove existing comfortable-swipe - rm $(which comfortable-swipe) + systemctl stop comfortable-swipe.service >> .log 2>> .log fi -#copy config file -mkdir -p ~/.config -DCONF_PATH=$DIR/src/defaults.conf -CONF_PATH=${XDG_CONFIG_HOME:-$HOME/.config}/comfortable-swipe.conf +mkdir -p $(dirname $CONF_PATH) + +# copy config file if [ ! -f $CONF_PATH ]; then - cat $DCONF_PATH > $CONF_PATH + if [ ! -f $OLD_CONF_PATH ]; then + # old config file not found, create from scratch + cp -a $DCONF_PATH $(dirname $CONF_PATH) >> .log 2>> .log + else + # old config file found, move to the new path + echo "Configuration copied from $OLD_CONF_PATH to $CONF_PATH" + cp -a $OLD_CONF_PATH $(dirname $CONF_PATH) >> .log 2>> .log + fi else # config file found, ask user if overwrite echo "Old conf file found in $CONF_PATH" @@ -24,18 +37,44 @@ else read -r -p "Conf file will be overwritten. Are you sure? [Y/n] " response response=${response,,} if [[ "$response" =~ ^(yes|y)$ ]]; then - cat $DCONF_PATH > $CONF_PATH + cp -a $DCONF_PATH $(dirname $CONF_PATH) >> .log 2>> .log else + echo "Installation aborted." >> .log exec echo "Installation aborted." fi fi fi + +# install with g++ echo "Installing..." -# mkdir -p ~/.local/bin -g++ -std=c++11 -O2 $DIR/src/comfortable-swipe.cpp -lxdo -o $PROGRAM || exec echo "Installation aborted" +echo "Running g++" >> .log +g++ -std=c++11 -O2 $DIR/src/comfortable-swipe.cpp -lxdo -o $PROGRAM >> .log 2>> .log || exec echo "Installation aborted" -# toggle autostart twice to refresh any changes -$PROGRAM autostart > /dev/null || exec echo "Installation aborted" -$PROGRAM autostart > /dev/null || exec echo "Installation aborted" +# add as service +SERVICE_PATH="/lib/systemd/system/comfortable-swipe.service" -echo "Successfully installed. You may now run 'comfortable-swipe start'." +echo "[Unit] +Description=Comfortable 3 or 4 finger gestures + +[Service] +Environment=DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY +ExecStart=$PROGRAM start + +[Install] +WantedBy=multi-user.target" \ +> $SERVICE_PATH + +# run immediately +systemctl daemon-reload >> .log 2>> .log +systemctl enable comfortable-swipe.service >> .log 2>> .log +systemctl start comfortable-swipe.service >> .log 2>> .log +echo "Installation successful" >> .log + +# prvide success message +echo "" +echo "Successfully installed comfortable-swipe. Try swiping and see if it works." +echo "Configuration file is located at $CONF_PATH" + +echo "" +echo "[Report bugs to https://github.com/Hikari9/comfortable-swipe-ubuntu/issues/new]" +echo "" diff --git a/src/comfortable-swipe.cpp b/src/comfortable-swipe.cpp index 20edbc3..05070ac 100644 --- a/src/comfortable-swipe.cpp +++ b/src/comfortable-swipe.cpp @@ -186,15 +186,8 @@ namespace service { // get the full path of the .conf file string conf_filename() { static string *filename = NULL; - if (filename == NULL) { - const char* xdg_config = getenv("XDG_CONFIG_HOME"); - string config( - xdg_config == NULL - ? string(getenv("HOME")) + "/.config" - : xdg_config - ); - filename = new string(config + "/comfortable-swipe.conf"); - } + if (filename == NULL) + filename = new string("/usr/local/share/comfortable-swipe.conf"); return *filename; } // get the full path of the .desktop file associated @@ -268,13 +261,13 @@ namespace service { } // starts service void start() { - int x = system("stdbuf -oL -eL libinput-debug-events | " PROGRAM " buffer"); + int x = system("/usr/bin/stdbuf -oL -eL /usr/bin/libinput-debug-events | " PROGRAM " buffer"); } // stops service void stop() { // kill all comfortable-swipe, except self char* buffer = new char[20]; - FILE* pipe = popen("pgrep -f comfortable-swipe", "r"); + FILE* pipe = popen("/usr/bin/pgrep -f comfortable-swipe", "r"); if (!pipe) throw std::runtime_error("stop command failed"); string kill = "kill"; while (!feof(pipe)) { diff --git a/uninstall b/uninstall index 487ead6..3fb1de4 100644 --- a/uninstall +++ b/uninstall @@ -1,7 +1,24 @@ #!/bin/bash +if [ $(id -u) != "0" ]; then + sudo bash "$0" "$@" + exit $? +fi echo "Uninstalling..." -rm ${XDG_CONFIG_HOME:-$HOME/.config}/autostart/comfortable-swipe.desktop 2> /dev/null -comfortable-swipe stop 2> /dev/null -rm $HOME/.local/bin/comfortable-swipe 2> /dev/null # compat -rm /usr/local/bin/comfortable-swipe 2> /dev/null +echo "Uninstall $(date)" >> .log + +# stop service +systemctl stop comfortable-swipe.service >> .log 2>> .log +systemctl disable comfortable-swipe.service >> .log 2>> .log + +# remove service +SERVICE_PATH="/lib/systemd/system/comfortable-swipe.service" +rm $SERVICE_PATH >> .log 2>> .log + +# remove old program +OLD_PROGRAM=$HOME/.local/bin/comfortable-swipe +rm $OLD_PROGRAM >> .log 2>> .log # compat + +# remove program +PROGRAM=/usr/local/bin/comfortable-swipe +rm $PROGRAM >> .log 2>> .log echo "Successfully uninstalled comfortable-swipe" \ No newline at end of file