Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/ioctl.h>
- #include <linux/input.h>
- #include <linux/uinput.h>
- #include <errno.h>
- #include <string.h>
- #include <stdint.h>
- #include <cutils/log.h>
- #ifndef BUS_VIRTUAL
- /* copy from linux/input.h */
- #define BUS_VIRTUAL 0x06
- #endif
- int uinput_create(const char *name, struct keymap *map) {
- struct uinput_user_dev dev;
- int fd, err;
- const char *devs[] = {
- "/dev/uinput",
- "/dev/input/uinput",
- "/dev/misc/uinput",
- NULL,
- };
- while (*devs) {
- fd = open(*devs, O_RDWR);
- if (fd >= 0)
- break;
- }
- if (fd < 0) {
- err = errno;
- fprintf(stderr, "open error: %s\n", strerror(err));
- return -err;
- }
- memset(&dev, 0, sizeof(dev));
- if (name) strncpy(dev.name, name, UINPUT_MAX_NAME_SIZE - 1);
- dev.id.bustype = BUS_VIRTUAL;
- dev.id.vendor = 0x0000;
- dev.id.product = 0x0000;
- dev.id.version = 0x0000;
- if (write(fd, &dev, sizeof(dev)) < 0) {
- err = errno;
- fprintf(stderr, "write device information error: %s\n", strerror(err));
- close(fd);
- return -err;
- }
- ioctl(fd, UI_SET_EVBIT, EV_KEY);
- while (map->value != 0xff) {
- ioctl(fd, UI_SET_KEYBIT, map->scancode);
- map++;
- }
- if (ioctl(fd, UI_DEV_CREATE, NULL) < 0) {
- err = errno;
- fprintf(stderr, "write device information error: %s\n", strerror(err));
- close(fd);
- return -err;
- }
- return fd;
- }
- static void send_event(int fd, uint16_t type, uint16_t code, int32_t value) {
- struct input_event event;
- int err;
- memset(&event, 0, sizeof(event));
- event.type = type;
- event.code = code;
- event.value = value;
- gettimeofday(&event.time, NULL);
- if (write(fd, &event, sizeof(event)) < 0) {
- LOGE("event sent error: %s", strerror(errno));
- }
- }
- void send_key(int fd, uint16_t key, int press)
- {
- LOGW("send key fd = %d, 0x%04x press: %s", fd, key, press ? "true" : "false");
- send_event(fd, EV_KEY, key, press ? 1 : 0);
- send_event(fd, EV_SYN, SYN_REPORT, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment