browser.c 80 KB

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