Przeglądaj źródła

wm: make the resize border invisible and own the pointer during drags

Windows had a 4 px resize margin inside the outer edge and the WM's
polling resize leaked the held button to the content: the right-edge
scrollbar could start a drag while the window was being resized.

Borderless windows now have a resize band of 1 px inside and 3 px
outside the outer edge.  The WM and kernel share the topmost hit rule,
so a front window's outside band wins over a window behind it, and the
WM shows the matching edge/corner resize cursor.

Add SYS_WM_MOUSE_CAPTURE (121): the WM takes the pointer when a title
drag or resize starts and releases it on the button-up.  While owned,
the kernel drops canvas press/release routing, reports buttons=0 and
inside=0 from SYS_WIN_POINTER, and discards scroll deltas, so a held
button cannot reach a scrollbar inside the window.  Capture is reset
when the WM dies or the target window disappears.

Update the WM, cursor and syscall documentation.
Johannes Zellner 2 dni temu
rodzic
commit
69b271dc69
12 zmienionych plików z 390 dodań i 136 usunięć
  1. 23 10
      AGENTS.md
  2. 10 3
      docs/graphics.html
  3. 1 1
      docs/userland.html
  4. 2 0
      include/syscall.h
  5. 7 0
      include/wm_abi.h
  6. 5 0
      kernel/sys_wm.c
  7. 1 0
      kernel/sys_wm.h
  8. 2 0
      kernel/syscall.c
  9. 129 26
      kernel/wm.c
  10. 1 0
      kernel/wm.h
  11. 202 96
      user/apps/wm/ucmd_wm.c
  12. 7 0
      user/ucmd_lib.h

+ 23 - 10
AGENTS.md

@@ -408,7 +408,12 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   detection and the boot-console handoff.  Every window is a canvas client;
   detection and the boot-console handoff.  Every window is a canvas client;
   there is no in-kernel WM or terminal fallback.  The WM paints the desktop,
   there is no in-kernel WM or terminal fallback.  The WM paints the desktop,
   spawns the first terminal after `wm_server_boot_ready()` (set after the
   spawns the first terminal after `wm_server_boot_ready()` (set after the
-  storage mount), and composites the canvases into the shadow.  The boot
+  storage mount), and composites the canvases into the shadow.  A normal
+  window has no visible frame: its `WM_TITLE_H` title bar starts at the
+  window origin and a resize band (`WM_RESIZE_IN` 1 px inside,
+  `WM_RESIZE_OUT` 3 px outside every outer edge) is the edge/corner resize
+  grab area.  The focused title bar uses
+  the toolkit highlight `ngui_theme_dark.selection`.  The boot
   console stays visible until the WM attaches (`wm_server_attach` disables
   console stays visible until the WM attaches (`wm_server_attach` disables
   it), so a missing `/ramfs/bin/wm` leaves the kernel boot log on screen and
   it), so a missing `/ramfs/bin/wm` leaves the kernel boot log on screen and
   the supervisor retries.  `wm_server_present` flushes the damage rects;
   the supervisor retries.  `wm_server_present` flushes the damage rects;
@@ -433,9 +438,13 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   keeping the grab point at the same fraction of the title bar.  While
   keeping the grab point at the same fraction of the title bar.  While
   dragging, screen edges and the facing edges of windows that overlap on the
   dragging, screen edges and the facing edges of windows that overlap on the
   other axis resist the window: it stays flush until the pointer overshoots
   other axis resist the window: it stays flush until the pointer overshoots
-  by `WM_RESIST` (24 px) (`resist_geom()` in `ucmd_wm.c`).  The resize grip
-  grows a window to the full work area (snapping when dragged into the screen
-  corner).  The kernel hit-test uses the same order.
+  by `WM_RESIST` (24 px) (`resist_geom()` in `ucmd_wm.c`).  A press in the
+  resize band (1 px inside, 3 px outside) along any edge or corner starts a
+  resize: the grabbed edges follow the pointer while the opposite edges stay
+  put, clamped to the work area.  The topmost window's band wins over a
+  window behind it.  While dragged or resized the WM owns the pointer
+  (`SYS_WM_MOUSE_CAPTURE`), so canvas clients see no held button and cannot
+  start a scrollbar drag.  The kernel hit-test uses the same order.
   The right-aligned date/time text is itself a click target: `draw_bar()`
   The right-aligned date/time text is itself a click target: `draw_bar()`
   records its rectangle, the pointer highlights it, and a click opens the
   records its rectangle, the pointer highlights it, and a click opens the
   control panel (the `Clock` launcher still opens the analog clock).
   control panel (the `Clock` launcher still opens the analog clock).
@@ -461,7 +470,8 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   The canvas fills the whole window rectangle: there is no outer shadow, rim
   The canvas fills the whole window rectangle: there is no outer shadow, rim
   or rounded corner, so it reads as a flat menu panel.  The kernel uses the
   or rounded corner, so it reads as a flat menu panel.  The kernel uses the
   window origin for content-relative pointer and scroll coordinates (a normal
   window origin for content-relative pointer and scroll coordinates (a normal
-  window uses the 1 px border plus title bar).  An outside press dismisses the
+  window starts its content one title bar below the origin; a popover uses
+  the origin directly).  An outside press dismisses the
   popover and then continues with the normal click handling; a second click
   popover and then continues with the normal click handling; a second click
   on a status target toggles it.
   on a status target toggles it.
   `Chat` spawns `/ramfs/bin/chat` and the last launcher, `Disks`, spawns
   `Chat` spawns `/ramfs/bin/chat` and the last launcher, `Disks`, spawns
@@ -490,8 +500,10 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   input.  `SYS_WIN_SCROLL` returns scroll accumulated for the canvas window
   input.  `SYS_WIN_SCROLL` returns scroll accumulated for the canvas window
   under the pointer, in pixels (`+dy` = later content), so every client sees
   under the pointer, in pixels (`+dy` = later content), so every client sees
   one unit independent of whether it came from the touchpad or a wheel.
   one unit independent of whether it came from the touchpad or a wheel.
-  `SYS_WIN_SET_CURSOR` (117) sets the caller's content cursor and
-  `SYS_WM_SET_CURSOR` (118) the WM's override (see the Mouse cursors bullet).
+  `SYS_WIN_SET_CURSOR` (117) sets the caller's content cursor,
+  `SYS_WM_SET_CURSOR` (118) the WM's override and `SYS_WM_MOUSE_CAPTURE`
+  (121) takes/releases the pointer for a WM drag or resize (see the Mouse
+  cursors bullet).
   `SYS_WIN_RESIZE_SELF` (115) lets a canvas window grow or shrink itself and
   `SYS_WIN_RESIZE_SELF` (115) lets a canvas window grow or shrink itself and
   `SYS_SCREEN_INFO` (116) returns the framebuffer dimensions; the panel uses
   `SYS_SCREEN_INFO` (116) returns the framebuffer dimensions; the panel uses
   them to collapse to the pills or grow over an expanded section.  The kernel
   them to collapse to the pills or grow over an expanded section.  The kernel
@@ -1069,9 +1081,10 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   `SYS_WM_POLL`: the WM override (`SYS_WM_SET_CURSOR`), else the cursor of
   `SYS_WM_POLL`: the WM override (`SYS_WM_SET_CURSOR`), else the cursor of
   the window holding the pointer capture, else the content cursor
   the window holding the pointer capture, else the content cursor
   (`SYS_WIN_SET_CURSOR`, `struct window.cursor`) of the window under the
   (`SYS_WIN_SET_CURSOR`, `struct window.cursor`) of the window under the
