Porting of apps to userspace

This commit is contained in:
boreddevnl
2026-02-26 20:07:21 +01:00
parent c2ead0d6a7
commit 786eac0345
84 changed files with 2887 additions and 2392 deletions

View File

@@ -8,7 +8,7 @@ LDFLAGS = -m elf_x86_64 -nostdlib -static -no-pie -Ttext=0x40000000 --no-dynamic
LIBC_SOURCES = $(wildcard libc/*.c)
LIBC_OBJS = $(LIBC_SOURCES:.c=.o) crt0.o
APP_SOURCES = $(wildcard *.c)
APP_SOURCES = $(filter-out nanojpeg.c, $(wildcard *.c))
APP_OBJS = $(APP_SOURCES:.c=.o)
APP_ELFS = $(APP_SOURCES:.c=.elf)
@@ -23,6 +23,9 @@ libc/%.o: libc/%.c
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
viewer.elf: $(LIBC_OBJS) viewer.o nanojpeg.o
$(LD) $(LDFLAGS) $^ -o $@
%.elf: $(LIBC_OBJS) %.o
$(LD) $(LDFLAGS) $^ -o $@

View File

@@ -4,7 +4,6 @@
#define SCALE 1000000LL
// Dark Mode Colors mapping to libui
#define COLOR_DARK_BG 0xFF1E1E1E
#define COLOR_DARK_PANEL 0xFF2D2D2D
#define COLOR_DARK_TEXT 0xFFF0F0F0

Binary file not shown.

View File

@@ -4,7 +4,7 @@ int main() {
const char* msg = "Attempting to crash via null dereference...\n";
sys_write(1, msg, 45);
// Null pointer dereference
// Null pointer dereference (should not crash the system and instead get this process killed)
volatile int* p = (int*)0;
*p = 123;

Binary file not shown.

Binary file not shown.

View File

@@ -20,10 +20,15 @@
#define GUI_EVENT_CLOSE 4
#define GUI_EVENT_KEY 5
#define GUI_EVENT_MOUSE_DOWN 6
#define GUI_EVENT_MOUSE_UP 7
#define GUI_EVENT_MOUSE_MOVE 8
typedef struct {
int type;
int arg1; // For click: x
int arg2; // For click: y
int arg3; // For click: button state
} gui_event_t;
// Window Handle

Binary file not shown.

View File

@@ -0,0 +1,140 @@
#include "stdlib.h"
#include "syscall.h"
// Simple block allocator over sys_sbrk
typedef struct BlockMeta {
size_t size;
int free;
struct BlockMeta *next;
} BlockMeta;
#define META_SIZE sizeof(BlockMeta)
static void *global_base = NULL;
static BlockMeta *find_free_block(BlockMeta **last, size_t size) {
BlockMeta *current = global_base;
while (current && !(current->free && current->size >= size)) {
*last = current;
current = current->next;
}
return current;
}
static BlockMeta *request_space(BlockMeta* last, size_t size) {
BlockMeta *block;
block = (BlockMeta *)sys_sbrk(0);
// Ask for space, ensuring everything stays 8-byte aligned if sizes are odd.
// For simplicity, we just request exactly what is needed,
// but typically `size` should be aligned.
size_t align = 8;
if (size % align != 0) {
size += align - (size % align);
}
void *request = sys_sbrk(size + META_SIZE);
if (request == (void*)-1) {
return NULL; // sbrk failed
}
if (last) { // NULL on first request
last->next = block;
}
block->size = size;
block->next = NULL;
block->free = 0;
return block;
}
void *malloc(size_t size) {
BlockMeta *block;
if (size <= 0) {
return NULL;
}
// Align size to 8 bytes for safety
size_t align = 8;
if (size % align != 0) {
size += align - (size % align);
}
if (!global_base) { // First call
block = request_space(NULL, size);
if (!block) return NULL;
global_base = block;
} else {
BlockMeta *last = global_base;
block = find_free_block(&last, size);
if (!block) { // Failed to find free block
block = request_space(last, size);
if (!block) return NULL;
} else { // Found free block
block->free = 0;
// We could split the block here if block->size is much larger than size...
}
}
return (block + 1);
}
void free(void *ptr) {
if (!ptr) {
return;
}
BlockMeta *block = (BlockMeta*)ptr - 1;
block->free = 1;
}
void *calloc(size_t nelem, size_t elsize) {
size_t size = nelem * elsize;
void *ptr = malloc(size);
if (ptr) {
char *p = ptr;
for (size_t i = 0; i < size; i++) {
p[i] = 0;
}
}
return ptr;
}
void *realloc(void *ptr, size_t size) {
if (!ptr) {
return malloc(size);
}
if (size == 0) {
free(ptr);
return NULL;
}
BlockMeta *block = (BlockMeta*)ptr - 1;
if (block->size >= size) {
return ptr;
}
void *new_ptr = malloc(size);
if (!new_ptr) {
return NULL;
}
char *src = ptr;
char *dst = new_ptr;
for (size_t i = 0; i < block->size; i++) {
dst[i] = src[i];
}
free(ptr);
return new_ptr;
}
void *memset(void *s, int c, size_t n) {
unsigned char *p = s;
while (n--) *p++ = (unsigned char)c;
return s;
}
void *memcpy(void *dest, const void *src, size_t n) {
unsigned char *d = dest;
const unsigned char *s = src;
while (n--) *d++ = *s++;
return dest;
}

View File

@@ -0,0 +1,14 @@
#ifndef STDLIB_H
#define STDLIB_H
#include <stddef.h>
#include <stdint.h>
void* malloc(size_t size);
void free(void* ptr);
void* calloc(size_t nmemb, size_t size);
void* realloc(void* ptr, size_t size);
void *memset(void *s, int c, size_t n);
void *memcpy(void *dest, const void *src, size_t n);
#endif

Binary file not shown.

View File

@@ -70,6 +70,14 @@ int sys_write(int fd, const char *buf, int len) {
return (int)syscall3(SYS_WRITE, (uint64_t)fd, (uint64_t)buf, (uint64_t)len);
}
void *sys_sbrk(int incr) {
return (void *)syscall1(SYS_SBRK, (uint64_t)incr);
}
int sys_system(int cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4) {
return (int)syscall5(SYS_SYSTEM, (uint64_t)cmd, arg1, arg2, arg3, arg4);
}
int sys_open(const char *path, const char *mode) {
return (int)syscall3(SYS_FS, FS_CMD_OPEN, (uint64_t)path, (uint64_t)mode);
}

View File

@@ -8,6 +8,8 @@
#define SYS_WRITE 1
#define SYS_GUI 3
#define SYS_FS 4
#define SYS_SYSTEM 5
#define SYS_SBRK 9
// FS Commands
#define FS_CMD_OPEN 1
@@ -33,6 +35,8 @@ extern uint64_t syscall5(uint64_t sys_num, uint64_t arg1, uint64_t arg2, uint64_
// Public API
void sys_exit(int status);
int sys_write(int fd, const char *buf, int len);
void *sys_sbrk(int incr);
int sys_system(int cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4);
// FS API
int sys_open(const char *path, const char *mode);

Binary file not shown.

View File

@@ -0,0 +1,441 @@
#include "libc/syscall.h"
#include "libc/libui.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define MD_MAX_CONTENT 16384
#define MD_MAX_LINES 256
#define MD_CHAR_WIDTH 8
#define MD_LINE_HEIGHT 16
#define COLOR_DARK_PANEL 0xFF202020
#define COLOR_DARK_BG 0xFF121212
#define COLOR_DARK_TEXT 0xFFE0E0E0
#define COLOR_DARK_TITLEBAR 0xFF303030
#define COLOR_BLACK 0xFF000000
typedef enum {
MD_LINE_NORMAL,
MD_LINE_HEADING1,
MD_LINE_HEADING2,
MD_LINE_HEADING3,
MD_LINE_BOLD,
MD_LINE_ITALIC,
MD_LINE_LIST,
MD_LINE_BLOCKQUOTE,
MD_LINE_CODE
} MDLineType;
typedef struct {
char content[256];
int length;
MDLineType type;
int indent_level;
} MDLine;
static MDLine lines[MD_MAX_LINES];
static int line_count = 0;
static int scroll_top = 0;
static char open_filename[256] = "";
static int win_w = 600;
static int win_h = 400;
static size_t md_strlen(const char *str) {
size_t len = 0;
while (str[len]) len++;
return len;
}
static void md_strcpy(char *dest, const char *src) {
while (*src) *dest++ = *src++;
*dest = 0;
}
static int md_strncpy(char *dest, const char *src, int n) {
int i = 0;
while (i < n && src[i]) {
dest[i] = src[i];
i++;
}
dest[i] = 0;
return i;
}
static void md_parse_line(const char *raw_line, char *output, MDLineType *type, int *indent) {
int i = 0;
int out_idx = 0;
*indent = 0;
*type = MD_LINE_NORMAL;
while (raw_line[i] == ' ' || raw_line[i] == '\t') {
if (raw_line[i] == '\t') *indent += 2;
else *indent += 1;
i++;
}
if (raw_line[i] == '#') {
int hash_count = 0;
while (raw_line[i] == '#') {
hash_count++;
i++;
}
if (raw_line[i] == ' ') i++;
if (hash_count == 1) *type = MD_LINE_HEADING1;
else if (hash_count == 2) *type = MD_LINE_HEADING2;
else if (hash_count <= 6) *type = MD_LINE_HEADING3;
} else if (raw_line[i] == '-' || raw_line[i] == '*') {
if ((raw_line[i] == '-' || raw_line[i] == '*') && (raw_line[i+1] == ' ' || raw_line[i+1] == '\t')) {
*type = MD_LINE_LIST;
i += 2;
while (raw_line[i] == ' ' || raw_line[i] == '\t') i++;
}
} else if (raw_line[i] == '>') {
*type = MD_LINE_BLOCKQUOTE;
i++;
if (raw_line[i] == ' ') i++;
}
while (raw_line[i] && out_idx < 255) {
if (raw_line[i] == '*' && raw_line[i+1] == '*') {
i += 2;
while (raw_line[i] && !(raw_line[i] == '*' && raw_line[i+1] == '*') && out_idx < 255) {
output[out_idx++] = raw_line[i++];
}
if (raw_line[i] == '*' && raw_line[i+1] == '*') i += 2;
continue;
}
if ((raw_line[i] == '*' || raw_line[i] == '_') && out_idx > 0 && raw_line[i-1] != '\\') {
char delim = raw_line[i];
i++;
while (raw_line[i] && raw_line[i] != delim && out_idx < 255) {
output[out_idx++] = raw_line[i++];
}
if (raw_line[i] == delim) i++;
continue;
}
if (raw_line[i] == '`') {
i++;
while (raw_line[i] && raw_line[i] != '`' && out_idx < 255) {
output[out_idx++] = raw_line[i++];
}
if (raw_line[i] == '`') i++;
continue;
}
if (raw_line[i] == '[') {
i++;
while (raw_line[i] && raw_line[i] != ']' && out_idx < 255) {
output[out_idx++] = raw_line[i++];
}
if (raw_line[i] == ']') i++;
if (raw_line[i] == '(') {
while (raw_line[i] && raw_line[i] != ')') i++;
if (raw_line[i] == ')') i++;
}
continue;
}
output[out_idx++] = raw_line[i++];
}
output[out_idx] = 0;
}
static void md_clear_all(void) {
for (int i = 0; i < MD_MAX_LINES; i++) {
lines[i].content[0] = 0;
lines[i].length = 0;
lines[i].type = MD_LINE_NORMAL;
lines[i].indent_level = 0;
}
line_count = 0;
scroll_top = 0;
open_filename[0] = 0;
}
void markdown_open_file(const char *filename) {
md_clear_all();
md_strcpy(open_filename, filename);
int fd = sys_open(filename, "r");
if (fd < 0) return;
static char buffer[MD_MAX_CONTENT];
int bytes_read = sys_read(fd, buffer, sizeof(buffer) - 1);
sys_close(fd);
if (bytes_read <= 0) return;
buffer[bytes_read] = 0;
int line = 0;
int col = 0;
char raw_line[256] = "";
bool in_code_block = false;
for (int i = 0; i < bytes_read && line < MD_MAX_LINES; i++) {
char ch = buffer[i];
if (ch == '\n') {
raw_line[col] = 0;
if (raw_line[0] == '`' && raw_line[1] == '`' && raw_line[2] == '`') {
in_code_block = !in_code_block;
} else {
if (in_code_block) {
md_strcpy(lines[line].content, raw_line);
lines[line].length = md_strlen(raw_line);
lines[line].type = MD_LINE_CODE;
lines[line].indent_level = 0;
line++;
} else {
char parsed_content[256];
MDLineType type;
int indent;
md_parse_line(raw_line, parsed_content, &type, &indent);
md_strcpy(lines[line].content, parsed_content);
lines[line].length = md_strlen(parsed_content);
lines[line].type = type;
lines[line].indent_level = indent;
line++;
}
}
col = 0;
raw_line[0] = 0;
} else if (col < 255) {
raw_line[col++] = ch;
}
}
if (col > 0 && line < MD_MAX_LINES) {
raw_line[col] = 0;
if (raw_line[0] == '`' && raw_line[1] == '`' && raw_line[2] == '`') {
} else if (in_code_block) {
md_strcpy(lines[line].content, raw_line);
lines[line].length = md_strlen(raw_line);
lines[line].type = MD_LINE_CODE;
lines[line].indent_level = 0;
line++;
} else {
char parsed_content[256];
MDLineType type;
int indent;
md_parse_line(raw_line, parsed_content, &type, &indent);
md_strcpy(lines[line].content, parsed_content);
lines[line].length = md_strlen(parsed_content);
lines[line].type = type;
lines[line].indent_level = indent;
line++;
}
}
line_count = line;
}
static void md_draw_text_bold(ui_window_t win, int x, int y, const char *text, uint32_t color) {
ui_draw_string(win, x, y, text, color);
ui_draw_string(win, x + 1, y, text, color);
}
static void md_paint(ui_window_t win) {
int offset_x = 4;
int offset_y = 24;
int content_width = win_w - 8;
int content_height = win_h - 28;
ui_draw_rounded_rect_filled(win, offset_x, offset_y, content_width, 20, 6, COLOR_DARK_PANEL);
ui_draw_string(win, offset_x + 4, offset_y + 4, "File", COLOR_DARK_TEXT);
ui_draw_string(win, offset_x + 50, offset_y + 4, open_filename, COLOR_DARK_TEXT);
int btn_x_up = offset_x + content_width - 50;
int btn_y = offset_y + 2;
ui_draw_rounded_rect_filled(win, btn_x_up, btn_y, 20, 16, 4, COLOR_DARK_TITLEBAR);
ui_draw_string(win, btn_x_up + 6, btn_y, "^", COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, btn_x_up + 24, btn_y, 20, 16, 4, COLOR_DARK_TITLEBAR);
ui_draw_string(win, btn_x_up + 30, btn_y, "v", COLOR_DARK_TEXT);
int content_start_y = offset_y + 24;
int content_start_x = offset_x + 4;
int usable_content_width = content_width - 8 - 20;
int usable_content_height = content_height - 28;
int max_display_lines = usable_content_height / MD_LINE_HEIGHT;
ui_draw_rounded_rect_filled(win, 4, content_start_y, win_w - 24, usable_content_height, 6, COLOR_DARK_BG);
int display_line = 0;
int i = scroll_top;
while (i < line_count && display_line < max_display_lines) {
MDLine *line = &lines[i];
int line_height = MD_LINE_HEIGHT;
int extra_spacing = 0;
uint32_t text_color = COLOR_DARK_TEXT;
bool use_bold = false;
switch (line->type) {
case MD_LINE_HEADING1:
line_height = MD_LINE_HEIGHT * 2;
text_color = 0xFF87CEEB;
use_bold = true;
extra_spacing = 4;
break;
case MD_LINE_HEADING2:
line_height = MD_LINE_HEIGHT + 6;
text_color = 0xFF4A90E2;
use_bold = true;
extra_spacing = 2;
break;
case MD_LINE_HEADING3:
line_height = MD_LINE_HEIGHT + 2;
text_color = 0xFF87CEEB;
use_bold = false;
break;
case MD_LINE_BLOCKQUOTE:
text_color = 0xFFA0A0A0;
break;
case MD_LINE_CODE:
text_color = 0xFF90EE90;
break;
default:
text_color = COLOR_DARK_TEXT;
break;
}
if (display_line + (line_height / MD_LINE_HEIGHT) > max_display_lines) break;
int x_offset = content_start_x + (line->indent_level * 4);
int available_width = usable_content_width - (line->indent_level * 4);
int max_chars_per_line = available_width / MD_CHAR_WIDTH;
if (max_chars_per_line < 1) max_chars_per_line = 1;
const char *text = line->content;
int text_len = line->length;
int char_idx = 0;
int local_display_line = 0;
int wrapped_line_count = 0;
while (char_idx < text_len || (text_len == 0 && local_display_line == 0)) {
int line_y = content_start_y + display_line * MD_LINE_HEIGHT + (local_display_line * MD_LINE_HEIGHT);
char line_segment[256];
int segment_len = 0;
int segment_start = char_idx;
while (char_idx < text_len && segment_len < max_chars_per_line) {
line_segment[segment_len++] = text[char_idx++];
}
line_segment[segment_len] = 0;
if (char_idx < text_len && segment_len > 0) {
int last_space = -1;
for (int j = segment_len - 1; j >= 0; j--) {
if (line_segment[j] == ' ') {
last_space = j; break;
}
}
if (last_space > 0) {
segment_len = last_space;
line_segment[segment_len] = 0;
char_idx = segment_start + last_space + 1;
while (char_idx < text_len && text[char_idx] == ' ') char_idx++;
}
}
if (line->type == MD_LINE_CODE && segment_len > 0) {
ui_draw_rect(win, x_offset - 2, line_y - 2, (segment_len * MD_CHAR_WIDTH) + 4, 12, COLOR_BLACK);
}
if (local_display_line == 0) {
switch (line->type) {
case MD_LINE_LIST:
ui_draw_rect(win, x_offset, line_y + MD_LINE_HEIGHT/2 - 1, 2, 2, COLOR_BLACK);
x_offset += 12;
if (segment_len > 0 && line_segment[0] == ' ') {
for (int j = 0; j < segment_len - 1; j++) line_segment[j] = line_segment[j + 1];
segment_len--;
}
break;
case MD_LINE_BLOCKQUOTE:
ui_draw_rect(win, x_offset - 4, line_y, 2, line_height, 0xFF404080);
break;
default: break;
}
}
if (segment_len > 0) {
if (use_bold) {
md_draw_text_bold(win, x_offset, line_y + extra_spacing, line_segment, text_color);
} else {
ui_draw_string(win, x_offset, line_y, line_segment, text_color);
}
}
local_display_line++;
wrapped_line_count++;
if (char_idx >= text_len) break;
}
display_line += (wrapped_line_count > 0 ? wrapped_line_count : 1);
i++;
}
}
static void md_handle_key(char c) {
if (c == 'w' || c == 'W' || c == 17) {
scroll_top -= 3;
if (scroll_top < 0) scroll_top = 0;
} else if (c == 's' || c == 'S' || c == 18) {
scroll_top += 3;
int max_scroll = line_count - 10;
if (scroll_top > max_scroll) scroll_top = max_scroll;
if (scroll_top < 0) scroll_top = 0;
}
}
static void md_handle_click(int x, int y) {
int content_width = win_w - 8;
int btn_x_up = 4 + content_width - 50;
int btn_y = 24 + 2;
if (x >= btn_x_up && x < btn_x_up + 20 && y >= btn_y && y < btn_y + 16) {
scroll_top -= 3;
if (scroll_top < 0) scroll_top = 0;
return;
}
int btn_x_down_top = 4 + content_width - 50 + 24;
if (x >= btn_x_down_top && x < btn_x_down_top + 20 && y >= btn_y && y < btn_y + 16) {
scroll_top += 3;
int max_scroll = line_count - 10;
if (scroll_top > max_scroll) scroll_top = max_scroll;
if (scroll_top < 0) scroll_top = 0;
return;
}
}
int main(int argc, char **argv) {
ui_window_t win = ui_window_create("Markdown Viewer", 150, 180, win_w, win_h);
if (!win) return 1;
md_clear_all();
if (argc > 1) {
markdown_open_file(argv[1]);
}
gui_event_t ev;
while (1) {
if (ui_get_event(win, &ev)) {
if (ev.type == GUI_EVENT_PAINT) {
md_paint(win);
ui_mark_dirty(win, 0, 0, win_w, win_h);
} else if (ev.type == GUI_EVENT_CLICK) {
md_handle_click(ev.arg1, ev.arg2);
md_paint(win);
ui_mark_dirty(win, 0, 0, win_w, win_h);
} else if (ev.type == GUI_EVENT_KEY) {
md_handle_key((char)ev.arg1);
md_paint(win);
ui_mark_dirty(win, 0, 0, win_w, win_h);
} else if (ev.type == GUI_EVENT_CLOSE) {
sys_exit(0);
}
}
}
return 0;
}

BIN
src/kernel/userland/markdown.elf Executable file

Binary file not shown.

View File

@@ -0,0 +1,260 @@
#include "libc/syscall.h"
#include "libc/libui.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define COLOR_DARK_BG 0xFF121212
#define COLOR_DARK_PANEL 0xFF202020
#define COLOR_DARK_BORDER 0xFF404040
#define COLOR_DARK_TEXT 0xFFE0E0E0
#define COLOR_TRAFFIC_RED 0xFFFF6B6B
// Debugging helper
static void debug_print(const char *msg) {
sys_write(1, msg, 0);
int i = 0;
while (msg[i]) i++;
sys_write(1, msg, i);
sys_write(1, "\n", 1);
}
// 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 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_paint(ui_window_t win) {
int win_w = 240, win_h = 340;
// Background - dark mode
ui_draw_rect(win, 4, 30, win_w - 8, win_h - 34, COLOR_DARK_BG);
// Game status
if (game_over) {
ui_draw_string(win, 10, 36, "Game Over!", COLOR_TRAFFIC_RED);
} else if (game_won) {
ui_draw_string(win, 10, 36, "You Won!", 0xFF00FF00); // Bright green
} else {
ui_draw_string(win, 10, 36, "", COLOR_DARK_TEXT);
}
// Draw grid
int grid_start_x = 10;
int grid_start_y = 56;
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 - dark mode
ui_draw_rounded_rect_filled(win, px, py, CELL_SIZE, CELL_SIZE, 2, COLOR_DARK_PANEL);
if (grid[y][x] == -1) {
// Mine
ui_draw_string(win, px + 8, py + 6, "*", COLOR_TRAFFIC_RED);
} else if (grid[y][x] > 0) {
// Number
char num[2] = { '0' + grid[y][x], 0 };
ui_draw_string(win, px + 8, py + 6, num, COLOR_DARK_TEXT);
}
} else {
// Unrevealed cell
ui_draw_rounded_rect_filled(win, px, py, CELL_SIZE, CELL_SIZE, 2, COLOR_DARK_BORDER);
if (flagged[y][x]) {
ui_draw_string(win, px + 7, py + 6, "F", COLOR_TRAFFIC_RED);
}
}
}
}
// Draw new game button
int btn_y = grid_start_y + GRID_HEIGHT * CELL_SIZE + 10;
ui_draw_rounded_rect_filled(win, grid_start_x, btn_y, 70, 24, 4, COLOR_DARK_BORDER);
ui_draw_string(win, grid_start_x + 6, btn_y + 8, "New Game", COLOR_DARK_TEXT);
}
static void minesweeper_handle_click(ui_window_t win, int x, int y, int button) {
int grid_start_x = 10;
int grid_start_y = 56;
int btn_y = grid_start_y + GRID_HEIGHT * CELL_SIZE + 10;
// Check "New Game" button
if (x >= grid_start_x && x < grid_start_x + 70 &&
y >= btn_y && y < btn_y + 24) {
init_game();
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;
if (button == GUI_EVENT_RIGHT_CLICK) {
debug_print("[MINESWEEPER] Flagging cell");
flag_cell(cell_x, cell_y);
} else {
debug_print("[MINESWEEPER] Revealing cell");
reveal_cell(cell_x, cell_y);
}
}
}
int main(int argc, char **argv) {
(void)argc;
(void)argv;
ui_window_t win = ui_window_create("Minesweeper", 250, 100, 240, 340);
if (!win) return 1;
// Use current time or something for seed? No syscall for time right now.
random_seed = 987654321;
init_game();
gui_event_t ev;
while (1) {
if (ui_get_event(win, &ev)) {
if (ev.type == GUI_EVENT_PAINT) {
minesweeper_paint(win);
ui_mark_dirty(win, 0, 0, 240, 340);
} else if (ev.type == GUI_EVENT_CLICK) {
debug_print("[MINESWEEPER] LEFT CLICK");
minesweeper_handle_click(win, ev.arg1, ev.arg2, ev.type);
minesweeper_paint(win);
ui_mark_dirty(win, 0, 0, 240, 340);
} else if (ev.type == GUI_EVENT_RIGHT_CLICK) {
debug_print("[MINESWEEPER] RIGHT CLICK DETECTED");
minesweeper_handle_click(win, ev.arg1, ev.arg2, ev.type);
minesweeper_paint(win);
ui_mark_dirty(win, 0, 0, 240, 340);
} else if (ev.type == GUI_EVENT_CLOSE) {
sys_exit(0);
}
}
}
return 0;
}

Binary file not shown.

View File

@@ -0,0 +1,967 @@
// NanoJPEG -- KeyJ's Tiny Baseline JPEG Decoder
// version 1.3 (2012-03-05)
// by Martin J. Fiedler <martin.fiedler@gmx.net>
//
// This software is published under the terms of KeyJ's Research License,
// version 0.2. Usage of this software is subject to the following conditions:
// 0. There's no warranty whatsoever. The author(s) of this software can not
// be held liable for any damages that occur when using this software.
// 1. This software may be used freely for both non-commercial and commercial
// purposes.
// 2. This software may be redistributed freely as long as no fees are charged
// for the distribution and this license information is included.
// 3. This software may be modified freely except for this license information,
// which must not be changed in any way.
// 4. If anything other than configuration, indentation or comments have been
// altered in the code, the original author(s) must receive a copy of the
// modified code.
///////////////////////////////////////////////////////////////////////////////
// DOCUMENTATION SECTION //
// read this if you want to know what this is all about //
///////////////////////////////////////////////////////////////////////////////
// INTRODUCTION
// ============
//
// This is a minimal decoder for baseline JPEG images. It accepts memory dumps
// of JPEG files as input and generates either 8-bit grayscale or packed 24-bit
// RGB images as output. It does not parse JFIF or Exif headers; all JPEG files
// are assumed to be either grayscale or YCbCr. CMYK or other color spaces are
// not supported. All YCbCr subsampling schemes with power-of-two ratios are
// supported, as are restart intervals. Progressive or lossless JPEG is not
// supported.
// Summed up, NanoJPEG should be able to decode all images from digital cameras
// and most common forms of other non-progressive JPEG images.
// The decoder is not optimized for speed, it's optimized for simplicity and
// small code. Image quality should be at a reasonable level. A bicubic chroma
// upsampling filter ensures that subsampled YCbCr images are rendered in
// decent quality. The decoder is not meant to deal with broken JPEG files in
// a graceful manner; if anything is wrong with the bitstream, decoding will
// simply fail.
// The code should work with every modern C compiler without problems and
// should not emit any warnings. It uses only (at least) 32-bit integer
// arithmetic and is supposed to be endianness independent and 64-bit clean.
// However, it is not thread-safe.
// COMPILE-TIME CONFIGURATION
// ==========================
//
// The following aspects of NanoJPEG can be controlled with preprocessor
// defines:
//
// _NJ_EXAMPLE_PROGRAM = Compile a main() function with an example
// program.
// _NJ_INCLUDE_HEADER_ONLY = Don't compile anything, just act as a header
// file for NanoJPEG. Example:
// #define _NJ_INCLUDE_HEADER_ONLY
// #include "nanojpeg.c"
// int main(void) {
// njInit();
// // your code here
// njDone();
// }
// NJ_USE_LIBC=1 = Use the malloc(), free(), memset() and memcpy()
// functions from the standard C library (default).
// NJ_USE_LIBC=0 = Don't use the standard C library. In this mode,
// external functions njAlloc(), njFreeMem(),
// njFillMem() and njCopyMem() need to be defined
// and implemented somewhere.
// NJ_USE_WIN32=0 = Normal mode (default).
// NJ_USE_WIN32=1 = If compiling with MSVC for Win32 and
// NJ_USE_LIBC=0, NanoJPEG will use its own
// implementations of the required C library
// functions (default if compiling with MSVC and
// NJ_USE_LIBC=0).
// NJ_CHROMA_FILTER=1 = Use the bicubic chroma upsampling filter
// (default). // 图像resize的一种算法
// NJ_CHROMA_FILTER=0 = Use simple pixel repetition for chroma upsampling
// (bad quality, but faster and less code).
// API
// ===
//
// For API documentation, read the "header section" below.
// EXAMPLE
// =======
//
// A few pages below, you can find an example program that uses NanoJPEG to
// convert JPEG files into PGM or PPM. To compile it, use something like
// gcc -O3 -D_NJ_EXAMPLE_PROGRAM -o nanojpeg nanojpeg.c
// You may also add -std=c99 -Wall -Wextra -pedantic -Werror, if you want :)
///////////////////////////////////////////////////////////////////////////////
// HEADER SECTION //
// copy and pase this into nanojpeg.h if you want //
///////////////////////////////////////////////////////////////////////////////
#ifndef _NANOJPEG_H
#define _NANOJPEG_H
// nj_result_t: Result codes for njDecode().
typedef enum _nj_result {
NJ_OK = 0, // no error, decoding successful
NJ_NO_JPEG, // not a JPEG file
NJ_UNSUPPORTED, // unsupported format
NJ_OUT_OF_MEM, // out of memory
NJ_INTERNAL_ERR, // internal error
NJ_SYNTAX_ERROR, // syntax error
__NJ_FINISHED, // used internally, will never be reported
} nj_result_t;
// njInit: Initialize NanoJPEG.
// For safety reasons, this should be called at least one time before using
// using any of the other NanoJPEG functions.
void njInit(void);
// njDecode: Decode a JPEG image.
// Decodes a memory dump of a JPEG file into internal buffers.
// Parameters:
// jpeg = The pointer to the memory dump.
// size = The size of the JPEG file.
// Return value: The error code in case of failure, or NJ_OK (zero) on success.
nj_result_t njDecode(const void* jpeg, const int size);
// njGetWidth: Return the width (in pixels) of the most recently decoded
// image. If njDecode() failed, the result of njGetWidth() is undefined.
int njGetWidth(void);
// njGetHeight: Return the height (in pixels) of the most recently decoded
// image. If njDecode() failed, the result of njGetHeight() is undefined.
int njGetHeight(void);
// njIsColor: Return 1 if the most recently decoded image is a color image
// (RGB) or 0 if it is a grayscale image. If njDecode() failed, the result
// of njGetWidth() is undefined.
int njIsColor(void);
// njGetImage: Returns the decoded image data.
// Returns a pointer to the most recently image. The memory layout it byte-
// oriented, top-down, without any padding between lines. Pixels of color
// images will be stored as three consecutive bytes for the red, green and
// blue channels. This data format is thus compatible with the PGM or PPM
// file formats and the OpenGL texture formats GL_LUMINANCE8 or GL_RGB8.
// If njDecode() failed, the result of njGetImage() is undefined.
unsigned char* njGetImage(void);
// njGetImageSize: Returns the size (in bytes) of the image data returned
// by njGetImage(). If njDecode() failed, the result of njGetImageSize() is
// undefined.
int njGetImageSize(void);
// njDone: Uninitialize NanoJPEG.
// Resets NanoJPEG's internal state and frees all memory that has been
// allocated at run-time by NanoJPEG. It is still possible to decode another
// image after a njDone() call.
void njDone(void);
#endif//_NANOJPEG_H
///////////////////////////////////////////////////////////////////////////////
// CONFIGURATION SECTION //
// adjust the default settings for the NJ_ defines here //
///////////////////////////////////////////////////////////////////////////////
#ifndef NJ_USE_LIBC
#define NJ_USE_LIBC 1
#endif
#ifndef NJ_USE_WIN32
#ifdef _MSC_VER
#define NJ_USE_WIN32 (!NJ_USE_LIBC)
#else
#define NJ_USE_WIN32 0
#endif
#endif
#ifndef NJ_CHROMA_FILTER
#define NJ_CHROMA_FILTER 1
#endif
///////////////////////////////////////////////////////////////////////////////
// EXAMPLE PROGRAM //
// just define _NJ_EXAMPLE_PROGRAM to compile this (requires NJ_USE_LIBC) //
///////////////////////////////////////////////////////////////////////////////
#ifdef _NJ_EXAMPLE_PROGRAM
#include <stdio.h>
#include "libc/stdlib.h"
#include "libc/string.h"
int main(int argc, char* argv[]) {
int size;
char *buf;
FILE *f;
if (argc < 2) {
printf("Usage: %s <input.jpg> [<output.ppm>]\n", argv[0]);
return 2;
}
f = fopen(argv[1], "rb");
if (!f) {
printf("Error opening the input file.\n");
return 1;
}
fseek(f, 0, SEEK_END);
size = (int) ftell(f); // 字节
buf = malloc(size);
fseek(f, 0, SEEK_SET);
size = (int) fread(buf, 1, size, f); // 读取整个文件内容到buf
fclose(f);
njInit(); // 初始化nj_context_t
if (njDecode(buf, size)) {
printf("Error decoding the input file.\n");
return 1;
}
f = fopen((argc > 2) ? argv[2] : (njIsColor() ? "nanojpeg_out.ppm" : "nanojpeg_out.pgm"), "wb");
if (!f) {
printf("Error opening the output file.\n");
return 1;
}
fprintf(f, "P%d\n%d %d\n255\n", njIsColor() ? 6 : 5, njGetWidth(), njGetHeight());
fwrite(njGetImage(), 1, njGetImageSize(), f);
fclose(f);
njDone();
return 0;
}
#endif
// 解释什么是stride http://msdn.microsoft.com/en-us/library/windows/desktop/aa473780(v=vs.85).aspx
///////////////////////////////////////////////////////////////////////////////
// IMPLEMENTATION SECTION //
// you may stop reading here //
///////////////////////////////////////////////////////////////////////////////
#ifndef _NJ_INCLUDE_HEADER_ONLY
#include <stddef.h> // For NULL in freestanding mode
#ifdef _MSC_VER
#define NJ_INLINE static __inline
#define NJ_FORCE_INLINE static __forceinline
#else
#define NJ_INLINE static inline
#define NJ_FORCE_INLINE static inline
#endif
#if NJ_USE_LIBC
#include "libc/stdlib.h"
#define njAllocMem malloc
#define njFreeMem free
#define njFillMem memset
#define njCopyMem memcpy
#elif NJ_USE_WIN32
#include <windows.h>
#define njAllocMem(size) ((void*) LocalAlloc(LMEM_FIXED, (SIZE_T)(size)))
#define njFreeMem(block) ((void) LocalFree((HLOCAL) block))
NJ_INLINE void njFillMem(void* block, unsigned char value, int count) { __asm {
mov edi, block
mov al, value
mov ecx, count
rep stosb
} }
NJ_INLINE void njCopyMem(void* dest, const void* src, int count) { __asm {
mov edi, dest
mov esi, src
mov ecx, count
rep movsb
} }
#else
extern void* njAllocMem(int size);
extern void njFreeMem(void* block);
extern void njFillMem(void* block, unsigned char byte, int size);
extern void njCopyMem(void* dest, const void* src, int size);
#endif
typedef struct _nj_code {
unsigned char bits, code;
} nj_vlc_code_t;
typedef struct _nj_cmp {
int cid;
int ssx, ssy; // 水平/垂直因子
int width, height;
int stride;
int qtsel; // Quantization Table量化表
int actabsel, dctabsel; // AC/DC Huffman Table
int dcpred; // DC prediction
unsigned char *pixels;
} nj_component_t; // 颜色分量
typedef struct _nj_ctx {
nj_result_t error;
const unsigned char *pos; // 待解码数据指针(按字节来)
int size; // 整个数据的长度
int length; // 某一个marker内容的长度
int width, height; // 图片宽和高度
int mbwidth, mbheight; // MCU水平/垂直个数
int mbsizex, mbsizey; // MCU宽/高
int ncomp; // 颜色分量数
nj_component_t comp[3]; // YCbCr
int qtused, qtavail; // 这两个目前看不出来很大用处
unsigned char qtab[4][64]; // 但是目前似乎只有2个
nj_vlc_code_t vlctab[4][65536]; // 构造所有16位数的Huffman基数
// 目前基本上是4个(直/交/0/1)
int buf, bufbits; // 这是用来做什么的 buf是存放内容的 bufbits是计数器存放了多少个bits
int block[64];
int rstinterval;
unsigned char *rgb; // 解析出来的RGB所要占用的内存 // 每1个点包含3个字节按找RGB的顺序
} nj_context_t;
static nj_context_t nj;
static const char njZZ[64] = { 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18,
11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35,
42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45,
38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63 };
/*
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31
32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
*/
NJ_FORCE_INLINE unsigned char njClip(const int x) { // 限定范围是0 ~ 255之间
return (x < 0) ? 0 : ((x > 0xFF) ? 0xFF : (unsigned char) x);
}
#define W1 2841
#define W2 2676
#define W3 2408
#define W5 1609
#define W6 1108
#define W7 565
NJ_INLINE void njRowIDCT(int* blk) { // 按行来操作的 0 ~ 7 // 8 ~ 15
int x0, x1, x2, x3, x4, x5, x6, x7, x8;
if (!((x1 = blk[4] << 11)
| (x2 = blk[6])
| (x3 = blk[2])
| (x4 = blk[1])
| (x5 = blk[7])
| (x6 = blk[5])
| (x7 = blk[3])))
{
blk[0] = blk[1] = blk[2] = blk[3] = blk[4] = blk[5] = blk[6] = blk[7] = blk[0] << 3;
return;
}
x0 = (blk[0] << 11) + 128;
x8 = W7 * (x4 + x5);
x4 = x8 + (W1 - W7) * x4;
x5 = x8 - (W1 + W7) * x5;
x8 = W3 * (x6 + x7);
x6 = x8 - (W3 - W5) * x6;
x7 = x8 - (W3 + W5) * x7;
x8 = x0 + x1;
x0 -= x1;
x1 = W6 * (x3 + x2);
x2 = x1 - (W2 + W6) * x2;
x3 = x1 + (W2 - W6) * x3;
x1 = x4 + x6;
x4 -= x6;
x6 = x5 + x7;
x5 -= x7;
x7 = x8 + x3;
x8 -= x3;
x3 = x0 + x2;
x0 -= x2;
x2 = (181 * (x4 + x5) + 128) >> 8;
x4 = (181 * (x4 - x5) + 128) >> 8;
blk[0] = (x7 + x1) >> 8;
blk[1] = (x3 + x2) >> 8;
blk[2] = (x0 + x4) >> 8;
blk[3] = (x8 + x6) >> 8;
blk[4] = (x8 - x6) >> 8;
blk[5] = (x0 - x4) >> 8;
blk[6] = (x3 - x2) >> 8;
blk[7] = (x7 - x1) >> 8;
}
NJ_INLINE void njColIDCT(const int* blk, unsigned char *out, int stride) {
int x0, x1, x2, x3, x4, x5, x6, x7, x8;
if (!((x1 = blk[8*4] << 8)
| (x2 = blk[8*6])
| (x3 = blk[8*2])
| (x4 = blk[8*1])
| (x5 = blk[8*7])
| (x6 = blk[8*5])
| (x7 = blk[8*3])))
{
x1 = njClip(((blk[0] + 32) >> 6) + 128);
for (x0 = 8; x0; --x0) {
*out = (unsigned char) x1;
out += stride;
}
return;
}
x0 = (blk[0] << 8) + 8192;
x8 = W7 * (x4 + x5) + 4;
x4 = (x8 + (W1 - W7) * x4) >> 3;
x5 = (x8 - (W1 + W7) * x5) >> 3;
x8 = W3 * (x6 + x7) + 4;
x6 = (x8 - (W3 - W5) * x6) >> 3;
x7 = (x8 - (W3 + W5) * x7) >> 3;
x8 = x0 + x1;
x0 -= x1;
x1 = W6 * (x3 + x2) + 4;
x2 = (x1 - (W2 + W6) * x2) >> 3;
x3 = (x1 + (W2 - W6) * x3) >> 3;
x1 = x4 + x6;
x4 -= x6;
x6 = x5 + x7;
x5 -= x7;
x7 = x8 + x3;
x8 -= x3;
x3 = x0 + x2;
x0 -= x2;
x2 = (181 * (x4 + x5) + 128) >> 8; // YCb和Cr的值都范围都是-128 ~ 127并且在FDCT的时候有先减去128所以现在要IDCT之后再加上128
x4 = (181 * (x4 - x5) + 128) >> 8;
*out = njClip(((x7 + x1) >> 14) + 128); out += stride;
*out = njClip(((x3 + x2) >> 14) + 128); out += stride;
*out = njClip(((x0 + x4) >> 14) + 128); out += stride;
*out = njClip(((x8 + x6) >> 14) + 128); out += stride;
*out = njClip(((x8 - x6) >> 14) + 128); out += stride;
*out = njClip(((x0 - x4) >> 14) + 128); out += stride;
*out = njClip(((x3 - x2) >> 14) + 128); out += stride;
*out = njClip(((x7 - x1) >> 14) + 128);
}
#define njThrow(e) do { nj.error = e; return; } while (0)
#define njCheckError() do { if (nj.error) return; } while (0)
static int njShowBits(int bits) { // 能放得下大于32位的值么
unsigned char newbyte;
if (!bits) return 0;
while (nj.bufbits < bits) { // 也就是说要buf的位数小于已经buf的位数的时候就直接读出来
if (nj.size <= 0) {
nj.buf = (nj.buf << 8) | 0xFF;
nj.bufbits += 8;
continue;
}
newbyte = *nj.pos++; // 数据指针是按字节
nj.size--;
nj.bufbits += 8;
nj.buf = (nj.buf << 8) | newbyte; // 高位最终会被覆盖掉比如我要buf一个64位的值怎么办
if (newbyte == 0xFF) {
if (nj.size) {
unsigned char marker = *nj.pos++;
nj.size--;
switch (marker) {
case 0x00:
case 0xFF:
break;
case 0xD9: nj.size = 0; break;
default:
if ((marker & 0xF8) != 0xD0)
nj.error = NJ_SYNTAX_ERROR;
else {
nj.buf = (nj.buf << 8) | marker;
nj.bufbits += 8;
}
}
} else
nj.error = NJ_SYNTAX_ERROR;
}
}
return (nj.buf >> (nj.bufbits - bits)) & ((1 << bits) - 1);
}
NJ_INLINE void njSkipBits(int bits) {
if (nj.bufbits < bits)
(void) njShowBits(bits);
nj.bufbits -= bits;
}
NJ_INLINE int njGetBits(int bits) {
int res = njShowBits(bits);
njSkipBits(bits);
return res;
}
NJ_INLINE void njByteAlign(void) {
nj.bufbits &= 0xF8; // (1111 1000)8的倍数不满8的部分丢弃
}
static void njSkip(int count) {
nj.pos += count; // 数据指针增加
nj.size -= count; // 总体数据大小减去count
nj.length -= count; // 当前marker长度减去count
if (nj.size < 0) nj.error = NJ_SYNTAX_ERROR;
}
NJ_INLINE unsigned short njDecode16(const unsigned char *pos) {
return (pos[0] << 8) | pos[1]; // 00000000 00001101
}
static void njDecodeLength(void) { // decode长度字段这个方法调用一般都是已经进入到特定的marker之后
if (nj.size < 2) njThrow(NJ_SYNTAX_ERROR);
nj.length = njDecode16(nj.pos); // 该marker的长度(除去marker名字所占用的2个字节)
if (nj.length > nj.size) njThrow(NJ_SYNTAX_ERROR);
njSkip(2);
}
NJ_INLINE void njSkipMarker(void) {
njDecodeLength();
njSkip(nj.length);
}
NJ_INLINE void njDecodeSOF(void) { // 解析Start of Frame的时候就会把所需要的内存都分配好
int i, ssxmax = 0, ssymax = 0;
nj_component_t* c;
njDecodeLength(); // 解析长度并移动数据指针
if (nj.length < 9) njThrow(NJ_SYNTAX_ERROR);
if (nj.pos[0] != 8) njThrow(NJ_UNSUPPORTED); // 样本精度一般都是8
nj.height = njDecode16(nj.pos + 1); // 图片高度/宽度
nj.width = njDecode16(nj.pos + 3);
nj.ncomp = nj.pos[5]; // 颜色分量数据一般都是3
njSkip(6); // 之前共6个字节数据所以移动数据指针6个字节
switch (nj.ncomp) { // 目前只支持1和3这两种
case 1:
case 3:
break;
default:
njThrow(NJ_UNSUPPORTED);
}
if (nj.length < (nj.ncomp * 3)) njThrow(NJ_SYNTAX_ERROR); // 数据量肯定是要大于颜色分量数 multiply 3因为接着存颜色分量信息的每个结构占3个字节
// 颜色分量ID占用1个字节水平/垂直因子占用1个字节(高4位水平低4位垂直)量化表占用1个字节
for (i = 0, c = nj.comp; i < nj.ncomp; ++i, ++c) {
c->cid = nj.pos[0]; // 颜色分量ID
if (!(c->ssx = nj.pos[1] >> 4)) njThrow(NJ_SYNTAX_ERROR); // 高4位(水平因子)
if (c->ssx & (c->ssx - 1)) njThrow(NJ_UNSUPPORTED); // non-power of two
if (!(c->ssy = nj.pos[1] & 15)) njThrow(NJ_SYNTAX_ERROR); // (00001111)低4位(垂直因子)
if (c->ssy & (c->ssy - 1)) njThrow(NJ_UNSUPPORTED); // non-power of two
if ((c->qtsel = nj.pos[2]) & 0xFC) njThrow(NJ_SYNTAX_ERROR); // (11111101) 这里0xFC是用在这里干什么的
njSkip(3); // 移动数据指针到下一个颜色分量
nj.qtused |= 1 << c->qtsel; // 这里是做什么用的?看不出来
if (c->ssx > ssxmax) ssxmax = c->ssx; // 记录最大水平因子
if (c->ssy > ssymax) ssymax = c->ssy; // 记录最大垂直因子
}
if (nj.ncomp == 1) { // 只有一种颜色分量的时候就简单啦
c = nj.comp;
c->ssx = c->ssy = ssxmax = ssymax = 1;
}
nj.mbsizex = ssxmax << 3; // MCU宽 是 水平采样因子最大值 multiply 8
nj.mbsizey = ssymax << 3; // MCU高 是 垂直采样因子最大值 multiply 8
nj.mbwidth = (nj.width + nj.mbsizex - 1) / nj.mbsizex; // 分子采用+ nj.mbsizex - 1就取到大于但是最接近(等于)宽度的值,
// 并且这个值是MCU宽度整数倍 // 这里是水平方向MCU的个数
nj.mbheight = (nj.height + nj.mbsizey - 1) / nj.mbsizey; // 这里是垂直方向MCU的个数
for (i = 0, c = nj.comp; i < nj.ncomp; ++i, ++c) {
c->width = (nj.width * c->ssx + ssxmax - 1) / ssxmax; // 采样宽度? 最大水平/垂直因子的值就是图片原来的值,否则就会根据因子做相应的减少
c->stride = (c->width + 7) & 0x7FFFFFF8; // (0111 1111 1111 1111 1111 1111 1111 1000) 做什么以1234567结尾的都省略掉
// 变成8的整数
// 补齐8位注意前面有加7所以总是不会比原来的少比如原来是227那么这里就会变成232
// 这是按照数据单元计算的,所以不对
c->height = (nj.height * c->ssy + ssymax - 1) / ssymax;
c->stride = nj.mbwidth * nj.mbsizex * c->ssx / ssxmax; // 再计算一遍stride有什么用前面计算的是错误的没有考虑MCU宽度
// 这里都已经是round过的了所以直接计算
if (((c->width < 3) && (c->ssx != ssxmax)) || ((c->height < 3) && (c->ssy != ssymax))) njThrow(NJ_UNSUPPORTED);
if (!(c->pixels = njAllocMem(c->stride * (nj.mbheight * nj.mbsizey * c->ssy / ssymax)))) njThrow(NJ_OUT_OF_MEM); // 为分量分配内存
// 大小是所有MCU的
// 可能比图片实际
// 尺寸大
}
if (nj.ncomp == 3) { // 只有有3个颜色分量的时候才需要
nj.rgb = njAllocMem(nj.width * nj.height * nj.ncomp);
if (!nj.rgb) njThrow(NJ_OUT_OF_MEM);
}
njSkip(nj.length);
}
NJ_INLINE void njDecodeDHT(void) {
int codelen, currcnt, remain, spread, i, j;
nj_vlc_code_t *vlc;
static unsigned char counts[16]; // 码字
njDecodeLength();
while (nj.length >= 17) { // 码字的数量(16) + 类型和ID(1)
i = nj.pos[0]; // 类型和ID
if (i & 0xEC) njThrow(NJ_SYNTAX_ERROR); // (11101100)
if (i & 0x02) njThrow(NJ_UNSUPPORTED); // (00000010)
i = (i | (i >> 3)) & 3; // combined DC/AC + tableid value
// 直流0直流1交流0交流1
for (codelen = 1; codelen <= 16; ++codelen) // 码字长度
counts[codelen - 1] = nj.pos[codelen]; // 读取码字 DHT 当中的16个字节 00 01 05 01 01 01 01 01 01 00 00 00 00 00 00 00
njSkip(17);
vlc = &nj.vlctab[i][0];
remain = spread = 65536;
for (codelen = 1; codelen <= 16; ++codelen) {
spread >>= 1; // 干什么? // 65536 >> 16 = 1 每个category所包含的编码个数
currcnt = counts[codelen - 1];
if (!currcnt) continue; // 如果该位数没有码字
if (nj.length < currcnt) njThrow(NJ_SYNTAX_ERROR);
remain -= currcnt << (16 - codelen); // 干什么? 计算当前size的码字占用多少VLC表的空间得到剩下的空间
if (remain < 0) njThrow(NJ_SYNTAX_ERROR);
for (i = 0; i < currcnt; ++i) { // 码字个数,同样位数的码字可以有多个
register unsigned char code = nj.pos[i]; // 有多少个就,读多少个字节
for (j = spread; j; --j) { // 保存这么多个有什么作用?
vlc->bits = (unsigned char) codelen; // 码字位数
vlc->code = code; // 码字值(这个读取出来的到底是什么00 01 02 03 04 05 06 07 08 09 0A 0B是值还是权重)
++vlc;
}
}
njSkip(currcnt);
}
while (remain--) { // 16位都填充完成剩下的就用0填(1位码字XX个2位码字XX个...)
// printf("i'm nothing vlc id %d\n", tblid);
vlc->bits = 0;
++vlc;
}
// for debug
// printf("Huffman vlc id %d\n", tblid);
// njPrintHT(tblid);
}
if (nj.length) njThrow(NJ_SYNTAX_ERROR);
}
NJ_INLINE void njDecodeDQT(void) {
int i;
unsigned char *t;
njDecodeLength();
while (nj.length >= 65) {
i = nj.pos[0]; // QT信息高4位为QT精度低4位为QT号
if (i & 0xFC) njThrow(NJ_SYNTAX_ERROR); // (1111 1110)这个用来检测QT号码是否正确的吗目前精度好像都为0所以这么写
nj.qtavail |= 1 << i; // XXX 直接通过这里转换为数量?
t = &nj.qtab[i][0];
for (i = 0; i < 64; ++i)
t[i] = nj.pos[i + 1]; // 读取到QT数组当中但应该还是按照文件流当中的排列
njSkip(65);
}
if (nj.length) njThrow(NJ_SYNTAX_ERROR);
}
NJ_INLINE void njDecodeDRI(void) {
njDecodeLength();
if (nj.length < 2) njThrow(NJ_SYNTAX_ERROR);
nj.rstinterval = njDecode16(nj.pos);
njSkip(nj.length);
}
static int njGetVLC(nj_vlc_code_t* vlc, unsigned char* code) { // Variable Length Coding
int value = njShowBits(16); // 为什么是2个字节 这又是什么? 或许是这里的Huffman编码的码字永远是少于16位的
int bits = vlc[value].bits;
if (!bits) { nj.error = NJ_SYNTAX_ERROR; return 0; }
njSkipBits(bits);
value = vlc[value].code;
if (code) *code = (unsigned char) value;
bits = value & 15; // 这个value必须是0~15之间
if (!bits) {
return 0;
}
value = njGetBits(bits); // 如果这里需要读取的值的位数超过之前njShowBits剩余的值这里会重新读取
if (value < (1 << (bits - 1)))
value += ((-1) << bits) + 1;
return value;
}
NJ_INLINE void njDecodeBlock(nj_component_t* c, unsigned char* out) { // 8 x 8
unsigned char code = 0;
int value, coef = 0;
njFillMem(nj.block, 0, sizeof(nj.block));
int dcvlcval = njGetVLC(&nj.vlctab[c->dctabsel][0], NULL);
c->dcpred += dcvlcval;
nj.block[0] = (c->dcpred) * nj.qtab[c->qtsel][0]; // DC // 这里是反量化?
do {
value = njGetVLC(&nj.vlctab[c->actabsel][0], &code); // DC 2/3
if (!code) break; // EOB
if (!(code & 0x0F) && (code != 0xF0)) njThrow(NJ_SYNTAX_ERROR); // 这是什么字段?(难道是为了兼容这个过程中可以遇到0xF0这样的数据)
coef += (code >> 4) + 1; // coefficient 系数
if (coef > 63) njThrow(NJ_SYNTAX_ERROR);
nj.block[(int) njZZ[coef]] = value * nj.qtab[c->qtsel][coef]; // AC 这里是反量化?
} while (coef < 63);
for (coef = 0; coef < 64; coef += 8)
njRowIDCT(&nj.block[coef]); // 上面先Huffman解码/反量化,这里行(反DCT)
for (coef = 0; coef < 8; ++coef)
njColIDCT(&nj.block[coef], &out[coef], c->stride);
}
NJ_INLINE void njDecodeScan(void) {
// njPrintHT(0);
// njPrintHT(2);
// njPrintHT(1);
// njPrintHT(3);
int i, mbx, mby, sbx, sby;
int rstcount = nj.rstinterval, nextrst = 0;
nj_component_t* c;
njDecodeLength();
if (nj.length < (4 + 2 * nj.ncomp)) njThrow(NJ_SYNTAX_ERROR);
if (nj.pos[0] != nj.ncomp) njThrow(NJ_UNSUPPORTED);
njSkip(1); // 颜色分量数量
for (i = 0, c = nj.comp; i < nj.ncomp; ++i, ++c) {
if (nj.pos[0] != c->cid) njThrow(NJ_SYNTAX_ERROR); // 颜色分量ID
if (nj.pos[1] & 0xEE) njThrow(NJ_SYNTAX_ERROR);
c->dctabsel = nj.pos[1] >> 4; // 高4位为直流表DC Table
c->actabsel = (nj.pos[1] & 1) | 2; // 低4位为交流表AC Table(这里有做特殊处理所以AC的表名不会和DC相同)
njSkip(2);
}
if (nj.pos[0] || (nj.pos[1] != 63) || nj.pos[2]) njThrow(NJ_UNSUPPORTED);
njSkip(nj.length); // 忽略3个字节 通常为 00 3F 00
// 2 + 1 + 6 + 3为12字节这个marker的长度刚好为12字节
// 接下来都是编码过的图像数据
for (mbx = mby = 0;;) {
for (i = 0, c = nj.comp; i < nj.ncomp; ++i, ++c) // 每个分量都要decode
for (sby = 0; sby < c->ssy; ++sby) // 水平/垂直因子
for (sbx = 0; sbx < c->ssx; ++sbx) {
njDecodeBlock(c, &c->pixels[((mby * c->ssy + sby) * c->stride + mbx * c->ssx + sbx) << 3]); // 读取原始编码过
// 的图片数据到block中
// 并反量化,反离散余弦变换
njCheckError();
}
if (++mbx >= nj.mbwidth) { // 读完所有的MCU到达最右就返回从下一行开始
mbx = 0;
if (++mby >= nj.mbheight) break; // 到达最底行的时候推出decode结束
}
if (nj.rstinterval && !(--rstcount)) { // restart marker
njByteAlign();
i = njGetBits(16);
if (((i & 0xFFF8) != 0xFFD0) || ((i & 7) != nextrst)) njThrow(NJ_SYNTAX_ERROR);
nextrst = (nextrst + 1) & 7;
rstcount = nj.rstinterval;
for (i = 0; i < 3; ++i)
nj.comp[i].dcpred = 0;
}
}
nj.error = __NJ_FINISHED;
}
#if NJ_CHROMA_FILTER
#define CF4A (-9)
#define CF4B (111)
#define CF4C (29)
#define CF4D (-3)
#define CF3A (28)
#define CF3B (109)
#define CF3C (-9)
#define CF3X (104)
#define CF3Y (27)
#define CF3Z (-3)
#define CF2A (139)
#define CF2B (-11)
#define CF(x) njClip(((x) + 64) >> 7)
// 通常我们放大图片的时候就需要upsampling缩小的时候就downsampling通称为resampling
// 这里Cb/Cr分量的会少些所以需要upsampling
NJ_INLINE void njUpsampleH(nj_component_t* c) {
const int xmax = c->width - 3;
unsigned char *out, *lin, *lout;
int x, y;
out = njAllocMem((c->width * c->height) << 1);
if (!out) njThrow(NJ_OUT_OF_MEM);
lin = c->pixels;
lout = out;
for (y = c->height; y; --y) {
lout[0] = CF(CF2A * lin[0] + CF2B * lin[1]);
lout[1] = CF(CF3X * lin[0] + CF3Y * lin[1] + CF3Z * lin[2]);
lout[2] = CF(CF3A * lin[0] + CF3B * lin[1] + CF3C * lin[2]);
for (x = 0; x < xmax; ++x) {
lout[(x << 1) + 3] = CF(CF4A * lin[x] + CF4B * lin[x + 1] + CF4C * lin[x + 2] + CF4D * lin[x + 3]);
lout[(x << 1) + 4] = CF(CF4D * lin[x] + CF4C * lin[x + 1] + CF4B * lin[x + 2] + CF4A * lin[x + 3]);
}
lin += c->stride;
lout += c->width << 1;
lout[-3] = CF(CF3A * lin[-1] + CF3B * lin[-2] + CF3C * lin[-3]);
lout[-2] = CF(CF3X * lin[-1] + CF3Y * lin[-2] + CF3Z * lin[-3]);
lout[-1] = CF(CF2A * lin[-1] + CF2B * lin[-2]);
}
c->width <<= 1;
c->stride = c->width;
njFreeMem(c->pixels);
c->pixels = out;
}
NJ_INLINE void njUpsampleV(nj_component_t* c) {
const int w = c->width, s1 = c->stride, s2 = s1 + s1;
unsigned char *out, *cin, *cout;
int x, y;
out = njAllocMem((c->width * c->height) << 1);
if (!out) njThrow(NJ_OUT_OF_MEM);
for (x = 0; x < w; ++x) {
cin = &c->pixels[x];
cout = &out[x];
*cout = CF(CF2A * cin[0] + CF2B * cin[s1]); cout += w;
*cout = CF(CF3X * cin[0] + CF3Y * cin[s1] + CF3Z * cin[s2]); cout += w;
*cout = CF(CF3A * cin[0] + CF3B * cin[s1] + CF3C * cin[s2]); cout += w;
cin += s1;
for (y = c->height - 3; y; --y) {
*cout = CF(CF4A * cin[-s1] + CF4B * cin[0] + CF4C * cin[s1] + CF4D * cin[s2]); cout += w;
*cout = CF(CF4D * cin[-s1] + CF4C * cin[0] + CF4B * cin[s1] + CF4A * cin[s2]); cout += w;
cin += s1;
}
cin += s1;
*cout = CF(CF3A * cin[0] + CF3B * cin[-s1] + CF3C * cin[-s2]); cout += w;
*cout = CF(CF3X * cin[0] + CF3Y * cin[-s1] + CF3Z * cin[-s2]); cout += w;
*cout = CF(CF2A * cin[0] + CF2B * cin[-s1]);
}
c->height <<= 1;
c->stride = c->width;
njFreeMem(c->pixels);
c->pixels = out;
}
#else
NJ_INLINE void njUpsample(nj_component_t* c) {
int x, y, xshift = 0, yshift = 0;
unsigned char *out, *lin, *lout;
while (c->width < nj.width) { c->width <<= 1; ++xshift; }
while (c->height < nj.height) { c->height <<= 1; ++yshift; }
out = njAllocMem(c->width * c->height); // 放大后的尺寸
if (!out) njThrow(NJ_OUT_OF_MEM);
lin = c->pixels;
lout = out;
for (y = 0; y < c->height; ++y) {
lin = &c->pixels[(y >> yshift) * c->stride];
for (x = 0; x < c->width; ++x)
lout[x] = lin[x >> xshift];
lout += c->width;
}
c->stride = c->width;
njFreeMem(c->pixels);
c->pixels = out;
}
#endif
NJ_INLINE void njConvert() {
int i;
nj_component_t* c;
for (i = 0, c = nj.comp; i < nj.ncomp; ++i, ++c) { // 如果需要的话就upsampling
#if NJ_CHROMA_FILTER
while ((c->width < nj.width) || (c->height < nj.height)) {
if (c->width < nj.width) njUpsampleH(c);
njCheckError();
if (c->height < nj.height) njUpsampleV(c);
njCheckError();
}
#else
if ((c->width < nj.width) || (c->height < nj.height))
njUpsample(c);
#endif
if ((c->width < nj.width) || (c->height < nj.height)) njThrow(NJ_INTERNAL_ERR);
}
if (nj.ncomp == 3) { // SEE njGetImage()
// convert to RGB
int x, yy;
unsigned char *prgb = nj.rgb;
const unsigned char *py = nj.comp[0].pixels;
const unsigned char *pcb = nj.comp[1].pixels;
const unsigned char *pcr = nj.comp[2].pixels;
// 多余的数据(编/解码是对齐用的)会被丢弃吗?
for (yy = nj.height; yy; --yy) { // 列
for (x = 0; x < nj.width; ++x) { // 行
register int y = py[x] << 8; // 这是为什么? 色彩空间转换公式计算需要
register int cb = pcb[x] - 128; // YCbCr的Cb和Cr一般都是有符号数但是在JPEG当中都是无符号数
register int cr = pcr[x] - 128;
*prgb++ = njClip((y + 359 * cr + 128) >> 8); // 色彩空间转换YCbCr到RGB
*prgb++ = njClip((y - 88 * cb - 183 * cr + 128) >> 8);
*prgb++ = njClip((y + 454 * cb + 128) >> 8);
}
py += nj.comp[0].stride; // 移动YCbCr数据指针每一行都是有stride的所以当需要的数据都得到时后面的就不管直接丢弃移动到下一行
pcb += nj.comp[1].stride;
pcr += nj.comp[2].stride;
}
} else if (nj.comp[0].width != nj.comp[0].stride) { // 如果宽度和stride都一样什么都不用做
// grayscale -> only remove stride
unsigned char *pin = &nj.comp[0].pixels[nj.comp[0].stride];
unsigned char *pout = &nj.comp[0].pixels[nj.comp[0].width];
int y;
for (y = nj.comp[0].height - 1; y; --y) {
njCopyMem(pout, pin, nj.comp[0].width);
pin += nj.comp[0].stride;
pout += nj.comp[0].width;
}
nj.comp[0].stride = nj.comp[0].width;
}
}
void njInit(void) {
njFillMem(&nj, 0, sizeof(nj_context_t)); // 初始化nj_context_t
}
void njDone(void) {
int i;
for (i = 0; i < 3; ++i)
if (nj.comp[i].pixels) njFreeMem((void*) nj.comp[i].pixels);
if (nj.rgb) njFreeMem((void*) nj.rgb);
njInit();
}
nj_result_t njDecode(const void* jpeg, const int size) {
njDone();
nj.pos = (const unsigned char*) jpeg;
nj.size = size & 0x7FFFFFFF; //
if (nj.size < 2) return NJ_NO_JPEG;
if ((nj.pos[0] ^ 0xFF) | (nj.pos[1] ^ 0xD8)) return NJ_NO_JPEG; // 不以0xFFD8打头(为什么要用异或来判断?)
njSkip(2);
while (!nj.error) { // 有“错误”的时候离开
if ((nj.size < 2) || (nj.pos[0] != 0xFF)) return NJ_SYNTAX_ERROR; // 太小或者不以0xFF打头
njSkip(2); // 移动到标签的后面(长度字段的前面)
switch (nj.pos[-1]) {
case 0xC0: njDecodeSOF(); break;
case 0xC4: njDecodeDHT(); break;
case 0xDB: njDecodeDQT(); break;
case 0xDD: njDecodeDRI(); break;
case 0xDA: njDecodeScan(); break;
case 0xFE: njSkipMarker(); break;
default:
if ((nj.pos[-1] & 0xF0) == 0xE0) // JPG0和APP0字段目前都忽略
njSkipMarker();
else
return NJ_UNSUPPORTED;
}
}
if (nj.error != __NJ_FINISHED) return nj.error;
nj.error = NJ_OK;
njConvert();
return nj.error;
}
int njGetWidth(void) { return nj.width; }
int njGetHeight(void) { return nj.height; }
int njIsColor(void) { return (nj.ncomp != 1); }
unsigned char* njGetImage(void) { return (nj.ncomp == 1) ? nj.comp[0].pixels : nj.rgb; } // 一/三个分量
int njGetImageSize(void) { return nj.width * nj.height * nj.ncomp; }
#endif // _NJ_INCLUDE_HEADER_ONLY

View File

@@ -0,0 +1,10 @@
// nanojpeg.h - Header for NanoJPEG decoder (freestanding kernel use)
#ifndef NANOJPEG_H
#define NANOJPEG_H
// Include naojpeg.c in header-only mode to get the type/function declarations
#define _NJ_INCLUDE_HEADER_ONLY
#include "nanojpeg.c"
#undef _NJ_INCLUDE_HEADER_ONLY
#endif // NANOJPEG_H

Binary file not shown.

Binary file not shown.

251
src/kernel/userland/paint.c Normal file
View File

@@ -0,0 +1,251 @@
#include "libc/syscall.h"
#include "libc/libui.h"
#include "libc/stdlib.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define CANVAS_W 300
#define CANVAS_H 200
#define PAINT_MAGIC 0x544E5042 // 'BPNT'
#define COLOR_BLACK 0xFF000000
#define COLOR_WHITE 0xFFFFFFFF
#define COLOR_RED 0xFFFF0000
#define COLOR_APPLE_GREEN 0xFF4CD964
#define COLOR_APPLE_BLUE 0xFF007AFF
#define COLOR_APPLE_YELLOW 0xFFFFCC00
#define COLOR_DARK_BG 0xFF121212
#define COLOR_DARK_PANEL 0xFF202020
#define COLOR_DARK_BORDER 0xFF404040
#define COLOR_DARK_TEXT 0xFFE0E0E0
static uint32_t *canvas_buffer = NULL;
static uint32_t current_color = COLOR_BLACK;
static int last_mx = -1;
static int last_my = -1;
static char current_file_path[256] = "/Desktop/drawing.pnt";
static void paint_strcpy(char *dest, const char *src) {
while (*src) *dest++ = *src++;
*dest = 0;
}
static void debug_print(const char *msg) {
sys_write(1, msg, 0);
int i = 0;
while (msg[i]) i++;
sys_write(1, msg, i);
sys_write(1, "\n", 1);
}
static void paint_reset(void) {
if (canvas_buffer) {
for (int i = 0; i < CANVAS_W * CANVAS_H; i++) {
canvas_buffer[i] = COLOR_WHITE;
}
}
}
static void paint_paint(ui_window_t win) {
int canvas_x = 60;
int canvas_y = 30;
// Canvas Area with dark background and rounded corners (draw first so it's behind everything)
ui_draw_rounded_rect_filled(win, canvas_x - 2, canvas_y - 2, CANVAS_W + 4, CANVAS_H + 4, 4, COLOR_DARK_BG);
// Toolbar area - dark mode
ui_draw_rounded_rect_filled(win, 10, 30, 40, 260 - 40, 6, COLOR_DARK_PANEL);
// Color Palette with rounded corners
uint32_t colors[] = {COLOR_BLACK, COLOR_RED, COLOR_APPLE_GREEN, COLOR_APPLE_BLUE, COLOR_APPLE_YELLOW, COLOR_WHITE};
for (int i = 0; i < 6; i++) {
int cy = 40 + (i * 25);
ui_draw_rounded_rect_filled(win, 15, cy, 30, 20, 3, colors[i]);
// Highlight selected color with border
if (current_color == colors[i]) {
// Note: libui might not have draw_rounded_rect (hollow), so we just draw four lines simulating it
// or we use ui_draw_rect for hollow border
ui_draw_rect(win, 13, cy - 2, 34, 1, COLOR_DARK_TEXT);
ui_draw_rect(win, 13, cy - 2 + 24, 34, 1, COLOR_DARK_TEXT);
ui_draw_rect(win, 13, cy - 2, 1, 24, COLOR_DARK_TEXT);
ui_draw_rect(win, 13 + 34, cy - 2, 1, 24, COLOR_DARK_TEXT);
}
}
// Toolbar Buttons - dark mode with rounded corners
ui_draw_rounded_rect_filled(win, 12, 260 - 65, 36, 20, 4, COLOR_DARK_BORDER);
ui_draw_string(win, 18, 260 - 58, "CLR", COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, 12, 260 - 40, 36, 20, 4, COLOR_DARK_BORDER);
ui_draw_string(win, 18, 260 - 33, "SAV", COLOR_DARK_TEXT);
// Draw canvas content
if (canvas_buffer) {
for (int y = 0; y < CANVAS_H; y++) {
for (int x = 0; x < CANVAS_W; x++) {
uint32_t color = canvas_buffer[y * CANVAS_W + x];
ui_draw_rect(win, canvas_x + x, canvas_y + y, 1, 1, color);
}
}
}
}
static void paint_put_brush(ui_window_t win, int cx, int cy) {
if (!canvas_buffer) return;
for (int dy = 0; dy < 2; dy++) {
for (int dx = 0; dx < 2; dx++) {
int px = cx + dx;
int py = cy + dy;
if (px >= 0 && px < CANVAS_W && py >= 0 && py < CANVAS_H) {
canvas_buffer[py * CANVAS_W + px] = current_color;
ui_draw_rect(win, 60 + px, 30 + py, 1, 1, current_color);
}
}
}
ui_mark_dirty(win, 60 + cx, 30 + cy, 2, 2);
}
void paint_handle_mouse(ui_window_t win, int x, int y) {
int cx = x - 60;
int cy = y - 30;
if (cx < 0 || cx >= CANVAS_W || cy < 0 || cy >= CANVAS_H) {
last_mx = -1;
return;
}
if (last_mx == -1) {
paint_put_brush(win, cx, cy);
} else {
// Bresenham's line algorithm to fill gaps between points
int x0 = last_mx, y0 = last_my;
int x1 = cx, y1 = cy;
int dx = (x1 - x0 > 0) ? (x1 - x0) : (x0 - x1);
int dy = (y1 - y0 > 0) ? (y1 - y0) : (y0 - y1);
int sx = x0 < x1 ? 1 : -1;
int sy = y0 < y1 ? 1 : -1;
int err = dx - dy;
while (1) {
paint_put_brush(win, x0, y0);
if (x0 == x1 && y0 == y1) break;
int e2 = 2 * err;
if (e2 > -dy) { err -= dy; x0 += sx; }
if (e2 < dx) { err += dx; y0 += sy; }
}
}
last_mx = cx;
last_my = cy;
}
void paint_reset_last_pos(void) {
last_mx = -1;
last_my = -1;
}
// Simple window message dialog wrapper using syscall
static void wm_show_message(const char *title, const char *msg) {
// Wait, userland doesn't have wm_show_message syscall available yet, or maybe it does?
// We didn't add it or GUI_EVENT doesn't support it directly.
// For now we do nothing, or just open a small window.
}
static void paint_save(const char *path) {
int fd = sys_open(path, "w");
if (fd >= 0) {
uint32_t header[3] = {PAINT_MAGIC, CANVAS_W, CANVAS_H};
sys_write_fs(fd, (char*)header, sizeof(header));
sys_write_fs(fd, (char*)canvas_buffer, CANVAS_W * CANVAS_H * sizeof(uint32_t));
sys_close(fd);
wm_show_message("Paint", "Image saved.");
}
}
void paint_load(const char *path) {
paint_strcpy(current_file_path, path);
int fd = sys_open(path, "r");
if (fd >= 0) {
uint32_t header[3];
if (sys_read(fd, (char*)header, sizeof(header)) == sizeof(header)) {
if (header[0] == PAINT_MAGIC) {
sys_read(fd, (char*)canvas_buffer, CANVAS_W * CANVAS_H * sizeof(uint32_t));
}
}
sys_close(fd);
}
}
static void paint_click(ui_window_t win, int x, int y) {
// Check Buttons
if (x >= 12 && x < 48) {
if (y >= 260 - 65 && y < 260 - 45) {
paint_reset();
paint_paint(win);
ui_mark_dirty(win, 0, 0, 380, 260);
return;
}
if (y >= 260 - 40 && y < 260 - 20) {
paint_save(current_file_path);
return;
}
}
// Check Palette
if (x >= 15 && x < 45) {
for (int i = 0; i < 6; i++) {
int cy = 40 + (i * 25);
if (y >= cy && y < cy + 20) {
uint32_t colors[] = {COLOR_BLACK, COLOR_RED, COLOR_APPLE_GREEN, COLOR_APPLE_BLUE, COLOR_APPLE_YELLOW, COLOR_WHITE};
current_color = colors[i];
paint_paint(win);
ui_mark_dirty(win, 0, 0, 380, 260);
return;
}
}
}
paint_handle_mouse(win, x, y);
}
int main(int argc, char **argv) {
ui_window_t win = ui_window_create("Paint", 150, 100, 380, 260);
if (!win) return 1;
canvas_buffer = malloc(CANVAS_W * CANVAS_H * sizeof(uint32_t));
if (!canvas_buffer) return 1;
paint_reset();
if (argc > 1) {
paint_load(argv[1]);
}
gui_event_t ev;
while (1) {
if (ui_get_event(win, &ev)) {
if (ev.type == GUI_EVENT_PAINT) {
paint_paint(win);
ui_mark_dirty(win, 0, 0, 380, 260);
} else if (ev.type == GUI_EVENT_CLICK) {
paint_click(win, ev.arg1, ev.arg2);
paint_paint(win);
ui_mark_dirty(win, 0, 0, 380, 260);
} else if (ev.type == GUI_EVENT_MOUSE_DOWN) {
paint_handle_mouse(win, ev.arg1, ev.arg2);
} else if (ev.type == GUI_EVENT_MOUSE_UP) {
paint_reset_last_pos();
} else if (ev.type == GUI_EVENT_MOUSE_MOVE) {
if (ev.arg3 & 0x01) { // Left button down
paint_handle_mouse(win, ev.arg1, ev.arg2);
} else {
paint_reset_last_pos();
}
} else if (ev.type == GUI_EVENT_CLOSE) {
sys_exit(0);
}
}
}
return 0;
}

BIN
src/kernel/userland/paint.elf Executable file

Binary file not shown.

View File

@@ -0,0 +1,691 @@
#include "libc/syscall.h"
#include "libc/libui.h"
#include <stddef.h>
#define COLOR_COFFEE 0xFF6B4423
#define COLOR_TEAL 0xFF008080
#define COLOR_GREEN 0xFF008000
#define COLOR_BLUE_BG 0xFF000080
#define COLOR_PURPLE 0xFF800080
#define COLOR_GREY 0xFF454545
#define COLOR_BLACK 0xFF000000
#define COLOR_DARK_PANEL 0xFF2D2D2D
#define COLOR_DARK_TEXT 0xFFE0E0E0
#define COLOR_DARK_BORDER 0xFF404040
#define COLOR_DKGRAY 0xFFAAAAAA
#define COLOR_DARK_BG 0xFF1E1E1E
// Control panel state
#define VIEW_MAIN 0
#define VIEW_WALLPAPER 1
#define VIEW_NETWORK 2
#define VIEW_DESKTOP 3
#define VIEW_MOUSE 4
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;
static int input_cursor = 0;
static char net_status[64] = "";
// Pattern buffers (128x128)
#define PATTERN_SIZE 128
static uint32_t pattern_lumberjack[PATTERN_SIZE * PATTERN_SIZE];
static uint32_t pattern_blue_diamond[PATTERN_SIZE * PATTERN_SIZE];
#define WALLPAPER_THUMB_W 100
#define WALLPAPER_THUMB_H 60
static uint32_t moon_thumb[WALLPAPER_THUMB_W * WALLPAPER_THUMB_H];
static uint32_t mtn_thumb[WALLPAPER_THUMB_W * WALLPAPER_THUMB_H];
static _Bool moon_thumb_valid = 0;
static _Bool mtn_thumb_valid = 0;
static _Bool desktop_snap_to_grid = 1;
static _Bool desktop_auto_align = 1;
static int desktop_max_rows_per_col = 10;
static int desktop_max_cols = 10;
static int mouse_speed = 10;
static void cli_itoa(int num, char *str) {
if (num == 0) {
str[0] = '0';
str[1] = '\0';
return;
}
int t = num;
int len = 0;
while (t > 0) { len++; t /= 10; }
str[len] = '\0';
for (int i = len - 1; i >= 0; i--) {
str[i] = (num % 10) + '0';
num /= 10;
}
}
static void generate_lumberjack_pattern(void) {
uint32_t red = 0xFFDC143C;
uint32_t dark_grey = 0xFF404040;
uint32_t black = 0xFF000000;
int scale = 5;
for (int y = 0; y < PATTERN_SIZE; y++) {
for (int x = 0; x < PATTERN_SIZE; x++) {
int cell_x = (x / scale) % 3;
int cell_y = (y / scale) % 3;
uint32_t color;
if (cell_x == 1 && cell_y == 1) {
color = black;
} else if (cell_x == 1 || cell_y == 1) {
color = dark_grey;
} else {
color = red;
}
pattern_lumberjack[y * PATTERN_SIZE + x] = color;
}
}
}
static void generate_blue_diamond_pattern(void) {
uint32_t bg_color = 0xFFADD8E6;
uint32_t diamond_color = 0xFF0000CD;
for (int y = 0; y < PATTERN_SIZE; y++) {
for (int x = 0; x < PATTERN_SIZE; x++) {
pattern_blue_diamond[y * PATTERN_SIZE + x] = bg_color;
}
}
for (int dy = -24; dy <= 24; dy++) {
for (int dx = -24; dx <= 24; dx++) {
int abs_dx = dx < 0 ? -dx : dx;
int abs_dy = dy < 0 ? -dy : dy;
if (abs_dx + abs_dy <= 24) {
int x1 = 32 + dx;
int y1 = 32 + dy;
if (x1 >= 0 && x1 < PATTERN_SIZE && y1 >= 0 && y1 < PATTERN_SIZE) {
pattern_blue_diamond[y1 * PATTERN_SIZE + x1] = diamond_color;
}
int x2 = 96 + dx;
int y2 = 96 + dy;
if (x2 >= 0 && x2 < PATTERN_SIZE && y2 >= 0 && y2 < PATTERN_SIZE) {
pattern_blue_diamond[y2 * PATTERN_SIZE + x2] = diamond_color;
}
}
}
}
}
static uint32_t parse_rgb_separate(const char *r, const char *g, const char *b) {
int rv = 0, gv = 0, bv = 0;
for (int i = 0; r[i] && i < 3; i++) {
if (r[i] >= '0' && r[i] <= '9') rv = rv * 10 + (r[i] - '0');
}
for (int i = 0; g[i] && i < 3; i++) {
if (g[i] >= '0' && g[i] <= '9') gv = gv * 10 + (g[i] - '0');
}
for (int i = 0; b[i] && i < 3; i++) {
if (b[i] >= '0' && b[i] <= '9') bv = bv * 10 + (b[i] - '0');
}
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(ui_window_t win) {
int offset_x = 8;
int offset_y = 21;
int win_w = 350;
int item_y = 15;
int item_h = 60;
int item_spacing = 10;
// Wallpaper
ui_draw_rounded_rect_filled(win, offset_x, offset_y + item_y, win_w - 16, item_h, 8, COLOR_DARK_PANEL);
ui_draw_rect(win, offset_x + 12, offset_y + item_y + 8, 40, 40, 0xFF87CEEB);
ui_draw_rect(win, offset_x + 12, offset_y + item_y + 28, 40, 20, 0xFF90EE90);
ui_draw_rect(win, offset_x + 24, offset_y + item_y + 22, 3, 6, 0xFF654321);
ui_draw_rect(win, offset_x + 21, offset_y + item_y + 18, 9, 8, 0xFF228B22);
ui_draw_string(win, offset_x + 60, offset_y + item_y + 15, "Wallpaper", COLOR_DARK_TEXT);
ui_draw_string(win, offset_x + 60, offset_y + item_y + 35, "Choose wallpaper", COLOR_DKGRAY);
// Network
item_y += item_h + item_spacing;
ui_draw_rounded_rect_filled(win, offset_x, offset_y + item_y, win_w - 16, item_h, 8, COLOR_DARK_PANEL);
ui_draw_rect(win, offset_x + 18, offset_y + item_y + 12, 24, 24, 0xFF4169E1);
ui_draw_rect(win, offset_x + 22, offset_y + item_y + 16, 16, 16, 0xFF87CEEB);
ui_draw_string(win, offset_x + 60, offset_y + item_y + 15, "Network", COLOR_DARK_TEXT);
ui_draw_string(win, offset_x + 60, offset_y + item_y + 35, "Internet and connectivity", COLOR_DKGRAY);
// Desktop
item_y += item_h + item_spacing;
ui_draw_rounded_rect_filled(win, offset_x, offset_y + item_y, win_w - 16, item_h, 8, COLOR_DARK_PANEL);
ui_draw_rect(win, offset_x + 12, offset_y + item_y + 10, 36, 8, 0xFFE0C060);
ui_draw_rect(win, offset_x + 12, offset_y + item_y + 18, 36, 22, 0xFFD4A574);
ui_draw_string(win, offset_x + 60, offset_y + item_y + 15, "Desktop", COLOR_DARK_TEXT);
ui_draw_string(win, offset_x + 60, offset_y + item_y + 35, "Desktop alignment", COLOR_DKGRAY);
// Mouse
item_y += item_h + item_spacing;
ui_draw_rounded_rect_filled(win, offset_x, offset_y + item_y, win_w - 16, item_h, 8, COLOR_DARK_PANEL);
ui_draw_rect(win, offset_x + 18, offset_y + item_y + 8, 20, 28, 0xFFD3D3D3);
ui_draw_rect(win, offset_x + 20, offset_y + item_y + 10, 16, 10, 0xFFB0B0B0);
ui_draw_string(win, offset_x + 60, offset_y + item_y + 15, "Mouse", COLOR_DARK_TEXT);
ui_draw_string(win, offset_x + 60, offset_y + item_y + 35, "Pointer settings", COLOR_DKGRAY);
}
static void control_panel_paint_wallpaper(ui_window_t win) {
int offset_x = 8;
int offset_y = 21;
ui_draw_rounded_rect_filled(win, offset_x, offset_y + 5, 80, 25, 6, COLOR_DARK_PANEL);
ui_draw_string(win, offset_x + 10, offset_y + 13, "< Back", COLOR_DARK_TEXT);
ui_draw_string(win, offset_x, offset_y + 40, "Presets:", COLOR_DARK_TEXT);
int button_y = offset_y + 65;
int button_x = offset_x;
// Colors
ui_draw_rounded_rect_filled(win, button_x, button_y, 91, 25, 6, COLOR_DARK_PANEL);
ui_draw_rect(win, button_x + 8, button_y + 6, 18, 13, COLOR_COFFEE);
ui_draw_string(win, button_x + 35, button_y + 8, "Coffee", COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, button_x + 100, button_y, 91, 25, 6, COLOR_DARK_PANEL);
ui_draw_rect(win, button_x + 108, button_y + 6, 18, 13, COLOR_TEAL);
ui_draw_string(win, button_x + 135, button_y + 8, "Teal", COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, button_x + 200, button_y, 91, 25, 6, COLOR_DARK_PANEL);
ui_draw_rect(win, button_x + 208, button_y + 6, 18, 13, COLOR_GREEN);
ui_draw_string(win, button_x + 235, button_y + 8, "Green", COLOR_DARK_TEXT);
button_y += 35;
ui_draw_rounded_rect_filled(win, button_x, button_y, 91, 25, 6, COLOR_DARK_PANEL);
ui_draw_rect(win, button_x + 8, button_y + 6, 18, 13, COLOR_BLUE_BG);
ui_draw_string(win, button_x + 35, button_y + 8, "Blue", COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, button_x + 100, button_y, 91, 25, 6, COLOR_DARK_PANEL);
ui_draw_rect(win, button_x + 108, button_y + 6, 18, 13, COLOR_PURPLE);
ui_draw_string(win, button_x + 132, button_y + 8, "Purple", COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, button_x + 200, button_y, 91, 25, 6, COLOR_DARK_PANEL);
ui_draw_rect(win, button_x + 208, button_y + 6, 18, 13, COLOR_GREY);
ui_draw_string(win, button_x + 235, button_y + 8, "Grey", COLOR_DARK_TEXT);
// Patterns
button_y += 40;
ui_draw_string(win, offset_x, button_y, "Patterns:", COLOR_DARK_TEXT);
button_y += 20;
ui_draw_rounded_rect_filled(win, button_x, button_y, 132, 25, 6, COLOR_DARK_PANEL);
for (int py = 0; py < 10; py++) {
for (int px = 0; px < 12; px++) {
int cell_x = px % 3;
int cell_y = py % 3;
uint32_t color = (cell_x == 1 && cell_y == 1) ? 0xFF000000 :
(cell_x == 1 || cell_y == 1) ? 0xFF404040 : 0xFFDC143C;
ui_draw_rect(win, button_x + 8 + px, button_y + 7 + py, 1, 1, color);
}
}
ui_draw_string(win, button_x + 28, button_y + 8, "Lumberjack", COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, button_x + 145, button_y, 132, 25, 6, COLOR_DARK_PANEL);
for (int py = 0; py < 8; py++) {
for (int px = 0; px < 10; px++) {
int cx = px - 5;
int cy = py - 4;
int abs_cx = cx < 0 ? -cx : cx;
int abs_cy = cy < 0 ? -cy : cy;
uint32_t color = (abs_cx + abs_cy <= 3) ? 0xFF0000CD : 0xFFADD8E6;
ui_draw_rect(win, button_x + 153 + px, button_y + 8 + py, 1, 1, color);
}
}
ui_draw_string(win, button_x + 165, button_y + 8, "Blue Diamond", COLOR_DARK_TEXT);
// Custom color
button_y += 40;
ui_draw_string(win, offset_x, button_y, "Custom color:", COLOR_DARK_TEXT);
button_y += 20;
ui_draw_string(win, button_x, button_y + 4, "R:", COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, button_x + 25, button_y, 50, 18, 4, COLOR_DARK_PANEL);
ui_draw_string(win, button_x + 30, button_y + 4, rgb_r, (focused_field == 0) ? 0xFFFF6B6B : COLOR_DARK_TEXT);
if (focused_field == 0) {
int cursor_x = button_x + 30 + input_cursor * 8;
ui_draw_rect(win, cursor_x, button_y + 4, 1, 9, 0xFFFF6B6B);
}
ui_draw_string(win, button_x + 90, button_y + 4, "G:", COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, button_x + 115, button_y, 50, 18, 4, COLOR_DARK_PANEL);
ui_draw_string(win, button_x + 120, button_y + 4, rgb_g, (focused_field == 1) ? 0xFF90EE90 : COLOR_DARK_TEXT);
if (focused_field == 1) {
int cursor_x = button_x + 120 + input_cursor * 8;
ui_draw_rect(win, cursor_x, button_y + 4, 1, 9, 0xFF90EE90);
}
ui_draw_string(win, button_x + 180, button_y + 4, "B:", COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, button_x + 205, button_y, 50, 18, 4, COLOR_DARK_PANEL);
ui_draw_string(win, button_x + 210, button_y + 4, rgb_b, (focused_field == 2) ? 0xFF87CEEB : COLOR_DARK_TEXT);
if (focused_field == 2) {
int cursor_x = button_x + 210 + input_cursor * 8;
ui_draw_rect(win, cursor_x, button_y + 4, 1, 9, 0xFF87CEEB);
}
ui_draw_rounded_rect_filled(win, button_x, button_y + 25, 70, 25, 6, COLOR_DARK_PANEL);
ui_draw_string(win, button_x + 18, button_y + 33, "Apply", COLOR_DARK_TEXT);
// Wallpapers section
button_y += 60;
ui_draw_string(win, offset_x, button_y, "Wallpapers:", COLOR_DARK_TEXT);
button_y += 20;
ui_draw_rounded_rect_filled(win, button_x, button_y, WALLPAPER_THUMB_W + 8, WALLPAPER_THUMB_H + 24, 6, COLOR_DARK_PANEL);
if (moon_thumb_valid) {
for (int ty = 0; ty < WALLPAPER_THUMB_H; ty++) {
for (int tx = 0; tx < WALLPAPER_THUMB_W; tx++) {
ui_draw_rect(win, button_x + 4 + tx, button_y + 4 + ty, 1, 1, moon_thumb[ty * WALLPAPER_THUMB_W + tx]);
}
}
} else {
ui_draw_string(win, button_x + 20, button_y + 30, "Error", 0xFFFF4444);
}
ui_draw_string(win, button_x + 30, button_y + WALLPAPER_THUMB_H + 8, "Moon", COLOR_DARK_TEXT);
int thumb2_x = button_x + WALLPAPER_THUMB_W + 20;
ui_draw_rounded_rect_filled(win, thumb2_x, button_y, WALLPAPER_THUMB_W + 8, WALLPAPER_THUMB_H + 24, 6, COLOR_DARK_PANEL);
if (mtn_thumb_valid) {
for (int ty = 0; ty < WALLPAPER_THUMB_H; ty++) {
for (int tx = 0; tx < WALLPAPER_THUMB_W; tx++) {
ui_draw_rect(win, thumb2_x + 4 + tx, button_y + 4 + ty, 1, 1, mtn_thumb[ty * WALLPAPER_THUMB_W + tx]);
}
}
} else {
ui_draw_string(win, thumb2_x + 20, button_y + 30, "Error", 0xFFFF4444);
}
ui_draw_string(win, thumb2_x + 16, button_y + WALLPAPER_THUMB_H + 8, "Mountain", COLOR_DARK_TEXT);
}
static void control_panel_paint_network(ui_window_t win) {
int offset_x = 8;
int offset_y = 21;
ui_draw_rounded_rect_filled(win, offset_x, offset_y + 5, 80, 25, 6, COLOR_DARK_PANEL);
ui_draw_string(win, offset_x + 10, offset_y + 13, "< Back", COLOR_DARK_TEXT);
ui_draw_string(win, offset_x, offset_y + 40, "Network:", COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, offset_x, offset_y + 55, 140, 25, 6, COLOR_DARK_PANEL);
ui_draw_string(win, offset_x + 30, offset_y + 63, "Init Network", COLOR_DARK_TEXT);
if (net_status[0] != '\0') {
ui_draw_string(win, offset_x + 150, offset_y + 63, net_status, 0xFF90EE90);
}
}
static void control_panel_paint_desktop(ui_window_t win) {
int offset_x = 8;
int offset_y = 21;
ui_draw_rounded_rect_filled(win, offset_x, offset_y + 5, 80, 25, 6, COLOR_DARK_PANEL);
ui_draw_string(win, offset_x + 10, offset_y + 13, "< Back", COLOR_DARK_TEXT);
ui_draw_string(win, offset_x, offset_y + 40, "Desktop Settings:", COLOR_DARK_TEXT);
int section_y = offset_y + 65;
ui_draw_rounded_rect_filled(win, offset_x, section_y, 16, 16, 3, COLOR_DARK_PANEL);
if (desktop_snap_to_grid) ui_draw_string(win, offset_x + 3, section_y + 1, "v", 0xFF90EE90);
ui_draw_string(win, offset_x + 25, section_y + 3, "Snap to Grid", COLOR_DARK_TEXT);
section_y += 25;
ui_draw_rounded_rect_filled(win, offset_x, section_y, 16, 16, 3, COLOR_DARK_PANEL);
if (desktop_auto_align) ui_draw_string(win, offset_x + 3, section_y + 1, "v", 0xFF90EE90);
ui_draw_string(win, offset_x + 25, section_y + 3, "Auto Align Icons", COLOR_DARK_TEXT);
section_y += 30;
ui_draw_string(win, offset_x, section_y + 3, "Apps per column:", COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, offset_x + 130, section_y, 20, 20, 4, COLOR_DARK_PANEL);
ui_draw_string(win, offset_x + 135, section_y + 4, "-", COLOR_DARK_TEXT);
char num[4];
cli_itoa(desktop_max_rows_per_col, num);
ui_draw_string(win, offset_x + 160, section_y + 5, num, COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, offset_x + 180, section_y, 20, 20, 4, COLOR_DARK_PANEL);
ui_draw_string(win, offset_x + 186, section_y + 4, "+", COLOR_DARK_TEXT);
section_y += 30;
ui_draw_string(win, offset_x, section_y + 3, "Columns:", COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, offset_x + 130, section_y, 20, 20, 4, COLOR_DARK_PANEL);
ui_draw_string(win, offset_x + 135, section_y + 4, "-", COLOR_DARK_TEXT);
char num_c[4];
cli_itoa(desktop_max_cols, num_c);
ui_draw_string(win, offset_x + 160, section_y + 5, num_c, COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, offset_x + 180, section_y, 20, 20, 4, COLOR_DARK_PANEL);
ui_draw_string(win, offset_x + 186, section_y + 4, "+", COLOR_DARK_TEXT);
}
static void control_panel_paint_mouse(ui_window_t win) {
int offset_x = 8;
int offset_y = 21;
ui_draw_rounded_rect_filled(win, offset_x, offset_y + 5, 80, 25, 6, COLOR_DARK_PANEL);
ui_draw_string(win, offset_x + 10, offset_y + 13, "< Back", COLOR_DARK_TEXT);
ui_draw_string(win, offset_x, offset_y + 40, "Mouse Settings:", COLOR_DARK_TEXT);
int section_y = offset_y + 65;
ui_draw_string(win, offset_x, section_y, "Speed:", COLOR_DARK_TEXT);
ui_draw_rounded_rect_filled(win, offset_x + 60, section_y + 8, 200, 8, 4, COLOR_DARK_PANEL);
int knob_x = offset_x + 60 + (mouse_speed - 1) * 190 / 49;
ui_draw_rounded_rect_filled(win, knob_x, section_y + 2, 10, 14, 3, 0xFF4A90E2);
ui_draw_string(win, offset_x + 270, section_y + 4, "x", COLOR_DARK_TEXT);
char speed_str[4];
cli_itoa(mouse_speed, speed_str);
ui_draw_string(win, offset_x + 280, section_y + 4, speed_str, COLOR_DARK_TEXT);
}
static void control_panel_paint(ui_window_t win) {
// Fill background
ui_draw_rect(win, 0, 20, 350, 480, COLOR_DARK_BG);
if (current_view == VIEW_MAIN) {
control_panel_paint_main(win);
} else if (current_view == VIEW_WALLPAPER) {
control_panel_paint_wallpaper(win);
} else if (current_view == VIEW_NETWORK) {
control_panel_paint_network(win);
} else if (current_view == VIEW_DESKTOP) {
control_panel_paint_desktop(win);
} else if (current_view == VIEW_MOUSE) {
control_panel_paint_mouse(win);
}
}
static void save_desktop_config(void) {
sys_system(4 /*SET_DESKTOP_PROP*/, 1, desktop_snap_to_grid, 0, 0);
sys_system(4, 2, desktop_auto_align, 0, 0);
sys_system(4, 3, desktop_max_rows_per_col, 0, 0);
sys_system(4, 4, desktop_max_cols, 0, 0);
}
static void save_mouse_config(void) {
sys_system(5 /*SET_MOUSE_SPEED*/, mouse_speed, 0, 0, 0);
}
static void fetch_kernel_state(void) {
desktop_snap_to_grid = sys_system(7 /*GET_DESKTOP_PROP*/, 1, 0, 0, 0);
desktop_auto_align = sys_system(7, 2, 0, 0, 0);
desktop_max_rows_per_col = sys_system(7, 3, 0, 0, 0);
desktop_max_cols = sys_system(7, 4, 0, 0, 0);
mouse_speed = sys_system(8 /*GET_MOUSE_SPEED*/, 0, 0, 0, 0);
if (sys_system(9, 0, (uint64_t)moon_thumb, 0, 0) == 0) {
moon_thumb_valid = 1;
}
if (sys_system(9, 1, (uint64_t)mtn_thumb, 0, 0) == 0) {
mtn_thumb_valid = 1;
}
}
static void control_panel_handle_click(int x, int y) {
int win_w = 350;
if (current_view == VIEW_MAIN) {
int offset_x = 8;
int offset_y = 21;
int item_h = 60;
int item_spacing = 10;
int item_y = offset_y + 15;
if (x >= offset_x && x < win_w - 8 && y >= item_y && y < item_y + item_h) {
current_view = VIEW_WALLPAPER;
focused_field = -1;
}
item_y += item_h + item_spacing;
if (x >= offset_x && x < win_w - 8 && y >= item_y && y < item_y + item_h) {
current_view = VIEW_NETWORK;
focused_field = -1;
}
item_y += item_h + item_spacing;
if (x >= offset_x && x < win_w - 8 && y >= item_y && y < item_y + item_h) {
current_view = VIEW_DESKTOP;
}
item_y += item_h + item_spacing;
if (x >= offset_x && x < win_w - 8 && y >= item_y && y < item_y + item_h) {
current_view = VIEW_MOUSE;
}
} else if (current_view == VIEW_WALLPAPER) {
int offset_x = 8;
int offset_y = 21;
int button_y = offset_y + 65;
int button_x = offset_x;
if (x >= offset_x && x < offset_x + 80 && y >= offset_y + 5 && y < offset_y + 30) {
current_view = VIEW_MAIN;
return;
}
if (x >= button_x && x < button_x + 91 && y >= button_y && y < button_y + 25) {
sys_system(1, COLOR_COFFEE, 0, 0, 0);
return;
}
if (x >= button_x + 100 && x < button_x + 191 && y >= button_y && y < button_y + 25) {
sys_system(1, COLOR_TEAL, 0, 0, 0);
return;
}
if (x >= button_x + 200 && x < button_x + 291 && y >= button_y && y < button_y + 25) {
sys_system(1, COLOR_GREEN, 0, 0, 0);
return;
}
button_y += 35;
if (x >= button_x && x < button_x + 91 && y >= button_y && y < button_y + 25) {
sys_system(1, COLOR_BLUE_BG, 0, 0, 0);
return;
}
if (x >= button_x + 100 && x < button_x + 191 && y >= button_y && y < button_y + 25) {
sys_system(1, COLOR_PURPLE, 0, 0, 0);
return;
}
if (x >= button_x + 200 && x < button_x + 291 && y >= button_y && y < button_y + 25) {
sys_system(1, COLOR_GREY, 0, 0, 0);
return;
}
button_y += 60; // 40 + 20
if (x >= button_x && x < button_x + 132 && y >= button_y && y < button_y + 25) {
sys_system(2, (uint64_t)pattern_lumberjack, 0, 0, 0);
return;
}
if (x >= button_x + 145 && x < button_x + 277 && y >= button_y && y < button_y + 25) {
sys_system(2, (uint64_t)pattern_blue_diamond, 0, 0, 0);
return;
}
button_y += 60;
if (x >= button_x + 25 && x < button_x + 75 && y >= button_y && y < button_y + 18) {
if (focused_field != 0) rgb_r[0] = 0;
focused_field = 0; input_cursor = 0; return;
}
if (x >= button_x + 115 && x < button_x + 165 && y >= button_y && y < button_y + 18) {
if (focused_field != 1) rgb_g[0] = 0;
focused_field = 1; input_cursor = 0; return;
}
if (x >= button_x + 205 && x < button_x + 255 && y >= button_y && y < button_y + 18) {
if (focused_field != 2) rgb_b[0] = 0;
focused_field = 2; input_cursor = 0; return;
}
if (x >= button_x && x < button_x + 70 && y >= button_y + 25 && y < button_y + 50) {
uint32_t cust = parse_rgb_separate(rgb_r, rgb_g, rgb_b);
sys_system(1, cust, 0, 0, 0);
return;
}
button_y += 80;
if (x >= button_x && x < button_x + WALLPAPER_THUMB_W + 8 && y >= button_y && y < button_y + WALLPAPER_THUMB_H + 24) {
sys_system(3, 0, 0, 0, 0);
return;
}
int thumb2_x = button_x + WALLPAPER_THUMB_W + 20;
if (x >= thumb2_x && x < thumb2_x + WALLPAPER_THUMB_W + 8 && y >= button_y && y < button_y + WALLPAPER_THUMB_H + 24) {
sys_system(3, 1, 0, 0, 0);
return;
}
} else if (current_view == VIEW_NETWORK) {
int offset_x = 8;
int offset_y = 21;
if (x >= offset_x && x < offset_x + 80 && y >= offset_y + 5 && y < offset_y + 30) {
current_view = VIEW_MAIN;
focused_field = -1;
return;
}
if (x >= offset_x && x < offset_x + 140 && y >= offset_y + 55 && y < offset_y + 80) {
if (sys_system(6, 0, 0, 0, 0) == 0) {
net_status[0] = 'I'; net_status[1] = 'n'; net_status[2] = 'i';
net_status[3] = 't'; net_status[4] = 'e'; net_status[5] = 'd'; net_status[6] = 0;
} else {
net_status[0] = 'F'; net_status[1] = 'a'; net_status[2] = 'i';
net_status[3] = 'l'; net_status[4] = 'e'; net_status[5] = 'd'; net_status[6] = 0;
}
}
} else if (current_view == VIEW_DESKTOP) {
int offset_x = 8;
int offset_y = 21;
if (x >= offset_x && x < offset_x + 80 && y >= offset_y + 5 && y < offset_y + 30) {
current_view = VIEW_MAIN;
return;
}
int section_y = offset_y + 65;
if (x >= offset_x && x < offset_x + 16 && y >= section_y && y < section_y + 16) {
desktop_snap_to_grid = !desktop_snap_to_grid;
if (!desktop_snap_to_grid) desktop_auto_align = 0;
save_desktop_config();
return;
}
section_y += 25;
if (x >= offset_x && x < offset_x + 16 && y >= section_y && y < section_y + 16) {
desktop_auto_align = !desktop_auto_align;
if (desktop_auto_align) desktop_snap_to_grid = 1;
save_desktop_config();
return;
}
section_y += 25;
if (x >= offset_x + 130 && x < offset_x + 150 && y >= section_y && y < section_y + 20) {
if (desktop_max_rows_per_col > 1) {
desktop_max_rows_per_col--;
save_desktop_config();
}
}
if (x >= offset_x + 180 && x < offset_x + 200 && y >= section_y && y < section_y + 20) {
if (desktop_max_rows_per_col < 15) desktop_max_rows_per_col++;
save_desktop_config();
}
section_y += 25;
if (x >= offset_x + 130 && x < offset_x + 150 && y >= section_y && y < section_y + 20) {
if (desktop_max_cols > 1) {
desktop_max_cols--;
save_desktop_config();
}
}
if (x >= offset_x + 180 && x < offset_x + 200 && y >= section_y && y < section_y + 20) {
desktop_max_cols++;
save_desktop_config();
}
} else if (current_view == VIEW_MOUSE) {
int offset_x = 8;
int offset_y = 21;
if (x >= offset_x && x < offset_x + 80 && y >= offset_y + 5 && y < offset_y + 30) {
current_view = VIEW_MAIN;
return;
}
int section_y = offset_y + 65;
if (x >= offset_x + 60 && x <= offset_x + 260 && y >= section_y && y <= section_y + 20) {
int new_speed = 1 + (x - (offset_x + 60)) * 49 / 200;
if (new_speed < 1) new_speed = 1;
if (new_speed > 50) new_speed = 50;
mouse_speed = new_speed;
save_mouse_config();
return;
}
}
}
static void control_panel_handle_key(char c) {
if (focused_field < 0) return;
if (current_view == VIEW_WALLPAPER) {
char *focused_buffer = NULL;
int max_len = 3;
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') {
if (input_cursor > 0) {
input_cursor--;
focused_buffer[input_cursor] = '\0';
}
} else if (c >= '0' && c <= '9') {
if (input_cursor < max_len) {
focused_buffer[input_cursor] = c;
input_cursor++;
focused_buffer[input_cursor] = '\0';
}
} else if (c == '\t') {
focused_field = (focused_field + 1) % 3;
input_cursor = 0;
}
}
}
int main(int argc, char **argv) {
(void)argc;
(void)argv;
ui_window_t win = ui_window_create("Settings", 200, 150, 350, 500);
if (!win) return 1;
generate_lumberjack_pattern();
generate_blue_diamond_pattern();
fetch_kernel_state();
gui_event_t ev;
while (1) {
if (ui_get_event(win, &ev)) {
if (ev.type == GUI_EVENT_PAINT) {
control_panel_paint(win);
ui_mark_dirty(win, 0, 0, 350, 500);
} else if (ev.type == GUI_EVENT_CLICK) {
control_panel_handle_click(ev.arg1, ev.arg2);
control_panel_paint(win);
ui_mark_dirty(win, 0, 0, 350, 500);
} else if (ev.type == GUI_EVENT_KEY) {
control_panel_handle_key((char)ev.arg1);
control_panel_paint(win);
ui_mark_dirty(win, 0, 0, 350, 500);
} else if (ev.type == GUI_EVENT_CLOSE) {
sys_exit(0);
}
}
}
return 0;
}

BIN
src/kernel/userland/settings.elf Executable file

Binary file not shown.

View File

@@ -0,0 +1,431 @@
#include "libc/syscall.h"
#include "libc/libui.h"
#include <stddef.h>
#define COLOR_DARK_PANEL 0xFF202020
#define COLOR_DARK_TEXT 0xFFE0E0E0
#define COLOR_DARK_BORDER 0xFF404040
#define COLOR_RED 0xFFFF4444
#define COLOR_DARK_BG 0xFF121212
#define COLOR_DKGRAY 0xFF808080
#define COLOR_WHITE 0xFFFFFFFF
#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 = 0;
static int win_w = 700;
static int win_h = 450;
static void editor_strcpy(char *dest, const char *src) {
while (*src) *dest++ = *src++;
*dest = 0;
}
static void editor_clear_all(void) {
for (int i = 0; i < EDITOR_MAX_LINES; i++) {
for (int j = 0; j < EDITOR_MAX_LINE_LEN; j++) {
lines[i].content[j] = 0;
}
lines[i].length = 0;
}
line_count = 1;
cursor_line = 0;
cursor_col = 0;
scroll_top = 0;
open_filename[0] = 0;
file_modified = 0;
}
static void editor_ensure_cursor_visible(void) {
int visible_lines = 22;
if (cursor_line < scroll_top) {
scroll_top = cursor_line;
}
if (cursor_line >= scroll_top + visible_lines) {
scroll_top = cursor_line - visible_lines + 1;
}
}
void editor_open_file(const char *filename) {
editor_clear_all();
editor_strcpy(open_filename, filename);
int fd = sys_open(filename, "r");
if (fd < 0) {
file_modified = 0;
return;
}
static char buffer[16384];
int bytes_read = sys_read(fd, buffer, sizeof(buffer));
sys_close(fd);
if (bytes_read <= 0) {
file_modified = 0;
return;
}
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 = 0;
}
static void editor_save_file(void) {
if (!open_filename[0]) return;
int fd = sys_open(open_filename, "w");
if (fd < 0) return;
for (int i = 0; i < line_count; i++) {
sys_write_fs(fd, lines[i].content, lines[i].length);
sys_write_fs(fd, "\n", 1);
}
sys_close(fd);
file_modified = 0;
}
static void editor_insert_char(char ch) {
if (cursor_line >= EDITOR_MAX_LINES) return;
EditorLine *line = &lines[cursor_line];
if (ch == '\n') {
if (line_count >= EDITOR_MAX_LINES) return;
for (int j = line_count; j > cursor_line; j--) {
lines[j] = lines[j - 1];
}
line_count++;
for (int k = 0; k < EDITOR_MAX_LINE_LEN; k++) {
lines[cursor_line + 1].content[k] = 0;
}
lines[cursor_line + 1].length = 0;
int current_len = lines[cursor_line].length;
int new_len = current_len - cursor_col;
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;
lines[cursor_line].content[cursor_col] = 0;
lines[cursor_line].length = cursor_col;
cursor_line++;
cursor_col = 0;
} else if (ch == '\b') {
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) {
EditorLine *prev = &lines[cursor_line - 1];
int merge_point = prev->length;
int i = 0;
while (i < line->length && (merge_point + i) < EDITOR_MAX_LINE_LEN - 1) {
prev->content[merge_point + i] = line->content[i];
i++;
}
prev->content[merge_point + i] = 0;
prev->length = merge_point + i;
for (int j = cursor_line; j < line_count - 1; j++) {
lines[j] = lines[j + 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) {
if (cursor_col < EDITOR_MAX_LINE_LEN - 1) {
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 = 1;
editor_ensure_cursor_visible();
}
static void editor_paint(ui_window_t win) {
int offset_x = 4;
int offset_y = 24;
int content_width = win_w - 8;
int content_height = win_h - 28;
// Top content bar
ui_draw_rounded_rect_filled(win, offset_x, offset_y, content_width, 25, 6, COLOR_DARK_PANEL);
ui_draw_string(win, offset_x + 10, offset_y + 6, "File", COLOR_DARK_TEXT);
ui_draw_string(win, offset_x + 55, offset_y + 6, open_filename, COLOR_DARK_TEXT);
// Save button
int save_btn_x = offset_x + content_width - 80;
int save_btn_y = offset_y + 3;
ui_draw_rounded_rect_filled(win, save_btn_x, save_btn_y, 70, 20, 6, COLOR_DARK_BORDER);
ui_draw_string(win, save_btn_x + 20, save_btn_y + 6, "Save", COLOR_DARK_TEXT);
if (file_modified) {
ui_draw_string(win, offset_x + content_width - 200, offset_y + 5, "[Modified]", COLOR_RED);
}
// Editor background
ui_draw_rect(win, 4, 54, win_w - 8, win_h - 58, COLOR_DARK_BG);
int text_start_x = offset_x + 40;
int available_width = content_width - 40;
int max_chars_per_line = available_width / EDITOR_CHAR_WIDTH;
if (max_chars_per_line < 1) max_chars_per_line = 1;
int display_line = 0;
int visible_lines = (content_height - 55) / EDITOR_LINE_HEIGHT;
int max_display_lines = visible_lines;
int line_idx = scroll_top;
while (line_idx < line_count && display_line < max_display_lines) {
int display_y = offset_y + 35 + display_line * EDITOR_LINE_HEIGHT;
if (display_line == 0 || line_idx < line_count) {
char line_num_str[16];
int temp = line_idx + 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;
}
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;
ui_draw_string(win, offset_x + 4, display_y, line_num_str, COLOR_DKGRAY);
}
const char *text = lines[line_idx].content;
int text_len = lines[line_idx].length;
int char_idx = 0;
int local_display_line = 0;
_Bool first_pass = 1;
while ((char_idx < text_len || (text_len == 0 && first_pass)) && display_line < max_display_lines) {
first_pass = 0;
int current_display_y = offset_y + 35 + display_line * EDITOR_LINE_HEIGHT;
char segment[256];
int segment_len = 0;
int segment_start = char_idx;
while (char_idx < text_len && segment_len < max_chars_per_line) {
segment[segment_len++] = text[char_idx++];
}
segment[segment_len] = 0;
if (char_idx < text_len && segment_len > 0) {
int last_space = -1;
for (int i = segment_len - 1; i >= 0; i--) {
if (segment[i] == ' ') {
last_space = i;
break;
}
}
if (last_space > 0) {
segment_len = last_space;
segment[segment_len] = 0;
char_idx = segment_start + last_space + 1;
while (char_idx < text_len && text[char_idx] == ' ') {
char_idx++;
}
}
}
if (segment_len > 0) {
ui_draw_string(win, text_start_x, current_display_y, segment, COLOR_DARK_TEXT);
}
if (line_idx == cursor_line) {
int segment_end = segment_start + segment_len;
_Bool draw_cursor = 0;
if (cursor_col >= segment_start && cursor_col < segment_end) {
draw_cursor = 1;
} else if (cursor_col == text_len && segment_end == text_len) {
draw_cursor = 1;
}
if (draw_cursor) {
int cursor_x = text_start_x + ((cursor_col - segment_start) * EDITOR_CHAR_WIDTH);
ui_draw_rect(win, cursor_x, current_display_y, 2, 10, COLOR_WHITE);
}
}
display_line++;
local_display_line++;
if (char_idx >= text_len) break;
}
line_idx++;
}
int status_y = offset_y + content_height - 20;
ui_draw_rounded_rect_filled(win, offset_x, status_y, content_width, 20, 6, COLOR_DARK_PANEL);
ui_draw_string(win, offset_x + 10, status_y + 5, "Line: ", COLOR_DARK_TEXT);
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;
ui_draw_string(win, offset_x + 60, status_y + 5, line_str, COLOR_DARK_TEXT);
ui_draw_string(win, offset_x + 100, status_y + 5, " Col: ", COLOR_DARK_TEXT);
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;
ui_draw_string(win, offset_x + 170, status_y + 5, col_str, COLOR_DARK_TEXT);
}
static void editor_handle_key(char c) {
if (c == 17) { // UP
if (cursor_line > 0) {
cursor_line--;
if (cursor_col > lines[cursor_line].length) cursor_col = lines[cursor_line].length;
if (cursor_line < scroll_top) scroll_top = cursor_line;
}
} else if (c == 18) { // DOWN
if (cursor_line < line_count - 1) {
cursor_line++;
if (cursor_col > 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;
}
} else if (c == 19) { // LEFT
if (cursor_col > 0) {
cursor_col--;
} else if (cursor_line > 0) {
cursor_line--;
cursor_col = lines[cursor_line].length;
}
} else if (c == 20) { // RIGHT
if (cursor_col < lines[cursor_line].length) {
cursor_col++;
} else if (cursor_line < line_count - 1) {
cursor_line++;
cursor_col = 0;
}
} else {
editor_insert_char(c);
}
}
static void editor_handle_click(int x, int y) {
int content_width = win_w - 8;
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();
}
}
int main(int argc, char **argv) {
ui_window_t win = ui_window_create("Text Editor", 100, 150, win_w, win_h);
if (!win) return 1;
editor_clear_all();
if (argc > 1) {
editor_open_file(argv[1]);
} else {
editor_strcpy(open_filename, "untitled.txt");
}
gui_event_t ev;
while (1) {
if (ui_get_event(win, &ev)) {
if (ev.type == GUI_EVENT_PAINT) {
editor_paint(win);
ui_mark_dirty(win, 0, 0, win_w, win_h);
} else if (ev.type == GUI_EVENT_CLICK) {
editor_handle_click(ev.arg1, ev.arg2);
editor_paint(win);
ui_mark_dirty(win, 0, 0, win_w, win_h);
} else if (ev.type == GUI_EVENT_KEY) {
editor_handle_key((char)ev.arg1);
editor_paint(win);
ui_mark_dirty(win, 0, 0, win_w, win_h);
} else if (ev.type == GUI_EVENT_CLOSE) {
sys_exit(0);
}
}
}
return 0;
}

BIN
src/kernel/userland/txtedit.elf Executable file

Binary file not shown.

View File

@@ -0,0 +1,226 @@
#include "nanojpeg.h"
#include "libc/syscall.h"
#include "libc/libui.h"
#include "libc/stdlib.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define VIEWER_MAX_W 800
#define VIEWER_MAX_H 600
static uint32_t *viewer_pixels = NULL;
static int viewer_img_w = 0;
static int viewer_img_h = 0;
static char viewer_title[64] = "Viewer";
static bool viewer_has_image = false;
static char viewer_file_path[256];
static int win_w = 500;
static int win_h = 400;
static void viewer_strcpy(char *dst, const char *src) {
while (*src) *dst++ = *src++;
*dst = 0;
}
static int viewer_strlen(const char *s) {
int len = 0;
while (s[len]) len++;
return len;
}
static void viewer_scale_rgb_to_argb(const unsigned char *rgb, int src_w, int src_h,
uint32_t *dst, int dst_w, int dst_h) {
for (int y = 0; y < dst_h; y++) {
int src_y = y * src_h / dst_h;
if (src_y >= src_h) src_y = src_h - 1;
for (int x = 0; x < dst_w; x++) {
int src_x = x * src_w / dst_w;
if (src_x >= src_w) src_x = src_w - 1;
int idx = (src_y * src_w + src_x) * 3;
unsigned char r = rgb[idx];
unsigned char g = rgb[idx + 1];
unsigned char b = rgb[idx + 2];
dst[y * dst_w + x] = 0xFF000000 | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
}
}
}
static void viewer_paint(ui_window_t win) {
int cx = 4;
int cy = 24;
int cw = win_w - 8;
int ch = win_h - 28;
ui_draw_rect(win, cx, cy, cw, ch, 0xFF1A1A1A);
if (!viewer_has_image) {
ui_draw_string(win, cx + 20, cy + ch / 2, "No image loaded", 0xFF888888);
return;
}
int disp_w = viewer_img_w;
int disp_h = viewer_img_h;
if (disp_w > cw - 8) {
disp_h = disp_h * (cw - 8) / disp_w;
disp_w = cw - 8;
}
if (disp_h > ch - 40) {
disp_w = disp_w * (ch - 40) / disp_h;
disp_h = ch - 40;
}
int ox = cx + (cw - disp_w) / 2;
int oy = cy + (ch - disp_h - 30) / 2;
for (int y = 0; y < disp_h; y++) {
int src_y = y * viewer_img_h / disp_h;
if (src_y >= viewer_img_h) src_y = viewer_img_h - 1;
for (int x = 0; x < disp_w; x++) {
int src_x = x * viewer_img_w / disp_w;
if (src_x >= viewer_img_w) src_x = viewer_img_w - 1;
uint32_t pixel = viewer_pixels[src_y * viewer_img_w + src_x];
ui_draw_rect(win, ox + x, oy + y, 1, 1, pixel);
}
}
int btn_w = 160;
int btn_h = 22;
int btn_x = cx + (cw - btn_w) / 2;
int btn_y = win_h - 30;
ui_draw_rounded_rect_filled(win, btn_x, btn_y, btn_w, btn_h, 6, 0xFF2D2D2D);
ui_draw_string(win, btn_x + 10, btn_y + 6, "Set as Wallpaper", 0xFFF0F0F0);
}
static void viewer_handle_click(ui_window_t win, int x, int y) {
if (!viewer_has_image) return;
int cx = 4;
int cw = win_w - 8;
int btn_w = 160;
int btn_x = cx + (cw - btn_w) / 2;
int btn_y = win_h - 30;
if (x >= btn_x && x < btn_x + btn_w && y >= btn_y && y < btn_y + 22) {
// SYSTEM_CMD_SET_WALLPAPER is 3 based on syscall.c code
sys_system(3, (uint64_t)viewer_file_path, 0, 0, 0);
}
}
void viewer_open_file(const char *path) {
int fd = sys_open(path, "r");
if (fd < 0) return;
// We can't use stat yet, so read chunks until EOF
// Alternatively, use a large buffer if sys_read handles large chunks.
int alloc_size = 2 * 1024 * 1024;
unsigned char *buf = malloc(alloc_size);
if (!buf) {
sys_close(fd);
return;
}
int total_read = 0;
while (total_read < alloc_size) {
int chunk = sys_read(fd, (char*)buf + total_read, alloc_size - total_read);
if (chunk <= 0) break;
total_read += chunk;
}
sys_close(fd);
if (total_read <= 0) {
free(buf);
return;
}
njInit();
nj_result_t result = njDecode(buf, total_read);
if (result != NJ_OK) {
njDone();
free(buf);
return;
}
int img_w = njGetWidth();
int img_h = njGetHeight();
unsigned char *rgb = njGetImage();
if (!rgb || img_w <= 0 || img_h <= 0) {
njDone();
free(buf);
return;
}
int fit_w = img_w;
int fit_h = img_h;
if (fit_w > VIEWER_MAX_W) {
fit_h = fit_h * VIEWER_MAX_W / fit_w;
fit_w = VIEWER_MAX_W;
}
if (fit_h > VIEWER_MAX_H) {
fit_w = fit_w * VIEWER_MAX_H / fit_h;
fit_h = VIEWER_MAX_H;
}
viewer_pixels = malloc(fit_w * fit_h * sizeof(uint32_t));
if (viewer_pixels) {
viewer_scale_rgb_to_argb(rgb, img_w, img_h, viewer_pixels, fit_w, fit_h);
viewer_img_w = fit_w;
viewer_img_h = fit_h;
viewer_has_image = true;
}
njDone();
free(buf);
viewer_strcpy(viewer_file_path, path);
const char *fname = path;
int plen = viewer_strlen(path);
for (int i = plen - 1; i >= 0; i--) {
if (path[i] == '/') {
fname = &path[i + 1];
break;
}
}
viewer_title[0] = 'V'; viewer_title[1] = 'i'; viewer_title[2] = 'e';
viewer_title[3] = 'w'; viewer_title[4] = 'e'; viewer_title[5] = 'r';
viewer_title[6] = ' '; viewer_title[7] = '-'; viewer_title[8] = ' ';
int ti = 9;
for (int i = 0; fname[i] && ti < 60; i++) {
viewer_title[ti++] = fname[i];
}
viewer_title[ti] = 0;
win_w = fit_w + 16;
if (win_w < 200) win_w = 200;
win_h = fit_h + 64;
if (win_h < 100) win_h = 100;
}
int main(int argc, char **argv) {
if (argc > 1) {
viewer_open_file(argv[1]);
}
ui_window_t win = ui_window_create(viewer_title, 100, 50, win_w, win_h);
if (!win) return 1;
gui_event_t ev;
while (1) {
if (ui_get_event(win, &ev)) {
if (ev.type == GUI_EVENT_PAINT) {
viewer_paint(win);
ui_mark_dirty(win, 0, 0, win_w, win_h);
} else if (ev.type == GUI_EVENT_CLICK) {
viewer_handle_click(win, ev.arg1, ev.arg2);
} else if (ev.type == GUI_EVENT_CLOSE) {
sys_exit(0);
}
}
}
return 0;
}

BIN
src/kernel/userland/viewer.elf Executable file

Binary file not shown.

Binary file not shown.