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.
This commit is contained in:
Isaac Freund
2021-04-26 21:03:04 +02:00
parent a6f908d7eb
commit 871fc7c8de
17 changed files with 65 additions and 1449 deletions

View File

@ -1,119 +0,0 @@
// This file is part of river, a dynamic tiling wayland compositor.
//
// Copyright 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 cstr = std.cstr;
const root = @import("root");
pub const FlagDef = struct {
name: [*:0]const u8,
kind: enum { boolean, arg },
};
pub fn Args(comptime num_positionals: comptime_int, comptime flag_defs: []const FlagDef) type {
return struct {
const Self = @This();
positionals: [num_positionals][*:0]const u8,
flags: [flag_defs.len]struct {
name: [*:0]const u8,
value: union {
boolean: bool,
arg: ?[*:0]const u8,
},
},
pub fn parse(argv: [][*:0]const u8) Self {
var ret: Self = undefined;
// Init all flags in the flags array to false/null
inline for (flag_defs) |flag_def, flag_idx| {
switch (flag_def.kind) {
.boolean => ret.flags[flag_idx] = .{
.name = flag_def.name,
.value = .{ .boolean = false },
},
.arg => ret.flags[flag_idx] = .{
.name = flag_def.name,
.value = .{ .arg = null },
},
}
}
// Parse the argv in to the positionals and flags arrays
var arg_idx: usize = 0;
var positional_idx: usize = 0;
outer: while (arg_idx < argv.len) : (arg_idx += 1) {
var should_continue = false;
inline for (flag_defs) |flag_def, flag_idx| {
if (cstr.cmp(flag_def.name, argv[arg_idx]) == 0) {
switch (flag_def.kind) {
.boolean => ret.flags[flag_idx].value.boolean = true,
.arg => {
arg_idx += 1;
ret.flags[flag_idx].value.arg = if (arg_idx < argv.len)
argv[arg_idx]
else
root.printErrorExit("flag '" ++ flag_def.name ++
"' requires an argument but none was provided!", .{});
},
}
// TODO: this variable exists as a workaround for the fact that
// using continue :outer here crashes the stage1 compiler.
should_continue = true;
}
}
if (should_continue) continue;
if (positional_idx == num_positionals) {
root.printErrorExit(
"{} positional arguments expected but more were provided!",
.{num_positionals},
);
}
ret.positionals[positional_idx] = argv[arg_idx];
positional_idx += 1;
}
if (positional_idx < num_positionals) {
root.printErrorExit(
"{} positional arguments expected but only {} were provided!",
.{ num_positionals, positional_idx },
);
}
return ret;
}
pub fn boolFlag(self: Self, flag_name: [*:0]const u8) bool {
for (self.flags) |flag| {
if (cstr.cmp(flag.name, flag_name) == 0) return flag.value.boolean;
}
unreachable;
}
pub fn argFlag(self: Self, flag_name: [*:0]const u8) ?[*:0]const u8 {
for (self.flags) |flag| {
if (cstr.cmp(flag.name, flag_name) == 0) return flag.value.arg;
}
unreachable;
}
};
}

View File

