reorder main
Browse files
+90 −92 src/main.zig
| @@ -9,6 +9,84 @@ const lua = @import("config/lua.zig"); | |||
| 9 | 9 | ||
| 10 | const WindowManager = window_manager.WindowManager; | 10 | const WindowManager = window_manager.WindowManager; |
| 11 | 11 | ||
| 12 | +pub fn main() !void { | ||
| 13 | + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
| 14 | + const allocator = gpa.allocator(); | ||
| 15 | + defer _ = gpa.deinit(); | ||
| 16 | + | ||
| 17 | + const default_config_path = try getConfigPath(allocator); | ||
| 18 | + defer allocator.free(default_config_path); | ||
| 19 | + | ||
| 20 | + var config_path: []const u8 = default_config_path; | ||
| 21 | + var validate_mode: bool = false; | ||
| 22 | + var args = std.process.args(); | ||
| 23 | + _ = args.skip(); | ||
| 24 | + while (args.next()) |arg| { | ||
| 25 | + if (std.mem.eql(u8, arg, "-c") or std.mem.eql(u8, arg, "--config")) { | ||
| 26 | + if (args.next()) |path| config_path = path; | ||
| 27 | + } else if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) { | ||
| 28 | + printHelp(); | ||
| 29 | + return; | ||
| 30 | + } else if (std.mem.eql(u8, arg, "-v") or std.mem.eql(u8, arg, "--version")) { | ||
| 31 | + std.debug.print("{s}\n", .{build_options.version}); | ||
| 32 | + return; | ||
| 33 | + } else if (std.mem.eql(u8, arg, "--init")) { | ||
| 34 | + initConfig(allocator); | ||
| 35 | + return; | ||
| 36 | + } else if (std.mem.eql(u8, arg, "--validate")) { | ||
| 37 | + validate_mode = true; | ||
| 38 | + } | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + if (validate_mode) { | ||
| 42 | + try validateConfig(allocator, config_path); | ||
| 43 | + return; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + std.debug.print("oxwm starting\n", .{}); | ||
| 47 | + | ||
| 48 | + var config = config_mod.Config.init(allocator); | ||
| 49 | + | ||
| 50 | + if (lua.init(&config)) { | ||
| 51 | + const loaded = if (std.fs.cwd().statFile(config_path)) |_| blk: { | ||
| 52 | + break :blk lua.loadFile(config_path); | ||
| 53 | + } else |_| blk: { | ||
| 54 | + initConfig(allocator); | ||
| 55 | + break :blk lua.loadConfig(); | ||
| 56 | + }; | ||
| 57 | + | ||
| 58 | + if (loaded) { | ||
| 59 | + std.debug.print("loaded config from {s}\n", .{config_path}); | ||
| 60 | + } else { | ||
| 61 | + std.debug.print("no config found, using defaults\n", .{}); | ||
| 62 | + config_mod.initializeDefaultConfig(&config); | ||
| 63 | + } | ||
| 64 | + } else { | ||
| 65 | + std.debug.print("failed to init lua, using defaults\n", .{}); | ||
| 66 | + config_mod.initializeDefaultConfig(&config); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + var wm = WindowManager.init(allocator, config, config_path) catch |err| { | ||
| 70 | + std.debug.print("failed to start window manager: {}\n", .{err}); | ||
| 71 | + return; | ||
| 72 | + }; | ||
| 73 | + defer wm.deinit(); | ||
| 74 | + | ||
| 75 | + std.debug.print("display opened: screen={d} root=0x{x}\n", .{ wm.display.screen, wm.display.root }); | ||
| 76 | + std.debug.print("successfully became window manager\n", .{}); | ||
| 77 | + std.debug.print("atoms initialized with EWMH support\n", .{}); | ||
| 78 | + | ||
| 79 | + wm.grabKeybinds(); | ||
| 80 | + wm.scanExistingWindows(window_manager.core.manage); | ||
| 81 | + | ||
| 82 | + try runAutostartCommands(&wm); | ||
| 83 | + std.debug.print("entering event loop\n", .{}); | ||
| 84 | + wm.run(handlers.handleEvent, window_manager.core.tickAnimations); | ||
| 85 | + | ||
| 86 | + lua.deinit(); | ||
| 87 | + std.debug.print("oxwm exiting\n", .{}); | ||
| 88 | +} | ||
| 89 | + | ||
| 12 | fn printHelp() void { | 90 | fn printHelp() void { |
| 13 | std.debug.print( | 91 | std.debug.print( |
| 14 | \\oxwm - A window manager | 92 | \\oxwm - A window manager |
| @@ -35,18 +113,6 @@ fn printHelp() void { | |||
| 35 | , .{}); | 113 | , .{}); |
| 36 | } | 114 | } |
| 37 | 115 | ||
| 38 | -fn getConfigPath(allocator: std.mem.Allocator) ![]u8 { | ||
| 39 | - const config_home = std.posix.getenv("XDG_CONFIG_HOME") orelse blk: { | ||
| 40 | - const home = std.posix.getenv("HOME") orelse return error.CouldNotGetHomeDir; | ||
| 41 | - break :blk try std.fs.path.join(allocator, &.{ home, ".config" }); | ||
| 42 | - }; | ||
| 43 | - // TODO: wtf is this shit | ||
| 44 | - defer if (std.posix.getenv("XDG_CONFIG_HOME") == null) allocator.free(config_home); | ||
| 45 | - | ||
| 46 | - const config_path = try std.fs.path.join(allocator, &.{ config_home, "oxwm", "config.lua" }); | ||
| 47 | - return config_path; | ||
| 48 | -} | ||
| 49 | - | ||
| 50 | fn initConfig(allocator: std.mem.Allocator) void { | 116 | fn initConfig(allocator: std.mem.Allocator) void { |
| 51 | const config_path = getConfigPath(allocator) catch return; | 117 | const config_path = getConfigPath(allocator) catch return; |
| 52 | defer allocator.free(config_path); | 118 | defer allocator.free(config_path); |
| @@ -83,6 +149,18 @@ fn initConfig(allocator: std.mem.Allocator) void { | |||
| 83 | std.debug.print("No compilation needed - changes take effect immediately!\n", .{}); | 149 | std.debug.print("No compilation needed - changes take effect immediately!\n", .{}); |
| 84 | } | 150 | } |
| 85 | 151 | ||
| 152 | +fn getConfigPath(allocator: std.mem.Allocator) ![]u8 { | ||
| 153 | + const config_home = std.posix.getenv("XDG_CONFIG_HOME") orelse blk: { | ||
| 154 | + const home = std.posix.getenv("HOME") orelse return error.CouldNotGetHomeDir; | ||
| 155 | + break :blk try std.fs.path.join(allocator, &.{ home, ".config" }); | ||
| 156 | + }; | ||
| 157 | + // TODO: wtf is this shit | ||
| 158 | + defer if (std.posix.getenv("XDG_CONFIG_HOME") == null) allocator.free(config_home); | ||
| 159 | + | ||
| 160 | + const config_path = try std.fs.path.join(allocator, &.{ config_home, "oxwm", "config.lua" }); | ||
| 161 | + return config_path; | ||
| 162 | +} | ||
| 163 | + | ||
| 86 | fn validateConfig(allocator: std.mem.Allocator, config_path: []const u8) !void { | 164 | fn validateConfig(allocator: std.mem.Allocator, config_path: []const u8) !void { |
| 87 | var config = config_mod.Config.init(allocator); | 165 | var config = config_mod.Config.init(allocator); |
| 88 | defer config.deinit(); | 166 | defer config.deinit(); |
| @@ -108,86 +186,6 @@ fn validateConfig(allocator: std.mem.Allocator, config_path: []const u8) !void { | |||
| 108 | } | 186 | } |
| 109 | } | 187 | } |
| 110 | 188 | ||
| 111 | -pub fn main() !void { | ||
| 112 | - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
| 113 | - const allocator = gpa.allocator(); | ||
| 114 | - defer _ = gpa.deinit(); | ||
| 115 | - | ||
| 116 | - const default_config_path = try getConfigPath(allocator); | ||
| 117 | - defer allocator.free(default_config_path); | ||
| 118 | - | ||
| 119 | - var config_path: []const u8 = default_config_path; | ||
| 120 | - var validate_mode: bool = false; | ||
| 121 | - var args = std.process.args(); | ||
| 122 | - _ = args.skip(); | ||
| 123 | - while (args.next()) |arg| { | ||
| 124 | - if (std.mem.eql(u8, arg, "-c") or std.mem.eql(u8, arg, "--config")) { | ||
| 125 | - if (args.next()) |path| config_path = path; | ||
| 126 | - } else if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) { | ||
| 127 | - printHelp(); | ||
| 128 | - return; | ||
| 129 | - } else if (std.mem.eql(u8, arg, "-v") or std.mem.eql(u8, arg, "--version")) { | ||
| 130 | - std.debug.print("{s}\n", .{build_options.version}); | ||
| 131 | - return; | ||
| 132 | - } else if (std.mem.eql(u8, arg, "--init")) { | ||
| 133 | - initConfig(allocator); | ||
| 134 | - return; | ||
| 135 | - } else if (std.mem.eql(u8, arg, "--validate")) { | ||
| 136 | - validate_mode = true; | ||
| 137 | - } | ||
| 138 | - } | ||
| 139 | - | ||
| 140 | - if (validate_mode) { | ||
| 141 | - try validateConfig(allocator, config_path); | ||
| 142 | - return; | ||
| 143 | - } | ||
| 144 | - | ||
| 145 | - std.debug.print("oxwm starting\n", .{}); | ||
| 146 | - | ||
| 147 | - var config = config_mod.Config.init(allocator); | ||
| 148 | - var config_path_global: ?[]const u8 = null; | ||
| 149 | - | ||
| 150 | - if (lua.init(&config)) { | ||
| 151 | - const loaded = if (std.fs.cwd().statFile(config_path)) |_| | ||
| 152 | - lua.loadFile(config_path) | ||
| 153 | - else |_| blk: { | ||
| 154 | - initConfig(allocator); | ||
| 155 | - break :blk lua.loadConfig(); | ||
| 156 | - }; | ||
| 157 | - | ||
| 158 | - if (loaded) { | ||
| 159 | - config_path_global = config_path; | ||
| 160 | - std.debug.print("loaded config from {s}\n", .{config_path}); | ||
| 161 | - } else { | ||
| 162 | - std.debug.print("no config found, using defaults\n", .{}); | ||
| 163 | - config_mod.initializeDefaultConfig(&config); | ||
| 164 | - } | ||
| 165 | - } else { | ||
| 166 | - std.debug.print("failed to init lua, using defaults\n", .{}); | ||
| 167 | - config_mod.initializeDefaultConfig(&config); | ||
| 168 | - } | ||
| 169 | - | ||
| 170 | - var wm = WindowManager.init(allocator, config, config_path_global) catch |err| { | ||
| 171 | - std.debug.print("failed to start window manager: {}\n", .{err}); | ||
| 172 | - return; | ||
| 173 | - }; | ||
| 174 | - defer wm.deinit(); | ||
| 175 | - | ||
| 176 | - std.debug.print("display opened: screen={d} root=0x{x}\n", .{ wm.display.screen, wm.display.root }); | ||
| 177 | - std.debug.print("successfully became window manager\n", .{}); | ||
| 178 | - std.debug.print("atoms initialized with EWMH support\n", .{}); | ||
| 179 | - | ||
| 180 | - wm.grabKeybinds(); | ||
| 181 | - wm.scanExistingWindows(window_manager.core.manage); | ||
| 182 | - | ||
| 183 | - try runAutostartCommands(&wm); | ||
| 184 | - std.debug.print("entering event loop\n", .{}); | ||
| 185 | - wm.run(handlers.handleEvent, window_manager.core.tickAnimations); | ||
| 186 | - | ||
| 187 | - lua.deinit(); | ||
| 188 | - std.debug.print("oxwm exiting\n", .{}); | ||
| 189 | -} | ||
| 190 | - | ||
| 191 | fn runAutostartCommands(wm: *WindowManager) !void { | 189 | fn runAutostartCommands(wm: *WindowManager) !void { |
| 192 | const commands = wm.config.autostart.items; | 190 | const commands = wm.config.autostart.items; |
| 193 | for (commands) |cmd| actions.spawnCommand(wm, cmd); | 191 | for (commands) |cmd| actions.spawnCommand(wm, cmd); |