summaryrefslogtreecommitdiff
blob: d4288ef822fb961241e9d3546afea22c62bfbbb4 (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <errno.h>
#include <string.h>

#include "error.h"
#include "pervasive.h"
#include "axis.h"
#include "config.h"

/*
 * "errno is never set to zero by any system call or library function."
 * This file makes use of this, by setting it to zero and checking if
 * it was modified after calling an function (I'm guessing this is common
 * practice, but I think it's worth explaining).
 * Following the principle of least astonishment, if a function sets errno to
 * zero, it will not return before setting it back either to its previous
 * value or to a arbitrary nonzero value.
 */

/*
 * Returns -1 on error,
 *          0 on EOF,
 *          1 on newline.
 */
static int reach_next_line_or_eof (FILE * const f)
{
   int prev_errno;
   char c;

   prev_errno = errno;

   errno = 0;

   c = getc(f);

   while ((c != '\n') && c != EOF)
   {
      c = getc(f);
   }

   if (errno != 0)
   {
      errno = prev_errno;

      return -1;
   }

   errno = prev_errno;

   if (c == EOF)
   {
      return 0;
   }

   return 1;
}

/*
 * Returns -1 on (fatal) error,
 *          0 on succes.
 */
static int parse_axis_configuration_line
(
   struct relabsd_config * const conf,
   FILE * const f,
   const char * const buffer
)
{
   int valc, prev_errno;
   enum relabsd_axis axis;

   axis = relabsd_axis_from_name(buffer);

   if (axis == RELABSD_UNKNOWN)
   {
      _FATAL
      (
         "[CONFIG] Unknown axis '%s'.",
         buffer
      );

      return -1;
   }

   prev_errno = errno;
   errno = 0;

   valc =
   fscanf
   (
      f,
      "%d %d %d %d %d",
      &(conf->axis[axis].min),
      &(conf->axis[axis].max),
      &(conf->axis[axis].fuzz),
      &(conf->axis[axis].flat),
      &(conf->axis[axis].resolution)
   );

   if (valc == EOF)
   {
      if (errno == 0)
      {
         _FATAL
         (
            "[CONFIG] Unexpected end of file while reading axis '%s'.",
            buffer
         );
      }
      else
      {
         _FATAL
         (
            "[CONFIG] An error occured while reading axis '%s': %s.",
            buffer,
            strerror(errno)
         );
      }

      errno = prev_errno;

      return -1;
   }
   else if (valc < 5)
   {
      _FATAL
      (
         "[CONFIG] Invalid parameter count for axis '%s'.",
         buffer
      );

      errno = prev_errno;

      return -1;
   }

   errno = prev_errno;

   conf->axis[axis].enabled = 1;

   return 0;
}

/*
 * Returns -1 on (fatal) error,
 *          0 on EOF,
 *          1 on newline.
 */
static int read_config_line
(
   struct relabsd_config * const conf,
   FILE * const f,
   const char * const prefix
)
{
   if (!_IS_PREFIX("#", prefix))
   {
      if (parse_axis_configuration_line(conf, f, prefix) < 0)
      {
         /* Fatal error. */
         return -1;
      }
   }

   return reach_next_line_or_eof(f);
}

/*
 * Returns -1 on (fatal) error,
 *         0 on success.
 */
static int read_config_file
(
   struct relabsd_config * const conf,
   char * const filename
)
{
   FILE * f;
   char buffer[3];
   int continue_reading, prev_errno;

   buffer[2] = '\0';

   f = fopen(filename, "r");

   if (f == (FILE *) NULL)
   {
      _FATAL
      (
         "[CONFIG] Could not open file: %s.",
         strerror(errno)
      );

      return -1;
   }

   prev_errno = errno;
   errno = 0;

   continue_reading = 1;

   while ((continue_reading == 1) && (fscanf(f, "%2s", buffer) != EOF))
   {
      switch (read_config_line(conf, f, buffer))
      {
         case 1:
            /* Everything is going well. */
            break;

         case 0:
            /* EOF reached. */
            continue_reading = 0;
            break;

         case -1:
            /* A fatal error occured. */
            errno = prev_errno;

            fclose(f);
            return -1;
      }
   }

   if (errno != 0)
   {
      /* An error happened in the while loop condition. */

      _FATAL
      (
         "[CONFIG] Error while reading file: %s, last read '%s'.",
         strerror(errno),
         buffer
      );

      errno = prev_errno;

      fclose(f);

      return -1;
   }

   errno = prev_errno;

   fclose(f);

   return 0;
}

/*
 * Returns -1 on (fatal) error,
 *          0 on valid usage.
 */
static int check_usage
(
   int const argc,
   char * const * const argv
)
{
   if ((argc < 3) || (argc > 4))
   {
      _FATAL
      (
         "Usage: %s input_device config_file [<relabsd_device_name>]",
         argv[0]
      );

      return -1;
   }

   return 0;
}

static void init_axes_config (struct relabsd_config * const conf)
{
   int i;

   for (i = RELABSD_VALID_AXES_COUNT; i --> 0;)
   {
      conf->axis[i].enabled = 0;
   }
}

int relabsd_config_parse
(
   struct relabsd_config * const conf,
   int const argc,
   char * const * const argv
)
{
   if (check_usage(argc, argv) < 0)
   {
      return -1;
   }

   if (argc == 3)
   {
      conf->device_name = "relabsd device";
   }
   else
   {
      conf->device_name = argv[3];
   }

   conf->input_file = argv[1];

   init_axes_config(conf);

   if (read_config_file(conf, argv[2]) < 0)
   {
      return -1;
   }

   return 0;
}

int relabsd_config_filter
(
   const struct relabsd_config * const conf,
   enum relabsd_axis const axis,
   int * const value
)
{
   if ((axis == RELABSD_UNKNOWN) || !conf->axis[axis].enabled)
   {
      return 0;
   }

   if (*value < conf->axis[axis].min)
   {
      *value = conf->axis[axis].min;
   }
   else if (*value > conf->axis[axis].max)
   {
      *value = conf->axis[axis].max;
   }

   /* TODO: handle the other properties. */

   return 1;
}

void relabsd_config_get_absinfo
(
   const struct relabsd_config * const conf,
   enum relabsd_axis const axis,
   struct input_absinfo * const absinfo
)
{
   absinfo->value = (__s32) 0;
   absinfo->minimum = (__s32) conf->axis[axis].min;
   absinfo->maximum = (__s32) conf->axis[axis].max;
   absinfo->fuzz = (__s32) conf->axis[axis].fuzz;
   absinfo->flat = (__s32) conf->axis[axis].flat;
   absinfo->resolution = (__s32) conf->axis[axis].resolution;
}