Skip to content

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

feat(files): support dynamic Ctrl copy during drag

Petar Margetić06b11a3397cc
diff --git a/src/Keyboard.zig b/src/Keyboard.zig
index a935c17..f05ef2c 100644
--- a/src/Keyboard.zig
+++ b/src/Keyboard.zig
@@ -96,6 +96,62 @@ 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;
@@ -117,7 +173,12 @@ fn handleModifiers(listener: *wl.Listener(*wlr.Keyboard), wlr_keyboard: *wlr.Key
const synthetic_locked = keyboard.server.locker != null and keyboard.server.virtual_input != null and keyboard.device == &keyboard.server.virtual_input.?.keyboard.wlr_kbd.base;
if (!synthetic_locked) @import("osd.zig").show(keyboard.server, .{ .kind = .caps_lock, .active = caps });
}
- keyboard.server.input.seat.setKeyboard(wlr_keyboard);
+ const seat = keyboard.server.input.seat;
+ seat.setKeyboard(wlr_keyboard);
+ 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);
@import("input/layouts.zig").activate(keyboard);
if (keyboard.server.locker) |lock| {
lock.modifiers(wlr_keyboard.getModifiers());
diff --git a/src/files/client_clipboard.zig b/src/files/client_clipboard.zig
index cef1794..384b2df 100644
--- a/src/files/client_clipboard.zig
+++ b/src/files/client_clipboard.zig
@@ -13,6 +13,13 @@ const Client = @import("Client.zig");
pub fn clipboardEvent(source: *wl.DataSource, event: wl.DataSource.Event, clip: *ClipboardSource) void {
switch (event) {
+ .action => |ev| {
+ if (clip.client.drag_source == clip) {
+ if (clip.client.drag_icon) |*icon| {
+ icon.update(clip.client, ev.dnd_action.copy) catch {};
+ }
+ }
+ },
.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;
diff --git a/src/files/client_dnd.zig b/src/files/client_dnd.zig
index 3601a58..3606de9 100644
--- a/src/files/client_dnd.zig
+++ b/src/files/client_dnd.zig
@@ -79,7 +79,7 @@ pub fn dragOver(self: *Client, x: f64, y: f64, entered: bool) void {
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;
+ const moving = folder and offer.files and !self.app.ctrl;
if (!entered and wanted == self.drag_accepting and moving == self.drag_moving) return;
self.drag_accepting = wanted;
self.drag_moving = moving;
@@ -235,7 +235,7 @@ pub fn startFileDrag(self: *Client) !void {
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 });
+ if (source.getVersion() >= 3) source.setActions(.{ .copy = true, .move = true });
self.drag_source = clip;
self.app.cancelWheel();
self.app.cancelDrag();
diff --git a/src/files/client_input.zig b/src/files/client_input.zig
index abae88d..ce2b704 100644
--- a/src/files/client_input.zig
+++ b/src/files/client_input.zig
@@ -246,6 +246,13 @@ pub fn keyboardEvent(_: *wl.Keyboard, event: wl.Keyboard.Event, self: *Client) v
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;
+ if (self.drag_offer != null) {
+ self.dragOver(
+ self.drag_x,
+ self.drag_y + @as(f64, @floatFromInt(self.titlebarHeight())),
+ false,
+ );
+ }
}
},
.key => |ev| {
diff --git a/src/files/main.zig b/src/files/main.zig
index d43c719..08401fc 100644
--- a/src/files/main.zig
+++ b/src/files/main.zig
@@ -51,10 +51,12 @@ pub const ClipboardSource = struct {
pub const Client = @import("Client.zig");
-/// A separate, immutable buffer: window repaints must never recycle the drag image.
+/// A drag icon whose buffers remain alive until the drag ends.
pub const DragIcon = struct {
surface: *wl.Surface,
buffer: *Buffer,
+ retired: std.ArrayList(*Buffer) = .empty,
+ copy: bool = false,
pub fn create(client: *Client) !DragIcon {
const surface = try client.compositor.?.createSurface();
@@ -62,25 +64,72 @@ pub const DragIcon = struct {
const buffer = try Buffer.create(client, 48 * client.scale, 56 * client.scale);
errdefer buffer.destroy();
buffer.counts_frame = false;
+
const cr = c.cairo_create(buffer.image) orelse return error.CairoFailed;
defer c.cairo_destroy(cr);
c.cairo_scale(cr, @floatFromInt(client.scale), @floatFromInt(client.scale));
+ drawFile(client, cr);
+ c.cairo_surface_flush(buffer.image);
+
+ surface.setBufferScale(client.scale);
+ surface.attach(buffer.wl_buffer, 0, 0);
+ surface.damageBuffer(0, 0, buffer.w, buffer.h);
+ return .{ .surface = surface, .buffer = buffer };
+ }
+
+ pub fn update(self: *DragIcon, client: *Client, copy: bool) !void {
+ if (self.copy == copy) return;
+
+ const buffer = try Buffer.create(client, 48 * client.scale, 56 * client.scale);
+ errdefer buffer.destroy();
+ buffer.counts_frame = false;
+
+ const cr = c.cairo_create(buffer.image) orelse return error.CairoFailed;
+ defer c.cairo_destroy(cr);
+ c.cairo_scale(cr, @floatFromInt(client.scale), @floatFromInt(client.scale));
+ drawFile(client, cr);
+ if (copy) drawCopyBadge(cr);
+ c.cairo_surface_flush(buffer.image);
+
+ try self.retired.append(a, self.buffer);
+ self.buffer = buffer;
+ self.copy = copy;
+
+ self.surface.attach(buffer.wl_buffer, 0, 0);
+ self.surface.damageBuffer(0, 0, buffer.w, buffer.h);
+ self.surface.commit();
+ }
+
+ fn drawFile(client: *Client, cr: *c.cairo_t) void {
// Transparent padding places the file below and to the right of the pointer.
for (client.app.items.items) |item| {
if (!item.selected or item.missing) continue;
client.app.drawItemIcon(cr, item, 8, 16, 32);
break;
}
- c.cairo_surface_flush(buffer.image);
- surface.setBufferScale(client.scale);
- surface.attach(buffer.wl_buffer, 0, 0);
- surface.damageBuffer(0, 0, buffer.w, buffer.h);
- return .{ .surface = surface, .buffer = buffer };
+ }
+
+ fn drawCopyBadge(cr: *c.cairo_t) void {
+ c.cairo_arc(cr, 36, 44, 8, 0, 2 * std.math.pi);
+ c.cairo_set_source_rgba(cr, 0.08, 0.48, 0.24, 1);
+ c.cairo_fill_preserve(cr);
+ c.cairo_set_source_rgba(cr, 1, 1, 1, 1);
+ c.cairo_set_line_width(cr, 1.5);
+ c.cairo_stroke(cr);
+
+ c.cairo_move_to(cr, 32.5, 44);
+ c.cairo_line_to(cr, 39.5, 44);
+ c.cairo_move_to(cr, 36, 40.5);
+ c.cairo_line_to(cr, 36, 47.5);
+ c.cairo_set_line_width(cr, 2);
+ c.cairo_stroke(cr);
}
pub fn destroy(self: *DragIcon) void {
self.surface.destroy();
self.buffer.destroy();
+ for (self.retired.items) |buffer| buffer.destroy();
+ self.retired.deinit(a);
}
};