@ -22,26 +22,13 @@ const assert = std.debug.assert;
const wayland = @import("wayland");
const wl = wayland.client.wl;
const river = wayland.client.river;
const zriver = wayland.client.zriver;
const zxdg = wayland.client.zxdg;
const gpa = std.heap.c_allocator;
const options = @import("options.zig");
pub const Output = struct {
wl_output: *wl.Output,
name: []const u8,
};
pub const Globals = struct {
control: ?*zriver.ControlV1 = null,
options_manager: ?*river.OptionsManagerV2 = null,
status_manager: ?*zriver.StatusManagerV1 = null,
seat: ?*wl.Seat = null,
output_manager: ?*zxdg.OutputManagerV1 = null,
outputs: std.ArrayList(Output) = std.ArrayList(Output).init(gpa),
};
pub fn main() !void {
@ -54,20 +41,9 @@ pub fn main() !void {
\\The Wayland server does not support river-control-unstable-v1.
\\Do your versions of river and riverctl match?
, .{}),
error.RiverStatusManagerNotAdvertised => printErrorExit(
\\The Wayland server does not support river-status-unstable-v1.
\\Do your versions of river and riverctl match?
, .{}),
error.RiverOptionsManagerNotAdvertised => printErrorExit(
\\The Wayland server does not support river-options-unstable-v1.
\\Do your versions of river and riverctl match?
, .{}),
error.SeatNotAdverstised => printErrorExit(
\\The Wayland server did not advertise any seat.
, .{}),
error.XdgOutputNotAdvertised => printErrorExit(
\\The Wayland server does not support xdg-output-unstable-v1.
, .{}),
else => return err,
}
};
@ -82,32 +58,20 @@ fn _main() !void {
registry.setListener(*Globals, registryListener, &globals) catch unreachable;
_ = try display.roundtrip();
if (os.argv.len > 2 and mem.eql(u8, "declare-option", mem.span(os.argv[1]))) {
try options.declareOption(display, &globals);
} else if (os.argv.len > 2 and mem.eql(u8, "get-option", mem.span(os.argv[1]))) {
try options.getOption(display, &globals);
} else if (os.argv.len > 2 and mem.eql(u8, "set-option", mem.span(os.argv[1]))) {
try options.setOption(display, &globals);
} else if (os.argv.len > 2 and mem.eql(u8, "unset-option", mem.span(os.argv[1]))) {
try options.unsetOption(display, &globals);
} else if (os.argv.len > 2 and mem.eql(u8, "mod-option", mem.span(os.argv[1]))) {
try options.modOption(display, &globals);
} else {
const control = globals.control orelse return error.RiverControlNotAdvertised;
const seat = globals.seat orelse return error.SeatNotAdverstised;
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);
// 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);
const callback = try control.runCommand(seat);
callback.setListener(?*c_void, callbackListener, null) catch unreachable;
callback.setListener(?*c_void, callbackListener, null) catch unreachable;
// Loop until our callback is called and we exit.
while (true) _ = try display.dispatch();
}
// 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 {
@ -118,15 +82,6 @@ fn registryListener(registry: *wl.Registry, event: wl.Registry.Event, globals: *
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");
} else if (std.cstr.cmp(global.interface, river.OptionsManagerV2.getInterface().name) == 0) {
globals.options_manager = registry.bind(global.name, river.OptionsManagerV2, 1) catch @panic("out of memory");
} else if (std.cstr.cmp(global.interface, zriver.StatusManagerV1.getInterface().name) == 0) {
globals.status_manager = registry.bind(global.name, zriver.StatusManagerV1, 1) catch @panic("out of memory");
} else if (std.cstr.cmp(global.interface, zxdg.OutputManagerV1.getInterface().name) == 0 and global.version >= 2) {
globals.output_manager = registry.bind(global.name, zxdg.OutputManagerV1, 2) catch @panic("out of memory");
} else if (std.cstr.cmp(global.interface, wl.Output.getInterface().name) == 0) {
const output = registry.bind(global.name, wl.Output, 1) catch @panic("out of memory");
globals.outputs.append(.{ .wl_output = output, .name = undefined }) catch @panic("out of memory");
}
},
.global_remove => {},
@ -142,10 +97,7 @@ fn callbackListener(callback: *zriver.CommandCallbackV1, event: zriver.CommandCa
}
os.exit(0);
},
.failure => |failure| {
std.debug.print("Error: {}\n", .{failure.failure_message});
os.exit(1);
},
.failure => |failure| printErrorExit("Error: {}\n", .{failure.failure_message}),
}
}

View File

