Просмотр исходного кода

browser: downloads from a context menu with a toolbar panel

Johannes Zellner 1 день назад
Родитель
Сommit
4a08a46b4b

+ 38 - 7
AGENTS.md

@@ -616,8 +616,11 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   `SYS_GFX_BEGIN/PRESENT/END` terminal-takeover service and the kernel
   text-canvas renderer are removed.  Pointer input: `SYS_WIN_POINTER` reports
   the content-relative position, buttons and focus, and `SYS_WIN_EVENT` queues
-  press/release edges with a capture window, so a click that starts in a
-  window still gets its release after the pointer leaves.  The userland
+  left and right press/release edges with a capture window (its `button` field
+  says which button changed), so a click that starts in a window still gets
+  its release after the pointer leaves.  `ngui` exposes `right_pressed`/
+  `right_released` beside the left `pressed`/`released` edges, which widgets
+  do not consume; the browser uses them for its page context menu.  The userland
   `ngui.c/h` immediate-mode toolkit builds on those: clipped RGB565
   primitives, buttons, text input, the Adwaita symbolic icons and scrolling panes
   (`ngui_scroll`: an overlay scrollbar that is hidden while the pointer is
@@ -1245,8 +1248,8 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   ngui) uses the same ids for the bar's network and battery icons.
   `tools/mkicons.c` rasterizes
   each SVG with the browser's `svg_raster.c` into a 16x16 8-bit coverage
-  mask and writes one `NBICO1` bundle (16-byte header + masks, 24 icons,
-  ~6.2 KB, ~1.3 KB gzip) packed as `icons/adwaita16.icn` in **both** packs.
+  mask and writes one `NBICO1` bundle (16-byte header + masks, 25 icons,
+  ~6.3 KB, ~1.4 KB gzip) packed as `icons/adwaita16.icn` in **both** packs.
   Its XML reader handles only what symbolic icons use (`<svg viewBox>`,
   `<g>`, `<path>`, `<rect>`, `<circle>`, `<ellipse>`,
   fill/fill-rule/opacity) and
@@ -1788,6 +1791,29 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   Up/Down preview a row in the bar, Delete forgets the highlighted row,
   Esc closes the list (and drops the completion), a click opens a row.
   The in-session back/forward stack (`history[]`, 16 entries) is separate.
+  **Downloads**: right-clicking the page opens an in-window context menu
+  (`ngui.right_pressed`): over an `<img>` or a CSS `background-image` it
+  offers *Save image*, over a link *Save link*, and anywhere *Save page*
+  (not for `about:` URLs).  `dl_queue()` puts one job in a 24-entry static
+  list; a dedicated fifth `curl __serve` helper outside `fetch_workers`
+  fetches it, so a navigation that cancels the page's image workers does not
+  cancel the download.  The request line is `<seq> <cap> DL <url>`; the
+  helper skips gzip, reads the body in 16 KB slices and writes
+  `/share/bf...prog` (`seq`, bytes received, Content-Length or 0) at most
+  every 100 ms.  When `/data` is mounted the browser copies the body to
+  `/data/DOWNLOAD`, else `/share/DOWNLOAD`, with a FAT-safe 8.3 name from
+  the URL basename (MIME-type extension when there is none, `~N` collision
+  suffixes), then drops the helper files.  The body cap is the helper's
+  4 MB.  The toolbar's download button opens a panel with the newest twelve
+  jobs: live percentage/progress bar for the active one, size for a finished
+  file, error text for a failure, and a *Clear* action for completed
+  entries.  The icon carries a progress bar while a job runs and an accent
+  dot for unseen completions.  The list is `/share/bdownloads` plus
+  `/data/BDOWN.DAT` (8.3, plaintext `NBDL1\n` then
+  `state\nsize\nname\npath\n`), so history survives a reload/reboot; the
+  panel reads `/share` first, else `/data`.  One download runs at a time
+  (the extra helper keeps the page pool intact) and it continues while the
+  page scrolls, a new page loads or the window loses focus.
   Images are prefetched up to four at a time and only when their measured
   position is within the viewport plus 160 px; deferred images are loaded as
   the page scrolls.  Network work runs in up to `FETCH_WORKERS` (four)
