Initial commit

This commit is contained in:
Chris
2026-02-04 20:51:17 +01:00
commit ddac1a791e
132 changed files with 11491 additions and 0 deletions

65
src/kernel/about.c Normal file
View File

@@ -0,0 +1,65 @@
#include "about.h"
#include "graphics.h"
#include "wm.h"
#include <stddef.h>
Window win_about;
// Color definitions
#define COLOR_BLUE_LOGO 0xFF1E8AF5
#define COLOR_GREEN_LOGO 0xFF6DD651
#define COLOR_YELLOW_LOGO 0xFFF5BE34
#define COLOR_RED_LOGO 0xFFF05456
#define COLOR_PURPLE_LOGO 0xFFA65DC2
#define COLOR_CYAN_LOGO 0xFF368DF7
static void about_paint(Window *win) {
// Background
draw_rect(win->x + 4, win->y + 24, win->w - 8, win->h - 28, COLOR_LTGRAY);
int offset_x = win->x + 15;
int offset_y = win->y + 35;
// Draw brewkernel ASCII logo
draw_string(offset_x, offset_y, "( (", COLOR_BLUE_LOGO);
draw_string(offset_x, offset_y + 15, " ) )", COLOR_GREEN_LOGO);
draw_string(offset_x, offset_y + 30, " ........", COLOR_YELLOW_LOGO);
draw_string(offset_x, offset_y + 45, " | |]", COLOR_RED_LOGO);
draw_string(offset_x, offset_y + 60, " \\ /", COLOR_PURPLE_LOGO);
draw_string(offset_x, offset_y + 75, " `----'", COLOR_CYAN_LOGO);
// Version info
draw_string(offset_x, offset_y + 105, "BrewOS", COLOR_BLACK);
draw_string(offset_x, offset_y + 120, "Version 1.0", COLOR_BLACK);
// Copyright
draw_string(offset_x, offset_y + 150, "(C) 2026 boreddevnl.", COLOR_BLACK);
draw_string(offset_x, offset_y + 165, "All rights reserved.", COLOR_BLACK);
}
static void about_click(Window *win, int x, int y) {
(void)win;
(void)x;
(void)y;
// No interactive elements needed for About dialog
}
void about_init(void) {
win_about.title = "About BrewOS";
win_about.x = 250;
win_about.y = 180;
win_about.w = 185;
win_about.h = 240;
win_about.visible = false;
win_about.focused = false;
win_about.z_index = 0;
win_about.paint = about_paint;
win_about.handle_click = about_click;
win_about.handle_right_click = NULL;
win_about.handle_key = NULL;
}

10
src/kernel/about.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef ABOUT_H
#define ABOUT_H
#include "wm.h"
extern Window win_about;
void about_init(void);
#endif

23
src/kernel/boot.asm Normal file
View File

@@ -0,0 +1,23 @@
; brew-os/src/kernel/boot.asm
; 64-bit Entry Point for BrewOS
section .text
global _start
extern kmain
bits 64
_start:
; Ensure interrupts are disabled
cli
; Setup stack is handled by Limine, but we can re-align if paranoid
; (Limine guarantees 16-byte alignment)
; Call the C kernel entry point
call kmain
; Halt if kmain returns
hlt
.loop:
jmp .loop

170
src/kernel/calculator.c Normal file
View File

@@ -0,0 +1,170 @@
#include "calculator.h"
#include "graphics.h"
#include "wm.h"
#include <stdbool.h>
#include <stddef.h>
Window win_calculator;
static long long calc_acc = 0;
static long long calc_curr = 0;
static char calc_op = 0;
static bool calc_new_entry = true;
static bool calc_error = false;
// Helper to convert int to string
static void int_to_str(long long n, char *buf) {
if (n == 0) {
buf[0] = '0'; buf[1] = 0; return;
}
int i = 0;
bool neg = n < 0;
if (neg) n = -n;
char temp[32];
while (n > 0) {
temp[i++] = '0' + (n % 10);
n /= 10;
}
if (neg) temp[i++] = '-';
int j = 0;
while (i > 0) {
buf[j++] = temp[--i];
}
buf[j] = 0;
}
static void update_display(Window *win) {
if (calc_error) {
char *err = "Error";
int i = 0; while(err[i]) { win->buffer[i] = err[i]; i++; }
win->buffer[i] = 0;
} else {
int_to_str(calc_curr, win->buffer);
}
win->buf_len = 0; while(win->buffer[win->buf_len]) win->buf_len++;
}
static void calculator_paint(Window *win) {
// Background
draw_rect(win->x + 4, win->y + 24, win->w - 8, win->h - 28, COLOR_LTGRAY);
// Display Area
draw_bevel_rect(win->x + 10, win->y + 30, win->w - 20, 25, true);
// Right align text
int text_w = win->buf_len * 8;
int text_x = win->x + win->w - 15 - text_w;
draw_string(text_x, win->y + 38, win->buffer, COLOR_BLACK);
// Buttons
const char *labels[] = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", "C", "=", "+"
};
int bw = 30;
int bh = 25;
int gap = 5;
int start_x = win->x + 10;
int start_y = win->y + 65;
for (int i = 0; i < 16; i++) {
int r = i / 4;
int c = i % 4;
draw_button(start_x + c*(bw+gap), start_y + r*(bh+gap), bw, bh, labels[i], false);
}
}
static void do_op(void) {
if (calc_op == '+') calc_acc += calc_curr;
else if (calc_op == '-') calc_acc -= calc_curr;
else if (calc_op == '*') calc_acc *= calc_curr;
else if (calc_op == '/') {
if (calc_curr == 0) calc_error = true;
else calc_acc /= calc_curr;
} else {
calc_acc = calc_curr;
}
}
static void calculator_click(Window *win, int x, int y) {
int bw = 30;
int bh = 25;
int gap = 5;
int start_x = 10;
int start_y = 65;
for (int i = 0; i < 16; i++) {
int r = i / 4;
int c = i % 4;
int bx = start_x + c*(bw+gap);
int by = start_y + r*(bh+gap);
if (x >= bx && x < bx + bw && y >= by && y < by + bh) {
// Clicked button i
const char *labels[] = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", "C", "=", "+"
};
char lbl = labels[i][0];
if (lbl >= '0' && lbl <= '9') {
if (calc_new_entry || calc_curr == 0) {
calc_curr = lbl - '0';
calc_new_entry = false;
} else {
calc_curr = calc_curr * 10 + (lbl - '0');
}
calc_error = false;
} else if (lbl == 'C') {
calc_curr = 0;
calc_acc = 0;
calc_op = 0;
calc_new_entry = true;
calc_error = false;
} else if (lbl == '=') {
do_op();
calc_curr = calc_acc;
calc_op = 0;
calc_new_entry = true;
} else {
if (!calc_new_entry) {
if (calc_op) do_op();
else calc_acc = calc_curr;
}
calc_op = lbl;
calc_new_entry = true;
}
update_display(win);
wm_paint(); // Request repaint
return;
}
}
}
void calculator_init(void) {
win_calculator.title = "Calculator";
win_calculator.x = 200;
win_calculator.y = 200;
win_calculator.w = 160;
win_calculator.h = 200;
win_calculator.visible = false;
win_calculator.focused = false;
win_calculator.z_index = 0;
win_calculator.paint = calculator_paint;
win_calculator.handle_click = calculator_click;
win_calculator.handle_right_click = NULL;
calc_curr = 0;
calc_acc = 0;
calc_op = 0;
calc_new_entry = true;
update_display(&win_calculator);
}

10
src/kernel/calculator.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include "wm.h"
extern Window win_calculator;
void calculator_init(void);
#endif

View File

@@ -0,0 +1,6 @@
#include "cli_utils.h"
void cli_cmd_about(char *args) {
(void)args;
cli_write("BrewOS Desktop v0.01 Alpha\n");
}

View File

@@ -0,0 +1,16 @@
#include "cli_utils.h"
#include "io.h"
void cli_cmd_beep(char *args) {
(void)args;
cli_write("BEEP!\n");
outb(0x43, 0xB6);
int freq = 1000;
int div = 1193180 / freq;
outb(0x42, div & 0xFF);
outb(0x42, (div >> 8) & 0xFF);
outb(0x61, inb(0x61) | 0x03);
cli_delay(10000000);
outb(0x61, inb(0x61) & 0xFC);
}

View File

@@ -0,0 +1,8 @@
#include "cli_utils.h"
void cli_cmd_blind(char *args) {
(void)args;
cli_write("Woah.. is this heaven?\n");
cli_write("no.\n");
cli_write("This isn't TempleOS you fucking retard.\n");
}

View File

@@ -0,0 +1,9 @@
#include "cli_utils.h"
// Public declaration from cmd.c
extern void cmd_screen_clear(void);
void cli_cmd_clear(char *args) {
(void)args;
cmd_screen_clear();
}

View File

@@ -0,0 +1,39 @@
#ifndef CLI_APPS_H
#define CLI_APPS_H
// All CLI command function declarations
void cli_cmd_help(char *args);
void cli_cmd_date(char *args);
void cli_cmd_math(char *args);
void cli_cmd_beep(char *args);
void cli_cmd_cowsay(char *args);
void cli_cmd_reboot(char *args);
void cli_cmd_shutdown(char *args);
void cli_cmd_uptime(char *args);
void cli_cmd_man(char *args);
void cli_cmd_license(char *args);
void cli_cmd_txtedit(char *args);
void cli_cmd_blind(char *args);
void cli_cmd_readtheman(char *args);
void cli_cmd_about(char *args);
void cli_cmd_clear(char *args);
void cli_cmd_exit(char *args);
// Filesystem commands
void cli_cmd_cd(char *args);
void cli_cmd_pwd(char *args);
void cli_cmd_ls(char *args);
void cli_cmd_mkdir(char *args);
void cli_cmd_rm(char *args);
void cli_cmd_echo(char *args);
void cli_cmd_cat(char *args);
// Memory management commands
void cli_cmd_meminfo(char *args);
void cli_cmd_malloc(char *args);
void cli_cmd_free_mem(char *args);
void cli_cmd_memblock(char *args);
void cli_cmd_memvalid(char *args);
void cli_cmd_memtest(char *args);
#endif

View File

@@ -0,0 +1,22 @@
#ifndef CLI_COMMAND_H
#define CLI_COMMAND_H
#include <stddef.h>
// Standard interface for CLI command output
// Commands should call these functions to write to the terminal
extern void cli_write(const char *str);
extern void cli_write_int(int n);
extern void cli_putchar(char c);
// Callback function type for command execution
typedef void (*cmd_callback_t)(char *args);
// Command entry in dispatch table
typedef struct {
const char *name;
cmd_callback_t callback;
const char *help_text;
} CLI_Command;
#endif

View File

@@ -0,0 +1,80 @@
#include "cli_utils.h"
// Forward declarations - these will be provided by cmd.c
extern void cmd_putchar(char c);
extern void cmd_write(const char *str);
extern void cmd_write_int(int n);
void cli_memset(void *dest, int val, size_t len) {
unsigned char *ptr = dest;
while (len-- > 0) *ptr++ = val;
}
size_t cli_strlen(const char *str) {
size_t len = 0;
while (str[len]) len++;
return len;
}
int cli_strcmp(const char *s1, const char *s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}
void cli_strcpy(char *dest, const char *src) {
while (*src) *dest++ = *src++;
*dest = 0;
}
int cli_atoi(const char *str) {
int res = 0;
int sign = 1;
if (*str == '-') { sign = -1; str++; }
while (*str >= '0' && *str <= '9') {
res = res * 10 + (*str - '0');
str++;
}
return res * sign;
}
void cli_itoa(int n, char *buf) {
if (n == 0) {
buf[0] = '0'; buf[1] = 0; return;
}
int i = 0;
int sign = n < 0;
if (sign) n = -n;
while (n > 0) {
buf[i++] = (n % 10) + '0';
n /= 10;
}
if (sign) buf[i++] = '-';
buf[i] = 0;
// Reverse
for (int j = 0; j < i / 2; j++) {
char t = buf[j];
buf[j] = buf[i - 1 - j];
buf[i - 1 - j] = t;
}
}
void cli_write(const char *str) {
cmd_write(str);
}
void cli_write_int(int n) {
cmd_write_int(n);
}
void cli_putchar(char c) {
cmd_putchar(c);
}
void cli_delay(int iterations) {
for (volatile int i = 0; i < iterations; i++) {
__asm__ __volatile__("nop");
}
}

View File

@@ -0,0 +1,27 @@
#ifndef CLI_UTILS_H
#define CLI_UTILS_H
#include <stddef.h>
#include <stdint.h>
// String utilities
void cli_memset(void *dest, int val, size_t len);
size_t cli_strlen(const char *str);
int cli_strcmp(const char *s1, const char *s2);
void cli_strcpy(char *dest, const char *src);
int cli_atoi(const char *str);
void cli_itoa(int n, char *buf);
// IO utilities
void cli_write(const char *str);
void cli_write_int(int n);
void cli_putchar(char c);
// Timing utility
void cli_delay(int iterations);
// CLI Command declarations
void cli_cmd_shutdown(char *args);
void cli_cmd_reboot(char *args);
#endif

View File

@@ -0,0 +1,17 @@
#include "cli_utils.h"
void cli_cmd_cowsay(char *args) {
if (!args || !*args) args = (char*)"Brew!";
size_t len = cli_strlen(args);
cli_write(" ");
for(size_t i=0; i<len+2; i++) cli_write("_");
cli_write("\n< "); cli_write(args); cli_write(" >\n ");
for(size_t i=0; i<len+2; i++) cli_write("-");
cli_write("\n");
cli_write(" \\ ^__^\n");
cli_write(" \\ (oo)\\_______\n");
cli_write(" (__)\\ )\\/\\\n");
cli_write(" ||----w |\n");
cli_write(" || ||\n\n");
}

View File

@@ -0,0 +1,15 @@
#include "cli_utils.h"
// Forward declaration from cmd.c
extern void rtc_get_datetime(int *y, int *m, int *d, int *h, int *min, int *s);
void cli_cmd_date(char *args) {
(void)args;
int y, m, d, h, min, s;
rtc_get_datetime(&y, &m, &d, &h, &min, &s);
cli_write("Current Date: ");
cli_write_int(y); cli_write("-"); cli_write_int(m); cli_write("-"); cli_write_int(d);
cli_write(" ");
cli_write_int(h); cli_write(":"); cli_write_int(min); cli_write(":"); cli_write_int(s);
cli_write("\n");
}

View File

@@ -0,0 +1,9 @@
#include "cli_utils.h"
// Public declaration from cmd.c
extern void cmd_window_exit(void);
void cli_cmd_exit(char *args) {
(void)args;
cmd_window_exit();
}

View File

@@ -0,0 +1,241 @@
#include "cli_utils.h"
#include "fat32.h"
void cli_cmd_cd(char *args) {
if (!args || args[0] == 0) {
// No argument - show current directory
char cwd[256];
fat32_get_current_dir(cwd, sizeof(cwd));
cli_write("Current directory: ");
cli_write(cwd);
cli_write("\n");
return;
}
// Parse argument
char path[256];
int i = 0;
while (args[i] && args[i] != ' ' && args[i] != '\t') {
path[i] = args[i];
i++;
}
path[i] = 0;
if (fat32_chdir(path)) {
char cwd[256];
fat32_get_current_dir(cwd, sizeof(cwd));
cli_write("Changed to: ");
cli_write(cwd);
cli_write("\n");
} else {
cli_write("Error: Cannot change to directory: ");
cli_write(path);
cli_write("\n");
}
}
void cli_cmd_pwd(char *args) {
(void)args;
char cwd[256];
fat32_get_current_dir(cwd, sizeof(cwd));
cli_write(cwd);
cli_write("\n");
}
void cli_cmd_ls(char *args) {
char path[256];
if (!args || args[0] == 0) {
// List current directory
fat32_get_current_dir(path, sizeof(path));
} else {
// Parse argument
int i = 0;
while (args[i] && args[i] != ' ' && args[i] != '\t') {
path[i] = args[i];
i++;
}
path[i] = 0;
}
FAT32_FileInfo entries[256];
int count = fat32_list_directory(path, entries, 256);
if (count < 0) {
cli_write("Error: Cannot list directory\n");
return;
}
for (int i = 0; i < count; i++) {
cli_write(entries[i].name);
if (entries[i].is_directory) {
cli_write("/");
}
cli_write(" (");
cli_write_int(entries[i].size);
cli_write(" bytes)\n");
}
cli_write("\n");
cli_write("Total: ");
cli_write_int(count);
cli_write(" items\n");
}
void cli_cmd_mkdir(char *args) {
if (!args || args[0] == 0) {
cli_write("Usage: mkdir <dirname>\n");
return;
}
char dirname[256];
int i = 0;
while (args[i] && args[i] != ' ' && args[i] != '\t') {
dirname[i] = args[i];
i++;
}
dirname[i] = 0;
if (fat32_mkdir(dirname)) {
cli_write("Created directory: ");
cli_write(dirname);
cli_write("\n");
} else {
cli_write("Error: Cannot create directory\n");
}
}
void cli_cmd_rm(char *args) {
if (!args || args[0] == 0) {
cli_write("Usage: rm <filename>\n");
return;
}
char filename[256];
int i = 0;
while (args[i] && args[i] != ' ' && args[i] != '\t') {
filename[i] = args[i];
i++;
}
filename[i] = 0;
if (fat32_delete(filename)) {
cli_write("Deleted: ");
cli_write(filename);
cli_write("\n");
} else {
cli_write("Error: Cannot delete file\n");
}
}
void cli_cmd_echo(char *args) {
if (!args || args[0] == 0) {
cli_write("\n");
return;
}
// Check for redirection operators
char *redirect_ptr = NULL;
char redirect_mode = 0; // '>' for write, 'a' for append
char output_file[256] = {0};
char echo_text[512] = {0};
// Find > or >>
for (int i = 0; args[i]; i++) {
if (args[i] == '>' && args[i+1] == '>') {
redirect_ptr = args + i + 2;
redirect_mode = 'a'; // append
// Copy text before redirection
for (int j = 0; j < i; j++) {
echo_text[j] = args[j];
}
echo_text[i] = 0;
break;
} else if (args[i] == '>') {
redirect_ptr = args + i + 1;
redirect_mode = '>'; // write
// Copy text before redirection
for (int j = 0; j < i; j++) {
echo_text[j] = args[j];
}
echo_text[i] = 0;
break;
}
}
// If no redirection, just print the text
if (!redirect_ptr) {
cli_write(args);
cli_write("\n");
return;
}
// Parse output filename
int i = 0;
while (redirect_ptr[i] && (redirect_ptr[i] == ' ' || redirect_ptr[i] == '\t')) {
i++;
}
int j = 0;
while (redirect_ptr[i] && redirect_ptr[i] != ' ' && redirect_ptr[i] != '\t') {
output_file[j++] = redirect_ptr[i++];
}
output_file[j] = 0;
if (!output_file[0]) {
cli_write("Error: No output file specified\n");
return;
}
// Open file
const char *mode = (redirect_mode == 'a') ? "a" : "w";
FAT32_FileHandle *fh = fat32_open(output_file, mode);
if (!fh) {
cli_write("Error: Cannot open file for writing\n");
return;
}
// Write text
int text_len = 0;
while (echo_text[text_len]) text_len++;
fat32_write(fh, echo_text, text_len);
fat32_write(fh, "\n", 1);
fat32_close(fh);
cli_write("Wrote to: ");
cli_write(output_file);
cli_write("\n");
}
void cli_cmd_cat(char *args) {
if (!args || args[0] == 0) {
cli_write("Usage: cat <filename>\n");
return;
}
char filename[256];
int i = 0;
while (args[i] && args[i] != ' ' && args[i] != '\t') {
filename[i] = args[i];
i++;
}
filename[i] = 0;
FAT32_FileHandle *fh = fat32_open(filename, "r");
if (!fh) {
cli_write("Error: Cannot open file\n");
return;
}
// Read and display file
char buffer[4096];
int bytes_read;
while ((bytes_read = fat32_read(fh, buffer, sizeof(buffer))) > 0) {
for (int j = 0; j < bytes_read; j++) {
cli_putchar(buffer[j]);
}
}
fat32_close(fh);
}

View File

@@ -0,0 +1,18 @@
#include "cli_utils.h"
void cli_cmd_help(char *args) {
(void)args;
cli_write("Available commands:\n");
cli_write(" HELP - Display this help message\n");
cli_write(" DATE - Display current date and time\n");
cli_write(" CLEAR - Clear the screen\n");
cli_write(" ABOUT - System info\n");
cli_write(" MATH - math <op> <a> <b> (e.g. math + 1 2)\n");
cli_write(" MAN - Show user manual (interactive)\n");
cli_write(" LICENSE - Show license (interactive)\n");
cli_write(" UPTIME - System uptime\n");
cli_write(" BEEP - Make a sound\n");
cli_write(" COWSAY - cowsay <msg>\n");
cli_write(" REBOOT - Reboot system\n");
cli_write(" SHUTDOWN- Shutdown system\n");
}

View File

@@ -0,0 +1,21 @@
#include "cli_utils.h"
// Forward declaration from cmd.c
extern void pager_wrap_content(const char **lines, int count);
extern void pager_set_mode(void);
const char* license_pages[] = {
" GNU GENERAL PUBLIC LICENSE",
" Version 3, 29 June 2007",
" Copyright (C) 2024-2026 boreddevnl",
"",
" (License text abbreviated for build size. See https://www.gnu.org/licenses/gpl-3.0.txt)",
"--- End of License ---"
};
const int license_num_lines = sizeof(license_pages) / sizeof(char*);
void cli_cmd_license(char *args) {
(void)args;
pager_wrap_content(license_pages, license_num_lines);
pager_set_mode();
}

46
src/kernel/cli_apps/man.c Normal file
View File

@@ -0,0 +1,46 @@
#include "cli_utils.h"
// Forward declaration from cmd.c
extern void pager_wrap_content(const char **lines, int count);
extern void pager_set_mode(void);
const char* manual_pages[] = {
"BrewKernel User Manual",
"----------------------",
"",
"Welcome to the BrewKernel.",
"",
"== Features ==",
"* Ramdisk-based Filesystem: A simple in-memory filesystem.",
"* VGA Text Mode Driver: Full control over text/colors.",
"* PS/2 Keyboard Driver: Handles key presses.",
"* Simple CLI: A basic shell.",
"",
"== Available Commands ==",
"HELP: Displays a short list of available commands.",
"MAN: Shows this detailed user manual.",
"ABOUT: Displays information about the kernel.",
"MATH: A simple calculator.",
"DATE: Displays the current date and time.",
"TXTEDIT: A simple text editor with file path support.",
" USAGE: txtedit <filename>",
" EXAMPLES:",
" txtedit file.txt (relative path in current directory)",
" txtedit /file.txt (absolute path in root)",
" txtedit /docs/note.txt (absolute path with subdirectories)",
" FEATURES: Create/Edit files, Save (to RAM), Navigation.",
"CLEAR: Clears the entire screen.",
"EXIT: Exits the CLI mode.",
"LICENSE: Displays the full GNU GPL v3.",
"COWSAY: Moo!",
"UPTIME: Shows how long the system has been running.",
"BEEP: Makes a beep sound.",
"--- End of Manual ---"
};
const int manual_num_lines = sizeof(manual_pages) / sizeof(char*);
void cli_cmd_man(char *args) {
(void)args;
pager_wrap_content(manual_pages, manual_num_lines);
pager_set_mode();
}

View File

@@ -0,0 +1,34 @@
#include "cli_utils.h"
void cli_cmd_math(char *args) {
while (*args == ' ') args++;
if (!*args) {
cli_write("Usage: math <op> <n1> <n2>\n");
return;
}
char op = *args;
args++;
while (*args == ' ') args++;
char *end = args;
while (*end && *end != ' ') end++;
int saved = *end;
*end = 0;
int n1 = cli_atoi(args);
if (saved) *end = saved;
args = end;
while (*args == ' ') args++;
int n2 = cli_atoi(args);
int res = 0;
switch(op) {
case '+': res = n1 + n2; break;
case '-': res = n1 - n2; break;
case '*': res = n1 * n2; break;
case '/': if(n2!=0) res = n1/n2; else { cli_write("Div by zero\n"); return; } break;
default: cli_write("Invalid op.\n"); return;
}
cli_write("Result: "); cli_write_int(res); cli_write("\n");
}

View File

@@ -0,0 +1,143 @@
#include "cli_utils.h"
#include "../memory_manager.h"
#define MAX_TEST_ALLOCS 64
static void *test_allocs[MAX_TEST_ALLOCS];
static int test_alloc_count = 0;
void cli_cmd_malloc(char *args) {
if (!args || !args[0]) {
cli_write("Usage: malloc <size_in_kb>\n");
cli_write("Example: malloc 10\n");
return;
}
// Parse size in KB
int size_kb = cli_atoi(args);
if (size_kb <= 0 || size_kb > 1024) {
cli_write("Invalid size. Use 1-1024 KB\n");
return;
}
size_t size = size_kb * 1024;
void *ptr = kmalloc(size);
if (ptr == NULL) {
cli_write("Allocation failed!\n");
return;
}
// Track allocation for later freeing
if (test_alloc_count < MAX_TEST_ALLOCS) {
test_allocs[test_alloc_count++] = ptr;
}
cli_write("Allocated ");
cli_write_int(size_kb);
cli_write("KB at address 0x");
cli_write_int((uintptr_t)ptr / 1024);
cli_write("\n");
cli_write("Test allocation index: ");
cli_write_int(test_alloc_count - 1);
cli_write("\n");
memory_print_stats();
}
void cli_cmd_free_mem(char *args) {
if (!args || !args[0]) {
cli_write("Usage: freemem <index>\n");
cli_write("Specify the allocation index from malloc output\n");
return;
}
int idx = cli_atoi(args);
if (idx < 0 || idx >= test_alloc_count) {
cli_write("Invalid index. Must be 0-");
cli_write_int(test_alloc_count - 1);
cli_write("\n");
return;
}
void *ptr = test_allocs[idx];
if (ptr == NULL) {
cli_write("Allocation at index ");
cli_write_int(idx);
cli_write(" is NULL\n");
return;
}
kfree(ptr);
test_allocs[idx] = NULL;
cli_write("Freed allocation at index ");
cli_write_int(idx);
cli_write("\n");
memory_print_stats();
}
void cli_cmd_memblock(char *args) {
(void)args;
cli_write("Detailed block information:\n");
memory_print_detailed();
}
void cli_cmd_memvalid(char *args) {
(void)args;
cli_write("Validating memory integrity...\n");
memory_validate();
}
void cli_cmd_memtest(char *args) {
(void)args;
cli_write("\n=== MEMORY STRESS TEST ===\n");
// Allocate multiple blocks
cli_write("Allocating 10 blocks of 256KB each...\n");
void *test_ptrs[10];
for (int i = 0; i < 10; i++) {
test_ptrs[i] = kmalloc(256 * 1024);
if (test_ptrs[i] == NULL) {
cli_write("Allocation ");
cli_write_int(i);
cli_write(" failed\n");
// Free previous allocations
for (int j = 0; j < i; j++) {
kfree(test_ptrs[j]);
}
return;
}
cli_write("Allocated block ");
cli_write_int(i);
cli_write("\n");
}
memory_print_stats();
// Free every other block (create fragmentation)
cli_write("\nFreeing alternate blocks to create fragmentation...\n");
for (int i = 0; i < 10; i += 2) {
kfree(test_ptrs[i]);
cli_write("Freed block ");
cli_write_int(i);
cli_write("\n");
}
memory_print_stats();
// Free remaining blocks
cli_write("\nFreeing remaining blocks...\n");
for (int i = 1; i < 10; i += 2) {
kfree(test_ptrs[i]);
}
memory_print_stats();
cli_write("=== TEST COMPLETE ===\n\n");
}

View File

@@ -0,0 +1,9 @@
#include "cli_utils.h"
#include "../memory_manager.h"
void cli_cmd_meminfo(char *args) {
(void)args;
// Print memory statistics
memory_print_stats();
}

View File

@@ -0,0 +1,14 @@
#include "cli_utils.h"
// Forward declaration from cmd.c
extern void cli_cmd_beep(char *args);
void cli_cmd_readtheman(char *args) {
(void)args;
cli_write("\nYou read the manual? NERD. you know what?\n");
cli_write("Fuck you.\n");
for(int i=0; i<3; i++) {
cli_cmd_beep(NULL);
cli_delay(1000000);
}
}

View File

@@ -0,0 +1,11 @@
#include "cli_utils.h"
#include "io.h"
void cli_cmd_reboot(char *args) {
(void)args;
cli_write("Rebooting...\n");
cli_delay(10000000);
while ((inb(0x64) & 2) != 0) cli_delay(1000);
outb(0x64, 0xFE);
asm volatile ("int $0x3");
}

View File

@@ -0,0 +1,11 @@
#include "cli_utils.h"
#include "io.h"
void cli_cmd_shutdown(char *args) {
(void)args;
cli_write("Shutting down...\n");
cli_delay(10000000);
outb(0x64, 0xFE);
outw(0x604, 0x2000);
outw(0xB004, 0x2000);
}

View File

@@ -0,0 +1,61 @@
#include "cli_utils.h"
#include "fat32.h"
#include "wm.h"
// Forward declarations from editor.h and wm.c
extern void editor_open_file(const char *filename);
extern void editor_init(void);
extern Window win_editor;
extern Window win_explorer;
extern Window win_cmd;
extern Window win_notepad;
extern Window win_calculator;
void cli_cmd_txtedit(char *args) {
// Parse the file path argument
char filepath[256];
int i = 0;
// Skip leading whitespace
while (args && args[i] && (args[i] == ' ' || args[i] == '\t')) {
i++;
}
// Extract filepath
int j = 0;
while (args && args[i] && args[i] != ' ' && args[i] != '\t' && j < 255) {
filepath[j++] = args[i++];
}
filepath[j] = 0;
// If no filepath provided, create a new empty file
if (j == 0) {
cli_write("Usage: txtedit <filename>\n");
cli_write("Example: txtedit myfile.txt\n");
cli_write(" txtedit /document.txt\n");
return;
}
// Normalize the path (handles relative and absolute paths)
char normalized_path[256];
fat32_normalize_path(filepath, normalized_path);
// Open the file in the GUI editor
editor_open_file(normalized_path);
// Make editor window visible and focused, bring to front
win_editor.visible = true;
win_editor.focused = true;
// Calculate max z_index to bring window to front
int max_z = 0;
if (win_explorer.z_index > max_z) max_z = win_explorer.z_index;
if (win_cmd.z_index > max_z) max_z = win_cmd.z_index;
if (win_notepad.z_index > max_z) max_z = win_notepad.z_index;
if (win_calculator.z_index > max_z) max_z = win_calculator.z_index;
win_editor.z_index = max_z + 1;
cli_write("Opening: ");
cli_write(normalized_path);
cli_write("\n");
}

View File

@@ -0,0 +1,26 @@
#include "cli_utils.h"
// Forward declarations from cmd.c
extern void rtc_get_datetime(int *y, int *m, int *d, int *h, int *min, int *s);
extern int boot_time_init;
extern int boot_year, boot_month, boot_day, boot_hour, boot_min, boot_sec;
void cli_cmd_uptime(char *args) {
(void)args;
int y, m, d, h, min, s;
rtc_get_datetime(&y, &m, &d, &h, &min, &s);
int start_sec = boot_hour * 3600 + boot_min * 60 + boot_sec;
int curr_sec = h * 3600 + min * 60 + s;
if (curr_sec < start_sec) curr_sec += 24 * 3600;
int diff = curr_sec - start_sec;
int up_h = diff / 3600;
int up_m = (diff % 3600) / 60;
int up_s = diff % 60;
cli_write("Uptime: ");
cli_write_int(up_h); cli_write("h ");
cli_write_int(up_m); cli_write("m ");
cli_write_int(up_s); cli_write("s\n");
}

View File

