Skip to content

Commit 9a4e8cc

Browse files
authored
native: marshal Wayland requests without libwayland 1.20 (#1846)
libwayland-client before 1.20, as shipped by Ubuntu 20.04 and Debian 11, has no `wl_proxy_marshal_array_flags`. OpenTUI required that symbol, so on those systems it dropped the Wayland clipboard without a message. Under WSLg the X11 fallback cannot serve images, so pasting an image gave 0 bytes of text, or `unsupported` without `DISPLAY` (anomalyco/opencode#55639). Requests now go through `wl_proxy_marshal_array_constructor_versioned`, which every libwayland since 1.10 exports; since 1.20 it is the flags call with no flags. Destructor requests call `wl_proxy_destroy` after the request, as generated code did before 1.20. One thread owns each connection, so destroying outside the request's lock changes nothing. On 1.26 the requests on the wire are the same as before. The tests now check the order of destroys and the arguments that reach libwayland. With Ubuntu 20.04's 1.18 library, a nested Hyprland holding a PNG now reads `image/png`, as the system 1.26 library does.
1 parent 1e041e9 commit 9a4e8cc

2 files changed

Lines changed: 48 additions & 46 deletions

File tree

‎packages/native/src/clipboard/linux.zig‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ pub const WaylandSymbols = struct {
225225
wl_display_prepare_read: *const fn (*WlDisplay) callconv(.c) c_int,
226226
wl_display_read_events: *const fn (*WlDisplay) callconv(.c) c_int,
227227
wl_display_cancel_read: *const fn (*WlDisplay) callconv(.c) void,
228-
wl_proxy_marshal_array_flags: *const fn (*WlProxy, u32, ?*const WlInterface, u32, u32, [*]WlArgument) callconv(.c) ?*WlProxy,
228+
// Exported since libwayland 1.10; 1.20 added the flags variant, which Ubuntu 20.04 lacks.
229+
wl_proxy_marshal_array_constructor_versioned: *const fn (*WlProxy, u32, [*]WlArgument, ?*const WlInterface, u32) callconv(.c) ?*WlProxy,
229230
wl_proxy_add_listener: *const fn (*WlProxy, [*]const *const anyopaque, ?*anyopaque) callconv(.c) c_int,
230231
wl_proxy_destroy: *const fn (*WlProxy) callconv(.c) void,
231232
wl_proxy_get_version: *const fn (*WlProxy) callconv(.c) u32,

‎packages/native/src/clipboard/wayland.zig‎

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ pub const Connection = struct {
284284
fn issueSelectionBarrier(self: *Connection) ?u64 {
285285
std.debug.assert(self.barrier_callback == null);
286286
const display = self.display orelse return null;
287-
const callback = self.marshal(@ptrCast(display), 0, self.symbols.wl_callback_interface, 1, 0, &.{}) orelse
287+
const callback = self.marshal(@ptrCast(display), 0, self.symbols.wl_callback_interface, 1, &.{}) orelse
288288
return null;
289289
if (self.addListener(callback, &barrier_listener) != 0) {
290290
self.symbols.wl_proxy_destroy(callback);
@@ -364,7 +364,7 @@ pub const Connection = struct {
364364
@memcpy(mime_z[0..mime.len], mime);
365365
mime_z[mime.len] = 0;
366366
var arguments = [_]WlArgument{ .{ .s = mime_z[0..mime.len :0].ptr }, .{ .h = fd } };
367-
_ = self.marshal(offer.proxy, if (self.core_data_device) 1 else 0, null, 1, 0, &arguments);
367+
_ = self.marshal(offer.proxy, if (self.core_data_device) 1 else 0, null, 1, &arguments);
368368
return self.queueFlush() != .failed;
369369
}
370370

@@ -381,7 +381,7 @@ pub const Connection = struct {
381381
self.allocator.destroy(provider);
382382
return self.selectionFailure(.provider);
383383
};
384-
const source = self.marshal(manager, 0, &self.metadata.source, 1, 0, &.{}) orelse {
384+
const source = self.marshal(manager, 0, &self.metadata.source, 1, &.{}) orelse {
385385
self.allocator.free(transfers);
386386
self.allocator.destroy(provider);
387387
return self.selectionFailure(.provider);
@@ -403,7 +403,7 @@ pub const Connection = struct {
403403
return self.selectionFailure(.provider);
404404
}
405405
var arguments = [_]WlArgument{.{ .o = source }};
406-
_ = self.marshal(device, if (primary) 2 else 0, null, self.symbols.wl_proxy_get_version(device), 0, &arguments);
406+
_ = self.marshal(device, if (primary) 2 else 0, null, self.symbols.wl_proxy_get_version(device), &arguments);
407407
if (!self.displayHealthy()) {
408408
self.symbols.wl_proxy_destroy(source);
409409
self.allocator.free(transfers);
@@ -431,7 +431,7 @@ pub const Connection = struct {
431431
if (primary and !self.primary_supported) return .unsupported;
432432
const device = self.device orelse return self.selectionFailure(.protocol);
433433
var arguments = [_]WlArgument{.{ .o = null }};
434-
_ = self.marshal(device, if (primary) 2 else 0, null, self.symbols.wl_proxy_get_version(device), 0, &arguments);
434+
_ = self.marshal(device, if (primary) 2 else 0, null, self.symbols.wl_proxy_get_version(device), &arguments);
435435
if (!self.displayHealthy()) return self.selectionFailure(.protocol);
436436
const flush_result = self.queueFlush();
437437
if (flush_result == .failed) {
@@ -533,7 +533,7 @@ pub const Connection = struct {
533533
set_max_buffer_size(display, WAYLAND_CONNECTION_BUFFER_SIZE_MAX);
534534
}
535535
self.display = display;
536-
const registry = self.marshal(@ptrCast(display), 1, self.symbols.wl_registry_interface, 1, 0, &.{}) orelse
536+
const registry = self.marshal(@ptrCast(display), 1, self.symbols.wl_registry_interface, 1, &.{}) orelse
537537
return error.RegistryFailed;
538538
self.registry = registry;
539539
if (self.addListener(registry, &registry_listener) != 0) return error.ListenerFailed;
@@ -544,7 +544,7 @@ pub const Connection = struct {
544544

545545
fn sendSync(self: *Connection) bool {
546546
const display = self.display orelse return false;
547-
const callback = self.marshal(@ptrCast(display), 0, self.symbols.wl_callback_interface, 1, 0, &.{}) orelse
547+
const callback = self.marshal(@ptrCast(display), 0, self.symbols.wl_callback_interface, 1, &.{}) orelse
548548
return false;
549549
self.sync_callback = callback;
550550
return self.addListener(callback, &callback_listener) == 0 and self.queueFlush() != .failed;
@@ -623,7 +623,7 @@ pub const Connection = struct {
623623
@memcpy(mime_z[0..mime.len], mime);
624624
mime_z[mime.len] = 0;
625625
var arguments = [_]WlArgument{.{ .s = mime_z[0..mime.len :0].ptr }};
626-
_ = self.marshal(source, 0, null, 1, 0, &arguments);
626+
_ = self.marshal(source, 0, null, 1, &arguments);
627627
return self.displayHealthy();
628628
}
629629

@@ -671,7 +671,8 @@ pub const Connection = struct {
671671
}
672672

673673
fn destroyProtocolProxy(self: *Connection, proxy: *WlProxy, opcode: u32) void {
674-
_ = self.marshal(proxy, opcode, null, self.symbols.wl_proxy_get_version(proxy), 1, &.{});
674+
_ = self.marshal(proxy, opcode, null, self.symbols.wl_proxy_get_version(proxy), &.{});
675+
self.symbols.wl_proxy_destroy(proxy);
675676
}
676677

677678
fn destroyOffer(self: *Connection, proxy: *WlProxy) void {
@@ -696,7 +697,6 @@ pub const Connection = struct {
696697
0,
697698
self.symbols.wl_surface_interface,
698699
2,
699-
0,
700700
&new_id,
701701
) orelse {
702702
self.destroyCoreHelper();
@@ -708,7 +708,6 @@ pub const Connection = struct {
708708
0,
709709
self.symbols.wl_shell_surface_interface,
710710
1,
711-
0,
712711
&shell_arguments,
713712
) orelse {
714713
self.destroyCoreHelper();
@@ -718,17 +717,16 @@ pub const Connection = struct {
718717
self.destroyCoreHelper();
719718
return false;
720719
}
721-
_ = self.marshal(self.helper_shell_surface.?, 3, null, 1, 0, &.{});
720+
_ = self.marshal(self.helper_shell_surface.?, 3, null, 1, &.{});
722721
var title = [_]WlArgument{.{ .s = "OpenTUI Clipboard" }};
723-
_ = self.marshal(self.helper_shell_surface.?, 8, null, 1, 0, &title);
722+
_ = self.marshal(self.helper_shell_surface.?, 8, null, 1, &title);
724723

725724
var pool_arguments = [_]WlArgument{ .{ .n = 0 }, .{ .h = fd }, .{ .i = 4 } };
726725
self.helper_pool = self.marshal(
727726
self.shm.?,
728727
0,
729728
self.symbols.wl_shm_pool_interface,
730729
1,
731-
0,
732730
&pool_arguments,
733731
) orelse {
734732
self.destroyCoreHelper();
@@ -747,17 +745,16 @@ pub const Connection = struct {
747745
0,
748746
self.symbols.wl_buffer_interface,
749747
1,
750-
0,
751748
&buffer_arguments,
752749
) orelse {
753750
self.destroyCoreHelper();
754751
return false;
755752
};
756753
var attach_arguments = [_]WlArgument{ .{ .o = self.helper_buffer.? }, .{ .i = 0 }, .{ .i = 0 } };
757-
_ = self.marshal(self.helper_surface.?, 1, null, 2, 0, &attach_arguments);
754+
_ = self.marshal(self.helper_surface.?, 1, null, 2, &attach_arguments);
758755
var damage_arguments = [_]WlArgument{ .{ .i = 0 }, .{ .i = 0 }, .{ .i = 1 }, .{ .i = 1 } };
759-
_ = self.marshal(self.helper_surface.?, 2, null, 2, 0, &damage_arguments);
760-
_ = self.marshal(self.helper_surface.?, 6, null, 2, 0, &.{});
756+
_ = self.marshal(self.helper_surface.?, 2, null, 2, &damage_arguments);
757+
_ = self.marshal(self.helper_surface.?, 6, null, 2, &.{});
761758
if (self.queueFlush() != .failed) return true;
762759
self.destroyCoreHelper();
763760
return false;
@@ -811,7 +808,7 @@ pub const Connection = struct {
811808
self.manager = manager;
812809
self.bound_manager_global = manager_global;
813810
var arguments = [_]WlArgument{ .{ .n = 0 }, .{ .o = seat.proxy } };
814-
const device = self.marshal(manager, 1, &self.metadata.device, manager_version, 0, &arguments) orelse return .failed;
811+
const device = self.marshal(manager, 1, &self.metadata.device, manager_version, &arguments) orelse return .failed;
815812
self.device = device;
816813
self.bound_seat_global = seat.global_name;
817814
if (self.addListener(device, &device_listener) != 0) return .failed;
@@ -853,7 +850,6 @@ pub const Connection = struct {
853850
1,
854851
self.symbols.wl_data_device_interface,
855852
1,
856-
0,
857853
&device_arguments,
858854
) orelse return .failed;
859855
if (self.addListener(self.device.?, &core_device_listener) != 0) return .failed;
@@ -864,7 +860,6 @@ pub const Connection = struct {
864860
1,
865861
self.symbols.wl_keyboard_interface,
866862
1,
867-
0,
868863
&keyboard_arguments,
869864
) orelse return .failed;
870865
if (self.addListener(self.keyboard.?, &keyboard_listener) != 0) return .failed;
@@ -902,7 +897,7 @@ pub const Connection = struct {
902897
.{ .u = version },
903898
.{ .n = 0 },
904899
};
905-
return self.marshal(self.registry.?, 0, interface, version, 0, &arguments);
900+
return self.marshal(self.registry.?, 0, interface, version, &arguments);
906901
}
907902

908903
fn marshal(
@@ -911,13 +906,12 @@ pub const Connection = struct {
911906
opcode: u32,
912907
interface: ?*const WlInterface,
913908
version: u32,
914-
flags: u32,
915909
arguments: []const WlArgument,
916910
) ?*WlProxy {
917911
if (comptime builtin.is_test) self.test_marshal_count += 1;
918912
var empty: [1]WlArgument = undefined;
919913
const pointer: [*]WlArgument = if (arguments.len == 0) &empty else @constCast(arguments.ptr);
920-
return self.symbols.wl_proxy_marshal_array_flags(proxy, opcode, interface, version, flags, pointer);
914+
return self.symbols.wl_proxy_marshal_array_constructor_versioned(proxy, opcode, pointer, interface, version);
921915
}
922916

923917
fn displayHealthy(self: *const Connection) bool {
@@ -1142,7 +1136,7 @@ pub const Connection = struct {
11421136
fn shellSurfacePing(data: ?*anyopaque, shell_surface: ?*WlProxy, serial: u32) callconv(.c) void {
11431137
const self: *Connection = @ptrCast(@alignCast(data.?));
11441138
var arguments = [_]WlArgument{.{ .u = serial }};
1145-
_ = self.marshal(shell_surface.?, 0, null, 1, 0, &arguments);
1139+
_ = self.marshal(shell_surface.?, 0, null, 1, &arguments);
11461140
_ = self.queueFlush();
11471141
}
11481142

@@ -1370,7 +1364,7 @@ test "Wayland seat selection treats XDG_SEAT as advisory but explicit configurat
13701364
.events = null,
13711365
};
13721366
var symbols: linux.WaylandSymbols = undefined;
1373-
symbols.wl_proxy_marshal_array_flags = testMarshalFailure;
1367+
symbols.wl_proxy_marshal_array_constructor_versioned = testMarshalFailure;
13741368
symbols.wl_seat_interface = &seat_interface;
13751369
var connection = Connection.init(std.testing.allocator, &symbols, "", "", 1);
13761370
connection.registry = @ptrFromInt(3);
@@ -1412,18 +1406,20 @@ test "Wayland seat selection treats XDG_SEAT as advisory but explicit configurat
14121406

14131407
test "Wayland core helper destroys its role before its surface" {
14141408
var symbols: linux.WaylandSymbols = undefined;
1415-
symbols.wl_proxy_marshal_array_flags = testMarshal;
1409+
symbols.wl_proxy_marshal_array_constructor_versioned = testMarshal;
14161410
symbols.wl_proxy_destroy = testRoleDestroy;
14171411
symbols.wl_proxy_get_version = testProxyVersion;
14181412
var connection = Connection.init(std.testing.allocator, &symbols, "", "", 1);
14191413
connection.helper_surface = @ptrFromInt(1);
14201414
connection.helper_shell_surface = @ptrFromInt(2);
1421-
test_role_destroyed_before_surface = false;
1415+
test_role_destroy_count = 0;
14221416
test_role_connection = &connection;
14231417

14241418
connection.destroyCoreHelper();
14251419

1426-
try std.testing.expect(test_role_destroyed_before_surface);
1420+
// Destroyed proxy and marshal count: the role first, then the surface after its request.
1421+
const expected = [_][2]usize{ .{ 2, 0 }, .{ 1, 1 } };
1422+
try std.testing.expectEqualSlices([2]usize, &expected, test_role_destroyed[0..test_role_destroy_count]);
14271423
}
14281424

14291425
test "Wayland connection setup applies the finite receive buffer cap and records an inherited socket" {
@@ -1505,7 +1501,7 @@ test "Wayland dispatch bulk-drains pending events without the 1.25 single-event
15051501

15061502
test "Wayland selection barrier orders admissions across in-flight syncs" {
15071503
var symbols: linux.WaylandSymbols = undefined;
1508-
symbols.wl_proxy_marshal_array_flags = testMarshal;
1504+
symbols.wl_proxy_marshal_array_constructor_versioned = testMarshal;
15091505
symbols.wl_proxy_add_listener = testAddListener;
15101506
symbols.wl_proxy_destroy = testDestroyProxy;
15111507
var connection = Connection.init(std.testing.allocator, &symbols, "", "", 1);
@@ -1538,14 +1534,14 @@ test "Wayland selection barrier orders admissions across in-flight syncs" {
15381534

15391535
test "Wayland selection barrier reports sync issue failure to the caller" {
15401536
var symbols: linux.WaylandSymbols = undefined;
1541-
symbols.wl_proxy_marshal_array_flags = testMarshalFailure;
1537+
symbols.wl_proxy_marshal_array_constructor_versioned = testMarshalFailure;
15421538
var connection = Connection.init(std.testing.allocator, &symbols, "", "", 1);
15431539
connection.display = @ptrFromInt(1);
15441540

15451541
try std.testing.expect(connection.requestSelectionBarrier() == null);
15461542
try std.testing.expect(connection.barrier_callback == null);
15471543

1548-
symbols.wl_proxy_marshal_array_flags = testMarshal;
1544+
symbols.wl_proxy_marshal_array_constructor_versioned = testMarshal;
15491545
symbols.wl_proxy_add_listener = testAddListener;
15501546
symbols.wl_proxy_destroy = testDestroyProxy;
15511547
var flush_failure = Connection.init(std.testing.allocator, &symbols, "", "", 1);
@@ -1642,7 +1638,7 @@ test "Wayland fatal selection flush preserves local ownership state" {
16421638
// Fake libwayland entry points for tests that drive a connection without a compositor.
16431639
pub fn testSymbols() linux.WaylandSymbols {
16441640
var symbols: linux.WaylandSymbols = undefined;
1645-
symbols.wl_proxy_marshal_array_flags = testMarshal;
1641+
symbols.wl_proxy_marshal_array_constructor_versioned = testMarshal;
16461642
symbols.wl_proxy_add_listener = testAddListener;
16471643
symbols.wl_proxy_destroy = testDestroyProxy;
16481644
symbols.wl_proxy_get_version = testProxyVersion;
@@ -1672,7 +1668,8 @@ var test_display_fd: std.posix.fd_t = -1;
16721668
var test_read_event_count: u32 = 0;
16731669
var test_display_error_call_count: u32 = 0;
16741670
var test_role_connection: ?*Connection = null;
1675-
var test_role_destroyed_before_surface = false;
1671+
var test_role_destroyed: [2][2]usize = undefined;
1672+
var test_role_destroy_count: usize = 0;
16761673

16771674
fn testSetMaxBufferSize(_: *linux.WlDisplay, size: usize) callconv(.c) void {
16781675
test_max_buffer_size = size;
@@ -1724,27 +1721,28 @@ fn testDisplayError(_: *linux.WlDisplay) callconv(.c) c_int {
17241721
fn testMarshal(
17251722
_: *WlProxy,
17261723
_: u32,
1724+
_: [*]WlArgument,
17271725
_: ?*const WlInterface,
17281726
_: u32,
1729-
_: u32,
1730-
_: [*]WlArgument,
17311727
) callconv(.c) ?*WlProxy {
17321728
return @ptrFromInt(4);
17331729
}
17341730

17351731
fn testMarshalFailure(
1736-
_: *WlProxy,
1732+
proxy: *WlProxy,
17371733
_: u32,
1734+
arguments: [*]WlArgument,
17381735
_: ?*const WlInterface,
1739-
_: u32,
1740-
_: u32,
1741-
_: [*]WlArgument,
1736+
version: u32,
17421737
) callconv(.c) ?*WlProxy {
1738+
// The seat test binds global 3 through registry 3: the bind arguments must reach libwayland.
1739+
if (@intFromPtr(proxy) == 3) std.debug.assert(arguments[0].u == 3 and arguments[2].u == version);
17431740
return null;
17441741
}
17451742

17461743
fn testRoleDestroy(proxy: *WlProxy) callconv(.c) void {
1747-
test_role_destroyed_before_surface = @intFromPtr(proxy) == 2 and test_role_connection.?.test_marshal_count == 0;
1744+
test_role_destroyed[test_role_destroy_count] = .{ @intFromPtr(proxy), test_role_connection.?.test_marshal_count };
1745+
test_role_destroy_count += 1;
17481746
}
17491747

17501748
fn testAddListener(_: *WlProxy, _: [*]const *const anyopaque, _: ?*anyopaque) callconv(.c) c_int {
@@ -1886,7 +1884,8 @@ test "Wayland core helper requires an advertised keyboard capability" {
18861884

18871885
test "Wayland core focus lifecycle rejects stale and unfocused offers" {
18881886
var symbols: linux.WaylandSymbols = undefined;
1889-
symbols.wl_proxy_marshal_array_flags = testMarshal;
1887+
symbols.wl_proxy_marshal_array_constructor_versioned = testMarshal;
1888+
symbols.wl_proxy_destroy = testDestroyProxy;
18901889
symbols.wl_proxy_get_version = testProxyVersion;
18911890
const offer: *WlProxy = @ptrFromInt(1);
18921891
const surface: *WlProxy = @ptrFromInt(2);
@@ -1981,7 +1980,8 @@ test "Wayland bound core seat keyboard loss fails active and future acquisitions
19811980

19821981
test "Wayland failed connection cancels providers and frees idle generations" {
19831982
var symbols: linux.WaylandSymbols = undefined;
1984-
symbols.wl_proxy_marshal_array_flags = testMarshal;
1983+
symbols.wl_proxy_marshal_array_constructor_versioned = testMarshal;
1984+
symbols.wl_proxy_destroy = testDestroyProxy;
19851985
symbols.wl_proxy_get_version = testProxyVersion;
19861986
var connection = Connection.init(std.testing.allocator, &symbols, "", "", 1);
19871987
const provider = try std.testing.allocator.create(Provider);
@@ -2004,7 +2004,8 @@ test "Wayland failed connection cancels providers and frees idle generations" {
20042004
test "Wayland failed connection lets active transfers drain but rejects new sends" {
20052005
if (comptime builtin.os.tag != .linux) return error.SkipZigTest;
20062006
var symbols: linux.WaylandSymbols = undefined;
2007-
symbols.wl_proxy_marshal_array_flags = testMarshal;
2007+
symbols.wl_proxy_marshal_array_constructor_versioned = testMarshal;
2008+
symbols.wl_proxy_destroy = testDestroyProxy;
20082009
symbols.wl_proxy_get_version = testProxyVersion;
20092010
var connection = Connection.init(std.testing.allocator, &symbols, "", "", 1);
20102011
const provider = try std.testing.allocator.create(Provider);
@@ -2104,7 +2105,7 @@ test "Wayland publication reclaims cancelled providers before checking capacity"
21042105

21052106
test "Wayland clear retires the current provider at the generation bound" {
21062107
var symbols: linux.WaylandSymbols = undefined;
2107-
symbols.wl_proxy_marshal_array_flags = testMarshal;
2108+
symbols.wl_proxy_marshal_array_constructor_versioned = testMarshal;
21082109
symbols.wl_proxy_destroy = testDestroyProxy;
21092110
symbols.wl_proxy_get_version = testProxyVersion;
21102111
var connection = testReadyConnection(&symbols, .{ .result = 0, .errno = .SUCCESS });

0 commit comments

Comments
 (0)