@@ -2875,9 +2901,9 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
    with `--gc-sections`; the current full pack with the UI fonts, both
    WiFi firmwares (7265D 1,036,312 bytes, ~442 KiB gzip; 6000 454,608 bytes,
    ~204 KiB gzip), the RTL8821A firmware and tables (31,898 + 11,668
-   bytes) and `install/kernel.bin` is ~1.98 MiB gzip (~20 KB under the
-   cap), inflating to ~4.1 MB, which adds about
-   half a second to the boot; the floppy lite pack is ~638 KiB gzip and
+   bytes) and `install/kernel.bin` is ~2.00 MiB gzip (~3 KB under the
+   cap), inflating to ~4.13 MB, which adds about
+   half a second to the boot; the floppy lite pack is ~641 KiB gzip and
    deliberately omits the firmware, `install/kernel.bin` and the browser
    (`BROWSER` on `root.img` is the floppy copy).  `libdeflate-gzip -12` is still preferred.  Over
    115200 serial a raw `cmds-dev` pack (4.1 MiB) now takes about four
@@ -3103,6 +3129,11 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     normal browser's.  `@layer` ranks nested layers flat by first
     appearance (a parent layer's own rules lose to its sublayers, the spec
     says the reverse).
+    Downloads are capped at the helper's 4 MB, run one at a time, cannot be
+    cancelled or retried from the panel, use 8.3 names on both `/data` and
+    `/share`, skip `data:`/`mailto:`/`javascript:` targets, and the panel
+    shows only the newest twelve of 24 entries.  The history file is
+    last-writer-wins between browser windows.
     TLS is phase A (no certificate verification); `https` is shown as
     unverified.  The disk app
     is loaded from FAT at spawn time, so a cold floppy start takes a few

+ 1 - 0
assets/icons/adwaita/folder-download-symbolic.svg

@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="16px" height="16px" viewBox="0 0 16 16"><path fill="#2e3436" d="M7.984375 0c-.550781 0-1 .449219-1 1v8.585938L4.691406 7.292969C4.503906 7.105469 4.25 7 3.984375 7s-.519531.105469-.707031.292969c-.390625.390625-.390625 1.023437 0 1.414062l4 4c.390625.390625 1.023437.390625 1.414062 0l4-4c.390625-.390625.390625-1.023437 0-1.414062s-1.023437-.390625-1.414062 0L8.984375 9.585938V1c0-.550781-.449219-1-1-1m-6 14v2h12v-2zm0 0"/></svg>

+ 1 - 1
docs/development.html

@@ -239,7 +239,7 @@
 					<tr><td><code>make test-chat</code></td><td>Host tests (ASan/UBSan) for the chat client's JSON parser, cookie jar and the OIDC login page/reply/config parsers.</td></tr>
 					<tr><td><code>make test-chat-e2e</code></td><td>Boots QEMU with an NE2000 on slirp, starts <code>tools/mock_chat.py</code> and drives <code>chat</code> through QMP: native login, send, live poll, read marker, older paging, browse/join, new DM, saved-session resume and sign-out.  <code>CHAT_E2E=--totp|--consent|--passkey|--wrong</code> runs a login variant.</td></tr>
 					<tr><td><code>make compat-serve</code></td><td>Serve the browser compatibility ladder from <code>testdata/browser/compat/</code> on port 8000 for the board or QEMU guest.</td></tr>
-					<tr><td><code>make browser-host</code></td><td>Build the Linux SDL3 development harness (<code>browser-host</code> and its <code>host-curl</code> helper). It implements the ring-3 <code>SYS_*</code> ABI with host files, BSD sockets, <code>fork/exec</code> and an SDL3 window, so pages render and accept input without QEMU. Run <code>./browser-host [--size WxH] [--root DIR] URL|FILE</code>; <code>--headless --wait 3000 --shot out.ppm</code> writes the canvas without a display, and <code>--keys 'clo{down}{enter}'</code> types into it (one key per 100 ms; <code>{enter}</code>, <code>{esc}</code>, <code>{tab}</code>, <code>{bs}</code>, <code>{del}</code>, arrows, <code>{ctrl-l}</code>, <code>{wait}</code>, <code>{click:X,Y}</code> for a left click at canvas coordinates, and <code>{move:X,Y}</code> to only move the pointer, for hover). Needs the 32-bit SDL3/Wayland libraries (<code>lib32-sdl3</code>, <code>lib32-wayland</code>, <code>lib32-libxkbcommon</code>).</td></tr>
+					<tr><td><code>make browser-host</code></td><td>Build the Linux SDL3 development harness (<code>browser-host</code> and its <code>host-curl</code> helper). It implements the ring-3 <code>SYS_*</code> ABI with host files, BSD sockets, <code>fork/exec</code> and an SDL3 window, so pages render and accept input without QEMU. Run <code>./browser-host [--size WxH] [--root DIR] URL|FILE</code>; <code>--headless --wait 3000 --shot out.ppm</code> writes the canvas without a display, and <code>--keys 'clo{down}{enter}'</code> types into it (one key per 100 ms; <code>{enter}</code>, <code>{esc}</code>, <code>{tab}</code>, <code>{bs}</code>, <code>{del}</code>, arrows, <code>{ctrl-l}</code>, <code>{wait}</code>, <code>{click:X,Y}</code> for a left click at canvas coordinates, <code>{rclick:X,Y}</code> for a right click (context menus), and <code>{move:X,Y}</code> to only move the pointer, for hover). Needs the 32-bit SDL3/Wayland libraries (<code>lib32-sdl3</code>, <code>lib32-wayland</code>, <code>lib32-libxkbcommon</code>).</td></tr>
 					<tr><td><code>make fonts</code></td><td>Regenerate the checked-in Arimo/Liberation Mono subsets with <code>pyftsubset</code>.</td></tr>
 				</tbody>
 			</table>

