diff --git a/README.md b/README.md index d3f96db..3b421bc 100644 --- a/README.md +++ b/README.md @@ -26,14 +26,14 @@ there, but it basically works like this: ## clipmenud -1. `clipmenud` polls the clipboard every 0.5 seconds (or another interval as - configured with the `CM_SLEEP` environment variable). Unfortunately there's - no interface to subscribe for changes in X11, so we must poll. +1. `clipmenud` uses [clipnotify](https://github.com/cdown/clipnotify) to wait + for new clipboard events. If clipnotify is not present on the system, we + poll every 0.5 seconds (or another interval as configured with the + `CM_SLEEP` environment variable). - Instead of polling, you can bind your copy key binding to also issue - `CM_ONESHOT=1 clipmenud`. However, there's no generic way to do this, since - any keys or mouse buttons could be bound to do this action in a number of - ways. + You can also bind your copy key binding to also issue `CM_ONESHOT=1 + clipmenud`. However, there's no generic way to do this, since any keys or + mouse buttons could be bound to do this action in a number of ways. 2. If `clipmenud` detects changes to the clipboard contents, it writes them out to the cache directory. diff --git a/clipmenud b/clipmenud index d43d806..5ee6e00 100755 --- a/clipmenud +++ b/clipmenud @@ -11,6 +11,7 @@ cache_dir=$CM_DIR/clipmenu.$major_version.$USER/ cache_file=$cache_dir/line_cache lock_file=$cache_dir/lock lock_timeout=2 +has_clipnotify=0 xsel_log=/dev/null for file in /proc/self/fd/2 /dev/stderr; do @@ -86,9 +87,25 @@ mkdir -p -m0700 "$cache_dir" declare -A last_data +command -v clipnotify >/dev/null 2>&1 && has_clipnotify=1 + +if ! (( has_clipnotify )); then + echo "WARN: Consider installing clipnotify for better performance." >&2 + echo "WARN: See https://github.com/cdown/clipnotify." >&2 +fi + exec {lock_fd}> "$lock_file" -while (( CM_ONESHOT )) || sleep "${CM_SLEEP:-0.5}"; do +while true; do + if ! (( CM_ONESHOT )); then + if (( has_clipnotify )); then + clipnotify + else + # Use old polling method + sleep "${CM_SLEEP:-0.5}" + 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