browser.c 48 KB

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