browser.c 74 KB

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