browser.c 78 KB

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