dev: implement writing/reading input events to/from input devices

This commit is contained in:
2023-05-10 20:33:40 +01:00
parent ad8865fe66
commit 05395542a8
4 changed files with 242 additions and 0 deletions

View File

@@ -79,6 +79,26 @@ struct device *device_alloc(void)
return DEVICE_CAST(dev_object);
}
kern_status_t device_read(struct device *dev, void *buf, size_t size, size_t *bytes_read)
{
switch (dev->dev_type) {
case DEV_TYPE_INPUT:
return input_device_read(dev, buf, size, bytes_read);
default:
return KERN_UNSUPPORTED;
}
}
kern_status_t device_write(struct device *dev, const void *buf, size_t size, size_t *bytes_written)
{
return KERN_UNSUPPORTED;
}
struct device *cast_to_device(struct object *obj)
{
return DEVICE_CAST(obj);
}
static kern_status_t device_object_destroy(struct object *obj)
{
return KERN_OK;

View File

@@ -1,4 +1,5 @@
#include <socks/device.h>
#include <socks/input.h>
struct input_device *input_device_create(void)
{
@@ -8,5 +9,43 @@ struct input_device *input_device_create(void)
}
dev->dev_type = DEV_TYPE_INPUT;
struct input_device *input_dev = INPUT_DEVICE(dev);
if (ringbuffer_init(&input_dev->i_events, INPUT_DEVICE_EVENT_QUEUE_SIZE * sizeof(struct input_event)) != KERN_OK) {
/* TODO destroy device */
return NULL;
}
return INPUT_DEVICE(dev);
}
kern_status_t input_device_report_event(struct input_device *dev, const struct input_event *ev, bool noblock)
{
struct ringbuffer *event_queue = &dev->i_events;
enum ringbuffer_flags flags = RB_NORMAL;
if (noblock) {
flags = RB_NOBLOCK;
}
size_t r = ringbuffer_write(event_queue, sizeof *ev, ev, flags);
return r == sizeof *ev ? KERN_OK : KERN_WOULD_BLOCK;
}
kern_status_t input_device_read(struct device *dev, void *buf, size_t size, size_t *bytes_read)
{
if (dev->dev_type != DEV_TYPE_INPUT || (size % sizeof (struct input_event)) != 0) {
return KERN_INVALID_ARGUMENT;
}
struct input_device *input_dev = INPUT_DEVICE(dev);
struct ringbuffer *event_queue = &input_dev->i_events;
size_t r = ringbuffer_read(event_queue, size, buf, RB_NORMAL);
if (bytes_read) {
*bytes_read = r;
}
return KERN_OK;
}