Procházet zdrojové kódy

Add resizable windows

Johannes Zellner před 2 týdny
rodič
revize
f3846a06f3
13 změnil soubory, kde provedl 360 přidání a 28 odebrání
  1. 29 0
      sys_wm.c
  2. 3 0
      sys_wm.h
  3. 7 0
      syscall.c
  4. 3 0
      syscall.h
  5. 28 11
      ucmd_clock.c
  6. 9 0
      ucmd_colors.c
  7. 13 0
      ucmd_edit.c
  8. 20 0
      ucmd_lib.h
  9. 42 16
      ucmd_term.c
  10. 72 0
      ucmd_wm.c
  11. 124 1
      wm.c
  12. 6 0
      wm.h
  13. 4 0
      wm_abi.h

+ 29 - 0
sys_wm.c

@@ -169,6 +169,35 @@ int sys_win_close(void)
 	return wm_server_win_close();
 }
 
+int sys_win_info(struct win_info *out)
+{
+	struct win_info info;
+	int r;
+
+	if (!user_ok(out, sizeof(info), 1))
+		return -1;
+	zero_struct(&info, sizeof(info));
+	r = wm_server_win_info(&info);
+	if (r == 0)
+		copy_to_user(out, &info, sizeof(info));
+	return r;
+}
+
+int sys_win_ack_resize(void)
+{
+	return wm_server_win_ack_resize();
+}
+
+int sys_wm_resize(unsigned int index, const struct wm_size *in)
+{
+	struct wm_size s;
+
+	if (!user_ok(in, sizeof(s), 0))
+		return -1;
+	copy_from_user(&s, in, sizeof(s));
+	return wm_server_resize(index, s.w, s.h);
+}
+
 int sys_win_key_wait(unsigned int timeout_ms)
 {
 	return wm_server_key_wait(timeout_ms);

+ 3 - 0
sys_wm.h

@@ -20,6 +20,9 @@ int sys_wm_sync(void);
 int sys_win_create(const struct win_create *in, struct win_info *out);
 int sys_win_present(void);
 int sys_win_close(void);
+int sys_win_info(struct win_info *out);
+int sys_win_ack_resize(void);
+int sys_wm_resize(unsigned int index, const struct wm_size *in);
 int sys_win_key_wait(unsigned int timeout_ms);
 
 #endif

+ 7 - 0
syscall.c

@@ -232,6 +232,13 @@ int syscall_dispatch(int num, int a1, int a2, int a3)
 		return sys_win_present();
 	case SYS_WIN_CLOSE:
 		return sys_win_close();
+	case SYS_WM_RESIZE:
+		return sys_wm_resize((unsigned int)a1,
+				     (const struct wm_size *)a2);
+	case SYS_WIN_INFO:
+		return sys_win_info((struct win_info *)a1);
+	case SYS_WIN_ACK_RESIZE:
+		return sys_win_ack_resize();
 	case SYS_WIN_KEY_WAIT:
 		return sys_win_key_wait((unsigned int)a1);
 	case SYS_SCHEDINFO:

+ 3 - 0
syscall.h

@@ -103,6 +103,9 @@
 #define SYS_WIN_CREATE     70  /* (struct win_create *in, struct win_info *out) */
 #define SYS_WIN_PRESENT    71  /* () -> 0/-1; publish the back canvas */
 #define SYS_WIN_CLOSE      72  /* () -> 0/-1; close the caller's window */
+#define SYS_WM_RESIZE      83  /* (index, struct wm_size *content) -> 0/-1 */
+#define SYS_WIN_INFO       84  /* (struct win_info *out) -> 0/-1 */
+#define SYS_WIN_ACK_RESIZE 85  /* () -> 0/-1; swap in the pending canvas */
 
 /* Terminal / process services used by the ring-3 shell. */
 #define SYS_TTY_CREATE     73  /* () -> tty id 1..TTY_MAX / -1 */

+ 28 - 11
ucmd_clock.c

@@ -47,6 +47,21 @@ static unsigned short *fb;
 static unsigned int cw, ch;
 static unsigned int cx, cy, radius, rad_hour, rad_min, rad_sec, tick_len;
 
+static void clock_geometry(void)
+{
+	cx = cw / 2;
+	cy = ch / 2;
+	radius = (cw < ch) ? cw / 2 : ch / 2;
+	if (radius > 200)
+		radius = 200;
+	rad_hour = radius / 2;
+	rad_min = radius * 7 / 10;
+	rad_sec = radius * 9 / 10;
+	tick_len = radius / 13;
+	if (tick_len < 2)
+		tick_len = 2;
+}
+
 static unsigned short rgb565(unsigned int r, unsigned int g, unsigned int b)
 {
 	return (unsigned short)(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
@@ -170,21 +185,23 @@ void _start(const char *arg)
 	cw = info.width;
 	ch = info.height;
 
-	cx = cw / 2;
-	cy = ch / 2;
-	radius = (cw < ch) ? cw / 2 : ch / 2;
-	if (radius > 200)
-		radius = 200;
-	rad_hour = radius / 2;
-	rad_min = radius * 7 / 10;
-	rad_sec = radius * 9 / 10;
-	tick_len = radius / 13;
-	if (tick_len < 2)
-		tick_len = 2;
+	clock_geometry();
 
 	for (;;) {
+		struct win_info cur;
 		int k = u_key_poll();
 
+		if (u_win_info(&cur) == 0 &&
+		    (cur.width != cw || cur.height != ch)) {
+			if (u_win_ack_resize() == 0) {
+				fb = (unsigned short *)cur.fb;
+				cw = cur.width;
+				ch = cur.height;
+				clock_geometry();
+				last_sec = 99;
+			}
+		}
+
 		if (k == 0x1B || k == 0x03)
 			break;
 		u_rtc_hms(&tm);

+ 9 - 0
ucmd_colors.c

@@ -154,8 +154,17 @@ void _start(const char *arg)
 	last_fps = u_ticks_ms();
 
 	for (;;) {
+		struct win_info cur;
 		unsigned int frame_start, elapsed;
 
+		if (u_win_info(&cur) == 0 &&
+		    (cur.width != w || cur.height != h)) {
+			if (u_win_ack_resize() == 0) {
+				px = (unsigned short *)cur.fb;
+				w = cur.width;
+				h = cur.height;
+			}
+		}
 		{
 			int k = u_key_poll();
 			if (k == 0x1B || k == 0x03)

+ 13 - 0
ucmd_edit.c

@@ -615,9 +615,22 @@ void _start(const char *arg)
 
 	redraw();
 	for (;;) {
+		struct win_info cur;
 		int quit = 0, got = 0, k;
 		unsigned int now;
 
+		if (u_win_info(&cur) == 0 &&
+		    (cur.width != fb_w || cur.height != fb_h)) {
+			if (u_win_ack_resize() == 0) {
+				fb = (unsigned short *)cur.fb;
+				fb_w = cur.width;
+				fb_h = cur.height;
+				cols = fb_w / CELL_W;
+				rows = fb_h / CELL_H;
+				text_rows = (rows > 2) ? rows - 2 : 1;
+				got = 1;
+			}
+		}
 		while ((k = u_key_poll()) != 0) {
 			got = 1;
 			if (handle_key(k)) {

+ 20 - 0
ucmd_lib.h

@@ -285,6 +285,26 @@ static inline int u_win_close(void)
 	return u_syscall0(SYS_WIN_CLOSE);
 }
 
+static inline int u_win_info(struct win_info *out)
+{
+	return u_syscall1(SYS_WIN_INFO, (int)out);
+}
+
+static inline int u_win_ack_resize(void)
+{
+	return u_syscall0(SYS_WIN_ACK_RESIZE);
+}
+
+static inline int u_wm_resize(unsigned int index, unsigned int cw,
+			      unsigned int ch)
+{
+	struct wm_size s;
+
+	s.w = cw;
+	s.h = ch;
+	return u_syscall2(SYS_WM_RESIZE, (int)index, (int)&s);
+}
+
 static inline int u_win_key_wait(unsigned int timeout_ms)
 {
 	return u_syscall1(SYS_WIN_KEY_WAIT, (int)timeout_ms);

+ 42 - 16
ucmd_term.c

@@ -7,8 +7,8 @@
 #include "io.h"
 #include "vfs.h"
 
-#define COLS 64
-#define ROWS 32
+#define MAX_COLS 128
+#define MAX_ROWS 64
 #define BUF_MAX 256
 #define LINE_MAX 128
 #define HIST_MAX 16
@@ -16,7 +16,8 @@
 static struct win_info wi;
 static struct usurf surf;
 static int tty;
-static unsigned short cells[COLS * ROWS];
+static unsigned short cells[MAX_COLS * MAX_ROWS];
+static unsigned int cols = 64, rows = 32;
 static unsigned int cx, cy;
 static char line[LINE_MAX];
 static unsigned int line_len;
@@ -33,7 +34,7 @@ static unsigned int last_blink;
 
 static unsigned short *cell_at(unsigned int x, unsigned int y)
 {
-	return &cells[y * COLS + x];
+	return &cells[y * cols + x];
 }
 
 static void cell_put(unsigned int x, unsigned int y, char ch)
@@ -45,9 +46,9 @@ static void scroll_up(void)
 {
 	unsigned int i;
 
-	for (i = 0; i < (ROWS - 1) * COLS; i++)
-		cells[i] = cells[i + COLS];
-	for (; i < ROWS * COLS; i++)
+	for (i = 0; i < (rows - 1) * cols; i++)
+		cells[i] = cells[i + cols];
+	for (; i < rows * cols; i++)
 		cells[i] = 0x0A20;
 }
 
@@ -55,9 +56,9 @@ static void term_newline(void)
 {
 	cx = 0;
 	cy++;
-	if (cy >= ROWS) {
+	if (cy >= rows) {
 		scroll_up();
-		cy = ROWS - 1;
+		cy = rows - 1;
 	}
 	dirty = 1;
 }
@@ -90,7 +91,7 @@ static void term_putc(char c)
 		return;
 	cell_put(cx, cy, c);
 	cx++;
-	if (cx >= COLS)
+	if (cx >= cols)
 		term_newline();
 	dirty = 1;
 }
@@ -105,7 +106,7 @@ static void term_clear(void)
 {
 	unsigned int i;
 
-	for (i = 0; i < COLS * ROWS; i++)
+	for (i = 0; i < cols * rows; i++)
 		cells[i] = 0x0A20;
 	cx = cy = 0;
 	dirty = 1;
@@ -113,8 +114,8 @@ static void term_clear(void)
 
 static void cursor_set(unsigned int row, unsigned int col)
 {
-	if (row >= ROWS) row = ROWS - 1;
-	if (col >= COLS) col = COLS - 1;
+	if (row >= rows) row = rows - 1;
+	if (col >= cols) col = cols - 1;
 	cy = row;
 	cx = col;
 	dirty = 1;
@@ -201,8 +202,8 @@ static void render(void)
 	if (!dirty)
 		return;
 	usurf_fill(&surf, 0, 0, wi.width, wi.height, 0x000000u);
-	usurf_cells(&surf, cells, COLS, ROWS, 0, 0, 0, 0, wi.width, wi.height);
-	if (cursor_on && cx < COLS && cy < ROWS)
+	usurf_cells(&surf, cells, cols, rows, 0, 0, 0, 0, wi.width, wi.height);
+	if (cursor_on && cx < cols && cy < rows)
 		usurf_fill(&surf, (int)cx * 8, (int)cy * 16, 2, 16, 0xFFFFFFu);
 	u_win_present();
 	dirty = 0;
@@ -216,7 +217,7 @@ static void line_show(void)
 	cx = line_col;
 	for (i = 0; i < line_len; i++)
 		term_putc(line[i]);
-	for (x = cx; x < COLS; x++)
+	for (x = cx; x < cols; x++)
 		cell_put(x, cy, ' ');
 	dirty = 1;
 }
@@ -741,6 +742,10 @@ void _start(const char *arg)
 	if (u_win_create(512, 512, "term", &wi) != 0)
 		u_exit(1);
 	usurf_init(&surf, wi.fb, wi.width, wi.height, wi.width * 2, 16);
+	cols = wi.width / 8;
+	rows = wi.height / 16;
+	if (cols > MAX_COLS) cols = MAX_COLS;
+	if (rows > MAX_ROWS) rows = MAX_ROWS;
 	term_clear();
 	u_getcwd(cwd, sizeof(cwd));
 	{
@@ -754,8 +759,29 @@ void _start(const char *arg)
 	render();
 
 	for (;;) {
+		struct win_info cur;
 		int got = 0;
 
+		if (u_win_info(&cur) == 0 &&
+		    (cur.width != wi.width || cur.height != wi.height)) {
+			if (u_win_ack_resize() == 0) {
+				wi = cur;
+				usurf_init(&surf, wi.fb, wi.width, wi.height,
+					   wi.width * 2, 16);
+				cols = wi.width / 8;
+				rows = wi.height / 16;
+				if (cols > MAX_COLS) cols = MAX_COLS;
+				if (rows > MAX_ROWS) rows = MAX_ROWS;
+				term_clear();
+				if (fg_pid < 0)
+					prompt();
+				else {
+					cx = cy = 0;
+					line_row = line_col = 0;
+				}
+				dirty = 1;
+			}
+		}
 		drain_tty();
 		if (fg_pid > 0) {
 			if (!u_alive((unsigned int)fg_pid)) {

+ 72 - 0
ucmd_wm.c

@@ -27,6 +27,8 @@ 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 resize_id = -1;
+static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
 static unsigned int prev_buttons;
 static int full_redraw = 1;
 static unsigned int bar_kexec_busy, bar_kexec_progress, bar_kexec_total;
@@ -161,6 +163,20 @@ static void draw_window_clip(unsigned int idx, int cx, int cy,
 		fill_clip((int)px, (int)py, 2, 10, COL_WHITE,
 			  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);
+		}
+	}
 }
 
 static void draw_rect(int x, int y, unsigned int w, unsigned int h)
@@ -422,6 +438,13 @@ static void handle_mouse(struct wm_status *st)
 				    my < (unsigned int)(u->info.y + 1 + 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)) {
+					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;
@@ -429,6 +452,54 @@ static void handle_mouse(struct wm_status *st)
 				}
 			}
 		}
+	} else if (down && was && resize_id >= 0) {
+		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;
+
+			if (nw < 200) nw = 200;
+			if (nh < 140) nh = 140;
+			if (nw > maxw) nw = maxw;
+			if (nh > maxh) 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);
+					}
+				}
+			}
+		} else {
+			resize_id = -1;
+		}
 	} else if (down && was && drag_id >= 0) {
 		struct uwin *u = &wins[drag_id];
 
@@ -463,6 +534,7 @@ static void handle_mouse(struct wm_status *st)
 		}
 	} else if (!down && was) {
 		drag_id = -1;
+		resize_id = -1;
 	}
 	prev_buttons = buttons;
 }

+ 124 - 1
wm.c

@@ -245,6 +245,13 @@ static void wm_free_canvas_window(unsigned int idx)
 		w->back = 0;
 	}
 	w->back_pages = 0;
+	if (w->pending_back) {
+		for (i = 0; i < w->pending_pages; i++)
+			mm_free_page((unsigned int)w->pending_back + i * PAGE_SIZE);
+		w->pending_back = 0;
+	}
+	w->pending_pages = 0;
+	w->resize_pending = 0;
 	gfx_free(&w->canvas);
 	w->canvas_window = 0;
 }
@@ -313,6 +320,9 @@ int wm_server_win_create(const struct win_create *in, struct win_info *out)
 	w->back_pages = pages;
 	w->canvas.seq = 1;
 	w->canvas.drawn = 0;
+	w->pending_back = 0;
+	w->pending_pages = 0;
+	w->resize_pending = 0;
 	w->pw = cw + 2;
 	w->ph = ch + WM_TITLE_H + 2;
 	w->x = 24 + i * 32;
@@ -355,7 +365,8 @@ int wm_server_win_present(void)
 	if (idx < 0)
 		return -1;
 	w = &windows[idx];
-	if (!w->canvas_window || !w->back || !w->canvas.px)
+	if (!w->canvas_window || !w->back || !w->canvas.px ||
+	    w->resize_pending)
 		return -1;
 	src = (const unsigned short *)USER_GFX_BASE;
 	n = w->canvas.w * w->canvas.h;
