tony / oxwm

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