summaryrefslogtreecommitdiff
path: root/input.c
blob: e47f3633672fc5509e6ceb0c789a8fb8d64dd06a (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
/*
 * Copyright (C) 2023 Mikhail Burakov. This file is part of streamer.
 *
 * streamer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * streamer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with streamer.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "input.h"

#include <errno.h>
#include <fcntl.h>
#include <linux/uhid.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "proto.h"
#include "toolbox/buffer.h"
#include "toolbox/utils.h"

struct InputHandler {
  struct Buffer buffer;
  int uhid_fd;
};

struct InputHandler* InputHandlerCreate(bool disable_uhid) {
  struct InputHandler* input_handler = malloc(sizeof(struct InputHandler));
  if (!input_handler) {
    LOG("Failed to allocate input handler (%s)", strerror(errno));
    return NULL;
  }
  *input_handler = (struct InputHandler){
      .uhid_fd = -1,
  };

  BufferCreate(&input_handler->buffer);
  input_handler->uhid_fd =
      open(disable_uhid ? "/dev/null" : "/dev/uhid", O_RDWR);
  if (input_handler->uhid_fd == -1) {
    LOG("Failed to open uhid device (%s)", strerror(errno));
    goto rollback_input_handler;
  }
  return input_handler;

rollback_input_handler:
  free(input_handler);
  return NULL;
}

int InputHandlerGetEventsFd(struct InputHandler* input_handler) {
  return input_handler->uhid_fd;
}

bool InputHandlerProcessEvents(struct InputHandler* input_handler) {
  struct uhid_event uhid_event;
  if (read(input_handler->uhid_fd, &uhid_event, sizeof(uhid_event)) < 0) {
    LOG("Failed to process uhid events (%s)", strerror(errno));
    return false;
  }
  // TODO(mburakov): Add logging?
  return true;
}

bool InputHandlerHandle(struct InputHandler* input_handler, int fd) {
  switch (BufferAppendFrom(&input_handler->buffer, fd)) {
    case -1:
      LOG("Failed to append input data to buffer (%s)", strerror(errno));
      return false;
    case 0:
      LOG("Client closed connection");
      return false;
    default:
      break;
  }

  for (;;) {
    struct uhid_event* event = input_handler->buffer.data;
    if (input_handler->buffer.size < sizeof(event->type)) {
      // mburakov: Packet type is not yet available.
      return true;
    }

    if (event->type == ~0u) {
      // mburakov: Special case, a ping message.
      size_t size = sizeof(event->type) + sizeof(uint64_t);
      if (input_handler->buffer.size < size) {
        // mburakov: Payload of ping message is not yet available.
        return true;
      }
      char buffer[sizeof(struct Proto) + sizeof(uint64_t)];
      struct Proto* proto = (void*)buffer;
      proto->size = sizeof(uint64_t);
      proto->type = PROTO_TYPE_MISC;
      memcpy(proto->data, &event->u, sizeof(uint64_t));
      if (write(fd, buffer, sizeof(buffer)) != sizeof(buffer)) {
        LOG("Failed to write pong message (%s)", strerror(errno));
        return false;
      }
      BufferDiscard(&input_handler->buffer, size);
      continue;
    }

    size_t size;
    switch (event->type) {
      case UHID_CREATE2:
        if (input_handler->buffer.size <
            offsetof(struct uhid_event, u.create2.rd_size) +
                sizeof(event->u.create2.rd_size)) {
          // mburakov: Report descriptor size is not yet available.
          return true;
        }
        size = offsetof(struct uhid_event, u.create2.rd_data) +
               event->u.create2.rd_size;
        if (input_handler->buffer.size < size) {
          // mburakov: Report descriptor data is not yet available.
          return true;
        }
        break;
      case UHID_INPUT2:
        if (input_handler->buffer.size <
            offsetof(struct uhid_event, u.input2.size) +
                sizeof(event->u.input2.size)) {
          // mburakov: Report size is not yet available.
          return true;
        }
        size =
            offsetof(struct uhid_event, u.input2.data) + event->u.input2.size;
        if (input_handler->buffer.size < size) {
          // mburakov: Report data is not yet available.
          return true;
        }
        break;
      case UHID_DESTROY:
        size = sizeof(event->type);
        break;
      default:
        __builtin_unreachable();
    }

    // mburakov: This write has to be atomic.
    if (write(input_handler->uhid_fd, event, size) != (ssize_t)size) {
      LOG("Failed to write uhid event (%s)", strerror(errno));
      return false;
    }
    BufferDiscard(&input_handler->buffer, size);
  }
}

void InputHandlerDestroy(struct InputHandler* input_handler) {
  close(input_handler->uhid_fd);
  BufferDestroy(&input_handler->buffer);
  free(input_handler);
}