@@ -0,0 +1,81 @@
/*
* Brew Kernel
* Copyright (C) 2024-2026 boreddevnl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef BEEP_H
#define BEEP_H
#include "../print.h"
#include "../io.h"
// Forward declaration of brewing function from main.c
void brewing(int iterations);
void beep_command() {
brew_str("\n");
brew_str("BEEP!");
// Add a 500ms delay before the beep
brewing(5000000);
// Set the PIT to the desired frequency (1000 Hz for high pitch)
outb(0x43, 0xB6); // Command byte: channel 2, mode 3, binary
int frequency = 1000;
int divisor = 1193180 / frequency;
outb(0x42, divisor & 0xFF);
outb(0x42, (divisor >> 8) & 0xFF);
// Turn speaker on
outb(0x61, inb(0x61) | 0x03);
// Keep the sound for 1 second
brewing(10000000);
// Turn speaker off
outb(0x61, inb(0x61) & 0xFC);
brewing(1000000);
outb(0x61, inb(0x61) | 0x03);
// Keep the sound for 1 second
brewing(50000000);
// Turn speaker off
outb(0x61, inb(0x61) & 0xFC);
// Turn speaker on
outb(0x61, inb(0x61) | 0x03);
// Keep the sound for 1 second
brewing(10000000);
// Turn speaker off
outb(0x61, inb(0x61) & 0xFC);
brewing(1000000);
outb(0x61, inb(0x61) | 0x03);
// Keep the sound for 1 second
brewing(50000000);
// Turn speaker off
outb(0x61, inb(0x61) & 0xFC);
}
#endif

View File

@@ -0,0 +1,33 @@
/*
* Brew Kernel
* Copyright (C) 2024-2026 boreddevnl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef APPS_BLIND_H
#define APPS_BLIND_H
#include "print.h"
static void blindme() {
print_set_color(PRINT_INDEX_0, PRINT_INDEX_15);
print_clear();
brew_str("Woah.. is this heaven?\n");
brew_str("no.\n");
brew_str("This isn't TempleOS you fucking retard.\n");
}
#endif // APPS_BLIND_H

View File

@@ -0,0 +1,291 @@
/*
* Brew Kernel
* Copyright (C) 2024-2026 boreddevnl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef APPS_CALC_H
#define APPS_CALC_H
#include "print.h"
#include "keyboard.h"
extern int buffer_pos;
// Calculator state structure
struct {
char display[64]; // Display buffer for calculation
int display_len;
long current_val;
long prev_val;
char operation;
int cursor_row; // 0-3 for numpad rows
int cursor_col; // 0-3 for numpad cols
int has_operation;
} calc_state;
// Redraw the calculator UI
static void calc_redraw_ui() {
// Clear screen by printing newlines (but fewer to avoid deadspace)
for (int i = 0; i < 24; i++) {
brew_str("\n");
}
// Display header
brew_str("===== CALCULATOR =====\n");
brew_str("Display: ");
brew_str(calc_state.display);
brew_str("\n\n");
// Draw numpad with cursor
const char* buttons[4][4] = {
{"7", "8", "9", "/"},
{"4", "5", "6", "*"},
{"1", "2", "3", "-"},
{"0", ".", "=", "+"}
};
for (int row = 0; row < 4; row++) {
brew_str(" ");
for (int col = 0; col < 4; col++) {
if (row == calc_state.cursor_row && col == calc_state.cursor_col) {
brew_str("[");
brew_str(buttons[row][col]);
brew_str("]");
} else {
brew_str(" ");
brew_str(buttons[row][col]);
brew_str(" ");
}
brew_str(" ");
}
brew_str("\n");
}
brew_str("\nNavigate: Arrow Keys | Select: Enter | Clear: C | Quit: Q/ESC\n");
}
// Get button at current cursor position
static const char* calc_get_button() {
const char* buttons[4][4] = {
{"7", "8", "9", "/"},
{"4", "5", "6", "*"},
{"1", "2", "3", "-"},
{"0", ".", "=", "+"}
};
return buttons[calc_state.cursor_row][calc_state.cursor_col];
}
// Append character to display
static void calc_append_to_display(const char* text) {
int i = 0;
while (text[i] && calc_state.display_len < 63) {
calc_state.display[calc_state.display_len++] = text[i++];
}
calc_state.display[calc_state.display_len] = '\0';
}
// Parse and calculate result
static void calc_perform_calculation() {
if (calc_state.display_len == 0) return;
// Parse the current display value
long val = 0;
int i = 0;
int is_negative = 0;
if (calc_state.display[0] == '-') {
is_negative = 1;
i = 1;
}
while (i < calc_state.display_len && calc_state.display[i] != '.') {
if (calc_state.display[i] >= '0' && calc_state.display[i] <= '9') {
val = val * 10 + (calc_state.display[i] - '0');
}
i++;
}
if (is_negative) val = -val;
long result = val;
if (calc_state.has_operation) {
switch (calc_state.operation) {
case '+': result = calc_state.prev_val + val; break;
case '-': result = calc_state.prev_val - val; break;
case '*': result = calc_state.prev_val * val; break;
case '/':
if (val != 0) result = calc_state.prev_val / val;
else {
calc_state.display_len = 0;
calc_append_to_display("ERROR");
return;
}
break;
}
}
// Convert result back to string
calc_state.display_len = 0;
if (result < 0) {
calc_append_to_display("-");
result = -result;
}
if (result == 0) {
calc_append_to_display("0");
} else {
char temp[32];
int len = 0;
long temp_val = result;
while (temp_val > 0) {
temp[len++] = '0' + (temp_val % 10);
temp_val /= 10;
}
// Reverse and append
for (int j = len - 1; j >= 0; j--) {
char c[2] = {temp[j], '\0'};
calc_append_to_display(c);
}
}
calc_state.prev_val = result;
}
// Main calculator command
static void calc_cmd() {
// Initialize calculator state
calc_state.display[0] = '\0';
calc_state.display_len = 0;
calc_state.current_val = 0;
calc_state.prev_val = 0;
calc_state.operation = '\0';
calc_state.cursor_row = 0;
calc_state.cursor_col = 0;
calc_state.has_operation = 0;
calc_redraw_ui();
buffer_pos = 0;
while (1) {
if (check_keyboard()) {
unsigned char scan_code = read_scan_code();
// Handle arrow keys for navigation
if (scan_code == 0x48) { // UP arrow
if (calc_state.cursor_row > 0) {
calc_state.cursor_row--;
calc_redraw_ui();
}
continue;
} else if (scan_code == 0x50) { // DOWN arrow
if (calc_state.cursor_row < 3) {
calc_state.cursor_row++;
calc_redraw_ui();
}
continue;
} else if (scan_code == 0x4B) { // LEFT arrow
if (calc_state.cursor_col > 0) {
calc_state.cursor_col--;
calc_redraw_ui();
}
continue;
} else if (scan_code == 0x4D) { // RIGHT arrow
if (calc_state.cursor_col < 3) {
calc_state.cursor_col++;
calc_redraw_ui();
}
continue;
}
char ascii_char = scan_code_to_ascii(scan_code);
// Handle C key to clear
if (ascii_char == 'c' || ascii_char == 'C') {
calc_state.display[0] = '\0';
calc_state.display_len = 0;
calc_state.current_val = 0;
calc_state.prev_val = 0;
calc_state.operation = '\0';
calc_state.has_operation = 0;
calc_redraw_ui();
continue;
}
// Handle Q key or ESC to quit
if (ascii_char == 'q' || ascii_char == 'Q' || scan_code == 0x01) {
brew_str("\n");
return;
}
// Handle Enter to select button
if (ascii_char == '\n' || scan_code == 0x1C) {
const char* button = calc_get_button();
if (button[0] >= '0' && button[0] <= '9') {
// Number button
calc_append_to_display(button);
calc_redraw_ui();
} else if (button[0] == '.') {
// Decimal point
int has_dot = 0;
for (int i = 0; i < calc_state.display_len; i++) {
if (calc_state.display[i] == '.') {
has_dot = 1;
break;
}
}
if (!has_dot && calc_state.display_len > 0) {
calc_append_to_display(".");
calc_redraw_ui();
}
} else if (button[0] == '=' || button[0] == '+' ||
button[0] == '-' || button[0] == '*' || button[0] == '/') {
if (button[0] == '=') {
if (calc_state.has_operation && calc_state.display_len > 0) {
calc_perform_calculation();
calc_state.operation = '\0';
calc_state.has_operation = 0;
calc_redraw_ui();
}
} else {
// Operation button pressed
if (calc_state.display_len > 0) {
// If we already have an operation pending, calculate first
if (calc_state.has_operation) {
calc_perform_calculation();
} else {
// First operation: save display value to prev_val
calc_perform_calculation();
}
// Set new operation
calc_state.operation = button[0];
calc_state.has_operation = 1;
calc_state.display_len = 0;
calc_state.display[0] = '\0';
calc_redraw_ui();
}
}
continue;
}
}
}
}
}
#endif // APPS_CALC_H

View File

@@ -0,0 +1,109 @@
/*
* Brew Kernel Cowsay Utility
* Copyright (C) 2024-2026 boreddevnl
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//
// Implementation of cowsay from GNU/LINUX for Brew Kernel.
// Props to Tony Monroe for the original concept.
// One of these days we WONT port it to Windows. :3
//
#ifndef APPS_COWSAY_H
#define APPS_COWSAY_H
#include "print.h"
// Function to calculate string length since we don't have string.h
static int brew_strlen(const char* str) {
int len = 0;
while (str[len]) len++;
return len;
}
// String comparison for parsing command
static int strncmp_kernel(const char* s1, const char* s2, int n) {
while (n-- > 0) {
if (*s1 != *s2) {
return (*s1 - *s2);
}
if (*s1 == '\0') {
return 0;
}
s1++;
s2++;
}
return 0;
}
// Function to find where the command arguments start
static const char* find_args(const char* cmd) {
while (*cmd && *cmd != ' ') cmd++;
while (*cmd == ' ') cmd++;
return cmd;
}
// Function to draw the top border of the speech bubble
static void draw_top_border(int width) {
brew_str(" ");
for (int i = 0; i < width + 2; i++) {
brew_str("_");
}
brew_str("\n");
}
// Function to draw the bottom border of the speech bubble
static void draw_bottom_border(int width) {
brew_str(" ");
for (int i = 0; i < width + 2; i++) {
brew_str("-");
}
brew_str("\n");
}
// Main cowsay function
static void cowsay(const char* message) {
int len = brew_strlen(message);
// Draw speech bubble
brew_str("\n"); // Start with a newline for better spacing
draw_top_border(len);
brew_str("< ");
brew_str(message);
brew_str(" >\n");
draw_bottom_border(len);
// Draw the cow
brew_str(" \\ ^__^\n");
brew_str(" \\ (oo)\\_______\n");
brew_str(" (__)\\ )\\/\\\n");
brew_str(" ||----w |\n");
brew_str(" || ||\n\n");
}
// Entry point for the cowsay command
static void display_cowsay(const char* command) {
const char* message;
if (!command || brew_strlen(command) == 0) {
message = "Brew!";
} else {
message = find_args(command);
if (brew_strlen(message) == 0) {
message = "Brew!";
}
}
cowsay(message);
}
#endif // APPS_COWSAY_H

View File

@@ -0,0 +1,196 @@
/*
* Brew Kernel
* Copyright (C) 2024-2026 boreddevnl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef DATE_H
#define DATE_H
#include "print.h"
#include "keyboard.h"
#include "rtc.h"
#include "timezones.h"
// String comparison function for kernel
static int strcmp_kernel_date(const char *s1, const char *s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}
// Simple delay function
static inline void brewing_date(int iterations) {
for (volatile int i = 0; i < iterations; i++) {
__asm__ __volatile__("nop");
}
}
// --- Timezone Selection ---
static void select_timezone_for_continent_date(const char* continent, int* timezone_offset_h, int* timezone_offset_m) {
int selected_timezone = 0;
int needs_redraw = 1;
while (1) {
if (needs_redraw) {
print_clear();
brew_str("Select a timezone:\n");
int current_timezone = 0;
for (int i = 0; i < num_timezones; i++) {
if (strcmp_kernel_date(timezones[i].continent, continent) == 0) {
if (current_timezone == selected_timezone) {
print_set_color(PRINT_INDEX_0, PRINT_INDEX_7);
}
brew_str(timezones[i].name);
brew_str("\n");
print_set_color(PRINT_INDEX_7, PRINT_INDEX_0);
current_timezone++;
}
}
needs_redraw = 0;
}
while (!check_keyboard()) { /* Do nothing */ }
unsigned char sc = read_scan_code();
if (sc == 0x48) { // Up Arrow
if (selected_timezone > 0) {
selected_timezone--;
needs_redraw = 1;
}
} else if (sc == 0x50) { // Down Arrow
int max_timezone = 0;
for (int i = 0; i < num_timezones; i++) {
if (strcmp_kernel_date(timezones[i].continent, continent) == 0) {
max_timezone++;
}
}
if (selected_timezone < max_timezone - 1) {
selected_timezone++;
needs_redraw = 1;
}
} else if (scan_code_to_ascii(sc) == '\n' || scan_code_to_ascii(sc) == '\r') {
int current_timezone = 0;
for (int i = 0; i < num_timezones; i++) {
if (strcmp_kernel_date(timezones[i].continent, continent) == 0) {
if (current_timezone == selected_timezone) {
*timezone_offset_h = timezones[i].offset_h; // DST
*timezone_offset_m = timezones[i].offset_m;
return;
}
current_timezone++;
}
}
}
brewing_date(10000000);
}
}
static void select_continent_date(int* timezone_offset_h, int* timezone_offset_m) {
const char* continents[] = {"North America", "South America", "Europe", "Asia", "Oceania", "Africa"};
int num_continents = sizeof(continents) / sizeof(char*);
int selected_continent = 0;
int needs_redraw = 1;
while (1) {
if (needs_redraw) {
print_clear();
brew_str("Select a continent:\n");
for (int i = 0; i < num_continents; i++) {
if (i == selected_continent) {
print_set_color(PRINT_INDEX_0, PRINT_INDEX_7);
}
brew_str(continents[i]);
brew_str("\n");
print_set_color(PRINT_INDEX_7, PRINT_INDEX_0);
}
needs_redraw = 0;
}
while (!check_keyboard()) { /* Do nothing */ }
unsigned char sc = read_scan_code();
if (sc == 0x48) { // Up Arrow
if (selected_continent > 0) {
selected_continent--;
needs_redraw = 1;
}
} else if (sc == 0x50) { // Down Arrow
if (selected_continent < num_continents - 1) {
selected_continent++;
needs_redraw = 1;
}
} else if (scan_code_to_ascii(sc) == '\n' || scan_code_to_ascii(sc) == '\r') {
select_timezone_for_continent_date(continents[selected_continent], timezone_offset_h, timezone_offset_m);
return;
}
brewing_date(10000000);
}
}
// Function to print a zero-padded integer
static void brew_int_padded_date(int n) {
if (n >= 0 && n < 10) {
print_char('0');
}
brew_int(n);
}
static void date_command(int* timezone_offset_h, int* timezone_offset_m) {
select_continent_date(timezone_offset_h, timezone_offset_m);
int year, month, day, hour, minute, second;
get_datetime(&year, &month, &day, &hour, &minute, &second);
// Apply timezone offset
hour += *timezone_offset_h;
minute += *timezone_offset_m;
// Handle minute overflow/underflow
if (minute >= 60) {
hour++;
minute -= 60;
} else if (minute < 0) {
hour--;
minute += 60;
}
// Handle hour overflow/underflow
if (hour >= 24) {
day++;
hour -= 24;
} else if (hour < 0) {
day--;
hour += 24;
}
// Note: This doesn't handle day/month/year wrapping correctly. It's a simplified implementation.
brew_str("\nCurrent Date and Time:\n");
brew_int(year);
brew_str("-");
brew_int_padded_date(month);
brew_str("-");
brew_int_padded_date(day);
brew_str(" ");
brew_int_padded_date(hour);
brew_str(":");
brew_int_padded_date(minute);
brew_str(":");
brew_int_padded_date(second);
brew_str("\n");
}
#endif // DATE_H

View File

@@ -0,0 +1,54 @@
/*
* Brew Kernel
* Copyright (C) 2024-2026 boreddevnl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef APPS_HELP_H
#define APPS_HELP_H
#include "../print.h"
static void display_help() {
brew_str("\nAvailable commands:\n");
brew_str(" HELP - Display this help message\n");
brew_str(" DATE - Display the current date and time\n");
brew_str(" EXIT - Exit CLI mode and return to regular typing\n");
brew_str(" CLEAR - Clear the screen\n");
brew_str(" ABOUT - Display system information\n");
brew_str(" MATH - Perform basic arithmetic\n");
brew_str(" MAN - Show the detailed user manual\n");
brew_str(" LICENSE - Display the GNU GPLv3 license\n");
brew_str(" UPTIME - Show how long the system has been running\n");
brew_str(" MEMORY - Display memory usage statistics\n");
brew_str(" BEEP - Makes a beep sound using the PC speaker\n");
brew_str(" TXTEDIT - Open the text editor\n");
brew_str(" COWSAY. - MOO!\n");
brew_str(" LS - List files in current directory\n");
brew_str(" CD - Change current directory\n");
brew_str(" PWD - Print working directory\n");
brew_str(" MKDIR - Create one or more directories\n");
brew_str(" RM - Remove a file or empty directory\n");
brew_str(" CAT - Display file contents\n");
brew_str(" TOUCH - Create an empty file\n");
brew_str(" ECHO - Print text (can redirect to file with >)\n");
brew_str(" NETINIT - Initialize network card\n");
brew_str(" NETINFO - Show network status (MAC, IP)\n");
brew_str(" UDPTEST - Start UDP echo server on port 12345 (broken)\n");
brew_str(" UDPSEND - Send UDP packet (UDPSEND <ip> <port> <msg>)\n");
brew_str("\nPipe support:\n");
brew_str(" CAT <file> | UDPSEND <ip> <port> - Send file contents via UDP\n");
}
#endif // APPS_HELP_H

View File

@@ -0,0 +1,738 @@
/*
* Brew Kernel
* Copyright (C) 2024-2026 boreddevnl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef APPS_LICENSE_H
#define APPS_LICENSE_H
#include "print.h"
#include "keyboard.h"
const char* license_pages[] = {
" GNU GENERAL PUBLIC LICENSE",
" Version 3, 29 June 2007",
" Copyright (C) 2024-2026 boreddevnl",
"",
" ",
" Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>",
" Everyone is permitted to copy and distribute verbatim copies",
" of this license document, but changing it is not allowed.",
"",
" Preamble",
"",
" The GNU General Public License is a free, copyleft license for",
"software and other kinds of works.",
"",
" The licenses for most software and other practical works are designed",
"to take away your freedom to share and change the works. By contrast,",
"the GNU General Public License is intended to guarantee your freedom to",
"share and change all versions of a program--to make sure it remains free",
"software for all its users. We, the Free Software Foundation, use the",
"GNU General Public License for most of our software; it applies also to",
"any other work released this way by its authors. You can apply it to",
"your programs, too.",
"",
" When we speak of free software, we are referring to freedom, not",
"price. Our General Public Licenses are designed to make sure that you",
"have the freedom to distribute copies of free software (and charge for",
"them if you wish), that you receive source code or can get it if you",
"want it, that you can change the software or use pieces of it in new",
"free programs, and that you know you can do these things.",
"",
" To protect your rights, we need to prevent others from denying you",
"these rights or asking you to surrender the rights. Therefore, you have",
"certain responsibilities if you distribute copies of the software, or if",
"you modify it: responsibilities to respect the freedom of others.",
"",
" For example, if you distribute copies of such a program, whether",
"gratis or for a fee, you must pass on to the recipients the same",
"freedoms that you received. You must make sure that they, too, receive",
"or can get the source code. And you must show them these terms so they",
"know their rights.",
"",
" Developers that use the GNU GPL protect your rights with two steps:",
"(1) assert copyright on the software, and (2) offer you this License",
"giving you legal permission to copy, distribute and/or modify it.",
"",
" For the developers' and authors' protection, the GPL clearly explains",
"that there is no warranty for this free software. For both users' and",
"authors' sake, the GPL requires that modified versions be marked as",
"changed, so that their problems will not be attributed erroneously to",
"authors of previous versions.",
"",
" Some devices are designed to deny users access to install or run",
"modified versions of the software inside them, although the manufacturer",
"can do so. This is fundamentally incompatible with the aim of",
"protecting users' freedom to change the software. The systematic",
"pattern of such abuse occurs in the area of products for individuals to",
"use, which is precisely where it is most unacceptable. Therefore, we",
"have designed this version of the GPL to prohibit the practice for those",
"products. If such problems arise substantially in other domains, we",
"stand ready to extend this provision to those domains in future versions",
"of the GPL, as needed to protect the freedom of users.",
"",
" Finally, every program is threatened constantly by software patents.",
"States should not allow patents to restrict development and use of",
"software on general-purpose computers, but in those that do, we wish to",
"avoid the special danger that patents applied to a free program could",
"make it effectively proprietary. To prevent this, the GPL assures that",
"patents cannot be used to render the program non-free.",
"",
" The precise terms and conditions for copying, distribution and",
"modification follow.",
"",
" TERMS AND CONDITIONS",
"",
" 0. Definitions.",
"",
" 'This License' refers to version 3 of the GNU General Public License.",
"",
" 'Copyright' also means copyright-like laws that apply to other kinds of",
"works, such as semiconductor masks.",
"",
" 'The Program' refers to any copyrightable work licensed under this",
"License. Each licensee is addressed as 'you'. 'Licensees' and",
"'recipients' may be individuals or organizations.",
"",
" To 'modify' a work means to copy from or adapt all or part of the work",
"in a fashion requiring copyright permission, other than the making of an",
"exact copy. The resulting work is called a 'modified version' of the",
"earlier work or a work 'based on' the earlier work.",
"",
" A 'covered work' means either the unmodified Program or a work based",
"on the Program.",
"",
" To 'propagate' a work means to do anything with it that, without",
"permission, would make you directly or secondarily liable for",
"infringement under applicable copyright law, except executing it on a",
"computer or modifying a private copy. Propagation includes copying,",
"distribution (with or without modification), making available to the",
"public, and in some countries other activities as well.",
"",
" To 'convey' a work means any kind of propagation that enables other",
"parties to make or receive copies. Mere interaction with a user through",
"a computer network, with no transfer of a copy, is not conveying.",
"",
" An interactive user interface displays 'Appropriate Legal Notices'",
"to the extent that it includes a convenient and prominently visible",
"feature that (1) displays an appropriate copyright notice, and (2)",
"tells the user that there is no warranty for the work (except to the",
"extent that warranties are provided), that licensees may convey the",
"work under this License, and how to view a copy of this License. If",
"the interface presents a list of user commands or options, such as a",
"menu, a prominent item in the list meets this criterion.",
"",
" 1. Source Code.",
"",
" The 'source code' for a work means the preferred form of the work",
"for making modifications to it. 'Object code' means any non-source",
"form of a work.",
"",
" A 'Standard Interface' means an interface that either is an official",
"standard defined by a recognized standards body, or, in the case of",
"interfaces specified for a particular programming language, one that",
"is widely used among developers working in that language.",
"",
" The 'System Libraries' of an executable work include anything, other",
"than the work as a whole, that (a) is included in the normal form of",
"packaging a Major Component, but which is not part of that Major",
"Component, and (b) serves only to enable use of the work with that",
"Major Component, or to implement a Standard Interface for which an",
"implementation is available to the public in source code form. A",
"'Major Component', in this context, means a major essential component",
"(kernel, window system, and so on) of the specific operating system",
"(if any) on which the executable work runs, or a compiler used to",
"produce the work, or an object code interpreter used to run it.",
"",
" The 'Corresponding Source' for a work in object code form means all",
"the source code needed to generate, install, and (for an executable",
"work) run the object code and to modify the work, including scripts to",
"control those activities. However, it does not include the work's",
"System Libraries, or general-purpose tools or generally available free",
"programs which are used unmodified in performing those activities but",
"which are not part of the work. For example, Corresponding Source",
"includes interface definition files associated with source files for",
"the work, and the source code for shared libraries and dynamically",
"linked subprograms that the work is specifically designed to require,",
"such as by intimate data communication or control flow between those",
"subprograms and other parts of the work.",
"",
" The Corresponding Source need not include anything that users",
"can regenerate automatically from other parts of the Corresponding",
"Source.",
"",
" The Corresponding Source for a work in source code form is that",
"same work.",
"",
" 2. Basic Permissions.",
"",
" All rights granted under this License are granted for the term of",
"copyright on the Program, and are irrevocable provided the stated",
"conditions are met. This License explicitly affirms your unlimited",
"permission to run the unmodified Program. The output from running a",
"covered work is covered by this License only if the output, given its",
"content, constitutes a covered work. This License acknowledges your",
"rights of fair use or other equivalent, as provided by copyright law.",
"",
" You may make, run and propagate covered works that you do not",
"convey, without conditions so long as your license otherwise remains",
"in force. You may convey covered works to others for the sole purpose",
"of having them make modifications exclusively for you, or provide you",
"with facilities for running those works, provided that you comply with",
"the terms of this License in conveying all material for which you do",
"not control copyright. Those thus making or running the covered works",
"for you must do so exclusively on your behalf, under your direction",
"and control, on terms that prohibit them from making any copies of",
"your copyrighted material outside their relationship with you.",
"",
" Conveying under any other circumstances is permitted solely under",
"the conditions stated below. Sublicensing is not allowed; section 10",
"makes it unnecessary.",
"",
" 3. Protecting Users' Legal Rights From Anti-Circumvention Law.",
"",
" No covered work shall be deemed part of an effective technological",
"measure under any applicable law fulfilling obligations under article",
"11 of the WIPO copyright treaty adopted on 20 December 1996, or",
"similar laws prohibiting or restricting circumvention of such",
"measures.",
"",
" When you convey a covered work, you waive any legal power to forbid",
"circumvention of technological measures to the extent such circumvention",
"is effected by exercising rights under this License with respect to",
"the covered work, and you disclaim any intention to limit operation or",
"modification of the work as a means of enforcing, against the work's",
"users, your or third parties' legal rights to forbid circumvention of",
"technological measures.",
"",
" 4. Conveying Verbatim Copies.",
"",
" You may convey verbatim copies of the Program's source code as you",
"receive it, in any medium, provided that you conspicuously and",
"appropriately publish on each copy an appropriate copyright notice;",
"keep intact all notices stating that this License and any",
"non-permissive terms added in accord with section 7 apply to the code;",
"keep intact all notices of the absence of any warranty; and give all",
"recipients a copy of this License along with the Program.",
"",
" You may charge any price or no price for each copy that you convey,",
"and you may offer support or warranty protection for a fee.",
"",
" 5. Conveying Modified Source Versions.",
"",
" You may convey a work based on the Program, or the modifications to",
"produce it from the Program, in the form of source code under the",
"terms of section 4, provided that you also meet all of these conditions:",
"",
" a) The work must carry prominent notices stating that you modified",
" it, and giving a relevant date.",
"",
" b) The work must carry prominent notices stating that it is",
" released under this License and any conditions added under section",
" 7. This requirement modifies the requirement in section 4 to",
" 'keep intact all notices'.",
"",
" c) You must license the entire work, as a whole, under this",
" License to anyone who comes into possession of a copy. This",
" License will therefore apply, along with any applicable section 7",
" additional terms, to the whole of the work, and all its parts,",
" regardless of how they are packaged. This License gives no",
" permission to license the work in any other way, but it does not",
" invalidate such permission if you have separately received it.",
"",
" d) If the work has interactive user interfaces, each must display",
" Appropriate Legal Notices; however, if the Program has interactive",
" interfaces that do not display Appropriate Legal Notices, your",
" work need not make them do so.",
"",
" A compilation of a covered work with other separate and independent",
"works, which are not by their nature extensions of the covered work,",
"and which are not combined with it such as to form a larger program,",
"in or on a volume of a storage or distribution medium, is called an",
"'aggregate' if the compilation and its resulting copyright are not",
"used to limit the access or legal rights of the compilation's users",
"beyond what the individual works permit. Inclusion of a covered work",
"in an aggregate does not cause this License to apply to the other",
"parts of the aggregate.",
"",
" 6. Conveying Non-Source Forms.",
"",
" You may convey a covered work in object code form under the terms",
"of sections 4 and 5, provided that you also convey the",
"machine-readable Corresponding Source under the terms of this License,",
"in one of these ways:",
"",
" a) Convey the object code in, or embodied in, a physical product",
" (including a physical distribution medium), accompanied by the",
" Corresponding Source fixed on a durable physical medium",
" customarily used for software interchange.",
"",
" b) Convey the object code in, or embodied in, a physical product",
" (including a physical distribution medium), accompanied by a",
" written offer, valid for at least three years and valid for as",
" long as you offer spare parts or customer support for that product",
" model, to give anyone who possesses the object code either (1) a",
" copy of the Corresponding Source for all the software in the",
" product that is covered by this License, on a durable physical",
" medium customarily used for software interchange, for a price no",
" more than your reasonable cost of physically performing this",
" conveying of source, or (2) access to copy the",
" Corresponding Source from a network server at no charge.",
"",
" c) Convey individual copies of the object code with a copy of the",
" written offer to provide the Corresponding Source. This",
" alternative is allowed only occasionally and noncommercially, and",
" only if you received the object code with such an offer, in accord",
" with subsection 6b.",
"",
" d) Convey the object code by offering access from a designated",
" place (gratis or for a charge), and offer equivalent access to the",
" Corresponding Source in the same way through the same place at no",
" further charge. You need not require recipients to copy the",
" Corresponding Source along with the object code. If the place to",
" copy the object code is a network server, the Corresponding Source",
" may be on a different server (operated by you or a third party)",
" that supports equivalent copying facilities, provided you maintain",
" clear directions next to the object code saying where to find the",
" Corresponding Source. Regardless of what server hosts the",
" Corresponding Source, you remain obligated to ensure that it is",
" available for as long as needed to satisfy these requirements.",
"",
" e) Convey the object code using peer-to-peer transmission, provided",
" you inform other peers where the object code and Corresponding",
" Source of the work are being offered to the general public at no",
" charge under subsection 6d.",
"",
" A separable portion of the object code, whose source code is excluded",
"from the Corresponding Source as a System Library, need not be",
"included in conveying the object code work.",
"",
" A 'User Product' is either (1) a 'consumer product', which means any",
"tangible personal property which is normally used for personal, family,",
"or household purposes, or (2) anything designed or sold for incorporation",
"into a dwelling. In determining whether a product is a consumer product,",
"doubtful cases shall be resolved in favor of coverage. For a particular",
"product received by a particular user, 'normally used' refers to a",
"typical or common use of that class of product, regardless of the status",
"of the particular user or of the way in which the particular user",
"actually uses, or expects or is expected to use, the product. A product",
"is a consumer product regardless of whether the product has substantial",
"commercial, industrial or non-consumer uses, unless such uses represent",
"the only significant mode of use of the product.",
"",
" 'Installation Information' for a User Product means any methods,",
"procedures, authorization keys, or other information required to install",
"and execute modified versions of a covered work in that User Product from",
"a modified version of its Corresponding Source. The information must",
"suffice to ensure that the continued functioning of the modified object",
"code is in no case prevented or interfered with solely because",
"modification has been made.",
"",
" If you convey an object code work under this section in, or with, or",
"specifically for use in, a User Product, and the conveying occurs as",
"part of a transaction in which the right of possession and use of the",
"User Product is transferred to the recipient in perpetuity or for a",
"fixed term (regardless of how the transaction is characterized), the",
"Corresponding Source conveyed under this section must be accompanied",
"by the Installation Information. But this requirement does not apply",
"if neither you nor any third party retains the ability to install",
"modified object code on the User Product (for example, the work has",
"been installed in ROM).",
"",
" The requirement to provide Installation Information does not include a",
"requirement to continue to provide support service, warranty, or updates",
"for a work that has been modified or installed by the recipient, or for",
"the User Product in which it has been modified or installed. Access to a",
"network may be denied when the modification itself materially and",
"adversely affects the operation of the network or violates the rules and",
"protocols for communication across the network.",
"",
" Corresponding Source conveyed, and Installation Information provided,",
"in accord with this section must be in a format that is publicly",
"documented (and with an implementation available to the public in",
"source code form), and must require no special password or key for",
"unpacking, reading or copying.",
"",
" 7. Additional Terms.",
"",
" 'Additional permissions' are terms that supplement the terms of this",
"License by making exceptions from one or more of its conditions.",
"Additional permissions that are applicable to the entire Program shall",
"be treated as though they were included in this License, to the extent",
"that they are valid under applicable law. If additional permissions",
"apply only to part of the Program, that part may be used separately",
"under those permissions, but the entire Program remains governed by",
"this License without regard to the additional permissions.",
"",
" When you convey a copy of a covered work, you may at your option",
"remove any additional permissions from that copy, or from any part of",
"it. (Additional permissions may be written to require their own",
"removal in certain cases when you modify the work.) You may place",
"additional permissions on material, added by you to a covered work,",
"for which you have or can give appropriate copyright permission.",
"",
" Notwithstanding any other provision of this License, for material you",
"add to a covered work, you may (if authorized by the copyright holders of",
"that material) supplement the terms of this License with terms:",
"",
" a) Disclaiming warranty or limiting liability differently from the",
" terms of sections 15 and 16 of this License; or",
"",
" b) Requiring preservation of specified reasonable legal notices or",
" author attributions in that material or in the Appropriate Legal",
" Notices displayed by works containing it; or",
"",
" c) Prohibiting misrepresentation of the origin of that material, or",
" requiring that modified versions of such material be marked in",
" reasonable ways as different from the original version; or",
"",
" d) Limiting the use for publicity purposes of names of licensors or",
" authors of the material; or",
"",
" e) Declining to grant rights under trademark law for use of some",
" trade names, trademarks, or service marks; or",
"",
" f) Requiring indemnification of licensors and authors of that",
" material by anyone who conveys the material (or modified versions of",
" it) with contractual assumptions of liability to the recipient, for",
" any liability that these contractual assumptions directly impose on",
" those licensors and authors.",
"",
" All other non-permissive additional terms are considered 'further",
"restrictions' within the meaning of section 10. If the Program as you",
"received it, or any part of it, contains a notice stating that it is",
"governed by this License along with a term that is a further",
"restriction, you may remove that term. If a license document contains",
"a further restriction but permits relicensing or conveying under this",
"License, you may add to a covered work material governed by the terms",
"of that license document, provided that the further restriction does",
"not survive such relicensing or conveying.",
"",
" If you add terms to a covered work in accord with this section, you",
"must place, in the relevant source files, a statement of the",
"additional terms that apply to those files, or a notice indicating",
"where to find the applicable terms.",
"",
" Additional terms, permissive or non-permissive, may be stated in the",
"form of a separately written license, or stated as exceptions;",
"the above requirements apply either way.",
"",
" 8. Termination.",
"",
" You may not propagate or modify a covered work except as expressly",
"provided under this License. Any attempt otherwise to propagate or",
"modify it is void, and will automatically terminate your rights under",
"this License (including any patent licenses granted under the third",
"paragraph of section 11).",
"",
" However, if you cease all violation of this License, then your",
"license from a particular copyright holder is reinstated (a)",
"provisionally, unless and until the copyright holder explicitly and",
"finally terminates your license, and (b) permanently, if the copyright",
"holder fails to notify you of the violation by some reasonable means",
"prior to 60 days after the cessation.",
"",
" Moreover, your license from a particular copyright holder is",
"reinstated permanently if the copyright holder notifies you of the",
"violation by some reasonable means, this is the first time you have",
"received notice of violation of this License (for any work) from that",
"copyright holder, and you cure the violation prior to 30 days after",
"your receipt of the notice.",
"",
" Termination of your rights under this section does not terminate the",
"licenses of parties who have received copies or rights from you under",
"this License. If your rights have been terminated and not permanently",
"reinstated, you do not qualify to receive new licenses for the same",
"material under section 10.",
"",
" 9. Acceptance Not Required for Having Copies.",
"",
" You are not required to accept this License in order to receive or",
"run a copy of the Program. Ancillary propagation of a covered work",
"occurring solely as a consequence of using peer-to-peer transmission",
"to receive a copy likewise does not require acceptance. However,",
"nothing other than this License grants you permission to propagate or",
"modify any covered work. These actions infringe copyright if you do",
"not accept this License. Therefore, by modifying or propagating a",
"covered work, you indicate your acceptance of this License to do so.",
"",
" 10. Automatic Licensing of Downstream Recipients.",
"",
" Each time you convey a covered work, the recipient automatically",
"receives a license from the original licensors, to run, modify and",
"propagate that work, subject to this License. You are not responsible",
"for enforcing compliance by third parties with this License.",
"",
" An 'entity transaction' is a transaction transferring control of an",
"organization, or substantially all assets of one, or subdividing an",
"organization, or merging organizations. If propagation of a covered",
"work results from an entity transaction, each party to that",
"transaction who receives a copy of the work also receives whatever",
"licenses to the work the party's predecessor in interest had or could",
"give under the previous paragraph, plus a right to possession of the",
"Corresponding Source of the work from the predecessor in interest, if",
"the predecessor has it or can get it with reasonable efforts.",
"",
" You may not impose any further restrictions on the exercise of the",
"rights granted or affirmed under this License. For example, you may",
"not impose a license fee, royalty, or other charge for exercise of",
"rights granted under this License, and you may not initiate litigation",
"(including a cross-claim or counterclaim in a lawsuit) alleging that",
"any patent claim is infringed by making, using, selling, offering for",
"sale, or importing the Program or any portion of it.",
"",
" 11. Patents.",
"",
" A 'contributor' is a copyright holder who authorizes use under this",
"License of the Program or a work on which the Program is based. The",
"work thus licensed is called the contributor's 'contributor version'.",
"",
" A contributor's 'essential patent claims' are all patent claims",
"owned or controlled by the contributor, whether already acquired or",
"hereafter acquired, that would be infringed by some manner, permitted",
"by this License, of making, using, or selling its contributor version,",
"but do not include claims that would be infringed only as a",
"consequence of further modification of the contributor version. For",
"purposes of this definition, 'control' includes the right to grant",
"patent sublicenses in a manner consistent with the requirements of",
"this License.",
"",
" Each contributor grants you a non-exclusive, worldwide, royalty-free",
"patent license under the contributor's essential patent claims, to",
"make, use, sell, offer for sale, import and otherwise run, modify and",
"propagate the contents of its contributor version.",
"",
" In the following three paragraphs, a 'patent license' is any express",
"agreement or commitment, however denominated, not to enforce a patent",
"(such as an express permission to practice a patent or covenant not to",
"sue for patent infringement). To 'grant' such a patent license to a",
"party means to make such an agreement or commitment not to enforce a",
"patent against the party.",
"",
" If you convey a covered work, knowingly relying on a patent license,",
"and the Corresponding Source of the work is not available for anyone",
"to copy, free of charge and under the terms of this License, through a",
"publicly available network server or other readily accessible means,",
"then you must either (1) cause the Corresponding Source to be so",
"available, or (2) arrange to deprive yourself of the benefit of the",
"patent license for this particular work, or (3) arrange, in a manner",
"consistent with the requirements of this License, to extend the patent",
"license to downstream recipients. 'Knowingly relying' means you have",
"actual knowledge that, but for the patent license, your conveying the",
"covered work in a country, or your recipient's use of the covered work",
"in a country, would infringe one or more identifiable patents in that",
"country that you have reason to believe are valid.",
"",
" If, pursuant to or in connection with a single transaction or",
"arrangement, you convey, or propagate by procuring conveyance of, a",
"covered work, and grant a patent license to some of the parties",
"receiving the covered work authorizing them to use, propagate, modify",
"or convey a specific copy of the covered work, then the patent license",
"you grant is automatically extended to all recipients of the covered",
"work and works based on it.",
"",
" A patent license is 'discriminatory' if it does not include within",
"the scope of its coverage, prohibits the exercise of, or is",
"conditioned on the non-exercise of one or more of the rights that are",
"specifically granted under this License. You may not convey a covered",
"work if you are a party to an arrangement with a third party that is",
"in the business of distributing software, under which you make payment",
"to the third party based on the extent of your activity of conveying",
"the work, and under which the third party grants, to any of the",
"parties who would receive the covered work from you, a discriminatory",
"patent license (a) in connection with copies of the covered work",
"conveyed by you (or copies made from those copies), or (b) primarily",
"for and in connection with specific products or compilations that",
"contain the covered work, unless you entered into that arrangement,",
"or that patent license was granted, prior to 28 March 2007.",
"",
" Nothing in this License shall be construed as excluding or limiting",
"any implied license or other defenses to infringement that may",
"otherwise be available to you under applicable patent law.",
"",
" 12. No Surrender of Others' Freedom.",
"",
" If conditions are imposed on you (whether by court order, agreement or",
"otherwise) that contradict the conditions of this License, they do not",
"excuse you from the conditions of this License. If you cannot convey a",
"covered work so as to satisfy simultaneously your obligations under this",
"License and any other pertinent obligations, then as a consequence you may",
"not convey it at all. For example, if you agree to terms that obligate you",
"to collect a royalty for further conveying from those to whom you convey",
"the Program, the only way you could satisfy both those terms and this",
"License would be to refrain entirely from conveying the Program.",
"",
" 13. Use with the GNU Affero General Public License.",
"",
" Notwithstanding any other provision of this License, you have",
"permission to link or combine any covered work with a work licensed",
"under version 3 of the GNU Affero General Public License into a single",
"combined work, and to convey the resulting work. The terms of this",
"License will continue to apply to the part which is the covered work,",
"but the special requirements of the GNU Affero General Public License,",
"section 13, concerning interaction through a network will apply to the",
"combination as such.",
"",
" 14. Revised Versions of this License.",
"",
" The Free Software Foundation may publish revised and/or new versions of",
"the GNU General Public License from time to time. Such new versions will",
"be similar in spirit to the present version, but may differ in detail to",
"address new problems or concerns.",
"",
" Each version is given a distinguishing version number. If the",
"Program specifies that a certain numbered version of the GNU General",
"Public License 'or any later version' applies to it, you have the",
"option of following the terms and conditions either of that numbered",
"version or of any later version published by the Free Software",
"Foundation. If the Program does not specify a version number of the",
"GNU General Public License, you may choose any version ever published",
"by the Free Software Foundation.",
"",
" If the Program specifies that a proxy can decide which future",
"versions of the GNU General Public License can be used, that proxy's",
"public statement of acceptance of a version permanently authorizes you",
"to choose that version for the Program.",
"",
" Later license versions may give you additional or different",
"permissions. However, no additional obligations are imposed on any",
"author or copyright holder as a result of your choosing to follow a",
"later version.",
"",
" 15. Disclaimer of Warranty.",
"",
" THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY",
"APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT",
"HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM 'AS IS' WITHOUT WARRANTY",
"OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,",
"THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR",
"PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM",
"IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF",
"ALL NECESSARY SERVICING, REPAIR OR CORRECTION.",
"",
" 16. Limitation of Liability.",
"",
" IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING",
"WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS",
"THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY",
"GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE",
"USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF",
"DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD",
"PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),",
"EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF",
"SUCH DAMAGES.",
"",
" 17. Interpretation of Sections 15 and 16.",
"",
" If the disclaimer of warranty and limitation of liability provided",
"above cannot be given local legal effect according to their terms,",
"reviewing courts shall apply local law that most closely approximates",
"an absolute waiver of all civil liability in connection with the",
"Program, unless a warranty or assumption of liability accompanies a",
"copy of the Program in return for a fee.",
"",
" END OF TERMS AND CONDITIONS",
"",
" How to Apply These Terms to Your New Programs",
"",
" If you develop a new program, and you want it to be of the greatest",
"possible use to the public, the best way to achieve this is to make it",
"free software which everyone can redistribute and change under these terms.",
"",
" To do so, attach the following notices to the program. It is safest",
"to attach them to the start of each source file to most effectively",
"state the exclusion of warranty; and each file should have at least",
"the 'copyright' line and a pointer to where the full notice is found.",
"",
" <one line to give the program's name and a brief idea of what it does.>",
" Copyright (C) <year> <name of author>",
"",
" This program is free software: you can redistribute it and/or modify",
" it under the terms of the GNU General Public License as published by",
" the Free Software Foundation, either version 3 of the License, or",
" (at your option) any later version.",
"",
" This program is distributed in the hope that it will be useful,",
" but WITHOUT ANY WARRANTY; without even the implied warranty of",
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the",
" GNU General Public License for more details.",
"",
" You should have received a copy of the GNU General Public License",
" along with this program. If not, see <https://www.gnu.org/licenses/>.",
"",
"Also add information on how to contact you by electronic and paper mail.",
"",
" If the program does terminal interaction, make it output a short",
"notice like this when it starts in an interactive mode:",
"",
" <program> Copyright (C) <year> <name of author>",
" This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.",
" This is free software, and you are welcome to redistribute it",
" under certain conditions; type `show c' for details.",
"",
"The hypothetical commands `show w' and `show c' should show the appropriate",
"parts of the General Public License. Of course, your program's commands",
"might be different; for a GUI interface, you would use an 'about box'.",
"",
" You should also get your employer (if you work as a programmer) or school,",
"if any, to sign a 'copyright disclaimer' for the program, if necessary.",
"For more information on this, and how to apply and follow the GNU GPL, see",
"<https://www.gnu.org/licenses/>.",
"",
" The GNU General Public License does not permit incorporating your program",
"into proprietary programs. If your program is a subroutine library, you",
"may consider it more useful to permit linking proprietary applications with",
"the library. If this is what you want to do, use the GNU Lesser General",
"Public License instead of this License. But first, please read",
"<https://www.gnu.org/licenses/why-not-lgpl.html>.",
"--- End of License ---"
};
const int license_num_lines = sizeof(license_pages) / sizeof(char*);
static void show_license() {
int top_line = 0;
int needs_redraw = 1;
while (1) {
if (needs_redraw) {
print_clear();
print_set_color(PRINT_INDEX_7, PRINT_INDEX_0);
for (int i = 0; i < 24 && (top_line + i) < license_num_lines; i++) {
brew_str(license_pages[top_line + i]);
brew_str("\n");
}
print_set_color(PRINT_INDEX_15, PRINT_INDEX_9);
brew_str("-- (Up/Down to scroll, 'q' to quit) --");
print_set_color(PRINT_INDEX_7, PRINT_INDEX_0);
needs_redraw = 0;
}
// Wait for a key press before doing anything
while (!check_keyboard()) { /* Do nothing */ }
unsigned char sc = read_scan_code(); // Now read the key
if (sc == 0x48 && top_line > 0) { top_line--; needs_redraw = 1; } // Up Arrow
else if (sc == 0x50 && (top_line + 24) < license_num_lines) { top_line++; needs_redraw = 1; } // Down Arrow
else if (scan_code_to_ascii(sc) == 'q') {
print_set_color(PRINT_INDEX_7, PRINT_INDEX_0);
break;
}
brewing(10000000); // Delay to handle keyboard typematic rate (key-repeat)
}
}
#endif // APPS_MAN_H

View File

