1extern crate alloc;
7use alloc::sync::Weak;
8use alloc::vec::Vec;
9use core::any::Any;
10use spin::Mutex;
11
12pub trait DeviceEvent: Send + Sync {
16 fn event_type(&self) -> &'static str;
17 fn as_any(&self) -> &dyn Any;
18}
19
20pub trait DeviceEventListener: Send + Sync {
24 fn on_device_event(&self, event: &dyn DeviceEvent);
25 fn interested_in(&self, event_type: &str) -> bool;
26}
27
28pub trait EventCapableDevice {
32 fn register_event_listener(&self, listener: Weak<dyn DeviceEventListener>);
33 fn unregister_event_listener(&self, listener_id: &str);
34 fn emit_event(&self, event: &dyn DeviceEvent);
35}
36
37pub struct DeviceEventEmitter {
41 listeners: Mutex<Vec<Weak<dyn DeviceEventListener>>>,
42}
43
44impl DeviceEventEmitter {
45 pub fn new() -> Self {
46 Self {
47 listeners: Mutex::new(Vec::new()),
48 }
49 }
50
51 pub fn register_listener(&self, listener: Weak<dyn DeviceEventListener>) {
52 let mut listeners = self.listeners.lock();
53 listeners.push(listener);
54 }
55
56 pub fn emit(&self, event: &dyn DeviceEvent) {
57 let mut listeners = self.listeners.lock();
58
59 listeners.retain(|weak_listener| {
61 if let Some(listener) = weak_listener.upgrade() {
62 if listener.interested_in(event.event_type()) {
63 listener.on_device_event(event);
64 }
65 true } else {
67 crate::early_println!(
68 "Removing dead listener for event type: {}",
69 event.event_type()
70 );
71 false }
73 });
74 }
75}
76
77#[derive(Debug)]
81pub struct InputEvent {
82 pub data: u8,
83}
84
85impl DeviceEvent for InputEvent {
86 fn event_type(&self) -> &'static str {
87 "input"
88 }
89
90 fn as_any(&self) -> &dyn Any {
91 self
92 }
93}
94
95#[derive(Debug)]
99pub struct OutputCompleteEvent;
100
101impl DeviceEvent for OutputCompleteEvent {
102 fn event_type(&self) -> &'static str {
103 "output_complete"
104 }
105
106 fn as_any(&self) -> &dyn Any {
107 self
108 }
109}
110
111#[derive(Debug)]
115pub struct ErrorEvent {
116 pub error_code: u32,
117}
118
119impl DeviceEvent for ErrorEvent {
120 fn event_type(&self) -> &'static str {
121 "error"
122 }
123
124 fn as_any(&self) -> &dyn Any {
125 self
126 }
127}
128
129pub trait InterruptCapableDevice: Send + Sync {
133 fn handle_interrupt(&self) -> crate::interrupt::InterruptResult<()>;
134 fn interrupt_id(&self) -> Option<crate::interrupt::InterruptId>;
135}