-  pointer (not on a normal window's 16 px grip), else `CURSOR_DEFAULT`; a WM
-  restart clears the override.  The ring-3 WM overrides with resize arrows
-  on the grip and while resizing, the move cross during a title-bar drag
+  pointer (not on a normal window's resize band), else
+  `CURSOR_DEFAULT`; a WM restart clears the override.  The ring-3 WM
+  overrides with resize arrows on the resize band and while
+  resizing, the move cross during a title-bar drag
   (once it moved), and a hand over launchers and the status area.  ngui
   (once it moved), and a hand over launchers and the status area.  ngui
   resets `ui->cursor` to the arrow for every redrawn frame (begin returned
   resets `ui->cursor` to the arrow for every redrawn frame (begin returned
   1), `ngui_text_input` asks for the I-beam (not-allowed when disabled),
   1), `ngui_text_input` asks for the I-beam (not-allowed when disabled),

+ 10 - 3
docs/graphics.html

@@ -152,6 +152,10 @@
 			level-filled battery on battery, with a percentage tooltip below
 			level-filled battery on battery, with a percentage tooltip below
 			the bar on hover, painted above the windows), plus draggable
 			the bar on hover, painted above the windows), plus draggable
 			terminal and canvas windows with a title bar and close button.
 			terminal and canvas windows with a title bar and close button.
+			A normal window has no visible frame; an invisible resize band
+			(1 px inside, 3 px outside its outer edge) is the resize grab
+			area, and the active title bar uses the toolkit's highlight
+			colour.
 			Click-to-focus, drag-by-title-bar and close are routed through the
 			Click-to-focus, drag-by-title-bar and close are routed through the
 			same pointer path. To the left of the rightmost AC/battery status
 			same pointer path. To the left of the rightmost AC/battery status
 			icon, a network status icon shows a
 			icon, a network status icon shows a
@@ -330,8 +334,11 @@
 			which makes side-by-side placement exact. Windows can be dragged past the left, right and
 			which makes side-by-side placement exact. Windows can be dragged past the left, right and
 			bottom edges; <code>WM_DRAG_VISIBLE</code> (48 px) of the title bar
 			bottom edges; <code>WM_DRAG_VISIBLE</code> (48 px) of the title bar
 			always stays on-screen for recovery, and the top bar is never
 			always stays on-screen for recovery, and the top bar is never
-			overlapped. The resize grip is clamped to the full work area and
-			snaps to it when dragged into the screen corner.
+			overlapped. An invisible resize band (1 px inside, 3 px outside)
+			runs along every edge and corner: the grabbed edges follow the
+			pointer while the opposite edges stay put, clamped to the work
+			area. While a drag or resize is active the WM owns the pointer,
+			so the window content does not see the held button.
 		</p>
 		</p>
 		<div class="note">
 		<div class="note">
 			<p><strong>Buttons fire on the release edge.</strong> The toolkit keeps
 			<p><strong>Buttons fire on the release edge.</strong> The toolkit keeps
@@ -494,7 +501,7 @@
 		</p>
 		</p>
 		<ul>
 		<ul>
 			<li><strong>Loading.</strong> <span class="file">cursor.c</span> copies the bundle out of the ramfs into its own pages after <code>ramfs_init()</code> and after every <code>ramfs_replace()</code> (the old pack is freed), then tells <span class="file">mouse.c</span> to re-fetch its image.</li>
 			<li><strong>Loading.</strong> <span class="file">cursor.c</span> copies the bundle out of the ramfs into its own pages after <code>ramfs_init()</code> and after every <code>ramfs_replace()</code> (the old pack is freed), then tells <span class="file">mouse.c</span> to re-fetch its image.</li>
