From 70cd296d198172a7f392d874a1886abcdf217614 Mon Sep 17 00:00:00 2001 From: boreddevnl Date: Mon, 23 Mar 2026 17:26:41 +0100 Subject: [PATCH] BFIX: Fix gpf's in .elf applications --- src/sys/process.c | 2 ++ src/sys/syscall.c | 22 +++++++++++----------- src/wm/wm.c | 46 +++++++++++++++++++++++++++++----------------- 3 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/sys/process.c b/src/sys/process.c index 7cad188..0512b9c 100644 --- a/src/sys/process.c +++ b/src/sys/process.c @@ -589,6 +589,8 @@ uint64_t process_terminate_current(void) { // Mark slot as free to_delete->pid = 0xFFFFFFFF; to_delete->cpu_affinity = 0xFFFFFFFF; + to_delete->ui_window = NULL; + to_delete->is_terminal_proc = false; // 4. Load context for the NEXT process if (current_process[my_cpu]->is_user && current_process[my_cpu]->kernel_stack) { diff --git a/src/sys/syscall.c b/src/sys/syscall.c index fe200ad..31a00bb 100644 --- a/src/sys/syscall.c +++ b/src/sys/syscall.c @@ -40,49 +40,49 @@ void syscall_init(void) { } static void user_window_close(Window *win) { - process_t *proc = (process_t *)win->data; + process_t *proc = process_get_by_ui_window(win); if (!proc) return; gui_event_t ev = { .type = GUI_EVENT_CLOSE }; process_push_gui_event(proc, &ev); } static void user_window_paint(Window *win) { - process_t *proc = (process_t *)win->data; + process_t *proc = process_get_by_ui_window(win); if (!proc) return; gui_event_t ev = { .type = GUI_EVENT_PAINT }; process_push_gui_event(proc, &ev); } static void user_window_click(Window *win, int x, int y) { - process_t *proc = (process_t *)win->data; + process_t *proc = process_get_by_ui_window(win); if (!proc) return; gui_event_t ev = { .type = GUI_EVENT_CLICK, .arg1 = x, .arg2 = y }; process_push_gui_event(proc, &ev); } static void user_window_right_click(Window *win, int x, int y) { - process_t *proc = (process_t *)win->data; + process_t *proc = process_get_by_ui_window(win); if (!proc) return; gui_event_t ev = { .type = GUI_EVENT_RIGHT_CLICK, .arg1 = x, .arg2 = y }; process_push_gui_event(proc, &ev); } static void user_window_mouse_down(Window *win, int x, int y) { - process_t *proc = (process_t *)win->data; + process_t *proc = process_get_by_ui_window(win); if (!proc) return; gui_event_t ev = { .type = GUI_EVENT_MOUSE_DOWN, .arg1 = x, .arg2 = y }; process_push_gui_event(proc, &ev); } static void user_window_mouse_up(Window *win, int x, int y) { - process_t *proc = (process_t *)win->data; + process_t *proc = process_get_by_ui_window(win); if (!proc) return; gui_event_t ev = { .type = GUI_EVENT_MOUSE_UP, .arg1 = x, .arg2 = y }; process_push_gui_event(proc, &ev); } static void user_window_mouse_move(Window *win, int x, int y, uint8_t buttons) { - process_t *proc = (process_t *)win->data; + process_t *proc = process_get_by_ui_window(win); if (!proc) return; gui_event_t ev = { .type = GUI_EVENT_MOUSE_MOVE, .arg1 = x, .arg2 = y, .arg3 = buttons }; process_push_gui_event(proc, &ev); @@ -90,22 +90,22 @@ static void user_window_mouse_move(Window *win, int x, int y, uint8_t buttons) { // Helper function for WM to send mouse events void syscall_send_mouse_move_event(Window *win, int x, int y, uint8_t buttons) { - if (!win || !win->data) return; + if (!win) return; user_window_mouse_move(win, x, y, buttons); } void syscall_send_mouse_down_event(Window *win, int x, int y) { - if (!win || !win->data) return; + if (!win) return; user_window_mouse_down(win, x, y); } void syscall_send_mouse_up_event(Window *win, int x, int y) { - if (!win || !win->data) return; + if (!win) return; user_window_mouse_up(win, x, y); } static void user_window_key(Window *win, char c, bool pressed) { - process_t *proc = (process_t *)win->data; + process_t *proc = process_get_by_ui_window(win); if (!proc) return; gui_event_t ev = { .type = pressed ? GUI_EVENT_KEY : GUI_EVENT_KEYUP, .arg1 = (int)c }; process_push_gui_event(proc, &ev); diff --git a/src/wm/wm.c b/src/wm/wm.c index 1755bfc..9101d9a 100644 --- a/src/wm/wm.c +++ b/src/wm/wm.c @@ -1576,20 +1576,24 @@ void wm_add_window(Window *win) { wm_lock_release(rflags); } -Window* wm_find_window_by_title(const char *title) { +Window* wm_find_window_by_title_locked(const char *title) { if (!title) return NULL; - uint64_t rflags; - rflags = wm_lock_acquire(); for (int i = 0; i < window_count; i++) { if (all_windows[i] && all_windows[i]->title && str_eq(all_windows[i]->title, title)) { - wm_lock_release(rflags); return all_windows[i]; } } - wm_lock_release(rflags); return NULL; } +Window* wm_find_window_by_title(const char *title) { + if (!title) return NULL; + uint64_t rflags = wm_lock_acquire(); + Window *win = wm_find_window_by_title_locked(title); + wm_lock_release(rflags); + return win; +} + void wm_remove_window(Window *win) { if (!win) return; @@ -1811,7 +1815,7 @@ void wm_handle_click(int x, int y) { if (item == 0) { // About process_create_elf("/bin/about.elf", NULL); } else if (item == 1) { // Settings - Window *existing = wm_find_window_by_title("Settings"); + Window *existing = wm_find_window_by_title_locked("Settings"); if (existing) wm_bring_to_front_locked(existing); else process_create_elf("/bin/settings.elf", NULL); } else if (item == 2) { // Shutdown @@ -1946,7 +1950,9 @@ void wm_handle_right_click(int x, int y) { } force_redraw = true; -}void wm_handle_mouse(int dx, int dy, uint8_t buttons, int dz) { +} + +static void wm_handle_mouse_internal(int dx, int dy, uint8_t buttons, int dz) { int sw = get_screen_width(); int sh = get_screen_height(); @@ -2153,53 +2159,53 @@ void wm_handle_right_click(int x, int y) { if (str_starts_with(start_menu_pending_app, "Files")) { explorer_open_directory("/"); } else if (str_starts_with(start_menu_pending_app, "Notepad")) { - Window *existing = wm_find_window_by_title("Notepad"); + Window *existing = wm_find_window_by_title_locked("Notepad"); if (existing) { wm_bring_to_front_locked(existing); } else { process_create_elf("/bin/notepad.elf", NULL); } } else if (str_starts_with(start_menu_pending_app, "Editor")) { - Window *existing = wm_find_window_by_title("Txtedit"); + Window *existing = wm_find_window_by_title_locked("Txtedit"); if (existing) wm_bring_to_front_locked(existing); else process_create_elf("/bin/txtedit.elf", NULL); } else if (str_starts_with(start_menu_pending_app, "Word Processor")) { - Window *existing = wm_find_window_by_title("Word Processor"); + Window *existing = wm_find_window_by_title_locked("Word Processor"); if (existing) wm_bring_to_front_locked(existing); else process_create_elf("/bin/boredword.elf", NULL); } else if (str_starts_with(start_menu_pending_app, "Terminal")) { cmd_reset(); wm_bring_to_front_locked(&win_cmd); } else if (str_starts_with(start_menu_pending_app, "Calculator")) { - Window *existing = wm_find_window_by_title("Calculator"); + Window *existing = wm_find_window_by_title_locked("Calculator"); if (existing) { wm_bring_to_front_locked(existing); } else { process_create_elf("/bin/calculator.elf", NULL); } } else if (str_starts_with(start_menu_pending_app, "Minesweeper")) { - Window *existing = wm_find_window_by_title("Minesweeper"); + Window *existing = wm_find_window_by_title_locked("Minesweeper"); if (existing) wm_bring_to_front_locked(existing); else process_create_elf("/bin/minesweeper.elf", NULL); } else if (str_starts_with(start_menu_pending_app, "Settings")) { - Window *existing = wm_find_window_by_title("Settings"); + Window *existing = wm_find_window_by_title_locked("Settings"); if (existing) wm_bring_to_front_locked(existing); else process_create_elf("/bin/settings.elf", NULL); } else if (str_starts_with(start_menu_pending_app, "Paint")) { - Window *existing = wm_find_window_by_title("Paint"); + Window *existing = wm_find_window_by_title_locked("Paint"); if (existing) wm_bring_to_front_locked(existing); else process_create_elf("/bin/paint.elf", NULL); } else if (str_starts_with(start_menu_pending_app, "Clock")) { - Window *existing = wm_find_window_by_title("Clock"); + Window *existing = wm_find_window_by_title_locked("Clock"); if (existing) wm_bring_to_front_locked(existing); else process_create_elf("/bin/clock.elf", NULL); } else if (str_starts_with(start_menu_pending_app, "Browser")) { - Window *existing = wm_find_window_by_title("Web Browser"); + Window *existing = wm_find_window_by_title_locked("Web Browser"); if (existing) wm_bring_to_front_locked(existing); else process_create_elf("/bin/browser.elf", NULL); } else if (str_starts_with(start_menu_pending_app, "About")) { process_create_elf("/bin/about.elf", NULL); } else if (str_starts_with(start_menu_pending_app, "Task Manager")) { - Window *existing = wm_find_window_by_title("Task Manager"); + Window *existing = wm_find_window_by_title_locked("Task Manager"); if (existing) wm_bring_to_front_locked(existing); else process_create_elf("/bin/taskman.elf", NULL); } else if (str_starts_with(start_menu_pending_app, "Shutdown")) { @@ -2575,6 +2581,12 @@ void wm_handle_right_click(int x, int y) { prev_left = left; } +void wm_handle_mouse(int dx, int dy, uint8_t buttons, int dz) { + uint64_t rflags = wm_lock_acquire(); + wm_handle_mouse_internal(dx, dy, buttons, dz); + wm_lock_release(rflags); +} + // Input Queue #define INPUT_QUEUE_SIZE 128 typedef struct {