Add REL_RX, REL_RY, REL_RZ
[evtest.git] / evtest-create-device.xsl
blobf461a4486fa046eb78f525da4577b4c3421b8067
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!--
4 Copyright (c) 2009 Red Hat, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 -->
21 <!-- use this file with xsltproc evtest-replace.xml <input.xml>
22 where <input.xml> is the xml file generated by evtest-capture.
23 -->
25 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
26 <xsl:output method="text" indent="yes" encoding="UTF-8"/>
29 <xsl:template match="/evtest-capture">
30 <![CDATA[
32 Copyright (c) 2009 Red Hat, Inc.
34 This program is free software; you can redistribute it and/or modify
35 it under the terms of the GNU General Public License as published by
36 the Free Software Foundation; either version 2 of the License, or
37 (at your option) any later version.
39 This program is distributed in the hope that it will be useful,
40 but WITHOUT ANY WARRANTY; without even the implied warranty of
41 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 GNU General Public License for more details.
44 You should have received a copy of the GNU General Public License
45 along with this program; if not, write to the Free Software
46 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
49 #include <stdio.h>
50 #include <string.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <unistd.h>
54 #include <linux/input.h>
55 #include <linux/uinput.h>
56 #include <dlfcn.h>
58 static int fd = -1;
60 static void send_event(int fd, int type, int code, int value, int sec, int usec);
62 static int setup(struct uinput_user_dev *dev, int fd)
64 ]]>
65 <xsl:apply-templates select="info/id"/>
66 <xsl:apply-templates select="info/event-type"/>
67 <![CDATA[
69 return 0;
70 error:
71 perror("ioctl failed.");
72 return -1;
75 static int run(int fd)
77 ]]>
78 <xsl:apply-templates select="events"/>
79 <![CDATA[
80 return 0;
83 static int init_uinput()
85 struct uinput_user_dev dev;
87 fd = open("/dev/uinput", O_RDWR);
88 if (fd < 0 && errno == ENODEV)
89 fd = open("/dev/input/uinput", O_RDWR);
90 if (fd < 0)
91 goto error;
93 memset(&dev, 0, sizeof(dev));
94 setup(&dev, fd);
96 if (write(fd, &dev, sizeof(dev)) < sizeof(dev))
97 goto error;
98 if (ioctl(fd, UI_DEV_CREATE, NULL) == -1) goto error;
100 return 0;
102 error:
103 fprintf(stderr, "Error: %s\n", strerror(errno));
105 if (fd != -1)
106 close(fd);
108 return -1;
111 static void cleanup_uinput(void)
113 if (fd == -1)
114 return;
116 ioctl(fd, UI_DEV_DESTROY, NULL);
117 close(fd);
118 fd = -1;
121 static void send_event(int fd, int type, int code, int value, int sec, int usec)
123 static int sec_offset = -1;
124 static long last_time = -1;
125 long newtime;
126 struct input_event event;
128 event.type = type;
129 event.code = code;
130 event.value = value;
132 if (sec_offset == -1)
133 sec_offset = sec;
135 sec -= sec_offset;
136 newtime = sec * 1000000 + usec;
138 if (last_time > 0)
139 usleep(newtime - last_time);
141 if (write(fd, &event, sizeof(event)) < sizeof(event))
142 perror("Send event failed.");
144 last_time = newtime;
147 int main (int argc, char **argv)
149 if (init_uinput() < 0) {
150 fprintf(stderr,
151 "Failed to initialize /dev/input/uinput. Exiting.\n");
152 return -1;
155 printf("Device created. Press CTRL+C to terminate.\n");
156 run(fd);
158 cleanup_uinput();
160 return 0;
163 </xsl:template>
165 <!-- Setup the required uinput bits to make the device look real
166 The funky comments at the start of the lines are just to improve
167 the output indenting.
169 <xsl:template match="event-type">
170 if (ioctl(fd, UI_SET_EVBIT, <xsl:value-of select="@type"/>) == -1) goto error;
171 <!-- --><xsl:for-each select="code">
172 <xsl:choose>
173 <xsl:when test="../@type = 'EV_KEY'">
174 if (ioctl(fd, UI_SET_KEYBIT, <xsl:value-of select="@value"/>) == -1) goto error;
175 <!-- --></xsl:when>
176 <xsl:when test="../@type = 'EV_REL'">
177 if (ioctl(fd, UI_SET_RELBIT, <xsl:value-of select="@value"/>) == -1) goto error;
178 <!-- --></xsl:when>
179 <xsl:when test="../@type = 'EV_MSC'">
180 if (ioctl(fd, UI_SET_MSCBIT, <xsl:value-of select="@value"/>) == -1) goto error;
181 <!-- --></xsl:when>
182 <xsl:when test="../@type = 'EV_ABS'">
183 if (ioctl(fd, UI_SET_ABSBIT, <xsl:value-of select="@value"/>) == -1) goto error;
184 else {
185 int idx = <xsl:value-of select="@value"/>;
186 dev->absmin[idx] = <xsl:value-of select="@abs-min"/>;
187 dev->absmax[idx] = <xsl:value-of select="@abs-max"/>;
188 dev->absfuzz[idx] = <xsl:value-of select="@abs-fuzz"/>;
189 dev->absflat[idx] = <xsl:value-of select="@abs-flat"/>;
191 if (dev->absmin[idx] == dev->absmax[idx])
192 dev->absmax[idx]++;
194 <!-- --></xsl:when>
195 </xsl:choose>
196 </xsl:for-each>
197 </xsl:template>
199 <xsl:template match="id">
200 strcpy(dev->name, "<xsl:value-of select="."/>");
201 dev->id.bustype = <xsl:value-of select="@bus"/>;
202 dev->id.vendor = <xsl:value-of select="@vendor"/>;
203 dev->id.product = <xsl:value-of select="@product"/>;
204 dev->id.version = <xsl:value-of select="@version"/>;
205 </xsl:template>
207 <!-- code replaying the events -->
208 <xsl:template match="events">
209 <xsl:for-each select="event">
210 send_event(fd, <xsl:value-of select="@type"/>, <xsl:value-of select="@code"/>, <xsl:value-of select="@value"/>, <xsl:value-of select="@sec"/>, <xsl:value-of select="@usec"/>);</xsl:for-each>
211 </xsl:template>
213 </xsl:stylesheet>