@@ -0,0 +1,156 @@
/*
* Brew Kernel
* Copyright (C) 2024-2026 boreddevnl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef APPS_MAN_H
#define APPS_MAN_H
#include "print.h"
#include "keyboard.h"
const char* manual_pages[] = {
"BrewKernel User Manual",
"----------------------",
"",
"Welcome to the BrewKernel, a simple hobby operating system kernel designed",
"for x86_64 architecture. This manual provides an overview of the system,",
"its features, and available commands.",
"",
"== System Overview ==",
"BrewKernel boots into a VGA text mode display (80x25 characters). It",
"initializes a custom color palette and provides basic keyboard input",
"handling. The primary user interface is a simple command-line",
"interface (CLI).",
"",
"== Features ==",
"* Ramdisk-based Filesystem: A simple in-memory filesystem supporting",
" files and directories.",
"* VGA Text Mode Driver: Full control over text and background colors.",
"* PS/2 Keyboard Driver: Handles key presses, including modifier keys",
" like Shift.",
"* Simple CLI: A basic shell to interact with the kernel.",
"* Command History (sort of): The last entered command remains in the",
" buffer but is not yet a full history feature.",
"",
"== How to Use the CLI ==",
"Upon boot, you can type 'CLI' and press Enter to start the command-line",
"interface. Once in the CLI, you can type commands followed by Enter.",
"Commands are case-insensitive.",
"",
"== Special Keys ==",
"* Up/down arrow keys, scrolls through command history. (CLI mode only)",
"",
"== File System ==",
"The BrewKernel includes a simple ramdisk-based filesystem. You can create,",
"read, write, and list files and directories using the following commands:",
"This filesystem will NOT save to disk and only saves to RAM.",
"This filesystem is UNIX like, using '/' as the directory separator.",
"Commands like ls, cd, mkdir work with absolute and relative paths.",
"",
"== Brew Language==",
"== This version of brewkernel contains a simple interpreter for the",
"== brew programming language. Currently the only implemented function is",
"== brew_str, which prints a string to the screen. More features will be",
"== added in future versions. Brew files have the extension .brew",
"== you can run a brew file using the command: brewer >filename.brew<",
"== Available Commands ==",
"HELP: Displays a short list of available commands.",
"LS [path]: Lists files in the specified directory or current directory if",
" no path is given.",
"",
"CD [path]: Changes the current directory to 'path'.",
"",
"PWD: Prints the current working directory path.",
"",
"MKDIR [path]: Creates a new directory at the specified path.",
"",
"MAN: Shows this detailed user manual. Use UP/DOWN arrow keys to",
" scroll and 'q' to quit the manual viewer.",
"",
"ABOUT: Displays information about the kernel, including version, build",
" date, architecture, and compiler.",
"",
"MATH:",
"A simple calculator for basic arithmetic operations",
"(add, subtract, multiply, divide) on integers.",
"",
"DATE: Displays the current date and time, with an option to select your",
" timezone.",
"",
"TXTEDIT: A simple text editor. Features:",
" - Create and edit multiple text files",
" - Files are preserved between editor sessions (until reboot)",
" - Navigate with arrow keys",
" - Save/load files with custom names",
" - ESC to exit (with save prompt)",
"USAGE: txtedit >filename< or:",
"txtedit and choose name on save",
"",
"IREADTHEMANUAL: Wow. You actually read the manual. Run this command",
" for a special surprise!",
"",
"CLEAR: Clears the entire screen and moves the cursor to the top-left.",
"",
"EXIT: Exits the CLI mode and returns to the initial kernel screen.",
"",
"LICENSE: Displays the full GNU General Public License v3, under which",
" BrewKernel is distributed. Use UP/DOWN to scroll, 'q' to quit.",
"",
"COWSAY: Moo! Displays a cow saying a message. Usage: COWSAY [message]",
" Inspired by GNU/LINUX",
"",
"UPTIME: Shows how long the system has been running since boot.",
"BEEP: Makes a beep sound using the PC speaker.",
"--- End of Manual ---"
};
const int manual_num_lines = sizeof(manual_pages) / sizeof(char*);
static void show_manual() {
int top_line = 0;
int needs_redraw = 1;
while (1) {
if (needs_redraw) {
print_clear();
print_set_color(PRINT_INDEX_7, PRINT_INDEX_0);
for (int i = 0; i < 24 && (top_line + i) < manual_num_lines; i++) {
brew_str(manual_pages[top_line + i]);
brew_str("\n");
}
print_set_color(PRINT_INDEX_15, PRINT_INDEX_9);
brew_str("-- (Up/Down to scroll, 'q' to quit) --");
print_set_color(PRINT_INDEX_7, PRINT_INDEX_0);
needs_redraw = 0;
}
// Wait for a key press before doing anything
while (!check_keyboard()) { /* Do nothing */ }
unsigned char sc = read_scan_code(); // Now read the key
if (sc == 0x48 && top_line > 0) { top_line--; needs_redraw = 1; } // Up Arrow
else if (sc == 0x50 && (top_line + 24) < manual_num_lines) { top_line++; needs_redraw = 1; } // Down Arrow
else if (scan_code_to_ascii(sc) == 'q') {
print_set_color(PRINT_INDEX_7, PRINT_INDEX_0);
break;
}
brewing(10000000); // Delay to handle keyboard typematic rate (key-repeat)
}
}
#endif // APPS_MAN_H

View File

