browser.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311
  1. /* browser: a small HTML/CSS browser built on ngui, the image library and the
  2. * shared uhttp client.
  3. *
  4. * No JavaScript and no forms; external CSS is loaded over uhttp, and images
  5. * are decoded with img.c and drawn inline. Large buffers (page body and the
  6. * image decode arena) live in the per-process SYS_MEM_ALLOC heap; the DOM and
  7. * the image/CSS I/O scratch use fixed static arenas.
  8. */
  9. #include "ucmd_lib.h"
  10. #include "ngui.h"
  11. #include "browser_html.h"
  12. #include "uhttp.h"
  13. #include "img.h"
  14. #include "inflate.h"
  15. #define BR_W 960
  16. #define BR_H 640
  17. /* CSS media queries are evaluated as a desktop browser. */
  18. #define BR_VIEWPORT_W 1000
  19. #define BR_TOOLBAR_H 30
  20. #define BR_STATUS_H 18
  21. #define BR_HEAP_INIT (6u * 1024u * 1024u)
  22. #define BR_HEAP_MAX (32u * 1024u * 1024u)
  23. #define BR_PAGE_MAX (2u * 1024u * 1024u)
  24. #define BR_IMG_FILE_MAX (512u * 1024u)
  25. #define BR_IMG_PIX_MAX (8u * 1024u * 1024u)
  26. #define BR_MAX_IMAGES 16
  27. #define BR_IMG_ARENA (24u * 1024u * 1024u)
  28. #define BR_IMG_LAYOUT_W 1000
  29. #define BR_CSS_MAX (832u * 1024u)
  30. #define BR_CSS_COMP_MAX (192u * 1024u)
  31. #define BR_MAX_SHEETS 6
  32. #define BR_HISTORY 16
  33. #define BR_URL_MAX 512
  34. #define BR_STATUS_MAX 200
  35. #define BR_IMG_RGB565(r, g, b) \
  36. ((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3)))
  37. #define BR_GRAY 0x8410
  38. enum {
  39. ID_URL = 1,
  40. ID_GO,
  41. ID_BACK,
  42. ID_FORWARD,
  43. ID_RELOAD,
  44. ID_HOME
  45. };
  46. #define ACT_NAV (1u << 0)
  47. #define ACT_BACK (1u << 1)
  48. #define ACT_FORWARD (1u << 2)
  49. #define ACT_RELOAD (1u << 3)
  50. #define ACT_HOME (1u << 4)
  51. #define ACT_CLOSE (1u << 5)
  52. #define ACT_WAIT_NAV (ACT_NAV | ACT_BACK | ACT_FORWARD | ACT_RELOAD | \
  53. ACT_HOME | ACT_CLOSE)
  54. struct br_image {
  55. int state; /* 0 empty, 1 ready */
  56. struct img_image img; /* scaled bitmap at <= BR_IMG_LAYOUT_W */
  57. int native_w, native_h;
  58. char err[32];
  59. };
  60. static struct ngui ui;
  61. static struct ngui_input url_in;
  62. static struct ngui_scroll page_scroll;
  63. static struct br_doc doc;
  64. static struct br_image images[BR_MAX_IMAGES];
  65. static int image_count;
  66. /* Images are loaded one per main-loop iteration so the parsed page stays
  67. * interactive while a slow network/TLS fetch or 4K decode is in progress. */
  68. static int preload_active;
  69. static int preload_pending;
  70. static int preload_wait;
  71. static unsigned int preload_node;
  72. static int preload_loaded;
  73. static int preload_scroll;
  74. static char post_status_text[BR_STATUS_MAX];
  75. static int post_status_error;
  76. static unsigned int post_status_ttl;
  77. /* Content-relative position/height of each <img>, recorded by the measure
  78. * pass so image loading can be deferred until it is near the viewport. */
  79. static unsigned short img_top[BR_MAX_NODES];
  80. static unsigned short img_h[BR_MAX_NODES];
  81. static unsigned char *heap;
  82. static unsigned int heap_size = 0;
  83. static unsigned int heap_used = 0;
  84. /* One reusable I/O buffer and one shared decode arena: decoded pixels are
  85. * scaled down to a display bitmap, then the arena is reused for the next
  86. * image. This keeps 4K screenshots inside the 32 MB heap. The two halves
  87. * double as the gzip input scratch for page/CSS fetches, where the heap has
  88. * no room for another multi-megabyte body. */
  89. static unsigned char *image_io_buf;
  90. #define image_file_buf (image_io_buf)
  91. #define image_comp_buf (image_io_buf + BR_IMG_FILE_MAX)
  92. #define BR_PAGE_COMP_MAX (2u * BR_IMG_FILE_MAX)
  93. static struct inflate image_inflate_ws;
  94. static unsigned char *image_arena;
  95. static unsigned int image_arena_size;
  96. static void *heap_alloc(unsigned int bytes, unsigned int align);
  97. #if BR_CSS_MAX + BR_CSS_COMP_MAX > 2u * BR_IMG_FILE_MAX
  98. #error "CSS scratch must fit in image_io_buf"
  99. #endif
  100. static char page_url[BR_URL_MAX];
  101. static char page_title[BR_TITLE_MAX];
  102. static char cwd[64];
  103. static int page_is_local;
  104. static int page_loading;
  105. static char status_text[BR_STATUS_MAX];
  106. static unsigned int status_until;
  107. static int status_error;
  108. static char history[BR_HISTORY][BR_URL_MAX];
  109. static int hist_len, hist_pos;
  110. static char click_url[BR_URL_MAX];
  111. static char pending_url[BR_URL_MAX];
  112. static unsigned int pending_action;
  113. static int cancel_fetch;
  114. static int quit;
  115. static struct ngui_rect page_rect;
  116. /* layout output */
  117. struct lay_run {
  118. unsigned short off, len;
  119. unsigned short gap;
  120. unsigned short color;
  121. unsigned short href;
  122. char lit[28];
  123. short img;
  124. unsigned short iw, ih;
  125. unsigned char scale, bold, underline;
  126. unsigned char has_lit;
  127. };
  128. struct lay {
  129. int draw;
  130. struct ngui *ui;
  131. int left, right;
  132. int y;
  133. int line_x, line_y, line_h;
  134. int run_x;
  135. int align;
  136. int depth;
  137. int space_pending;
  138. struct lay_run runs[96];
  139. int nruns;
  140. };
  141. struct hit {
  142. int x, y, w, h;
  143. unsigned short node;
  144. };
  145. #define HIT_MAX 96
  146. static struct hit hits[HIT_MAX];
  147. static int nhits;
  148. static unsigned short block_h[BR_MAX_NODES];
  149. static char li_marker[BR_MAX_NODES][6];
  150. /* one connection object; the browser fetches one resource at a time */
  151. /* ---- tiny strings ---- */
  152. static unsigned int s_len(const char *s)
  153. {
  154. unsigned int n = 0;
  155. while (s && s[n])
  156. n++;
  157. return n;
  158. }
  159. static void s_copy(char *d, const char *s, unsigned int cap)
  160. {
  161. unsigned int i = 0;
  162. if (!cap)
  163. return;
  164. while (s && s[i] && i + 1 < cap) {
  165. d[i] = s[i];
  166. i++;
  167. }
  168. d[i] = '\0';
  169. }
  170. static void s_append(char *d, unsigned int *n, const char *s, unsigned int cap)
  171. {
  172. while (s && *s && *n + 1 < cap)
  173. d[(*n)++] = *s++;
  174. d[*n] = '\0';
  175. }
  176. static void s_append_dec(char *d, unsigned int *n, unsigned int v,
  177. unsigned int cap)
  178. {
  179. char tmp[12];
  180. unsigned int i = 0, j;
  181. do {
  182. tmp[i++] = (char)('0' + v % 10);
  183. v /= 10;
  184. } while (v && i < sizeof(tmp));
  185. for (j = 0; j < i && *n + 1 < cap; j++)
  186. d[(*n)++] = tmp[i - 1 - j];
  187. d[*n] = '\0';
  188. }
  189. static int s_eq(const char *a, const char *b)
  190. {
  191. while (*a && *b) {
  192. if (*a != *b)
  193. return 0;
  194. a++;
  195. b++;
  196. }
  197. return *a == *b;
  198. }
  199. static int starts_with(const char *s, const char *p)
  200. {
  201. unsigned int i = 0;
  202. while (p[i]) {
  203. if (s[i] != p[i])
  204. return 0;
  205. i++;
  206. }
  207. return 1;
  208. }
  209. static int contains_ci(const char *s, const char *p)
  210. {
  211. unsigned int i, j;
  212. for (i = 0; s[i]; i++) {
  213. for (j = 0; p[j]; j++) {
  214. char a = s[i + j];
  215. if (a >= 'A' && a <= 'Z')
  216. a = (char)(a - 'A' + 'a');
  217. if (a != p[j])
  218. break;
  219. }
  220. if (!p[j])
  221. return 1;
  222. }
  223. return 0;
  224. }
  225. static char *trim(char *s)
  226. {
  227. unsigned int n;
  228. while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')
  229. s++;
  230. n = s_len(s);
  231. while (n && (s[n - 1] == ' ' || s[n - 1] == '\t' ||
  232. s[n - 1] == '\r' || s[n - 1] == '\n'))
  233. s[--n] = '\0';
  234. return s;
  235. }
  236. /* ---- per-process heap ---- */
  237. static int heap_start(void)
  238. {
  239. heap = (unsigned char *)u_mem_alloc(BR_HEAP_INIT);
  240. if (!heap) {
  241. heap_size = heap_used = 0;
  242. return -1;
  243. }
  244. heap_size = BR_HEAP_INIT;
  245. heap_used = 0;
  246. image_io_buf = (unsigned char *)heap_alloc(2u * BR_IMG_FILE_MAX, 16);
  247. if (!image_io_buf) {
  248. heap_size = heap_used = 0;
  249. return -1;
  250. }
  251. return 0;
  252. }
  253. static void heap_clear(void)
  254. {
  255. u_mem_free();
  256. heap = 0;
  257. heap_size = heap_used = 0;
  258. image_io_buf = 0;
  259. image_arena = 0;
  260. image_arena_size = 0;
  261. }
  262. static void *heap_alloc(unsigned int bytes, unsigned int align)
  263. {
  264. unsigned int start, want;
  265. if (!heap || bytes == 0)
  266. return 0;
  267. if (align < 1)
  268. align = 1;
  269. if (align & (align - 1))
  270. return 0;
  271. start = (heap_used + align - 1) & ~(align - 1);
  272. if (start > BR_HEAP_MAX || bytes > BR_HEAP_MAX - start)
  273. return 0;
  274. if (start + bytes > heap_size) {
  275. want = heap_size ? heap_size : BR_HEAP_INIT;
  276. while (want < start + bytes) {
  277. if (want > BR_HEAP_MAX / 2) {
  278. want = BR_HEAP_MAX;
  279. break;
  280. }
  281. want *= 2;
  282. }
  283. if (want < start + bytes || want > BR_HEAP_MAX)
  284. return 0;
  285. if (u_mem_grow(want) != 0)
  286. return 0;
  287. heap_size = want;
  288. }
  289. heap_used = start + bytes;
  290. return heap + start;
  291. }
  292. static void browser_rules_alloc(void)
  293. {
  294. struct br_rule *rules;
  295. rules = (struct br_rule *)heap_alloc(
  296. (unsigned int)(BR_MAX_RULES * sizeof(struct br_rule)), 16);
  297. br_set_rule_store(&doc, rules, rules ? BR_MAX_RULES : 0);
  298. }
  299. /* ---- status ---- */
  300. static void set_status(const char *s, int err, unsigned int ttl_ms)
  301. {
  302. s_copy(status_text, s, sizeof(status_text));
  303. status_error = err;
  304. status_until = ttl_ms ? u_ticks_ms() + ttl_ms : 0;
  305. ngui_invalidate(&ui);
  306. }
  307. /* ---- images ---- */
  308. static void image_reset(void)
  309. {
  310. int i;
  311. for (i = 0; i < BR_MAX_IMAGES; i++) {
  312. images[i].state = 0;
  313. images[i].native_w = images[i].native_h = 0;
  314. images[i].err[0] = '\0';
  315. }
  316. image_count = 0;
  317. preload_active = 0;
  318. preload_pending = 0;
  319. preload_wait = 0;
  320. preload_node = 0;
  321. preload_loaded = 0;
  322. }
  323. /* Decode into the shared arena, then copy/scale the result into a display
  324. * bitmap of at most BR_IMG_LAYOUT_W pixels wide. The arena is reused by the
  325. * next image, so the per-image cost is its scaled bitmap, not full 4K RGBA
  326. * pixels. Transparent edges are composited over white. */
  327. static int decode_image_data(const unsigned char *data, unsigned int len,
  328. struct br_image *bi)
  329. {
  330. struct img_info info;
  331. struct img_options opt;
  332. struct img_mem mem;
  333. struct img_image decoded;
  334. unsigned int est, bytes, i;
  335. unsigned short *px;
  336. int dw, dh, r;
  337. if (img_sniff(data, len, &info) != 0) {
  338. s_copy(bi->err, "unknown image format", sizeof(bi->err));
  339. return -1;
  340. }
  341. est = img_estimate(&info);
  342. if (est == 0xFFFFFFFFu || est > BR_IMG_ARENA) {
  343. s_copy(bi->err, "image too large", sizeof(bi->err));
  344. return -1;
  345. }
  346. if (!image_arena) {
  347. image_arena = (unsigned char *)heap_alloc(BR_IMG_ARENA, 16);
  348. if (!image_arena) {
  349. s_copy(bi->err, "out of memory", sizeof(bi->err));
  350. return -1;
  351. }
  352. image_arena_size = BR_IMG_ARENA;
  353. }
  354. img_mem_init(&mem, image_arena, image_arena_size);
  355. opt.max_pixels = BR_IMG_PIX_MAX;
  356. r = img_decode(data, len, &opt, &mem, &decoded);
  357. if (r != 0) {
  358. s_copy(bi->err, img_error_string(r), sizeof(bi->err));
  359. return -1;
  360. }
  361. if (decoded.w == 0 || decoded.h == 0) {
  362. s_copy(bi->err, "empty image", sizeof(bi->err));
  363. return -1;
  364. }
  365. bi->native_w = (int)decoded.w;
  366. bi->native_h = (int)decoded.h;
  367. dw = (int)decoded.w;
  368. dh = (int)decoded.h;
  369. if (dw > BR_IMG_LAYOUT_W) {
  370. dh = (int)((unsigned int)decoded.h * BR_IMG_LAYOUT_W /
  371. decoded.w);
  372. dw = BR_IMG_LAYOUT_W;
  373. }
  374. if (dh < 1)
  375. dh = 1;
  376. if (dw < 1)
  377. dw = 1;
  378. bytes = (unsigned int)dw * (unsigned int)dh * 2u;
  379. px = (unsigned short *)heap_alloc(bytes, 16);
  380. if (!px) {
  381. s_copy(bi->err, "out of memory", sizeof(bi->err));
  382. return -1;
  383. }
  384. for (i = 0; i < (unsigned int)dw * (unsigned int)dh; i++)
  385. px[i] = ui.theme.bg;
  386. {
  387. struct img_blit b;
  388. b.dst = px;
  389. b.dst_w = dw;
  390. b.dst_h = dh;
  391. b.clip_x = 0;
  392. b.clip_y = 0;
  393. b.clip_w = dw;
  394. b.clip_h = dh;
  395. b.x = 0;
  396. b.y = 0;
  397. b.scale = ((unsigned int)dw << 16) / decoded.w;
  398. b.bg = ui.theme.bg;
  399. b.bg2 = ui.theme.bg;
  400. b.flags = IMG_BLIT_OVER_DST;
  401. img_draw(&decoded, &b);
  402. }
  403. bi->img.format = decoded.format;
  404. bi->img.w = (unsigned int)dw;
  405. bi->img.h = (unsigned int)dh;
  406. bi->img.px = px;
  407. bi->img.alpha = 0;
  408. bi->img.flags = 0;
  409. bi->state = 1;
  410. return 0;
  411. }
  412. /* ---- URL / local path handling ---- */
  413. static int is_http(const char *s)
  414. {
  415. return starts_with(s, "http://") || starts_with(s, "https://");
  416. }
  417. /* Collapse "/./", "/../" and duplicate slashes in an absolute path. */
  418. static int normalize_abs(const char *in, char *out, unsigned int cap)
  419. {
  420. char seg[64];
  421. unsigned int i = 0, o = 1;
  422. int depth = 0;
  423. int trailing;
  424. if (!in || in[0] != '/' || cap < 2)
  425. return -1;
  426. trailing = 0;
  427. {
  428. unsigned int n = s_len(in);
  429. if (n > 1 && in[n - 1] == '/')
  430. trailing = 1;
  431. }
  432. out[0] = '/';
  433. while (in[i]) {
  434. unsigned int n = 0;
  435. while (in[i] == '/')
  436. i++;
  437. while (in[i] && in[i] != '/' && in[i] != '?' && in[i] != '#') {
  438. if (n + 1 < sizeof(seg))
  439. seg[n++] = in[i];
  440. i++;
  441. }
  442. seg[n] = '\0';
  443. if (n == 0)
  444. break;
  445. if (s_eq(seg, "."))
  446. continue;
  447. if (s_eq(seg, "..")) {
  448. if (depth > 0) {
  449. while (o > 1 && out[o - 1] != '/')
  450. o--;
  451. if (o > 1)
  452. o--;
  453. depth--;
  454. }
  455. continue;
  456. }
  457. if (o > 1) {
  458. if (o + 1 >= cap)
  459. return -1;
  460. out[o++] = '/';
  461. }
  462. if (o + n + 1 >= cap)
  463. return -1;
  464. {
  465. unsigned int j;
  466. for (j = 0; j < n; j++)
  467. out[o++] = seg[j];
  468. }
  469. depth++;
  470. }
  471. if (trailing && o > 1) {
  472. if (o + 1 >= cap)
  473. return -1;
  474. out[o++] = '/';
  475. }
  476. out[o] = '\0';
  477. return 0;
  478. }
  479. static void make_abs_local(const char *in, char *out, unsigned int cap)
  480. {
  481. char joined[BR_URL_MAX];
  482. if (!in || !in[0]) {
  483. s_copy(out, cwd[0] ? cwd : "/", cap);
  484. return;
  485. }
  486. if (in[0] == '/') {
  487. if (normalize_abs(in, out, cap) != 0)
  488. s_copy(out, in, cap);
  489. return;
  490. }
  491. {
  492. unsigned int n = 0;
  493. s_append(joined, &n, cwd[0] ? cwd : "/", sizeof(joined));
  494. s_append(joined, &n, "/", sizeof(joined));
  495. s_append(joined, &n, in, sizeof(joined));
  496. }
  497. if (normalize_abs(joined, out, cap) != 0)
  498. s_copy(out, joined, cap);
  499. }
  500. static int path_dir(const char *path, char *out, unsigned int cap)
  501. {
  502. unsigned int i, last = 0;
  503. for (i = 0; path[i]; i++)
  504. if (path[i] == '/')
  505. last = i;
  506. if (last == 0) {
  507. if (cap < 2)
  508. return -1;
  509. out[0] = '/';
  510. out[1] = '\0';
  511. return 0;
  512. }
  513. if (last + 1 >= cap)
  514. return -1;
  515. {
  516. unsigned int j;
  517. for (j = 0; j < last; j++)
  518. out[j] = path[j];
  519. out[last] = '\0';
  520. }
  521. return 0;
  522. }
  523. /* Resolve an href against the current page. 0 = ok, -1 = ignore. */
  524. static int resolve_link(const char *base, const char *href, char *out,
  525. unsigned int cap)
  526. {
  527. char joined[BR_URL_MAX];
  528. if (!href || !href[0] || href[0] == '#')
  529. return -1;
  530. if (starts_with(href, "mailto:") || starts_with(href, "javascript:") ||
  531. starts_with(href, "data:") || starts_with(href, "tel:"))
  532. return -1;
  533. if (starts_with(href, "about:")) {
  534. s_copy(out, href, cap);
  535. return 0;
  536. }
  537. if (is_http(href)) {
  538. s_copy(out, href, cap);
  539. return 0;
  540. }
  541. if (starts_with(href, "//")) {
  542. if (is_http(base)) {
  543. unsigned int n = 0;
  544. s_append(out, &n, starts_with(base, "https:") ?
  545. "https:" : "http:", cap);
  546. s_append(out, &n, href, cap);
  547. return 0;
  548. }
  549. return -1;
  550. }
  551. if (is_http(base)) {
  552. return uhttp_resolve(base, href, out, cap) == 0 ? 0 : -1;
  553. }
  554. /* local page */
  555. if (href[0] == '/') {
  556. if (normalize_abs(href, out, cap) != 0)
  557. return -1;
  558. return 0;
  559. }
  560. {
  561. char dir[BR_URL_MAX];
  562. unsigned int n = 0;
  563. if (s_eq(base, "about:home"))
  564. s_copy(dir, cwd[0] ? cwd : "/", sizeof(dir));
  565. else if (path_dir(base, dir, sizeof(dir)) != 0)
  566. s_copy(dir, "/", sizeof(dir));
  567. s_append(joined, &n, dir, sizeof(joined));
  568. if (n == 0 || joined[n - 1] != '/')
  569. s_append(joined, &n, "/", sizeof(joined));
  570. s_append(joined, &n, href, sizeof(joined));
  571. }
  572. return normalize_abs(joined, out, cap) == 0 ? 0 : -1;
  573. }
  574. /* Best-effort decompression: keep the first `cap` bytes and discard the
  575. * rest instead of failing when a page or stylesheet is larger than the
  576. * buffer. The stream is still walked to the end, so a normal page is
  577. * verified; a truncated compressed input still returns whatever was decoded
  578. * so far. */
  579. struct trunc_buf {
  580. unsigned char *dst;
  581. unsigned int cap, pos;
  582. int full;
  583. };
  584. static int trunc_write(void *ctx, const unsigned char *data, unsigned int len)
  585. {
  586. struct trunc_buf *m = (struct trunc_buf *)ctx;
  587. if (len > m->cap - m->pos) {
  588. unsigned int room = m->cap - m->pos;
  589. if (room) {
  590. unsigned int i;
  591. for (i = 0; i < room; i++)
  592. m->dst[m->pos + i] = data[i];
  593. m->pos += room;
  594. }
  595. m->full = 1;
  596. return 0;
  597. }
  598. {
  599. unsigned int i;
  600. for (i = 0; i < len; i++)
  601. m->dst[m->pos + i] = data[i];
  602. }
  603. m->pos += len;
  604. return 0;
  605. }
  606. static void worker_wait_ui(void);
  607. static void inflate_ui_pump(void *ctx)
  608. {
  609. static unsigned int last;
  610. unsigned int now = u_ticks_ms();
  611. (void)ctx;
  612. if ((int)(now - last) >= 50) {
  613. last = now;
  614. worker_wait_ui();
  615. }
  616. }
  617. static int inflate_trunc(const unsigned char *src, unsigned int len,
  618. unsigned char *dst, unsigned int cap,
  619. unsigned int *outlen, int gzip, int *truncated)
  620. {
  621. struct inflate_src in;
  622. struct inflate_sink sink;
  623. struct trunc_buf m;
  624. unsigned int got = 0;
  625. int r;
  626. in.p = src;
  627. in.len = len;
  628. in.more = 0;
  629. in.ctx = 0;
  630. m.dst = dst;
  631. m.cap = cap;
  632. m.pos = 0;
  633. m.full = 0;
  634. sink.write = trunc_write;
  635. sink.ctx = &m;
  636. inflate_init(&image_inflate_ws, &in, &sink);
  637. image_inflate_ws.progress = inflate_ui_pump;
  638. image_inflate_ws.progress_ctx = 0;
  639. r = gzip ? inflate_gzip(&image_inflate_ws, &got) :
  640. inflate_zlib(&image_inflate_ws, &got);
  641. *outlen = m.pos;
  642. *truncated = m.full || r != 0;
  643. if (r != 0 && m.pos == 0)
  644. return -1;
  645. return 0;
  646. }
  647. /* ---- headless fetch helper ------------------------------------------- *
  648. *
  649. * Network I/O runs in a separate ring-3 process (`curl __fetch`): it writes
  650. * the raw response and a small metadata file under /share. While it works,
  651. * the browser keeps drawing and scrolling its own window, so a slow DNS/TLS
  652. * handshake or a multi-megabyte body never freezes the UI.
  653. */
  654. #define FETCH_REQ "/share/bfreq"
  655. #define FETCH_BODY "/share/bfbody"
  656. #define FETCH_META "/share/bfmeta"
  657. #define FETCH_DONE "/share/bfdone"
  658. #define FETCH_ABORT (-2)
  659. struct fetch_meta {
  660. int status;
  661. int error;
  662. int trunc;
  663. unsigned int len;
  664. char encoding[16];
  665. char ctype[64];
  666. char final[BR_URL_MAX];
  667. };
  668. static void draw(unsigned int *action);
  669. static void handle_keys(unsigned int *action);
  670. static void handle_page_click(unsigned int *action);
  671. static int fetch_helper_tty = -2;
  672. static int fetch_helper_pid = -1;
  673. static unsigned int fetch_seq;
  674. static struct fetch_meta fetch_meta;
  675. static unsigned int meta_uint(char **pp)
  676. {
  677. char *p = *pp;
  678. unsigned int v = 0;
  679. while (*p >= '0' && *p <= '9') {
  680. v = v * 10 + (unsigned int)(*p - '0');
  681. p++;
  682. }
  683. while (*p && *p != '\n')
  684. p++;
  685. if (*p == '\n')
  686. p++;
  687. *pp = p;
  688. return v;
  689. }
  690. static void meta_str(char **pp, char *out, unsigned int cap)
  691. {
  692. char *p = *pp;
  693. unsigned int n = 0;
  694. while (*p && *p != '\n' && n + 1 < cap)
  695. out[n++] = *p++;
  696. out[n] = '\0';
  697. while (*p && *p != '\n')
  698. p++;
  699. if (*p == '\n')
  700. p++;
  701. *pp = p;
  702. }
  703. static void worker_wait_ui(void)
  704. {
  705. unsigned int action = 0;
  706. if (!ngui_begin(&ui))
  707. return;
  708. draw(&action);
  709. handle_keys(&action);
  710. handle_page_click(&action);
  711. if (action & ACT_WAIT_NAV) {
  712. pending_action |= action & ACT_WAIT_NAV;
  713. if ((action & ACT_NAV) && click_url[0])
  714. s_copy(pending_url, click_url, sizeof(pending_url));
  715. click_url[0] = '\0';
  716. cancel_fetch = 1;
  717. }
  718. if (!ngui_focus(&ui) && ngui_scroll_keys(&ui, &page_scroll))
  719. ngui_invalidate(&ui);
  720. ngui_end(&ui);
  721. }
  722. static void fetch_helper_stop(void)
  723. {
  724. int i;
  725. if (fetch_helper_pid > 0) {
  726. u_kill((unsigned int)fetch_helper_pid);
  727. for (i = 0; i < 40 && u_alive((unsigned int)fetch_helper_pid); i++)
  728. u_sleep_ms(5);
  729. /* Let the reaper release the NIC before the next request. */
  730. u_sleep_ms(10);
  731. }
  732. fetch_helper_pid = -1;
  733. }
  734. static int fetch_helper_start(char *err, unsigned int errcap)
  735. {
  736. if (fetch_helper_pid > 0 &&
  737. u_alive((unsigned int)fetch_helper_pid))
  738. return 0;
  739. fetch_helper_pid = -1;
  740. if (fetch_helper_tty == -2) {
  741. int t = u_tty_create();
  742. fetch_helper_tty = t > 0 ? t : 0;
  743. }
  744. fetch_helper_pid = u_spawn("/ramfs/bin/curl", "__serve",
  745. fetch_helper_tty, "/");
  746. if (fetch_helper_pid <= 0) {
  747. fetch_helper_pid = -1;
  748. s_copy(err, "cannot start network helper", errcap);
  749. return -1;
  750. }
  751. return 0;
  752. }
  753. static int worker_fetch(const char *url, unsigned int cap,
  754. struct fetch_meta *m, char *err, unsigned int errcap)
  755. {
  756. static char req[768], meta[1024], done[24];
  757. char *p;
  758. unsigned int n = 0, deadline;
  759. int rn, mseq, retried = 0;
  760. struct sys_stat st;
  761. if (cancel_fetch || pending_action)
  762. return FETCH_ABORT;
  763. if (u_stat("/share", &st) != 0 || !st.is_dir ||
  764. u_stat("/ramfs/bin/curl", &st) != 0) {
  765. s_copy(err, "network helper unavailable", errcap);
  766. return -1;
  767. }
  768. fetch_seq++;
  769. if (!fetch_seq)
  770. fetch_seq = 1;
  771. s_append_dec(req, &n, fetch_seq, sizeof(req));
  772. s_append(req, &n, " ", sizeof(req));
  773. s_append_dec(req, &n, cap, sizeof(req));
  774. s_append(req, &n, " ", sizeof(req));
  775. s_append(req, &n, url, sizeof(req));
  776. s_append(req, &n, "\n", sizeof(req));
  777. u_syscall1(SYS_UNLINK, (int)FETCH_DONE);
  778. u_syscall1(SYS_UNLINK, (int)FETCH_META);
  779. if (u_syscall3(SYS_WRITE_FILE, (int)FETCH_REQ, (int)req, (int)n) < 0) {
  780. s_copy(err, "cannot queue network request", errcap);
  781. return -1;
  782. }
  783. if (fetch_helper_start(err, errcap) != 0)
  784. return -1;
  785. deadline = u_ticks_ms() + UHTTP_TOTAL_TIMEOUT_MS + 5000;
  786. for (;;) {
  787. worker_wait_ui();
  788. if (cancel_fetch || pending_action) {
  789. fetch_helper_stop();
  790. return FETCH_ABORT;
  791. }
  792. rn = u_syscall3(SYS_READ_FILE, (int)FETCH_DONE, (int)done,
  793. (int)(sizeof(done) - 1));
  794. if (rn > 0) {
  795. done[rn] = '\0';
  796. p = done;
  797. mseq = (int)meta_uint(&p);
  798. if (mseq == (int)fetch_seq)
  799. break;
  800. }
  801. if (!u_alive((unsigned int)fetch_helper_pid)) {
  802. fetch_helper_pid = -1;
  803. if (!retried && fetch_helper_start(err, errcap) == 0) {
  804. retried = 1;
  805. continue;
  806. }
  807. s_copy(err, "network helper stopped", errcap);
  808. return -1;
  809. }
  810. if ((int)(u_ticks_ms() - deadline) >= 0) {
  811. fetch_helper_stop();
  812. s_copy(err, "network request timed out", errcap);
  813. return -1;
  814. }
  815. u_sleep_ms(20);
  816. }
  817. if (cancel_fetch || pending_action)
  818. return FETCH_ABORT;
  819. rn = u_syscall3(SYS_READ_FILE, (int)FETCH_META, (int)meta,
  820. (int)(sizeof(meta) - 1));
  821. if (rn <= 0) {
  822. s_copy(err, "network helper failed", errcap);
  823. return -1;
  824. }
  825. meta[rn] = '\0';
  826. p = meta;
  827. mseq = (int)meta_uint(&p);
  828. if (mseq != (int)fetch_seq) {
  829. s_copy(err, "stale network response", errcap);
  830. return -1;
  831. }
  832. m->status = (int)meta_uint(&p);
  833. m->error = (int)meta_uint(&p);
  834. m->trunc = (int)meta_uint(&p);
  835. meta_str(&p, m->encoding, sizeof(m->encoding));
  836. meta_str(&p, m->ctype, sizeof(m->ctype));
  837. meta_str(&p, m->final, sizeof(m->final));
  838. m->len = meta_uint(&p);
  839. if (m->error) {
  840. s_copy(err, uhttp_error_string(m->error), errcap);
  841. return -1;
  842. }
  843. return 0;
  844. }
  845. static int fetch_remote(const char *url, unsigned int cap,
  846. unsigned char *plain, unsigned int plaincap,
  847. unsigned char *comp, unsigned int compcap,
  848. struct fetch_meta *m, int *truncated,
  849. char *err, unsigned int errcap)
  850. {
  851. int rv, zr, was_trunc;
  852. rv = worker_fetch(url, cap, m, err, errcap);
  853. if (rv == FETCH_ABORT)
  854. return FETCH_ABORT;
  855. if (rv != 0)
  856. return -1;
  857. if (contains_ci(m->encoding, "gzip") ||
  858. contains_ci(m->encoding, "deflate")) {
  859. unsigned int comp_len = 0, plen = 0;
  860. rv = u_syscall3(SYS_READ_FILE, (int)FETCH_BODY, (int)comp,
  861. (int)compcap);
  862. if (rv < 0) {
  863. s_copy(err, "empty response", errcap);
  864. return -1;
  865. }
  866. comp_len = (unsigned int)rv;
  867. if (comp_len < m->len) {
  868. s_copy(err, "compressed response is too large", errcap);
  869. return -1;
  870. }
  871. if (contains_ci(m->encoding, "deflate"))
  872. zr = inflate_trunc(comp, comp_len, plain, plaincap,
  873. &plen, 0, &was_trunc);
  874. else
  875. zr = inflate_trunc(comp, comp_len, plain, plaincap,
  876. &plen, 1, &was_trunc);
  877. if (zr != 0) {
  878. s_copy(err, "compressed response is corrupt", errcap);
  879. return -1;
  880. }
  881. *truncated = m->trunc || was_trunc;
  882. return (cancel_fetch || pending_action) ? FETCH_ABORT :
  883. (int)plen;
  884. }
  885. rv = u_syscall3(SYS_READ_FILE, (int)FETCH_BODY, (int)plain,
  886. (int)plaincap);
  887. if (rv < 0) {
  888. s_copy(err, "empty response", errcap);
  889. return -1;
  890. }
  891. *truncated = m->trunc || ((unsigned int)rv < m->len);
  892. return (cancel_fetch || pending_action) ? FETCH_ABORT : rv;
  893. }
  894. /* ---- HTTP / file fetch ---- */
  895. static int read_url(const char *url, unsigned int cap, unsigned char **out,
  896. unsigned int *outlen, int *is_local, char *ctype,
  897. unsigned int ctcap, char *final, unsigned int finalcap,
  898. char *err, unsigned int errcap, int *truncated)
  899. {
  900. unsigned char *buf;
  901. unsigned int len;
  902. int tr = 0;
  903. int rv;
  904. *out = 0;
  905. *outlen = 0;
  906. *truncated = 0;
  907. if (final && finalcap)
  908. final[0] = '\0';
  909. if (ctype && ctcap)
  910. ctype[0] = '\0';
  911. if (url[0] == '/') {
  912. buf = (unsigned char *)heap_alloc(cap, 16);
  913. if (!buf) {
  914. s_copy(err, "out of memory", errcap);
  915. return -1;
  916. }
  917. rv = u_syscall3(SYS_READ_FILE, (int)url, (int)buf, (int)cap);
  918. if (rv < 0) {
  919. s_copy(err, "file not found", errcap);
  920. return -1;
  921. }
  922. *out = buf;
  923. *outlen = (unsigned int)rv;
  924. *is_local = 1;
  925. if (ctype)
  926. s_copy(ctype, "text/html", ctcap);
  927. if (final)
  928. s_copy(final, url, finalcap);
  929. if ((unsigned int)rv == cap)
  930. *truncated = 1;
  931. return 0;
  932. }
  933. buf = (unsigned char *)heap_alloc(cap, 16);
  934. if (!buf) {
  935. s_copy(err, "out of memory", errcap);
  936. return -1;
  937. }
  938. rv = fetch_remote(url, cap, buf, cap, image_io_buf, BR_PAGE_COMP_MAX,
  939. &fetch_meta, &tr, err, errcap);
  940. if (rv == FETCH_ABORT)
  941. return FETCH_ABORT;
  942. if (rv < 0)
  943. return -1;
  944. len = (unsigned int)rv;
  945. if (len == 0) {
  946. s_copy(err, "empty response", errcap);
  947. return -1;
  948. }
  949. if (ctype)
  950. s_copy(ctype, fetch_meta.ctype, ctcap);
  951. if (final && fetch_meta.final[0])
  952. s_copy(final, fetch_meta.final, finalcap);
  953. *is_local = 0;
  954. *out = buf;
  955. *outlen = len;
  956. *truncated = tr;
  957. return 0;
  958. }
  959. /* Fetch an image into the fixed scratch buffers. The page heap is already
  960. * carrying the 24 MB decode arena and every scaled bitmap, so images must not
  961. * allocate their file body from it as well. */
  962. static int fetch_image(const char *url, unsigned int cap,
  963. unsigned char **out, unsigned int *outlen,
  964. char *err, unsigned int errcap)
  965. {
  966. unsigned int len;
  967. int tr = 0;
  968. int rv;
  969. *out = 0;
  970. *outlen = 0;
  971. if (cap > BR_IMG_FILE_MAX)
  972. cap = BR_IMG_FILE_MAX;
  973. if (url[0] == '/') {
  974. rv = u_syscall3(SYS_READ_FILE, (int)url, (int)image_file_buf,
  975. (int)cap);
  976. if (rv < 0) {
  977. s_copy(err, "file not found", errcap);
  978. return -1;
  979. }
  980. *out = image_file_buf;
  981. *outlen = (unsigned int)rv;
  982. return 0;
  983. }
  984. rv = fetch_remote(url, cap, image_file_buf, cap, image_comp_buf,
  985. BR_IMG_FILE_MAX, &fetch_meta, &tr, err, errcap);
  986. if (rv == FETCH_ABORT)
  987. return FETCH_ABORT;
  988. if (rv < 0)
  989. return -1;
  990. len = (unsigned int)rv;
  991. if (len == 0) {
  992. s_copy(err, "empty image", errcap);
  993. return -1;
  994. }
  995. *out = image_file_buf;
  996. *outlen = len;
  997. return 0;
  998. }
  999. /* Read one stylesheet into the shared static buffer. CSS is parsed
  1000. * immediately, so it does not need the page heap; this leaves the heap for
  1001. * the 2 MB page body, the decode arena and the scaled image bitmaps. */
  1002. static int fetch_css(const char *url, unsigned char **out, unsigned int *outlen,
  1003. char *err, unsigned int errcap)
  1004. {
  1005. unsigned int len;
  1006. int tr = 0;
  1007. int rv;
  1008. *out = 0;
  1009. *outlen = 0;
  1010. if (url[0] == '/') {
  1011. rv = u_syscall3(SYS_READ_FILE, (int)url, (int)image_io_buf,
  1012. (int)BR_CSS_MAX);
  1013. if (rv < 0) {
  1014. s_copy(err, "stylesheet not found", errcap);
  1015. return -1;
  1016. }
  1017. *out = image_io_buf;
  1018. *outlen = (unsigned int)rv;
  1019. return 0;
  1020. }
  1021. rv = fetch_remote(url, BR_CSS_MAX, image_io_buf, BR_CSS_MAX,
  1022. image_io_buf + BR_CSS_MAX, BR_CSS_COMP_MAX,
  1023. &fetch_meta, &tr, err, errcap);
  1024. if (rv == FETCH_ABORT)
  1025. return FETCH_ABORT;
  1026. if (rv < 0)
  1027. return -1;
  1028. len = (unsigned int)rv;
  1029. if (len == 0) {
  1030. s_copy(err, "empty stylesheet", errcap);
  1031. return -1;
  1032. }
  1033. *out = image_io_buf;
  1034. *outlen = len;
  1035. return 0;
  1036. }
  1037. static void redraw_now(void);
  1038. static char error_page[1024];
  1039. static void show_error(const char *title, const char *detail)
  1040. {
  1041. char *page = error_page;
  1042. unsigned int n = 0;
  1043. s_append(page, &n, "<html><body><h2>", sizeof(error_page));
  1044. s_append(page, &n, title, sizeof(error_page));
  1045. s_append(page, &n, "</h2><p>", sizeof(error_page));
  1046. {
  1047. const char *p = detail;
  1048. while (*p && n + 1 < sizeof(error_page)) {
  1049. if (*p == '<') s_append(page, &n, "&lt;", sizeof(error_page));
  1050. else if (*p == '>') s_append(page, &n, "&gt;", sizeof(error_page));
  1051. else if (*p == '&') s_append(page, &n, "&amp;", sizeof(error_page));
  1052. else page[n++] = *p;
  1053. p++;
  1054. }
  1055. }
  1056. page[n] = '\0';
  1057. s_append(page, &n, "</p><p>", sizeof(error_page));
  1058. s_append(page, &n, page_url, sizeof(error_page));
  1059. s_append(page, &n, "</p><p><a href=\"/\">home</a></p></body></html>",
  1060. sizeof(error_page));
  1061. br_parse(&doc, page, n);
  1062. preload_active = 0;
  1063. preload_pending = 0;
  1064. preload_wait = 0;
  1065. s_copy(page_title, title, sizeof(page_title));
  1066. page_loading = 0;
  1067. set_status(title, 1, 0);
  1068. ngui_invalidate(&ui);
  1069. }
  1070. static int decode_direct_image(const unsigned char *data, unsigned int len,
  1071. unsigned int node)
  1072. {
  1073. if (image_count >= BR_MAX_IMAGES)
  1074. return -1;
  1075. if (decode_image_data(data, len, &images[0]) != 0)
  1076. return -1;
  1077. doc.nodes[node].img_index = 0;
  1078. image_count = 1;
  1079. return 0;
  1080. }
  1081. static int ancestor_is(unsigned int node, unsigned int tag)
  1082. {
  1083. unsigned int p = doc.nodes[node].parent;
  1084. while (p) {
  1085. if (doc.nodes[p].tag == tag)
  1086. return 1;
  1087. p = doc.nodes[p].parent;
  1088. }
  1089. return 0;
  1090. }
  1091. static int load_stylesheets(void)
  1092. {
  1093. unsigned int i, sheets = 0;
  1094. for (i = 0; i < doc.nnode && sheets < BR_MAX_SHEETS; i++) {
  1095. struct br_node *nd = &doc.nodes[i];
  1096. const char *rel, *href;
  1097. char url[BR_URL_MAX];
  1098. unsigned char *data;
  1099. unsigned int len;
  1100. char err[64];
  1101. int rv;
  1102. if (nd->type != BR_NODE_ELEM || nd->tag != BR_TAG_LINK)
  1103. continue;
  1104. rel = br_rel(&doc, i);
  1105. if (!contains_ci(rel, "stylesheet"))
  1106. continue;
  1107. href = br_href(&doc, i);
  1108. if (!href[0])
  1109. continue;
  1110. if (resolve_link(page_url, href, url, sizeof(url)) != 0)
  1111. continue;
  1112. rv = fetch_css(url, &data, &len, err, sizeof(err));
  1113. if (rv == FETCH_ABORT)
  1114. return FETCH_ABORT;
  1115. if (rv != 0)
  1116. continue;
  1117. if (len)
  1118. br_parse_css_text(&doc, (const char *)data, len);
  1119. sheets++;
  1120. }
  1121. br_parse_inline_css(&doc);
  1122. br_reset_styles(&doc);
  1123. return 0;
  1124. }
  1125. static void preload_begin(void)
  1126. {
  1127. preload_active = 1;
  1128. preload_pending = 0;
  1129. preload_wait = 1;
  1130. preload_node = 0;
  1131. preload_loaded = 0;
  1132. preload_scroll = 0;
  1133. }
  1134. /* Load at most one image per call. Images below the current viewport are
  1135. * left pending until the page is scrolled; this keeps a long article with
  1136. * many 4K screenshots from stalling on content the user cannot see yet. */
  1137. static void preload_step(void)
  1138. {
  1139. while (preload_active && preload_node < doc.nnode) {
  1140. unsigned int i = preload_node;
  1141. struct br_node *nd = &doc.nodes[i];
  1142. const char *src;
  1143. char url[BR_URL_MAX];
  1144. unsigned char *data;
  1145. unsigned int len;
  1146. char err[64];
  1147. char msg[64];
  1148. unsigned int n = 0;
  1149. int rv;
  1150. if (nd->type != BR_NODE_ELEM || nd->tag != BR_TAG_IMG ||
  1151. nd->img_index >= 0) {
  1152. preload_node++;
  1153. continue;
  1154. }
  1155. if (ancestor_is(i, BR_TAG_NOSCRIPT)) {
  1156. preload_node++;
  1157. continue;
  1158. }
  1159. src = br_src(&doc, i);
  1160. if (!src[0] || contains_ci(src, ".svg") ||
  1161. starts_with(src, "data:")) {
  1162. preload_node++;
  1163. continue;
  1164. }
  1165. if (resolve_link(page_url, src, url, sizeof(url)) != 0) {
  1166. preload_node++;
  1167. continue;
  1168. }
  1169. if (image_count >= BR_MAX_IMAGES) {
  1170. preload_node = doc.nnode;
  1171. break;
  1172. }
  1173. if (img_top[i] >
  1174. (unsigned int)(page_scroll.offset + page_rect.h + 160)) {
  1175. preload_active = 0;
  1176. preload_pending = 1;
  1177. preload_scroll = page_scroll.offset;
  1178. set_status(post_status_text, post_status_error,
  1179. post_status_ttl);
  1180. ngui_invalidate(&ui);
  1181. return;
  1182. }
  1183. preload_node = i + 1;
  1184. s_append(msg, &n, "Loading images ", sizeof(msg));
  1185. s_append_dec(msg, &n, (unsigned int)preload_loaded + 1,
  1186. sizeof(msg));
  1187. s_append(msg, &n, "...", sizeof(msg));
  1188. set_status(msg, 0, 0);
  1189. ngui_invalidate(&ui);
  1190. data = 0;
  1191. len = 0;
  1192. rv = fetch_image(url, BR_IMG_FILE_MAX, &data, &len, err,
  1193. sizeof(err));
  1194. if (rv == FETCH_ABORT) {
  1195. preload_active = 0;
  1196. preload_pending = 0;
  1197. return;
  1198. }
  1199. if (rv == 0 &&
  1200. decode_image_data(data, len, &images[image_count]) == 0) {
  1201. nd->img_index = (short)image_count;
  1202. image_count++;
  1203. preload_loaded++;
  1204. }
  1205. ngui_invalidate(&ui);
  1206. return;
  1207. }
  1208. preload_active = 0;
  1209. preload_pending = 0;
  1210. set_status(post_status_text, post_status_error, post_status_ttl);
  1211. ngui_invalidate(&ui);
  1212. }
  1213. static void history_push(const char *url)
  1214. {
  1215. if (!url || !url[0])
  1216. return;
  1217. if (hist_len > 0 && hist_pos >= 0 && hist_pos < hist_len &&
  1218. s_eq(history[hist_pos], url))
  1219. return;
  1220. if (hist_len > 0 && hist_pos + 1 < hist_len)
  1221. hist_len = hist_pos + 1;
  1222. if (hist_len < BR_HISTORY) {
  1223. s_copy(history[hist_len], url, BR_URL_MAX);
  1224. hist_pos = hist_len;
  1225. hist_len++;
  1226. } else {
  1227. int i;
  1228. for (i = 1; i < BR_HISTORY; i++)
  1229. s_copy(history[i - 1], history[i], BR_URL_MAX);
  1230. s_copy(history[BR_HISTORY - 1], url, BR_URL_MAX);
  1231. hist_pos = BR_HISTORY - 1;
  1232. }
  1233. }
  1234. static void load_page(const char *url, int local, int push)
  1235. {
  1236. unsigned char *body = 0;
  1237. unsigned int len = 0;
  1238. int truncated = 0;
  1239. struct img_info image_info;
  1240. char ctype[64];
  1241. char final[BR_URL_MAX];
  1242. char err[80];
  1243. const char *detail = 0;
  1244. int rv;
  1245. final[0] = '\0';
  1246. cancel_fetch = 0;
  1247. preload_active = 0;
  1248. preload_pending = 0;
  1249. heap_clear();
  1250. image_reset();
  1251. page_scroll.offset = 0;
  1252. page_scroll.dragging = 0;
  1253. br_parse_plain(&doc, "", 0); /* blank while loading */
  1254. page_title[0] = '\0';
  1255. page_loading = 1;
  1256. page_is_local = local;
  1257. s_copy(page_url, url, sizeof(page_url));
  1258. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  1259. url_in.len = s_len(url_in.text);
  1260. url_in.cursor = url_in.len;
  1261. url_in.scroll = 0;
  1262. ngui_set_focus(&ui, 0);
  1263. if (final[0])
  1264. final[0] = '\0';
  1265. if (push)
  1266. history_push(url);
  1267. set_status("Loading...", 0, 0);
  1268. ngui_invalidate(&ui);
  1269. redraw_now();
  1270. if (heap_start() != 0) {
  1271. show_error("Out of memory", "Cannot allocate the page buffer");
  1272. return;
  1273. }
  1274. browser_rules_alloc();
  1275. rv = read_url(url, BR_PAGE_MAX, &body, &len, &local, ctype,
  1276. sizeof(ctype), final, sizeof(final), err, sizeof(err),
  1277. &truncated);
  1278. if (rv == FETCH_ABORT)
  1279. return;
  1280. if (rv != 0) {
  1281. detail = err;
  1282. show_error("Cannot load this page", detail);
  1283. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  1284. url_in.len = s_len(url_in.text);
  1285. url_in.cursor = url_in.len;
  1286. url_in.scroll = 0;
  1287. return;
  1288. }
  1289. page_is_local = local;
  1290. if (final[0]) {
  1291. s_copy(page_url, final, sizeof(page_url));
  1292. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  1293. url_in.len = s_len(url_in.text);
  1294. url_in.cursor = url_in.len;
  1295. url_in.scroll = 0;
  1296. }
  1297. if (contains_ci(ctype, "text/plain")) {
  1298. br_parse_plain(&doc, (const char *)body, len);
  1299. } else if (contains_ci(ctype, "image/") ||
  1300. (!contains_ci(ctype, "text/html") &&
  1301. img_sniff(body, len, &image_info) == 0)) {
  1302. unsigned int node = br_parse_image_doc(&doc, page_url);
  1303. if (node)
  1304. decode_direct_image(body, len, node);
  1305. s_copy(page_title, "Image", sizeof(page_title));
  1306. } else {
  1307. br_parse(&doc, (const char *)body, len);
  1308. s_copy(page_title, doc.title[0] ? doc.title : page_url,
  1309. sizeof(page_title));
  1310. if (load_stylesheets() == FETCH_ABORT)
  1311. return;
  1312. if (pending_action)
  1313. return;
  1314. }
  1315. post_status_text[0] = '\0';
  1316. post_status_error = 0;
  1317. post_status_ttl = 0;
  1318. if (truncated) {
  1319. s_copy(post_status_text, "Page truncated at the buffer limit",
  1320. sizeof(post_status_text));
  1321. post_status_ttl = 6000;
  1322. } else if (doc.truncated) {
  1323. s_copy(post_status_text, "Page truncated by the parser",
  1324. sizeof(post_status_text));
  1325. post_status_ttl = 6000;
  1326. } else if (starts_with(page_url, "https://")) {
  1327. s_copy(post_status_text, "HTTPS: certificate NOT verified",
  1328. sizeof(post_status_text));
  1329. post_status_ttl = 6000;
  1330. }
  1331. preload_begin();
  1332. page_loading = 0;
  1333. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  1334. url_in.len = s_len(url_in.text);
  1335. url_in.cursor = url_in.len;
  1336. url_in.scroll = 0;
  1337. ngui_set_focus(&ui, 0);
  1338. ngui_invalidate(&ui);
  1339. }
  1340. static void load_home(int push)
  1341. {
  1342. cancel_fetch = 0;
  1343. preload_active = 0;
  1344. preload_pending = 0;
  1345. heap_clear();
  1346. image_reset();
  1347. page_scroll.offset = 0;
  1348. page_scroll.dragging = 0;
  1349. br_parse_plain(&doc, "", 0);
  1350. page_loading = 1;
  1351. page_is_local = 0;
  1352. s_copy(page_url, "about:home", sizeof(page_url));
  1353. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  1354. url_in.len = s_len(url_in.text);
  1355. url_in.cursor = url_in.len;
  1356. url_in.scroll = 0;
  1357. ngui_set_focus(&ui, 0);
  1358. if (push)
  1359. history_push(page_url);
  1360. ngui_invalidate(&ui);
  1361. redraw_now();
  1362. if (heap_start() != 0) {
  1363. show_error("Out of memory", "Cannot allocate the browser heap");
  1364. return;
  1365. }
  1366. browser_rules_alloc();
  1367. {
  1368. static const char home[] =
  1369. "<html><head><title>Nebs Browser</title><style>"
  1370. "body { margin: 10px; color: #e4e6f0; }"
  1371. "h1 { color: #7fb0ff; }"
  1372. ".box { background-color: #262b3a; margin: 12px; "
  1373. "padding: 10px; }"
  1374. "a { color: #7fb0ff; }"
  1375. "pre { background-color: #32384a; }"
  1376. "</style></head><body>"
  1377. "<h1>Nebs Browser</h1>"
  1378. "<p>A small HTML/CSS browser built on <b>ngui</b>, the "
  1379. "shared HTTP client and the image library.</p>"
  1380. "<div class=\"box\"><b>Try:</b>"
  1381. "<ul><li><a href=\"/floppy1/DEMO.HTM\">/"
  1382. "floppy1/DEMO.HTM</a> on the root floppy</li>"
  1383. "<li><a href=\"http://10.0.2.2:8000/\">"
  1384. "http://10.0.2.2:8000/</a> on the QEMU host</li>"
  1385. "<li><a href=\"https://example.com/\">"
  1386. "https://example.com/</a> (TLS, not verified)</li>"
  1387. "</ul></div>"
  1388. "<p>Type a URL in the bar above. Esc closes the "
  1389. "window, Ctrl+L focuses the address bar.</p>"
  1390. "<pre>javascript: not supported\nforms: not supported"
  1391. "\nimages: PNG JPEG GIF BMP TGA PNM ICO</pre>"
  1392. "</body></html>";
  1393. br_parse(&doc, home, sizeof(home) - 1);
  1394. br_parse_inline_css(&doc);
  1395. br_reset_styles(&doc);
  1396. s_copy(page_title, doc.title, sizeof(page_title));
  1397. }
  1398. post_status_text[0] = '\0';
  1399. post_status_error = 0;
  1400. post_status_ttl = 0;
  1401. page_loading = 0;
  1402. set_status("", 0, 0);
  1403. s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
  1404. url_in.len = s_len(url_in.text);
  1405. url_in.cursor = url_in.len;
  1406. url_in.scroll = 0;
  1407. ngui_set_focus(&ui, 0);
  1408. ngui_invalidate(&ui);
  1409. }
  1410. static void navigate(const char *input, int push)
  1411. {
  1412. char target[BR_URL_MAX];
  1413. char *t;
  1414. if (!input)
  1415. return;
  1416. s_copy(target, input, sizeof(target));
  1417. t = trim(target);
  1418. if (!t[0])
  1419. return;
  1420. if (s_eq(t, "about:home")) {
  1421. load_home(push);
  1422. return;
  1423. }
  1424. if (starts_with(t, "file://")) {
  1425. make_abs_local(t + 7, target, sizeof(target));
  1426. load_page(target, 1, push);
  1427. return;
  1428. }
  1429. if (is_http(t)) {
  1430. load_page(t, 0, push);
  1431. return;
  1432. }
  1433. {
  1434. struct sys_stat st;
  1435. if (t[0] == '/' || (u_stat(t, &st) == 0 && !st.is_dir)) {
  1436. make_abs_local(t, target, sizeof(target));
  1437. load_page(target, 1, push);
  1438. return;
  1439. }
  1440. }
  1441. {
  1442. char with_scheme[BR_URL_MAX];
  1443. unsigned int n = 0;
  1444. s_append(with_scheme, &n, "http://", sizeof(with_scheme));
  1445. s_append(with_scheme, &n, t, sizeof(with_scheme));
  1446. load_page(with_scheme, 0, push);
  1447. }
  1448. }
  1449. /* ---- layout ---- */
  1450. static struct br_style base_style(void)
  1451. {
  1452. struct br_style s = { 0 };
  1453. s.color = ui.theme.text;
  1454. s.bg = ui.theme.bg;
  1455. s.has_bg = 0;
  1456. s.border_color = 0;
  1457. s.scale = 1;
  1458. s.bold = 0;
  1459. s.underline = 0;
  1460. s.align = BR_ALIGN_LEFT;
  1461. s.display = BR_DISPLAY_BLOCK;
  1462. s.no_bullet = 0;
  1463. s.uppercase = 0;
  1464. s.border_w = 0;
  1465. s.width_kind = s.max_width_kind = s.height_kind = 0;
  1466. s.width = s.max_width = s.height = 0;
  1467. s.line_h_pct = 0;
  1468. s.line_h_px = 0;
  1469. s.mt = s.mb = s.ml = s.mr = 0;
  1470. s.pt = s.pb = s.pl = s.pr = 0;
  1471. return s;
  1472. }
  1473. static int run_width(const struct lay_run *r)
  1474. {
  1475. if (r->img >= 0)
  1476. return r->iw;
  1477. if (r->img == -2)
  1478. return r->iw;
  1479. {
  1480. unsigned int n = r->has_lit ? s_len(r->lit) : r->len;
  1481. return (int)(n * (unsigned int)(8 * r->scale) +
  1482. (r->bold ? 1u : 0u));
  1483. }
  1484. }
  1485. static void flush_line(struct lay *l)
  1486. {
  1487. int total, x, i;
  1488. if (!l->nruns && l->line_x == l->left) {
  1489. l->line_h = 0;
  1490. return;
  1491. }
  1492. total = l->line_x - l->left;
  1493. x = l->left;
  1494. if (l->align == BR_ALIGN_CENTER)
  1495. x += (l->right - l->left - total) / 2;
  1496. else if (l->align == BR_ALIGN_RIGHT)
  1497. x = l->right - total;
  1498. if (x < l->left)
  1499. x = l->left;
  1500. for (i = 0; i < l->nruns; i++) {
  1501. struct lay_run *r = &l->runs[i];
  1502. int w = run_width(r);
  1503. x += r->gap;
  1504. if (l->draw) {
  1505. if (r->img == -2) {
  1506. int hh = r->ih ? r->ih : 20;
  1507. ngui_fill_rect(l->ui,
  1508. (struct ngui_rect){x, l->line_y, w, hh},
  1509. l->ui->theme.panel_alt);
  1510. ngui_frame(l->ui,
  1511. (struct ngui_rect){x, l->line_y, w, hh},
  1512. l->ui->theme.border);
  1513. ngui_text_styled(l->ui, x + 6, l->line_y + 2,
  1514. r->lit, s_len(r->lit), r->color,
  1515. r->scale, r->bold);
  1516. } else if (r->img >= 0) {
  1517. struct br_image *bi = &images[r->img];
  1518. if (bi->state == 1) {
  1519. struct img_blit b;
  1520. b.dst = l->ui->px;
  1521. b.dst_w = l->ui->w;
  1522. b.dst_h = l->ui->h;
  1523. b.clip_x = page_rect.x;
  1524. b.clip_y = page_rect.y;
  1525. b.clip_w = page_rect.w - 12;
  1526. b.clip_h = page_rect.h;
  1527. b.x = x;
  1528. b.y = l->line_y;
  1529. b.scale = ((unsigned int)r->iw << 16) /
  1530. bi->img.w;
  1531. b.bg = ui.theme.bg;
  1532. b.bg2 = ui.theme.bg;
  1533. b.flags = IMG_BLIT_OVER_DST;
  1534. img_draw(&bi->img, &b);
  1535. }
  1536. } else {
  1537. const char *s = r->has_lit ? r->lit :
  1538. doc.text + r->off;
  1539. unsigned int n = r->has_lit ? s_len(r->lit) :
  1540. r->len;
  1541. ngui_text_styled(l->ui, x, l->line_y, s, n,
  1542. r->color, r->scale, r->bold);
  1543. if (r->underline)
  1544. ngui_fill_rect(l->ui,
  1545. (struct ngui_rect){x,
  1546. l->line_y + 16 * r->scale - 2,
  1547. w, 1}, r->color);
  1548. }
  1549. if (r->href) {
  1550. if (nhits < HIT_MAX) {
  1551. struct hit *h = &hits[nhits];
  1552. int hh = r->img >= 0 ? r->ih :
  1553. 16 * r->scale;
  1554. int merge = nhits > 0 &&
  1555. hits[nhits - 1].node == r->href &&
  1556. hits[nhits - 1].y == l->line_y &&
  1557. hits[nhits - 1].h == hh &&
  1558. x <= hits[nhits - 1].x +
  1559. hits[nhits - 1].w + 2;
  1560. if (merge) {
  1561. struct hit *p = &hits[nhits - 1];
  1562. int end = x + w;
  1563. if (end > p->x + p->w)
  1564. p->w = end - p->x;
  1565. } else {
  1566. h->x = x;
  1567. h->y = l->line_y;
  1568. h->w = w;
  1569. h->h = hh;
  1570. h->node = r->href;
  1571. nhits++;
  1572. }
  1573. }
  1574. }
  1575. }
  1576. x += w;
  1577. }
  1578. l->y += l->line_h + 2;
  1579. l->nruns = 0;
  1580. l->line_x = l->left;
  1581. l->run_x = l->left;
  1582. l->line_h = 0;
  1583. l->space_pending = 0;
  1584. }
  1585. static void add_run(struct lay *l, unsigned short off, unsigned short len,
  1586. const struct br_style *st, unsigned short href,
  1587. const char *lit)
  1588. {
  1589. unsigned int n = lit ? s_len(lit) : len;
  1590. int w = (int)(n * (unsigned int)(8 * st->scale) +
  1591. (st->bold ? 1u : 0u));
  1592. if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0])))
  1593. flush_line(l);
  1594. if (l->nruns == 0) {
  1595. l->line_y = l->y;
  1596. l->line_h = 0;
  1597. }
  1598. {
  1599. struct lay_run *r = &l->runs[l->nruns++];
  1600. int gap = l->line_x - l->run_x;
  1601. if (gap < 0)
  1602. gap = 0;
  1603. r->gap = (unsigned short)gap;
  1604. r->off = off;
  1605. r->len = len;
  1606. r->color = st->color;
  1607. r->href = href;
  1608. r->has_lit = 0;
  1609. r->lit[0] = '\0';
  1610. if (lit && lit[0]) {
  1611. unsigned int i;
  1612. for (i = 0; i + 1 < sizeof(r->lit) && lit[i]; i++)
  1613. r->lit[i] = lit[i];
  1614. r->lit[i] = '\0';
  1615. r->has_lit = 1;
  1616. }
  1617. r->img = -1;
  1618. r->iw = r->ih = 0;
  1619. r->scale = st->scale;
  1620. r->bold = st->bold;
  1621. r->underline = st->underline;
  1622. }
  1623. l->line_x += w;
  1624. l->run_x = l->line_x;
  1625. {
  1626. int lh = 16 * st->scale;
  1627. if (st->line_h_px)
  1628. lh = st->line_h_px;
  1629. else if (st->line_h_pct)
  1630. lh = (16 * st->scale) * st->line_h_pct / 100;
  1631. if (lh < 16 * st->scale)
  1632. lh = 16 * st->scale;
  1633. if (l->line_h < lh)
  1634. l->line_h = lh;
  1635. }
  1636. }
  1637. static void add_image_run(struct lay *l, unsigned int node,
  1638. const struct br_style *st, unsigned short href)
  1639. {
  1640. struct br_image *bi = 0;
  1641. int idx = doc.nodes[node].img_index;
  1642. int dw, dh, maxw;
  1643. if (idx >= 0 && idx < image_count && images[idx].state == 1)
  1644. bi = &images[idx];
  1645. if (l->space_pending && (l->nruns > 0 || l->line_x > l->left)) {
  1646. if (l->line_x + 8 <= l->right)
  1647. l->line_x += 8;
  1648. }
  1649. l->space_pending = 0;
  1650. if (!bi) {
  1651. const char *alt = br_alt(&doc, node);
  1652. struct br_style gs = *st;
  1653. char tmp[96];
  1654. unsigned int n = 0;
  1655. int w;
  1656. gs.color = BR_GRAY;
  1657. gs.underline = 0;
  1658. s_append(tmp, &n, "[", sizeof(tmp));
  1659. s_append(tmp, &n, alt[0] ? alt : "image", sizeof(tmp));
  1660. s_append(tmp, &n, "]", sizeof(tmp));
  1661. w = (int)(n * (unsigned int)(8 * gs.scale));
  1662. if (l->line_x + w > l->right &&
  1663. (l->nruns > 0 || l->line_x > l->left))
  1664. flush_line(l);
  1665. add_run(l, 0, 0, &gs, href, tmp);
  1666. if (!l->draw) {
  1667. int t = l->line_y < 0 ? 0 : l->line_y;
  1668. int hh = 16 * gs.scale;
  1669. if (t > 65535)
  1670. t = 65535;
  1671. if (hh > 65535)
  1672. hh = 65535;
  1673. img_top[node] = (unsigned short)t;
  1674. img_h[node] = (unsigned short)hh;
  1675. }
  1676. return;
  1677. }
  1678. maxw = l->right - l->left;
  1679. if (maxw < 8)
  1680. maxw = 8;
  1681. {
  1682. int iw = bi->native_w, ih = bi->native_h;
  1683. int have_w = 0, have_h = 0, css_w = 0, css_h = 0;
  1684. if (iw <= 0 || ih <= 0) {
  1685. iw = (int)bi->img.w;
  1686. ih = (int)bi->img.h;
  1687. }
  1688. dw = iw;
  1689. dh = ih;
  1690. if (doc.nodes[node].width_attr) {
  1691. int want = doc.nodes[node].width_attr;
  1692. dw = doc.nodes[node].width_pct ? maxw * want / 100 : want;
  1693. have_w = 1;
  1694. }
  1695. if (doc.nodes[node].height_attr) {
  1696. int want = doc.nodes[node].height_attr;
  1697. dh = doc.nodes[node].height_pct ? maxw * want / 100 : want;
  1698. have_h = 1;
  1699. }
  1700. if (have_w && !have_h)
  1701. dh = ih ? ih * dw / iw : 0;
  1702. if (have_h && !have_w)
  1703. dw = iw ? iw * dh / ih : 0;
  1704. if (st->width_kind) {
  1705. css_w = 1;
  1706. dw = st->width_kind == 2 ? maxw * (int)st->width / 100 :
  1707. (int)st->width;
  1708. }
  1709. if (st->height_kind) {
  1710. css_h = 1;
  1711. dh = st->height_kind == 2 ? maxw * (int)st->height / 100 :
  1712. (int)st->height;
  1713. }
  1714. if (css_w && !css_h)
  1715. dh = ih ? ih * dw / iw : 0;
  1716. if (css_h && !css_w)
  1717. dw = iw ? iw * dh / ih : 0;
  1718. if (st->max_width_kind) {
  1719. int mw = st->max_width_kind == 2 ?
  1720. maxw * (int)st->max_width / 100 :
  1721. (int)st->max_width;
  1722. if (mw > 0 && dw > mw) {
  1723. dh = iw ? ih * mw / iw : 0;
  1724. dw = mw;
  1725. }
  1726. }
  1727. }
  1728. if (dw < 8)
  1729. dw = 8;
  1730. if (dh < 8)
  1731. dh = 8;
  1732. if (dw > maxw) {
  1733. dh = dw ? dh * maxw / dw : dh;
  1734. dw = maxw;
  1735. }
  1736. if (!l->draw) {
  1737. int t = l->line_y < 0 ? 0 : l->line_y;
  1738. if (t > 65535)
  1739. t = 65535;
  1740. if (dh > 65535)
  1741. dh = 65535;
  1742. img_top[node] = (unsigned short)t;
  1743. img_h[node] = (unsigned short)dh;
  1744. }
  1745. if (l->line_x + dw > l->right && (l->nruns > 0 ||
  1746. l->line_x > l->left))
  1747. flush_line(l);
  1748. if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0])))
  1749. flush_line(l);
  1750. if (l->nruns == 0) {
  1751. l->line_y = l->y;
  1752. l->line_h = 0;
  1753. }
  1754. {
  1755. struct lay_run *r = &l->runs[l->nruns++];
  1756. int gap = l->line_x - l->run_x;
  1757. if (gap < 0)
  1758. gap = 0;
  1759. r->gap = (unsigned short)gap;
  1760. r->off = 0;
  1761. r->len = 0;
  1762. r->color = 0;
  1763. r->href = href;
  1764. r->has_lit = 0;
  1765. r->lit[0] = '\0';
  1766. r->img = (short)idx;
  1767. r->iw = (unsigned short)dw;
  1768. r->ih = (unsigned short)dh;
  1769. r->scale = 1;
  1770. r->bold = 0;
  1771. r->underline = 0;
  1772. }
  1773. l->line_x += dw;
  1774. l->run_x = l->line_x;
  1775. if (l->line_h < dh)
  1776. l->line_h = dh;
  1777. }
  1778. static void emit_text(struct lay *l, const char *s, unsigned int len,
  1779. const struct br_style *st, unsigned short href, int pre)
  1780. {
  1781. unsigned int i = 0;
  1782. unsigned int base;
  1783. int cw = 8 * st->scale;
  1784. int adv = cw + (st->bold ? 1 : 0);
  1785. if (!s || !len)
  1786. return;
  1787. base = (unsigned int)(s - doc.text);
  1788. if (st->uppercase) {
  1789. char *p = (char *)s;
  1790. unsigned int j;
  1791. for (j = 0; j < len; j++)
  1792. if (p[j] >= 'a' && p[j] <= 'z')
  1793. p[j] = (char)(p[j] - 'a' + 'A');
  1794. }
  1795. if (pre) {
  1796. while (i < len) {
  1797. unsigned int start;
  1798. if (s[i] == '\n') {
  1799. flush_line(l);
  1800. i++;
  1801. continue;
  1802. }
  1803. if (s[i] == ' ') {
  1804. if (l->line_x + cw > l->right &&
  1805. (l->nruns > 0 || l->line_x > l->left))
  1806. flush_line(l);
  1807. else
  1808. l->line_x += cw;
  1809. i++;
  1810. continue;
  1811. }
  1812. start = i;
  1813. while (i < len && s[i] != ' ' && s[i] != '\n')
  1814. i++;
  1815. {
  1816. unsigned int n = i - start;
  1817. int w = (int)(n * (unsigned int)adv);
  1818. if (l->line_x + w > l->right &&
  1819. (l->nruns > 0 || l->line_x > l->left))
  1820. flush_line(l);
  1821. if (w > l->right - l->left) {
  1822. int maxc = (l->right - l->left) / adv;
  1823. unsigned int off = start;
  1824. if (maxc < 1)
  1825. maxc = 1;
  1826. while (n > 0) {
  1827. unsigned int take = n >
  1828. (unsigned int)maxc ?
  1829. (unsigned int)maxc : n;
  1830. add_run(l, (unsigned short)(base + off),
  1831. (unsigned short)take,
  1832. st, href, 0);
  1833. off += take;
  1834. n -= take;
  1835. if (n)
  1836. flush_line(l);
  1837. }
  1838. } else {
  1839. add_run(l, (unsigned short)(base + start),
  1840. (unsigned short)n, st, href, 0);
  1841. }
  1842. }
  1843. }
  1844. return;
  1845. }
  1846. while (i < len) {
  1847. unsigned int start, n;
  1848. int w;
  1849. if (s[i] == ' ') {
  1850. l->space_pending = 1;
  1851. i++;
  1852. continue;
  1853. }
  1854. start = i;
  1855. while (i < len && s[i] != ' ')
  1856. i++;
  1857. n = i - start;
  1858. w = (int)(n * (unsigned int)adv);
  1859. if (l->space_pending &&
  1860. (l->nruns > 0 || l->line_x > l->left)) {
  1861. if (l->line_x + cw <= l->right)
  1862. l->line_x += cw;
  1863. }
  1864. l->space_pending = 0;
  1865. if (l->line_x + w > l->right &&
  1866. (l->nruns > 0 || l->line_x > l->left))
  1867. flush_line(l);
  1868. if (w > l->right - l->left) {
  1869. int maxc = (l->right - l->left) / adv;
  1870. unsigned int off = start;
  1871. if (maxc < 1)
  1872. maxc = 1;
  1873. while (n > 0) {
  1874. unsigned int take = n > (unsigned int)maxc ?
  1875. (unsigned int)maxc : n;
  1876. add_run(l, (unsigned short)(base + off),
  1877. (unsigned short)take, st, href, 0);
  1878. off += take;
  1879. n -= take;
  1880. if (n)
  1881. flush_line(l);
  1882. }
  1883. } else if (n) {
  1884. add_run(l, (unsigned short)(base + start),
  1885. (unsigned short)n, st, href, 0);
  1886. }
  1887. }
  1888. }
  1889. static int list_index(unsigned int parent, unsigned int node)
  1890. {
  1891. unsigned int n = 0, i;
  1892. for (i = doc.nodes[parent].first; i && i != node;
  1893. i = doc.nodes[i].next)
  1894. if (doc.nodes[i].tag == BR_TAG_LI)
  1895. n++;
  1896. return (int)n + 1;
  1897. }
  1898. static const char *form_text(unsigned int node)
  1899. {
  1900. const char *v = br_value(&doc, node);
  1901. const char *p = br_placeholder(&doc, node);
  1902. const char *t = br_type(&doc, node);
  1903. if (s_eq(t, "hidden"))
  1904. return 0;
  1905. if (s_eq(t, "submit") || s_eq(t, "button") || s_eq(t, "reset"))
  1906. return v[0] ? v : "Button";
  1907. if (v[0])
  1908. return v;
  1909. if (p[0])
  1910. return p;
  1911. if (doc.nodes[node].tag == BR_TAG_TEXTAREA)
  1912. return "Text";
  1913. if (doc.nodes[node].tag == BR_TAG_SELECT)
  1914. return "Select";
  1915. return "Search";
  1916. }
  1917. static void add_form_run(struct lay *l, unsigned int node,
  1918. const struct br_style *st, unsigned short href)
  1919. {
  1920. const char *text = form_text(node);
  1921. struct lay_run *r;
  1922. int tw, w, h = 20, i;
  1923. if (!text)
  1924. return;
  1925. tw = (int)s_len(text) * 8 * st->scale;
  1926. if (st->width_kind == 1)
  1927. w = st->width;
  1928. else if (st->width_kind == 2)
  1929. w = (l->right - l->left) * (int)st->width / 100;
  1930. else
  1931. w = tw + 18;
  1932. if (w < 64)
  1933. w = 64;
  1934. if (w > l->right - l->left)
  1935. w = l->right - l->left;
  1936. if (w < 8)
  1937. w = 8;
  1938. if (l->line_x + w > l->right &&
  1939. (l->nruns > 0 || l->line_x > l->left))
  1940. flush_line(l);
  1941. if (l->nruns >= 96)
  1942. return;
  1943. r = &l->runs[l->nruns++];
  1944. r->gap = (unsigned short)(l->line_x - l->run_x);
  1945. r->off = r->len = 0;
  1946. r->color = st->color;
  1947. r->href = href;
  1948. r->has_lit = 1;
  1949. for (i = 0; i + 1 < (int)sizeof(r->lit) && text[i]; i++)
  1950. r->lit[i] = text[i];
  1951. r->lit[i] = '\0';
  1952. r->img = -2;
  1953. r->iw = (unsigned short)w;
  1954. r->ih = (unsigned short)h;
  1955. r->scale = st->scale;
  1956. r->bold = st->bold;
  1957. r->underline = 0;
  1958. l->line_x += w;
  1959. l->run_x = l->line_x;
  1960. if (l->line_h < h)
  1961. l->line_h = h;
  1962. }
  1963. static void layout_children(struct lay *l, unsigned int parent,
  1964. const struct br_style *pst, int depth,
  1965. unsigned short link);
  1966. static void layout_flex_children(struct lay *l, unsigned int parent,
  1967. const struct br_style *pst, int depth,
  1968. unsigned short link);
  1969. static void layout_grid_children(struct lay *l, unsigned int parent,
  1970. const struct br_style *pst, int depth,
  1971. unsigned short link);
  1972. /* A flex container whose items are only inline spans is usually just a
  1973. * heading/icon pair. Our flex row positions such items without emitting
  1974. * their inline text in some deep page contexts; normal inline flow renders
  1975. * the same content correctly. */
  1976. static int flex_wants_row(unsigned int parent, const struct br_style *pst)
  1977. {
  1978. unsigned int c;
  1979. for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
  1980. struct br_node *nd = &doc.nodes[c];
  1981. struct br_style cs;
  1982. if (nd->type == BR_NODE_TEXT)
  1983. continue;
  1984. br_style_for(&doc, c, pst, &cs);
  1985. if (cs.display == BR_DISPLAY_NONE)
  1986. continue;
  1987. if (cs.display != BR_DISPLAY_INLINE || cs.flex_grow ||
  1988. cs.width_kind || nd->tag == BR_TAG_IMG)
  1989. return 1;
  1990. }
  1991. return 0;
  1992. }
  1993. static void layout_block(struct lay *l, unsigned int node,
  1994. const struct br_style *st, int depth,
  1995. unsigned short link)
  1996. {
  1997. int old_left = l->left, old_right = l->right, old_align = l->align;
  1998. int top, l0, r0;
  1999. int flow_y, flow_left, flow_right, positioned;
  2000. if (depth > 24 || st->display == BR_DISPLAY_NONE)
  2001. return;
  2002. flow_y = l->y;
  2003. flow_left = l->left;
  2004. flow_right = l->right;
  2005. positioned = st->pos;
  2006. if (positioned) {
  2007. if (st->pos == 2) {
  2008. l->y = page_rect.y + (st->top_set ? st->top : 0);
  2009. l->left = page_rect.x;
  2010. l->right = page_rect.x + page_rect.w - 12;
  2011. } else {
  2012. l->y = flow_y + (st->top_set ? st->top : 0);
  2013. l->left = flow_left + (st->left_set ? st->left : 0);
  2014. l->right = flow_right + (st->left_set ? st->left : 0);
  2015. }
  2016. old_left = l->left;
  2017. old_right = l->right;
  2018. }
  2019. if (doc.nodes[node].tag == BR_TAG_INPUT ||
  2020. doc.nodes[node].tag == BR_TAG_TEXTAREA ||
  2021. doc.nodes[node].tag == BR_TAG_SELECT) {
  2022. if (!(doc.nodes[node].tag == BR_TAG_INPUT &&
  2023. s_eq(br_type(&doc, node), "hidden")))
  2024. add_form_run(l, node, st, link);
  2025. l->y += 20;
  2026. return;
  2027. }
  2028. flush_line(l);
  2029. l->y += st->mt;
  2030. top = l->y;
  2031. l0 = old_left + st->ml;
  2032. r0 = old_right - st->mr;
  2033. if (r0 < l0 + 16)
  2034. r0 = l0 + 16;
  2035. if (l->draw && st->has_bg && block_h[node])
  2036. ngui_fill_rect(l->ui, (struct ngui_rect){l0, top, r0 - l0,
  2037. block_h[node]},
  2038. st->bg);
  2039. if (l->draw && st->border_w && block_h[node])
  2040. ngui_frame(l->ui, (struct ngui_rect){l0, top, r0 - l0,
  2041. block_h[node]},
  2042. st->border_color);
  2043. l->left = l0 + st->pl;
  2044. l->right = r0 - st->pr;
  2045. if (l->right < l->left + 8)
  2046. l->right = l->left + 8;
  2047. l->align = st->align;
  2048. l->y += st->pt;
  2049. if (doc.nodes[node].tag == BR_TAG_HR) {
  2050. if (l->draw)
  2051. ngui_fill_rect(l->ui, (struct ngui_rect){l->left,
  2052. l->y + 2, l->right - l->left, 1}, BR_GRAY);
  2053. l->y += 8;
  2054. } else if (doc.nodes[node].tag == BR_TAG_VIDEO) {
  2055. int vw = l->right - l->left;
  2056. int vh;
  2057. if (doc.nodes[node].width_pct)
  2058. vw = vw * doc.nodes[node].width_attr / 100;
  2059. else if (doc.nodes[node].width_attr)
  2060. vw = doc.nodes[node].width_attr;
  2061. if (vw > l->right - l->left)
  2062. vw = l->right - l->left;
  2063. if (vw < 8)
  2064. vw = 8;
  2065. vh = vw * 9 / 16;
  2066. if (vh > 240)
  2067. vh = 240;
  2068. if (vh < 80)
  2069. vh = 80;
  2070. if (l->draw) {
  2071. ngui_fill_rect(l->ui, (struct ngui_rect){l->left, l->y,
  2072. vw, vh}, 0xD69A);
  2073. ngui_frame(l->ui, (struct ngui_rect){l->left, l->y,
  2074. vw, vh}, 0x8410);
  2075. ngui_text(l->ui, l->left + 8, l->y + 8, "[video]",
  2076. 0x4208);
  2077. }
  2078. l->y += vh + 4;
  2079. } else {
  2080. if (doc.nodes[node].tag == BR_TAG_LI && !st->no_bullet) {
  2081. char *m = li_marker[node];
  2082. if (!m[0]) {
  2083. unsigned int parent = doc.nodes[node].parent;
  2084. if (parent &&
  2085. doc.nodes[parent].tag == BR_TAG_OL) {
  2086. int idx = list_index(parent, node);
  2087. unsigned int n = 0;
  2088. s_append_dec(m, &n, (unsigned int)idx, 6);
  2089. s_append(m, &n, ".", 6);
  2090. } else {
  2091. s_copy(m, "-", 6);
  2092. }
  2093. }
  2094. if (l->draw) {
  2095. int mw = (int)s_len(m) * 8;
  2096. ngui_text(l->ui, l->left - mw - 6, l->y, m,
  2097. st->color);
  2098. }
  2099. }
  2100. if (st->display == BR_DISPLAY_GRID && st->grid_ncols >= 2 &&
  2101. st->grid_gap > 0)
  2102. layout_grid_children(l, node, st, depth + 1, link);
  2103. else if (st->display == BR_DISPLAY_FLEX && !st->flex_col &&
  2104. flex_wants_row(node, st))
  2105. layout_flex_children(l, node, st, depth + 1, link);
  2106. else
  2107. layout_children(l, node, st, depth + 1, link);
  2108. }
  2109. flush_line(l);
  2110. l->y += st->pb;
  2111. /* Honour an explicit CSS height. Fixed-height header/icon boxes often
  2112. * contain only absolutely positioned children, so their flow height
  2113. * would otherwise collapse to zero. */
  2114. if (st->height_kind == 1 && st->height > 0 &&
  2115. l->y < top + st->height)
  2116. l->y = top + st->height;
  2117. l->left = old_left;
  2118. l->right = old_right;
  2119. l->align = old_align;
  2120. l->y += st->mb;
  2121. if (!l->draw) {
  2122. int h = l->y - top;
  2123. if (h < 0)
  2124. h = 0;
  2125. if (h > 65535)
  2126. h = 65535;
  2127. block_h[node] = (unsigned short)h;
  2128. }
  2129. if (positioned) {
  2130. l->y = flow_y;
  2131. l->left = flow_left;
  2132. l->right = flow_right;
  2133. }
  2134. }
  2135. static int text_node_blank(const struct br_node *nd)
  2136. {
  2137. const char *t = doc.text + nd->text_off;
  2138. unsigned int i;
  2139. for (i = 0; i < nd->text_len; i++)
  2140. if (t[i] != ' ' && t[i] != '\t' && t[i] != '\n' &&
  2141. t[i] != '\r')
  2142. return 0;
  2143. return 1;
  2144. }
  2145. /* Rough shrink-to-fit width for a float without an explicit width: sum the
  2146. * text/image widths in the subtree, plus padding and margins. This keeps
  2147. * Bootstrap's floated nav items on one line instead of giving each the whole
  2148. * row. */
  2149. static int intrinsic_width(unsigned int node, const struct br_style *pst,
  2150. int depth)
  2151. {
  2152. unsigned int c;
  2153. int w = 0;
  2154. if (depth > 12)
  2155. return 0;
  2156. for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
  2157. struct br_node *nd = &doc.nodes[c];
  2158. struct br_style cs;
  2159. if (nd->type == BR_NODE_TEXT) {
  2160. w += (int)nd->text_len * (8 * pst->scale) +
  2161. (pst->bold ? 1 : 0);
  2162. continue;
  2163. }
  2164. br_style_for(&doc, c, pst, &cs);
  2165. if (cs.display == BR_DISPLAY_NONE)
  2166. continue;
  2167. if (cs.float_left)
  2168. w += 8;
  2169. if (nd->tag == BR_TAG_BR) {
  2170. w += 8;
  2171. continue;
  2172. }
  2173. if (nd->tag == BR_TAG_IMG) {
  2174. int idx = nd->img_index;
  2175. if (idx >= 0 && idx < image_count &&
  2176. images[idx].state == 1)
  2177. w += (int)images[idx].img.w;
  2178. else if (nd->width_attr)
  2179. w += nd->width_attr;
  2180. else
  2181. w += 64;
  2182. continue;
  2183. }
  2184. w += cs.pl + cs.pr + cs.ml + cs.mr;
  2185. w += intrinsic_width(c, &cs, depth + 1);
  2186. if (w > 4096)
  2187. break;
  2188. }
  2189. return w;
  2190. }
  2191. /* Lay consecutive floated block children side by side. Bootstrap's grid is
  2192. * the main user: .row > .col-md-* with float:left and a percentage width.
  2193. * Each column is laid out with its own left/right edge and the row advances
  2194. * to the tallest column. Returns the first child not consumed. */
  2195. static unsigned int layout_float_group(struct lay *l, unsigned int first,
  2196. const struct br_style *pst, int depth,
  2197. unsigned short link)
  2198. {
  2199. unsigned int n = first;
  2200. int row_left = l->left, row_right = l->right;
  2201. int row_top, row_bottom, x, any = 0;
  2202. flush_line(l);
  2203. row_top = l->y;
  2204. row_bottom = l->y;
  2205. x = row_left;
  2206. while (n) {
  2207. struct br_node *nd = &doc.nodes[n];
  2208. struct br_style cs;
  2209. int colw;
  2210. if (nd->type == BR_NODE_TEXT) {
  2211. if (text_node_blank(nd)) {
  2212. n = nd->next;
  2213. continue;
  2214. }
  2215. break;
  2216. }
  2217. br_style_for(&doc, n, pst, &cs);
  2218. if (cs.display == BR_DISPLAY_NONE) {
  2219. n = nd->next;
  2220. continue;
  2221. }
  2222. if (!cs.float_left || cs.display != BR_DISPLAY_BLOCK)
  2223. break;
  2224. if (cs.width_kind == 2)
  2225. colw = (row_right - row_left) * (int)cs.width / 100;
  2226. else if (cs.width_kind == 1)
  2227. colw = (int)cs.width;
  2228. else {
  2229. colw = intrinsic_width(n, &cs, 0) +
  2230. cs.pl + cs.pr + cs.ml + cs.mr + 8;
  2231. if (colw < 8)
  2232. colw = 8;
  2233. }
  2234. if (colw < 8)
  2235. colw = 8;
  2236. if (colw > row_right - row_left)
  2237. colw = row_right - row_left;
  2238. if (any && x + colw > row_right) {
  2239. row_top = row_bottom;
  2240. row_bottom = row_top;
  2241. x = row_left;
  2242. }
  2243. l->left = x;
  2244. l->right = x + colw;
  2245. if (l->right < l->left + 8)
  2246. l->right = l->left + 8;
  2247. l->y = row_top;
  2248. l->line_x = l->left;
  2249. l->run_x = l->left;
  2250. l->line_y = l->y;
  2251. l->line_h = 0;
  2252. l->nruns = 0;
  2253. l->space_pending = 0;
  2254. l->align = cs.align;
  2255. layout_block(l, n, &cs, depth + 1, link);
  2256. if (l->y > row_bottom)
  2257. row_bottom = l->y;
  2258. x += colw;
  2259. any++;
  2260. n = nd->next;
  2261. }
  2262. l->left = row_left;
  2263. l->right = row_right;
  2264. l->y = row_bottom;
  2265. l->line_x = l->left;
  2266. l->run_x = l->left;
  2267. l->line_y = l->y;
  2268. l->line_h = 0;
  2269. l->nruns = 0;
  2270. l->space_pending = 0;
  2271. return n;
  2272. }
  2273. /* A deliberately simple flex row: items are placed left to right, grow
  2274. * items share the leftover width, and a row wraps when the next item does not
  2275. * fit. It is enough for site headers, cards and Wikipedia's main-page boxes;
  2276. * flex-direction: column uses the normal block path. */
  2277. static int flex_item_width(struct lay *l, unsigned int n,
  2278. const struct br_style *cs, int avail)
  2279. {
  2280. struct br_node *nd = &doc.nodes[n];
  2281. (void)l;
  2282. int w;
  2283. if (cs->width_kind == 2)
  2284. w = avail * (int)cs->width / 100;
  2285. else if (cs->width_kind == 1)
  2286. w = (int)cs->width;
  2287. else if (nd->tag == BR_TAG_IMG) {
  2288. int idx = nd->img_index;
  2289. if (idx >= 0 && idx < image_count &&
  2290. images[idx].state == 1)
  2291. w = (int)images[idx].img.w;
  2292. else if (nd->width_attr)
  2293. w = (int)nd->width_attr;
  2294. else
  2295. w = 64;
  2296. } else {
  2297. w = intrinsic_width(n, cs, 0);
  2298. w += cs->pl + cs->pr + cs->ml + cs->mr;
  2299. }
  2300. if (w < 24)
  2301. w = 24;
  2302. if (w > avail)
  2303. w = avail;
  2304. return w;
  2305. }
  2306. static void layout_flex_children(struct lay *l, unsigned int parent,
  2307. const struct br_style *pst, int depth,
  2308. unsigned short link)
  2309. {
  2310. unsigned int n, c;
  2311. int row_left = l->left, row_right = l->right;
  2312. int avail = row_right - row_left;
  2313. int grow_count = 0, fixed = 0, grow_w;
  2314. int x = row_left, row_top = l->y, row_bottom = l->y, any = 0;
  2315. if (avail < 160 || depth > 24) {
  2316. layout_children(l, parent, pst, depth, link);
  2317. return;
  2318. }
  2319. /* If direct text is not just whitespace, normal block flow handles it
  2320. * better than pretending it is a flex item. */
  2321. for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
  2322. struct br_node *nd = &doc.nodes[c];
  2323. if (nd->type == BR_NODE_TEXT && !text_node_blank(nd)) {
  2324. layout_children(l, parent, pst, depth, link);
  2325. return;
  2326. }
  2327. }
  2328. for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
  2329. struct br_node *nd = &doc.nodes[c];
  2330. struct br_style cs;
  2331. if (nd->type == BR_NODE_TEXT)
  2332. continue;
  2333. br_style_for(&doc, c, pst, &cs);
  2334. if (cs.display == BR_DISPLAY_NONE)
  2335. continue;
  2336. if (cs.flex_grow && !cs.width_kind)
  2337. grow_count++;
  2338. else
  2339. fixed += flex_item_width(l, c, &cs, avail);
  2340. }
  2341. grow_w = grow_count ? (avail - fixed) / grow_count : 0;
  2342. if (grow_w < 96)
  2343. grow_w = 96;
  2344. n = doc.nodes[parent].first;
  2345. while (n) {
  2346. struct br_node *nd = &doc.nodes[n];
  2347. struct br_style cs;
  2348. unsigned short lk = link;
  2349. int colw;
  2350. if (nd->type == BR_NODE_TEXT) {
  2351. n = nd->next;
  2352. continue;
  2353. }
  2354. br_style_for(&doc, n, pst, &cs);
  2355. if (cs.display == BR_DISPLAY_NONE) {
  2356. n = nd->next;
  2357. continue;
  2358. }
  2359. if (nd->tag == BR_TAG_A && nd->href_off)
  2360. lk = (unsigned short)n;
  2361. if (cs.flex_grow && !cs.width_kind)
  2362. colw = grow_w;
  2363. else
  2364. colw = flex_item_width(l, n, &cs, avail);
  2365. if (colw > row_right - row_left)
  2366. colw = row_right - row_left;
  2367. if (pst->flex_wrap != 2 && any && x + colw > row_right) {
  2368. row_top = row_bottom;
  2369. row_bottom = row_top;
  2370. x = row_left;
  2371. }
  2372. l->left = x;
  2373. l->right = x + colw;
  2374. if (l->right < l->left + 8)
  2375. l->right = l->left + 8;
  2376. l->y = row_top;
  2377. l->line_x = l->left;
  2378. l->run_x = l->left;
  2379. l->line_y = l->y;
  2380. l->line_h = 0;
  2381. l->nruns = 0;
  2382. l->space_pending = 0;
  2383. l->align = cs.align;
  2384. layout_block(l, n, &cs, depth + 1, lk);
  2385. if (l->y > row_bottom)
  2386. row_bottom = l->y;
  2387. x += colw;
  2388. any++;
  2389. n = nd->next;
  2390. }
  2391. l->left = row_left;
  2392. l->right = row_right;
  2393. l->y = row_bottom;
  2394. l->line_x = l->left;
  2395. l->run_x = l->left;
  2396. l->line_y = l->y;
  2397. l->line_h = 0;
  2398. l->nruns = 0;
  2399. l->space_pending = 0;
  2400. }
  2401. /* Two-column CSS grid: track sizes from grid-template-columns (px, %, fr)
  2402. * plus the grid-area hints used by site shells (site notice/footer full
  2403. * width, column-start left, page-content right). Anything more elaborate
  2404. * falls back to normal block flow. */
  2405. static int grid_track_width(unsigned char kind, unsigned short val,
  2406. int avail, int other)
  2407. {
  2408. if (kind == 2)
  2409. return avail * (int)val / 100;
  2410. if (kind == 1)
  2411. return val;
  2412. return other;
  2413. }
  2414. static void layout_grid_children(struct lay *l, unsigned int parent,
  2415. const struct br_style *pst, int depth,
  2416. unsigned short link)
  2417. {
  2418. unsigned int n;
  2419. int row_left = l->left, row_right = l->right;
  2420. int avail = row_right - row_left;
  2421. int gap = pst->grid_gap;
  2422. int track_total = avail - gap;
  2423. int col0, col1, x0, x1;
  2424. int row_y, row_bottom, have_left = 0, have_right = 0;
  2425. unsigned int c;
  2426. if (avail < 32 || depth > 24)
  2427. return;
  2428. if (track_total < 16)
  2429. track_total = 16;
  2430. col0 = grid_track_width((unsigned char)(pst->width_kind),
  2431. pst->width, track_total, 0);
  2432. col1 = grid_track_width((unsigned char)(pst->max_width_kind),
  2433. pst->max_width, track_total, 0);
  2434. if (pst->width_kind == 3 && pst->max_width_kind == 3) {
  2435. int w0 = pst->width ? pst->width : 1;
  2436. int w1 = pst->max_width ? pst->max_width : 1;
  2437. col0 = track_total * w0 / (w0 + w1);
  2438. col1 = track_total - col0;
  2439. } else if (pst->width_kind == 3) {
  2440. col0 = track_total - col1;
  2441. if (col0 < 24)
  2442. col0 = 24;
  2443. } else if (pst->max_width_kind == 3) {
  2444. col1 = track_total - col0;
  2445. if (col1 < 24)
  2446. col1 = 24;
  2447. }
  2448. if (col0 < 24)
  2449. col0 = 24;
  2450. if (col1 < 24)
  2451. col1 = 24;
  2452. if (col0 + col1 > track_total) {
  2453. col1 = track_total - col0;
  2454. if (col1 < 24)
  2455. col1 = 24;
  2456. }
  2457. x0 = row_left;
  2458. x1 = row_left + col0 + gap;
  2459. /* Direct text makes a grid ambiguous; use block flow instead. */
  2460. for (c = doc.nodes[parent].first; c; c = doc.nodes[c].next) {
  2461. struct br_node *nd = &doc.nodes[c];
  2462. if (nd->type == BR_NODE_TEXT && !text_node_blank(nd)) {
  2463. layout_children(l, parent, pst, depth, link);
  2464. return;
  2465. }
  2466. }
  2467. row_y = row_bottom = l->y;
  2468. n = doc.nodes[parent].first;
  2469. while (n) {
  2470. struct br_node *nd = &doc.nodes[n];
  2471. struct br_style cs;
  2472. unsigned short lk = link;
  2473. int slot, x, w;
  2474. if (nd->type == BR_NODE_TEXT) {
  2475. n = nd->next;
  2476. continue;
  2477. }
  2478. br_style_for(&doc, n, pst, &cs);
  2479. if (cs.display == BR_DISPLAY_NONE) {
  2480. n = nd->next;
  2481. continue;
  2482. }
  2483. if (nd->tag == BR_TAG_A && nd->href_off)
  2484. lk = (unsigned short)n;
  2485. slot = cs.grid_slot;
  2486. if (!slot) {
  2487. if (!have_left)
  2488. slot = 2;
  2489. else if (!have_right)
  2490. slot = 3;
  2491. else
  2492. slot = 2;
  2493. }
  2494. if (slot == 1) {
  2495. if (have_left || have_right) {
  2496. row_y = row_bottom;
  2497. have_left = have_right = 0;
  2498. }
  2499. x = row_left;
  2500. w = avail;
  2501. } else if (slot == 2) {
  2502. if (have_left) {
  2503. row_y = row_bottom;
  2504. have_left = have_right = 0;
  2505. }
  2506. x = x0;
  2507. w = col0;
  2508. have_left = 1;
  2509. } else {
  2510. if (have_right) {
  2511. row_y = row_bottom;
  2512. have_left = have_right = 0;
  2513. }
  2514. x = x1;
  2515. w = col1;
  2516. have_right = 1;
  2517. }
  2518. l->left = x;
  2519. l->right = x + w;
  2520. if (l->right < l->left + 8)
  2521. l->right = l->left + 8;
  2522. l->y = row_y;
  2523. l->line_x = l->left;
  2524. l->run_x = l->left;
  2525. l->line_y = l->y;
  2526. l->line_h = 0;
  2527. l->nruns = 0;
  2528. l->space_pending = 0;
  2529. l->align = cs.align;
  2530. layout_block(l, n, &cs, depth + 1, lk);
  2531. if (l->y > row_bottom)
  2532. row_bottom = l->y;
  2533. n = nd->next;
  2534. }
  2535. l->left = row_left;
  2536. l->right = row_right;
  2537. l->y = row_bottom;
  2538. l->line_x = l->left;
  2539. l->run_x = l->left;
  2540. l->line_y = l->y;
  2541. l->line_h = 0;
  2542. l->nruns = 0;
  2543. l->space_pending = 0;
  2544. }
  2545. /* Positioned elements (fixed headers, overlays) are drawn after the normal
  2546. * flow so they sit above it, like CSS painting order. Their flow origin is
  2547. * saved for absolute boxes; fixed boxes only need the viewport rect. */
  2548. struct br_deferred {
  2549. unsigned int node;
  2550. int flow_y, flow_left, flow_right;
  2551. unsigned short link;
  2552. };
  2553. static struct br_deferred deferred[16];
  2554. static int ndeferred;
  2555. static void layout_children(struct lay *l, unsigned int parent,
  2556. const struct br_style *pst, int depth,
  2557. unsigned short link)
  2558. {
  2559. unsigned int n;
  2560. if (depth > 24)
  2561. return;
  2562. n = doc.nodes[parent].first;
  2563. while (n) {
  2564. struct br_node *nd = &doc.nodes[n];
  2565. struct br_style cs;
  2566. unsigned short lk = link;
  2567. if (nd->type == BR_NODE_TEXT) {
  2568. emit_text(l, doc.text + nd->text_off, nd->text_len, pst,
  2569. link, (nd->flags & BR_NODE_PREFORMAT) != 0);
  2570. n = nd->next;
  2571. continue;
  2572. }
  2573. br_style_for(&doc, n, pst, &cs);
  2574. if (cs.display == BR_DISPLAY_NONE) {
  2575. n = nd->next;
  2576. continue;
  2577. }
  2578. if (nd->tag == BR_TAG_A && nd->href_off)
  2579. lk = (unsigned short)n;
  2580. if (cs.pos) {
  2581. if (l->draw && ndeferred < 16) {
  2582. deferred[ndeferred].node = n;
  2583. deferred[ndeferred].flow_y = l->y;
  2584. deferred[ndeferred].flow_left = l->left;
  2585. deferred[ndeferred].flow_right = l->right;
  2586. deferred[ndeferred].link = lk;
  2587. ndeferred++;
  2588. } else {
  2589. layout_block(l, n, &cs, depth + 1, lk);
  2590. }
  2591. n = nd->next;
  2592. continue;
  2593. }
  2594. if (nd->tag == BR_TAG_BR) {
  2595. flush_line(l);
  2596. n = nd->next;
  2597. continue;
  2598. }
  2599. if (nd->tag == BR_TAG_HR) {
  2600. flush_line(l);
  2601. if (l->draw)
  2602. ngui_fill_rect(l->ui,
  2603. (struct ngui_rect){l->left, l->y + 2,
  2604. l->right - l->left, 1}, BR_GRAY);
  2605. l->y += 8;
  2606. n = nd->next;
  2607. continue;
  2608. }
  2609. if (nd->tag == BR_TAG_INPUT || nd->tag == BR_TAG_TEXTAREA ||
  2610. nd->tag == BR_TAG_SELECT) {
  2611. if (!(nd->tag == BR_TAG_INPUT &&
  2612. s_eq(br_type(&doc, n), "hidden")))
  2613. add_form_run(l, n, &cs, lk);
  2614. n = nd->next;
  2615. continue;
  2616. }
  2617. if (nd->tag == BR_TAG_IMG) {
  2618. add_image_run(l, n, &cs, lk);
  2619. n = nd->next;
  2620. continue;
  2621. }
  2622. if (nd->tag == BR_TAG_VIDEO) {
  2623. layout_block(l, n, &cs, depth + 1, lk);
  2624. n = nd->next;
  2625. continue;
  2626. }
  2627. if (cs.float_left && cs.display == BR_DISPLAY_BLOCK) {
  2628. n = layout_float_group(l, n, pst, depth, link);
  2629. continue;
  2630. }
  2631. if (cs.display == BR_DISPLAY_BLOCK || br_tag_is_block(nd->tag))
  2632. layout_block(l, n, &cs, depth + 1, lk);
  2633. else {
  2634. layout_children(l, n, &cs, depth + 1, lk);
  2635. if (nd->tag == BR_TAG_TD || nd->tag == BR_TAG_TH)
  2636. l->space_pending = 1;
  2637. }
  2638. n = nd->next;
  2639. }
  2640. }
  2641. /* One layout state, kept out of the 8 KB user stack: it holds 96 line
  2642. * runs and a recursive page can otherwise overflow the stack. */
  2643. static struct lay layout_state;
  2644. static int measure_page(int left, int right)
  2645. {
  2646. struct lay *l = &layout_state;
  2647. struct br_style base = base_style();
  2648. unsigned int i;
  2649. for (i = 0; i < BR_MAX_NODES; i++) {
  2650. block_h[i] = 0;
  2651. img_top[i] = 0;
  2652. img_h[i] = 0;
  2653. }
  2654. l->draw = 0;
  2655. l->ui = 0;
  2656. l->left = left;
  2657. l->right = right;
  2658. l->y = 0;
  2659. l->line_x = left;
  2660. l->run_x = left;
  2661. l->line_y = 0;
  2662. l->line_h = 0;
  2663. l->align = BR_ALIGN_LEFT;
  2664. l->depth = 0;
  2665. l->nruns = 0;
  2666. l->space_pending = 0;
  2667. layout_children(l, doc.root, &base, 0, 0);
  2668. return l->y + 8;
  2669. }
  2670. static void render_page(int left, int right, int y0)
  2671. {
  2672. struct lay *l = &layout_state;
  2673. struct br_style base = base_style();
  2674. l->draw = 1;
  2675. l->ui = &ui;
  2676. l->left = left;
  2677. l->right = right;
  2678. l->y = y0;
  2679. l->line_x = left;
  2680. l->run_x = left;
  2681. l->line_y = y0;
  2682. l->line_h = 0;
  2683. l->align = BR_ALIGN_LEFT;
  2684. l->depth = 0;
  2685. l->nruns = 0;
  2686. l->space_pending = 0;
  2687. nhits = 0;
  2688. ndeferred = 0;
  2689. layout_children(l, doc.root, &base, 0, 0);
  2690. {
  2691. int i;
  2692. for (i = 0; i < ndeferred; i++) {
  2693. struct br_style st;
  2694. br_style_for(&doc, deferred[i].node, &base, &st);
  2695. if (st.display == BR_DISPLAY_NONE)
  2696. continue;
  2697. l->draw = 1;
  2698. l->left = deferred[i].flow_left;
  2699. l->right = deferred[i].flow_right;
  2700. l->y = deferred[i].flow_y;
  2701. l->line_x = l->left;
  2702. l->run_x = l->left;
  2703. l->line_y = l->y;
  2704. l->line_h = 0;
  2705. l->nruns = 0;
  2706. l->space_pending = 0;
  2707. l->align = st.align;
  2708. if (st.pos == 2 && block_h[deferred[i].node]) {
  2709. struct ngui_rect clip;
  2710. int ch = block_h[deferred[i].node] + 24;
  2711. clip.x = page_rect.x;
  2712. clip.y = page_rect.y;
  2713. clip.w = page_rect.w - 12;
  2714. if (ch > page_rect.h)
  2715. ch = page_rect.h;
  2716. clip.h = ch;
  2717. ngui_push_clip(&ui, clip);
  2718. if (st.has_bg)
  2719. ngui_fill_rect(&ui, clip, st.bg);
  2720. layout_block(l, deferred[i].node, &st, 0,
  2721. deferred[i].link);
  2722. ngui_pop_clip(&ui);
  2723. } else {
  2724. layout_block(l, deferred[i].node, &st, 0,
  2725. deferred[i].link);
  2726. }
  2727. }
  2728. }
  2729. }
  2730. /* ---- UI ---- */
  2731. static int find_hit(int x, int y)
  2732. {
  2733. int i;
  2734. for (i = nhits - 1; i >= 0; i--) {
  2735. struct hit *h = &hits[i];
  2736. if (x >= h->x && y >= h->y && x < h->x + h->w &&
  2737. y < h->y + h->h)
  2738. return (int)h->node;
  2739. }
  2740. return 0;
  2741. }
  2742. static void draw_toolbar(struct ngui_rect r, unsigned int *action)
  2743. {
  2744. int x = r.x + 4, y = r.y + 4, h = 22;
  2745. int compact = r.w < 560;
  2746. int go_w = r.w < 420 ? 32 : 42;
  2747. int in_x, in_w, res;
  2748. ngui_fill_rect(&ui, r, ui.theme.panel);
  2749. ngui_fill_rect(&ui, (struct ngui_rect){r.x, r.y + r.h - 1, r.w, 1},
  2750. ui.theme.border);
  2751. if (ngui_icon_button(&ui, ID_BACK, (struct ngui_rect){x, y, 28, h},
  2752. NGUI_ICON_CHEVRON_LEFT))
  2753. *action |= ACT_BACK;
  2754. x += 32;
  2755. if (!compact) {
  2756. if (ngui_icon_button(&ui, ID_FORWARD,
  2757. (struct ngui_rect){x, y, 28, h},
  2758. NGUI_ICON_CHEVRON_RIGHT))
  2759. *action |= ACT_FORWARD;
  2760. x += 32;
  2761. }
  2762. if (ngui_icon_button(&ui, ID_RELOAD, (struct ngui_rect){x, y, 28, h},
  2763. NGUI_ICON_REFRESH))
  2764. *action |= ACT_RELOAD;
  2765. x += 32;
  2766. if (!compact) {
  2767. if (ngui_icon_button(&ui, ID_HOME,
  2768. (struct ngui_rect){x, y, 28, h},
  2769. NGUI_ICON_HOME))
  2770. *action |= ACT_HOME;
  2771. x += 32;
  2772. }
  2773. in_x = x + 2;
  2774. in_w = r.x + r.w - 6 - go_w - 4 - in_x;
  2775. if (in_w < 60)
  2776. in_w = 60;
  2777. res = ngui_text_input(&ui, ID_URL,
  2778. (struct ngui_rect){in_x, y, in_w, h}, &url_in);
  2779. if (res & NGUI_INPUT_SUBMIT)
  2780. *action |= ACT_NAV;
  2781. if (res & NGUI_INPUT_CANCEL)
  2782. ngui_set_focus(&ui, 0);
  2783. if (ngui_button(&ui, ID_GO,
  2784. (struct ngui_rect){r.x + r.w - 4 - go_w, y, go_w, h},
  2785. "Go"))
  2786. *action |= ACT_NAV;
  2787. }
  2788. static int page_hover_node(void)
  2789. {
  2790. if (!ui.inside)
  2791. return 0;
  2792. if (ui.mouse_x < page_rect.x ||
  2793. ui.mouse_x >= page_rect.x + page_rect.w - 12 ||
  2794. ui.mouse_y < page_rect.y ||
  2795. ui.mouse_y >= page_rect.y + page_rect.h)
  2796. return 0;
  2797. return find_hit(ui.mouse_x, ui.mouse_y);
  2798. }
  2799. static void draw_page(struct ngui_rect page)
  2800. {
  2801. int left, right, ch;
  2802. int old_panel, old_dim;
  2803. page_rect = page;
  2804. ngui_fill_rect(&ui, page, ui.theme.bg);
  2805. if (!doc.nnode || (page_loading && doc.nnode <= 1)) {
  2806. const char *s = page_loading ? "Loading..." : "No page";
  2807. int w = (int)ngui_text_width(s);
  2808. ngui_text(&ui, page.x + (page.w - w) / 2,
  2809. page.y + page.h / 2 - 8, s, ui.theme.text_dim);
  2810. page_scroll.content_h = 0;
  2811. page_scroll.view_h = page.h;
  2812. return;
  2813. }
  2814. if (page.w < 40 || page.h < 40)
  2815. return;
  2816. left = page.x + 8;
  2817. right = page.x + page.w - 16;
  2818. ch = measure_page(left, right);
  2819. page_scroll.content_h = ch;
  2820. page_scroll.view_h = page.h;
  2821. ngui_scroll_begin(&ui, page, &page_scroll);
  2822. render_page(left, right, page.y + 4 - page_scroll.offset);
  2823. old_panel = ui.theme.panel_alt;
  2824. old_dim = ui.theme.text_dim;
  2825. ui.theme.panel_alt = 0xC618;
  2826. ui.theme.text_dim = 0x8410;
  2827. ngui_scroll_end(&ui, page, &page_scroll);
  2828. ui.theme.panel_alt = old_panel;
  2829. ui.theme.text_dim = old_dim;
  2830. }
  2831. static void draw_status(struct ngui_rect r)
  2832. {
  2833. char msg[BR_URL_MAX + 32];
  2834. unsigned int n = 0;
  2835. int hover = page_hover_node();
  2836. unsigned short color;
  2837. const char *kind;
  2838. ngui_fill_rect(&ui, r, ui.theme.panel);
  2839. ngui_fill_rect(&ui, (struct ngui_rect){r.x, r.y, r.w, 1},
  2840. ui.theme.border);
  2841. if (hover) {
  2842. const char *href = br_href(&doc, (unsigned int)hover);
  2843. char url[BR_URL_MAX];
  2844. if (resolve_link(page_url, href, url, sizeof(url)) == 0) {
  2845. s_append(msg, &n, "Link: ", sizeof(msg));
  2846. s_append(msg, &n, url, sizeof(msg));
  2847. } else {
  2848. s_copy(msg, "Link", sizeof(msg));
  2849. }
  2850. color = ui.theme.accent;
  2851. } else if (status_text[0]) {
  2852. s_copy(msg, status_text, sizeof(msg));
  2853. color = status_error ? ui.theme.error : ui.theme.text;
  2854. } else if (page_title[0]) {
  2855. s_copy(msg, page_title, sizeof(msg));
  2856. color = ui.theme.text;
  2857. } else {
  2858. s_copy(msg, "Ready", sizeof(msg));
  2859. color = ui.theme.text_dim;
  2860. }
  2861. kind = s_eq(page_url, "about:home") ? "HOME" :
  2862. page_is_local ? "FILE" :
  2863. (starts_with(page_url, "https://") ? "HTTPS" : "HTTP");
  2864. ngui_text_ellipsis(&ui, r, r.x + 6, r.y + 1, msg, color);
  2865. {
  2866. int kw = (int)ngui_text_width(kind);
  2867. ngui_text(&ui, r.x + r.w - 6 - kw, r.y + 1, kind,
  2868. ui.theme.text_dim);
  2869. }
  2870. }
  2871. static void draw(unsigned int *action)
  2872. {
  2873. struct ngui_rect r = {0, 0, ui.w, ui.h};
  2874. struct ngui_rect toolbar, status, page;
  2875. toolbar = ngui_cut_top(&r, BR_TOOLBAR_H);
  2876. status = ngui_cut_bottom(&r, BR_STATUS_H);
  2877. page = r;
  2878. draw_toolbar(toolbar, action);
  2879. draw_page(page);
  2880. draw_status(status);
  2881. }
  2882. static void redraw_now(void)
  2883. {
  2884. unsigned int action = 0;
  2885. ngui_begin(&ui);
  2886. draw(&action);
  2887. ngui_end(&ui);
  2888. }
  2889. static void handle_keys(unsigned int *action)
  2890. {
  2891. int k;
  2892. if (ngui_focus(&ui))
  2893. return;
  2894. if (ngui_scroll_keys(&ui, &page_scroll))
  2895. ngui_invalidate(&ui);
  2896. while ((k = ngui_key(&ui)) != 0) {
  2897. switch (k) {
  2898. case NGUI_KEY_ESC:
  2899. *action |= ACT_CLOSE;
  2900. break;
  2901. case NGUI_KEY_TAB:
  2902. case 0x0C: /* Ctrl+L */
  2903. ngui_set_focus(&ui, ID_URL);
  2904. break;
  2905. case 0x12: /* Ctrl+R */
  2906. *action |= ACT_RELOAD;
  2907. break;
  2908. case NGUI_KEY_BACKSPACE:
  2909. *action |= ACT_BACK;
  2910. break;
  2911. case NGUI_KEY_UP:
  2912. ngui_scroll_to(&page_scroll,
  2913. page_scroll.offset - 24);
  2914. ngui_invalidate(&ui);
  2915. break;
  2916. case NGUI_KEY_DOWN:
  2917. ngui_scroll_to(&page_scroll,
  2918. page_scroll.offset + 24);
  2919. ngui_invalidate(&ui);
  2920. break;
  2921. case NGUI_KEY_HOME:
  2922. ngui_scroll_to(&page_scroll, 0);
  2923. ngui_invalidate(&ui);
  2924. break;
  2925. case NGUI_KEY_END:
  2926. ngui_scroll_to(&page_scroll,
  2927. page_scroll.content_h -
  2928. page_scroll.view_h);
  2929. ngui_invalidate(&ui);
  2930. break;
  2931. default:
  2932. break;
  2933. }
  2934. }
  2935. }
  2936. static void handle_page_click(unsigned int *action)
  2937. {
  2938. int node;
  2939. char url[BR_URL_MAX];
  2940. if (!ui.pressed)
  2941. return;
  2942. if (ui.press_x < page_rect.x ||
  2943. ui.press_x >= page_rect.x + page_rect.w - 12 ||
  2944. ui.press_y < page_rect.y ||
  2945. ui.press_y >= page_rect.y + page_rect.h)
  2946. return;
  2947. node = find_hit(ui.press_x, ui.press_y);
  2948. if (!node)
  2949. return;
  2950. if (resolve_link(page_url, br_href(&doc, (unsigned int)node), url,
  2951. sizeof(url)) != 0)
  2952. return;
  2953. s_copy(click_url, url, sizeof(click_url));
  2954. *action |= ACT_NAV;
  2955. }
  2956. void _start(const char *arg)
  2957. {
  2958. struct win_info wi;
  2959. if (u_win_create(BR_W, BR_H, "Browser", &wi) != 0) {
  2960. u_puts("browser: no graphics window\n");
  2961. u_exit(1);
  2962. }
  2963. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  2964. url_in.placeholder = "http://...";
  2965. br_doc_init(&doc);
  2966. br_set_theme(ui.theme.text, ui.theme.bg, ui.theme.panel,
  2967. ui.theme.accent);
  2968. br_set_viewport(BR_VIEWPORT_W);
  2969. if (u_getcwd(cwd, sizeof(cwd)) != 0 || !cwd[0])
  2970. s_copy(cwd, "/", sizeof(cwd));
  2971. heap_clear();
  2972. page_scroll.offset = 0;
  2973. page_scroll.content_h = 0;
  2974. if (arg && arg[0])
  2975. navigate(arg, 1);
  2976. else
  2977. load_home(1);
  2978. for (;;) {
  2979. unsigned int action = 0;
  2980. unsigned int now;
  2981. click_url[0] = '\0';
  2982. if (pending_action) {
  2983. action |= pending_action & ACT_WAIT_NAV;
  2984. if (pending_url[0])
  2985. s_copy(click_url, pending_url, sizeof(click_url));
  2986. pending_action = 0;
  2987. pending_url[0] = '\0';
  2988. cancel_fetch = 0;
  2989. } else if (preload_active) {
  2990. if (preload_wait)
  2991. preload_wait = 0;
  2992. else
  2993. preload_step();
  2994. } else if (preload_pending &&
  2995. page_scroll.offset != preload_scroll) {
  2996. preload_scroll = page_scroll.offset;
  2997. preload_pending = 0;
  2998. preload_active = 1;
  2999. }
  3000. now = u_ticks_ms();
  3001. if (status_until && (int)(now - status_until) >= 0) {
  3002. status_text[0] = '\0';
  3003. status_until = 0;
  3004. status_error = 0;
  3005. ngui_invalidate(&ui);
  3006. }
  3007. if (!ngui_begin(&ui)) {
  3008. /* A queued navigation (e.g. Enter in the URL bar during
  3009. * a slow fetch) must be handled even when this frame
  3010. * needs no redraw. */
  3011. if (!action) {
  3012. u_sleep_ms(10);
  3013. continue;
  3014. }
  3015. } else {
  3016. draw(&action);
  3017. handle_keys(&action);
  3018. handle_page_click(&action);
  3019. ngui_end(&ui);
  3020. }
  3021. if (action & ACT_NAV) {
  3022. if (click_url[0]) {
  3023. navigate(click_url, 1);
  3024. click_url[0] = '\0';
  3025. } else {
  3026. navigate(url_in.text, 1);
  3027. }
  3028. }
  3029. if (action & ACT_BACK) {
  3030. if (hist_pos > 0) {
  3031. hist_pos--;
  3032. navigate(history[hist_pos], 0);
  3033. }
  3034. }
  3035. if (action & ACT_FORWARD) {
  3036. if (hist_pos + 1 < hist_len) {
  3037. hist_pos++;
  3038. navigate(history[hist_pos], 0);
  3039. }
  3040. }
  3041. if (action & ACT_RELOAD) {
  3042. if (s_eq(page_url, "about:home"))
  3043. load_home(0);
  3044. else
  3045. navigate(page_url, 0);
  3046. }
  3047. if (action & ACT_HOME)
  3048. load_home(1);
  3049. if (action & ACT_CLOSE)
  3050. quit = 1;
  3051. if (quit)
  3052. break;
  3053. if (pending_action)
  3054. continue;
  3055. u_sleep_ms(10);
  3056. }
  3057. fetch_helper_stop();
  3058. u_win_close();
  3059. u_exit(0);
  3060. }