Repository: cdown/clipnotify Branch: master Commit: 25ba143c7da8 Files: 4 Total size: 4.6 KB Directory structure: gitextract_ocwvpy70/ ├── LICENSE ├── Makefile ├── README.md └── clipnotify.c ================================================ FILE CONTENTS ================================================ ================================================ FILE: LICENSE ================================================ This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. ================================================ FILE: Makefile ================================================ PREFIX ?= /usr/local x11_bsd_flags = -I/usr/X11R6/include -L/usr/X11R6/lib all: ${CC} ${CFLAGS} ${LDFLAGS} clipnotify.c -o clipnotify $(x11_bsd_flags) -lX11 -lXfixes install: all mkdir -p ${DESTDIR}${PREFIX}/bin cp -f clipnotify ${DESTDIR}${PREFIX}/bin chmod 755 ${DESTDIR}${PREFIX}/bin/clipnotify uninstall: rm -f ${DESTDIR}${PREFIX}/bin/clipnotify clean: rm -f *.o *~ clipnotify ================================================ FILE: README.md ================================================ clipnotify is a simple program that, using the [XFIXES](https://cgit.freedesktop.org/xorg/proto/fixesproto/plain/fixesproto.txt) extension to X11, waits until a new selection is available and then exits. It was primarily designed for [clipmenu](https://github.com/cdown/clipmenu), to avoid polling for new selections. Here's how it's intended to be used: while read; do [an event happened, do something with the selection] done < <(clipnotify -l) Or: while clipnotify; do [an event happened, do something with the selection] done clipnotify doesn't try to print anything about the contents of the selection, it just exits when it changes. This is intentional -- X11's selection API is verging on the insane, and there are plenty of others who have already lost their sanity to bring us xclip/xsel/etc. Use one of those tools to complement clipnotify. You can choose a particular selection with `-s`, and loop instead of exiting with `-l`. See `clipmenu -h` for more information. ================================================ FILE: clipnotify.c ================================================ #include #include #include #include #include #include #include static enum selections { NONE = 0, SELECTION_CLIPBOARD = (1 << 0), SELECTION_PRIMARY = (1 << 1), SELECTION_SECONDARY = (1 << 2) } selections = NONE; static int loop; int main(int argc, char *argv[]) { static const char *usage = "%s: Notify by exiting on clipboard events.\n\n" " -l Instead of exiting, print a newline when a new selection is available.\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:l")) != -1) { switch (opt) { case 'h': printf(usage, argv[0]); return EXIT_SUCCESS; case 'l': loop = 1; break; 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"); return EXIT_FAILURE; } root = DefaultRootWindow(disp); clip = XInternAtom(disp, "CLIPBOARD", False); /* <= 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); if (loop) { (void)setvbuf(stdout, NULL, _IONBF, 0); do { XNextEvent(disp, &evt); printf("\n"); } while (1); } else { XNextEvent(disp, &evt); } XCloseDisplay(disp); }