Skip to content

Commit c9b160e

Browse files
committed
Refactor device handling, update shortcut structure, and enhance error reporting
1 parent dbbf9c8 commit c9b160e

File tree

19 files changed

+327
-102
lines changed

19 files changed

+327
-102
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
/.embuild
33
/target
44
Cargo.lock
5-
/Garbage
5+
/Garbage
6+
7+
*.img

Drive

-12.9 MB
Binary file not shown.

Examples/Native.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn main() {
3535

3636
// - Initialize the file system
3737
// Create a memory device
38-
let Drive = Create_device!(Drivers::Native::File_drive_device_type::New(&"./Drive"));
38+
let Drive = Create_device!(Drivers::Native::File_drive_device_type::New(&"./Drive.img"));
3939
// Mount the file system
4040
let File_system = match LittleFS::File_system_type::New(Drive.clone(), 256) {
4141
Ok(File_system) => File_system,
@@ -97,10 +97,20 @@ fn main() {
9797
Virtual_file_system,
9898
Task,
9999
&[
100-
Graphical_shell::Shell_executable_type,
101-
Command_line_shell::Shell_executable_type,
102-
Terminal::Terminal_executable_type,
103-
WASM::WASM_device_type
100+
(
101+
&"/Binaries/Graphical_shell",
102+
Graphical_shell::Shell_executable_type
103+
),
104+
(
105+
&"/Binaries/Command_line_shell",
106+
Command_line_shell::Shell_executable_type
107+
),
108+
(
109+
&"/Binaries/Terminal",
110+
Terminal::Terminal_executable_type::New(Virtual_file_system::Get_instance(), Task)
111+
.unwrap()
112+
),
113+
(&"/Binaries/WASM", WASM::WASM_device_type)
104114
]
105115
)
106116
.unwrap();

Modules/ABI/src/File_system.rs

-6
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,7 @@ pub unsafe extern "C" fn Xila_file_system_get_statistics(
7676
File: Unique_file_identifier_type,
7777
Statistics: *mut Statistics_type,
7878
) -> u32 {
79-
println!("Getting file statistics : {:?}", File);
80-
8179
Into_u32(move || {
82-
println!("Getting file statistics : {:?}", File);
83-
8480
let Task_identifier = Get_task_manager_instance()
8581
.Get_current_task_identifier()
8682
.map_err(|_| Error_type::Failed_to_get_task_informations)?;
@@ -92,8 +88,6 @@ pub unsafe extern "C" fn Xila_file_system_get_statistics(
9288
.Get_statistics(File, Task_identifier)
9389
.expect("Failed to get file statistics.");
9490

95-
println!("Statistics : {:?}", *Statistics);
96-
9791
Ok(())
9892
})
9993
}

Modules/Executable/src/Device_trait.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ macro_rules! Mount_static_executables {
1616

1717
|| -> Result<(), File_system::Error_type>
1818
{
19-
use File_system::Create_device;
19+
use File_system::{Create_device, Permissions_type};
2020

2121
$(
2222
$Virtual_file_system.Mount_static_device($Task_identifier, $Path, Create_device!($Device))?;
23-
$Virtual_file_system.Set_permissions($Task_identifier, $Path, Permissions_type::Executable )?;
23+
$Virtual_file_system.Set_permissions($Path, Permissions_type::Executable )?;
2424
)*
2525

26-
27-
2826
Ok(())
2927
}()
3028
};

Modules/Executables/Shell/Graphical/src/Desk.rs

+141-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::BTreeMap, ffi::CString};
1+
use std::{collections::BTreeMap, ffi::CString, os::raw::c_void};
22

33
use crate::{
44
Error::{Error_type, Result_type},
@@ -11,6 +11,9 @@ use File_system::{Mode_type, Type_type};
1111
use Graphics::{Color_type, Event_code_type, Point_type, Window_type, LVGL};
1212
use Virtual_file_system::Directory_type;
1313

14+
pub const Windows_parent_child_changed: Graphics::Event_code_type =
15+
Graphics::Event_code_type::Custom_2;
16+
1417
pub struct Desk_type {
1518
Window: Window_type,
1619
Tile_view: *mut LVGL::lv_obj_t,
@@ -21,6 +24,30 @@ pub struct Desk_type {
2124
Shortcuts: BTreeMap<*mut LVGL::lv_obj_t, String>,
2225
}
2326

27+
unsafe extern "C" fn Event_handler(Event: *mut LVGL::lv_event_t) {
28+
let Code = Event_code_type::From_LVGL_code(LVGL::lv_event_get_code(Event));
29+
30+
if Code == Event_code_type::Child_created || Code == Event_code_type::Child_deleted {
31+
let Target = LVGL::lv_event_get_target(Event) as *mut LVGL::lv_obj_t;
32+
let Target_parent = LVGL::lv_obj_get_parent(Target);
33+
34+
let Current_target = LVGL::lv_event_get_current_target(Event) as *mut LVGL::lv_obj_t;
35+
36+
// If the event is not for the current target, ignore it (not the parent window)
37+
if Target_parent != Current_target {
38+
return;
39+
}
40+
41+
let Desk = LVGL::lv_event_get_user_data(Event) as *mut LVGL::lv_obj_t;
42+
43+
LVGL::lv_obj_send_event(
44+
Desk,
45+
Windows_parent_child_changed as u32,
46+
Target as *mut c_void,
47+
);
48+
}
49+
}
50+
2451
impl Drop for Desk_type {
2552
fn drop(&mut self) {
2653
unsafe {
@@ -45,15 +72,24 @@ impl Desk_type {
4572
unsafe { LVGL::lv_obj_has_flag(self.Dock, LVGL::lv_obj_flag_t_LV_OBJ_FLAG_HIDDEN) }
4673
}
4774

48-
pub fn New() -> Result_type<Self> {
75+
pub fn New(Windows_parent: *mut LVGL::lv_obj_t) -> Result_type<Self> {
4976
// - Lock the graphics
5077
let _Lock = Graphics::Get_instance().Lock()?; // Lock the graphics
5178

5279
// - Create a window
53-
let Window = Graphics::Get_instance().Create_window()?;
80+
let mut Window = Graphics::Get_instance().Create_window()?;
81+
82+
Window.Set_icon("De", Color_type::Black);
5483

5584
unsafe {
5685
LVGL::lv_obj_set_style_pad_all(Window.Get_object(), 0, LVGL::LV_STATE_DEFAULT);
86+
87+
LVGL::lv_obj_add_event_cb(
88+
Windows_parent,
89+
Some(Event_handler),
90+
Event_code_type::All as u32,
91+
Window.Get_object() as *mut core::ffi::c_void,
92+
);
5793
}
5894

5995
// - Create the logo
@@ -144,30 +180,26 @@ impl Desk_type {
144180
// - Create the main button
145181
let Main_button = unsafe { Create_logo(Dock, 1, Color_type::White)? };
146182

147-
// Create some fake icons
148-
// for i in 0..5 {
149-
// unsafe {
150-
// Create_icon(Dock, &format!("Icon {}", i), Self::Dock_icon_size)?;
151-
// }
152-
// }
153-
154183
let Shortcuts = BTreeMap::new();
155184

156-
Ok(Self {
185+
let Desk = Self {
157186
Window,
187+
Tile_view,
158188
Desk_tile,
159189
Drawer_tile,
160-
Tile_view,
161190
Dock,
162191
Main_button,
163192
Shortcuts,
164-
})
193+
};
194+
195+
Ok(Desk)
165196
}
166197

167198
unsafe fn Create_drawer_shortcut(
168199
&mut self,
169200
Entry_name: &str,
170201
Name: &str,
202+
Icon_color: Color_type,
171203
Icon_string: &str,
172204
Drawer: *mut LVGL::lv_obj_t,
173205
) -> Result_type<()> {
@@ -186,7 +218,7 @@ impl Desk_type {
186218
LVGL::lv_flex_align_t_LV_FLEX_ALIGN_CENTER,
187219
);
188220

189-
let Icon = Create_icon(Container, Name, Icon_string, Self::Drawer_icon_size)?;
221+
let Icon = Create_icon(Container, Icon_color, Icon_string, Self::Drawer_icon_size)?;
190222

191223
let Label = LVGL::lv_label_create(Container);
192224

@@ -217,8 +249,6 @@ impl Desk_type {
217249
.map_err(Error_type::Failed_to_read_shortcut_directory)?;
218250

219251
for Shortcut_entry in Shortcuts_directory {
220-
println!("Shortcut: {}", Shortcut_entry.Get_name());
221-
222252
if Shortcut_entry.Get_type() != Type_type::File {
223253
continue;
224254
}
@@ -232,13 +262,13 @@ impl Desk_type {
232262
self.Create_drawer_shortcut(
233263
Shortcut_entry.Get_name(),
234264
Shortcut.Get_name(),
265+
Shortcut.Get_icon_color(),
235266
Shortcut.Get_icon_string(),
236267
Drawer,
237268
)?;
238269
}
239-
Err(Error) => {
270+
Err(_) => {
240271
// ? : Log error ?
241-
println!("Failed to read shortcut. {}", Error);
242272
continue;
243273
}
244274
}
@@ -284,12 +314,81 @@ impl Desk_type {
284314
Ok(())
285315
}
286316

317+
fn Refresh_dock(&self) -> Result_type<()> {
318+
let Dock_child_count = unsafe { LVGL::lv_obj_get_child_count(self.Dock) };
319+
320+
let Graphics_manager = Graphics::Get_instance();
321+
322+
let Window_count = Graphics_manager.Get_window_count()?;
323+
324+
// Remove the icons of the windows that are not in the dock
325+
for i in 1..Dock_child_count {
326+
let Icon = unsafe { LVGL::lv_obj_get_child(self.Dock, i as i32) };
327+
328+
if Icon == self.Main_button {
329+
continue;
330+
}
331+
332+
let Dock_window_identifier = unsafe { LVGL::lv_obj_get_user_data(Icon) as usize };
333+
334+
let Found = (1..Window_count).find(|&i| {
335+
if let Ok(Window_identifier) = Graphics_manager.Get_window_identifier(i) {
336+
Window_identifier == Dock_window_identifier
337+
} else {
338+
false
339+
}
340+
});
341+
342+
if Found.is_none() {
343+
unsafe {
344+
LVGL::lv_obj_delete(Icon);
345+
}
346+
}
347+
}
348+
349+
// Add the new icons
350+
for i in 1..Window_count {
351+
let Window_identifier =
352+
if let Ok(Window_identifier) = Graphics_manager.Get_window_identifier(i) {
353+
Window_identifier
354+
} else {
355+
continue;
356+
};
357+
358+
// Find the index of the window in the dock
359+
let Found = (1..Dock_child_count).find(|&i| {
360+
let Dock_window_identifier = unsafe {
361+
let Icon = LVGL::lv_obj_get_child(self.Dock, i as i32);
362+
363+
LVGL::lv_obj_get_user_data(Icon) as usize
364+
};
365+
366+
Dock_window_identifier == Window_identifier
367+
});
368+
369+
// If the window is not in the dock, add it
370+
if Found.is_none() {
371+
let (Icon_string, Icon_color) = Graphics_manager.Get_window_icon(i)?;
372+
373+
let Window_identifier = Graphics_manager.Get_window_identifier(i)?;
374+
375+
unsafe {
376+
let Icon =
377+
Create_icon(self.Dock, Icon_color, &Icon_string, Self::Dock_icon_size)?;
378+
379+
LVGL::lv_obj_set_user_data(Icon, Window_identifier as *mut c_void);
380+
}
381+
}
382+
}
383+
384+
Ok(())
385+
}
386+
287387
pub fn Event_handler(&mut self) {
388+
let _Lock = Graphics::Get_instance().Lock().unwrap();
288389
while let Some(Event) = self.Window.Pop_event() {
289390
match Event.Get_code() {
290391
Self::Home_event => unsafe {
291-
let _Lock = Graphics::Get_instance().Lock().unwrap();
292-
293392
LVGL::lv_tileview_set_tile_by_index(
294393
self.Tile_view,
295394
0,
@@ -299,7 +398,6 @@ impl Desk_type {
299398
},
300399
Event_code_type::Value_changed => {
301400
if Event.Get_target() == self.Tile_view {
302-
let _Lock = Graphics::Get_instance().Lock().unwrap();
303401
unsafe {
304402
if LVGL::lv_tileview_get_tile_active(self.Tile_view) == self.Desk_tile {
305403
LVGL::lv_obj_clean(self.Drawer_tile);
@@ -310,16 +408,24 @@ impl Desk_type {
310408
}
311409
}
312410
Event_code_type::Clicked => {
313-
let _Lock = Graphics::Get_instance().Lock().unwrap();
314-
411+
// If the target is a shortcut, execute the shortcut
315412
if let Some(Shortcut_name) = self.Shortcuts.get(&Event.Get_target()) {
316413
if let Err(Error) = self.Execute_shortcut(Shortcut_name) {
317-
println!("Failed to execute shortcut. {}", Error);
414+
// ? : Log error ?
415+
todo!("Failed to execute shortcut {}", Error.to_string());
318416
}
319417
}
418+
// If the target is a dock icon, move the window to the foreground
419+
else if unsafe { LVGL::lv_obj_get_parent(Event.Get_target()) == self.Dock } {
420+
let Window_identifier =
421+
unsafe { LVGL::lv_obj_get_user_data(Event.Get_target()) as usize };
422+
423+
Graphics::Get_instance()
424+
.Maximize_window(Window_identifier)
425+
.unwrap();
426+
}
320427
}
321428
Event_code_type::Pressed => {
322-
let _Lock = Graphics::Get_instance().Lock().unwrap();
323429
if Event.Get_target() == self.Main_button
324430
|| unsafe {
325431
LVGL::lv_obj_get_parent(Event.Get_target()) == self.Main_button
@@ -362,6 +468,16 @@ impl Desk_type {
362468
}
363469
}
364470
}
471+
Windows_parent_child_changed => {
472+
// Ignore consecutive windows parent child changed events
473+
if let Some(Peeked_event) = self.Window.Peek_event() {
474+
if Peeked_event.Get_code() == Windows_parent_child_changed {
475+
continue;
476+
}
477+
}
478+
479+
self.Refresh_dock().unwrap();
480+
}
365481
_ => {}
366482
}
367483
}

Modules/Executables/Shell/Graphical/src/Error.rs

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern crate alloc;
1010
pub enum Error_type {
1111
Graphics(Graphics::Error_type) = 1,
1212
Failed_to_create_object,
13+
Failed_to_get_child,
1314
Failed_to_set_environment_variable(Task::Error_type),
1415
Invalid_UTF_8(core::str::Utf8Error),
1516
Authentication_failed(Authentication::Error_type),
@@ -53,6 +54,9 @@ impl Display for Error_type {
5354
Self::Failed_to_create_object => {
5455
write!(Formatter, "Failed to create object")
5556
}
57+
Self::Failed_to_get_child => {
58+
write!(Formatter, "Failed to get child")
59+
}
5660
Self::Failed_to_set_environment_variable(Error) => {
5761
write!(Formatter, "Failed to set environment variable: {}", Error)
5862
}

0 commit comments

Comments
 (0)