aboutsummaryrefslogtreecommitdiff
path: root/riverctl/options.zig
diff options
context:
space:
mode:
authorLeon Henrik Plickat <leonhenrik.plickat@stud.uni-goettingen.de>2021-02-09 22:53:17 +0100
committerGitHub <noreply@github.com>2021-02-09 22:53:17 +0100
commitaeeae92611683f1330d56dc8cbbda93a6868d3c6 (patch)
tree93ccf4155c3fa139157b873db392dea49d6f4292 /riverctl/options.zig
parent98d51f6d24ef8cc9166833e89c26e7663dbb5831 (diff)
downloadriver-aeeae92611683f1330d56dc8cbbda93a6868d3c6.tar.gz
river-aeeae92611683f1330d56dc8cbbda93a6868d3c6.tar.xz
riverctl: add mod-option command
Diffstat (limited to 'riverctl/options.zig')
-rw-r--r--riverctl/options.zig71
1 files changed, 71 insertions, 0 deletions
diff --git a/riverctl/options.zig b/riverctl/options.zig
index 18df45b..aab9fa0 100644
--- a/riverctl/options.zig
+++ b/riverctl/options.zig
@@ -93,6 +93,27 @@ fn setFixedValueRaw(handle: *zriver.OptionHandleV1, raw_value: [*:0]const u8) vo
root.printErrorExit("{} is not a valid fixed", .{raw_value})));
}
+fn modIntValueRaw(handle: *zriver.OptionHandleV1, 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 modificator", .{raw_value});
+ handle.setIntValue(current + mod);
+}
+
+fn modUintValueRaw(handle: *zriver.OptionHandleV1, 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 modificator", .{raw_value});
+ const new = @intCast(i32, current) + mod;
+ handle.setUintValue(if (new < 0) 0 else @intCast(u32, new));
+}
+
+fn modFixedValueRaw(handle: *zriver.OptionHandleV1, 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 modificator", .{raw_value});
+ handle.setFixedValue(wl.Fixed.fromDouble(current.toDouble() + mod));
+}
+
pub fn getOption(display: *wl.Display, globals: *Globals) !void {
// https://github.com/ziglang/zig/issues/7807
const argv: [][*:0]const u8 = os.argv;
@@ -153,6 +174,36 @@ pub fn setOption(display: *wl.Display, globals: *Globals) !void {
while (true) _ = try display.dispatch();
}
+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| {
@@ -237,3 +288,23 @@ fn setOptionListener(
_ = ctx.display.flush() catch os.exit(1);
os.exit(0);
}
+
+fn modOptionListener(
+ handle: *zriver.OptionHandleV1,
+ event: zriver.OptionHandleV1.Event,
+ ctx: *const Context,
+) void {
+ switch (event) {
+ .unset => if (ctx.output) |output| {
+ root.printErrorExit("option '{}' has not been declared on output '{}'", .{ ctx.key, output.name });
+ } else {
+ root.printErrorExit("option '{}' has not been declared globally", .{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);
+}