Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

editor: windows are Clients on one shared connection (stage 1)

Claude5c44867b65b6
diff --git a/docs/plans/editor-multiwindow.md b/docs/plans/editor-multiwindow.md
new file mode 100644
index 0000000..e01102f
--- /dev/null
+++ b/docs/plans/editor-multiwindow.md
@@ -0,0 +1,50 @@
+# Plan: rediwm-editor with several windows in one process
+
+Why: tearing a tab off into a window and merging it into another window (see
+`docs/plans/titlebar-tabs.md`, phase 6) needs two editor windows that belong to
+one Wayland client, because `drop_on` only names strips of the same client and
+the compositor places a torn-off window by client.
+
+## Shape
+
+Every window is a `Client` (as before) with its own registry bindings, seat
+objects, surface and `App`. What changes is only that they share one
+`wl_display` connection, so the compositor sees a single client.
+
+* The seat reports every surface of the connection to every `wl_pointer`,
+ `wl_keyboard` and `text_input` of it. Each `Client` keeps only its own:
+ enter and leave are filtered by surface, motion/button/axis by `inside`, keys
+ by `app.focused`. Keymap, modifiers and repeat info go to all (each window
+ has its own xkb state).
+* Each window has its own clipboard device and IME; the selection reaches all of them.
+* `main.run` owns the loop: a list of windows, one poll over the display, the
+ instance socket, the drafts wake-up and each window's own file descriptors.
+ A window that closes is destroyed; the process ends with the last one.
+* Window creation, one loop step and teardown live in `client_lifecycle.zig`.
+* The first window owns the drafts, the instance socket's requests (they go to
+ the focused window) and the activation token.
+
+## Stages
+
+1. **One window, new structure** (this change): no behaviour change. Gate: the
+ editor builds, runs, edits, saves, restores drafts and quits as before.
+2. **Drafts across windows**: the keeper snapshots the tabs of every window, not
+ one `App`; ownership moves to the next window when the first closes.
+3. **More than one window**: a New Window action (Ctrl+Shift+N and a menu row)
+ opens a window with one empty tab; closing the last tab closes that window;
+ quitting the last window quits the process. Tab ids are per `App`, so a tab
+ that moves gets a fresh id in its new window.
+4. **Tab gestures** (titlebar tabs phase 6c): `tear_off` creates a window holding
+ the tab, `drop_on` moves it to the other window; `take`/`adopt` on `App` move
+ the `Tab` with its document and undo history.
+
+## Risks
+
+* A second `wl_seat` binding per window is allowed, but the compositor sends
+ pointer events to every pointer resource of the client: the filters above are
+ what keeps a click from landing in two windows. Test by clicking in window
+ B while window A is open.
+* `roundtrip` during window creation runs the other windows' listeners
+ re-entrantly; they only update state, so this should be safe.
+* Drafts (stage 2) are the real risk: a snapshot that ignores another window's
+ tabs would delete their drafts.
diff --git a/src/editor/client_input.zig b/src/editor/client_input.zig
index ef04cd5..63762ed 100644
--- a/src/editor/client_input.zig
+++ b/src/editor/client_input.zig
@@ -15,6 +15,9 @@ const close = Client.close;
pub fn pointerEvent(_: *wl.Pointer, event: wl.Pointer.Event, self: *Client) void {
switch (event) {
.enter => |ev| {
+ // The seat reports every surface of this connection; only this
+ // window's own count (another window of the process has its own client).
+ if (ev.surface != self.surface) return;
self.cursor_serial = ev.serial;
self.input_serial = ev.serial;
self.inside = true;
@@ -24,20 +27,24 @@ pub fn pointerEvent(_: *wl.Pointer, event: wl.Pointer.Event, self: *Client) void
self.cursor_name = "";
self.handlePointerMotion(ev.surface_x.toDouble(), ev.surface_y.toDouble());
},
- .leave => {
+ .leave => |ev| {
+ if (ev.surface != self.surface) return;
self.inside = false;
self.app.cancelDrag();
self.app.handleMotion(-1, -1);
self.app.dirty = true;
},
.motion => |ev| {
+ if (!self.inside) return;
self.handlePointerMotion(ev.surface_x.toDouble(), ev.surface_y.toDouble());
},
.button => |ev| {
+ if (!self.inside) return;
self.input_serial = ev.serial;
self.handlePointerButton(ev.button, ev.state == .pressed);
},
.axis => |ev| {
+ if (!self.inside) return;
if (ev.axis == .vertical_scroll) {
self.app.handleScroll(ev.value.toDouble());
}
@@ -202,11 +209,13 @@ pub fn repeatDue(self: *Client) void {
pub fn keyboardEvent(_: *wl.Keyboard, event: wl.Keyboard.Event, self: *Client) void {
switch (event) {
- .enter => {
+ .enter => |ev| {
+ if (ev.surface != self.surface) return;
self.app.focused = true;
self.app.dirty = true;
},
- .leave => {
+ .leave => |ev| {
+ if (ev.surface != self.surface) return;
self.app.focused = false;
self.app.dismissMenu();
self.compose.reset();
@@ -250,6 +259,8 @@ pub fn keyboardEvent(_: *wl.Keyboard, event: wl.Keyboard.Event, self: *Client) v
}
},
.key => |ev| {
+ // Keys arrive at every window's keyboard object; the focused one takes them.
+ if (!self.app.focused) return;
self.input_serial = ev.serial;
if (ev.state == .pressed) {
const state = self.xkb_state orelse return;
diff --git a/src/editor/client_lifecycle.zig b/src/editor/client_lifecycle.zig
new file mode 100644
index 0000000..a3fc384
--- /dev/null
+++ b/src/editor/client_lifecycle.zig
@@ -0,0 +1,155 @@
+//! A window's life on the process's one Wayland connection: creating it, one
+//! step of its loop, and taking it down. Every window is a `Client` with its
+//! own registry bindings, surface and `App`; they share the display only.
+
+const std = @import("std");
+const wl = @import("wayland").client.wl;
+const ClientCursor = @import("ui").client_cursor;
+const key_repeat = @import("ui").key_repeat;
+const c = @import("../files/c.zig").api;
+const app_mod = @import("app.zig");
+const client_tabs = @import("client_tabs.zig");
+const main_mod = @import("main.zig");
+const Client = main_mod.Client;
+const OutputInfo = main_mod.OutputInfo;
+const Setup = main_mod.Setup;
+const a = std.heap.c_allocator;
+
+/// Opens a window. The primary one owns the drafts. A failed creation leaks
+/// what it had made: the first window failing ends the process, and a later
+/// one failing is rare enough to cost a few proxies.
+pub fn create(display: *wl.Display, setup: Setup, primary: bool, paths: []const [:0]const u8) !*Client {
+ const self = try a.create(Client);
+ errdefer a.destroy(self);
+ self.* = .{ .display = display, .app = try app_mod.App.init(setup.io) };
+ self.app.environ = setup.environ;
+ if (primary) self.app.openDrafts(setup.environ);
+ for (paths) |path| self.app.open(path);
+ self.ime = .{ .app = &self.app };
+ self.locale = @import("ui").compose.localeFromEnviron(setup.environ);
+ self.clipboard = .{ .app = &self.app };
+
+ const registry = try display.getRegistry();
+ self.registry = registry;
+ registry.setListener(*Client, Client.registryEvent, self);
+
+ if (display.roundtrip() != .SUCCESS) return error.DisplayFailed;
+ if (self.compositor == null or self.shm == null or self.wm_base == null) {
+ return error.MissingWaylandGlobals;
+ }
+
+ if (self.output_manager) |manager| {
+ for (self.outputs.items) |info| {
+ info.logical = try manager.getXdgOutput(info.proxy);
+ info.logical.?.setListener(*OutputInfo, Client.logicalOutputEvent, info);
+ }
+ if (display.roundtrip() != .SUCCESS) return error.DisplayFailed;
+ }
+
+ if (self.seat) |seat| {
+ try self.clipboard.setup(seat);
+ try self.ime.setup(seat);
+ }
+
+ self.surface = try self.compositor.?.createSurface();
+ self.surface.setListener(*Client, Client.surfaceEvent, self);
+ self.ime.surface = self.surface;
+
+ self.cursor_surface = try self.compositor.?.createSurface();
+ if (self.cursor_shape.manager == null) self.cursor_theme = ClientCursor.loadFallback(self.shm.?);
+
+ // Before the shell role, like the decoration: the compositor adopts the strip
+ // when the toplevel appears. It stays hidden until `client_tabs.sync` commits.
+ client_tabs.attach(self);
+
+ self.xdg_surface = try self.wm_base.?.getXdgSurface(self.surface);
+ self.xdg_surface.setListener(*Client, Client.xdgSurfaceEvent, self);
+
+ self.xdg_toplevel = try self.xdg_surface.getToplevel();
+ self.xdg_toplevel.setListener(*Client, Client.xdgToplevelEvent, self);
+
+ self.xdg_toplevel.setAppId("rediwm-editor");
+ self.xdg_toplevel.setTitle(self.app.getTitle());
+ // Real min size is set from xdgToplevelEvent once the decoration mode
+ // (and thus titlebar height) is known; this is just a safe pre-configure
+ // default sized for the worst case (self-decorated).
+ self.xdg_toplevel.setMinSize(520, 240 + self.titlebarHeight());
+
+ // Prefer the shared compositor chrome. The app keeps its menu in the tab
+ // bar with SSD, or in its fallback header when it must self-decorate.
+ if (self.decoration_manager) |mgr| {
+ if (!setup.force_csd) {
+ self.decoration = mgr.getToplevelDecoration(self.xdg_toplevel) catch null;
+ if (self.decoration) |dec| {
+ dec.setListener(*Client, Client.decorationEvent, self);
+ dec.setMode(.server_side);
+ }
+ }
+ }
+
+ // Initial commit to start xdg configuration sequence
+ self.surface.commit();
+ return self;
+}
+
+/// One turn of this window's part of the loop: tabs, closing, the bars, the
+/// caret and a frame. A window that is closing ends with `running` false.
+pub fn step(self: *Client) !void {
+ client_tabs.sync(self);
+ if (self.app.closed) {
+ self.close();
+ return;
+ }
+ self.clipboard.sync(self.input_serial);
+ self.app.stepBar(key_repeat.nowMs());
+ if (!self.app.closed) self.ime.sync(self.titlebarHeight());
+ if (self.app.closed) self.close();
+ if (!self.running) return;
+
+ if (self.app.title_changed) {
+ self.xdg_toplevel.setTitle(self.app.getTitle());
+ self.app.title_changed = false;
+ }
+
+ const now = key_repeat.nowMs();
+ const caret_state = self.app.caretState();
+ const restart = self.caret_state == null or !std.meta.eql(self.caret_state.?, caret_state);
+ const snap = if (self.caret_state) |before| !before.active or before.tab != caret_state.tab or before.find != caret_state.find or
+ before.top != caret_state.top or before.dy != caret_state.dy or before.scroll_x != caret_state.scroll_x or
+ before.width != caret_state.width or before.height != caret_state.height or before.scale != caret_state.scale else true;
+ self.caret_state = caret_state;
+ self.app.stepReflow(now);
+ self.app.stepCaret(now, caret_state.active, restart, snap);
+ try self.render();
+}
+
+/// Takes the window down and frees it. Its `App` writes the drafts if it owns them.
+pub fn destroy(self: *Client) void {
+ for (self.outputs.items) |info| {
+ if (info.logical) |logical| logical.destroy();
+ info.proxy.destroy();
+ a.destroy(info);
+ }
+ self.outputs.deinit(a);
+ self.entered_outputs.deinit(a);
+ if (self.xkb_state) |state| c.xkb_state_unref(state);
+ if (self.xkb_context) |context| c.xkb_context_unref(context);
+
+ if (self.decoration) |dec| dec.destroy();
+ self.xdg_toplevel.destroy();
+ self.xdg_surface.destroy();
+ self.tabs.deinit();
+ if (self.cursor_theme) |theme_ptr| theme_ptr.destroy();
+ self.cursor_shape.deinit();
+ self.cursor_surface.destroy();
+ self.surface.destroy();
+ if (self.registry) |registry| registry.destroy();
+ for (self.buffers) |slot| {
+ if (slot) |buffer| buffer.destroy();
+ }
+ self.app.deinit();
+ self.clipboard.deinit();
+ self.compose.deinit();
+ self.ime.deinit();
+ a.destroy(self);
+}
diff --git a/src/editor/ime.zig b/src/editor/ime.zig
index 9e0c5e4..10ea603 100644
--- a/src/editor/ime.zig
+++ b/src/editor/ime.zig
@@ -6,6 +6,9 @@ const App = @import("app.zig").App;
const a = std.heap.c_allocator;
pub const Ime = struct {
app: *App,
+ /// The window's surface: with several windows on one connection the seat's
+ /// enter and leave events name the surface, and only its own count.
+ surface: ?*wl.Surface = null,
manager: ?*zwp.TextInputManagerV3 = null,
input: ?*zwp.TextInputV3 = null,
entered: bool = false,
@@ -46,11 +49,13 @@ pub const Ime = struct {
}
fn event(input: *zwp.TextInputV3, ev: zwp.TextInputV3.Event, self: *Ime) void {
switch (ev) {
- .enter => {
+ .enter => |ev| {
+ if (ev.surface != self.surface) return;
self.entered = true;
self.revision = std.math.maxInt(usize);
},
- .leave => {
+ .leave => |ev| {
+ if (ev.surface != self.surface) return;
// enable belongs to the surface from enter: disable it before
// dropping that surface, or the compositor still believes the
// input is enabled and a later enable/commit after re-entering
diff --git a/src/editor/main.zig b/src/editor/main.zig
index 9e96cf9..ba8c1b1 100644
--- a/src/editor/main.zig
+++ b/src/editor/main.zig
@@ -17,6 +17,7 @@ const IconId = @import("ui").layout.IconId;
const client_window = @import("client_window.zig");
const client_input = @import("client_input.zig");
const client_render = @import("client_render.zig");
+const client_lifecycle = @import("client_lifecycle.zig");
const a = std.heap.c_allocator;
// The fallback client-side header owns the menu and window controls.
@@ -36,6 +37,7 @@ pub const OutputInfo = struct {
pub const Client = struct {
display: *wl.Display,
+ registry: ?*wl.Registry = null,
compositor: ?*wl.Compositor = null,
shm: ?*wl.Shm = null,
output_manager: ?*zxdg.OutputManagerV1 = null,
@@ -85,6 +87,9 @@ pub const Client = struct {
/// LC_CTYPE of the session; picks the Compose table (´+c is ç in pt_BR).
locale: []const u8 = "",
+ pub const create = client_lifecycle.create;
+ pub const step = client_lifecycle.step;
+ pub const destroy = client_lifecycle.destroy;
pub const registryEvent = client_window.registryEvent;
pub const close = client_window.close;
const logicalOutputEvent = client_window.logicalOutputEvent;
@@ -181,6 +186,9 @@ pub const Buffer = struct {
}
};
+/// What every window of the process shares when it is created.
+pub const Setup = struct { io: std.Io, environ: std.process.Environ, force_csd: bool };
+
pub fn run(init: std.process.Init, paths: []const [:0]const u8, instance: *@import("instance.zig").Instance, initial_token: []const u8) !void {
platform.process.ignoreBrokenPipes();
const force_csd = init.minimal.environ.getPosix("REDIWM_EDITOR_FORCE_CSD") != null;
@@ -197,137 +205,51 @@ pub fn run(init: std.process.Init, paths: []const [:0]const u8, instance: *@impo
};
@import("ui").anim.applySettings(cfg.animations);
} else |_| {}
+ // One connection for every window: the compositor sees one client, which
+ // is what lets a tab move from one of its windows to another.
const display = try wl.Display.connect(null);
defer display.disconnect();
- var self = Client{ .display = display, .app = try app_mod.App.init(init.io) };
- self.app.environ = init.minimal.environ;
- self.app.openDrafts(init.minimal.environ);
- for (paths) |path| self.app.open(path);
- self.ime = .{ .app = &self.app };
- defer self.ime.deinit();
- self.locale = @import("ui").compose.localeFromEnviron(init.minimal.environ);
- defer self.compose.deinit();
- self.clipboard = .{ .app = &self.app };
- defer self.clipboard.deinit();
- defer self.app.deinit();
- defer for (self.buffers) |slot| {
- if (slot) |buffer| buffer.destroy();
- };
-
- const registry = try display.getRegistry();
- defer registry.destroy();
- registry.setListener(*Client, Client.registryEvent, &self);
-
- if (display.roundtrip() != .SUCCESS) return error.DisplayFailed;
- if (self.compositor == null or self.shm == null or self.wm_base == null) {
- return error.MissingWaylandGlobals;
- }
-
- if (self.output_manager) |manager| {
- for (self.outputs.items) |info| {
- info.logical = try manager.getXdgOutput(info.proxy);
- info.logical.?.setListener(*OutputInfo, Client.logicalOutputEvent, info);
- }
- if (display.roundtrip() != .SUCCESS) return error.DisplayFailed;
- }
-
- if (self.seat) |seat| {
- try self.clipboard.setup(seat);
- try self.ime.setup(seat);
- }
+ const setup: Setup = .{ .io = init.io, .environ = init.minimal.environ, .force_csd = force_csd };
- self.surface = try self.compositor.?.createSurface();
- defer self.surface.destroy();
- self.surface.setListener(*Client, Client.surfaceEvent, &self);
-
- self.cursor_surface = try self.compositor.?.createSurface();
- defer self.cursor_surface.destroy();
- defer self.cursor_shape.deinit();
- if (self.cursor_shape.manager == null) self.cursor_theme = ClientCursor.loadFallback(self.shm.?);
- defer if (self.cursor_theme) |theme_ptr| theme_ptr.destroy();
-
- // Before the shell role, like the decoration: the compositor adopts the strip
- // when the toplevel appears. It stays hidden until `client_tabs.sync` commits.
- client_tabs.attach(&self);
- defer self.tabs.deinit();
-
- self.xdg_surface = try self.wm_base.?.getXdgSurface(self.surface);
- defer self.xdg_surface.destroy();
- self.xdg_surface.setListener(*Client, Client.xdgSurfaceEvent, &self);
-
- self.xdg_toplevel = try self.xdg_surface.getToplevel();
- defer self.xdg_toplevel.destroy();
- self.xdg_toplevel.setListener(*Client, Client.xdgToplevelEvent, &self);
-
- self.xdg_toplevel.setAppId("rediwm-editor");
- self.xdg_toplevel.setTitle(self.app.getTitle());
- // Real min size is set from xdgToplevelEvent once the decoration mode
- // (and thus titlebar height) is known; this is just a safe pre-configure
- // default sized for the worst case (self-decorated).
- self.xdg_toplevel.setMinSize(520, 240 + self.titlebarHeight());
-
- // Prefer the shared compositor chrome. The app keeps its menu in the tab
- // bar with SSD, or in its fallback header when it must self-decorate.
- if (self.decoration_manager) |mgr| {
- if (!force_csd) {
- self.decoration = mgr.getToplevelDecoration(self.xdg_toplevel) catch null;
- if (self.decoration) |dec| {
- dec.setListener(*Client, Client.decorationEvent, &self);
- dec.setMode(.server_side);
- }
- }
+ var windows: std.ArrayList(*Client) = .empty;
+ defer {
+ for (windows.items) |window| window.destroy();
+ windows.deinit(a);
}
- defer if (self.decoration) |dec| dec.destroy();
-
- // Initial commit to start xdg configuration sequence
- self.surface.commit();
+ // The first window owns the drafts; it is also where the instance socket's
+ // requests and the activation token go.
+ try windows.append(a, try Client.create(display, setup, true, paths));
var pending_token = try a.dupeSentinel(u8, initial_token, 0);
defer a.free(pending_token);
- while (self.running) {
- self.app.sync();
- client_tabs.sync(&self);
- self.app.stepDrafts(key_repeat.nowMs());
+ while (windows.items.len > 0) {
+ for (windows.items) |window| window.app.sync();
+ windows.items[0].app.stepDrafts(key_repeat.nowMs());
// The session is ending: leave without asking; `deinit` writes the drafts.
- if (self.app.signaled()) self.app.closed = true;
- if (self.app.closed) {
- self.close();
- break;
- }
- self.clipboard.sync(self.input_serial);
- self.app.stepBar(key_repeat.nowMs());
- if (!self.app.closed) self.ime.sync(self.titlebarHeight());
- if (self.app.closed) self.close();
- if (!self.running) break;
-
- if (self.app.title_changed) {
- self.xdg_toplevel.setTitle(self.app.getTitle());
- self.app.title_changed = false;
- }
-
- const now = key_repeat.nowMs();
- const caret_state = self.app.caretState();
- const restart = self.caret_state == null or !std.meta.eql(self.caret_state.?, caret_state);
- const snap = if (self.caret_state) |before| !before.active or before.tab != caret_state.tab or before.find != caret_state.find or
- before.top != caret_state.top or before.dy != caret_state.dy or before.scroll_x != caret_state.scroll_x or
- before.width != caret_state.width or before.height != caret_state.height or before.scale != caret_state.scale else true;
- self.caret_state = caret_state;
- self.app.stepReflow(now);
- self.app.stepCaret(now, caret_state.active, restart, snap);
- try self.render();
- if (self.configured and self.applied_scale > 0 and pending_token.len > 0) {
- if (self.activation) |activation| activation.activate(pending_token, self.surface);
- a.free(pending_token);
- pending_token = try a.dupeSentinel(u8, "", 0);
+ if (windows.items[0].app.signaled()) for (windows.items) |window| {
+ window.app.closed = true;
+ };
+ var index: usize = 0;
+ while (index < windows.items.len) {
+ const window = windows.items[index];
+ try window.step();
+ if (!window.running) {
+ window.destroy();
+ _ = windows.orderedRemove(index);
+ continue;
+ }
+ if (index == 0 and window.configured and window.applied_scale > 0 and pending_token.len > 0) {
+ if (window.activation) |activation| activation.activate(pending_token, window.surface);
+ a.free(pending_token);
+ pending_token = try a.dupeSentinel(u8, "", 0);
+ }
+ index += 1;
}
+ if (windows.items.len == 0) break;
while (!display.prepareRead()) {
if (display.dispatchPending() != .SUCCESS) return error.DisplayFailed;
}
- if (!self.running) {
- display.cancelRead();
- break;
- }
const flushed = display.flush();
if (flushed != .SUCCESS and flushed != .AGAIN) {
@@ -335,20 +257,29 @@ pub fn run(init: std.process.Init, paths: []const [:0]const u8, instance: *@impo
return error.DisplayFailed;
}
- var fds: [16]c.struct_pollfd = @splat(.{ .fd = -1, .events = 0, .revents = 0 });
+ var fds: [128]c.struct_pollfd = @splat(.{ .fd = -1, .events = 0, .revents = 0 });
fds[0] = .{ .fd = display.getFd(), .events = @intCast(c.POLLIN | @as(c_int, if (flushed == .AGAIN) c.POLLOUT else 0)), .revents = 0 };
- fds[1] = .{ .fd = if (self.app.operation) |job| job.fd else -1, .events = c.POLLIN, .revents = 0 };
- fds[2] = .{ .fd = instance.fd, .events = c.POLLIN, .revents = 0 };
- fds[3] = .{ .fd = self.app.draftWake(), .events = c.POLLIN, .revents = 0 };
- const fd_count = 4 + self.clipboard.addPoll(fds[4..]);
+ fds[1] = .{ .fd = instance.fd, .events = c.POLLIN, .revents = 0 };
+ fds[2] = .{ .fd = windows.items[0].app.draftWake(), .events = c.POLLIN, .revents = 0 };
+ var fd_count: usize = 3;
const poll_now = key_repeat.nowMs();
- var timeout: c_int = if (self.key_repeat.timeoutMs(poll_now)) |wait| @intCast(@min(wait, 60_000)) else -1;
- if (self.app.barTimeout(poll_now)) |wait| timeout = if (timeout < 0) @intCast(wait) else @min(timeout, @as(c_int, @intCast(wait)));
- if (self.app.draftTimeout(poll_now)) |wait| {
+ var timeout: c_int = -1;
+ for (windows.items) |window| {
+ fds[fd_count] = .{ .fd = if (window.app.operation) |job| job.fd else -1, .events = c.POLLIN, .revents = 0 };
+ fd_count += 1;
+ fd_count += window.clipboard.addPoll(fds[fd_count..]);
+ if (window.key_repeat.timeoutMs(poll_now)) |wait| {
+ const ms: c_int = @intCast(@min(wait, 60_000));
+ timeout = if (timeout < 0) ms else @min(timeout, ms);
+ }
+ if (window.app.barTimeout(poll_now)) |wait| timeout = if (timeout < 0) @intCast(wait) else @min(timeout, @as(c_int, @intCast(wait)));
+ timeout = window.app.caret.timeout(poll_now, window.clipboard.timeout(timeout));
+ }
+ if (windows.items[0].app.draftTimeout(poll_now)) |wait| {
const ms: c_int = @intCast(@min(wait, 60_000));
timeout = if (timeout < 0) ms else @min(timeout, ms);
}
- const result = c.poll(&fds, @intCast(fd_count), self.app.caret.timeout(poll_now, self.clipboard.timeout(timeout)));
+ const result = c.poll(&fds, @intCast(fd_count), timeout);
if (result > 0 and (fds[0].revents & c.POLLIN != 0)) {
if (display.readEvents() != .SUCCESS) return error.DisplayFailed;
} else {
@@ -356,14 +287,19 @@ pub fn run(init: std.process.Init, paths: []const [:0]const u8, instance: *@impo
}
if ((fds[0].revents & (c.POLLERR | c.POLLHUP)) != 0) return error.DisplayFailed;
if (display.dispatchPending() != .SUCCESS) return error.DisplayFailed;
- if (fds[3].revents & c.POLLIN != 0) self.app.drainDrafts();
- if (fds[2].revents & c.POLLIN != 0) {
+ if (fds[2].revents & c.POLLIN != 0) windows.items[0].app.drainDrafts();
+ if (fds[1].revents & c.POLLIN != 0) {
var request_buf: [16384]u8 = undefined;
if (instance.receive(&request_buf)) |bytes| {
if (std.json.parseFromSlice(@import("instance.zig").Request, a, bytes, .{})) |parsed| {
defer parsed.deinit();
const req = parsed.value;
- if (req.path.len == 0) self.app.newTab() catch {} else if (std.fs.path.isAbsolute(req.path) and std.mem.indexOfScalar(u8, req.path, 0) == null) self.app.open(req.path);
+ // Requests go to the window the user is in, else the first.
+ var target = windows.items[0];
+ for (windows.items) |window| {
+ if (window.app.focused) target = window;
+ }
+ if (req.path.len == 0) target.app.newTab() catch {} else if (std.fs.path.isAbsolute(req.path) and std.mem.indexOfScalar(u8, req.path, 0) == null) target.app.open(req.path);
if (req.token.len > 0 and std.mem.indexOfScalar(u8, req.token, 0) == null) {
const owned = try a.dupeSentinel(u8, req.token, 0);
a.free(pending_token);
@@ -372,18 +308,10 @@ pub fn run(init: std.process.Init, paths: []const [:0]const u8, instance: *@impo
} else |_| {}
}
}
- if (self.app.closed) continue;
- self.clipboard.dispatch();
- self.repeatDue();
- }
-
- for (self.outputs.items) |info| {
- if (info.logical) |logical| logical.destroy();
- info.proxy.destroy();
- a.destroy(info);
+ for (windows.items) |window| {
+ if (window.app.closed) continue;
+ window.clipboard.dispatch();
+ window.repeatDue();
+ }
}
- self.outputs.deinit(a);
- self.entered_outputs.deinit(a);
- if (self.xkb_state) |state| c.xkb_state_unref(state);
- if (self.xkb_context) |context| c.xkb_context_unref(context);
}