browser.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841
  1. /* browser: a small HTML/CSS browser built on ngui, the image library and the
  2. * shared uhttp client.
  3. *
  4. * No JavaScript, no forms, no external CSS; images are decoded with img.c and
  5. * drawn inline. Large buffers (page body, compressed body, image files and
  6. * decode arenas) live in the per-process SYS_MEM_ALLOC heap; the DOM itself
  7. * uses the fixed arenas in browser_html.c.
  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 720
  16. #define BR_H 520
  17. #define BR_TOOLBAR_H 30
  18. #define BR_STATUS_H 18
  19. #define BR_HEAP_INIT (6u * 1024u * 1024u)
  20. #define BR_HEAP_MAX (28u * 1024u * 1024u)
  21. #define BR_PAGE_MAX (512u * 1024u)
  22. #define BR_IMG_FILE_MAX (4u * 1024u * 1024u)
  23. #define BR_IMG_PIX_MAX (4u * 1024u * 1024u)
  24. #define BR_MAX_IMAGES 6
  25. #define BR_HISTORY 16
  26. #define BR_URL_MAX 320
  27. #define BR_STATUS_MAX 200
  28. #define BR_IMG_RGB565(r, g, b) \
  29. ((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3)))
  30. #define BR_WHITE 0xFFFF
  31. #define BR_BLACK 0x0000
  32. #define BR_GRAY 0x8410
  33. #define BR_LINK 0x001B
  34. enum {
  35. ID_URL = 1,
  36. ID_GO,
  37. ID_BACK,
  38. ID_FORWARD,
  39. ID_RELOAD,
  40. ID_HOME
  41. };
  42. #define ACT_NAV (1u << 0)
  43. #define ACT_BACK (1u << 1)
  44. #define ACT_FORWARD (1u << 2)
  45. #define ACT_RELOAD (1u << 3)
  46. #define ACT_HOME (1u << 4)
  47. #define ACT_CLOSE (1u << 5)
  48. struct br_image {
  49. int state; /* 0 empty, 1 ready */
  50. struct img_image img;
  51. char err[32];
  52. };
  53. static struct ngui ui;
  54. static struct ngui_input url_in;
  55. static struct ngui_scroll page_scroll;
  56. static struct br_doc doc;
  57. static struct br_image images[BR_MAX_IMAGES];
  58. static int image_count;
  59. static unsigned char *heap;
  60. static unsigned int heap_size = 0;
  61. static unsigned int heap_used = 0;
  62. static char page_url[BR_URL_MAX];
  63. static char page_title[BR_TITLE_MAX];
  64. static char cwd[64];
  65. static int page_is_local;
  66. static int page_loading;
  67. static char status_text[BR_STATUS_MAX];
  68. static unsigned int status_until;
  69. static int status_error;
  70. static char history[BR_HISTORY][BR_URL_MAX];
  71. static int hist_len, hist_pos;
  72. static char click_url[BR_URL_MAX];
  73. static int quit;
  74. static struct ngui_rect page_rect;
  75. /* layout output */
  76. struct lay_run {
  77. unsigned short off, len;
  78. unsigned short gap;
  79. unsigned short color;
  80. unsigned short href;
  81. char lit[28];
  82. short img;
  83. unsigned short iw, ih;
  84. unsigned char scale, bold, underline;
  85. unsigned char has_lit;
  86. };
  87. struct lay {
  88. int draw;
  89. struct ngui *ui;
  90. int left, right;
  91. int y;
  92. int line_x, line_y, line_h;
  93. int run_x;
  94. int align;
  95. int depth;
  96. int space_pending;
  97. struct lay_run runs[96];
  98. int nruns;
  99. };
  100. struct hit {
  101. int x, y, w, h;
  102. unsigned short node;
  103. };
  104. #define HIT_MAX 96
  105. static struct hit hits[HIT_MAX];
  106. static int nhits;
  107. static unsigned short block_h[BR_MAX_NODES];
  108. static char li_marker[BR_MAX_NODES][6];
  109. /* one connection object; the browser fetches one resource at a time */
  110. static struct uhttp http;
  111. /* ---- tiny strings ---- */
  112. static unsigned int s_len(const char *s)
  113. {
  114. unsigned int n = 0;
  115. while (s && s[n])
  116. n++;
  117. return n;
  118. }
  119. static void s_copy(char *d, const char *s, unsigned int cap)
  120. {
  121. unsigned int i = 0;
  122. if (!cap)
  123. return;
  124. while (s && s[i] && i + 1 < cap) {
  125. d[i] = s[i];
  126. i++;
  127. }
  128. d[i] = '\0';
  129. }
  130. static void s_append(char *d, unsigned int *n, const char *s, unsigned int cap)
  131. {
  132. while (s && *s && *n + 1 < cap)
  133. d[(*n)++] = *s++;
  134. d[*n] = '\0';
  135. }
  136. static void s_append_dec(char *d, unsigned int *n, unsigned int v,
  137. unsigned int cap)
  138. {
  139. char tmp[12];
  140. unsigned int i = 0, j;
  141. do {
  142. tmp[i++] = (char)('0' + v % 10);
  143. v /= 10;
  144. } while (v && i < sizeof(tmp));
  145. for (j = 0; j < i && *n + 1 < cap; j++)
  146. d[(*n)++] = tmp[i - 1 - j];
  147. d[*n] = '\0';
  148. }
  149. static int s_eq(const char *a, const char *b)
  150. {
  151. while (*a && *b) {
  152. if (*a != *b)
  153. return 0;
  154. a++;
  155. b++;
  156. }
  157. return *a == *b;
  158. }
  159. static int starts_with(const char *s, const char *p)
  160. {
  161. unsigned int i = 0;
  162. while (p[i]) {
  163. if (s[i] != p[i])
  164. return 0;
  165. i++;
  166. }
  167. return 1;
  168. }
  169. static int contains_ci(const char *s, const char *p)
  170. {
  171. unsigned int i, j;
  172. for (i = 0; s[i]; i++) {
  173. for (j = 0; p[j]; j++) {
  174. char a = s[i + j];
  175. if (a >= 'A' && a <= 'Z')
  176. a = (char)(a - 'A' + 'a');
  177. if (a != p[j])
  178. break;
  179. }
  180. if (!p[j])
  181. return 1;
  182. }
  183. return 0;
  184. }
  185. static char *trim(char *s)
  186. {
  187. unsigned int n;
  188. while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')
  189. s++;
  190. n = s_len(s);
  191. while (n && (s[n - 1] == ' ' || s[n - 1] == '\t' ||
  192. s[n - 1] == '\r' || s[n - 1] == '\n'))
  193. s[--n] = '\0';
  194. return s;
  195. }
  196. /* ---- per-process heap ---- */
  197. static int heap_start(void)
  198. {
  199. heap = (unsigned char *)u_mem_alloc(BR_HEAP_INIT);
  200. if (!heap) {
  201. heap_size = heap_used = 0;
  202. return -1;
  203. }
  204. heap_size = BR_HEAP_INIT;
  205. heap_used = 0;
  206. return 0;
  207. }
  208. static void heap_clear(void)
  209. {
  210. u_mem_free();
  211. heap = 0;
  212. heap_size = heap_used = 0;
  213. }
  214. static void *heap_alloc(unsigned int bytes, unsigned int align)
  215. {
  216. unsigned int start, want;
  217. if (!heap || bytes == 0)
  218. return 0;
  219. if (align < 1)
  220. align = 1;
  221. if (align & (align - 1))
  222. return 0;
  223. start = (heap_used + align - 1) & ~(align - 1);
  224. if (start > BR_HEAP_MAX || bytes > BR_HEAP_MAX - start)
  225. return 0;
  226. if (start + bytes > heap_size) {
  227. want = heap_size ? heap_size : BR_HEAP_INIT;
  228. while (want < start + bytes) {
  229. if (want > BR_HEAP_MAX / 2) {
  230. want = BR_HEAP_MAX;
  231. break;
  232. }
  233. want *= 2;
  234. }
  235. if (want < start + bytes || want > BR_HEAP_MAX)
  236. return 0;
  237. if (u_mem_grow(want) != 0)
  238. return 0;
  239. heap_size = want;
  240. }
  241. heap_used = start + bytes;
  242. return heap + start;
  243. }
  244. /* ---- status ---- */
  245. static void set_status(const char *s, int err, unsigned int ttl_ms)
  246. {
  247. s_copy(status_text, s, sizeof(status_text));
  248. status_error = err;
  249. status_until = ttl_ms ? u_ticks_ms() + ttl_ms : 0;
  250. ngui_invalidate(&ui);
  251. }
  252. /* ---- images ---- */
  253. static void image_reset(void)
  254. {
  255. int i;
  256. for (i = 0; i < BR_MAX_IMAGES; i++) {
  257. images[i].state = 0;
  258. images[i].err[0] = '\0';
  259. }
  260. image_count = 0;
  261. }
  262. static int decode_image_data(const unsigned char *data, unsigned int len,
  263. struct br_image *bi)
  264. {
  265. struct img_info info;
  266. struct img_options opt;
  267. struct img_mem mem;
  268. void *arena;
  269. unsigned int est;
  270. int r;
  271. if (img_sniff(data, len, &info) != 0) {
  272. s_copy(bi->err, "unknown image format", sizeof(bi->err));
  273. return -1;
  274. }
  275. est = img_estimate(&info);
  276. if (est == 0xFFFFFFFFu || est > BR_HEAP_MAX / 2) {
  277. s_copy(bi->err, "image too large", sizeof(bi->err));
  278. return -1;
  279. }
  280. est += 64u * 1024u;
  281. arena = heap_alloc(est, 16);
  282. if (!arena) {
  283. s_copy(bi->err, "out of memory", sizeof(bi->err));
  284. return -1;
  285. }
  286. img_mem_init(&mem, arena, est);
  287. opt.max_pixels = BR_IMG_PIX_MAX;
  288. r = img_decode(data, len, &opt, &mem, &bi->img);
  289. if (r != 0) {
  290. s_copy(bi->err, img_error_string(r), sizeof(bi->err));
  291. return -1;
  292. }
  293. bi->state = 1;
  294. return 0;
  295. }
  296. /* ---- URL / local path handling ---- */
  297. static int is_http(const char *s)
  298. {
  299. return starts_with(s, "http://") || starts_with(s, "https://");
  300. }
  301. /* Collapse "/./", "/../" and duplicate slashes in an absolute path. */
  302. static int normalize_abs(const char *in, char *out, unsigned int cap)
  303. {
  304. char seg[64];
  305. unsigned int i = 0, o = 1;
  306. int depth = 0;
  307. int trailing;
  308. if (!in || in[0] != '/' || cap < 2)
  309. return -1;
  310. trailing = 0;
  311. {
  312. unsigned int n = s_len(in);
  313. if (n > 1 && in[n - 1] == '/')
  314. trailing = 1;
  315. }
  316. out[0] = '/';
  317. while (in[i]) {
  318. unsigned int n = 0;
  319. while (in[i] == '/')
  320. i++;
  321. while (in[i] && in[i] != '/' && in[i] != '?' && in[i] != '#') {
  322. if (n + 1 < sizeof(seg))
  323. seg[n++] = in[i];
  324. i++;
  325. }
  326. seg[n] = '\0';
  327. if (n == 0)
  328. break;
  329. if (s_eq(seg, "."))
  330. continue;
  331. if (s_eq(seg, "..")) {
  332. if (depth > 0) {
  333. while (o > 1 && out[o - 1] != '/')
  334. o--;
  335. if (o > 1)
  336. o--;
  337. depth--;
  338. }
  339. continue;
  340. }
  341. if (o > 1) {
  342. if (o + 1 >= cap)
  343. return -1;
  344. out[o++] = '/';
  345. }
  346. if (o + n + 1 >= cap)
  347. return -1;
  348. {
  349. unsigned int j;
  350. for (j = 0; j < n; j++)
  351. out[o++] = seg[j];
  352. }
  353. depth++;
  354. }
  355. if (trailing && o > 1) {
  356. if (o + 1 >= cap)
  357. return -1;
  358. out[o++] = '/';
  359. }
  360. out[o] = '\0';
  361. return 0;
  362. }
  363. static void make_abs_local(const char *in, char *out, unsigned int cap)
  364. {
  365. char joined[BR_URL_MAX];
  366. if (!in || !in[0]) {
  367. s_copy(out, cwd[0] ? cwd : "/", cap);
  368. return;
  369. }
  370. if (in[0] == '/') {
  371. if (normalize_abs(in, out, cap) != 0)
  372. s_copy(out, in, cap);
  373. return;
  374. }
  375. {
  376. unsigned int n = 0;
  377. s_append(joined, &n, cwd[0] ? cwd : "/", sizeof(joined));
  378. s_append(joined, &n, "/", sizeof(joined));
  379. s_append(joined, &n, in, sizeof(joined));
  380. }
  381. if (normalize_abs(joined, out, cap) != 0)
  382. s_copy(out, joined, cap);
  383. }
  384. static int path_dir(const char *path, char *out, unsigned int cap)
  385. {
  386. unsigned int i, last = 0;
  387. for (i = 0; path[i]; i++)
  388. if (path[i] == '/')
  389. last = i;
  390. if (last == 0) {
  391. if (cap < 2)
  392. return -1;
  393. out[0] = '/';
  394. out[1] = '\0';
  395. return 0;
  396. }
  397. if (last + 1 >= cap)
  398. return -1;
  399. {
  400. unsigned int j;
  401. for (j = 0; j < last; j++)
  402. out[j] = path[j];
  403. out[last] = '\0';
  404. }
  405. return 0;
  406. }
  407. /* Resolve an href against the current page. 0 = ok, -1 = ignore. */
  408. static int resolve_link(const char *base, const char *href, char *out,
  409. unsigned int cap)
  410. {
  411. char joined[BR_URL_MAX];
  412. if (!href || !href[0] || href[0] == '#')
  413. return -1;
  414. if (starts_with(href, "mailto:") || starts_with(href, "javascript:") ||
  415. starts_with(href, "data:") || starts_with(href, "tel:"))
  416. return -1;
  417. if (starts_with(href, "about:")) {
  418. s_copy(out, href, cap);
  419. return 0;
  420. }
  421. if (is_http(href)) {
  422. s_copy(out, href, cap);
  423. return 0;
  424. }
  425. if (starts_with(href, "//")) {
  426. if (is_http(base)) {
  427. unsigned int n = 0;
  428. s_append(out, &n, starts_with(base, "https:") ?
  429. "https:" : "http:", cap);
  430. s_append(out, &n, href, cap);
  431. return 0;
  432. }
  433. return -1;
  434. }
  435. if (is_http(base)) {
  436. return uhttp_resolve(base, href, out, cap) == 0 ? 0 : -1;
  437. }
  438. /* local page */
  439. if (href[0] == '/') {
  440. if (normalize_abs(href, out, cap) != 0)
  441. return -1;
  442. return 0;
  443. }
  444. {
  445. char dir[BR_URL_MAX];
  446. unsigned int n = 0;
  447. if (s_eq(base, "about:home"))
  448. s_copy(dir, cwd[0] ? cwd : "/", sizeof(dir));
  449. else if (path_dir(base, dir, sizeof(dir)) != 0)
  450. s_copy(dir, "/", sizeof(dir));
  451. s_append(joined, &n, dir, sizeof(joined));
  452. if (n == 0 || joined[n - 1] != '/')
  453. s_append(joined, &n, "/", sizeof(joined));
  454. s_append(joined, &n, href, sizeof(joined));
  455. }
  456. return normalize_abs(joined, out, cap) == 0 ? 0 : -1;
  457. }
  458. /* ---- HTTP / file fetch ---- */
  459. static int read_url(const char *url, unsigned int cap, unsigned char **out,
  460. unsigned int *outlen, int *is_local, char *ctype,
  461. unsigned int ctcap, char *final, unsigned int finalcap,
  462. char *err, unsigned int errcap, int *truncated)
  463. {
  464. unsigned char *buf = 0;
  465. unsigned int total = 0;
  466. int rv;
  467. *out = 0;
  468. *outlen = 0;
  469. *truncated = 0;
  470. if (final && finalcap)
  471. final[0] = '\0';
  472. if (ctype && ctcap)
  473. ctype[0] = '\0';
  474. if (url[0] == '/') {
  475. buf = (unsigned char *)heap_alloc(cap, 16);
  476. if (!buf) {
  477. s_copy(err, "out of memory", errcap);
  478. return -1;
  479. }
  480. rv = u_syscall3(SYS_READ_FILE, (int)url, (int)buf, (int)cap);
  481. if (rv < 0) {
  482. s_copy(err, "file not found", errcap);
  483. return -1;
  484. }
  485. *out = buf;
  486. *outlen = (unsigned int)rv;
  487. *is_local = 1;
  488. if (ctype)
  489. s_copy(ctype, "text/html", ctcap);
  490. if (final)
  491. s_copy(final, url, finalcap);
  492. if ((unsigned int)rv == cap)
  493. *truncated = 1;
  494. return 0;
  495. }
  496. uhttp_init(&http);
  497. http.user_agent = "nebos-browser/0.1";
  498. rv = uhttp_open(&http, url, UHTTP_GZIP, 0);
  499. if (rv != 0) {
  500. s_copy(err, uhttp_error_string(rv), errcap);
  501. return -1;
  502. }
  503. if (ctype)
  504. s_copy(ctype, http.resp.content_type, ctcap);
  505. if (final && http.resp.final_url[0])
  506. s_copy(final, http.resp.final_url, finalcap);
  507. *is_local = 0;
  508. if (http.resp.gzip ||
  509. contains_ci(http.resp.content_encoding, "gzip") ||
  510. contains_ci(http.resp.content_encoding, "deflate")) {
  511. unsigned char *comp = (unsigned char *)heap_alloc(cap, 16);
  512. unsigned int climit = cap * 2;
  513. unsigned char *plain;
  514. struct inflate *ws;
  515. unsigned int plen = 0;
  516. int zr;
  517. if (climit > 4u * 1024u * 1024u)
  518. climit = 4u * 1024u * 1024u;
  519. if (!comp) {
  520. s_copy(err, "out of memory", errcap);
  521. uhttp_close(&http);
  522. return -1;
  523. }
  524. total = 0;
  525. for (;;) {
  526. rv = uhttp_read(&http, comp + total, climit - total,
  527. UHTTP_IDLE_TIMEOUT_MS);
  528. if (rv > 0) {
  529. total += (unsigned int)rv;
  530. if (total >= climit) {
  531. *truncated = 1;
  532. break;
  533. }
  534. continue;
  535. }
  536. if (rv < 0) {
  537. s_copy(err, uhttp_error_string(rv), errcap);
  538. uhttp_close(&http);
  539. return -1;
  540. }
  541. break;
  542. }
  543. uhttp_close(&http);
  544. plain = (unsigned char *)heap_alloc(cap, 16);
  545. ws = (struct inflate *)heap_alloc(sizeof(struct inflate), 16);
  546. if (!plain || !ws) {
  547. s_copy(err, "out of memory", errcap);
  548. return -1;
  549. }
  550. if (contains_ci(http.resp.content_encoding, "deflate"))
  551. zr = inflate_zlib_mem(comp, total, plain, cap, &plen, ws);
  552. else
  553. zr = inflate_gzip_mem(comp, total, plain, cap, &plen, ws);
  554. if (zr != 0) {
  555. s_copy(err, "compressed page is corrupt", errcap);
  556. return -1;
  557. }
  558. *out = plain;
  559. *outlen = plen;
  560. return 0;
  561. }
  562. buf = (unsigned char *)heap_alloc(cap, 16);
  563. if (!buf) {
  564. s_copy(err, "out of memory", errcap);
  565. uhttp_close(&http);
  566. return -1;
  567. }
  568. for (;;) {
  569. rv = uhttp_read(&http, buf + total, cap - total,
  570. UHTTP_IDLE_TIMEOUT_MS);
  571. if (rv > 0) {
  572. total += (unsigned int)rv;
  573. if (total >= cap) {
  574. *truncated = 1;
  575. break;
  576. }
  577. continue;
  578. }
  579. if (rv < 0) {
  580. s_copy(err, uhttp_error_string(rv), errcap);
  581. uhttp_close(&http);
  582. return -1;
  583. }
  584. break;
  585. }
  586. uhttp_close(&http);
  587. if (total == 0) {
  588. s_copy(err, "empty response", errcap);
  589. return -1;
  590. }
  591. *out = buf;
  592. *outlen = total;
  593. return 0;
  594. }
  595. /* ---- page loading ---- */
  596. static void redraw_now(void);
  597. static char error_page[1024];
  598. static void show_error(const char *title, const char *detail)
  599. {
  600. char *page = error_page;
  601. unsigned int n = 0;
  602. s_append(page, &n, "<html><body><h2>", sizeof(error_page));
  603. s_append(page, &n, title, sizeof(error_page));
  604. s_append(page, &n, "</h2><p>", sizeof(error_page));
  605. {
  606. const char *p = detail;
  607. while (*p && n + 1 < sizeof(error_page)) {
  608. if (*p == '<') s_append(page, &n, "&lt;", sizeof(error_page));
  609. else if (*p == '>') s_append(page, &n, "&gt;", sizeof(error_page));
  610. else if (*p == '&') s_append(page, &n, "&amp;", sizeof(error_page));
  611. else page[n++] = *p;
  612. p++;
  613. }
  614. }
  615. page[n] = '\0';
  616. s_append(page, &n, "</p><p>", sizeof(error_page));
  617. s_append(page, &n, page_url, sizeof(error_page));
  618. s_append(page, &n, "</p><p><a href=\"/\">home</a></p></body></html>",
  619. sizeof(error_page));
  620. br_parse(&doc, page, n);
  621. s_copy(page_title, title, sizeof(page_title));
  622. page_loading = 0;
  623. set_status(title, 1, 0);
  624. ngui_invalidate(&ui);
  625. }
  626. static int decode_direct_image(const unsigned char *data, unsigned int len,
  627. unsigned int node)
  628. {
  629. if (image_count >= BR_MAX_IMAGES)
  630. return -1;
  631. if (decode_image_data(data, len, &images[0]) != 0)
  632. return -1;
  633. doc.nodes[node].img_index = 0;
  634. image_count = 1;
  635. return 0;
  636. }
  637. static void preload_images(void)
  638. {
  639. unsigned int i;
  640. int loaded = 0;
  641. for (i = 0; i < doc.nnode && loaded < BR_MAX_IMAGES; i++) {
  642. struct br_node *nd = &doc.nodes[i];
  643. const char *src;
  644. char url[BR_URL_MAX];
  645. unsigned char *data;
  646. unsigned int len;
  647. int local, trunc;
  648. char ctype[64];
  649. char final[BR_URL_MAX];
  650. char err[64];
  651. char msg[64];
  652. unsigned int n = 0;
  653. if (nd->type != BR_NODE_ELEM || nd->tag != BR_TAG_IMG ||
  654. nd->img_index >= 0)
  655. continue;
  656. src = br_src(&doc, i);
  657. if (!src[0])
  658. continue;
  659. if (resolve_link(page_url, src, url, sizeof(url)) != 0)
  660. continue;
  661. s_append(msg, &n, "Loading images ", sizeof(msg));
  662. s_append_dec(msg, &n, (unsigned int)loaded + 1, sizeof(msg));
  663. s_append(msg, &n, "...", sizeof(msg));
  664. s_copy(status_text, msg, sizeof(status_text));
  665. status_until = 0;
  666. status_error = 0;
  667. redraw_now();
  668. if (read_url(url, BR_IMG_FILE_MAX, &data, &len, &local, ctype,
  669. sizeof(ctype), final, sizeof(final), err,
  670. sizeof(err), &trunc) != 0) {
  671. continue;
  672. }
  673. (void)local;
  674. (void)trunc;
  675. if (decode_image_data(data, len, &images[image_count]) != 0)
  676. continue;
  677. nd->img_index = (short)image_count;
  678. image_count++;
  679. loaded++;
  680. }
  681. }
  682. static void history_push(const char *url)
  683. {
  684. if (!url || !url[0])
  685. return;
  686. if (hist_len > 0 && hist_pos >= 0 && hist_pos < hist_len &&
  687. s_eq(history[hist_pos], url))
  688. return;
  689. if (hist_len > 0 && hist_pos + 1 < hist_len)
  690. hist_len = hist_pos + 1;
  691. if (hist_len < BR_HISTORY) {
  692. s_copy(history[hist_len], url, BR_URL_MAX);
  693. hist_pos = hist_len;
  694. hist_len++;
  695. } else {
  696. int i;
  697. for (i = 1; i < BR_HISTORY; i++)
  698. s_copy(history[i - 1], history[i], BR_URL_MAX);
  699. s_copy(history[BR_HISTORY - 1], url, BR_URL_MAX);
  700. hist_pos = BR_HISTORY - 1;
  701. }
  702. }
  703. static void load_page(const char *url, int local, int push)
  704. {
  705. unsigned char *body = 0;
  706. unsigned int len = 0;
  707. int truncated = 0;
  708. struct img_info image_info;
  709. char ctype[64];
  710. char final[BR_URL_MAX];
  711. char err[80];
  712. const char *detail = 0;
  713. final[0] = '\0';
  714. heap_clear();
  715. image_reset();
  716. br_parse_plain(&doc, "", 0); /* blank while loading */
  717. page_title[0] = '\0';
  718. page_loading = 1;
  719. page_is_local = local;
  720. s_copy(page_url, url, sizeof(page_url));
  721. if (final[0])
  722. final[0] = '\0';
  723. if (push)
  724. history_push(url);
  725. set_status("Loading...", 0, 0);
  726. redraw_now();
  727. if (heap_start() != 0) {
  728. show_error("Out of memory", "Cannot allocate the page buffer");
  729. return;
  730. }
  731. if (read_url(url, BR_PAGE_MAX, &body, &len, &local, ctype,
  732. sizeof(ctype), final, sizeof(final), err, sizeof(err),
  733. &truncated) != 0) {
  734. detail = err;
  735. show_error("Cannot load this page", detail);
  736. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  737. url_in.len = s_len(url_in.text);
  738. url_in.cursor = url_in.len;
  739. url_in.scroll = 0;
  740. return;
  741. }
  742. page_is_local = local;
  743. if (final[0])
  744. s_copy(page_url, final, sizeof(page_url));
  745. if (contains_ci(ctype, "text/plain")) {
  746. br_parse_plain(&doc, (const char *)body, len);
  747. } else if (contains_ci(ctype, "image/") ||
  748. (!contains_ci(ctype, "text/html") &&
  749. img_sniff(body, len, &image_info) == 0)) {
  750. unsigned int node = br_parse_image_doc(&doc, page_url);
  751. if (node)
  752. decode_direct_image(body, len, node);
  753. s_copy(page_title, "Image", sizeof(page_title));
  754. } else {
  755. br_parse(&doc, (const char *)body, len);
  756. s_copy(page_title, doc.title[0] ? doc.title : page_url,
  757. sizeof(page_title));
  758. preload_images();
  759. }
  760. if (truncated)
  761. set_status("Page truncated at the buffer limit", 0, 6000);
  762. else if (doc.truncated)
  763. set_status("Page truncated by the parser", 0, 6000);
  764. else if (starts_with(page_url, "https://"))
  765. set_status("HTTPS: certificate NOT verified", 0, 6000);
  766. else
  767. set_status("", 0, 0);
  768. page_loading = 0;
  769. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  770. url_in.len = s_len(url_in.text);
  771. url_in.cursor = url_in.len;
  772. url_in.scroll = 0;
  773. ngui_set_focus(&ui, 0);
  774. ngui_invalidate(&ui);
  775. }
  776. static void load_home(int push)
  777. {
  778. heap_clear();
  779. image_reset();
  780. br_parse_plain(&doc, "", 0);
  781. page_loading = 1;
  782. page_is_local = 0;
  783. s_copy(page_url, "about:home", sizeof(page_url));
  784. if (push)
  785. history_push(page_url);
  786. redraw_now();
  787. if (heap_start() != 0) {
  788. show_error("Out of memory", "Cannot allocate the browser heap");
  789. return;
  790. }
  791. {
  792. static const char home[] =
  793. "<html><head><title>Nebs Browser</title><style>"
  794. "body { margin: 10px; color: #222222; }"
  795. "h1 { color: #2255cc; }"
  796. ".box { background-color: #eeeeff; margin: 12px; "
  797. "padding: 10px; }"
  798. "a { color: #0033cc; }"
  799. "pre { background-color: #f4f4f4; }"
  800. "</style></head><body>"
  801. "<h1>Nebs Browser</h1>"
  802. "<p>A small HTML/CSS browser built on <b>ngui</b>, the "
  803. "shared HTTP client and the image library.</p>"
  804. "<div class=\"box\"><b>Try:</b>"
  805. "<ul><li><a href=\"/floppy1/DEMO.HTM\">/"
  806. "floppy1/DEMO.HTM</a> on the root floppy</li>"
  807. "<li><a href=\"http://10.0.2.2:8000/\">"
  808. "http://10.0.2.2:8000/</a> on the QEMU host</li>"
  809. "<li><a href=\"https://example.com/\">"
  810. "https://example.com/</a> (TLS, not verified)</li>"
  811. "</ul></div>"
  812. "<p>Type a URL in the bar above. Esc closes the "
  813. "window, Ctrl+L focuses the address bar.</p>"
  814. "<pre>javascript: not supported\nforms: not supported"
  815. "\nimages: PNG JPEG GIF BMP TGA PNM ICO</pre>"
  816. "</body></html>";
  817. br_parse(&doc, home, sizeof(home) - 1);
  818. s_copy(page_title, doc.title, sizeof(page_title));
  819. }
  820. page_loading = 0;
  821. set_status("", 0, 0);
  822. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  823. url_in.len = s_len(url_in.text);
  824. url_in.cursor = url_in.len;
  825. url_in.scroll = 0;
  826. ngui_set_focus(&ui, 0);
  827. ngui_invalidate(&ui);
  828. }
  829. static void navigate(const char *input, int push)
  830. {
  831. char target[BR_URL_MAX];
  832. char *t;
  833. if (!input)
  834. return;
  835. s_copy(target, input, sizeof(target));
  836. t = trim(target);
  837. if (!t[0])
  838. return;
  839. if (s_eq(t, "about:home")) {
  840. load_home(push);
  841. return;
  842. }
  843. if (starts_with(t, "file://")) {
  844. make_abs_local(t + 7, target, sizeof(target));
  845. load_page(target, 1, push);
  846. return;
  847. }
  848. if (is_http(t)) {
  849. load_page(t, 0, push);
  850. return;
  851. }
  852. {
  853. struct sys_stat st;
  854. if (t[0] == '/' || (u_stat(t, &st) == 0 && !st.is_dir)) {
  855. make_abs_local(t, target, sizeof(target));
  856. load_page(target, 1, push);
  857. return;
  858. }
  859. }
  860. {
  861. char with_scheme[BR_URL_MAX];
  862. unsigned int n = 0;
  863. s_append(with_scheme, &n, "http://", sizeof(with_scheme));
  864. s_append(with_scheme, &n, t, sizeof(with_scheme));
  865. load_page(with_scheme, 0, push);
  866. }
  867. }
  868. /* ---- layout ---- */
  869. static struct br_style base_style(void)
  870. {
  871. struct br_style s;
  872. s.color = BR_BLACK;
  873. s.bg = BR_WHITE;
  874. s.has_bg = 0;
  875. s.scale = 1;
  876. s.bold = 0;
  877. s.underline = 0;
  878. s.align = BR_ALIGN_LEFT;
  879. s.display = BR_DISPLAY_BLOCK;
  880. s.mt = s.mb = s.ml = s.mr = 0;
  881. s.pt = s.pb = s.pl = s.pr = 0;
  882. return s;
  883. }
  884. static int run_width(const struct lay_run *r)
  885. {
  886. if (r->img >= 0)
  887. return r->iw;
  888. {
  889. unsigned int n = r->has_lit ? s_len(r->lit) : r->len;
  890. return (int)(n * (unsigned int)(8 * r->scale) +
  891. (r->bold ? 1u : 0u));
  892. }
  893. }
  894. static void flush_line(struct lay *l)
  895. {
  896. int total, x, i;
  897. if (!l->nruns && l->line_x == l->left) {
  898. l->line_h = 0;
  899. return;
  900. }
  901. total = l->line_x - l->left;
  902. x = l->left;
  903. if (l->align == BR_ALIGN_CENTER)
  904. x += (l->right - l->left - total) / 2;
  905. else if (l->align == BR_ALIGN_RIGHT)
  906. x = l->right - total;
  907. if (x < l->left)
  908. x = l->left;
  909. for (i = 0; i < l->nruns; i++) {
  910. struct lay_run *r = &l->runs[i];
  911. int w = run_width(r);
  912. x += r->gap;
  913. if (l->draw) {
  914. if (r->img >= 0) {
  915. struct br_image *bi = &images[r->img];
  916. if (bi->state == 1) {
  917. struct img_blit b;
  918. b.dst = l->ui->px;
  919. b.dst_w = l->ui->w;
  920. b.dst_h = l->ui->h;
  921. b.clip_x = page_rect.x;
  922. b.clip_y = page_rect.y;
  923. b.clip_w = page_rect.w - 12;
  924. b.clip_h = page_rect.h;
  925. b.x = x;
  926. b.y = l->line_y;
  927. b.scale = ((unsigned int)r->iw << 16) /
  928. bi->img.w;
  929. b.bg = BR_WHITE;
  930. b.bg2 = BR_WHITE;
  931. b.flags = IMG_BLIT_OVER_DST;
  932. img_draw(&bi->img, &b);
  933. }
  934. } else {
  935. const char *s = r->has_lit ? r->lit :
  936. doc.text + r->off;
  937. unsigned int n = r->has_lit ? s_len(r->lit) :
  938. r->len;
  939. ngui_text_styled(l->ui, x, l->line_y, s, n,
  940. r->color, r->scale, r->bold);
  941. if (r->underline)
  942. ngui_fill_rect(l->ui,
  943. (struct ngui_rect){x,
  944. l->line_y + 16 * r->scale - 2,
  945. w, 1}, r->color);
  946. }
  947. if (r->href) {
  948. if (nhits < HIT_MAX) {
  949. struct hit *h = &hits[nhits];
  950. int hh = r->img >= 0 ? r->ih :
  951. 16 * r->scale;
  952. int merge = nhits > 0 &&
  953. hits[nhits - 1].node == r->href &&
  954. hits[nhits - 1].y == l->line_y &&
  955. hits[nhits - 1].h == hh &&
  956. x <= hits[nhits - 1].x +
  957. hits[nhits - 1].w + 2;
  958. if (merge) {
  959. struct hit *p = &hits[nhits - 1];
  960. int end = x + w;
  961. if (end > p->x + p->w)
  962. p->w = end - p->x;
  963. } else {
  964. h->x = x;
  965. h->y = l->line_y;
  966. h->w = w;
  967. h->h = hh;
  968. h->node = r->href;
  969. nhits++;
  970. }
  971. }
  972. }
  973. }
  974. x += w;
  975. }
  976. l->y += l->line_h + 2;
  977. l->nruns = 0;
  978. l->line_x = l->left;
  979. l->run_x = l->left;
  980. l->line_h = 0;
  981. l->space_pending = 0;
  982. }
  983. static void add_run(struct lay *l, unsigned short off, unsigned short len,
  984. const struct br_style *st, unsigned short href,
  985. const char *lit)
  986. {
  987. unsigned int n = lit ? s_len(lit) : len;
  988. int w = (int)(n * (unsigned int)(8 * st->scale) +
  989. (st->bold ? 1u : 0u));
  990. if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0])))
  991. flush_line(l);
  992. if (l->nruns == 0) {
  993. l->line_y = l->y;
  994. l->line_h = 0;
  995. }
  996. {
  997. struct lay_run *r = &l->runs[l->nruns++];
  998. int gap = l->line_x - l->run_x;
  999. if (gap < 0)
  1000. gap = 0;
  1001. r->gap = (unsigned short)gap;
  1002. r->off = off;
  1003. r->len = len;
  1004. r->color = st->color;
  1005. r->href = href;
  1006. r->has_lit = 0;
  1007. r->lit[0] = '\0';
  1008. if (lit && lit[0]) {
  1009. unsigned int i;
  1010. for (i = 0; i + 1 < sizeof(r->lit) && lit[i]; i++)
  1011. r->lit[i] = lit[i];
  1012. r->lit[i] = '\0';
  1013. r->has_lit = 1;
  1014. }
  1015. r->img = -1;
  1016. r->iw = r->ih = 0;
  1017. r->scale = st->scale;
  1018. r->bold = st->bold;
  1019. r->underline = st->underline;
  1020. }
  1021. l->line_x += w;
  1022. l->run_x = l->line_x;
  1023. if (l->line_h < 16 * st->scale)
  1024. l->line_h = 16 * st->scale;
  1025. }
  1026. static void add_image_run(struct lay *l, unsigned int node,
  1027. const struct br_style *st, unsigned short href)
  1028. {
  1029. struct br_image *bi = 0;
  1030. int idx = doc.nodes[node].img_index;
  1031. int dw, dh, maxw;
  1032. if (idx >= 0 && idx < image_count && images[idx].state == 1)
  1033. bi = &images[idx];
  1034. if (l->space_pending && (l->nruns > 0 || l->line_x > l->left)) {
  1035. if (l->line_x + 8 <= l->right)
  1036. l->line_x += 8;
  1037. }
  1038. l->space_pending = 0;
  1039. if (!bi) {
  1040. const char *alt = br_alt(&doc, node);
  1041. struct br_style gs = *st;
  1042. char tmp[96];
  1043. unsigned int n = 0;
  1044. int w;
  1045. gs.color = BR_GRAY;
  1046. gs.underline = 0;
  1047. s_append(tmp, &n, "[", sizeof(tmp));
  1048. s_append(tmp, &n, alt[0] ? alt : "image", sizeof(tmp));
  1049. s_append(tmp, &n, "]", sizeof(tmp));
  1050. w = (int)(n * (unsigned int)(8 * gs.scale));
  1051. if (l->line_x + w > l->right &&
  1052. (l->nruns > 0 || l->line_x > l->left))
  1053. flush_line(l);
  1054. add_run(l, 0, 0, &gs, href, tmp);
  1055. return;
  1056. }
  1057. if (l->space_pending && (l->nruns > 0 || l->line_x > l->left)) {
  1058. if (l->line_x + 8 <= l->right)
  1059. l->line_x += 8;
  1060. }
  1061. maxw = l->right - l->left;
  1062. dw = (int)bi->img.w;
  1063. dh = (int)bi->img.h;
  1064. if (doc.nodes[node].width_attr) {
  1065. int want = doc.nodes[node].width_attr;
  1066. dh = dh * want / dw;
  1067. dw = want;
  1068. }
  1069. if (doc.nodes[node].height_attr) {
  1070. int want = doc.nodes[node].height_attr;
  1071. if (dw > 0)
  1072. dw = dw * want / dh;
  1073. dh = want;
  1074. }
  1075. if (dw < 8)
  1076. dw = 8;
  1077. if (dh < 8)
  1078. dh = 8;
  1079. if (dw > maxw && maxw > 0) {
  1080. dh = dh * maxw / dw;
  1081. dw = maxw;
  1082. }
  1083. if (l->line_x + dw > l->right && (l->nruns > 0 ||
  1084. l->line_x > l->left))
  1085. flush_line(l);
  1086. if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0])))
  1087. flush_line(l);
  1088. if (l->nruns == 0) {
  1089. l->line_y = l->y;
  1090. l->line_h = 0;
  1091. }
  1092. {
  1093. struct lay_run *r = &l->runs[l->nruns++];
  1094. int gap = l->line_x - l->run_x;
  1095. if (gap < 0)
  1096. gap = 0;
  1097. r->gap = (unsigned short)gap;
  1098. r->off = 0;
  1099. r->len = 0;
  1100. r->color = 0;
  1101. r->href = href;
  1102. r->has_lit = 0;
  1103. r->lit[0] = '\0';
  1104. r->img = (short)idx;
  1105. r->iw = (unsigned short)dw;
  1106. r->ih = (unsigned short)dh;
  1107. r->scale = 1;
  1108. r->bold = 0;
  1109. r->underline = 0;
  1110. }
  1111. l->line_x += dw;
  1112. l->run_x = l->line_x;
  1113. if (l->line_h < dh)
  1114. l->line_h = dh;
  1115. }
  1116. static void emit_text(struct lay *l, const char *s, unsigned int len,
  1117. const struct br_style *st, unsigned short href, int pre)
  1118. {
  1119. unsigned int i = 0;
  1120. unsigned int base;
  1121. int cw = 8 * st->scale;
  1122. int adv = cw + (st->bold ? 1 : 0);
  1123. if (!s || !len)
  1124. return;
  1125. base = (unsigned int)(s - doc.text);
  1126. if (pre) {
  1127. while (i < len) {
  1128. unsigned int start;
  1129. if (s[i] == '\n') {
  1130. flush_line(l);
  1131. i++;
  1132. continue;
  1133. }
  1134. if (s[i] == ' ') {
  1135. if (l->line_x + cw > l->right &&
  1136. (l->nruns > 0 || l->line_x > l->left))
  1137. flush_line(l);
  1138. else
  1139. l->line_x += cw;
  1140. i++;
  1141. continue;
  1142. }
  1143. start = i;
  1144. while (i < len && s[i] != ' ' && s[i] != '\n')
  1145. i++;
  1146. {
  1147. unsigned int n = i - start;
  1148. int w = (int)(n * (unsigned int)adv);
  1149. if (l->line_x + w > l->right &&
  1150. (l->nruns > 0 || l->line_x > l->left))
  1151. flush_line(l);
  1152. if (w > l->right - l->left) {
  1153. int maxc = (l->right - l->left) / adv;
  1154. unsigned int off = start;
  1155. if (maxc < 1)
  1156. maxc = 1;
  1157. while (n > 0) {
  1158. unsigned int take = n >
  1159. (unsigned int)maxc ?
  1160. (unsigned int)maxc : n;
  1161. add_run(l, (unsigned short)(base + off),
  1162. (unsigned short)take,
  1163. st, href, 0);
  1164. off += take;
  1165. n -= take;
  1166. if (n)
  1167. flush_line(l);
  1168. }
  1169. } else {
  1170. add_run(l, (unsigned short)(base + start),
  1171. (unsigned short)n, st, href, 0);
  1172. }
  1173. }
  1174. }
  1175. return;
  1176. }
  1177. while (i < len) {
  1178. unsigned int start, n;
  1179. int w;
  1180. if (s[i] == ' ') {
  1181. l->space_pending = 1;
  1182. i++;
  1183. continue;
  1184. }
  1185. start = i;
  1186. while (i < len && s[i] != ' ')
  1187. i++;
  1188. n = i - start;
  1189. w = (int)(n * (unsigned int)adv);
  1190. if (l->space_pending &&
  1191. (l->nruns > 0 || l->line_x > l->left)) {
  1192. if (l->line_x + cw <= l->right)
  1193. l->line_x += cw;
  1194. }
  1195. l->space_pending = 0;
  1196. if (l->line_x + w > l->right &&
  1197. (l->nruns > 0 || l->line_x > l->left))
  1198. flush_line(l);
  1199. if (w > l->right - l->left) {
  1200. int maxc = (l->right - l->left) / adv;
  1201. unsigned int off = start;
  1202. if (maxc < 1)
  1203. maxc = 1;
  1204. while (n > 0) {
  1205. unsigned int take = n > (unsigned int)maxc ?
  1206. (unsigned int)maxc : n;
  1207. add_run(l, (unsigned short)(base + off),
  1208. (unsigned short)take, st, href, 0);
  1209. off += take;
  1210. n -= take;
  1211. if (n)
  1212. flush_line(l);
  1213. }
  1214. } else if (n) {
  1215. add_run(l, (unsigned short)(base + start),
  1216. (unsigned short)n, st, href, 0);
  1217. }
  1218. }
  1219. }
  1220. static int list_index(unsigned int parent, unsigned int node)
  1221. {
  1222. unsigned int n = 0, i;
  1223. for (i = doc.nodes[parent].first; i && i != node;
  1224. i = doc.nodes[i].next)
  1225. if (doc.nodes[i].tag == BR_TAG_LI)
  1226. n++;
  1227. return (int)n + 1;
  1228. }
  1229. static void layout_children(struct lay *l, unsigned int parent,
  1230. const struct br_style *pst, int depth,
  1231. unsigned short link);
  1232. static void layout_block(struct lay *l, unsigned int node,
  1233. const struct br_style *st, int depth,
  1234. unsigned short link)
  1235. {
  1236. int old_left = l->left, old_right = l->right, old_align = l->align;
  1237. int top, l0, r0;
  1238. if (depth > 24 || st->display == BR_DISPLAY_NONE)
  1239. return;
  1240. flush_line(l);
  1241. l->y += st->mt;
  1242. top = l->y;
  1243. l0 = old_left + st->ml;
  1244. r0 = old_right - st->mr;
  1245. if (r0 < l0 + 16)
  1246. r0 = l0 + 16;
  1247. if (l->draw && st->has_bg && block_h[node])
  1248. ngui_fill_rect(l->ui, (struct ngui_rect){l0, top, r0 - l0,
  1249. block_h[node]},
  1250. st->bg);
  1251. l->left = l0 + st->pl;
  1252. l->right = r0 - st->pr;
  1253. if (l->right < l->left + 8)
  1254. l->right = l->left + 8;
  1255. l->align = st->align;
  1256. l->y += st->pt;
  1257. if (doc.nodes[node].tag == BR_TAG_HR) {
  1258. if (l->draw)
  1259. ngui_fill_rect(l->ui, (struct ngui_rect){l->left,
  1260. l->y + 2, l->right - l->left, 1}, BR_GRAY);
  1261. l->y += 8;
  1262. } else {
  1263. if (doc.nodes[node].tag == BR_TAG_LI) {
  1264. char *m = li_marker[node];
  1265. if (!m[0]) {
  1266. unsigned int parent = doc.nodes[node].parent;
  1267. if (parent &&
  1268. doc.nodes[parent].tag == BR_TAG_OL) {
  1269. int idx = list_index(parent, node);
  1270. unsigned int n = 0;
  1271. s_append_dec(m, &n, (unsigned int)idx, 6);
  1272. s_append(m, &n, ".", 6);
  1273. } else {
  1274. s_copy(m, "-", 6);
  1275. }
  1276. }
  1277. if (l->draw) {
  1278. int mw = (int)s_len(m) * 8;
  1279. ngui_text(l->ui, l->left - mw - 6, l->y, m,
  1280. st->color);
  1281. }
  1282. }
  1283. layout_children(l, node, st, depth + 1, link);
  1284. }
  1285. flush_line(l);
  1286. l->y += st->pb;
  1287. l->left = old_left;
  1288. l->right = old_right;
  1289. l->align = old_align;
  1290. l->y += st->mb;
  1291. if (!l->draw) {
  1292. int h = l->y - top;
  1293. if (h < 0)
  1294. h = 0;
  1295. if (h > 65535)
  1296. h = 65535;
  1297. block_h[node] = (unsigned short)h;
  1298. }
  1299. }
  1300. static void layout_children(struct lay *l, unsigned int parent,
  1301. const struct br_style *pst, int depth,
  1302. unsigned short link)
  1303. {
  1304. unsigned int n;
  1305. if (depth > 24)
  1306. return;
  1307. for (n = doc.nodes[parent].first; n; n = doc.nodes[n].next) {
  1308. struct br_node *nd = &doc.nodes[n];
  1309. struct br_style cs;
  1310. unsigned short lk = link;
  1311. if (nd->type == BR_NODE_TEXT) {
  1312. emit_text(l, doc.text + nd->text_off, nd->text_len, pst,
  1313. link, (nd->flags & BR_NODE_PREFORMAT) != 0);
  1314. continue;
  1315. }
  1316. br_style_for(&doc, n, pst, &cs);
  1317. if (cs.display == BR_DISPLAY_NONE)
  1318. continue;
  1319. if (nd->tag == BR_TAG_A && nd->href_off)
  1320. lk = (unsigned short)n;
  1321. if (nd->tag == BR_TAG_BR) {
  1322. flush_line(l);
  1323. continue;
  1324. }
  1325. if (nd->tag == BR_TAG_HR) {
  1326. flush_line(l);
  1327. if (l->draw)
  1328. ngui_fill_rect(l->ui,
  1329. (struct ngui_rect){l->left, l->y + 2,
  1330. l->right - l->left, 1}, BR_GRAY);
  1331. l->y += 8;
  1332. continue;
  1333. }
  1334. if (nd->tag == BR_TAG_IMG) {
  1335. add_image_run(l, n, &cs, lk);
  1336. continue;
  1337. }
  1338. if (cs.display == BR_DISPLAY_BLOCK || br_tag_is_block(nd->tag)) {
  1339. layout_block(l, n, &cs, depth + 1, lk);
  1340. } else {
  1341. layout_children(l, n, &cs, depth + 1, lk);
  1342. if (nd->tag == BR_TAG_TD || nd->tag == BR_TAG_TH)
  1343. l->space_pending = 1;
  1344. }
  1345. }
  1346. }
  1347. /* One layout state, kept out of the 8 KB user stack: it holds 96 line
  1348. * runs and a recursive page can otherwise overflow the stack. */
  1349. static struct lay layout_state;
  1350. static int measure_page(int left, int right)
  1351. {
  1352. struct lay *l = &layout_state;
  1353. struct br_style base = base_style();
  1354. unsigned int i;
  1355. for (i = 0; i < BR_MAX_NODES; i++)
  1356. block_h[i] = 0;
  1357. l->draw = 0;
  1358. l->ui = 0;
  1359. l->left = left;
  1360. l->right = right;
  1361. l->y = 0;
  1362. l->line_x = left;
  1363. l->run_x = left;
  1364. l->line_y = 0;
  1365. l->line_h = 0;
  1366. l->align = BR_ALIGN_LEFT;
  1367. l->depth = 0;
  1368. l->nruns = 0;
  1369. l->space_pending = 0;
  1370. layout_children(l, doc.root, &base, 0, 0);
  1371. return l->y + 8;
  1372. }
  1373. static void render_page(int left, int right, int y0)
  1374. {
  1375. struct lay *l = &layout_state;
  1376. struct br_style base = base_style();
  1377. l->draw = 1;
  1378. l->ui = &ui;
  1379. l->left = left;
  1380. l->right = right;
  1381. l->y = y0;
  1382. l->line_x = left;
  1383. l->run_x = left;
  1384. l->line_y = y0;
  1385. l->line_h = 0;
  1386. l->align = BR_ALIGN_LEFT;
  1387. l->depth = 0;
  1388. l->nruns = 0;
  1389. l->space_pending = 0;
  1390. nhits = 0;
  1391. layout_children(l, doc.root, &base, 0, 0);
  1392. }
  1393. /* ---- UI ---- */
  1394. static int find_hit(int x, int y)
  1395. {
  1396. int i;
  1397. for (i = nhits - 1; i >= 0; i--) {
  1398. struct hit *h = &hits[i];
  1399. if (x >= h->x && y >= h->y && x < h->x + h->w &&
  1400. y < h->y + h->h)
  1401. return (int)h->node;
  1402. }
  1403. return 0;
  1404. }
  1405. static void draw_toolbar(struct ngui_rect r, unsigned int *action)
  1406. {
  1407. int x = r.x + 4, y = r.y + 4, h = 22;
  1408. int compact = r.w < 560;
  1409. int go_w = r.w < 420 ? 32 : 42;
  1410. int in_x, in_w, res;
  1411. ngui_fill_rect(&ui, r, ui.theme.panel);
  1412. ngui_fill_rect(&ui, (struct ngui_rect){r.x, r.y + r.h - 1, r.w, 1},
  1413. ui.theme.border);
  1414. if (ngui_icon_button(&ui, ID_BACK, (struct ngui_rect){x, y, 28, h},
  1415. NGUI_ICON_CHEVRON_LEFT))
  1416. *action |= ACT_BACK;
  1417. x += 32;
  1418. if (!compact) {
  1419. if (ngui_icon_button(&ui, ID_FORWARD,
  1420. (struct ngui_rect){x, y, 28, h},
  1421. NGUI_ICON_CHEVRON_RIGHT))
  1422. *action |= ACT_FORWARD;
  1423. x += 32;
  1424. }
  1425. if (ngui_icon_button(&ui, ID_RELOAD, (struct ngui_rect){x, y, 28, h},
  1426. NGUI_ICON_REFRESH))
  1427. *action |= ACT_RELOAD;
  1428. x += 32;
  1429. if (!compact) {
  1430. if (ngui_icon_button(&ui, ID_HOME,
  1431. (struct ngui_rect){x, y, 28, h},
  1432. NGUI_ICON_HOME))
  1433. *action |= ACT_HOME;
  1434. x += 32;
  1435. }
  1436. in_x = x + 2;
  1437. in_w = r.x + r.w - 6 - go_w - 4 - in_x;
  1438. if (in_w < 60)
  1439. in_w = 60;
  1440. res = ngui_text_input(&ui, ID_URL,
  1441. (struct ngui_rect){in_x, y, in_w, h}, &url_in);
  1442. if (res & NGUI_INPUT_SUBMIT)
  1443. *action |= ACT_NAV;
  1444. if (res & NGUI_INPUT_CANCEL)
  1445. ngui_set_focus(&ui, 0);
  1446. if (ngui_button(&ui, ID_GO,
  1447. (struct ngui_rect){r.x + r.w - 4 - go_w, y, go_w, h},
  1448. "Go"))
  1449. *action |= ACT_NAV;
  1450. }
  1451. static int page_hover_node(void)
  1452. {
  1453. if (!ui.inside)
  1454. return 0;
  1455. if (ui.mouse_x < page_rect.x ||
  1456. ui.mouse_x >= page_rect.x + page_rect.w - 12 ||
  1457. ui.mouse_y < page_rect.y ||
  1458. ui.mouse_y >= page_rect.y + page_rect.h)
  1459. return 0;
  1460. return find_hit(ui.mouse_x, ui.mouse_y);
  1461. }
  1462. static void draw_page(struct ngui_rect page)
  1463. {
  1464. int left, right, ch;
  1465. int old_panel, old_dim;
  1466. page_rect = page;
  1467. ngui_fill_rect(&ui, page, BR_WHITE);
  1468. if (!doc.nnode || (page_loading && doc.nnode <= 1)) {
  1469. const char *s = page_loading ? "Loading..." : "No page";
  1470. int w = (int)ngui_text_width(s);
  1471. ngui_text(&ui, page.x + (page.w - w) / 2,
  1472. page.y + page.h / 2 - 8, s, ui.theme.text_dim);
  1473. page_scroll.content_h = 0;
  1474. page_scroll.view_h = page.h;
  1475. return;
  1476. }
  1477. if (page.w < 40 || page.h < 40)
  1478. return;
  1479. left = page.x + 8;
  1480. right = page.x + page.w - 16;
  1481. ch = measure_page(left, right);
  1482. page_scroll.content_h = ch;
  1483. page_scroll.view_h = page.h;
  1484. ngui_scroll_begin(&ui, page, &page_scroll);
  1485. render_page(left, right, page.y + 4 - page_scroll.offset);
  1486. old_panel = ui.theme.panel_alt;
  1487. old_dim = ui.theme.text_dim;
  1488. ui.theme.panel_alt = 0xC618;
  1489. ui.theme.text_dim = 0x8410;
  1490. ngui_scroll_end(&ui, page, &page_scroll);
  1491. ui.theme.panel_alt = old_panel;
  1492. ui.theme.text_dim = old_dim;
  1493. }
  1494. static void draw_status(struct ngui_rect r)
  1495. {
  1496. char msg[BR_URL_MAX + 32];
  1497. unsigned int n = 0;
  1498. int hover = page_hover_node();
  1499. unsigned short color;
  1500. const char *kind;
  1501. ngui_fill_rect(&ui, r, ui.theme.panel);
  1502. ngui_fill_rect(&ui, (struct ngui_rect){r.x, r.y, r.w, 1},
  1503. ui.theme.border);
  1504. if (hover) {
  1505. const char *href = br_href(&doc, (unsigned int)hover);
  1506. char url[BR_URL_MAX];
  1507. if (resolve_link(page_url, href, url, sizeof(url)) == 0) {
  1508. s_append(msg, &n, "Link: ", sizeof(msg));
  1509. s_append(msg, &n, url, sizeof(msg));
  1510. } else {
  1511. s_copy(msg, "Link", sizeof(msg));
  1512. }
  1513. color = ui.theme.accent;
  1514. } else if (status_text[0]) {
  1515. s_copy(msg, status_text, sizeof(msg));
  1516. color = status_error ? ui.theme.error : ui.theme.text;
  1517. } else if (page_title[0]) {
  1518. s_copy(msg, page_title, sizeof(msg));
  1519. color = ui.theme.text;
  1520. } else {
  1521. s_copy(msg, "Ready", sizeof(msg));
  1522. color = ui.theme.text_dim;
  1523. }
  1524. kind = s_eq(page_url, "about:home") ? "HOME" :
  1525. page_is_local ? "FILE" :
  1526. (starts_with(page_url, "https://") ? "HTTPS" : "HTTP");
  1527. ngui_text_ellipsis(&ui, r, r.x + 6, r.y + 1, msg, color);
  1528. {
  1529. int kw = (int)ngui_text_width(kind);
  1530. ngui_text(&ui, r.x + r.w - 6 - kw, r.y + 1, kind,
  1531. ui.theme.text_dim);
  1532. }
  1533. }
  1534. static void draw(unsigned int *action)
  1535. {
  1536. struct ngui_rect r = {0, 0, ui.w, ui.h};
  1537. struct ngui_rect toolbar, status, page;
  1538. toolbar = ngui_cut_top(&r, BR_TOOLBAR_H);
  1539. status = ngui_cut_bottom(&r, BR_STATUS_H);
  1540. page = r;
  1541. draw_toolbar(toolbar, action);
  1542. draw_page(page);
  1543. draw_status(status);
  1544. }
  1545. static void redraw_now(void)
  1546. {
  1547. unsigned int action = 0;
  1548. ngui_begin(&ui);
  1549. draw(&action);
  1550. ngui_end(&ui);
  1551. }
  1552. static void handle_keys(unsigned int *action)
  1553. {
  1554. int k;
  1555. if (ngui_focus(&ui))
  1556. return;
  1557. if (ngui_scroll_keys(&ui, &page_scroll))
  1558. ngui_invalidate(&ui);
  1559. while ((k = ngui_key(&ui)) != 0) {
  1560. switch (k) {
  1561. case NGUI_KEY_ESC:
  1562. *action |= ACT_CLOSE;
  1563. break;
  1564. case NGUI_KEY_TAB:
  1565. case 0x0C: /* Ctrl+L */
  1566. ngui_set_focus(&ui, ID_URL);
  1567. break;
  1568. case 0x12: /* Ctrl+R */
  1569. *action |= ACT_RELOAD;
  1570. break;
  1571. case NGUI_KEY_BACKSPACE:
  1572. *action |= ACT_BACK;
  1573. break;
  1574. case NGUI_KEY_UP:
  1575. ngui_scroll_to(&page_scroll,
  1576. page_scroll.offset - 24);
  1577. ngui_invalidate(&ui);
  1578. break;
  1579. case NGUI_KEY_DOWN:
  1580. ngui_scroll_to(&page_scroll,
  1581. page_scroll.offset + 24);
  1582. ngui_invalidate(&ui);
  1583. break;
  1584. case NGUI_KEY_HOME:
  1585. ngui_scroll_to(&page_scroll, 0);
  1586. ngui_invalidate(&ui);
  1587. break;
  1588. case NGUI_KEY_END:
  1589. ngui_scroll_to(&page_scroll,
  1590. page_scroll.content_h -
  1591. page_scroll.view_h);
  1592. ngui_invalidate(&ui);
  1593. break;
  1594. default:
  1595. break;
  1596. }
  1597. }
  1598. }
  1599. static void handle_page_click(unsigned int *action)
  1600. {
  1601. int node;
  1602. char url[BR_URL_MAX];
  1603. if (!ui.pressed || !ui.inside)
  1604. return;
  1605. if (ui.press_x < page_rect.x ||
  1606. ui.press_x >= page_rect.x + page_rect.w - 12 ||
  1607. ui.press_y < page_rect.y ||
  1608. ui.press_y >= page_rect.y + page_rect.h)
  1609. return;
  1610. node = find_hit(ui.press_x, ui.press_y);
  1611. if (!node)
  1612. return;
  1613. if (resolve_link(page_url, br_href(&doc, (unsigned int)node), url,
  1614. sizeof(url)) != 0)
  1615. return;
  1616. s_copy(click_url, url, sizeof(click_url));
  1617. *action |= ACT_NAV;
  1618. }
  1619. void _start(const char *arg)
  1620. {
  1621. struct win_info wi;
  1622. if (u_win_create(BR_W, BR_H, "Browser", &wi) != 0) {
  1623. u_puts("browser: no graphics window\n");
  1624. u_exit(1);
  1625. }
  1626. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  1627. url_in.placeholder = "http://...";
  1628. br_doc_init(&doc);
  1629. if (u_getcwd(cwd, sizeof(cwd)) != 0 || !cwd[0])
  1630. s_copy(cwd, "/", sizeof(cwd));
  1631. heap_clear();
  1632. page_scroll.offset = 0;
  1633. page_scroll.content_h = 0;
  1634. if (arg && arg[0])
  1635. navigate(arg, 1);
  1636. else
  1637. load_home(1);
  1638. for (;;) {
  1639. unsigned int action = 0;
  1640. unsigned int now;
  1641. click_url[0] = '\0';
  1642. now = u_ticks_ms();
  1643. if (status_until && (int)(now - status_until) >= 0) {
  1644. status_text[0] = '\0';
  1645. status_until = 0;
  1646. status_error = 0;
  1647. ngui_invalidate(&ui);
  1648. }
  1649. if (!ngui_begin(&ui)) {
  1650. u_sleep_ms(10);
  1651. continue;
  1652. }
  1653. draw(&action);
  1654. handle_keys(&action);
  1655. handle_page_click(&action);
  1656. ngui_end(&ui);
  1657. if (action & ACT_NAV) {
  1658. if (click_url[0]) {
  1659. navigate(click_url, 1);
  1660. click_url[0] = '\0';
  1661. } else {
  1662. navigate(url_in.text, 1);
  1663. }
  1664. }
  1665. if (action & ACT_BACK) {
  1666. if (hist_pos > 0) {
  1667. hist_pos--;
  1668. navigate(history[hist_pos], 0);
  1669. }
  1670. }
  1671. if (action & ACT_FORWARD) {
  1672. if (hist_pos + 1 < hist_len) {
  1673. hist_pos++;
  1674. navigate(history[hist_pos], 0);
  1675. }
  1676. }
  1677. if (action & ACT_RELOAD) {
  1678. if (s_eq(page_url, "about:home"))
  1679. load_home(0);
  1680. else
  1681. navigate(page_url, 0);
  1682. }
  1683. if (action & ACT_HOME)
  1684. load_home(1);
  1685. if (action & ACT_CLOSE)
  1686. quit = 1;
  1687. if (quit)
  1688. break;
  1689. u_sleep_ms(10);
  1690. }
  1691. u_win_close();
  1692. u_exit(0);
  1693. }