Merge branch 'async' into develop

This commit is contained in:
Chris Down 2018-02-19 15:12:46 +00:00
commit 8906e1d96c

View file

@ -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