1 #include <lunaix/clock.h>
2 #include <lunaix/input.h>
3 #include <lunaix/mm/page.h>
4 #include <lunaix/mm/valloc.h>
5 #include <lunaix/spike.h>
6 #include <lunaix/status.h>
8 #include <klibc/string.h>
10 static DEFINE_LLIST(listener_chain);
12 static struct device_cat* input_devcat = NULL;
17 input_devcat = device_addcat(NULL, "input");
21 input_fire_event(struct input_device* idev, struct input_evt_pkt* pkt)
23 pkt->timestamp = clock_systime();
24 idev->current_pkt = *pkt;
26 struct input_evt_chain *pos, *n;
27 llist_for_each(pos, n, &listener_chain, chain)
29 if (pos->evt_cb(idev) == INPUT_EVT_CATCH) {
34 // wake up all pending readers
35 pwake_all(&idev->readers);
39 input_add_listener(input_evt_cb listener)
43 struct input_evt_chain* chain = vzalloc(sizeof(*chain));
44 llist_append(&listener_chain, &chain->chain);
46 chain->evt_cb = listener;
50 __input_dev_read(struct device* dev, void* buf, size_t offset, size_t len)
52 struct input_device* idev = dev->underlay;
54 if (len < sizeof(struct input_evt_pkt)) {
59 pwait(&idev->readers);
61 memcpy(buf, &idev->current_pkt, sizeof(struct input_evt_pkt));
63 return sizeof(struct input_evt_pkt);
67 __input_dev_read_pg(struct device* dev, void* buf, size_t offset)
69 return __input_dev_read(dev, buf, offset, PG_SIZE);
73 input_add_device(struct devclass* class, char* name_fmt, ...)
77 struct input_device* idev = vzalloc(sizeof(*idev));
78 waitq_init(&idev->readers);
81 va_start(args, name_fmt);
83 struct device* dev = device_allocseq(dev_meta(input_devcat), idev);
85 device_setname_vargs(dev_meta(dev), name_fmt, args);
86 register_device(dev, class, NULL);
89 dev->ops.read = __input_dev_read;
90 dev->ops.read_page = __input_dev_read_pg;