This repository has been archived by the owner on May 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
clipped.c
77 lines (67 loc) · 1.98 KB
/
clipped.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
/* This project is licensed under the MIT License (see LICENSE). */
#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>
#ifndef VERSION
#define VERSION ""
#endif /* VERSION */
static void
print_usage(int code)
{
fputs("usage: clipped [options]\n"
"\n"
"options:\n"
" -h Show help message\n"
" -s SELECTION Monitor this selection\n"
" -v Show version\n",
code ? stderr : stdout);
exit(code);
}
int
main(int argc, char *argv[])
{
const char *selection = NULL;
int opt;
while ((opt = getopt(argc, argv, "hs:v")) != -1) {
switch (opt) {
case 'h':
print_usage(EXIT_SUCCESS);
break;
case 's':
selection = optarg;
break;
case 'v':
printf("%s\n", VERSION);
return EXIT_SUCCESS;
default:
print_usage(2);
}
}
Display *dpy = XOpenDisplay(NULL);
if (!dpy) {
fprintf(stderr, "Failed to open X display\n");
return EXIT_FAILURE;
}
Window root = DefaultRootWindow(dpy);
Atom clipboard = XInternAtom(dpy, "CLIPBOARD", False);
if (!selection || strcmp(selection, "primary") == 0)
XFixesSelectSelectionInput(dpy, root, XA_PRIMARY,
XFixesSetSelectionOwnerNotifyMask);
if (!selection || strcmp(selection, "clipboard") == 0)
XFixesSelectSelectionInput(dpy, root, clipboard,
XFixesSetSelectionOwnerNotifyMask);
XEvent ev;
while (!XNextEvent(dpy, &ev)) {
XFixesSelectionNotifyEvent *e;
e = (XFixesSelectionNotifyEvent *)&ev.xgeneric;
if (e->selection == XA_PRIMARY)
printf("PRIMARY\n");
else if (e->selection == clipboard)
printf("CLIPBOARD\n");
fflush(stdout);
}
}