browser.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503
  1. /* browser: a small HTML/CSS browser built on ngui, the image library and the
  2. * shared uhttp client.
  3. *
  4. * No JavaScript and no forms; external CSS is loaded over uhttp, and images
  5. * are decoded with img.c and drawn inline. Large buffers (page body and the
  6. * image decode arena) live in the per-process SYS_MEM_ALLOC heap; the DOM and
  7. * the image/CSS I/O scratch use fixed static arenas.
  8. */
  9. #include "ucmd_lib.h"
  10. #include "ngui.h"
  11. #include "browser_html.h"
  12. #include "uhttp.h"
  13. #include "img.h"
  14. #include "inflate.h"
  15. #define BR_W 960
  16. #define BR_H 640
  17. /* CSS media queries are evaluated as a desktop browser. */
  18. #define BR_VIEWPORT_W 1000
  19. #define BR_TOOLBAR_H 30
  20. #define BR_STATUS_H 18
  21. #define BR_HEAP_INIT (6u * 1024u * 1024u)
  22. #define BR_HEAP_MAX (32u * 1024u * 1024u)
  23. #define BR_PAGE_MAX (2u * 1024u * 1024u)
  24. #define BR_IMG_FILE_MAX (512u * 1024u)
  25. #define BR_IMG_PIX_MAX (8u * 1024u * 1024u)
  26. #define BR_MAX_IMAGES 16
  27. #define BR_IMG_ARENA (24u * 1024u * 1024u)
  28. #define BR_IMG_LAYOUT_W 1000
  29. #define BR_CSS_MAX (832u * 1024u)
  30. #define BR_CSS_COMP_MAX (192u * 1024u)
  31. #define BR_MAX_SHEETS 6
  32. #define BR_HISTORY 16
  33. #define BR_URL_MAX 320
  34. #define BR_STATUS_MAX 200
  35. #define BR_IMG_RGB565(r, g, b) \
  36. ((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3)))
  37. #define BR_GRAY 0x8410
  38. enum {
  39. ID_URL = 1,
  40. ID_GO,
  41. ID_BACK,
  42. ID_FORWARD,
  43. ID_RELOAD,
  44. ID_HOME
  45. };
  46. #define ACT_NAV (1u << 0)
  47. #define ACT_BACK (1u << 1)
  48. #define ACT_FORWARD (1u << 2)
  49. #define ACT_RELOAD (1u << 3)
  50. #define ACT_HOME (1u << 4)
  51. #define ACT_CLOSE (1u << 5)
  52. struct br_image {
  53. int state; /* 0 empty, 1 ready */
  54. struct img_image img; /* scaled bitmap at <= BR_IMG_LAYOUT_W */
  55. int native_w, native_h;
  56. char err[32];
  57. };
  58. static struct ngui ui;
  59. static struct ngui_input url_in;
  60. static struct ngui_scroll page_scroll;
  61. static struct br_doc doc;
  62. static struct br_image images[BR_MAX_IMAGES];
  63. static int image_count;
  64. static unsigned char *heap;
  65. static unsigned int heap_size = 0;
  66. static unsigned int heap_used = 0;
  67. /* One reusable I/O buffer and one shared decode arena: decoded pixels are
  68. * scaled down to a display bitmap, then the arena is reused for the next
  69. * image. This keeps 4K screenshots inside the 32 MB heap. The two halves
  70. * double as the gzip input scratch for page/CSS fetches, where the heap has
  71. * no room for another multi-megabyte body. */
  72. static unsigned char image_io_buf[2u * BR_IMG_FILE_MAX];
  73. #define image_file_buf (image_io_buf)
  74. #define image_comp_buf (image_io_buf + BR_IMG_FILE_MAX)
  75. #define BR_PAGE_COMP_MAX (2u * BR_IMG_FILE_MAX)
  76. static struct inflate image_inflate_ws;
  77. static unsigned char *image_arena;
  78. static unsigned int image_arena_size;
  79. #if BR_CSS_MAX + BR_CSS_COMP_MAX > 2u * BR_IMG_FILE_MAX
  80. #error "CSS scratch must fit in image_io_buf"
  81. #endif
  82. static char page_url[BR_URL_MAX];
  83. static char page_title[BR_TITLE_MAX];
  84. static char cwd[64];
  85. static int page_is_local;
  86. static int page_loading;
  87. static char status_text[BR_STATUS_MAX];
  88. static unsigned int status_until;
  89. static int status_error;
  90. static char history[BR_HISTORY][BR_URL_MAX];
  91. static int hist_len, hist_pos;
  92. static char click_url[BR_URL_MAX];
  93. static int quit;
  94. static struct ngui_rect page_rect;
  95. /* layout output */
  96. struct lay_run {
  97. unsigned short off, len;
  98. unsigned short gap;
  99. unsigned short color;
  100. unsigned short href;
  101. char lit[28];
  102. short img;
  103. unsigned short iw, ih;
  104. unsigned char scale, bold, underline;
  105. unsigned char has_lit;
  106. };
  107. struct lay {
  108. int draw;
  109. struct ngui *ui;
  110. int left, right;
  111. int y;
  112. int line_x, line_y, line_h;
  113. int run_x;
  114. int align;
  115. int depth;
  116. int space_pending;
  117. struct lay_run runs[96];
  118. int nruns;
  119. };
  120. struct hit {
  121. int x, y, w, h;
  122. unsigned short node;
  123. };
  124. #define HIT_MAX 96
  125. static struct hit hits[HIT_MAX];
  126. static int nhits;
  127. static unsigned short block_h[BR_MAX_NODES];
  128. static char li_marker[BR_MAX_NODES][6];
  129. /* one connection object; the browser fetches one resource at a time */
  130. static struct uhttp http;
  131. /* ---- tiny strings ---- */
  132. static unsigned int s_len(const char *s)
  133. {
  134. unsigned int n = 0;
  135. while (s && s[n])
  136. n++;
  137. return n;
  138. }
  139. static void s_copy(char *d, const char *s, unsigned int cap)
  140. {
  141. unsigned int i = 0;
  142. if (!cap)
  143. return;
  144. while (s && s[i] && i + 1 < cap) {
  145. d[i] = s[i];
  146. i++;
  147. }
  148. d[i] = '\0';
  149. }
  150. static void s_append(char *d, unsigned int *n, const char *s, unsigned int cap)
  151. {
  152. while (s && *s && *n + 1 < cap)
  153. d[(*n)++] = *s++;
  154. d[*n] = '\0';
  155. }
  156. static void s_append_dec(char *d, unsigned int *n, unsigned int v,
  157. unsigned int cap)
  158. {
  159. char tmp[12];
  160. unsigned int i = 0, j;
  161. do {
  162. tmp[i++] = (char)('0' + v % 10);
  163. v /= 10;
  164. } while (v && i < sizeof(tmp));
  165. for (j = 0; j < i && *n + 1 < cap; j++)
  166. d[(*n)++] = tmp[i - 1 - j];
  167. d[*n] = '\0';
  168. }
  169. static int s_eq(const char *a, const char *b)
  170. {
  171. while (*a && *b) {
  172. if (*a != *b)
  173. return 0;
  174. a++;
  175. b++;
  176. }
  177. return *a == *b;
  178. }
  179. static int starts_with(const char *s, const char *p)
  180. {
  181. unsigned int i = 0;
  182. while (p[i]) {
  183. if (s[i] != p[i])
  184. return 0;
  185. i++;
  186. }
  187. return 1;
  188. }
  189. static int contains_ci(const char *s, const char *p)
  190. {
  191. unsigned int i, j;
  192. for (i = 0; s[i]; i++) {
  193. for (j = 0; p[j]; j++) {
  194. char a = s[i + j];
  195. if (a >= 'A' && a <= 'Z')
  196. a = (char)(a - 'A' + 'a');
  197. if (a != p[j])
  198. break;
  199. }
  200. if (!p[j])
  201. return 1;
  202. }
  203. return 0;
  204. }
  205. static char *trim(char *s)
  206. {
  207. unsigned int n;
  208. while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')
  209. s++;
  210. n = s_len(s);
  211. while (n && (s[n - 1] == ' ' || s[n - 1] == '\t' ||
  212. s[n - 1] == '\r' || s[n - 1] == '\n'))
  213. s[--n] = '\0';
  214. return s;
  215. }
  216. /* ---- per-process heap ---- */
  217. static int heap_start(void)
  218. {
  219. heap = (unsigned char *)u_mem_alloc(BR_HEAP_INIT);
  220. if (!heap) {
  221. heap_size = heap_used = 0;
  222. return -1;
  223. }
  224. heap_size = BR_HEAP_INIT;
  225. heap_used = 0;
  226. return 0;
  227. }
  228. static void heap_clear(void)
  229. {
  230. u_mem_free();
  231. heap = 0;
  232. heap_size = heap_used = 0;
  233. image_arena = 0;
  234. image_arena_size = 0;
  235. }
  236. static void *heap_alloc(unsigned int bytes, unsigned int align)
  237. {
  238. unsigned int start, want;
  239. if (!heap || bytes == 0)
  240. return 0;
  241. if (align < 1)
  242. align = 1;
  243. if (align & (align - 1))
  244. return 0;
  245. start = (heap_used + align - 1) & ~(align - 1);
  246. if (start > BR_HEAP_MAX || bytes > BR_HEAP_MAX - start)
  247. return 0;
  248. if (start + bytes > heap_size) {
  249. want = heap_size ? heap_size : BR_HEAP_INIT;
  250. while (want < start + bytes) {
  251. if (want > BR_HEAP_MAX / 2) {
  252. want = BR_HEAP_MAX;
  253. break;
  254. }
  255. want *= 2;
  256. }
  257. if (want < start + bytes || want > BR_HEAP_MAX)
  258. return 0;
  259. if (u_mem_grow(want) != 0)
  260. return 0;
  261. heap_size = want;
  262. }
  263. heap_used = start + bytes;
  264. return heap + start;
  265. }
  266. /* ---- status ---- */
  267. static void set_status(const char *s, int err, unsigned int ttl_ms)
  268. {
  269. s_copy(status_text, s, sizeof(status_text));
  270. status_error = err;
  271. status_until = ttl_ms ? u_ticks_ms() + ttl_ms : 0;
  272. ngui_invalidate(&ui);
  273. }
  274. /* ---- images ---- */
  275. static void image_reset(void)
  276. {
  277. int i;
  278. for (i = 0; i < BR_MAX_IMAGES; i++) {
  279. images[i].state = 0;
  280. images[i].native_w = images[i].native_h = 0;
  281. images[i].err[0] = '\0';
  282. }
  283. image_count = 0;
  284. }
  285. /* Decode into the shared arena, then copy/scale the result into a display
  286. * bitmap of at most BR_IMG_LAYOUT_W pixels wide. The arena is reused by the
  287. * next image, so the per-image cost is its scaled bitmap, not full 4K RGBA
  288. * pixels. Transparent edges are composited over white. */
  289. static int decode_image_data(const unsigned char *data, unsigned int len,
  290. struct br_image *bi)
  291. {
  292. struct img_info info;
  293. struct img_options opt;
  294. struct img_mem mem;
  295. struct img_image decoded;
  296. unsigned int est, bytes, i;
  297. unsigned short *px;
  298. int dw, dh, r;
  299. if (img_sniff(data, len, &info) != 0) {
  300. s_copy(bi->err, "unknown image format", sizeof(bi->err));
  301. return -1;
  302. }
  303. est = img_estimate(&info);
  304. if (est == 0xFFFFFFFFu || est > BR_IMG_ARENA) {
  305. s_copy(bi->err, "image too large", sizeof(bi->err));
  306. return -1;
  307. }
  308. if (!image_arena) {
  309. image_arena = (unsigned char *)heap_alloc(BR_IMG_ARENA, 16);
  310. if (!image_arena) {
  311. s_copy(bi->err, "out of memory", sizeof(bi->err));
  312. return -1;
  313. }
  314. image_arena_size = BR_IMG_ARENA;
  315. }
  316. img_mem_init(&mem, image_arena, image_arena_size);
  317. opt.max_pixels = BR_IMG_PIX_MAX;
  318. r = img_decode(data, len, &opt, &mem, &decoded);
  319. if (r != 0) {
  320. s_copy(bi->err, img_error_string(r), sizeof(bi->err));
  321. return -1;
  322. }
  323. if (decoded.w == 0 || decoded.h == 0) {
  324. s_copy(bi->err, "empty image", sizeof(bi->err));
  325. return -1;
  326. }
  327. bi->native_w = (int)decoded.w;
  328. bi->native_h = (int)decoded.h;
  329. dw = (int)decoded.w;
  330. dh = (int)decoded.h;
  331. if (dw > BR_IMG_LAYOUT_W) {
  332. dh = (int)((unsigned int)decoded.h * BR_IMG_LAYOUT_W /
  333. decoded.w);
  334. dw = BR_IMG_LAYOUT_W;
  335. }
  336. if (dh < 1)
  337. dh = 1;
  338. if (dw < 1)
  339. dw = 1;
  340. bytes = (unsigned int)dw * (unsigned int)dh * 2u;
  341. px = (unsigned short *)heap_alloc(bytes, 16);
  342. if (!px) {
  343. s_copy(bi->err, "out of memory", sizeof(bi->err));
  344. return -1;
  345. }
  346. for (i = 0; i < (unsigned int)dw * (unsigned int)dh; i++)
  347. px[i] = ui.theme.bg;
  348. {
  349. struct img_blit b;
  350. b.dst = px;
  351. b.dst_w = dw;
  352. b.dst_h = dh;
  353. b.clip_x = 0;
  354. b.clip_y = 0;
  355. b.clip_w = dw;
  356. b.clip_h = dh;
  357. b.x = 0;
  358. b.y = 0;
  359. b.scale = ((unsigned int)dw << 16) / decoded.w;
  360. b.bg = ui.theme.bg;
  361. b.bg2 = ui.theme.bg;
  362. b.flags = IMG_BLIT_OVER_DST;
  363. img_draw(&decoded, &b);
  364. }
  365. bi->img.format = decoded.format;
  366. bi->img.w = (unsigned int)dw;
  367. bi->img.h = (unsigned int)dh;
  368. bi->img.px = px;
  369. bi->img.alpha = 0;
  370. bi->img.flags = 0;
  371. bi->state = 1;
  372. return 0;
  373. }
  374. /* ---- URL / local path handling ---- */
  375. static int is_http(const char *s)
  376. {
  377. return starts_with(s, "http://") || starts_with(s, "https://");
  378. }
  379. /* Collapse "/./", "/../" and duplicate slashes in an absolute path. */
  380. static int normalize_abs(const char *in, char *out, unsigned int cap)
  381. {
  382. char seg[64];
  383. unsigned int i = 0, o = 1;
  384. int depth = 0;
  385. int trailing;
  386. if (!in || in[0] != '/' || cap < 2)
  387. return -1;
  388. trailing = 0;
  389. {
  390. unsigned int n = s_len(in);
  391. if (n > 1 && in[n - 1] == '/')
  392. trailing = 1;
  393. }
  394. out[0] = '/';
  395. while (in[i]) {
  396. unsigned int n = 0;
  397. while (in[i] == '/')
  398. i++;
  399. while (in[i] && in[i] != '/' && in[i] != '?' && in[i] != '#') {
  400. if (n + 1 < sizeof(seg))
  401. seg[n++] = in[i];
  402. i++;
  403. }
  404. seg[n] = '\0';
  405. if (n == 0)
  406. break;
  407. if (s_eq(seg, "."))
  408. continue;
  409. if (s_eq(seg, "..")) {
  410. if (depth > 0) {
  411. while (o > 1 && out[o - 1] != '/')
  412. o--;
  413. if (o > 1)
  414. o--;
  415. depth--;
  416. }
  417. continue;
  418. }
  419. if (o > 1) {
  420. if (o + 1 >= cap)
  421. return -1;
  422. out[o++] = '/';
  423. }
  424. if (o + n + 1 >= cap)
  425. return -1;
  426. {
  427. unsigned int j;
  428. for (j = 0; j < n; j++)
  429. out[o++] = seg[j];
  430. }
  431. depth++;
  432. }
  433. if (trailing && o > 1) {
  434. if (o + 1 >= cap)
  435. return -1;
  436. out[o++] = '/';
  437. }
  438. out[o] = '\0';
  439. return 0;
  440. }
  441. static void make_abs_local(const char *in, char *out, unsigned int cap)
  442. {
  443. char joined[BR_URL_MAX];
  444. if (!in || !in[0]) {
  445. s_copy(out, cwd[0] ? cwd : "/", cap);
  446. return;
  447. }
  448. if (in[0] == '/') {
  449. if (normalize_abs(in, out, cap) != 0)
  450. s_copy(out, in, cap);
  451. return;
  452. }
  453. {
  454. unsigned int n = 0;
  455. s_append(joined, &n, cwd[0] ? cwd : "/", sizeof(joined));
  456. s_append(joined, &n, "/", sizeof(joined));
  457. s_append(joined, &n, in, sizeof(joined));
  458. }
  459. if (normalize_abs(joined, out, cap) != 0)
  460. s_copy(out, joined, cap);
  461. }
  462. static int path_dir(const char *path, char *out, unsigned int cap)
  463. {
  464. unsigned int i, last = 0;
  465. for (i = 0; path[i]; i++)
  466. if (path[i] == '/')
  467. last = i;
  468. if (last == 0) {
  469. if (cap < 2)
  470. return -1;
  471. out[0] = '/';
  472. out[1] = '\0';
  473. return 0;
  474. }
  475. if (last + 1 >= cap)
  476. return -1;
  477. {
  478. unsigned int j;
  479. for (j = 0; j < last; j++)
  480. out[j] = path[j];
  481. out[last] = '\0';
  482. }
  483. return 0;
  484. }
  485. /* Resolve an href against the current page. 0 = ok, -1 = ignore. */
  486. static int resolve_link(const char *base, const char *href, char *out,
  487. unsigned int cap)
  488. {
  489. char joined[BR_URL_MAX];
  490. if (!href || !href[0] || href[0] == '#')
  491. return -1;
  492. if (starts_with(href, "mailto:") || starts_with(href, "javascript:") ||
  493. starts_with(href, "data:") || starts_with(href, "tel:"))
  494. return -1;
  495. if (starts_with(href, "about:")) {
  496. s_copy(out, href, cap);
  497. return 0;
  498. }
  499. if (is_http(href)) {
  500. s_copy(out, href, cap);
  501. return 0;
  502. }
  503. if (starts_with(href, "//")) {
  504. if (is_http(base)) {
  505. unsigned int n = 0;
  506. s_append(out, &n, starts_with(base, "https:") ?
  507. "https:" : "http:", cap);
  508. s_append(out, &n, href, cap);
  509. return 0;
  510. }
  511. return -1;
  512. }
  513. if (is_http(base)) {
  514. return uhttp_resolve(base, href, out, cap) == 0 ? 0 : -1;
  515. }
  516. /* local page */
  517. if (href[0] == '/') {
  518. if (normalize_abs(href, out, cap) != 0)
  519. return -1;
  520. return 0;
  521. }
  522. {
  523. char dir[BR_URL_MAX];
  524. unsigned int n = 0;
  525. if (s_eq(base, "about:home"))
  526. s_copy(dir, cwd[0] ? cwd : "/", sizeof(dir));
  527. else if (path_dir(base, dir, sizeof(dir)) != 0)
  528. s_copy(dir, "/", sizeof(dir));
  529. s_append(joined, &n, dir, sizeof(joined));
  530. if (n == 0 || joined[n - 1] != '/')
  531. s_append(joined, &n, "/", sizeof(joined));
  532. s_append(joined, &n, href, sizeof(joined));
  533. }
  534. return normalize_abs(joined, out, cap) == 0 ? 0 : -1;
  535. }
  536. /* Best-effort decompression: keep the first `cap` bytes and discard the
  537. * rest instead of failing when a page or stylesheet is larger than the
  538. * buffer. The stream is still walked to the end, so a normal page is
  539. * verified; a truncated compressed input still returns whatever was decoded
  540. * so far. */
  541. struct trunc_buf {
  542. unsigned char *dst;
  543. unsigned int cap, pos;
  544. int full;
  545. };
  546. static int trunc_write(void *ctx, const unsigned char *data, unsigned int len)
  547. {
  548. struct trunc_buf *m = (struct trunc_buf *)ctx;
  549. if (len > m->cap - m->pos) {
  550. unsigned int room = m->cap - m->pos;
  551. if (room) {
  552. unsigned int i;
  553. for (i = 0; i < room; i++)
  554. m->dst[m->pos + i] = data[i];
  555. m->pos += room;
  556. }
  557. m->full = 1;
  558. return 0;
  559. }
  560. {
  561. unsigned int i;
  562. for (i = 0; i < len; i++)
  563. m->dst[m->pos + i] = data[i];
  564. }
  565. m->pos += len;
  566. return 0;
  567. }
  568. static int inflate_trunc(const unsigned char *src, unsigned int len,
  569. unsigned char *dst, unsigned int cap,
  570. unsigned int *outlen, int gzip, int *truncated)
  571. {
  572. struct inflate_src in;
  573. struct inflate_sink sink;
  574. struct trunc_buf m;
  575. unsigned int got = 0;
  576. int r;
  577. in.p = src;
  578. in.len = len;
  579. in.more = 0;
  580. in.ctx = 0;
  581. m.dst = dst;
  582. m.cap = cap;
  583. m.pos = 0;
  584. m.full = 0;
  585. sink.write = trunc_write;
  586. sink.ctx = &m;
  587. inflate_init(&image_inflate_ws, &in, &sink);
  588. r = gzip ? inflate_gzip(&image_inflate_ws, &got) :
  589. inflate_zlib(&image_inflate_ws, &got);
  590. *outlen = m.pos;
  591. *truncated = m.full || r != 0;
  592. if (r != 0 && m.pos == 0)
  593. return -1;
  594. return 0;
  595. }
  596. /* ---- HTTP / file fetch ---- */
  597. static int read_url(const char *url, unsigned int cap, unsigned char **out,
  598. unsigned int *outlen, int *is_local, char *ctype,
  599. unsigned int ctcap, char *final, unsigned int finalcap,
  600. char *err, unsigned int errcap, int *truncated)
  601. {
  602. unsigned char *buf = 0;
  603. unsigned int total = 0;
  604. int rv;
  605. *out = 0;
  606. *outlen = 0;
  607. *truncated = 0;
  608. if (final && finalcap)
  609. final[0] = '\0';
  610. if (ctype && ctcap)
  611. ctype[0] = '\0';
  612. if (url[0] == '/') {
  613. buf = (unsigned char *)heap_alloc(cap, 16);
  614. if (!buf) {
  615. s_copy(err, "out of memory", errcap);
  616. return -1;
  617. }
  618. rv = u_syscall3(SYS_READ_FILE, (int)url, (int)buf, (int)cap);
  619. if (rv < 0) {
  620. s_copy(err, "file not found", errcap);
  621. return -1;
  622. }
  623. *out = buf;
  624. *outlen = (unsigned int)rv;
  625. *is_local = 1;
  626. if (ctype)
  627. s_copy(ctype, "text/html", ctcap);
  628. if (final)
  629. s_copy(final, url, finalcap);
  630. if ((unsigned int)rv == cap)
  631. *truncated = 1;
  632. return 0;
  633. }
  634. uhttp_init(&http);
  635. http.user_agent = "nebos-browser/0.1";
  636. rv = uhttp_open(&http, url, UHTTP_GZIP, 0);
  637. if (rv != 0) {
  638. s_copy(err, uhttp_error_string(rv), errcap);
  639. return -1;
  640. }
  641. if (ctype)
  642. s_copy(ctype, http.resp.content_type, ctcap);
  643. if (final && http.resp.final_url[0])
  644. s_copy(final, http.resp.final_url, finalcap);
  645. *is_local = 0;
  646. if (http.resp.gzip ||
  647. contains_ci(http.resp.content_encoding, "gzip") ||
  648. contains_ci(http.resp.content_encoding, "deflate")) {
  649. unsigned char *comp = image_io_buf;
  650. unsigned int climit = cap;
  651. unsigned char *plain;
  652. unsigned int plen = 0;
  653. int zr, was_trunc;
  654. if (climit > BR_PAGE_COMP_MAX)
  655. climit = BR_PAGE_COMP_MAX;
  656. total = 0;
  657. for (;;) {
  658. rv = uhttp_read(&http, comp + total, climit - total,
  659. UHTTP_IDLE_TIMEOUT_MS);
  660. if (rv > 0) {
  661. total += (unsigned int)rv;
  662. if (total >= climit) {
  663. *truncated = 1;
  664. break;
  665. }
  666. continue;
  667. }
  668. if (rv < 0) {
  669. s_copy(err, uhttp_error_string(rv), errcap);
  670. uhttp_close(&http);
  671. return -1;
  672. }
  673. break;
  674. }
  675. uhttp_close(&http);
  676. plain = (unsigned char *)heap_alloc(cap, 16);
  677. if (!plain) {
  678. s_copy(err, "out of memory", errcap);
  679. return -1;
  680. }
  681. if (contains_ci(http.resp.content_encoding, "deflate"))
  682. zr = inflate_trunc(comp, total, plain, cap, &plen, 0,
  683. &was_trunc);
  684. else
  685. zr = inflate_trunc(comp, total, plain, cap, &plen, 1,
  686. &was_trunc);
  687. if (zr != 0) {
  688. s_copy(err, "compressed page is corrupt", errcap);
  689. return -1;
  690. }
  691. if (was_trunc)
  692. *truncated = 1;
  693. *out = plain;
  694. *outlen = plen;
  695. return 0;
  696. }
  697. buf = (unsigned char *)heap_alloc(cap, 16);
  698. if (!buf) {
  699. s_copy(err, "out of memory", errcap);
  700. uhttp_close(&http);
  701. return -1;
  702. }
  703. for (;;) {
  704. rv = uhttp_read(&http, buf + total, cap - total,
  705. UHTTP_IDLE_TIMEOUT_MS);
  706. if (rv > 0) {
  707. total += (unsigned int)rv;
  708. if (total >= cap) {
  709. *truncated = 1;
  710. break;
  711. }
  712. continue;
  713. }
  714. if (rv < 0) {
  715. s_copy(err, uhttp_error_string(rv), errcap);
  716. uhttp_close(&http);
  717. return -1;
  718. }
  719. break;
  720. }
  721. uhttp_close(&http);
  722. if (total == 0) {
  723. s_copy(err, "empty response", errcap);
  724. return -1;
  725. }
  726. *out = buf;
  727. *outlen = total;
  728. return 0;
  729. }
  730. /* Fetch an image into the fixed scratch buffers. The page heap is already
  731. * carrying the 24 MB decode arena and every scaled bitmap, so images must not
  732. * allocate their file body from it as well. */
  733. static int fetch_image(const char *url, unsigned int cap,
  734. unsigned char **out, unsigned int *outlen,
  735. char *err, unsigned int errcap)
  736. {
  737. unsigned int total = 0;
  738. int rv;
  739. *out = 0;
  740. *outlen = 0;
  741. if (cap > BR_IMG_FILE_MAX)
  742. cap = BR_IMG_FILE_MAX;
  743. if (url[0] == '/') {
  744. rv = u_syscall3(SYS_READ_FILE, (int)url, (int)image_file_buf,
  745. (int)cap);
  746. if (rv < 0) {
  747. s_copy(err, "file not found", errcap);
  748. return -1;
  749. }
  750. *out = image_file_buf;
  751. *outlen = (unsigned int)rv;
  752. return 0;
  753. }
  754. uhttp_init(&http);
  755. http.user_agent = "nebos-browser/0.1";
  756. rv = uhttp_open(&http, url, UHTTP_GZIP, 0);
  757. if (rv != 0) {
  758. s_copy(err, uhttp_error_string(rv), errcap);
  759. return -1;
  760. }
  761. if (http.resp.gzip ||
  762. contains_ci(http.resp.content_encoding, "gzip") ||
  763. contains_ci(http.resp.content_encoding, "deflate")) {
  764. unsigned int plen = 0;
  765. int zr;
  766. total = 0;
  767. for (;;) {
  768. rv = uhttp_read(&http, image_comp_buf + total,
  769. BR_IMG_FILE_MAX - total,
  770. UHTTP_IDLE_TIMEOUT_MS);
  771. if (rv > 0) {
  772. total += (unsigned int)rv;
  773. if (total >= BR_IMG_FILE_MAX)
  774. break;
  775. continue;
  776. }
  777. if (rv < 0) {
  778. s_copy(err, uhttp_error_string(rv), errcap);
  779. uhttp_close(&http);
  780. return -1;
  781. }
  782. break;
  783. }
  784. uhttp_close(&http);
  785. if (!total) {
  786. s_copy(err, "empty image", errcap);
  787. return -1;
  788. }
  789. if (contains_ci(http.resp.content_encoding, "deflate"))
  790. zr = inflate_zlib_mem(image_comp_buf, total,
  791. image_file_buf, cap, &plen,
  792. &image_inflate_ws);
  793. else
  794. zr = inflate_gzip_mem(image_comp_buf, total,
  795. image_file_buf, cap, &plen,
  796. &image_inflate_ws);
  797. if (zr != 0) {
  798. s_copy(err, "compressed image is corrupt", errcap);
  799. return -1;
  800. }
  801. *out = image_file_buf;
  802. *outlen = plen;
  803. return 0;
  804. }
  805. for (;;) {
  806. rv = uhttp_read(&http, image_file_buf + total, cap - total,
  807. UHTTP_IDLE_TIMEOUT_MS);
  808. if (rv > 0) {
  809. total += (unsigned int)rv;
  810. if (total >= cap)
  811. break;
  812. continue;
  813. }
  814. if (rv < 0) {
  815. s_copy(err, uhttp_error_string(rv), errcap);
  816. uhttp_close(&http);
  817. return -1;
  818. }
  819. break;
  820. }
  821. uhttp_close(&http);
  822. if (!total) {
  823. s_copy(err, "empty image", errcap);
  824. return -1;
  825. }
  826. *out = image_file_buf;
  827. *outlen = total;
  828. return 0;
  829. }
  830. /* Read one stylesheet into the shared static buffer. CSS is parsed
  831. * immediately, so it does not need the page heap; this leaves the heap for
  832. * the 2 MB page body, the decode arena and the scaled image bitmaps. */
  833. static int fetch_css(const char *url, unsigned char **out, unsigned int *outlen,
  834. char *err, unsigned int errcap)
  835. {
  836. unsigned int total = 0;
  837. int rv;
  838. *out = 0;
  839. *outlen = 0;
  840. if (url[0] == '/') {
  841. rv = u_syscall3(SYS_READ_FILE, (int)url, (int)image_io_buf,
  842. (int)BR_CSS_MAX);
  843. if (rv < 0) {
  844. s_copy(err, "stylesheet not found", errcap);
  845. return -1;
  846. }
  847. *out = image_io_buf;
  848. *outlen = (unsigned int)rv;
  849. return 0;
  850. }
  851. uhttp_init(&http);
  852. http.user_agent = "nebos-browser/0.1";
  853. rv = uhttp_open(&http, url, UHTTP_GZIP, 0);
  854. if (rv != 0) {
  855. s_copy(err, uhttp_error_string(rv), errcap);
  856. return -1;
  857. }
  858. if (http.resp.gzip ||
  859. contains_ci(http.resp.content_encoding, "gzip") ||
  860. contains_ci(http.resp.content_encoding, "deflate")) {
  861. unsigned char *comp = image_io_buf + BR_CSS_MAX;
  862. unsigned int plen = 0;
  863. int zr, was_trunc;
  864. total = 0;
  865. for (;;) {
  866. rv = uhttp_read(&http, comp + total,
  867. BR_CSS_COMP_MAX - total,
  868. UHTTP_IDLE_TIMEOUT_MS);
  869. if (rv > 0) {
  870. total += (unsigned int)rv;
  871. if (total >= BR_CSS_COMP_MAX)
  872. break;
  873. continue;
  874. }
  875. if (rv < 0) {
  876. s_copy(err, uhttp_error_string(rv), errcap);
  877. uhttp_close(&http);
  878. return -1;
  879. }
  880. break;
  881. }
  882. uhttp_close(&http);
  883. if (!total) {
  884. s_copy(err, "empty stylesheet", errcap);
  885. return -1;
  886. }
  887. if (contains_ci(http.resp.content_encoding, "deflate"))
  888. zr = inflate_trunc(comp, total, image_io_buf,
  889. BR_CSS_MAX, &plen, 0, &was_trunc);
  890. else
  891. zr = inflate_trunc(comp, total, image_io_buf,
  892. BR_CSS_MAX, &plen, 1, &was_trunc);
  893. if (zr != 0) {
  894. s_copy(err, "stylesheet is corrupt", errcap);
  895. return -1;
  896. }
  897. *out = image_io_buf;
  898. *outlen = plen;
  899. return 0;
  900. }
  901. for (;;) {
  902. rv = uhttp_read(&http, image_io_buf + total,
  903. BR_CSS_MAX - total, UHTTP_IDLE_TIMEOUT_MS);
  904. if (rv > 0) {
  905. total += (unsigned int)rv;
  906. if (total >= BR_CSS_MAX)
  907. break;
  908. continue;
  909. }
  910. if (rv < 0) {
  911. s_copy(err, uhttp_error_string(rv), errcap);
  912. uhttp_close(&http);
  913. return -1;
  914. }
  915. break;
  916. }
  917. uhttp_close(&http);
  918. if (!total) {
  919. s_copy(err, "empty stylesheet", errcap);
  920. return -1;
  921. }
  922. *out = image_io_buf;
  923. *outlen = total;
  924. return 0;
  925. }
  926. /* ---- page loading ---- */
  927. static void redraw_now(void);
  928. static char error_page[1024];
  929. static void show_error(const char *title, const char *detail)
  930. {
  931. char *page = error_page;
  932. unsigned int n = 0;
  933. s_append(page, &n, "<html><body><h2>", sizeof(error_page));
  934. s_append(page, &n, title, sizeof(error_page));
  935. s_append(page, &n, "</h2><p>", sizeof(error_page));
  936. {
  937. const char *p = detail;
  938. while (*p && n + 1 < sizeof(error_page)) {
  939. if (*p == '<') s_append(page, &n, "&lt;", sizeof(error_page));
  940. else if (*p == '>') s_append(page, &n, "&gt;", sizeof(error_page));
  941. else if (*p == '&') s_append(page, &n, "&amp;", sizeof(error_page));
  942. else page[n++] = *p;
  943. p++;
  944. }
  945. }
  946. page[n] = '\0';
  947. s_append(page, &n, "</p><p>", sizeof(error_page));
  948. s_append(page, &n, page_url, sizeof(error_page));
  949. s_append(page, &n, "</p><p><a href=\"/\">home</a></p></body></html>",
  950. sizeof(error_page));
  951. br_parse(&doc, page, n);
  952. s_copy(page_title, title, sizeof(page_title));
  953. page_loading = 0;
  954. set_status(title, 1, 0);
  955. ngui_invalidate(&ui);
  956. }
  957. static int decode_direct_image(const unsigned char *data, unsigned int len,
  958. unsigned int node)
  959. {
  960. if (image_count >= BR_MAX_IMAGES)
  961. return -1;
  962. if (decode_image_data(data, len, &images[0]) != 0)
  963. return -1;
  964. doc.nodes[node].img_index = 0;
  965. image_count = 1;
  966. return 0;
  967. }
  968. static int ancestor_is(unsigned int node, unsigned int tag)
  969. {
  970. unsigned int p = doc.nodes[node].parent;
  971. while (p) {
  972. if (doc.nodes[p].tag == tag)
  973. return 1;
  974. p = doc.nodes[p].parent;
  975. }
  976. return 0;
  977. }
  978. static void load_stylesheets(void)
  979. {
  980. unsigned int i, sheets = 0;
  981. for (i = 0; i < doc.nnode && sheets < BR_MAX_SHEETS; i++) {
  982. struct br_node *nd = &doc.nodes[i];
  983. const char *rel, *href;
  984. char url[BR_URL_MAX];
  985. unsigned char *data;
  986. unsigned int len;
  987. char err[64];
  988. if (nd->type != BR_NODE_ELEM || nd->tag != BR_TAG_LINK)
  989. continue;
  990. rel = br_rel(&doc, i);
  991. if (!contains_ci(rel, "stylesheet"))
  992. continue;
  993. href = br_href(&doc, i);
  994. if (!href[0])
  995. continue;
  996. if (resolve_link(page_url, href, url, sizeof(url)) != 0)
  997. continue;
  998. if (fetch_css(url, &data, &len, err, sizeof(err)) != 0)
  999. continue;
  1000. if (len)
  1001. br_parse_css_text(&doc, (const char *)data, len);
  1002. sheets++;
  1003. }
  1004. br_parse_inline_css(&doc);
  1005. br_reset_styles(&doc);
  1006. }
  1007. static void preload_images(void)
  1008. {
  1009. unsigned int i;
  1010. int loaded = 0;
  1011. for (i = 0; i < doc.nnode && loaded < BR_MAX_IMAGES; i++) {
  1012. struct br_node *nd = &doc.nodes[i];
  1013. const char *src;
  1014. char url[BR_URL_MAX];
  1015. unsigned char *data;
  1016. unsigned int len;
  1017. char err[64];
  1018. char msg[64];
  1019. unsigned int n = 0;
  1020. if (nd->type != BR_NODE_ELEM || nd->tag != BR_TAG_IMG ||
  1021. nd->img_index >= 0)
  1022. continue;
  1023. if (ancestor_is(i, BR_TAG_NOSCRIPT))
  1024. continue;
  1025. src = br_src(&doc, i);
  1026. if (!src[0] || contains_ci(src, ".svg") ||
  1027. starts_with(src, "data:"))
  1028. continue;
  1029. if (resolve_link(page_url, src, url, sizeof(url)) != 0)
  1030. continue;
  1031. s_append(msg, &n, "Loading images ", sizeof(msg));
  1032. s_append_dec(msg, &n, (unsigned int)loaded + 1, sizeof(msg));
  1033. s_append(msg, &n, "...", sizeof(msg));
  1034. s_copy(status_text, msg, sizeof(status_text));
  1035. status_until = 0;
  1036. status_error = 0;
  1037. redraw_now();
  1038. if (fetch_image(url, BR_IMG_FILE_MAX, &data, &len, err,
  1039. sizeof(err)) != 0)
  1040. continue;
  1041. if (decode_image_data(data, len, &images[image_count]) != 0)
  1042. continue;
  1043. nd->img_index = (short)image_count;
  1044. image_count++;
  1045. loaded++;
  1046. }
  1047. }
  1048. static void history_push(const char *url)
  1049. {
  1050. if (!url || !url[0])
  1051. return;
  1052. if (hist_len > 0 && hist_pos >= 0 && hist_pos < hist_len &&
  1053. s_eq(history[hist_pos], url))
  1054. return;
  1055. if (hist_len > 0 && hist_pos + 1 < hist_len)
  1056. hist_len = hist_pos + 1;
  1057. if (hist_len < BR_HISTORY) {
  1058. s_copy(history[hist_len], url, BR_URL_MAX);
  1059. hist_pos = hist_len;
  1060. hist_len++;
  1061. } else {
  1062. int i;
  1063. for (i = 1; i < BR_HISTORY; i++)
  1064. s_copy(history[i - 1], history[i], BR_URL_MAX);
  1065. s_copy(history[BR_HISTORY - 1], url, BR_URL_MAX);
  1066. hist_pos = BR_HISTORY - 1;
  1067. }
  1068. }
  1069. static void load_page(const char *url, int local, int push)
  1070. {
  1071. unsigned char *body = 0;
  1072. unsigned int len = 0;
  1073. int truncated = 0;
  1074. struct img_info image_info;
  1075. char ctype[64];
  1076. char final[BR_URL_MAX];
  1077. char err[80];
  1078. const char *detail = 0;
  1079. final[0] = '\0';
  1080. heap_clear();
  1081. image_reset();
  1082. page_scroll.offset = 0;
  1083. page_scroll.dragging = 0;
  1084. br_parse_plain(&doc, "", 0); /* blank while loading */
  1085. page_title[0] = '\0';
  1086. page_loading = 1;
  1087. page_is_local = local;
  1088. s_copy(page_url, url, sizeof(page_url));
  1089. if (final[0])
  1090. final[0] = '\0';
  1091. if (push)
  1092. history_push(url);
  1093. set_status("Loading...", 0, 0);
  1094. redraw_now();
  1095. if (heap_start() != 0) {
  1096. show_error("Out of memory", "Cannot allocate the page buffer");
  1097. return;
  1098. }
  1099. if (read_url(url, BR_PAGE_MAX, &body, &len, &local, ctype,
  1100. sizeof(ctype), final, sizeof(final), err, sizeof(err),
  1101. &truncated) != 0) {
  1102. detail = err;
  1103. show_error("Cannot load this page", detail);
  1104. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  1105. url_in.len = s_len(url_in.text);
  1106. url_in.cursor = url_in.len;
  1107. url_in.scroll = 0;
  1108. return;
  1109. }
  1110. page_is_local = local;
  1111. if (final[0])
  1112. s_copy(page_url, final, sizeof(page_url));
  1113. if (contains_ci(ctype, "text/plain")) {
  1114. br_parse_plain(&doc, (const char *)body, len);
  1115. } else if (contains_ci(ctype, "image/") ||
  1116. (!contains_ci(ctype, "text/html") &&
  1117. img_sniff(body, len, &image_info) == 0)) {
  1118. unsigned int node = br_parse_image_doc(&doc, page_url);
  1119. if (node)
  1120. decode_direct_image(body, len, node);
  1121. s_copy(page_title, "Image", sizeof(page_title));
  1122. } else {
  1123. br_parse(&doc, (const char *)body, len);
  1124. s_copy(page_title, doc.title[0] ? doc.title : page_url,
  1125. sizeof(page_title));
  1126. load_stylesheets();
  1127. preload_images();
  1128. }
  1129. if (truncated)
  1130. set_status("Page truncated at the buffer limit", 0, 6000);
  1131. else if (doc.truncated)
  1132. set_status("Page truncated by the parser", 0, 6000);
  1133. else if (starts_with(page_url, "https://"))
  1134. set_status("HTTPS: certificate NOT verified", 0, 6000);
  1135. else
  1136. set_status("", 0, 0);
  1137. page_loading = 0;
  1138. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  1139. url_in.len = s_len(url_in.text);
  1140. url_in.cursor = url_in.len;
  1141. url_in.scroll = 0;
  1142. ngui_set_focus(&ui, 0);
  1143. ngui_invalidate(&ui);
  1144. }
  1145. static void load_home(int push)
  1146. {
  1147. heap_clear();
  1148. image_reset();
  1149. page_scroll.offset = 0;
  1150. page_scroll.dragging = 0;
  1151. br_parse_plain(&doc, "", 0);
  1152. page_loading = 1;
  1153. page_is_local = 0;
  1154. s_copy(page_url, "about:home", sizeof(page_url));
  1155. if (push)
  1156. history_push(page_url);
  1157. redraw_now();
  1158. if (heap_start() != 0) {
  1159. show_error("Out of memory", "Cannot allocate the browser heap");
  1160. return;
  1161. }
  1162. {
  1163. static const char home[] =
  1164. "<html><head><title>Nebs Browser</title><style>"
  1165. "body { margin: 10px; color: #e4e6f0; }"
  1166. "h1 { color: #7fb0ff; }"
  1167. ".box { background-color: #262b3a; margin: 12px; "
  1168. "padding: 10px; }"
  1169. "a { color: #7fb0ff; }"
  1170. "pre { background-color: #32384a; }"
  1171. "</style></head><body>"
  1172. "<h1>Nebs Browser</h1>"
  1173. "<p>A small HTML/CSS browser built on <b>ngui</b>, the "
  1174. "shared HTTP client and the image library.</p>"
  1175. "<div class=\"box\"><b>Try:</b>"
  1176. "<ul><li><a href=\"/floppy1/DEMO.HTM\">/"
  1177. "floppy1/DEMO.HTM</a> on the root floppy</li>"
  1178. "<li><a href=\"http://10.0.2.2:8000/\">"
  1179. "http://10.0.2.2:8000/</a> on the QEMU host</li>"
  1180. "<li><a href=\"https://example.com/\">"
  1181. "https://example.com/</a> (TLS, not verified)</li>"
  1182. "</ul></div>"
  1183. "<p>Type a URL in the bar above. Esc closes the "
  1184. "window, Ctrl+L focuses the address bar.</p>"
  1185. "<pre>javascript: not supported\nforms: not supported"
  1186. "\nimages: PNG JPEG GIF BMP TGA PNM ICO</pre>"
  1187. "</body></html>";
  1188. br_parse(&doc, home, sizeof(home) - 1);
  1189. br_parse_inline_css(&doc);
  1190. br_reset_styles(&doc);
  1191. s_copy(page_title, doc.title, sizeof(page_title));
  1192. }
  1193. page_loading = 0;
  1194. set_status("", 0, 0);
  1195. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  1196. url_in.len = s_len(url_in.text);
  1197. url_in.cursor = url_in.len;
  1198. url_in.scroll = 0;
  1199. ngui_set_focus(&ui, 0);
  1200. ngui_invalidate(&ui);
  1201. }
  1202. static void navigate(const char *input, int push)
  1203. {
  1204. char target[BR_URL_MAX];
  1205. char *t;
  1206. if (!input)
  1207. return;
  1208. s_copy(target, input, sizeof(target));
  1209. t = trim(target);
  1210. if (!t[0])
  1211. return;
  1212. if (s_eq(t, "about:home")) {
  1213. load_home(push);
  1214. return;
  1215. }
  1216. if (starts_with(t, "file://")) {
  1217. make_abs_local(t + 7, target, sizeof(target));
  1218. load_page(target, 1, push);
  1219. return;
  1220. }
  1221. if (is_http(t)) {
  1222. load_page(t, 0, push);
  1223. return;
  1224. }
  1225. {
  1226. struct sys_stat st;
  1227. if (t[0] == '/' || (u_stat(t, &st) == 0 && !st.is_dir)) {
  1228. make_abs_local(t, target, sizeof(target));
  1229. load_page(target, 1, push);
  1230. return;
  1231. }
  1232. }
  1233. {
  1234. char with_scheme[BR_URL_MAX];
  1235. unsigned int n = 0;
  1236. s_append(with_scheme, &n, "http://", sizeof(with_scheme));
  1237. s_append(with_scheme, &n, t, sizeof(with_scheme));
  1238. load_page(with_scheme, 0, push);
  1239. }
  1240. }
  1241. /* ---- layout ---- */
  1242. static struct br_style base_style(void)
  1243. {
  1244. struct br_style s = { 0 };
  1245. s.color = ui.theme.text;
  1246. s.bg = ui.theme.bg;
  1247. s.has_bg = 0;
  1248. s.border_color = 0;
  1249. s.scale = 1;
  1250. s.bold = 0;
  1251. s.underline = 0;
  1252. s.align = BR_ALIGN_LEFT;
  1253. s.display = BR_DISPLAY_BLOCK;
  1254. s.no_bullet = 0;
  1255. s.uppercase = 0;
  1256. s.border_w = 0;
  1257. s.width_kind = s.max_width_kind = s.height_kind = 0;
  1258. s.width = s.max_width = s.height = 0;
  1259. s.line_h_pct = 0;
  1260. s.line_h_px = 0;
  1261. s.mt = s.mb = s.ml = s.mr = 0;
  1262. s.pt = s.pb = s.pl = s.pr = 0;
  1263. return s;
  1264. }
  1265. static int run_width(const struct lay_run *r)
  1266. {
  1267. if (r->img >= 0)
  1268. return r->iw;
  1269. {
  1270. unsigned int n = r->has_lit ? s_len(r->lit) : r->len;
  1271. return (int)(n * (unsigned int)(8 * r->scale) +
  1272. (r->bold ? 1u : 0u));
  1273. }
  1274. }
  1275. static void flush_line(struct lay *l)
  1276. {
  1277. int total, x, i;
  1278. if (!l->nruns && l->line_x == l->left) {
  1279. l->line_h = 0;
  1280. return;
  1281. }
  1282. total = l->line_x - l->left;
  1283. x = l->left;
  1284. if (l->align == BR_ALIGN_CENTER)
  1285. x += (l->right - l->left - total) / 2;
  1286. else if (l->align == BR_ALIGN_RIGHT)
  1287. x = l->right - total;
  1288. if (x < l->left)
  1289. x = l->left;
  1290. for (i = 0; i < l->nruns; i++) {
  1291. struct lay_run *r = &l->runs[i];
  1292. int w = run_width(r);
  1293. x += r->gap;
  1294. if (l->draw) {
  1295. if (r->img >= 0) {
  1296. struct br_image *bi = &images[r->img];
  1297. if (bi->state == 1) {
  1298. struct img_blit b;
  1299. b.dst = l->ui->px;
  1300. b.dst_w = l->ui->w;
  1301. b.dst_h = l->ui->h;
  1302. b.clip_x = page_rect.x;
  1303. b.clip_y = page_rect.y;
  1304. b.clip_w = page_rect.w - 12;
  1305. b.clip_h = page_rect.h;
  1306. b.x = x;
  1307. b.y = l->line_y;
  1308. b.scale = ((unsigned int)r->iw << 16) /
  1309. bi->img.w;
  1310. b.bg = ui.theme.bg;
  1311. b.bg2 = ui.theme.bg;
  1312. b.flags = IMG_BLIT_OVER_DST;
  1313. img_draw(&bi->img, &b);
  1314. }
  1315. } else {
  1316. const char *s = r->has_lit ? r->lit :
  1317. doc.text + r->off;
  1318. unsigned int n = r->has_lit ? s_len(r->lit) :
  1319. r->len;
  1320. ngui_text_styled(l->ui, x, l->line_y, s, n,
  1321. r->color, r->scale, r->bold);
  1322. if (r->underline)
  1323. ngui_fill_rect(l->ui,
  1324. (struct ngui_rect){x,
  1325. l->line_y + 16 * r->scale - 2,
  1326. w, 1}, r->color);
  1327. }
  1328. if (r->href) {
  1329. if (nhits < HIT_MAX) {
  1330. struct hit *h = &hits[nhits];
  1331. int hh = r->img >= 0 ? r->ih :
  1332. 16 * r->scale;
  1333. int merge = nhits > 0 &&
  1334. hits[nhits - 1].node == r->href &&
  1335. hits[nhits - 1].y == l->line_y &&
  1336. hits[nhits - 1].h == hh &&
  1337. x <= hits[nhits - 1].x +
  1338. hits[nhits - 1].w + 2;
  1339. if (merge) {
  1340. struct hit *p = &hits[nhits - 1];
  1341. int end = x + w;
  1342. if (end > p->x + p->w)
  1343. p->w = end - p->x;
  1344. } else {
  1345. h->x = x;
  1346. h->y = l->line_y;
  1347. h->w = w;
  1348. h->h = hh;
  1349. h->node = r->href;
  1350. nhits++;
  1351. }
  1352. }
  1353. }
  1354. }
  1355. x += w;
  1356. }
  1357. l->y += l->line_h + 2;
  1358. l->nruns = 0;
  1359. l->line_x = l->left;
  1360. l->run_x = l->left;
  1361. l->line_h = 0;
  1362. l->space_pending = 0;
  1363. }
  1364. static void add_run(struct lay *l, unsigned short off, unsigned short len,
  1365. const struct br_style *st, unsigned short href,
  1366. const char *lit)
  1367. {
  1368. unsigned int n = lit ? s_len(lit) : len;
  1369. int w = (int)(n * (unsigned int)(8 * st->scale) +
  1370. (st->bold ? 1u : 0u));
  1371. if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0])))
  1372. flush_line(l);
  1373. if (l->nruns == 0) {
  1374. l->line_y = l->y;
  1375. l->line_h = 0;
  1376. }
  1377. {
  1378. struct lay_run *r = &l->runs[l->nruns++];
  1379. int gap = l->line_x - l->run_x;
  1380. if (gap < 0)
  1381. gap = 0;
  1382. r->gap = (unsigned short)gap;
  1383. r->off = off;
  1384. r->len = len;
  1385. r->color = st->color;
  1386. r->href = href;
  1387. r->has_lit = 0;
  1388. r->lit[0] = '\0';
  1389. if (lit && lit[0]) {
  1390. unsigned int i;
  1391. for (i = 0; i + 1 < sizeof(r->lit) && lit[i]; i++)
  1392. r->lit[i] = lit[i];
  1393. r->lit[i] = '\0';
  1394. r->has_lit = 1;
  1395. }
  1396. r->img = -1;
  1397. r->iw = r->ih = 0;
  1398. r->scale = st->scale;
  1399. r->bold = st->bold;
  1400. r->underline = st->underline;
  1401. }
  1402. l->line_x += w;
  1403. l->run_x = l->line_x;
  1404. {
  1405. int lh = 16 * st->scale;
  1406. if (st->line_h_px)
  1407. lh = st->line_h_px;
  1408. else if (st->line_h_pct)
  1409. lh = (16 * st->scale) * st->line_h_pct / 100;
  1410. if (lh < 16 * st->scale)
  1411. lh = 16 * st->scale;
  1412. if (l->line_h < lh)
  1413. l->line_h = lh;
  1414. }
  1415. }
  1416. static void add_image_run(struct lay *l, unsigned int node,
  1417. const struct br_style *st, unsigned short href)
  1418. {
  1419. struct br_image *bi = 0;
  1420. int idx = doc.nodes[node].img_index;
  1421. int dw, dh, maxw;
  1422. if (idx >= 0 && idx < image_count && images[idx].state == 1)
  1423. bi = &images[idx];
  1424. if (l->space_pending && (l->nruns > 0 || l->line_x > l->left)) {
  1425. if (l->line_x + 8 <= l->right)
  1426. l->line_x += 8;
  1427. }
  1428. l->space_pending = 0;
  1429. if (!bi) {
  1430. const char *alt = br_alt(&doc, node);
  1431. struct br_style gs = *st;
  1432. char tmp[96];
  1433. unsigned int n = 0;
  1434. int w;
  1435. gs.color = BR_GRAY;
  1436. gs.underline = 0;
  1437. s_append(tmp, &n, "[", sizeof(tmp));
  1438. s_append(tmp, &n, alt[0] ? alt : "image", sizeof(tmp));
  1439. s_append(tmp, &n, "]", sizeof(tmp));
  1440. w = (int)(n * (unsigned int)(8 * gs.scale));
  1441. if (l->line_x + w > l->right &&
  1442. (l->nruns > 0 || l->line_x > l->left))
  1443. flush_line(l);
  1444. add_run(l, 0, 0, &gs, href, tmp);
  1445. return;
  1446. }
  1447. maxw = l->right - l->left;
  1448. if (maxw < 8)
  1449. maxw = 8;
  1450. {
  1451. int iw = bi->native_w, ih = bi->native_h;
  1452. int have_w = 0, have_h = 0, css_w = 0, css_h = 0;
  1453. if (iw <= 0 || ih <= 0) {
  1454. iw = (int)bi->img.w;
  1455. ih = (int)bi->img.h;
  1456. }
  1457. dw = iw;
  1458. dh = ih;
  1459. if (doc.nodes[node].width_attr) {
  1460. int want = doc.nodes[node].width_attr;
  1461. dw = doc.nodes[node].width_pct ? maxw * want / 100 : want;
  1462. have_w = 1;
  1463. }
  1464. if (doc.nodes[node].height_attr) {
  1465. int want = doc.nodes[node].height_attr;
  1466. dh = doc.nodes[node].height_pct ? maxw * want / 100 : want;
  1467. have_h = 1;
  1468. }
  1469. if (have_w && !have_h)
  1470. dh = ih ? ih * dw / iw : 0;
  1471. if (have_h && !have_w)
  1472. dw = iw ? iw * dh / ih : 0;
  1473. if (st->width_kind) {
  1474. css_w = 1;
  1475. dw = st->width_kind == 2 ? maxw * (int)st->width / 100 :
  1476. (int)st->width;
  1477. }
  1478. if (st->height_kind) {
  1479. css_h = 1;
  1480. dh = st->height_kind == 2 ? maxw * (int)st->height / 100 :
  1481. (int)st->height;
  1482. }
  1483. if (css_w && !css_h)
  1484. dh = ih ? ih * dw / iw : 0;
  1485. if (css_h && !css_w)
  1486. dw = iw ? iw * dh / ih : 0;
  1487. if (st->max_width_kind) {
  1488. int mw = st->max_width_kind == 2 ?
  1489. maxw * (int)st->max_width / 100 :
  1490. (int)st->max_width;
  1491. if (mw > 0 && dw > mw) {
  1492. dh = iw ? ih * mw / iw : 0;
  1493. dw = mw;
  1494. }
  1495. }
  1496. }
  1497. if (dw < 8)
  1498. dw = 8;
  1499. if (dh < 8)
  1500. dh = 8;
  1501. if (dw > maxw) {
  1502. dh = dw ? dh * maxw / dw : dh;
  1503. dw = maxw;
  1504. }
  1505. if (l->line_x + dw > l->right && (l->nruns > 0 ||
  1506. l->line_x > l->left))
  1507. flush_line(l);
  1508. if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0])))
  1509. flush_line(l);
  1510. if (l->nruns == 0) {
  1511. l->line_y = l->y;
  1512. l->line_h = 0;
  1513. }
  1514. {
  1515. struct lay_run *r = &l->runs[l->nruns++];
  1516. int gap = l->line_x - l->run_x;
  1517. if (gap < 0)
  1518. gap = 0;
  1519. r->gap = (unsigned short)gap;
  1520. r->off = 0;
  1521. r->len = 0;
  1522. r->color = 0;
  1523. r->href = href;
  1524. r->has_lit = 0;
  1525. r->lit[0] = '\0';
  1526. r->img = (short)idx;
  1527. r->iw = (unsigned short)dw;
  1528. r->ih = (unsigned short)dh;
  1529. r->scale = 1;
  1530. r->bold = 0;
  1531. r->underline = 0;
  1532. }
  1533. l->line_x += dw;
  1534. l->run_x = l->line_x;
  1535. if (l->line_h < dh)
  1536. l->line_h = dh;
  1537. }
  1538. static void emit_text(struct lay *l, const char *s, unsigned int len,
  1539. const struct br_style *st, unsigned short href, int pre)
  1540. {
  1541. unsigned int i = 0;
  1542. unsigned int base;
  1543. int cw = 8 * st->scale;
  1544. int adv = cw + (st->bold ? 1 : 0);
  1545. if (!s || !len)
  1546. return;
  1547. base = (unsigned int)(s - doc.text);
  1548. if (st->uppercase) {
  1549. char *p = (char *)s;
  1550. unsigned int j;
  1551. for (j = 0; j < len; j++)
  1552. if (p[j] >= 'a' && p[j] <= 'z')
  1553. p[j] = (char)(p[j] - 'a' + 'A');
  1554. }
  1555. if (pre) {
  1556. while (i < len) {
  1557. unsigned int start;
  1558. if (s[i] == '\n') {
  1559. flush_line(l);
  1560. i++;
  1561. continue;
  1562. }
  1563. if (s[i] == ' ') {
  1564. if (l->line_x + cw > l->right &&
  1565. (l->nruns > 0 || l->line_x > l->left))
  1566. flush_line(l);
  1567. else
  1568. l->line_x += cw;
  1569. i++;
  1570. continue;
  1571. }
  1572. start = i;
  1573. while (i < len && s[i] != ' ' && s[i] != '\n')
  1574. i++;
  1575. {
  1576. unsigned int n = i - start;
  1577. int w = (int)(n * (unsigned int)adv);
  1578. if (l->line_x + w > l->right &&
  1579. (l->nruns > 0 || l->line_x > l->left))
  1580. flush_line(l);
  1581. if (w > l->right - l->left) {
  1582. int maxc = (l->right - l->left) / adv;
  1583. unsigned int off = start;
  1584. if (maxc < 1)
  1585. maxc = 1;
  1586. while (n > 0) {
  1587. unsigned int take = n >
  1588. (unsigned int)maxc ?
  1589. (unsigned int)maxc : n;
  1590. add_run(l, (unsigned short)(base + off),
  1591. (unsigned short)take,
  1592. st, href, 0);
  1593. off += take;
  1594. n -= take;
  1595. if (n)
  1596. flush_line(l);
  1597. }
  1598. } else {
  1599. add_run(l, (unsigned short)(base + start),
  1600. (unsigned short)n, st, href, 0);
  1601. }
  1602. }
  1603. }
  1604. return;
  1605. }
  1606. while (i < len) {
  1607. unsigned int start, n;
  1608. int w;
  1609. if (s[i] == ' ') {
  1610. l->space_pending = 1;
  1611. i++;
  1612. continue;
  1613. }
  1614. start = i;
  1615. while (i < len && s[i] != ' ')
  1616. i++;
  1617. n = i - start;
  1618. w = (int)(n * (unsigned int)adv);
  1619. if (l->space_pending &&
  1620. (l->nruns > 0 || l->line_x > l->left)) {
  1621. if (l->line_x + cw <= l->right)
  1622. l->line_x += cw;
  1623. }
  1624. l->space_pending = 0;
  1625. if (l->line_x + w > l->right &&
  1626. (l->nruns > 0 || l->line_x > l->left))
  1627. flush_line(l);
  1628. if (w > l->right - l->left) {
  1629. int maxc = (l->right - l->left) / adv;
  1630. unsigned int off = start;
  1631. if (maxc < 1)
  1632. maxc = 1;
  1633. while (n > 0) {
  1634. unsigned int take = n > (unsigned int)maxc ?
  1635. (unsigned int)maxc : n;
  1636. add_run(l, (unsigned short)(base + off),
  1637. (unsigned short)take, st, href, 0);
  1638. off += take;
  1639. n -= take;
  1640. if (n)
  1641. flush_line(l);
  1642. }
  1643. } else if (n) {
  1644. add_run(l, (unsigned short)(base + start),
  1645. (unsigned short)n, st, href, 0);
  1646. }
  1647. }
  1648. }
  1649. static int list_index(unsigned int parent, unsigned int node)
  1650. {
  1651. unsigned int n = 0, i;
  1652. for (i = doc.nodes[parent].first; i && i != node;
  1653. i = doc.nodes[i].next)
  1654. if (doc.nodes[i].tag == BR_TAG_LI)
  1655. n++;
  1656. return (int)n + 1;
  1657. }
  1658. static void layout_children(struct lay *l, unsigned int parent,
  1659. const struct br_style *pst, int depth,
  1660. unsigned short link);
  1661. static void layout_block(struct lay *l, unsigned int node,
  1662. const struct br_style *st, int depth,
  1663. unsigned short link)
  1664. {
  1665. int old_left = l->left, old_right = l->right, old_align = l->align;
  1666. int top, l0, r0;
  1667. if (depth > 24 || st->display == BR_DISPLAY_NONE)
  1668. return;
  1669. flush_line(l);
  1670. l->y += st->mt;
  1671. top = l->y;
  1672. l0 = old_left + st->ml;
  1673. r0 = old_right - st->mr;
  1674. if (r0 < l0 + 16)
  1675. r0 = l0 + 16;
  1676. if (l->draw && st->has_bg && block_h[node])
  1677. ngui_fill_rect(l->ui, (struct ngui_rect){l0, top, r0 - l0,
  1678. block_h[node]},
  1679. st->bg);
  1680. if (l->draw && st->border_w && block_h[node])
  1681. ngui_frame(l->ui, (struct ngui_rect){l0, top, r0 - l0,
  1682. block_h[node]},
  1683. st->border_color);
  1684. l->left = l0 + st->pl;
  1685. l->right = r0 - st->pr;
  1686. if (l->right < l->left + 8)
  1687. l->right = l->left + 8;
  1688. l->align = st->align;
  1689. l->y += st->pt;
  1690. if (doc.nodes[node].tag == BR_TAG_HR) {
  1691. if (l->draw)
  1692. ngui_fill_rect(l->ui, (struct ngui_rect){l->left,
  1693. l->y + 2, l->right - l->left, 1}, BR_GRAY);
  1694. l->y += 8;
  1695. } else if (doc.nodes[node].tag == BR_TAG_VIDEO) {
  1696. int vw = l->right - l->left;
  1697. int vh;
  1698. if (doc.nodes[node].width_pct)
  1699. vw = vw * doc.nodes[node].width_attr / 100;
  1700. else if (doc.nodes[node].width_attr)
  1701. vw = doc.nodes[node].width_attr;
  1702. if (vw > l->right - l->left)
  1703. vw = l->right - l->left;
  1704. if (vw < 8)
  1705. vw = 8;
  1706. vh = vw * 9 / 16;
  1707. if (vh > 240)
  1708. vh = 240;
  1709. if (vh < 80)
  1710. vh = 80;
  1711. if (l->draw) {
  1712. ngui_fill_rect(l->ui, (struct ngui_rect){l->left, l->y,
  1713. vw, vh}, 0xD69A);
  1714. ngui_frame(l->ui, (struct ngui_rect){l->left, l->y,
  1715. vw, vh}, 0x8410);
  1716. ngui_text(l->ui, l->left + 8, l->y + 8, "[video]",
  1717. 0x4208);
  1718. }
  1719. l->y += vh + 4;
  1720. } else {
  1721. if (doc.nodes[node].tag == BR_TAG_LI && !st->no_bullet) {
  1722. char *m = li_marker[node];
  1723. if (!m[0]) {
  1724. unsigned int parent = doc.nodes[node].parent;
  1725. if (parent &&
  1726. doc.nodes[parent].tag == BR_TAG_OL) {
  1727. int idx = list_index(parent, node);
  1728. unsigned int n = 0;
  1729. s_append_dec(m, &n, (unsigned int)idx, 6);
  1730. s_append(m, &n, ".", 6);
  1731. } else {
  1732. s_copy(m, "-", 6);
  1733. }
  1734. }
  1735. if (l->draw) {
  1736. int mw = (int)s_len(m) * 8;
  1737. ngui_text(l->ui, l->left - mw - 6, l->y, m,
  1738. st->color);
  1739. }
  1740. }
  1741. layout_children(l, node, st, depth + 1, link);
  1742. }
  1743. flush_line(l);
  1744. l->y += st->pb;
  1745. l->left = old_left;
  1746. l->right = old_right;
  1747. l->align = old_align;
  1748. l->y += st->mb;
  1749. if (!l->draw) {
  1750. int h = l->y - top;
  1751. if (h < 0)
  1752. h = 0;
  1753. if (h > 65535)
  1754. h = 65535;
  1755. block_h[node] = (unsigned short)h;
  1756. }
  1757. }
  1758. static int text_node_blank(const struct br_node *nd)
  1759. {
  1760. const char *t = doc.text + nd->text_off;
  1761. unsigned int i;
  1762. for (i = 0; i < nd->text_len; i++)
  1763. if (t[i] != ' ' && t[i] != '\t' && t[i] != '\n' &&
  1764. t[i] != '\r')
  1765. return 0;
  1766. return 1;
  1767. }
  1768. /* Rough shrink-to-fit width for a float without an explicit width: sum the
  1769. * text/image widths in the subtree, plus padding and margins. This keeps
  1770. * Bootstrap's floated nav items on one line instead of giving each the whole
  1771. * row. */
  1772. static int intrinsic_width(unsigned int node, const struct br_style *pst,
  1773. int depth)
  1774. {
  1775. unsigned int c;
  1776. int w = 0;
  1777. if (depth > 12)
  1778. return 0;
  1779. for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
  1780. struct br_node *nd = &doc.nodes[c];
  1781. struct br_style cs;
  1782. if (nd->type == BR_NODE_TEXT) {
  1783. w += (int)nd->text_len * (8 * pst->scale) +
  1784. (pst->bold ? 1 : 0);
  1785. continue;
  1786. }
  1787. br_style_for(&doc, c, pst, &cs);
  1788. if (cs.display == BR_DISPLAY_NONE)
  1789. continue;
  1790. if (cs.float_left)
  1791. w += 8;
  1792. if (nd->tag == BR_TAG_BR) {
  1793. w += 8;
  1794. continue;
  1795. }
  1796. if (nd->tag == BR_TAG_IMG) {
  1797. int idx = nd->img_index;
  1798. if (idx >= 0 && idx < image_count &&
  1799. images[idx].state == 1)
  1800. w += (int)images[idx].img.w;
  1801. else if (nd->width_attr)
  1802. w += nd->width_attr;
  1803. else
  1804. w += 64;
  1805. continue;
  1806. }
  1807. w += cs.pl + cs.pr + cs.ml + cs.mr;
  1808. w += intrinsic_width(c, &cs, depth + 1);
  1809. if (w > 4096)
  1810. break;
  1811. }
  1812. return w;
  1813. }
  1814. /* Lay consecutive floated block children side by side. Bootstrap's grid is
  1815. * the main user: .row > .col-md-* with float:left and a percentage width.
  1816. * Each column is laid out with its own left/right edge and the row advances
  1817. * to the tallest column. Returns the first child not consumed. */
  1818. static unsigned int layout_float_group(struct lay *l, unsigned int first,
  1819. const struct br_style *pst, int depth,
  1820. unsigned short link)
  1821. {
  1822. unsigned int n = first;
  1823. int row_left = l->left, row_right = l->right;
  1824. int row_top, row_bottom, x, any = 0;
  1825. flush_line(l);
  1826. row_top = l->y;
  1827. row_bottom = l->y;
  1828. x = row_left;
  1829. while (n) {
  1830. struct br_node *nd = &doc.nodes[n];
  1831. struct br_style cs;
  1832. int colw;
  1833. if (nd->type == BR_NODE_TEXT) {
  1834. if (text_node_blank(nd)) {
  1835. n = nd->next;
  1836. continue;
  1837. }
  1838. break;
  1839. }
  1840. br_style_for(&doc, n, pst, &cs);
  1841. if (cs.display == BR_DISPLAY_NONE) {
  1842. n = nd->next;
  1843. continue;
  1844. }
  1845. if (!cs.float_left)
  1846. break;
  1847. if (cs.width_kind == 2)
  1848. colw = (row_right - row_left) * (int)cs.width / 100;
  1849. else if (cs.width_kind == 1)
  1850. colw = (int)cs.width;
  1851. else {
  1852. colw = intrinsic_width(n, &cs, 0) +
  1853. cs.pl + cs.pr + cs.ml + cs.mr + 8;
  1854. if (colw < 8)
  1855. colw = 8;
  1856. }
  1857. if (colw < 8)
  1858. colw = 8;
  1859. if (colw > row_right - row_left)
  1860. colw = row_right - row_left;
  1861. if (any && x + colw > row_right) {
  1862. row_top = row_bottom;
  1863. row_bottom = row_top;
  1864. x = row_left;
  1865. }
  1866. l->left = x;
  1867. l->right = x + colw;
  1868. if (l->right < l->left + 8)
  1869. l->right = l->left + 8;
  1870. l->y = row_top;
  1871. l->line_x = l->left;
  1872. l->run_x = l->left;
  1873. l->line_y = l->y;
  1874. l->line_h = 0;
  1875. l->nruns = 0;
  1876. l->space_pending = 0;
  1877. l->align = cs.align;
  1878. layout_block(l, n, &cs, depth + 1, link);
  1879. if (l->y > row_bottom)
  1880. row_bottom = l->y;
  1881. x += colw;
  1882. any++;
  1883. n = nd->next;
  1884. }
  1885. l->left = row_left;
  1886. l->right = row_right;
  1887. l->y = row_bottom;
  1888. l->line_x = l->left;
  1889. l->run_x = l->left;
  1890. l->line_y = l->y;
  1891. l->line_h = 0;
  1892. l->nruns = 0;
  1893. l->space_pending = 0;
  1894. return n;
  1895. }
  1896. static void layout_children(struct lay *l, unsigned int parent,
  1897. const struct br_style *pst, int depth,
  1898. unsigned short link)
  1899. {
  1900. unsigned int n;
  1901. if (depth > 24)
  1902. return;
  1903. n = doc.nodes[parent].first;
  1904. while (n) {
  1905. struct br_node *nd = &doc.nodes[n];
  1906. struct br_style cs;
  1907. unsigned short lk = link;
  1908. if (nd->type == BR_NODE_TEXT) {
  1909. emit_text(l, doc.text + nd->text_off, nd->text_len, pst,
  1910. link, (nd->flags & BR_NODE_PREFORMAT) != 0);
  1911. n = nd->next;
  1912. continue;
  1913. }
  1914. br_style_for(&doc, n, pst, &cs);
  1915. if (cs.display == BR_DISPLAY_NONE) {
  1916. n = nd->next;
  1917. continue;
  1918. }
  1919. if (nd->tag == BR_TAG_A && nd->href_off)
  1920. lk = (unsigned short)n;
  1921. if (nd->tag == BR_TAG_BR) {
  1922. flush_line(l);
  1923. n = nd->next;
  1924. continue;
  1925. }
  1926. if (nd->tag == BR_TAG_HR) {
  1927. flush_line(l);
  1928. if (l->draw)
  1929. ngui_fill_rect(l->ui,
  1930. (struct ngui_rect){l->left, l->y + 2,
  1931. l->right - l->left, 1}, BR_GRAY);
  1932. l->y += 8;
  1933. n = nd->next;
  1934. continue;
  1935. }
  1936. if (nd->tag == BR_TAG_IMG) {
  1937. add_image_run(l, n, &cs, lk);
  1938. n = nd->next;
  1939. continue;
  1940. }
  1941. if (nd->tag == BR_TAG_VIDEO) {
  1942. layout_block(l, n, &cs, depth + 1, lk);
  1943. n = nd->next;
  1944. continue;
  1945. }
  1946. if (cs.float_left) {
  1947. n = layout_float_group(l, n, pst, depth, link);
  1948. continue;
  1949. }
  1950. if (cs.display == BR_DISPLAY_BLOCK || br_tag_is_block(nd->tag))
  1951. layout_block(l, n, &cs, depth + 1, lk);
  1952. else {
  1953. layout_children(l, n, &cs, depth + 1, lk);
  1954. if (nd->tag == BR_TAG_TD || nd->tag == BR_TAG_TH)
  1955. l->space_pending = 1;
  1956. }
  1957. n = nd->next;
  1958. }
  1959. }
  1960. /* One layout state, kept out of the 8 KB user stack: it holds 96 line
  1961. * runs and a recursive page can otherwise overflow the stack. */
  1962. static struct lay layout_state;
  1963. static int measure_page(int left, int right)
  1964. {
  1965. struct lay *l = &layout_state;
  1966. struct br_style base = base_style();
  1967. unsigned int i;
  1968. for (i = 0; i < BR_MAX_NODES; i++)
  1969. block_h[i] = 0;
  1970. l->draw = 0;
  1971. l->ui = 0;
  1972. l->left = left;
  1973. l->right = right;
  1974. l->y = 0;
  1975. l->line_x = left;
  1976. l->run_x = left;
  1977. l->line_y = 0;
  1978. l->line_h = 0;
  1979. l->align = BR_ALIGN_LEFT;
  1980. l->depth = 0;
  1981. l->nruns = 0;
  1982. l->space_pending = 0;
  1983. layout_children(l, doc.root, &base, 0, 0);
  1984. return l->y + 8;
  1985. }
  1986. static void render_page(int left, int right, int y0)
  1987. {
  1988. struct lay *l = &layout_state;
  1989. struct br_style base = base_style();
  1990. l->draw = 1;
  1991. l->ui = &ui;
  1992. l->left = left;
  1993. l->right = right;
  1994. l->y = y0;
  1995. l->line_x = left;
  1996. l->run_x = left;
  1997. l->line_y = y0;
  1998. l->line_h = 0;
  1999. l->align = BR_ALIGN_LEFT;
  2000. l->depth = 0;
  2001. l->nruns = 0;
  2002. l->space_pending = 0;
  2003. nhits = 0;
  2004. layout_children(l, doc.root, &base, 0, 0);
  2005. }
  2006. /* ---- UI ---- */
  2007. static int find_hit(int x, int y)
  2008. {
  2009. int i;
  2010. for (i = nhits - 1; i >= 0; i--) {
  2011. struct hit *h = &hits[i];
  2012. if (x >= h->x && y >= h->y && x < h->x + h->w &&
  2013. y < h->y + h->h)
  2014. return (int)h->node;
  2015. }
  2016. return 0;
  2017. }
  2018. static void draw_toolbar(struct ngui_rect r, unsigned int *action)
  2019. {
  2020. int x = r.x + 4, y = r.y + 4, h = 22;
  2021. int compact = r.w < 560;
  2022. int go_w = r.w < 420 ? 32 : 42;
  2023. int in_x, in_w, res;
  2024. ngui_fill_rect(&ui, r, ui.theme.panel);
  2025. ngui_fill_rect(&ui, (struct ngui_rect){r.x, r.y + r.h - 1, r.w, 1},
  2026. ui.theme.border);
  2027. if (ngui_icon_button(&ui, ID_BACK, (struct ngui_rect){x, y, 28, h},
  2028. NGUI_ICON_CHEVRON_LEFT))
  2029. *action |= ACT_BACK;
  2030. x += 32;
  2031. if (!compact) {
  2032. if (ngui_icon_button(&ui, ID_FORWARD,
  2033. (struct ngui_rect){x, y, 28, h},
  2034. NGUI_ICON_CHEVRON_RIGHT))
  2035. *action |= ACT_FORWARD;
  2036. x += 32;
  2037. }
  2038. if (ngui_icon_button(&ui, ID_RELOAD, (struct ngui_rect){x, y, 28, h},
  2039. NGUI_ICON_REFRESH))
  2040. *action |= ACT_RELOAD;
  2041. x += 32;
  2042. if (!compact) {
  2043. if (ngui_icon_button(&ui, ID_HOME,
  2044. (struct ngui_rect){x, y, 28, h},
  2045. NGUI_ICON_HOME))
  2046. *action |= ACT_HOME;
  2047. x += 32;
  2048. }
  2049. in_x = x + 2;
  2050. in_w = r.x + r.w - 6 - go_w - 4 - in_x;
  2051. if (in_w < 60)
  2052. in_w = 60;
  2053. res = ngui_text_input(&ui, ID_URL,
  2054. (struct ngui_rect){in_x, y, in_w, h}, &url_in);
  2055. if (res & NGUI_INPUT_SUBMIT)
  2056. *action |= ACT_NAV;
  2057. if (res & NGUI_INPUT_CANCEL)
  2058. ngui_set_focus(&ui, 0);
  2059. if (ngui_button(&ui, ID_GO,
  2060. (struct ngui_rect){r.x + r.w - 4 - go_w, y, go_w, h},
  2061. "Go"))
  2062. *action |= ACT_NAV;
  2063. }
  2064. static int page_hover_node(void)
  2065. {
  2066. if (!ui.inside)
  2067. return 0;
  2068. if (ui.mouse_x < page_rect.x ||
  2069. ui.mouse_x >= page_rect.x + page_rect.w - 12 ||
  2070. ui.mouse_y < page_rect.y ||
  2071. ui.mouse_y >= page_rect.y + page_rect.h)
  2072. return 0;
  2073. return find_hit(ui.mouse_x, ui.mouse_y);
  2074. }
  2075. static void draw_page(struct ngui_rect page)
  2076. {
  2077. int left, right, ch;
  2078. int old_panel, old_dim;
  2079. page_rect = page;
  2080. ngui_fill_rect(&ui, page, ui.theme.bg);
  2081. if (!doc.nnode || (page_loading && doc.nnode <= 1)) {
  2082. const char *s = page_loading ? "Loading..." : "No page";
  2083. int w = (int)ngui_text_width(s);
  2084. ngui_text(&ui, page.x + (page.w - w) / 2,
  2085. page.y + page.h / 2 - 8, s, ui.theme.text_dim);
  2086. page_scroll.content_h = 0;
  2087. page_scroll.view_h = page.h;
  2088. return;
  2089. }
  2090. if (page.w < 40 || page.h < 40)
  2091. return;
  2092. left = page.x + 8;
  2093. right = page.x + page.w - 16;
  2094. ch = measure_page(left, right);
  2095. page_scroll.content_h = ch;
  2096. page_scroll.view_h = page.h;
  2097. ngui_scroll_begin(&ui, page, &page_scroll);
  2098. render_page(left, right, page.y + 4 - page_scroll.offset);
  2099. old_panel = ui.theme.panel_alt;
  2100. old_dim = ui.theme.text_dim;
  2101. ui.theme.panel_alt = 0xC618;
  2102. ui.theme.text_dim = 0x8410;
  2103. ngui_scroll_end(&ui, page, &page_scroll);
  2104. ui.theme.panel_alt = old_panel;
  2105. ui.theme.text_dim = old_dim;
  2106. }
  2107. static void draw_status(struct ngui_rect r)
  2108. {
  2109. char msg[BR_URL_MAX + 32];
  2110. unsigned int n = 0;
  2111. int hover = page_hover_node();
  2112. unsigned short color;
  2113. const char *kind;
  2114. ngui_fill_rect(&ui, r, ui.theme.panel);
  2115. ngui_fill_rect(&ui, (struct ngui_rect){r.x, r.y, r.w, 1},
  2116. ui.theme.border);
  2117. if (hover) {
  2118. const char *href = br_href(&doc, (unsigned int)hover);
  2119. char url[BR_URL_MAX];
  2120. if (resolve_link(page_url, href, url, sizeof(url)) == 0) {
  2121. s_append(msg, &n, "Link: ", sizeof(msg));
  2122. s_append(msg, &n, url, sizeof(msg));
  2123. } else {
  2124. s_copy(msg, "Link", sizeof(msg));
  2125. }
  2126. color = ui.theme.accent;
  2127. } else if (status_text[0]) {
  2128. s_copy(msg, status_text, sizeof(msg));
  2129. color = status_error ? ui.theme.error : ui.theme.text;
  2130. } else if (page_title[0]) {
  2131. s_copy(msg, page_title, sizeof(msg));
  2132. color = ui.theme.text;
  2133. } else {
  2134. s_copy(msg, "Ready", sizeof(msg));
  2135. color = ui.theme.text_dim;
  2136. }
  2137. kind = s_eq(page_url, "about:home") ? "HOME" :
  2138. page_is_local ? "FILE" :
  2139. (starts_with(page_url, "https://") ? "HTTPS" : "HTTP");
  2140. ngui_text_ellipsis(&ui, r, r.x + 6, r.y + 1, msg, color);
  2141. {
  2142. int kw = (int)ngui_text_width(kind);
  2143. ngui_text(&ui, r.x + r.w - 6 - kw, r.y + 1, kind,
  2144. ui.theme.text_dim);
  2145. }
  2146. }
  2147. static void draw(unsigned int *action)
  2148. {
  2149. struct ngui_rect r = {0, 0, ui.w, ui.h};
  2150. struct ngui_rect toolbar, status, page;
  2151. toolbar = ngui_cut_top(&r, BR_TOOLBAR_H);
  2152. status = ngui_cut_bottom(&r, BR_STATUS_H);
  2153. page = r;
  2154. draw_toolbar(toolbar, action);
  2155. draw_page(page);
  2156. draw_status(status);
  2157. }
  2158. static void redraw_now(void)
  2159. {
  2160. unsigned int action = 0;
  2161. ngui_begin(&ui);
  2162. draw(&action);
  2163. ngui_end(&ui);
  2164. }
  2165. static void handle_keys(unsigned int *action)
  2166. {
  2167. int k;
  2168. if (ngui_focus(&ui))
  2169. return;
  2170. if (ngui_scroll_keys(&ui, &page_scroll))
  2171. ngui_invalidate(&ui);
  2172. while ((k = ngui_key(&ui)) != 0) {
  2173. switch (k) {
  2174. case NGUI_KEY_ESC:
  2175. *action |= ACT_CLOSE;
  2176. break;
  2177. case NGUI_KEY_TAB:
  2178. case 0x0C: /* Ctrl+L */
  2179. ngui_set_focus(&ui, ID_URL);
  2180. break;
  2181. case 0x12: /* Ctrl+R */
  2182. *action |= ACT_RELOAD;
  2183. break;
  2184. case NGUI_KEY_BACKSPACE:
  2185. *action |= ACT_BACK;
  2186. break;
  2187. case NGUI_KEY_UP:
  2188. ngui_scroll_to(&page_scroll,
  2189. page_scroll.offset - 24);
  2190. ngui_invalidate(&ui);
  2191. break;
  2192. case NGUI_KEY_DOWN:
  2193. ngui_scroll_to(&page_scroll,
  2194. page_scroll.offset + 24);
  2195. ngui_invalidate(&ui);
  2196. break;
  2197. case NGUI_KEY_HOME:
  2198. ngui_scroll_to(&page_scroll, 0);
  2199. ngui_invalidate(&ui);
  2200. break;
  2201. case NGUI_KEY_END:
  2202. ngui_scroll_to(&page_scroll,
  2203. page_scroll.content_h -
  2204. page_scroll.view_h);
  2205. ngui_invalidate(&ui);
  2206. break;
  2207. default:
  2208. break;
  2209. }
  2210. }
  2211. }
  2212. static void handle_page_click(unsigned int *action)
  2213. {
  2214. int node;
  2215. char url[BR_URL_MAX];
  2216. if (!ui.pressed || !ui.inside)
  2217. return;
  2218. if (ui.press_x < page_rect.x ||
  2219. ui.press_x >= page_rect.x + page_rect.w - 12 ||
  2220. ui.press_y < page_rect.y ||
  2221. ui.press_y >= page_rect.y + page_rect.h)
  2222. return;
  2223. node = find_hit(ui.press_x, ui.press_y);
  2224. if (!node)
  2225. return;
  2226. if (resolve_link(page_url, br_href(&doc, (unsigned int)node), url,
  2227. sizeof(url)) != 0)
  2228. return;
  2229. s_copy(click_url, url, sizeof(click_url));
  2230. *action |= ACT_NAV;
  2231. }
  2232. void _start(const char *arg)
  2233. {
  2234. struct win_info wi;
  2235. if (u_win_create(BR_W, BR_H, "Browser", &wi) != 0) {
  2236. u_puts("browser: no graphics window\n");
  2237. u_exit(1);
  2238. }
  2239. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  2240. url_in.placeholder = "http://...";
  2241. br_doc_init(&doc);
  2242. br_set_theme(ui.theme.text, ui.theme.bg, ui.theme.panel,
  2243. ui.theme.accent);
  2244. br_set_viewport(BR_VIEWPORT_W);
  2245. if (u_getcwd(cwd, sizeof(cwd)) != 0 || !cwd[0])
  2246. s_copy(cwd, "/", sizeof(cwd));
  2247. heap_clear();
  2248. page_scroll.offset = 0;
  2249. page_scroll.content_h = 0;
  2250. if (arg && arg[0])
  2251. navigate(arg, 1);
  2252. else
  2253. load_home(1);
  2254. for (;;) {
  2255. unsigned int action = 0;
  2256. unsigned int now;
  2257. click_url[0] = '\0';
  2258. now = u_ticks_ms();
  2259. if (status_until && (int)(now - status_until) >= 0) {
  2260. status_text[0] = '\0';
  2261. status_until = 0;
  2262. status_error = 0;
  2263. ngui_invalidate(&ui);
  2264. }
  2265. if (!ngui_begin(&ui)) {
  2266. u_sleep_ms(10);
  2267. continue;
  2268. }
  2269. draw(&action);
  2270. handle_keys(&action);
  2271. handle_page_click(&action);
  2272. ngui_end(&ui);
  2273. if (action & ACT_NAV) {
  2274. if (click_url[0]) {
  2275. navigate(click_url, 1);
  2276. click_url[0] = '\0';
  2277. } else {
  2278. navigate(url_in.text, 1);
  2279. }
  2280. }
  2281. if (action & ACT_BACK) {
  2282. if (hist_pos > 0) {
  2283. hist_pos--;
  2284. navigate(history[hist_pos], 0);
  2285. }
  2286. }
  2287. if (action & ACT_FORWARD) {
  2288. if (hist_pos + 1 < hist_len) {
  2289. hist_pos++;
  2290. navigate(history[hist_pos], 0);
  2291. }
  2292. }
  2293. if (action & ACT_RELOAD) {
  2294. if (s_eq(page_url, "about:home"))
  2295. load_home(0);
  2296. else
  2297. navigate(page_url, 0);
  2298. }
  2299. if (action & ACT_HOME)
  2300. load_home(1);
  2301. if (action & ACT_CLOSE)
  2302. quit = 1;
  2303. if (quit)
  2304. break;
  2305. u_sleep_ms(10);
  2306. }
  2307. u_win_close();
  2308. u_exit(0);
  2309. }