clipnotify

Notify on new X clipboard events
git clone https://git.sinitax.com/cdown/clipnotify
Log | Files | Refs | README | LICENSE | sfeed.txt

commit 094dd7e20a06eba80c2e6f27dee775106e0eeca9
parent a9faca40b3929f22ad1e21c071c853af47b0b7bc
Author: Chris Down <chris@chrisdown.name>
Date:   Fri,  4 Nov 2022 18:11:14 +0000

Add -s option to monitor specific selections

Originally suggested by Andre Blanke in #7, with changes in this patch
around code readability and backwards compatibility.

Diffstat:
Mclipnotify.c | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 58 insertions(+), 4 deletions(-)

diff --git a/clipnotify.c b/clipnotify.c @@ -3,25 +3,79 @@ #include <X11/extensions/Xfixes.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> +#include <unistd.h> -int main(void) { +static enum selections { + NONE = 0, + SELECTION_CLIPBOARD = (1 << 0), + SELECTION_PRIMARY = (1 << 1), + SELECTION_SECONDARY = (1 << 2) +} selections = NONE; + +int main(int argc, char *argv[]) { + static const char *usage = + "%s: Notify on clipboard events.\n\n" + " -s The selection to use. Available selections:\n" + " clipboard, primary, secondary\n" + " The default is to monitor clipboard and primary.\n"; Display *disp; Window root; Atom clip; XEvent evt; + int opt; + + while ((opt = getopt(argc, argv, "hs:")) != -1) { + switch (opt) { + case 'h': + printf(usage, argv[0]); + return EXIT_SUCCESS; + case 's': { + char *token = strtok(optarg, ","); + while (token != NULL) { + if (strcmp(token, "clipboard") == 0) { + selections |= SELECTION_CLIPBOARD; + } else if (strcmp(token, "primary") == 0) { + selections |= SELECTION_PRIMARY; + } else if (strcmp(token, "secondary") == 0) { + selections |= SELECTION_SECONDARY; + } else { + fprintf(stderr, "Unknown selection '%s'\n", token); + return EXIT_FAILURE; + } + token = strtok(NULL, ","); + } + break; + } + default: + fprintf(stderr, usage, argv[0]); + return EXIT_FAILURE; + } + } disp = XOpenDisplay(NULL); if (!disp) { fprintf(stderr, "Can't open X display\n"); - exit(1); + return EXIT_FAILURE; } root = DefaultRootWindow(disp); clip = XInternAtom(disp, "CLIPBOARD", False); - XFixesSelectSelectionInput(disp, root, XA_PRIMARY, XFixesSetSelectionOwnerNotifyMask); - XFixesSelectSelectionInput(disp, root, clip, XFixesSetSelectionOwnerNotifyMask); + /* <= 1.0.2 backwards compatibility */ + if (!selections) + selections = SELECTION_CLIPBOARD | SELECTION_PRIMARY; + + if (selections & SELECTION_CLIPBOARD) + XFixesSelectSelectionInput(disp, root, clip, + XFixesSetSelectionOwnerNotifyMask); + if (selections & SELECTION_PRIMARY) + XFixesSelectSelectionInput(disp, root, XA_PRIMARY, + XFixesSetSelectionOwnerNotifyMask); + if (selections & SELECTION_SECONDARY) + XFixesSelectSelectionInput(disp, root, XA_SECONDARY, + XFixesSetSelectionOwnerNotifyMask); XNextEvent(disp, &evt); XCloseDisplay(disp);