-
Notifications
You must be signed in to change notification settings - Fork 33
/
pdw.c
81 lines (61 loc) · 1.44 KB
/
pdw.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
/* See LICENSE file for copyright and license details. */
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>
#include "util.h"
static xcb_connection_t *conn;
static xcb_screen_t *scrn;
static void usage(char *);
static xcb_window_t focus_window(void);
static xcb_window_t parent(xcb_window_t);
static void
usage(char *name)
{
fprintf(stderr, "usage: %s [wid]\n", name);
exit(1);
}
static xcb_window_t
focus_window(void)
{
xcb_window_t w = 0;
xcb_get_input_focus_cookie_t c;
xcb_get_input_focus_reply_t *r;
c = xcb_get_input_focus(conn);
r = xcb_get_input_focus_reply(conn, c, NULL);
if (r == NULL)
errx(1, "xcb_get_input_focus");
w = r->focus;
free(r);
if (w == XCB_NONE || w == XCB_INPUT_FOCUS_POINTER_ROOT)
errx(1, "focus not set");
return w;
}
static xcb_window_t
parent(xcb_window_t w)
{
xcb_query_tree_cookie_t c;
xcb_query_tree_reply_t *r;
c = xcb_query_tree(conn, w);
r = xcb_query_tree_reply(conn, c, NULL);
if (r == NULL)
errx(1, "failed to get parent");
w = (!r->parent || r->parent == scrn->root) ? w : parent(r->parent);
free(r);
return w;
}
int
main(int argc, char **argv)
{
xcb_window_t w;
if (argc > 2 || (argc > 1 && (!strncmp(argv[1], "-h", 2))))
usage(argv[0]);
init_xcb(&conn);
get_screen(conn, &scrn);
w = (argc > 1) ? strtoul(argv[1], NULL, 16) : focus_window();
printf("0x%08x\n", parent(w));
kill_xcb(&conn);
return 0;
}