1 #include <lunaix/clock.h>
2 #include <lunaix/input.h>
3 #include <lunaix/mm/valloc.h>
4 #include <lunaix/spike.h>
5 #include <lunaix/status.h>
7 #include <klibc/string.h>
9 static DEFINE_LLIST(listener_chain);
11 static struct device* input_devcat = NULL;
16 input_devcat = device_addcat(NULL, "input");
20 input_fire_event(struct input_device* idev, struct input_evt_pkt* pkt)
22 pkt->timestamp = clock_systime();
23 idev->current_pkt = *pkt;
25 struct input_evt_chain *pos, *n;
26 llist_for_each(pos, n, &listener_chain, chain)
28 if (pos->evt_cb(idev) == INPUT_EVT_CATCH) {
33 // wake up all pending readers
34 pwake_all(&idev->readers);
38 input_add_listener(input_evt_cb listener)
42 struct input_evt_chain* chain = vzalloc(sizeof(*chain));
43 llist_append(&listener_chain, &chain->chain);
45 chain->evt_cb = listener;
49 __input_dev_read(struct device* dev, void* buf, size_t offset, size_t len)
51 struct input_device* idev = dev->underlay;
53 if (len < sizeof(struct input_evt_pkt)) {
58 pwait(&idev->readers);
60 memcpy(buf, &idev->current_pkt, sizeof(struct input_evt_pkt));
62 return sizeof(struct input_evt_pkt);
66 input_add_device(char* name_fmt, ...)
70 struct input_device* idev = vzalloc(sizeof(*idev));
71 waitq_init(&idev->readers);
74 va_start(args, name_fmt);
77 device_add(input_devcat, idev, name_fmt, DEV_IFSEQ, args);
80 dev->read = __input_dev_read;