hyprwm / contrib

Community scripts and utilities for Hypr projects
MIT License
294 stars 39 forks source link

grimblast: only allow one instance (modified) #114

Closed DHDcc closed 3 months ago

DHDcc commented 4 months ago

Description of changes

Things done

DHDcc commented 4 months ago

@fufexan I fixed it

fufexan commented 3 months ago

I don't think it's necessary to write to a file, just have it exist. This is similar to something I've tried which didn't work. Maybe we can combine the two approaches?

diff --git a/grimblast/grimblast b/grimblast/grimblast
index 3d8026c..531de5d 100755
--- a/grimblast/grimblast
+++ b/grimblast/grimblast
@@ -18,9 +18,15 @@
 ## hyprctl equivalents.
 ## https://github.com/swaywm/sway/blob/master/contrib/grimshot

+# Set up lockfile
+LOCKFILE="${XDG_RUNTIME_DIR:-$XDG_CACHE_DIR:-$HOME/.cache}/grimblast.lock"
+
 # Check whether another instance is running
-if pgrep slurp; then
+if [ -e "$LOCKFILE" ]; then
   exit 2
+# Else create a lockfile
+else
+  touch "$LOCKFILE"
 fi

 getTargetDirectory() {
@@ -153,6 +159,7 @@ killHyprpicker() {

 die() {
   killHyprpicker
+  rm "$LOCKFILE"
   MSG=${1:-Bye}
   notifyError "Error: $MSG"
   exit 2
@@ -228,7 +235,7 @@ elif [ "$SUBJECT" = "area" ]; then
   WORKSPACES="$(hyprctl monitors -j | jq -r '[(foreach .[] as $monitor (0; if $monitor.specialWorkspace.name == "" then $monitor.activeWorkspace else $monitor.specialWorkspace end)).id]')"
   WINDOWS="$(hyprctl clients -j | jq -r --argjson workspaces "$WORKSPACES" 'map(select([.workspace.id] | inside($workspaces)))')"
   # shellcheck disable=2086 # if we don't split, spaces mess up slurp
-  GEOM=$(echo "$WINDOWS" | jq -r '.[] | "\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"' | slurp $SLURP_ARGS)
+  GEOM=$(echo "$WINDOWS" | jq -r '.[] | "\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"' | slurp $SLURP_ARGS || die "selection cancelled")

   # Check if user exited slurp without selecting the area
   if [ -z "$GEOM" ]; then
@@ -278,3 +285,4 @@ else
 fi

 killHyprpicker
+rm "$LOCKFILE"
DHDcc commented 3 months ago

I tried this and it's working for me. Can you check if it's also working for you @fufexan? :

# Check whether another instance is running

grimblastInstanceCheck="${XDG_RUNTIME_DIR:-$XDG_CACHE_DIR:-$HOME/.cache}/grimblast.lock"
if [[ -e "$grimblastInstanceCheck" ]]; then
        exit 2
else
        touch "$grimblastInstanceCheck"
fi
trap "rm -f '$grimblastInstanceCheck'" EXIT
fufexan commented 3 months ago

That seems to work, as long as I don't launch using ./grimblast copy area & ./grimblast copy area.

DHDcc commented 3 months ago

I can't think of a fix for that... But I don't think people are going to do that anyways.

fufexan commented 3 months ago

All good, just needs the Changelog entry.

DHDcc commented 3 months ago

Done @fufexan