browser.c 60 KB

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