@@ -0,0 +1,131 @@
/*
* Brew Kernel
* Copyright (C) 2024-2026 boreddevnl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef APPS_MATH_H
#define APPS_MATH_H
#include "print.h"
#include "keyboard.h"
extern int buffer_pos;
static void math_cmd() {
brew_str("\nMath Calculator\n");
brew_str("Choose operation:\n");
brew_str("1. Addition (+)\n");
brew_str("2. Subtraction (-)\n");
brew_str("3. Multiplication (*)\n");
brew_str("4. Division (/)\n");
brew_str("\nEnter operation number: ");
// Clear buffer for new input
buffer_pos = 0;
// Wait for operation choice
while (1) {
if (check_keyboard()) {
unsigned char scan_code = read_scan_code();
char ascii_char = scan_code_to_ascii(scan_code);
if (ascii_char >= '1' && ascii_char <= '4') {
print_char(ascii_char);
char operation = ascii_char;
brew_str("\nEnter first number: ");
// Get first number
buffer_pos = 0;
int first_num = 0;
while (1) {
if (check_keyboard()) {
scan_code = read_scan_code();
ascii_char = scan_code_to_ascii(scan_code);
if (ascii_char >= '0' && ascii_char <= '9') {
print_char(ascii_char);
first_num = first_num * 10 + (ascii_char - '0');
} else if (ascii_char == '\n' || ascii_char == '\r') {
break;
}
}
}
brew_str("\nEnter second number: ");
// Get second number
buffer_pos = 0;
int second_num = 0;
while (1) {
if (check_keyboard()) {
scan_code = read_scan_code();
ascii_char = scan_code_to_ascii(scan_code);
if (ascii_char >= '0' && ascii_char <= '9') {
print_char(ascii_char);
second_num = second_num * 10 + (ascii_char - '0');
} else if (ascii_char == '\n' || ascii_char == '\r') {
break;
}
}
}
brew_str("\nResult: ");
switch (operation) {
case '1':
brew_str("\n");
brew_int(first_num);
brew_str(" + ");
brew_int(second_num);
brew_str(" = ");
brew_int(first_num + second_num);
break;
case '2':
brew_str("\n");
brew_int(first_num);
brew_str(" - ");
brew_int(second_num);
brew_str(" = ");
brew_int(first_num - second_num);
break;
case '3':
brew_str("\n");
brew_int(first_num);
brew_str(" * ");
brew_int(second_num);
brew_str(" = ");
brew_int(first_num * second_num);
break;
case '4':
if (second_num == 0) {
brew_str("Error: Division by zero!\n");
} else {
brew_str("\n");
brew_int(first_num);
brew_str(" / ");
brew_int(second_num);
brew_str(" = ");
brew_int(first_num / second_num);
}
break;
}
brew_str("\n");
break;
}
}
}
}
#endif // APPS_MATH_H

View File

@@ -0,0 +1,52 @@
/*
* Brew Kernel
* Copyright (C) 2024-2026 boreddevnl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef APPS_NERD_H
#define APPS_NERD_H
#include "../io.h"
#include "../print.h"
static void nerd() {
brew_str("\n");
brew_str("You read the manual? NERD. you know what?\n");
brew_str("Fuck you.\n");
brewing(5000000000);
// Set the PIT to the desired frequency (1000 Hz for high pitch)
outb(0x43, 0xB6); // Command byte: channel 2, mode 3, binary
int frequency = 1000;
int divisor = 1193180 / frequency;
outb(0x42, divisor & 0xFF);
outb(0x42, (divisor >> 8) & 0xFF);
outb(0x61, inb(0x61) | 0x03);
for(int i = 0; i < 1000000000000000000; i++) {
print_set_color(PRINT_INDEX_0, PRINT_INDEX_15); // black on white
print_clear();
brewing(500000);
print_set_color(PRINT_INDEX_15, PRINT_INDEX_0); // white on black
print_clear();
brewing(500000);
}
}
#endif // APPS_NERD_H

View File

@@ -0,0 +1,40 @@
/*
* Brew Kernel
* Copyright (C) 2024-2026 boreddevnl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef REBOOT_H
#define REBOOT_H
#include "../print.h"
#include "../io.h"
void brewing(int iterations);
void reboot_command() {
brew_str("\nInitiating system reboot...\n");
brewing(10000000);
while ((inb(0x64) & 2) != 0) {
brewing(1000);
}
outb(0x64, 0xFE);
brewing(50000000);
asm volatile ("lidt %0" : : "m"(*(char*)0));
asm volatile ("int $0x3");
brew_str("WARNING: System reboot failed.\n");
brew_str("Please reset your computer manually.\n");
}
#endif

View File

@@ -0,0 +1,35 @@
/*
* Brew Kernel
* Copyright (C) 2024-2026 boreddevnl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef SHUTDOWN_H
#define SHUTDOWN_H
#include "../print.h"
#include "../io.h"
void brewing(int iterations);
void shutdown_command() {
brew_str("\nInitiating system shutdown...\n");
brewing(10000000);
outw(0xB004, 0x2000);
outb(0x64, 0xFE);
brew_str("WARNING: System shutdown failed.\n");
brew_str("It is now safe to turn off your computer.\n");
}
#endif

View File

@@ -0,0 +1,420 @@
/*
* Brew Kernel
* Copyright (C) 2024-2026 boreddevnl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef TXTEDIT_H
#define TXTEDIT_H
#include "../print.h"
#include "../keyboard.h"
#include "../filesys.h"
#include "../file.h"
#define BUFFER_SIZE 4096
#define MAX_LINES 25
#define MAX_LINE_LENGTH 80
// Helper string functions for txtedit
static int strcmp_txtedit(const char *s1, const char *s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}
static size_t strlen_txtedit(const char* str) {
size_t len = 0;
while (str[len]) len++;
return len;
}
// Buffer for the current file being edited
static char text_buffer[BUFFER_SIZE];
static int buffer_size = 0;
static int cursor_pos = 0;
static int cursor_row = 0;
static int cursor_col = 0;
static int scroll_offset = 0; // Line number at top of visible area
static int has_unsaved_changes = 0; // Flag to track if file has been modified
#define ESC_KEY 0x01
#define ENTER_KEY 0x1C
#define BACKSPACE_KEY 0x0E
#define SCAN_CODE_LEFT_ARROW 0x4B
#define SCAN_CODE_RIGHT_ARROW 0x4D
#define VISIBLE_LINES 23 // Lines 0-22 visible (line 23 is status)
// Status messages
static const char* MSG_SAVED = "Text saved to RAM buffer";
static const char* MSG_LOADED = "Text loaded from RAM buffer";
static const char* MSG_HELP = "ESC:Exit ENTER:NewLine Arrows:Navigate";
static void draw_status_line(const char* msg) {
size_t row, col;
print_get_cursor_pos(&row, &col);
print_set_cursor_pos(24, 0); // Bottom line
print_set_color(PRINT_INDEX_0, PRINT_INDEX_7); // Black on light grey
for(int i = 0; i < 80; i++) print_char(' '); // Clear line
print_set_cursor_pos(24, 0);
brew_str(msg);
print_set_cursor_pos(row, col);
print_set_color(PRINT_INDEX_7, PRINT_INDEX_0); // Reset colors to latte
}
static void get_filename(char* filename) {
int pos = 0;
print_clear();
brew_str("Enter filename (e.g., file.txt): ");
while(1) {
if(check_keyboard()) {
unsigned char scan_code = read_scan_code();
if(scan_code == 0x1C) { // Enter
filename[pos] = '\0';
break;
}
else if(scan_code == 0x0E) { // Backspace
if(pos > 0) {
pos--;
print_backspace();
}
}
else {
char c = scan_code_to_ascii(scan_code);
if(c && pos < FS_MAX_FILENAME - 1) {
filename[pos++] = c;
print_char(c);
}
}
}
}
}
static char current_filename[FS_MAX_FILENAME];
static void save_current_buffer(const char* text_buffer, int buffer_size, bool prompt_for_filename) {
// If we don't have a current filename and need to prompt, ask for one
if (current_filename[0] == '\0' && prompt_for_filename) {
get_filename(current_filename);
} else if (current_filename[0] == '\0') {
draw_status_line("Error: No filename specified");
return;
}
// Create a new file in the current directory
File* file = fs_find_file(current_filename);
if (!file) {
file = fs_create_file(current_filename);
}
if (file && file_write_content(file, text_buffer, buffer_size)) {
draw_status_line("File saved successfully");
} else {
draw_status_line("Error: Could not save file");
}
}
static void calculate_cursor_position() {
// Calculate cursor position based on buffer position
cursor_row = 0;
cursor_col = 0;
int current_line = 0;
for(int i = 0; i < cursor_pos && i < buffer_size; i++) {
if(text_buffer[i] == '\n') {
current_line++;
cursor_col = 0;
} else {
cursor_col++;
}
}
// Adjust display row based on scroll offset
cursor_row = current_line - scroll_offset;
// Auto-scroll up if cursor goes above visible area
if(cursor_row < 0) {
scroll_offset += cursor_row;
cursor_row = 0;
}
// Auto-scroll down if cursor goes below visible area
if(cursor_row >= VISIBLE_LINES) {
scroll_offset += (cursor_row - VISIBLE_LINES + 1);
cursor_row = VISIBLE_LINES - 1;
}
}
static void redraw_screen() {
print_disable_cursor();
print_clear();
print_set_cursor_pos(0, 0);
// Draw the text buffer, starting from scroll_offset
int current_line = 0;
int display_line = 0;
for(int i = 0; i < buffer_size && display_line < VISIBLE_LINES; i++) {
// Skip lines until we reach scroll_offset
if(current_line < scroll_offset) {
if(text_buffer[i] == '\n') {
current_line++;
}
continue;
}
// We're now in the visible area
if(text_buffer[i] == '\n') {
print_char('\n');
current_line++;
display_line++;
if(display_line >= VISIBLE_LINES) break;
} else {
print_char(text_buffer[i]);
}
}
// Calculate cursor position based on cursor_pos in buffer
calculate_cursor_position();
draw_status_line(MSG_HELP);
// Position cursor correctly and re-enable it
print_set_cursor_pos(cursor_row, cursor_col);
print_enable_cursor();
}
static void insert_char(char c) {
if(buffer_size >= BUFFER_SIZE - 1) return;
// Move following text forward
for(int i = buffer_size; i > cursor_pos; i--) {
text_buffer[i] = text_buffer[i-1];
}
text_buffer[cursor_pos] = c;
cursor_pos++;
buffer_size++;
has_unsaved_changes = 1; // Mark file as modified
// Redraw the screen to properly display the inserted character
redraw_screen();
}
static void delete_char() {
if(cursor_pos <= 0) return;
cursor_pos--;
// Move following text backward
for(int i = cursor_pos; i < buffer_size - 1; i++) {
text_buffer[i] = text_buffer[i+1];
}
buffer_size--;
has_unsaved_changes = 1; // Mark file as modified
// Redraw the screen to properly display after deletion
redraw_screen();
}
static void handle_special_key(unsigned char scan_code) {
switch(scan_code) {
case SCAN_CODE_UP_ARROW: {
// Find the start of the current line
int line_start = cursor_pos;
while(line_start > 0 && text_buffer[line_start - 1] != '\n') {
line_start--;
}
// If we're on the first line, can't go up
if(line_start == 0) break;
// Find the start of the previous line
int prev_line_start = line_start - 1;
while(prev_line_start > 0 && text_buffer[prev_line_start - 1] != '\n') {
prev_line_start--;
}
// Calculate target column position
int target_col = cursor_pos - line_start;
// Find the end of the previous line
int prev_line_end = prev_line_start;
while(prev_line_end < line_start - 1 && text_buffer[prev_line_end] != '\n') {
prev_line_end++;
}
// Move cursor to same column in previous line (or end of line if shorter)
int prev_line_length = prev_line_end - prev_line_start;
cursor_pos = prev_line_start + (target_col < prev_line_length ? target_col : prev_line_length);
calculate_cursor_position();
redraw_screen();
break;
}
case SCAN_CODE_DOWN_ARROW: {
// Find the start of the current line
int line_start = cursor_pos;
while(line_start > 0 && text_buffer[line_start - 1] != '\n') {
line_start--;
}
// Find the end of the current line
int line_end = cursor_pos;
while(line_end < buffer_size && text_buffer[line_end] != '\n') {
line_end++;
}
// If we're on the last line, can't go down
if(line_end >= buffer_size) break;
// Move to start of next line
int next_line_start = line_end + 1;
if(next_line_start >= buffer_size) break;
// Find the end of the next line
int next_line_end = next_line_start;
while(next_line_end < buffer_size && text_buffer[next_line_end] != '\n') {
next_line_end++;
}
// Calculate target column position
int target_col = cursor_pos - line_start;
// Move cursor to same column in next line (or end of line if shorter)
int next_line_length = next_line_end - next_line_start;
cursor_pos = next_line_start + (target_col < next_line_length ? target_col : next_line_length);
calculate_cursor_position();
redraw_screen();
break;
}
case SCAN_CODE_LEFT_ARROW:
if(cursor_pos > 0) {
cursor_pos--;
calculate_cursor_position();
redraw_screen();
}
break;
case SCAN_CODE_RIGHT_ARROW:
if(cursor_pos < buffer_size) {
cursor_pos++;
calculate_cursor_position();
redraw_screen();
}
break;
}
}
static void load_file(const char* filename) {
File* file = fs_find_file(filename);
if (!file) {
draw_status_line("Creating new file");
return;
}
size_t size;
const char* content = file_get_content(file, &size);
if (!content || size > BUFFER_SIZE) {
draw_status_line("Error: Could not load file or file too large");
return;
}
// Copy content to buffer
for (size_t i = 0; i < size; i++) {
text_buffer[i] = content[i];
}
buffer_size = size;
cursor_pos = 0; // Start at beginning of file
has_unsaved_changes = 0; // File is not modified after loading
draw_status_line("File loaded successfully");
}
void txtedit_run(const char* filename) {
print_set_color(PRINT_INDEX_7, PRINT_INDEX_0); // Set initial color to latte
// Clear the buffers and filename
for(int i = 0; i < BUFFER_SIZE; i++) {
text_buffer[i] = 0;
}
buffer_size = 0;
cursor_pos = 0;
scroll_offset = 0;
has_unsaved_changes = 0;
current_filename[0] = '\0';
// If filename is provided, store it
if (filename) {
int i;
for (i = 0; filename[i] && i < FS_MAX_FILENAME - 1; i++) {
current_filename[i] = filename[i];
}
current_filename[i] = '\0';
load_file(filename);
}
print_clear();
redraw_screen();
print_enable_cursor();
while(1) {
if(check_keyboard()) {
unsigned char scan_code = read_scan_code();
if(scan_code == ESC_KEY) {
// Only ask to save if there are unsaved changes
if(has_unsaved_changes) {
draw_status_line("Save before exit? (Y/N)");
while(1) {
if(check_keyboard()) {
unsigned char save_scan = read_scan_code();
char save_char = scan_code_to_ascii(save_scan);
if(save_char == 'Y' || save_char == 'y') {
// Only prompt for filename if we don't have one (started with txtedit with no args)
save_current_buffer(text_buffer, buffer_size, current_filename[0] == '\0');
break;
}
else if(save_char == 'N' || save_char == 'n') {
break;
}
}
}
}
break;
}
else if(scan_code == ENTER_KEY) {
insert_char('\n');
}
else if(scan_code == BACKSPACE_KEY) {
delete_char();
}
else if(scan_code == SCAN_CODE_UP_ARROW || scan_code == SCAN_CODE_DOWN_ARROW ||
scan_code == SCAN_CODE_LEFT_ARROW || scan_code == SCAN_CODE_RIGHT_ARROW) {
handle_special_key(scan_code);
}
else {
char c = scan_code_to_ascii(scan_code);
if(c) insert_char(c);
}
}
}
}
#endif // TXTEDIT_H

View File

@@ -0,0 +1,97 @@
/*
* Brew Kernel
* Copyright (C) 2024-2026 boreddevnl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef UPTIME_H
#define UPTIME_H
#include "print.h"
#include "rtc.h"
// Global variable to store boot time
static int boot_time_hour = -1;
static int boot_time_min = -1;
static int boot_time_sec = -1;
// Initialize boot time
void init_uptime(void) {
if (boot_time_hour == -1) {
int year, month, day;
get_datetime(&year, &month, &day, &boot_time_hour, &boot_time_min, &boot_time_sec);
}
}
// Function to print a zero-padded integer
static void brew_int_padded_uptime(int n) {
if (n >= 0 && n < 10) {
print_char('0');
}
brew_int(n);
}
// Calculate time difference considering wraparound
static void calc_time_diff(int start_h, int start_m, int start_s,
int curr_h, int curr_m, int curr_s,
int *diff_h, int *diff_m, int *diff_s) {
int total_start_secs = start_h * 3600 + start_m * 60 + start_s;
int total_curr_secs = curr_h * 3600 + curr_m * 60 + curr_s;
// Handle day wraparound
if (total_curr_secs < total_start_secs) {
total_curr_secs += 24 * 3600; // Add a full day of seconds
}
int diff_secs = total_curr_secs - total_start_secs;
*diff_h = diff_secs / 3600;
diff_secs %= 3600;
*diff_m = diff_secs / 60;
*diff_s = diff_secs % 60;
}
// Display uptime command
static void display_uptime(void) {
init_uptime(); // Ensure boot time is initialized
int curr_hour, curr_min, curr_sec;
int year, month, day;
get_datetime(&year, &month, &day, &curr_hour, &curr_min, &curr_sec);
int up_hours, up_minutes, up_seconds;
calc_time_diff(boot_time_hour, boot_time_min, boot_time_sec,
curr_hour, curr_min, curr_sec,
&up_hours, &up_minutes, &up_seconds);
brew_str("\nSystem uptime: ");
if (up_hours > 0) {
brew_int(up_hours);
brew_str(" hour");
if (up_hours != 1) brew_str("s");
brew_str(" ");
}
if (up_minutes > 0 || up_hours > 0) {
brew_int(up_minutes);
brew_str(" minute");
if (up_minutes != 1) brew_str("s");
brew_str(" ");
}
brew_int(up_seconds);
brew_str(" second");
if (up_seconds != 1) brew_str("s");
brew_str("\n");
}
#endif // UPTIME_H

711
src/kernel/cmd.c Normal file
View File

@@ -0,0 +1,711 @@
#include "cmd.h"
#include "graphics.h"
#include "wm.h"
#include "io.h"
#include "rtc.h"
#include "notepad.h"
#include "calculator.h"
#include "fat32.h"
#include "cli_apps/cli_apps.h"
#include "licensewr.h"
#include <stddef.h>
#include "memory_manager.h"
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#define CMD_COLS 70
#define CMD_ROWS 25
#define LINE_HEIGHT 10
#define CHAR_WIDTH 8
#define PROMPT "> "
#define COLOR_RED 0xFFFF0000
#define TXT_BUFFER_SIZE 4096
#define TXT_VISIBLE_LINES (CMD_ROWS - 2)
#define FS_MAX_FILES 16
#define FS_MAX_FILENAME 64
#define FS_MAX_SIZE 4096
typedef struct {
char name[FS_MAX_FILENAME];
char content[FS_MAX_SIZE];
int size;
bool used;
} RamFile;
static RamFile ram_fs[FS_MAX_FILES];
static void fs_init() {
for (int i = 0; i < FS_MAX_FILES; i++) {
ram_fs[i].used = false;
ram_fs[i].size = 0;
}
}
static RamFile* fs_find(const char *name) {
for (int i = 0; i < FS_MAX_FILES; i++) {
if (ram_fs[i].used) {
// Simple strcmp
const char *a = ram_fs[i].name;
const char *b = name;
bool match = true;
while (*a && *b) {
if (*a != *b) { match = false; break; }
a++; b++;
}
if (match && *a == *b) return &ram_fs[i];
}
}
return NULL;
}
static RamFile* fs_create(const char *name) {
if (fs_find(name)) return fs_find(name);
for (int i = 0; i < FS_MAX_FILES; i++) {
if (!ram_fs[i].used) {
ram_fs[i].used = true;
int j = 0;
while (name[j] && j < FS_MAX_FILENAME - 1) {
ram_fs[i].name[j] = name[j];
j++;
}
ram_fs[i].name[j] = 0;
ram_fs[i].size = 0;
return &ram_fs[i];
}
}
return NULL;
}
// --- Structs ---
typedef struct {
char c;
uint32_t color;
} CharCell;
typedef enum {
MODE_SHELL,
MODE_PAGER
} CmdMode;
// --- State ---
Window win_cmd;
// Shell State
static CharCell screen_buffer[CMD_ROWS][CMD_COLS];
static int cursor_row = 0;
static int cursor_col = 0;
static uint32_t current_color = COLOR_LTGRAY;
// Pager State
static CmdMode current_mode = MODE_SHELL;
static char pager_wrapped_lines[2000][CMD_COLS + 1];
static int pager_total_lines = 0;
static int pager_top_line = 0;
// Boot time for uptime
int boot_time_init = 0;
int boot_year, boot_month, boot_day, boot_hour, boot_min, boot_sec;
// --- Helpers ---
static void cmd_memset(void *dest, int val, size_t len) {
unsigned char *ptr = dest;
while (len-- > 0) *ptr++ = val;
}
static size_t cmd_strlen(const char *str) {
size_t len = 0;
while (str[len]) len++;
return len;
}
static int cmd_strcmp(const char *s1, const char *s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}
static void cmd_strcpy(char *dest, const char *src) {
while (*src) *dest++ = *src++;
*dest = 0;
}
static int cmd_atoi(const char *str) {
int res = 0;
int sign = 1;
if (*str == '-') { sign = -1; str++; }
while (*str >= '0' && *str <= '9') {
res = res * 10 + (*str - '0');
str++;
}
return res * sign;
}
static void brewing(int iterations) {
for (volatile int i = 0; i < iterations; i++) {
__asm__ __volatile__("nop");
}
}
static void itoa(int n, char *buf) {
if (n == 0) {
buf[0] = '0'; buf[1] = 0; return;
}
int i = 0;
int sign = n < 0;
if (sign) n = -n;
while (n > 0) {
buf[i++] = (n % 10) + '0';
n /= 10;
}
if (sign) buf[i++] = '-';
buf[i] = 0;
// Reverse
for (int j = 0; j < i / 2; j++) {
char t = buf[j];
buf[j] = buf[i - 1 - j];
buf[i - 1 - j] = t;
}
}
// Manual and license pages are now in the individual command files
// --- Terminal Emulation ---
static void cmd_scroll_up() {
for (int r = 1; r < CMD_ROWS; r++) {
for (int c = 0; c < CMD_COLS; c++) {
screen_buffer[r - 1][c] = screen_buffer[r][c];
}
}
// Clear bottom row
for (int c = 0; c < CMD_COLS; c++) {
screen_buffer[CMD_ROWS - 1][c].c = ' ';
screen_buffer[CMD_ROWS - 1][c].color = current_color;
}
}
// Public for CLI apps to use
void cmd_putchar(char c) {
if (c == '\n') {
cursor_col = 0;
cursor_row++;
} else if (c == '\b') {
if (cursor_col > 0) {
cursor_col--;
screen_buffer[cursor_row][cursor_col].c = ' ';
}
} else {
if (cursor_col >= CMD_COLS) {
cursor_col = 0;
cursor_row++;
}
if (cursor_row >= CMD_ROWS) {
cmd_scroll_up();
cursor_row = CMD_ROWS - 1;
}
screen_buffer[cursor_row][cursor_col].c = c;
screen_buffer[cursor_row][cursor_col].color = current_color;
cursor_col++;
}
if (cursor_row >= CMD_ROWS) {
cmd_scroll_up();
cursor_row = CMD_ROWS - 1;
}
}
// Public for CLI apps to use
void cmd_write(const char *str) {
while (*str) {
cmd_putchar(*str++);
}
}
// Public for CLI apps to use
void cmd_write_int(int n) {
char buf[32];
itoa(n, buf);
cmd_write(buf);
}
// --- Pager Logic ---
// Public for CLI apps to use - clear the terminal screen
void cmd_screen_clear() {
for(int r=0; r<CMD_ROWS; r++) {
for(int c=0; c<CMD_COLS; c++) {
screen_buffer[r][c].c = ' ';
screen_buffer[r][c].color = COLOR_LTGRAY;
}
}
cursor_row = 0;
cursor_col = 0;
}
// Public for CLI apps to use - exit/close the terminal window
void cmd_window_exit() {
win_cmd.visible = false;
}
// Public for CLI apps to use
void pager_wrap_content(const char **lines, int count) {
pager_total_lines = 0;
pager_top_line = 0;
for (int i = 0; i < count; i++) {
const char *line = lines[i];
int len = cmd_strlen(line);
if (len == 0) {
pager_wrapped_lines[pager_total_lines][0] = 0;
pager_total_lines++;
continue;
}
// Intelligent Word Wrap
int processed = 0;
while (processed < len) {
if (pager_total_lines >= 2000) break;
int remaining = len - processed;
int chunk_len = remaining;
if (chunk_len > CMD_COLS) chunk_len = CMD_COLS;
// If we are cutting a word, backtrack to last space
if (chunk_len < remaining) { // Only check if we are actually wrapping
int split_point = chunk_len;
while (split_point > 0 && line[processed + split_point] != ' ') {
split_point--;
}
if (split_point > 0) {
chunk_len = split_point; // Cut at space
}
// If split_point == 0, the word is longer than the line, so forced split is okay.
}
// Copy chunk
for (int k = 0; k < chunk_len; k++) {
pager_wrapped_lines[pager_total_lines][k] = line[processed + k];
}
pager_wrapped_lines[pager_total_lines][chunk_len] = 0;
pager_total_lines++;
processed += chunk_len;
// Skip the space we just split on
if (processed < len && line[processed] == ' ') {
processed++;
}
}
}
}
// Public for CLI apps to use
void pager_set_mode(void) {
current_mode = MODE_PAGER;
}
// --- Commands (now delegated to cli_apps/) ---
// Command dispatch table
typedef struct {
const char *name;
void (*func)(char *args);
} CommandEntry;
static const CommandEntry commands[] = {
{"HELP", cli_cmd_help},
{"help", cli_cmd_help},
{"DATE", cli_cmd_date},
{"date", cli_cmd_date},
{"CLEAR", cli_cmd_clear},
{"clear", cli_cmd_clear},
{"ABOUT", cli_cmd_about},
{"about", cli_cmd_about},
{"MATH", cli_cmd_math},
{"math", cli_cmd_math},
{"MAN", cli_cmd_man},
{"man", cli_cmd_man},
{"LICENSE", cli_cmd_license},
{"license", cli_cmd_license},
{"TXTEDIT", cli_cmd_txtedit},
{"txtedit", cli_cmd_txtedit},
{"UPTIME", cli_cmd_uptime},
{"uptime", cli_cmd_uptime},
{"BEEP", cli_cmd_beep},
{"beep", cli_cmd_beep},
{"COWSAY", cli_cmd_cowsay},
{"cowsay", cli_cmd_cowsay},
{"REBOOT", cli_cmd_reboot},
{"reboot", cli_cmd_reboot},
{"SHUTDOWN", cli_cmd_shutdown},
{"shutdown", cli_cmd_shutdown},
{"IREADTHEMANUAL", cli_cmd_readtheman},
{"ireadthemanual", cli_cmd_readtheman},
{"BLIND", cli_cmd_blind},
{"blind", cli_cmd_blind},
{"EXIT", cli_cmd_exit},
{"exit", cli_cmd_exit},
// Filesystem Commands
{"CD", cli_cmd_cd},
{"cd", cli_cmd_cd},
{"PWD", cli_cmd_pwd},
{"pwd", cli_cmd_pwd},
{"LS", cli_cmd_ls},
{"ls", cli_cmd_ls},
{"MKDIR", cli_cmd_mkdir},
{"mkdir", cli_cmd_mkdir},
{"RM", cli_cmd_rm},
{"rm", cli_cmd_rm},
{"ECHO", cli_cmd_echo},
{"echo", cli_cmd_echo},
{"CAT", cli_cmd_cat},
{"cat", cli_cmd_cat},
// Memory Management Commands
{"MEMINFO", cli_cmd_meminfo},
{"meminfo", cli_cmd_meminfo},
{"MALLOC", cli_cmd_malloc},
{"malloc", cli_cmd_malloc},
{"FREEMEM", cli_cmd_free_mem},
{"freemem", cli_cmd_free_mem},
{"MEMBLOCK", cli_cmd_memblock},
{"memblock", cli_cmd_memblock},
{"MEMVALID", cli_cmd_memvalid},
{"memvalid", cli_cmd_memvalid},
{"MEMTEST", cli_cmd_memtest},
{"memtest", cli_cmd_memtest},
{NULL, NULL}
};
// --- Dispatcher ---
// Buffer for capturing command output
static char pipe_buffer[4096];
static int pipe_buffer_pos = 0;
static void pipe_capture_write(const char *str) {
while (*str && pipe_buffer_pos < (int)sizeof(pipe_buffer) - 1) {
pipe_buffer[pipe_buffer_pos++] = *str++;
}
}
// Execute a single command
static void cmd_exec_single(char *cmd) {
while (*cmd == ' ') cmd++;
if (!*cmd) return;
// Split cmd and args
char *args = cmd;
while (*args && *args != ' ') args++;
if (*args) {
*args = 0; // Null terminate cmd
args++; // Point to start of args
}
// Use command dispatch table
for (int i = 0; commands[i].name != NULL; i++) {
if (cmd_strcmp(cmd, commands[i].name) == 0) {
commands[i].func(args);
return;
}
}
cmd_write("Unknown command: ");
cmd_write(cmd);
cmd_write("\n");
}
// Execute command with pipe support
static void cmd_exec(char *cmd) {
// Check for pipe operator
char *pipe_ptr = NULL;
for (int i = 0; cmd[i]; i++) {
if (cmd[i] == '|' && (i == 0 || cmd[i-1] != '>' && cmd[i+1] != '>' )) {
pipe_ptr = &cmd[i];
break;
}
}
if (!pipe_ptr) {
// No pipe - execute normally
cmd_exec_single(cmd);
return;
}
// Split into two commands
*pipe_ptr = 0;
char *second_cmd = pipe_ptr + 1;
// Execute first command with output captured
pipe_buffer_pos = 0;
cmd_memset(pipe_buffer, 0, sizeof(pipe_buffer));
FAT32_FileHandle *pipe_file = fat32_open("_pipe_temp.tmp", "w");
if (!pipe_file) {
cmd_write("Error: Cannot create pipe\n");
return;
}
cmd_exec_single(cmd);
fat32_close(pipe_file);
cmd_exec_single(second_cmd);
}
// --- Window Functions ---
static void cmd_paint(Window *win) {
// Draw Window Content Background
int offset_x = win->x + 4;
int offset_y = win->y + 24;
// Fill background
draw_rect(offset_x, offset_y, win->w - 8, win->h - 28, COLOR_BLACK);
int start_y = offset_y + 4;
int start_x = offset_x + 4;
if (current_mode == MODE_PAGER) {
// Draw Pager Content (Wrapped)
for (int i = 0; i < CMD_ROWS && (pager_top_line + i) < pager_total_lines; i++) {
draw_string(start_x, start_y + (i * LINE_HEIGHT), pager_wrapped_lines[pager_top_line + i], COLOR_LTGRAY);
}
// Status Bar
draw_string(start_x, start_y + (CMD_ROWS * LINE_HEIGHT), "-- Press Q to quit --", COLOR_WHITE);
} else {
// Draw Shell Buffer
for (int r = 0; r < CMD_ROWS; r++) {
for (int c = 0; c < CMD_COLS; c++) {
char ch = screen_buffer[r][c].c;
if (ch != 0 && ch != ' ') {
draw_char(start_x + (c * CHAR_WIDTH), start_y + (r * LINE_HEIGHT), ch, screen_buffer[r][c].color);
}
}
}
// Draw Cursor
if (win->focused) {
draw_rect(start_x + (cursor_col * CHAR_WIDTH), start_y + (cursor_row * LINE_HEIGHT) + 8, CHAR_WIDTH, 2, COLOR_WHITE);
}
}
}
static void cmd_key(Window *target, char c) {
(void)target;
if (current_mode == MODE_PAGER) {
if (c == 'q' || c == 'Q') {
current_mode = MODE_SHELL;
} else if (c == 17) { // UP
if (pager_top_line > 0) pager_top_line--;
} else if (c == 18) { // DOWN
if (pager_top_line < pager_total_lines - CMD_ROWS) pager_top_line++;
}
return;
}
// Shell Mode
if (c == '\n') { // Enter
char cmd_buf[CMD_COLS + 1];
int len = 0;
int prompt_len = cmd_strlen(PROMPT);
for (int i = prompt_len; i < CMD_COLS; i++) {
char ch = screen_buffer[cursor_row][i].c;
if (ch == 0) break;
cmd_buf[len++] = ch;
}
while (len > 0 && cmd_buf[len-1] == ' ') len--;
cmd_buf[len] = 0;
cmd_putchar('\n');
cmd_exec(cmd_buf);
cmd_write(PROMPT);
} else if (c == 17) { // UP
// History not implemented
} else if (c == 18) { // DOWN
} else if (c == 19) { // LEFT
if (cursor_col > (int)cmd_strlen(PROMPT)) {
cursor_col--;
}
} else if (c == 20) { // RIGHT
if (cursor_col < CMD_COLS - 1) {
cursor_col++;
}
} else if (c == '\b') { // Backspace
if (cursor_col > (int)cmd_strlen(PROMPT)) {
cursor_col--;
screen_buffer[cursor_row][cursor_col].c = ' ';
}
} else {
if (c >= 32 && c <= 126) {
cmd_putchar(c);
}
}
}
void cmd_reset(void) {
// Reset terminal to fresh state
cmd_screen_clear();
cmd_write("BrewOS Command Prompt\n");
cmd_write(PROMPT);
}
static void create_test_files(void) {
fat32_mkdir("Documents");
fat32_mkdir("Projects");
fat32_mkdir("Documents/Important");
FAT32_FileHandle *fh = fat32_open("README.md", "w");
if (fh) {
const char *content =
"BREW OS 1.01 ALPHA\n"
"==================\n\n"
"BREWKERNEL IS NOW BREWOS!\n\n"
"Brewkernel will from now on be deprecated as its core became too messy.\n"
"I have built a less bloated kernel and wrote a DE above it, which is why\n"
"it is now an OS instead of a kernel.\n\n"
"Brew Kernel is a simple x86_64 hobbyist operating system.\n"
"It features a DE (and WM), a FAT32 filesystem, customizable UI and much much more!\n"
"ramdisk-like filesystem.\n\n"
"FEATURES\n"
"--------\n"
"* Brew WM (Window Manager)\n"
"* FAT32 Filesystem\n"
"* 64-bit long mode support\n"
"* Multiboot2 compliant\n"
"* Text editor and file explorer\n"
"* IDT (Interrupt Descriptor Table)\n"
"* Ability to run on actual x86_64 hardware\n"
"* Command-line interface (CLI)\n\n"
"PREREQUISITES\n"
"-------------\n"
"To build BrewOS, you'll need the following tools installed:\n\n"
"* x86_64 ELF Toolchain (x86_64-elf-gcc, x86_64-elf-ld)\n"
"* NASM (Netwide Assembler)\n"
"* xorriso (for creating bootable ISO images)\n"
"* QEMU (optional, for testing in emulator)\n\n"
"On macOS, install via Homebrew:\n"
" brew install x86_64-elf-binutils x86_64-elf-gcc nasm xorriso qemu\n\n"
"BUILDING\n"
"--------\n"
"Simply run 'make' from the project root:\n\n"
" make\n\n"
"This will:\n"
"1. Download Limine v7.0.0 bootloader files (if not present)\n"
"2. Compile all kernel C sources and assembly files\n"
"3. Link the kernel ELF binary\n"
"4. Generate a bootable ISO image (brewos.iso)\n\n"
"Build output:\n"
"* Compiled object files: build/\n"
"* ISO root filesystem: iso_root/\n"
"* Final ISO image: brewos.iso\n\n"
"RUNNING\n"
"-------\n"
"QEMU EMULATION:\n"
"Run in QEMU with:\n"
" make run\n\n"
"Or manually:\n"
" qemu-system-x86_64 -m 2G -serial stdio -cdrom brewos.iso -boot d\n\n"
"RUNNING ON REAL HARDWARE:\n"
"WARNING: This is at YOUR OWN RISK. This software comes with ZERO warranty\n"
"and may break your system.\n\n"
"1. Create bootable USB using Balena Etcher to flash brewos.iso\n"
"2. Enable legacy (BIOS) boot in your system BIOS/UEFI settings\n"
"3. Disable Secure Boot if needed\n"
"4. Insert USB drive and select it in boot menu during startup\n\n"
"Tested Hardware:\n"
"* HP EliteDesk 705 G4 DM (AMD Ryzen 5 PRO 2400G, Radeon Vega)\n"
"* Lenovo ThinkPad A475 20KL002VMH (AMD Pro A12-8830B, Radeon R7)\n\n"
"PROJECT STRUCTURE\n"
"-----------------\n"
"* src/kernel/ - Main kernel implementation\n"
" - boot.asm - Boot assembly code\n"
" - main.c - Kernel entry point\n"
" - *.c / *.h - Core kernel modules\n"
" - cli_apps/ - Command-line applications\n"
" - wallpaper.ppm - Default desktop wallpaper\n"
"* build/ - Compiled object files (generated during build)\n"
"* iso_root/ - ISO filesystem layout (generated during build)\n"
"* limine/ - Limine bootloader files (downloaded automatically)\n"
"* linker.ld - Linker script for x86_64 ELF\n"
"* limine.cfg - Limine bootloader configuration\n"
"* Makefile - Build configuration and targets\n\n"
"LICENSE\n"
"-------\n"
"Copyright (C) 2024-2026 boreddevnl\n\n"
"This program is free software: you can redistribute it and/or modify\n"
"it under the terms of the GNU General Public License as published by\n"
"the Free Software Foundation, either version 3 of the License, or\n"
"(at your option) any later version.\n\n"
"For full license details, see the LICENSE file in the repository.\n";
fat32_write(fh, (void *)content, cmd_strlen(content));
fat32_close(fh);
}
write_license_file();
fh = fat32_open("Documents/notes.txt", "w");
if (fh) {
const char *content = "My Notes\n\n- First note\n- Second note\n";
fat32_write(fh, (void *)content, 39);
fat32_close(fh);
}
fh = fat32_open("Documents/notes.txt", "w");
if (fh) {
const char *content = "My Notes\n\n- First note\n- Second note\n";
fat32_write(fh, (void *)content, 39);
fat32_close(fh);
}
fh = fat32_open("Projects/project1.txt", "w");
if (fh) {
const char *content = "Project 1\n\nStatus: In Progress\n";
fat32_write(fh, (void *)content, 32);
fat32_close(fh);
}
}
void cmd_init(void) {
fs_init(); // Init RAMFS
fat32_init(); // Init FAT32 filesystem
create_test_files();
win_cmd.title = "Command Prompt";
win_cmd.x = 50;
win_cmd.y = 50;
win_cmd.w = (CMD_COLS * CHAR_WIDTH) + 20;
win_cmd.h = (CMD_ROWS * LINE_HEIGHT) + 40;
win_cmd.visible = false;
win_cmd.focused = false;
win_cmd.z_index = 0;
win_cmd.paint = cmd_paint;
win_cmd.handle_key = cmd_key;
win_cmd.handle_click = NULL;
win_cmd.handle_right_click = NULL;
cmd_reset();
if (!boot_time_init) {
rtc_get_datetime(&boot_year, &boot_month, &boot_day, &boot_hour, &boot_min, &boot_sec);
boot_time_init = 1;
}
}

11
src/kernel/cmd.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef CMD_H
#define CMD_H
#include "wm.h"
extern Window win_cmd;
void cmd_init(void);
void cmd_reset(void);
#endif

329
src/kernel/control_panel.c Normal file
View File

@@ -0,0 +1,329 @@
#include "control_panel.h"
#include "graphics.h"
#include <stddef.h>
#include "wm.h"
Window win_control_panel;
#define COLOR_COFFEE 0xFF6B4423
#define COLOR_TEAL 0xFF008080
#define COLOR_GREEN 0xFF008000
#define COLOR_BLUE_BG 0xFF000080
#define COLOR_PURPLE 0xFF800080
// Control panel state
#define VIEW_MAIN 0
#define VIEW_WALLPAPER 1
static int current_view = VIEW_MAIN;
static char rgb_r[4] = "";
static char rgb_g[4] = "";
static char rgb_b[4] = "";
static int focused_field = -1; // -1=none, 0=R, 1=G, 2=B
static int input_cursor = 0;
static uint32_t parse_rgb_separate(const char *r, const char *g, const char *b) {
int rv = 0, gv = 0, bv = 0;
// Parse R
for (int i = 0; r[i] && i < 3; i++) {
if (r[i] >= '0' && r[i] <= '9') {
rv = rv * 10 + (r[i] - '0');
}
}
// Parse G
for (int i = 0; g[i] && i < 3; i++) {
if (g[i] >= '0' && g[i] <= '9') {
gv = gv * 10 + (g[i] - '0');
}
}
// Parse B
for (int i = 0; b[i] && i < 3; i++) {
if (b[i] >= '0' && b[i] <= '9') {
bv = bv * 10 + (b[i] - '0');
}
}
// Clamp values
if (rv > 255) rv = 255;
if (gv > 255) gv = 255;
if (bv > 255) bv = 255;
return 0xFF000000 | (rv << 16) | (gv << 8) | bv;
}
static void control_panel_paint_main(Window *win) {
int offset_x = win->x + 8;
int offset_y = win->y + 30;
// Draw wallpaper folder icon
// Folder icon
draw_rect(offset_x + 5, offset_y, 15, 6, COLOR_LTGRAY);
draw_rect(offset_x + 5, offset_y, 15, 1, COLOR_BLACK);
draw_rect(offset_x + 5, offset_y, 1, 6, COLOR_BLACK);
draw_rect(offset_x + 19, offset_y, 1, 6, COLOR_BLACK);
draw_rect(offset_x + 5, offset_y + 6, 25, 15, COLOR_LTGRAY);
draw_rect(offset_x + 5, offset_y + 6, 25, 1, COLOR_BLACK);
draw_rect(offset_x + 5, offset_y + 6, 1, 15, COLOR_BLACK);
draw_rect(offset_x + 29, offset_y + 6, 1, 15, COLOR_BLACK);
draw_rect(offset_x + 5, offset_y + 20, 25, 1, COLOR_BLACK);
// Label
draw_string(offset_x + 40, offset_y + 8, "Wallpaper", 0xFF000000);
}
static void control_panel_paint_wallpaper(Window *win) {
int offset_x = win->x + 8;
int offset_y = win->y + 30;
// Back button
draw_string(offset_x, offset_y, "< Back", 0xFF000080);
draw_string(offset_x, offset_y + 25, "Presets:", 0xFF000000);
// Color buttons
int button_y = offset_y + 45;
int button_x = offset_x;
// Coffee button
draw_button(button_x, button_y, 60, 20, "Coffee", false);
draw_rect(button_x + 65, button_y + 5, 20, 10, COLOR_COFFEE);
// Teal button
draw_button(button_x + 100, button_y, 60, 20, "Teal", false);
draw_rect(button_x + 165, button_y + 5, 20, 10, COLOR_TEAL);
// Green button
draw_button(button_x + 200, button_y, 60, 20, "Green", false);
draw_rect(button_x + 265, button_y + 5, 20, 10, COLOR_GREEN);
// Blue button
button_y += 30;
draw_button(button_x, button_y, 60, 20, "Blue", false);
draw_rect(button_x + 65, button_y + 5, 20, 10, COLOR_BLUE_BG);
// Purple button
draw_button(button_x + 100, button_y, 60, 20, "Purple", false);
draw_rect(button_x + 165, button_y + 5, 20, 10, COLOR_PURPLE);
// Custom color section
button_y += 40;
draw_string(offset_x, button_y, "Or something custom", 0xFF000000);
button_y += 20;
// R input box
draw_string(button_x, button_y, "R:", 0xFF000000);
draw_rect(button_x + 25, button_y, 50, 15, 0xFFFFFFFF);
draw_rect(button_x + 25, button_y, 50, 1, COLOR_BLACK);
draw_rect(button_x + 25, button_y, 1, 15, COLOR_BLACK);
draw_rect(button_x + 74, button_y, 1, 15, COLOR_BLACK);
draw_rect(button_x + 25, button_y + 14, 50, 1, COLOR_BLACK);
draw_string(button_x + 30, button_y + 3, rgb_r, (focused_field == 0) ? 0xFFFF0000 : COLOR_BLACK);
if (focused_field == 0) {
// Draw cursor
int cursor_x = button_x + 30 + input_cursor * 8;
draw_rect(cursor_x, button_y + 3, 1, 9, 0xFFFF0000);
}
// G input box
draw_string(button_x + 90, button_y, "G:", 0xFF000000);
draw_rect(button_x + 115, button_y, 50, 15, 0xFFFFFFFF);
draw_rect(button_x + 115, button_y, 50, 1, COLOR_BLACK);
draw_rect(button_x + 115, button_y, 1, 15, COLOR_BLACK);
draw_rect(button_x + 164, button_y, 1, 15, COLOR_BLACK);
draw_rect(button_x + 115, button_y + 14, 50, 1, COLOR_BLACK);
draw_string(button_x + 120, button_y + 3, rgb_g, (focused_field == 1) ? 0xFF00AA00 : COLOR_BLACK);
if (focused_field == 1) {
// Draw cursor
int cursor_x = button_x + 120 + input_cursor * 8;
draw_rect(cursor_x, button_y + 3, 1, 9, 0xFF00AA00);
}
// B input box
draw_string(button_x + 180, button_y, "B:", 0xFF000000);
draw_rect(button_x + 205, button_y, 50, 15, 0xFFFFFFFF);
draw_rect(button_x + 205, button_y, 50, 1, COLOR_BLACK);
draw_rect(button_x + 205, button_y, 1, 15, COLOR_BLACK);
draw_rect(button_x + 254, button_y, 1, 15, COLOR_BLACK);
draw_rect(button_x + 205, button_y + 14, 50, 1, COLOR_BLACK);
draw_string(button_x + 210, button_y + 3, rgb_b, (focused_field == 2) ? 0xFF0000FF : COLOR_BLACK);
if (focused_field == 2) {
// Draw cursor
int cursor_x = button_x + 210 + input_cursor * 8;
draw_rect(cursor_x, button_y + 3, 1, 9, 0xFF0000FF);
}
// Apply button
draw_button(button_x, button_y + 25, 70, 20, "Apply", false);
}
static void control_panel_paint(Window *win) {
if (current_view == VIEW_MAIN) {
control_panel_paint_main(win);
} else if (current_view == VIEW_WALLPAPER) {
control_panel_paint_wallpaper(win);
}
}
static void control_panel_handle_click(Window *win, int x, int y) {
(void)win; // Unused parameter
if (current_view == VIEW_MAIN) {
int offset_x = 8;
int offset_y = 30;
// Check wallpaper folder click
if (x >= offset_x + 5 && x < offset_x + 35 &&
y >= offset_y && y < offset_y + 25) {
current_view = VIEW_WALLPAPER;
}
} else if (current_view == VIEW_WALLPAPER) {
int offset_x = 8;
int offset_y = 30;
int button_y = offset_y + 45;
int button_x = offset_x;
// Back button
if (x >= offset_x && x < offset_x + 40 &&
y >= offset_y && y < offset_y + 15) {
current_view = VIEW_MAIN;
return;
}
// Check Coffee button
if (x >= button_x && x < button_x + 60 && y >= button_y && y < button_y + 20) {
graphics_set_bg_color(COLOR_COFFEE);
return;
}
// Check Teal button
if (x >= button_x + 100 && x < button_x + 160 && y >= button_y && y < button_y + 20) {
graphics_set_bg_color(COLOR_TEAL);
return;
}
// Check Green button
if (x >= button_x + 200 && x < button_x + 260 && y >= button_y && y < button_y + 20) {
graphics_set_bg_color(COLOR_GREEN);
return;
}
// Check Blue button
button_y += 30;
if (x >= button_x && x < button_x + 60 && y >= button_y && y < button_y + 20) {
graphics_set_bg_color(COLOR_BLUE_BG);
return;
}
// Check Purple button
if (x >= button_x + 100 && x < button_x + 160 && y >= button_y && y < button_y + 20) {
graphics_set_bg_color(COLOR_PURPLE);
return;
}
// Custom RGB section
button_y += 40;
button_y += 20;
// Check R input box click
if (x >= button_x + 25 && x < button_x + 75 && y >= button_y && y < button_y + 15) {
if (focused_field != 0) {
rgb_r[0] = '\0'; // Clear when first focused
}
focused_field = 0;
input_cursor = 0;
return;
}
// Check G input box click
if (x >= button_x + 115 && x < button_x + 165 && y >= button_y && y < button_y + 15) {
if (focused_field != 1) {
rgb_g[0] = '\0'; // Clear when first focused
}
focused_field = 1;
input_cursor = 0;
return;
}
// Check B input box click
if (x >= button_x + 205 && x < button_x + 255 && y >= button_y && y < button_y + 15) {
if (focused_field != 2) {
rgb_b[0] = '\0'; // Clear when first focused
}
focused_field = 2;
input_cursor = 0;
return;
}
// Check Apply button
if (x >= button_x && x < button_x + 70 && y >= button_y + 25 && y < button_y + 45) {
graphics_set_bg_color(parse_rgb_separate(rgb_r, rgb_g, rgb_b));
return;
}
}
}
static void control_panel_handle_key(Window *win, char c) {
(void)win; // Unused parameter
if (current_view != VIEW_WALLPAPER) return;
if (focused_field < 0) return; // No field focused
// Get the currently focused field buffer
char *focused_buffer = NULL;
int max_len = 3; // RGB values are 0-255, max 3 digits
if (focused_field == 0) {
focused_buffer = rgb_r;
} else if (focused_field == 1) {
focused_buffer = rgb_g;
} else if (focused_field == 2) {
focused_buffer = rgb_b;
} else {
return;
}
if (c == '\b') { // Backspace
if (input_cursor > 0) {
input_cursor--;
focused_buffer[input_cursor] = '\0';
}
} else if (c >= '0' && c <= '9') { // Digits only
if (input_cursor < max_len) {
focused_buffer[input_cursor] = c;
input_cursor++;
focused_buffer[input_cursor] = '\0';
}
} else if (c == '\t') { // Tab - switch to next field
focused_field = (focused_field + 1) % 3;
input_cursor = 0;
}
}
void control_panel_init(void) {
win_control_panel.title = "Control Panel";
win_control_panel.x = 200;
win_control_panel.y = 150;
win_control_panel.w = 350;
win_control_panel.h = 300;
win_control_panel.visible = false;
win_control_panel.focused = false;
win_control_panel.z_index = 0;
win_control_panel.paint = control_panel_paint;
win_control_panel.handle_key = control_panel_handle_key;
win_control_panel.handle_click = control_panel_handle_click;
win_control_panel.handle_right_click = NULL;
win_control_panel.buf_len = 0;
win_control_panel.cursor_pos = 0;
}
void control_panel_reset(void) {
win_control_panel.focused = false;
current_view = VIEW_MAIN;
focused_field = -1;
input_cursor = 0;
}

View File

@@ -0,0 +1,11 @@
#ifndef CONTROL_PANEL_H
#define CONTROL_PANEL_H
#include "wm.h"
extern Window win_control_panel;
void control_panel_init(void);
void control_panel_reset(void);
#endif

445
src/kernel/editor.c Normal file
View File

@@ -0,0 +1,445 @@
#include "editor.h"
#include "graphics.h"
#include "fat32.h"
#include "wm.h"
#include <stdbool.h>
#include <stddef.h>
// === Text Editor State ===
Window win_editor;
#define EDITOR_MAX_LINES 128
#define EDITOR_MAX_LINE_LEN 256
#define EDITOR_LINE_HEIGHT 16
#define EDITOR_CHAR_WIDTH 8
typedef struct {
char content[EDITOR_MAX_LINE_LEN];
int length;
} EditorLine;
static EditorLine lines[EDITOR_MAX_LINES];
static int line_count = 1;
static int cursor_line = 0;
static int cursor_col = 0;
static int scroll_top = 0;
static char open_filename[256] = "";
static bool file_modified = false;
// === Helper Functions ===
static size_t editor_strlen(const char *str) {
size_t len = 0;
while (str[len]) len++;
return len;
}
static void editor_strcpy(char *dest, const char *src) {
while (*src) *dest++ = *src++;
*dest = 0;
}
static int editor_strcmp(const char *s1, const char *s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}
// === Editor Logic ===
// Forward declaration
static void editor_ensure_cursor_visible(void);
static void editor_clear_all(void) {
for (int i = 0; i < EDITOR_MAX_LINES; i++) {
lines[i].content[0] = 0;
lines[i].length = 0;
}
line_count = 1;
cursor_line = 0;
cursor_col = 0;
scroll_top = 0;
open_filename[0] = 0;
file_modified = false;
}
void editor_open_file(const char *filename) {
editor_clear_all();
editor_strcpy(open_filename, filename);
FAT32_FileHandle *fh = fat32_open(filename, "r");
if (!fh) {
// New file
file_modified = false;
return;
}
// Read file content
char buffer[16384];
int bytes_read = fat32_read(fh, buffer, sizeof(buffer));
fat32_close(fh);
if (bytes_read <= 0) {
file_modified = false;
return;
}
// Parse into lines
int line = 0;
int col = 0;
for (int i = 0; i < bytes_read && line < EDITOR_MAX_LINES; i++) {
char ch = buffer[i];
if (ch == '\n') {
lines[line].content[col] = 0;
lines[line].length = col;
line++;
col = 0;
} else if (ch != '\r') {
if (col < EDITOR_MAX_LINE_LEN - 1) {
lines[line].content[col] = ch;
col++;
}
}
}
if (col > 0) {
lines[line].content[col] = 0;
lines[line].length = col;
line++;
}
line_count = (line > 0) ? line : 1;
file_modified = false;
}
static void editor_save_file(void) {
if (!open_filename[0]) {
// No filename set
return;
}
FAT32_FileHandle *fh = fat32_open(open_filename, "w");
if (!fh) {
return;
}
// Write lines
for (int i = 0; i < line_count; i++) {
fat32_write(fh, lines[i].content, lines[i].length);
fat32_write(fh, "\n", 1);
}
fat32_close(fh);
file_modified = false;
}
// Insert character at cursor position
static void editor_insert_char(char ch) {
if (cursor_line >= EDITOR_MAX_LINES) return;
EditorLine *line = &lines[cursor_line];
if (ch == '\n') {
// Split line - shift all lines below down first
if (line_count >= EDITOR_MAX_LINES) return;
// Shift all lines from cursor_line+1 onwards down by one position
for (int j = line_count; j > cursor_line; j--) {
lines[j] = lines[j - 1];
}
line_count++;
// Clear the new line completely (zero entire buffer)
for (int k = 0; k < EDITOR_MAX_LINE_LEN; k++) {
lines[cursor_line + 1].content[k] = 0;
}
lines[cursor_line + 1].length = 0;
// Now split the current line at cursor position
int current_len = lines[cursor_line].length;
int new_len = current_len - cursor_col;
// Copy the second part to the new line
for (int i = 0; i < new_len; i++) {
lines[cursor_line + 1].content[i] = lines[cursor_line].content[cursor_col + i];
}
lines[cursor_line + 1].content[new_len] = 0;
lines[cursor_line + 1].length = new_len;
// Truncate current line
lines[cursor_line].content[cursor_col] = 0;
lines[cursor_line].length = cursor_col;
cursor_line++;
cursor_col = 0;
} else if (ch == '\b') {
// Backspace
if (cursor_col > 0) {
for (int i = cursor_col - 1; i < line->length; i++) {
line->content[i] = line->content[i + 1];
}
line->length--;
cursor_col--;
} else if (cursor_line > 0) {
// Merge with previous line
EditorLine *prev = &lines[cursor_line - 1];
int merge_point = prev->length;
for (int i = 0; i < line->length; i++) {
if (merge_point + i < EDITOR_MAX_LINE_LEN - 1) {
prev->content[merge_point + i] = line->content[i];
}
}
prev->content[merge_point + line->length] = 0;
prev->length = merge_point + line->length;
// Shift lines up
for (int i = cursor_line; i < line_count - 1; i++) {
lines[i] = lines[i + 1];
}
lines[line_count - 1].length = 0;
lines[line_count - 1].content[0] = 0;
cursor_line--;
cursor_col = merge_point;
line_count--;
}
} else if (ch >= 32 && ch <= 126) {
// Regular character
if (cursor_col < EDITOR_MAX_LINE_LEN - 1) {
// Shift characters right
for (int i = line->length; i > cursor_col; i--) {
line->content[i] = line->content[i - 1];
}
line->content[cursor_col] = ch;
line->length++;
cursor_col++;
}
}
file_modified = true;
editor_ensure_cursor_visible();
}
// Ensure cursor is visible by adjusting scroll position
static void editor_ensure_cursor_visible(void) {
int visible_lines = 22; // Allow ~24 lines to use available window space
// Scroll up if cursor is above visible area
if (cursor_line < scroll_top) {
scroll_top = cursor_line;
}
// Scroll down if cursor is below visible area
if (cursor_line >= scroll_top + visible_lines) {
scroll_top = cursor_line - visible_lines + 1;
}
}
// === Paint Function ===
static void editor_paint(Window *win) {
int offset_x = win->x + 4;
int offset_y = win->y + 24;
int content_width = win->w - 8;
int content_height = win->h - 28;
// Draw filename and save button area at top of content
draw_rect(offset_x, offset_y, content_width, 25, COLOR_GRAY);
draw_string(offset_x + 10, offset_y + 5, "File: ", COLOR_BLACK);
draw_string(offset_x + 55, offset_y + 5, open_filename, COLOR_BLACK);
// Draw save button
draw_button(offset_x + content_width - 80, offset_y + 3, 70, 20, "Save", false);
// Draw modification indicator
if (file_modified) {
draw_string(offset_x + content_width - 200, offset_y + 5, "[Modified]", COLOR_RED);
}
// Fill editor background
draw_rect(offset_x, offset_y + 30, content_width, content_height - 55, COLOR_WHITE);
// Draw line numbers and content
int visible_lines = (content_height - 55) / EDITOR_LINE_HEIGHT;
int max_line = scroll_top + visible_lines;
if (max_line > line_count) max_line = line_count;
for (int i = scroll_top; i < max_line; i++) {
int display_y = offset_y + 35 + (i - scroll_top) * EDITOR_LINE_HEIGHT;
// Draw line number
char line_num_str[16];
int temp = i + 1;
int str_len = 0;
if (temp == 0) {
line_num_str[0] = '0';
str_len = 1;
} else {
while (temp > 0) {
line_num_str[str_len++] = (temp % 10) + '0';
temp /= 10;
}
// Reverse
for (int j = 0; j < str_len / 2; j++) {
char t = line_num_str[j];
line_num_str[j] = line_num_str[str_len - 1 - j];
line_num_str[str_len - 1 - j] = t;
}
}
line_num_str[str_len] = 0;
draw_string(offset_x + 4, display_y, line_num_str, COLOR_DKGRAY);
// Draw line content
draw_string(offset_x + 40, display_y, lines[i].content, COLOR_BLACK);
// Draw cursor if on this line
if (i == cursor_line) {
int cursor_x = offset_x + 40 + (cursor_col * EDITOR_CHAR_WIDTH);
draw_rect(cursor_x, display_y, 2, 10, COLOR_BLACK);
}
}
// Draw status bar at bottom
draw_rect(offset_x, offset_y + content_height - 20, content_width, 20, COLOR_GRAY);
draw_string(offset_x + 10, offset_y + content_height - 15, "Line: ", COLOR_WHITE);
char line_str[32];
int temp = cursor_line + 1;
int idx = 0;
while (temp > 0) {
line_str[idx++] = (temp % 10) + '0';
temp /= 10;
}
for (int j = 0; j < idx / 2; j++) {
char t = line_str[j];
line_str[j] = line_str[idx - 1 - j];
line_str[idx - 1 - j] = t;
}
line_str[idx] = 0;
draw_string(offset_x + 60, offset_y + content_height - 15, line_str, COLOR_WHITE);
draw_string(offset_x + 100, offset_y + content_height - 15, " Col: ", COLOR_WHITE);
char col_str[32];
temp = cursor_col + 1;
idx = 0;
while (temp > 0) {
col_str[idx++] = (temp % 10) + '0';
temp /= 10;
}
for (int j = 0; j < idx / 2; j++) {
char t = col_str[j];
col_str[j] = col_str[idx - 1 - j];
col_str[idx - 1 - j] = t;
}
col_str[idx] = 0;
draw_string(offset_x + 170, offset_y + content_height - 15, col_str, COLOR_WHITE);
}
// === Key Handler ===
static void editor_handle_key(Window *win, char c) {
if (c == 'q' || c == 'Q') {
if (file_modified) {
}
win->visible = false;
return;
}
// Arrow keys - UP
if (c == 17) {
if (cursor_line > 0) {
cursor_line--;
if (cursor_col > (int)lines[cursor_line].length) {
cursor_col = lines[cursor_line].length;
}
if (cursor_line < scroll_top) {
scroll_top = cursor_line;
}
}
return;
}
// Arrow keys - DOWN
if (c == 18) {
if (cursor_line < line_count - 1) {
cursor_line++;
if (cursor_col > (int)lines[cursor_line].length) {
cursor_col = lines[cursor_line].length;
}
int visible_lines = 20;
if (cursor_line >= scroll_top + visible_lines) {
scroll_top = cursor_line - visible_lines + 1;
}
}
return;
}
// Arrow keys - LEFT
if (c == 19) {
if (cursor_col > 0) {
cursor_col--;
} else if (cursor_line > 0) {
cursor_line--;
cursor_col = lines[cursor_line].length;
}
return;
}
// Arrow keys - RIGHT
if (c == 20) {
if (cursor_col < (int)lines[cursor_line].length) {
cursor_col++;
} else if (cursor_line < line_count - 1) {
cursor_line++;
cursor_col = 0;
}
return;
}
// Regular character input
editor_insert_char(c);
}
// === Click Handler ===
static void editor_handle_click(Window *win, int x, int y) {
// x and y are relative to window origin
int content_width = win->w - 8;
// Check save button - position is at (4 + content_width - 80, 24 + 3) = (4 + w - 8 - 80, 27)
// Button dimensions: 70 wide, 20 tall
int button_x = 4 + content_width - 80;
int button_y = 24 + 3;
if (x >= button_x && x < button_x + 70 &&
y >= button_y && y < button_y + 20) {
editor_save_file();
return;
}
}
// === Initialization ===
void editor_init(void) {
win_editor.title = "Text Editor";
win_editor.x = 100;
win_editor.y = 150;
win_editor.w = 700;
win_editor.h = 450;
win_editor.visible = false;
win_editor.focused = false;
win_editor.z_index = 0;
win_editor.paint = editor_paint;
win_editor.handle_key = editor_handle_key;
win_editor.handle_click = editor_handle_click;
win_editor.handle_right_click = NULL;
editor_clear_all();
}

11
src/kernel/editor.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef EDITOR_H
#define EDITOR_H
#include "wm.h"
extern Window win_editor;
void editor_init(void);
void editor_open_file(const char *filename);
#endif

696
src/kernel/explorer.c Normal file
View File

@@ -0,0 +1,696 @@
#include "explorer.h"
#include "graphics.h"
#include "fat32.h"
#include "wm.h"
#include "editor.h"
#include <stdbool.h>
#include <stddef.h>
// === File Explorer State ===
Window win_explorer;
#define EXPLORER_MAX_FILES 64
#define EXPLORER_ITEM_HEIGHT 80
#define EXPLORER_ITEM_WIDTH 120
#define EXPLORER_COLS 3
#define EXPLORER_ROWS 4
#define EXPLORER_PADDING 15
// Dialog states
#define DIALOG_NONE 0
#define DIALOG_CREATE_FILE 1
#define DIALOG_CREATE_FOLDER 2
#define DIALOG_DELETE_CONFIRM 3
#define DIALOG_INPUT_MAX 256
typedef struct {
char name[256];
bool is_directory;
uint32_t size;
} ExplorerItem;
typedef enum {
ACTION_NONE,
ACTION_CREATE_FILE,
ACTION_CREATE_FOLDER,
ACTION_DELETE_FILE,
ACTION_DELETE_FOLDER
} ContextMenuAction;
static ExplorerItem items[EXPLORER_MAX_FILES];
static int item_count = 0;
static int selected_item = -1;
static char current_path[256] = "/";
static int last_clicked_item = -1;
static uint32_t last_click_time = 0;
// Dialog state
static int dialog_state = DIALOG_NONE;
static char dialog_input[DIALOG_INPUT_MAX] = "";
static int dialog_input_cursor = 0;
static char dialog_target_path[256] = ""; // For delete confirmations
static bool dialog_target_is_dir = false; // For delete confirmations
// Dropdown menu state
static bool dropdown_menu_visible = false;
static int dropdown_menu_item_height = 25;
#define DROPDOWN_MENU_WIDTH 120
#define DROPDOWN_MENU_ITEMS 3
// === Helper Functions ===
static size_t explorer_strlen(const char *str);
static void explorer_strcpy(char *dest, const char *src);
static int explorer_strcmp(const char *s1, const char *s2);
static void explorer_strcat(char *dest, const char *src);
static void explorer_load_directory(const char *path);
static size_t explorer_strlen(const char *str) {
size_t len = 0;
while (str[len]) len++;
return len;
}
static void explorer_strcpy(char *dest, const char *src) {
while (*src) *dest++ = *src++;
*dest = 0;
}
static int explorer_strcmp(const char *s1, const char *s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}
static void explorer_strcat(char *dest, const char *src) {
while (*dest) dest++;
explorer_strcpy(dest, src);
}
// === Dialog and File Operations ===
static void dialog_open_create_file(void) {
dialog_state = DIALOG_CREATE_FILE;
dialog_input[0] = 0;
dialog_input_cursor = 0;
}
static void dialog_open_create_folder(void) {
dialog_state = DIALOG_CREATE_FOLDER;
dialog_input[0] = 0;
dialog_input_cursor = 0;
}
static void dialog_open_delete_confirm(int item_idx) {
if (item_idx < 0 || item_idx >= item_count) return;
dialog_state = DIALOG_DELETE_CONFIRM;
dialog_target_is_dir = items[item_idx].is_directory;
// Build full path to target
explorer_strcpy(dialog_target_path, current_path);
if (dialog_target_path[explorer_strlen(dialog_target_path) - 1] != '/') {
explorer_strcat(dialog_target_path, "/");
}
explorer_strcat(dialog_target_path, items[item_idx].name);
}
static void dialog_close(void) {
dialog_state = DIALOG_NONE;
dialog_input[0] = 0;
dialog_input_cursor = 0;
dialog_target_path[0] = 0;
}
static void dialog_confirm_create_file(void) {
if (dialog_input[0] == 0) return;
char full_path[256];
explorer_strcpy(full_path, current_path);
if (full_path[explorer_strlen(full_path) - 1] != '/') {
explorer_strcat(full_path, "/");
}
explorer_strcat(full_path, dialog_input);
// Create empty file
FAT32_FileHandle *file = fat32_open(full_path, "w");
if (file) {
fat32_close(file);
explorer_load_directory(current_path);
}
dialog_close();
}
static void dialog_confirm_create_folder(void) {
if (dialog_input[0] == 0) return;
char full_path[256];
explorer_strcpy(full_path, current_path);
if (full_path[explorer_strlen(full_path) - 1] != '/') {
explorer_strcat(full_path, "/");
}
explorer_strcat(full_path, dialog_input);
// Create directory
if (fat32_mkdir(full_path)) {
explorer_load_directory(current_path);
}
dialog_close();
}
// Recursive delete for directories
static bool explorer_delete_recursive(const char *path) {
if (fat32_is_directory(path)) {
// List contents and delete recursively
FAT32_FileInfo entries[64];
int count = fat32_list_directory(path, entries, 64);
for (int i = 0; i < count; i++) {
char child_path[256];
explorer_strcpy(child_path, path);
if (child_path[explorer_strlen(child_path) - 1] != '/') {
explorer_strcat(child_path, "/");
}
explorer_strcat(child_path, entries[i].name);
if (entries[i].is_directory) {
explorer_delete_recursive(child_path);
} else {
fat32_delete(child_path);
}
}
// Delete the directory itself
return fat32_rmdir(path);
} else {
// Regular file
return fat32_delete(path);
}
}
static void dialog_confirm_delete(void) {
explorer_delete_recursive(dialog_target_path);
explorer_load_directory(current_path);
dialog_close();
}
static void dropdown_menu_toggle(void) {
dropdown_menu_visible = !dropdown_menu_visible;
}
// === Helper Functions (continued)
// === Explorer Logic ===
static void explorer_load_directory(const char *path) {
explorer_strcpy(current_path, path);
FAT32_FileInfo entries[EXPLORER_MAX_FILES];
int count = fat32_list_directory(path, entries, EXPLORER_MAX_FILES);
item_count = 0;
for (int i = 0; i < count && i < EXPLORER_MAX_FILES; i++) {
explorer_strcpy(items[i].name, entries[i].name);
items[i].is_directory = entries[i].is_directory;
items[i].size = entries[i].size;
item_count++;
}
selected_item = -1;
}
static void explorer_navigate_to(const char *dirname) {
char new_path[256];
if (explorer_strcmp(dirname, "..") == 0) {
// Go to parent directory
int len = explorer_strlen(current_path);
int i = len - 1;
// Skip trailing slashes
while (i > 0 && current_path[i] == '/') i--;
// Find last slash
while (i > 0 && current_path[i] != '/') i--;
if (i == 0) {
explorer_strcpy(new_path, "/");
} else {
for (int j = 0; j < i; j++) {
new_path[j] = current_path[j];
}
new_path[i] = 0;
}
} else {
// Go to subdirectory
explorer_strcpy(new_path, current_path);
if (new_path[explorer_strlen(new_path) - 1] != '/') {
explorer_strcat(new_path, "/");
}
explorer_strcat(new_path, dirname);
}
explorer_load_directory(new_path);
}
// Draw a simple file icon
static void explorer_draw_file_icon(int x, int y, bool is_dir) {
if (is_dir) {
// Folder icon - larger
draw_rect(x + 10, y + 10, 30, 5, COLOR_BLUE); // Tab
draw_rect(x + 10, y + 15, 30, 25, COLOR_WHITE); // Main folder
draw_rect(x + 10, y + 15, 2, 25, COLOR_BLACK);
draw_rect(x + 10, y + 15, 30, 2, COLOR_BLACK);
draw_rect(x + 38, y + 15, 2, 25, COLOR_BLACK);
draw_rect(x + 10, y + 38, 30, 2, COLOR_BLACK);
} else {
// Document icon - larger
draw_rect(x + 12, y + 10, 20, 25, COLOR_WHITE);
draw_rect(x + 12, y + 10, 20, 2, COLOR_BLACK);
draw_rect(x + 12, y + 10, 2, 25, COLOR_BLACK);
draw_rect(x + 30, y + 10, 2, 25, COLOR_BLACK);
draw_rect(x + 12, y + 33, 20, 2, COLOR_BLACK);
// Lines on document
draw_rect(x + 15, y + 18, 14, 1, COLOR_DKGRAY);
draw_rect(x + 15, y + 23, 14, 1, COLOR_DKGRAY);
draw_rect(x + 15, y + 28, 14, 1, COLOR_DKGRAY);
}
}
// === Paint Function ===
static void explorer_paint(Window *win) {
int offset_x = win->x + 4;
int offset_y = win->y + 24;
// Fill background
draw_rect(offset_x, offset_y, win->w - 8, win->h - 28, COLOR_LTGRAY);
// Draw path bar
int path_height = 30;
draw_bevel_rect(offset_x + 4, offset_y + 4, win->w - 16, path_height, true);
draw_string(offset_x + 10, offset_y + 10, "Path: ", COLOR_BLACK);
draw_string(offset_x + 50, offset_y + 10, current_path, COLOR_BLACK);
// Draw dropdown menu button (right-aligned, before back button)
int dropdown_btn_x = win->x + win->w - 90;
draw_button(dropdown_btn_x, offset_y + 4, 35, 30, "...", false);
// Draw back button (right-aligned)
draw_button(win->x + win->w - 40, offset_y + 4, 30, 30, "<", false);
// Draw dropdown menu if visible
if (dropdown_menu_visible) {
int menu_x = dropdown_btn_x;
int menu_y = offset_y + 34;
// Draw menu background
draw_rect(menu_x, menu_y, DROPDOWN_MENU_WIDTH, dropdown_menu_item_height * DROPDOWN_MENU_ITEMS, COLOR_LTGRAY);
draw_bevel_rect(menu_x, menu_y, DROPDOWN_MENU_WIDTH, dropdown_menu_item_height * DROPDOWN_MENU_ITEMS, true);
// Draw menu items
draw_string(menu_x + 8, menu_y + 5, "New File", COLOR_BLACK);
draw_string(menu_x + 8, menu_y + dropdown_menu_item_height + 5, "New Folder", COLOR_BLACK);
draw_string(menu_x + 8, menu_y + dropdown_menu_item_height * 2 + 5, "Delete", COLOR_RED);
}
// Draw file list
int content_start_y = offset_y + 40;
for (int i = 0; i < item_count; i++) {
int row = i / EXPLORER_COLS;
int col = i % EXPLORER_COLS;
int item_x = offset_x + 10 + (col * (EXPLORER_ITEM_WIDTH + EXPLORER_PADDING));
int item_y = content_start_y + (row * (EXPLORER_ITEM_HEIGHT + EXPLORER_PADDING));
// Draw item background
uint32_t bg_color = (i == selected_item) ? COLOR_BLUE : COLOR_WHITE;
draw_bevel_rect(item_x, item_y, EXPLORER_ITEM_WIDTH, EXPLORER_ITEM_HEIGHT, false);
draw_rect(item_x + 2, item_y + 2, EXPLORER_ITEM_WIDTH - 4, EXPLORER_ITEM_HEIGHT - 4, bg_color);
// Draw icon (larger area)
explorer_draw_file_icon(item_x + 5, item_y + 5, items[i].is_directory);
// Draw name below icon
uint32_t text_color = (i == selected_item) ? COLOR_WHITE : COLOR_BLACK;
int name_len = explorer_strlen(items[i].name);
int text_x = item_x + 5;
int text_y = item_y + 50;
// Truncate name if too long
char display_name[24];
int copy_len = name_len > 18 ? 18 : name_len;
for (int j = 0; j < copy_len; j++) {
display_name[j] = items[i].name[j];
}
display_name[copy_len] = 0;
draw_string(text_x, text_y, display_name, text_color);
}
// Draw dialogs
if (dialog_state == DIALOG_CREATE_FILE) {
int dlg_x = win->x + win->w / 2 - 150;
int dlg_y = win->y + win->h / 2 - 60;
// Dialog background
draw_rect(dlg_x - 5, dlg_y - 5, 310, 120, COLOR_LTGRAY);
draw_bevel_rect(dlg_x, dlg_y, 300, 110, true);
// Title
draw_string(dlg_x + 10, dlg_y + 10, "Create New File", COLOR_BLACK);
// Input field
draw_bevel_rect(dlg_x + 10, dlg_y + 35, 280, 20, false);
draw_string(dlg_x + 15, dlg_y + 40, dialog_input, COLOR_BLACK);
draw_string(dlg_x + 15 + dialog_input_cursor * 8, dlg_y + 40, "|", COLOR_BLACK);
// Buttons
draw_button(dlg_x + 50, dlg_y + 65, 80, 25, "Create", false);
draw_button(dlg_x + 170, dlg_y + 65, 80, 25, "Cancel", false);
} else if (dialog_state == DIALOG_CREATE_FOLDER) {
int dlg_x = win->x + win->w / 2 - 150;
int dlg_y = win->y + win->h / 2 - 60;
// Dialog background
draw_rect(dlg_x - 5, dlg_y - 5, 310, 120, COLOR_LTGRAY);
draw_bevel_rect(dlg_x, dlg_y, 300, 110, true);
// Title
draw_string(dlg_x + 10, dlg_y + 10, "Create New Folder", COLOR_BLACK);
// Input field
draw_bevel_rect(dlg_x + 10, dlg_y + 35, 280, 20, false);
draw_string(dlg_x + 15, dlg_y + 40, dialog_input, COLOR_BLACK);
draw_string(dlg_x + 15 + dialog_input_cursor * 8, dlg_y + 40, "|", COLOR_BLACK);
// Buttons
draw_button(dlg_x + 50, dlg_y + 65, 80, 25, "Create", false);
draw_button(dlg_x + 170, dlg_y + 65, 80, 25, "Cancel", false);
} else if (dialog_state == DIALOG_DELETE_CONFIRM) {
int dlg_x = win->x + win->w / 2 - 150;
int dlg_y = win->y + win->h / 2 - 60;
// Dialog background
draw_rect(dlg_x - 5, dlg_y - 5, 310, 120, COLOR_LTGRAY);
draw_bevel_rect(dlg_x, dlg_y, 300, 110, true);
// Title
const char *title = dialog_target_is_dir ? "Delete Folder?" : "Delete File?";
draw_string(dlg_x + 10, dlg_y + 10, title, COLOR_BLACK);
// Message
draw_string(dlg_x + 10, dlg_y + 35, "This action cannot be undone.", COLOR_BLACK);
// Buttons
draw_button(dlg_x + 50, dlg_y + 65, 80, 25, "Delete", false);
draw_button(dlg_x + 170, dlg_y + 65, 80, 25, "Cancel", false);
}
}
// === Mouse Handler ===
static void explorer_handle_click(Window *win, int x, int y) {
// Handle dialog clicks first
if (dialog_state == DIALOG_CREATE_FILE || dialog_state == DIALOG_CREATE_FOLDER) {
int dlg_x = win->w / 2 - 150;
int dlg_y = win->h / 2 - 60;
// Create button
if (x >= dlg_x + 50 && x < dlg_x + 130 &&
y >= dlg_y + 65 && y < dlg_y + 90) {
if (dialog_state == DIALOG_CREATE_FILE) {
dialog_confirm_create_file();
} else {
dialog_confirm_create_folder();
}
return;
}
// Cancel button
if (x >= dlg_x + 170 && x < dlg_x + 250 &&
y >= dlg_y + 65 && y < dlg_y + 90) {
dialog_close();
return;
}
// Input field click
if (x >= dlg_x + 10 && x < dlg_x + 290 &&
y >= dlg_y + 35 && y < dlg_y + 55) {
dialog_input_cursor = (x - dlg_x - 15) / 8;
if (dialog_input_cursor > (int)explorer_strlen(dialog_input)) {
dialog_input_cursor = explorer_strlen(dialog_input);
}
return;
}
} else if (dialog_state == DIALOG_DELETE_CONFIRM) {
int dlg_x = win->w / 2 - 150;
int dlg_y = win->h / 2 - 60;
// Delete button
if (x >= dlg_x + 50 && x < dlg_x + 130 &&
y >= dlg_y + 65 && y < dlg_y + 90) {
dialog_confirm_delete();
return;
}
// Cancel button
if (x >= dlg_x + 170 && x < dlg_x + 250 &&
y >= dlg_y + 65 && y < dlg_y + 90) {
dialog_close();
return;
}
}
// Handle dropdown menu clicks
if (dropdown_menu_visible) {
int dropdown_btn_x = win->w - 90; // Window-relative
int menu_y = 58; // Window-relative (offset_y + 34, where offset_y = 24)
// New File
if (x >= dropdown_btn_x && x < dropdown_btn_x + DROPDOWN_MENU_WIDTH &&
y >= menu_y && y < menu_y + dropdown_menu_item_height) {
dropdown_menu_toggle();
dialog_open_create_file();
return;
}
// New Folder
if (x >= dropdown_btn_x && x < dropdown_btn_x + DROPDOWN_MENU_WIDTH &&
y >= menu_y + dropdown_menu_item_height &&
y < menu_y + dropdown_menu_item_height * 2) {
dropdown_menu_toggle();
dialog_open_create_folder();
return;
}
// Delete
if (x >= dropdown_btn_x && x < dropdown_btn_x + DROPDOWN_MENU_WIDTH &&
y >= menu_y + dropdown_menu_item_height * 2 &&
y < menu_y + dropdown_menu_item_height * 3) {
dropdown_menu_toggle();
if (selected_item >= 0) {
dialog_open_delete_confirm(selected_item);
}
return;
}
// Click outside menu closes it
dropdown_menu_toggle();
return;
}
// x, y are already relative to window (0,0 is top-left of window content area)
// Check dropdown menu button
int button_y = 28; // Position from top of window title bar
if (x >= win->w - 90 && x < win->w - 55 &&
y >= button_y && y < button_y + 30) {
// Dropdown menu button clicked
dropdown_menu_toggle();
return;
}
// Check back button (right-aligned)
if (x >= win->w - 40 && x < win->w - 10 &&
y >= button_y && y < button_y + 30) {
// Back button clicked
explorer_navigate_to("..");
return;
}
// File items start at y=64 relative to window
int content_start_y = 64;
int offset_x = 4;
for (int i = 0; i < item_count; i++) {
int row = i / EXPLORER_COLS;
int col = i % EXPLORER_COLS;
int item_x = offset_x + 10 + (col * (EXPLORER_ITEM_WIDTH + EXPLORER_PADDING));
int item_y = content_start_y + (row * (EXPLORER_ITEM_HEIGHT + EXPLORER_PADDING));
if (x >= item_x && x < item_x + EXPLORER_ITEM_WIDTH &&
y >= item_y && y < item_y + EXPLORER_ITEM_HEIGHT) {
// Check for double-click
if (last_clicked_item == i) {
// Double-click detected
if (items[i].is_directory) {
explorer_navigate_to(items[i].name);
} else {
// Open file in editor
char full_path[256];
explorer_strcpy(full_path, current_path);
if (full_path[explorer_strlen(full_path) - 1] != '/') {
explorer_strcat(full_path, "/");
}
explorer_strcat(full_path, items[i].name);
// Open in editor and bring to front
win_editor.visible = true;
win_editor.focused = true;
int max_z = 0;
for (int j = 0; j < 5; j++) { // window_count is 5
// Need to find max z_index - check all windows
if (win_explorer.z_index > max_z) max_z = win_explorer.z_index;
if (win_cmd.z_index > max_z) max_z = win_cmd.z_index;
if (win_notepad.z_index > max_z) max_z = win_notepad.z_index;
if (win_calculator.z_index > max_z) max_z = win_calculator.z_index;
}
win_editor.z_index = max_z + 1;
editor_open_file(full_path);
}
last_clicked_item = -1;
} else {
// Single-click - select
selected_item = i;
last_clicked_item = i;
last_click_time = 0; // Reset for next click
}
return;
}
}
}
// === Key Handler ===
static void explorer_handle_key(Window *win, char c) {
(void)win;
// Handle dialog input
if (dialog_state == DIALOG_CREATE_FILE || dialog_state == DIALOG_CREATE_FOLDER) {
if (c == 27) { // ESC - close dialog
dialog_close();
return;
} else if (c == '\n') { // ENTER - confirm
if (dialog_state == DIALOG_CREATE_FILE) {
dialog_confirm_create_file();
} else {
dialog_confirm_create_folder();
}
return;
} else if (c == 8 || c == 127) { // BACKSPACE
if (dialog_input_cursor > 0) {
dialog_input_cursor--;
// Shift characters
for (int i = dialog_input_cursor; i < (int)explorer_strlen(dialog_input); i++) {
dialog_input[i] = dialog_input[i + 1];
}
}
return;
} else if (c >= 32 && c < 127) { // Printable character
int len = explorer_strlen(dialog_input);
if (len < DIALOG_INPUT_MAX - 1) {
// Shift characters to make room
for (int i = len; i >= dialog_input_cursor; i--) {
dialog_input[i + 1] = dialog_input[i];
}
dialog_input[dialog_input_cursor] = c;
dialog_input_cursor++;
}
return;
}
return;
}
if (dialog_state == DIALOG_DELETE_CONFIRM) {
if (c == 27) { // ESC
dialog_close();
return;
}
return;
}
if (c == 'q' || c == 'Q') {
win->visible = false;
return;
}
// Close dropdown menu if open with ESC
if (dropdown_menu_visible && c == 27) {
dropdown_menu_toggle();
return;
}
if (c == 17) { // UP
if (selected_item > 0) {
selected_item -= EXPLORER_COLS;
if (selected_item < 0) selected_item = 0;
}
} else if (c == 18) { // DOWN
if (selected_item < item_count - 1) {
selected_item += EXPLORER_COLS;
if (selected_item >= item_count) selected_item = item_count - 1;
}
} else if (c == 19) { // LEFT
if (selected_item > 0) {
selected_item--;
}
} else if (c == 20) { // RIGHT
if (selected_item < item_count - 1) {
selected_item++;
}
} else if (c == '\n') { // ENTER
if (selected_item >= 0 && selected_item < item_count) {
if (items[selected_item].is_directory) {
explorer_navigate_to(items[selected_item].name);
}
}
} else if (c == 'd' || c == 'D') { // Delete key
if (selected_item >= 0) {
dialog_open_delete_confirm(selected_item);
}
} else if (c == 'n' || c == 'N') { // New file
dialog_open_create_file();
} else if (c == 'f' || c == 'F') { // New folder
dialog_open_create_folder();
}
}
// === Initialization ===
void explorer_init(void) {
win_explorer.title = "File Explorer";
win_explorer.x = 300;
win_explorer.y = 100;
win_explorer.w = 600;
win_explorer.h = 400;
win_explorer.visible = false;
win_explorer.focused = false;
win_explorer.z_index = 0;
win_explorer.paint = explorer_paint;
win_explorer.handle_key = explorer_handle_key;
win_explorer.handle_click = explorer_handle_click;
win_explorer.handle_right_click = NULL;
explorer_load_directory("/");
}
void explorer_reset(void) {
// Reset explorer to root directory on close/reopen
explorer_load_directory("/");
win_explorer.focused = false;
}

15
src/kernel/explorer.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef EXPLORER_H
#define EXPLORER_H
#include "wm.h"
extern Window win_explorer;
extern Window win_editor;
extern Window win_cmd;
extern Window win_notepad;
extern Window win_calculator;
void explorer_init(void);
void explorer_reset(void);
#endif

480
src/kernel/fat32.c Normal file
View File

@@ -0,0 +1,480 @@
#include "fat32.h"
#include <stdbool.h>
#include <stddef.h>
// === Memory-based FAT32 Implementation ===
// This is a simplified FAT32 for the OS kernel
// It allocates everything in RAM instead of using a real disk
#define MAX_FILES 256
#define MAX_CLUSTERS 1024
#define MAX_OPEN_HANDLES 32
// In-memory FAT table
static uint32_t fat_table[MAX_CLUSTERS];
static uint8_t cluster_data[MAX_CLUSTERS][FAT32_CLUSTER_SIZE];
// File/Directory tracking
typedef struct {
char full_path[FAT32_MAX_PATH];
char filename[FAT32_MAX_FILENAME];
uint32_t start_cluster;
uint32_t size;
uint32_t attributes;
bool used;
char parent_path[FAT32_MAX_PATH];
} FileEntry;
static FileEntry files[MAX_FILES];
static uint32_t next_cluster = 3; // Start after reserved clusters 0, 1, 2
static FAT32_FileHandle open_handles[MAX_OPEN_HANDLES];
static char current_dir[FAT32_MAX_PATH] = "/";
// === Helper Functions ===
static size_t fs_strlen(const char *str) {
size_t len = 0;
while (str[len]) len++;
return len;
}
static void fs_strcpy(char *dest, const char *src) {
while (*src) *dest++ = *src++;
*dest = 0;
}
static int fs_strcmp(const char *s1, const char *s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}
static void fs_strcat(char *dest, const char *src) {
while (*dest) dest++;
fs_strcpy(dest, src);
}
static bool fs_ends_with(const char *str, const char *suffix) {
int str_len = fs_strlen(str);
int suffix_len = fs_strlen(suffix);
if (suffix_len > str_len) return false;
return fs_strcmp(str + str_len - suffix_len, suffix) == 0;
}
// Extract filename from path
static void extract_filename(const char *path, char *filename) {
int len = fs_strlen(path);
int i = len - 1;
// Skip trailing slashes
while (i > 0 && path[i] == '/') i--;
// Find last slash
int start = i;
while (start >= 0 && path[start] != '/') start--;
start++;
// Copy filename
int j = 0;
for (int k = start; k <= i; k++) {
filename[j++] = path[k];
}
filename[j] = 0;
}
// Extract parent path
static void extract_parent_path(const char *path, char *parent) {
int len = fs_strlen(path);
int i = len - 1;
// Skip trailing slashes
while (i > 0 && path[i] == '/') i--;
// Find last slash
while (i > 0 && path[i] != '/') i--;
if (i == 0) {
parent[0] = '/';
parent[1] = 0;
} else {
for (int j = 0; j < i; j++) {
parent[j] = path[j];
}
parent[i] = 0;
}
}
// Normalize path (remove .., ., etc)
void fat32_normalize_path(const char *path, char *normalized) {
if (path[0] == '/') {
// Absolute path
fs_strcpy(normalized, path);
} else {
// Relative path - prepend current directory
if (fs_strcmp(current_dir, "/") == 0) {
normalized[0] = '/';
fs_strcpy(normalized + 1, path);
} else {
fs_strcpy(normalized, current_dir);
if (normalized[fs_strlen(normalized) - 1] != '/') {
fs_strcat(normalized, "/");
}
fs_strcat(normalized, path);
}
}
// Remove trailing slashes (except for root)
int len = fs_strlen(normalized);
while (len > 1 && normalized[len - 1] == '/') {
normalized[--len] = 0;
}
}
// Find file entry by path
static FileEntry* find_file(const char *path) {
char normalized[FAT32_MAX_PATH];
fat32_normalize_path(path, normalized);
for (int i = 0; i < MAX_FILES; i++) {
if (files[i].used && fs_strcmp(files[i].full_path, normalized) == 0) {
return &files[i];
}
}
return NULL;
}
// Find first unused file entry
static FileEntry* find_free_entry(void) {
for (int i = 0; i < MAX_FILES; i++) {
if (!files[i].used) {
return &files[i];
}
}
return NULL;
}
// Find free handle
static FAT32_FileHandle* find_free_handle(void) {
for (int i = 0; i < MAX_OPEN_HANDLES; i++) {
if (!open_handles[i].valid) {
return &open_handles[i];
}
}
return NULL;
}
// Allocate cluster
static uint32_t allocate_cluster(void) {
if (next_cluster >= MAX_CLUSTERS) return 0;
uint32_t cluster = next_cluster++;
fat_table[cluster] = 0xFFFFFFFF; // End of chain
return cluster;
}
// === Public API ===
void fat32_init(void) {
// Initialize FAT table
for (int i = 0; i < MAX_CLUSTERS; i++) {
fat_table[i] = 0;
}
fat_table[0] = 0xFFFFFFF8; // Media descriptor
fat_table[1] = 0xFFFFFFFF; // Bad sector marker
// Create root directory entry
FileEntry *root = find_free_entry();
if (root) {
root->used = true;
root->filename[0] = 0;
fs_strcpy(root->full_path, "/");
root->start_cluster = 2; // Root cluster
root->size = 0;
root->attributes = ATTR_DIRECTORY;
fat_table[2] = 0xFFFFFFFF; // Root is EOF
}
next_cluster = 3;
current_dir[0] = '/';
current_dir[1] = 0;
}
FAT32_FileHandle* fat32_open(const char *path, const char *mode) {
char normalized[FAT32_MAX_PATH];
fat32_normalize_path(path, normalized);
FileEntry *entry = find_file(normalized);
if (mode[0] == 'r') {
// Read mode
if (!entry || (entry->attributes & ATTR_DIRECTORY)) {
return NULL; // File not found or is directory
}
} else if (mode[0] == 'w' || (mode[0] == 'a')) {
// Write/append mode - create if not exists
if (!entry) {
entry = find_free_entry();
if (!entry) return NULL;
entry->used = true;
fs_strcpy(entry->full_path, normalized);
extract_filename(normalized, entry->filename);
extract_parent_path(normalized, entry->parent_path);
entry->start_cluster = allocate_cluster();
if (!entry->start_cluster) return NULL;
entry->size = 0;
entry->attributes = 0; // Regular file
}
if (mode[0] == 'w') {
entry->size = 0; // Truncate
}
}
// Find free handle
FAT32_FileHandle *handle = find_free_handle();
if (!handle) return NULL;
handle->valid = true;
handle->cluster = entry->start_cluster;
handle->position = 0;
handle->size = entry->size;
if (mode[0] == 'r') {
handle->mode = 0;
} else if (mode[0] == 'w') {
handle->mode = 1;
} else {
handle->mode = 2; // append
handle->position = entry->size;
}
return handle;
}
void fat32_close(FAT32_FileHandle *handle) {
if (handle) {
handle->valid = false;
}
}
int fat32_read(FAT32_FileHandle *handle, void *buffer, int size) {
if (!handle || !handle->valid || handle->mode != 0) {
return -1;
}
int bytes_read = 0;
uint8_t *buf = (uint8_t *)buffer;
while (bytes_read < size && handle->position < handle->size) {
uint32_t offset_in_cluster = handle->position % FAT32_CLUSTER_SIZE;
int to_read = size - bytes_read;
int available = handle->size - handle->position;
if (to_read > available) {
to_read = available;
}
if (to_read > FAT32_CLUSTER_SIZE - offset_in_cluster) {
to_read = FAT32_CLUSTER_SIZE - offset_in_cluster;
}
if (handle->cluster >= MAX_CLUSTERS) {
break;
}
uint8_t *src = cluster_data[handle->cluster] + offset_in_cluster;
for (int i = 0; i < to_read; i++) {
buf[bytes_read + i] = src[i];
}
bytes_read += to_read;
handle->position += to_read;
// Move to next cluster if needed
if (handle->position % FAT32_CLUSTER_SIZE == 0 && handle->position < handle->size) {
handle->cluster = fat_table[handle->cluster];
}
}
return bytes_read;
}
int fat32_write(FAT32_FileHandle *handle, const void *buffer, int size) {
if (!handle || !handle->valid || (handle->mode != 1 && handle->mode != 2)) {
return -1;
}
int bytes_written = 0;
const uint8_t *buf = (const uint8_t *)buffer;
uint32_t initial_cluster = handle->cluster;
while (bytes_written < size) {
uint32_t offset_in_cluster = handle->position % FAT32_CLUSTER_SIZE;
int to_write = size - bytes_written;
if (to_write > FAT32_CLUSTER_SIZE - offset_in_cluster) {
to_write = FAT32_CLUSTER_SIZE - offset_in_cluster;
}
if (handle->cluster >= MAX_CLUSTERS) {
break;
}
uint8_t *dest = cluster_data[handle->cluster] + offset_in_cluster;
for (int i = 0; i < to_write; i++) {
dest[i] = buf[bytes_written + i];
}
bytes_written += to_write;
handle->position += to_write;
if (handle->position > handle->size) {
handle->size = handle->position;
}
// Move to next cluster if needed
if (offset_in_cluster + to_write >= FAT32_CLUSTER_SIZE && bytes_written < size) {
uint32_t next = allocate_cluster();
if (!next) break;
fat_table[handle->cluster] = next;
handle->cluster = next;
}
}
// Update file entry
for (int i = 0; i < MAX_FILES; i++) {
if (files[i].used && files[i].start_cluster == initial_cluster) {
files[i].size = handle->size;
break;
}
}
return bytes_written;
}
int fat32_seek(FAT32_FileHandle *handle, int offset, int whence) {
if (!handle || !handle->valid) {
return -1;
}
uint32_t new_position = handle->position;
if (whence == 0) { // SEEK_SET
new_position = offset;
} else if (whence == 1) { // SEEK_CUR
new_position += offset;
} else if (whence == 2) { // SEEK_END
new_position = handle->size + offset;
}
if (new_position > handle->size) {
new_position = handle->size;
}
handle->position = new_position;
return new_position;
}
bool fat32_mkdir(const char *path) {
char normalized[FAT32_MAX_PATH];
fat32_normalize_path(path, normalized);
if (find_file(normalized)) {
return false; // Already exists
}
FileEntry *entry = find_free_entry();
if (!entry) return false;
entry->used = true;
fs_strcpy(entry->full_path, normalized);
extract_filename(normalized, entry->filename);
extract_parent_path(normalized, entry->parent_path);
entry->start_cluster = allocate_cluster();
entry->size = 0;
entry->attributes = ATTR_DIRECTORY;
return true;
}
bool fat32_rmdir(const char *path) {
char normalized[FAT32_MAX_PATH];
fat32_normalize_path(path, normalized);
FileEntry *entry = find_file(normalized);
if (!entry || !(entry->attributes & ATTR_DIRECTORY)) {
return false;
}
entry->used = false;
return true;
}
bool fat32_delete(const char *path) {
char normalized[FAT32_MAX_PATH];
fat32_normalize_path(path, normalized);
FileEntry *entry = find_file(normalized);
if (!entry || (entry->attributes & ATTR_DIRECTORY)) {
return false;
}
entry->used = false;
return true;
}
bool fat32_exists(const char *path) {
return find_file(path) != NULL;
}
bool fat32_is_directory(const char *path) {
FileEntry *entry = find_file(path);
return entry && (entry->attributes & ATTR_DIRECTORY);
}
int fat32_list_directory(const char *path, FAT32_FileInfo *entries, int max_entries) {
char normalized[FAT32_MAX_PATH];
fat32_normalize_path(path, normalized);
FileEntry *dir = find_file(normalized);
if (!dir || !(dir->attributes & ATTR_DIRECTORY)) {
return 0; // Not a directory
}
int count = 0;
for (int i = 0; i < MAX_FILES && count < max_entries; i++) {
if (files[i].used && fs_strcmp(files[i].parent_path, normalized) == 0) {
fs_strcpy(entries[count].name, files[i].filename);
entries[count].size = files[i].size;
entries[count].is_directory = (files[i].attributes & ATTR_DIRECTORY) != 0;
entries[count].start_cluster = files[i].start_cluster;
count++;
}
}
return count;
}
bool fat32_chdir(const char *path) {
char normalized[FAT32_MAX_PATH];
fat32_normalize_path(path, normalized);
FileEntry *entry = find_file(normalized);
if (!entry || !(entry->attributes & ATTR_DIRECTORY)) {
return false;
}
fs_strcpy(current_dir, normalized);
return true;
}
void fat32_get_current_dir(char *buffer, int size) {
int len = fs_strlen(current_dir);
if (len >= size) len = size - 1;
for (int i = 0; i < len; i++) {
buffer[i] = current_dir[i];
}
buffer[len] = 0;
}

126
src/kernel/fat32.h Normal file
View File

@@ -0,0 +1,126 @@
#ifndef FAT32_H
#define FAT32_H
#include <stdint.h>
#include <stdbool.h>
// === FAT32 Structures ===
// Boot Sector (512 bytes)
typedef struct {
uint8_t jmp[3]; // Jump instruction
uint8_t oem[8]; // OEM identifier
uint16_t bytes_per_sector; // Bytes per sector (usually 512)
uint8_t sectors_per_cluster; // Sectors per cluster
uint16_t reserved_sectors; // Reserved sectors (usually 1)
uint8_t num_fats; // Number of FATs (usually 2)
uint16_t root_entries; // Root directory entries (0 for FAT32)
uint16_t total_sectors_16; // Total sectors 16-bit (0 for FAT32)
uint8_t media_descriptor; // Media descriptor
uint16_t sectors_per_fat_16; // Sectors per FAT 16-bit (0 for FAT32)
uint16_t sectors_per_track; // Sectors per track
uint16_t num_heads; // Number of heads
uint32_t hidden_sectors; // Hidden sectors
uint32_t total_sectors_32; // Total sectors 32-bit
// FAT32 Specific
uint32_t sectors_per_fat_32; // Sectors per FAT 32-bit
uint16_t flags; // Flags
uint16_t version; // Version
uint32_t root_cluster; // Root directory cluster
uint16_t fsinfo_sector; // FSInfo sector number
uint16_t backup_boot_sector; // Backup boot sector number
uint8_t reserved[12]; // Reserved
uint8_t drive_number; // Drive number
uint8_t reserved2; // Reserved
uint8_t boot_signature; // Boot signature
uint32_t serial_number; // Volume serial number
uint8_t volume_label[11]; // Volume label
uint8_t fs_type[8]; // Filesystem type ("FAT32 ")
uint8_t boot_code[420]; // Boot code
uint16_t boot_signature_value; // Boot signature value (0xAA55)
} __attribute__((packed)) FAT32_BootSector;
// Directory Entry (32 bytes)
typedef struct {
uint8_t filename[8]; // Filename (8 bytes)
uint8_t extension[3]; // Extension (3 bytes)
uint8_t attributes; // File attributes
uint8_t reserved; // Reserved
uint8_t creation_time_tenths; // Creation time (tenths of second)
uint16_t creation_time; // Creation time (HH:MM:SS)
uint16_t creation_date; // Creation date (YYYY:MM:DD)
uint16_t last_access_date; // Last access date
uint16_t start_cluster_high; // Start cluster (high word)
uint16_t write_time; // Write time
uint16_t write_date; // Write date
uint16_t start_cluster_low; // Start cluster (low word)
uint32_t file_size; // File size
} __attribute__((packed)) FAT32_DirEntry;
// File Attributes
#define ATTR_READ_ONLY 0x01
#define ATTR_HIDDEN 0x02
#define ATTR_SYSTEM 0x04
#define ATTR_VOLUME_ID 0x08
#define ATTR_DIRECTORY 0x10
#define ATTR_ARCHIVE 0x20
#define ATTR_DEVICE 0x40
#define ATTR_RESERVED 0x80
// FAT32 Constants
#define FAT32_SECTOR_SIZE 512
#define FAT32_CLUSTER_SIZE 4096 // 8 sectors per cluster
#define FAT32_MAX_FILENAME 256
#define FAT32_MAX_PATH 1024
#define FAT32_ROOT_CLUSTER 2
// File Handle
typedef struct {
uint32_t cluster; // Current cluster
uint32_t position; // Current position in file
uint32_t size; // File size
uint32_t mode; // 0=read, 1=write, 2=append
bool valid; // Is this handle valid?
} FAT32_FileHandle;
// Directory Entry Info (for listing)
typedef struct {
char name[FAT32_MAX_FILENAME];
uint32_t size;
bool is_directory;
uint32_t start_cluster;
uint16_t write_date;
uint16_t write_time;
} FAT32_FileInfo;
// === Function Declarations ===
// Initialization
void fat32_init(void);
// File Operations
FAT32_FileHandle* fat32_open(const char *path, const char *mode);
void fat32_close(FAT32_FileHandle *handle);
int fat32_read(FAT32_FileHandle *handle, void *buffer, int size);
int fat32_write(FAT32_FileHandle *handle, const void *buffer, int size);
int fat32_seek(FAT32_FileHandle *handle, int offset, int whence);
// Directory Operations
bool fat32_mkdir(const char *path);
bool fat32_rmdir(const char *path);
bool fat32_delete(const char *path);
bool fat32_exists(const char *path);
bool fat32_is_directory(const char *path);
// Listing
int fat32_list_directory(const char *path, FAT32_FileInfo *entries, int max_entries);
// Working Directory
bool fat32_chdir(const char *path);
void fat32_get_current_dir(char *buffer, int size);
// Utilities
void fat32_normalize_path(const char *path, char *normalized);
#endif

206
src/kernel/font.h Normal file
View File

@@ -0,0 +1,206 @@
#ifndef FONT_H
#define FONT_H
#include <stdint.h>
// Minimal 8x8 font for ASCII 32-127
// Derived from standard VGA font
static const uint8_t font8x8_basic[128][8] = {
// 0-31 Control chars (empty)
{0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0},
{0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0},
// 32 Space
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
// 33 !
{0x18, 0x3C, 0x3C, 0x18, 0x18, 0x00, 0x18, 0x00},
// 34 "
{0x66, 0x66, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00},
// 35 #
{0x6C, 0x6C, 0xFE, 0x6C, 0xFE, 0x6C, 0x6C, 0x00},
// 36 $
{0x18, 0x3E, 0x60, 0x3C, 0x06, 0x7C, 0x18, 0x00},
// 37 %
{0x00, 0xC6, 0xCC, 0x18, 0x30, 0x66, 0xC6, 0x00},
// 38 &
{0x38, 0x6C, 0x38, 0x76, 0xDC, 0xCC, 0x76, 0x00},
// 39 '
{0x18, 0x18, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00},
// 40 (
{0x0C, 0x18, 0x30, 0x30, 0x30, 0x18, 0x0C, 0x00},
// 41 )
{0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x18, 0x30, 0x00},
// 42 *
{0x00, 0x66, 0x3C, 0xFF, 0x3C, 0x66, 0x00, 0x00},
// 43 +
{0x00, 0x18, 0x18, 0x7E, 0x18, 0x18, 0x00, 0x00},
// 44 ,
{0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x30},
// 45 -
{0x00, 0x00, 0x00, 0x7E, 0x00, 0x00, 0x00, 0x00},
// 46 .
{0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00},
// 47 /
{0x00, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0x00},
// 48 0
{0x3C, 0x66, 0x6E, 0x76, 0x66, 0x66, 0x3C, 0x00},
// 49 1
{0x18, 0x18, 0x38, 0x18, 0x18, 0x18, 0x7E, 0x00},
// 50 2
{0x3C, 0x66, 0x06, 0x0C, 0x30, 0x60, 0x7E, 0x00},
// 51 3
{0x3C, 0x66, 0x06, 0x1C, 0x06, 0x66, 0x3C, 0x00},
// 52 4
{0x06, 0x0E, 0x1E, 0x36, 0x66, 0x7F, 0x06, 0x00},
// 53 5
{0x7E, 0x60, 0x7C, 0x06, 0x06, 0x66, 0x3C, 0x00},
// 54 6
{0x3C, 0x66, 0x60, 0x7C, 0x66, 0x66, 0x3C, 0x00},
// 55 7
{0x7E, 0x06, 0x0C, 0x18, 0x30, 0x30, 0x30, 0x00},
// 56 8
{0x3C, 0x66, 0x66, 0x3C, 0x66, 0x66, 0x3C, 0x00},
// 57 9
{0x3C, 0x66, 0x66, 0x3E, 0x06, 0x66, 0x3C, 0x00},
// 58 :
{0x00, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x00},
// 59 ;
{0x00, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x30},
// 60 <
{0x0C, 0x18, 0x30, 0x60, 0x30, 0x18, 0x0C, 0x00},
// 61 =
{0x00, 0x00, 0x7E, 0x00, 0x7E, 0x00, 0x00, 0x00},
// 62 >
{0x30, 0x18, 0x0C, 0x06, 0x0C, 0x18, 0x30, 0x00},
// 63 ?
{0x3C, 0x66, 0x06, 0x0C, 0x18, 0x00, 0x18, 0x00},
// 64 @
{0x3C, 0x66, 0x6E, 0x6A, 0x68, 0x60, 0x3C, 0x00},
// 65 A
{0x18, 0x3C, 0x66, 0x66, 0x7E, 0x66, 0x66, 0x00},
// 66 B
{0x7C, 0x66, 0x66, 0x7C, 0x66, 0x66, 0x7C, 0x00},
// 67 C
{0x3C, 0x66, 0x60, 0x60, 0x60, 0x66, 0x3C, 0x00},
// 68 D
{0x78, 0x6C, 0x66, 0x66, 0x66, 0x6C, 0x78, 0x00},
// 69 E
{0x7E, 0x60, 0x60, 0x7C, 0x60, 0x60, 0x7E, 0x00},
// 70 F
{0x7E, 0x60, 0x60, 0x7C, 0x60, 0x60, 0x60, 0x00},
// 71 G
{0x3C, 0x66, 0x60, 0x6E, 0x66, 0x66, 0x3C, 0x00},
// 72 H
{0x66, 0x66, 0x66, 0x7E, 0x66, 0x66, 0x66, 0x00},
// 73 I
{0x3C, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x00},
// 74 J
{0x06, 0x06, 0x06, 0x06, 0x06, 0x66, 0x3C, 0x00},
// 75 K
{0x66, 0x6C, 0x78, 0x70, 0x78, 0x6C, 0x66, 0x00},
// 76 L
{0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x7E, 0x00},
// 77 M
{0x63, 0x77, 0x7F, 0x6B, 0x63, 0x63, 0x63, 0x00},
// 78 N
{0x66, 0x76, 0x7E, 0x7E, 0x6E, 0x66, 0x66, 0x00},
// 79 O
{0x3C, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3C, 0x00},
// 80 P
{0x7C, 0x66, 0x66, 0x7C, 0x60, 0x60, 0x60, 0x00},
// 81 Q
{0x3C, 0x66, 0x66, 0x66, 0x6A, 0x3C, 0x0E, 0x00},
// 82 R
{0x7C, 0x66, 0x66, 0x7C, 0x78, 0x6C, 0x66, 0x00},
// 83 S
{0x3C, 0x66, 0x60, 0x3C, 0x06, 0x66, 0x3C, 0x00},
// 84 T
{0x7E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00},
// 85 U
{0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3C, 0x00},
// 86 V
{0x66, 0x66, 0x66, 0x66, 0x66, 0x3C, 0x18, 0x00},
// 87 W
{0x63, 0x63, 0x63, 0x6B, 0x7F, 0x77, 0x63, 0x00},
// 88 X
{0x66, 0x66, 0x3C, 0x18, 0x3C, 0x66, 0x66, 0x00},
// 89 Y
{0x66, 0x66, 0x66, 0x3C, 0x18, 0x18, 0x18, 0x00},
// 90 Z
{0x7E, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x7E, 0x00},
// 91 [
{0x1E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1E, 0x00},
// 92 backslash
{0x00, 0xC0, 0x60, 0x30, 0x18, 0x0C, 0x06, 0x00},
// 93 ]
{0x78, 0x18, 0x18, 0x18, 0x18, 0x18, 0x78, 0x00},
// 94 ^
{0x08, 0x1C, 0x36, 0x63, 0x00, 0x00, 0x00, 0x00},
// 95 _
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF},
// 96 `
{0x18, 0x0C, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00},
// 97 a
{0x00, 0x00, 0x3C, 0x06, 0x3E, 0x66, 0x3E, 0x00},
// 98 b
{0x60, 0x60, 0x7C, 0x66, 0x66, 0x66, 0x7C, 0x00},
// 99 c
{0x00, 0x00, 0x3C, 0x66, 0x60, 0x66, 0x3C, 0x00},
// 100 d
{0x06, 0x06, 0x3E, 0x66, 0x66, 0x66, 0x3E, 0x00},
// 101 e
{0x00, 0x00, 0x3C, 0x66, 0x7E, 0x60, 0x3C, 0x00},
// 102 f
{0x0C, 0x18, 0x18, 0x3E, 0x18, 0x18, 0x18, 0x00},
// 103 g
{0x00, 0x00, 0x3E, 0x66, 0x66, 0x3E, 0x06, 0x3C},
// 104 h
{0x60, 0x60, 0x7C, 0x66, 0x66, 0x66, 0x66, 0x00},
// 105 i
{0x18, 0x00, 0x38, 0x18, 0x18, 0x18, 0x3C, 0x00},
// 106 j
{0x06, 0x00, 0x06, 0x06, 0x06, 0x06, 0x3C, 0x00},
// 107 k
{0x60, 0x60, 0x66, 0x6C, 0x78, 0x6C, 0x66, 0x00},
// 108 l
{0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x00},
// 109 m
{0x00, 0x00, 0x76, 0x7F, 0x6B, 0x6B, 0x63, 0x00},
// 110 n
{0x00, 0x00, 0x7C, 0x66, 0x66, 0x66, 0x66, 0x00},
// 111 o
{0x00, 0x00, 0x3C, 0x66, 0x66, 0x66, 0x3C, 0x00},
// 112 p
{0x00, 0x00, 0x7C, 0x66, 0x66, 0x7C, 0x60, 0x60},
// 113 q
{0x00, 0x00, 0x3E, 0x66, 0x66, 0x3E, 0x06, 0x06},
// 114 r
{0x00, 0x00, 0x5C, 0x62, 0x60, 0x60, 0x60, 0x00},
// 115 s
{0x00, 0x00, 0x3C, 0x60, 0x3C, 0x06, 0x3C, 0x00},
// 116 t
{0x10, 0x10, 0x3E, 0x10, 0x10, 0x10, 0x0C, 0x00},
// 117 u
{0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x3E, 0x00},
// 118 v
{0x00, 0x00, 0x66, 0x66, 0x66, 0x3C, 0x18, 0x00},
// 119 w
{0x00, 0x00, 0x63, 0x6B, 0x7F, 0x3E, 0x36, 0x00},
// 120 x
{0x00, 0x00, 0x66, 0x3C, 0x18, 0x3C, 0x66, 0x00},
// 121 y
{0x00, 0x00, 0x66, 0x66, 0x66, 0x3E, 0x06, 0x3C},
// 122 z
{0x00, 0x00, 0x7E, 0x0C, 0x18, 0x30, 0x7E, 0x00},
// 123 {
{0x0E, 0x18, 0x18, 0x70, 0x18, 0x18, 0x0E, 0x00},
// 124 |
{0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00},
// 125 }
{0x70, 0x18, 0x18, 0x0E, 0x18, 0x18, 0x70, 0x00},
// 126 ~
{0x76, 0xDC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
// 127 DEL
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
};
#endif

180
src/kernel/graphics.c Normal file
View File

@@ -0,0 +1,180 @@
#include <stddef.h>
#include "graphics.h"
#include "font.h"
static struct limine_framebuffer *g_fb = NULL;
static uint32_t g_bg_color = 0xFF6B4423; // Coffee color by default
// Dirty rectangle tracking
static DirtyRect g_dirty = {0, 0, 0, 0, false};
// Double buffering - allocate a back buffer
// Max screen size: 2048x2048 @ 32bpp = 16MB, but we'll allocate for common sizes
// Using a simple approach: allocate max size buffer
#define MAX_FB_WIDTH 2048
#define MAX_FB_HEIGHT 2048
static uint32_t g_back_buffer[MAX_FB_WIDTH * MAX_FB_HEIGHT] __attribute__((aligned(4096)));
void graphics_init(struct limine_framebuffer *fb) {
g_fb = fb;
g_dirty.active = false;
// Initialize back buffer to black
for (int i = 0; i < MAX_FB_WIDTH * MAX_FB_HEIGHT; i++) {
g_back_buffer[i] = 0;
}
}
int get_screen_width(void) {
return g_fb ? g_fb->width : 0;
}
int get_screen_height(void) {
return g_fb ? g_fb->height : 0;
}
// Merge new dirty rect with existing one
static void merge_dirty_rect(int x, int y, int w, int h) {
if (!g_dirty.active) {
g_dirty.x = x;
g_dirty.y = y;
g_dirty.w = w;
g_dirty.h = h;
g_dirty.active = true;
} else {
// Calculate union of two rectangles
int x1 = g_dirty.x;
int y1 = g_dirty.y;
int x2 = g_dirty.x + g_dirty.w;
int y2 = g_dirty.y + g_dirty.h;
int new_x1 = x;
int new_y1 = y;
int new_x2 = x + w;
int new_y2 = y + h;
g_dirty.x = new_x1 < x1 ? new_x1 : x1;
g_dirty.y = new_y1 < y1 ? new_y1 : y1;
g_dirty.w = (new_x2 > x2 ? new_x2 : x2) - g_dirty.x;
g_dirty.h = (new_y2 > y2 ? new_y2 : y2) - g_dirty.y;
}
}
void graphics_mark_dirty(int x, int y, int w, int h) {
// Clamp to screen bounds
if (x < 0) {
w += x;
x = 0;
}
if (y < 0) {
h += y;
y = 0;
}
if (x + w > get_screen_width()) {
w = get_screen_width() - x;
}
if (y + h > get_screen_height()) {
h = get_screen_height() - y;
}
if (w <= 0 || h <= 0) return;
merge_dirty_rect(x, y, w, h);
}
void graphics_mark_screen_dirty(void) {
g_dirty.x = 0;
g_dirty.y = 0;
g_dirty.w = get_screen_width();
g_dirty.h = get_screen_height();
g_dirty.active = true;
}
DirtyRect graphics_get_dirty_rect(void) {
return g_dirty;
}
void graphics_clear_dirty(void) {
g_dirty.active = false;
}
void put_pixel(int x, int y, uint32_t color) {
if (!g_fb) return;
if (x < 0 || x >= (int)g_fb->width || y < 0 || y >= (int)g_fb->height) return;
// Draw to back buffer
uint32_t pixel_offset = y * g_fb->width + x;
g_back_buffer[pixel_offset] = color;
}
void draw_rect(int x, int y, int w, int h, uint32_t color) {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
put_pixel(x + j, y + i, color);
}
}
}
void draw_char(int x, int y, char c, uint32_t color) {
unsigned char uc = (unsigned char)c;
if (uc > 127) return;
const uint8_t *glyph = font8x8_basic[uc];
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if ((glyph[row] >> (7 - col)) & 1) {
put_pixel(x + col, y + row, color);
}
}
}
}
void draw_string(int x, int y, const char *s, uint32_t color) {
int cur_x = x;
int cur_y = y;
while (*s) {
if (*s == '\n') {
cur_x = x;
cur_y += 10;
} else {
draw_char(cur_x, cur_y, *s, color);
cur_x += 8;
}
s++;
}
}
void draw_desktop_background(void) {
if (!g_fb) return;
draw_rect(0, 0, g_fb->width, g_fb->height, g_bg_color);
}
void graphics_set_bg_color(uint32_t color) {
g_bg_color = color;
}
// Double buffering functions
void graphics_clear_back_buffer(uint32_t color) {
if (!g_fb) return;
uint32_t *buf = g_back_buffer;
for (int i = 0; i < (int)g_fb->width * (int)g_fb->height; i++) {
*buf++ = color;
}
}
void graphics_flip_buffer(void) {
if (!g_fb) return;
// Copy back buffer to framebuffer
uint32_t *src = g_back_buffer;
uint8_t *dst = (uint8_t *)g_fb->address;
for (int y = 0; y < (int)g_fb->height; y++) {
// Copy one scanline
uint32_t *dst_row = (uint32_t *)dst;
for (int x = 0; x < (int)g_fb->width; x++) {
dst_row[x] = src[x];
}
src += g_fb->width;
dst += g_fb->pitch;
}
}

36
src/kernel/graphics.h Normal file
View File

@@ -0,0 +1,36 @@
#ifndef GRAPHICS_H
#define GRAPHICS_H
#include <stdint.h>
#include <stdbool.h>
#include "limine.h"
// Dirty rectangle structure
typedef struct {
int x, y, w, h;
bool active;
} DirtyRect;
void graphics_init(struct limine_framebuffer *fb);
void put_pixel(int x, int y, uint32_t color);
void draw_rect(int x, int y, int w, int h, uint32_t color);
void draw_char(int x, int y, char c, uint32_t color);
void draw_string(int x, int y, const char *s, uint32_t color);
void draw_desktop_background(void);
void graphics_set_bg_color(uint32_t color);
// Get screen dimensions
int get_screen_width(void);
int get_screen_height(void);
// Dirty rectangle management
void graphics_mark_dirty(int x, int y, int w, int h);
void graphics_mark_screen_dirty(void);
DirtyRect graphics_get_dirty_rect(void);
void graphics_clear_dirty(void);
// Double buffering
void graphics_flip_buffer(void);
void graphics_clear_back_buffer(uint32_t color);
#endif

103
src/kernel/idt.c Normal file
View File

@@ -0,0 +1,103 @@
#include "idt.h"
#include "io.h"
#define IDT_ENTRIES 256
struct idt_entry {
uint16_t isr_low;
uint16_t kernel_cs;
uint8_t ist;
uint8_t attributes;
uint16_t isr_mid;
uint32_t isr_high;
uint32_t reserved;
} __attribute__((packed));
struct idt_ptr {
uint16_t limit;
uint64_t base;
} __attribute__((packed));
static struct idt_entry idt[IDT_ENTRIES];
static struct idt_ptr idtr;
void idt_set_gate(uint8_t vector, void *isr, uint16_t cs, uint8_t flags) {
uint64_t addr = (uint64_t)isr;
idt[vector].isr_low = addr & 0xFFFF;
idt[vector].kernel_cs = cs;
idt[vector].ist = 0;
idt[vector].attributes = flags;
idt[vector].isr_mid = (addr >> 16) & 0xFFFF;
idt[vector].isr_high = (addr >> 32) & 0xFFFFFFFF;
idt[vector].reserved = 0;
}
// Remap PIC
static void pic_remap(void) {
uint8_t a1, a2;
a1 = inb(0x21);
a2 = inb(0xA1);
outb(0x20, 0x11); io_wait();
outb(0xA0, 0x11); io_wait();
outb(0x21, 0x20); io_wait(); // Master offset 0x20 (32)
outb(0xA1, 0x28); io_wait(); // Slave offset 0x28 (40)
outb(0x21, 0x04); io_wait();
outb(0xA1, 0x02); io_wait();
outb(0x21, 0x01); io_wait();
outb(0xA1, 0x01); io_wait();
// Restore masks (but verify we don't mask IRQ 1 and 12)
// Actually, simple OSs often just mask everything except what they want.
// Let's unmask IRQ 1 (Keyboard) and IRQ 12 (Mouse) explicitly and mask others.
// 0xFD = 1111 1101 (IRQ 1 unmasked)
// 0xEF = 1110 1111 (IRQ 12 (4 on slave) unmasked)
outb(0x21, 0xF9); // Unmask Keyboard (IRQ1) and Cascade (IRQ2)
outb(0xA1, 0xEF); // Unmask Mouse (IRQ12)
}
// Set up PIT (Programmable Interval Timer) for ~60Hz (16.67ms intervals)
static void pit_setup(void) {
uint16_t divisor = 1193182 / 60; // ~60Hz
// Send command byte
outb(0x43, 0x36); // Channel 0, lobyte/hibyte, mode 3 (square wave), binary
// Send divisor
outb(0x40, divisor & 0xFF);
outb(0x40, (divisor >> 8) & 0xFF);
}
void idt_init(void) {
uint16_t cs;
asm volatile ("mov %%cs, %0" : "=r"(cs));
for (int i = 0; i < IDT_ENTRIES; i++) {
idt[i] = (struct idt_entry){0};
}
pic_remap();
// Unmask IRQ 0 (Timer) in addition to IRQ 1 and 12
outb(0x21, 0xF8); // Unmask Timer (IRQ0), Keyboard (IRQ1) and Cascade (IRQ2)
outb(0xA1, 0xEF); // Unmask Mouse (IRQ12)
pit_setup();
}
void idt_register_interrupts(void) {
uint16_t cs;
asm volatile ("mov %%cs, %0" : "=r"(cs));
idt_set_gate(32, isr0_wrapper, cs, 0x8E); // Timer (IRQ 0)
idt_set_gate(33, isr1_wrapper, cs, 0x8E); // Keyboard (IRQ 1)
idt_set_gate(44, isr12_wrapper, cs, 0x8E); // Mouse (IRQ 12)
}
void idt_load(void) {
idtr.base = (uint64_t)&idt;
idtr.limit = sizeof(struct idt_entry) * IDT_ENTRIES - 1;
asm volatile ("lidt %0" : : "m"(idtr));
asm volatile ("sti");
}

16
src/kernel/idt.h Normal file
View File

@@ -0,0 +1,16 @@
#ifndef IDT_H
#define IDT_H
#include <stdint.h>
void idt_init(void);
void idt_set_gate(uint8_t vector, void *isr, uint16_t cs, uint8_t flags);
void idt_register_interrupts(void);
void idt_load(void);
// ISR wrappers defined in assembly
extern void isr0_wrapper(void); // Timer
extern void isr1_wrapper(void); // Keyboard
extern void isr12_wrapper(void); // Mouse
#endif

65
src/kernel/interrupts.asm Normal file
View File

@@ -0,0 +1,65 @@
section .text
global isr0_wrapper
global isr1_wrapper
global isr12_wrapper
extern timer_handler
extern keyboard_handler
extern mouse_handler
; Helper to send EOI (End of Interrupt) to PIC
send_eoi:
push rax
mov al, 0x20
out 0x20, al ; Master PIC
; If IRQ > 7, send to Slave too (Mouse is IRQ 12)
; We'll handle this in the specific wrappers or C code.
; Actually, simpler to do EOI in C or here.
; Let's just do it in C.
pop rax
ret
%macro ISR_NOERRCODE 1
push rdi
push rsi
push rdx
push rcx
push r8
push r9
push rax
push rbx
push rbp
push r10
push r11
push r12
push r13
push r14
push r15
call %1
pop r15
pop r14
pop r13
pop r12
pop r11
pop r10
pop rbp
pop rbx
pop rax
pop r9
pop r8
pop rcx
pop rdx
pop rsi
pop rdi
iretq
%endmacro
isr0_wrapper:
ISR_NOERRCODE timer_handler
isr1_wrapper:
ISR_NOERRCODE keyboard_handler
isr12_wrapper:
ISR_NOERRCODE mouse_handler

24
src/kernel/io.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef IO_H
#define IO_H
#include <stdint.h>
static inline void outb(uint16_t port, uint8_t val) {
asm volatile ("outb %0, %1" : : "a"(val), "Nd"(port));
}
static inline void outw(uint16_t port, uint16_t val) {
asm volatile ("outw %0, %1" : : "a"(val), "Nd"(port));
}
static inline uint8_t inb(uint16_t port) {
uint8_t ret;
asm volatile ("inb %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
static inline void io_wait(void) {
outb(0x80, 0);
}
#endif

573
src/kernel/licensewr.c Normal file
View File

@@ -0,0 +1,573 @@
#include "fat32.h"
#include <stddef.h>
// Helper function to calculate string length
static size_t lic_strlen(const char *str) {
size_t len = 0;
while (str[len]) len++;
return len;
}
void write_license_file(void) {
FAT32_FileHandle *fh = fat32_open("LICENSE", "w");
if (fh) {
const char *content =
" GNU GENERAL PUBLIC LICENSE\n"
" Version 3, 29 June 2007\n\n"
" Copyright(C) Chris (boreddevnl) 2024-2026\n\n"
" Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>\n"
" Everyone is permitted to copy and distribute verbatim copies\n"
" of this license document, but changing it is not allowed.\n\n"
" Preamble\n\n"
" The GNU General Public License is a free, copyleft license for\n"
"software and other kinds of works.\n\n"
" The licenses for most software and other practical works are designed\n"
"to take away your freedom to share and change the works. By contrast,\n"
"the GNU General Public License is intended to guarantee your freedom to\n"
"share and change all versions of a program--to make sure it remains free\n"
"software for all its users. We, the Free Software Foundation, use the\n"
"GNU General Public License for most of our software; it applies also to\n"
"any other work released this way by its authors. You can apply it to\n"
"your programs, too.\n\n"
" When we speak of free software, we are referring to freedom, not\n"
"price. Our General Public Licenses are designed to make sure that you\n"
"have the freedom to distribute copies of free software (and charge for\n"
"them if you wish), that you receive source code or can get it if you\n"
"want it, that you can change the software or use pieces of it in new\n"
"free programs, and that you know you can do these things.\n\n"
" To protect your rights, we need to prevent others from denying you\n"
"these rights or asking you to surrender the rights. Therefore, you have\n"
"certain responsibilities if you distribute copies of the software, or if\n"
"you modify it: responsibilities to respect the freedom of others.\n\n"
" For example, if you distribute copies of such a program, whether\n"
"gratis or for a fee, you must pass on to the recipients the same\n"
"freedoms that you received. You must make sure that they, too, receive\n"
"or can get the source code. And you must show them these terms so they\n"
"know their rights.\n\n"
" Developers that use the GNU GPL protect your rights with two steps:\n"
"(1) assert copyright on the software, and (2) offer you this License\n"
"giving you legal permission to copy, distribute and/or modify it.\n\n"
" For the developers' and authors' protection, the GPL clearly explains\n"
"that there is no warranty for this free software. For both users' and\n"
"authors' sake, the GPL requires that modified versions be marked as\n"
"changed, so that their problems will not be attributed erroneously to\n"
"authors of previous versions.\n\n"
" Some devices are designed to deny users access to install or run\n"
"modified versions of the software inside them, although the manufacturer\n"
"can do so. This is fundamentally incompatible with the aim of\n"
"protecting users' freedom to change the software. The systematic\n"
"pattern of such abuse occurs in the area of products for individuals to\n"
"use, which is precisely where it is most unacceptable. Therefore, we\n"
"have designed this version of the GPL to prohibit the practice for those\n"
"products. If such problems arise substantially in other domains, we\n"
"stand ready to extend this provision to those domains in future versions\n"
"of the GPL, as needed to protect the freedom of users.\n\n"
" Finally, every program is threatened constantly by software patents.\n"
"States should not allow patents to restrict development and use of\n"
"software on general-purpose computers, but in those that do, we wish to\n"
"avoid the special danger that patents applied to a free program could\n"
"make it effectively proprietary. To prevent this, the GPL assures that\n"
"patents cannot be used to render the program non-free.\n\n"
" The precise terms and conditions for copying, distribution and\n"
"modification follow.\n\n"
" TERMS AND CONDITIONS\n\n"
" 0. Definitions.\n\n"
" \"This License\" refers to version 3 of the GNU General Public License.\n\n"
" \"Copyright\" also means copyright-like laws that apply to other kinds of\n"
"works, such as semiconductor masks.\n\n"
" \"The Program\" refers to any copyrightable work licensed under this\n"
"License. Each licensee is addressed as \"you\". \"Licensees\" and\n"
"\"recipients\" may be individuals or organizations.\n\n"
" To \"modify\" a work means to copy from or adapt all or part of the work\n"
"in a fashion requiring copyright permission, other than the making of an\n"
"exact copy. The resulting work is called a \"modified version\" of the\n"
"earlier work or a work \"based on\" the earlier work.\n\n"
" A \"covered work\" means either the unmodified Program or a work based\n"
"on the Program.\n\n"
" To \"propagate\" a work means to do anything with it that, without\n"
"permission, would make you directly or secondarily liable for\n"
"infringement under applicable copyright law, except executing it on a\n"
"computer or modifying a private copy. Propagation includes copying,\n"
"distribution (with or without modification), making available to the\n"
"public, and in some countries other activities as well.\n\n"
" To \"convey\" a work means any kind of propagation that enables other\n"
"parties to make or receive copies. Mere interaction with a user through\n"
"a computer network, with no transfer of a copy, is not conveying.\n\n"
" An interactive user interface displays \"Appropriate Legal Notices\"\n"
"to the extent that it includes a convenient and prominently visible\n"
"feature that (1) displays an appropriate copyright notice, and (2)\n"
"tells the user that there is no warranty for the work (except to the\n"
"extent that warranties are provided), that licensees may convey the\n"
"work under this License, and how to view a copy of this License. If\n"
"the interface presents a list of user commands or options, such as a\n"
"menu, a prominent item in the list meets this criterion.\n\n"
" 1. Source Code.\n\n"
" The \"source code\" for a work means the preferred form of the work\n"
"for making modifications to it. \"Object code\" means any non-source\n"
"form of a work.\n\n"
" A \"Standard Interface\" means an interface that either is an official\n"
"standard defined by a recognized standards body, or, in the case of\n"
"interfaces specified for a particular programming language, one that\n"
"is widely used among developers working in that language.\n\n"
" The \"System Libraries\" of an executable work include anything, other\n"
"than the work as a whole, that (a) is included in the normal form of\n"
"packaging a Major Component, but which is not part of that Major\n"
"Component, and (b) serves only to enable use of the work with that\n"
"Major Component, or to implement a Standard Interface for which an\n"
"implementation is available to the public in source code form. A\n"
"\"Major Component\", in this context, means a major essential component\n"
"(kernel, window system, and so on) of the specific operating system\n"
"(if any) on which the executable work runs, or a compiler used to\n"
"produce the work, or an object code interpreter used to run it.\n\n"
" The \"Corresponding Source\" for a work in object code form means all\n"
"the source code needed to generate, install, and (for an executable\n"
"work) run the object code and to modify the work, including scripts to\n"
"control those activities. However, it does not include the work's\n"
"System Libraries, or general-purpose tools or generally available free\n"
"programs which are used unmodified in performing those activities but\n"
"which are not part of the work. For example, Corresponding Source\n"
"includes interface definition files associated with source files for\n"
"the work, and the source code for shared libraries and dynamically\n"
"linked subprograms that the work is specifically designed to require,\n"
"such as by intimate data communication or control flow between those\n"
"subprograms and other parts of the work.\n\n"
" The Corresponding Source need not include anything that users\n"
"can regenerate automatically from other parts of the Corresponding\n"
"Source.\n\n"
" The Corresponding Source for a work in source code form is that\n"
"same work.\n\n"
" 2. Basic Permissions.\n\n"
" All rights granted under this License are granted for the term of\n"
"copyright on the Program, and are irrevocable provided the stated\n"
"conditions are met. This License explicitly affirms your unlimited\n"
"permission to run the unmodified Program. The output from running a\n"
"covered work is covered by this License only if the output, given its\n"
"content, constitutes a covered work. This License acknowledges your\n"
"rights of fair use or other equivalent, as provided by copyright law.\n\n"
" You may make, run and propagate covered works that you do not\n"
"convey, without conditions so long as your license otherwise remains\n"
"in force. You may convey covered works to others for the sole purpose\n"
"of having them make modifications exclusively for you, or provide you\n"
"with facilities for running those works, provided that you comply with\n"
"the terms of this License in conveying all material for which you do\n"
"not control copyright. Those thus making or running the covered works\n"
"for you must do so exclusively on your behalf, under your direction\n"
"and control, on terms that prohibit them from making any copies of\n"
"your copyrighted material outside their relationship with you.\n\n"
" Conveying under any other circumstances is permitted solely under\n"
"the conditions stated below. Sublicensing is not allowed; section 10\n"
"makes it unnecessary.\n\n"
" 3. Protecting Users' Legal Rights From Anti-Circumvention Law.\n\n"
" No covered work shall be deemed part of an effective technological\n"
"measure under any applicable law fulfilling obligations under article\n"
"11 of the WIPO copyright treaty adopted on 20 December 1996, or\n"
"similar laws prohibiting or restricting circumvention of such\n"
"measures.\n\n"
" When you convey a covered work, you waive any legal power to forbid\n"
"circumvention of technological measures to the extent such circumvention\n"
"is effected by exercising rights under this License with respect to\n"
"the covered work, and you disclaim any intention to limit operation or\n"
"modification of the work as a means of enforcing, against the work's\n"
"users, your or third parties' legal rights to forbid circumvention of\n"
"technological measures.\n\n"
" 4. Conveying Verbatim Copies.\n\n"
" You may convey verbatim copies of the Program's source code as you\n"
"receive it, in any medium, provided that you conspicuously and\n"
"appropriately publish on each copy an appropriate copyright notice;\n"
"keep intact all notices stating that this License and any\n"
"non-permissive terms added in accord with section 7 apply to the code;\n"
"keep intact all notices of the absence of any warranty; and give all\n"
"recipients a copy of this License along with the Program.\n\n"
" You may charge any price or no price for each copy that you convey,\n"
"and you may offer support or warranty protection for a fee.\n\n"
" 5. Conveying Modified Source Versions.\n\n"
" You may convey a work based on the Program, or the modifications to\n"
"produce it from the Program, in the form of source code under the\n"
"terms of section 4, provided that you also meet all of these conditions:\n\n"
" a) The work must carry prominent notices stating that you modified\n"
" it, and giving a relevant date.\n\n"
" b) The work must carry prominent notices stating that it is\n"
" released under this License and any conditions added under section\n"
" 7. This requirement modifies the requirement in section 4 to\n"
" \"keep intact all notices\".\n\n"
" c) You must license the entire work, as a whole, under this\n"
" License to anyone who comes into possession of a copy. This\n"
" License will therefore apply, along with any applicable section 7\n"
" additional terms, to the whole of the work, and all its parts,\n"
" regardless of how they are packaged. This License gives no\n"
" permission to license the work in any other way, but it does not\n"
" invalidate such permission if you have separately received it.\n\n"
" d) If the work has interactive user interfaces, each must display\n"
" Appropriate Legal Notices; however, if the Program has interactive\n"
" interfaces that do not display Appropriate Legal Notices, your\n"
" work need not make them do so.\n\n"
" A compilation of a covered work with other separate and independent\n"
"works, which are not by their nature extensions of the covered work,\n"
"and which are not combined with it such as to form a larger program,\n"
"in or on a volume of a storage or distribution medium, is called an\n"
"\"aggregate\" if the compilation and its resulting copyright are not\n"
"used to limit the access or legal rights of the compilation's users\n"
"beyond what the individual works permit. Inclusion of a covered work\n"
"in an aggregate does not cause this License to apply to the other\n"
"parts of the aggregate.\n\n"
" 6. Conveying Non-Source Forms.\n\n"
" You may convey a covered work in object code form under the terms\n"
"of sections 4 and 5, provided that you also convey the\n"
"machine-readable Corresponding Source under the terms of this License,\n"
"in one of these ways:\n\n"
" a) Convey the object code in, or embodied in, a physical product\n"
" (including a physical distribution medium), accompanied by the\n"
" Corresponding Source fixed on a durable physical medium\n"
" customarily used for software interchange.\n\n"
" b) Convey the object code in, or embodied in, a physical product\n"
" (including a physical distribution medium), accompanied by a\n"
" written offer, valid for at least three years and valid for as\n"
" long as you offer spare parts or customer support for that product\n"
" model, to give anyone who possesses the object code either (1) a\n"
" copy of the Corresponding Source for all the software in the\n"
" product that is covered by this License, on a durable physical\n"
" medium customarily used for software interchange, for a price no\n"
" more than your reasonable cost of physically performing this\n"
" conveying of source, or (2) access to copy the\n"
" Corresponding Source from a network server at no charge.\n\n"
" c) Convey individual copies of the object code with a copy of the\n"
" written offer to provide the Corresponding Source. This\n"
" alternative is allowed only occasionally and noncommercially, and\n"
" only if you received the object code with such an offer, in accord\n"
" with subsection 6b.\n\n"
" d) Convey the object code by offering access from a designated\n"
" place (gratis or for a charge), and offer equivalent access to the\n"
" Corresponding Source in the same way through the same place at no\n"
" further charge. You need not require recipients to copy the\n"
" Corresponding Source along with the object code. If the place to\n"
" copy the object code is a network server, the Corresponding Source\n"
" may be on a different server (operated by you or a third party)\n"
" that supports equivalent copying facilities, provided you maintain\n"
" clear directions next to the object code saying where to find the\n"
" Corresponding Source. Regardless of what server hosts the\n"
" Corresponding Source, you remain obligated to ensure that it is\n"
" available for as long as needed to satisfy these requirements.\n\n"
" e) Convey the object code using peer-to-peer transmission, provided\n"
" you inform other peers where the object code and Corresponding\n"
" Source of the work are being offered to the general public at no\n"
" charge under subsection 6d.\n\n"
" A separable portion of the object code, whose source code is excluded\n"
"from the Corresponding Source as a System Library, need not be\n"
"included in conveying the object code work.\n\n"
" A \"User Product\" is either (1) a \"consumer product\", which means any\n"
"tangible personal property which is normally used for personal, family,\n"
"or household purposes, or (2) anything designed or sold for incorporation\n"
"into a dwelling. In determining whether a product is a consumer product,\n"
"doubtful cases shall be resolved in favor of coverage. For a particular\n"
"product received by a particular user, \"normally used\" refers to a\n"
"typical or common use of that class of product, regardless of the status\n"
"of the particular user or of the way in which the particular user\n"
"actually uses, or expects or is expected to use, the product. A product\n"
"is a consumer product regardless of whether the product has substantial\n"
"commercial, industrial or non-consumer uses, unless such uses represent\n"
"the only significant mode of use of the product.\n\n"
" \"Installation Information\" for a User Product means any methods,\n"
"procedures, authorization keys, or other information required to install\n"
"and execute modified versions of a covered work in that User Product from\n"
"a modified version of its Corresponding Source. The information must\n"
"suffice to ensure that the continued functioning of the modified object\n"
"code is in no case prevented or interfered with solely because\n"
"modification has been made.\n\n"
" If you convey an object code work under this section in, or with, or\n"
"specifically for use in, a User Product, and the conveying occurs as\n"
"part of a transaction in which the right of possession and use of the\n"
"User Product is transferred to the recipient in perpetuity or for a\n"
"fixed term (regardless of how the transaction is characterized), the\n"
"Corresponding Source conveyed under this section must be accompanied\n"
"by the Installation Information. But this requirement does not apply\n"
"if neither you nor any third party retains the ability to install\n"
"modified object code on the User Product (for example, the work has\n"
"been installed in ROM).\n\n"
" The requirement to provide Installation Information does not include a\n"
"requirement to continue to provide support service, warranty, or updates\n"
"for a work that has been modified or installed by the recipient, or for\n"
"the User Product in which it has been modified or installed. Access to a\n"
"network may be denied when the modification itself materially and\n"
"adversely affects the operation of the network or violates the rules and\n"
"protocols for communication across the network.\n\n"
" Corresponding Source conveyed, and Installation Information provided,\n"
"in accord with this section must be in a format that is publicly\n"
"documented (and with an implementation available to the public in\n"
"source code form), and must require no special password or key for\n"
"unpacking, reading or copying.\n\n"
" 7. Additional Terms.\n\n"
" \"Additional permissions\" are terms that supplement the terms of this\n"
"License by making exceptions from one or more of its conditions.\n"
"Additional permissions that are applicable to the entire Program shall\n"
"be treated as though they were included in this License, to the extent\n"
"that they are valid under applicable law. If additional permissions\n"
"apply only to part of the Program, that part may be used separately\n"
"under those permissions, but the entire Program remains governed by\n"
"this License without regard to the additional permissions.\n\n"
" When you convey a copy of a covered work, you may at your option\n"
"remove any additional permissions from that copy, or from any part of\n"
"it. (Additional permissions may be written to require their own\n"
"removal in certain cases when you modify the work.) You may place\n"
"additional permissions on material, added by you to a covered work,\n"
"for which you have or can give appropriate copyright permission.\n\n"
" Notwithstanding any other provision of this License, for material you\n"
"add to a covered work, you may (if authorized by the copyright holders of\n"
"that material) supplement the terms of this License with terms:\n\n"
" a) Disclaiming warranty or limiting liability differently from the\n"
" terms of sections 15 and 16 of this License; or\n\n"
" b) Requiring preservation of specified reasonable legal notices or\n"
" author attributions in that material or in the Appropriate Legal\n"
" Notices displayed by works containing it; or\n\n"
" c) Prohibiting misrepresentation of the origin of that material, or\n"
" requiring that modified versions of such material be marked in\n"
" reasonable ways as different from the original version; or\n\n"
" d) Limiting the use for publicity purposes of names of licensors or\n"
" authors of the material; or\n\n"
" e) Declining to grant rights under trademark law for use of some\n"
" trade names, trademarks, or service marks; or\n\n"
" f) Requiring indemnification of licensors and authors of that\n"
" material by anyone who conveys the material (or modified versions of\n"
" it) with contractual assumptions of liability to the recipient, for\n"
" any liability that these contractual assumptions directly impose on\n"
" those licensors and authors.\n\n"
" All other non-permissive additional terms are considered \"further\n"
"restrictions\" within the meaning of section 10. If the Program as you\n"
"received it, or any part of it, contains a notice stating that it is\n"
"governed by this License along with a term that is a further\n"
"restriction, you may remove that term. If a license document contains\n"
"a further restriction but permits relicensing or conveying under this\n"
"License, you may add to a covered work material governed by the terms\n"
"of that license document, provided that the further restriction does\n"
"not survive such relicensing or conveying.\n\n"
" If you add terms to a covered work in accord with this section, you\n"
"must place, in the relevant source files, a statement of the\n"
"additional terms that apply to those files, or a notice indicating\n"
"where to find the applicable terms.\n\n"
" Additional terms, permissive or non-permissive, may be stated in the\n"
"form of a separately written license, or stated as exceptions;\n"
"the above requirements apply either way.\n\n"
" 8. Termination.\n\n"
" You may not propagate or modify a covered work except as expressly\n"
"provided under this License. Any attempt otherwise to propagate or\n"
"modify it is void, and will automatically terminate your rights under\n"
"this License (including any patent licenses granted under the third\n"
"paragraph of section 11).\n\n"
" However, if you cease all violation of this License, then your\n"
"license from a particular copyright holder is reinstated (a)\n"
"provisionally, unless and until the copyright holder explicitly and\n"
"finally terminates your license, and (b) permanently, if the copyright\n"
"holder fails to notify you of the violation by some reasonable means\n"
"prior to 60 days after the cessation.\n\n"
" Moreover, your license from a particular copyright holder is\n"
"reinstated permanently if the copyright holder notifies you of the\n"
"violation by some reasonable means, this is the first time you have\n"
"received notice of violation of this License (for any work) from that\n"
"copyright holder, and you cure the violation prior to 30 days after\n"
"your receipt of the notice.\n\n"
" Termination of your rights under this section does not terminate the\n"
"licenses of parties who have received copies or rights from you under\n"
"this License. If your rights have been terminated and not permanently\n"
"reinstated, you do not qualify to receive new licenses for the same\n"
"material under section 10.\n\n"
" 9. Acceptance Not Required for Having Copies.\n\n"
" You are not required to accept this License in order to receive or\n"
"run a copy of the Program. Ancillary propagation of a covered work\n"
"occurring solely as a consequence of using peer-to-peer transmission\n"
"to receive a copy likewise does not require acceptance. However,\n"
"nothing other than this License grants you permission to propagate or\n"
"modify any covered work. These actions infringe copyright if you do\n"
"not accept this License. Therefore, by modifying or propagating a\n"
"covered work, you indicate your acceptance of this License to do so.\n\n"
" 10. Automatic Licensing of Downstream Recipients.\n\n"
" Each time you convey a covered work, the recipient automatically\n"
"receives a license from the original licensors, to run, modify and\n"
"propagate that work, subject to this License. You are not responsible\n"
"for enforcing compliance by third parties with this License.\n\n"
" An \"entity transaction\" is a transaction transferring control of an\n"
"organization, or substantially all assets of one, or subdividing an\n"
"organization, or merging organizations. If propagation of a covered\n"
"work results from an entity transaction, each party to that\n"
"transaction who receives a copy of the work also receives whatever\n"
"licenses to the work the party's predecessor in interest had or could\n"
"give under the previous paragraph, plus a right to possession of the\n"
"Corresponding Source of the work from the predecessor in interest, if\n"
"the predecessor has it or can get it with reasonable efforts.\n\n"
" You may not impose any further restrictions on the exercise of the\n"
"rights granted or affirmed under this License. For example, you may\n"
"not impose a license fee, royalty, or other charge for exercise of\n"
"rights granted under this License, and you may not initiate litigation\n"
"(including a cross-claim or counterclaim in a lawsuit) alleging that\n"
"any patent claim is infringed by making, using, selling, offering for\n"
"sale, or importing the Program or any portion of it.\n\n"
" 11. Patents.\n\n"
" A \"contributor\" is a copyright holder who authorizes use under this\n"
"License of the Program or a work on which the Program is based. The\n"
"work thus licensed is called the contributor's \"contributor version\".\n\n"
" A contributor's \"essential patent claims\" are all patent claims\n"
"owned or controlled by the contributor, whether already acquired or\n"
"hereafter acquired, that would be infringed by some manner, permitted\n"
"by this License, of making, using, or selling its contributor version,\n"
"but do not include claims that would be infringed only as a\n"
"consequence of further modification of the contributor version. For\n"
"purposes of this definition, \"control\" includes the right to grant\n"
"patent sublicenses in a manner consistent with the requirements of\n"
"this License.\n\n"
" Each contributor grants you a non-exclusive, worldwide, royalty-free\n"
"patent license under the contributor's essential patent claims, to\n"
"make, use, sell, offer for sale, import and otherwise run, modify and\n"
"propagate the contents of its contributor version.\n\n"
" In the following three paragraphs, a \"patent license\" is any express\n"
"agreement or commitment, however denominated, not to enforce a patent\n"
"(such as an express permission to practice a patent or covenant not to\n"
"sue for patent infringement). To \"grant\" such a patent license to a\n"
"party means to make such an agreement or commitment not to enforce a\n"
"patent against the party.\n\n"
" If you convey a covered work, knowingly relying on a patent license,\n"
"and the Corresponding Source of the work is not available for anyone\n"
"to copy, free of charge and under the terms of this License, through a\n"
"publicly available network server or other readily accessible means,\n"
"then you must either (1) cause the Corresponding Source to be so\n"
"available, or (2) arrange to deprive yourself of the benefit of the\n"
"patent license for this particular work, or (3) arrange, in a manner\n"
"consistent with the requirements of this License, to extend the patent\n"
"license to downstream recipients. \"Knowingly relying\" means you have\n"
"actual knowledge that, but for the patent license, your conveying the\n"
"covered work in a country, or your recipient's use of the covered work\n"
"in a country, would infringe one or more identifiable patents in that\n"
"country that you have reason to believe are valid.\n\n"
" If, pursuant to or in connection with a single transaction or\n"
"arrangement, you convey, or propagate by procuring conveyance of, a\n"
"covered work, and grant a patent license to some of the parties\n"
"receiving the covered work authorizing them to use, propagate, modify\n"
"or convey a specific copy of the covered work, then the patent license\n"
"you grant is automatically extended to all recipients of the covered\n"
"work and works based on it.\n\n"
" A patent license is \"discriminatory\" if it does not include within\n"
"the scope of its coverage, prohibits the exercise of, or is\n"
"conditioned on the non-exercise of one or more of the rights that are\n"
"specifically granted under this License. You may not convey a covered\n"
"work if you are a party to an arrangement with a third party that is\n"
"in the business of distributing software, under which you make payment\n"
"to the third party based on the extent of your activity of conveying\n"
"the work, and under which the third party grants, to any of the\n"
"parties who would receive the covered work from you, a discriminatory\n"
"patent license (a) in connection with copies of the covered work\n"
"conveyed by you (or copies made from those copies), or (b) primarily\n"
"for and in connection with specific products or compilations that\n"
"contain the covered work, unless you entered into that arrangement,\n"
"or that patent license was granted, prior to 28 March 2007.\n\n"
" Nothing in this License shall be construed as excluding or limiting\n"
"any implied license or other defenses to infringement that may\n"
"otherwise be available to you under applicable patent law.\n\n"
" 12. No Surrender of Others' Freedom.\n\n"
" If conditions are imposed on you (whether by court order, agreement or\n"
"otherwise) that contradict the conditions of this License, they do not\n"
"excuse you from the conditions of this License. If you cannot convey a\n"
"covered work so as to satisfy simultaneously your obligations under this\n"
"License and any other pertinent obligations, then as a consequence you may\n"
"not convey it at all. For example, if you agree to terms that obligate you\n"
"to collect a royalty for further conveying from those to whom you convey\n"
"the Program, the only way you could satisfy both those terms and this\n"
"License would be to refrain entirely from conveying the Program.\n\n"
" 13. Use with the GNU Affero General Public License.\n\n"
" Notwithstanding any other provision of this License, you have\n"
"permission to link or combine any covered work with a work licensed\n"
"under version 3 of the GNU Affero General Public License into a single\n"
"combined work, and to convey the resulting work. The terms of this\n"
"License will continue to apply to the part which is the covered work,\n"
"but the special requirements of the GNU Affero General Public License,\n"
"section 13, concerning interaction through a network will apply to the\n"
"combination as such.\n\n"
" 14. Revised Versions of this License.\n\n"
" The Free Software Foundation may publish revised and/or new versions of\n"
"the GNU General Public License from time to time. Such new versions will\n"
"be similar in spirit to the present version, but may differ in detail to\n"
"address new problems or concerns.\n\n"
" Each version is given a distinguishing version number. If the\n"
"Program specifies that a certain numbered version of the GNU General\n"
"Public License \"or any later version\" applies to it, you have the\n"
"option of following the terms and conditions either of that numbered\n"
"version or of any later version published by the Free Software\n"
"Foundation. If the Program does not specify a version number of the\n"
"GNU General Public License, you may choose any version ever published\n"
"by the Free Software Foundation.\n\n"
" If the Program specifies that a proxy can decide which future\n"
"versions of the GNU General Public License can be used, that proxy's\n"
"public statement of acceptance of a version permanently authorizes you\n"
"to choose that version for the Program.\n\n"
" Later license versions may give you additional or different\n"
"permissions. However, no additional obligations are imposed on any\n"
"author or copyright holder as a result of your choosing to follow a\n"
"later version.\n\n"
" 15. Disclaimer of Warranty.\n\n"
" THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY\n"
"APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT\n"
"HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY\n"
"OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,\n"
"THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\n"
"PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM\n"
"IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF\n"
"ALL NECESSARY SERVICING, REPAIR OR CORRECTION.\n\n"
" 16. Limitation of Liability.\n\n"
" IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\n"
"WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS\n"
"THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY\n"
"GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE\n"
"USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF\n"
"DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD\n"
"PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),\n"
"EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF\n"
"SUCH DAMAGES.\n\n"
" 17. Interpretation of Sections 15 and 16.\n\n"
" If the disclaimer of warranty and limitation of liability provided\n"
"above cannot be given local legal effect according to their terms,\n"
"reviewing courts shall apply local law that most closely approximates\n"
"an absolute waiver of all civil liability in connection with the\n"
"Program, unless a warranty or assumption of liability accompanies a\n"
"copy of the Program in return for a fee.\n\n"
" END OF TERMS AND CONDITIONS\n\n"
" How to Apply These Terms to Your New Programs\n\n"
" If you develop a new program, and you want it to be of the greatest\n"
"possible use to the public, the best way to achieve this is to make it\n"
"free software which everyone can redistribute and change under these terms.\n\n"
" To do so, attach the following notices to the program. It is safest\n"
"to attach them to the start of each source file to most effectively\n"
"state the exclusion of warranty; and each file should have at least\n"
"the \"copyright\" line and a pointer to where the full notice is found.\n\n"
" <one line to give the program's name and a brief idea of what it does.>\n"
" Copyright (C) <year> <name of author>\n\n"
" This program is free software: you can redistribute it and/or modify\n"
" it under the terms of the GNU General Public License as published by\n"
" the Free Software Foundation, either version 3 of the License, or\n"
" (at your option) any later version.\n\n"
" This program is distributed in the hope that it will be useful,\n"
" but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
" GNU General Public License for more details.\n\n"
" You should have received a copy of the GNU General Public License\n"
" along with this program. If not, see <https://www.gnu.org/licenses/>.\n\n"
"Also add information on how to contact you by electronic and paper mail.\n\n"
" If the program does terminal interaction, make it output a short\n"
"notice like this when it starts in an interactive mode:\n\n"
" <program> Copyright (C) <year> <name of author>\n"
" This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.\n"
" This is free software, and you are welcome to redistribute it\n"
" under certain conditions; type `show c' for details.\n\n"
"The hypothetical commands `show w' and `show c' should show the appropriate\n"
"parts of the General Public License. Of course, your program's commands\n"
"might be different; for a GUI interface, you would use an \"about box\".\n\n"
" You should also get your employer (if you work as a programmer) or school,\n"
"if any, to sign a \"copyright disclaimer\" for the program, if necessary.\n"
"For more information on this, and how to apply and follow the GNU GPL, see\n"
"<https://www.gnu.org/licenses/>.\n\n"
" The GNU General Public License does not permit incorporating your program\n"
"into proprietary programs. If your program is a subroutine library, you\n"
"may consider it more useful to permit linking proprietary applications with\n"
"the library. If this is what you want to do, use the GNU Lesser General\n"
"Public License instead of this License. But first, please read\n"
"<https://www.gnu.org/licenses/why-not-lgpl.html>.\n";
fat32_write(fh, (void *)content, lic_strlen(content));
fat32_close(fh);
}
}

6
src/kernel/licensewr.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef LICENSEWR_H
#define LICENSEWR_H
void write_license_file(void);
#endif

579
src/kernel/limine.h Normal file
View File

@@ -0,0 +1,579 @@
/* BSD Zero Clause License */
/* Copyright (C) 2022-2023 mintsuki and contributors.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef _LIMINE_H
#define _LIMINE_H 1
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
/* Misc */
#ifdef LIMINE_NO_POINTERS
# define LIMINE_PTR(TYPE) uint64_t
#else
# define LIMINE_PTR(TYPE) TYPE
#endif
#ifdef __GNUC__
# define LIMINE_DEPRECATED __attribute__((__deprecated__))
# define LIMINE_DEPRECATED_IGNORE_START \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
# define LIMINE_DEPRECATED_IGNORE_END \
_Pragma("GCC diagnostic pop")
#else
# define LIMINE_DEPRECATED
# define LIMINE_DEPRECATED_IGNORE_START
# define LIMINE_DEPRECATED_IGNORE_END
#endif
#define LIMINE_BASE_REVISION(N) \
uint64_t limine_base_revision[3] = { 0xf9562b2d5c95a6c8, 0x6a7b384944536bdc, (N) };
#define LIMINE_BASE_REVISION_SUPPORTED (limine_base_revision[2] == 0)
#define LIMINE_COMMON_MAGIC 0xc7b1dd30df4c8b88, 0x0a82e883a194f07b
struct limine_uuid {
uint32_t a;
uint16_t b;
uint16_t c;
uint8_t d[8];
};
#define LIMINE_MEDIA_TYPE_GENERIC 0
#define LIMINE_MEDIA_TYPE_OPTICAL 1
#define LIMINE_MEDIA_TYPE_TFTP 2
struct limine_file {
uint64_t revision;
LIMINE_PTR(void *) address;
uint64_t size;
LIMINE_PTR(char *) path;
LIMINE_PTR(char *) cmdline;
uint32_t media_type;
uint32_t unused;
uint32_t tftp_ip;
uint32_t tftp_port;
uint32_t partition_index;
uint32_t mbr_disk_id;
struct limine_uuid gpt_disk_uuid;
struct limine_uuid gpt_part_uuid;
struct limine_uuid part_uuid;
};
/* Boot info */
#define LIMINE_BOOTLOADER_INFO_REQUEST { LIMINE_COMMON_MAGIC, 0xf55038d8e2a1202f, 0x279426fcf5f59740 }
struct limine_bootloader_info_response {
uint64_t revision;
LIMINE_PTR(char *) name;
LIMINE_PTR(char *) version;
};
struct limine_bootloader_info_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_bootloader_info_response *) response;
};
/* Stack size */
#define LIMINE_STACK_SIZE_REQUEST { LIMINE_COMMON_MAGIC, 0x224ef0460a8e8926, 0xe1cb0fc25f46ea3d }
struct limine_stack_size_response {
uint64_t revision;
};
struct limine_stack_size_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_stack_size_response *) response;
uint64_t stack_size;
};
/* HHDM */
#define LIMINE_HHDM_REQUEST { LIMINE_COMMON_MAGIC, 0x48dcf1cb8ad2b852, 0x63984e959a98244b }
struct limine_hhdm_response {
uint64_t revision;
uint64_t offset;
};
struct limine_hhdm_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_hhdm_response *) response;
};
/* Framebuffer */
#define LIMINE_FRAMEBUFFER_REQUEST { LIMINE_COMMON_MAGIC, 0x9d5827dcd881dd75, 0xa3148604f6fab11b }
#define LIMINE_FRAMEBUFFER_RGB 1
struct limine_video_mode {
uint64_t pitch;
uint64_t width;
uint64_t height;
uint16_t bpp;
uint8_t memory_model;
uint8_t red_mask_size;
uint8_t red_mask_shift;
uint8_t green_mask_size;
uint8_t green_mask_shift;
uint8_t blue_mask_size;
uint8_t blue_mask_shift;
};
struct limine_framebuffer {
LIMINE_PTR(void *) address;
uint64_t width;
uint64_t height;
uint64_t pitch;
uint16_t bpp;
uint8_t memory_model;
uint8_t red_mask_size;
uint8_t red_mask_shift;
uint8_t green_mask_size;
uint8_t green_mask_shift;
uint8_t blue_mask_size;
uint8_t blue_mask_shift;
uint8_t unused[7];
uint64_t edid_size;
LIMINE_PTR(void *) edid;
/* Response revision 1 */
uint64_t mode_count;
LIMINE_PTR(struct limine_video_mode **) modes;
};
struct limine_framebuffer_response {
uint64_t revision;
uint64_t framebuffer_count;
LIMINE_PTR(struct limine_framebuffer **) framebuffers;
};
struct limine_framebuffer_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_framebuffer_response *) response;
};
/* Terminal */
#define LIMINE_TERMINAL_REQUEST { LIMINE_COMMON_MAGIC, 0xc8ac59310c2b0844, 0xa68d0c7265d38878 }
#define LIMINE_TERMINAL_CB_DEC 10
#define LIMINE_TERMINAL_CB_BELL 20
#define LIMINE_TERMINAL_CB_PRIVATE_ID 30
#define LIMINE_TERMINAL_CB_STATUS_REPORT 40
#define LIMINE_TERMINAL_CB_POS_REPORT 50
#define LIMINE_TERMINAL_CB_KBD_LEDS 60
#define LIMINE_TERMINAL_CB_MODE 70
#define LIMINE_TERMINAL_CB_LINUX 80
#define LIMINE_TERMINAL_CTX_SIZE ((uint64_t)(-1))
#define LIMINE_TERMINAL_CTX_SAVE ((uint64_t)(-2))
#define LIMINE_TERMINAL_CTX_RESTORE ((uint64_t)(-3))
#define LIMINE_TERMINAL_FULL_REFRESH ((uint64_t)(-4))
/* Response revision 1 */
#define LIMINE_TERMINAL_OOB_OUTPUT_GET ((uint64_t)(-10))
#define LIMINE_TERMINAL_OOB_OUTPUT_SET ((uint64_t)(-11))
#define LIMINE_TERMINAL_OOB_OUTPUT_OCRNL (1 << 0)
#define LIMINE_TERMINAL_OOB_OUTPUT_OFDEL (1 << 1)
#define LIMINE_TERMINAL_OOB_OUTPUT_OFILL (1 << 2)
#define LIMINE_TERMINAL_OOB_OUTPUT_OLCUC (1 << 3)
#define LIMINE_TERMINAL_OOB_OUTPUT_ONLCR (1 << 4)
#define LIMINE_TERMINAL_OOB_OUTPUT_ONLRET (1 << 5)
#define LIMINE_TERMINAL_OOB_OUTPUT_ONOCR (1 << 6)
#define LIMINE_TERMINAL_OOB_OUTPUT_OPOST (1 << 7)
LIMINE_DEPRECATED_IGNORE_START
struct LIMINE_DEPRECATED limine_terminal;
typedef void (*limine_terminal_write)(struct limine_terminal *, const char *, uint64_t);
typedef void (*limine_terminal_callback)(struct limine_terminal *, uint64_t, uint64_t, uint64_t, uint64_t);
struct LIMINE_DEPRECATED limine_terminal {
uint64_t columns;
uint64_t rows;
LIMINE_PTR(struct limine_framebuffer *) framebuffer;
};
struct LIMINE_DEPRECATED limine_terminal_response {
uint64_t revision;
uint64_t terminal_count;
LIMINE_PTR(struct limine_terminal **) terminals;
LIMINE_PTR(limine_terminal_write) write;
};
struct LIMINE_DEPRECATED limine_terminal_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_terminal_response *) response;
LIMINE_PTR(limine_terminal_callback) callback;
};
LIMINE_DEPRECATED_IGNORE_END
/* Paging mode */
#define LIMINE_PAGING_MODE_REQUEST { LIMINE_COMMON_MAGIC, 0x95c1a0edab0944cb, 0xa4e5cb3842f7488a }
#if defined (__x86_64__) || defined (__i386__)
#define LIMINE_PAGING_MODE_X86_64_4LVL 0
#define LIMINE_PAGING_MODE_X86_64_5LVL 1
#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_X86_64_5LVL
#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_X86_64_4LVL
#elif defined (__aarch64__)
#define LIMINE_PAGING_MODE_AARCH64_4LVL 0
#define LIMINE_PAGING_MODE_AARCH64_5LVL 1
#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_AARCH64_5LVL
#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_AARCH64_4LVL
#elif defined (__riscv) && (__riscv_xlen == 64)
#define LIMINE_PAGING_MODE_RISCV_SV39 0
#define LIMINE_PAGING_MODE_RISCV_SV48 1
#define LIMINE_PAGING_MODE_RISCV_SV57 2
#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_RISCV_SV57
#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_RISCV_SV48
#else
#error Unknown architecture
#endif
struct limine_paging_mode_response {
uint64_t revision;
uint64_t mode;
uint64_t flags;
};
struct limine_paging_mode_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_paging_mode_response *) response;
uint64_t mode;
uint64_t flags;
};
/* 5-level paging */
#define LIMINE_5_LEVEL_PAGING_REQUEST { LIMINE_COMMON_MAGIC, 0x94469551da9b3192, 0xebe5e86db7382888 }
LIMINE_DEPRECATED_IGNORE_START
struct LIMINE_DEPRECATED limine_5_level_paging_response {
uint64_t revision;
};
struct LIMINE_DEPRECATED limine_5_level_paging_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_5_level_paging_response *) response;
};
LIMINE_DEPRECATED_IGNORE_END
/* SMP */
#define LIMINE_SMP_REQUEST { LIMINE_COMMON_MAGIC, 0x95a67b819a1b857e, 0xa0b61b723b6a73e0 }
struct limine_smp_info;
typedef void (*limine_goto_address)(struct limine_smp_info *);
#if defined (__x86_64__) || defined (__i386__)
#define LIMINE_SMP_X2APIC (1 << 0)
struct limine_smp_info {
uint32_t processor_id;
uint32_t lapic_id;
uint64_t reserved;
LIMINE_PTR(limine_goto_address) goto_address;
uint64_t extra_argument;
};
struct limine_smp_response {
uint64_t revision;
uint32_t flags;
uint32_t bsp_lapic_id;
uint64_t cpu_count;
LIMINE_PTR(struct limine_smp_info **) cpus;
};
#elif defined (__aarch64__)
struct limine_smp_info {
uint32_t processor_id;
uint32_t gic_iface_no;
uint64_t mpidr;
uint64_t reserved;
LIMINE_PTR(limine_goto_address) goto_address;
uint64_t extra_argument;
};
struct limine_smp_response {
uint64_t revision;
uint64_t flags;
uint64_t bsp_mpidr;
uint64_t cpu_count;
LIMINE_PTR(struct limine_smp_info **) cpus;
};
#elif defined (__riscv) && (__riscv_xlen == 64)
struct limine_smp_info {
uint64_t processor_id;
uint64_t hartid;
uint64_t reserved;
LIMINE_PTR(limine_goto_address) goto_address;
uint64_t extra_argument;
};
struct limine_smp_response {
uint64_t revision;
uint64_t flags;
uint64_t bsp_hartid;
uint64_t cpu_count;
LIMINE_PTR(struct limine_smp_info **) cpus;
};
#else
#error Unknown architecture
#endif
struct limine_smp_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_smp_response *) response;
uint64_t flags;
};
/* Memory map */
#define LIMINE_MEMMAP_REQUEST { LIMINE_COMMON_MAGIC, 0x67cf3d9d378a806f, 0xe304acdfc50c3c62 }
#define LIMINE_MEMMAP_USABLE 0
#define LIMINE_MEMMAP_RESERVED 1
#define LIMINE_MEMMAP_ACPI_RECLAIMABLE 2
#define LIMINE_MEMMAP_ACPI_NVS 3
#define LIMINE_MEMMAP_BAD_MEMORY 4
#define LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE 5
#define LIMINE_MEMMAP_KERNEL_AND_MODULES 6
#define LIMINE_MEMMAP_FRAMEBUFFER 7
struct limine_memmap_entry {
uint64_t base;
uint64_t length;
uint64_t type;
};
struct limine_memmap_response {
uint64_t revision;
uint64_t entry_count;
LIMINE_PTR(struct limine_memmap_entry **) entries;
};
struct limine_memmap_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_memmap_response *) response;
};
/* Entry point */
#define LIMINE_ENTRY_POINT_REQUEST { LIMINE_COMMON_MAGIC, 0x13d86c035a1cd3e1, 0x2b0caa89d8f3026a }
typedef void (*limine_entry_point)(void);
struct limine_entry_point_response {
uint64_t revision;
};
struct limine_entry_point_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_entry_point_response *) response;
LIMINE_PTR(limine_entry_point) entry;
};
/* Kernel File */
#define LIMINE_KERNEL_FILE_REQUEST { LIMINE_COMMON_MAGIC, 0xad97e90e83f1ed67, 0x31eb5d1c5ff23b69 }
struct limine_kernel_file_response {
uint64_t revision;
LIMINE_PTR(struct limine_file *) kernel_file;
};
struct limine_kernel_file_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_kernel_file_response *) response;
};
/* Module */
#define LIMINE_MODULE_REQUEST { LIMINE_COMMON_MAGIC, 0x3e7e279702be32af, 0xca1c4f3bd1280cee }
#define LIMINE_INTERNAL_MODULE_REQUIRED (1 << 0)
#define LIMINE_INTERNAL_MODULE_COMPRESSED (1 << 1)
struct limine_internal_module {
LIMINE_PTR(const char *) path;
LIMINE_PTR(const char *) cmdline;
uint64_t flags;
};
struct limine_module_response {
uint64_t revision;
uint64_t module_count;
LIMINE_PTR(struct limine_file **) modules;
};
struct limine_module_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_module_response *) response;
/* Request revision 1 */
uint64_t internal_module_count;
LIMINE_PTR(struct limine_internal_module **) internal_modules;
};
/* RSDP */
#define LIMINE_RSDP_REQUEST { LIMINE_COMMON_MAGIC, 0xc5e77b6b397e7b43, 0x27637845accdcf3c }
struct limine_rsdp_response {
uint64_t revision;
LIMINE_PTR(void *) address;
};
struct limine_rsdp_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_rsdp_response *) response;
};
/* SMBIOS */
#define LIMINE_SMBIOS_REQUEST { LIMINE_COMMON_MAGIC, 0x9e9046f11e095391, 0xaa4a520fefbde5ee }
struct limine_smbios_response {
uint64_t revision;
LIMINE_PTR(void *) entry_32;
LIMINE_PTR(void *) entry_64;
};
struct limine_smbios_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_smbios_response *) response;
};
/* EFI system table */
#define LIMINE_EFI_SYSTEM_TABLE_REQUEST { LIMINE_COMMON_MAGIC, 0x5ceba5163eaaf6d6, 0x0a6981610cf65fcc }
struct limine_efi_system_table_response {
uint64_t revision;
LIMINE_PTR(void *) address;
};
struct limine_efi_system_table_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_efi_system_table_response *) response;
};
/* EFI memory map */
#define LIMINE_EFI_MEMMAP_REQUEST { LIMINE_COMMON_MAGIC, 0x7df62a431d6872d5, 0xa4fcdfb3e57306c8 }
struct limine_efi_memmap_response {
uint64_t revision;
LIMINE_PTR(void *) memmap;
uint64_t memmap_size;
uint64_t desc_size;
uint64_t desc_version;
};
struct limine_efi_memmap_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_efi_memmap_response *) response;
};
/* Boot time */
#define LIMINE_BOOT_TIME_REQUEST { LIMINE_COMMON_MAGIC, 0x502746e184c088aa, 0xfbc5ec83e6327893 }
struct limine_boot_time_response {
uint64_t revision;
int64_t boot_time;
};
struct limine_boot_time_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_boot_time_response *) response;
};
/* Kernel address */
#define LIMINE_KERNEL_ADDRESS_REQUEST { LIMINE_COMMON_MAGIC, 0x71ba76863cc55f63, 0xb2644a48c516a487 }
struct limine_kernel_address_response {
uint64_t revision;
uint64_t physical_base;
uint64_t virtual_base;
};
struct limine_kernel_address_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_kernel_address_response *) response;
};
/* Device Tree Blob */
#define LIMINE_DTB_REQUEST { LIMINE_COMMON_MAGIC, 0xb40ddb48fb54bac7, 0x545081493f81ffb7 }
struct limine_dtb_response {
uint64_t revision;
LIMINE_PTR(void *) dtb_ptr;
};
struct limine_dtb_request {
uint64_t id[4];
uint64_t revision;
LIMINE_PTR(struct limine_dtb_response *) response;
};
#ifdef __cplusplus
}
#endif
#endif