-			<li><strong>Selection.</strong> On every <code>SYS_WM_POLL</code> the window server resolves the shape: the WM's override (<code>SYS_WM_SET_CURSOR</code>: resize arrows on the grip and while resizing, the move cross during a title-bar drag, a hand over launchers and the status area), else the cursor of the window holding the pointer capture, else the content cursor (<code>SYS_WIN_SET_CURSOR</code>) of the window under the pointer, else the arrow.</li>
+			<li><strong>Selection.</strong> On every <code>SYS_WM_POLL</code> the window server resolves the shape: the WM's override (<code>SYS_WM_SET_CURSOR</code>: resize arrows on the resize band and while resizing, the move cross during a title-bar drag, a hand over launchers and the status area), else the cursor of the window holding the pointer capture, else the content cursor (<code>SYS_WIN_SET_CURSOR</code>) of the window under the pointer, else the arrow.</li>
 			<li><strong>Clients.</strong> ngui resets the cursor to the arrow for each redrawn frame, a hovered text field asks for the I-beam, <code>ngui_cursor_rect()</code>/<code>ngui_set_cursor()</code> serve apps, and <code>ngui_end_ex()</code> sends it when it changed.  The terminal uses the I-beam; the browser maps the inherited CSS <code>cursor</code> property and, for <code>auto</code>, a hand over links, an I-beam over text fields and the progress arrow while a page loads.  The gallery's Cursors section shows every shape.</li>
 			<li><strong>Clients.</strong> ngui resets the cursor to the arrow for each redrawn frame, a hovered text field asks for the I-beam, <code>ngui_cursor_rect()</code>/<code>ngui_set_cursor()</code> serve apps, and <code>ngui_end_ex()</code> sends it when it changed.  The terminal uses the I-beam; the browser maps the inherited CSS <code>cursor</code> property and, for <code>auto</code>, a hand over links, an I-beam over text fields and the progress arrow while a page loads.  The gallery's Cursors section shows every shape.</li>
 			<li><strong>Drawing.</strong> <code>cur_x</code>/<code>cur_y</code> stay the hotspot; the image's top-left is the hotspot minus the image's hotspot and may be off-screen.  The software cursor saves up to 32&times;32 pixels under it and blends the premultiplied pixels over them.  Intel Ironlake and Radeon copy the ARGB image into their 64&times;64 planes (Intel uses CURPOS sign bits for negative positions and follows the matching primary plane's gamma enable, because the display palette is after the plane blender and a cursor-only gamma pass brightens its antialiased edges on a VBE mode whose primary bypasses the palette; Radeon uses <code>HORZ_VERT_OFF</code> plus a shifted <code>CUR_OFFSET</code>); Cirrus reduces it to its two colours and clamps the position at 0.  Animated frames advance from <code>mouse_poll_update()</code>.</li>
 			<li><strong>Drawing.</strong> <code>cur_x</code>/<code>cur_y</code> stay the hotspot; the image's top-left is the hotspot minus the image's hotspot and may be off-screen.  The software cursor saves up to 32&times;32 pixels under it and blends the premultiplied pixels over them.  Intel Ironlake and Radeon copy the ARGB image into their 64&times;64 planes (Intel uses CURPOS sign bits for negative positions and follows the matching primary plane's gamma enable, because the display palette is after the plane blender and a cursor-only gamma pass brightens its antialiased edges on a VBE mode whose primary bypasses the palette; Radeon uses <code>HORZ_VERT_OFF</code> plus a shifted <code>CUR_OFFSET</code>); Cirrus reduces it to its two colours and clamps the position at 0.  Animated frames advance from <code>mouse_poll_update()</code>.</li>
 		</ul>
 		</ul>

+ 1 - 1
docs/userland.html

@@ -231,7 +231,7 @@
 					<tr><td>Hardware settings</td><td>113–114</td><td><code>SYS_BACKLIGHT(op, value)</code> get/set/step the panel PWM, <code>SYS_CPUFREQ(mode)</code> get/set the P-state mode (control panel sliders/tiles)</td></tr>
 					<tr><td>Hardware settings</td><td>113–114</td><td><code>SYS_BACKLIGHT(op, value)</code> get/set/step the panel PWM, <code>SYS_CPUFREQ(mode)</code> get/set the P-state mode (control panel sliders/tiles)</td></tr>
 					<tr><td>Popover layout</td><td>115–116</td><td><code>SYS_WIN_RESIZE_SELF(w, h)</code> resizes the caller's canvas, <code>SYS_SCREEN_INFO(out)</code> returns the framebuffer dimensions (the control panel collapses/grows)</td></tr>
 					<tr><td>Popover layout</td><td>115–116</td><td><code>SYS_WIN_RESIZE_SELF(w, h)</code> resizes the caller's canvas, <code>SYS_SCREEN_INFO(out)</code> returns the framebuffer dimensions (the control panel collapses/grows)</td></tr>
 					<tr><td>Storage</td><td>119–120</td><td><code>SYS_BLK_READ(index, lba, buf)</code> reads one raw sector, <code>SYS_MOUNT_CTL(op, ...)</code> sets/gets a mount policy (name, read-only, no automount); used by <code>disks</code></td></tr>
 					<tr><td>Storage</td><td>119–120</td><td><code>SYS_BLK_READ(index, lba, buf)</code> reads one raw sector, <code>SYS_MOUNT_CTL(op, ...)</code> sets/gets a mount policy (name, read-only, no automount); used by <code>disks</code></td></tr>
-					<tr><td>Mouse cursor</td><td>117–118</td><td><code>SYS_WIN_SET_CURSOR(shape)</code> sets the caller's content cursor, <code>SYS_WM_SET_CURSOR(shape | -1)</code> is the WM's override for its chrome (shapes in <span class="file">cursor_abi.h</span>)</td></tr>
+					<tr><td>Mouse cursor</td><td>117–118, 121</td><td><code>SYS_WIN_SET_CURSOR(shape)</code> sets the caller's content cursor, <code>SYS_WM_SET_CURSOR(shape | -1)</code> is the WM's override for its chrome, and <code>SYS_WM_MOUSE_CAPTURE(on)</code> takes/releases the pointer while the WM drags or resizes a window (shapes in <span class="file">cursor_abi.h</span>)</td></tr>
 				</tbody>
 				</tbody>
 			</table>
 			</table>
 		</div>
 		</div>

+ 2 - 0
include/syscall.h

@@ -121,6 +121,8 @@
 				  cursor (enum cursor_shape, cursor_abi.h) */
 				  cursor (enum cursor_shape, cursor_abi.h) */
 #define SYS_WM_SET_CURSOR 118 /* (int shape | CURSOR_NO_OVERRIDE) -> 0/-1;
 #define SYS_WM_SET_CURSOR 118 /* (int shape | CURSOR_NO_OVERRIDE) -> 0/-1;
 				  WM only: cursor for its chrome/drags */
 				  WM only: cursor for its chrome/drags */
+#define SYS_WM_MOUSE_CAPTURE 121 /* (int on) -> 0/-1; WM only: own the pointer
+				    while it drags or resizes a window */
 
 
 /* Storage management (see sys_fs.c, the `disks` app).  SYS_BLK_READ reads one
 /* Storage management (see sys_fs.c, the `disks` app).  SYS_BLK_READ reads one
  * raw 512-byte sector of block device `index` (SYS_BLKDEV order) so ring 3
  * raw 512-byte sector of block device `index` (SYS_BLKDEV order) so ring 3

+ 7 - 0
include/wm_abi.h

@@ -55,6 +55,13 @@
 #define WM_BTN_END_X (WM_BTN_DISKS_X + WM_BTN_DISKS_W)
 #define WM_BTN_END_X (WM_BTN_DISKS_X + WM_BTN_DISKS_W)
 #define WM_TITLE_H 22
 #define WM_TITLE_H 22
 #define WM_CLOSE_W 16
 #define WM_CLOSE_W 16
+/* No visible frame: the title bar and content start at the window origin. */
+#define WM_BORDER_W 0
+/* Invisible resize band around a normal window: WM_RESIZE_IN px inside the
+ * outer edge and WM_RESIZE_OUT px outside it.  Presses in the inside part
+ * are not routed to the window content; the whole band starts a resize. */
+#define WM_RESIZE_IN  1
+#define WM_RESIZE_OUT 3
 
 
 /* Keep at least this much of the title bar on-screen when a window is dragged
 /* Keep at least this much of the title bar on-screen when a window is dragged
  * past an edge, so it can always be grabbed and brought back. */
  * past an edge, so it can always be grabbed and brought back. */

+ 5 - 0
kernel/sys_wm.c

@@ -295,3 +295,8 @@ int sys_wm_set_cursor(int shape)
 {
 {
 	return wm_server_set_cursor(shape);
 	return wm_server_set_cursor(shape);
 }
 }
+
+int sys_wm_mouse_capture(int on)
+{
+	return wm_server_set_pointer_capture(on);
+}

+ 1 - 0
kernel/sys_wm.h

@@ -31,6 +31,7 @@ int sys_win_key_wait(unsigned int timeout_ms);
 int sys_win_resize_self(unsigned int w, unsigned int h);
 int sys_win_resize_self(unsigned int w, unsigned int h);
 int sys_win_set_cursor(int shape);
 int sys_win_set_cursor(int shape);
 int sys_wm_set_cursor(int shape);
 int sys_wm_set_cursor(int shape);
+int sys_wm_mouse_capture(int on);
 
 
 /* Set once the first application window (the boot terminal) has presented
 /* Set once the first application window (the boot terminal) has presented
  * a frame; kernel_main waits for it before the USB bring-up. */
  * a frame; kernel_main waits for it before the USB bring-up. */

+ 2 - 0
kernel/syscall.c

@@ -358,6 +358,8 @@ static int syscall_dispatch_locked(int num, int a1, int a2, int a3)
 		return sys_win_set_cursor((int)a1);
 		return sys_win_set_cursor((int)a1);
 	case SYS_WM_SET_CURSOR:
 	case SYS_WM_SET_CURSOR:
 		return sys_wm_set_cursor((int)a1);
 		return sys_wm_set_cursor((int)a1);
+	case SYS_WM_MOUSE_CAPTURE:
+		return sys_wm_mouse_capture((int)a1);
 	case SYS_SCREEN_INFO: {
 	case SYS_SCREEN_INFO: {
 		struct sys_screen *out = (struct sys_screen *)a1;
 		struct sys_screen *out = (struct sys_screen *)a1;
 
 

+ 129 - 26
kernel/wm.c

@@ -34,9 +34,12 @@ static unsigned int wm_next_content_gen(void)
  * called by the ring-3 WM every loop, so it is the cheapest place to turn the
  * called by the ring-3 WM every loop, so it is the cheapest place to turn the
  * sampled mouse state into press/release edges. */
  * sampled mouse state into press/release edges. */
 static unsigned int wm_ptr_buttons;
 static unsigned int wm_ptr_buttons;
-/* SYS_WM_SET_CURSOR: the WM's shape for its own chrome (resize grip,
+/* SYS_WM_SET_CURSOR: the WM's shape for its own chrome (resize border,
  * title-bar drag, launchers), or CURSOR_NO_OVERRIDE. */
  * title-bar drag, launchers), or CURSOR_NO_OVERRIDE. */
 static int wm_cursor_override = CURSOR_NO_OVERRIDE;
 static int wm_cursor_override = CURSOR_NO_OVERRIDE;
+/* The WM owns the pointer while it drags or resizes a window: no pointer
+ * events reach canvas clients and SYS_WIN_POINTER reports no buttons. */
+static int wm_pointer_owned;
 static int wm_ptr_capture = -1;
 static int wm_ptr_capture = -1;
 
 
 /* Ring-3 WM lifecycle.  The kernel only admits the process it spawned; a
 /* Ring-3 WM lifecycle.  The kernel only admits the process it spawned; a
@@ -75,17 +78,19 @@ static void wm_close_window(unsigned int idx);
 static void wm_clamp_window(struct window *w, int nx, int ny);
 static void wm_clamp_window(struct window *w, int nx, int ny);
 static void wm_server_unmap_index(unsigned int idx);
 static void wm_server_unmap_index(unsigned int idx);
 static int wm_window_at(int px, int py);
 static int wm_window_at(int px, int py);
+static int wm_resize_target(int px, int py, int *edges);
 
 
-/* Content origin: a normal window has a 1 px border and title bar, a popover
- * is a flat chromeless panel whose canvas fills the window. */
+/* Content origin: a normal window has no visible frame, so its content
+ * starts one title bar below the window origin; a popover is a flat
+ * chromeless panel whose canvas fills the window. */
 static int win_content_x(const struct window *w)
 static int win_content_x(const struct window *w)
 {
 {
-	return w->x + (w->popover ? 0 : 1);
+	return w->x + (w->popover ? 0 : WM_BORDER_W);
 }
 }
 
 
 static int win_content_y(const struct window *w)
 static int win_content_y(const struct window *w)
 {
 {
-	return w->y + (w->popover ? 0 : 1 + WM_TITLE_H);
+	return w->y + (w->popover ? 0 : WM_BORDER_W + WM_TITLE_H);
 }
 }
 
 
 void wm_request_redraw(void)
 void wm_request_redraw(void)
@@ -208,8 +213,8 @@ int wm_server_win_create(const struct win_create *in, struct win_info *out)
 	unsigned int i, j, pages, cw, ch, base;
 	unsigned int i, j, pages, cw, ch, base;
 	unsigned int flags;
 	unsigned int flags;
 	int popover = (in && (in->flags & WIN_F_POPOVER)) ? 1 : 0;
 	int popover = (in && (in->flags & WIN_F_POPOVER)) ? 1 : 0;
-	unsigned int pad = popover ? 0 : 1;
-	unsigned int top = popover ? 0 : WM_TITLE_H + 1;
+	unsigned int pad = popover ? 0 : WM_BORDER_W;
+	unsigned int top = popover ? 0 : WM_TITLE_H + WM_BORDER_W;
 
 
 	if (!in || !out)
 	if (!in || !out)
 		return -1;
 		return -1;
@@ -486,11 +491,17 @@ int wm_server_win_pointer(struct win_pointer *out)
 	cy = (int)my - win_content_y(w);
 	cy = (int)my - win_content_y(w);
 	out->x = cx;
 	out->x = cx;
 	out->y = cy;
 	out->y = cy;
-	out->buttons = mouse_buttons_state();
-	out->inside = (cx >= 0 && cy >= 0 &&
-		       cx < (int)w->canvas.w && cy < (int)w->canvas.h &&
-		       (w->focused || wm_window_at((int)mx, (int)my) == idx)) ?
-		      1 : 0;
+	if (wm_pointer_owned) {
+		out->buttons = 0;
+		out->inside = 0;
+	} else {
+		out->buttons = mouse_buttons_state();
+		out->inside = (cx >= 0 && cy >= 0 &&
+			       cx < (int)w->canvas.w && cy < (int)w->canvas.h &&
+			       (w->focused ||
+				wm_window_at((int)mx, (int)my) == idx)) ?
+			       1 : 0;
+	}
 	out->focused = w->focused ? 1 : 0;
 	out->focused = w->focused ? 1 : 0;
 	return 0;
 	return 0;
 }
 }
@@ -536,8 +547,8 @@ static int wm_resize_window(unsigned int index, unsigned int cw, unsigned int ch
 	w = &windows[index];
 	w = &windows[index];
 	if (!w->canvas_window || !process_is_alive(w->pid))
 	if (!w->canvas_window || !process_is_alive(w->pid))
 		return -1;
 		return -1;
-	pad = w->popover ? 0 : 1;
-	top = w->popover ? 0 : WM_TITLE_H + 1;
+	pad = w->popover ? 0 : WM_BORDER_W;
+	top = w->popover ? 0 : WM_TITLE_H + WM_BORDER_W;
 	if (cw < 160 || ch < 80 || cw > WM_WIN_MAX_W || ch > WM_WIN_MAX_H)
 	if (cw < 160 || ch < 80 || cw > WM_WIN_MAX_W || ch > WM_WIN_MAX_H)
 		return -1;
 		return -1;
 	if (cw * ch * 2 > WM_CONTENT_SLOT || cw * ch * 2 > USER_WIN_RANGE)
 	if (cw * ch * 2 > WM_CONTENT_SLOT || cw * ch * 2 > USER_WIN_RANGE)
@@ -617,24 +628,55 @@ int wm_server_set_cursor(int shape)
 	return 0;
 	return 0;
 }
 }
 
 
