tony / oxwm

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