79
src/kernel/main.c Normal file
View File

@@ -0,0 +1,79 @@
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include "limine.h"
#include "graphics.h"
#include "idt.h"
#include "ps2.h"
#include "wm.h"
#include "io.h"
#include "memory_manager.h"
// --- Limine Requests ---
__attribute__((used, section(".requests")))
static volatile LIMINE_BASE_REVISION(2);
__attribute__((used, section(".requests")))
static volatile struct limine_framebuffer_request framebuffer_request = {
.id = LIMINE_FRAMEBUFFER_REQUEST,
.revision = 1
};
__attribute__((used, section(".requests_start")))
static volatile struct limine_request *const requests_start_marker[] = {
(struct limine_request *)&framebuffer_request,
NULL
};
__attribute__((used, section(".requests_end")))
static volatile struct limine_request *const requests_end_marker[] = {
NULL
};
static void hcf(void) {
asm("cli");
for (;;) {
asm("hlt");
}
}
// Kernel Entry Point
void kmain(void) {
// 1. Graphics Init
if (LIMINE_BASE_REVISION_SUPPORTED == false) {
// Warning
}
if (framebuffer_request.response == NULL || framebuffer_request.response->framebuffer_count < 1) {
hcf();
}
struct limine_framebuffer *fb = framebuffer_request.response->framebuffers[0];
graphics_init(fb);
// 2. Interrupts Init
idt_init();
// Register ISRs with correct CS
idt_register_interrupts();
// Load IDT and Enable Interrupts
idt_load();
// 2.5 Memory Manager Init
memory_manager_init();
// 3. PS/2 Init (Mouse/Keyboard)
asm("cli");
ps2_init();
asm("sti");
// 4. Window Manager Init (Draws initial desktop)
wm_init();
// 5. Main loop - just wait for interrupts
// Timer interrupt will drive the redraw system
while (1) {
asm("hlt");
}
}

