Skip to content

Commit 43b9b76

Browse files
Chatgippity
1 parent 24b8972 commit 43b9b76

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

src/hardware/framebuffer_platform.rs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Based on https://github.com/nilclass/slint-framebuffer-example
22

3-
use std::{cell::{Cell, RefCell}, rc::Rc, sync::Mutex, time::{Duration, Instant}};
4-
use evdev::{Device, EventSummary};
3+
use std::{cell::RefCell, rc::Rc, sync::{Arc, Mutex}, time::{Duration, Instant}, collections::VecDeque};
4+
use evdev::Device;
55
use linuxfb::{double::Buffer, Framebuffer};
66
use slint::{platform::{software_renderer::{MinimalSoftwareWindow, PremultipliedRgbaColor, RepaintBufferType, Rgb565Pixel, TargetPixel}, Platform, WindowEvent}, PhysicalSize, Rgb8Pixel};
77

@@ -20,6 +20,8 @@ pub struct FramebufferPlatform {
2020
stride: usize,
2121
bytes_per_pixel: usize,
2222
touch_device: Option<Box<dyn TouchPlatform>>,
23+
event_queue: Arc<Mutex<VecDeque<Box<dyn FnOnce() + Send>>>>,
24+
quit_requested: Arc<Mutex<bool>>,
2325
}
2426

2527
impl FramebufferPlatform {
@@ -56,6 +58,8 @@ impl FramebufferPlatform {
5658
stride: size.0 as usize,
5759
bytes_per_pixel: bytes_per_pixel as usize,
5860
touch_device: mutex_touch_device,
61+
event_queue: Arc::new(Mutex::new(VecDeque::new())),
62+
quit_requested: Arc::new(Mutex::new(false)),
5963
}
6064
}
6165
}
@@ -87,14 +91,69 @@ impl TargetPixel for PremultipliedAbgrColor {
8791
}
8892
}
8993

94+
struct FramebufferEventLoopProxy {
95+
event_queue: Arc<Mutex<VecDeque<Box<dyn FnOnce() + Send>>>>,
96+
quit_requested: Arc<Mutex<bool>>,
97+
}
98+
99+
impl FramebufferEventLoopProxy {
100+
fn new(event_queue: Arc<Mutex<VecDeque<Box<dyn FnOnce() + Send>>>>, quit_requested: Arc<Mutex<bool>>) -> Self {
101+
Self { event_queue, quit_requested }
102+
}
103+
}
104+
105+
impl slint::platform::EventLoopProxy for FramebufferEventLoopProxy {
106+
fn quit_event_loop(&self) -> Result<(), slint::EventLoopError> {
107+
if let Ok(mut quit_flag) = self.quit_requested.lock() {
108+
*quit_flag = true;
109+
Ok(())
110+
} else {
111+
Err(slint::EventLoopError::EventLoopTerminated)
112+
}
113+
}
114+
115+
fn invoke_from_event_loop(
116+
&self,
117+
event: Box<dyn FnOnce() + Send>,
118+
) -> Result<(), slint::EventLoopError> {
119+
if let Ok(mut queue) = self.event_queue.lock() {
120+
queue.push_back(event);
121+
Ok(())
122+
} else {
123+
Err(slint::EventLoopError::EventLoopTerminated)
124+
}
125+
}
126+
}
127+
90128
impl Platform for FramebufferPlatform {
91129
fn create_window_adapter(&self) -> Result<Rc<dyn slint::platform::WindowAdapter>, slint::PlatformError> {
92130
Ok(self.window.clone())
93131
}
94132

133+
fn new_event_loop_proxy(&self) -> Option<Box<dyn slint::platform::EventLoopProxy>> {
134+
Some(Box::new(FramebufferEventLoopProxy::new(
135+
self.event_queue.clone(),
136+
self.quit_requested.clone()
137+
)))
138+
}
139+
95140
fn run_event_loop(&self) -> Result<(), slint::PlatformError> {
96141
let mut fb = self.fb.borrow_mut();
97142
loop {
143+
// Check for quit request
144+
if let Ok(quit_flag) = self.quit_requested.lock() {
145+
if *quit_flag {
146+
break;
147+
}
148+
}
149+
150+
// Process queued events from other threads
151+
if let Ok(mut queue) = self.event_queue.lock() {
152+
while let Some(event) = queue.pop_front() {
153+
event();
154+
}
155+
}
156+
98157
slint::platform::update_timers_and_animations();
99158

100159
if let Some(touch_device) = &self.touch_device
@@ -134,5 +193,6 @@ impl Platform for FramebufferPlatform {
134193
std::thread::sleep(slint::platform::duration_until_next_timer_update().unwrap_or(Duration::from_millis(100)));
135194
}
136195
}
196+
Ok(())
137197
}
138198
}

src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
4343
let ui_weak_3 = ui.as_weak();
4444

4545
tokio::spawn(async move {
46-
tokio::time::sleep(Duration::from_secs(1)).await;
4746
moonraker_connection.connection_loop().await;
4847
});
4948

0 commit comments

Comments
 (0)