mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:27:35 +00:00
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.
This commit is contained in:
parent
75183402fd
commit
8a0a3638f0
6 changed files with 111 additions and 0 deletions
57
Kernel/Devices/HID/AllMiceDevice.cpp
Normal file
57
Kernel/Devices/HID/AllMiceDevice.cpp
Normal file
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue