ladybird/Kernel/Devices/HID/AllMiceDevice.cpp
Liav A 8a0a3638f0 Kernel/HID: Introduce the all-mice device
This device will be used by userspace to read mouse packets from all
mouse devices that are attached to the machine.

This change is a preparation before we can enable seamless hotplug
capabilities in WindowServer for mouse devices, without any major change
on the userspace side.
2024-01-12 16:08:08 -07:00

58 lines
1.5 KiB
C++

/*
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/HID/AllMiceDevice.h>
#include <Kernel/Devices/HID/Management.h>
namespace Kernel {
NonnullRefPtr<AllMiceDevice> AllMiceDevice::must_create()
{
return *MUST(DeviceManagement::try_create_device<AllMiceDevice>());
}
AllMiceDevice::AllMiceDevice()
: CharacterDevice(12, 0)
{
}
void AllMiceDevice::enqueue_mouse_packet(MousePacket packet)
{
{
SpinlockLocker lock(m_queue_lock);
m_queue.enqueue(packet);
}
evaluate_block_conditions();
}
AllMiceDevice::~AllMiceDevice() = default;
bool AllMiceDevice::can_read(OpenFileDescription const&, u64) const
{
SpinlockLocker lock(m_queue_lock);
return !m_queue.is_empty();
}
ErrorOr<size_t> AllMiceDevice::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)
{
VERIFY(size > 0);
size_t nread = 0;
size_t remaining_space_in_buffer = static_cast<size_t>(size) - nread;
SpinlockLocker lock(m_queue_lock);
while (!m_queue.is_empty() && remaining_space_in_buffer) {
auto packet = m_queue.dequeue();
size_t bytes_read_from_packet = min(remaining_space_in_buffer, sizeof(MousePacket));
TRY(buffer.write(&packet, nread, bytes_read_from_packet));
nread += bytes_read_from_packet;
remaining_space_in_buffer -= bytes_read_from_packet;
}
return nread;
}
}