Programs work now.

This commit is contained in:
Rasmus Rasmussen 2026-01-05 16:57:37 +01:00
parent aac26f012e
commit 6d04d15449
12 changed files with 128 additions and 51 deletions

View File

@ -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) {

View File

@ -2,13 +2,11 @@
void Helpers::recalculate_taskbar(std::vector<TaskBarItem>& 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;
}
}
}

View File

@ -23,8 +23,9 @@ void Program::close() {
void Program::loop(void* pvParameters) {
Program* self = static_cast<Program*>(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
}
}

View File

@ -4,6 +4,7 @@
#include "GLOBALS.h"
#include <string>
#include <vector>
#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();

View File

@ -0,0 +1,22 @@
#include "program_registry.h"
#include <Arduino.h>
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");
}

View File

@ -0,0 +1,9 @@
#pragma once
struct InstalledProgram {
void do_stuff();
void do_some_other_stuff();
void lol();
void omagelol();
void sheeesh();
};

View File

@ -1,3 +1,4 @@
#include <string>
#include "shell.h"
#include "GLOBALS.h"
#include <algorithm>
@ -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;
}
}

View File

@ -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 =======

View File

@ -1,26 +1,48 @@
#include "program.h"
#include "system_manager.h"
#include <Arduino.h>
std::vector<Program*> 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
}
}
}

View File

@ -1,14 +1,19 @@
#pragma once
#include "program.h"
#include <vector>
#include "program_registry.h"
#include <unordered_map>
#include <string>
class SystemManager {
private:
DISPLAY_STATE* _display_state;
int _next_program_id = 1;
public:
std::unordered_map<std::string, void(InstalledProgram::*)()> funcMap;
std::vector<Program*> _programs;
std::vector<Program*> init(DISPLAY_STATE* display_state);
void spawn_program(Window* window);
bool spawn_program(Window* window, std::string program_name);
void close_program(int id);
};

View File

@ -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);
}

View File

@ -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);