434
src/kernel/memory_manager.c Normal file
View File

@@ -0,0 +1,434 @@
#include "memory_manager.h"
#include "io.h"
#include <stdint.h>
// --- Internal State ---
static uint8_t memory_pool[MEMORY_POOL_SIZE] __attribute__((aligned(4096)));
static MemBlock block_list[MAX_ALLOCATIONS];
static int block_count = 0;
static size_t total_allocated = 0;
static size_t peak_allocated = 0;
static uint32_t allocation_counter = 0;
static bool initialized = false;
// --- Helper Functions ---
// Simple memset for internal use
static void mem_memset(void *dest, int val, size_t len) {
uint8_t *ptr = (uint8_t *)dest;
while (len-- > 0) {
*ptr++ = (uint8_t)val;
}
}
// Simple memmove
static void mem_memmove(void *dest, const void *src, size_t len) {
uint8_t *d = (uint8_t *)dest;
const uint8_t *s = (const uint8_t *)src;
if (d < s) {
while (len--) *d++ = *s++;
} else {
d += len;
s += len;
while (len--) *(--d) = *(--s);
}
}
// Get current time in ticks (simple counter)
static uint32_t get_timestamp(void) {
static uint32_t tick = 0;
return tick++;
}
// Find free space in memory pool
static void* find_free_space(size_t size) {
size_t offset = 0;
while (offset + size <= MEMORY_POOL_SIZE) {
bool space_free = true;
// Check if this range is free
for (int i = 0; i < block_count; i++) {
if (!block_list[i].allocated) continue;
void *block_start = block_list[i].address;
void *block_end = (uint8_t *)block_start + block_list[i].size;
void *check_start = (uint8_t *)memory_pool + offset;
void *check_end = (uint8_t *)check_start + size;
// Check for overlap
if (check_start < block_end && check_end > block_start) {
space_free = false;
// Skip past this block
size_t block_offset = (uintptr_t)block_end - (uintptr_t)memory_pool;
if (block_offset > offset) {
offset = block_offset;
}
break;
}
}
if (space_free) {
return (uint8_t *)memory_pool + offset;
}
}
return NULL;
}
// Calculate fragmentation
static size_t calculate_fragmentation(void) {
if (total_allocated == 0) return 0;
// Sort blocks by address
for (int i = 0; i < block_count - 1; i++) {
for (int j = i + 1; j < block_count; j++) {
if ((uintptr_t)block_list[i].address > (uintptr_t)block_list[j].address) {
MemBlock tmp = block_list[i];
block_list[i] = block_list[j];
block_list[j] = tmp;
}
}
}
// Count gaps between allocated blocks
size_t total_gaps = 0;
void *pool_end = (uint8_t *)memory_pool + MEMORY_POOL_SIZE;
void *current_end = memory_pool;
for (int i = 0; i < block_count; i++) {
if (!block_list[i].allocated) continue;
if (block_list[i].address > current_end) {
total_gaps += (uintptr_t)block_list[i].address - (uintptr_t)current_end;
}
current_end = (uint8_t *)block_list[i].address + block_list[i].size;
}
if (total_allocated == 0) return 0;
return (total_gaps * 100) / total_allocated;
}
// --- Public API ---
void memory_manager_init(void) {
if (initialized) return;
// Clear metadata
mem_memset(block_list, 0, sizeof(block_list));
block_count = 0;
total_allocated = 0;
peak_allocated = 0;
allocation_counter = 0;
// Create initial free block representing entire pool
block_list[0].address = memory_pool;
block_list[0].size = MEMORY_POOL_SIZE;
block_list[0].allocated = false;
block_list[0].allocation_id = 0;
block_count = 1;
initialized = true;
}
void* kmalloc(size_t size) {
if (!initialized) {
memory_manager_init();
}
if (size == 0 || size > MEMORY_POOL_SIZE) {
return NULL;
}
// Check if we can allocate
if (total_allocated + size > MEMORY_POOL_SIZE) {
return NULL;
}
// Find free space
void *ptr = find_free_space(size);
if (ptr == NULL) {
return NULL;
}
// Add block entry
if (block_count >= MAX_ALLOCATIONS) {
return NULL;
}
allocation_counter++;
int idx = block_count++;
block_list[idx].address = ptr;
block_list[idx].size = size;
block_list[idx].allocated = true;
block_list[idx].allocation_id = allocation_counter;
block_list[idx].timestamp = get_timestamp();
total_allocated += size;
if (total_allocated > peak_allocated) {
peak_allocated = total_allocated;
}
// Clear memory
mem_memset(ptr, 0, size);
return ptr;
}
void kfree(void *ptr) {
if (ptr == NULL || !initialized) {
return;
}
// Find and free the block
for (int i = 0; i < block_count; i++) {
if (block_list[i].allocated && block_list[i].address == ptr) {
total_allocated -= block_list[i].size;
block_list[i].allocated = false;
// Compact: remove freed entry and shift remaining
for (int j = i; j < block_count - 1; j++) {
block_list[j] = block_list[j + 1];
}
block_count--;
return;
}
}
}
void* krealloc(void *ptr, size_t new_size) {
if (!initialized) {
memory_manager_init();
}
if (new_size == 0) {
kfree(ptr);
return NULL;
}
if (ptr == NULL) {
return kmalloc(new_size);
}
// Find the block
for (int i = 0; i < block_count; i++) {
if (block_list[i].allocated && block_list[i].address == ptr) {
if (block_list[i].size >= new_size) {
// Allocation is large enough
return ptr;
}
// Need to allocate new space
void *new_ptr = kmalloc(new_size);
if (new_ptr == NULL) {
return NULL;
}
// Copy data
mem_memmove(new_ptr, ptr, block_list[i].size);
// Free old pointer
kfree(ptr);
return new_ptr;
}
}
return NULL;
}
MemStats memory_get_stats(void) {
MemStats stats;
stats.total_memory = MEMORY_POOL_SIZE;
stats.used_memory = total_allocated;
stats.available_memory = MEMORY_POOL_SIZE - total_allocated;
stats.allocated_blocks = 0;
stats.free_blocks = 0;
stats.largest_free_block = 0;
stats.smallest_free_block = MEMORY_POOL_SIZE;
stats.peak_memory_used = peak_allocated;
// Count and analyze blocks
for (int i = 0; i < block_count; i++) {
if (block_list[i].allocated) {
stats.allocated_blocks++;
} else {
stats.free_blocks++;
if (block_list[i].size > stats.largest_free_block) {
stats.largest_free_block = block_list[i].size;
}
if (block_list[i].size < stats.smallest_free_block) {
stats.smallest_free_block = block_list[i].size;
}
}
}
if (stats.free_blocks == 0) {
stats.smallest_free_block = 0;
}
stats.fragmentation_percent = calculate_fragmentation();
return stats;
}
void memory_print_stats(void) {
MemStats stats = memory_get_stats();
// We need to use the CLI write functions - declare them as extern
extern void cmd_write(const char *str);
extern void cmd_write_int(int n);
extern void cmd_putchar(char c);
cmd_write("\n=== MEMORY STATISTICS ===\n");
cmd_write("Total Memory: ");
cmd_write_int(stats.total_memory / 1024);
cmd_write(" KB\n");
cmd_write("Used Memory: ");
cmd_write_int(stats.used_memory / 1024);
cmd_write(" KB\n");
cmd_write("Available Memory: ");
cmd_write_int(stats.available_memory / 1024);
cmd_write(" KB\n");
cmd_write("Allocated Blocks: ");
cmd_write_int(stats.allocated_blocks);
cmd_write("\n");
cmd_write("Free Blocks: ");
cmd_write_int(stats.free_blocks);
cmd_write("\n");
cmd_write("Largest Free: ");
cmd_write_int(stats.largest_free_block / 1024);
cmd_write(" KB\n");
cmd_write("Peak Usage: ");
cmd_write_int(stats.peak_memory_used / 1024);
cmd_write(" KB\n");
cmd_write("Fragmentation: ");
cmd_write_int(stats.fragmentation_percent);
cmd_write("%\n");
cmd_write("Usage: ");
int usage_percent = (stats.used_memory * 100) / stats.total_memory;
cmd_write_int(usage_percent);
cmd_write("%\n");
cmd_write("========================\n\n");
}
void memory_print_detailed(void) {
extern void cmd_write(const char *str);
extern void cmd_write_int(int n);
extern void cmd_putchar(char c);
cmd_write("\n=== DETAILED MEMORY BLOCKS ===\n");
cmd_write("ID Address Size Status\n");
cmd_write("------ -------- -------- --------\n");
for (int i = 0; i < block_count; i++) {
if (block_list[i].size == 0) continue;
// ID
cmd_write_int(block_list[i].allocation_id);
cmd_write(" ");
// Address (simplified hex output)
cmd_write("0x");
cmd_write_int((uintptr_t)block_list[i].address / 1024);
cmd_write(" ");
// Size
cmd_write_int(block_list[i].size / 1024);
cmd_write("KB ");
// Status
if (block_list[i].allocated) {
cmd_write("ALLOC\n");
} else {
cmd_write("FREE\n");
}
}
cmd_write("==============================\n\n");
}
void memory_validate(void) {
extern void cmd_write(const char *str);
extern void cmd_write_int(int n);
int errors = 0;
// Check for overlapping blocks
for (int i = 0; i < block_count; i++) {
for (int j = i + 1; j < block_count; j++) {
void *i_start = block_list[i].address;
void *i_end = (uint8_t *)i_start + block_list[i].size;
void *j_start = block_list[j].address;
void *j_end = (uint8_t *)j_start + block_list[j].size;
if (i_start < j_end && i_end > j_start) {
errors++;
cmd_write("ERROR: Overlapping blocks detected!\n");
}
}
}
if (errors == 0) {
cmd_write("Memory validation: OK\n");
} else {
cmd_write("Memory validation failed with ");
cmd_write_int(errors);
cmd_write(" errors\n");
}
}
void memory_dump_blocks(void) {
extern void cmd_write(const char *str);
extern void cmd_write_int(int n);
cmd_write("\nMemory block dump:\n");
cmd_write("Total blocks: ");
cmd_write_int(block_count);
cmd_write("\n");
memory_print_detailed();
}
size_t memory_get_peak_usage(void) {
return peak_allocated;
}
void memory_reset_peak(void) {
peak_allocated = total_allocated;
}
bool memory_is_valid_ptr(void *ptr) {
if (ptr == NULL) return false;
void *pool_start = memory_pool;
void *pool_end = (uint8_t *)memory_pool + MEMORY_POOL_SIZE;
if (ptr < pool_start || ptr >= pool_end) {
return false;
}
// Check if it's an allocated block
for (int i = 0; i < block_count; i++) {
if (block_list[i].allocated && block_list[i].address == ptr) {
return true;
}
}
return false;
}