@@ -384,6 +395,118 @@ int wm_server_win_close(void)
 	return 0;
 }
 
+int wm_server_win_info(struct win_info *out)
+{
+	int idx = wm_window_for_pid(current_proc()->pid);
+	struct window *w;
+
+	if (!out || idx < 0)
+		return -1;
+	w = &windows[idx];
+	if (!w->canvas_window)
+		return -1;
+	out->fb = (void *)USER_GFX_BASE;
+	out->width = w->canvas.w;
+	out->height = w->canvas.h;
+	out->id = (unsigned int)idx;
+	return 0;
+}
+
+int wm_server_win_ack_resize(void)
+{
+	int idx = wm_window_for_pid(current_proc()->pid);
+	struct window *w;
+	unsigned int i, old_pages;
+
+	if (idx < 0)
+		return -1;
+	w = &windows[idx];
+	if (!w->canvas_window || !w->resize_pending || !w->pending_back)
+		return -1;
+
+	old_pages = w->back_pages;
+	if (w->owner_cr3)
+		paging_release_user_range(w->owner_cr3, USER_GFX_BASE,
+					  old_pages * PAGE_SIZE, 0);
+	for (i = 0; i < w->pending_pages; i++) {
+		if (!paging_map_page(current_proc()->cr3,
+				     USER_GFX_BASE + i * PAGE_SIZE,
+				     (unsigned int)w->pending_back + i * PAGE_SIZE,
+				     PTE_USER | PTE_WRITABLE | PTE_PRESENT)) {
+			/* Put the old mapping back; this is a last-resort path. */
+			paging_release_user_range(current_proc()->cr3,
+						  USER_GFX_BASE,
+						  i * PAGE_SIZE, 0);
+			for (i = 0; i < old_pages; i++)
+				paging_map_page(current_proc()->cr3,
+					USER_GFX_BASE + i * PAGE_SIZE,
+					(unsigned int)w->back + i * PAGE_SIZE,
+					PTE_USER | PTE_WRITABLE | PTE_PRESENT);
+			for (i = 0; i < w->pending_pages; i++)
+				mm_free_page((unsigned int)w->pending_back +
+					     i * PAGE_SIZE);
+			w->pending_back = 0;
+			w->pending_pages = 0;
+			w->resize_pending = 0;
+			return -1;
+		}
+		invlpg_va(USER_GFX_BASE + i * PAGE_SIZE);
+	}
+	for (i = 0; i < old_pages; i++)
+		mm_free_page((unsigned int)w->back + i * PAGE_SIZE);
+	w->back = w->pending_back;
+	w->back_pages = w->pending_pages;
+	w->pending_back = 0;
+	w->pending_pages = 0;
+	w->resize_pending = 0;
+	return 0;
+}
+
+int wm_server_resize(unsigned int index, unsigned int cw, unsigned int ch)
+{
+	struct window *w;
+	struct gfx_canvas nf;
+	unsigned short *nb;
+	unsigned int i, pages;
+
+	if (!wm_server_is_wm() || index >= MAX_WINDOWS || !windows[index].used)
+		return -1;
+	w = &windows[index];
+	if (!w->canvas_window || !process_is_alive(w->pid))
+		return -1;
+	if (cw < 160 || ch < 80 || cw > 1024 || ch > 1024)
+		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)
+		return -1;
+	pages = (cw * ch * 2 + PAGE_SIZE - 1) / PAGE_SIZE;
+	nb = (unsigned short *)mm_alloc_pages(pages);
+	if (!nb) {
+		gfx_free(&nf);
+		return -1;
+	}
+	for (i = 0; i < cw * ch; i++)
+		nb[i] = 0;
+
+	wm_server_unmap_index(index);
+	gfx_free(&w->canvas);
+	if (w->pending_back) {
+		for (i = 0; i < w->pending_pages; i++)
+			mm_free_page((unsigned int)w->pending_back + i * PAGE_SIZE);
+	}
+	w->canvas = nf;
+	w->canvas.seq = 1;
+	w->canvas.drawn = 0;
+	w->pending_back = nb;
+	w->pending_pages = pages;
+	w->resize_pending = 1;
+	w->pw = cw + 2;
+	w->ph = ch + WM_TITLE_H + 2;
+	w->content_gen++;
+	return 0;
+}
+
 int wm_server_key_poll(void)
 {
 	int idx = wm_window_for_pid(current_proc()->pid);

+ 6 - 0
wm.h

@@ -21,6 +21,9 @@ struct window {
 	struct gfx_canvas canvas;
 	unsigned short *back;
 	unsigned int back_pages;
+	unsigned short *pending_back;   /* new canvas waiting for the app ACK */
+	unsigned int pending_pages;
+	int resize_pending;
 	unsigned int owner_cr3;
 	int keys[16];
 	unsigned int key_head, key_tail;
@@ -63,6 +66,9 @@ int  wm_server_sync(void);
 int  wm_server_win_create(const struct win_create *in, struct win_info *out);
 int  wm_server_win_present(void);
 int  wm_server_win_close(void);
+int  wm_server_win_info(struct win_info *out);
+int  wm_server_win_ack_resize(void);
+int  wm_server_resize(unsigned int index, unsigned int cw, unsigned int ch);
 int  wm_server_key_poll(void);
 int  wm_server_key_wait(unsigned int timeout_ms);
 void wm_content_changed(struct text_screen *screen);

+ 4 - 0
wm_abi.h

@@ -91,6 +91,10 @@ struct wm_geom {
 	int x, y;
 };
 
+struct wm_size {
+	unsigned int w, h;
+};
+
 /* Generic canvas-window client API (gfx commands own their own window). */
 struct win_create {
 	unsigned int w, h;