From f797290174e3a8ee7c17ea2689ae4ab5841ee1c5 Mon Sep 17 00:00:00 2001 From: Rico Tiongson Date: Mon, 13 Nov 2017 23:58:05 +0800 Subject: [PATCH] [feature-systemd] Fix autostart errors by handling Environment variables --- install | 7 ++++++- src/comfortable-swipe.cpp | 29 +++++++++++++++++++++++------ uninstall | 3 ++- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/install b/install index 92b077e..ccf639b 100755 --- a/install +++ b/install @@ -48,6 +48,8 @@ fi # install with g++ echo "Installing..." +echo " +Install $(date)" >> .log echo "Running g++" >> .log g++ -std=c++11 -O2 $DIR/src/comfortable-swipe.cpp -lxdo -o $PROGRAM >> .log 2>> .log || exec echo "Installation aborted" @@ -56,10 +58,13 @@ SERVICE_PATH="/lib/systemd/system/comfortable-swipe.service" echo "[Unit] Description=Comfortable 3 or 4 finger gestures +After=display-manager.service [Service] Environment=DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY -ExecStart=/bin/sh -c '/usr/bin/stdbuf -oL -eL /usr/bin/libinput-debug-events | $PROGRAM buffer' +ExecStart=$PROGRAM exec +Restart=always +RestartSec=2 [Install] WantedBy=multi-user.target" \ diff --git a/src/comfortable-swipe.cpp b/src/comfortable-swipe.cpp index e9b80c8..722c582 100644 --- a/src/comfortable-swipe.cpp +++ b/src/comfortable-swipe.cpp @@ -66,9 +66,10 @@ namespace service { void buffer(); void start(); void stop(); + void status(); void restart(); - void autostart(); void help(); + void exec(); } /* MAIN DRIVER FUNCTION */ @@ -79,8 +80,10 @@ int main(int argc, char** args) { // select based on argument if (arg == "start") service::start(); else if (arg == "stop") service::stop(); + else if (arg == "status") service::status(); else if (arg == "restart") service::restart(); else if (arg == "buffer") service::buffer(); + else if (arg == "exec") service::exec(); else service::help(); } else { service::help(); @@ -191,7 +194,11 @@ namespace service { } } -namespace service { +namespace service { + void exec() { + sleep(1); + exit(system("/usr/bin/stdbuf -oL -eL /usr/bin/libinput-debug-events 2>&1 | " PROGRAM " buffer")); + } // parses output from libinput-debug-events void buffer() { // check first if $user @@ -216,6 +223,11 @@ namespace service { config["down4"].c_str() ); while (getline(cin, sentence)) { + // exit on core dump + if (sentence.find("Segmentation fault") != string::npos) + exit(EXIT_FAILURE); + if (sentence.find("Error") != string::npos) + exit(EXIT_FAILURE); auto data = sentence.data(); cmatch matches; if (regex_match(data, matches, gesture_begin)) { @@ -244,22 +256,27 @@ namespace service { } // starts service void start() { - int x = system("systemctl start comfortable-swipe.service"); + exit(system("systemctl start comfortable-swipe.service")); } // stops service void stop() { - int x = system("systemctl stop comfortable-swipe.service"); + exit(system("systemctl stop comfortable-swipe.service")); + } + // shows the status of the program + void status() { + exit(system("systemctl status comfortable-swipe.service")); } // stops then starts service void restart() { - int x = system("systemctl restart comfortable-swipe.service"); + exit(system("systemctl restart comfortable-swipe.service")); } // shows help void help() { - puts("comfortable-swipe [start|stop|restart|buffer|help]"); + puts("comfortable-swipe [start|stop|status|restart|buffer|help]"); puts(""); puts("start - starts 3/4-finger gesture service"); puts("stop - stops 3/4-finger gesture service"); + puts("status - shows the status of the program"); puts("restart - stops then starts 3/4-finger gesture service"); puts("buffer - parses output of libinput-debug-events"); puts("help - shows the help dialog"); diff --git a/uninstall b/uninstall index 74e365e..1c4912b 100644 --- a/uninstall +++ b/uninstall @@ -4,7 +4,8 @@ if [ $(id -u) != "0" ]; then exit $? fi echo "Uninstalling..." -echo "Uninstall $(date)" >> .log +echo " +Uninstall $(date)" >> .log # stop service systemctl stop comfortable-swipe.service >> .log 2>> .log