aboutsummaryrefslogtreecommitdiff
path: root/uinput_mng
blob: b8d60e68c12e72968c908001ab20d8747c3e4158 (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
355
356
357
358
359
360
#!/usr/bin/python3

import argparse
import contextlib
from evdev import ecodes, ff, uinput, InputDevice
import json
import logging
import os
import selectors
import stat
import sys

class Controller:

    _file       = None
    _in_devices = None
    _logger     = None

    def __init__(self, path, sel, devices, logger):
        self._logger     = logger.getChild(str(self))
        self._in_devices = devices

        fd = os.open(path, os.O_RDWR | os.O_NONBLOCK)
        try:
            self._file = os.fdopen(fd)
        except:
            os.close(fd)
            raise

    def __str__(self):
        return 'Controller'

    def __enter__(self):
        return self
    def __exit__(self, exc_type, exc_value, traceback):
        self.close()

    def close(self):
        self._file.close()

    def fileno(self):
        return self._file.fileno()

    def process(self, sel_key):
        for line in self._file:
            cmd, _, rest = line.rstrip('\n').partition(' ')
            self._logger.debug('Processing command: %s %s', cmd, rest)

            if cmd == 'add':
                self._in_devices.add(rest)
            elif cmd == 'remove':
                self._in_devices.remove(rest)
            elif cmd == 'list':
                for d in self._in_devices:
                    sys.stderr.write(d + '\n')
            elif cmd == 'clear':
                self._in_devices.clear()
            else:
                self._logger.error('Unknown command: %s', line)

class UInputDevice:

    _dev         = None
    _tgt_devices = None

    _logger      = None

    def __init__(self, desc_path, devices, logger):
        self._logger = logger.getChild(str(self))

        self._tgt_devices = devices

        with open(desc_path, 'rb') as f:
            desc = json.load(f)

        if not 'linux-evdev-device-desc' in desc:
            raise ValueError('device_desc is not a valid device description')
        if desc['linux-evdev-device-desc'] > 0:
            raise ValueError('Unsupported device description version: %d' % desc['linux-evdev-device-desc'])

        events = {}
        for key, val in desc['capabilities'].items():
            events[int(key)] = val

        self._dev = uinput.UInput(events  = events,
                                  name    = desc['name'],
                                  vendor  = desc['info']['vendor'],
                                  product = desc['info']['product'],
                                  version = desc['info']['version'],
                                  bustype = desc['info']['bustype'])
        self._logger.info('Created UInput device "%s": %s' % (self._dev.name, self._dev.device.path))

    def __str__(self):
        return 'UInputDevice'

    def __enter__(self):
        return self
    def __exit__(self, exc_type, exc_value, traceback):
        self._dev.close()

    def fileno(self):
        return self._dev.fileno()

    def write_event(self, ev):
        self._dev.write_event(ev)

    def process(self, sel_key):
        for ev in self._dev.read():
            self._logger.debug('processing event: %s', ev)

            if ev.type == ecodes.EV_FF:
                # force feedback command, forward to devices
                self._logger.debug('Forwarding an FF event')
                self._tgt_devices.write_ff_event(ev)
            elif ev.type == ecodes.EV_LED:
                for d in self._tgt_devices.values():
                    d.set_led(ev.code, ev.value)
            elif ev.type == ecodes.EV_UINPUT:
                if ev.code == ecodes.UI_FF_UPLOAD:
                    upload = self._dev.begin_upload(ev.value)

                    self._logger.debug('Effect upload: id %d; type 0x%x direction 0x%x',
                                       upload.effect.id, upload.effect.type, upload.effect.direction)

                    try:
                        self._tgt_devices.upload_effect(upload.effect)
                        upload.retval = 0
                    except:
                        upload.retval = -1
                        raise
                    finally:
                        self._dev.end_upload(upload)
                elif ev.code == ecodes.UI_FF_ERASE:
                    erase = self._dev.begin_erase(ev.value)

                    self._logger.debug('Effect erase: %d', erase.effect_id)

                    try:
                        self._tgt_devices.erase_effect(erase.effect_id)
                        erase.retval = 0
                    except:
                        erase.retval = -1
                        raise
                    finally:
                        self._dev.end_erase(erase)
            elif ev.type == ecodes.EV_MSC and ev.type == ecodes.MSC_SCAN:
                self._logger.debug('Ignoring MSC_SCAN: %d', ev.value)
            else:
                self._logger.warning('Unhandled event on the virtual device: %s', ev)

class PhysDevices(dict):
    _sel    = None
    _logger = None

    # dict of FF effects currently uploaded to the uinput device
    # indexed by their id on the uinput device
    _ff_effects = None

    # FF effect ids are assigned by the driver, so we need to translate
    # between effect ids on the uinput device and the individual physical
    # devices
    # this dict mapps each device path (same keys as self) to a dict that
    # maps the caller's FF effect ids to that device's ids
    _ff_effect_idmap = None

    uinput_dev = None

    def __init__(self, sel, logger):
        self._sel    = sel
        self._logger = logger.getChild(str(self))

        self._ff_effects      = {}
        self._ff_effect_idmap = {}

        super().__init__(self)

    def __str__(self):
        return 'PhysDevices(%d)' % len(self)

    def __enter__(self):
        return self
    def __exit__(self, exc_type, exc_value, traceback):
        self.close()

    def clear(self):
        for path in list(self.keys()):
            self.remove(path)
    def close(self):
        self.clear()

    def add(self, path):
        if path in self:
            raise KeyError("Device already added: %s" % path)

        d = InputDevice(path)
        try:
            self._sel.register(d, selectors.EVENT_READ, self)

            # upload FF effects that already exist on the virtual device
            idmap = {}
            for eid, effect in self._ff_effects.items():
                # make a copy of the effect and reset its ID, to let
                # the driver know we are uploading a new effect
                effect_d    = ff.Effect(type = effect.type, direction = effect.direction,
                                        ff_trigger = effect.ff_trigger,
                                        ff_replay = effect.ff_replay, u = effect.u, id = -1)

                did = d.upload_effect(effect_d)
                idmap[eid] = did
            self._ff_effect_idmap[path] = idmap

            self[path] = d
        except:
            d.close()
            raise

        self._logger.info('Added: %s', str(d))

    def remove(self, path):
        d = self.pop(path)

        self._logger.info('Removing: %s', str(d))

        self._sel.unregister(d)

        # the following might fail if the device has been unplugged
        try:
            # erase any uploaded FF effects
            for did in self._ff_effect_idmap[path].values():
                d.erase_effect(did)

        except OSError as e:
            self._logger.error('Error unloading device "%s": %s',
                               path, os.strerror(e.errno))
        d.close()

        del self._ff_effect_idmap[path]

    def upload_effect(self, effect):
        # upload the effect to all currently tracked devices
        for name, d in self.items():
            # is this a new effect or a change of an existing one
            try:
                upload_id = self._ff_effect_idmap[name][effect.id]
            except LookupError:
                upload_id = -1

            effect_d    = ff.Effect(type = effect.type, direction = effect.direction,
                                    ff_trigger = effect.ff_trigger,
                                    ff_replay = effect.ff_replay, u = effect.u, id = upload_id)

            did = d.upload_effect(effect_d)
            self._ff_effect_idmap[name][effect.id] = did

        # store the effect for upload to any newly added devices
        self._ff_effects[effect.id] = effect

    def erase_effect(self, effect_id):
        if not effect_id in self._ff_effects:
            raise KeyError('No such effect uploaded: %d' % effect_id)

        for name, d in self.items():
            did = self._ff_effect_idmap[name].pop(effect_id)
            d.erase_effect(did)

        del self._ff_effects[effect_id]

    def write_ff_event(self, ev):
        for name, d in self.items():
            c = ev.code
            if c in self._ff_effects:
                c = self._ff_effect_idmap[name][c]
            d.write(ecodes.EV_FF, c, ev.value)

    def process(self, sel_key):
        fo = sel_key.fileobj
        if not fo.path in self:
            self._logger.debug("Process request for removed device %s", str(fo))
            return

        try:
            for ev in fo.read():
                self._logger.debug('phys->uinput: %s', ev)

                if ev.type == ecodes.EV_FF:
                    self._logger.debug('disregarding FF event')
                    continue

                self.uinput_dev.write_event(ev)
        except OSError as e:
            self._logger.error('Error reading from device "%s": %s',
                               str(fo), os.strerror(e.errno))
            self.remove(fo.path)

parser = argparse.ArgumentParser()

parser.add_argument('-v', '--verbose', action = 'count', default = 0)
parser.add_argument('-q', '--quiet',   action = 'count', default = 0)
parser.add_argument('-c', '--control')
parser.add_argument('device_desc')

args = parser.parse_args(sys.argv[1:])

# setup logging
# default to 20 (INFO), every -q goes a level up, every -v a level down
log_level  = max(10 * (2 + args.quiet - args.verbose), 0)
log_format = '%(asctime)s:%(name)s:%(levelname)s: %(message)s'
logging.basicConfig(format = log_format, datefmt = '%F %T', level = log_level)
logger = logging.getLogger(os.path.basename(sys.argv[0]))

# the device description argument is either a full path or the root name of
# a file in ~/.local/var/inputfwd/
desc_path = os.path.expanduser(args.device_desc)
if not desc_path or desc_path[0] != '/':
    desc_path = os.path.join(os.path.expanduser('~/.local/var/inputfwd/'),
                             args.device_desc + '.desc')
if not os.path.exists(desc_path):
    logger.error('Device description "%s"->"%s" does not exist',
                 args.device_desc, desc_path)
    sys.exit(1)

# by default the control FIFO is in ~/.cache/inputfwd/<desc basename root>
control_path = args.control
if not control_path:
    control_path = os.path.join(os.path.expanduser('~/.cache/inputfwd'),
                                os.path.splitext(os.path.basename(desc_path))[0])

# create the FIFO if it does not exist yet
if not os.path.exists(control_path):
    logger.info('Creating control FIFO: %s', control_path)
    os.makedirs(os.path.dirname(control_path), exist_ok = True)
    os.mkfifo(control_path, mode = 0o600)
else:
    st = os.stat(control_path)
    if not stat.S_ISFIFO(st.st_mode):
        logger.error('Control path "%s" is not a FIFO', control_path)
        sys.exit(1)

with contextlib.ExitStack() as stack:
    sel = stack.enter_context(selectors.DefaultSelector())

    devices    = stack.enter_context(PhysDevices(sel, logger))
    uinput     = stack.enter_context(UInputDevice(desc_path, devices, logger))
    controller = stack.enter_context(Controller(control_path, sel, devices, logger))

    devices.uinput_dev = uinput

    sel.register(controller, selectors.EVENT_READ)
    sel.register(uinput,     selectors.EVENT_READ)

    while True:
        logger.debug('polling')
        events = sel.select()
        logger.debug('got events:')
        for key, mask in events:
            tgt = key.data if key.data is not None else key.fileobj
            try:
                tgt.process(key)
            except Exception as e:
                logging.exception('Error processing event on: %s', str(tgt))