code: port riverctl to zig-wayland
This commit is contained in:
parent
a895970561
commit
a7459026f6
@ -20,6 +20,12 @@ separate `riverctl` binary implementing it.
|
|||||||
|
|
||||||
## Building
|
## 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
|
To compile river first ensure that you have the following dependencies
|
||||||
installed:
|
installed:
|
||||||
|
|
||||||
|
26
build.zig
26
build.zig
@ -1,5 +1,7 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
|
const ScanProtocolsStep = @import("deps/zig-wayland/build.zig").ScanProtocolsStep;
|
||||||
|
|
||||||
pub fn build(b: *std.build.Builder) !void {
|
pub fn build(b: *std.build.Builder) !void {
|
||||||
// Standard target options allows the person running `zig build` to choose
|
// Standard target options allows the person running `zig build` to choose
|
||||||
// what target to build for. Here we do not override the defaults, which
|
// 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",
|
"Set to true to build examples",
|
||||||
) orelse false;
|
) 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");
|
const river = b.addExecutable("river", "river/main.zig");
|
||||||
@ -60,11 +66,13 @@ pub fn build(b: *std.build.Builder) !void {
|
|||||||
riverctl.setTarget(target);
|
riverctl.setTarget(target);
|
||||||
riverctl.setBuildMode(mode);
|
riverctl.setBuildMode(mode);
|
||||||
|
|
||||||
addProtocolDeps(riverctl, &scan_protocols.step);
|
riverctl.step.dependOn(&scanner.step);
|
||||||
|
riverctl.addPackage(scanner.getPkg());
|
||||||
riverctl.linkLibC();
|
riverctl.linkLibC();
|
||||||
riverctl.linkSystemLibrary("wayland-client");
|
riverctl.linkSystemLibrary("wayland-client");
|
||||||
|
|
||||||
|
scanner.addCSource(riverctl);
|
||||||
|
|
||||||
riverctl.install();
|
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"});
|
exe.addCSourceFile("protocol/river-status-unstable-v1-protocol.c", &[_][]const u8{"-std=c99"});
|
||||||
}
|
}
|
||||||
|
|
||||||
const ScanProtocolsStep = struct {
|
const OldScanProtocolsStep = struct {
|
||||||
builder: *std.build.Builder,
|
builder: *std.build.Builder,
|
||||||
step: std.build.Step,
|
step: std.build.Step,
|
||||||
|
|
||||||
fn create(builder: *std.build.Builder) *ScanProtocolsStep {
|
fn create(builder: *std.build.Builder) *OldScanProtocolsStep {
|
||||||
const self = builder.allocator.create(ScanProtocolsStep) catch @panic("out of memory");
|
const self = builder.allocator.create(OldScanProtocolsStep) catch @panic("out of memory");
|
||||||
self.* = init(builder);
|
self.* = init(builder);
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init(builder: *std.build.Builder) ScanProtocolsStep {
|
fn init(builder: *std.build.Builder) OldScanProtocolsStep {
|
||||||
return ScanProtocolsStep{
|
return OldScanProtocolsStep{
|
||||||
.builder = builder,
|
.builder = builder,
|
||||||
.step = std.build.Step.init(.Custom, "Scan Protocols", builder.allocator, make),
|
.step = std.build.Step.init(.Custom, "Scan Protocols", builder.allocator, make),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make(step: *std.build.Step) !void {
|
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 protocol_dir = std.fmt.trim(try self.builder.exec(
|
||||||
&[_][]const u8{ "pkg-config", "--variable=pkgdatadir", "wayland-protocols" },
|
&[_][]const u8{ "pkg-config", "--variable=pkgdatadir", "wayland-protocols" },
|
||||||
|
2
deps/zig-wayland
vendored
2
deps/zig-wayland
vendored
@ -1 +1 @@
|
|||||||
Subproject commit f335337bca742c8f0181798be75de65bfb4e8283
|
Subproject commit d0fcd31b54c930d58267ebaf03a898c2d01ccfa4
|
@ -17,97 +17,65 @@
|
|||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
const c = @cImport({
|
const wayland = @import("wayland");
|
||||||
@cInclude("wayland-client.h");
|
const wl = wayland.client.wl;
|
||||||
@cInclude("river-control-unstable-v1-client-protocol.h");
|
const river = wayland.client.river;
|
||||||
});
|
|
||||||
|
|
||||||
const wl_registry_listener = c.wl_registry_listener{
|
const SetupContext = struct {
|
||||||
.global = handleGlobal,
|
river_control: ?*river.ControlV1 = null,
|
||||||
.global_remove = handleGlobalRemove,
|
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 {
|
pub fn main() !void {
|
||||||
const wl_display = c.wl_display_connect(null) orelse return error.ConnectError;
|
const display = try wl.Display.connect(null);
|
||||||
const wl_registry = c.wl_display_get_registry(wl_display);
|
const registry = try display.getRegistry();
|
||||||
|
|
||||||
if (c.wl_registry_add_listener(wl_registry, &wl_registry_listener, null) < 0) unreachable;
|
var context = SetupContext{};
|
||||||
if (c.wl_display_roundtrip(wl_display) < 0) return error.RoundtripFailed;
|
|
||||||
|
|
||||||
const river_control = river_control_optional orelse return error.RiverControlNotAdvertised;
|
registry.setListener(*SetupContext, registryListener, &context) catch unreachable;
|
||||||
const wl_seat = wl_seat_optional orelse return error.SeatNotAdverstised;
|
_ = 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
|
// Skip our name, send all other args
|
||||||
// This next line is needed cause of https://github.com/ziglang/zig/issues/2622
|
// This next line is needed cause of https://github.com/ziglang/zig/issues/2622
|
||||||
const args = std.os.argv;
|
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);
|
const callback = try river_control.runCommand(seat);
|
||||||
if (c.zriver_command_callback_v1_add_listener(
|
|
||||||
command_callback,
|
callback.setListener(?*c_void, callbackListener, null) catch unreachable;
|
||||||
&command_callback_listener,
|
|
||||||
null,
|
|
||||||
) < 0) unreachable;
|
|
||||||
|
|
||||||
// Loop until our callback is called and we exit.
|
// 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(
|
fn registryListener(registry: *wl.Registry, event: wl.Registry.Event, context: *SetupContext) void {
|
||||||
data: ?*c_void,
|
switch (event) {
|
||||||
wl_registry: ?*c.wl_registry,
|
.global => |global| {
|
||||||
name: u32,
|
if (context.seat == null and std.cstr.cmp(global.interface, wl.Seat.interface().name) == 0) {
|
||||||
interface: ?[*:0]const u8,
|
context.seat = registry.bind(global.name, wl.Seat, 1) catch return;
|
||||||
version: u32,
|
} else if (std.cstr.cmp(global.interface, river.ControlV1.interface().name) == 0) {
|
||||||
) callconv(.C) void {
|
context.river_control = registry.bind(global.name, river.ControlV1, 1) catch return;
|
||||||
// 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(
|
.global_remove => {},
|
||||||
*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),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ignore the event
|
fn callbackListener(callback: *river.CommandCallbackV1, event: river.CommandCallbackV1.Event, _: ?*c_void) void {
|
||||||
fn handleGlobalRemove(data: ?*c_void, wl_registry: ?*c.wl_registry, name: u32) callconv(.C) void {}
|
switch (event) {
|
||||||
|
.success => |success| {
|
||||||
/// Print the output of the command if any and exit
|
if (std.mem.len(success.output) > 0) {
|
||||||
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();
|
const stdout = std.io.getStdOut().outStream();
|
||||||
stdout.print("{}\n", .{output}) catch @panic("failed to write to stdout");
|
stdout.print("{}\n", .{success.output}) catch @panic("failed to write to stdout");
|
||||||
}
|
}
|
||||||
std.os.exit(0);
|
std.os.exit(0);
|
||||||
}
|
},
|
||||||
|
.failure => |failure| {
|
||||||
/// Print the failure message and exit non-zero
|
std.debug.print("Error: {}\n", .{failure.failure_message});
|
||||||
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);
|
std.os.exit(1);
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user