+ 4 - 4
docs/graphics.html

@@ -475,7 +475,7 @@
 			</div>
 			<div class="card">
 				<h3>browser</h3>
-				<p>HTML/CSS renderer with flex rows, auto-fit and <code>repeat(N, 1fr)</code> card grids, <code>aspect-ratio</code> frames, inline-block runs, inline SVG icons, basic tables and inline SVG diagrams. It decodes inline images at their laid-out bounds (a JPEG gets a reduced IDCT) in the fetch helper, caches a display-size bitmap per image so repaints are 1:1, and fetches through the same helper; image repaints are coalesced and <code>/share/IMGSTAT</code> reports where a load spent its time. Inline and external scripts run on the vendored QuickJS engine (ES2025), and forms render and submit natively. The largest ring-3 application in the tree.</p>
+				<p>HTML/CSS renderer with flex rows, auto-fit and <code>repeat(N, 1fr)</code> card grids, <code>aspect-ratio</code> frames, inline-block runs, inline SVG icons, basic tables and inline SVG diagrams. It decodes inline images at their laid-out bounds (a JPEG gets a reduced IDCT) in the fetch helper, caches a display-size bitmap per image so repaints are 1:1, and fetches through the same helper; image repaints are coalesced and <code>/share/IMGSTAT</code> reports where a load spent its time. Inline and external scripts run on the vendored QuickJS engine (ES2025), and forms render and submit natively. Right-clicking the page opens a context menu that saves images, link targets or the page itself into <code>/data/DOWNLOAD</code> (or <code>/share/DOWNLOAD</code>); a toolbar download button shows live progress and the download history. The largest ring-3 application in the tree.</p>
 			</div>
 		</div>
 	</section>
@@ -502,8 +502,8 @@
 		<h2>UI icons</h2>
 		<p>
 			ngui's icons (<code>ngui_icon()</code>, <code>ngui_icon_button()</code>:
-			back/forward, reload and home in the browser toolbar, folder/file/drive
-			in <code>files</code>) and the WM's top-bar network/battery
+			back/forward, reload, home and downloads in the browser toolbar,
+			folder/file/drive in <code>files</code>) and the WM's top-bar network/battery
 			indicators are GNOME Adwaita symbolic icons
 			(adwaita-icon-theme 51, LGPL-3 or CC-BY-SA-3.0).  The unchanged
 			<code>*-symbolic.svg</code> files live in
@@ -513,7 +513,7 @@
 			8-bit coverage mask and writes one <code>NBICO1</code> bundle,
 			<code>/ramfs/icons/adwaita16.icn</code> (layout in
 			<span class="file">include/icon_abi.h</span>): a 16-byte header and
-			24 masks, ~6.2&nbsp;KB raw and ~1.3&nbsp;KB gzip, so both the USB
+			25 masks, ~6.3&nbsp;KB raw and ~1.4&nbsp;KB gzip, so both the USB
 			and the floppy pack carry it.
 		</p>
 		<ul>

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
docs/network.html


+ 1 - 0
docs/userland.html

