diff options
Diffstat (limited to 'src/gui/gui.zig')
| -rw-r--r-- | src/gui/gui.zig | 225 |
1 files changed, 216 insertions, 9 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index a1590b48..2d9124cc 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -1550,6 +1550,68 @@ const Gui = struct { deck: deck.Deck = .{}, }; +/// The Gui's fields that are each window's own, one a view (views.zig): +/// the window and its presenting, its scroll and glide, its scene and post +/// chain, its pointer, its blink, its images as presented, its capture. The +/// device, the atlas, the font and the pipelines are every window's. The +/// Gui holds the live window's; the others wait in `Shell.windows`. +const window_fields = [_][:0]const u8{ + "window", "soft_renderer", "soft_texture", "soft_tex_w", "soft_tex_h", + "window_w", "window_h", "scroll_pane", "scroll_rect", "scroll_body_y", + "scroll_body_h", "scroll_held_px", "scroll_col", "scroll_row", "scroll_delta", + "scroll_lag", "scroll_edge", "scroll_edge_len", "scroll_notch", "program_wheel", + "glide", "pace_ns", "sel_glow", "hover_glow", "frame_arrival_ns", + "blink_due", "cursor_quad_alpha", "scene_tex", "scene_tex_w", "scene_tex_h", + "scene_failures", "scene_target_failed", "post", "pointer_present", "pointer_shape", + "pointer_mapped", "pointer_cell", "presented_images", "prepared_images", "touch", + "live_ctrl", "live_alt", "applied_window_opacity", "capture_tex", "capture_tex_w", + "capture_tex_h", "capture_xfer", "capture_xfer_size", "captured", "capture_dir", +}; + +const GuiWindow = blk: { + @setEvalBranchQuota(100_000); + const st = @typeInfo(Gui).@"struct"; + var types: [window_fields.len]type = undefined; + var attrs: [window_fields.len]std.lang.Type.Struct.FieldAttributes = undefined; + for (window_fields, 0..) |name, i| { + const at = for (st.field_names, 0..) |n, j| { + if (std.mem.eql(u8, n, name)) break j; + } else @compileError("gui: Gui has no field " ++ name); + types[i] = st.field_types[at]; + attrs[i] = .{ .default_value_ptr = st.field_attrs[at].default_value_ptr }; + } + break :blk @Struct(.auto, null, &window_fields, &types, &attrs); +}; + +fn copyWindow(dst: anytype, src: anytype) void { + inline for (window_fields) |name| @field(dst, name) = @field(src, name); +} + +/// The live window's share of `g` as a window starts, but its SDL window +/// (reopenWindow makes that). +fn freshWindow(g: *Gui) void { + @setEvalBranchQuota(100_000); + const st = @typeInfo(Gui).@"struct"; + inline for (window_fields) |name| { + const at = comptime for (st.field_names, 0..) |n, j| { + if (std.mem.eql(u8, n, name)) break j; + } else unreachable; + if (comptime st.field_attrs[at].defaultValue(st.field_types[at])) |d| @field(g, name) = d; + } +} + +/// What a window held of its own let go (closeWindow lets the window go). +fn releaseWindowParts(g: *Gui, gpa: std.mem.Allocator) void { + if (g.scene_tex) |texture| c.SDL_ReleaseGPUTexture(g.device, texture); + if (g.capture_tex) |texture| c.SDL_ReleaseGPUTexture(g.device, texture); + if (g.capture_xfer) |transfer| c.SDL_ReleaseGPUTransferBuffer(g.device, transfer); + g.post.deinit(gpa, g.device); + g.glide.deinit(gpa); + gpa.free(g.scroll_edge); + g.presented_images.deinit(gpa); + g.prepared_images.deinit(gpa); +} + fn setGuiFontName(g: *Gui, fallback: []const u8) void { const name = if (c.ui_font_name(g.font)) |name_z| std.mem.span(name_z) else fallback; const len = @min(name.len, g.font_name.len); @@ -2247,7 +2309,9 @@ fn closeWindow(g: *Gui) void { g.soft_renderer = null; } else c.SDL_ReleaseWindowFromGPUDevice(g.device, g.window); c.SDL_DestroyWindow(g.window); - if (g.window_slot) |slot| slot.* = null; + if (g.window_slot) |slot| if (slot.* == g.window) { + slot.* = null; + }; } /// `pardes-gui --attach`: a window again, as the first was made, at the @@ -2399,6 +2463,14 @@ fn localSession( if (fs) |f| try f.watch(&queue, wakeFs); _ = c.SDL_StartTextInput(g.window); + // PARDES_TEST_VIEWS=N: N more windows from the start, each a view of the + // session (views.zig), captured under view-<k>; view 0 live after. + if (test_mode) if (std.c.getenv("PARDES_TEST_VIEWS")) |n| { + for (0..std.fmt.parseInt(u8, std.mem.span(n), 10) catch 0) |_| shell.openAnotherWindow(); + core.toView(0); + shell.follow(); + }; + defer shell.closeOtherWindows(); if (test_mode) setStdinRaw() catch {}; shell.threads_ok = true; @@ -2431,6 +2503,10 @@ fn localSession( }; pipe_tasks.cancelAll(io); queue.discardCompletions(); + // ponytail: a restored session is one view, the live window's: + // the others close. + shell.closeOtherWindows(); + shell.gview = 0; shell.stopPtys(); for (0..pardes.MAX_PANES) |wid| file_watch.watchPane( inotify_fd, @@ -2635,6 +2711,24 @@ const StdinFeed = struct { // A mouse event at window pixels, built as SDL would and run // through dispatch, so a driver exercises the real pointer path // (pixel to cell, tag and body hits). SGR mouse carries only cells. + // ESC]777;view;<k>BEL: what follows is window k's (k from 1), + // as an event of that window's makes its view live (route). + const view_prefix = "\x1b]777;view;"; + if (std.mem.startsWith(u8, f.buf[seq_start..len], view_prefix)) { + const body = f.buf[seq_start + view_prefix.len .. len]; + const end = std.mem.indexOfScalar(u8, body, 0x07) orelse { + std.mem.copyForwards(u8, f.buf[0 .. len - seq_start], f.buf[seq_start..len]); + f.fill = len - seq_start; + break; + }; + const k = std.fmt.parseInt(u8, body[0..end], 10) catch 0; + if (in.shell) |sh| if (k >= 1 and pardes.views.isOpen(sh.core, k - 1)) { + sh.core.toView(k - 1); + sh.follow(); + }; + seq_start += view_prefix.len + end + 1; + continue; + } const mouse_prefix = "\x1b]777;mouse;"; if (std.mem.startsWith(u8, f.buf[seq_start..len], mouse_prefix)) { const body = f.buf[seq_start + mouse_prefix.len .. len]; @@ -2975,6 +3069,9 @@ test "the window's function and editing keys reach a terminal as its cursor keys const Input = struct { core: ?*pardes.Pardes = null, + /// The shell whose windows these are: a window closed while others + /// stay takes its view with it (views.zig). + shell: ?*Shell = null, quit: bool = false, fn post(in: *Input, ev: pardes.Event) void { @@ -2982,6 +3079,7 @@ const Input = struct { } fn close(in: *Input) void { + if (in.shell) |s| if (pardes.views.count(s.core) > 1) return s.closeView(s.core.view); if (in.core) |core| core.quit = true; in.quit = true; } @@ -3018,7 +3116,11 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { else => {}, } switch (sev.type) { - c.SDL_EVENT_QUIT, c.SDL_EVENT_WINDOW_CLOSE_REQUESTED => in.close(), + c.SDL_EVENT_QUIT => { + in.shell = null; + in.close(); + }, + c.SDL_EVENT_WINDOW_CLOSE_REQUESTED => in.close(), c.SDL_EVENT_WINDOW_FOCUS_GAINED => in.post(.{ .focus = true }), c.SDL_EVENT_WINDOW_FOCUS_LOST => in.post(.{ .focus = false }), c.SDL_EVENT_WINDOW_MOUSE_LEAVE => { @@ -3677,6 +3779,10 @@ const Shell = struct { /// The tick a paced frame is sampled at (waitInput): the clock the /// core is advanced to while anything moves. paced_ns: ?u64 = null, + /// Each other view's window, while another's is in the Gui (views.zig), + /// and the view whose window the Gui holds (`follow`). + windows: [pardes.views.max]GuiWindow = undefined, + gview: u8 = 0, fn reap(s: *Shell) void { const now = std.Io.Clock.awake.now(s.queue.io).toMilliseconds(); @@ -3766,10 +3872,92 @@ const Shell = struct { fn detachWindow(ctx: ?*anyopaque) void { const s = shellOf(ctx); if (s.gui == null) return s.core.reportFailure(s.core.active, "Detach: no window is open"); + // One of several: its window goes, and the session stays in the rest. + if (pardes.views.count(s.core) > 1) return s.closeView(s.core.view); if (!daemon.kept()) return s.core.reportFailure(s.core.active, "Detach: no pardes daemon keeps this session; closing its window ends it"); s.closeTheWindow(); } + /// The Gui holds the window of the core's live view. + fn follow(s: *Shell) void { + const g = s.gui orelse return; + const k = s.core.view; + if (s.gview == k) return; + if (s.gview < s.windows.len) copyWindow(&s.windows[s.gview], g); + copyWindow(g, &s.windows[k]); + s.gview = k; + } + + /// An event of another view's window: that view goes live. + fn route(s: *Shell, sev: *const c.SDL_Event) void { + if (!pardes.views.several(s.core)) return; + const g = s.gui orelse return; + const w = c.SDL_GetWindowFromEvent(sev) orelse return; + if (w == g.window) return; + for (s.windows, 0..) |win, k| if (k != s.gview and pardes.views.isOpen(s.core, @intCast(k)) and win.window == w) { + s.core.toView(@intCast(k)); + return s.follow(); + }; + } + + /// `pardes-gui --attach` while a window is open: another window, a view + /// of its own (views.zig), opened as the first was. + fn openAnotherWindow(s: *Shell) void { + const g = s.gui orelse return; + s.follow(); + const k = s.core.openView() orelse return log.err("--attach: this session has as many windows as it takes", .{}); + const live = s.gview; + copyWindow(&s.windows[live], g); + freshWindow(g); + if (!reopenWindow(g)) { + log.err("--attach: no window: {s}", .{c.SDL_GetError()}); + copyWindow(g, &s.windows[live]); + return s.core.closeView(k); + } + if (s.test_mode) { + // Its own grid, its captures under view-<k> (PARDES_TEST_VIEWS). + // ponytail: the path is kept for the session, a few bytes. + const cols: u32 = if (std.c.getenv("PARDES_TEST_COLS")) |v| std.fmt.parseInt(u16, std.mem.span(v), 10) catch 80 else 80; + const rows: u32 = if (std.c.getenv("PARDES_TEST_ROWS")) |v| std.fmt.parseInt(u16, std.mem.span(v), 10) catch 24 else 24; + _ = c.SDL_SetWindowSize(g.window, @intCast(cols * g.cell_w + g.capture_pad), @intCast(rows * g.cell_h + g.capture_pad)); + _ = c.SDL_SyncWindow(g.window); + g.capture = true; + g.capture_dir = std.fmt.allocPrint(s.gpa, "{s}/view-{d}", .{ s.windows[live].capture_dir, k }) catch ""; + if (g.capture_dir.len > 0) std.Io.Dir.cwd().createDirPath(s.io, g.capture_dir) catch {}; + } + s.gview = k; + s.core.toView(k); + s.core.needs_frame = true; + } + + /// View `k`'s window closed while others stay: the view goes with it. + fn closeView(s: *Shell, k: u8) void { + const g = s.gui orelse return; + s.core.toView(k); + s.follow(); + closeWindow(g); + releaseWindowParts(g, s.gpa); + s.gview = std.math.maxInt(u8); + s.core.closeView(k); + s.follow(); + s.core.needs_frame = true; + } + + /// The session's end: every window but the live one closed. + fn closeOtherWindows(s: *Shell) void { + const g = s.gui orelse return; + for (0..s.windows.len) |i| { + const k: u8 = @intCast(i); + if (k == s.gview or !pardes.views.isOpen(s.core, k)) continue; + var live: GuiWindow = undefined; + copyWindow(&live, g); + copyWindow(g, &s.windows[k]); + closeWindow(g); + releaseWindowParts(g, s.gpa); + copyWindow(g, &live); + } + } + fn closeTheWindow(s: *Shell) void { const g = s.gui orelse return; closeWindow(g); @@ -3975,7 +4163,7 @@ const Shell = struct { .files_changed => check_files = true, .fs_ready => {}, .window => { - s.openTheWindow(); + if (s.gui != null) s.openAnotherWindow() else s.openTheWindow(); s.saw_event = true; }, .adopted => |a| { @@ -4008,7 +4196,8 @@ fn shellOf(ctx: ?*anyopaque) *Shell { fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { const s = shellOf(ctx); const core = s.core; - var in: Input = .{ .core = core }; + var in: Input = .{ .core = core, .shell = s }; + s.follow(); if (s.gui) |g| { var sev = std.mem.zeroes(c.SDL_Event); // Idle is asleep until an event: SDL's own, and the user events the @@ -4100,16 +4289,27 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { // each would repeat hover and selection work the next undoes. var motion: ?c.SDL_Event = null; while (true) { - if (sev.type == c.SDL_EVENT_MOUSE_MOTION) { + if (sev.type == c.SDL_EVENT_MOUSE_MOTION and (motion == null or motion.?.motion.windowID == sev.motion.windowID)) { motion = sev; } else { - if (motion) |*m| dispatch(g, &in, m); + if (motion) |*m| { + s.route(m); + dispatch(g, &in, m); + } motion = null; - dispatch(g, &in, &sev); + if (sev.type == c.SDL_EVENT_MOUSE_MOTION) { + motion = sev; + } else { + s.route(&sev); + dispatch(g, &in, &sev); + } } if (!c.SDL_PollEvent(&sev)) break; } - if (motion) |*m| dispatch(g, &in, m); + if (motion) |*m| { + s.route(m); + dispatch(g, &in, m); + } } runDemo(g, &in, core); // Paced: what came before the tick is in; wait out the rest of it, @@ -4133,8 +4333,12 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { const more = c.SDL_PollEvent(&sev); pardes.turn.wake(); if (more) { + s.route(&sev); dispatch(g, &in, &sev); - while (c.SDL_PollEvent(&sev)) dispatch(g, &in, &sev); + while (c.SDL_PollEvent(&sev)) { + s.route(&sev); + dispatch(g, &in, &sev); + } } else { // The events asked for every 100 µs, the core busy between. const until = c.SDL_GetTicksNS() + 100 * std.time.ns_per_us; @@ -4182,6 +4386,7 @@ fn waitDetached(core: *pardes.Pardes, timeout_ms: u32) void { fn pollFrame(ctx: ?*anyopaque) void { const s = shellOf(ctx); + s.follow(); s.reconcilePtys(); const core = s.core; if (s.fs) |f| if (f.tick().pending) s.queue.push(.fs_ready); @@ -4414,6 +4619,7 @@ test "background opacity acknowledges success and rolls back unsupported surface fn present(ctx: ?*anyopaque, surface: *const pardes.Surface) void { const s = shellOf(ctx); const core = s.core; + s.follow(); const g = s.gui orelse return; frame_started_ns = monoNs(); const frame = @constCast(surface); @@ -4447,6 +4653,7 @@ fn present(ctx: ?*anyopaque, surface: *const pardes.Surface) void { fn postPresent(ctx: ?*anyopaque) void { const s = shellOf(ctx); + s.follow(); const g = s.gui orelse return; // No image (a minimized window) shows no tracks; time still passes and // the pump advances it all the same. |
