Skip to content

Commit

Permalink
exit gui app when pressing Esc key
Browse files Browse the repository at this point in the history
  • Loading branch information
Godones committed Aug 6, 2024
1 parent f5c6f57 commit 5fd6ef4
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 9 deletions.
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ TALLOC ?=y
BUDDY ?=n
FS ?=fat
INITRD ?=y


QEMU := qemu-system-riscv64
comma:= ,
empty:=
space:= $(empty) $(empty)
Expand Down Expand Up @@ -83,7 +82,7 @@ endif
FEATURES := $(subst $(space),$(comma),$(FEATURES))

define boot_qemu
qemu-system-riscv64 \
$(QEMU) \
-M virt $(1)\
-bios $(BOOTLOADER) \
-drive file=$(IMG),if=none,format=raw,id=x0 \
Expand Down
3 changes: 1 addition & 2 deletions user/apps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ BUILD_CRATES := \
print \

ifeq ($(GUI),y)
BUILD_CRATES += egui \
sysinfo \
BUILD_CRATES += sysinfo \
todo \
slint \
memory-game \
Expand Down
31 changes: 31 additions & 0 deletions user/apps/memory-game/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use slint_helper::{MyPlatform, SwapBuffer};
use virt2slint::Converter;
use Mstd::{
io::{keyboard_or_mouse_event, VIRTGPU_XRES, VIRTGPU_YRES},
println,
process::exit,
time::get_time_ms,
};

Expand Down Expand Up @@ -108,10 +110,39 @@ fn checkout_event(converter: &mut Converter, x: &mut isize, y: &mut isize) -> Ve
for i in 0..event_num as usize {
let event = events[i];
// let window_event = input2event(event, x, y);
let e = InputEvent::from(event);
if e.event_type == 1 && (e.code == 1 || e.code == 273) && e.value == 0 {
println!("ESC or right-click pressed, exit");
exit(0);
}
let window_event = converter.convert(event, x, y);
window_event.map(|e| {
res.push(e);
});
}
res
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub struct InputEvent {
/// Event type.
pub event_type: u16,
/// Event code.
pub code: u16,
/// Event value.
pub value: u32,
}

impl From<u64> for InputEvent {
fn from(event: u64) -> Self {
let event_type = (event >> 48) as u16;
let code = ((event >> 32) & 0xFFFF) as u16;
let value = (event & 0xFFFFFFFF) as u32;
Self {
event_type,
code,
value,
}
}
}
35 changes: 34 additions & 1 deletion user/apps/printdemo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ use alloc::{rc::Rc, vec::Vec};
use slint::{platform::WindowEvent, Model};
use slint_helper::{MyPlatform, SwapBuffer};
use virt2slint::Converter;
use Mstd::io::{keyboard_or_mouse_event, VIRTGPU_XRES, VIRTGPU_YRES};
use Mstd::{
io::{keyboard_or_mouse_event, VIRTGPU_XRES, VIRTGPU_YRES},
println,
process::exit,
};

