browser.c 60 KB

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