aboutsummaryrefslogtreecommitdiff
path: root/riverctl/main.zig
blob: e3453eb2d39889b9562aa6dfc00ecfbca8a87453 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// This file is part of river, a dynamic tiling wayland compositor.
//
// Copyright 2020 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 wayland = @import("wayland");
const wl = wayland.client.wl;
const river = wayland.client.river;

const SetupContext = struct {
    river_control: ?*river.ControlV1 = null,
    seat: ?*wl.Seat = null,
};

pub fn main() !void {
    const display = try wl.Display.connect(null);
    const registry = try display.getRegistry();

    var context = SetupContext{};

    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;

    // 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;
    for (args[1..]) |arg| river_control.addArgument(arg);

    const callback = try river_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, context: *SetupContext) void {
    switch (event) {
        .global => |global| {
            if (context.seat == null and std.cstr.cmp(global.interface, wl.Seat.interface().name) == 0) {
                context.seat = registry.bind(global.name, wl.Seat, 1) catch return;
            } else if (std.cstr.cmp(global.interface, river.ControlV1.interface().name) == 0) {
                context.river_control = registry.bind(global.name, river.ControlV1, 1) catch return;
            }
        },
        .global_remove => {},
    }
}

fn callbackListener(callback: *river.CommandCallbackV1, event: river.CommandCallbackV1.Event, _: ?*c_void) void {
    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);
        },
    }
}