-
Notifications
You must be signed in to change notification settings - Fork 0
/
xclipowner.c
135 lines (126 loc) · 2.65 KB
/
xclipowner.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xfixes.h>
#include "util.h"
static void
usage(void)
{
(void)fprintf(stderr, "usage: xclipowner [-pw]\n");
exit(EXIT_FAILURE);
}
void
printwinid(Window win)
{
(void)printf("0x%08lX\n", win);
fflush(stdout);
}
int
printowner(Display *display, Window window, int xfixes, Atom selection)
{
(void)xfixes;
(void)window;
printwinid(XGetSelectionOwner(display, selection));
return 1;
}
int
watchowner(Display *display, Window window, int xfixes, Atom selection)
{
XEvent xev;
XFixesSelectionNotifyEvent *xselev;
Atom manageratom;
Window manager;
manageratom = XInternAtom(display, "CLIPBOARD_MANAGER", False);
if (manageratom == None) {
warnx("could not intern atom");
return 0;
}
XFixesSelectSelectionInput(
display,
window,
manageratom,
XFixesSetSelectionOwnerNotifyMask
);
manager = XGetSelectionOwner(display, manageratom);
printowner(display, window, xfixes, selection);
while (!XNextEvent(display, &xev)) switch (xev.type) {
case DestroyNotify:
if (xev.xdestroywindow.window != window)
break;
return 1;
default:
if (xev.type != xfixes + XFixesSelectionNotify)
break;
xselev = (XFixesSelectionNotifyEvent *)&xev;
if (xselev->selection == manageratom) {
manager = xselev->owner;
break;
}
if (xselev->selection != selection)
break;
if (xselev->owner != manager)
printwinid(xselev->owner);
break;
}
/* unreachable */
return 0;
}
int
main(int argc, char *argv[])
{
int (*func)(Display *, Window, int, Atom);
Display *display = NULL;
Window window = None;
Atom selection = None;
int exitval = EXIT_FAILURE;
int xfixes, ch, i;
#if __OpenBSD__
if (pledge("unix stdio rpath", NULL) == -1)
err(EXIT_FAILURE, "pledge");
#endif
func = &printowner;
while ((ch = getopt(argc, argv, "pw")) != -1) {
switch (ch) {
case 'p':
selection = XA_PRIMARY;
break;
case 'w':
func = &watchowner;
break;
default:
usage();
break;
}
}
if (!xinit(&display, &window))
goto error;
#if __OpenBSD__
if (pledge("stdio", NULL) == -1)
err(EXIT_FAILURE, "pledge");
#endif
if (!XFixesQueryExtension(display, &xfixes, &i)) {
warnx("could not use XFixes");
goto error;
}
if (selection == None)
selection = XInternAtom(display, "CLIPBOARD", False);
if (selection == None) {
warnx("could not intern atom");
goto error;
}
XFixesSelectSelectionInput(
display,
window,
selection,
XFixesSetSelectionOwnerNotifyMask
);
if (!(*func)(display, window, xfixes, selection))
goto error;
exitval = EXIT_SUCCESS;
error:
xclose(display, window);
return exitval;
}