aboutsummaryrefslogtreecommitdiff
path: root/toys/android/sendevent.c
blob: b952d1924a0e862e8c080e56c81c186162d0d329 (plain)
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
/* sendevent.c - Send Linux input events.
 *
 * Copyright 2016 The Android Open Source Project

USE_SENDEVENT(NEWTOY(sendevent, "<4>4", TOYFLAG_USR|TOYFLAG_SBIN))

config SENDEVENT
  bool "sendevent"
  default y
  depends on TOYBOX_ON_ANDROID
  help
    usage: sendevent DEVICE TYPE CODE VALUE

    Sends a Linux input event.
*/

#define FOR_sendevent
#include "toys.h"

#include <linux/input.h>

void sendevent_main(void)
{
  int fd = xopen(*toys.optargs, O_RDWR);
  int version;
  struct input_event ev;

  if (ioctl(fd, EVIOCGVERSION, &version))
    perror_exit("EVIOCGVERSION failed for %s", *toys.optargs);

  memset(&ev, 0, sizeof(ev));
  // TODO: error checking and support for named constants.
  ev.type = atoi(toys.optargs[1]);
  ev.code = atoi(toys.optargs[2]);
  ev.value = atoi(toys.optargs[3]);
  xwrite(fd, &ev, sizeof(ev));
}