File: uinput.c

package info (click to toggle)
triggerhappy 0.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 264 kB
  • sloc: ansic: 1,449; sh: 81; makefile: 53
file content (82 lines) | stat: -rw-r--r-- 1,692 bytes parent folder | download | duplicates (4)
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
/* Copyright 2010 Stefan Tomanek <stefan.tomanek+th@wertarbyte.de>
 * You have permission to copy, modify, and redistribute under the
 * terms of the GPLv3 or any later version.
 * For full license terms, see COPYING.
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/uinput.h>

#include "uinput.h"

static int uinput_fd = -1;

int open_uinput(const char *path) {
	close_uinput();
	struct uinput_user_dev device;
	uinput_fd = open(path, O_WRONLY);
	if (!uinput_fd) {
		return -1;
	}
	memset(&device, 0, sizeof device);
	strcpy(device.name,"triggerhappy");
	device.id.bustype = BUS_USB;
	device.id.vendor = 1;
	device.id.product = 1;
	device.id.version = 1;
	if (write(uinput_fd,&device,sizeof(device)) != sizeof(device)) {
		close_uinput();
		return -1;
	}

	if (ioctl(uinput_fd,UI_SET_EVBIT,EV_KEY) < 0) {
		close_uinput();
		return -1;
	}

	/* we can generate _any_ key event */
	int i;
	for (i=0; i<KEY_MAX && uinput_fd; i++) {
		if (ioctl(uinput_fd, UI_SET_KEYBIT, i) < 0) {
			close_uinput();
			return -1;
		}
	}

	if (ioctl(uinput_fd, UI_DEV_CREATE) < 0) {
		close_uinput();
		return -1;
	}

	return uinput_fd;
}

void close_uinput() {
	if (uinput_fd >= 0) {
		close(uinput_fd);
		uinput_fd = -1;
	}
}

int send_event(const int type, const int code, const int value) {
	if (!uinput_fd) {
		return -1;
	}
	struct input_event event;
	memset(&event, 0, sizeof event);
	event.type = type;
	event.code = code;
	event.value = value;
	if (write(uinput_fd, &event, sizeof(event)) != sizeof(event)) {
		fprintf(stderr, "Error on send_event\n");
		return -1;
	}
	return 0;
}