@ -1,320 +0,0 @@
// This file is part of river, a dynamic tiling wayland compositor.
//
// Copyright 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 os = std.os;
const math = std.math;
const mem = std.mem;
const fmt = std.fmt;
const wayland = @import("wayland");
const wl = wayland.client.wl;
const river = wayland.client.river;
const zriver = wayland.client.zriver;
const zxdg = wayland.client.zxdg;
const root = @import("root");
const Args = @import("args.zig").Args;
const FlagDef = @import("args.zig").FlagDef;
const Globals = @import("main.zig").Globals;
const Output = @import("main.zig").Output;
const ValueType = enum {
int,
uint,
fixed,
string,
};
const Context = struct {
display: *wl.Display,
key: [*:0]const u8,
raw_value: [*:0]const u8,
output: ?*Output,
};
pub fn declareOption(display: *wl.Display, globals: *Globals) !void {
// https://github.com/ziglang/zig/issues/7807
const argv: [][*:0]const u8 = os.argv;
const args = Args(3, &[_]FlagDef{}).parse(argv[2..]);
const key = args.positionals[0];
const value_type = std.meta.stringToEnum(ValueType, mem.span(args.positionals[1])) orelse {
root.printErrorExit(
"'{}' is not a valid type, must be int, uint, fixed, or string",
.{args.positionals[1]},
);
};
const raw_value = args.positionals[2];
const options_manager = globals.options_manager orelse return error.RiverOptionsManagerNotAdvertised;
switch (value_type) {
.int => options_manager.declareIntOption(key, parseInt(raw_value)),
.uint => options_manager.declareUintOption(key, parseUint(raw_value)),
.fixed => options_manager.declareFixedOption(key, parseFixed(raw_value)),
.string => options_manager.declareStringOption(key, raw_value),
}
_ = try display.flush();
}
fn parseInt(raw_value: [*:0]const u8) i32 {
return fmt.parseInt(i32, mem.span(raw_value), 10) catch
root.printErrorExit("{} is not a valid int", .{raw_value});
}
fn parseUint(raw_value: [*:0]const u8) u32 {
return fmt.parseInt(u32, mem.span(raw_value), 10) catch
root.printErrorExit("{} is not a valid uint", .{raw_value});
}
fn parseFixed(raw_value: [*:0]const u8) wl.Fixed {
return wl.Fixed.fromDouble(fmt.parseFloat(f64, mem.span(raw_value)) catch
root.printErrorExit("{} is not a valid fixed", .{raw_value}));
}
pub fn getOption(display: *wl.Display, globals: *Globals) !void {
// https://github.com/ziglang/zig/issues/7807
const argv: [][*:0]const u8 = os.argv;
const args = Args(1, &[_]FlagDef{
.{ .name = "-output", .kind = .arg },
.{ .name = "-focused-output", .kind = .boolean },
}).parse(argv[2..]);
const output = if (args.argFlag("-output")) |o|
try parseOutputName(display, globals, o)
else if (args.boolFlag("-focused-output"))
try getFocusedOutput(display, globals)
else
null;
const ctx = Context{
.display = display,
.key = args.positionals[0],
.raw_value = undefined,
.output = output,
};
const options_manager = globals.options_manager orelse return error.RiverOptionsManagerNotAdvertised;
const handle = try options_manager.getOptionHandle(ctx.key, if (ctx.output) |o| o.wl_output else null);
handle.setListener(*const Context, getOptionListener, &ctx) catch unreachable;
// We always exit when our listener is called
while (true) _ = try display.dispatch();
}
pub fn setOption(display: *wl.Display, globals: *Globals) !void {
// https://github.com/ziglang/zig/issues/7807
const argv: [][*:0]const u8 = os.argv;
const args = Args(2, &[_]FlagDef{
.{ .name = "-output", .kind = .arg },
.{ .name = "-focused-output", .kind = .boolean },
}).parse(argv[2..]);
const output = if (args.argFlag("-output")) |o|
try parseOutputName(display, globals, o)
else if (args.boolFlag("-focused-output"))
try getFocusedOutput(display, globals)
else
null;
const ctx = Context{
.display = display,
.key = args.positionals[0],
.raw_value = args.positionals[1],
.output = output,
};
const options_manager = globals.options_manager orelse return error.RiverOptionsManagerNotAdvertised;
const handle = try options_manager.getOptionHandle(ctx.key, if (ctx.output) |o| o.wl_output else null);
handle.setListener(*const Context, setOptionListener, &ctx) catch unreachable;
// We always exit when our listener is called
while (true) _ = try display.dispatch();
}
pub fn unsetOption(display: *wl.Display, globals: *Globals) !void {
// https://github.com/ziglang/zig/issues/7807
const argv: [][*:0]const u8 = os.argv;
const args = Args(1, &[_]FlagDef{
.{ .name = "-output", .kind = .arg },
.{ .name = "-focused-output", .kind = .boolean },
}).parse(argv[2..]);
const output = if (args.argFlag("-output")) |o|
try parseOutputName(display, globals, o)
else if (args.boolFlag("-focused-output"))
try getFocusedOutput(display, globals)
else
root.printErrorExit("unset requires either -output or -focused-output", .{});
const key = args.positionals[0];
const options_manager = globals.options_manager orelse return error.RiverOptionsManagerNotAdvertised;
options_manager.unsetOption(key, output.wl_output);
_ = try display.flush();
}
pub fn modOption(display: *wl.Display, globals: *Globals) !void {
// https://github.com/ziglang/zig/issues/7807
const argv: [][*:0]const u8 = os.argv;
const args = Args(2, &[_]FlagDef{
.{ .name = "-output", .kind = .arg },
.{ .name = "-focused-output", .kind = .boolean },
}).parse(argv[2..]);
const output = if (args.argFlag("-output")) |o|
try parseOutputName(display, globals, o)
else if (args.boolFlag("-focused-output"))
try getFocusedOutput(display, globals)
else
null;
const ctx = Context{
.display = display,
.key = args.positionals[0],
.raw_value = args.positionals[1],
.output = output,
};
const options_manager = globals.options_manager orelse return error.RiverOptionsManagerNotAdvertised;
const handle = try options_manager.getOptionHandle(ctx.key, if (ctx.output) |o| o.wl_output else null);
handle.setListener(*const Context, modOptionListener, &ctx) catch unreachable;
// We always exit when our listener is called
while (true) _ = try display.dispatch();
}
fn parseOutputName(display: *wl.Display, globals: *Globals, output_name: [*:0]const u8) !*Output {
const output_manager = globals.output_manager orelse return error.XdgOutputNotAdvertised;
for (globals.outputs.items) |*output| {
const xdg_output = try output_manager.getXdgOutput(output.wl_output);
xdg_output.setListener(*Output, xdgOutputListener, output) catch unreachable;
}
_ = try display.roundtrip();
for (globals.outputs.items) |*output| {
if (mem.eql(u8, output.name, mem.span(output_name))) return output;
}
root.printErrorExit("unknown output '{}'", .{output_name});
}
fn xdgOutputListener(xdg_output: *zxdg.OutputV1, event: zxdg.OutputV1.Event, output: *Output) void {
switch (event) {
.name => |ev| output.name = std.heap.c_allocator.dupe(u8, mem.span(ev.name)) catch @panic("out of memory"),
else => {},
}
}
fn getFocusedOutput(display: *wl.Display, globals: *Globals) !*Output {
const status_manager = globals.status_manager orelse return error.RiverStatusManagerNotAdvertised;
const seat = globals.seat orelse return error.SeatNotAdverstised;
const seat_status = try status_manager.getRiverSeatStatus(seat);
var result: ?*wl.Output = null;
seat_status.setListener(*?*wl.Output, seatStatusListener, &result) catch unreachable;
_ = try display.roundtrip();
const wl_output = if (result) |output| output else return error.NoOutputFocused;
for (globals.outputs.items) |*output| {
if (output.wl_output == wl_output) return output;
} else unreachable;
}
fn seatStatusListener(seat_status: *zriver.SeatStatusV1, event: zriver.SeatStatusV1.Event, result: *?*wl.Output) void {
switch (event) {
.focused_output => |ev| result.* = ev.output,
.unfocused_output, .focused_view => {},
}
}
fn getOptionListener(
handle: *river.OptionHandleV2,
event: river.OptionHandleV2.Event,
ctx: *const Context,
) void {
switch (event) {
.undeclared => root.printErrorExit("option '{}' has not been declared", .{ctx.key}),
.int_value => |ev| printOutputExit("{}", .{ev.value}),
.uint_value => |ev| printOutputExit("{}", .{ev.value}),
.fixed_value => |ev| printOutputExit("{d}", .{ev.value.toDouble()}),
.string_value => |ev| if (ev.value) |s| printOutputExit("{}", .{s}) else os.exit(0),
}
}
fn printOutputExit(comptime format: []const u8, args: anytype) noreturn {
const stdout = std.io.getStdOut().writer();
stdout.print(format ++ "\n", args) catch os.exit(1);
os.exit(0);
}
fn setOptionListener(
handle: *river.OptionHandleV2,
event: river.OptionHandleV2.Event,
ctx: *const Context,
) void {
switch (event) {
.undeclared => root.printErrorExit("option '{}' has not been declared", .{ctx.key}),
.int_value => |ev| handle.setIntValue(parseInt(ctx.raw_value)),
.uint_value => |ev| handle.setUintValue(parseUint(ctx.raw_value)),
.fixed_value => |ev| handle.setFixedValue(parseFixed(ctx.raw_value)),
.string_value => |ev| handle.setStringValue(if (ctx.raw_value[0] == 0) null else ctx.raw_value),
}
_ = ctx.display.flush() catch os.exit(1);
os.exit(0);
}
fn modOptionListener(
handle: *river.OptionHandleV2,
event: river.OptionHandleV2.Event,
ctx: *const Context,
) void {
switch (event) {
.undeclared => root.printErrorExit("option '{}' has not been declared", .{ctx.key}),
.int_value => |ev| modIntValueRaw(handle, ev.value, ctx.raw_value),
.uint_value => |ev| modUintValueRaw(handle, ev.value, ctx.raw_value),
.fixed_value => |ev| modFixedValueRaw(handle, ev.value, ctx.raw_value),
.string_value => root.printErrorExit("can not modify string options, use set-option to overwrite them", .{}),
}
_ = ctx.display.flush() catch os.exit(1);
os.exit(0);
}
fn modIntValueRaw(handle: *river.OptionHandleV2, current: i32, raw_value: [*:0]const u8) void {
const mod = fmt.parseInt(i32, mem.span(raw_value), 10) catch
root.printErrorExit("{} is not a valid int modifier", .{raw_value});
const new_value = math.add(i32, current, mod) catch
root.printErrorExit("provided value of {d} would overflow option if added", .{mod});
handle.setIntValue(new_value);
}
fn modUintValueRaw(handle: *river.OptionHandleV2, current: u32, raw_value: [*:0]const u8) void {
// We need to allow negative mod values, but the value of the option may
// never be below zero.
const mod = fmt.parseInt(i32, mem.span(raw_value), 10) catch
root.printErrorExit("{} is not a valid uint modifier", .{raw_value});
const new = @intCast(i32, current) + mod;
handle.setUintValue(if (new < 0) 0 else @intCast(u32, new));
}
fn modFixedValueRaw(handle: *river.OptionHandleV2, current: wl.Fixed, raw_value: [*:0]const u8) void {
const mod = fmt.parseFloat(f64, mem.span(raw_value)) catch
root.printErrorExit("{} is not a valid fixed modifier", .{raw_value});
handle.setFixedValue(wl.Fixed.fromDouble(current.toDouble() + mod));
}