From 6d04d154494824514dc909d3e818c3ed55864ec9 Mon Sep 17 00:00:00 2001 From: rasmus Date: Mon, 5 Jan 2026 16:57:37 +0100 Subject: [PATCH] Programs work now. --- Desktop_Test/event_manager.cpp | 2 +- Desktop_Test/helpers.cpp | 6 +-- Desktop_Test/program.cpp | 7 +-- Desktop_Test/program.h | 7 ++- Desktop_Test/program_registry.cpp | 22 +++++++++ Desktop_Test/program_registry.h | 9 ++++ Desktop_Test/shell.cpp | 77 ++++++++++++++++++------------- Desktop_Test/shell.h | 1 + Desktop_Test/system_manager.cpp | 32 +++++++++++-- Desktop_Test/system_manager.h | 7 ++- Desktop_Test/tft_handler.cpp | 7 +-- Desktop_Test/tft_handler.h | 2 +- 12 files changed, 128 insertions(+), 51 deletions(-) create mode 100644 Desktop_Test/program_registry.cpp create mode 100644 Desktop_Test/program_registry.h diff --git a/Desktop_Test/event_manager.cpp b/Desktop_Test/event_manager.cpp index a2a8f92..36d16fd 100644 --- a/Desktop_Test/event_manager.cpp +++ b/Desktop_Test/event_manager.cpp @@ -2,7 +2,7 @@ #include "event_manager.h" void EventManager::init() { - xTaskCreatePinnedToCore(dispatch_task, "event_dispatch", 4096, this, 2, &_dispatcher_task, 0); + xTaskCreatePinnedToCore(dispatch_task, "event_dispatch", 4096, this, 10, &_dispatcher_task, 0); } void EventManager::publish(const CLICK_EVENT& event) { diff --git a/Desktop_Test/helpers.cpp b/Desktop_Test/helpers.cpp index 664903a..0cfc281 100644 --- a/Desktop_Test/helpers.cpp +++ b/Desktop_Test/helpers.cpp @@ -2,13 +2,11 @@ void Helpers::recalculate_taskbar(std::vector& task_bar_items) { for (size_t i = 0; i < task_bar_items.size(); ++i) { - TaskBarItem item = task_bar_items[i]; - if (i == 0) { - item.place_x = 60; + task_bar_items[i].place_x = 60; } else { const TaskBarItem& prev = task_bar_items[i - 1]; - item.place_x = prev.place_x + prev.offset_x; + task_bar_items[i].place_x = prev.place_x + prev.offset_x; } } } \ No newline at end of file diff --git a/Desktop_Test/program.cpp b/Desktop_Test/program.cpp index 8874ad1..9354481 100644 --- a/Desktop_Test/program.cpp +++ b/Desktop_Test/program.cpp @@ -23,8 +23,9 @@ void Program::close() { void Program::loop(void* pvParameters) { Program* self = static_cast(pvParameters); - for(;;) { - //self->_display_state->update_display.store(true); - vTaskDelay(500); + (self->_installed_program.*(self->_program_func))(); + + while (true) { + vTaskDelay(pdMS_TO_TICKS(1000)); // Sleep for 1 second } } \ No newline at end of file diff --git a/Desktop_Test/program.h b/Desktop_Test/program.h index 92ddf02..3c32fe6 100644 --- a/Desktop_Test/program.h +++ b/Desktop_Test/program.h @@ -4,6 +4,7 @@ #include "GLOBALS.h" #include #include +#include "program_registry.h" struct TaskBarItem { int id; @@ -22,6 +23,7 @@ struct DesktopItem { int place_y; int icon_size_x; int icon_size_y; + std::string name; }; enum class WindowAction { @@ -58,12 +60,15 @@ class Program { private: Window* _window; DISPLAY_STATE* _display_state; - std::string _task_name = ""; + TaskHandle_t _task_handle; static void loop(void* pvParameters); public: int _id; + std::string _task_name = ""; + void (InstalledProgram::*_program_func)(); + InstalledProgram _installed_program; Program(DISPLAY_STATE* display_state); int init(int id, std::string name, Window* window); void close(); diff --git a/Desktop_Test/program_registry.cpp b/Desktop_Test/program_registry.cpp new file mode 100644 index 0000000..91c5ac0 --- /dev/null +++ b/Desktop_Test/program_registry.cpp @@ -0,0 +1,22 @@ +#include "program_registry.h" +#include + +void InstalledProgram::do_stuff() { + Serial.println("lol"); +} + +void InstalledProgram::do_some_other_stuff() { + Serial.println("ASDDSAASDASD"); +} + +void InstalledProgram::lol() { + Serial.println("ffffrf"); +} + +void InstalledProgram::omagelol() { + Serial.println("11111"); +} + +void InstalledProgram::sheeesh() { + Serial.println("44444"); +} \ No newline at end of file diff --git a/Desktop_Test/program_registry.h b/Desktop_Test/program_registry.h new file mode 100644 index 0000000..68d22d2 --- /dev/null +++ b/Desktop_Test/program_registry.h @@ -0,0 +1,9 @@ +#pragma once + +struct InstalledProgram { + void do_stuff(); + void do_some_other_stuff(); + void lol(); + void omagelol(); + void sheeesh(); +}; \ No newline at end of file diff --git a/Desktop_Test/shell.cpp b/Desktop_Test/shell.cpp index bff42a4..56bc54f 100644 --- a/Desktop_Test/shell.cpp +++ b/Desktop_Test/shell.cpp @@ -1,3 +1,4 @@ +#include #include "shell.h" #include "GLOBALS.h" #include @@ -8,7 +9,7 @@ void Shell::init(TFT_Handler* th, DISPLAY_STATE* ds, SystemManager* system_manag tft = th; display_state = ds; _system_manager = system_manager; - + // Subscribe to click events EventManager::getInstance().subscribe(this); @@ -23,6 +24,7 @@ void Shell::init(TFT_Handler* th, DISPLAY_STATE* ds, SystemManager* system_manag .place_y = 10, .icon_size_x = 45, .icon_size_y = 50, + .name = std::to_string(i), }; items.push_back(di); } @@ -34,7 +36,8 @@ void Shell::init(TFT_Handler* th, DISPLAY_STATE* ds, SystemManager* system_manag void Shell::on_click_event(CLICK_EVENT event) { if (event.event != CLICK_EVENTS::LEFT_CLICK) return; - + + // Always check windows first int window_id = handle_window_click(event); if (window_id >= 0) { display_state->update_display.store(true); @@ -42,21 +45,18 @@ void Shell::on_click_event(CLICK_EVENT event) { } int icon_id = handle_desktop_click(event); - if (icon_id != -1) { display_state->update_display.store(true); return; } int tast_icon_id = handle_taskbar_click(event); - if (tast_icon_id != -1) { display_state->update_display.store(true); return; } bool ckicked_start_button = handle_start_button_click(event); - if (ckicked_start_button) { display_state->update_display.store(true); return; @@ -73,7 +73,7 @@ void Shell::create_window(Window* window) { if (total_pos_y > MAX_SCREEN_HEIGHT) window->y = MAX_SCREEN_HEIGHT - window->height - 29; windows.push_back(window); - + TaskBarItem item; item.id = window->id; item.focused = window->focused; @@ -176,9 +176,9 @@ void Shell::draw_window(Window window) { tft->draw_box(window.x + it->x_offset, window.y + it->y_offset, it->height, it->width, COL_LIGHT_GREY); } } - + // Window title - tft->draw_text(window.x + 6 + 65, window.y + 10, window.title.c_str()); + tft->draw_text(window.x + 6 + 65, window.y + 10, window.title, COL_WHITE); // Window content tft->draw_box(window.x + 3, window.y + 35, window.width - 6, window.height - 38, COL_WHITE); @@ -218,58 +218,68 @@ void Shell::draw_task_bar(int color) { tft->draw_box(item.place_x - 1, item.place_y - 1, item.width + 2, item.height + 2, COL_DARK_GREY); tft->draw_box(item.place_x, item.place_y, item.width + 2, item.height + 2, COL_WHITE); tft->draw_box(item.place_x, item.place_y, item.width, item.height, COL_GREY); - } - else { + + tft->draw_text(item.place_x + 2, item.place_y + 2, item.name, COL_BLACK); + } else { tft->draw_box(item.place_x - 1, item.place_y - 1, item.width + 1, item.height + 1, COL_WHITE); tft->draw_box(item.place_x, item.place_y, item.width + 2, item.height + 2, COL_BLACK); tft->draw_rect(item.place_x, item.place_y, item.width + 1, item.height + 1, 1, COL_DARK_GREY); tft->draw_box(item.place_x, item.place_y, item.width, item.height, COL_GREY); + + tft->draw_text(item.place_x + 2, item.place_y + 2, item.name, COL_BLACK); } } } +void Shell::draw_start_menu() { + +} + int Shell::handle_window_click(CLICK_EVENT event) { // Iterate BACKWARDS - last window is on top for (int i = windows.size() - 1; i >= 0; i--) { Window* window = windows[i]; - + if (window->minimized) continue; + // Check if click is within window bounds - if (event.x >= window->x && event.x <= (window->x + window->width) && - event.y >= window->y && event.y <= (window->y + window->height)) { - + if (event.x >= window->x && event.x <= (window->x + window->width) + && event.y >= window->y && event.y <= (window->y + window->height)) { + for (auto it = window->window_decorations.begin(); it != window->window_decorations.end(); ++it) { int x = window->x + it->x_offset; int y = window->y + it->y_offset; + // Check if click is within either of the buttons if (event.x >= x && event.x <= (x + it->width) && event.y >= y && event.y <= (y + it->height) && it->action == WindowAction::CLOSE && !window->minimized) { close_window(window->id); return window->id; } - if (event.x >= x && event.x <= (x + it->width) && event.y >= y && event.y <= (y + it->height) && it->action == WindowAction::MINIMIZE && !window->minimized) { + if (event.x >= x && event.x <= (x + it->width) && event.y >= y && event.y <= (y + it->height) && it->action == WindowAction::MINIMIZE) { minimize_window(window->id); return window->id; } } + // If the window is already focused, return if (window->focused) return -1; - + // Unfocus all windows for (auto& win : windows) { win->focused = false; } - + // Focus this window window->focused = true; - + // Move window to end (top of z-order) auto it = windows.begin() + i; std::rotate(it, it + 1, windows.end()); - + return window->id; } } - + return -1; } @@ -277,14 +287,13 @@ int Shell::handle_desktop_click(CLICK_EVENT event) { // Check if click is on desktop (not on taskbar or windows) // This is where you'd check if an icon was clicked for (const auto& item : items) { - if (event.x >= item.place_x && - event.x <= (item.place_x + item.icon_size_x) && - event.y >= item.place_y && - event.y <= (item.place_y + item.icon_size_y)) { - - Serial.print("Desktop item clicked: "); - Serial.println(item.id); - + if (event.x >= item.place_x && event.x <= (item.place_x + item.icon_size_x) && event.y >= item.place_y && event.y <= (item.place_y + item.icon_size_y)) { + + for (int i = 0; i < _system_manager->_programs.size(); ++i) { + Serial.println(_system_manager->_programs[i]->_task_name.c_str()); + if (_system_manager->_programs[i]->_task_name == item.name) return -1; + } + WindowDecoration dec; Window* win = new Window(); @@ -307,7 +316,6 @@ int Shell::handle_desktop_click(CLICK_EVENT event) { win->window_decorations.push_back(dec1); win->window_decorations.push_back(dec2); - win->id = 0; win->x = 10; win->y = 0; win->width = 350; @@ -316,12 +324,17 @@ int Shell::handle_desktop_click(CLICK_EVENT event) { win->foreground_color = COL_DARK_BLUE; win->focused = true; win->minimized = false; - win->title = "Hello World!"; + win->title = item.name; + + bool result = _system_manager->spawn_program(win, win->title); + + if (!result) { + delete win; + return -1; + } - _system_manager->spawn_program(win); create_window(win); - // TODO: Handle icon click (open window, etc.) return item.id; } } diff --git a/Desktop_Test/shell.h b/Desktop_Test/shell.h index fbe861a..d4eaaf7 100644 --- a/Desktop_Test/shell.h +++ b/Desktop_Test/shell.h @@ -33,6 +33,7 @@ public: // ======= Desktop ======= void draw_background(int color); void draw_task_bar(int color); + void draw_start_menu(); void draw_desktop_Item(DesktopItem desktop_item); // ======= Shared ======= diff --git a/Desktop_Test/system_manager.cpp b/Desktop_Test/system_manager.cpp index 4c0ae84..8a31bc2 100644 --- a/Desktop_Test/system_manager.cpp +++ b/Desktop_Test/system_manager.cpp @@ -1,26 +1,48 @@ #include "program.h" #include "system_manager.h" +#include std::vector SystemManager::init(DISPLAY_STATE* display_state) { _display_state = display_state; + + funcMap["0"] = &InstalledProgram::do_stuff; + funcMap["1"] = &InstalledProgram::do_some_other_stuff; + funcMap["2"] = &InstalledProgram::lol; + funcMap["3"] = &InstalledProgram::omagelol; + funcMap["4"] = &InstalledProgram::sheeesh; + return _programs; } -void SystemManager::spawn_program(Window* window) { +bool SystemManager::spawn_program(Window* window, std::string program_name) { + for (size_t i = 0; i < _programs.size(); ++i) { + if (_programs[i]->_task_name == window->title) return false; + } + + // Check if the program exists in the map + if (funcMap.find(program_name) == funcMap.end()) { + return false; + } + + Serial.println(window->title.c_str()); + Program* program = new Program(_display_state); - window->id = _programs.size() + 1; - program->init(_programs.size() + 1, "lol", window); + program->_program_func = funcMap[program_name]; + window->id = _next_program_id; + program->init(_next_program_id, window->title, window); + _next_program_id++; _programs.push_back(program); + return true; } void SystemManager::close_program(int id) { for (size_t i = 0; i < _programs.size(); ++i) { if (_programs[i]->_id == id) { _programs[i]->close(); - delete _programs[i]; // Free the memory + delete _programs[i]; // Free the memory _programs.erase(_programs.begin() + i); - break; // Exit after erase + break; // Exit after erase } } } \ No newline at end of file diff --git a/Desktop_Test/system_manager.h b/Desktop_Test/system_manager.h index 2abecb6..52e4d71 100644 --- a/Desktop_Test/system_manager.h +++ b/Desktop_Test/system_manager.h @@ -1,14 +1,19 @@ #pragma once #include "program.h" #include +#include "program_registry.h" +#include +#include class SystemManager { private: DISPLAY_STATE* _display_state; + int _next_program_id = 1; public: + std::unordered_map funcMap; std::vector _programs; std::vector init(DISPLAY_STATE* display_state); - void spawn_program(Window* window); + bool spawn_program(Window* window, std::string program_name); void close_program(int id); }; \ No newline at end of file diff --git a/Desktop_Test/tft_handler.cpp b/Desktop_Test/tft_handler.cpp index 090146c..a91e121 100644 --- a/Desktop_Test/tft_handler.cpp +++ b/Desktop_Test/tft_handler.cpp @@ -1,3 +1,4 @@ +#include "GLOBALS.h" #include "tft_handler.h" void TFT_Handler::init(DISPLAY_STATE* display_state) { @@ -30,9 +31,9 @@ void TFT_Handler::draw_line(int x1, int y1, int x2, int y2, int color) { tft.drawLine(x1, y1, x2, y2, color); } -void TFT_Handler::draw_text(int x, int y, std::string str) { - tft.setTextColor(TFT_WHITE); - tft.setTextSize(2); +void TFT_Handler::draw_text(int x, int y, std::string str, int color) { + tft.setTextColor(color); + tft.setTextSize(1); tft.drawString(str.c_str(), x, y); } diff --git a/Desktop_Test/tft_handler.h b/Desktop_Test/tft_handler.h index d6fe287..3ed237b 100644 --- a/Desktop_Test/tft_handler.h +++ b/Desktop_Test/tft_handler.h @@ -62,7 +62,7 @@ public: void draw_box(int x, int y, int size_x, int size_y, int color); void draw_rect(int x, int y, int size_x, int size_y, int thickness, int color); void draw_line(int x1, int y1, int x2, int y2, int color); - void draw_text(int x, int y, std::string str); + void draw_text(int x, int y, std::string str, int color); void fill_screen(int color); void draw_box_sprite(int x, int y, int size_x, int size_y, int color);