2020-05-02 10:21:10 -07:00
|
|
|
// This file is part of river, a dynamic tiling wayland compositor.
|
|
|
|
//
|
2020-11-11 11:30:21 -08:00
|
|
|
// Copyright 2020 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
|
|
|
|
// 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/>.
|
|
|
|
|
2020-03-19 13:30:38 -07:00
|
|
|
const std = @import("std");
|
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
|
|
|
|
2020-11-02 04:59:59 -08:00
|
|
|
const SetupContext = struct {
|
2020-11-03 15:23:21 -08:00
|
|
|
river_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 {
|
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
|
|
|
|
2020-11-02 04:59:59 -08:00
|
|
|
var context = SetupContext{};
|
2020-05-19 09:22:22 -07:00
|
|
|
|
2020-11-02 04:59:59 -08:00
|
|
|
registry.setListener(*SetupContext, registryListener, &context) catch unreachable;
|
|
|
|
_ = try display.roundtrip();
|
|
|
|
|
|
|
|
const river_control = context.river_control orelse return error.RiverControlNotAdvertised;
|
|
|
|
const seat = context.seat orelse return error.SeatNotAdverstised;
|
2020-05-19 09:22:22 -07:00
|
|
|
|
2020-06-15 13:24:54 -07:00
|
|
|
// Skip our name, send all other args
|
|
|
|
// This next line is needed cause of https://github.com/ziglang/zig/issues/2622
|
|
|
|
const args = std.os.argv;
|
2020-11-02 04:59:59 -08:00
|
|
|
for (args[1..]) |arg| river_control.addArgument(arg);
|
2020-05-19 09:22:22 -07:00
|
|
|
|
2020-11-02 04:59:59 -08:00
|
|
|
const callback = try river_control.runCommand(seat);
|
2020-05-24 06:18:57 -07:00
|
|
|
|
2020-11-02 04:59:59 -08:00
|
|
|
callback.setListener(?*c_void, callbackListener, null) catch unreachable;
|
2020-05-19 09:22:22 -07:00
|
|
|
|
2020-11-02 04:59:59 -08:00
|
|
|
// Loop until our callback is called and we exit.
|
|
|
|
while (true) _ = try display.dispatch();
|
2020-03-19 08:29:22 -07:00
|
|
|
}
|
2020-05-19 09:22:22 -07:00
|
|
|
|
2020-11-02 04:59:59 -08:00
|
|
|
fn registryListener(registry: *wl.Registry, event: wl.Registry.Event, context: *SetupContext) void {
|
|
|
|
switch (event) {
|
|
|
|
.global => |global| {
|
2020-11-03 15:23:21 -08:00
|
|
|
if (context.seat == null and std.cstr.cmp(global.interface, wl.Seat.getInterface().name) == 0) {
|
2020-11-02 04:59:59 -08:00
|
|
|
context.seat = registry.bind(global.name, wl.Seat, 1) catch return;
|
2020-11-03 15:23:21 -08:00
|
|
|
} else if (std.cstr.cmp(global.interface, zriver.ControlV1.getInterface().name) == 0) {
|
|
|
|
context.river_control = registry.bind(global.name, zriver.ControlV1, 1) catch return;
|
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
|
|
|
}
|
|
|
|
|
2020-11-03 15:23:21 -08:00
|
|
|
fn callbackListener(callback: *zriver.CommandCallbackV1, event: zriver.CommandCallbackV1.Event, _: ?*c_void) void {
|
2020-11-02 04:59:59 -08:00
|
|
|
switch (event) {
|
|
|
|
.success => |success| {
|
|
|
|
if (std.mem.len(success.output) > 0) {
|
|
|
|
const stdout = std.io.getStdOut().outStream();
|
|
|
|
stdout.print("{}\n", .{success.output}) catch @panic("failed to write to stdout");
|
|
|
|
}
|
|
|
|
std.os.exit(0);
|
|
|
|
},
|
|
|
|
.failure => |failure| {
|
|
|
|
std.debug.print("Error: {}\n", .{failure.failure_message});
|
|
|
|
std.os.exit(1);
|
|
|
|
},
|
2020-05-24 06:18:57 -07:00
|
|
|
}
|
|
|
|
}
|