serenity/Kernel/Devices/HID/MouseDevice.cpp
Liav A. 3edc6ae0d6 Kernel/Devices: Improve construction paths semantically
Do this by:
- Removing more instances of `LockRefPtr` and `NonnullLockRefPtr`.
- Using better names of construction methods (i.e. `create` instead of
  `try_create`).
- Only returning `NonnullRefPtr` on the `Device::try_create_device`
  method.
- Removing a version of the `Device::try_create_device` method that
  called `DeviceType::try_create(forward<Args>(args)...)`, which was
  only used in a construction point in a VirtIO driver which now doesn't
  need this anymore.
2024-10-05 12:26:48 +02:00

63 lines
1.9 KiB
C++

/*
* Copyright (c) 2021-2023, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/API/MajorNumberAllocation.h>
#include <Kernel/Devices/Device.h>
#include <Kernel/Devices/HID/Management.h>
#include <Kernel/Devices/HID/MouseDevice.h>
namespace Kernel {
ErrorOr<NonnullRefPtr<MouseDevice>> MouseDevice::try_to_initialize()
{
return TRY(Device::try_create_device<MouseDevice>());
}
MouseDevice::MouseDevice()
: HIDDevice(MajorAllocation::CharacterDeviceFamily::Mouse, HIDManagement::the().generate_minor_device_number_for_mouse())
{
}
void MouseDevice::handle_mouse_packet_input_event(MousePacket packet)
{
m_entropy_source.add_random_event(packet);
{
SpinlockLocker lock(m_queue_lock);
m_queue.enqueue(packet);
}
evaluate_block_conditions();
}
MouseDevice::~MouseDevice() = default;
bool MouseDevice::can_read(OpenFileDescription const&, u64) const
{
SpinlockLocker lock(m_queue_lock);
return !m_queue.is_empty();
}
ErrorOr<size_t> MouseDevice::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();
dbgln_if(MOUSE_DEBUG, "Mouse Read: Buttons {:x}", packet.buttons);
dbgln_if(MOUSE_DEBUG, "Mouse: X {}, Y {}, Z {}, W {}, Relative {}", packet.x, packet.y, packet.z, packet.w, packet.buttons);
dbgln_if(MOUSE_DEBUG, "Mouse Read: Filter packets");
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;
}
}