/* browser: a small HTML/CSS browser built on ngui, the image library and the * shared uhttp client. * * Inline JavaScript runs through the vendored MuJS engine (console.log, * alert, document.getElementById(...).textContent and location.href); forms * are not supported. External CSS is loaded over uhttp, and images are * decoded with img.c and drawn inline. Page-scoped buffers (page body, * rules and image bitmaps) come from the ring-3 libc heap; each image gets an * exact-size decode arena that is freed as soon as the scaled bitmap exists. * The DOM and the image/CSS I/O scratch use fixed static arenas. */ #include #include #include #include "ucmd_lib.h" #include "ngui.h" #include "browser_html.h" #include "js_browser.h" #include "uhttp.h" #include "img.h" #include "inflate.h" /* Start wide enough for the desktop nav; smaller screens fall back to * 960x640 and 780x540, which still fit a 1024x768 panel. */ #define BR_W 1100 #define BR_H 700 #define BR_TOOLBAR_H 30 #define BR_STATUS_H 18 #define BR_PAGE_MAX (2u * 1024u * 1024u) #define BR_IMG_FILE_MAX (512u * 1024u) #define BR_IMG_PIX_MAX (8u * 1024u * 1024u) #define BR_MAX_IMAGES 16 #define BR_IMG_ARENA_MAX (24u * 1024u * 1024u) #define BR_IMG_LAYOUT_W 1000 #define BR_CSS_MAX (832u * 1024u) #define BR_CSS_COMP_MAX (192u * 1024u) #define BR_SCRIPT_MAX (512u * 1024u) #define FETCH_ABORT (-2) /* Session cache for remote stylesheets (per launch; cleared at startup). */ #define CSS_CACHE_MAX 16 #define CSS_CACHE_FILE_MAX (256u * 1024u) #define CSS_CACHE_PREFIX "bcss-" #define SCRIPT_CACHE_MAX 16 #define SCRIPT_CACHE_FILE_MAX (512u * 1024u) #define SCRIPT_CACHE_PREFIX "bjs-" #define BR_MAX_SHEETS 6 #define BR_HISTORY 16 #define BR_URL_MAX 512 #define BR_STATUS_MAX 200 #define BR_IMG_RGB565(r, g, b) \ ((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3))) #define BR_GRAY 0x8410 enum { ID_URL = 1, ID_GO, ID_BACK, ID_FORWARD, ID_RELOAD, ID_HOME }; #define ACT_NAV (1u << 0) #define ACT_BACK (1u << 1) #define ACT_FORWARD (1u << 2) #define ACT_RELOAD (1u << 3) #define ACT_HOME (1u << 4) #define ACT_CLOSE (1u << 5) #define ACT_WAIT_NAV (ACT_NAV | ACT_BACK | ACT_FORWARD | ACT_RELOAD | \ ACT_HOME | ACT_CLOSE) struct br_image { int state; /* 0 empty, 1 ready */ struct img_image img; /* scaled bitmap at <= BR_IMG_LAYOUT_W */ int native_w, native_h; char err[32]; }; static struct ngui ui; static struct ngui_input url_in; static struct ngui_scroll page_scroll; static struct br_doc doc; static struct br_image images[BR_MAX_IMAGES]; static int image_count; /* Images are loaded one per main-loop iteration so the parsed page stays * interactive while a slow network/TLS fetch or 4K decode is in progress. */ static int preload_active; static int preload_pending; static int preload_wait; static unsigned int preload_node; static int preload_loaded; static int preload_scroll; static char post_status_text[BR_STATUS_MAX]; static int post_status_error; static unsigned int post_status_ttl; /* Content-relative position/height of each , recorded by the measure * pass so image loading can be deferred until it is near the viewport. */ static unsigned short img_top[BR_MAX_NODES]; static unsigned short img_h[BR_MAX_NODES]; /* One reusable I/O buffer: the two halves double as the gzip input scratch * for page/CSS fetches, where the heap has no room for another multi-megabyte * body. Decode arenas are allocated per image instead of living here. */ static unsigned char *image_io_buf; #define image_file_buf (image_io_buf) #define image_comp_buf (image_io_buf + BR_IMG_FILE_MAX) #define BR_PAGE_COMP_MAX (2u * BR_IMG_FILE_MAX) static struct inflate image_inflate_ws; #if BR_CSS_MAX + BR_CSS_COMP_MAX > 2u * BR_IMG_FILE_MAX #error "CSS scratch must fit in image_io_buf" #endif static char page_url[BR_URL_MAX]; static char page_title[BR_TITLE_MAX]; static char cwd[64]; static int page_is_local; static int page_loading; static char status_text[BR_STATUS_MAX]; static unsigned int status_until; static int status_error; static char history[BR_HISTORY][BR_URL_MAX]; static int hist_len, hist_pos; static char click_url[BR_URL_MAX]; static char pending_url[BR_URL_MAX]; static unsigned int pending_action; static int cancel_fetch; static int quit; static struct ngui_rect page_rect; static unsigned short *page_backbuf; static int pagebuf_w, pagebuf_h; static int page_cache_valid; static int page_cache_scroll; static unsigned int page_cache_nnode; static int page_cache_x, page_cache_y, page_cache_w, page_cache_h; static int layout_valid, layout_left, layout_right; static unsigned int layout_nnode; static int layout_content_h; /* layout output */ struct lay_run { unsigned short off, len; unsigned short gap; unsigned short color; unsigned short href; char lit[28]; short img; signed short dy; /* inline image vertical offset */ unsigned short iw, ih; unsigned short w; unsigned char font_px, bold, italic, mono, underline; unsigned char has_lit; }; struct lay { int draw; struct ngui *ui; int left, right; int y; int line_x, line_y, line_h; int run_x; int align; int depth; int space_pending; struct lay_run runs[96]; int nruns; }; struct hit { int x, y, w, h; unsigned short node; }; #define HIT_MAX 96 static struct hit hits[HIT_MAX]; static int nhits; static unsigned short block_h[BR_MAX_NODES]; static char li_marker[BR_MAX_NODES][6]; /* one connection object; the browser fetches one resource at a time */ /* ---- tiny strings ---- */ static unsigned int s_len(const char *s) { unsigned int n = 0; while (s && s[n]) n++; return n; } static void s_copy(char *d, const char *s, unsigned int cap) { unsigned int i = 0; if (!cap) return; while (s && s[i] && i + 1 < cap) { d[i] = s[i]; i++; } d[i] = '\0'; } static void s_append(char *d, unsigned int *n, const char *s, unsigned int cap) { while (s && *s && *n + 1 < cap) d[(*n)++] = *s++; d[*n] = '\0'; } static void s_append_dec(char *d, unsigned int *n, unsigned int v, unsigned int cap) { char tmp[12]; unsigned int i = 0, j; do { tmp[i++] = (char)('0' + v % 10); v /= 10; } while (v && i < sizeof(tmp)); for (j = 0; j < i && *n + 1 < cap; j++) d[(*n)++] = tmp[i - 1 - j]; d[*n] = '\0'; } static int s_eq(const char *a, const char *b) { while (*a && *b) { if (*a != *b) return 0; a++; b++; } return *a == *b; } static int starts_with(const char *s, const char *p) { unsigned int i = 0; while (p[i]) { if (s[i] != p[i]) return 0; i++; } return 1; } static int contains_ci(const char *s, const char *p) { unsigned int i, j; for (i = 0; s[i]; i++) { for (j = 0; p[j]; j++) { char a = s[i + j]; if (a >= 'A' && a <= 'Z') a = (char)(a - 'A' + 'a'); if (a != p[j]) break; } if (!p[j]) return 1; } return 0; } static char *trim(char *s) { unsigned int n; while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n') s++; n = s_len(s); while (n && (s[n - 1] == ' ' || s[n - 1] == '\t' || s[n - 1] == '\r' || s[n - 1] == '\n')) s[--n] = '\0'; return s; } /* ---- per-process heap ---- */ static int heap_start(void) { image_io_buf = (unsigned char *)malloc(2u * BR_IMG_FILE_MAX); if (!image_io_buf) return -1; page_backbuf = (unsigned short *)malloc( (unsigned int)ui.w * (unsigned int)ui.h * 2u); pagebuf_w = page_backbuf ? ui.w : 0; pagebuf_h = page_backbuf ? ui.h : 0; page_cache_valid = 0; return 0; } static void heap_clear(void) { ngui_font_detach(&ui); heap_reset(); image_io_buf = 0; page_backbuf = 0; pagebuf_w = pagebuf_h = 0; page_cache_valid = 0; } static void browser_rules_alloc(void) { struct br_rule *rules; rules = (struct br_rule *)malloc( (unsigned int)(BR_MAX_RULES * sizeof(struct br_rule))); br_set_rule_store(&doc, rules, rules ? BR_MAX_RULES : 0); } /* ---- status ---- */ static void set_status(const char *s, int err, unsigned int ttl_ms) { s_copy(status_text, s, sizeof(status_text)); status_error = err; status_until = ttl_ms ? u_ticks_ms() + ttl_ms : 0; ngui_invalidate(&ui); } /* ---- JavaScript ---- */ static int resolve_link(const char *base, const char *href, char *out, unsigned int cap); static char js_last_status[BR_STATUS_MAX]; static int js_last_error; static void js_host_status(const char *msg, int error) { s_copy(js_last_status, msg, sizeof(js_last_status)); js_last_error = error; set_status(msg, error, 6000); } static void js_host_report(const char *msg) { u_puts("[js] "); u_puts(msg); u_putc('\n'); s_copy(js_last_status, msg, sizeof(js_last_status)); js_last_error = 1; set_status(msg, 1, 8000); } static void js_host_navigate(const char *url) { char resolved[BR_URL_MAX]; if (!url || !url[0]) return; if (resolve_link(page_url, url, resolved, sizeof(resolved)) != 0) return; s_copy(pending_url, resolved, sizeof(pending_url)); pending_action |= ACT_NAV; cancel_fetch = 1; } static void js_host_changed(void) { /* textContent only replaces text nodes; computed styles are unchanged. * A future class/style binding must call br_reset_styles() itself. */ layout_valid = 0; ngui_invalidate(&ui); } static int fetch_script_text(const char *url, unsigned char **out, unsigned int *outlen, char *err, unsigned int errcap); static void script_error(const char *what, const char *url, const char *why) { char msg[192]; unsigned int n = 0; s_append(msg, &n, what, sizeof(msg)); if (url && url[0]) { s_append(msg, &n, " ", sizeof(msg)); s_append(msg, &n, url, sizeof(msg)); } if (why && why[0]) { s_append(msg, &n, ": ", sizeof(msg)); s_append(msg, &n, why, sizeof(msg)); } js_host_report(msg); } /* Run the page's scripts in document order: external