+/* Take/release the pointer for a WM drag or resize.  While owned, a held
+ * button is invisible to the canvas clients, so a press that started on a
+ * window edge cannot also drag a scrollbar inside the window. */
+int wm_server_set_pointer_capture(int on)
+{
+	if (!wm_server_is_wm())
+		return -1;
+	wm_pointer_owned = on ? 1 : 0;
+	if (on) {
+		wm_ptr_capture = -1;
+		wm_ptr_buttons = mouse_buttons_state();
+	}
+	return 0;
+}
+
+/* Invisible resize border inside a normal window's outer edge.  Presses here
+ * belong to the ring-3 WM's resize path, not to the content client.  Only the
+ * WM_RESIZE_IN part of the band lies inside the window; the rest is outside
+ * the rectangle and never reaches the content anyway. */
+static int win_in_resize_border(const struct window *w, unsigned int mx,
+				unsigned int my)
+{
+	if (w->popover)
+		return 0;
+	return (int)mx < w->x + WM_RESIZE_IN ||
+	       (int)mx >= w->x + (int)w->pw - WM_RESIZE_IN ||
+	       (int)my < w->y + WM_RESIZE_IN ||
+	       (int)my >= w->y + (int)w->ph - WM_RESIZE_IN;
+}
+
 /* The shape for the pointer at (mx,my): the WM's override, else the
 /* The shape for the pointer at (mx,my): the WM's override, else the
  * content cursor of the window that holds the pointer capture (a drag that
  * content cursor of the window that holds the pointer capture (a drag that
  * started in the content keeps its cursor), else of the window whose
  * started in the content keeps its cursor), else of the window whose
- * content is under the pointer (not on a normal window's resize grip),
+ * content is under the pointer (not on a normal window's resize border),
  * else the default arrow (title bars, the desktop, the top bar). */
  * else the default arrow (title bars, the desktop, the top bar). */
 static int wm_cursor_at(unsigned int mx, unsigned int my)
 static int wm_cursor_at(unsigned int mx, unsigned int my)
 {
 {
 	int idx;
 	int idx;
 	struct window *w;
 	struct window *w;
 	int cx, cy;
 	int cx, cy;
+	int edges = 0;
 
 
 	if (wm_cursor_override != CURSOR_NO_OVERRIDE)
 	if (wm_cursor_override != CURSOR_NO_OVERRIDE)
 		return wm_cursor_override;
 		return wm_cursor_override;
 	if (wm_ptr_capture >= 0 && windows[wm_ptr_capture].used &&
 	if (wm_ptr_capture >= 0 && windows[wm_ptr_capture].used &&
 	    windows[wm_ptr_capture].canvas_window)
 	    windows[wm_ptr_capture].canvas_window)
 		return windows[wm_ptr_capture].cursor;
 		return windows[wm_ptr_capture].cursor;
-	idx = wm_window_at((int)mx, (int)my);
-	if (idx < 0)
+	idx = wm_resize_target((int)mx, (int)my, &edges);
+	if (idx < 0 || edges)
 		return CURSOR_DEFAULT;
 		return CURSOR_DEFAULT;
 	w = &windows[idx];
 	w = &windows[idx];
 	if (!w->canvas_window)
 	if (!w->canvas_window)
@@ -644,8 +686,7 @@ static int wm_cursor_at(unsigned int mx, unsigned int my)
 	if (cx < 0 || cy < 0 || cx >= (int)w->canvas.w ||
 	if (cx < 0 || cy < 0 || cx >= (int)w->canvas.w ||
 	    cy >= (int)w->canvas.h)
 	    cy >= (int)w->canvas.h)
 		return CURSOR_DEFAULT;
 		return CURSOR_DEFAULT;
-	if (!w->popover && (int)mx >= w->x + (int)w->pw - 16 &&
-	    (int)my >= w->y + (int)w->ph - 16)
+	if (win_in_resize_border(w, mx, my))
 		return CURSOR_DEFAULT;
 		return CURSOR_DEFAULT;
 	return w->cursor;
 	return w->cursor;
 }
 }