View File

@@ -0,0 +1,56 @@
#ifndef MEMORY_MANAGER_H
#define MEMORY_MANAGER_H
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
// Memory Manager Configuration
#define MEMORY_POOL_SIZE (4 * 1024 * 1024) // 4MB pool
#define MAX_ALLOCATIONS 1024
#define MAX_FRAGMENTATION_SLOTS 2048
// Allocation block metadata
typedef struct {
void *address;
size_t size;
bool allocated;
uint32_t allocation_id;
uint32_t timestamp;
} MemBlock;
// Memory statistics
typedef struct {
size_t total_memory;
size_t used_memory;
size_t available_memory;
size_t allocated_blocks;
size_t free_blocks;
size_t largest_free_block;
size_t smallest_free_block;
size_t fragmentation_percent;
size_t peak_memory_used;
} MemStats;
// Public API
void memory_manager_init(void);
// Allocation/Deallocation
void* kmalloc(size_t size);
void kfree(void *ptr);
void* krealloc(void *ptr, size_t new_size);
// Statistics and Information
MemStats memory_get_stats(void);
void memory_print_stats(void);
void memory_print_detailed(void);
void memory_validate(void);
void memory_dump_blocks(void);
// Internal utilities
size_t memory_get_peak_usage(void);
void memory_reset_peak(void);
bool memory_is_valid_ptr(void *ptr);
#endif // MEMORY_MANAGER_H

248
src/kernel/minesweeper.c Normal file
View File

@@ -0,0 +1,248 @@
#include "minesweeper.h"
#include "graphics.h"
#include "wm.h"
#include <stdbool.h>
#include <stddef.h>
Window win_minesweeper;
// Game constants
#define GRID_WIDTH 10
#define GRID_HEIGHT 10
#define MINE_COUNT 10
#define CELL_SIZE 20
// Game state
static int grid[GRID_HEIGHT][GRID_WIDTH]; // -1 = mine, 0-8 = adjacent mine count
static bool revealed[GRID_HEIGHT][GRID_WIDTH];
static bool flagged[GRID_HEIGHT][GRID_WIDTH];
static bool game_over = false;
static bool game_won = false;
static int revealed_count = 0;
// Helper: Random number generator (simple LCG)
static uint32_t random_seed = 12345;
static uint32_t random_next(void) {
random_seed = random_seed * 1103515245 + 12345;
return (random_seed / 65536) % 32768;
}
static void init_game(void) {
// Clear arrays
for (int y = 0; y < GRID_HEIGHT; y++) {
for (int x = 0; x < GRID_WIDTH; x++) {
grid[y][x] = 0;
revealed[y][x] = false;
flagged[y][x] = false;
}
}
// Place mines randomly
int mines_placed = 0;
while (mines_placed < MINE_COUNT) {
int x = random_next() % GRID_WIDTH;
int y = random_next() % GRID_HEIGHT;
if (grid[y][x] != -1) {
grid[y][x] = -1;
mines_placed++;
}
}
// Calculate adjacent mine counts
for (int y = 0; y < GRID_HEIGHT; y++) {
for (int x = 0; x < GRID_WIDTH; x++) {
if (grid[y][x] != -1) {
int count = 0;
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
int ny = y + dy;
int nx = x + dx;
if (ny >= 0 && ny < GRID_HEIGHT && nx >= 0 && nx < GRID_WIDTH) {
if (grid[ny][nx] == -1) count++;
}
}
}
grid[y][x] = count;
}
}
}
game_over = false;
game_won = false;
revealed_count = 0;
}
static void reveal_cell(int x, int y);
// Flood fill for empty cells
static void flood_fill(int x, int y) {
if (x < 0 || x >= GRID_WIDTH || y < 0 || y >= GRID_HEIGHT) return;
if (revealed[y][x] || flagged[y][x]) return;
if (grid[y][x] == -1) return;
revealed[y][x] = true;
revealed_count++;
// If cell is empty, reveal adjacent cells
if (grid[y][x] == 0) {
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
flood_fill(x + dx, y + dy);
}
}
}
}
static void reveal_cell(int x, int y) {
if (x < 0 || x >= GRID_WIDTH || y < 0 || y >= GRID_HEIGHT) return;
if (revealed[y][x] || flagged[y][x]) return;
if (grid[y][x] == -1) {
// Hit a mine - game over
game_over = true;
// Reveal all mines
for (int yy = 0; yy < GRID_HEIGHT; yy++) {
for (int xx = 0; xx < GRID_WIDTH; xx++) {
if (grid[yy][xx] == -1) {
revealed[yy][xx] = true;
}
}
}
} else if (grid[y][x] == 0) {
// Empty cell - flood fill
flood_fill(x, y);
} else {
// Numbered cell
revealed[y][x] = true;
revealed_count++;
}
// Check win condition
if (revealed_count == (GRID_WIDTH * GRID_HEIGHT - MINE_COUNT)) {
game_won = true;
}
}
static void flag_cell(int x, int y) {
if (x < 0 || x >= GRID_WIDTH || y < 0 || y >= GRID_HEIGHT) return;
if (revealed[y][x]) return;
flagged[y][x] = !flagged[y][x];
}
static void minesweeper_right_click(Window *win, int x, int y) {
// x and y are relative to window content (0,0 is top-left of window)
int grid_start_x = 10;
int grid_start_y = 50;
// Check grid cells
if (x >= grid_start_x && x < grid_start_x + GRID_WIDTH * CELL_SIZE &&
y >= grid_start_y && y < grid_start_y + GRID_HEIGHT * CELL_SIZE) {
if (game_over || game_won) return;
int cell_x = (x - grid_start_x) / CELL_SIZE;
int cell_y = (y - grid_start_y) / CELL_SIZE;
flag_cell(cell_x, cell_y);
wm_paint();
}
}
static void minesweeper_paint(Window *win) {
// Background
draw_rect(win->x + 4, win->y + 24, win->w - 8, win->h - 28, COLOR_LTGRAY);
// Game status
if (game_over) {
draw_string(win->x + 10, win->y + 30, "Game Over!", COLOR_RED);
} else if (game_won) {
draw_string(win->x + 10, win->y + 30, "You Won!", COLOR_BLUE);
} else {
draw_string(win->x + 10, win->y + 30, "Minesweeper", COLOR_BLACK);
}
// Draw grid
int grid_start_x = win->x + 10;
int grid_start_y = win->y + 50;
for (int y = 0; y < GRID_HEIGHT; y++) {
for (int x = 0; x < GRID_WIDTH; x++) {
int px = grid_start_x + x * CELL_SIZE;
int py = grid_start_y + y * CELL_SIZE;
if (revealed[y][x]) {
// Revealed cell - sunken
draw_bevel_rect(px, py, CELL_SIZE, CELL_SIZE, true);
if (grid[y][x] == -1) {
// Mine
draw_string(px + 8, py + 6, "*", COLOR_RED);
} else if (grid[y][x] > 0) {
// Number
char num[2] = { '0' + grid[y][x], 0 };
draw_string(px + 8, py + 6, num, COLOR_BLACK);
}
// 0 = empty, nothing to draw
} else {
// Unrevealed cell - raised
draw_bevel_rect(px, py, CELL_SIZE, CELL_SIZE, false);
if (flagged[y][x]) {
draw_string(px + 7, py + 6, "F", COLOR_RED);
}
}
}
}
// Draw new game button
int btn_y = grid_start_y + GRID_HEIGHT * CELL_SIZE + 10;
draw_button(grid_start_x, btn_y, 90, 24, "New Game", false);
}
static void minesweeper_click(Window *win, int x, int y) {
// x and y are relative to window content (0,0 is top-left of window)
int grid_start_x = 10;
int grid_start_y = 50;
int btn_y = grid_start_y + GRID_HEIGHT * CELL_SIZE + 10;
// Check "New Game" button
if (x >= grid_start_x && x < grid_start_x + 90 &&
y >= btn_y && y < btn_y + 24) {
init_game();
wm_paint();
return;
}
// Check grid cells
if (x >= grid_start_x && x < grid_start_x + GRID_WIDTH * CELL_SIZE &&
y >= grid_start_y && y < grid_start_y + GRID_HEIGHT * CELL_SIZE) {
if (game_over || game_won) return;
int cell_x = (x - grid_start_x) / CELL_SIZE;
int cell_y = (y - grid_start_y) / CELL_SIZE;
reveal_cell(cell_x, cell_y);
wm_paint();
}
}
void minesweeper_init(void) {
win_minesweeper.title = "Minesweeper";
win_minesweeper.x = 250;
win_minesweeper.y = 100;
win_minesweeper.w = 240;
win_minesweeper.h = 340;
win_minesweeper.visible = false;
win_minesweeper.focused = false;
win_minesweeper.z_index = 0;
win_minesweeper.paint = minesweeper_paint;
win_minesweeper.handle_click = minesweeper_click;
win_minesweeper.handle_right_click = minesweeper_right_click;
// Initialize game
init_game();
}

