tony / oxwm

Branch: master
Switch branches/tags
master
tonybanters updated for scrolling with gestures, and size of windows in scrolling layout. b419fcd 2026-09-20
192 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(init: std.process.Init) !void {
13 const allocator = init.gpa;
14 const env = init.environ_map;
15
16 const default_config_path = try getConfigPath(allocator, env);
17 defer allocator.free(default_config_path);
18
19 var config_path: []const u8 = default_config_path;
20 var validate_mode: bool = false;
21 var args = init.minimal.args.iterate();
22 _ = args.skip();
23 while (args.next()) |arg| {
24 if (std.mem.eql(u8, arg, "-c") or std.mem.eql(u8, arg, "--config")) {
25 if (args.next()) |path| config_path = path;
26 } else if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) {
27 printHelp();
28 return;
29 } else if (std.mem.eql(u8, arg, "-v") or std.mem.eql(u8, arg, "--version")) {
30 std.debug.print("v{s}\n", .{build_options.version});
31 return;
32 } else if (std.mem.eql(u8, arg, "--init")) {
33 initConfig(allocator, init.io, env);
34 return;
35 } else if (std.mem.eql(u8, arg, "--validate")) {
36 validate_mode = true;
37 if (args.next()) |path| config_path = path;
38 }
39 }
40
41 if (validate_mode) {
42 try validateConfig(allocator, init.io, 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.Io.Dir.cwd().statFile(init.io, config_path, .{})) |_| blk: {
52 break :blk lua.loadFile(config_path);
53 } else |_| blk: {
54 initConfig(allocator, init.io, env);
55 break :blk lua.loadConfig(env);
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, env, init.io) 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, window_manager.core.handleGesture);
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, io: std.Io, env: *std.process.Environ.Map) void {
117 const config_path = getConfigPath(allocator, env) 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.Io.Dir.openDirAbsolute(io, "/", .{}) catch |err| {
124 std.debug.print("error: could not open root directory: {}\n", .{err});
125 return;
126 };
127 defer root.close(io);
128
129 const relative_path = std.mem.trimStart(u8, dir_path, "/");
130 root.createDirPath(io, 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.Io.Dir.createFileAbsolute(io, config_path, .{}) catch |err| {
137 std.debug.print("error: could not create config file: {}\n", .{err});
138 return;
139 };
140 defer file.close(io);
141
142 file.writeStreamingAll(io, 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, env: *std.process.Environ.Map) ![]u8 {
153 if (env.get("XDG_CONFIG_HOME")) |xdg| {
154 return std.fs.path.join(allocator, &.{ xdg, "oxwm", "config.lua" });
155 }
156 const home = env.get("HOME") orelse return error.CouldNotGetHomeDir;
157 return std.fs.path.join(allocator, &.{ home, ".config", "oxwm", "config.lua" });
158 }
159
160 fn validateConfig(allocator: std.mem.Allocator, io: std.Io, config_path: []const u8) !void {
161 var config = config_mod.Config.init(allocator);
162 defer config.deinit();
163
164 if (!lua.init(&config)) {
165 std.debug.print("error: failed to initialize lua\n", .{});
166 std.process.exit(1);
167 }
168 defer lua.deinit();
169
170 _ = std.Io.Dir.cwd().statFile(io, config_path, .{}) catch |err| {
171 std.debug.print("error: config file not found: {s}\n", .{config_path});
172 std.debug.print(" {}\n", .{err});
173 std.process.exit(1);
174 };
175
176 if (lua.loadFile(config_path)) {
177 std.debug.print("✓ config valid: {s}\n", .{config_path});
178 std.process.exit(0);
179 } else {
180 std.debug.print("✗ config validation failed\n", .{});
181 std.process.exit(1);
182 }
183 }
184
185 fn runAutostartCommands(wm: *WindowManager) !void {
186 const commands = wm.config.autostart.items;
187 for (commands) |cmd| actions.spawnCommand(wm, cmd);
188 }
189
190 test {
191 _ = @import("x11/events.zig");
192 }