monome / norns

norns is many sound instruments.
http://monome.org
GNU General Public License v3.0
630 stars 147 forks source link

handling input events #16

Closed catfact closed 7 years ago

catfact commented 7 years ago

need to add usb hotplugging (see #11), which means udev. a basic example, works for me to see plug/unplug events from grid: https://gist.github.com/catfact/5ec9ce9af77c0185c88026c52f38b98d

so while we're at it, we could also add libevdev to get arbitrary hid data. a basic example, works for me to read joystick: https://github.com/catfact/kit/blob/master/hid/libevdev/main.c

libevdev gets us partly away from requiring SDL, the other part is replacing the event queue

catfact commented 7 years ago

hotplug is added in PR #23

catfact commented 7 years ago

event queue is rewritten in PR #24. this is just a basic posix queue driven by mutexes and condition variables, no asynchronous messaging system.

catfact commented 7 years ago

i am wondering exactly how to organize input events. in our case that mostly means HID events, but it could include other things (say, the appearance or disappearance of network devices.)

the way usb monitoring is set up so far, we get a "device added" event every time a new node shows up in /dev/inputs/eventX.

libevdev can query the new node to see what kind of events it supports. there are more devices and more events than one might think; for example on my thinkpad, there are separate nodes for the builtin keyboard, media keys, lid switch, power button, touchpad (which sends absolute position), track pointer(which sends relative position, like a mouse), HID devices connected by USB, &c.

in addition there are event nodes that track things like changes to the video bus (when a display device is added), headphone jack plugged/unplugged, &c.


anyways, the question is: when a device is discovered (either by hotplugging or scan at startup), what to do with it: decide if a "device added" event should be posted to lua, and if the event node should be polled for input events.

i think it should be easy enough to filter only event nodes with a parent in the usb subsystem, if we want. that's step one.

in usb monitor/scan functions, i'm simply using regex searches to identify e.g. monome devices just by their device path. i propose to extend this so that regex patterns are specified in the configuration file (which doesn't exist yet, but that's another issue...) this would let us exclude event nodes that are always present and we don't want to attach to lua.

then, i'd propose a relatively simple and transparent system for mapping devices and events. on device detection, libevdev can generate a report including:

all this can be sent to lua on device addition. on device removal, just send the device ID.

on reception of an event, trigger a lua function like input_event(device_id, event_type, event_code).

lua scripts then have the freedom to decide how to map input devices to internal functions:

let me know any thoughts. i'll start building out the infrastructure, API details can change as needed.

catfact commented 7 years ago

PR #27 provides the infrastructure.

catfact commented 7 years ago

infrastructure seems ok so i am closing this for now, pending specific issues