From a7459026f62481707a03ab2064f12a761c75f2f2 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Mon, 2 Nov 2020 13:59:59 +0100 Subject: [PATCH] code: port riverctl to zig-wayland --- README.md | 6 +++ build.zig | 26 +++++++---- deps/zig-wayland | 2 +- riverctl/main.zig | 116 +++++++++++++++++----------------------------- 4 files changed, 66 insertions(+), 84 deletions(-) diff --git a/README.md b/README.md index 9f98ba4..963f51c 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,12 @@ separate `riverctl` binary implementing it. ## Building +On cloning the repository, you must init and update the submodules as well with e.g. + +``` +git submodule update --init +``` + To compile river first ensure that you have the following dependencies installed: diff --git a/build.zig b/build.zig index 332f11c..ff906b8 100644 --- a/build.zig +++ b/build.zig @@ -1,5 +1,7 @@ const std = @import("std"); +const ScanProtocolsStep = @import("deps/zig-wayland/build.zig").ScanProtocolsStep; + pub fn build(b: *std.build.Builder) !void { // Standard target options allows the person running `zig build` to choose // what target to build for. Here we do not override the defaults, which @@ -35,7 +37,11 @@ pub fn build(b: *std.build.Builder) !void { "Set to true to build examples", ) orelse false; - const scan_protocols = ScanProtocolsStep.create(b); + // TODO: port all parts of river to zig-wayland and delete this + const scan_protocols = OldScanProtocolsStep.create(b); + + const scanner = ScanProtocolsStep.create(b, "deps/zig-wayland/"); + scanner.addProtocolPath("protocol/river-control-unstable-v1.xml"); { const river = b.addExecutable("river", "river/main.zig"); @@ -60,11 +66,13 @@ pub fn build(b: *std.build.Builder) !void { riverctl.setTarget(target); riverctl.setBuildMode(mode); - addProtocolDeps(riverctl, &scan_protocols.step); - + riverctl.step.dependOn(&scanner.step); + riverctl.addPackage(scanner.getPkg()); riverctl.linkLibC(); riverctl.linkSystemLibrary("wayland-client"); + scanner.addCSource(riverctl); + riverctl.install(); } @@ -126,25 +134,25 @@ fn addProtocolDeps(exe: *std.build.LibExeObjStep, protocol_step: *std.build.Step exe.addCSourceFile("protocol/river-status-unstable-v1-protocol.c", &[_][]const u8{"-std=c99"}); } -const ScanProtocolsStep = struct { +const OldScanProtocolsStep = struct { builder: *std.build.Builder, step: std.build.Step, - fn create(builder: *std.build.Builder) *ScanProtocolsStep { - const self = builder.allocator.create(ScanProtocolsStep) catch @panic("out of memory"); + fn create(builder: *std.build.Builder) *OldScanProtocolsStep { + const self = builder.allocator.create(OldScanProtocolsStep) catch @panic("out of memory"); self.* = init(builder); return self; } - fn init(builder: *std.build.Builder) ScanProtocolsStep { - return ScanProtocolsStep{ + fn init(builder: *std.build.Builder) OldScanProtocolsStep { + return OldScanProtocolsStep{ .builder = builder, .step = std.build.Step.init(.Custom, "Scan Protocols", builder.allocator, make), }; } fn make(step: *std.build.Step) !void { - const self = @fieldParentPtr(ScanProtocolsStep, "step", step); + const self = @fieldParentPtr(OldScanProtocolsStep, "step", step); const protocol_dir = std.fmt.trim(try self.builder.exec( &[_][]const u8{ "pkg-config", "--variable=pkgdatadir", "wayland-protocols" }, diff --git a/deps/zig-wayland b/deps/zig-wayland index f335337..d0fcd31 160000 --- a/deps/zig-wayland +++ b/deps/zig-wayland @@ -1 +1 @@ -Subproject commit f335337bca742c8f0181798be75de65bfb4e8283 +Subproject commit d0fcd31b54c930d58267ebaf03a898c2d01ccfa4 diff --git a/riverctl/main.zig b/riverctl/main.zig index ec804d7..7bfb448 100644 --- a/riverctl/main.zig +++ b/riverctl/main.zig @@ -17,97 +17,65 @@ const std = @import("std"); -const c = @cImport({ - @cInclude("wayland-client.h"); - @cInclude("river-control-unstable-v1-client-protocol.h"); -}); +const wayland = @import("wayland"); +const wl = wayland.client.wl; +const river = wayland.client.river; -const wl_registry_listener = c.wl_registry_listener{ - .global = handleGlobal, - .global_remove = handleGlobalRemove, +const SetupContext = struct { + river_control: ?*river.ControlV1 = null, + seat: ?*wl.Seat = null, }; -const command_callback_listener = c.zriver_command_callback_v1_listener{ - .success = handleSuccess, - .failure = handleFailure, -}; - -var river_control_optional: ?*c.zriver_control_v1 = null; -var wl_seat_optional: ?*c.wl_seat = null; - pub fn main() !void { - const wl_display = c.wl_display_connect(null) orelse return error.ConnectError; - const wl_registry = c.wl_display_get_registry(wl_display); + const display = try wl.Display.connect(null); + const registry = try display.getRegistry(); - if (c.wl_registry_add_listener(wl_registry, &wl_registry_listener, null) < 0) unreachable; - if (c.wl_display_roundtrip(wl_display) < 0) return error.RoundtripFailed; + var context = SetupContext{}; - const river_control = river_control_optional orelse return error.RiverControlNotAdvertised; - const wl_seat = wl_seat_optional orelse return error.SeatNotAdverstised; + registry.setListener(*SetupContext, registryListener, &context) catch unreachable; + _ = try display.roundtrip(); + + const river_control = context.river_control orelse return error.RiverControlNotAdvertised; + const seat = context.seat orelse return error.SeatNotAdverstised; // Skip our name, send all other args // This next line is needed cause of https://github.com/ziglang/zig/issues/2622 const args = std.os.argv; - for (args[1..]) |arg| c.zriver_control_v1_add_argument(river_control, arg); + for (args[1..]) |arg| river_control.addArgument(arg); - const command_callback = c.zriver_control_v1_run_command(river_control, wl_seat); - if (c.zriver_command_callback_v1_add_listener( - command_callback, - &command_callback_listener, - null, - ) < 0) unreachable; + const callback = try river_control.runCommand(seat); + + callback.setListener(?*c_void, callbackListener, null) catch unreachable; // Loop until our callback is called and we exit. - while (true) if (c.wl_display_dispatch(wl_display) < 0) return error.DispatchFailed; + while (true) _ = try display.dispatch(); } -fn handleGlobal( - data: ?*c_void, - wl_registry: ?*c.wl_registry, - name: u32, - interface: ?[*:0]const u8, - version: u32, -) callconv(.C) void { - // We only care about the river_control global - if (std.cstr.cmp(interface.?, @ptrCast([*:0]const u8, c.zriver_control_v1_interface.name.?)) == 0) { - river_control_optional = @ptrCast( - *c.zriver_control_v1, - c.wl_registry_bind(wl_registry, name, &c.zriver_control_v1_interface, 1), - ); - } else if (std.cstr.cmp(interface.?, @ptrCast([*:0]const u8, c.wl_seat_interface.name.?)) == 0) { - // river does not yet support multi-seat, so just use the first - // (and only) seat advertised - wl_seat_optional = @ptrCast( - *c.wl_seat, - c.wl_registry_bind(wl_registry, name, &c.wl_seat_interface, 1), - ); +fn registryListener(registry: *wl.Registry, event: wl.Registry.Event, context: *SetupContext) void { + switch (event) { + .global => |global| { + if (context.seat == null and std.cstr.cmp(global.interface, wl.Seat.interface().name) == 0) { + context.seat = registry.bind(global.name, wl.Seat, 1) catch return; + } else if (std.cstr.cmp(global.interface, river.ControlV1.interface().name) == 0) { + context.river_control = registry.bind(global.name, river.ControlV1, 1) catch return; + } + }, + .global_remove => {}, } } -/// Ignore the event -fn handleGlobalRemove(data: ?*c_void, wl_registry: ?*c.wl_registry, name: u32) callconv(.C) void {} - -/// Print the output of the command if any and exit -fn handleSuccess( - data: ?*c_void, - callback: ?*c.zriver_command_callback_v1, - output: ?[*:0]const u8, -) callconv(.C) void { - if (std.mem.len(output.?) > 0) { - const stdout = std.io.getStdOut().outStream(); - stdout.print("{}\n", .{output}) catch @panic("failed to write to stdout"); +fn callbackListener(callback: *river.CommandCallbackV1, event: river.CommandCallbackV1.Event, _: ?*c_void) void { + switch (event) { + .success => |success| { + if (std.mem.len(success.output) > 0) { + const stdout = std.io.getStdOut().outStream(); + stdout.print("{}\n", .{success.output}) catch @panic("failed to write to stdout"); + } + std.os.exit(0); + }, + .failure => |failure| { + std.debug.print("Error: {}\n", .{failure.failure_message}); + std.os.exit(1); + }, } - std.os.exit(0); -} - -/// Print the failure message and exit non-zero -fn handleFailure( - data: ?*c_void, - callback: ?*c.zriver_command_callback_v1, - failure_message: ?[*:0]const u8, -) callconv(.C) void { - if (failure_message) |message| { - std.debug.warn("Error: {}\n", .{failure_message}); - } - std.os.exit(1); }