tony / oxwm

Branch: abe5493
Switch branches/tags
master
emzywastaken treewide: change function names to camelCase b3ae92e 2026-02-23
198 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 fn printHelp() void {
13 std.debug.print(
14 \\oxwm - A window manager
15 \\
16 \\USAGE:
17 \\ oxwm [OPTIONS]
18 \\
19 \\OPTIONS:
20 \\ --init Create default config in ~/.config/oxwm/config.lua
21 \\ --config <PATH> Use custom config file
22 \\ --validate Validate config file without starting window manager
23 \\ --version Print version information
24 \\ --help Print this help message
25 \\
26 \\CONFIG:
27 \\ Location: ~/.config/oxwm/config.lua
28 \\ Edit the config file and use Mod+Shift+R to reload
29 \\ No compilation needed - instant hot-reload!
30 \\
31 \\FIRST RUN:
32 \\ Run 'oxwm --init' to create a config file
33 \\ Or just start oxwm and it will create one automatically
34 \\
35 , .{});
36 }
37
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 {
51 const config_path = getConfigPath(allocator) catch return;
52 defer allocator.free(config_path);
53
54 const template = @embedFile("templates/config.lua");
55
56 if (std.fs.path.dirname(config_path)) |dir_path| {
57 var root = std.fs.openDirAbsolute("/", .{}) catch |err| {
58 std.debug.print("error: could not open root directory: {}\n", .{err});
59 return;
60 };
61 defer root.close();
62
63 const relative_path = std.mem.trimLeft(u8, dir_path, "/");
64 root.makePath(relative_path) catch |err| {
65 std.debug.print("error: could not create config directory: {}\n", .{err});
66 return;
67 };
68 }
69
70 const file = std.fs.createFileAbsolute(config_path, .{}) catch |err| {
71 std.debug.print("error: could not create config file: {}\n", .{err});
72 return;
73 };
74 defer file.close();
75
76 _ = file.writeAll(template) catch |err| {
77 std.debug.print("error: could not write config file: {}\n", .{err});
78 return;
79 };
80
81 std.debug.print("Config created at {s}\n", .{config_path});
82 std.debug.print("Edit the file and reload with Mod+Shift+R\n", .{});
83 std.debug.print("No compilation needed - changes take effect immediately!\n", .{});
84 }
85
86 fn validateConfig(allocator: std.mem.Allocator, config_path: []const u8) !void {
87 var config = config_mod.Config.init(allocator);
88 defer config.deinit();
89
90 if (!lua.init(&config)) {
91 std.debug.print("error: failed to initialize lua\n", .{});
92 std.process.exit(1);
93 }
94 defer lua.deinit();
95
96 _ = std.fs.cwd().statFile(config_path) catch |err| {
97 std.debug.print("error: config file not found: {s}\n", .{config_path});
98 std.debug.print(" {}\n", .{err});
99 std.process.exit(1);
100 };
101
102 if (lua.loadFile(config_path)) {
103 std.debug.print("✓ config valid: {s}\n", .{config_path});
104 std.process.exit(0);
105 } else {
106 std.debug.print("✗ config validation failed\n", .{});
107 std.process.exit(1);
108 }
109 }
110
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 {
192 const commands = wm.config.autostart.items;
193 for (commands) |cmd| actions.spawnCommand(wm, cmd);
194 }
195
196 test {
197 _ = @import("x11/events.zig");
198 }