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
|
// SPDX-License-Identifier: BSD-3-Clause
// xtest_move.c - drive the game with XSendEvent-synthetic keys (SDL2 on this
// Xorg ignores XTEST-faked keys; it accepts send_event KeyPress/KeyRelease).
// Finds the game window by name, then holds forward+fire and alternates
// turning left/right so the captured footage has constant motion.
// Usage: xtest_move <display> <seconds>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static Display *d;
static Window game;
static Window find_named(const char *sub)
{
Window root = DefaultRootWindow(d), r, parent, *kids = NULL;
unsigned int nk = 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)) {
Window w = kids[i];
XFree(name);
if (kids)
XFree(kids);
return w;
}
XFree(name);
}
}
if (kids)
XFree(kids);
return 0;
}
static void send_key(KeySym ks, int down)
{
XEvent e;
Window root = DefaultRootWindow(d);
int rx, ry, wx, wy;
unsigned int mask;
Window cr, pr;
if (!game)
return;
memset(&e, 0, sizeof(e));
e.xkey.type = down ? KeyPress : KeyRelease;
e.xkey.display = d;
e.xkey.window = game;
e.xkey.root = root;
e.xkey.subwindow = None;
e.xkey.time = CurrentTime;
XQueryPointer(d, root, &cr, &pr, &rx, &ry, &wx, &wy, &mask);
e.xkey.x = wx;
e.xkey.y = wy;
e.xkey.x_root = rx;
e.xkey.y_root = ry;
e.xkey.same_screen = True;
e.xkey.keycode = XKeysymToKeycode(d, ks);
e.xkey.state = 0;
XSendEvent(d, game, True, down ? KeyPressMask : KeyReleaseMask, &e);
XFlush(d);
}
int main(int argc, char **argv)
{
int secs = argc > 2 ? atoi(argv[2]) : 12;
int t;
d = XOpenDisplay(argc > 1 ? argv[1] : ":1");
if (!d) {
fprintf(stderr, "xtest_move: no display\n");
return 1;
}
game = find_named("Freedoom");
if (!game)
game = find_named("Outer Prison");
if (!game)
game = find_named("Doom");
fprintf(stderr, "xtest_move: game window 0x%lx\n", game);
if (!game)
return 1;
XSetInputFocus(d, game, RevertToParent, CurrentTime);
XFlush(d);
/* Hold forward only (no fire: fire pulls enemies and the player can
* die mid-capture; with +noclip from the command line this is a smooth
* fly-through). */
send_key(XK_Up, 1);
for (t = 0; t < secs * 10; t++) {
static int dir; /* 0 = left held, 1 = right held */
XSetInputFocus(d, game, RevertToParent, CurrentTime); /* keep focus */
/* Re-assert the held forward key: SDL's held state drops between
* synthetic events, so forward motion stalls unless re-sent. */
if ((t % 4) == 0)
send_key(XK_Up, 1);
if ((t % 9) == 8) {
KeySym off = dir ? XK_Right : XK_Left;
KeySym on = dir ? XK_Left : XK_Right;
send_key(off, 0);
usleep(30000);
send_key(on, 1);
dir = !dir;
}
XFlush(d);
usleep(100000);
}
send_key(XK_Left, 0);
send_key(XK_Right, 0);
send_key(XK_Up, 0);
XCloseDisplay(d);
fprintf(stderr, "xtest_move: done\n");
return 0;
}
|