-
Notifications
You must be signed in to change notification settings - Fork 0
/
wselect.c
96 lines (78 loc) · 1.97 KB
/
wselect.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 <stdio.h>
#include <stdlib.h>
#include <X11/X.h>
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#define USED(x)
#define MASK (ButtonPressMask|ButtonReleaseMask)
static char *argv0;
static void
exits(char *p)
{
fprintf(stderr, "%s: %s\n", argv0, p);
exit(EXIT_FAILURE);
}
static Window
get_window_id(Display * dpy, int screen, int button)
{
Cursor cursor;
Window root;
Window retwin = None;
int retbutton = -1;
int pressed = 0;
int idummy;
unsigned udummy;
XEvent event;
root = RootWindow(dpy, screen);
cursor = XCreateFontCursor(dpy, XC_draped_box);
if(cursor == None)
exits("unable to create selection cursor");
if(XGrabPointer(dpy, root, False, MASK, GrabModeSync, GrabModeAsync, None, cursor, CurrentTime) != GrabSuccess)
exits("unable to grab cursor");
while(retwin == None || pressed != 0) {
XAllowEvents(dpy, SyncPointer, CurrentTime);
XWindowEvent(dpy, root, MASK, &event);
switch(event.type) {
case ButtonPress:
if(retwin == None) {
retbutton = event.xbutton.button;
retwin =((event.xbutton.subwindow != None) ?
event.xbutton.subwindow : root);
}
pressed++;
break;
case ButtonRelease:
if(pressed > 0)
pressed--;
break;
}
}
XUngrabPointer(dpy, CurrentTime);
XFreeCursor(dpy, cursor);
XSync(dpy, 0);
/* Find the client window corresponding to the window that was clicked in. */
if (XGetGeometry(dpy, retwin, &root, &idummy, &idummy, &udummy, &udummy, &udummy, &udummy) && retwin != root)
retwin = XmuClientWindow(dpy, retwin);
return (button == -1 || retbutton == button) ? retwin : None;
}
extern int
main(int argc, char **argv)
{
Display * display;
int screen;
Window id;
USED(argc);
argv0 = argv[0];
display = XOpenDisplay("");
if(display == 0)
exits("can't open display");
screen = DefaultScreen(display);
id = get_window_id(display, screen, 1);
if(id == None)
return EXIT_FAILURE;
printf("%#x\n",(int) id);
return 0;
}