tony / oxwm

Branch: 4b35081
Switch branches/tags
master
tonybanters fixed validate function cddc305 2026-02-28
197 lines · 6.7 KB Raw Blame History
1 const std = @import("std");
2 const build_options = @import("build_options");
3
4 const actions = @import("wm/actions.zig");
5 const handlers = @import("wm/handlers.zig");
6 const window_manager = @import("wm/wm.zig");
7 const config_mod = @import("config/config.zig");
8 const lua = @import("config/lua.zig");
9
10 const WindowManager = window_manager.WindowManager;
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("v{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 if (args.next()) |path| config_path = path;
39 }
40 }
41
42 if (validate_mode) {
43 try validateConfig(allocator, config_path);
44 return;
45 }
46
47 std.debug.print("oxwm starting\n", .{});
48
49 var config = config_mod.Config.init(allocator);
50
51 if (lua.init(&config)) {
52 const loaded = if (std.fs.cwd().statFile(config_path)) |_| blk: {
53 break :blk lua.loadFile(config_path);
54 } else |_| blk: {
55 initConfig(allocator);
56 break :blk lua.loadConfig();
57 };
58
59 if (loaded) {
60 std.debug.print("loaded config from {s}\n", .{config_path});
61 } else {
62 std.debug.print("no config found, using defaults\n", .{});
63 config_mod.initializeDefaultConfig(&config);
64 }
65 } else {
66 std.debug.print("failed to init lua, using defaults\n", .{});
67 config_mod.initializeDefaultConfig(&config);
68 }
69
70 var wm = WindowManager.init(allocator, config, config_path) catch |err| {
71 std.debug.print("failed to start window manager: {}\n", .{err});
72 return;
73 };
74 defer wm.deinit();
75
76 std.debug.print("display opened: screen={d} root=0x{x}\n", .{ wm.display.screen, wm.display.root });
77 std.debug.print("successfully became window manager\n", .{});
78 std.debug.print("atoms initialized with EWMH support\n", .{});
79
80 wm.grabKeybinds();
81 wm.scanExistingWindows(window_manager.core.manage);
82
83 try runAutostartCommands(&wm);
84 std.debug.print("entering event loop\n", .{});
85 wm.run(handlers.handleEvent, window_manager.core.tickAnimations);
86
87 lua.deinit();
88 std.debug.print("oxwm exiting\n", .{});
89 }
90
91 fn printHelp() void {
92 std.debug.print(
93 \\oxwm - A window manager
94 \\
95 \\USAGE:
96 \\ oxwm [OPTIONS]
97 \\
98 \\OPTIONS:
99 \\ --init Create default config in ~/.config/oxwm/config.lua
100 \\ --config <PATH> Use custom config file
101 \\ --validate Validate config file without starting window manager
102 \\ --version Print version information
103 \\ --help Print this help message
104 \\
105 \\CONFIG:
106 \\ Location: ~/.config/oxwm/config.lua
107 \\ Edit the config file and use Mod+Shift+R to reload
108 \\ No compilation needed - instant hot-reload!
109 \\
110 \\FIRST RUN:
111 \\ Run 'oxwm --init' to create a config file
112 \\ Or just start oxwm and it will create one automatically
113 \\
114 , .{});
115 }
116
117 fn initConfig(allocator: std.mem.Allocator) void {
118 const config_path = getConfigPath(allocator) catch return;
119 defer allocator.free(config_path);
120
121 const template = @embedFile("templates/config.lua");
122
123 if (std.fs.path.dirname(config_path)) |dir_path| {
124 var root = std.fs.openDirAbsolute("/", .{}) catch |err| {
125 std.debug.print("error: could not open root directory: {}\n", .{err});
126 return;
127 };
128 defer root.close();
129
130 const relative_path = std.mem.trimLeft(u8, dir_path, "/");
131 root.makePath(relative_path) catch |err| {
132 std.debug.print("error: could not create config directory: {}\n", .{err});
133 return;
134 };
135 }
136
137 const file = std.fs.createFileAbsolute(config_path, .{}) catch |err| {
138 std.debug.print("error: could not create config file: {}\n", .{err});
139 return;
140 };
141 defer file.close();
142
143 _ = file.writeAll(template) catch |err| {
144 std.debug.print("error: could not write config file: {}\n", .{err});
145 return;
146 };
147
148 std.debug.print("Config created at {s}\n", .{config_path});
149 std.debug.print("Edit the file and reload with Mod+Shift+R\n", .{});
150 std.debug.print("No compilation needed - changes take effect immediately!\n", .{});
151 }
152
153 fn getConfigPath(allocator: std.mem.Allocator) ![]u8 {
154 const config_home = std.posix.getenv("XDG_CONFIG_HOME") orelse blk: {
155 const home = std.posix.getenv("HOME") orelse return error.CouldNotGetHomeDir;
156 break :blk try std.fs.path.join(allocator, &.{ home, ".config" });
157 };
158 // TODO: wtf is this shit
159 defer if (std.posix.getenv("XDG_CONFIG_HOME") == null) allocator.free(config_home);
160
161 const config_path = try std.fs.path.join(allocator, &.{ config_home, "oxwm", "config.lua" });
162 return config_path;
163 }
164
165 fn validateConfig(allocator: std.mem.Allocator, config_path: []const u8) !void {
166 var config = config_mod.Config.init(allocator);
167 defer config.deinit();
168
169 if (!lua.init(&config)) {
170 std.debug.print("error: failed to initialize lua\n", .{});
171 std.process.exit(1);
172 }
173 defer lua.deinit();
174
175 _ = std.fs.cwd().statFile(config_path) catch |err| {
176 std.debug.print("error: config file not found: {s}\n", .{config_path});
177 std.debug.print(" {}\n", .{err});
178 std.process.exit(1);
179 };
180
181 if (lua.loadFile(config_path)) {
182 std.debug.print("✓ config valid: {s}\n", .{config_path});
183 std.process.exit(0);
184 } else {
185 std.debug.print("✗ config validation failed\n", .{});
186 std.process.exit(1);
187 }
188 }
189
190 fn runAutostartCommands(wm: *WindowManager) !void {
191 const commands = wm.config.autostart.items;
192 for (commands) |cmd| actions.spawnCommand(wm, cmd);
193 }
194
195 test {
196 _ = @import("x11/events.zig");
197 }