Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grimblast: only allow one instance (modified) #114

Merged
merged 4 commits into from
Aug 2, 2024
Merged

grimblast: only allow one instance (modified) #114

merged 4 commits into from
Aug 2, 2024

Conversation

DHDcc
Copy link
Contributor

@DHDcc DHDcc commented Jul 30, 2024

Description of changes

Things done

  • For new programs

    • Add the program to the README table, with yourself as the
      maintainer
    • Add a README for the program itself
    • Add Makefile (and Nix derivation optionally, otherwise @fufexan will do
      it)
    • If the program is a script, add it to the
      CI checks matrix
  • For changes

@DHDcc
Copy link
Contributor Author

DHDcc commented Jul 30, 2024

@fufexan I fixed it

@DHDcc DHDcc closed this Jul 30, 2024
@DHDcc DHDcc reopened this Jul 30, 2024
@DHDcc DHDcc changed the title grimblast: changed how to allow one instance grimblast: allow one instance (fix) Jul 30, 2024
@DHDcc DHDcc changed the title grimblast: allow one instance (fix) grimblast: only allow one instance (modified) Jul 30, 2024
@fufexan
Copy link
Member

fufexan commented Aug 2, 2024

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
Copy link
Contributor Author

DHDcc commented Aug 2, 2024

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
Copy link
Member

fufexan commented Aug 2, 2024

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

@DHDcc
Copy link
Contributor Author

DHDcc commented Aug 2, 2024

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

@fufexan
Copy link
Member

fufexan commented Aug 2, 2024

All good, just needs the Changelog entry.

@DHDcc
Copy link
Contributor Author

DHDcc commented Aug 2, 2024

Done @fufexan

@fufexan fufexan merged commit 79e18aa into hyprwm:grimblast Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants