Răsfoiți Sursa

Fix window size limits, maximize and off-screen drag

Raise the WM content slot to 4 MB so a full-screen RGB565 canvas fits (1280x1024 needs about 2.5 MB), add canvas-size checks to create/resize, and raise the terminal to 160 columns.

Double-clicking a title bar toggles maximize/restore against the work area below the top bar. Dragging a maximized window restores it first, a plain click leaves it maximized, and a manual resize clears the state.

Windows can be dragged past the left, right and bottom edges while 48 px of the title bar stays recoverable. Window coordinates are signed and an off-screen drag repaints the window rect instead of copying desktop pixels from the shadow.
Johannes Zellner 1 săptămână în urmă
părinte
comite
739019ab8b
8 a modificat fișierele cu 314 adăugiri și 132 ștergeri
  1. 13 2
      AGENTS.md
  2. 20 4
      docs/graphics.html
  3. 1 1
      docs/memory.html
  4. 1 1
      ucmd_term.c
  5. 227 90
      ucmd_wm.c
  6. 44 30
      wm.c
  7. 1 1
      wm.h
  8. 7 3
      wm_abi.h

+ 13 - 2
AGENTS.md

@@ -288,7 +288,16 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   by subtracting the windows in front, so an occluded window is never
   repainted and the stack is not drawn back-to-front.  A focused-window drag
   copies its shadow block (`usurf_copy`) and repaints only the old strips the
-  new rectangle does not cover.  The kernel hit-test uses the same order.
+  new rectangle does not cover; when either rectangle is partly off-screen the
+  shadow has no valid pixels for the hidden part, so that step repaints the
+  window rect instead of copying desktop background into it.  Windows may be
+  dragged past the left, right and bottom edges while at least `WM_DRAG_VISIBLE`
+  (48 px) of the title bar stays on-screen; the top bar is never overlapped.
+  Double-clicking the title bar toggles maximize/restore against the work area
+  below the bar (`0, WM_BAR_H, fbw, fbh - WM_BAR_H`); dragging a maximized
+  window restores it first, and 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.
   The right-aligned date/time text is itself a click target: `draw_bar()`
   records its rectangle, the pointer highlights it, and a click spawns
   `/ramfs/bin/settime` (the `Clock` launcher still opens the analog clock).
@@ -297,7 +306,9 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   but the kernel terminal entry/shell and the cell-window ABI are gone.
 - **Window canvas API**: `gfx.c/h` allocates the RGB565 front/back canvases;
   `sys_wm.c` is the ABI bridge.  `SYS_WIN_CREATE` gives a command its window
-  canvas mapped RW at `USER_WIN_BASE` (`0x50000000`).  `SYS_WIN_PRESENT`
+  canvas mapped RW at `USER_WIN_BASE` (`0x50000000`); the WM maps each front
+  canvas in its own 4 MB `WM_CONTENT_SLOT`, which covers a full-screen
+  1280x1024 canvas minus the chrome.  `SYS_WIN_PRESENT`
   optionally takes a content-relative `struct wm_win_present`; the kernel
   copies only that rectangle into the front canvas (with interrupts off, so
   a half-drawn frame can never reach the screen), unions it into

+ 20 - 4
docs/graphics.html

@@ -197,7 +197,10 @@
 			pointer hit-test uses the same order. A focused-window drag copies
 			its shadow block to the new position and repaints only the old
 			strips the new rectangle does not cover, instead of compositing the
-			window at both positions.
+			window at both positions. If either rectangle is partly off-screen
+			the shadow has no valid pixels for the hidden part, so that step
+			repaints the window rect instead of copying desktop background into
+			it.
 		</p>
 		<p>
 			<code>SYS_WIN_PRESENT</code> carries a content-relative damage
@@ -234,13 +237,26 @@
 			in a window still receives its release after the pointer leaves.
 			The WM process itself is special: it maps the screen shadow at
 			<code>0x58000000</code> and each front canvas at
-			<code>0x59000000 + index × 2 MB</code>, which is how it composites
-			without reading another process's memory.
+			<code>0x59000000 + index × 4 MB</code>, which is how it composites
+			without reading another process's memory. The 4 MB slot covers a
+			full-screen 1280×1024 canvas minus the chrome; the old 2 MB slot
+			capped a full-height window at roughly 1050 px wide.
 			<code>SYS_WM_RESIZE</code> (WM side) and
 			<code>SYS_WIN_ACK_RESIZE</code> (client side) swap in a resized
 			canvas; the content size is bounded by the framebuffer, so a
 			window can grow to the edge of the screen.
 		</p>
+		<p>
+			Double-clicking a title bar maximizes the window to the work area
+			below the system bar (<code>0, WM_BAR_H, fbw, fbh - WM_BAR_H</code>);
+			the next double-click restores the saved geometry. Dragging a
+			maximized window restores it first, and a manual resize clears the
+			maximized state. Windows can be dragged past the left, right and
+			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
+			overlapped. The resize grip is clamped to the full work area and
+			snaps to it when dragged into the screen corner.
+		</p>
 		<div class="note">
 			<p><strong>Buttons fire on the release edge.</strong> The toolkit keeps
 			a button <code>active</code> until <code>ui-&gt;released</code>;
@@ -350,7 +366,7 @@
 			<li><code>vga_copy.S</code> — copy primitives</li>
 			<li><code>blit.c</code>, <code>blit.h</code> — pixel vtable</li>
 			<li><code>cpu.c</code> — SSE2 probing</li>
-			<li><code>wm.c</code>, <code>wm.h</code> — kernel window server</li>
+			<li><code>wm.c</code>, <code>wm.h</code>, <code>wm_abi.h</code> — kernel window server and shared ABI</li>
 			<li><code>ucmd_wm.c</code>, <code>uwm_lib.c</code> — ring-3 WM</li>
 			<li><code>ucmd_settime.c</code>, <code>rtc.c</code> — Date &amp; Time editor and RTC access</li>
 			<li><code>gfx.c</code>, <code>gfx.h</code> — RGB565 window canvases</li>

+ 1 - 1
docs/memory.html

@@ -137,7 +137,7 @@
 					<tr><td><code>0x40000000</code></td><td>Ring-3 ELF link/load address</td><td><code>USER_BASE</code>, 2 MB window up to <code>0x40200000</code></td></tr>
 					<tr><td><code>0x50000000</code></td><td>Ring-3 window canvas</td><td><code>USER_WIN_BASE</code>, 4 MB slot</td></tr>
 					<tr><td><code>0x58000000</code></td><td>WM shadow mapping</td><td><code>WM_SHADOW_VA</code>; ring-3 WM only</td></tr>
-					<tr><td><code>0x59000000</code></td><td>WM content slots</td><td><code>WM_CONTENT_VA_BASE</code> + index × 2 MB (<code>WM_CONTENT_SLOT</code>)</td></tr>
+					<tr><td><code>0x59000000</code></td><td>WM content slots</td><td><code>WM_CONTENT_VA_BASE</code> + index × 4 MB (<code>WM_CONTENT_SLOT</code>)</td></tr>
 					<tr><td><code>0x60000000</code></td><td>Ring-3 anonymous heap</td><td><code>USER_HEAP_BASE</code>, up to 32 MB</td></tr>
 				</tbody>
 			</table>

+ 1 - 1
ucmd_term.c

@@ -15,7 +15,7 @@
 #include "io.h"
 #include "vfs.h"
 
-#define MAX_COLS 128
+#define MAX_COLS 160
 #define MAX_ROWS 64
 #define BUF_MAX 256
 #define LINE_MAX 128

+ 227 - 90
ucmd_wm.c

@@ -20,15 +20,29 @@ struct uwin {
 	struct wm_map_info map;
 	int mapped;
 	unsigned int ack_seq;
+	/* Double-click title bar: the geometry to return to when maximized. */
+	int maximized;
+	int restore_x, restore_y;
+	unsigned int restore_w, restore_h;
 };
 
+/* Double-click detection on the title bar. */
+#define WM_DBLCLICK_MS   500
+#define WM_DBLCLICK_SLOP 8
+/* A maximized window restores only after the pointer actually starts a drag,
+ * so a plain click on its title bar leaves it maximized. */
+#define WM_DRAG_START    3
+
 static struct usurf screen;
 static struct uwin wins[WM_MAX_WINDOWS];
 static unsigned int fbw, fbh;
 static int drag_id = -1;
 static int drag_dx, drag_dy;
+static int drag_mx, drag_my;       /* press position, for restore-on-drag */
 static int resize_id = -1;
 static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
+static unsigned int last_title_ms;
+static int last_title_id = -1, last_title_x, last_title_y;
 static unsigned int prev_buttons;
 static int full_redraw = 1;
 static int boot_term_spawned;
@@ -333,9 +347,9 @@ static void draw_window_clip(unsigned int idx, int cx, int cy,
 	struct wm_window_info *w = &u->info;
 	int x = w->x, y = w->y;
 	unsigned int pw = w->w, ph = w->h;
-	unsigned int bx = (unsigned int)(x + (int)pw - 1 - WM_CLOSE_W);
-	unsigned int cony = (unsigned int)(y + 1 + WM_TITLE_H);
-	unsigned int content_x = (unsigned int)(x + 1);
+	int bx = x + (int)pw - 1 - WM_CLOSE_W;
+	int cony = y + 1 + WM_TITLE_H;
+	int content_x = x + 1;
 	unsigned int content_w = w->content_w;
 	unsigned int content_h = w->content_h;
 
@@ -345,9 +359,9 @@ static void draw_window_clip(unsigned int idx, int cx, int cy,
 		  w->focused ? COL_TITLE_ON : COL_TITLE_OFF, cx, cy, cw, ch);
 	usurf_text(&screen, x + 4, y + 3, w->title, COL_WHITE,
 		   cx, cy, cw, ch);
-	fill_clip((int)bx, y + 1, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE,
+	fill_clip(bx, y + 1, WM_CLOSE_W, WM_TITLE_H, COL_CLOSE,
 		  cx, cy, cw, ch);
-	usurf_text(&screen, (int)bx + 4, y + 3, "x", COL_WHITE,
+	usurf_text(&screen, bx + 4, y + 3, "x", COL_WHITE,
 		   cx, cy, cw, ch);
 
 	/* Content: every window presents an RGB565 canvas (the terminal
@@ -361,12 +375,12 @@ static void draw_window_clip(unsigned int idx, int cx, int cy,
 		c.w = u->map.w;
 		c.h = u->map.h;
 		if (c.w != content_w || c.h != content_h)
-			fill_clip((int)content_x, (int)cony, content_w, content_h,
+			fill_clip(content_x, cony, content_w, content_h,
 				  0x000000u, cx, cy, cw, ch);
-		usurf_canvas(&screen, &c, (int)content_x, (int)cony,
+		usurf_canvas(&screen, &c, content_x, cony,
 			     cx, cy, cw, ch);
 	} else {
-		fill_clip((int)content_x, (int)cony, content_w, content_h,
+		fill_clip(content_x, cony, content_w, content_h,
 			  0x000000u, cx, cy, cw, ch);
 	}
 
@@ -551,7 +565,7 @@ 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);
 }
 
-static int window_at(unsigned int mx, unsigned int my)
+static int window_at(int mx, int my)
 {
 	int order[WM_MAX_WINDOWS];
 	unsigned int n = build_order(order), i;
@@ -559,9 +573,9 @@ static int window_at(unsigned int mx, unsigned int my)
 	for (i = 0; i < n; i++) {
 		struct wm_window_info *w = &wins[order[i]].info;
 
-		if (mx >= (unsigned int)w->x && my >= (unsigned int)w->y &&
-		    mx < (unsigned int)(w->x + (int)w->w) &&
-		    my < (unsigned int)(w->y + (int)w->h))
+		if (mx >= w->x && my >= w->y &&
+		    mx < w->x + (int)w->w &&
+		    my < w->y + (int)w->h)
 			return order[i];
 	}
 	return -1;
@@ -569,15 +583,26 @@ static int window_at(unsigned int mx, unsigned int my)
 
 static void clamp_geom(struct uwin *u, int *x, int *y)
 {
-	int maxx = (int)fbw - (int)u->info.w;
-	int maxy = (int)fbh - (int)u->info.h;
-
-	if (*x < 0) *x = 0;
-	if (*y < WM_BAR_H) *y = WM_BAR_H;
-	if (maxx < 0) maxx = 0;
-	if (maxy < WM_BAR_H) maxy = WM_BAR_H;
-	if (*x > maxx) *x = maxx;
-	if (*y > maxy) *y = maxy;
+	/* Match wm_clamp_window(): allow the window to hang off the left,
+	 * right and bottom edges but keep the title bar reachable and below
+	 * the system bar. */
+	int minx = WM_DRAG_VISIBLE - (int)u->info.w;
+	int maxx = (int)fbw - WM_DRAG_VISIBLE;
+	int miny = WM_BAR_H;
+	int maxy = (int)fbh - WM_TITLE_H;
+
+	if (minx > maxx)
+		minx = maxx = 0;
+	if (miny > maxy)
+		miny = maxy = WM_BAR_H;
+	if (*x < minx)
+		*x = minx;
+	else if (*x > maxx)
+		*x = maxx;
+	if (*y < miny)
+		*y = miny;
+	else if (*y > maxy)
+		*y = maxy;
 }
 
 static void add_rect(struct wm_present_info *p, int x, int y,
@@ -611,16 +636,26 @@ static void add_rect(struct wm_present_info *p, int x, int y,
 
 /* A focused window is topmost, so the shadow contains exactly its pixels in
  * the old rectangle.  Move that block and repaint only the old part the new
- * rectangle does not cover. */
+ * rectangle does not cover.  When either rectangle is partly off-screen the
+ * shadow has no valid pixels for the hidden part, so repaint the new rect
+ * instead of copying desktop background into the window. */
 static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
 {
 	struct wm_present_info present;
 	struct orect oldr, newr, pieces[4];
 	unsigned int np, k;
+	int w = (int)u->info.w, h = (int)u->info.h;
+	int old_on = ox >= 0 && oy >= 0 &&
+		     ox + w <= (int)fbw && oy + h <= (int)fbh;
+	int new_on = nx >= 0 && ny >= 0 &&
+		     nx + w <= (int)fbw && ny + h <= (int)fbh;
 
 	if (u_wm_begin() != 0)
 		return;
-	usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
+	if (old_on && new_on)
+		usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
+	else
+		draw_rect(nx, ny, u->info.w, u->info.h);
 	oldr.x = ox;
 	oldr.y = oy;
 	oldr.w = u->info.w;
@@ -641,6 +676,81 @@ static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
 	u_wm_present(&present);
 }
 
+static int iabs(int v)
+{
+	return v < 0 ? -v : v;
+}
+
+/* Repaint the old and new rectangles of a geometry change. */
+static void redraw_geom(struct uwin *u, int ox, int oy,
+			unsigned int ow, unsigned int oh)
+{
+	struct wm_present_info present;
+	unsigned int i;
+
+	present.count = 0;
+	present.immediate = 1;
+	add_rect(&present, ox, oy, ow, oh);
+	add_rect(&present, u->info.x, u->info.y, u->info.w, u->info.h);
+	if (u_wm_begin() == 0) {
+		for (i = 0; i < present.count; i++)
+			draw_rect(present.rect[i].x, present.rect[i].y,
+				  present.rect[i].w, present.rect[i].h);
+		u_wm_present(&present);
+	}
+}
+
+/* Resize (when needed), move and remap a window in one step. */
+static int set_window_geometry(struct uwin *u, unsigned int idx,
+			       int nx, int ny, unsigned int nw, unsigned int nh)
+{
+	struct wm_window_info ni;
+	int ox = u->info.x, oy = u->info.y;
+	unsigned int ow = u->info.w, oh = u->info.h;
+
+	if ((int)nw != (int)ow || (int)nh != (int)oh) {
+		if (u_wm_resize(idx, nw - 2, nh - WM_TITLE_H - 2) != 0)
+			return -1;
+	}
+	if (nx != ox || ny != oy)
+		u_wm_set_geom(idx, nx, ny);
+	u->mapped = 0;
+	if (u_wm_info(idx, &ni) == 1)
+		u->info = ni;
+	if (u_wm_map(idx, &u->map) == 0)
+		u->mapped = 1;
+	redraw_geom(u, ox, oy, ow, oh);
+	return 0;
+}
+
+static int restore_window(struct uwin *u, unsigned int idx)
+{
+	if (!u->maximized)
+		return 0;
+	if (set_window_geometry(u, idx, u->restore_x, u->restore_y,
+				u->restore_w, u->restore_h) != 0)
+		return -1;
+	u->maximized = 0;
+	return 0;
+}
+
+/* Double-click title bar: fill the work area below the system bar, or go back
+ * to the geometry the window had before it was maximized. */
+static int toggle_maximize(struct uwin *u, unsigned int idx)
+{
+	if (u->maximized)
+		return restore_window(u, idx);
+	u->restore_x = u->info.x;
+	u->restore_y = u->info.y;
+	u->restore_w = u->info.w;
+	u->restore_h = u->info.h;
+	if (set_window_geometry(u, idx, 0, WM_BAR_H,
+				fbw, fbh - WM_BAR_H) != 0)
+		return -1;
+	u->maximized = 1;
+	return 0;
+}
+
 /* The top-bar date/time text is also a button: it opens the Date & Time
  * editor.  The rectangle is recorded by draw_bar(). */
 static int bar_clock_hit(unsigned int mx, unsigned int my)
@@ -900,6 +1010,10 @@ static void handle_mouse(struct wm_status *st)
 	int was = (prev_buttons & 1) != 0;
 
 	if (down && !was) {
+		int prev_title_id = last_title_id;
+		unsigned int now = u_ticks_ms();
+
+		last_title_id = -1;
 		if (bar_clock_hit(mx, my)) {
 			spawn_app_checked("/ramfs/bin/settime");
 		} else if (mx >= WM_BTN_X && my >= WM_BTN_Y &&
@@ -942,30 +1056,48 @@ static void handle_mouse(struct wm_status *st)
 			   my < WM_BTN_GALLERY_Y + WM_BTN_GALLERY_H) {
 			spawn_app_checked("/ramfs/bin/gallery");
 		} else {
-			int idx = window_at(mx, my);
+			int idx = window_at((int)mx, (int)my);
 
 			if (idx >= 0) {
 				struct uwin *u = &wins[idx];
+				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);
-				if (mx >= (unsigned int)(u->info.x + (int)u->info.w -
-							 1 - WM_CLOSE_W) &&
-				    mx < (unsigned int)(u->info.x + (int)u->info.w) &&
-				    my >= (unsigned int)(u->info.y + 1) &&
-				    my < (unsigned int)(u->info.y + 1 + WM_TITLE_H)) {
+				if (pmx >= rx &&
+				    pmx < u->info.x + (int)u->info.w &&
+				    pmy >= ry && pmy < ry + WM_TITLE_H) {
 					u_wm_close((unsigned int)idx);
 					full_redraw = 1;
-				} else if (mx >= (unsigned int)(u->info.x + (int)u->info.w - 16) &&
-					   my >= (unsigned int)(u->info.y + (int)u->info.h - 16)) {
+				} 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 (my < (unsigned int)(u->info.y + WM_TITLE_H)) {
-					drag_id = idx;
-					drag_dx = (int)mx - u->info.x;
-					drag_dy = (int)my - u->info.y;
+				} else if (pmy < u->info.y + WM_TITLE_H) {
+					if (prev_title_id == idx &&
+					    now - last_title_ms <= WM_DBLCLICK_MS &&
+					    iabs(pmx - last_title_x) <= WM_DBLCLICK_SLOP &&
+					    iabs(pmy - last_title_y) <= WM_DBLCLICK_SLOP) {
+						last_title_ms = 0;
+						if (toggle_maximize(u,
+						    (unsigned int)idx) != 0)
+							bar_notice("resize failed");
+					} else {
+						last_title_id = idx;
+						last_title_ms = now;
+						last_title_x = pmx;
+						last_title_y = pmy;
+						drag_id = idx;
+						drag_mx = pmx;
+						drag_my = pmy;
+						drag_dx = pmx - u->info.x;
+						drag_dy = pmy - u->info.y;
+					}
 				}
 			}
 		}
@@ -973,46 +1105,28 @@ static void handle_mouse(struct wm_status *st)
 		struct uwin *u = &wins[resize_id];
 
 		if (u->info.used) {
-			unsigned int oldw = u->info.w, oldh = u->info.h;
 			int nw = (int)resize_ow + (int)mx - (int)resize_mx;
 			int nh = (int)resize_oh + (int)my - (int)resize_my;
-			int maxw = (int)fbw - u->info.x;
-			int maxh = (int)fbh - u->info.y;
+			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) {
-				struct wm_window_info ni;
-				struct wm_map_info nm;
-				struct wm_present_info present;
-				unsigned int i;
-
-				if (u_wm_resize((unsigned int)resize_id,
-						(unsigned int)(nw - 2),
-						(unsigned int)(nh - WM_TITLE_H - 2)) == 0) {
-					if (u_wm_info((unsigned int)resize_id, &ni) == 1)
-						u->info = ni;
-					u->mapped = 0;
-					if (u_wm_map((unsigned int)resize_id, &nm) == 0) {
-						u->map = nm;
-						u->mapped = 1;
-					}
-					present.count = 0;
-					present.immediate = 1;
-					add_rect(&present, u->info.x, u->info.y, oldw, oldh);
-					add_rect(&present, u->info.x, u->info.y,
-						 u->info.w, u->info.h);
-					if (u_wm_begin() == 0) {
-						for (i = 0; i < present.count; i++)
-							draw_rect(present.rect[i].x,
-								  present.rect[i].y,
-								  present.rect[i].w,
-								  present.rect[i].h);
-						u_wm_present(&present);
-					}
-				}
+				if (set_window_geometry(u,
+					(unsigned int)resize_id, u->info.x,
+					u->info.y, (unsigned int)nw,
+					(unsigned int)nh) == 0)
+					u->maximized = 0;
 			}
 		} else {
 			resize_id = -1;
@@ -1021,32 +1135,55 @@ static void handle_mouse(struct wm_status *st)
 		struct uwin *u = &wins[drag_id];
 
 		if (u->info.used) {
-			int ox = u->info.x, oy = u->info.y;
-			int nx = (int)mx - drag_dx;
-			int ny = (int)my - drag_dy;
-
-			clamp_geom(u, &nx, &ny);
-			if (nx != ox || ny != oy) {
-				struct wm_present_info present;
-				unsigned int i;
-
-				u_wm_set_geom((unsigned int)drag_id, nx, ny);
-				u->info.x = nx;
-				u->info.y = ny;
-				if (u->info.focused) {
-					move_window_pixels(u, ox, oy, nx, ny);
-				} else {
-					present.count = 0;
-					present.immediate = 1;
-					add_rect(&present, ox, oy, u->info.w, u->info.h);
-					add_rect(&present, nx, ny, u->info.w, u->info.h);
-					if (u_wm_begin() == 0) {
-						for (i = 0; i < present.count; i++)
-							draw_rect(present.rect[i].x,
+			int ox, oy, nx, ny;
+			int started = !u->maximized ||
+				iabs((int)mx - drag_mx) >= WM_DRAG_START ||
+				iabs((int)my - drag_my) >= WM_DRAG_START;
+
+			/* A maximized window is restored as soon as the drag
+			 * starts; keep the pointer's grab point relative to the
+			 * restored geometry.  A click that does not move must
+			 * leave it maximized. */
+			if (u->maximized && started &&
+			    restore_window(u, (unsigned int)drag_id) == 0) {
+				drag_dx = drag_mx - u->info.x;
+				drag_dy = drag_my - u->info.y;
+			}
+			if (!u->maximized) {
+				ox = u->info.x;
+				oy = u->info.y;
+				nx = (int)mx - drag_dx;
+				ny = (int)my - drag_dy;
+
+				clamp_geom(u, &nx, &ny);
+				if (nx != ox || ny != oy) {
+					struct wm_present_info present;
+					unsigned int i;
+
+					u_wm_set_geom((unsigned int)drag_id,
+						      nx, ny);
+					u->info.x = nx;
+					u->info.y = ny;
+					if (u->info.focused) {
+						move_window_pixels(u, ox, oy,
+								   nx, ny);
+					} else {
+						present.count = 0;
+						present.immediate = 1;
+						add_rect(&present, ox, oy,
+							 u->info.w, u->info.h);
+						add_rect(&present, nx, ny,
+							 u->info.w, u->info.h);
+						if (u_wm_begin() == 0) {
+							for (i = 0;
+							     i < present.count; i++)
+								draw_rect(
+								  present.rect[i].x,
 								  present.rect[i].y,
 								  present.rect[i].w,
 								  present.rect[i].h);
-						u_wm_present(&present);
+							u_wm_present(&present);
+						}
 					}
 				}
 			}

+ 44 - 30
wm.c

@@ -61,7 +61,7 @@ static void wm_focus_top(void);
 static void wm_close_window(unsigned int idx);
 static void wm_clamp_window(struct window *w, int nx, int ny);
 static void wm_server_unmap_index(unsigned int idx);
-static int wm_window_at(unsigned int px, unsigned int py);
+static int wm_window_at(int px, int py);
 
 void wm_request_redraw(void)
 {
@@ -192,6 +192,8 @@ int wm_server_win_create(const struct win_create *in, struct win_info *out)
 	ch = in->h;
 	if (cw == 0 || ch == 0 || cw > WM_WIN_MAX_W || ch > WM_WIN_MAX_H)
 		return -1;
+	if (cw * ch * 2 > WM_CONTENT_SLOT || cw * ch * 2 > USER_WIN_RANGE)
+		return -1;
 	if (cw + 2 > vga_fb_width() || ch + WM_TITLE_H + 2 > vga_fb_height())
 		return -1;
 	for (i = 0; i < MAX_WINDOWS; i++)
@@ -247,12 +249,14 @@ int wm_server_win_create(const struct win_create *in, struct win_info *out)
 	w->canvas_dmg_w = w->canvas_dmg_h = 0;
 	w->pw = cw + 2;
 	w->ph = ch + WM_TITLE_H + 2;
-	w->x = 24 + i * 32;
-	w->y = WM_BAR_H + 12 + i * 28;
-	if (w->x + w->pw > vga_fb_width())
-		w->x = (vga_fb_width() > w->pw) ? vga_fb_width() - w->pw : 0;
-	if (w->y + w->ph > vga_fb_height())
+	w->x = 24 + (int)i * 32;
+	w->y = WM_BAR_H + 12 + (int)i * 28;
+	if (w->x + (int)w->pw > (int)vga_fb_width())
+		w->x = ((int)vga_fb_width() > (int)w->pw) ?
+		       (int)vga_fb_width() - (int)w->pw : 0;
+	if (w->y + (int)w->ph > (int)vga_fb_height())
 		w->y = WM_BAR_H + 4;
+	wm_clamp_window(w, w->x, w->y);
 	w->focused = 0;
 	w->content_gen = 1;
 	w->mapped_kind = WM_KIND_NONE;
@@ -445,7 +449,8 @@ int wm_server_win_pointer(struct win_pointer *out)
 	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(mx, my) == idx)) ? 1 : 0;
+		       (w->focused || wm_window_at((int)mx, (int)my) == idx)) ?
+		      1 : 0;
 	out->focused = w->focused ? 1 : 0;
 	return 0;
 }
@@ -479,6 +484,8 @@ int wm_server_resize(unsigned int index, unsigned int cw, unsigned int ch)
 		return -1;
 	if (cw < 160 || ch < 80 || cw > WM_WIN_MAX_W || ch > WM_WIN_MAX_H)
 		return -1;
+	if (cw * ch * 2 > WM_CONTENT_SLOT || cw * ch * 2 > USER_WIN_RANGE)
+		return -1;
 	if (cw + 2 > vga_fb_width() || ch + WM_TITLE_H + 2 > vga_fb_height())
 		return -1;
 	if (gfx_alloc(&nf, cw, ch) != 0)
@@ -827,17 +834,17 @@ static void wm_route_pointer(unsigned int mx, unsigned int my,
 	unsigned int release = wm_ptr_buttons & ~buttons;
 
 	if (press & 1) {
-		int idx = wm_window_at(mx, my);
+		int idx = wm_window_at((int)mx, (int)my);
 
 		if (idx >= 0) {
 			struct window *w = &windows[idx];
-			int cx = (int)mx - (int)w->x - 1;
-			int cy = (int)my - (int)w->y - 1 - WM_TITLE_H;
+			int cx = (int)mx - w->x - 1;
+			int cy = (int)my - w->y - 1 - WM_TITLE_H;
 
 			if (w->canvas_window && cx >= 0 && cy >= 0 &&
 			    cx < (int)w->canvas.w && cy < (int)w->canvas.h &&
-			    !(mx >= w->x + w->pw - 16 &&
-			      my >= w->y + w->ph - 16)) {
+			    !((int)mx >= w->x + (int)w->pw - 16 &&
+			      (int)my >= w->y + (int)w->ph - 16)) {
 				wm_queue_ptr_event(w, WIN_EV_POINTER_DOWN,
 						   cx, cy, buttons);
 				wm_ptr_capture = idx;
@@ -1071,10 +1078,11 @@ void wm_server_init(void)
 	 * says the storage scan is done. */
 }
 
-static int wm_point_in_rect(unsigned int px, unsigned int py,
-	unsigned int rx, unsigned int ry, unsigned int rw, unsigned int rh)
+static int wm_point_in_rect(int px, int py,
+	int rx, int ry, unsigned int rw, unsigned int rh)
 {
-	return px >= rx && px < rx + rw && py >= ry && py < ry + rh;
+	return px >= rx && px < rx + (int)rw &&
+	       py >= ry && py < ry + (int)rh;
 }
 
 
@@ -1135,7 +1143,7 @@ static void wm_close_window(unsigned int idx)
 }
 
 /* Topmost window containing (px,py), or -1. */
-static int wm_window_at(unsigned int px, unsigned int py)
+static int wm_window_at(int px, int py)
 {
 	unsigned int i;
 	for (i = 0; i < MAX_WINDOWS; i++)
@@ -1155,24 +1163,30 @@ static int wm_window_at(unsigned int px, unsigned int py)
 
 static void wm_clamp_window(struct window *w, int nx, int ny)
 {
-	unsigned int fbw = vga_fb_width();
-	unsigned int fbh = vga_fb_height();
-	int maxx = (int)fbw - (int)w->pw;
-	int maxy = (int)fbh - (int)w->ph;
-	if (maxx < 0)
-		maxx = 0;
-	if (maxy < (int)WM_BAR_H)
-		maxy = (int)WM_BAR_H;
-	if (nx < 0)
-		nx = 0;
+	int fbw = (int)vga_fb_width();
+	int fbh = (int)vga_fb_height();
+	/* Allow a window to hang off the left, right and bottom edges, but
+	 * keep a strip of the title bar reachable and never let it overlap the
+	 * system bar at the top. */
+	int minx = WM_DRAG_VISIBLE - (int)w->pw;
+	int maxx = fbw - WM_DRAG_VISIBLE;
+	int miny = WM_BAR_H;
+	int maxy = fbh - WM_TITLE_H;
+
+	if (minx > maxx)
+		minx = maxx = 0;
+	if (miny > maxy)
+		miny = maxy = WM_BAR_H;
+	if (nx < minx)
+		nx = minx;
 	else if (nx > maxx)
 		nx = maxx;
-	if (ny < (int)WM_BAR_H)
-		ny = (int)WM_BAR_H;
+	if (ny < miny)
+		ny = miny;
 	else if (ny > maxy)
 		ny = maxy;
-	w->x = (unsigned int)nx;
-	w->y = (unsigned int)ny;
+	w->x = nx;
+	w->y = ny;
 }
 
 /* Fill the intersection of rect (rx,ry,rw,rh) with clip rect (cx,cy,cw,ch). */

+ 1 - 1
wm.h

@@ -16,7 +16,7 @@
 
 struct window {
 	int used;
-	unsigned int x, y;       /* pixel origin (title bar top-left) */
+	int x, y;                /* title bar top-left; may be negative */
 	unsigned int pw, ph;     /* pixel size */
 	char title[16];
 	int focused;

+ 7 - 3
wm_abi.h

@@ -45,12 +45,16 @@
 #define WM_TITLE_H 22
 #define WM_CLOSE_W 16
 
+/* 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. */
+#define WM_DRAG_VISIBLE 48
+
 /* Fixed user virtual addresses for the WM's mappings.  The shadow is one
- * mapping; each window gets a private content slot (large enough for the
- * current canvas and future bigger ones). */
+ * mapping; each window gets a private content slot large enough for a
+ * full-screen RGB565 canvas (1280x1024 minus the chrome is ~2.5 MB). */
 #define WM_SHADOW_VA        0x58000000u
 #define WM_CONTENT_VA_BASE  0x59000000u
-#define WM_CONTENT_SLOT     0x00200000u   /* 2 MB per window */
+#define WM_CONTENT_SLOT     0x00400000u   /* 4 MB per window */
 
 /* Window content kind. */
 #define WM_KIND_NONE 0