@@ -962,10 +1003,16 @@ static void wm_route_pointer(unsigned int mx, unsigned int my,
 	unsigned int press = buttons & ~wm_ptr_buttons;
 	unsigned int press = buttons & ~wm_ptr_buttons;
 	unsigned int release = wm_ptr_buttons & ~buttons;
 	unsigned int release = wm_ptr_buttons & ~buttons;
 
 
+	if (wm_pointer_owned) {
+		wm_ptr_buttons = buttons;
+		return;
+	}
+
 	if (press & 1) {
 	if (press & 1) {
-		int idx = wm_window_at((int)mx, (int)my);
+		int edges = 0;
+		int idx = wm_resize_target((int)mx, (int)my, &edges);
 
 
-		if (idx >= 0) {
+		if (idx >= 0 && !edges) {
 			struct window *w = &windows[idx];
 			struct window *w = &windows[idx];
 			int cx = (int)mx - win_content_x(w);
 			int cx = (int)mx - win_content_x(w);
 			int cy = (int)my - win_content_y(w);
 			int cy = (int)my - win_content_y(w);
@@ -973,8 +1020,7 @@ static void wm_route_pointer(unsigned int mx, unsigned int my,
 			if (w->canvas_window && cx >= 0 && cy >= 0 &&
 			if (w->canvas_window && cx >= 0 && cy >= 0 &&
 			    cx < (int)w->canvas.w && cy < (int)w->canvas.h &&
 			    cx < (int)w->canvas.w && cy < (int)w->canvas.h &&
 			    (w->popover ||
 			    (w->popover ||
-			     !((int)mx >= w->x + (int)w->pw - 16 &&
-			       (int)my >= w->y + (int)w->ph - 16))) {
+			     !win_in_resize_border(w, mx, my))) {
 				wm_queue_ptr_event(w, WIN_EV_POINTER_DOWN,
 				wm_queue_ptr_event(w, WIN_EV_POINTER_DOWN,
 						   cx, cy, buttons);
 						   cx, cy, buttons);
 				wm_ptr_capture = idx;
 				wm_ptr_capture = idx;
@@ -1001,12 +1047,15 @@ static void wm_route_pointer(unsigned int mx, unsigned int my,
 static void wm_route_scroll(unsigned int mx, unsigned int my)
 static void wm_route_scroll(unsigned int mx, unsigned int my)
 {
 {
 	int sdx, sdy;
 	int sdx, sdy;
-	int idx = wm_window_at((int)mx, (int)my);
+	int edges = 0;
+	int idx = wm_resize_target((int)mx, (int)my, &edges);
 	struct window *w;
 	struct window *w;
 	int cx, cy;
 	int cx, cy;
 
 
 	mouse_scroll_take(&sdx, &sdy);
 	mouse_scroll_take(&sdx, &sdy);
-	if ((!sdx && !sdy) || idx < 0)
+	if (wm_pointer_owned)
+		return;
+	if (edges || (!sdx && !sdy) || idx < 0)
 		return;
 		return;
 	w = &windows[idx];
 	w = &windows[idx];
 	if (!w->canvas_window)
 	if (!w->canvas_window)
@@ -1134,6 +1183,7 @@ static void wm_server_wm_died(void)
 	wm_ptr_buttons = 0;
 	wm_ptr_buttons = 0;
 	wm_ptr_capture = -1;
 	wm_ptr_capture = -1;
 	wm_cursor_override = CURSOR_NO_OVERRIDE;
 	wm_cursor_override = CURSOR_NO_OVERRIDE;
+	wm_pointer_owned = 0;
 	mouse_set_shape(CURSOR_DEFAULT);
 	mouse_set_shape(CURSOR_DEFAULT);
 	for (i = 0; i < MAX_WINDOWS; i++) {
 	for (i = 0; i < MAX_WINDOWS; i++) {
 		windows[i].mapped_kind = WM_KIND_NONE;
 		windows[i].mapped_kind = WM_KIND_NONE;
@@ -1344,6 +1394,59 @@ static int wm_window_at(int px, int py)
 	return -1;
 	return -1;
 }
 }
 
 
+/* Resize band of a normal window: WM_RESIZE_IN px inside and WM_RESIZE_OUT
+ * px outside every edge.  Mirrors the ring-3 WM's resize_edges_at(). */
+static int wm_in_resize_band(const struct window *w, int mx, int my)
+{
+	int l = w->x, t = w->y;
+	int r = l + (int)w->pw, b = t + (int)w->ph;
+	int near_x, near_y;
+
+	if (w->popover || my < WM_BAR_H)
+		return 0;
+	near_x = mx >= l - WM_RESIZE_OUT && mx < r + WM_RESIZE_OUT;
+	near_y = my >= t - WM_RESIZE_OUT && my < b + WM_RESIZE_OUT;
+	return (near_y && mx >= l - WM_RESIZE_OUT && mx < l + WM_RESIZE_IN) ||
+	       (near_y && mx >= r - WM_RESIZE_IN && mx < r + WM_RESIZE_OUT) ||
+	       (near_x && my >= t - WM_RESIZE_OUT && my < t + WM_RESIZE_IN) ||
+	       (near_x && my >= b - WM_RESIZE_IN && my < b + WM_RESIZE_OUT);
+}
+
+static int wm_hit_one(struct window *w, int mx, int my, int *edges)
+{
+	*edges = 0;
+	if (!w->used)
+		return 0;
+	if (wm_in_resize_band(w, mx, my)) {
+		*edges = 1;
+		return 1;
+	}
+	return wm_point_in_rect(mx, my, w->x, w->y, w->pw, w->ph);
+}
+
+/* Topmost window for a pointer position, in the WM's painting order.  A
+ * resize band wins over a plain rectangle hit, so a front window's outside
+ * band is not stolen by a window behind it.  *edges is 1 for a band hit. */
+static int wm_resize_target(int px, int py, int *edges)
+{
+	unsigned int i;
+
+	*edges = 0;
+	for (i = 0; i < MAX_WINDOWS; i++)
+		if (windows[i].popover &&
+		    wm_hit_one(&windows[i], px, py, edges))
+			return (int)i;
+	for (i = 0; i < MAX_WINDOWS; i++)
+		if (windows[i].focused && !windows[i].popover &&
+		    wm_hit_one(&windows[i], px, py, edges))
+			return (int)i;
+	for (i = MAX_WINDOWS; i-- > 0;)
+		if (!windows[i].focused && !windows[i].popover &&
+		    wm_hit_one(&windows[i], px, py, edges))
+			return (int)i;
+	return -1;
+}
+
 static void wm_clamp_window(struct window *w, int nx, int ny)
 static void wm_clamp_window(struct window *w, int nx, int ny)
 {
 {
 	int fbw = (int)vga_fb_width();
 	int fbw = (int)vga_fb_width();
@@ -1354,7 +1457,7 @@ static void wm_clamp_window(struct window *w, int nx, int ny)
 	int minx = WM_DRAG_VISIBLE - (int)w->pw;
 	int minx = WM_DRAG_VISIBLE - (int)w->pw;
 	int maxx = fbw - WM_DRAG_VISIBLE;
 	int maxx = fbw - WM_DRAG_VISIBLE;
 	int miny = WM_BAR_H;
 	int miny = WM_BAR_H;
-	int maxy = fbh - WM_TITLE_H;
+	int maxy = fbh - WM_BORDER_W - WM_TITLE_H;
 
 
 	if (w->popover) {
 	if (w->popover) {
 		/* A popover is an overlay panel: keep all of it on screen,
 		/* A popover is an overlay panel: keep all of it on screen,

+ 1 - 0
kernel/wm.h

@@ -92,6 +92,7 @@ int  wm_server_win_scroll(struct win_scroll *out);
 int  wm_server_win_resize_self(unsigned int cw, unsigned int ch);
 int  wm_server_win_resize_self(unsigned int cw, unsigned int ch);
 int  wm_server_win_set_cursor(int shape);
 int  wm_server_win_set_cursor(int shape);
 int  wm_server_set_cursor(int shape);
 int  wm_server_set_cursor(int shape);
+int  wm_server_set_pointer_capture(int on);
 void wm_server_boot_ready(void);
 void wm_server_boot_ready(void);
 struct window *wm_focused_window(void);
 struct window *wm_focused_window(void);
 int  wm_is_focused(const struct window *w);
 int  wm_is_focused(const struct window *w);

+ 202 - 96
user/apps/wm/ucmd_wm.c

@@ -8,8 +8,9 @@
 #include "version.h"
 #include "version.h"
 
 
 #define COL_BG        0x202030u
 #define COL_BG        0x202030u
-#define COL_BORDER    0xC8C8C8u
-#define COL_TITLE_ON  0x465A96u
+/* The active title bar uses the toolkit highlight (ngui_theme_dark.selection);
+ * the inactive one stays neutral.  Windows have no visible frame. */
+#define COL_TITLE_ON  0x365CA6u
 #define COL_TITLE_OFF 0x646464u
 #define COL_TITLE_OFF 0x646464u
 #define COL_CLOSE     0xC64040u
 #define COL_CLOSE     0xC64040u
 #define COL_WHITE     0xFFFFFFu
 #define COL_WHITE     0xFFFFFFu
@@ -57,6 +58,8 @@ static int drag_dx, drag_dy;
 static int drag_mx, drag_my;       /* press position, for restore-on-drag */
 static int drag_mx, drag_my;       /* press position, for restore-on-drag */
 static int drag_moved;             /* the pointer passed WM_DRAG_START */
 static int drag_moved;             /* the pointer passed WM_DRAG_START */
 static int resize_id = -1;
 static int resize_id = -1;
+static int resize_edges;           /* WM_EDGE_* bits captured at the press */
+static int resize_ox, resize_oy;
 static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
 static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
 static unsigned int last_title_ms;
 static unsigned int last_title_ms;
 static int last_title_id = -1, last_title_x, last_title_y;
 static int last_title_id = -1, last_title_x, last_title_y;
@@ -621,10 +624,10 @@ static void draw_window_clip(unsigned int idx, int cx, int cy,
 	struct uwin *u = &wins[idx];
 	struct uwin *u = &wins[idx];
 	struct wm_window_info *w = &u->info;
 	struct wm_window_info *w = &u->info;
 	int x = w->x, y = w->y;
 	int x = w->x, y = w->y;
-	unsigned int pw = w->w, ph = w->h;
-	int bx = x + (int)pw - 1 - WM_CLOSE_W;
-	int cony = y + 1 + WM_TITLE_H;
-	int content_x = x + 1;
+	unsigned int pw = w->w;
+	int bx = x + (int)pw - WM_BORDER_W - WM_CLOSE_W;
+	int cony = y + WM_BORDER_W + WM_TITLE_H;
+	int content_x = x + WM_BORDER_W;
 	unsigned int content_w = w->content_w;
 	unsigned int content_w = w->content_w;
 	unsigned int content_h = w->content_h;
 	unsigned int content_h = w->content_h;
 
 
@@ -633,15 +636,13 @@ static void draw_window_clip(unsigned int idx, int cx, int cy,
 		return;
 		return;
 	}
 	}
 
 
-	/* Border, title bar, close button. */
-	fill_clip(x, y, pw, ph, COL_BORDER, cx, cy, cw, ch);
-	fill_clip(x + 1, y + 1, pw - 2, WM_TITLE_H,
+	/* Title bar and close button; windows have no visible frame. */
+	fill_clip(x, y, pw, WM_TITLE_H,
 		  w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
 		  w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
-	usurf_text(&screen, x + 4, y + 3, w->title, COL_WHITE,
+	usurf_text(&screen, x + 3, y + 2, w->title, COL_WHITE,
 		   cx, cy, cw, ch);
 		   cx, cy, cw, ch);
-	fill_clip(bx, y + 1, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE,
-		  cx, cy, cw, ch);
-	usurf_text(&screen, bx + 4, y + 3, "x", COL_WHITE,
+	fill_clip(bx, y, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE, cx, cy, cw, ch);
+	usurf_text(&screen, bx + 4, y + 2, "x", COL_WHITE,
 		   cx, cy, cw, ch);
 		   cx, cy, cw, ch);
 
 
 	/* Content: every window presents an RGB565 canvas (the terminal
 	/* Content: every window presents an RGB565 canvas (the terminal
@@ -663,20 +664,6 @@ static void draw_window_clip(unsigned int idx, int cx, int cy,
 		fill_clip(content_x, cony, content_w, content_h,
 		fill_clip(content_x, cony, content_w, content_h,
 			  0x000000u, cx, cy, cw, ch);
 			  0x000000u, cx, cy, cw, ch);
 	}
 	}
-
-	/* Resize grip (bottom-right corner). */
-	if (w->focused) {
-		unsigned int i;
-
-		for (i = 0; i < 10; i++) {
-			fill_clip(x + (int)pw - 3 - (int)i,
-				  y + (int)ph - 4, 1, 1, 0xD2D2D2u,
-				  cx, cy, cw, ch);
-			fill_clip(x + (int)pw - 4,
-				  y + (int)ph - 3 - (int)i, 1, 1, 0xD2D2D2u,
-				  cx, cy, cw, ch);
-		}
-	}
 }
 }
 
 
 /* Front-to-back window order: popovers float above everything, then the
 /* Front-to-back window order: popovers float above everything, then the
@@ -860,34 +847,95 @@ static void draw_rect(int x, int y, unsigned int w, unsigned int h)
 		draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
 		draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
 }
 }
 
 
-static int window_at(int mx, int my)
+/* Invisible resize border: edge bits for a point in the band around the
+ * window's outer edge (WM_RESIZE_IN px inside and WM_RESIZE_OUT px outside).
+ * Popovers have no chrome and never resize. */
+#define WM_EDGE_LEFT   1
+#define WM_EDGE_RIGHT  2
+#define WM_EDGE_TOP    4
+#define WM_EDGE_BOTTOM 8
+
+static int resize_edges_at(const struct uwin *u, int mx, int my)
+{
+	const struct wm_window_info *w = &u->info;
+	int l = w->x, t = w->y;
+	int r = l + (int)w->w, b = t + (int)w->h;
+	int near_x, near_y, e = 0;
+
+	if (w->popover)
+		return 0;
+	near_x = mx >= l - WM_RESIZE_OUT && mx < r + WM_RESIZE_OUT;
+	near_y = my >= t - WM_RESIZE_OUT && my < b + WM_RESIZE_OUT;
+	if (near_y && mx >= l - WM_RESIZE_OUT && mx < l + WM_RESIZE_IN)
+		e |= WM_EDGE_LEFT;
+	if (near_y && mx >= r - WM_RESIZE_IN && mx < r + WM_RESIZE_OUT)
+		e |= WM_EDGE_RIGHT;
+	if (near_x && my >= t - WM_RESIZE_OUT && my < t + WM_RESIZE_IN)
+		e |= WM_EDGE_TOP;
+	if (near_x && my >= b - WM_RESIZE_IN && my < b + WM_RESIZE_OUT)
+		e |= WM_EDGE_BOTTOM;
+	return e;
+}
+
+/* Topmost window at (mx,my): its resize band when the point is inside one,
+ * otherwise the window rectangle.  *edges is 0 for a plain rectangle hit.
+ * Bands are not considered inside the top bar, which the WM owns. */
+static int resize_hit(int mx, int my, int *edges)
 {
 {
 	int order[WM_MAX_WINDOWS];
 	int order[WM_MAX_WINDOWS];
 	unsigned int n = build_order(order), i;
 	unsigned int n = build_order(order), i;
 
 
+	*edges = 0;
 	for (i = 0; i < n; i++) {
 	for (i = 0; i < n; i++) {
-		struct wm_window_info *w = &wins[order[i]].info;
+		struct uwin *u = &wins[order[i]];
+		int e = 0;
 
 
-		if (!w->popover)
+		if (!u->info.used)
 			continue;
 			continue;
-		if (mx >= w->x && my >= w->y &&
-		    mx < w->x + (int)w->w &&
-		    my < w->y + (int)w->h)
+		if (my >= WM_BAR_H)
+			e = resize_edges_at(u, mx, my);
+		if (e) {
+			*edges = e;
 			return order[i];
 			return order[i];
-	}
-	for (i = 0; i < n; i++) {
-		struct wm_window_info *w = &wins[order[i]].info;
-
-		if (w->popover)
-			continue;
-		if (mx >= w->x && my >= w->y &&
-		    mx < w->x + (int)w->w &&
-		    my < w->y + (int)w->h)
+		}
+		if (mx >= u->info.x && my >= u->info.y &&
+		    mx < u->info.x + (int)u->info.w &&
+		    my < u->info.y + (int)u->info.h)
 			return order[i];
 			return order[i];
 	}
 	}
 	return -1;
 	return -1;
 }
 }
 
 
+static int resize_cursor(int edges)
+{
+	switch (edges) {
+	case WM_EDGE_LEFT:
+	case WM_EDGE_RIGHT:
+		return CURSOR_EW_RESIZE;
+	case WM_EDGE_TOP:
+	case WM_EDGE_BOTTOM:
+		return CURSOR_NS_RESIZE;
+	case WM_EDGE_LEFT | WM_EDGE_TOP:
+	case WM_EDGE_RIGHT | WM_EDGE_BOTTOM:
+		return CURSOR_NWSE_RESIZE;
+	case WM_EDGE_RIGHT | WM_EDGE_TOP:
+	case WM_EDGE_LEFT | WM_EDGE_BOTTOM:
+		return CURSOR_NESW_RESIZE;
+	default:
+		return CURSOR_DEFAULT;
+	}
+}
+
+/* Close button hit area (the top-right WM_CLOSE_W columns of the title bar). */
+static int close_hit(const struct wm_window_info *w, int mx, int my)
+{
+	if (w->popover)
+		return 0;
+	return mx >= w->x + (int)w->w - WM_CLOSE_W &&
+	       mx < w->x + (int)w->w &&
+	       my >= w->y && my < w->y + WM_TITLE_H;
+}
+
 static void clamp_geom(struct uwin *u, int *x, int *y)
 static void clamp_geom(struct uwin *u, int *x, int *y)
 {
 {
 	/* Match wm_clamp_window(): allow the window to hang off the left,
 	/* Match wm_clamp_window(): allow the window to hang off the left,
@@ -896,7 +944,7 @@ static void clamp_geom(struct uwin *u, int *x, int *y)
 	int minx = WM_DRAG_VISIBLE - (int)u->info.w;
 	int minx = WM_DRAG_VISIBLE - (int)u->info.w;
 	int maxx = (int)fbw - WM_DRAG_VISIBLE;
 	int maxx = (int)fbw - WM_DRAG_VISIBLE;
 	int miny = WM_BAR_H;
 	int miny = WM_BAR_H;
-	int maxy = (int)fbh - WM_TITLE_H;
+	int maxy = (int)fbh - WM_BORDER_W - WM_TITLE_H;
 
 
 	if (minx > maxx)
 	if (minx > maxx)
 		minx = maxx = 0;
 		minx = maxx = 0;
@@ -1120,7 +1168,8 @@ static int set_window_geometry(struct uwin *u, unsigned int idx,
 	unsigned int ow = u->info.w, oh = u->info.h;
 	unsigned int ow = u->info.w, oh = u->info.h;
 
 
 	if ((int)nw != (int)ow || (int)nh != (int)oh) {
 	if ((int)nw != (int)ow || (int)nh != (int)oh) {
-		if (u_wm_resize(idx, nw - 2, nh - WM_TITLE_H - 2) != 0)
+		if (u_wm_resize(idx, nw - 2 * WM_BORDER_W,
+				nh - WM_TITLE_H - 2 * WM_BORDER_W) != 0)
 			return -1;
 			return -1;
 	}
 	}
 	if (nx != ox || ny != oy)
 	if (nx != ox || ny != oy)
@@ -1572,8 +1621,8 @@ static void do_update(struct wm_status *st)
 			continue;
 			continue;
 		if (!w->dmg_w || !w->dmg_h)
 		if (!w->dmg_w || !w->dmg_h)
 			continue;
 			continue;
-		rx = w->x + (w->popover ? 0 : 1) + (int)w->dmg_x;
-		ry = w->y + (w->popover ? 0 : 1 + WM_TITLE_H) +
+		rx = w->x + (w->popover ? 0 : WM_BORDER_W) + (int)w->dmg_x;
+		ry = w->y + (w->popover ? 0 : WM_BORDER_W + WM_TITLE_H) +
 		     (int)w->dmg_y;
 		     (int)w->dmg_y;
 		rw = w->dmg_w;
 		rw = w->dmg_w;
 		rh = w->dmg_h;
 		rh = w->dmg_h;
@@ -1750,17 +1799,100 @@ static int bar_launcher_hit(unsigned int mx, unsigned int my)
 	return 0;
 	return 0;
 }
 }
 
 
-/* The WM's cursor override (SYS_WM_SET_CURSOR): resize arrows on the grip
- * and while resizing, the move cross while a title-bar drag is under way,
- * a hand over the launchers and the status area.  Everywhere else the
- * kernel picks the window's own content cursor (or the arrow). */
+/* Apply one edge-drag step.  The grabbed edges follow the pointer while the
+ * opposite edges stay put; the work area clamps the active edges. */
+static void resize_window_drag(struct uwin *u, unsigned int idx,
+			       unsigned int mx, unsigned int my)
+{
+	int dx = (int)mx - (int)resize_mx;
+	int dy = (int)my - (int)resize_my;
+	int left = resize_ox;
+	int right = resize_ox + (int)resize_ow;
+	int top = resize_oy;
+	int bottom = resize_oy + (int)resize_oh;
+	int nx, ny, nw, nh;
+
+	if (resize_edges & WM_EDGE_LEFT) {
+		left = resize_ox + dx;
+		if (left < 0)
+			left = 0;
+	} else if (resize_edges & WM_EDGE_RIGHT) {
+		right = resize_ox + (int)resize_ow + dx;
+		if (right > (int)fbw)
+			right = (int)fbw;
+	}
+	if (resize_edges & WM_EDGE_TOP) {
+		top = resize_oy + dy;
+		if (top < WM_BAR_H)
+			top = WM_BAR_H;
+	} else if (resize_edges & WM_EDGE_BOTTOM) {
+		bottom = resize_oy + (int)resize_oh + dy;
+		if (bottom > (int)fbh)
+			bottom = (int)fbh;
+	}
+	if ((resize_edges & (WM_EDGE_LEFT | WM_EDGE_RIGHT)) &&
+	    right - left < 200) {
+		if (resize_edges & WM_EDGE_LEFT)
+			left = right - 200;
+		else
+			right = left + 200;
+	}
+	if ((resize_edges & (WM_EDGE_TOP | WM_EDGE_BOTTOM)) &&
+	    bottom - top < 140) {
+		if (resize_edges & WM_EDGE_TOP)
+			top = bottom - 140;
+		else
+			bottom = top + 140;
+	}
+	if (left < 0)
+		left = 0;
+	if (top < WM_BAR_H)
+		top = WM_BAR_H;
+	nx = left;
+	ny = top;
+	nw = right - left;
+	nh = bottom - top;
+	if (nw > (int)fbw)
+		nw = (int)fbw;
+	if (nh > (int)fbh - WM_BAR_H)
+		nh = (int)fbh - WM_BAR_H;
+	if (nx == u->info.x && ny == u->info.y &&
+	    nw == (int)u->info.w && nh == (int)u->info.h)
+		return;
+	if (set_window_geometry(u, idx, nx, ny, (unsigned int)nw,
+				(unsigned int)nh) == 0)
+		u->state = WIN_NORMAL;
+}
+
+/* Capture the pointer for an edge resize that started at (mx,my). */
+static void begin_resize(unsigned int idx, int edges, unsigned int mx,
+			 unsigned int my)
+{
+	struct uwin *u = &wins[idx];
+
+	resize_id = (int)idx;
+	resize_edges = edges;
+	resize_mx = mx;
+	resize_my = my;
+	resize_ox = u->info.x;
+	resize_oy = u->info.y;
+	resize_ow = u->info.w;
+	resize_oh = u->info.h;
+	u_wm_mouse_capture(1);
+}
+
+/* The WM's cursor override (SYS_WM_SET_CURSOR): resize arrows on the
+ * invisible resize border and while resizing, the move cross while a
+ * title-bar drag is under way, a hand over the launchers and the status
+ * area.  Everywhere else the kernel picks the window's own content cursor
+ * (or the arrow). */
 static void update_cursor(unsigned int mx, unsigned int my)
 static void update_cursor(unsigned int mx, unsigned int my)
 {
 {
 	static int current = CURSOR_NO_OVERRIDE;
 	static int current = CURSOR_NO_OVERRIDE;
 	int want = CURSOR_NO_OVERRIDE;
 	int want = CURSOR_NO_OVERRIDE;
 
 
 	if (resize_id >= 0) {
 	if (resize_id >= 0) {
-		want = CURSOR_NWSE_RESIZE;
+		want = resize_cursor(resize_edges);
 	} else if (drag_id >= 0) {
 	} else if (drag_id >= 0) {
 		if (drag_moved)
 		if (drag_moved)
 			want = CURSOR_MOVE;
 			want = CURSOR_MOVE;
@@ -1768,12 +1900,12 @@ static void update_cursor(unsigned int mx, unsigned int my)
 		if (bar_launcher_hit(mx, my) || bar_status_hit(mx, my))
 		if (bar_launcher_hit(mx, my) || bar_status_hit(mx, my))
 			want = CURSOR_POINTER;
 			want = CURSOR_POINTER;
 	} else {
 	} else {
-		int idx = window_at((int)mx, (int)my);
+		int edges = 0;
+		int idx = resize_hit((int)mx, (int)my, &edges);
 
 
-		if (idx >= 0 && !wins[idx].info.popover &&
-		    (int)mx >= wins[idx].info.x + (int)wins[idx].info.w - 16 &&
-		    (int)my >= wins[idx].info.y + (int)wins[idx].info.h - 16)
-			want = CURSOR_NWSE_RESIZE;
+		if (idx >= 0 && edges &&
+		    !close_hit(&wins[idx].info, (int)mx, (int)my))
+			want = resize_cursor(edges);
 	}
 	}
 	if (want != current && u_wm_set_cursor(want) == 0)
 	if (want != current && u_wm_set_cursor(want) == 0)
 		current = want;
 		current = want;
@@ -1843,32 +1975,24 @@ static void handle_mouse(struct wm_status *st)
 			   my < WM_BTN_DISKS_Y + WM_BTN_DISKS_H) {
 			   my < WM_BTN_DISKS_Y + WM_BTN_DISKS_H) {
 			spawn_app_checked("/ramfs/bin/disks");
 			spawn_app_checked("/ramfs/bin/disks");
 		} else {
 		} else {
-			int idx = window_at((int)mx, (int)my);
+			int edges = 0;
+			int idx = resize_hit((int)mx, (int)my, &edges);
 
 
 			if (idx >= 0) {
 			if (idx >= 0) {
 				struct uwin *u = &wins[idx];
 				struct uwin *u = &wins[idx];
 				int pmx = (int)mx, pmy = (int)my;
 				int pmx = (int)mx, pmy = (int)my;
-				int rx = u->info.x + (int)u->info.w - 1 -
-					 WM_CLOSE_W;
-				int ry = u->info.y + 1;
 
 
 				u_wm_focus((unsigned int)idx);
 				u_wm_focus((unsigned int)idx);
 				if (u->info.popover) {
 				if (u->info.popover) {
 					/* A popover has no title bar, close
 					/* A popover has no title bar, close
-					 * button or resize grip; the app owns
+					 * button or resize border; the app owns
 					 * its content. */
 					 * its content. */
-				} else if (pmx >= rx &&
-				    pmx < u->info.x + (int)u->info.w &&
-				    pmy >= ry && pmy < ry + WM_TITLE_H) {
+				} else if (close_hit(&u->info, pmx, pmy)) {
 					u_wm_close((unsigned int)idx);
 					u_wm_close((unsigned int)idx);
 					full_redraw = 1;
 					full_redraw = 1;
-				} else if (pmx >= u->info.x + (int)u->info.w - 16 &&
-					   pmy >= u->info.y + (int)u->info.h - 16) {
-					resize_id = idx;
-					resize_mx = mx;
-					resize_my = my;
-					resize_ow = u->info.w;
-					resize_oh = u->info.h;
+				} else if (edges) {
+					begin_resize((unsigned int)idx, edges,
+						     mx, my);
 				} else if (pmy < u->info.y + WM_TITLE_H) {
 				} else if (pmy < u->info.y + WM_TITLE_H) {
 					if (prev_title_id == idx &&
 					if (prev_title_id == idx &&
 					    now - last_title_ms <= WM_DBLCLICK_MS &&
 					    now - last_title_ms <= WM_DBLCLICK_MS &&
@@ -1889,6 +2013,7 @@ static void handle_mouse(struct wm_status *st)
 						drag_dx = pmx - u->info.x;
 						drag_dx = pmx - u->info.x;
 						drag_dy = pmy - u->info.y;
 						drag_dy = pmy - u->info.y;
 						drag_moved = 0;
 						drag_moved = 0;
+						u_wm_mouse_capture(1);
 					}
 					}
 				}
 				}
 			}
 			}
@@ -1896,32 +2021,11 @@ static void handle_mouse(struct wm_status *st)
 	} else if (down && was && resize_id >= 0) {
 	} else if (down && was && resize_id >= 0) {
 		struct uwin *u = &wins[resize_id];
 		struct uwin *u = &wins[resize_id];
 
 
-		if (u->info.used) {
-			int nw = (int)resize_ow + (int)mx - (int)resize_mx;
-			int nh = (int)resize_oh + (int)my - (int)resize_my;
-			int maxw = (int)fbw;
-			int maxh = (int)fbh - WM_BAR_H;
-
-			if (nw < 200) nw = 200;
-			if (nh < 140) nh = 140;
-			if (nw > maxw) nw = maxw;
-			if (nh > maxh) nh = maxh;
-			/* Dragging the grip into the screen corner means "as
-			 * large as the work area", even though the grip starts
-			 * 16 px inside the window edge. */
-			if ((int)mx >= (int)fbw - 1)
-				nw = maxw;
-			if ((int)my >= (int)fbh - 1)
-				nh = maxh;
-			if (nw != (int)u->info.w || nh != (int)u->info.h) {
-				if (set_window_geometry(u,
-					(unsigned int)resize_id, u->info.x,
-					u->info.y, (unsigned int)nw,
-					(unsigned int)nh) == 0)
-					u->state = WIN_NORMAL;
-			}
-		} else {
+		if (u->info.used)
+			resize_window_drag(u, (unsigned int)resize_id, mx, my);
+		else {
 			resize_id = -1;
 			resize_id = -1;
+			u_wm_mouse_capture(0);
 		}
 		}
 	} else if (down && was && drag_id >= 0) {
 	} else if (down && was && drag_id >= 0) {
 		struct uwin *u = &wins[drag_id];
 		struct uwin *u = &wins[drag_id];
@@ -1993,6 +2097,7 @@ static void handle_mouse(struct wm_status *st)
 			}
 			}
 		} else {
 		} else {
 			drag_id = -1;
 			drag_id = -1;
+			u_wm_mouse_capture(0);
 		}
 		}
 	} else if (!down && was) {
 	} else if (!down && was) {
 		/* Dropping a window with the pointer on the left/right screen
 		/* Dropping a window with the pointer on the left/right screen
@@ -2014,6 +2119,7 @@ static void handle_mouse(struct wm_status *st)
 		}
 		}
 		drag_id = -1;
 		drag_id = -1;
 		resize_id = -1;
 		resize_id = -1;
+		u_wm_mouse_capture(0);
 	}
 	}
 	prev_buttons = buttons;
 	prev_buttons = buttons;
 	update_cursor(mx, my);
 	update_cursor(mx, my);

+ 7 - 0
user/ucmd_lib.h

@@ -365,6 +365,13 @@ static inline int u_wm_set_cursor(int shape)
 	return u_syscall1(SYS_WM_SET_CURSOR, shape);
 	return u_syscall1(SYS_WM_SET_CURSOR, shape);
 }
 }
 
 
+/* WM only: take the pointer while dragging/resizing, so canvas clients do
+ * not see the held button; release it when the drag ends. */
+static inline int u_wm_mouse_capture(int on)
+{
+	return u_syscall1(SYS_WM_MOUSE_CAPTURE, on);
+}
+
 static inline int u_screen_size(unsigned int *w, unsigned int *h)
 static inline int u_screen_size(unsigned int *w, unsigned int *h)
 {
 {
 	struct sys_screen s;
 	struct sys_screen s;