mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-08 12:56:23 +03:00
8a0a3638f0
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.
58 lines
1.5 KiB
C++
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;
|
|
}
|
|
|
|
}
|