From ca5ca3c3c683ad4be05225d7405ca2b4145810c5 Mon Sep 17 00:00:00 2001 From: Sweetbread Date: Mon, 11 Mar 2024 23:50:22 +0300 Subject: [PATCH] feat: Add event filters (#5) --- examples/hwa.rs | 4 +++- src/func_constants.inc | 1 + src/modules/threads.rs | 27 +++++++++++++++------------ src/sys.rs | 4 ++++ src/syscalls.S | 8 ++++++++ 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/examples/hwa.rs b/examples/hwa.rs index e9fe2d6..c6f8fbc 100644 --- a/examples/hwa.rs +++ b/examples/hwa.rs @@ -94,7 +94,9 @@ fn button_handler(c: &mut usize) { fn kol_main() { let mut c = 0; - while let Some(ev) = fetch_event() { + while let Some(ev) = + fetch_event((Event::Redraw as u32) | (Event::KeyPress as u32) | (Event::BtnPress as u32)) + { match ev { Event::Redraw => draw_window(c), Event::KeyPress => drop(fetch_key()), diff --git a/src/func_constants.inc b/src/func_constants.inc index 771dcef..cda41b6 100644 --- a/src/func_constants.inc +++ b/src/func_constants.inc @@ -12,6 +12,7 @@ SF_REDRAW = 12 SF_GET_BUTTON = 17 SF_SYSTEM_GET = 26 SSF_SYS_LANG = 5 +SF_SET_EVENTS_MASK = 40 SF_BOARD = 63 SSF_DEBUG_WRITE = 1 SF_SYS_MISC = 68 diff --git a/src/modules/threads.rs b/src/modules/threads.rs index 02d4547..584f901 100644 --- a/src/modules/threads.rs +++ b/src/modules/threads.rs @@ -1,4 +1,4 @@ -use crate::sys; +use crate::sys::{self, set_event_mask}; pub fn exit() -> ! { unsafe { sys::exit() } @@ -6,18 +6,19 @@ pub fn exit() -> ! { #[non_exhaustive] pub enum Event { - Redraw, - KeyPress, - BtnPress, - BgRedraw, - Mouse, - IPC, - Network, - Debug, + Redraw = 1 << 0, + KeyPress = 1 << 1, + BtnPress = 1 << 2, + BgRedraw = 1 << 4, + Mouse = 1 << 5, + IPC = 1 << 6, + Network = 1 << 7, + Debug = 1 << 8, } -pub fn fetch_event() -> Option { - match unsafe { sys::wait_event() } { +pub fn fetch_event(flags: u32) -> Option { + let old_mask = unsafe { sys::set_event_mask(flags as u32) }; + let e = match unsafe { sys::wait_event() } { 1 => Some(Event::Redraw), 2 => Some(Event::KeyPress), 3 => Some(Event::BtnPress), @@ -27,5 +28,7 @@ pub fn fetch_event() -> Option { 8 => Some(Event::Network), 9 => Some(Event::Debug), _ => None, - } + }; + unsafe { set_event_mask(old_mask) }; + e } diff --git a/src/sys.rs b/src/sys.rs index 86a3209..c0bd7e1 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -40,6 +40,10 @@ extern "C" { #[link_name = "_get_lang"] pub fn get_lang() -> u32; + // 40 + #[link_name = "_set_event_mask"] + pub fn set_event_mask(mask: u32) -> u32; + // 63.1 #[link_name = "_debug_write"] pub fn _debug_write(cl: u8); diff --git a/src/syscalls.S b/src/syscalls.S index 2dcb106..b314bf1 100644 --- a/src/syscalls.S +++ b/src/syscalls.S @@ -18,6 +18,7 @@ section '.text' public _free public _get_lang public _load_dll + public _set_event_mask _exit: mov eax, SF_TERMINATE_PROCESS @@ -126,3 +127,10 @@ _load_dll: mov ecx, [esp + 4 * 1] int 0x40 ret + +_set_event_mask: + mov eax, SF_SET_EVENTS_MASK + mov ebx, [esp + 4 * 1] + int 0x40 + ret +