summaryrefslogtreecommitdiff
path: root/spice-replay.c
blob: 81aec0e8f721163f4a6e4119ff72aeb7896c53a3 (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
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
127
128
129
130
131
132
/**
 * Program to record/reproduce key/mouse events
 *
 * The intention is to be able to record a Spice
 * session and reproduce it
 */
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <signal.h>
#include <X11/Xlib.h>
#include <X11/extensions/XInput.h>
#ifdef HAVE_XI2
#include <X11/extensions/XInput2.h>
#endif

#include "global.h"

Display *dpy = NULL;
int verbose = 0;

enum {
	MODE_UNKNOWN,
	MODE_RECORD,
	MODE_PLAY
};

static int mode = MODE_UNKNOWN;

static const char short_options[] = "hVvi:";
static const struct option long_options[] = {
	{"help",    no_argument,       0,        'h' },
	{"version", no_argument,       0,        'V' },
	{"record",  no_argument,       &mode,    MODE_RECORD },
	{"play",    no_argument,       &mode,    MODE_PLAY },
	{"verbose", no_argument,       0,        'v' },
	{"id",      required_argument, 0,        'i'},
	{"display", required_argument, 0,        'D'},
	{NULL,      0,                 0,        0 }
};

static void
usage(FILE *f)
{
	fprintf(f, "Usage: spice-replay [--record|--play] [OPTION].. [FILE]\n"
		"--display=<X>|-D <X>  X server to contact\n"
		"--record              record events\n"
		"--play                play registered events\n"
		"--verbose             print more verbose information\n"
		"--help|-h             this help\n"
		"--version|-V          print program version\n"
		"--id=<ID>|-i <ID>     specify a window id\n"
	);
	exit(f == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}

static void
version(void)
{
	printf("spice-replay version " VERSION "\n");
	exit(EXIT_SUCCESS);
}

int main(int argc, char **argv)
{
	Window win = 0;
	const char *fn = NULL;
	const char *display = NULL;

	while (1) {
		int option_index = 0, c;

		c = getopt_long(argc, argv, short_options,
				long_options, &option_index);
		if (c == -1)
			break;

		switch (c) {
		case 0:
			break;

		case 'h':
			usage(stdout);
			break;

		case 'V':
			version();
			break;

		case 'v':
			++verbose;
			break;

		case 'i':
			win = strtoul(optarg, NULL, 0);
			break;

		case 'd':
			display = optarg;
			break;

		default:
			usage(stderr);
			break;
		}
	}

	if (optind >= argc || mode == MODE_UNKNOWN)
		usage(stderr);

	fn = argv[optind];

	dpy = XOpenDisplay (display);
	if (!dpy) {
		fprintf (stderr, "unable to open display '%s'\n", XDisplayName (display));
		return EXIT_FAILURE;
	}

	switch (mode) {
	case MODE_RECORD:
		record(fn, win);
		break;
	case MODE_PLAY:
		play(fn, win);
		break;
	}

	XCloseDisplay (dpy);
	return EXIT_SUCCESS;
}