@@ -408,6 +408,7 @@
 			<li><strong>Inline SVG.</strong> <span class="file">svg_raster.c</span> flattens every path command (curves and arcs included), rects, circles, ellipses, lines, polylines and polygons through <code>transform</code> lists and the viewBox mapping, then fills (nonzero or even-odd) and strokes them with antialiased coverage into an RGB565 + alpha layer cached per node and size. <code>&lt;g&gt;</code>, nested <code>&lt;svg&gt;</code>, <code>&lt;use&gt;</code> of a <code>&lt;symbol&gt;</code>, opacity, <code>currentColor</code> and inherited <code>fill</code>/<code>stroke</code> work; a gradient paints the average of its stops, and patterns, clip paths, masks, filters and markers are not rendered. SVG paint has its own style state, so CSS <code>fill</code> on an HTML element no longer paints its background.</li>
 			<li><strong>Caps.</strong> 2 MB page body, 832 KB CSS, 6144 DOM nodes, 4608 rules, 16 images. Each image gets an <code>img_estimate()</code>-sized decode arena that is freed after the scaled bitmap is made; navigation calls <code>heap_reset()</code> and unmaps the page heap.</li>
 			<li><strong>Fetching.</strong> Up to four worker processes prefetch images in parallel within the viewport plus 160 px; each worker keeps its own TCP/TLS connection and its own request/body/meta files (keyed by browser pid and worker slot, so multiple windows do not collide).  Completed images are decoded as jobs finish.  Stylesheet and external-script bodies are downloaded in parallel but parsed/executed in document order, and one image starts before the script phase so it overlaps script loading.  Idle helpers stay warm for same-origin navigations (they exit after ten idle seconds, and losing window focus stops them), while a cross-origin navigation closes them first so the old page's connections cannot stall the new fetch.  A DNS failure is retried once.  While loading, the status bar reports the page fetch's header and first-byte times; when the load is done it shows <code>Loaded in N ms: page P (headers H), parse, css, paint, js</code> for 12 s.  On the Cloudron store under QEMU/TCG with e1000 that is 2572 ms, of which the scripts take 1911 ms; QuickJS is built <code>-O1</code>, which beat <code>-Os</code> and <code>-O2</code> there.</li>
+			<li><strong>Downloads.</strong> Right-clicking the page opens a small context menu: over an <code>&lt;img&gt;</code> or a CSS <code>background-image</code> it offers <em>Save image</em>, over a link <em>Save link</em>, and anywhere <em>Save page</em>. A download is queued, fetched by a dedicated fifth <code>curl __serve</code> helper (so it survives navigation and the page workers stopping), then copied into <code>/data/DOWNLOAD</code>, or <code>/share/DOWNLOAD</code> when no installed data volume exists. Files use FAT-safe 8.3 names derived from the URL basename, with <code>~1</code> suffixes for collisions and a MIME-type extension when the URL has none; the 4 MB helper body cap applies. The toolbar's download button opens a panel with the newest twelve entries, live percentage and progress bar for the active one, sizes for finished files, and errors for failures. History is kept in <code>/share/bdownloads</code> and <code>/data/BDOWN.DAT</code> (<code>NBDL1</code>, then state/size/name/path records), so it survives a reload and, on installed boards, a reboot; <em>Clear</em> removes completed entries. One download runs at a time, and downloads continue in the background while the page scrolls or a new page loads.</li>
 			<li><strong>URL entry.</strong> A dotted name without a scheme that is not a known local file extension is treated as a hostname, so <code>cloudron.io</code> does not hit the local-file stat. A typed hostname is tried as <code>https://</code> first, skipping the common <code>http://host</code> → <code>https://host</code> hop; if the https connection is refused, reset, times out or fails the TLS handshake, the browser retries once over <code>http://</code> and replaces the history entry. Origin-level redirects (such as <code>https://host</code> → <code>https://www.host</code>) are cached per process and in <code>/share/borigins</code>; the first visit still follows them, later visits go straight to the canonical origin. Remote stylesheets are cached under <code>/share/bcss-*</code> for the current boot, so a reload or a fresh browser launch skips the CSS downloads.</li>
 			<li><strong>History and autocomplete.</strong> Every page that finishes loading is recorded in a 128-entry table (URL, title, visit count and a stamp counter that orders visits across windows and reboots without a clock). Browser windows of one boot share <code>/share/bhist</code>, merged and rewritten on each visit, and it is saved to <code>/data/BHIST.DAT</code> at most every 3 s. While the address bar has the focus a dropdown lists up to six matches: an empty bar shows the most visited and recent sites; typing ranks URL prefixes (with or without the scheme and <code>www.</code>) above URL and title substrings. Typing at the end completes inline to the next <code>/</code> of the best match (<code>clo</code> &rarr; <code>cloudron.io/</code>, the added part selected), and Enter opens the history URL it came from. Up/Down preview a row, Delete forgets it, Esc closes the list and a click opens a row.</li>
 			</ul>

+ 2 - 1
host/main.c

@@ -6,7 +6,8 @@
  * --keys types into a headless window, one key every 100 ms from 300 ms
  * after the first frame: plain characters, plus {enter} {esc} {tab} {bs}
  * {del} {up} {down} {left} {right} {ctrl-l}, {wait} (a 1 s pause) and
- * {click:X,Y} (a left click at canvas coordinates X,Y) and {move:X,Y}
+ * {click:X,Y} (a left click at canvas coordinates X,Y),
+ * {rclick:X,Y} (a right click) and {move:X,Y}
  * (move the pointer there without a click, for hover).
  */
 #include <signal.h>

+ 14 - 7
host/window_headless.c

@@ -108,11 +108,12 @@ void host_headless_pump(unsigned int timeout_ms)
 		usleep(timeout_ms * 1000u);
 }
 
