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