summaryrefslogtreecommitdiff
path: root/clients/linux/linux-uinput.c
blob: d585e2f5fda36320028de2fe91fb40948f7d87ae (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <limits.h>
#include <assert.h>
#include <err.h>
#include <linux/uinput.h>
#include <fcntl.h>

#include "common/packet.h"

struct core {
   int input;
};

// we assume 64bit host, see packet.h for rant

#if UINTPTR_MAX != 0xffffffffffffffff
#define NOT_64BIT

struct timeval_64 {
   uint64_t tv_sec, tv_usec;
};

struct input_event_64 {
   struct timeval_64 time;
   uint16_t type;
   uint16_t code;
   int32_t value;
};

#endif

static int
_ioctl(const int fd, const unsigned long request, void *arg)
{
   int ret;
   do {
      ret = ioctl(fd, request, arg);
   } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
   return ret;
}

#define ioctl(...) error "Use _ioctl instead"

static bool
uinputd_ioctl_other(const enum ioctl_dir dir, const uint8_t type, const uint8_t nr, const uint16_t bytes, void *arg)
{
   const struct packet packet = {
      .type = PACKET_IOCTL,
      .ioctl.dir = dir,
      .ioctl.type = type,
      .ioctl.nr = nr,
      .ioctl.arg = IOCTL_ARG_OTHER,
      .ioctl.bytes = bytes,
   };

   if (fwrite(&packet, 1sizeof(packet), stdout) != sizeof(packet)) {
      warn("fwrite: failed to write %u bytes"sizeof(packet));
      return false;
   }

   if (bytes > 0 && fwrite(arg, 1, bytes, stdout) != bytes) {
      warn("fwrite: failed to write %u bytes", bytes);
      return false;
   }

   return true;
}

static bool
uinputd_ioctl_int(const enum ioctl_dir dir, const uint8_t type, const uint8_t nr, int arg)
{
   const struct packet packet = {
      .type = PACKET_IOCTL,
      .ioctl.dir = dir,
      .ioctl.type = type,
      .ioctl.nr = nr,
      .ioctl.arg = IOCTL_ARG_INT,
      .ioctl.integer = arg,
   };

   if (fwrite(&packet, 1sizeof(packet), stdout) != sizeof(packet)) {
      warn("fwrite: failed to write %u bytes"sizeof(packet));
      return false;
   }

   return true;
}

static bool
uinputd_write(const int32_t bytes, const void *data)
{
   if (bytes <= 0 || !data)
      return true;

   const struct packet packet = {
      .type = PACKET_WRITE,
      .write.bytes = bytes,
   };

   if (fwrite(&packet, 1sizeof(packet), stdout) != sizeof(packet)) {
      warn("fwrite: failed to write %u bytes"sizeof(packet));
      return false;
   }

   if (fwrite(data, 1, bytes, stdout) != bytes) {
      warn("fwrite: failed to write %u bytes", bytes);
      return false;
   }

   return true;
}

static void
fd_close_or_exit(const int fd)
{
   if (fd >= 0 && close(fd) != 0)
      err(EXIT_FAILURE"close");
}

static void
core_release(struct core *core)
{
   if (!core)
      return;

   fd_close_or_exit(core->input);
   *core = (struct core){0};
}

static void
core_init(struct core *core, const char *path)
{
   assert(core);

   if ((core->input = open(path, O_RDONLY | O_CLOEXEC)) < 0)
      err(EXIT_FAILURE"open(%s)", path);

   int evbit;
   if (_ioctl(core->input, EVIOCGBIT(0sizeof(evbit)), &evbit) == -1)
      err(EXIT_FAILURE"ioctl(EVIOCGBIT)");

   for (int i = 0; i < EV_MAX; ++i) {
      if (!(evbit & (1 << i)))
         continue;

      if (!uinputd_ioctl_int(IOCTL_IOW, UINPUT_IOCTL_BASE, 100, i))
         errx(EXIT_FAILURE"ioctl(UI_SET_EVBIT): %d", i);
   }

   if ((evbit & (1 << EV_KEY))) {
      unsigned char keybit[KEY_MAX / 8 + 1];
      if (_ioctl(core->input, EVIOCGBIT(EV_KEY, sizeof(keybit)), &keybit) == -1)
         err(EXIT_FAILURE"ioctl(EVIOCGBIT:EV_KEY)");

      for (int i = 0; i < KEY_MAX; ++i) {
         if (!(keybit[i / 8] & (1 << (i % 8))))
            continue;

         if (!uinputd_ioctl_int(IOCTL_IOW, UINPUT_IOCTL_BASE, 101, i))
            errx(EXIT_FAILURE"ioctl(UI_SET_KEYBIT): %d", i);
      }
   }

   if ((evbit & (1 << EV_REL))) {
      int keybit;
      if (_ioctl(core->input, EVIOCGBIT(EV_REL, sizeof(keybit)), &keybit) == -1)
         err(EXIT_FAILURE"ioctl(EVIOCGBIT:EV_ABS)");

      for (int i = 0; i < REL_MAX; ++i) {
         if (!(keybit & (1 << i)))
            continue;

         if (!uinputd_ioctl_int(IOCTL_IOW, UINPUT_IOCTL_BASE, 102, i))
            errx(EXIT_FAILURE"ioctl(UI_SET_RELBIT): %d", i);
      }
   }

   if ((evbit & (1 << EV_ABS))) {
      int keybit;
      if (_ioctl(core->input, EVIOCGBIT(EV_ABS, sizeof(keybit)), &keybit) == -1)
         err(EXIT_FAILURE"ioctl(EVIOCGBIT:EV_ABS)");

      for (int i = 0; i < ABS_MAX; ++i) {
         if (!(keybit & (1 << i)))
            continue;

         if (!uinputd_ioctl_int(IOCTL_IOW, UINPUT_IOCTL_BASE, 103, i))
            errx(EXIT_FAILURE"ioctl(UI_SET_ABSBIT): %d", i);
      }
   }

   struct uinput_user_dev setup = {0};

   if (_ioctl(core->input, EVIOCGNAME(sizeof(setup.name)), setup.name) == -1)
      err(EXIT_FAILURE"ioctl(EVIOCGNAME)");

   for (size_t i = 0; (evbit & (1 << EV_ABS)) && i < ABS_MAX; ++i) {
      struct input_absinfo info;
      if (_ioctl(core->input, EVIOCGABS(i), &info) == -1)
         err(EXIT_FAILURE"ioctl(EVIOCGABS)");

      setup.absmax[i] = info.maximum;
      setup.absmin[i] = info.minimum;
      setup.absfuzz[i] = info.fuzz;
      setup.absflat[i] = info.flat;
   }

   if (_ioctl(core->input, EVIOCGID, &setup.id) == -1)
      err(EXIT_FAILURE"ioctl(EVIOCGID)");

   if (!uinputd_write(sizeof(setup), &setup))
      errx(EXIT_FAILURE"write(uinput_user_dev)");

   if (!uinputd_ioctl_other(IOCTL_NONE, UINPUT_IOCTL_BASE, 10NULL))
      errx(EXIT_FAILURE"ioctl(UI_DEV_CREATE)");
}

static void
core_on_exit(const int signal, void *core)
{
   assert(core);
   warnx("core_on_exit (%d)", signal);
   core_release(core);
}

static void
info(void)
{
   const char *remote = getenv("TCPREMOTEIP");
   warnx("version %s", UINPUTD_VERSION);
   warnx("remote ip: %s", (remote ? remote : "none"));
   warnx("sizeof(struct packet) is %zu bytes"sizeof(struct packet));
#ifdef NOT_64BIT
   warnx("sizeof(struct input_event) is %zu bytes"sizeof(struct input_event_64));
#else
   warnx("sizeof(struct input_event) is %zu bytes"sizeof(struct input_event));
#endif
   warnx("sizeof(struct uinput_user_dev) is %zu bytes"sizeof(struct uinput_user_dev));
}

static void
sigterm(const int signal)
{
   warnx("%s", (signal == SIGTERM ? "SIGTERM" : "SIGINT"));
   exit(EXIT_SUCCESS);
}

int
main(int argc, const char *argv[])
{
   if (argc < 2)
      errx(EXIT_FAILURE"usage: %s input-device [write-fd]", argv[0]);

   {
      const struct sigaction action = {
         .sa_handler = sigterm,
      };

      if (sigaction(SIGTERM, &action, NULL) != 0 || sigaction(SIGINT, &action, NULL) != 0)
         err(EXIT_FAILURE"sigaction");
   }

   if (argc > 2)
      dup2(strtol(argv[2], NULL10), STDOUT_FILENO);

   setvbuf(stdoutNULL_IONBF0);

   info();
   struct core core = {0};
   on_exit(core_on_exit, &core);
   core_init(&core, argv[1]);

   {
      struct input_event event;
      while (read(core.input, &event, sizeof(event)) == sizeof(event)) {
#ifdef NOT_64BIT
         struct input_event_64 ev = {
            .time.tv_sec = event.time.tv_sec,
            .time.tv_usec = event.time.tv_usec,
            .type = event.type,
            .code = event.code,
            .value = event.value,
         };
         uinputd_write(sizeof(ev), &ev);
#else
         uinputd_write(sizeof(event), &event);
#endif
      }
      err(EXIT_FAILURE"read");
   }

   exit(EXIT_SUCCESS);
   return EXIT_SUCCESS;
}