From 8cb7f9f7694fca053448b853349fb5a503f04c1b Mon Sep 17 00:00:00 2001 From: Alexander Hoem Rosbach Date: Tue, 6 Jun 2023 15:24:22 +0200 Subject: [PATCH 1/2] Check autostart condition before launching --- sway-launcher-desktop.sh | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/sway-launcher-desktop.sh b/sway-launcher-desktop.sh index 39d8a61..99c3b35 100755 --- a/sway-launcher-desktop.sh +++ b/sway-launcher-desktop.sh @@ -201,9 +201,27 @@ function generate-command() { }' "$1" } +function shouldAutostart() { + local condition="$(cat $1 | grep "AutostartCondition" | cut -d'=' -f2)" + local filename="${XDG_CONFIG_HOME-${HOME}/.config}/${condition#* }" + case $condition in + if-exists*) + [[ -e $filename ]] + ;; + unless-exists*) + [[ ! -e $filename ]] + ;; + *) + return 0 + ;; + esac +} + function autostart() { for application in $(list-autostart); do - (exec setsid /bin/sh -c "$(run-desktop "${application}")" &>/dev/null &) + if shouldAutostart "$application" ; then + (exec setsid /bin/sh -c "$(run-desktop "${application}")" &>/dev/null &) + fi done } From e37172c5915c16448c23ffd7d88e4785d8c608ec Mon Sep 17 00:00:00 2001 From: Alexander Hoem Rosbach Date: Tue, 6 Jun 2023 16:50:22 +0200 Subject: [PATCH 2/2] add tests --- tests/autostart-condition.bats | 20 +++++++++++++++++++ .../.gsd-keyboard.settings-ported | 0 .../autostart/if-exists.file-exists.desktop | 12 +++++++++++ .../if-exists.file-not-exists.desktop | 12 +++++++++++ .../unless-exists.file-exists.desktop | 12 +++++++++++ .../unless-exists.file-not-exists.desktop | 12 +++++++++++ .../condition-home/marker-file.txt | 1 + 7 files changed, 69 insertions(+) create mode 100644 tests/autostart-condition.bats create mode 100644 tests/data/autostart-folders/condition-home/.gsd-keyboard.settings-ported create mode 100644 tests/data/autostart-folders/condition-home/autostart/if-exists.file-exists.desktop create mode 100644 tests/data/autostart-folders/condition-home/autostart/if-exists.file-not-exists.desktop create mode 100644 tests/data/autostart-folders/condition-home/autostart/unless-exists.file-exists.desktop create mode 100644 tests/data/autostart-folders/condition-home/autostart/unless-exists.file-not-exists.desktop create mode 100644 tests/data/autostart-folders/condition-home/marker-file.txt diff --git a/tests/autostart-condition.bats b/tests/autostart-condition.bats new file mode 100644 index 0000000..070d630 --- /dev/null +++ b/tests/autostart-condition.bats @@ -0,0 +1,20 @@ +#!/usr/bin/env bats + +setup() { + export DID_RUN="$(mktemp -d)" + export XDG_CONFIG_HOME=./data/autostart-folders/condition-home + export XDG_CONFIG_DIRS= +} + +teardown() { + rm -r $DID_RUN +} + +@test "Only starts applications with passing AutostartCondition" { + run ../sway-launcher-desktop.sh autostart 3>&2 + echo -e "DID_RUN=$DID_RUN" + [[ ! -e $DID_RUN/unless-exists.file-exists ]] + [[ -e $DID_RUN/unless-exists.file-not-exists ]] + [[ ! -e $DID_RUN/if-exists.file-not-exists ]] + [[ -e $DID_RUN/if-exists.file-exists ]] +} diff --git a/tests/data/autostart-folders/condition-home/.gsd-keyboard.settings-ported b/tests/data/autostart-folders/condition-home/.gsd-keyboard.settings-ported new file mode 100644 index 0000000..e69de29 diff --git a/tests/data/autostart-folders/condition-home/autostart/if-exists.file-exists.desktop b/tests/data/autostart-folders/condition-home/autostart/if-exists.file-exists.desktop new file mode 100644 index 0000000..759321c --- /dev/null +++ b/tests/data/autostart-folders/condition-home/autostart/if-exists.file-exists.desktop @@ -0,0 +1,12 @@ +[Desktop Entry] +Name=Initial Setup +Exec=touch $DID_RUN/if-exists.file-exists +Terminal=false +Type=Application +StartupNotify=true +Categories=GNOME;GTK;System; +OnlyShowIn=GNOME;Unity; +NoDisplay=true +AutostartCondition=if-exists marker-file.txt +X-GNOME-HiddenUnderSystemd=true +X-Ubuntu-Gettext-Domain=gnome-initial-setup diff --git a/tests/data/autostart-folders/condition-home/autostart/if-exists.file-not-exists.desktop b/tests/data/autostart-folders/condition-home/autostart/if-exists.file-not-exists.desktop new file mode 100644 index 0000000..b506b96 --- /dev/null +++ b/tests/data/autostart-folders/condition-home/autostart/if-exists.file-not-exists.desktop @@ -0,0 +1,12 @@ +[Desktop Entry] +Name=Initial Setup +Exec=touch $DID_RUN/if-exists.file-not-exists +Terminal=false +Type=Application +StartupNotify=true +Categories=GNOME;GTK;System; +OnlyShowIn=GNOME;Unity; +NoDisplay=true +AutostartCondition=if-exists non-existing-file.txt +X-GNOME-HiddenUnderSystemd=true +X-Ubuntu-Gettext-Domain=gnome-initial-setup diff --git a/tests/data/autostart-folders/condition-home/autostart/unless-exists.file-exists.desktop b/tests/data/autostart-folders/condition-home/autostart/unless-exists.file-exists.desktop new file mode 100644 index 0000000..1bc694f --- /dev/null +++ b/tests/data/autostart-folders/condition-home/autostart/unless-exists.file-exists.desktop @@ -0,0 +1,12 @@ +[Desktop Entry] +Name=Initial Setup +Exec=touch $DID_RUN/unless-exists.file-exists +Terminal=false +Type=Application +StartupNotify=true +Categories=GNOME;GTK;System; +OnlyShowIn=GNOME;Unity; +NoDisplay=true +AutostartCondition=unless-exists marker-file.txt +X-GNOME-HiddenUnderSystemd=true +X-Ubuntu-Gettext-Domain=gnome-initial-setup diff --git a/tests/data/autostart-folders/condition-home/autostart/unless-exists.file-not-exists.desktop b/tests/data/autostart-folders/condition-home/autostart/unless-exists.file-not-exists.desktop new file mode 100644 index 0000000..4c2c67e --- /dev/null +++ b/tests/data/autostart-folders/condition-home/autostart/unless-exists.file-not-exists.desktop @@ -0,0 +1,12 @@ +[Desktop Entry] +Name=Initial Setup +Exec=touch $DID_RUN/unless-exists.file-not-exists +Terminal=false +Type=Application +StartupNotify=true +Categories=GNOME;GTK;System; +OnlyShowIn=GNOME;Unity; +NoDisplay=true +AutostartCondition=unless-exists non-existing-file.txt +X-GNOME-HiddenUnderSystemd=true +X-Ubuntu-Gettext-Domain=gnome-initial-setup diff --git a/tests/data/autostart-folders/condition-home/marker-file.txt b/tests/data/autostart-folders/condition-home/marker-file.txt new file mode 100644 index 0000000..7cfab5b --- /dev/null +++ b/tests/data/autostart-folders/condition-home/marker-file.txt @@ -0,0 +1 @@ +yes