summaryrefslogtreecommitdiff
path: root/tools/evemu-record.c
blob: 312254f44f04863c111fef96ff8934c2e10822eb (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*****************************************************************************
 *
 * evemu - Kernel device emulation
 *
 * Copyright (C) 2010-2012 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3 as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 ****************************************************************************/

#define _GNU_SOURCE
#include "evemu.h"
#include <assert.h>
#include <getopt.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>

#include "find_event_devices.h"

#define INFINITE -1

FILE *output;
bool autorestart = false;

static int describe_device(FILE *output, int fd)
{
	struct evemu_device *dev;
	int ret = -ENOMEM;

	dev = evemu_new(NULL);
	if (!dev)
		goto out;
	ret = evemu_extract(dev, fd);
	if (ret)
		goto out;

	evemu_write(dev, output);
	fflush(output);
out:
	evemu_delete(dev);
	return ret;
}

static void handler (int sig __attribute__((unused)))
{
	fflush(output);
	if (output != stdout) {
		fclose(output);
		output = stdout;
	}
	autorestart = false;
}

static inline bool safe_atoi(const char *str, int *val)
{
	char *endptr;
	long v;

	v = strtol(str, &endptr, 10);
	if (str == endptr)
		return false;
	if (*str != '\0' && *endptr != '\0')
		return false;

	if (v > INT_MAX || v < INT_MIN)
		return false;

	*val = v;
	return true;
}

static inline void usage()
{
	fprintf(stderr, "Usage: %s [--autorestart=s] <device> [output file]\n",
		program_invocation_short_name);
	fprintf(stderr, "Options:\n");
	fprintf(stderr, "    --autorestart=s\n");
	fprintf(stderr, "	Terminate the current recording after <s> seconds\n"
			"	of inactivity and restart a new recording. This option requires\n"
			"	an output file, the file is suffixed with the date and time of \n"
			"	the recording's start.\n"
			"	The timeout must be greater than 0.\n"
			"	This option is only valid for evemu-record.\n");
}

static inline char* make_filename(const char *prefix)
{
	char *filename;
	struct tm *tm;
	time_t t;
	int rc;
	char buf[64];

	t = time(NULL);
	tm = localtime(&t);
	rc = strftime(buf, sizeof(buf), "%F-%T", tm);
	if (rc < 0)
		return NULL;

	rc = asprintf(&filename, "%s.%s", prefix, buf);
	if (rc < 0)
		return NULL;

	return filename;
}

static bool record_device(int fd, unsigned int timeout, const char *prefix)
{
	char *filename = NULL;
	bool rc = false;
	long ftell_start = 0 , ftell_end = 1;

	assert(!autorestart || prefix != NULL);

	do {
		free(filename);

		if (prefix == NULL) {
			output = stdout;
		} else {
			if (autorestart)
				filename = make_filename(prefix);
			else
				filename = strdup(prefix);
			if (filename == NULL) {
				fprintf(stderr, "error: failed to init the filename\n");
				goto out;
			}
			output = fopen(filename, "w");
			if (!output) {
				fprintf(stderr, "error: could not open output file (%m)");
				goto out;
			}
		}

		if (describe_device(output, fd)) {
			fprintf(stderr, "error: could not describe device\n");
			goto out;
		}

		fprintf(output,  "################################\n");
		fprintf(output,  "#      Waiting for events      #\n");
		fprintf(output,  "################################\n");
		if (autorestart) {
			fprintf(output, "# Autorestart timeout: %d\n", timeout);
			ftell_start = ftell(output);
		}

		if (evemu_record(output, fd, timeout)) {
			fprintf(stderr, "error: could not record device\n");
		} else if (autorestart) {
			ftell_end = ftell(output);
			fprintf(output, "# Closing after %ds inactivity\n",
				timeout/1000);
		}

		fflush(output);
		if (output != stdout) {
			fclose(output);
			output = stdout;

			if (autorestart && ftell_start == ftell_end)
				unlink(filename);
		}
	} while (autorestart);

	rc = true;

out:
	free(filename);
	return rc;
}

static inline bool test_grab_device(int fd)
{
	if (ioctl(fd, EVIOCGRAB, (void*)1) < 0) {
		fprintf(stderr, "error: this device is grabbed and I cannot record events\n");
		fprintf(stderr, "see the evemu-record man page for more information\n");
		return false;
	} else {
		ioctl(fd, EVIOCGRAB, (void*)0);
	}

	return true;
}

enum mode {
	EVEMU_RECORD,
	EVEMU_DESCRIBE
};

enum options {
	OPT_AUTORESTART,
};

int main(int argc, char *argv[])
{
	enum mode mode = EVEMU_RECORD;
	int fd = -1;
	struct sigaction act;
	char *prgm_name = program_invocation_short_name;
	char *device = NULL;
	int timeout = INFINITE;
	struct option opts[] = {
		{ "autorestart", required_argument, 0, OPT_AUTORESTART },
		{ 0, 0, 0, 0},
	};
	const char *prefix = NULL;
	int rc = 1;

	output = stdout;

	if (prgm_name && (strcmp(prgm_name, "evemu-describe") == 0 ||
			/* when run directly from the sources (not installed) */
			strcmp(prgm_name, "lt-evemu-describe") == 0))
		mode = EVEMU_DESCRIBE;

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

		c = getopt_long(argc, argv, "", opts, &option_index);
		if (c == -1)
			break;

		switch (c) {
			case OPT_AUTORESTART:
				if (!safe_atoi(optarg, &timeout) ||
				    timeout <= 0) {
					usage();
					goto out;
				}
				timeout *= 1000; /* sec to ms */
				autorestart = true;
				break;
			default:
				usage();
				goto out;
		}
	}

	device = (optind >= argc) ? find_event_devices() : strdup(argv[optind++]);

	if (device == NULL) {
		usage();
		goto out;
	}
	fd = open(device, O_RDONLY | O_NONBLOCK);
	if (fd < 0) {
		fprintf(stderr, "error: could not open device (%m)\n");
		goto out;
	}

	memset (&act, '\0', sizeof(act));
	act.sa_handler = &handler;

	if (sigaction(SIGTERM, &act, NULL) < 0) {
		fprintf (stderr, "Could not attach TERM signal handler (%m)\n");
		goto out;
	}
	if (sigaction(SIGINT, &act, NULL) < 0) {
		fprintf (stderr, "Could not attach INT signal handler (%m)\n");
		goto out;
	}

	if (optind >= argc) {
		if (autorestart) {
			fprintf(stderr, "Option --autoresume requires an output file\n");
			goto out;
		}
	} else {
		prefix = argv[optind++];
	}

	if (mode == EVEMU_RECORD) {
#ifdef EVIOCSCLOCKID
		int clockid = CLOCK_MONOTONIC;
		ioctl(fd, EVIOCSCLOCKID, &clockid);
#endif
		if (!test_grab_device(fd))
			goto out;

		record_device(fd, timeout,  prefix);

	} else if (mode == EVEMU_DESCRIBE) {
		if (prefix) {
			output = fopen(argv[optind++], "w");
			if (!output) {
				fprintf(stderr, "error: could not open output file (%m)");
				goto out;
			}
		}

		if (describe_device(output, fd)) {
			fprintf(stderr, "error: could not describe device\n");
			goto out;
		}
	}

	rc = 0;
out:
	free(device);
	close(fd);
	if (output != stdout) {
		fclose(output);
		output = stdout;
	}
	return rc;
}