2
* Copyright © 2008 Red Hat, Inc.
4
* Permission to use, copy, modify, distribute, and sell this software
5
* and its documentation for any purpose is hereby granted without
6
* fee, provided that the above copyright notice appear in all copies
7
* and that both that copyright notice and this permission notice
8
* appear in supporting documentation, and that the name of Red Hat
9
* not be used in advertising or publicity pertaining to distribution
10
* of the software without specific, written prior permission. Red
11
* Hat makes no representations about the suitability of this software
12
* for any purpose. It is provided "as is" without express or implied
15
* THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
17
* NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
19
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
20
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24
* Peter Hutterer (peter.hutterer@redhat.com)
33
#include <linux/input.h>
34
#include <linux/uinput.h>
39
/* "public interfaces" */
41
void send_event(int fd, int type, int code, int value)
43
struct input_event event;
48
gettimeofday(&event.time, NULL);
50
if (write(fd, &event, sizeof(event)) < sizeof(event))
51
perror("Send event failed.");
55
void move(int fd, int x, int y)
60
send_event(fd, EV_REL, REL_X, x);
61
send_event(fd, EV_REL, REL_Y, y);
62
send_event(fd, EV_SYN, SYN_REPORT, 0);
65
void absmove(int fd, int x, int y)
67
send_event(fd, EV_ABS, ABS_X, x);
68
send_event(fd, EV_ABS, ABS_Y, y);
69
send_event(fd, EV_SYN, SYN_REPORT, 0);
72
void click(int fd, int btn, int down)
74
send_event(fd, EV_KEY, btn, down);
75
send_event(fd, EV_SYN, SYN_REPORT, 0);
80
/* end public interfaces */
85
static void sighandler(int signum)
87
printf("Stopping.\n");
91
static void init_signal(void)
93
struct sigaction action;
98
action.sa_handler = sighandler;
99
action.sa_mask = mask;
102
sigaction(SIGTERM, &action, NULL);
103
sigaction(SIGINT, &action, NULL);
104
sigprocmask(SIG_UNBLOCK, &mask, 0);
108
static int init_uinput(struct test_device* test_dev)
110
struct uinput_user_dev dev;
112
fd = open("/dev/input/uinput", O_RDWR);
116
memset(&dev, 0, sizeof(dev));
117
strcpy(dev.name, test_dev->name);
119
dev.id.vendor = 0x1F;
120
dev.id.product = 0x1F;
124
test_dev->setup(&dev, fd);
126
if (write(fd, &dev, sizeof(dev)) < sizeof(dev))
128
if (ioctl(fd, UI_DEV_CREATE, NULL) == -1) goto error;
133
fprintf(stderr, "Error: %s\n", strerror(errno));
141
static void cleanup_uinput(void)
146
ioctl(fd, UI_DEV_DESTROY, NULL);
152
int main (int argc, char **argv)
154
struct test_device *dev;
155
void *dlhandle = NULL;
156
struct test_device* (*get_device)(void);
160
fprintf(stderr, "Usage: %s test_dev\n", argv[0]);
164
printf("Loading %s.\n", argv[1]);
166
dlhandle = dlopen(argv[1], RTLD_NOW | RTLD_GLOBAL);
169
fprintf(stderr, "Error: %s\n", dlerror());
173
*(void**)(&get_device) = dlsym(dlhandle, "get_device");
176
fprintf(stderr, "Error getting the symbol: %s.\n", dlerror());
180
dev = (*get_device)();
182
if (init_uinput(dev) < 0) {
183
fprintf(stderr, "Failed to initialize /dev/uinput. Exiting.\n");
189
printf("Device created. Press CTRL+C to terminate.\n");