summaryrefslogtreecommitdiffstats
path: root/tools/guest-diag/xresize.c
blob: 877f0e9f7fb77e16d1dd259fd953330aa62c911b (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
// SPDX-License-Identifier: BSD-3-Clause
// xresize.c - find a window by name substring and resize/move it (Xvfb has
// no WM, so a client-side XMoveResizeWindow is the only way to force the
// game window onto the screen).
// Usage: xresize <display> <name-substr> <w> <h> <x> <y>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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

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

        if (XFetchName(d, kids[i], &name) && name) {
            int hit = strstr(name, sub) != NULL;

            XFree(name);
            if (hit) {
                Window w = kids[i];

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

int main(int argc, char **argv)
{
    Display *d;
    Window w;

    if (argc < 6) {
        fprintf(stderr, "usage: xresize <display> <name> <w> <h> <x> <y>\n");
        return 2;
    }
    d = XOpenDisplay(argv[1]);
    if (!d) {
        fprintf(stderr, "no display\n");
        return 1;
    }
    w = find_named(d, DefaultRootWindow(d), argv[2]);
    if (!w) {
        fprintf(stderr, "window '%s' not found\n", argv[2]);
        return 1;
    }
    XMoveResizeWindow(d, w, atoi(argv[5]), atoi(argv[6]), atoi(argv[3]),
                      atoi(argv[4]));
    XFlush(d);
    printf("resized 0x%lx to %sx%s at %s,%s\n", w, argv[3], argv[4],
           argv[5], argv[6]);
    XCloseDisplay(d);
    return 0;
}