RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
feat(desktop): support dropping files onto Trash
Petar Margetića792a427ac46
diff --git a/src/desktop/dnd.zig b/src/desktop/dnd.zigindex 3608b81..9619fb6 100644--- a/src/desktop/dnd.zig+++ b/src/desktop/dnd.zig@@ -1,5 +1,20 @@//! URI-list transfer parsing. The worker owns received pipes and filesystem mutations.const std = @import("std");++pub const Destination = enum { trash, folder, link };++/// The built-in Trash icon must win over its folder-like representation.+pub fn destination(is_trash: bool, is_folder: bool) Destination {+ if (is_trash) return .trash;+ return if (is_folder) .folder else .link;+}++test "desktop drop destination gives Trash precedence over folders" {+ try std.testing.expectEqual(Destination.trash, destination(true, true));+ try std.testing.expectEqual(Destination.trash, destination(true, false));+ try std.testing.expectEqual(Destination.folder, destination(false, true));+ try std.testing.expectEqual(Destination.link, destination(false, false));+}pub fn localPath(a: std.mem.Allocator, uri: []const u8) ![]const u8 {var raw = uri;if (std.mem.startsWith(u8, raw, "file://localhost/")) raw = raw[16..] else if (std.mem.startsWith(u8, raw, "file:///")) raw = raw[7..] else return error.NotLocalFile;diff --git a/src/desktop/embedded.zig b/src/desktop/embedded.zigindex 208a25c..6549e79 100644--- a/src/desktop/embedded.zig+++ b/src/desktop/embedded.zig@@ -27,6 +27,7 @@ const geometry = @import("../geometry.zig");const SceneData = @import("../scene_data.zig").SceneData;const clipboard = @import("../clipboard.zig");const DropTarget = @import("../desktop_drop.zig").Target;+const dnd = @import("dnd.zig");const app_scope = @import("../session/app_scope.zig");const log = std.log.scoped(.desktop);@@ -73,6 +74,11 @@ pub const Desktop = struct {/// Hit-test role shared by every tile.scene_data: SceneData = undefined,+ const DropAt = struct {+ destination: dnd.Destination,+ path: ?[]const u8 = null,+ };+const Secondary = struct {output: *Output,node: *sn.Buffer,@@ -594,27 +600,41 @@ pub const Desktop = struct {self.schedule();}- /// The folder icon under the pointer, where a drop or paste would land.- fn dropFolder(self: *Desktop) ?[]const u8 {- const index = self.app.hit(self.app.x, self.app.y) orelse return null;+ /// The desktop item under the pointer, classified independently of its folder flag.+ fn dropAt(self: *Desktop) DropAt {+ const index = self.app.hit(self.app.x, self.app.y) orelse return .{ .destination = .link };const item = self.app.icons.items[index].item;- return if (item.folder) item.path else null;+ return .{+ .destination = dnd.destination(item.builtin == .trash, item.folder),+ .path = item.path,+ };}- /// A client drag moved over the desktop: dropping on a folder moves into- /// it, anywhere else links, as the desktop client negotiated.+ /// A client drag moved over the desktop. Trash and folders require move;+ /// only Trash forbids negotiation fallback to another action.pub fn dragHover(self: *Desktop) void {- self.drop_target.hover(self.server.input.seat, if (self.dropFolder() != null) .move else .copy);+ const target = self.dropAt();+ const preferred: @import("../desktop_drop.zig").Action = if (target.destination == .link) .copy else .move;+ self.drop_target.hover(self.server.input.seat, preferred, target.destination != .trash);}/// Receives a client drag released over the desktop. False leaves the/// drag to wlroots, which cancels it.pub fn drop(self: *Desktop, button: u32) bool {if (self.transfer != .none) return false;- const folder = self.dropFolder();- const taken = self.drop_target.take(self.server.input.seat, button, if (folder != null) .move else .copy) orelse return false;+ const target = self.dropAt();+ const is_trash = target.destination == .trash;+ const is_folder = target.destination == .folder;+ const preferred: @import("../desktop_drop.zig").Action = if (target.destination == .link) .copy else .move;+ const taken = self.drop_target.take(self.server.input.seat, button, preferred, !is_trash) orelse return false;self.worker.drop_done.store(false, .release);- self.worker.enqueue(.{ .kind = .drop, .path = folder orelse self.worker.dir, .folder = folder != null and taken.action == .move, .value = if (folder != null) "paste" else "", .fd = taken.fd });+ self.worker.enqueue(.{+ .kind = .drop,+ .path = if (is_folder) target.path.? else self.worker.dir,+ .folder = is_folder and taken.action == .move,+ .value = if (is_trash) "trash" else if (is_folder) "paste" else "",+ .fd = taken.fd,+ });self.transfer = .drop;self.app.leave();self.schedule();diff --git a/src/desktop/watcher.zig b/src/desktop/watcher.zigindex e122cfe..5012d3f 100644--- a/src/desktop/watcher.zig+++ b/src/desktop/watcher.zig@@ -545,8 +545,12 @@ pub const Worker = struct {const uri = std.mem.trim(u8, raw, " \t\r");if (uri.len == 0 or uri[0] == '#' or std.mem.eql(u8, uri, "copy") or std.mem.eql(u8, uri, "cut")) continue;const path = try @import("dnd.zig").localPath(mem, uri);- const dest = try std.fmt.allocPrint(mem, "{s}/{s}", .{ cmd.path, std.fs.path.basename(path) });- try self.execute(.{ .kind = if (cmd.folder or std.mem.startsWith(u8, data.items, "cut\n")) .move else if (std.mem.eql(u8, cmd.value, "paste")) .copy else .link, .path = path, .value = dest });+ if (std.mem.eql(u8, cmd.value, "trash")) {+ try self.execute(.{ .kind = .trash, .path = path });+ } else {+ const dest = try std.fmt.allocPrint(mem, "{s}/{s}", .{ cmd.path, std.fs.path.basename(path) });+ try self.execute(.{ .kind = if (cmd.folder or std.mem.startsWith(u8, data.items, "cut\n")) .move else if (std.mem.eql(u8, cmd.value, "paste")) .copy else .link, .path = path, .value = dest });+ }}self.drop_success.store(true, .release);},diff --git a/src/desktop_drop.zig b/src/desktop_drop.zigindex 4ea27d0..4875e4d 100644--- a/src/desktop_drop.zig+++ b/src/desktop_drop.zig@@ -44,9 +44,9 @@ pub const Target = struct {/// Tells a drag hovering the target what a drop would do, so its source/// can show the right cursor. Entering a client resets this.- pub fn hover(self: *const Target, seat: *wlr.Seat, preferred: Action) void {+ pub fn hover(self: *const Target, seat: *wlr.Seat, preferred: Action, allow_fallback: bool) void {const source = dragSource(seat) orelse return;- const action = if (self.active) null else negotiate(source, preferred);+ const action = if (self.active) null else negotiate(source, preferred, allow_fallback);// Motion arrives far faster than this changes; a client entering// resets `accepted`, so returning here sends it again.if (action) |value| {@@ -60,10 +60,10 @@ pub const Target = struct {/// before `pointerNotifyButton`. Returns the read end of the URI list and/// the negotiated action, or null (leaving the drag untouched, so wlroots/// cancels it) when there is nothing usable or a drop is still running.- pub fn take(self: *Target, seat: *wlr.Seat, button: u32, preferred: Action) ?struct { fd: i32, action: Action } {+ pub fn take(self: *Target, seat: *wlr.Seat, button: u32, preferred: Action, allow_fallback: bool) ?struct { fd: i32, action: Action } {if (self.active or button != seat.pointer_state.grab_button) return null;const source = dragSource(seat) orelse return null;- const action = negotiate(source, preferred) orelse return null;+ const action = negotiate(source, preferred, allow_fallback) orelse return null;var fds: [2]std.posix.fd_t = undefined;if (std.c.pipe2(&fds, .{ .CLOEXEC = true }) != 0) return null;@@ -102,15 +102,30 @@ pub fn offersUriList(source: *wlr.DataSource) bool {}/// The action a drop would perform, or null when the source allows neither.-/// Sources older than v3 have no action mask and always copy.-pub fn negotiate(source: *wlr.DataSource, preferred: Action) ?Action {- if (source.actions < 0) return .copy;- const mask: u32 = @bitCast(source.actions);+pub fn negotiate(source: *wlr.DataSource, preferred: Action, allow_fallback: bool) ?Action {+ return negotiateActions(source.actions, preferred, allow_fallback);+}++/// Pure action-mask negotiation, also covering legacy sources without an action mask.+pub fn negotiateActions(actions: i32, preferred: Action, allow_fallback: bool) ?Action {+ if (actions < 0) return if (preferred == .copy or allow_fallback) .copy else null;+ const mask: u32 = @bitCast(actions);const copy_bit: u32 = @intFromEnum(wl.DataDeviceManager.DndAction.Enum.copy);const move_bit: u32 = @intFromEnum(wl.DataDeviceManager.DndAction.Enum.move);- const bit = if (preferred == .move) move_bit else copy_bit;- if (mask & bit != 0) return preferred;- if (mask & copy_bit != 0) return .copy;- if (mask & move_bit != 0) return .move;+ const preferred_bit = if (preferred == .move) move_bit else copy_bit;+ if (mask & preferred_bit != 0) return preferred;+ if (!allow_fallback) return null;+ const fallback_bit = if (preferred == .move) copy_bit else move_bit;+ if (mask & fallback_bit != 0) return if (preferred == .move) .copy else .move;return null;}++test "move-only drop negotiation rejects copy-only and legacy sources" {+ const copy_bit: u32 = @intFromEnum(wl.DataDeviceManager.DndAction.Enum.copy);+ const move_bit: u32 = @intFromEnum(wl.DataDeviceManager.DndAction.Enum.move);+ try std.testing.expectEqual(@as(?Action, null), negotiateActions(@intCast(copy_bit), .move, false));+ try std.testing.expectEqual(@as(?Action, null), negotiateActions(-1, .move, false));+ try std.testing.expectEqual(@as(?Action, .move), negotiateActions(@intCast(move_bit), .move, false));+ try std.testing.expectEqual(@as(?Action, .copy), negotiateActions(@intCast(copy_bit), .move, true));+ try std.testing.expectEqual(@as(?Action, .move), negotiateActions(@intCast(move_bit), .copy, true));+}diff --git a/src/input/motion.zig b/src/input/motion.zigindex 406b8ce..73cf406 100644--- a/src/input/motion.zig+++ b/src/input/motion.zig@@ -455,9 +455,9 @@ pub fn passthroughMotion(input: *Input, time_msec: u32) void {input.hovered_resize_edges = null;input.clearTaskbarHover();input.setHover(null, null);- // Clearing client focus also leaves a DnD drag unfocused, which- // is what lets the desktop receive its drop.- input.seat.pointerClearFocus();+ // Defer focus clearing to the active pointer grab so wlroots+ // updates its DnD drag focus as well as ordinary pointer focus.+ input.seat.pointerNotifyClearFocus();res.desktop.pointerMotion(res.x, res.y);if (input.seat.drag != null) res.desktop.dragHover();},