commit 8906e1d96c2a7a16a305043bfc942f5356edf596
parent 3704ce34272b5ae0fc36a6bd63d24db00cece33f
Author: Chris Down <chris@chrisdown.name>
Date: Mon, 19 Feb 2018 15:12:46 +0000
Merge branch 'async' into develop
Diffstat:
M | clipmenud | | | 61 | +++++++++++++++++++++++++------------------------------------ |
1 file changed, 25 insertions(+), 36 deletions(-)
diff --git a/clipmenud b/clipmenud
@@ -10,7 +10,7 @@ major_version=4
cache_dir=$CM_DIR/clipmenu.$major_version.$USER/
cache_file=$cache_dir/line_cache
lock_file=$cache_dir/lock
-lock_timeout=2
+lock_timeout=5
has_clipnotify=0
xsel_log=/dev/null
@@ -85,8 +85,6 @@ fi
# shellcheck disable=SC2174
mkdir -p -m0700 "$cache_dir"
-declare -A last_data
-
command -v clipnotify >/dev/null 2>&1 && has_clipnotify=1
if ! (( has_clipnotify )); then
@@ -99,6 +97,22 @@ exec {lock_fd}> "$lock_file"
sleep_cmd=(sleep "${CM_SLEEP:-0.5}")
while true; do
+ # We need to take ownership synchronously before we run `clipnotify` as
+ # otherwise we could enter an infinite loop.
+ if (( CM_OWN_CLIPBOARD )); then
+ # Take ownership of the clipboard, in case the original application
+ # is unable to serve the clipboard request (due to being suspended,
+ # etc).
+ #
+ # Primary is excluded from the change of ownership as applications
+ # sometimes act up if clipboard focus is taken away from them --
+ # for example, urxvt will unhilight text, which is undesirable.
+ #
+ # We need to check if the clipboard is empty to mitigate #34.
+ data=$(_xsel -o --clipboard; printf x)
+ [[ $data != x ]] && _xsel -i --clipboard <<< "${data%x}"
+ fi
+
if ! (( CM_ONESHOT )); then
if (( has_clipnotify )); then
# Fall back to polling if clipnotify fails
@@ -109,6 +123,7 @@ while true; do
fi
fi
+ {
if ! flock -x -w "$lock_timeout" "$lock_fd"; then
if (( CM_ONESHOT )); then
printf 'ERROR: %s\n' 'Timed out waiting for lock' >&2
@@ -137,44 +152,16 @@ while true; do
continue
fi
- if [[ ${last_data[$selection]} == "$data" ]]; then
- debug 'Skipping as last selection is the same as this one'
- continue
- fi
-
- last_data[$selection]=$data
-
first_line=$(get_first_line "$data")
debug "New clipboard entry on $selection selection: \"$first_line\""
- # Without checking ${last_data[any]}, we often double write since both
- # selections get the same content
- if [[ ${last_data[any]} != "$data" ]]; then
- filename="$cache_dir/$(cksum <<< "$first_line")"
- debug "Writing $data to $filename"
- printf '%s' "$data" > "$filename"
+ filename="$cache_dir/$(cksum <<< "$first_line")"
+ debug "Writing $data to $filename"
+ printf '%s' "$data" > "$filename"
- debug "Writing $first_line to $cache_file"
- printf '%s\n' "$first_line" >> "$cache_file"
- fi
-
- last_data[any]=$data
-
- if (( CM_OWN_CLIPBOARD )) && [[ $selection != primary ]]; then
- # Take ownership of the clipboard, in case the original application
- # is unable to serve the clipboard request (due to being suspended,
- # etc).
- #
- # Primary is excluded from the change of ownership as applications
- # sometimes act up if clipboard focus is taken away from them --
- # for example, urxvt will unhilight text, which is undesirable.
- #
- # We can't colocate this with the above copying code because
- # https://github.com/cdown/clipmenu/issues/34 requires knowing if
- # we would skip first.
- _xsel -o --"$selection" | _xsel -i --"$selection"
- fi
+ debug "Writing $first_line to $cache_file"
+ printf '%s\n' "$first_line" >> "$cache_file"
if (( CM_MAX_CLIPS )); then
mapfile -t to_remove < <(
@@ -193,8 +180,10 @@ while true; do
done
flock -u "$lock_fd"
+ } &
if (( CM_ONESHOT )); then
+ wait
debug 'Oneshot mode enabled, exiting'
break
fi