-/* --keys `{click:X,Y}`: the pointer moves there and a left press is
- * queued, its release on the next event poll; `{move:X,Y}` only moves
- * it (hover). */
+/* --keys `{click:X,Y}` / `{rclick:X,Y}`: the pointer moves there and a
+ * left/right press is queued, its release on the next event poll;
+ * `{move:X,Y}` only moves it (hover). */
 static int ptr_x, ptr_y;
 static unsigned int ptr_buttons;
+static unsigned int click_button;   /* bit0 left, bit1 right */
 static int click_pending;       /* 1 press queued, 2 release queued */
 
 int host_headless_pointer(struct win_pointer *out)
@@ -132,13 +133,14 @@ int host_headless_event(struct win_event *out)
 		return 0;
 	out->x = ptr_x;
 	out->y = ptr_y;
+	out->button = click_button;
 	if (click_pending == 1) {
 		out->type = WIN_EV_POINTER_DOWN;
-		ptr_buttons = 1;
+		ptr_buttons |= click_button;
 		click_pending = 2;
 	} else {
 		out->type = WIN_EV_POINTER_UP;
-		ptr_buttons = 0;
+		ptr_buttons &= ~click_button;
 		click_pending = 0;
 	}
 	out->buttons = ptr_buttons;
@@ -185,11 +187,16 @@ int host_headless_key(void)
 		key_due = now + 300;
 		return 0;
 	}
-	if (code == '{' && !strncmp(key_pos, "click:", 6)) {
+	if (code == '{' && (!strncmp(key_pos, "click:", 6) ||
+			    !strncmp(key_pos, "rclick:", 7))) {
 		const char *end = strchr(key_pos, '}');
+		int right = key_pos[0] == 'r';
 
-		if (sscanf(key_pos + 6, "%d,%d", &ptr_x, &ptr_y) == 2)
+		if (sscanf(key_pos + (right ? 7 : 6), "%d,%d",
+			   &ptr_x, &ptr_y) == 2) {
+			click_button = right ? 2u : 1u;
 			click_pending = 1;
+		}
 		key_pos = end ? end + 1 : key_pos + strlen(key_pos);
 		key_due = now + 300;
 		return 0;

+ 3 - 2
host/window_sdl3.c

@@ -55,7 +55,7 @@ static int pop_key(void)
 	return k;
 }
 
-static void push_event(unsigned int type, int x, int y)
+static void push_event(unsigned int type, int x, int y, unsigned int button)
 {
 	unsigned int next = (ev_head + 1) % EVQ_MAX;
 	struct win_event *ev;
@@ -67,6 +67,7 @@ static void push_event(unsigned int type, int x, int y)
 	ev->x = x;
 	ev->y = y;
 	ev->buttons = mouse_buttons;
+	ev->button = button;
 	ev_head = next;
 }
 
@@ -238,7 +239,7 @@ static void handle_event(SDL_Event *e)
 				mouse_buttons &= ~bit;
 			push_event(e->type == SDL_EVENT_MOUSE_BUTTON_DOWN ?
 				   WIN_EV_POINTER_DOWN : WIN_EV_POINTER_UP,
-				   mouse_x, mouse_y);
+				   mouse_x, mouse_y, bit);
 		}
 		break;
 	}

+ 2 - 1
include/icon_abi.h

@@ -35,7 +35,8 @@
 	X(NET_WIFI_3,    "wifi-3",  "network-wireless-signal-good-symbolic") \
 	X(NET_WIFI_4,    "wifi-4",  "network-wireless-signal-excellent-symbolic") \
 	X(NET_WIFI_OFF,  "wifi-off", "network-wireless-offline-symbolic") \
-	X(NET_USB,       "usb",     "phone-symbolic")
+	X(NET_USB,       "usb",     "phone-symbolic")               \
+	X(DOWNLOAD,      "download", "folder-download-symbolic")
 
 /* The NBICO1 bundle, read by ngui and the WM's uwm_lib. */
 #define ICON_BUNDLE_PATH "/ramfs/icons/adwaita16.icn"

+ 1 - 0
include/wm_abi.h

@@ -155,6 +155,7 @@ struct win_event {
 	unsigned int type;         /* WIN_EV_POINTER_* */
 	int x, y;                  /* content-relative */
 	unsigned int buttons;      /* buttons after the event */
+	unsigned int button;       /* button that changed: bit0 left, bit1 right */
 };
 
 #endif

+ 26 - 13
kernel/wm.c

