browser.c 57 KB

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