tony / oxwm

Branch: 211ed13
Switch branches/tags
master
emzywastaken reorder main 211ed13 2026-02-23
196 lines · 6.6 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("{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
90 fn printHelp() void {
91 std.debug.print(
92 \\oxwm - A window manager
93 \\
94 \\USAGE:
95 \\ oxwm [OPTIONS]
96 \\
97 \\OPTIONS:
98 \\ --init Create default config in ~/.config/oxwm/config.lua
99 \\ --config <PATH> Use custom config file
100 \\ --validate Validate config file without starting window manager
101 \\ --version Print version information
102 \\ --help Print this help message
103 \\
104 \\CONFIG:
105 \\ Location: ~/.config/oxwm/config.lua
106 \\ Edit the config file and use Mod+Shift+R to reload
107 \\ No compilation needed - instant hot-reload!
108 \\
109 \\FIRST RUN:
110 \\ Run 'oxwm --init' to create a config file
111 \\ Or just start oxwm and it will create one automatically
112 \\
113 , .{});
114 }
115
116 fn initConfig(allocator: std.mem.Allocator) void {
117 const config_path = getConfigPath(allocator) catch return;
118 defer allocator.free(config_path);
119
120 const template = @embedFile("templates/config.lua");
121
122 if (std.fs.path.dirname(config_path)) |dir_path| {
123 var root = std.fs.openDirAbsolute("/", .{}) catch |err| {
124 std.debug.print("error: could not open root directory: {}\n", .{err});
125 return;
126 };
127 defer root.close();
128
129 const relative_path = std.mem.trimLeft(u8, dir_path, "/");
130 root.makePath(relative_path) catch |err| {
131 std.debug.print("error: could not create config directory: {}\n", .{err});
132 return;
133 };
134 }
135
136 const file = std.fs.createFileAbsolute(config_path, .{}) catch |err| {
137 std.debug.print("error: could not create config file: {}\n", .{err});
138 return;
139 };
140 defer file.close();
141
142 _ = file.writeAll(template) catch |err| {
143 std.debug.print("error: could not write config file: {}\n", .{err});
144 return;
145 };
146
147 std.debug.print("Config created at {s}\n", .{config_path});
148 std.debug.print("Edit the file and reload with Mod+Shift+R\n", .{});
149 std.debug.print("No compilation needed - changes take effect immediately!\n", .{});
150 }
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
164 fn validateConfig(allocator: std.mem.Allocator, config_path: []const u8) !void {
165 var config = config_mod.Config.init(allocator);
166 defer config.deinit();
167
168 if (!lua.init(&config)) {
169 std.debug.print("error: failed to initialize lua\n", .{});
170 std.process.exit(1);
171 }
172 defer lua.deinit();
173
174 _ = std.fs.cwd().statFile(config_path) catch |err| {
175 std.debug.print("error: config file not found: {s}\n", .{config_path});
176 std.debug.print(" {}\n", .{err});
177 std.process.exit(1);
178 };
179
180 if (lua.loadFile(config_path)) {
181 std.debug.print("✓ config valid: {s}\n", .{config_path});
182 std.process.exit(0);
183 } else {
184 std.debug.print("✗ config validation failed\n", .{});
185 std.process.exit(1);
186 }
187 }
188
189 fn runAutostartCommands(wm: *WindowManager) !void {
190 const commands = wm.config.autostart.items;
191 for (commands) |cmd| actions.spawnCommand(wm, cmd);
192 }
193
194 test {
195 _ = @import("x11/events.zig");
196 }