RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
files: move the Wayland Client into its own file and split it
Claude600410cd1f30
diff --git a/src/files/Client.zig b/src/files/Client.zignew file mode 100644index 0000000..4e2461a--- /dev/null+++ b/src/files/Client.zig@@ -0,0 +1,241 @@+//! The Files window's Wayland client: globals, seat, surface state and the+//! poll deadline. Its event handlers and drawing are in `client_*.zig` beside it+//! (window, input, dnd, clipboard, render); `main.zig` re-exports it.++const std = @import("std");+const ClientCursor = @import("ui").client_cursor;+const wl = @import("wayland").client.wl;+const xdg = @import("wayland").client.xdg;+const zxdg = @import("wayland").client.zxdg;+const c = @import("c.zig").api;+const app_mod = @import("app.zig");+const key_repeat_mod = @import("ui").key_repeat;+const shell_ui = @import("ui").cairo;+const ui_theme = @import("ui").theme;+const transfer = @import("transfer.zig");+const clipboard_mod = @import("clipboard.zig");+const a = std.heap.c_allocator;+const main = @import("main.zig");+const Offer = main.Offer;+const CSD_TITLEBAR_H = main.CSD_TITLEBAR_H;+const RESIZE_MARGIN = main.RESIZE_MARGIN;+const DecorationMode = main.DecorationMode;+const OutputInfo = main.OutputInfo;+const ClipboardSource = main.ClipboardSource;+const DragIcon = main.DragIcon;+const Buffer = main.Buffer;+const Client = @This();++display: *wl.Display,+compositor: ?*wl.Compositor = null,+shm: ?*wl.Shm = null,+outputs: std.ArrayList(*OutputInfo) = .empty,+entered_outputs: std.ArrayList(*OutputInfo) = .empty,+scale: i32 = 1,+seat: ?*wl.Seat = null,+pointer: ?*wl.Pointer = null,+keyboard: ?*wl.Keyboard = null,+data_manager: ?*wl.DataDeviceManager = null,+data_device: ?*wl.DataDevice = null,+selection_offer: ?*Offer = null,+drag_offer: ?*Offer = null,+/// The serial of the drag's last `enter`, which `accept` must quote.+drag_enter_serial: u32 = 0,+/// Whether the drag over the window was last told a drop here would work.+drag_accepting: bool = false,+drag_moving: bool = false,+drag_x: f64 = 0,+drag_y: f64 = 0,+/// A dropped offer being read, and the pin gap it was dropped on.+drop_offer: ?*Offer = null,+drop_receiving: ?transfer.Receive = null,+drop_slot: ?usize = null,+drop_terminal: bool = false,+drop_move: bool = false,+clipboard_source: ?*ClipboardSource = null,+drag_source: ?*ClipboardSource = null,+drag_serial: u32 = 0,+drag_icon: ?DragIcon = null,+axis_source: wl.Pointer.AxisSource = .wheel,+axis_delta: f64 = 0,+horizontal_delta: f64 = 0,+horizontal_notches: ?f64 = null,+axis_notches: ?f64 = null,+clipboard_revision: usize = 0,+offers: std.ArrayList(*Offer) = .empty,+input_serial: u32 = 0,+wm_base: ?*xdg.WmBase = null,+activation: ?*xdg.ActivationV1 = null,+preview_token: [4096:0]u8 = @splat(0),+preview_token_len: usize = 0,+editor_token_pending: bool = false,+decoration_manager: ?*zxdg.DecorationManagerV1 = null,+surface: *wl.Surface = undefined,+xdg_surface: *xdg.Surface = undefined,+xdg_toplevel: *xdg.Toplevel = undefined,+decoration: ?*zxdg.ToplevelDecorationV1 = null,+decoration_mode: DecorationMode = .unknown,+pointer_x: f64 = 0,+pointer_y: f64 = 0,+cursor_shape: ClientCursor = .{},+cursor_surface: *wl.Surface = undefined,+cursor_theme: ?*wl.CursorTheme = null,+cursor_serial: u32 = 0,+cursor_name: [*:0]const u8 = "",+inside: bool = false,+configured: bool = false,+/// An acked configure is applied only by a commit; the compositor holds+/// further interactive-resize configures until then, even when the new+/// frame is pixel-identical (e.g. the size-unchanged "resizing" configure).+ack_uncommitted: bool = false,+running: bool = true,+outstanding: usize = 0,+buffers: std.ArrayList(*Buffer) = .empty,+retained: std.ArrayList(u8) = .empty,+retained_w: i32 = 0,+applied_scale: i32 = 0,+receiving: ?transfer.Receive = null,+sends: std.ArrayList(transfer.Send) = .empty,+xkb_context: ?*c.xkb_context = null,+xkb_state: ?*c.xkb_state = null,+key_repeat: key_repeat_mod.KeyRepeat = .{},+compose: @import("ui").compose.Compose = .{},+ime: @import("terminal/ime.zig").Ime = undefined,+/// LC_CTYPE of the session; picks the Compose table (´+c is ç in pt_BR).+locale: []const u8 = "",+app: app_mod.App,++pub fn registryEvent(registry: *wl.Registry, event: wl.Registry.Event, self: *Client) void {+ switch (event) {+ .global => |g| {+ const iface = std.mem.span(g.interface);+ self.cursor_shape.bind(registry, g.name, iface, g.version);+ if (std.mem.eql(u8, iface, "zwp_text_input_manager_v3")) self.ime.manager = registry.bind(g.name, @import("wayland").client.zwp.TextInputManagerV3, 1) catch null;+ if (std.mem.eql(u8, iface, "wl_compositor")) {+ self.compositor = registry.bind(g.name, wl.Compositor, @min(g.version, 5)) catch null;+ } else if (std.mem.eql(u8, iface, "wl_shm")) {+ self.shm = registry.bind(g.name, wl.Shm, 1) catch null;+ } else if (std.mem.eql(u8, iface, "wl_output")) {+ if (registry.bind(g.name, wl.Output, @min(g.version, 4)) catch null) |proxy| {+ const info = a.create(OutputInfo) catch {+ proxy.destroy();+ return;+ };+ info.* = .{ .client = self, .proxy = proxy, .name = g.name };+ self.outputs.append(a, info) catch {+ proxy.destroy();+ a.destroy(info);+ return;+ };+ proxy.setListener(*OutputInfo, outputEvent, info);+ }+ } else if (std.mem.eql(u8, iface, "wl_seat") and self.seat == null) {+ self.seat = registry.bind(g.name, wl.Seat, @min(g.version, 8)) catch null;+ if (self.seat) |seat| seat.setListener(*Client, seatEvent, self);+ } else if (std.mem.eql(u8, iface, "wl_data_device_manager")) {+ self.data_manager = registry.bind(g.name, wl.DataDeviceManager, @min(g.version, 3)) catch null;+ } else if (std.mem.eql(u8, iface, "xdg_activation_v1")) {+ self.activation = registry.bind(g.name, xdg.ActivationV1, 1) catch null;+ } else if (std.mem.eql(u8, iface, "xdg_wm_base")) {+ self.wm_base = registry.bind(g.name, xdg.WmBase, @min(g.version, 2)) catch null;+ if (self.wm_base) |wm| wm.setListener(*Client, wmBaseEvent, self);+ } else if (std.mem.eql(u8, iface, "zxdg_decoration_manager_v1")) {+ self.decoration_manager = registry.bind(g.name, zxdg.DecorationManagerV1, 1) catch null;+ }+ },+ .global_remove => |ev| {+ for (self.outputs.items, 0..) |info, i| {+ if (info.name == ev.name) {+ _ = self.outputs.swapRemove(i);+ self.removeEnteredOutput(info);+ info.proxy.destroy();+ a.destroy(info);+ self.recomputeScale();+ break;+ }+ }+ },+ }+}++/// Worker results, the preview helper, clipboard pipes and input all wake+/// the poll, including file-operation state changes. Only animation and+/// known deadlines need a timeout; an idle window sleeps.+pub fn pollTimeout(self: *Client) c_int {+ if (self.app.wheel_glide.active() or self.app.selectionNeedsScroll() or self.app.hover_active or self.app.layout_active or self.app.scroll_appearance.active or self.app.sidebar_scroll_appearance.active or self.app.horizontal_appearance.active or self.app.terminal.active or self.app.terminal.bar.active) return 16;+ if (self.app.hasRunningFolderSize()) return 33;+ var deadline: ?i64 = self.app.torrents.retry_at;+ if (self.app.terminal.close_at) |at| deadline = earlier(deadline, at);+ if (self.app.terminal.visible) if (self.app.terminal.bar.deadline) |at| {+ deadline = earlier(deadline, at);+ };+ if (self.app.status_notice != null) deadline = earlier(deadline, self.app.status_notice_until);+ if (self.app.scanNoticeDeadline()) |at| deadline = earlier(deadline, at);+ if (self.app.scroll_appearance.deadline) |at| deadline = earlier(deadline, at);+ if (self.app.horizontal_appearance.deadline) |at| deadline = earlier(deadline, at);+ if (self.app.sidebar_scroll_appearance.deadline) |at| deadline = earlier(deadline, at);+ // Transfers expire once strictly past their timeout.+ if (self.receiving) |receive| deadline = earlier(deadline, receive.started + transfer.timeout_ms + 1);+ if (self.drop_receiving) |receive| deadline = earlier(deadline, receive.started + transfer.timeout_ms + 1);+ for (self.sends.items) |send| deadline = earlier(deadline, send.started + transfer.timeout_ms + 1);+ if (self.key_repeat.timeoutMs(app_mod.nowMs())) |wait| deadline = earlier(deadline, app_mod.nowMs() + wait);+ const at = deadline orelse return -1;+ return @intCast(std.math.clamp(at - app_mod.nowMs(), 0, 60_000));+}++fn earlier(current: ?i64, candidate: i64) i64 {+ return if (current) |value| @min(value, candidate) else candidate;+}++const client_window = @import("client_window.zig");+pub const openEditor = client_window.openEditor;+pub const previewReturned = client_window.previewReturned;+const wmBaseEvent = client_window.wmBaseEvent;+const outputEvent = client_window.outputEvent;+pub const recomputeScale = client_window.recomputeScale;+pub const removeEnteredOutput = client_window.removeEnteredOutput;+pub const surfaceEvent = client_window.surfaceEvent;+const seatEvent = client_window.seatEvent;+pub const xdgSurfaceEvent = client_window.xdgSurfaceEvent;+pub const wantsCsd = client_window.wantsCsd;+pub const titlebarHeight = client_window.titlebarHeight;+pub const decorationEvent = client_window.decorationEvent;+pub const xdgToplevelEvent = client_window.xdgToplevelEvent;++const client_input = @import("client_input.zig");+pub const pointerEvent = client_input.pointerEvent;+pub const flushScroll = client_input.flushScroll;+pub const handlePointerMotion = client_input.handlePointerMotion;+pub const refreshCursor = client_input.refreshCursor;+pub const handlePointerButton = client_input.handlePointerButton;+pub const handleTitlebarClick = client_input.handleTitlebarClick;+pub const deliverKey = client_input.deliverKey;+pub const appRepeats = client_input.appRepeats;+pub const repeatDue = client_input.repeatDue;+pub const keyboardEvent = client_input.keyboardEvent;+pub const setCursor = client_input.setCursor;++const client_dnd = @import("client_dnd.zig");+pub const discardOffer = client_dnd.discardOffer;+pub const dataEvent = client_dnd.dataEvent;+pub const dragOver = client_dnd.dragOver;+pub const dragDropped = client_dnd.dragDropped;+pub const startDropReceive = client_dnd.startDropReceive;+pub const receiveDrop = client_dnd.receiveDrop;+pub const finishDrop = client_dnd.finishDrop;+pub const endDrop = client_dnd.endDrop;+pub const clearDragIcon = client_dnd.clearDragIcon;+pub const startFileDrag = client_dnd.startFileDrag;++const client_clipboard = @import("client_clipboard.zig");+pub const clipboardEvent = client_clipboard.clipboardEvent;+pub const destroyClipboardSource = client_clipboard.destroyClipboardSource;+pub const publishClipboard = client_clipboard.publishClipboard;+pub const cancelReceive = client_clipboard.cancelReceive;+pub const receiveClipboard = client_clipboard.receiveClipboard;+pub const handleClipboard = client_clipboard.handleClipboard;++const client_render = @import("client_render.zig");+pub const acquireBuffer = client_render.acquireBuffer;+pub const render = client_render.render;+pub const renderTitlebar = client_render.renderTitlebar;diff --git a/src/files/client_clipboard.zig b/src/files/client_clipboard.zignew file mode 100644index 0000000..cef1794--- /dev/null+++ b/src/files/client_clipboard.zig@@ -0,0 +1,149 @@+//! Clipboard: publishing and receiving copied files and text.++const std = @import("std");+const wl = @import("wayland").client.wl;+const c = @import("c.zig").api;+const app_mod = @import("app.zig");+const transfer = @import("transfer.zig");+const clipboard_mod = @import("clipboard.zig");+const a = std.heap.c_allocator;+const main = @import("main.zig");+const ClipboardSource = main.ClipboardSource;+const Client = @import("Client.zig");++pub fn clipboardEvent(source: *wl.DataSource, event: wl.DataSource.Event, clip: *ClipboardSource) void {+ switch (event) {+ .send => |ev| {+ const mime = std.mem.span(ev.mime_type);+ const data = if (std.mem.eql(u8, mime, "x-special/gnome-copied-files")) clip.gnome else clip.uris;+ if (clip.client.sends.items.len >= 8) {+ _ = c.close(ev.fd);+ return;+ }+ var send = transfer.Send.init(ev.fd, data, app_mod.nowMs()) catch return;+ clip.client.sends.append(a, send) catch send.deinit();+ },+ .dnd_drop_performed => {+ if (clip.client.drag_source == clip) clip.client.clearDragIcon();+ },+ .cancelled, .dnd_finished => {+ if (clip.client.drag_source == clip) {+ clip.client.clearDragIcon();+ clip.client.drag_source = null;+ }+ if (clip.client.clipboard_source == clip) {+ clip.client.clipboard_source = null;+ clip.client.app.clearClipboard();+ clip.client.app.clipboard_cut = false;+ clip.client.app.clipboard_text.clearRetainingCapacity();+ clip.client.app.invalidate();+ }+ source.destroy();+ a.free(clip.uris);+ a.free(clip.gnome);+ a.destroy(clip);+ },+ else => {},+ }+}++pub fn destroyClipboardSource(self: *Client) void {+ if (self.clipboard_source) |clip| {+ clip.proxy.destroy();+ a.free(clip.uris);+ a.free(clip.gnome);+ a.destroy(clip);+ self.clipboard_source = null;+ }+}++pub fn publishClipboard(self: *Client) !void {+ const manager = self.data_manager orelse return;+ const device = self.data_device orelse return;+ if (!self.app.clipboard_is_text and self.app.clipboard_paths.items.len == 0) {+ device.setSelection(null, self.input_serial);+ self.destroyClipboardSource();+ return;+ }+ const source = try manager.createDataSource();+ errdefer source.destroy();+ const clip = try a.create(ClipboardSource);+ errdefer a.destroy(clip);+ const mode: clipboard_mod.ClipboardMode = if (self.app.clipboard_cut) .cut else .copy;+ const uris = if (self.app.clipboard_is_text) try a.dupe(u8, self.app.clipboard_text.items) else try clipboard_mod.encodeUriList(a, self.app.clipboard_paths.items);+ errdefer a.free(uris);+ const gnome = if (self.app.clipboard_is_text) try a.dupe(u8, "") else try clipboard_mod.encodeGnomeCopiedFiles(a, mode, self.app.clipboard_paths.items);+ clip.* = .{ .client = self, .proxy = source, .uris = uris, .gnome = gnome, .text = self.app.clipboard_is_text };+ source.setListener(*ClipboardSource, clipboardEvent, clip);+ if (clip.text) {+ source.offer("text/plain;charset=utf-8");+ source.offer("text/plain");+ } else {+ source.offer("text/uri-list");+ source.offer("x-special/gnome-copied-files");+ }+ device.setSelection(source, self.input_serial);+ self.destroyClipboardSource();+ self.clipboard_source = clip;+}++pub fn cancelReceive(self: *Client) void {+ if (self.receiving) |*receive| receive.deinit();+ self.receiving = null;+}++pub fn receiveClipboard(self: *Client) void {+ const receive = if (self.receiving) |*r| r else return;+ if (app_mod.nowMs() - receive.started > transfer.timeout_ms) {+ self.app.setStatusNotice(@import("i18n").tr("Clipboard transfer timed out"), true);+ self.cancelReceive();+ return;+ }+ const done = receive.read() catch {+ self.app.setStatusNotice(@import("i18n").tr("Clipboard transfer failed or exceeded 1 MiB"), true);+ self.cancelReceive();+ return;+ };+ if (!done) return;+ defer self.cancelReceive();+ if (receive.text) {+ if (receive.revision == self.app.edit_revision) self.app.pasteText(receive.bytes.items);+ } else {+ // A delayed reply must not paste into a folder navigated to meanwhile.+ if (!std.mem.eql(u8, receive.directory, self.app.history.current)) return;+ const decoded = clipboard_mod.decodeUris(a, receive.bytes.items) catch {+ self.app.setStatusNotice(@import("i18n").tr("Invalid file clipboard"), true);+ return;+ };+ defer decoded.deinit(a);+ self.app.executePasteExternal(decoded.mode, decoded.paths);+ }+}++pub fn handleClipboard(self: *Client) !void {+ if (self.app.clipboard_revision != self.clipboard_revision) {+ try self.publishClipboard();+ self.clipboard_revision = self.app.clipboard_revision;+ }+ if (!self.app.paste_requested and !self.app.text_paste_requested) return;+ const text = self.app.text_paste_requested;+ self.app.paste_requested = false;+ self.app.text_paste_requested = false;+ self.cancelReceive();+ if (self.clipboard_source) |clip| {+ if (text and clip.text) self.app.pasteText(clip.uris);+ if (!text and !clip.text) self.app.executePasteLocal();+ return;+ }+ const offer = self.selection_offer orelse return;+ const mime: [:0]const u8 = if (text) (if (offer.utf8) "text/plain;charset=utf-8" else if (offer.text) "text/plain" else return) else (if (offer.gnome) "x-special/gnome-copied-files" else if (offer.uri) "text/uri-list" else return);+ var fds: [2]c_int = undefined;+ if (c.pipe2(&fds, c.O_CLOEXEC) != 0) return error.PipeFailed;+ defer _ = c.close(fds[1]);+ errdefer _ = c.close(fds[0]);+ if (c.fcntl(fds[0], c.F_SETFL, @as(c_int, c.O_NONBLOCK)) < 0) return error.NonblockingFailed;+ const directory = try a.dupe(u8, self.app.history.current);+ self.receiving = .{ .fd = fds[0], .text = text, .revision = self.app.edit_revision, .directory = directory, .started = app_mod.nowMs() };+ offer.proxy.receive(mime, fds[1]);+ // The main loop flushes this request before polling the receive pipe.+}diff --git a/src/files/client_dnd.zig b/src/files/client_dnd.zignew file mode 100644index 0000000..76c372f--- /dev/null+++ b/src/files/client_dnd.zig@@ -0,0 +1,246 @@+//! Drag and drop: data offers, drops into folders or the terminal, and dragging files out.++const std = @import("std");+const wl = @import("wayland").client.wl;+const c = @import("c.zig").api;+const app_mod = @import("app.zig");+const transfer = @import("transfer.zig");+const clipboard_mod = @import("clipboard.zig");+const a = std.heap.c_allocator;+const main = @import("main.zig");+const Offer = main.Offer;+const ClipboardSource = main.ClipboardSource;+const DragIcon = main.DragIcon;+const Client = @import("Client.zig");+const clipboardEvent = Client.clipboardEvent;++pub fn offerEvent(_: *wl.DataOffer, event: wl.DataOffer.Event, offer: *Offer) void {+ if (event == .action) offer.action = event.action.dnd_action;+ if (event == .offer) {+ if (std.mem.eql(u8, std.mem.span(event.offer.mime_type), "text/plain")) offer.text = true;+ if (std.mem.eql(u8, std.mem.span(event.offer.mime_type), "text/plain;charset=utf-8")) offer.utf8 = true;+ if (std.mem.eql(u8, std.mem.span(event.offer.mime_type), "text/uri-list")) offer.uri = true;+ if (std.mem.eql(u8, std.mem.span(event.offer.mime_type), "application/x-rediwm-file-transfer")) offer.files = true;+ if (std.mem.eql(u8, std.mem.span(event.offer.mime_type), "x-special/gnome-copied-files")) offer.gnome = true;+ }+}++pub fn discardOffer(self: *Client, offer: *Offer) void {+ for (self.offers.items, 0..) |o, i| {+ if (o == offer) {+ _ = self.offers.swapRemove(i);+ break;+ }+ }+ offer.proxy.destroy();+ a.destroy(offer);+}++pub fn dataEvent(_: *wl.DataDevice, event: wl.DataDevice.Event, self: *Client) void {+ switch (event) {+ .data_offer => |ev| {+ const offer = a.create(Offer) catch return;+ offer.* = .{ .proxy = ev.id };+ self.offers.append(a, offer) catch {+ a.destroy(offer);+ return;+ };+ ev.id.setListener(*Offer, offerEvent, offer);+ },+ .enter => |ev| {+ if (self.drag_offer) |old| self.discardOffer(old);+ self.drag_offer = if (ev.id) |id| @ptrCast(@alignCast(id.getUserData())) else null;+ self.drag_enter_serial = ev.serial;+ // Offers are released on leave instead of retaining every re-entry.+ self.dragOver(ev.x.toDouble(), ev.y.toDouble(), true);+ },+ .motion => |ev| self.dragOver(ev.x.toDouble(), ev.y.toDouble(), false),+ .leave => {+ self.app.dropLeave();+ if (self.drag_offer) |offer| self.discardOffer(offer);+ self.drag_offer = null;+ },+ .drop => self.dragDropped(),+ .selection => |ev| {+ self.cancelReceive();+ if (self.selection_offer) |old| self.discardOffer(old);+ self.selection_offer = if (ev.id) |id| @ptrCast(@alignCast(id.getUserData())) else null;+ },+ }+}++pub fn dragOver(self: *Client, x: f64, y: f64, entered: bool) void {+ const offer = self.drag_offer orelse return;+ self.drag_x = x;+ self.drag_y = y - @as(f64, @floatFromInt(self.titlebarHeight()));+ const available = offer.uri and self.drop_receiving == null;+ const pin = available and self.app.dropMotion(x, self.drag_y);+ const terminal = available and !pin and self.app.terminalDropAt(x, self.drag_y);+ const folder = available and !pin and !terminal and self.app.dropDirectoryAt(x, self.drag_y) != null;+ self.app.highlightDrop(x, self.drag_y, folder);+ const wanted = pin or folder or terminal;+ const moving = folder and offer.files;+ if (!entered and wanted == self.drag_accepting and moving == self.drag_moving) return;+ self.drag_accepting = wanted;+ self.drag_moving = moving;+ offer.proxy.accept(self.drag_enter_serial, if (wanted) "text/uri-list" else null);+ if (offer.proxy.getVersion() >= 3) {+ // Files performs filesystem moves in the receiving job, never in+ // the source's finished callback. Other sources, pins and terminal+ // path insertion use copy only.+ offer.proxy.setActions(.{ .copy = wanted, .move = moving }, if (moving) .{ .move = true } else .{ .copy = wanted });+ }+}++pub fn dragDropped(self: *Client) void {+ const offer = self.drag_offer orelse return;+ self.drag_offer = null;+ defer self.app.dropLeave();+ const accepted = self.drag_accepting;+ self.drag_accepting = false;+ const slot = self.app.takePinDrop();+ const terminal = slot == null and self.app.terminalDropAt(self.drag_x, self.drag_y);+ const directory = if (slot != null or terminal) "" else self.app.dropDirectoryAt(self.drag_x, self.drag_y) orelse {+ self.discardOffer(offer);+ return;+ };+ if (!accepted or (offer.proxy.getVersion() >= 3 and !offer.action.copy and !offer.action.move)) {+ self.discardOffer(offer);+ return;+ }+ self.startDropReceive(offer, slot, directory, terminal) catch {+ self.app.setStatusNotice(@import("i18n").tr("Could not read the dropped items"), true);+ self.discardOffer(offer);+ };+}++pub fn startDropReceive(self: *Client, offer: *Offer, slot: ?usize, target: []const u8, terminal: bool) !void {+ var fds: [2]c_int = undefined;+ if (c.pipe2(&fds, c.O_CLOEXEC) != 0) return error.PipeFailed;+ defer _ = c.close(fds[1]);+ errdefer _ = c.close(fds[0]);+ if (c.fcntl(fds[0], c.F_SETFL, @as(c_int, c.O_NONBLOCK)) < 0) return error.NonblockingFailed;+ const directory = try a.dupe(u8, target);+ if (terminal) {+ self.app.focus = .terminal;+ self.app.edit_revision += 1;+ self.app.terminalDamage();+ }+ self.drop_receiving = .{ .fd = fds[0], .text = false, .revision = self.app.edit_revision, .directory = directory, .started = app_mod.nowMs() };+ self.drop_offer = offer;+ self.drop_slot = slot;+ self.drop_terminal = terminal;+ self.drop_move = slot == null and !terminal and offer.action.move;+ offer.proxy.receive("text/uri-list", fds[1]);+}++pub fn canonicalPath(path: []const u8) ![]u8 {+ const terminated = try a.dupeZ(u8, path);+ defer a.free(terminated);+ var buffer: [4096]u8 = undefined;+ const resolved = c.realpath(terminated, &buffer) orelse return error.InvalidPath;+ return a.dupe(u8, std.mem.span(resolved));+}++pub fn receiveDrop(self: *Client) void {+ const receive = if (self.drop_receiving) |*r| r else return;+ if (app_mod.nowMs() - receive.started > transfer.timeout_ms) {+ self.app.setStatusNotice(@import("i18n").tr("The drop timed out"), true);+ self.finishDrop();+ return;+ }+ const done = receive.read() catch {+ self.app.setStatusNotice(@import("i18n").tr("The dropped list failed or exceeded 1 MiB"), true);+ self.finishDrop();+ return;+ };+ if (!done) return;+ defer self.finishDrop();+ const decoded = clipboard_mod.decodeUris(a, receive.bytes.items) catch {+ self.app.setStatusNotice(@import("i18n").tr("Invalid dropped list"), true);+ return;+ };+ defer decoded.deinit(a);+ if (self.drop_terminal) {+ // Like clipboard paste, an asynchronous drop must not type into a+ // different editor or a restarted session after focus changes.+ if (receive.revision == self.app.edit_revision and self.app.focus == .terminal) self.app.terminalDropPaths(decoded.paths);+ } else if (self.drop_slot) |slot| {+ self.app.pinDropped(slot, decoded.paths);+ } else {+ const destination = canonicalPath(receive.directory) catch {+ self.app.setStatusNotice(@import("i18n").tr("The destination folder is unavailable"), true);+ return;+ };+ defer a.free(destination);+ var paths: std.ArrayList([]const u8) = .empty;+ defer paths.deinit(a);+ for (decoded.paths) |path| {+ const resolved = canonicalPath(path) catch continue;+ defer a.free(resolved);+ if (@import("ops.zig").isDescendantOrSame(resolved, destination)) {+ self.app.setStatusNotice(@import("i18n").tr("Cannot drop a folder into itself"), true);+ return;+ }+ const parent = std.fs.path.dirname(path) orelse continue;+ const resolved_parent = canonicalPath(parent) catch continue;+ defer a.free(resolved_parent);+ if (self.drop_move and std.mem.eql(u8, resolved_parent, destination)) continue;+ paths.append(a, path) catch return;+ }+ if (paths.items.len == 0) return;+ _ = self.app.executeTransfer(if (self.drop_move) .move else .copy, paths.items, destination, false);+ }+}++pub fn finishDrop(self: *Client) void {+ if (self.drop_receiving) |*receive| receive.deinit();+ self.drop_receiving = null;+ const offer = self.drop_offer orelse return;+ self.drop_offer = null;+ self.endDrop(offer);+}++/// Tells the source the drop is over, so it can let go of the drag, and+/// releases the offer.+pub fn endDrop(self: *Client, offer: *Offer) void {+ if (offer.proxy.getVersion() >= 3) offer.proxy.finish();+ self.discardOffer(offer);+}++pub fn clearDragIcon(self: *Client) void {+ if (self.drag_icon) |*icon| icon.destroy();+ self.drag_icon = null;+}++pub fn startFileDrag(self: *Client) !void {+ const manager = self.data_manager orelse return;+ const device = self.data_device orelse return;+ if (self.drag_source != null) return;+ var paths: std.ArrayList([]const u8) = .empty;+ defer paths.deinit(a);+ for (self.app.items.items) |item| {+ if (item.selected and !item.missing) try paths.append(a, item.path);+ }+ if (paths.items.len == 0) return;+ const source = try manager.createDataSource();+ errdefer source.destroy();+ const clip = try a.create(ClipboardSource);+ errdefer a.destroy(clip);+ const uris = try clipboard_mod.encodeUriList(a, paths.items);+ errdefer a.free(uris);+ const gnome = try a.dupe(u8, "");+ clip.* = .{ .client = self, .proxy = source, .uris = uris, .gnome = gnome };+ source.setListener(*ClipboardSource, clipboardEvent, clip);+ source.offer("text/uri-list");+ source.offer("application/x-rediwm-file-transfer");+ // The receiving file manager performs moves; upload targets choose copy.+ if (source.getVersion() >= 3) source.setActions(.{ .copy = true, .move = !self.app.ctrl });+ self.drag_source = clip;+ self.app.cancelWheel();+ self.app.cancelDrag();+ // The icon is optional: an allocation failure must not lose the drag.+ self.drag_icon = DragIcon.create(self) catch null;+ device.startDrag(source, self.surface, if (self.drag_icon) |icon| icon.surface else null, self.drag_serial);+ if (self.drag_icon) |icon| icon.surface.commit();+}diff --git a/src/files/client_input.zig b/src/files/client_input.zignew file mode 100644index 0000000..abae88d--- /dev/null+++ b/src/files/client_input.zig@@ -0,0 +1,285 @@+//! Pointer and keyboard: motion, buttons, resize edges, the titlebar, cursors, key delivery and repeat.++const std = @import("std");+const wl = @import("wayland").client.wl;+const xdg = @import("wayland").client.xdg;+const c = @import("c.zig").api;+const app_mod = @import("app.zig");+const key_repeat_mod = @import("ui").key_repeat;+const main = @import("main.zig");+const CSD_TITLEBAR_H = main.CSD_TITLEBAR_H;+const RESIZE_MARGIN = main.RESIZE_MARGIN;+const Client = @import("Client.zig");++pub fn pointerEvent(_: *wl.Pointer, event: wl.Pointer.Event, self: *Client) void {+ switch (event) {+ .enter => |ev| {+ self.cursor_serial = ev.serial;+ self.input_serial = ev.serial;+ self.inside = true;+ // Force the next setCursor to reassert the image: another+ // surface may have changed the shared per-seat pointer+ // cursor while we weren't hovered.+ self.cursor_name = "";+ self.handlePointerMotion(ev.surface_x.toDouble(), ev.surface_y.toDouble());+ },+ .leave => {+ self.inside = false;+ self.app.text_dragging = false;+ self.app.cancelDrag();+ self.app.handleMotion(-1, -1);+ },+ .motion => |ev| {+ self.handlePointerMotion(ev.surface_x.toDouble(), ev.surface_y.toDouble());+ },+ .button => |ev| {+ self.input_serial = ev.serial;+ if (ev.button == 0x110 and ev.state == .pressed) self.drag_serial = ev.serial;+ self.handlePointerButton(ev.button, ev.state == .pressed);+ },+ .axis => |ev| {+ if (ev.axis == .vertical_scroll) {+ self.axis_delta += ev.value.toDouble();+ } else self.horizontal_delta += ev.value.toDouble();+ if (self.pointer.?.getVersion() < 5) self.flushScroll();+ },+ .axis_source => |ev| self.axis_source = ev.axis_source,+ .axis_discrete => |ev| {+ if (ev.axis == .vertical_scroll) self.axis_notches = (self.axis_notches orelse 0) + @as(f64, @floatFromInt(ev.discrete)) else self.horizontal_notches = (self.horizontal_notches orelse 0) + @as(f64, @floatFromInt(ev.discrete));+ },+ .axis_value120 => |ev| {+ if (ev.axis == .vertical_scroll) self.axis_notches = (self.axis_notches orelse 0) + @as(f64, @floatFromInt(ev.value120)) / 120.0 else self.horizontal_notches = (self.horizontal_notches orelse 0) + @as(f64, @floatFromInt(ev.value120)) / 120.0;+ },+ .frame => self.flushScroll(),+ else => {},+ }+}++pub fn flushScroll(self: *Client) void {+ if (self.axis_source == .wheel or self.axis_source == .wheel_tilt) {+ const pixels_per_notch = 100.0;+ const pixels = if (self.axis_notches) |n| n * pixels_per_notch else self.axis_delta * (pixels_per_notch / 15.0);+ self.app.handleWheel(pixels, app_mod.nowMs());+ self.app.handleHorizontalScroll(if (self.horizontal_notches) |n| n * pixels_per_notch else self.horizontal_delta * (pixels_per_notch / 15.0));+ } else {+ if (self.axis_delta != 0) self.app.handleScroll(self.axis_delta);+ self.app.handleHorizontalScroll(self.horizontal_delta);+ }+ self.horizontal_delta = 0;+ self.horizontal_notches = null;+ self.axis_delta = 0;+ self.axis_notches = null;+}++pub fn edgeAt(x: f64, y: f64, w: f64, h: f64) ?xdg.Toplevel.ResizeEdge {+ const left = x <= RESIZE_MARGIN;+ const right = x >= w - RESIZE_MARGIN;+ const top = y <= RESIZE_MARGIN;+ const bottom = y >= h - RESIZE_MARGIN;+ if (top and left) return .top_left;+ if (top and right) return .top_right;+ if (bottom and left) return .bottom_left;+ if (bottom and right) return .bottom_right;+ if (top) return .top;+ if (bottom) return .bottom;+ if (left) return .left;+ if (right) return .right;+ return null;+}++pub fn cursorNameForEdge(edge: xdg.Toplevel.ResizeEdge) [*:0]const u8 {+ return switch (edge) {+ .top => "n-resize",+ .bottom => "s-resize",+ .left => "w-resize",+ .right => "e-resize",+ .top_left => "nw-resize",+ .top_right => "ne-resize",+ .bottom_left => "sw-resize",+ .bottom_right => "se-resize",+ .none => "default",+ else => "default",+ };+}++// Only the CSD titlebar and its resize border are handled here; anything+// below is forwarded to the App with the titlebar height subtracted so+// its hardcoded content-area layout stays oblivious to decorations.+pub fn handlePointerMotion(self: *Client, x: f64, y: f64) void {+ const old_close_hover = self.pointer_y < CSD_TITLEBAR_H and self.pointer_x >= @as(f64, @floatFromInt(self.app.w - 32));+ self.pointer_x = x;+ self.pointer_y = y;+ defer {+ if (self.app.drag_ready) {+ self.app.drag_ready = false;+ self.startFileDrag() catch self.app.setStatusNotice(@import("i18n").tr("Could not start file drag"), true);+ }+ }++ const tbh: f64 = @floatFromInt(self.titlebarHeight());+ self.app.handleMotion(x, @max(0, y - tbh));+ if (self.wantsCsd() and old_close_hover != (y < tbh and x >= @as(f64, @floatFromInt(self.app.w - 32)))) self.app.invalidate();+ self.refreshCursor();+}++pub fn refreshCursor(self: *Client) void {+ if (self.app.column_resize != null or self.app.sidebar_resize != null) {+ self.setCursor("col-resize");+ return;+ }+ const x = self.pointer_x;+ const y = self.pointer_y;+ const tbh: f64 = @floatFromInt(self.titlebarHeight());+ if (self.wantsCsd() and !self.app.text_dragging) {+ if (edgeAt(x, y, @floatFromInt(self.app.w), @as(f64, @floatFromInt(self.app.h)) + tbh)) |edge| {+ self.setCursor(cursorNameForEdge(edge));+ return;+ }+ }+ self.setCursor(if (y < tbh) "default" else self.app.pointerCursor(x, y - tbh));+}++pub fn handlePointerButton(self: *Client, button: u32, pressed: bool) void {+ defer self.refreshCursor();+ if (button == 0x110 and pressed and self.wantsCsd()) {+ const tbh_f: f64 = @floatFromInt(self.titlebarHeight());+ const w_f: f64 = @floatFromInt(self.app.w);+ const h_f: f64 = @as(f64, @floatFromInt(self.app.h)) + tbh_f;+ if (edgeAt(self.pointer_x, self.pointer_y, w_f, h_f)) |edge| {+ if (self.seat) |seat| self.xdg_toplevel.resize(seat, self.input_serial, edge);+ return;+ }+ if (self.pointer_y < tbh_f) {+ self.handleTitlebarClick(w_f, tbh_f);+ return;+ }+ }+ self.app.handleButton(button, pressed);+}++pub fn handleTitlebarClick(self: *Client, w_f: f64, tbh_f: f64) void {+ const close_w: f64 = @min(tbh_f, 32);+ if (self.pointer_x >= w_f - close_w) {+ self.app.terminalRequestClose();+ return;+ }+ if (self.seat) |seat| self.xdg_toplevel.move(seat, self.input_serial);+}++/// One press of evdev `key` through the current XKB state.+pub fn deliverKey(self: *Client, key: u32) void {+ const state = self.xkb_state orelse return;+ const sym = c.xkb_state_key_get_one_sym(state, key + 8);+ var buf: [64]u8 = undefined;+ const n = c.xkb_state_key_get_utf8(state, key + 8, &buf, buf.len);+ // Dead keys have no text of their own: sequences resolve here, and a+ // shortcut modifier abandons a half-typed one.+ if (!self.app.ctrl and !self.app.alt) {+ var composed: [64]u8 = undefined;+ if (self.compose.feed(self.locale, sym, &composed)) |text| {+ if (text.len > 0) self.app.handleKey(0, text);+ return;+ }+ } else self.compose.reset();+ self.app.handleKey(sym, buf[0..@intCast(std.math.clamp(n, 0, 63))]);+}++pub fn appRepeats(self: *Client, key: u32) bool {+ if (self.compose.consumed) return false;+ const state = self.xkb_state orelse return false;+ var buf: [64]u8 = undefined;+ const n = c.xkb_state_key_get_utf8(state, key + 8, &buf, buf.len);+ return self.app.keyRepeats(c.xkb_state_key_get_one_sym(state, key + 8), buf[0..@intCast(std.math.clamp(n, 0, 63))]);+}++/// A due repeat of the held key, if the app still takes it where its+/// focus is now (a repeat may have opened a dialog or left a field).+pub fn repeatDue(self: *Client) void {+ const key = self.key_repeat.due(key_repeat_mod.nowMs()) orelse return;+ if (!self.appRepeats(key)) return self.key_repeat.stop();+ self.deliverKey(key);+}++pub fn keyboardEvent(_: *wl.Keyboard, event: wl.Keyboard.Event, self: *Client) void {+ switch (event) {+ .enter => {+ self.app.terminal.window_focused = true;+ self.app.refreshPins();+ },+ .leave => {+ self.app.terminal.window_focused = false;+ self.key_repeat.stop();+ self.compose.reset();+ self.app.menu = null;+ self.app.ctrl = false;+ self.app.shift = false;+ self.app.alt = false;+ self.app.invalidate();+ },+ .keymap => |ev| {+ defer _ = c.close(ev.fd);+ self.key_repeat.stop();+ if (ev.format != .xkb_v1 or ev.size == 0) return;+ const memory = c.mmap(null, ev.size, c.PROT_READ, c.MAP_PRIVATE, ev.fd, 0);+ if (memory == c.MAP_FAILED) return;+ defer _ = c.munmap(memory, ev.size);++ const bytes: [*]const u8 = @ptrCast(memory);+ if (bytes[ev.size - 1] != 0) return;++ if (self.xkb_context == null) self.xkb_context = c.xkb_context_new(c.XKB_CONTEXT_NO_FLAGS);+ const keymap = c.xkb_keymap_new_from_string(+ self.xkb_context,+ @ptrCast(memory),+ c.XKB_KEYMAP_FORMAT_TEXT_V1,+ c.XKB_KEYMAP_COMPILE_NO_FLAGS,+ ) orelse return;+ defer c.xkb_keymap_unref(keymap);++ if (self.xkb_state) |state| c.xkb_state_unref(state);+ self.xkb_state = c.xkb_state_new(keymap);+ },+ .modifiers => |ev| {+ if (self.xkb_state) |state| {+ _ = c.xkb_state_update_mask(state, ev.mods_depressed, ev.mods_latched, ev.mods_locked, 0, 0, ev.group);+ self.app.ctrl = c.xkb_state_mod_name_is_active(state, "Control", c.XKB_STATE_MODS_EFFECTIVE) > 0;+ self.app.shift = c.xkb_state_mod_name_is_active(state, "Shift", c.XKB_STATE_MODS_EFFECTIVE) > 0;+ self.app.alt = c.xkb_state_mod_name_is_active(state, "Mod1", c.XKB_STATE_MODS_EFFECTIVE) > 0 or+ c.xkb_state_mod_name_is_active(state, "Alt", c.XKB_STATE_MODS_EFFECTIVE) > 0;+ }+ },+ .key => |ev| {+ self.input_serial = ev.serial;+ if (ev.state == .pressed) {+ const state = self.xkb_state orelse return;+ self.deliverKey(ev.key);+ // Asked after the key, of the state it left: the repeats+ // go wherever this press put focus.+ const keymap_repeats = c.xkb_keymap_key_repeats(c.xkb_state_get_keymap(state), ev.key + 8) != 0;+ self.key_repeat.press(ev.key, keymap_repeats, self.appRepeats(ev.key), key_repeat_mod.nowMs());+ } else self.key_repeat.release(ev.key);+ },+ .repeat_info => |ev| self.key_repeat.setInfo(ev.rate, ev.delay),+ }+}++pub fn setCursor(self: *Client, name: [*:0]const u8) void {+ if (!self.inside) return;+ if (std.mem.eql(u8, std.mem.span(self.cursor_name), std.mem.span(name))) return;+ const pointer = self.pointer orelse return;+ if (self.cursor_shape.set(pointer, self.cursor_serial, std.mem.span(name))) {+ self.cursor_name = name;+ return;+ }+ const theme_ptr = self.cursor_theme orelse return;+ const cursor = theme_ptr.getCursor(name) orelse theme_ptr.getCursor("default") orelse theme_ptr.getCursor("left_ptr") orelse return;+ if (cursor.image_count == 0) return;+ const img = cursor.images[0];+ const buffer = img.getBuffer() catch return;++ pointer.setCursor(self.cursor_serial, self.cursor_surface, @intCast(img.hotspot_x), @intCast(img.hotspot_y));+ self.cursor_surface.attach(buffer, 0, 0);+ self.cursor_surface.damage(0, 0, @intCast(img.width), @intCast(img.height));+ self.cursor_surface.commit();+ self.cursor_name = name;+}diff --git a/src/files/client_render.zig b/src/files/client_render.zignew file mode 100644index 0000000..0aeec59--- /dev/null+++ b/src/files/client_render.zig@@ -0,0 +1,152 @@+//! Rendering: shm buffers, the frame and the client-side titlebar.++const std = @import("std");+const c = @import("c.zig").api;+const app_mod = @import("app.zig");+const shell_ui = @import("ui").cairo;+const ui_theme = @import("ui").theme;+const a = std.heap.c_allocator;+const main = @import("main.zig");+const Buffer = main.Buffer;+const Client = @import("Client.zig");++pub fn acquireBuffer(self: *Client, w: i32, h: i32) !*Buffer {+ var i: usize = 0;+ while (i < self.buffers.items.len) {+ const buffer = self.buffers.items[i];+ if (!buffer.busy and (buffer.w != w or buffer.h != h)) {+ _ = self.buffers.swapRemove(i);+ buffer.destroy();+ } else i += 1;+ }+ for (self.buffers.items) |buffer| {+ if (!buffer.busy) return buffer;+ }+ const buffer = try Buffer.create(self, w, h);+ errdefer buffer.destroy();+ try self.buffers.append(a, buffer);+ return buffer;+}++pub fn render(self: *Client) !void {+ if (!self.configured or !self.app.dirty or self.outstanding >= 2) return;++ const tbh = self.titlebarHeight();+ const w = self.app.w;+ const h = self.app.h + tbh;+ const bw = w * self.scale;+ const bh = h * self.scale;+ const buffer = try self.acquireBuffer(bw, bh);++ const partial = self.retained.items.len == buffer.len and self.retained_w == bw and self.applied_scale == self.scale and self.app.paint_damage != null;+ if (partial) {+ const pixels: [*]u8 = @ptrCast(buffer.memory);+ @memcpy(pixels[0..buffer.len], self.retained.items);+ c.cairo_surface_mark_dirty(buffer.image);+ }+ const cr = c.cairo_create(buffer.image) orelse return error.CairoFailed;+ defer c.cairo_destroy(cr);++ c.cairo_scale(cr, @floatFromInt(self.scale), @floatFromInt(self.scale));+ if (partial) {+ var rect = self.app.paint_damage.?;+ rect.y += @floatFromInt(tbh);+ app_mod.clipDamage(cr, rect);+ }++ if (tbh > 0) self.renderTitlebar(cr, w, tbh);++ c.cairo_save(cr);+ c.cairo_translate(cr, 0, @floatFromInt(tbh));+ self.app.render(cr);+ c.cairo_restore(cr);++ c.cairo_surface_flush(buffer.image);++ const bytes: []const u8 = @as([*]const u8, @ptrCast(buffer.memory))[0..buffer.len];+ var x1 = bw;+ var y1 = bh;+ var x2: i32 = 0;+ var y2: i32 = 0;+ if (self.retained.items.len != bytes.len or self.retained_w != bw or self.applied_scale != self.scale) {+ x1 = 0;+ y1 = 0;+ x2 = bw;+ y2 = bh;+ } else {+ // Compare against the last published image, never against this recycled buffer.+ const stride: usize = @intCast(bw * 4);+ for (0..@intCast(bh)) |row| {+ const offset = row * stride;+ if (std.mem.eql(u8, bytes[offset..][0..stride], self.retained.items[offset..][0..stride])) continue;+ y1 = @min(y1, @as(i32, @intCast(row)));+ y2 = @as(i32, @intCast(row + 1));+ var left: usize = 0;+ while (left < stride and std.mem.eql(u8, bytes[offset + left ..][0..4], self.retained.items[offset + left ..][0..4])) : (left += 4) {}+ var right = stride;+ while (right > left and std.mem.eql(u8, bytes[offset + right - 4 ..][0..4], self.retained.items[offset + right - 4 ..][0..4])) : (right -= 4) {}+ x1 = @min(x1, @as(i32, @intCast(left / 4)));+ x2 = @max(x2, @as(i32, @intCast(right / 4)));+ }+ }+ try self.retained.resize(a, bytes.len);+ @memcpy(self.retained.items, bytes);+ self.retained_w = bw;+ if (self.applied_scale != self.scale) {+ self.surface.setBufferScale(self.scale);+ self.applied_scale = self.scale;+ }+ if (x2 > x1 and y2 > y1) {+ self.xdg_surface.setWindowGeometry(0, 0, w, h);+ self.surface.attach(buffer.wl_buffer, 0, 0);+ self.surface.damageBuffer(x1, y1, x2 - x1, y2 - y1);+ self.surface.commit();+ buffer.busy = true;+ self.outstanding += 1;+ } else if (self.ack_uncommitted) {+ // Keep the attached buffer; the commit only applies the ack.+ self.xdg_surface.setWindowGeometry(0, 0, w, h);+ self.surface.commit();+ }+ self.ack_uncommitted = false;+ self.app.dirty = false;+}++pub fn renderTitlebar(self: *Client, cr: *c.cairo_t, w: i32, h: i32) void {+ const w_f: f64 = @floatFromInt(w);+ const h_f: f64 = @floatFromInt(h);++ shell_ui.setSource(cr, ui_theme.global.window_bg);+ c.cairo_rectangle(cr, 0, 0, w_f, h_f);+ c.cairo_fill(cr);++ shell_ui.setSource(cr, ui_theme.global.app_divider);+ c.cairo_set_line_width(cr, 1);+ c.cairo_move_to(cr, 0, h_f - 0.5);+ c.cairo_line_to(cr, w_f, h_f - 0.5);+ c.cairo_stroke(cr);++ const title = std.mem.span(self.app.getTitle());+ shell_ui.setSource(cr, ui_theme.global.window_fg);+ const tw = app_mod.measureText(cr, title, 12, true);+ app_mod.drawText(cr, title, (w_f - tw) / 2.0, h_f / 2.0 + 4, 12, true);++ const close_w: f64 = @min(h_f, 32);+ const hovered = self.inside and self.pointer_y < h_f and self.pointer_x >= w_f - close_w;+ if (hovered) {+ shell_ui.setSource(cr, ui_theme.global.window_close_hover);+ c.cairo_rectangle(cr, w_f - close_w, 0, close_w, h_f);+ c.cairo_fill(cr);+ c.cairo_set_source_rgb(cr, 1, 1, 1);+ } else {+ shell_ui.setSource(cr, ui_theme.global.window_dim);+ }+ const cx = w_f - close_w / 2.0;+ const cy = h_f / 2.0;+ c.cairo_set_line_width(cr, 1.5);+ c.cairo_move_to(cr, cx - 5, cy - 5);+ c.cairo_line_to(cr, cx + 5, cy + 5);+ c.cairo_move_to(cr, cx + 5, cy - 5);+ c.cairo_line_to(cr, cx - 5, cy + 5);+ c.cairo_stroke(cr);+}diff --git a/src/files/client_window.zig b/src/files/client_window.zignew file mode 100644index 0000000..2e4a69b--- /dev/null+++ b/src/files/client_window.zig@@ -0,0 +1,200 @@+//! Window and seat events: the editor and preview handoff, xdg surface and toplevel, decorations, outputs and scale.++const std = @import("std");+const wl = @import("wayland").client.wl;+const xdg = @import("wayland").client.xdg;+const zxdg = @import("wayland").client.zxdg;+const c = @import("c.zig").api;+const a = std.heap.c_allocator;+const main = @import("main.zig");+const CSD_TITLEBAR_H = main.CSD_TITLEBAR_H;+const OutputInfo = main.OutputInfo;+const Client = @import("Client.zig");+const pointerEvent = Client.pointerEvent;+const keyboardEvent = Client.keyboardEvent;++pub fn editorToken(token: *xdg.ActivationTokenV1, event: xdg.ActivationTokenV1.Event, self: *Client) void {+ if (event == .done) {+ self.app.launchEditor(std.mem.span(event.done.token));+ token.destroy();+ self.editor_token_pending = false;+ }+}++pub fn openEditor(self: *Client) void {+ if (self.app.editor_path == null or self.editor_token_pending) return;+ if (self.activation) |activation| {+ const token = activation.getActivationToken() catch {+ self.app.launchEditor("");+ return;+ };+ token.setListener(*Client, editorToken, self);+ token.setSurface(self.surface);+ if (self.seat) |seat| token.setSerial(self.input_serial, seat);+ token.setAppId("rediwm-editor");+ token.commit();+ self.editor_token_pending = true;+ } else self.app.launchEditor("");+}++pub fn previewReturned(self: *Client) void {+ const fd = self.app.preview_return_fd orelse return;+ const n = c.read(fd, self.preview_token[self.preview_token_len..].ptr, self.preview_token.len - self.preview_token_len);+ if (n > 0) {+ self.preview_token_len += @intCast(n);+ if (std.mem.indexOfScalar(u8, self.preview_token[0..self.preview_token_len], '\n')) |end| {+ self.preview_token[end] = 0;+ if (self.activation) |activation| activation.activate(&self.preview_token, self.surface);+ } else if (self.preview_token_len < self.preview_token.len) return;+ } else if (n < 0 and std.posix.errno(n) == .INTR) return;+ _ = c.close(fd);+ self.app.preview_return_fd = null;+ self.preview_token_len = 0;+}++pub fn wmBaseEvent(_: *xdg.WmBase, event: xdg.WmBase.Event, self: *Client) void {+ switch (event) {+ .ping => |ev| {+ if (self.wm_base) |wm| wm.pong(ev.serial);+ },+ }+}++pub fn outputEvent(_: *wl.Output, event: wl.Output.Event, info: *OutputInfo) void {+ if (event == .scale) {+ info.scale = std.math.clamp(event.scale.factor, 1, 4);+ info.client.recomputeScale();+ }+}++// The window may span multiple outputs at different scales; use the+// largest scale among outputs the surface currently overlaps, per the+// usual Wayland client convention. Falls back to 1 before the first+// wl_surface.enter is received.+pub fn recomputeScale(self: *Client) void {+ var new_scale: i32 = 1;+ for (self.entered_outputs.items) |info| new_scale = @max(new_scale, info.scale);+ if (new_scale != self.scale) {+ self.scale = new_scale;+ self.app.scale = new_scale;+ self.app.invalidate();+ }+}++pub fn removeEnteredOutput(self: *Client, info: *OutputInfo) void {+ for (self.entered_outputs.items, 0..) |o, i| {+ if (o == info) {+ _ = self.entered_outputs.swapRemove(i);+ return;+ }+ }+}++pub fn surfaceEvent(_: *wl.Surface, event: wl.Surface.Event, self: *Client) void {+ switch (event) {+ .enter => |ev| {+ if (ev.output) |out| {+ for (self.outputs.items) |info| {+ if (info.proxy == out) {+ self.entered_outputs.append(a, info) catch {};+ self.recomputeScale();+ break;+ }+ }+ }+ self.app.invalidate();+ },+ .leave => |ev| {+ if (ev.output) |out| {+ for (self.outputs.items) |info| {+ if (info.proxy == out) {+ self.removeEnteredOutput(info);+ self.recomputeScale();+ break;+ }+ }+ }+ },+ }+}++pub fn seatEvent(seat: *wl.Seat, event: wl.Seat.Event, self: *Client) void {+ switch (event) {+ .capabilities => |ev| {+ if (ev.capabilities.pointer and self.pointer == null) {+ self.pointer = seat.getPointer() catch null;+ if (self.pointer) |p| p.setListener(*Client, pointerEvent, self);+ }+ if (!ev.capabilities.pointer) {+ self.cursor_shape.clearPointer();+ if (self.pointer) |p| p.release();+ self.pointer = null;+ }+ if (ev.capabilities.keyboard and self.keyboard == null) {+ self.keyboard = seat.getKeyboard() catch null;+ if (self.keyboard) |k| k.setListener(*Client, keyboardEvent, self);+ }+ if (!ev.capabilities.keyboard) {+ if (self.keyboard) |k| k.release();+ self.keyboard = null;+ }+ },+ else => {},+ }+}++pub fn xdgSurfaceEvent(xdg_surf: *xdg.Surface, event: xdg.Surface.Event, self: *Client) void {+ switch (event) {+ .configure => |ev| {+ xdg_surf.ackConfigure(ev.serial);+ self.configured = true;+ self.ack_uncommitted = true;+ self.app.invalidate();+ },+ }+}++// Whether this window must draw its own titlebar: either the compositor+// never advertised zxdg_decoration_manager_v1 (no configure event will+// ever arrive, so self-decorate unconditionally), or it did negotiate a+// mode and that mode isn't server_side. Per the xdg-decoration spec, a+// mode that hasn't been negotiated yet is assumed to be client_side.+pub fn wantsCsd(self: *const Client) bool {+ return self.decoration_mode != .server_side;+}++pub fn titlebarHeight(self: *const Client) i32 {+ return if (self.wantsCsd()) CSD_TITLEBAR_H else 0;+}++pub fn decorationEvent(_: *zxdg.ToplevelDecorationV1, event: zxdg.ToplevelDecorationV1.Event, self: *Client) void {+ switch (event) {+ .configure => |ev| {+ self.decoration_mode = switch (ev.mode) {+ .server_side => .server_side,+ .client_side => .client_side,+ else => .client_side,+ };+ self.app.invalidate();+ },+ }+}++pub fn xdgToplevelEvent(_: *xdg.Toplevel, event: xdg.Toplevel.Event, self: *Client) void {+ switch (event) {+ .configure => |ev| {+ const tbh = self.titlebarHeight();+ if (ev.width > 0 and ev.height > 0) {+ self.app.resize(ev.width, @max(1, ev.height - tbh));+ } else if (!self.configured) {+ self.app.resize(960, @max(540, 214 + self.app.footerHeight()));+ }+ self.xdg_toplevel.setMinSize(if (self.app.chooser != null) 600 else 360, 214 + self.app.footerHeight() + tbh);+ self.app.invalidate();+ },+ .close => {+ self.app.terminalRequestClose();+ },+ .configure_bounds, .wm_capabilities => {},+ }+}diff --git a/src/files/main.zig b/src/files/main.zigindex 3eb22b4..c9b3fb7 100644--- a/src/files/main.zig+++ b/src/files/main.zig@@ -16,7 +16,7 @@ const Allocator = std.mem.Allocator;const a = std.heap.c_allocator;const terminal_c = @import("terminal/model.zig").c;-const Offer = struct {+pub const Offer = struct {proxy: *wl.DataOffer,uri: bool = false,files: bool = false,@@ -29,19 +29,19 @@ const Offer = struct {// A window with no server-side decorations must draw its own titlebar so it// stays usable (movable, closable) on compositors that don't implement// zxdg-decoration, e.g. GNOME/Mutter. See plan-file-manager.md section 1.-const CSD_TITLEBAR_H: i32 = 32;-const RESIZE_MARGIN: f64 = 6;+pub const CSD_TITLEBAR_H: i32 = 32;+pub const RESIZE_MARGIN: f64 = 6;-const DecorationMode = enum { unknown, client_side, server_side };+pub const DecorationMode = enum { unknown, client_side, server_side };-const OutputInfo = struct {+pub const OutputInfo = struct {client: *Client,proxy: *wl.Output,name: u32,scale: i32 = 1,};-const ClipboardSource = struct {+pub const ClipboardSource = struct {client: *Client,proxy: *wl.DataSource,uris: []const u8,@@ -49,1142 +49,14 @@ const ClipboardSource = struct {text: bool = false,};-pub const Client = struct {- display: *wl.Display,- compositor: ?*wl.Compositor = null,- shm: ?*wl.Shm = null,- outputs: std.ArrayList(*OutputInfo) = .empty,- entered_outputs: std.ArrayList(*OutputInfo) = .empty,- scale: i32 = 1,- seat: ?*wl.Seat = null,- pointer: ?*wl.Pointer = null,- keyboard: ?*wl.Keyboard = null,- data_manager: ?*wl.DataDeviceManager = null,- data_device: ?*wl.DataDevice = null,- selection_offer: ?*Offer = null,- drag_offer: ?*Offer = null,- /// The serial of the drag's last `enter`, which `accept` must quote.- drag_enter_serial: u32 = 0,- /// Whether the drag over the window was last told a drop here would work.- drag_accepting: bool = false,- drag_moving: bool = false,- drag_x: f64 = 0,- drag_y: f64 = 0,- /// A dropped offer being read, and the pin gap it was dropped on.- drop_offer: ?*Offer = null,- drop_receiving: ?transfer.Receive = null,- drop_slot: ?usize = null,- drop_terminal: bool = false,- drop_move: bool = false,- clipboard_source: ?*ClipboardSource = null,- drag_source: ?*ClipboardSource = null,- drag_serial: u32 = 0,- drag_icon: ?DragIcon = null,- axis_source: wl.Pointer.AxisSource = .wheel,- axis_delta: f64 = 0,- horizontal_delta: f64 = 0,- horizontal_notches: ?f64 = null,- axis_notches: ?f64 = null,- clipboard_revision: usize = 0,- offers: std.ArrayList(*Offer) = .empty,- input_serial: u32 = 0,- wm_base: ?*xdg.WmBase = null,- activation: ?*xdg.ActivationV1 = null,- preview_token: [4096:0]u8 = @splat(0),- preview_token_len: usize = 0,- editor_token_pending: bool = false,- decoration_manager: ?*zxdg.DecorationManagerV1 = null,- surface: *wl.Surface = undefined,- xdg_surface: *xdg.Surface = undefined,- xdg_toplevel: *xdg.Toplevel = undefined,- decoration: ?*zxdg.ToplevelDecorationV1 = null,- decoration_mode: DecorationMode = .unknown,- pointer_x: f64 = 0,- pointer_y: f64 = 0,- cursor_shape: ClientCursor = .{},- cursor_surface: *wl.Surface = undefined,- cursor_theme: ?*wl.CursorTheme = null,- cursor_serial: u32 = 0,- cursor_name: [*:0]const u8 = "",- inside: bool = false,- configured: bool = false,- /// An acked configure is applied only by a commit; the compositor holds- /// further interactive-resize configures until then, even when the new- /// frame is pixel-identical (e.g. the size-unchanged "resizing" configure).- ack_uncommitted: bool = false,- running: bool = true,- outstanding: usize = 0,- buffers: std.ArrayList(*Buffer) = .empty,- retained: std.ArrayList(u8) = .empty,- retained_w: i32 = 0,- applied_scale: i32 = 0,- receiving: ?transfer.Receive = null,- sends: std.ArrayList(transfer.Send) = .empty,- xkb_context: ?*c.xkb_context = null,- xkb_state: ?*c.xkb_state = null,- key_repeat: key_repeat.KeyRepeat = .{},- compose: @import("ui").compose.Compose = .{},- ime: @import("terminal/ime.zig").Ime = undefined,- /// LC_CTYPE of the session; picks the Compose table (´+c is ç in pt_BR).- locale: []const u8 = "",- app: app_mod.App,-- pub fn registryEvent(registry: *wl.Registry, event: wl.Registry.Event, self: *Client) void {- switch (event) {- .global => |g| {- const iface = std.mem.span(g.interface);- self.cursor_shape.bind(registry, g.name, iface, g.version);- if (std.mem.eql(u8, iface, "zwp_text_input_manager_v3")) self.ime.manager = registry.bind(g.name, @import("wayland").client.zwp.TextInputManagerV3, 1) catch null;- if (std.mem.eql(u8, iface, "wl_compositor")) {- self.compositor = registry.bind(g.name, wl.Compositor, @min(g.version, 5)) catch null;- } else if (std.mem.eql(u8, iface, "wl_shm")) {- self.shm = registry.bind(g.name, wl.Shm, 1) catch null;- } else if (std.mem.eql(u8, iface, "wl_output")) {- if (registry.bind(g.name, wl.Output, @min(g.version, 4)) catch null) |proxy| {- const info = a.create(OutputInfo) catch {- proxy.destroy();- return;- };- info.* = .{ .client = self, .proxy = proxy, .name = g.name };- self.outputs.append(a, info) catch {- proxy.destroy();- a.destroy(info);- return;- };- proxy.setListener(*OutputInfo, outputEvent, info);- }- } else if (std.mem.eql(u8, iface, "wl_seat") and self.seat == null) {- self.seat = registry.bind(g.name, wl.Seat, @min(g.version, 8)) catch null;- if (self.seat) |seat| seat.setListener(*Client, seatEvent, self);- } else if (std.mem.eql(u8, iface, "wl_data_device_manager")) {- self.data_manager = registry.bind(g.name, wl.DataDeviceManager, @min(g.version, 3)) catch null;- } else if (std.mem.eql(u8, iface, "xdg_activation_v1")) {- self.activation = registry.bind(g.name, xdg.ActivationV1, 1) catch null;- } else if (std.mem.eql(u8, iface, "xdg_wm_base")) {- self.wm_base = registry.bind(g.name, xdg.WmBase, @min(g.version, 2)) catch null;- if (self.wm_base) |wm| wm.setListener(*Client, wmBaseEvent, self);- } else if (std.mem.eql(u8, iface, "zxdg_decoration_manager_v1")) {- self.decoration_manager = registry.bind(g.name, zxdg.DecorationManagerV1, 1) catch null;- }- },- .global_remove => |ev| {- for (self.outputs.items, 0..) |info, i| {- if (info.name == ev.name) {- _ = self.outputs.swapRemove(i);- self.removeEnteredOutput(info);- info.proxy.destroy();- a.destroy(info);- self.recomputeScale();- break;- }- }- },- }- }-- /// Worker results, the preview helper, clipboard pipes and input all wake- /// the poll, including file-operation state changes. Only animation and- /// known deadlines need a timeout; an idle window sleeps.- fn pollTimeout(self: *Client) c_int {- if (self.app.wheel_glide.active() or self.app.selectionNeedsScroll() or self.app.hover_active or self.app.layout_active or self.app.scroll_appearance.active or self.app.sidebar_scroll_appearance.active or self.app.horizontal_appearance.active or self.app.terminal.active or self.app.terminal.bar.active) return 16;- if (self.app.hasRunningFolderSize()) return 33;- var deadline: ?i64 = self.app.torrents.retry_at;- if (self.app.terminal.close_at) |at| deadline = earlier(deadline, at);- if (self.app.terminal.visible) if (self.app.terminal.bar.deadline) |at| {- deadline = earlier(deadline, at);- };- if (self.app.status_notice != null) deadline = earlier(deadline, self.app.status_notice_until);- if (self.app.scanNoticeDeadline()) |at| deadline = earlier(deadline, at);- if (self.app.scroll_appearance.deadline) |at| deadline = earlier(deadline, at);- if (self.app.horizontal_appearance.deadline) |at| deadline = earlier(deadline, at);- if (self.app.sidebar_scroll_appearance.deadline) |at| deadline = earlier(deadline, at);- // Transfers expire once strictly past their timeout.- if (self.receiving) |receive| deadline = earlier(deadline, receive.started + transfer.timeout_ms + 1);- if (self.drop_receiving) |receive| deadline = earlier(deadline, receive.started + transfer.timeout_ms + 1);- for (self.sends.items) |send| deadline = earlier(deadline, send.started + transfer.timeout_ms + 1);- if (self.key_repeat.timeoutMs(app_mod.nowMs())) |wait| deadline = earlier(deadline, app_mod.nowMs() + wait);- const at = deadline orelse return -1;- return @intCast(std.math.clamp(at - app_mod.nowMs(), 0, 60_000));- }-- fn earlier(current: ?i64, candidate: i64) i64 {- return if (current) |value| @min(value, candidate) else candidate;- }-- fn editorToken(token: *xdg.ActivationTokenV1, event: xdg.ActivationTokenV1.Event, self: *Client) void {- if (event == .done) {- self.app.launchEditor(std.mem.span(event.done.token));- token.destroy();- self.editor_token_pending = false;- }- }- fn openEditor(self: *Client) void {- if (self.app.editor_path == null or self.editor_token_pending) return;- if (self.activation) |activation| {- const token = activation.getActivationToken() catch {- self.app.launchEditor("");- return;- };- token.setListener(*Client, editorToken, self);- token.setSurface(self.surface);- if (self.seat) |seat| token.setSerial(self.input_serial, seat);- token.setAppId("rediwm-editor");- token.commit();- self.editor_token_pending = true;- } else self.app.launchEditor("");- }-- fn previewReturned(self: *Client) void {- const fd = self.app.preview_return_fd orelse return;- const n = c.read(fd, self.preview_token[self.preview_token_len..].ptr, self.preview_token.len - self.preview_token_len);- if (n > 0) {- self.preview_token_len += @intCast(n);- if (std.mem.indexOfScalar(u8, self.preview_token[0..self.preview_token_len], '\n')) |end| {- self.preview_token[end] = 0;- if (self.activation) |activation| activation.activate(&self.preview_token, self.surface);- } else if (self.preview_token_len < self.preview_token.len) return;- } else if (n < 0 and std.posix.errno(n) == .INTR) return;- _ = c.close(fd);- self.app.preview_return_fd = null;- self.preview_token_len = 0;- }-- fn wmBaseEvent(_: *xdg.WmBase, event: xdg.WmBase.Event, self: *Client) void {- switch (event) {- .ping => |ev| {- if (self.wm_base) |wm| wm.pong(ev.serial);- },- }- }-- fn outputEvent(_: *wl.Output, event: wl.Output.Event, info: *OutputInfo) void {- if (event == .scale) {- info.scale = std.math.clamp(event.scale.factor, 1, 4);- info.client.recomputeScale();- }- }-- // The window may span multiple outputs at different scales; use the- // largest scale among outputs the surface currently overlaps, per the- // usual Wayland client convention. Falls back to 1 before the first- // wl_surface.enter is received.- fn recomputeScale(self: *Client) void {- var new_scale: i32 = 1;- for (self.entered_outputs.items) |info| new_scale = @max(new_scale, info.scale);- if (new_scale != self.scale) {- self.scale = new_scale;- self.app.scale = new_scale;- self.app.invalidate();- }- }-- fn removeEnteredOutput(self: *Client, info: *OutputInfo) void {- for (self.entered_outputs.items, 0..) |o, i| {- if (o == info) {- _ = self.entered_outputs.swapRemove(i);- return;- }- }- }-- fn surfaceEvent(_: *wl.Surface, event: wl.Surface.Event, self: *Client) void {- switch (event) {- .enter => |ev| {- if (ev.output) |out| {- for (self.outputs.items) |info| {- if (info.proxy == out) {- self.entered_outputs.append(a, info) catch {};- self.recomputeScale();- break;- }- }- }- self.app.invalidate();- },- .leave => |ev| {- if (ev.output) |out| {- for (self.outputs.items) |info| {- if (info.proxy == out) {- self.removeEnteredOutput(info);- self.recomputeScale();- break;- }- }- }- },- }- }-- fn seatEvent(seat: *wl.Seat, event: wl.Seat.Event, self: *Client) void {- switch (event) {- .capabilities => |ev| {- if (ev.capabilities.pointer and self.pointer == null) {- self.pointer = seat.getPointer() catch null;- if (self.pointer) |p| p.setListener(*Client, pointerEvent, self);- }- if (!ev.capabilities.pointer) {- self.cursor_shape.clearPointer();- if (self.pointer) |p| p.release();- self.pointer = null;- }- if (ev.capabilities.keyboard and self.keyboard == null) {- self.keyboard = seat.getKeyboard() catch null;- if (self.keyboard) |k| k.setListener(*Client, keyboardEvent, self);- }- if (!ev.capabilities.keyboard) {- if (self.keyboard) |k| k.release();- self.keyboard = null;- }- },- else => {},- }- }-- fn xdgSurfaceEvent(xdg_surf: *xdg.Surface, event: xdg.Surface.Event, self: *Client) void {- switch (event) {- .configure => |ev| {- xdg_surf.ackConfigure(ev.serial);- self.configured = true;- self.ack_uncommitted = true;- self.app.invalidate();- },- }- }-- // Whether this window must draw its own titlebar: either the compositor- // never advertised zxdg_decoration_manager_v1 (no configure event will- // ever arrive, so self-decorate unconditionally), or it did negotiate a- // mode and that mode isn't server_side. Per the xdg-decoration spec, a- // mode that hasn't been negotiated yet is assumed to be client_side.- fn wantsCsd(self: *const Client) bool {- return self.decoration_mode != .server_side;- }-- fn titlebarHeight(self: *const Client) i32 {- return if (self.wantsCsd()) CSD_TITLEBAR_H else 0;- }-- fn decorationEvent(_: *zxdg.ToplevelDecorationV1, event: zxdg.ToplevelDecorationV1.Event, self: *Client) void {- switch (event) {- .configure => |ev| {- self.decoration_mode = switch (ev.mode) {- .server_side => .server_side,- .client_side => .client_side,- else => .client_side,- };- self.app.invalidate();- },- }- }-- fn xdgToplevelEvent(_: *xdg.Toplevel, event: xdg.Toplevel.Event, self: *Client) void {- switch (event) {- .configure => |ev| {- const tbh = self.titlebarHeight();- if (ev.width > 0 and ev.height > 0) {- self.app.resize(ev.width, @max(1, ev.height - tbh));- } else if (!self.configured) {- self.app.resize(960, @max(540, 214 + self.app.footerHeight()));- }- self.xdg_toplevel.setMinSize(if (self.app.chooser != null) 600 else 360, 214 + self.app.footerHeight() + tbh);- self.app.invalidate();- },- .close => {- self.app.terminalRequestClose();- },- .configure_bounds, .wm_capabilities => {},- }- }-- fn pointerEvent(_: *wl.Pointer, event: wl.Pointer.Event, self: *Client) void {- switch (event) {- .enter => |ev| {- self.cursor_serial = ev.serial;- self.input_serial = ev.serial;- self.inside = true;- // Force the next setCursor to reassert the image: another- // surface may have changed the shared per-seat pointer- // cursor while we weren't hovered.- self.cursor_name = "";- self.handlePointerMotion(ev.surface_x.toDouble(), ev.surface_y.toDouble());- },- .leave => {- self.inside = false;- self.app.text_dragging = false;- self.app.cancelDrag();- self.app.handleMotion(-1, -1);- },- .motion => |ev| {- self.handlePointerMotion(ev.surface_x.toDouble(), ev.surface_y.toDouble());- },- .button => |ev| {- self.input_serial = ev.serial;- if (ev.button == 0x110 and ev.state == .pressed) self.drag_serial = ev.serial;- self.handlePointerButton(ev.button, ev.state == .pressed);- },- .axis => |ev| {- if (ev.axis == .vertical_scroll) {- self.axis_delta += ev.value.toDouble();- } else self.horizontal_delta += ev.value.toDouble();- if (self.pointer.?.getVersion() < 5) self.flushScroll();- },- .axis_source => |ev| self.axis_source = ev.axis_source,- .axis_discrete => |ev| {- if (ev.axis == .vertical_scroll) self.axis_notches = (self.axis_notches orelse 0) + @as(f64, @floatFromInt(ev.discrete)) else self.horizontal_notches = (self.horizontal_notches orelse 0) + @as(f64, @floatFromInt(ev.discrete));- },- .axis_value120 => |ev| {- if (ev.axis == .vertical_scroll) self.axis_notches = (self.axis_notches orelse 0) + @as(f64, @floatFromInt(ev.value120)) / 120.0 else self.horizontal_notches = (self.horizontal_notches orelse 0) + @as(f64, @floatFromInt(ev.value120)) / 120.0;- },- .frame => self.flushScroll(),- else => {},- }- }-- fn flushScroll(self: *Client) void {- if (self.axis_source == .wheel or self.axis_source == .wheel_tilt) {- const pixels_per_notch = 100.0;- const pixels = if (self.axis_notches) |n| n * pixels_per_notch else self.axis_delta * (pixels_per_notch / 15.0);- self.app.handleWheel(pixels, app_mod.nowMs());- self.app.handleHorizontalScroll(if (self.horizontal_notches) |n| n * pixels_per_notch else self.horizontal_delta * (pixels_per_notch / 15.0));- } else {- if (self.axis_delta != 0) self.app.handleScroll(self.axis_delta);- self.app.handleHorizontalScroll(self.horizontal_delta);- }- self.horizontal_delta = 0;- self.horizontal_notches = null;- self.axis_delta = 0;- self.axis_notches = null;- }-- fn edgeAt(x: f64, y: f64, w: f64, h: f64) ?xdg.Toplevel.ResizeEdge {- const left = x <= RESIZE_MARGIN;- const right = x >= w - RESIZE_MARGIN;- const top = y <= RESIZE_MARGIN;- const bottom = y >= h - RESIZE_MARGIN;- if (top and left) return .top_left;- if (top and right) return .top_right;- if (bottom and left) return .bottom_left;- if (bottom and right) return .bottom_right;- if (top) return .top;- if (bottom) return .bottom;- if (left) return .left;- if (right) return .right;- return null;- }-- fn cursorNameForEdge(edge: xdg.Toplevel.ResizeEdge) [*:0]const u8 {- return switch (edge) {- .top => "n-resize",- .bottom => "s-resize",- .left => "w-resize",- .right => "e-resize",- .top_left => "nw-resize",- .top_right => "ne-resize",- .bottom_left => "sw-resize",- .bottom_right => "se-resize",- .none => "default",- else => "default",- };- }-- // Only the CSD titlebar and its resize border are handled here; anything- // below is forwarded to the App with the titlebar height subtracted so- // its hardcoded content-area layout stays oblivious to decorations.- fn handlePointerMotion(self: *Client, x: f64, y: f64) void {- const old_close_hover = self.pointer_y < CSD_TITLEBAR_H and self.pointer_x >= @as(f64, @floatFromInt(self.app.w - 32));- self.pointer_x = x;- self.pointer_y = y;- defer {- if (self.app.drag_ready) {- self.app.drag_ready = false;- self.startFileDrag() catch self.app.setStatusNotice(@import("i18n").tr("Could not start file drag"), true);- }- }-- const tbh: f64 = @floatFromInt(self.titlebarHeight());- self.app.handleMotion(x, @max(0, y - tbh));- if (self.wantsCsd() and old_close_hover != (y < tbh and x >= @as(f64, @floatFromInt(self.app.w - 32)))) self.app.invalidate();- self.refreshCursor();- }-- fn refreshCursor(self: *Client) void {- if (self.app.column_resize != null or self.app.sidebar_resize != null) {- self.setCursor("col-resize");- return;- }- const x = self.pointer_x;- const y = self.pointer_y;- const tbh: f64 = @floatFromInt(self.titlebarHeight());- if (self.wantsCsd() and !self.app.text_dragging) {- if (edgeAt(x, y, @floatFromInt(self.app.w), @as(f64, @floatFromInt(self.app.h)) + tbh)) |edge| {- self.setCursor(cursorNameForEdge(edge));- return;- }- }- self.setCursor(if (y < tbh) "default" else self.app.pointerCursor(x, y - tbh));- }-- fn handlePointerButton(self: *Client, button: u32, pressed: bool) void {- defer self.refreshCursor();- if (button == 0x110 and pressed and self.wantsCsd()) {- const tbh_f: f64 = @floatFromInt(self.titlebarHeight());- const w_f: f64 = @floatFromInt(self.app.w);- const h_f: f64 = @as(f64, @floatFromInt(self.app.h)) + tbh_f;- if (edgeAt(self.pointer_x, self.pointer_y, w_f, h_f)) |edge| {- if (self.seat) |seat| self.xdg_toplevel.resize(seat, self.input_serial, edge);- return;- }- if (self.pointer_y < tbh_f) {- self.handleTitlebarClick(w_f, tbh_f);- return;- }- }- self.app.handleButton(button, pressed);- }-- fn handleTitlebarClick(self: *Client, w_f: f64, tbh_f: f64) void {- const close_w: f64 = @min(tbh_f, 32);- if (self.pointer_x >= w_f - close_w) {- self.app.terminalRequestClose();- return;- }- if (self.seat) |seat| self.xdg_toplevel.move(seat, self.input_serial);- }-- /// One press of evdev `key` through the current XKB state.- fn deliverKey(self: *Client, key: u32) void {- const state = self.xkb_state orelse return;- const sym = c.xkb_state_key_get_one_sym(state, key + 8);- var buf: [64]u8 = undefined;- const n = c.xkb_state_key_get_utf8(state, key + 8, &buf, buf.len);- // Dead keys have no text of their own: sequences resolve here, and a- // shortcut modifier abandons a half-typed one.- if (!self.app.ctrl and !self.app.alt) {- var composed: [64]u8 = undefined;- if (self.compose.feed(self.locale, sym, &composed)) |text| {- if (text.len > 0) self.app.handleKey(0, text);- return;- }- } else self.compose.reset();- self.app.handleKey(sym, buf[0..@intCast(std.math.clamp(n, 0, 63))]);- }-- fn appRepeats(self: *Client, key: u32) bool {- if (self.compose.consumed) return false;- const state = self.xkb_state orelse return false;- var buf: [64]u8 = undefined;- const n = c.xkb_state_key_get_utf8(state, key + 8, &buf, buf.len);- return self.app.keyRepeats(c.xkb_state_key_get_one_sym(state, key + 8), buf[0..@intCast(std.math.clamp(n, 0, 63))]);- }-- /// A due repeat of the held key, if the app still takes it where its- /// focus is now (a repeat may have opened a dialog or left a field).- fn repeatDue(self: *Client) void {- const key = self.key_repeat.due(key_repeat.nowMs()) orelse return;- if (!self.appRepeats(key)) return self.key_repeat.stop();- self.deliverKey(key);- }-- fn keyboardEvent(_: *wl.Keyboard, event: wl.Keyboard.Event, self: *Client) void {- switch (event) {- .enter => {- self.app.terminal.window_focused = true;- self.app.refreshPins();- },- .leave => {- self.app.terminal.window_focused = false;- self.key_repeat.stop();- self.compose.reset();- self.app.menu = null;- self.app.ctrl = false;- self.app.shift = false;- self.app.alt = false;- self.app.invalidate();- },- .keymap => |ev| {- defer _ = c.close(ev.fd);- self.key_repeat.stop();- if (ev.format != .xkb_v1 or ev.size == 0) return;- const memory = c.mmap(null, ev.size, c.PROT_READ, c.MAP_PRIVATE, ev.fd, 0);- if (memory == c.MAP_FAILED) return;- defer _ = c.munmap(memory, ev.size);-- const bytes: [*]const u8 = @ptrCast(memory);- if (bytes[ev.size - 1] != 0) return;-- if (self.xkb_context == null) self.xkb_context = c.xkb_context_new(c.XKB_CONTEXT_NO_FLAGS);- const keymap = c.xkb_keymap_new_from_string(- self.xkb_context,- @ptrCast(memory),- c.XKB_KEYMAP_FORMAT_TEXT_V1,- c.XKB_KEYMAP_COMPILE_NO_FLAGS,- ) orelse return;- defer c.xkb_keymap_unref(keymap);-- if (self.xkb_state) |state| c.xkb_state_unref(state);- self.xkb_state = c.xkb_state_new(keymap);- },- .modifiers => |ev| {- if (self.xkb_state) |state| {- _ = c.xkb_state_update_mask(state, ev.mods_depressed, ev.mods_latched, ev.mods_locked, 0, 0, ev.group);- self.app.ctrl = c.xkb_state_mod_name_is_active(state, "Control", c.XKB_STATE_MODS_EFFECTIVE) > 0;- self.app.shift = c.xkb_state_mod_name_is_active(state, "Shift", c.XKB_STATE_MODS_EFFECTIVE) > 0;- self.app.alt = c.xkb_state_mod_name_is_active(state, "Mod1", c.XKB_STATE_MODS_EFFECTIVE) > 0 or- c.xkb_state_mod_name_is_active(state, "Alt", c.XKB_STATE_MODS_EFFECTIVE) > 0;- }- },- .key => |ev| {- self.input_serial = ev.serial;- if (ev.state == .pressed) {- const state = self.xkb_state orelse return;- self.deliverKey(ev.key);- // Asked after the key, of the state it left: the repeats- // go wherever this press put focus.- const keymap_repeats = c.xkb_keymap_key_repeats(c.xkb_state_get_keymap(state), ev.key + 8) != 0;- self.key_repeat.press(ev.key, keymap_repeats, self.appRepeats(ev.key), key_repeat.nowMs());- } else self.key_repeat.release(ev.key);- },- .repeat_info => |ev| self.key_repeat.setInfo(ev.rate, ev.delay),- }- }-- fn offerEvent(_: *wl.DataOffer, event: wl.DataOffer.Event, offer: *Offer) void {- if (event == .action) offer.action = event.action.dnd_action;- if (event == .offer) {- if (std.mem.eql(u8, std.mem.span(event.offer.mime_type), "text/plain")) offer.text = true;- if (std.mem.eql(u8, std.mem.span(event.offer.mime_type), "text/plain;charset=utf-8")) offer.utf8 = true;- if (std.mem.eql(u8, std.mem.span(event.offer.mime_type), "text/uri-list")) offer.uri = true;- if (std.mem.eql(u8, std.mem.span(event.offer.mime_type), "application/x-rediwm-file-transfer")) offer.files = true;- if (std.mem.eql(u8, std.mem.span(event.offer.mime_type), "x-special/gnome-copied-files")) offer.gnome = true;- }- }-- fn discardOffer(self: *Client, offer: *Offer) void {- for (self.offers.items, 0..) |o, i| {- if (o == offer) {- _ = self.offers.swapRemove(i);- break;- }- }- offer.proxy.destroy();- a.destroy(offer);- }-- fn dataEvent(_: *wl.DataDevice, event: wl.DataDevice.Event, self: *Client) void {- switch (event) {- .data_offer => |ev| {- const offer = a.create(Offer) catch return;- offer.* = .{ .proxy = ev.id };- self.offers.append(a, offer) catch {- a.destroy(offer);- return;- };- ev.id.setListener(*Offer, offerEvent, offer);- },- .enter => |ev| {- if (self.drag_offer) |old| self.discardOffer(old);- self.drag_offer = if (ev.id) |id| @ptrCast(@alignCast(id.getUserData())) else null;- self.drag_enter_serial = ev.serial;- // Offers are released on leave instead of retaining every re-entry.- self.dragOver(ev.x.toDouble(), ev.y.toDouble(), true);- },- .motion => |ev| self.dragOver(ev.x.toDouble(), ev.y.toDouble(), false),- .leave => {- self.app.dropLeave();- if (self.drag_offer) |offer| self.discardOffer(offer);- self.drag_offer = null;- },- .drop => self.dragDropped(),- .selection => |ev| {- self.cancelReceive();- if (self.selection_offer) |old| self.discardOffer(old);- self.selection_offer = if (ev.id) |id| @ptrCast(@alignCast(id.getUserData())) else null;- },- }- }-- fn dragOver(self: *Client, x: f64, y: f64, entered: bool) void {- const offer = self.drag_offer orelse return;- self.drag_x = x;- self.drag_y = y - @as(f64, @floatFromInt(self.titlebarHeight()));- const available = offer.uri and self.drop_receiving == null;- const pin = available and self.app.dropMotion(x, self.drag_y);- const terminal = available and !pin and self.app.terminalDropAt(x, self.drag_y);- const folder = available and !pin and !terminal and self.app.dropDirectoryAt(x, self.drag_y) != null;- self.app.highlightDrop(x, self.drag_y, folder);- const wanted = pin or folder or terminal;- const moving = folder and offer.files;- if (!entered and wanted == self.drag_accepting and moving == self.drag_moving) return;- self.drag_accepting = wanted;- self.drag_moving = moving;- offer.proxy.accept(self.drag_enter_serial, if (wanted) "text/uri-list" else null);- if (offer.proxy.getVersion() >= 3) {- // Files performs filesystem moves in the receiving job, never in- // the source's finished callback. Other sources, pins and terminal- // path insertion use copy only.- offer.proxy.setActions(.{ .copy = wanted, .move = moving }, if (moving) .{ .move = true } else .{ .copy = wanted });- }- }-- fn dragDropped(self: *Client) void {- const offer = self.drag_offer orelse return;- self.drag_offer = null;- defer self.app.dropLeave();- const accepted = self.drag_accepting;- self.drag_accepting = false;- const slot = self.app.takePinDrop();- const terminal = slot == null and self.app.terminalDropAt(self.drag_x, self.drag_y);- const directory = if (slot != null or terminal) "" else self.app.dropDirectoryAt(self.drag_x, self.drag_y) orelse {- self.discardOffer(offer);- return;- };- if (!accepted or (offer.proxy.getVersion() >= 3 and !offer.action.copy and !offer.action.move)) {- self.discardOffer(offer);- return;- }- self.startDropReceive(offer, slot, directory, terminal) catch {- self.app.setStatusNotice(@import("i18n").tr("Could not read the dropped items"), true);- self.discardOffer(offer);- };- }-- fn startDropReceive(self: *Client, offer: *Offer, slot: ?usize, target: []const u8, terminal: bool) !void {- var fds: [2]c_int = undefined;- if (c.pipe2(&fds, c.O_CLOEXEC) != 0) return error.PipeFailed;- defer _ = c.close(fds[1]);- errdefer _ = c.close(fds[0]);- if (c.fcntl(fds[0], c.F_SETFL, @as(c_int, c.O_NONBLOCK)) < 0) return error.NonblockingFailed;- const directory = try a.dupe(u8, target);- if (terminal) {- self.app.focus = .terminal;- self.app.edit_revision += 1;- self.app.terminalDamage();- }- self.drop_receiving = .{ .fd = fds[0], .text = false, .revision = self.app.edit_revision, .directory = directory, .started = app_mod.nowMs() };- self.drop_offer = offer;- self.drop_slot = slot;- self.drop_terminal = terminal;- self.drop_move = slot == null and !terminal and offer.action.move;- offer.proxy.receive("text/uri-list", fds[1]);- }-- fn canonicalPath(path: []const u8) ![]u8 {- const terminated = try a.dupeZ(u8, path);- defer a.free(terminated);- var buffer: [4096]u8 = undefined;- const resolved = c.realpath(terminated, &buffer) orelse return error.InvalidPath;- return a.dupe(u8, std.mem.span(resolved));- }-- fn receiveDrop(self: *Client) void {- const receive = if (self.drop_receiving) |*r| r else return;- if (app_mod.nowMs() - receive.started > transfer.timeout_ms) {- self.app.setStatusNotice(@import("i18n").tr("The drop timed out"), true);- self.finishDrop();- return;- }- const done = receive.read() catch {- self.app.setStatusNotice(@import("i18n").tr("The dropped list failed or exceeded 1 MiB"), true);- self.finishDrop();- return;- };- if (!done) return;- defer self.finishDrop();- const decoded = clipboard_mod.decodeUris(a, receive.bytes.items) catch {- self.app.setStatusNotice(@import("i18n").tr("Invalid dropped list"), true);- return;- };- defer decoded.deinit(a);- if (self.drop_terminal) {- // Like clipboard paste, an asynchronous drop must not type into a- // different editor or a restarted session after focus changes.- if (receive.revision == self.app.edit_revision and self.app.focus == .terminal) self.app.terminalDropPaths(decoded.paths);- } else if (self.drop_slot) |slot| {- self.app.pinDropped(slot, decoded.paths);- } else {- const destination = canonicalPath(receive.directory) catch {- self.app.setStatusNotice(@import("i18n").tr("The destination folder is unavailable"), true);- return;- };- defer a.free(destination);- var paths: std.ArrayList([]const u8) = .empty;- defer paths.deinit(a);- for (decoded.paths) |path| {- const resolved = canonicalPath(path) catch continue;- defer a.free(resolved);- if (@import("ops.zig").isDescendantOrSame(resolved, destination)) {- self.app.setStatusNotice(@import("i18n").tr("Cannot drop a folder into itself"), true);- return;- }- const parent = std.fs.path.dirname(path) orelse continue;- const resolved_parent = canonicalPath(parent) catch continue;- defer a.free(resolved_parent);- if (self.drop_move and std.mem.eql(u8, resolved_parent, destination)) continue;- paths.append(a, path) catch return;- }- if (paths.items.len == 0) return;- _ = self.app.executeTransfer(if (self.drop_move) .move else .copy, paths.items, destination, false);- }- }-- fn finishDrop(self: *Client) void {- if (self.drop_receiving) |*receive| receive.deinit();- self.drop_receiving = null;- const offer = self.drop_offer orelse return;- self.drop_offer = null;- self.endDrop(offer);- }-- /// Tells the source the drop is over, so it can let go of the drag, and- /// releases the offer.- fn endDrop(self: *Client, offer: *Offer) void {- if (offer.proxy.getVersion() >= 3) offer.proxy.finish();- self.discardOffer(offer);- }-- fn clipboardEvent(source: *wl.DataSource, event: wl.DataSource.Event, clip: *ClipboardSource) void {- switch (event) {- .send => |ev| {- const mime = std.mem.span(ev.mime_type);- const data = if (std.mem.eql(u8, mime, "x-special/gnome-copied-files")) clip.gnome else clip.uris;- if (clip.client.sends.items.len >= 8) {- _ = c.close(ev.fd);- return;- }- var send = transfer.Send.init(ev.fd, data, app_mod.nowMs()) catch return;- clip.client.sends.append(a, send) catch send.deinit();- },- .dnd_drop_performed => {- if (clip.client.drag_source == clip) clip.client.clearDragIcon();- },- .cancelled, .dnd_finished => {- if (clip.client.drag_source == clip) {- clip.client.clearDragIcon();- clip.client.drag_source = null;- }- if (clip.client.clipboard_source == clip) {- clip.client.clipboard_source = null;- clip.client.app.clearClipboard();- clip.client.app.clipboard_cut = false;- clip.client.app.clipboard_text.clearRetainingCapacity();- clip.client.app.invalidate();- }- source.destroy();- a.free(clip.uris);- a.free(clip.gnome);- a.destroy(clip);- },- else => {},- }- }-- fn clearDragIcon(self: *Client) void {- if (self.drag_icon) |*icon| icon.destroy();- self.drag_icon = null;- }-- fn startFileDrag(self: *Client) !void {- const manager = self.data_manager orelse return;- const device = self.data_device orelse return;- if (self.drag_source != null) return;- var paths: std.ArrayList([]const u8) = .empty;- defer paths.deinit(a);- for (self.app.items.items) |item| {- if (item.selected and !item.missing) try paths.append(a, item.path);- }- if (paths.items.len == 0) return;- const source = try manager.createDataSource();- errdefer source.destroy();- const clip = try a.create(ClipboardSource);- errdefer a.destroy(clip);- const uris = try clipboard_mod.encodeUriList(a, paths.items);- errdefer a.free(uris);- const gnome = try a.dupe(u8, "");- clip.* = .{ .client = self, .proxy = source, .uris = uris, .gnome = gnome };- source.setListener(*ClipboardSource, clipboardEvent, clip);- source.offer("text/uri-list");- source.offer("application/x-rediwm-file-transfer");- // The receiving file manager performs moves; upload targets choose copy.- if (source.getVersion() >= 3) source.setActions(.{ .copy = true, .move = !self.app.ctrl });- self.drag_source = clip;- self.app.cancelWheel();- self.app.cancelDrag();- // The icon is optional: an allocation failure must not lose the drag.- self.drag_icon = DragIcon.create(self) catch null;- device.startDrag(source, self.surface, if (self.drag_icon) |icon| icon.surface else null, self.drag_serial);- if (self.drag_icon) |icon| icon.surface.commit();- }-- fn destroyClipboardSource(self: *Client) void {- if (self.clipboard_source) |clip| {- clip.proxy.destroy();- a.free(clip.uris);- a.free(clip.gnome);- a.destroy(clip);- self.clipboard_source = null;- }- }-- fn publishClipboard(self: *Client) !void {- const manager = self.data_manager orelse return;- const device = self.data_device orelse return;- if (!self.app.clipboard_is_text and self.app.clipboard_paths.items.len == 0) {- device.setSelection(null, self.input_serial);- self.destroyClipboardSource();- return;- }- const source = try manager.createDataSource();- errdefer source.destroy();- const clip = try a.create(ClipboardSource);- errdefer a.destroy(clip);- const mode: clipboard_mod.ClipboardMode = if (self.app.clipboard_cut) .cut else .copy;- const uris = if (self.app.clipboard_is_text) try a.dupe(u8, self.app.clipboard_text.items) else try clipboard_mod.encodeUriList(a, self.app.clipboard_paths.items);- errdefer a.free(uris);- const gnome = if (self.app.clipboard_is_text) try a.dupe(u8, "") else try clipboard_mod.encodeGnomeCopiedFiles(a, mode, self.app.clipboard_paths.items);- clip.* = .{ .client = self, .proxy = source, .uris = uris, .gnome = gnome, .text = self.app.clipboard_is_text };- source.setListener(*ClipboardSource, clipboardEvent, clip);- if (clip.text) {- source.offer("text/plain;charset=utf-8");- source.offer("text/plain");- } else {- source.offer("text/uri-list");- source.offer("x-special/gnome-copied-files");- }- device.setSelection(source, self.input_serial);- self.destroyClipboardSource();- self.clipboard_source = clip;- }-- fn cancelReceive(self: *Client) void {- if (self.receiving) |*receive| receive.deinit();- self.receiving = null;- }-- fn receiveClipboard(self: *Client) void {- const receive = if (self.receiving) |*r| r else return;- if (app_mod.nowMs() - receive.started > transfer.timeout_ms) {- self.app.setStatusNotice(@import("i18n").tr("Clipboard transfer timed out"), true);- self.cancelReceive();- return;- }- const done = receive.read() catch {- self.app.setStatusNotice(@import("i18n").tr("Clipboard transfer failed or exceeded 1 MiB"), true);- self.cancelReceive();- return;- };- if (!done) return;- defer self.cancelReceive();- if (receive.text) {- if (receive.revision == self.app.edit_revision) self.app.pasteText(receive.bytes.items);- } else {- // A delayed reply must not paste into a folder navigated to meanwhile.- if (!std.mem.eql(u8, receive.directory, self.app.history.current)) return;- const decoded = clipboard_mod.decodeUris(a, receive.bytes.items) catch {- self.app.setStatusNotice(@import("i18n").tr("Invalid file clipboard"), true);- return;- };- defer decoded.deinit(a);- self.app.executePasteExternal(decoded.mode, decoded.paths);- }- }-- fn handleClipboard(self: *Client) !void {- if (self.app.clipboard_revision != self.clipboard_revision) {- try self.publishClipboard();- self.clipboard_revision = self.app.clipboard_revision;- }- if (!self.app.paste_requested and !self.app.text_paste_requested) return;- const text = self.app.text_paste_requested;- self.app.paste_requested = false;- self.app.text_paste_requested = false;- self.cancelReceive();- if (self.clipboard_source) |clip| {- if (text and clip.text) self.app.pasteText(clip.uris);- if (!text and !clip.text) self.app.executePasteLocal();- return;- }- const offer = self.selection_offer orelse return;- const mime: [:0]const u8 = if (text) (if (offer.utf8) "text/plain;charset=utf-8" else if (offer.text) "text/plain" else return) else (if (offer.gnome) "x-special/gnome-copied-files" else if (offer.uri) "text/uri-list" else return);- var fds: [2]c_int = undefined;- if (c.pipe2(&fds, c.O_CLOEXEC) != 0) return error.PipeFailed;- defer _ = c.close(fds[1]);- errdefer _ = c.close(fds[0]);- if (c.fcntl(fds[0], c.F_SETFL, @as(c_int, c.O_NONBLOCK)) < 0) return error.NonblockingFailed;- const directory = try a.dupe(u8, self.app.history.current);- self.receiving = .{ .fd = fds[0], .text = text, .revision = self.app.edit_revision, .directory = directory, .started = app_mod.nowMs() };- offer.proxy.receive(mime, fds[1]);- // The main loop flushes this request before polling the receive pipe.- }-- fn setCursor(self: *Client, name: [*:0]const u8) void {- if (!self.inside) return;- if (std.mem.eql(u8, std.mem.span(self.cursor_name), std.mem.span(name))) return;- const pointer = self.pointer orelse return;- if (self.cursor_shape.set(pointer, self.cursor_serial, std.mem.span(name))) {- self.cursor_name = name;- return;- }- const theme_ptr = self.cursor_theme orelse return;- const cursor = theme_ptr.getCursor(name) orelse theme_ptr.getCursor("default") orelse theme_ptr.getCursor("left_ptr") orelse return;- if (cursor.image_count == 0) return;- const img = cursor.images[0];- const buffer = img.getBuffer() catch return;-- pointer.setCursor(self.cursor_serial, self.cursor_surface, @intCast(img.hotspot_x), @intCast(img.hotspot_y));- self.cursor_surface.attach(buffer, 0, 0);- self.cursor_surface.damage(0, 0, @intCast(img.width), @intCast(img.height));- self.cursor_surface.commit();- self.cursor_name = name;- }-- fn acquireBuffer(self: *Client, w: i32, h: i32) !*Buffer {- var i: usize = 0;- while (i < self.buffers.items.len) {- const buffer = self.buffers.items[i];- if (!buffer.busy and (buffer.w != w or buffer.h != h)) {- _ = self.buffers.swapRemove(i);- buffer.destroy();- } else i += 1;- }- for (self.buffers.items) |buffer| {- if (!buffer.busy) return buffer;- }- const buffer = try Buffer.create(self, w, h);- errdefer buffer.destroy();- try self.buffers.append(a, buffer);- return buffer;- }-- fn render(self: *Client) !void {- if (!self.configured or !self.app.dirty or self.outstanding >= 2) return;-- const tbh = self.titlebarHeight();- const w = self.app.w;- const h = self.app.h + tbh;- const bw = w * self.scale;- const bh = h * self.scale;- const buffer = try self.acquireBuffer(bw, bh);-- const partial = self.retained.items.len == buffer.len and self.retained_w == bw and self.applied_scale == self.scale and self.app.paint_damage != null;- if (partial) {- const pixels: [*]u8 = @ptrCast(buffer.memory);- @memcpy(pixels[0..buffer.len], self.retained.items);- c.cairo_surface_mark_dirty(buffer.image);- }- const cr = c.cairo_create(buffer.image) orelse return error.CairoFailed;- defer c.cairo_destroy(cr);-- c.cairo_scale(cr, @floatFromInt(self.scale), @floatFromInt(self.scale));- if (partial) {- var rect = self.app.paint_damage.?;- rect.y += @floatFromInt(tbh);- app_mod.clipDamage(cr, rect);- }-- if (tbh > 0) self.renderTitlebar(cr, w, tbh);-- c.cairo_save(cr);- c.cairo_translate(cr, 0, @floatFromInt(tbh));- self.app.render(cr);- c.cairo_restore(cr);-- c.cairo_surface_flush(buffer.image);-- const bytes: []const u8 = @as([*]const u8, @ptrCast(buffer.memory))[0..buffer.len];- var x1 = bw;- var y1 = bh;- var x2: i32 = 0;- var y2: i32 = 0;- if (self.retained.items.len != bytes.len or self.retained_w != bw or self.applied_scale != self.scale) {- x1 = 0;- y1 = 0;- x2 = bw;- y2 = bh;- } else {- // Compare against the last published image, never against this recycled buffer.- const stride: usize = @intCast(bw * 4);- for (0..@intCast(bh)) |row| {- const offset = row * stride;- if (std.mem.eql(u8, bytes[offset..][0..stride], self.retained.items[offset..][0..stride])) continue;- y1 = @min(y1, @as(i32, @intCast(row)));- y2 = @as(i32, @intCast(row + 1));- var left: usize = 0;- while (left < stride and std.mem.eql(u8, bytes[offset + left ..][0..4], self.retained.items[offset + left ..][0..4])) : (left += 4) {}- var right = stride;- while (right > left and std.mem.eql(u8, bytes[offset + right - 4 ..][0..4], self.retained.items[offset + right - 4 ..][0..4])) : (right -= 4) {}- x1 = @min(x1, @as(i32, @intCast(left / 4)));- x2 = @max(x2, @as(i32, @intCast(right / 4)));- }- }- try self.retained.resize(a, bytes.len);- @memcpy(self.retained.items, bytes);- self.retained_w = bw;- if (self.applied_scale != self.scale) {- self.surface.setBufferScale(self.scale);- self.applied_scale = self.scale;- }- if (x2 > x1 and y2 > y1) {- self.xdg_surface.setWindowGeometry(0, 0, w, h);- self.surface.attach(buffer.wl_buffer, 0, 0);- self.surface.damageBuffer(x1, y1, x2 - x1, y2 - y1);- self.surface.commit();- buffer.busy = true;- self.outstanding += 1;- } else if (self.ack_uncommitted) {- // Keep the attached buffer; the commit only applies the ack.- self.xdg_surface.setWindowGeometry(0, 0, w, h);- self.surface.commit();- }- self.ack_uncommitted = false;- self.app.dirty = false;- }-- fn renderTitlebar(self: *Client, cr: *c.cairo_t, w: i32, h: i32) void {- const w_f: f64 = @floatFromInt(w);- const h_f: f64 = @floatFromInt(h);-- shell_ui.setSource(cr, ui_theme.global.window_bg);- c.cairo_rectangle(cr, 0, 0, w_f, h_f);- c.cairo_fill(cr);-- shell_ui.setSource(cr, ui_theme.global.app_divider);- c.cairo_set_line_width(cr, 1);- c.cairo_move_to(cr, 0, h_f - 0.5);- c.cairo_line_to(cr, w_f, h_f - 0.5);- c.cairo_stroke(cr);-- const title = std.mem.span(self.app.getTitle());- shell_ui.setSource(cr, ui_theme.global.window_fg);- const tw = app_mod.measureText(cr, title, 12, true);- app_mod.drawText(cr, title, (w_f - tw) / 2.0, h_f / 2.0 + 4, 12, true);-- const close_w: f64 = @min(h_f, 32);- const hovered = self.inside and self.pointer_y < h_f and self.pointer_x >= w_f - close_w;- if (hovered) {- shell_ui.setSource(cr, ui_theme.global.window_close_hover);- c.cairo_rectangle(cr, w_f - close_w, 0, close_w, h_f);- c.cairo_fill(cr);- c.cairo_set_source_rgb(cr, 1, 1, 1);- } else {- shell_ui.setSource(cr, ui_theme.global.window_dim);- }- const cx = w_f - close_w / 2.0;- const cy = h_f / 2.0;- c.cairo_set_line_width(cr, 1.5);- c.cairo_move_to(cr, cx - 5, cy - 5);- c.cairo_line_to(cr, cx + 5, cy + 5);- c.cairo_move_to(cr, cx + 5, cy - 5);- c.cairo_line_to(cr, cx - 5, cy + 5);- c.cairo_stroke(cr);- }-};+pub const Client = @import("Client.zig");/// A separate, immutable buffer: window repaints must never recycle the drag image.-const DragIcon = struct {+pub const DragIcon = struct {surface: *wl.Surface,buffer: *Buffer,- fn create(client: *Client) !DragIcon {+ pub fn create(client: *Client) !DragIcon {const surface = try client.compositor.?.createSurface();errdefer surface.destroy();const buffer = try Buffer.create(client, 48 * client.scale, 56 * client.scale);@@ -1206,13 +78,13 @@ const DragIcon = struct {return .{ .surface = surface, .buffer = buffer };}- fn destroy(self: *DragIcon) void {+ pub fn destroy(self: *DragIcon) void {self.surface.destroy();self.buffer.destroy();}};-const Buffer = struct {+pub const Buffer = struct {client: *Client,wl_buffer: *wl.Buffer,image: *c.cairo_surface_t,@@ -1223,7 +95,7 @@ const Buffer = struct {busy: bool = false,counts_frame: bool = true,- fn create(client: *Client, w: i32, h: i32) !*Buffer {+ pub fn create(client: *Client, w: i32, h: i32) !*Buffer {if (w <= 0 or h <= 0 or w > 16384 or h > 16384) return error.InvalidSize;const len: usize = @intCast(w * h * 4);const fd = c.memfd_create("rediwm-files", c.MFD_CLOEXEC);@@ -1268,7 +140,7 @@ const Buffer = struct {self.busy = false;if (self.counts_frame) self.client.outstanding -= 1;}- fn destroy(self: *Buffer) void {+ pub fn destroy(self: *Buffer) void {self.wl_buffer.destroy();c.cairo_surface_destroy(self.image);_ = c.munmap(self.memory, self.len);