tony / oxwm

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