`jobs -p' should be read into an array, as it might contain more than
one job.
[cdown: squash, massage changelog, remove space between local/readarray]
Since trapping a signal breaks `wait $_CM_CLIPNOTIFY_PID` w/o killing
the clipnotify job, call kill_background_jobs before restarting the
loop. This prevents `clipctl enable` from spawning another job.
Only set `$launcher_exit` if not running rofi-script, and only exit using `$launcher_exit` if it has been set. Otherwise, the last line (now with quoting around the variable) becomes `exit ""`, which causes an error.
This change is related to https://github.com/cdown/clipmenu/issues/57#issuecomment-740123283.
If `clipmenu` "preserves" the exit code from the launcher (exits with the same code as the launcher), the exit code's from Rofi's custom keybindings can be used with `clipmenu`.
For example, I'm using the following script bound to `Super+v`:
```
#!/bin/bash
trap "exit" INT
# Run clipmenu
CM_LAUNCHER=rofi clipmenu -p "Paste" -mesg "Use Shift+Delete to delete an item" \
-kb-delete-entry "" -kb-custom-1 "Shift+Delete" \
-kb-accept-alt "" -kb-custom-2 "Shift+Return"
exit_code=$?
case $exit_code in
0) xdotool key "shift+Insert" ;;
10) clipdel -d ^"$(xsel -b)"$; "$0" ;;
*) exit $exit_code ;;
esac
```
With the above script, I have 3 different options when running `clipmenu`:
- if I press `Return`, the selected item is pasted to where my cursor currently is (a bit hackily with `xdotool`)
- if I press `Shift+Return`, the selected item is sent to the clipboard ("default" `clipmenu` behavior)
- if I press `Shift+Delete`, the selected item is deleted from `clipmenu`, and the script runs itself again (essentially keeps `clipmenu` open)
In order for this to work, the only change needed in `clipmenu` is to preserve the exit codes from the custom keybindings.
Nowadays the desktop pushes the appropriate DISPLAY and WAYLAND_DISPLAY
values into the per-user systemd environment, so this isn't needed.
Also see #131.
For some reason, .* is really taxing for pgrep, but if you remove it
it's way faster...
% \time -v pgrep -nf '.*clipmenud$'
325286
Command being timed: "pgrep -nf .*clipmenud$"
User time (seconds): 0.83
System time (seconds): 0.00
Percent of CPU this job got: 100%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.83
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 7632
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 1418
Voluntary context switches: 1
Involuntary context switches: 3
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
% \time -v pgrep -nf 'clipmenud$'
325286
Command being timed: "pgrep -nf clipmenud$"
User time (seconds): 0.04
System time (seconds): 0.00
Percent of CPU this job got: 100%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.04
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 7320
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 1257
Voluntary context switches: 1
Involuntary context switches: 7
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
We also don't explain too much about systemd -- you're expected to find
out or know about that agnostically of this project.
Also some features that we don't really want to advertise super widely
are not worth mentioning here.
This allows avoiding having to delete after the fact for things like
issues #57 and #98.
Why have this over just stopping clipmenud? Well:
1. Stopping clipmenud should usually be an init system action, but we
are init-system agnostic. If we just exit, we don't have a way of
reliably starting again.
2. Even if we *do* do it using the init system, we don't want some
things (like a lingering xsel which owns the selection for
CM_OWN_CLIPBOARD) being killed as well.
3. This is a nicer interface for things like password managers to stop
clipmenu rather than stopping clipmenu entirely.