10
src/kernel/minesweeper.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef MINESWEEPER_H
#define MINESWEEPER_H
#include "wm.h"
extern Window win_minesweeper;
void minesweeper_init(void);
#endif

212
src/kernel/notepad.c Normal file
View File

@@ -0,0 +1,212 @@
#include "notepad.h"
#include "graphics.h"
#include <stddef.h>
Window win_notepad;
static int notepad_scroll_line = 0; // Track which line is at top of visible area
static void notepad_ensure_cursor_visible(Window *win) {
// Calculate how many lines fit in the notepad window (allow 3-4 extra lines)
int visible_lines = (win->h - 40) / 10 + 3; // Content area height / line height + buffer
if (visible_lines < 1) visible_lines = 1;
// Count which line the cursor is on
int cursor_line = 0;
for (int i = 0; i < win->cursor_pos && i < win->buf_len; i++) {
if (win->buffer[i] == '\n') cursor_line++;
}
// Scroll up if cursor is above visible area
if (cursor_line < notepad_scroll_line) {
notepad_scroll_line = cursor_line;
}
// Scroll down if cursor is below visible area
if (cursor_line >= notepad_scroll_line + visible_lines) {
notepad_scroll_line = cursor_line - visible_lines + 1;
}
}
static void notepad_paint(Window *win) {
// Draw text starting from scroll position
int display_line = 0;
int current_line = 0;
int current_x = win->x + 8;
int current_y = win->y + 30;
for (int i = 0; i < win->buf_len; i++) {
// Skip lines before scroll position
if (current_line < notepad_scroll_line) {
if (win->buffer[i] == '\n') {
current_line++;
}
continue;
}
// Stop if we've gone past visible area
if (display_line >= (win->h - 40) / 10) {
break;
}
if (win->buffer[i] == '\n') {
current_x = win->x + 8;
current_y += 10;
current_line++;
display_line++;
} else {
// Draw single character
char ch[2] = {win->buffer[i], 0};
draw_string(current_x, current_y, ch, COLOR_BLACK);
current_x += 8;
}
}
// Cursor
if (win->focused) {
int cx = win->x + 8;
int cy = win->y + 30;
int current_line = 0;
for (int i = 0; i < win->cursor_pos; i++) {
if (win->buffer[i] == '\n') {
cx = win->x + 8;
cy += 10;
current_line++;
} else {
cx += 8;
}
}
// Only draw cursor if it's in visible area
if (current_line >= notepad_scroll_line &&
current_line < notepad_scroll_line + (win->h - 40) / 10) {
draw_rect(cx, cy, 2, 8, COLOR_BLACK);
}
}
}
static void notepad_key(Window *target, char c) {
if (c == 17) { // UP
if (target->cursor_pos > 0) {
int curr = target->cursor_pos;
int line_start = curr;
while (line_start > 0 && target->buffer[line_start - 1] != '\n') {
line_start--;
}
int col = curr - line_start;
if (line_start > 0) {
int prev_line_end = line_start - 1;
int prev_line_start = prev_line_end;
while (prev_line_start > 0 && target->buffer[prev_line_start - 1] != '\n') {
prev_line_start--;
}
int prev_line_len = prev_line_end - prev_line_start;
if (col > prev_line_len) col = prev_line_len;
target->cursor_pos = prev_line_start + col;
}
}
notepad_ensure_cursor_visible(target);
} else if (c == 18) { // DOWN
int len = target->buf_len;
if (target->cursor_pos < len) {
int curr = target->cursor_pos;
int line_start = curr;
while (line_start > 0 && target->buffer[line_start - 1] != '\n') {
line_start--;
}
int col = curr - line_start;
int next_line_start = curr;
while (next_line_start < len && target->buffer[next_line_start] != '\n') {
next_line_start++;
}
if (next_line_start < len) {
next_line_start++; // Skip newline
int next_line_end = next_line_start;
while (next_line_end < len && target->buffer[next_line_end] != '\n') {
next_line_end++;
}
int next_line_len = next_line_end - next_line_start;
if (col > next_line_len) col = next_line_len;
target->cursor_pos = next_line_start + col;
} else {
target->cursor_pos = len;
}
}
notepad_ensure_cursor_visible(target);
} else if (c == 19) { // LEFT
if (target->cursor_pos > 0) target->cursor_pos--;
notepad_ensure_cursor_visible(target);
} else if (c == 20) { // RIGHT
if (target->cursor_pos < target->buf_len) target->cursor_pos++;
notepad_ensure_cursor_visible(target);
} else if (c == '\b') { // Backspace
if (target->cursor_pos > 0) {
// Shift left
for (int i = target->cursor_pos; i < target->buf_len; i++) {
target->buffer[i - 1] = target->buffer[i];
}
target->buf_len--;
target->cursor_pos--;
target->buffer[target->buf_len] = 0;
notepad_ensure_cursor_visible(target);
}
} else if (c == '\n') { // Enter
if (target->buf_len < 1023) {
// Shift right
for (int i = target->buf_len; i > target->cursor_pos; i--) {
target->buffer[i] = target->buffer[i - 1];
}
target->buffer[target->cursor_pos] = c;
target->buf_len++;
target->cursor_pos++;
target->buffer[target->buf_len] = 0;
notepad_ensure_cursor_visible(target);
}
} else {
// Printable char
if (target->buf_len < 1023) {
for (int i = target->buf_len; i > target->cursor_pos; i--) {
target->buffer[i] = target->buffer[i - 1];
}
target->buffer[target->cursor_pos] = c;
target->buf_len++;
target->cursor_pos++;
target->buffer[target->buf_len] = 0;
notepad_ensure_cursor_visible(target);
}
}
}
void notepad_init(void) {
win_notepad.title = "Notepad";
win_notepad.x = 100;
win_notepad.y = 100;
win_notepad.w = 400;
win_notepad.h = 300;
win_notepad.visible = false;
win_notepad.buf_len = 0;
win_notepad.cursor_pos = 0;
win_notepad.focused = false;
win_notepad.z_index = 0;
win_notepad.paint = notepad_paint;
win_notepad.handle_key = notepad_key;
win_notepad.handle_click = NULL;
win_notepad.handle_right_click = NULL;
notepad_scroll_line = 0;
for(int i=0; i<1024; i++) win_notepad.buffer[i] = 0;
}
void notepad_reset(void) {
// Clear notepad buffer and reset cursor on close/reopen
win_notepad.buf_len = 0;
win_notepad.cursor_pos = 0;
win_notepad.focused = false;
notepad_scroll_line = 0;
for(int i=0; i<1024; i++) win_notepad.buffer[i] = 0;
}

11
src/kernel/notepad.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef NOTEPAD_H
#define NOTEPAD_H
#include "wm.h"
extern Window win_notepad;
void notepad_init(void);
void notepad_reset(void);
#endif

167
src/kernel/ps2.c Normal file
View File

@@ -0,0 +1,167 @@
#include "ps2.h"
#include "io.h"
#include "wm.h"
#include <stdbool.h>
extern void serial_print(const char *s);
extern void serial_print_hex(uint64_t n);
// --- Timer Handler ---
void timer_handler(void) {
wm_timer_tick();
outb(0x20, 0x20); // EOI to Master PIC
}
// --- Keyboard ---
static bool shift_pressed = false;
static bool extended_scancode = false;
// Simple US QWERTY Scan Code Set 1 Map
static char scancode_map[128] = {
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',
'\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0,
'\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*',
0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
static char scancode_map_shift[128] = {
0, 27, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\b',
'\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
0, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', 0,
'|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 0, '*',
0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
void keyboard_handler(void) {
uint8_t scancode = inb(0x60);
if (scancode == 0xE0) {
extended_scancode = true;
outb(0x20, 0x20);
return;
}
if (scancode == 0x2A || scancode == 0x36) { // Shift Down
shift_pressed = true;
} else if (scancode == 0xAA || scancode == 0xB6) { // Shift Up
shift_pressed = false;
} else if (!(scancode & 0x80)) { // Key Press (not release)
if (extended_scancode) {
// Extended scancode - arrow keys and special keys
extended_scancode = false;
switch (scancode) {
case 0x48: wm_handle_key(17); break; // Up arrow
case 0x50: wm_handle_key(18); break; // Down arrow
case 0x4B: wm_handle_key(19); break; // Left arrow
case 0x4D: wm_handle_key(20); break; // Right arrow
}
} else {
// Regular scancode
char c = shift_pressed ? scancode_map_shift[scancode] : scancode_map[scancode];
if (c) {
wm_handle_key(c);
}
}
} else if (scancode & 0x80) {
// Key release
extended_scancode = false;
}
outb(0x20, 0x20); // EOI
}
// --- Mouse ---
static uint8_t mouse_cycle = 0;
static int8_t mouse_byte[3];
void mouse_wait(uint8_t type) {
uint32_t timeout = 100000;
if (type == 0) { // Write
while (timeout--) {
if ((inb(0x64) & 2) == 0) return;
}
} else { // Read
while (timeout--) {
if ((inb(0x64) & 1) == 1) return;
}
}
}
void mouse_write(uint8_t write) {
mouse_wait(0);
outb(0x64, 0xD4);
mouse_wait(0);
outb(0x60, write);
}
uint8_t mouse_read(void) {
mouse_wait(1);
return inb(0x60);
}
void mouse_init(void) {
uint8_t status;
// Enable Aux Device
mouse_wait(0);
outb(0x64, 0xA8);
// Enable Interrupts
mouse_wait(0);
outb(0x64, 0x20);
mouse_wait(1);
status = inb(0x60) | 2;
mouse_wait(0);
outb(0x64, 0x60);
mouse_wait(0);
outb(0x60, status);
// Set Defaults
mouse_write(0xF6);
mouse_read();
// Enable Streaming
mouse_write(0xF4);
mouse_read();
}
void mouse_handler(void) {
uint8_t status = inb(0x64);
if (!(status & 0x20)) {
outb(0x20, 0x20);
outb(0xA0, 0x20);
return;
}
uint8_t b = inb(0x60);
if (mouse_cycle == 0) {
if ((b & 0x08) == 0) { // Sync check
// Skip
} else {
mouse_byte[0] = b;
mouse_cycle++;
}
} else if (mouse_cycle == 1) {
mouse_byte[1] = b;
mouse_cycle++;
} else {
mouse_byte[2] = b;
mouse_cycle = 0;
// Packet Full
int8_t dx = mouse_byte[1];
int8_t dy = mouse_byte[2];
// Send to WM
wm_handle_mouse(dx, -dy, mouse_byte[0] & 0x07);
}
outb(0x20, 0x20);
outb(0xA0, 0x20); // Slave EOI
}
void ps2_init(void) {
mouse_init();
}

8
src/kernel/ps2.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef PS2_H
#define PS2_H
void ps2_init(void);
void keyboard_handler(void);
void mouse_handler(void);
#endif

76
src/kernel/rtc.c Normal file
View File

@@ -0,0 +1,76 @@
#include "rtc.h"
#include "io.h"
#define CMOS_ADDRESS 0x70
#define CMOS_DATA 0x71
static int updating_rtc() {
outb(CMOS_ADDRESS, 0x0A);
return (inb(CMOS_DATA) & 0x80);
}
static uint8_t get_rtc_register(int reg) {
outb(CMOS_ADDRESS, reg);
return inb(CMOS_DATA);
}
void rtc_get_datetime(int *year, int *month, int *day, int *hour, int *minute, int *second) {
uint8_t century;
uint8_t last_second;
uint8_t last_minute;
uint8_t last_hour;
uint8_t last_day;
uint8_t last_month;
uint8_t last_year;
uint8_t last_century;
uint8_t registerB;
while (updating_rtc());
*second = get_rtc_register(0x00);
*minute = get_rtc_register(0x02);
*hour = get_rtc_register(0x04);
*day = get_rtc_register(0x07);
*month = get_rtc_register(0x08);
*year = get_rtc_register(0x09);
// Note: Century register is not standard, but often 0x32 if ACPI table says so.
// For now, assume 20xx
do {
last_second = *second;
last_minute = *minute;
last_hour = *hour;
last_day = *day;
last_month = *month;
last_year = *year;
while (updating_rtc());
*second = get_rtc_register(0x00);
*minute = get_rtc_register(0x02);
*hour = get_rtc_register(0x04);
*day = get_rtc_register(0x07);
*month = get_rtc_register(0x08);
*year = get_rtc_register(0x09);
} while( (last_second != *second) || (last_minute != *minute) || (last_hour != *hour) ||
(last_day != *day) || (last_month != *month) || (last_year != *year) );
registerB = get_rtc_register(0x0B);
// Convert BCD to binary values if necessary
if (!(registerB & 0x04)) {
*second = (*second & 0x0F) + ((*second / 16) * 10);
*minute = (*minute & 0x0F) + ((*minute / 16) * 10);
*hour = ( (*hour & 0x0F) + (((*hour & 0x70) / 16) * 10) ) | (*hour & 0x80);
*day = (*day & 0x0F) + ((*day / 16) * 10);
*month = (*month & 0x0F) + ((*month / 16) * 10);
*year = (*year & 0x0F) + ((*year / 16) * 10);
}
// Convert 12 hour clock to 24 hour clock if necessary
if (!(registerB & 0x02) && (*hour & 0x80)) {
*hour = ((*hour & 0x7F) + 12) % 24;
}
// Calculate full year
*year += 2000;
}

8
src/kernel/rtc.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef RTC_H
#define RTC_H
#include <stdint.h>
void rtc_get_datetime(int *year, int *month, int *day, int *hour, int *minute, int *second);
#endif

746
src/kernel/wm.c Normal file
View File

@@ -0,0 +1,746 @@
#include "wm.h"
#include "graphics.h"
#include "io.h"
#include "cmd.h"
#include "calculator.h"
#include "cli_apps/cli_utils.h"
#include "explorer.h"
#include "editor.h"
#include <stdbool.h>
#include <stddef.h>
#include "notepad.h"
#include "control_panel.h"
#include "about.h"
#include "minesweeper.h"
// --- State ---
static int mx = 400, my = 300; // Mouse Pos
static int prev_mx = 400, prev_my = 300; // Previous mouse position
static bool start_menu_open = false;
// Dragging State
static bool is_dragging = false;
static Window *drag_window = NULL;
static int drag_offset_x = 0;
static int drag_offset_y = 0;
// Windows array for z-order management
static Window *all_windows[8];
static int window_count = 0;
// Redraw system
static bool force_redraw = true; // Force full redraw on next tick
static uint32_t timer_ticks = 0;
// Cursor state
static bool cursor_visible = true;
static int last_cursor_x = 400;
static int last_cursor_y = 300;
// --- Drawing Helpers ---
// Draw a bevelled box (Win 3.1 style)
void draw_bevel_rect(int x, int y, int w, int h, bool sunken) {
draw_rect(x, y, w, h, COLOR_GRAY);
uint32_t top_left = sunken ? COLOR_DKGRAY : COLOR_WHITE;
uint32_t bot_right = sunken ? COLOR_WHITE : COLOR_DKGRAY;
// Top
draw_rect(x, y, w, 1, top_left);
// Left
draw_rect(x, y, 1, h, top_left);
// Bottom
draw_rect(x, y + h - 1, w, 1, bot_right);
// Right
draw_rect(x + w - 1, y, 1, h, bot_right);
}
void draw_button(int x, int y, int w, int h, const char *text, bool pressed) {
draw_bevel_rect(x, y, w, h, pressed);
// Center Text
int len = 0; while(text[len]) len++;
int tx = x + (w - (len * 8)) / 2;
int ty = y + (h - 8) / 2;
if (pressed) { tx++; ty++; }
draw_string(tx, ty, text, COLOR_BLACK);
}
void draw_icon(int x, int y, const char *label) {
// Simple "File" Icon
draw_rect(x + 10, y, 20, 25, COLOR_WHITE);
draw_rect(x + 10, y, 20, 1, COLOR_BLACK);
draw_rect(x + 10, y, 1, 25, COLOR_BLACK);
draw_rect(x + 30, y, 1, 25, COLOR_BLACK);
draw_rect(x + 10, y + 25, 21, 1, COLOR_BLACK);
// Label
draw_string(x, y + 30, label, COLOR_WHITE);
}
void draw_folder_icon(int x, int y, const char *label) {
// Folder icon (yellow folder)
// Folder tab
draw_rect(x + 5, y, 15, 6, COLOR_LTGRAY);
draw_rect(x + 5, y, 15, 1, COLOR_BLACK);
draw_rect(x + 5, y, 1, 6, COLOR_BLACK);
draw_rect(x + 19, y, 1, 6, COLOR_BLACK);
// Folder body
draw_rect(x + 5, y + 6, 25, 15, COLOR_LTGRAY);
draw_rect(x + 5, y + 6, 25, 1, COLOR_BLACK);
draw_rect(x + 5, y + 6, 1, 15, COLOR_BLACK);
draw_rect(x + 29, y + 6, 1, 15, COLOR_BLACK);
draw_rect(x + 5, y + 20, 25, 1, COLOR_BLACK);
// Label
draw_string(x, y + 30, label, COLOR_WHITE);
}
void draw_document_icon(int x, int y, const char *label) {
// Document icon (white paper with lines)
draw_rect(x + 10, y, 20, 25, COLOR_WHITE);
draw_rect(x + 10, y, 20, 1, COLOR_BLACK);
draw_rect(x + 10, y, 1, 25, COLOR_BLACK);
draw_rect(x + 30, y, 1, 25, COLOR_BLACK);
draw_rect(x + 10, y + 25, 21, 1, COLOR_BLACK);
// Lines on document
draw_rect(x + 14, y + 8, 12, 1, COLOR_BLACK);
draw_rect(x + 14, y + 12, 12, 1, COLOR_BLACK);
draw_rect(x + 14, y + 16, 12, 1, COLOR_BLACK);
// Label
draw_string(x, y + 30, label, COLOR_WHITE);
}
void draw_window(Window *win) {
if (!win->visible) return;
// Main Body
draw_bevel_rect(win->x, win->y, win->w, win->h, false);
// Title Bar
uint32_t title_color = win->focused ? COLOR_BLUE : COLOR_DKGRAY;
draw_rect(win->x + 3, win->y + 3, win->w - 6, 18, title_color);
draw_string(win->x + 8, win->y + 8, win->title, COLOR_WHITE);
// Close Button (X)
draw_button(win->x + win->w - 20, win->y + 5, 14, 14, "X", false);
// Client Area
draw_rect(win->x + 4, win->y + 24, win->w - 8, win->h - 28, COLOR_WHITE);
if (win->paint) {
win->paint(win);
}
}
// Draw Mouse Cursor (Simple Arrow)
void draw_cursor(int x, int y) {
// 0 = Transparent (skip), 1 = Black, 2 = White
static const uint8_t cursor_bitmap[10][10] = {
{1,1,0,0,0,0,0,0,0,0},
{1,2,1,0,0,0,0,0,0,0},
{1,2,2,1,0,0,0,0,0,0},
{1,2,2,2,1,0,0,0,0,0},
{1,2,2,2,2,1,0,0,0,0},
{1,2,2,2,2,2,1,0,0,0},
{1,2,2,1,1,1,1,0,0,0},
{1,1,1,0,1,2,1,0,0,0},
{0,0,0,0,0,1,2,1,0,0},
{0,0,0,0,0,0,1,0,0,0}
};
for (int r = 0; r < 10; r++) {
for (int c = 0; c < 10; c++) {
uint8_t p = cursor_bitmap[r][c];
if (p == 1) put_pixel(x + c, y + r, COLOR_BLACK);
else if (p == 2) put_pixel(x + c, y + r, COLOR_WHITE);
}
}
}
// Erase cursor by redrawing the background in that area
static void erase_cursor(int x, int y) {
int sw = get_screen_width();
int sh = get_screen_height();
// Clamp to screen
int x1 = x < 0 ? 0 : x;
int y1 = y < 0 ? 0 : y;
int x2 = x + 10 > sw ? sw : x + 10;
int y2 = y + 10 > sh ? sh : y + 10;
int w = x2 - x1;
int h = y2 - y1;
// Check what's underneath the cursor and redraw it
if (y1 < sh - 28) {
// Desktop or window area - draw teal background
draw_rect(x1, y1, w, h, COLOR_TEAL);
} else {
// Taskbar area - draw gray background
draw_rect(x1, y1, w, h, COLOR_GRAY);
}
}
// --- Clock ---
static uint8_t rtc_read(uint8_t reg) {
outb(0x70, reg);
return inb(0x71);
}
static void draw_clock(int x, int y) {
// Wait for update in progress
while (rtc_read(0x0A) & 0x80);
uint8_t s = rtc_read(0x00);
uint8_t m = rtc_read(0x02);
uint8_t h = rtc_read(0x04);
uint8_t b = rtc_read(0x0B);
if (!(b & 0x04)) {
s = (s & 0x0F) + ((s >> 4) * 10);
m = (m & 0x0F) + ((m >> 4) * 10);
h = (h & 0x0F) + ((h >> 4) * 10);
}
char buf[9];
buf[0] = '0' + (h / 10);
buf[1] = '0' + (h % 10);
buf[2] = ':';
buf[3] = '0' + (m / 10);
buf[4] = '0' + (m % 10);
buf[5] = ':';
buf[6] = '0' + (s / 10);
buf[7] = '0' + (s % 10);
buf[8] = 0;
draw_string(x, y, buf, COLOR_BLACK);
}
// --- Main Paint Function ---
void wm_paint(void) {
int sw = get_screen_width();
int sh = get_screen_height();
// First, erase the old cursor (before redrawing anything)
if (cursor_visible) {
erase_cursor(last_cursor_x, last_cursor_y);
}
// 1. Desktop
draw_desktop_background();
draw_folder_icon(20, 20, "Explorer");
draw_document_icon(20, 80, "Notepad");
// 3. Windows - sort by z-index and draw
// Simple bubble sort by z-index (5 windows max)
Window *sorted_windows[6];
for (int i = 0; i < window_count; i++) {
sorted_windows[i] = all_windows[i];
}
for (int i = 0; i < window_count - 1; i++) {
for (int j = 0; j < window_count - i - 1; j++) {
if (sorted_windows[j]->z_index > sorted_windows[j + 1]->z_index) {
Window *temp = sorted_windows[j];
sorted_windows[j] = sorted_windows[j + 1];
sorted_windows[j + 1] = temp;
}
}
}
// Draw windows in z-order (lowest first)
for (int i = 0; i < window_count; i++) {
draw_window(sorted_windows[i]);
}
// 4. Taskbar
draw_rect(0, sh - 28, sw, 28, COLOR_GRAY);
draw_rect(0, sh - 28, sw, 2, COLOR_WHITE); // Top highlight
// 5. Start Button
draw_button(2, sh - 26, 60, 24, "Start", start_menu_open);
// Clock
draw_clock(sw - 80, sh - 20);
// 6. Start Menu (if open)
if (start_menu_open) {
int menu_h = 230;
int menu_y = sh - 28 - menu_h;
draw_bevel_rect(0, menu_y, 120, menu_h, false);
// Items
draw_string(8, menu_y + 8, "Explorer", COLOR_BLACK);
draw_string(8, menu_y + 28, "Notepad", COLOR_BLACK);
draw_string(8, menu_y + 48, "Editor", COLOR_BLACK);
draw_string(8, menu_y + 68, "CMD", COLOR_BLACK);
draw_string(8, menu_y + 88, "Calculator", COLOR_BLACK);
draw_string(8, menu_y + 108, "Minesweeper", COLOR_BLACK);
draw_string(8, menu_y + 128, "Control Panel", COLOR_BLACK);
draw_string(8, menu_y + 148, "About BrewOS", COLOR_BLACK);
// Separator line
draw_rect(5, menu_y + 165, 110, 1, COLOR_BLACK);
// Power options at bottom
draw_string(8, menu_y + 175, "Shutdown", COLOR_BLACK);
draw_string(8, menu_y + 195, "Restart", COLOR_BLACK);
}
// 7. Mouse cursor (draw last so it's on top)
draw_cursor(mx, my);
last_cursor_x = mx;
last_cursor_y = my;
// Flip the buffer - display the rendered frame atomically
graphics_flip_buffer();
}
// --- Input Handling ---
bool rect_contains(int x, int y, int w, int h, int px, int py) {
return px >= x && px < x + w && py >= y && py < y + h;
}
void wm_handle_click(int x, int y) {
int sh = get_screen_height();
// Check Start Button
if (rect_contains(2, sh - 26, 60, 24, x, y)) {
start_menu_open = !start_menu_open;
force_redraw = true;
return;
}
// Check Start Menu Items
if (start_menu_open) {
int menu_h = 230;
int menu_y = sh - 28 - menu_h;
if (rect_contains(0, menu_y, 120, menu_h, x, y)) {
// Clear focus from all windows first
for (int i = 0; i < window_count; i++) {
all_windows[i]->focused = false;
}
// Find which item was clicked
if (y < menu_y + 25) { // Explorer
win_explorer.visible = true;
win_explorer.focused = true;
// Bring to front
int max_z = 0;
for (int i = 0; i < window_count; i++) {
if (all_windows[i]->z_index > max_z) {
max_z = all_windows[i]->z_index;
}
}
win_explorer.z_index = max_z + 1;
start_menu_open = false;
} else if (y < menu_y + 45) { // Notepad
win_notepad.visible = true;
win_notepad.focused = true;
int max_z = 0;
for (int i = 0; i < window_count; i++) {
if (all_windows[i]->z_index > max_z) {
max_z = all_windows[i]->z_index;
}
}
win_notepad.z_index = max_z + 1;
start_menu_open = false;
} else if (y < menu_y + 65) { // Editor
win_editor.visible = true;
win_editor.focused = true;
int max_z = 0;
for (int i = 0; i < window_count; i++) {
if (all_windows[i]->z_index > max_z) {
max_z = all_windows[i]->z_index;
}
}
win_editor.z_index = max_z + 1;
start_menu_open = false;
} else if (y < menu_y + 85) { // CMD
win_cmd.visible = true;
win_cmd.focused = true;
int max_z = 0;
for (int i = 0; i < window_count; i++) {
if (all_windows[i]->z_index > max_z) {
max_z = all_windows[i]->z_index;
}
}
win_cmd.z_index = max_z + 1;
start_menu_open = false;
} else if (y < menu_y + 105) { // Calculator
win_calculator.visible = true;
win_calculator.focused = true;
int max_z = 0;
for (int i = 0; i < window_count; i++) {
if (all_windows[i]->z_index > max_z) {
max_z = all_windows[i]->z_index;
}
}
win_calculator.z_index = max_z + 1;
start_menu_open = false;
} else if (y < menu_y + 125) { // Minesweeper
win_minesweeper.visible = true;
win_minesweeper.focused = true;
int max_z = 0;
for (int i = 0; i < window_count; i++) {
if (all_windows[i]->z_index > max_z) {
max_z = all_windows[i]->z_index;
}
}
win_minesweeper.z_index = max_z + 1;
start_menu_open = false;
} else if (y < menu_y + 145) { // Control Panel
win_control_panel.visible = true;
win_control_panel.focused = true;
int max_z = 0;
for (int i = 0; i < window_count; i++) {
if (all_windows[i]->z_index > max_z) {
max_z = all_windows[i]->z_index;
}
}
win_control_panel.z_index = max_z + 1;
start_menu_open = false;
} else if (y < menu_y + 165) { // About BrewOS
win_about.visible = true;
win_about.focused = true;
int max_z = 0;
for (int i = 0; i < window_count; i++) {
if (all_windows[i]->z_index > max_z) {
max_z = all_windows[i]->z_index;
}
}
win_about.z_index = max_z + 1;
start_menu_open = false;
} else if (y < menu_y + 185) { // Shutdown
cli_cmd_shutdown(NULL);
start_menu_open = false;
} else { // Restart
cli_cmd_reboot(NULL);
start_menu_open = false;
}
force_redraw = true;
return;
}
}
// Find topmost window at click location
Window *topmost = NULL;
int topmost_z = -1;
for (int i = 0; i < window_count; i++) {
Window *win = all_windows[i];
if (win->visible && rect_contains(win->x, win->y, win->w, win->h, x, y)) {
if (win->z_index > topmost_z) {
topmost = win;
topmost_z = win->z_index;
}
}
}
// If a window was clicked
if (topmost != NULL) {
// Clear focus from all windows
for (int i = 0; i < window_count; i++) {
all_windows[i]->focused = false;
}
// Bring it to front
int max_z = 0;
for (int i = 0; i < window_count; i++) {
if (all_windows[i]->z_index > max_z) {
max_z = all_windows[i]->z_index;
}
}
topmost->z_index = max_z + 1;
topmost->focused = true;
// Check close button
if (rect_contains(topmost->x + topmost->w - 20, topmost->y + 5, 14, 14, x, y)) {
topmost->visible = false;
// Reset window state on close
if (topmost == &win_explorer) {
explorer_reset();
} else if (topmost == &win_notepad) {
notepad_reset();
} else if (topmost == &win_control_panel) {
control_panel_reset();
}
} else if (y < topmost->y + 24) {
// Dragging the title bar
is_dragging = true;
drag_window = topmost;
drag_offset_x = x - topmost->x;
drag_offset_y = y - topmost->y;
} else {
// Content click
if (topmost->handle_click) {
topmost->handle_click(topmost, x - topmost->x, y - topmost->y);
}
}
} else {
// No window clicked - check desktop icons
// Clear focus from all windows first
for (int i = 0; i < window_count; i++) {
all_windows[i]->focused = false;
}
if (rect_contains(20, 20, 40, 40, x, y)) {
// Explorer icon
win_explorer.visible = true;
win_explorer.focused = true;
int max_z = 0;
for (int i = 0; i < window_count; i++) {
if (all_windows[i]->z_index > max_z) {
max_z = all_windows[i]->z_index;
}
}
win_explorer.z_index = max_z + 1;
} else if (rect_contains(20, 80, 40, 40, x, y)) {
// Notepad icon
win_notepad.visible = true;
win_notepad.focused = true;
int max_z = 0;
for (int i = 0; i < window_count; i++) {
if (all_windows[i]->z_index > max_z) {
max_z = all_windows[i]->z_index;
}
}
win_notepad.z_index = max_z + 1;
} else if (rect_contains(20, 140, 40, 40, x, y)) {
win_calculator.visible = true;
win_calculator.focused = true;
int max_z = 0;
for (int i = 0; i < window_count; i++) {
if (all_windows[i]->z_index > max_z) {
max_z = all_windows[i]->z_index;
}
}
win_calculator.z_index = max_z + 1;
} else if (rect_contains(20, 200, 40, 40, x, y)) {
win_explorer.visible = true;
win_explorer.focused = true;
int max_z = 0;
for (int i = 0; i < window_count; i++) {
if (all_windows[i]->z_index > max_z) {
max_z = all_windows[i]->z_index;
}
}
win_explorer.z_index = max_z + 1;
} else if (rect_contains(20, 260, 40, 40, x, y)) {
win_editor.visible = true;
win_editor.focused = true;
int max_z = 0;
for (int i = 0; i < window_count; i++) {
if (all_windows[i]->z_index > max_z) {
max_z = all_windows[i]->z_index;
}
}
win_editor.z_index = max_z + 1;
}
}
// Close start menu if clicked elsewhere
if (start_menu_open) {
start_menu_open = false;
}
force_redraw = true;
}
// Handle right click (context menu or special actions)
void wm_handle_right_click(int x, int y) {
int sh = get_screen_height();
// Find topmost window at click location
Window *topmost = NULL;
int topmost_z = -1;
for (int i = 0; i < window_count; i++) {
Window *win = all_windows[i];
if (win->visible && rect_contains(win->x, win->y, win->w, win->h, x, y)) {
if (win->z_index > topmost_z) {
topmost = win;
topmost_z = win->z_index;
}
}
}
// If a window was clicked
if (topmost != NULL) {
// Don't process close button or title bar for right click
if (y >= topmost->y + 24) {
// Content right click
if (topmost->handle_right_click) {
topmost->handle_right_click(topmost, x - topmost->x, y - topmost->y);
}
}
}
force_redraw = true;
}void wm_handle_mouse(int dx, int dy, uint8_t buttons) {
int sw = get_screen_width();
int sh = get_screen_height();
prev_mx = mx;
prev_my = my;
mx += dx;
my += dy;
if (mx < 0) mx = 0;
if (my < 0) my = 0;
if (mx >= sw) mx = sw - 1;
if (my >= sh) my = sh - 1;
static bool prev_left = false;
static bool prev_right = false;
bool left = buttons & 0x01;
bool right = buttons & 0x02;
if (left && !prev_left) {
wm_handle_click(mx, my);
} else if (right && !prev_right) {
wm_handle_right_click(mx, my);
} else if (left && is_dragging && drag_window) {
drag_window->x = mx - drag_offset_x;
drag_window->y = my - drag_offset_y;
// Mark for full redraw since window moved
force_redraw = true;
} else if (left && !is_dragging && (dx != 0 || dy != 0)) {
// Mouse is moving while left button is held, but window isn't being dragged
force_redraw = true;
} else if (!left && is_dragging) {
is_dragging = false;
drag_window = NULL;
// Mark for full redraw since dragging ended
force_redraw = true;
}
prev_left = left;
prev_right = right;
if (prev_mx != mx || prev_my != my) {
// Cursor moved - just mark dirty cursor areas
wm_mark_dirty(prev_mx, prev_my, 10, 10);
wm_mark_dirty(mx, my, 10, 10);
}
prev_left = left;
}
void wm_handle_key(char c) {
Window *target = NULL;
if (win_notepad.focused && win_notepad.visible) target = &win_notepad;
else if (win_cmd.focused && win_cmd.visible) target = &win_cmd;
else if (win_calculator.focused && win_calculator.visible) target = &win_calculator;
else if (win_explorer.focused && win_explorer.visible) target = &win_explorer;
else if (win_editor.focused && win_editor.visible) target = &win_editor;
else if (win_control_panel.focused && win_control_panel.visible) target = &win_control_panel;
if (!target) return;
if (target->handle_key) {
target->handle_key(target, c);
}
// Mark window as needing redraw on next timer tick
wm_mark_dirty(target->x, target->y, target->w, target->h);
}
void wm_mark_dirty(int x, int y, int w, int h) {
graphics_mark_dirty(x, y, w, h);
}
void wm_refresh(void) {
force_redraw = true;
}
void wm_init(void) {
notepad_init();
cmd_init();
calculator_init();
explorer_init();
editor_init();
control_panel_init();
about_init();
minesweeper_init();
// Initialize z-indices
win_notepad.z_index = 0;
win_cmd.z_index = 1;
win_calculator.z_index = 2;
win_explorer.z_index = 3;
win_editor.z_index = 4;
win_control_panel.z_index = 5;
win_about.z_index = 6;
win_minesweeper.z_index = 7;
// Register windows in array
all_windows[0] = &win_notepad;
all_windows[1] = &win_cmd;
all_windows[2] = &win_calculator;
all_windows[3] = &win_explorer;
all_windows[4] = &win_editor;
all_windows[5] = &win_control_panel;
all_windows[6] = &win_about;
all_windows[7] = &win_minesweeper;
window_count = 8;
// Only show Explorer and Notepad on desktop (Explorer on top)
win_explorer.visible = false;
win_explorer.focused = false;
win_explorer.z_index = 10;
win_notepad.visible = false;
win_notepad.focused = false;
win_notepad.z_index = 9;
// Rest are hidden initially
win_cmd.visible = false;
win_calculator.visible = false;
win_editor.visible = false;
win_control_panel.visible = false;
win_about.visible = false;
win_minesweeper.visible = false;
force_redraw = true;
}
// Called by timer interrupt ~60Hz
void wm_timer_tick(void) {
timer_ticks++;
// Only redraw if there are dirty areas (clock updates at most every second, cursor rarely moves in timer only)
// Most of the time, nothing changes between ticks
static uint8_t last_second = 0xFF;
outb(0x70, 0x00);
uint8_t current_sec = inb(0x71);
if (current_sec != last_second) {
last_second = current_sec;
int sw = get_screen_width();
int sh = get_screen_height();
// Mark clock area + a bit of buffer
wm_mark_dirty(sw - 90, sh - 30, 90, 20);
}
// If force_redraw is set, do a full redraw
if (force_redraw) {
graphics_mark_screen_dirty();
force_redraw = false;
}
// Perform redraw if there are dirty areas
DirtyRect dirty = graphics_get_dirty_rect();
if (dirty.active) {
wm_paint();
graphics_clear_dirty();
}
}

52
src/kernel/wm.h Normal file
View File

@@ -0,0 +1,52 @@
#ifndef WM_H
#define WM_H
#include <stdint.h>
#include <stdbool.h>
// --- Constants ---
#define COLOR_TEAL 0xFF008080
#define COLOR_GRAY 0xFFC0C0C0
#define COLOR_WHITE 0xFFFFFFFF
#define COLOR_BLACK 0xFF000000
#define COLOR_BLUE 0xFF000080
#define COLOR_LTGRAY 0xFFDFDFDF
#define COLOR_DKGRAY 0xFF808080
#define COLOR_RED 0xFFFF0000
#define COLOR_COFFEE 0xFF6B4423
typedef struct Window Window;
struct Window {
char *title;
int x, y, w, h;
bool visible;
char buffer[1024];
int buf_len;
int cursor_pos;
bool focused;
int z_index; // Layering depth (higher = on top)
// Callbacks
void (*paint)(Window *win);
void (*handle_key)(Window *win, char c);
void (*handle_click)(Window *win, int x, int y);
void (*handle_right_click)(Window *win, int x, int y);
};
void wm_init(void);
void wm_handle_mouse(int dx, int dy, uint8_t buttons);
void wm_handle_key(char c);
void wm_handle_click(int x, int y);
void wm_handle_right_click(int x, int y);
// Redraw system
void wm_mark_dirty(int x, int y, int w, int h);
void wm_refresh(void);
void wm_paint(void);
void wm_timer_tick(void);
// Drawing helpers
void draw_bevel_rect(int x, int y, int w, int h, bool sunken);
void draw_button(int x, int y, int w, int h, const char *text, bool pressed);
#endif