33use std:: { cell:: RefCell , rc:: Rc , sync:: { Arc , Mutex } , time:: { Duration , Instant } , collections:: VecDeque } ;
44use evdev:: Device ;
55use linuxfb:: { double:: Buffer , Framebuffer } ;
6- use slint:: { platform:: { software_renderer:: { MinimalSoftwareWindow , PremultipliedRgbaColor , RepaintBufferType , Rgb565Pixel , TargetPixel } , Platform , WindowEvent } , PhysicalSize , Rgb8Pixel } ;
6+ use slint:: { platform:: { software_renderer:: { MinimalSoftwareWindow , PremultipliedRgbaColor , RepaintBufferType , Rgb565Pixel , TargetPixel } , EventLoopProxy , Platform , WindowEvent } , EventLoopError , PhysicalSize , PlatformError , Rgb8Pixel } ;
77
88use crate :: hardware:: EvdevMtTouchPlatform ;
99
@@ -20,8 +20,7 @@ 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 > > ,
23+ queue : Option < Queue > ,
2524}
2625
2726impl FramebufferPlatform {
@@ -58,8 +57,7 @@ impl FramebufferPlatform {
5857 stride : size. 0 as usize ,
5958 bytes_per_pixel : bytes_per_pixel as usize ,
6059 touch_device : mutex_touch_device,
61- event_queue : Arc :: new ( Mutex :: new ( VecDeque :: new ( ) ) ) ,
62- quit_requested : Arc :: new ( Mutex :: new ( false ) ) ,
60+ queue : Some ( Queue ( Default :: default ( ) , std:: thread:: current ( ) ) ) ,
6361 }
6462 }
6563}
@@ -91,71 +89,36 @@ impl TargetPixel for PremultipliedAbgrColor {
9189 }
9290}
9391
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-
12892impl Platform for FramebufferPlatform {
12993 fn create_window_adapter ( & self ) -> Result < Rc < dyn slint:: platform:: WindowAdapter > , slint:: PlatformError > {
13094 Ok ( self . window . clone ( ) )
13195 }
13296
13397 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- ) ) )
98+ self . queue
99+ . as_ref ( )
100+ . map ( |q| Box :: new ( q. clone ( ) ) as Box < dyn EventLoopProxy > )
138101 }
139102
140103 fn run_event_loop ( & self ) -> Result < ( ) , slint:: PlatformError > {
141104 let mut fb = self . fb . borrow_mut ( ) ;
142- loop {
143- // Check for quit request
144- if let Ok ( quit_flag) = self . quit_requested . lock ( ) {
145- if * quit_flag {
146- break ;
147- }
148- }
149105
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- }
106+ let queue = match self . queue . as_ref ( ) {
107+ Some ( queue) => queue. clone ( ) ,
108+ None => return Err ( PlatformError :: NoEventLoopProvider ) ,
109+ } ;
156110
111+ loop {
157112 slint:: platform:: update_timers_and_animations ( ) ;
158113
114+ let e = queue. 0 . lock ( ) . unwrap ( ) . pop_front ( ) ;
115+
116+ match e {
117+ Some ( Event :: Quit ) => break ,
118+ Some ( Event :: Event ( event) ) => event ( ) ,
119+ None => { }
120+ }
121+
159122 if let Some ( touch_device) = & self . touch_device
160123 {
161124 let now = Instant :: now ( ) ;
@@ -164,8 +127,8 @@ impl Platform for FramebufferPlatform {
164127
165128 for event in events
166129 {
167- println ! ( "Got event {:?}" , event) ;
168- println ! ( "Elapsed: {:.2?}" , elapsed) ;
130+ // println!("Got event {:?}", event);
131+ // println!("Elapsed: {:.2?}", elapsed);
169132 self . window . try_dispatch_event ( event) . unwrap ( ) ;
170133 }
171134 }
@@ -190,9 +153,37 @@ impl Platform for FramebufferPlatform {
190153 } ) ;
191154
192155 if !self . window . has_active_animations ( ) {
193- std:: thread:: sleep ( slint:: platform:: duration_until_next_timer_update ( ) . unwrap_or ( Duration :: from_millis ( 100 ) ) ) ;
156+ std:: thread:: park_timeout ( slint:: platform:: duration_until_next_timer_update ( ) . unwrap_or ( Duration :: from_millis ( 20 ) ) ) ;
194157 }
195158 }
196159 Ok ( ( ) )
197160 }
161+ }
162+
163+ enum Event {
164+ Quit ,
165+ Event ( Box < dyn FnOnce ( ) + Send > ) ,
166+ }
167+
168+ #[ derive( Clone ) ]
169+ struct Queue (
170+ std:: sync:: Arc < std:: sync:: Mutex < std:: collections:: VecDeque < Event > > > ,
171+ std:: thread:: Thread ,
172+ ) ;
173+
174+ impl EventLoopProxy for Queue {
175+ fn quit_event_loop ( & self ) -> Result < ( ) , EventLoopError > {
176+ self . 0 . lock ( ) . unwrap ( ) . push_back ( Event :: Quit ) ;
177+ self . 1 . unpark ( ) ;
178+ Ok ( ( ) )
179+ }
180+
181+ fn invoke_from_event_loop (
182+ & self ,
183+ event : Box < dyn FnOnce ( ) + Send > ,
184+ ) -> Result < ( ) , EventLoopError > {
185+ self . 0 . lock ( ) . unwrap ( ) . push_back ( Event :: Event ( event) ) ;
186+ self . 1 . unpark ( ) ;
187+ Ok ( ( ) )
188+ }
198189}
0 commit comments