-
Notifications
You must be signed in to change notification settings - Fork 34
/
clipnotify.c
96 lines (86 loc) · 2.98 KB
/
clipnotify.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xfixes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
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);
}