From aefd5ae9bc87a56081462ee56c50f5ba722d26e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20Mei=C3=9Felbach?= Date: Thu, 6 Feb 2020 23:57:42 +0100 Subject: Add function to list and execute application desktop files from XDG autostart folders. Fixes #16 --- sway-launcher-desktop.sh | 54 +++- tests/autostart.bats | 14 + .../autostart-folders/0/autostart/firefox.desktop | 340 +++++++++++++++++++++ .../autostart-folders/1/autostart/firefox.desktop | 340 +++++++++++++++++++++ .../autostart-folders/1/autostart/htop.desktop | 65 ++++ tests/generate-command.bats | 9 +- 6 files changed, 818 insertions(+), 4 deletions(-) create mode 100644 tests/autostart.bats create mode 100644 tests/data/autostart-folders/0/autostart/firefox.desktop create mode 100644 tests/data/autostart-folders/1/autostart/firefox.desktop create mode 100644 tests/data/autostart-folders/1/autostart/htop.desktop diff --git a/sway-launcher-desktop.sh b/sway-launcher-desktop.sh index 4b0a52b..6e1d055 100755 --- a/sway-launcher-desktop.sh +++ b/sway-launcher-desktop.sh @@ -18,8 +18,8 @@ GLYPH_DESKTOP=" " CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/sway-launcher-desktop" PROVIDERS_FILE="${PROVIDERS_FILE:=providers.conf}" if [[ "${PROVIDERS_FILE#/}" == "${PROVIDERS_FILE}" ]]; then - # $PROVIDERS_FILE is a relative path, prepend $CONFIG_DIR - PROVIDERS_FILE="${CONFIG_DIR}/${PROVIDERS_FILE}" + # $PROVIDERS_FILE is a relative path, prepend $CONFIG_DIR + PROVIDERS_FILE="${CONFIG_DIR}/${PROVIDERS_FILE}" fi # Provider config entries are separated by the field separator \034 and have the following structure: @@ -178,8 +178,56 @@ function generate-command() { }' "$1" } +function autostart() { + for application in $(list-autostart); do + (exec setsid /bin/sh -c "$(run-desktop "${application}")" &>/dev/null &) + done +} + +function list-autostart() { + # Get locations of desktop application folders according to spec + # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html + IFS=':' read -ra DIRS <<<"${XDG_CONFIG_HOME-${HOME}/.config}:${XDG_CONFIG_DIRS-/etc/xdg}" + for i in "${!DIRS[@]}"; do + if [[ ! -d "${DIRS[i]}" ]]; then + unset -v 'DIRS[$i]' + else + DIRS[$i]="${DIRS[i]}/autostart/*.desktop" + fi + done + + # shellcheck disable=SC2068 + awk -v pre="$GLYPH_DESKTOP" -F= ' + function desktopFileID(filename){ + sub("^.*autostart/", "", filename); + sub("/", "-", filename); + return filename + } + BEGINFILE{ + application=0; + block=""; + a=0 + + id=desktopFileID(FILENAME) + if(id in fileIds){ + nextfile; + }else{ + fileIds[id]=0 + } + } + /^\[Desktop Entry\]/{block="entry"} + /^Type=Application/{application=1} + /^Name=/{ iname=$2 } + ENDFILE{ + if (application){ + print FILENAME; + } + }' \ + ${DIRS[@]}