summaryrefslogtreecommitdiff
blob: da4b2e2c86746c5027f0636aa5459d4730700b38 (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
/**** POSIX *******************************************************************/
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

/**** LIBEVDEV ****************************************************************/
#include <libevdev/libevdev.h>

/**** RELABSD *****************************************************************/
#include <relabsd/debug.h>

#include <relabsd/config/parameters.h>

#include <relabsd/server.h>

#include <relabsd/device/physical_device.h>

/******************************************************************************/
/**** LOCAL FUNCTIONS *********************************************************/
/******************************************************************************/

/******************************************************************************/
/**** EXPORTED FUNCTIONS ******************************************************/
/******************************************************************************/
int relabsd_physical_device_compatibility_test
(
   const struct relabsd_physical_device device [const restrict static 1],
   const struct relabsd_parameters parameters [const restrict]
)
{
   if (!libevdev_has_event_type(device->libevdev, EV_REL))
   {
      RELABSD_S_FATAL("The physical device has no relative axes.");

      return -1;
   }

   if
   (
      (parameters != (const struct relabsd_parameters *) NULL)
      && relabsd_parameters_are_compatible_with(device->libevdev, parameters)
   )
   {
      RELABSD_S_FATAL
      (
         "Issue with the physical device's axes (or lack thereof), or their"
         " compatibility with the requested configuration."
      );

      return -2;
   }

   return 0;
}

int relabsd_physical_device_open
(
   const char filename [const restrict static 1],
   struct relabsd_physical_device device [const restrict static 1]
)
{
   int err;

   RELABSD_DEBUG
   (
      RELABSD_DEBUG_PROGRAM_FLOW,
      "Opening physical device %s...",
      filename
   );

   errno = 0;
   device->file = open(filename, O_RDONLY);
   device->is_late = 0;

   if (device->file == -1)
   {
      RELABSD_FATAL
      (
         "Could not open physical device '%s' in read only mode: %s.",
         filename,
         strerror(errno)
      );

      return -1;
   }

   err = libevdev_new_from_fd(device->file, &(device->libevdev));

   if (err != 0)
   {
      RELABSD_FATAL
      (
         "libevdev could not open physical device '%s': %s.",
         filename,
         strerror(-err)
      );

      close(device->file);

      return -1;
   }

   return 0;
}

void relabsd_physical_device_close
(
   const struct relabsd_physical_device device [const restrict static 1]
)
{
   RELABSD_S_DEBUG(RELABSD_DEBUG_PROGRAM_FLOW, "Closing input device...");

   libevdev_free(device->libevdev);

   errno = 0;

   if (close(device->file) == -1)
   {
      RELABSD_ERROR
      (
         "Could not properly close the input device: %s.",
         strerror(errno)
      );
   }
}

int relabsd_physical_device_read
(
   struct relabsd_physical_device device [const restrict static 1],
   unsigned int input_type [const restrict static 1],
   unsigned int input_code [const restrict static 1],
   int input_value [const restrict static 1]
)
{
   int returned_code;
   struct input_event event;

   if (!libevdev_has_event_pending(device->libevdev))
   {
      return 0;
   }

   returned_code =
      libevdev_next_event
      (
         device->libevdev,
         (
            (
               /*
                * If we were already late, reading in NORMAL mode discards all
                * the outdated input events, whereas reading in SYNC mode goes
                * through them in order.
                * TODO: add an option to allow users to drop events when late.
                */
               (device->is_late == 1) ?
               LIBEVDEV_READ_FLAG_SYNC
               : LIBEVDEV_READ_FLAG_NORMAL
            )
            /* "The fd is not in O_NONBLOCK and a read may block." */
            | LIBEVDEV_READ_FLAG_BLOCKING
         ),
         &event
      );

   switch (returned_code)
   {
      /* Read an actual input. */
      case LIBEVDEV_READ_STATUS_SUCCESS:
         RELABSD_DEBUG
         (
            RELABSD_DEBUG_REAL_EVENTS,
            "SUCCESS Valid event received: {type = %s; code = %s; value = %d}.",
             libevdev_event_type_get_name(event.type),
             libevdev_event_code_get_name(event.type, event.code),
             event.value
         );

         *input_type = event.type;
         *input_code = event.code;
         *input_value = event.value;

         device->is_late = 0;

         return 1;

      /* Code indicating that we are late. */
      case LIBEVDEV_READ_STATUS_SYNC:
         /* There are old input events waiting to be read. */
         device->is_late = 1;
         /*
          * From the documentation, the event we just read was an EV_SYN one,
          * so we don't actually have any input event in hand.
          */

         *input_type = event.type;
         *input_code = event.code;
         *input_value = event.value;

         RELABSD_DEBUG
         (
            RELABSD_DEBUG_REAL_EVENTS,
            "SYNC Valid event received: {type = %s; code = %s; value = %d}.",
             libevdev_event_type_get_name(event.type),
             libevdev_event_code_get_name(event.type, event.code),
             event.value
         );

         return 1;

      /* No event to read. */
      case -EAGAIN:
         device->is_late = 0;

         return 0;

      default:
         RELABSD_FATAL
         (
            "Unable to access the physical device: %s.",
            strerror(-returned_code)
         );

         relabsd_server_interrupt();

         return -1;
   }
}

int relabsd_physical_device_is_late
(
   const struct relabsd_physical_device device [const restrict static 1]
)
{
   return device->is_late;
}

int relabsd_physical_device_get_file_descriptor
(
   const struct relabsd_physical_device device [const restrict static 1]
)
{
   return device->file;
}

struct libevdev * relabsd_physical_device_get_libevdev
(
   const struct relabsd_physical_device device [const restrict static 1]
)
{
   return device->libevdev;
}