2020-05-02 10:21:10 -07:00
|
|
|
// This file is part of river, a dynamic tiling wayland compositor.
|
|
|
|
//
|
2021-01-17 07:30:47 -08:00
|
|
|
// Copyright 2020-2021 The River Developers
|
2020-05-02 10:21:10 -07:00
|
|
|
//
|
|
|
|
// 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
|
2022-01-31 10:33:22 -08:00
|
|
|
// the Free Software Foundation, version 3.
|
2020-05-02 10:21:10 -07:00
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
2020-03-19 13:30:38 -07:00
|
|
|
const std = @import("std");
|
2021-01-17 07:30:47 -08:00
|
|
|
const mem = std.mem;
|
2021-07-24 10:31:04 -07:00
|
|
|
const io = std.io;
|
2021-01-17 07:30:47 -08:00
|
|
|
const os = std.os;
|
2021-02-07 10:11:54 -08:00
|
|
|
const assert = std.debug.assert;
|
2021-10-11 03:44:46 -07:00
|
|
|
const builtin = @import("builtin");
|
2020-05-02 10:21:10 -07:00
|
|
|
|
2020-11-02 04:59:59 -08:00
|
|
|
const wayland = @import("wayland");
|
|
|
|
const wl = wayland.client.wl;
|
2020-11-03 15:23:21 -08:00
|
|
|
const zriver = wayland.client.zriver;
|
2020-03-19 08:29:22 -07:00
|
|
|
|
2021-07-24 10:31:04 -07:00
|
|
|
const flags = @import("flags");
|
|
|
|
|
|
|
|
const usage =
|
|
|
|
\\usage: riverctl [options] <command>
|
|
|
|
\\
|
2021-10-31 16:34:15 -07:00
|
|
|
\\ -h Print this help message and exit.
|
2021-07-24 10:31:04 -07:00
|
|
|
\\ -version Print the version number and exit.
|
|
|
|
\\
|
|
|
|
\\Complete documentation of the recognized commands may be found in
|
|
|
|
\\the riverctl(1) man page.
|
|
|
|
\\
|
|
|
|
;
|
|
|
|
|
2021-01-17 07:30:47 -08:00
|
|
|
const gpa = std.heap.c_allocator;
|
|
|
|
|
|
|
|
pub const Globals = struct {
|
|
|
|
control: ?*zriver.ControlV1 = null,
|
2020-11-02 04:59:59 -08:00
|
|
|
seat: ?*wl.Seat = null,
|
2020-05-19 09:22:22 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
pub fn main() !void {
|
2021-03-27 03:36:47 -07:00
|
|
|
_main() catch |err| {
|
|
|
|
switch (err) {
|
2021-07-24 10:31:04 -07:00
|
|
|
error.RiverControlNotAdvertised => fatal(
|
2021-03-27 03:36:47 -07:00
|
|
|
\\The Wayland server does not support river-control-unstable-v1.
|
|
|
|
\\Do your versions of river and riverctl match?
|
|
|
|
, .{}),
|
2021-07-24 10:31:04 -07:00
|
|
|
error.SeatNotAdverstised => fatal(
|
2021-03-27 03:36:47 -07:00
|
|
|
\\The Wayland server did not advertise any seat.
|
|
|
|
, .{}),
|
2023-06-03 21:45:01 -07:00
|
|
|
error.ConnectFailed => {
|
|
|
|
std.log.err("Unable to connect to the Wayland server.", .{});
|
|
|
|
if (os.getenvZ("WAYLAND_DISPLAY") == null) {
|
|
|
|
fatal("WAYLAND_DISPLAY is not set.", .{});
|
|
|
|
} else {
|
|
|
|
fatal("Does WAYLAND_DISPLAY contain the socket name of a running server?", .{});
|
|
|
|
}
|
|
|
|
},
|
2021-03-27 03:36:47 -07:00
|
|
|
else => return err,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _main() !void {
|
2022-12-28 10:47:09 -08:00
|
|
|
const result = flags.parser([*:0]const u8, &.{
|
2022-12-28 13:11:14 -08:00
|
|
|
.{ .name = "h", .kind = .boolean },
|
|
|
|
.{ .name = "version", .kind = .boolean },
|
2023-01-08 08:01:14 -08:00
|
|
|
}).parse(os.argv[1..]) catch {
|
2021-07-24 10:31:04 -07:00
|
|
|
try io.getStdErr().writeAll(usage);
|
|
|
|
os.exit(1);
|
|
|
|
};
|
2022-12-28 13:11:14 -08:00
|
|
|
if (result.flags.h) {
|
2021-07-24 10:31:04 -07:00
|
|
|
try io.getStdOut().writeAll(usage);
|
|
|
|
os.exit(0);
|
|
|
|
}
|
2022-12-28 13:11:14 -08:00
|
|
|
if (result.flags.version) {
|
2021-11-02 15:54:44 -07:00
|
|
|
try io.getStdOut().writeAll(@import("build_options").version ++ "\n");
|
2021-07-24 10:31:04 -07:00
|
|
|
os.exit(0);
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:59:59 -08:00
|
|
|
const display = try wl.Display.connect(null);
|
|
|
|
const registry = try display.getRegistry();
|
2020-05-19 09:22:22 -07:00
|
|
|
|
2021-01-17 07:30:47 -08:00
|
|
|
var globals = Globals{};
|
2020-05-19 09:22:22 -07:00
|
|
|
|
2021-04-27 03:24:30 -07:00
|
|
|
registry.setListener(*Globals, registryListener, &globals);
|
2022-05-09 12:56:28 -07:00
|
|
|
if (display.roundtrip() != .SUCCESS) fatal("initial roundtrip failed", .{});
|
2020-11-02 04:59:59 -08:00
|
|
|
|
2021-04-26 12:03:04 -07:00
|
|
|
const control = globals.control orelse return error.RiverControlNotAdvertised;
|
|
|
|
const seat = globals.seat orelse return error.SeatNotAdverstised;
|
|
|
|
|
2021-07-24 10:31:04 -07:00
|
|
|
for (result.args) |arg| control.addArgument(arg);
|
2021-04-26 12:03:04 -07:00
|
|
|
|
|
|
|
const callback = try control.runCommand(seat);
|
2021-10-11 03:44:46 -07:00
|
|
|
callback.setListener(?*anyopaque, callbackListener, null);
|
2021-04-26 12:03:04 -07:00
|
|
|
|
|
|
|
// Loop until our callback is called and we exit.
|
2022-05-09 12:56:28 -07:00
|
|
|
while (true) {
|
|
|
|
if (display.dispatch() != .SUCCESS) fatal("failed to dispatch wayland events", .{});
|
|
|
|
}
|
2020-03-19 08:29:22 -07:00
|
|
|
}
|
2020-05-19 09:22:22 -07:00
|
|
|
|
2021-01-17 07:30:47 -08:00
|
|
|
fn registryListener(registry: *wl.Registry, event: wl.Registry.Event, globals: *Globals) void {
|
2020-11-02 04:59:59 -08:00
|
|
|
switch (event) {
|
|
|
|
.global => |global| {
|
2023-10-16 07:18:36 -07:00
|
|
|
if (mem.orderZ(u8, global.interface, wl.Seat.getInterface().name) == .eq) {
|
2021-02-07 10:11:54 -08:00
|
|
|
assert(globals.seat == null); // TODO: support multiple seats
|
2021-01-17 07:30:47 -08:00
|
|
|
globals.seat = registry.bind(global.name, wl.Seat, 1) catch @panic("out of memory");
|
2023-10-16 07:18:36 -07:00
|
|
|
} else if (mem.orderZ(u8, global.interface, zriver.ControlV1.getInterface().name) == .eq) {
|
2021-01-17 07:30:47 -08:00
|
|
|
globals.control = registry.bind(global.name, zriver.ControlV1, 1) catch @panic("out of memory");
|
2020-11-02 04:59:59 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
.global_remove => {},
|
2020-06-15 13:24:54 -07:00
|
|
|
}
|
2020-05-24 06:18:57 -07:00
|
|
|
}
|
|
|
|
|
2021-10-11 03:44:46 -07:00
|
|
|
fn callbackListener(_: *zriver.CommandCallbackV1, event: zriver.CommandCallbackV1.Event, _: ?*anyopaque) void {
|
2020-11-02 04:59:59 -08:00
|
|
|
switch (event) {
|
|
|
|
.success => |success| {
|
2021-01-17 07:30:47 -08:00
|
|
|
if (mem.len(success.output) > 0) {
|
2022-02-07 13:07:27 -08:00
|
|
|
const stdout = io.getStdOut().writer();
|
2021-05-23 04:35:37 -07:00
|
|
|
stdout.print("{s}\n", .{success.output}) catch @panic("failed to write to stdout");
|
2020-11-02 04:59:59 -08:00
|
|
|
}
|
2021-01-17 07:30:47 -08:00
|
|
|
os.exit(0);
|
2020-11-02 04:59:59 -08:00
|
|
|
},
|
2021-07-24 10:31:04 -07:00
|
|
|
.failure => |failure| {
|
|
|
|
// A small hack to provide usage text when river reports an unknown command.
|
2023-10-16 07:18:36 -07:00
|
|
|
if (mem.orderZ(u8, failure.failure_message, "unknown command") == .eq) {
|
2021-07-24 10:31:04 -07:00
|
|
|
std.log.err("unknown command", .{});
|
|
|
|
io.getStdErr().writeAll(usage) catch {};
|
|
|
|
os.exit(1);
|
|
|
|
}
|
|
|
|
fatal("{s}", .{failure.failure_message});
|
|
|
|
},
|
2020-05-24 06:18:57 -07:00
|
|
|
}
|
|
|
|
}
|
2021-01-17 07:30:47 -08:00
|
|
|
|
2021-07-24 10:31:04 -07:00
|
|
|
fn fatal(comptime format: []const u8, args: anytype) noreturn {
|
|
|
|
std.log.err(format, args);
|
2022-02-07 13:07:27 -08:00
|
|
|
os.exit(1);
|
2021-01-17 07:30:47 -08:00
|
|
|
}
|