river/riverctl/main.zig
Isaac Freund 871fc7c8de river-options: remove protocol
This protocol involves far too much accidental complexity. The original
motivating use-case was to provide a convenient way to send arbitrary
data to layout clients at runtime in order to avoid layout clients
needing to implement their own IPC and do this over a side-channel.
Instead of implementing a quite complex but still rigid options protocol
and storing this state in the compositor, instead we will simply add
events to the layout protocol to support this use case.

Consider the status quo event sequence:

1. send get_option_handle request (riverctl)
2. roundtrip waiting for first event (riverctl)
3. send set_foo_value request (riverctl)
4. receive set_foo_value request (river)
5. send foo_value event to all current handles (river)
6. receive foo_value event (rivertile)
7. send parameters_changed request (rivertile)
8. receive parameters_changed request (river)
9. send layout_demand (river)

And compare with the event sequence after the proposed change:

1. send set_foo_value request (riverctl)
2. receive set_foo_value request (river)
3. send set_foo_value event (river)
4. send layout_demand (river)

This requires *much* less back and forth between the server and clients
and is clearly much simpler.
2021-04-27 00:10:20 +02:00

109 lines
3.9 KiB
Zig

// This file is part of river, a dynamic tiling wayland compositor.
//
// Copyright 2020-2021 The River Developers
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const mem = std.mem;
const os = std.os;
const assert = std.debug.assert;
const wayland = @import("wayland");
const wl = wayland.client.wl;
const zriver = wayland.client.zriver;
const gpa = std.heap.c_allocator;
pub const Globals = struct {
control: ?*zriver.ControlV1 = null,
seat: ?*wl.Seat = null,
};
pub fn main() !void {
_main() catch |err| {
if (std.builtin.mode == .Debug)
return err;
switch (err) {
error.RiverControlNotAdvertised => printErrorExit(
\\The Wayland server does not support river-control-unstable-v1.
\\Do your versions of river and riverctl match?
, .{}),
error.SeatNotAdverstised => printErrorExit(
\\The Wayland server did not advertise any seat.
, .{}),
else => return err,
}
};
}
fn _main() !void {
const display = try wl.Display.connect(null);
const registry = try display.getRegistry();
var globals = Globals{};
registry.setListener(*Globals, registryListener, &globals) catch unreachable;
_ = try display.roundtrip();
const control = globals.control orelse return error.RiverControlNotAdvertised;
const seat = globals.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 = os.argv;
for (args[1..]) |arg| control.addArgument(arg);
const callback = try control.runCommand(seat);
callback.setListener(?*c_void, callbackListener, null) catch unreachable;
// Loop until our callback is called and we exit.
while (true) _ = try display.dispatch();
}
fn registryListener(registry: *wl.Registry, event: wl.Registry.Event, globals: *Globals) void {
switch (event) {
.global => |global| {
if (std.cstr.cmp(global.interface, wl.Seat.getInterface().name) == 0) {
assert(globals.seat == null); // TODO: support multiple seats
globals.seat = registry.bind(global.name, wl.Seat, 1) catch @panic("out of memory");
} else if (std.cstr.cmp(global.interface, zriver.ControlV1.getInterface().name) == 0) {
globals.control = registry.bind(global.name, zriver.ControlV1, 1) catch @panic("out of memory");
}
},
.global_remove => {},
}
}
fn callbackListener(callback: *zriver.CommandCallbackV1, event: zriver.CommandCallbackV1.Event, _: ?*c_void) void {
switch (event) {
.success => |success| {
if (mem.len(success.output) > 0) {
const stdout = std.io.getStdOut().outStream();
stdout.print("{}\n", .{success.output}) catch @panic("failed to write to stdout");
}
os.exit(0);
},
.failure => |failure| printErrorExit("Error: {}\n", .{failure.failure_message}),
}
}
pub fn printErrorExit(comptime format: []const u8, args: anytype) noreturn {
const stderr = std.io.getStdErr().outStream();
stderr.print("err: " ++ format ++ "\n", args) catch std.os.exit(1);
std.os.exit(1);
}