Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mouse #72

Merged
merged 6 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ default-members = ["kernel"]
members = [
"kernel",
"libraries/kernel_user_link", "libraries/increasing_heap_allocator", "libraries/emerald_std",
"libraries/keyboard",
"libraries/emerald_runtime",
"userspace/init", "userspace/shell", "userspace/graphics",
]

Expand Down
1 change: 1 addition & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [Drivers/Devices](./kernel/drivers/index.md)
- [IDE](./kernel/drivers/ide.md)
- [Keyboard](./kernel/drivers/keyboard.md)
- [Mouse](./kernel/drivers/mouse.md)
- [UART](./kernel/drivers/uart.md)
- [Virtual Devices](./kernel/virtual_devices/index.md)
- [Console](./kernel/virtual_devices/console.md)
Expand Down
13 changes: 7 additions & 6 deletions book/src/kernel/drivers/keyboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

> This is implemented in [`keyboard`][keyboard]

The keyboard driver is simple, and uses the legacy PS/2 interface at `0x60` and `0x64` ports.
The keyboard driver is simple, and uses the legacy PS/2 interface at `0x60` and `0x64` ports,
its implemented alongside the [mouse](./mouse.md) driver in the same file.

The driver provide events broadcasts to all listeners using [`blinkcast`]. These listeners
are mostly processes reading from the `/devices/keyboard` file (see [keyboard reader](#keyboard-reader)).
Expand All @@ -29,8 +30,8 @@ There are 2 types of modifiers:
- Held modifiers: `SHIFT`, `CTRL`, `ALT`
- Toggled modifiers: `CAPSLOCK`, `NUMLOCK`, `SCROLLLOCK`

# Keyboard reader
The keyboard driver provide a way to get a [`blinkcast`] reader using [`get_reader`][keyboard_get_reader],
## Keyboard reader
The keyboard driver provide a way to get a [`blinkcast`] reader using [`get_keyboard_reader`][get_keyboard_reader],
where the user can read keyboard events without blocking anytime they want.

The [console](../virtual_devices/console.md) and userspace processes use this reader to read keyboard events.
Expand All @@ -41,12 +42,12 @@ A process can open a file descriptor to this device and read from it to get keyb

The file descriptor will hold a [`blinkcast`] reader to the keyboard driver, then each process can read events without blocking.

The user can open the file and read the content, but since we are performing some encoding, its better to use the library [`emerald_keyboard`] which provide easy way to read the events.
The user can open the file and read the content, but since we are performing some encoding, its better to use the library [`emerald_runtime`] which provide easy way to read the events.

Example:

```rust,no_run,no_compile
use emerald_keyboard::Keyboard;
use emerald_runtime::keyboard::Keyboard;

let mut keyboard = Keyboard::new();

Expand All @@ -63,4 +64,4 @@ for key in keyboard.iter_keys() {
[`blinkcast`]: https://crates.io/crates/blinkcast
[`Key::virtual_key`]: https://docs.rs/emerald_kernel_user_link/0.2.5/emerald_kernel_user_link/keyboard/struct.Key.html#method.virtual_char
[`modifier`]: https://docs.rs/emerald_kernel_user_link/0.2.5/emerald_kernel_user_link/keyboard/modifier
[`emerald_keyboard`]: https://crates.io/crates/emerald_keyboard
[`emerald_runtime`]: https://crates.io/crates/emerald_runtime
71 changes: 71 additions & 0 deletions book/src/kernel/drivers/mouse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{{ #include ../../links.md }}

# Mouse

> This is implemented in [`mouse`][mouse]


The mouse driver is simple, and uses the legacy PS/2 interface at `0x60` and `0x64` ports,
its implemented alongside the [keyboard](./keyboard.md) driver in the same file.

The driver provide events broadcasts to all listeners using [`blinkcast`]. These listeners
are mostly processes reading from the `/devices/mouse` file (see [mouse reader](#mouse-reader)).
```rust
pub enum ScrollType {
None = 0,
VerticalUp = 1,
VerticalDown = 2,
HorizontalRight = 3,
HorizontalNegative = 4,
}

pub struct MouseEvent {
pub x: i16,
pub y: i16,
pub scroll_type: ScrollType,
pub buttons: u8,
}
```

The `buttons` field is a bitflags from [`buttons`], so use these constants to check a button is pressed.

Note, that this is the state of the mouse, so you must keep the old state to know if a button was pressed or released.

The buttons are:
- `LEFT`: `0b0000_0001`
- `RIGHT`: `0b0000_0010`
- `MIDDLE`: `0b0000_0100`
- `FORTH`: `0b0000_1000`
- `FIFTH`: `0b0001_0000`

## Mouse reader
The keyboard driver provide a way to get a [`blinkcast`] reader using [`get_mouse_reader`][get_mouse_reader],
where the user can read mouse events without blocking anytime they want.

Userspace processes can read the mouse events through the virtual device at `/devices/mouse`.

A process can open a file descriptor to this device and read from it to get mouse events.

The file descriptor will hold a [`blinkcast`] reader to the mouse driver, then each process can read events without blocking.

The user can open the file and read the content, but since we are performing some encoding, its better to use the library [`emerald_runtime`] which provide easy way to read the events.

Example:

```rust,no_run,no_compile
use emerald_runtime::mouse::Mouse;

let mut mouse = Mouse::new();

if let Some(event) = mouse.get_event() {
println!("Event: {:?}", event);
}
// or
for event in mouse.iter_events() {
println!("Event: {:?}", event);
}
```

[`blinkcast`]: https://crates.io/crates/blinkcast
[`buttons`]: https://docs.rs/emerald_kernel_user_link/0.2.6/emerald_kernel_user_link/mouse/buttons/index.html
[`emerald_runtime`]: https://crates.io/crates/emerald_runtime
6 changes: 4 additions & 2 deletions book/src/links.md.template
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
[virtual_memory_mapper]: {ROOT_PATH}docs/kernel/memory_management/virtual_memory_mapper
[ide_device]: {ROOT_PATH}docs/kernel/devices/ide
[ide_read_sync]: {ROOT_PATH}docs/kernel/devices/ide/struct.IdeDevice.html#method.read_sync
[keyboard]: {ROOT_PATH}docs/kernel/devices/keyboard
[keyboard_get_reader]: {ROOT_PATH}docs/kernel/devices/keyboard/struct.Keyboard.html#method.get_reader
[keyboard]: {ROOT_PATH}docs/kernel/devices/keyboard_mouse/keyboard
[get_keyboard_reader]: {ROOT_PATH}docs/kernel/devices/keyboard_mouse/struct.KeyboardMouse.html#method.get_keyboard_reader
[get_mouse_reader]: {ROOT_PATH}docs/kernel/devices/keyboard_mouse/struct.KeyboardMouse.html#method.get_mouse_reader
[mouse]: {ROOT_PATH}docs/kernel/devices/keyboard_mouse/mouse
[uart]: {ROOT_PATH}docs/kernel/io/uart
[console]: {ROOT_PATH}docs/kernel/io/console
[kernel_filesystem]: {ROOT_PATH}docs/kernel/fs
Expand Down
8 changes: 7 additions & 1 deletion kernel/src/cpu/interrupts/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
//! Global handlers that have several purposes and doesn't belong in 1 place specifically

use crate::{cpu::idt::InterruptAllSavedState, devices::clock, process::scheduler};
use crate::{
cpu::idt::InterruptAllSavedState,
devices::{clock, keyboard_mouse},
process::scheduler,
};

use super::apic;

pub extern "cdecl" fn apic_timer_handler(all_state: &mut InterruptAllSavedState) {
// make sure its initialized
clock::clocks().tick_system_time();
// trigger poll if there is any events
keyboard_mouse::poll_events();

scheduler::yield_current_if_any(all_state);
apic::return_from_interrupt();
Expand Down
Loading
Loading