diff options
| -rw-r--r-- | include/evemu.h | 28 | ||||
| -rw-r--r-- | src/evemu-impl.h | 19 | ||||
| -rw-r--r-- | src/evemu.c | 31 | ||||
| -rw-r--r-- | tools/evemu-describe.c | 19 | ||||
| -rw-r--r-- | tools/evemu-device.c | 40 | ||||
| -rw-r--r-- | tools/evemu-echo.c | 18 |
6 files changed, 107 insertions, 48 deletions
diff --git a/include/evemu.h b/include/evemu.h index 70a5bd5..698a539 100644 --- a/include/evemu.h +++ b/include/evemu.h | |||
| @@ -26,28 +26,22 @@ | |||
| 26 | * | 26 | * |
| 27 | ****************************************************************************/ | 27 | ****************************************************************************/ |
| 28 | 28 | ||
| 29 | #ifndef _EVPLAY_H | 29 | #ifndef _EVEMU_H |
| 30 | #define _EVPLAY_H | 30 | #define _EVEMU_H |
| 31 | 31 | ||
| 32 | #include <linux/uinput.h> | ||
| 33 | #include <stdio.h> | 32 | #include <stdio.h> |
| 33 | #include <errno.h> | ||
| 34 | #include <linux/input.h> | ||
| 34 | 35 | ||
| 35 | #define EVPLAY_NBITS KEY_CNT | 36 | #define EVEMU_VERSION_MAJOR 1 |
| 36 | #define EVPLAY_NBYTES ((EVPLAY_NBITS + 7) / 8) | 37 | #define EVEMU_VERSION_MINOR 1 |
| 37 | 38 | ||
| 38 | struct evemu_device { | 39 | struct evemu_device *evemu_new(const char *name); |
| 39 | char name[UINPUT_MAX_NAME_SIZE]; | 40 | void evemu_delete(struct evemu_device *dev); |
| 40 | struct input_id id; | ||
| 41 | unsigned char mask[EV_CNT][EVPLAY_NBYTES]; | ||
| 42 | int bytes[EV_CNT]; | ||
| 43 | struct input_absinfo abs[ABS_CNT]; | ||
| 44 | }; | ||
| 45 | 41 | ||
| 46 | static inline int evemu_has(const struct evemu_device *dev, | 42 | const char *evemu_get_name(const struct evemu_device *dev); |
| 47 | int type, int code) | 43 | |
| 48 | { | 44 | int evemu_has(const struct evemu_device *dev, int type, int code); |
| 49 | return (dev->mask[type][code >> 3] >> (code & 7)) & 1; | ||
| 50 | } | ||
| 51 | 45 | ||
| 52 | int evemu_extract(struct evemu_device *dev, int fd); | 46 | int evemu_extract(struct evemu_device *dev, int fd); |
| 53 | int evemu_write(const struct evemu_device *dev, FILE *fp); | 47 | int evemu_write(const struct evemu_device *dev, FILE *fp); |
diff --git a/src/evemu-impl.h b/src/evemu-impl.h new file mode 100644 index 0000000..2346590 --- /dev/null +++ b/src/evemu-impl.h | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | #ifndef _EVEMU_IMPL_H | ||
| 2 | #define _EVEMU_IMPL_H | ||
| 3 | |||
| 4 | #include <evemu.h> | ||
| 5 | #include <linux/uinput.h> | ||
| 6 | |||
| 7 | #define EVPLAY_NBITS KEY_CNT | ||
| 8 | #define EVPLAY_NBYTES ((EVPLAY_NBITS + 7) / 8) | ||
| 9 | |||
| 10 | struct evemu_device { | ||
| 11 | int version_major, version_minor; | ||
| 12 | char name[UINPUT_MAX_NAME_SIZE]; | ||
| 13 | struct input_id id; | ||
| 14 | unsigned char mask[EV_CNT][EVPLAY_NBYTES]; | ||
| 15 | int bytes[EV_CNT]; | ||
| 16 | struct input_absinfo abs[ABS_CNT]; | ||
| 17 | }; | ||
| 18 | |||
| 19 | #endif | ||
diff --git a/src/evemu.c b/src/evemu.c index 54156aa..53bda86 100644 --- a/src/evemu.c +++ b/src/evemu.c | |||
| @@ -26,7 +26,8 @@ | |||
| 26 | * | 26 | * |
| 27 | ****************************************************************************/ | 27 | ****************************************************************************/ |
| 28 | 28 | ||
| 29 | #include "evemu.h" | 29 | #include "evemu-impl.h" |
| 30 | #include <stdlib.h> | ||
| 30 | #include <string.h> | 31 | #include <string.h> |
| 31 | #include <stdio.h> | 32 | #include <stdio.h> |
| 32 | #include <fcntl.h> | 33 | #include <fcntl.h> |
| @@ -44,6 +45,34 @@ static void copy_bits(unsigned char *mask, const unsigned long *bits, int bytes) | |||
| 44 | } | 45 | } |
| 45 | } | 46 | } |
| 46 | 47 | ||
| 48 | struct evemu_device *evemu_new(const char *name) | ||
| 49 | { | ||
| 50 | struct evemu_device *dev = calloc(1, sizeof(struct evemu_device)); | ||
| 51 | |||
| 52 | dev->version_major = EVEMU_VERSION_MAJOR; | ||
| 53 | dev->version_minor = EVEMU_VERSION_MINOR; | ||
| 54 | |||
| 55 | if (name && strlen(name) < sizeof(dev->name)) | ||
| 56 | strcpy(dev->name, name); | ||
| 57 | |||
| 58 | return dev; | ||
| 59 | } | ||
| 60 | |||
| 61 | void evemu_delete(struct evemu_device *dev) | ||
| 62 | { | ||
| 63 | free(dev); | ||
| 64 | } | ||
| 65 | |||
| 66 | const char *evemu_get_name(const struct evemu_device *dev) | ||
| 67 | { | ||
| 68 | return dev->name; | ||
| 69 | } | ||
| 70 | |||
| 71 | int evemu_has(const struct evemu_device *dev, int type, int code) | ||
| 72 | { | ||
| 73 | return (dev->mask[type][code >> 3] >> (code & 7)) & 1; | ||
| 74 | } | ||
| 75 | |||
| 47 | int evemu_extract(struct evemu_device *dev, int fd) | 76 | int evemu_extract(struct evemu_device *dev, int fd) |
| 48 | { | 77 | { |
| 49 | unsigned long bits[64]; | 78 | unsigned long bits[64]; |
diff --git a/tools/evemu-describe.c b/tools/evemu-describe.c index ec6b1ef..6c6cdab 100644 --- a/tools/evemu-describe.c +++ b/tools/evemu-describe.c | |||
| @@ -26,22 +26,27 @@ | |||
| 26 | * | 26 | * |
| 27 | ****************************************************************************/ | 27 | ****************************************************************************/ |
| 28 | 28 | ||
| 29 | #include "evemu.h" | 29 | #include <evemu.h> |
| 30 | #include <stdio.h> | 30 | #include <stdio.h> |
| 31 | #include <fcntl.h> | 31 | #include <fcntl.h> |
| 32 | #include <string.h> | 32 | #include <string.h> |
| 33 | 33 | ||
| 34 | static int describe_device(int fd) | 34 | static int describe_device(int fd) |
| 35 | { | 35 | { |
| 36 | struct evemu_device dev; | 36 | struct evemu_device *dev; |
| 37 | int ret; | 37 | int ret = -ENOMEM; |
| 38 | 38 | ||
| 39 | ret = evemu_extract(&dev, fd); | 39 | dev = evemu_new(0); |
| 40 | if (!dev) | ||
| 41 | goto out; | ||
| 42 | ret = evemu_extract(dev, fd); | ||
| 40 | if (ret) | 43 | if (ret) |
| 41 | return ret; | 44 | goto out; |
| 42 | evemu_write(&dev, stdout); | ||
| 43 | 45 | ||
| 44 | return 0; | 46 | evemu_write(dev, stdout); |
| 47 | out: | ||
| 48 | evemu_delete(dev); | ||
| 49 | return ret; | ||
| 45 | } | 50 | } |
| 46 | 51 | ||
| 47 | int main(int argc, char *argv[]) | 52 | int main(int argc, char *argv[]) |
diff --git a/tools/evemu-device.c b/tools/evemu-device.c index b318cf6..79193ff 100644 --- a/tools/evemu-device.c +++ b/tools/evemu-device.c | |||
| @@ -50,7 +50,7 @@ static void hold_device(const struct evemu_device *dev) | |||
| 50 | ret = read(fd, name, sizeof(name)); | 50 | ret = read(fd, name, sizeof(name)); |
| 51 | if (ret > 0) | 51 | if (ret > 0) |
| 52 | name[ret - 1] = 0; | 52 | name[ret - 1] = 0; |
| 53 | if (!strcmp(dev->name, name)) | 53 | if (!strcmp(evemu_get_name(dev), name)) |
| 54 | sprintf(node, "/dev/input/event%d", i + 1); | 54 | sprintf(node, "/dev/input/event%d", i + 1); |
| 55 | close(fd); | 55 | close(fd); |
| 56 | } | 56 | } |
| @@ -58,7 +58,7 @@ static void hold_device(const struct evemu_device *dev) | |||
| 58 | fd = open(node, O_RDONLY); | 58 | fd = open(node, O_RDONLY); |
| 59 | if (fd < 0) | 59 | if (fd < 0) |
| 60 | return; | 60 | return; |
| 61 | fprintf(stdout, "%s: %s\n", dev->name, node); | 61 | fprintf(stdout, "%s: %s\n", evemu_get_name(dev), node); |
| 62 | fflush(stdout); | 62 | fflush(stdout); |
| 63 | while ((ret = read(fd, data, sizeof(data))) > 0); | 63 | while ((ret = read(fd, data, sizeof(data))) > 0); |
| 64 | close(fd); | 64 | close(fd); |
| @@ -66,28 +66,36 @@ static void hold_device(const struct evemu_device *dev) | |||
| 66 | 66 | ||
| 67 | static int evemu_device(FILE *fp) | 67 | static int evemu_device(FILE *fp) |
| 68 | { | 68 | { |
| 69 | struct evemu_device dev; | 69 | struct evemu_device *dev; |
| 70 | int ret, fd; | 70 | char name[64]; |
| 71 | int ret = -ENOMEM; | ||
| 72 | int fd; | ||
| 71 | 73 | ||
| 72 | ret = evemu_read(&dev, fp); | 74 | sprintf(name, "evemu-%d", getpid()); |
| 73 | if (ret <= 0) | ||
| 74 | return ret; | ||
| 75 | 75 | ||
| 76 | sprintf(dev.name, "evemu-%d", getpid()); | 76 | dev = evemu_new(name); |
| 77 | if (!dev) | ||
| 78 | goto out; | ||
| 79 | ret = evemu_read(dev, fp); | ||
| 80 | if (ret <= 0) | ||
| 81 | goto out; | ||
| 77 | 82 | ||
| 78 | fd = open(UINPUT_NODE, O_WRONLY); | 83 | ret = fd = open(UINPUT_NODE, O_WRONLY); |
| 79 | if (fd < 0) | ||
| 80 | return fd; | ||
| 81 | ret = evemu_create(&dev, fd); | ||
| 82 | if (ret < 0) | 84 | if (ret < 0) |
| 83 | return ret; | 85 | goto out; |
| 84 | |||
| 85 | hold_device(&dev); | ||
| 86 | 86 | ||
| 87 | ret = evemu_create(dev, fd); | ||
| 88 | if (ret < 0) | ||
| 89 | goto out_close; | ||
| 90 | hold_device(dev); | ||
| 87 | evemu_destroy(fd); | 91 | evemu_destroy(fd); |
| 92 | |||
| 93 | out_close: | ||
| 88 | close(fd); | 94 | close(fd); |
| 95 | out: | ||
| 96 | evemu_delete(dev); | ||
| 89 | 97 | ||
| 90 | return 0; | 98 | return ret; |
| 91 | } | 99 | } |
| 92 | 100 | ||
| 93 | int main(int argc, char *argv[]) | 101 | int main(int argc, char *argv[]) |
diff --git a/tools/evemu-echo.c b/tools/evemu-echo.c index d3501ea..e9c89df 100644 --- a/tools/evemu-echo.c +++ b/tools/evemu-echo.c | |||
| @@ -33,16 +33,20 @@ | |||
| 33 | 33 | ||
| 34 | static int evemu_echo_describe(FILE *fp) | 34 | static int evemu_echo_describe(FILE *fp) |
| 35 | { | 35 | { |
| 36 | struct evemu_device dev; | 36 | struct evemu_device *dev; |
| 37 | int ret; | 37 | int ret = -ENOMEM; |
| 38 | 38 | ||
| 39 | ret = evemu_read(&dev, fp); | 39 | dev = evemu_new(0); |
| 40 | if (!dev) | ||
| 41 | goto out; | ||
| 42 | ret = evemu_read(dev, fp); | ||
| 40 | if (ret <= 0) | 43 | if (ret <= 0) |
| 41 | return ret; | 44 | goto out; |
| 42 | |||
| 43 | evemu_write(&dev, stdout); | ||
| 44 | 45 | ||
| 45 | return 0; | 46 | evemu_write(dev, stdout); |
| 47 | out: | ||
| 48 | evemu_delete(dev); | ||
| 49 | return ret; | ||
| 46 | } | 50 | } |
| 47 | 51 | ||
| 48 | static int evemu_echo_event(FILE *fp) | 52 | static int evemu_echo_event(FILE *fp) |
