summaryrefslogtreecommitdiff
blob: b60a0741be36e02692bd8cc32448f406dab4d137 (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
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>

#include "pervasive.h"
#include "error.h"
#include "config.h"
#include "input.h"
#include "relabsd_device.h"

static int RELABSD_RUN = 1;

static void interrupt (int signal_id)
{
   RELABSD_RUN = 0;

   _S_WARNING("Interrupted, will exit at the next input device event.");
}

static void convert_input
(
   const struct relabsd_config * const conf,
   const struct relabsd_input * const input,
   const struct relabsd_device * const dev
)
{
   unsigned int input_type, input_code, abs_code;
   enum relabsd_axis rad_code;
   int value;

   while (RELABSD_RUN == 1)
   {
      if (relabsd_input_read(input, &input_type, &input_code, &value) < 0)
      {
         continue;
      }

      if (input_type == EV_REL)
      {
         rad_code = relabsd_axis_convert_evdev_rel(input_code, &abs_code);

         if (rad_code == RELABSD_UNKNOWN)
         {
            relabsd_device_write_evdev_event
            (
               dev,
               input_type,
               input_code,
               value
            );
         }
         else if (relabsd_config_allows(conf, rad_code, value))
         {
            relabsd_device_write_evdev_event(dev, EV_ABS, abs_code, value);
         }
      }
      else
      {
         relabsd_device_write_evdev_event
         (
            dev,
            input_type,
            input_code,
            value
         );
      }
   }
}

int main (int argc, char ** argv)
{
   struct relabsd_config conf;
   struct relabsd_input input;
   struct relabsd_device dev;

   if (signal(SIGINT, interrupt) == SIG_ERR)
   {
      _S_FATAL("Unable to set the SIGINT signal handler.");

      return -1;
   }

   if (relabsd_config_parse(&conf, argc, argv) < 0)
   {
      return -2;
   }

   if (relabsd_input_open(&input, conf.input_file) < 0)
   {
      return -3;
   }

   if (relabsd_device_create(&dev, &conf) < 0)
   {
      return -4;
   }

   _S_DEBUG(10, "Converting inputs...");

   convert_input(&conf, &input, &dev);

   _S_DEBUG(10, "Terminating...");

   relabsd_device_destroy(&dev);
   relabsd_input_close(&input);

   return 0;
}