RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
Respect Ctrl already held when a file drag starts
oxydizerba729af5e83c
diff --git a/src/Keyboard.zig b/src/Keyboard.zigindex f05ef2c..926932c 100644--- a/src/Keyboard.zig+++ b/src/Keyboard.zig@@ -96,62 +96,6 @@ fn handleKeymap(listener: *wl.Listener(*wlr.Keyboard), _: *wlr.Keyboard) void {@import("input/layouts.zig").changed(keyboard, true);}-fn updateDragAction(seat: *wlr.Seat, ctrl: bool) void {- const drag = seat.drag orelse return;- const source = seat.drag_source orelse return;-- const Enum = wl.DataDeviceManager.DndAction.Enum;- const copy_bit: u32 = @intFromEnum(Enum.copy);- const move_bit: u32 = @intFromEnum(Enum.move);- const both_bits = copy_bit | move_bit;-- // Only change the action for sources that explicitly support both.- if (source.actions < 0) return;- const source_actions: u32 = @bitCast(source.actions);- if ((source_actions & both_bits) != both_bits) return;-- source.compositor_action = if (ctrl) copy_bit else move_bit;-- const focus_client = drag.focus_client orelse return;- var offers = seat.drag_offers.iterator(.forward);- while (offers.next()) |offer| {- if (offer.type != .drag or offer.source != source) continue;- if (offer.resource.getClient() != focus_client.client) continue;-- const version = offer.resource.getVersion();- const offer_actions: u32 = if (version >= 3) offer.actions else copy_bit;- const preferred_bits: u32 = if (version >= 3)- @as(u32, @intCast(@intFromEnum(offer.preferred_action)))- else- 0;- const available = offer_actions & source_actions;-- const compositor_bits = source.compositor_action & available;- const action_bits = if (compositor_bits != 0)- compositor_bits- else if ((preferred_bits & available) != 0)- preferred_bits & available- else- available & (~available +% 1);-- const action: Enum = @enumFromInt(action_bits);-- // wlroots tracks one current action per source, shared by its offers.- if (source.current_dnd_action == action) return;-- if (offer.in_ask) {- source.current_dnd_action = action;- return;- }-- source.dndAction(action);- if (version >= 3) {- offer.resource.sendAction(@bitCast(action_bits));- }- return;- }-}-fn handleModifiers(listener: *wl.Listener(*wlr.Keyboard), wlr_keyboard: *wlr.Keyboard) void {const keyboard: *Keyboard = @fieldParentPtr("modifiers", listener);if (keyboard.server.locker != null and @import("input/text_input.zig").isVirtual(keyboard.device)) return;@@ -175,7 +119,7 @@ fn handleModifiers(listener: *wl.Listener(*wlr.Keyboard), wlr_keyboard: *wlr.Key}const seat = keyboard.server.input.seat;seat.setKeyboard(wlr_keyboard);- updateDragAction(seat, mods.ctrl);+ @import("input/drag.zig").updateDragAction(seat, mods.ctrl);// wlroots' DnD keyboard grab does not forward modifier events.// Keep the focused source client synchronized while the drag is active.if (seat.drag != null) seat.keyboardSendModifiers(&wlr_keyboard.modifiers);diff --git a/src/files/AGENTS.md b/src/files/AGENTS.mdindex f4ad7b2..dd2f440 100644--- a/src/files/AGENTS.md+++ b/src/files/AGENTS.md@@ -170,8 +170,10 @@ revision so a delayed receive cannot type after focus/session changes;control bytes are refused because terminal paste would alter them.* Files sources advertise `application/x-rediwm-file-transfer`: the receiving- job owns moves and the source never deletes on `dnd_finished`. Ctrl at drag- start offers copy only; other sources copy.+ job owns moves and the source never deletes on `dnd_finished`. Files offers+ both copy and move; the compositor chooses from the current Ctrl state at+ drag start and on modifier changes. Pins, terminal drops and sources without+ the Files transfer marker use copy only.* After `drop` the offer leaves `drag_offer` (a `leave` would destroy it) andis read through a nonblocking pipe (`drop_receiving`, poll slot 15).Snapshot the destination before reading.diff --git a/src/input/drag.zig b/src/input/drag.zigindex c35c48a..9d73c3b 100644--- a/src/input/drag.zig+++ b/src/input/drag.zig@@ -11,8 +11,68 @@ pub fn positionDragIcon(input: *Input) void {}}+pub fn updateDragAction(seat: *wlr.Seat, ctrl: bool) void {+ const drag = seat.drag orelse return;+ const source = seat.drag_source orelse return;++ const Enum = wl.DataDeviceManager.DndAction.Enum;+ const copy_bit: u32 = @intFromEnum(Enum.copy);+ const move_bit: u32 = @intFromEnum(Enum.move);+ const both_bits = copy_bit | move_bit;++ // Only change the action for sources that explicitly support both.+ if (source.actions < 0) return;+ const source_actions: u32 = @bitCast(source.actions);+ if ((source_actions & both_bits) != both_bits) return;++ source.compositor_action = if (ctrl) copy_bit else move_bit;++ const focus_client = drag.focus_client orelse return;+ var offers = seat.drag_offers.iterator(.forward);+ while (offers.next()) |offer| {+ if (offer.type != .drag or offer.source != source) continue;+ if (offer.resource.getClient() != focus_client.client) continue;++ const version = offer.resource.getVersion();+ const offer_actions: u32 = if (version >= 3) offer.actions else copy_bit;+ const preferred_bits: u32 = if (version >= 3)+ @as(u32, @intCast(@intFromEnum(offer.preferred_action)))+ else+ 0;+ const available = offer_actions & source_actions;++ const compositor_bits = source.compositor_action & available;+ const action_bits = if (compositor_bits != 0)+ compositor_bits+ else if ((preferred_bits & available) != 0)+ preferred_bits & available+ else+ available & (~available +% 1);++ const action: Enum = @enumFromInt(action_bits);++ // wlroots tracks one current action per source, shared by its offers.+ if (source.current_dnd_action == action) return;++ if (offer.in_ask) {+ source.current_dnd_action = action;+ return;+ }++ source.dndAction(action);+ if (version >= 3) {+ offer.resource.sendAction(@bitCast(action_bits));+ }+ return;+ }+}+pub fn startDrag(listener: *wl.Listener(*wlr.Drag), drag: *wlr.Drag) void {const input: *Input = @fieldParentPtr("start_drag", listener);+ // Ctrl may already be held before the grab starts, with no modifier+ // change during the drag. Set the preference before the first offer.+ const ctrl = if (input.seat.getKeyboard()) |keyboard| keyboard.getModifiers().ctrl else false;+ updateDragAction(input.seat, ctrl);input.drag_dodge.begin(drag);if (drag.grab_type != .keyboard_pointer) return;const icon = drag.icon orelse return;diff --git a/tests/files_drag.py b/tests/files_drag.pyindex 77bfac8..73a7e37 100644--- a/tests/files_drag.py+++ b/tests/files_drag.py@@ -291,7 +291,8 @@ def run_pin():log.close()-def run_transfer(same_window=False, copy=False, list_view=False, folder=True):+def run_transfer(same_window=False, copy=False, list_view=False, folder=True, ctrl_changes=()):+ expected_copy = ctrl_changes[-1] if ctrl_changes else copywith tempfile.TemporaryDirectory(prefix="rediwm-files-transfer-") as directory:tmp = Path(directory)home = tmp / "home"@@ -308,7 +309,8 @@ def run_transfer(same_window=False, copy=False, list_view=False, folder=True):if not same_window:(source / "bundle").mkdir()(source / "bundle" / "child.txt").write_text("nested payload")- comp, log = spawn_compositor(tmp, config_content="[compositor]\nxwayland = false\ndodge_file_drags = false\n",+ comp, log = spawn_compositor(tmp, scale=os.environ.get("REDIWM_TEST_SCALE", "1"),+ config_content="[compositor]\nxwayland = false\ndodge_file_drags = false\n",env_extra={"HOME": str(home)})peers = []try:@@ -316,6 +318,7 @@ def run_transfer(same_window=False, copy=False, list_view=False, folder=True):display = next(p.name for p in tmp.glob("wayland-*") if not p.name.endswith(".lock"))env = dict(os.environ, HOME=str(home), XDG_RUNTIME_DIR=str(tmp),XDG_STATE_HOME=str(tmp / "state"), XDG_DATA_HOME=str(tmp / "data"), XDG_CONFIG_HOME=str(tmp / "config"),+ XDG_CACHE_HOME=str(tmp / "cache"),REDIWM_CONFIG=str(tmp / "rediwm-config.toml"), WAYLAND_DISPLAY=display,DBUS_SESSION_BUS_ADDRESS="unix:path=/nonexistent", REDIWM_FILES_DEVICES="0")env.pop("DISPLAY", None)@@ -333,6 +336,8 @@ def run_transfer(same_window=False, copy=False, list_view=False, folder=True):src = launch(source, 10)dst = src if same_window else launch(target.parent if folder else target, 680)ipc.action('focus_window', {"id": src["id"]})+ for window in ([src] if same_window else [src, dst]):+ ipc.wait_for("window_geometry_settled", window_id=window["id"])time.sleep(.5)box = ipc.get_window_debug(src["id"])["client_box"]dest = ipc.get_window_debug(dst["id"])["client_box"]@@ -366,24 +371,30 @@ def run_transfer(same_window=False, copy=False, list_view=False, folder=True):move(sx + (tx-sx)*step/16, sy + (ty-sy)*step/16)time.sleep(.04)time.sleep(.2)+ # Change Ctrl while stationary over the target: action updates+ # must not depend on another pointer motion or drag enter.+ for pressed in ctrl_changes:+ ipc.action('key', {"keycode": 29, "pressed": pressed})+ time.sleep(.2)button(False)- if copy:+ if expected_copy:ipc.action('key', {"keycode": 29, "pressed": False})wait_for(lambda: (target / name).exists(), "file did not arrive at drop destination")assert (target / name).read_text() == "transfer payload"- if copy:+ if expected_copy:assert (source / name).read_text() == "transfer payload"else:wait_for(lambda: not (source / name).exists(), "move left the source behind")if not same_window:wait_for(lambda: (target / "bundle" / "child.txt").exists(), "nested folder did not arrive")assert (target / "bundle" / "child.txt").read_text() == "nested payload"- if copy:+ if expected_copy:assert (source / "bundle" / "child.txt").read_text() == "nested payload"else:wait_for(lambda: not (source / "bundle").exists(), "folder move left the source behind")assert all(peer.poll() is None for peer in peers), "Files exited during transfer"- print(f"PASS Files {'copy' if copy else 'move'} {'within window' if same_window else 'between windows'} {'folder' if folder else 'background'} {'list' if list_view else 'grid'}")+ changes = f" Ctrl changes={ctrl_changes}" if ctrl_changes else ""+ print(f"PASS Files {'copy' if expected_copy else 'move'} {'within window' if same_window else 'between windows'} {'folder' if folder else 'background'} {'list' if list_view else 'grid'}{changes}")finally:for peer in peers:stop_process(peer)@@ -398,6 +409,10 @@ if __name__ == "__main__":run_transfer(folder=False)run_transfer(copy=True)run_transfer(list_view=True)+ run_transfer(same_window=True, ctrl_changes=(True,))+ run_transfer(same_window=True, copy=True, ctrl_changes=(False,))+ run_transfer(ctrl_changes=(True,))+ run_transfer(copy=True, ctrl_changes=(False,))sys.exit(0)if "--pin" in sys.argv:run_pin()