#[no_mangle]
fn fmod(x: f64, y: f64) -> f64 {
Expand Down Expand Up @@ -161,10 +165,39 @@ fn checkout_event(converter: &mut Converter, x: &mut isize, y: &mut isize) -> Ve
for i in 0..event_num as usize {
let event = events[i];
// let window_event = input2event(event, x, y);
let e = InputEvent::from(event);
if e.event_type == 1 && (e.code == 1 || e.code == 273) && e.value == 0 {
println!("ESC or right-click pressed, exit");
exit(0);
}
let window_event = converter.convert(event, x, y);
window_event.map(|e| {
res.push(e);
});
}
res
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub struct InputEvent {
/// Event type.
pub event_type: u16,
/// Event code.
pub code: u16,
/// Event value.
pub value: u32,
}

impl From<u64> for InputEvent {
fn from(event: u64) -> Self {
let event_type = (event >> 48) as u16;
let code = ((event >> 32) & 0xFFFF) as u16;
let value = (event & 0xFFFFFFFF) as u32;
Self {
event_type,
code,
value,
}
}
}
35 changes: 34 additions & 1 deletion user/apps/slint/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use alloc::vec::Vec;
use slint::platform::WindowEvent;
use slint_helper::{MyPlatform, SwapBuffer};
use virt2slint::Converter;
use Mstd::io::{keyboard_or_mouse_event, VIRTGPU_XRES, VIRTGPU_YRES};
use Mstd::{
io::{keyboard_or_mouse_event, VIRTGPU_XRES, VIRTGPU_YRES},
println,
process::exit,
};

slint::include_modules!();

Expand Down Expand Up @@ -61,10 +65,39 @@ fn checkout_event(converter: &mut Converter, x: &mut isize, y: &mut isize) -> Ve
for i in 0..event_num as usize {
let event = events[i];
// let window_event = input2event(event, x, y);
let e = InputEvent::from(event);
if e.event_type == 1 && (e.code == 1 || e.code == 273) && e.value == 0 {
println!("ESC or right-click pressed, exit");
exit(0);
}
let window_event = converter.convert(event, x, y);
window_event.map(|e| {
res.push(e);
});
}
res
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub struct InputEvent {
/// Event type.
pub event_type: u16,
/// Event code.
pub code: u16,
/// Event value.
pub value: u32,
}

impl From<u64> for InputEvent {
fn from(event: u64) -> Self {
let event_type = (event >> 48) as u16;
let code = ((event >> 32) & 0xFFFF) as u16;
let value = (event & 0xFFFFFFFF) as u32;
Self {
event_type,
code,
value,
}
}
}
35 changes: 34 additions & 1 deletion user/apps/sysinfo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use alloc::{boxed::Box, rc::Rc, vec, vec::Vec};
use slint::{platform::WindowEvent, SharedString};
use slint_helper::{MyPlatform, SwapBuffer};
use virt2slint::Converter;
use Mstd::io::{keyboard_or_mouse_event, VIRTGPU_XRES, VIRTGPU_YRES};
use Mstd::{
io::{keyboard_or_mouse_event, VIRTGPU_XRES, VIRTGPU_YRES},
println,
process::exit,
};

slint::include_modules!();
#[no_mangle]
Expand Down Expand Up @@ -79,10 +83,39 @@ fn checkout_event(converter: &mut Converter, x: &mut isize, y: &mut isize) -> Ve
for i in 0..event_num as usize {
let event = events[i];
// let window_event = input2event(event, x, y);
let e = InputEvent::from(event);
if e.event_type == 1 && (e.code == 1 || e.code == 273) && e.value == 0 {
println!("ESC or right-click pressed, exit");
exit(0);
}
let window_event = converter.convert(event, x, y);
window_event.map(|e| {
res.push(e);
});
}
res
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub struct InputEvent {
/// Event type.
pub event_type: u16,
/// Event code.
pub code: u16,
/// Event value.
pub value: u32,
}

impl From<u64> for InputEvent {
fn from(event: u64) -> Self {
let event_type = (event >> 48) as u16;
let code = ((event >> 32) & 0xFFFF) as u16;
let value = (event & 0xFFFFFFFF) as u32;
Self {
event_type,
code,
value,
}
}
}
35 changes: 34 additions & 1 deletion user/apps/todo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ use core::{clone::Clone, convert::Into, default::Default, iter::Iterator};
use slint::{platform::WindowEvent, FilterModel, Model, SortModel};
use slint_helper::{MyPlatform, SwapBuffer};
use virt2slint::Converter;
use Mstd::io::{keyboard_or_mouse_event, VIRTGPU_XRES, VIRTGPU_YRES};
use Mstd::{
io::{keyboard_or_mouse_event, VIRTGPU_XRES, VIRTGPU_YRES},
println,
process::exit,
};

slint::include_modules!();

Expand Down Expand Up @@ -162,10 +166,39 @@ fn checkout_event(converter: &mut Converter, x: &mut isize, y: &mut isize) -> Ve
for i in 0..event_num as usize {
let event = events[i];
// let window_event = input2event(event, x, y);
let e = InputEvent::from(event);
if e.event_type == 1 && (e.code == 1 || e.code == 273) && e.value == 0 {
println!("ESC or right-click pressed, exit");
exit(0);
}
let window_event = converter.convert(event, x, y);
window_event.map(|e| {
res.push(e);
});
}
res
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub struct InputEvent {
/// Event type.
pub event_type: u16,
/// Event code.
pub code: u16,
/// Event value.
pub value: u32,
}

impl From<u64> for InputEvent {
fn from(event: u64) -> Self {
let event_type = (event >> 48) as u16;
let code = ((event >> 32) & 0xFFFF) as u16;
let value = (event & 0xFFFFFFFF) as u32;
Self {
event_type,
code,
value,
}
}
}

0 comments on commit 5fd6ef4

Please sign in to comment.