summaryrefslogtreecommitdiffstats
path: root/tools/guest-diag/xfocus.c
blob: e0ec05dc453e735e2122660cfbb988bde56767cc (plain)
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
// xfocus.c - print the current X input focus and the gzdoom window, and try
// to focus the gzdoom window; then report again. Diagnostic for WM-less X.
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <string.h>

static Window find_named(Display *d, Window root, const char *sub,
                         char *title, size_t tl)
{
    Window r, parent, *kids = NULL;
    unsigned int nk = 0;

    title[0] = 0;
    if (!XQueryTree(d, root, &r, &parent, &kids, &nk))
        return 0;
    for (unsigned int i = 0; i < nk; i++) {
        char *name = NULL;

        if (XFetchName(d, kids[i], &name) && name) {
            if (strstr(name, sub)) {
                snprintf(title, tl, "%s", name);
                Window w = kids[i];

                XFree(name);
                if (kids)
                    XFree(kids);
                return w;
            }
            XFree(name);
        }
    }
    if (kids)
        XFree(kids);
    return 0;
}

int main(void)
{
    Display *d = XOpenDisplay(NULL);
    Window root, focus, w, croot;
    int rx, ry, wx, wy;
    unsigned int mask;
    char title[256];

    if (!d) {
        printf("no display\n");
        return 1;
    }
    root = DefaultRootWindow(d);
    w = find_named(d, root, "Freedoom", title, sizeof(title));
    XGetInputFocus(d, &focus, &rx);
    printf("gzdoom window: 0x%lx '%s'\n", w, title);
    printf("input focus:   0x%lx\n", focus);
    printf("pointer window: ");
    if (XQueryPointer(d, root, &croot, &rx /*reuse*/, &wx, &wy, &rx, &ry,
                      &mask))
        printf("0x%lx at %d,%d mask=0x%x\n", rx, wx, wy, mask);
    else
        printf("query failed\n");
    if (w) {
        XSetInputFocus(d, w, RevertToParent, CurrentTime);
        XFlush(d);
        XGetInputFocus(d, &focus, &rx);
        printf("after set:     0x%lx\n", focus);
    }
    XCloseDisplay(d);
    return 0;
}