2020-05-02 17:21:10 +00:00
|
|
|
// This file is part of river, a dynamic tiling wayland compositor.
|
|
|
|
//
|
2021-01-17 15:30:47 +00:00
|
|
|
// Copyright 2020-2021 The River Developers
|
2020-05-02 17:21:10 +00: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 20:30:38 +00:00
|
|
|
const std = @import("std");
|
2021-01-17 15:30:47 +00:00
|
|
|
const mem = std.mem;
|
|
|
|
const os = std.os;
|
2021-02-07 18:11:54 +00:00
|
|
|
const assert = std.debug.assert;
|
2020-05-02 17:21:10 +00:00
|
|
|
|
2020-11-02 12:59:59 +00:00
|
|
|
const wayland = @import("wayland");
|
|
|
|
const wl = wayland.client.wl;
|
2020-11-03 23:23:21 +00:00
|
|
|
const zriver = wayland.client.zriver;
|
2021-01-17 15:30:47 +00:00
|
|
|
const zxdg = wayland.client.zxdg;
|
2020-03-19 15:29:22 +00:00
|
|
|
|
2021-01-17 15:30:47 +00:00
|
|
|
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: ?*zriver.OptionsManagerV1 = null,
|
2021-02-07 18:11:54 +00:00
|
|
|
status_manager: ?*zriver.StatusManagerV1 = null,
|
2020-11-02 12:59:59 +00:00
|
|
|
seat: ?*wl.Seat = null,
|
2021-01-17 15:30:47 +00:00
|
|
|
output_manager: ?*zxdg.OutputManagerV1 = null,
|
|
|
|
outputs: std.ArrayList(Output) = std.ArrayList(Output).init(gpa),
|
2020-05-19 16:22:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub fn main() !void {
|
2020-11-02 12:59:59 +00:00
|
|
|
const display = try wl.Display.connect(null);
|
|
|
|
const registry = try display.getRegistry();
|
2020-05-19 16:22:22 +00:00
|
|
|
|
2021-01-17 15:30:47 +00:00
|
|
|
var globals = Globals{};
|
2020-05-19 16:22:22 +00:00
|
|
|
|
2021-01-17 15:30:47 +00:00
|
|
|
registry.setListener(*Globals, registryListener, &globals) catch unreachable;
|
2020-11-02 12:59:59 +00:00
|
|
|
_ = try display.roundtrip();
|
|
|
|
|
2021-01-17 15:30:47 +00:00
|
|
|
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);
|
2021-02-09 21:53:17 +00:00
|
|
|
} else if (os.argv.len > 2 and mem.eql(u8, "mod-option", mem.span(os.argv[1]))) {
|
|
|
|
try options.modOption(display, &globals);
|
2021-01-17 15:30:47 +00:00
|
|
|
} else {
|
|
|
|
const control = globals.control orelse return error.RiverControlNotAdvertised;
|
|
|
|
const seat = globals.seat orelse return error.SeatNotAdverstised;
|
2020-05-19 16:22:22 +00:00
|
|
|
|
2021-01-17 15:30:47 +00:00
|
|
|
// 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);
|
2020-05-19 16:22:22 +00:00
|
|
|
|
2021-01-17 15:30:47 +00:00
|
|
|
const callback = try control.runCommand(seat);
|
2020-05-24 13:18:57 +00:00
|
|
|
|
2021-01-17 15:30:47 +00:00
|
|
|
callback.setListener(?*c_void, callbackListener, null) catch unreachable;
|
2020-05-19 16:22:22 +00:00
|
|
|
|
2021-01-17 15:30:47 +00:00
|
|
|
// Loop until our callback is called and we exit.
|
|
|
|
while (true) _ = try display.dispatch();
|
|
|
|
}
|
2020-03-19 15:29:22 +00:00
|
|
|
}
|
2020-05-19 16:22:22 +00:00
|
|
|
|
2021-01-17 15:30:47 +00:00
|
|
|
fn registryListener(registry: *wl.Registry, event: wl.Registry.Event, globals: *Globals) void {
|
2020-11-02 12:59:59 +00:00
|
|
|
switch (event) {
|
|
|
|
.global => |global| {
|
2021-02-07 18:11:54 +00:00
|
|
|
if (std.cstr.cmp(global.interface, wl.Seat.getInterface().name) == 0) {
|
|
|
|
assert(globals.seat == null); // TODO: support multiple seats
|
2021-01-17 15:30:47 +00:00
|
|
|
globals.seat = registry.bind(global.name, wl.Seat, 1) catch @panic("out of memory");
|
2020-11-03 23:23:21 +00:00
|
|
|
} else if (std.cstr.cmp(global.interface, zriver.ControlV1.getInterface().name) == 0) {
|
2021-01-17 15:30:47 +00:00
|
|
|
globals.control = registry.bind(global.name, zriver.ControlV1, 1) catch @panic("out of memory");
|
|
|
|
} else if (std.cstr.cmp(global.interface, zriver.OptionsManagerV1.getInterface().name) == 0) {
|
|
|
|
globals.options_manager = registry.bind(global.name, zriver.OptionsManagerV1, 1) catch @panic("out of memory");
|
2021-02-07 18:11:54 +00:00
|
|
|
} 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");
|
2021-01-17 15:30:47 +00:00
|
|
|
} 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");
|
2020-11-02 12:59:59 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
.global_remove => {},
|
2020-06-15 20:24:54 +00:00
|
|
|
}
|
2020-05-24 13:18:57 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 23:23:21 +00:00
|
|
|
fn callbackListener(callback: *zriver.CommandCallbackV1, event: zriver.CommandCallbackV1.Event, _: ?*c_void) void {
|
2020-11-02 12:59:59 +00:00
|
|
|
switch (event) {
|
|
|
|
.success => |success| {
|
2021-01-17 15:30:47 +00:00
|
|
|
if (mem.len(success.output) > 0) {
|
2020-11-02 12:59:59 +00:00
|
|
|
const stdout = std.io.getStdOut().outStream();
|
|
|
|
stdout.print("{}\n", .{success.output}) catch @panic("failed to write to stdout");
|
|
|
|
}
|
2021-01-17 15:30:47 +00:00
|
|
|
os.exit(0);
|
2020-11-02 12:59:59 +00:00
|
|
|
},
|
|
|
|
.failure => |failure| {
|
|
|
|
std.debug.print("Error: {}\n", .{failure.failure_message});
|
2021-01-17 15:30:47 +00:00
|
|
|
os.exit(1);
|
2020-11-02 12:59:59 +00:00
|
|
|
},
|
2020-05-24 13:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-17 15:30:47 +00:00
|
|
|
|
|
|
|
pub fn printErrorExit(comptime format: []const u8, args: anytype) noreturn {
|
|
|
|
std.debug.print("err: " ++ format ++ "\n", args);
|
|
|
|
os.exit(1);
|
|
|
|
}
|