river: make server a global variable

This commit is contained in:
Isaac Freund 2021-05-13 14:25:34 +02:00
parent 7ffa2f4b9e
commit 271b1563a8
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11

View File

@ -26,7 +26,7 @@ const util = @import("util.zig");
const Server = @import("Server.zig"); const Server = @import("Server.zig");
const log_server = std.log.scoped(.server); pub var server: Server = undefined;
pub var level: std.log.Level = switch (std.builtin.mode) { pub var level: std.log.Level = switch (std.builtin.mode) {
.Debug => .debug, .Debug => .debug,
@ -44,47 +44,6 @@ const usage: []const u8 =
\\ \\
; ;
fn testConfigPath(comptime fmt: []const u8, args: anytype) std.fmt.AllocPrintError!?[:0]const u8 {
const path = try std.fmt.allocPrintZ(util.gpa, fmt, args);
os.access(path, os.X_OK) catch {
util.gpa.free(path);
return null;
};
return path;
}
fn getStartupCommand() std.fmt.AllocPrintError!?[:0]const u8 {
if (os.getenv("XDG_CONFIG_HOME")) |xdg_config_home| {
if (try testConfigPath("{}/river/init", .{xdg_config_home})) |path| {
return path;
}
} else if (os.getenv("HOME")) |home| {
if (try testConfigPath("{}/.config/river/init", .{home})) |path| {
return path;
}
}
if (try testConfigPath(build_options.default_config_path, .{})) |path| {
return path;
}
return null;
}
pub fn log(
comptime message_level: std.log.Level,
comptime scope: @TypeOf(.foobar),
comptime format: []const u8,
args: anytype,
) void {
if (@enumToInt(message_level) <= @enumToInt(level)) {
// Don't store/log messages in release small mode to save space
if (std.builtin.mode != .ReleaseSmall) {
const stderr = std.io.getStdErr().writer();
stderr.print(@tagName(message_level) ++ ": (" ++ @tagName(scope) ++ ") " ++
format ++ "\n", args) catch return;
}
}
}
pub fn main() anyerror!void { pub fn main() anyerror!void {
var startup_command: ?[:0]const u8 = null; var startup_command: ?[:0]const u8 = null;
{ {
@ -131,8 +90,7 @@ pub fn main() anyerror!void {
if (try getStartupCommand()) |path| startup_command = path; if (try getStartupCommand()) |path| startup_command = path;
} }
log_server.info("initializing server", .{}); std.log.info("initializing server", .{});
var server: Server = undefined;
try server.init(); try server.init();
defer server.deinit(); defer server.deinit();
@ -141,7 +99,7 @@ pub fn main() anyerror!void {
// Run the child in a new process group so that we can send SIGTERM to all // Run the child in a new process group so that we can send SIGTERM to all
// descendants on exit. // descendants on exit.
const child_pgid = if (startup_command) |cmd| blk: { const child_pgid = if (startup_command) |cmd| blk: {
log_server.info("running startup command '{}'", .{cmd}); std.log.info("running startup command '{}'", .{cmd});
const child_args = [_:null]?[*:0]const u8{ "/bin/sh", "-c", cmd, null }; const child_args = [_:null]?[*:0]const u8{ "/bin/sh", "-c", cmd, null };
const pid = try os.fork(); const pid = try os.fork();
if (pid == 0) { if (pid == 0) {
@ -154,13 +112,13 @@ pub fn main() anyerror!void {
break :blk pid; break :blk pid;
} else null; } else null;
defer if (child_pgid) |pgid| defer if (child_pgid) |pgid|
os.kill(-pgid, os.SIGTERM) catch |e| log_server.err("failed to kill startup process: {}", .{e}); os.kill(-pgid, os.SIGTERM) catch |e| std.log.err("failed to kill startup process: {}", .{e});
log_server.info("running server", .{}); std.log.info("running server", .{});
server.wl_server.run(); server.wl_server.run();
log_server.info("shutting down", .{}); std.log.info("shutting down", .{});
} }
fn printErrorExit(comptime format: []const u8, args: anytype) noreturn { fn printErrorExit(comptime format: []const u8, args: anytype) noreturn {
@ -168,3 +126,44 @@ fn printErrorExit(comptime format: []const u8, args: anytype) noreturn {
stderr.print(format ++ "\n", args) catch os.exit(1); stderr.print(format ++ "\n", args) catch os.exit(1);
os.exit(1); os.exit(1);
} }
fn testConfigPath(comptime fmt: []const u8, args: anytype) std.fmt.AllocPrintError!?[:0]const u8 {
const path = try std.fmt.allocPrintZ(util.gpa, fmt, args);
os.access(path, os.X_OK) catch {
util.gpa.free(path);
return null;
};
return path;
}
fn getStartupCommand() std.fmt.AllocPrintError!?[:0]const u8 {
if (os.getenv("XDG_CONFIG_HOME")) |xdg_config_home| {
if (try testConfigPath("{}/river/init", .{xdg_config_home})) |path| {
return path;
}
} else if (os.getenv("HOME")) |home| {
if (try testConfigPath("{}/.config/river/init", .{home})) |path| {
return path;
}
}
if (try testConfigPath(build_options.default_config_path, .{})) |path| {
return path;
}
return null;
}
pub fn log(
comptime message_level: std.log.Level,
comptime scope: @TypeOf(.foobar),
comptime format: []const u8,
args: anytype,
) void {
if (@enumToInt(message_level) <= @enumToInt(level)) {
// Don't store/log messages in release small mode to save space
if (std.builtin.mode != .ReleaseSmall) {
const stderr = std.io.getStdErr().writer();
stderr.print(@tagName(message_level) ++ ": (" ++ @tagName(scope) ++ ") " ++
format ++ "\n", args) catch return;
}
}
}