main.zig (2078B)
1 const std = @import("std"); 2 const heap = std.heap; 3 const io = std.io; 4 const log = std.log; 5 const mem = std.mem; 6 const os = std.os; 7 const process = std.process; 8 9 const fcft = @import("fcft"); 10 11 const Config = @import("Config.zig"); 12 const Loop = @import("Loop.zig"); 13 const Modules = @import("Modules.zig"); 14 const Wayland = @import("Wayland.zig"); 15 16 pub const State = struct { 17 gpa: mem.Allocator, 18 config: Config, 19 wayland: Wayland, 20 modules: Modules, 21 loop: Loop, 22 }; 23 24 pub var state: State = undefined; 25 26 pub fn main() anyerror!void { 27 var gpa: heap.GeneralPurposeAllocator(.{}) = .{}; 28 defer _ = gpa.deinit(); 29 30 _ = fcft.init(.auto, false, .warning); 31 32 // initialization 33 state.gpa = gpa.allocator(); 34 state.config = try Config.init(); 35 state.wayland = try Wayland.init(); 36 state.modules = Modules.init(); 37 state.loop = try Loop.init(); 38 39 defer { 40 state.wayland.deinit(); 41 state.modules.deinit(); 42 } 43 44 // modules 45 var args = process.args(); 46 const program_name = args.next() orelse unreachable; 47 48 while (args.next()) |arg| { 49 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) { 50 help(program_name); 51 return; 52 } 53 state.modules.register(arg) catch |err| { 54 switch (err) { 55 error.UnknownModule => { 56 log.err("unknown module: {s}", .{arg}); 57 }, 58 else => { 59 log.err( 60 "initialization error for module {s}: {s}", 61 .{ arg, @errorName(err) }, 62 ); 63 }, 64 } 65 return; 66 }; 67 } 68 69 // event loop 70 try state.wayland.registerGlobals(); 71 try state.loop.run(); 72 } 73 74 fn help(program_name: []const u8) void { 75 const text = 76 \\Usage: {s} [module]... 77 \\ 78 \\Available modules: 79 \\ pulse speaker volume with pulseaudio 80 \\ 81 ; 82 const w = io.getStdErr().writer(); 83 w.print(text, .{program_name}) catch unreachable; 84 }