tony / oxwm

Branch: 57531bf
Switch branches/tags
master
MaliCopter Attach Methods but no hot reload c95e5bb 2026-09-06
345 lines · 10.8 KB Raw Blame History
1 const std = @import("std");
2
3 pub const Action = enum {
4 spawn_terminal,
5 spawn,
6 kill_client,
7 quit,
8 reload_config,
9 restart,
10 show_keybinds,
11 focus_next,
12 focus_prev,
13 move_next,
14 move_prev,
15 resize_master,
16 inc_master,
17 dec_master,
18 toggle_floating,
19 toggle_fullscreen,
20 toggle_gaps,
21 toggle_bar,
22 cycle_layout,
23 set_layout,
24 set_layout_tiling,
25 set_layout_floating,
26 set_attach_method,
27 view_tag,
28 view_next_tag,
29 view_prev_tag,
30 view_next_nonempty_tag,
31 view_prev_nonempty_tag,
32 move_to_tag,
33 toggle_view_tag,
34 toggle_tag,
35 focus_monitor,
36 send_to_monitor,
37 scroll_left,
38 scroll_right,
39 };
40
41 pub const KeyPress = struct {
42 mod_mask: u32 = 0,
43 keysym: u64 = 0,
44 };
45
46 pub const Keybind = struct {
47 keys: [4]KeyPress = [_]KeyPress{.{}} ** 4,
48 key_count: u8 = 1,
49 action: Action,
50 int_arg: i32 = 0,
51 str_arg: ?[]const u8 = null,
52 };
53
54 pub const Rule = struct {
55 class: ?[]const u8,
56 instance: ?[]const u8,
57 title: ?[]const u8,
58 tags: u32,
59 is_floating: bool,
60 monitor: i32,
61 focus: bool,
62 };
63
64 pub const BlockType = enum {
65 static,
66 datetime,
67 ram,
68 shell,
69 battery,
70 cpu_temp,
71 systray,
72 };
73
74 pub const ClickTarget = enum {
75 client_win,
76 root_win,
77 tag_bar,
78 };
79
80 pub const MouseAction = enum {
81 move_mouse,
82 resize_mouse,
83 toggle_floating,
84 };
85
86 pub const Layouts = enum(u32) {
87 tiling,
88 monocle,
89 floating,
90 scrolling,
91 grid,
92 dwindle,
93
94 pub fn fromString(name: []const u8) ?Layouts {
95 if (std.meta.stringToEnum(Layouts, name)) |v| return v;
96 if (std.mem.eql(u8, name, "tile")) return .tiling;
97 if (std.mem.eql(u8, name, "normie")) return .floating;
98 if (std.mem.eql(u8, name, "float")) return .floating;
99 if (std.mem.eql(u8, name, "scroll")) return .scrolling;
100 if (std.mem.eql(u8, name, "fib")) return .dwindle;
101 if (std.mem.eql(u8, name, "fibonacci")) return .dwindle;
102 return null;
103 }
104 };
105
106 pub const AttachMethods = enum(u32) {
107 aside,
108 top,
109 bottom,
110 above,
111 below,
112
113 pub fn fromString(name: []const u8) ?AttachMethods {
114 if (std.meta.stringToEnum(AttachMethods, name)) |v| return v;
115 if (std.mem.eql(u8, name, "aside")) return .aside;
116 if (std.mem.eql(u8, name, "top")) return .top;
117 if (std.mem.eql(u8, name, "bottom")) return .bottom;
118 if (std.mem.eql(u8, name, "above")) return .above;
119 if (std.mem.eql(u8, name, "below")) return .below;
120 return null;
121 }
122 };
123
124 pub const MouseButton = struct {
125 click: ClickTarget,
126 mod_mask: u32,
127 button: u32,
128 action: MouseAction,
129 };
130
131 pub const FloatingPosition = enum {
132 top_left,
133 top_center,
134 top_right,
135 center_left,
136 center,
137 center_right,
138 bottom_left,
139 bottom_center,
140 bottom_right,
141
142 pub fn fromString(name: []const u8) ?FloatingPosition {
143 const map = .{
144 .{ "top-left", .top_left },
145 .{ "top_left", .top_left },
146 .{ "top-center", .top_center },
147 .{ "top_center", .top_center },
148 .{ "top-middle", .top_center },
149 .{ "top-right", .top_right },
150 .{ "top_right", .top_right },
151 .{ "center-left", .center_left },
152 .{ "center_left", .center_left },
153 .{ "center", .center },
154 .{ "center-right", .center_right },
155 .{ "center_right", .center_right },
156 .{ "bottom-left", .bottom_left },
157 .{ "bottom_left", .bottom_left },
158 .{ "bottom-center", .bottom_center },
159 .{ "bottom_center", .bottom_center },
160 .{ "bottom-middle", .bottom_center },
161 .{ "bottom-right", .bottom_right },
162 .{ "bottom_right", .bottom_right },
163 };
164 inline for (map) |entry| {
165 if (std.mem.eql(u8, name, entry[0])) return entry[1];
166 }
167 return null;
168 }
169 };
170
171 pub const ClickAction = struct {
172 command: []const u8,
173 floating: bool = false,
174 bypass_rules: bool = false,
175 };
176
177 pub const Block = struct {
178 block_type: BlockType,
179 format: []const u8,
180 command: ?[]const u8 = null,
181 interval: u32,
182 color: u32,
183 underline: bool = true,
184 datetime_format: ?[]const u8 = null,
185 format_charging: ?[]const u8 = null,
186 format_discharging: ?[]const u8 = null,
187 format_full: ?[]const u8 = null,
188 battery_name: ?[]const u8 = null,
189 thermal_zone: ?[]const u8 = null,
190 click: ?ClickAction = null,
191 };
192
193 pub const ColorScheme = struct {
194 foreground: u32 = 0xbbbbbb,
195 background: u32 = 0x1a1b26,
196 border: u32 = 0x444444,
197 };
198
199 pub const Config = struct {
200 allocator: std.mem.Allocator,
201 string_arena: std.heap.ArenaAllocator,
202
203 terminal: []const u8 = "st",
204 font: []const u8 = "monospace:size=10",
205 bar_position: []const u8 = "top",
206 tags: [12][]const u8 = .{ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" },
207 tag_count: u32 = 9,
208 layout: []const u8 = "tiling",
209 attach_method: []const u8 = "aside",
210 tag_layouts: [12]?[]const u8 = .{null} ** 12,
211
212 border_width: i32 = 2,
213 border_focused: u32 = 0x6dade3,
214 border_unfocused: u32 = 0x444444,
215
216 gaps_enabled: bool = true,
217 smartgaps_enabled: bool = false,
218 gap_inner_h: i32 = 5,
219 gap_inner_v: i32 = 5,
220 gap_outer_h: i32 = 5,
221 gap_outer_v: i32 = 5,
222
223 modkey: u32 = (1 << 6),
224 auto_tile: bool = false,
225 tag_back_and_forth: bool = false,
226 hide_vacant_tags: bool = false,
227 floating_position: FloatingPosition = .center,
228 tiled_resize_mode: bool = false,
229 warp_cursor_to_monitor: bool = false,
230 warp_cursor_on_send: bool = false,
231
232 layout_tile_symbol: []const u8 = "[]=",
233 layout_monocle_symbol: []const u8 = "[M]",
234 layout_floating_symbol: []const u8 = "><>",
235 layout_scrolling_symbol: []const u8 = "[S]",
236 layout_grid_symbol: []const u8 = "[#]",
237 layout_dwindle_symbol: []const u8 = "[\\]",
238
239 scheme_normal: ColorScheme = .{ .foreground = 0xbbbbbb, .background = 0x1a1b26, .border = 0x444444 },
240 scheme_selected: ColorScheme = .{ .foreground = 0x0db9d7, .background = 0x1a1b26, .border = 0xad8ee6 },
241 scheme_occupied: ColorScheme = .{ .foreground = 0x0db9d7, .background = 0x1a1b26, .border = 0x0db9d7 },
242 scheme_urgent: ColorScheme = .{ .foreground = 0xf7768e, .background = 0x1a1b26, .border = 0xf7768e },
243
244 keybinds: std.ArrayListUnmanaged(Keybind) = .empty,
245 rules: std.ArrayListUnmanaged(Rule) = .empty,
246 blocks: std.ArrayListUnmanaged(Block) = .empty,
247 buttons: std.ArrayListUnmanaged(MouseButton) = .empty,
248 autostart: std.ArrayListUnmanaged([]const u8) = .empty,
249
250 pub fn init(allocator: std.mem.Allocator) Config {
251 return Config{
252 .allocator = allocator,
253 .string_arena = std.heap.ArenaAllocator.init(allocator),
254 };
255 }
256
257 pub fn deinit(self: *Config) void {
258 self.string_arena.deinit();
259 self.keybinds.deinit(self.allocator);
260 self.rules.deinit(self.allocator);
261 self.blocks.deinit(self.allocator);
262 self.buttons.deinit(self.allocator);
263 self.autostart.deinit(self.allocator);
264 }
265
266 pub fn addKeybind(self: *Config, keybind: Keybind) !void {
267 try self.keybinds.append(self.allocator, keybind);
268 }
269
270 pub fn addRule(self: *Config, rule: Rule) !void {
271 try self.rules.append(self.allocator, rule);
272 }
273
274 pub fn addBlock(self: *Config, block: Block) !void {
275 try self.blocks.append(self.allocator, block);
276 }
277
278 pub fn addButton(self: *Config, button: MouseButton) !void {
279 try self.buttons.append(self.allocator, button);
280 }
281
282 pub fn addAutostart(self: *Config, cmd: []const u8) !void {
283 try self.autostart.append(self.allocator, cmd);
284 }
285 };
286
287 pub fn initializeDefaultConfig(cfg: *Config) void {
288 const mod_key: u32 = 1 << 6;
289 const shift_key: u32 = 1 << 0;
290 const control_key: u32 = 1 << 2;
291
292 cfg.addKeybind(makeKeybind(mod_key, 0xff0d, .spawn_terminal)) catch {};
293 cfg.addKeybind(makeKeybindStr(mod_key, 'd', .spawn, "rofi -show drun")) catch {};
294 cfg.addKeybind(makeKeybindStr(mod_key, 's', .spawn, "maim -s | xclip -selection clipboard -t image/png")) catch {};
295 cfg.addKeybind(makeKeybind(mod_key, 'q', .kill_client)) catch {};
296 cfg.addKeybind(makeKeybind(mod_key | shift_key, 'q', .quit)) catch {};
297 cfg.addKeybind(makeKeybind(mod_key | shift_key, 'r', .reload_config)) catch {};
298 cfg.addKeybind(makeKeybind(mod_key, 'j', .focus_next)) catch {};
299 cfg.addKeybind(makeKeybind(mod_key, 'k', .focus_prev)) catch {};
300 cfg.addKeybind(makeKeybind(mod_key | shift_key, 'j', .move_next)) catch {};
301 cfg.addKeybind(makeKeybind(mod_key | shift_key, 'k', .move_prev)) catch {};
302 cfg.addKeybind(makeKeybindInt(mod_key, 'h', .resize_master, -50)) catch {};
303 cfg.addKeybind(makeKeybindInt(mod_key, 'l', .resize_master, 50)) catch {};
304 cfg.addKeybind(makeKeybind(mod_key, 'i', .inc_master)) catch {};
305 cfg.addKeybind(makeKeybind(mod_key, 'p', .dec_master)) catch {};
306 cfg.addKeybind(makeKeybind(mod_key, 'a', .toggle_gaps)) catch {};
307 cfg.addKeybind(makeKeybind(mod_key, 'f', .toggle_fullscreen)) catch {};
308 cfg.addKeybind(makeKeybind(mod_key, 0x0020, .toggle_floating)) catch {};
309 cfg.addKeybind(makeKeybind(mod_key, 'n', .cycle_layout)) catch {};
310 cfg.addKeybind(makeKeybindInt(mod_key, 0x002c, .focus_monitor, -1)) catch {};
311 cfg.addKeybind(makeKeybindInt(mod_key, 0x002e, .focus_monitor, 1)) catch {};
312 cfg.addKeybind(makeKeybindInt(mod_key | shift_key, 0x002c, .send_to_monitor, -1)) catch {};
313 cfg.addKeybind(makeKeybindInt(mod_key | shift_key, 0x002e, .send_to_monitor, 1)) catch {};
314
315 var tag_index: i32 = 0;
316 while (tag_index < 9) : (tag_index += 1) {
317 const keysym: u64 = @as(u64, '1') + @as(u64, @intCast(tag_index));
318 cfg.addKeybind(makeKeybindInt(mod_key, keysym, .view_tag, tag_index)) catch {};
319 cfg.addKeybind(makeKeybindInt(mod_key | shift_key, keysym, .move_to_tag, tag_index)) catch {};
320 cfg.addKeybind(makeKeybindInt(mod_key | control_key, keysym, .toggle_view_tag, tag_index)) catch {};
321 cfg.addKeybind(makeKeybindInt(mod_key | control_key | shift_key, keysym, .toggle_tag, tag_index)) catch {};
322 }
323
324 cfg.addButton(.{ .click = .client_win, .mod_mask = mod_key, .button = 1, .action = .move_mouse }) catch {};
325 cfg.addButton(.{ .click = .client_win, .mod_mask = mod_key, .button = 3, .action = .resize_mouse }) catch {};
326 }
327
328 fn makeKeybind(mod: u32, key: u64, action: Action) Keybind {
329 var kb: Keybind = .{ .action = action };
330 kb.keys[0] = .{ .mod_mask = mod, .keysym = key };
331 kb.key_count = 1;
332 return kb;
333 }
334
335 fn makeKeybindInt(mod: u32, key: u64, action: Action, int_arg: i32) Keybind {
336 var kb = makeKeybind(mod, key, action);
337 kb.int_arg = int_arg;
338 return kb;
339 }
340
341 fn makeKeybindStr(mod: u32, key: u64, action: Action, str_arg: []const u8) Keybind {
342 var kb = makeKeybind(mod, key, action);
343 kb.str_arg = str_arg;
344 return kb;
345 }