xref: /third_party/toybox/toys/android/sendevent.c (revision 0f66f451)
10f66f451Sopenharmony_ci/* sendevent.c - Send Linux input events.
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2016 The Android Open Source Project
40f66f451Sopenharmony_ci
50f66f451Sopenharmony_ciUSE_SENDEVENT(NEWTOY(sendevent, "<4>4", TOYFLAG_USR|TOYFLAG_SBIN))
60f66f451Sopenharmony_ci
70f66f451Sopenharmony_ciconfig SENDEVENT
80f66f451Sopenharmony_ci  bool "sendevent"
90f66f451Sopenharmony_ci  default y
100f66f451Sopenharmony_ci  depends on TOYBOX_ON_ANDROID
110f66f451Sopenharmony_ci  help
120f66f451Sopenharmony_ci    usage: sendevent DEVICE TYPE CODE VALUE
130f66f451Sopenharmony_ci
140f66f451Sopenharmony_ci    Sends a Linux input event.
150f66f451Sopenharmony_ci*/
160f66f451Sopenharmony_ci
170f66f451Sopenharmony_ci#define FOR_sendevent
180f66f451Sopenharmony_ci#include "toys.h"
190f66f451Sopenharmony_ci
200f66f451Sopenharmony_ci#include <linux/input.h>
210f66f451Sopenharmony_ci
220f66f451Sopenharmony_civoid sendevent_main(void)
230f66f451Sopenharmony_ci{
240f66f451Sopenharmony_ci  int fd = xopen(*toys.optargs, O_RDWR);
250f66f451Sopenharmony_ci  int version;
260f66f451Sopenharmony_ci  struct input_event ev;
270f66f451Sopenharmony_ci
280f66f451Sopenharmony_ci  if (ioctl(fd, EVIOCGVERSION, &version))
290f66f451Sopenharmony_ci    perror_exit("EVIOCGVERSION failed for %s", *toys.optargs);
300f66f451Sopenharmony_ci
310f66f451Sopenharmony_ci  memset(&ev, 0, sizeof(ev));
320f66f451Sopenharmony_ci  // TODO: error checking and support for named constants.
330f66f451Sopenharmony_ci  ev.type = atoi(toys.optargs[1]);
340f66f451Sopenharmony_ci  ev.code = atoi(toys.optargs[2]);
350f66f451Sopenharmony_ci  ev.value = atoi(toys.optargs[3]);
360f66f451Sopenharmony_ci  xwrite(fd, &ev, sizeof(ev));
370f66f451Sopenharmony_ci}
38