@@ -978,7 +978,8 @@ int wm_server_close(unsigned int index)
 }
 
 static void wm_queue_ptr_event(struct window *w, unsigned int type,
-			       int x, int y, unsigned int buttons)
+			       int x, int y, unsigned int buttons,
+			       unsigned int button)
 {
 	unsigned int next = (w->ptr_head + 1) % 8;
 
@@ -988,6 +989,7 @@ static void wm_queue_ptr_event(struct window *w, unsigned int type,
 	w->ptr_events[w->ptr_head].x = x;
 	w->ptr_events[w->ptr_head].y = y;
 	w->ptr_events[w->ptr_head].buttons = buttons;
+	w->ptr_events[w->ptr_head].button = button;
 	w->ptr_head = next;
 	/* Wake a window blocked in SYS_WIN_KEY_WAIT so a click (e.g. on the
 	 * terminal's scrollbar) is handled now, not on the next poll timeout. */
@@ -1008,26 +1010,36 @@ static void wm_route_pointer(unsigned int mx, unsigned int my,
 		return;
 	}
 
-	if (press & 1) {
-		int edges = 0;
-		int idx = wm_resize_target((int)mx, (int)my, &edges);
+	if (press & 3) {
+		int idx = wm_ptr_capture;
 
-		if (idx >= 0 && !edges) {
+		if (idx < 0) {
+			int edges = 0;
+
+			idx = wm_resize_target((int)mx, (int)my, &edges);
+			if (edges)
+				idx = -1;
+		}
+		if (idx >= 0 && idx < (int)MAX_WINDOWS) {
 			struct window *w = &windows[idx];
 			int cx = (int)mx - win_content_x(w);
 			int cy = (int)my - win_content_y(w);
 
-			if (w->canvas_window && cx >= 0 && cy >= 0 &&
-			    cx < (int)w->canvas.w && cy < (int)w->canvas.h &&
-			    (w->popover ||
-			     !win_in_resize_border(w, mx, my))) {
+			/* A second button pressed after a capture may leave the
+			 * content, but still belongs to the window that owns the
+			 * pointer. */
+			if (w->used && w->canvas_window &&
+			    (wm_ptr_capture == idx ||
+			     (cx >= 0 && cy >= 0 && cx < (int)w->canvas.w &&
+			      cy < (int)w->canvas.h &&
+			      (w->popover || !win_in_resize_border(w, mx, my))))) {
 				wm_queue_ptr_event(w, WIN_EV_POINTER_DOWN,
-						   cx, cy, buttons);
+						   cx, cy, buttons, press & 3);
 				wm_ptr_capture = idx;
 			}
 		}
 	}
-	if ((release & 1) && wm_ptr_capture >= 0) {
+	if ((release & 3) && wm_ptr_capture >= 0) {
 		struct window *w = &windows[wm_ptr_capture];
 
 		if (w->used && w->canvas_window) {
@@ -1035,9 +1047,10 @@ static void wm_route_pointer(unsigned int mx, unsigned int my,
 			int cy = (int)my - win_content_y(w);
 
 			wm_queue_ptr_event(w, WIN_EV_POINTER_UP,
-					   cx, cy, buttons);
+					   cx, cy, buttons, release & 3);
 		}
-		wm_ptr_capture = -1;
+		if (!(buttons & 3))
+			wm_ptr_capture = -1;
 	}
 	wm_ptr_buttons = buttons;
 }

Разница между файлами не показана из-за своего большого размера
+ 972 - 14
user/apps/browser/browser.c


+ 61 - 9
user/apps/curl/ucmd_curl.c

@@ -75,6 +75,7 @@ struct fetch_paths {
 	char done[FETCH_PATH_MAX];
 	char post[FETCH_PATH_MAX];
 	char px[FETCH_PATH_MAX];
+	char prog[FETCH_PATH_MAX];
 };
 static struct fetch_paths fp;
 
@@ -126,6 +127,7 @@ static void fetch_paths_init(unsigned int session, int slot)
 	fetch_path(fp.done, sizeof(fp.done), "done", session, slot);
 	fetch_path(fp.post, sizeof(fp.post), "post", session, slot);
 	fetch_path(fp.px, sizeof(fp.px), "px", session, slot);
+	fetch_path(fp.prog, sizeof(fp.prog), "prog", session, slot);
 }
 
 /* Optional decode in the fetch helper.  The browser's UI thread only has
@@ -381,16 +383,37 @@ static void write_meta(unsigned int seq, int with_seq, struct uhttp *h,
 	u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n);
 }
 
-/* Fetch one URL through a possibly reused keep-alive connection. */
+/* Progress for the browser's download manager: the helper writes the bytes
+ * received so far while the body is still arriving.  `total` is 0 when the
+ * server sent no Content-Length. */
+static void write_progress(const char *path, unsigned int seq,
+			   unsigned int got, unsigned int total)
+{
+	static char buf[80];
+	unsigned int n = 0;
+
+	app_dec(buf, &n, seq, sizeof(buf));
+	app_ch(buf, &n, '\n', sizeof(buf));
+	app_dec(buf, &n, got, sizeof(buf));
+	app_ch(buf, &n, '\n', sizeof(buf));
+	app_dec(buf, &n, total, sizeof(buf));
+	app_ch(buf, &n, '\n', sizeof(buf));
+	u_syscall3(SYS_WRITE_FILE, (int)path, (int)buf, (int)n);
+}
+
+/* Fetch one URL through a possibly reused keep-alive connection.  `prog`
+ * (with `prog_seq`) turns on progress publishing for a download. */
 static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
 			 const char *method, const char *ctype,
 			 const char *post, unsigned int post_len,
 			 unsigned char *buf, unsigned int *outlen,
 			 unsigned int *outtrunc, int *outerr,
-			 struct fetch_times *times)
+			 struct fetch_times *times, unsigned int open_flags,
+			 const char *prog, unsigned int prog_seq)
 {
 	unsigned int total = 0;
 	unsigned int t0 = u_ticks_ms(), t1, tfirst = 0;
+	unsigned int last_prog = 0;
 	int rv, trunc = 0, err = 0;
 
 	*outlen = 0;
@@ -406,13 +429,13 @@ static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
 	if (!ctype || !ctype[0])
 		ctype = "application/x-www-form-urlencoded";
 	uhttp_set_method(h, method, post, post_len, ctype);
-	rv = uhttp_open(h, url, UHTTP_GZIP, 0);
+	rv = uhttp_open(h, url, open_flags, 0);
 	if (rv == UHTTP_ERR_BUSY) {
 		int tries;
 
 		for (tries = 0; tries < 50 && rv == UHTTP_ERR_BUSY; tries++) {
 			u_sleep_ms(20);
-			rv = uhttp_open(h, url, UHTTP_GZIP, 0);
+			rv = uhttp_open(h, url, open_flags, 0);
 		}
 	}
 	t1 = u_ticks_ms();
@@ -424,13 +447,29 @@ static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
 		*outerr = rv;
 		return;
 	}
+	if (prog && h->resp.have_length)
+		write_progress(prog, prog_seq, 0, h->resp.content_length);
+	last_prog = u_ticks_ms();
 	for (;;) {
-		rv = uhttp_read(h, buf + total, cap - total,
+		unsigned int want = cap - total;
+
+		/* A download reads in small slices so the progress file
+		 * changes while the body is still arriving; uhttp_read()
+		 * fills its whole buffer otherwise. */
+		if (prog && want > 16u * 1024u)
+			want = 16u * 1024u;
+		rv = uhttp_read(h, buf + total, want,
 				UHTTP_IDLE_TIMEOUT_MS);
 		if (rv > 0) {
 			if (!tfirst)
 				tfirst = u_ticks_ms();
 			total += (unsigned int)rv;
+			if (prog && (unsigned int)(u_ticks_ms() - last_prog) >= 100) {
+				last_prog = u_ticks_ms();
+				write_progress(prog, prog_seq, total,
+					       h->resp.have_length ?
+					       h->resp.content_length : 0);
+			}
 			if (total >= cap) {
 				trunc = 1;
 				break;
@@ -441,6 +480,9 @@ static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
 			err = rv;
 		break;
 	}
+	if (prog)
+		write_progress(prog, prog_seq, total,
+			       h->resp.have_length ? h->resp.content_length : 0);
 	*outlen = total;
 	*outtrunc = (unsigned int)trunc;
 	*outerr = err;
@@ -494,8 +536,8 @@ static void fetch_once(void)
 
 	uhttp_init(&h);
 	h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
-	helper_fetch(&h, req + i, cap, 0, 0, 0, 0, buf, &body_len, &body_trunc, &err,
-		     &times);
+	helper_fetch(&h, req + i, cap, 0, 0, 0, 0, buf, &body_len,
+		     &body_trunc, &err, &times, UHTTP_GZIP, 0, 0);
 	if (body_len)
 		u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
 			   (int)body_len);
@@ -560,7 +602,7 @@ static void fetch_serve(const char *arg)
 		unsigned int seq = 0, cap = 0, body_len = 0, body_trunc = 0;
 		unsigned int post_len = 0, jar_changes;
 		unsigned int dec_w = 0, dec_h = 0;
-		int decode = 0;
+		int decode = 0, download = 0;
 		int is_post = 0;
 		struct fetch_px px;
 		char method[12], ctype[96];
@@ -615,6 +657,12 @@ static void fetch_serve(const char *arg)
 					i++;
 				decode = 1;
 			}
+			/* "DL <url>": a browser download.  No gzip and a progress
+			 * file while the body arrives. */
+			if (!decode && !strncmp(req + i, "DL ", 3)) {
+				download = 1;
+				i += 3;
+			}
 			/* "X <method> <body length> <content type or -> <url>":
 			 * any method, for the browser's fetch()/XHR. */
 			if (!decode && !strncmp(req + i, "X ", 2)) {
@@ -696,9 +744,13 @@ static void fetch_serve(const char *arg)
 		 * jar-less request after the OIDC login POST drops the
 		 * interaction cookie ("authorization request has expired"). */
 		h.jar = &fetch_jar;
+		if (download)
+			u_syscall1(SYS_UNLINK, (int)fp.prog);
 		helper_fetch(&h, url, cap, method[0] ? method : 0, ctype,
 			     post_len ? (char *)post : 0, post_len,
-			     buf, &body_len, &body_trunc, &err, &times);
+			     buf, &body_len, &body_trunc, &err, &times,
+			     download ? 0 : UHTTP_GZIP,
+			     download ? fp.prog : 0, seq);
 		if (fetch_jar.changes != jar_changes)
 			jar_save();
 		if (body_len)

+ 2 - 1
user/apps/term/ucmd_term.c

@@ -475,7 +475,8 @@ static void handle_pointer(void)
 	struct win_pointer p;
 
 	while (u_win_event(&e) == 1) {
-		if (e.type == WIN_EV_POINTER_DOWN) {
+		if (e.type == WIN_EV_POINTER_DOWN &&
+		    ((e.button ? e.button : e.buttons) & 1)) {
 			if (e.x >= (int)(wi.width - SB_W) && e.y >= 0 &&
 			    e.y < (int)wi.height) {
 				unsigned int track_h, thumb_h, thumb_y;

+ 29 - 8
user/lib/ngui/ngui.c

@@ -210,6 +210,7 @@ int ngui_begin(struct ngui *ui)
 
 	ui->dirty = 0;
 	ui->pressed = ui->released = 0;
+	ui->right_pressed = ui->right_released = 0;
 	ui->clicked = 0;
 	ui->focus_claimed = 0;
 	ui->hot = 0;
@@ -256,14 +257,33 @@ int ngui_begin(struct ngui *ui)
 		if (r != 1)
 			break;
 		if (ev.type == WIN_EV_POINTER_DOWN) {
-			ui->pressed |= 1;
-			ui->press_x = ev.x;
-			ui->press_y = ev.y;
-			ui->clicked = 1;
+			unsigned int b = ev.button ? ev.button : ev.buttons;
+
+			if (b & 1) {
+				ui->pressed |= 1;
+				ui->press_x = ev.x;
+				ui->press_y = ev.y;
+				ui->clicked = 1;
+			}
+			if (b & 2) {
+				ui->right_pressed |= 1;
+				ui->right_press_x = ev.x;
+				ui->right_press_y = ev.y;
+			}
 		} else if (ev.type == WIN_EV_POINTER_UP) {
-			ui->released |= 1;
-			ui->release_x = ev.x;
-			ui->release_y = ev.y;
+			unsigned int b = ev.button ? ev.button :
+				(~ev.buttons & 3);
+
+			if (b & 1) {
+				ui->released |= 1;
+				ui->release_x = ev.x;
+				ui->release_y = ev.y;
+			}
+			if (b & 2) {
+				ui->right_released |= 1;
+				ui->right_release_x = ev.x;
+				ui->right_release_y = ev.y;
+			}
 		}
 	}
 
@@ -323,7 +343,8 @@ int ngui_begin(struct ngui *ui)
 	ui->last_inside = ui->inside;
 	ui->last_focused = ui->focused;
 
-	if (ui->pressed || ui->released)
+	if (ui->pressed || ui->released || ui->right_pressed ||
+	    ui->right_released)
 		want = 1;
 	if (want)
 		ui->motion_pending = 0;

+ 7 - 1
user/lib/ngui/ngui.h

@@ -162,11 +162,17 @@ struct ngui {
 	/* Scroll accumulated since the previous frame, in pixels. */
 	int scroll_x, scroll_y;
 
-	/* Press/release edges collected by ngui_begin(). */
+	/* Press/release edges collected by ngui_begin().  `pressed` and
+	 * `released` are the left button; the right-button edges let an app
+	 * open a context menu without taking the normal click path. */
 	unsigned int pressed;
 	unsigned int released;
+	unsigned int right_pressed;
+	unsigned int right_released;
 	int press_x, press_y;
 	int release_x, release_y;
+	int right_press_x, right_press_y;
+	int right_release_x, right_release_y;
 	int clicked;
 	int focus_claimed;
 

Некоторые файлы не были показаны из-за большого количества измененных файлов