File size: 11,318 Bytes
dcbeeb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#![no_std]
#![no_main]
#![feature(abi_x86_interrupt)]

extern crate alloc;

pub mod arch;
pub mod memory;
pub mod drivers;
pub mod graphics;
pub mod fs;
pub mod net;
pub mod window_manager;
pub mod desktop;
pub mod apps;

use core::panic::PanicInfo;
use bootloader_api::{entry_point, BootInfo, BootloaderConfig};

use crate::apps::AppType;
use crate::desktop::Desktop;
use crate::drivers::framebuffer::{self, FRAMEBUFFER};
use crate::drivers::ps2_mouse;
use crate::graphics::surface::Surface;
use crate::window_manager::compositor::Compositor;

pub static BOOTLOADER_CONFIG: BootloaderConfig = {
    let mut config = BootloaderConfig::new_default();
    config.mappings.physical_memory = Some(bootloader_api::config::Mapping::Dynamic);
    config.kernel_stack_size = 1024 * 1024; // 1 MiB stack
    config
};

entry_point!(kernel_main, config = &BOOTLOADER_CONFIG);

fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
    // 1. Initialize Serial Port Logger (COM1 0x3F8)
    drivers::serial::init();
    println!("==================================================");
    println!("       AeroOS Desktop Kernel v1.0.0 (x86_64)      ");
    println!("  Windows 11 & macOS Hybrid Graphical Interface   ");
    println!("==================================================");

    // 2. Initialize GDT, IDT, PIC, and CPU Interrupts
    arch::init();
    let t0 = arch::boot_timer::read_tsc();
    println!("[+] Hardware Interrupts & GDT/IDT initialized.");
    drivers::speaker::play_startup_melody();
    println!("[+] PC Speaker audio initialized (Chimes played).");

    // 3. Initialize Memory Management & 16 MiB Heap Allocator
    let phys_mem_offset = boot_info
        .physical_memory_offset
        .into_option()
        .expect("Bootloader did not map physical memory");
    memory::init(phys_mem_offset, &boot_info.memory_regions);
    println!("[+] 4-Level Paging & 16 MiB Heap Allocator initialized.");

    // 4. Initialize Virtual File System (RamDisk VFS)
    fs::init();
    println!("[+] Virtual File System initialized with default documents.");

    // 5. Initialize Keyboard Subsystem
    drivers::ps2_keyboard::init();
    println!("[+] PS/2 Keyboard driver active.");

    // 6. Initialize Framebuffer & Surface
    let fb = boot_info.framebuffer.as_mut().expect("No framebuffer available");
    framebuffer::init(fb);
    let (screen_w, screen_h) = {
        let guard = FRAMEBUFFER.lock();
        let cfg = guard.as_ref().unwrap();
        (cfg.width, cfg.height)
    };
    println!("[+] UEFI GOP Framebuffer: {}x{}", screen_w, screen_h);

    // 7. Initialize Mouse Driver
    drivers::ps2_mouse::init(screen_w as i32, screen_h as i32);
    println!("[+] PS/2 Mouse driver initialized.");

    // 8. Initialize CMOS Real-Time Clock
    let rtc_time = drivers::rtc::get_datetime();
    println!("[+] Hardware CMOS RTC Initialized: {} ({})", rtc_time.format_full(), rtc_time.format_time_12());
    arch::boot_timer::record_stage("1. Base Kernel & Core Subsystems", t0);

    // Component 1: Network connectivity (WiFi & LAN)
    let t_net = arch::boot_timer::read_tsc();
    drivers::virtio_net::init();
    net::init();
    net::wifi::init();
    println!("[+] Network connectivity active: VirtIO-Net (LAN) + 802.11ac (WiFi)");
    arch::boot_timer::record_stage("2. Network Connectivity (WiFi & LAN)", t_net);

    // Component 2: Laptop Hardware Compatibility (ACPI, Backlight, Thermal, Power Profiles)
    let t_laptop = arch::boot_timer::read_tsc();
    drivers::laptop::init();
    println!("[+] Laptop Hardware Compatibility active (ACPI battery, backlight, power profiles, sensors)");
    arch::boot_timer::record_stage("3. Laptop Hardware Compatibility", t_laptop);

    // Component 3: In-built package manager: APTITUDE alias APT (For debian)
    let t_apt = arch::boot_timer::read_tsc();
    apps::apt::init();
    println!("[+] Debian APT/Aptitude package manager engine initialized.");
    arch::boot_timer::record_stage("4. Package Manager (APT / Aptitude)", t_apt);

    // Component 4: In-built Store: snapstore
    let t_snap = arch::boot_timer::read_tsc();
    apps::snapstore::init();
    println!("[+] Canonical Snap Store subsystem & snapd daemon initialized.");
    arch::boot_timer::record_stage("5. In-built Store (Snap Store)", t_snap);

    // Component 5: Desktop Compositor & GUI Shell
    let t_gui = arch::boot_timer::read_tsc();
    let mut surface = Surface::new(screen_w, screen_h);
    let mut desktop_surface = Surface::new(screen_w, screen_h);
    let mut compositor = Compositor::new();
    let mut desktop = Desktop::new();

    // Open initial desktop windows: Snap Store (right side) and Terminal (left side)
    compositor.open_window(
        "Snap Store - Software Center",
        405,
        25,
        380,
        515,
        AppType::SnapStore,
    );
    compositor.open_window(
        "AeroShell - Terminal",
        15,
        25,
        380,
        515,
        AppType::Terminal,
    );
    arch::boot_timer::record_stage("6. Desktop Compositor (Fluent UI)", t_gui);
    arch::boot_timer::print_boot_summary();

    println!("[+] Windows 11 Fluent Desktop Compositor starting interactive event loop...");

    let mut last_left_button = false;
    let mut last_mouse_x = screen_w as i32 / 2;
    let mut last_mouse_y = screen_h as i32 / 2;
    let mut last_ticks = 0;

    // Initial desktop render
    desktop.render(&mut desktop_surface, &mut compositor);
    surface.copy_from(&desktop_surface);
    surface.draw_mouse_cursor(last_mouse_x, last_mouse_y);
    if let Some(ref mut fb_cfg) = *FRAMEBUFFER.lock() {
        surface.blit_to_framebuffer(fb_cfg);
    }

    // Enable CPU Hardware Interrupts now that all subsystems and initial desktop are ready
    x86_64::instructions::interrupts::enable();
    println!("[+] Hardware Interrupts enabled. AeroOS Desktop interactive.");

    // Main Desktop Compositor Loop
    loop {
        // A. Poll Network Stack (handles packet RX/TX, ARP replies, ICMP ping)
        net::poll();

        let mut full_dirty = false;
        let mut active_dirty = false;
        let mut mouse_dirty = false;

        let win_count_before = compositor.windows.len();

        // B. Process Keyboard Events (Fast Path)
        while let Some(key) = drivers::ps2_keyboard::pop_key() {
            compositor.handle_key(key);
            active_dirty = true;
        }

        // If a window was closed (e.g. Escape key), trigger full desktop render
        if compositor.windows.len() != win_count_before {
            full_dirty = true;
        }

        // Process pending GUI app launch requests
        if let Some(app) = apps::pop_app_launch() {
            desktop.launch_app(app, &mut compositor);
            full_dirty = true;
        }

        // C. Process Mouse Events
        let mouse_state = ps2_mouse::get_mouse_state();
        let mouse_x = mouse_state.x;
        let mouse_y = mouse_state.y;

        if mouse_x != last_mouse_x || mouse_y != last_mouse_y {
            mouse_dirty = true;
        }

        // Process Scroll Wheel / Touchpad Scrolling (Fast Path)
        if mouse_state.scroll_delta != 0 {
            if compositor.handle_scroll(mouse_x, mouse_y, mouse_state.scroll_delta) {
                active_dirty = true;
            }
        }

        // Mouse Down (Edge trigger)
        if mouse_state.left_button && !last_left_button {
            full_dirty = true;
            desktop.handle_click(
                mouse_x,
                mouse_y,
                &mut compositor,
                screen_w,
                screen_h,
            );
        } else if !mouse_state.left_button && last_left_button {
            // Mouse Up
            full_dirty = true;
            compositor.handle_mouse_up();
        }

        // Mouse Drag / Movement
        if mouse_state.left_button && compositor.dragging.is_some() && mouse_dirty {
            compositor.handle_mouse_move(mouse_x, mouse_y);
            full_dirty = true;
        }

        last_left_button = mouse_state.left_button;

        // Check system clock / timer ticks for UI updates (e.g. clock in taskbar)
        let ticks = arch::idt::SYSTEM_TICKS.load(core::sync::atomic::Ordering::Relaxed);
        let mut clock_dirty = false;
        if ticks / 100 != last_ticks / 100 {
            last_ticks = ticks;
            clock_dirty = true;
        }

        if full_dirty {
            desktop.render(&mut desktop_surface, &mut compositor);
            surface.copy_from(&desktop_surface);
            surface.draw_mouse_cursor(mouse_x, mouse_y);

            if let Some(ref mut fb_cfg) = *FRAMEBUFFER.lock() {
                surface.blit_to_framebuffer(fb_cfg);
            }
            last_mouse_x = mouse_x;
            last_mouse_y = mouse_y;
        } else if active_dirty {
            // High-Performance Fast Path for Typing & Scrolling (Zero Lag):
            // Redraw only the active window's client area and blit only that rectangle to VRAM!
            if let Some((cx, cy, cw, ch)) = compositor.render_active_client(&mut desktop_surface) {
                surface.copy_rect_from(&desktop_surface, cx, cy, cw, ch);
                if mouse_x >= cx && mouse_x < cx + cw as i32 && mouse_y >= cy && mouse_y < cy + ch as i32 {
                    surface.draw_mouse_cursor(mouse_x, mouse_y);
                }
                if let Some(ref mut fb_cfg) = *FRAMEBUFFER.lock() {
                    surface.blit_rect_to_framebuffer(fb_cfg, cx, cy, cw, ch);
                }
            }
        } else if clock_dirty {
            // Fast Path: Only redraw and blit the taskbar tray when the second ticks
            desktop::taskbar::render_taskbar(&mut desktop_surface, &mut compositor);
            let tb_y = (screen_h - desktop::taskbar::TASKBAR_HEIGHT) as i32;
            surface.copy_rect_from(&desktop_surface, 0, tb_y, screen_w, desktop::taskbar::TASKBAR_HEIGHT);
            if mouse_y >= tb_y {
                surface.draw_mouse_cursor(mouse_x, mouse_y);
            }
            if let Some(ref mut fb_cfg) = *FRAMEBUFFER.lock() {
                surface.blit_rect_to_framebuffer(fb_cfg, 0, tb_y, screen_w, desktop::taskbar::TASKBAR_HEIGHT);
            }
        } else if mouse_dirty {
            // Instant Sub-Millisecond Cursor Update:
            // Only restore old cursor bounding box (16x22) and draw new cursor bounding box!
            let cw = 16;
            let ch = 22;

            surface.copy_rect_from(&desktop_surface, last_mouse_x, last_mouse_y, cw, ch);
            surface.copy_rect_from(&desktop_surface, mouse_x, mouse_y, cw, ch);
            surface.draw_mouse_cursor(mouse_x, mouse_y);

            if let Some(ref mut fb_cfg) = *FRAMEBUFFER.lock() {
                surface.blit_rect_to_framebuffer(fb_cfg, last_mouse_x, last_mouse_y, cw, ch);
                surface.blit_rect_to_framebuffer(fb_cfg, mouse_x, mouse_y, cw, ch);
            }
            last_mouse_x = mouse_x;
            last_mouse_y = mouse_y;
        } else {
            // Halt CPU until next hardware interrupt (Timer, Keyboard, Mouse)
            x86_64::instructions::hlt();
        }
    }
}

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
    println!("\n[!] KERNEL PANIC: {}", info);
    loop {
        x86_64::instructions::hlt();
    }
}